microsoft/onnxruntime-extensions

Public

mirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
13d9e27ccd8a0de9a1225756fbf6860a1931484f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

operators/vision/encode_image.hpp

71lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include "ocos.h"
7#include "string_utils.h"
8
9#include <cstdint>
10
11namespace ort_extensions {
12struct KernelEncodeImage : BaseKernel {
13 KernelEncodeImage(const OrtApi& api, const std::string& format)
14 : BaseKernel{api},
15 extension_{std::string(".") + format} {
16 }
17
18 void Compute(OrtKernelContext* context);
19
20 private:
21 const std::string extension_;
22};
23
24/// <summary>
25/// EncodeImage
26///
27/// Converts rank 3 BGR input with channels last ordering to the requested file type.
28/// Default is 'jpg'
29/// </summary>
30struct CustomOpEncodeImage : OrtW::CustomOpBase<CustomOpEncodeImage, KernelEncodeImage> {
31 void* CreateKernel(const OrtApi& api, const OrtKernelInfo* info) const {
32 OrtW::CustomOpApi op_api{api};
33 std::string format = op_api.KernelInfoGetAttribute<std::string>(info, "format");
34 if (format != "jpg" && format != "png") {
35 ORTX_CXX_API_THROW("[EncodeImage] 'format' attribute value must be 'jpg' or 'png'.", ORT_RUNTIME_EXCEPTION);
36 }
37
38 return new KernelEncodeImage(api, format);
39 }
40
41 const char* GetName() const {
42 return "EncodeImage";
43 }
44
45 size_t GetInputTypeCount() const {
46 return 1;
47 }
48
49 ONNXTensorElementDataType GetInputType(size_t index) const {
50 switch (index) {
51 case 0:
52 return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8;
53 default:
54 ORTX_CXX_API_THROW(MakeString("Invalid input index ", index), ORT_INVALID_ARGUMENT);
55 }
56 }
57
58 size_t GetOutputTypeCount() const {
59 return 1;
60 }
61
62 ONNXTensorElementDataType GetOutputType(size_t index) const {
63 switch (index) {
64 case 0:
65 return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8;
66 default:
67 ORTX_CXX_API_THROW(MakeString("Invalid output index ", index), ORT_INVALID_ARGUMENT);
68 }
69 }
70};
71} // namespace ort_extensions
72