microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
operators/vision/encode_image.hpp
66lines · 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 | |
| 11 | namespace ort_extensions { |
| 12 | struct KernelEncodeImage : BaseKernel { |
| 13 | KernelEncodeImage(const OrtApi& api, const OrtKernelInfo& info) : BaseKernel{api, info} { |
| 14 | OrtW::CustomOpApi op_api{api}; |
| 15 | std::string format = op_api.KernelInfoGetAttribute<std::string>(&info, "format"); |
| 16 | if (format != "jpg" && format != "png") { |
| 17 | ORTX_CXX_API_THROW("[EncodeImage] 'format' attribute value must be 'jpg' or 'png'.", ORT_RUNTIME_EXCEPTION); |
| 18 | } |
| 19 | |
| 20 | extension_ = std::string(".") + format; |
| 21 | } |
| 22 | |
| 23 | void Compute(OrtKernelContext* context); |
| 24 | |
| 25 | private: |
| 26 | std::string extension_; |
| 27 | }; |
| 28 | |
| 29 | /// <summary> |
| 30 | /// EncodeImage |
| 31 | /// |
| 32 | /// Converts rank 3 BGR input with channels last ordering to the requested file type. |
| 33 | /// Default is 'jpg' |
| 34 | /// </summary> |
| 35 | struct CustomOpEncodeImage : OrtW::CustomOpBase<CustomOpEncodeImage, KernelEncodeImage> { |
| 36 | const char* GetName() const { |
| 37 | return "EncodeImage"; |
| 38 | } |
| 39 | |
| 40 | size_t GetInputTypeCount() const { |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | ONNXTensorElementDataType GetInputType(size_t index) const { |
| 45 | switch (index) { |
| 46 | case 0: |
| 47 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8; |
| 48 | default: |
| 49 | ORTX_CXX_API_THROW(MakeString("Invalid input index ", index), ORT_INVALID_ARGUMENT); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | size_t GetOutputTypeCount() const { |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | ONNXTensorElementDataType GetOutputType(size_t index) const { |
| 58 | switch (index) { |
| 59 | case 0: |
| 60 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8; |
| 61 | default: |
| 62 | ORTX_CXX_API_THROW(MakeString("Invalid output index ", index), ORT_INVALID_ARGUMENT); |
| 63 | } |
| 64 | } |
| 65 | }; |
| 66 | } // namespace ort_extensions |
| 67 | |