microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
skottmckay/BuildInfra_AndTestImageLibs

Branches

Tags

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

Clone

HTTPS

Download ZIP

operators/vision/impl/png_encoder_decoder.hpp

103lines · modecode

1// Originally from https://github.com/opencv/opencv/tree/4.x/modules/imgcodecs/src
2// Modified to remove opencv dependencies
3
4/*M///////////////////////////////////////////////////////////////////////////////////////
5//
6// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
7//
8// By downloading, copying, installing or using the software you agree to this license.
9// If you do not agree to this license, do not download, install,
10// copy or use the software.
11//
12//
13// License Agreement
14// For Open Source Computer Vision Library
15//
16// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
17// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
18// Third party copyrights are property of their respective owners.
19//
20// Redistribution and use in source and binary forms, with or without modification,
21// are permitted provided that the following conditions are met:
22//
23// * Redistribution's of source code must retain the above copyright notice,
24// this list of conditions and the following disclaimer.
25//
26// * Redistribution's in binary form must reproduce the above copyright notice,
27// this list of conditions and the following disclaimer in the documentation
28// and/or other materials provided with the distribution.
29//
30// * The name of the copyright holders may not be used to endorse or promote products
31// derived from this software without specific prior written permission.
32//
33// This software is provided by the copyright holders and contributors "as is" and
34// any express or implied warranties, including, but not limited to, the implied
35// warranties of merchantability and fitness for a particular purpose are disclaimed.
36// In no event shall the Intel Corporation or contributors be liable for any direct,
37// indirect, incidental, special, exemplary, or consequential damages
38// (including, but not limited to, procurement of substitute goods or services;
39// loss of use, data, or profits; or business interruption) however caused
40// and on any theory of liability, whether in contract, strict liability,
41// or tort (including negligence or otherwise) arising in any way out of
42// the use of this software, even if advised of the possibility of such damage.
43//
44//M*/
45
46#pragma once
47
48#include "vision/impl/image_encoder_decoder.hpp"
49
50#include <cassert>
51#include <vector>
52
53namespace ort_extensions {
54
55class PngDecoder : public BaseImageDecoder {
56 public:
57 static bool IsPng(const uint8_t* bytes, uint64_t num_bytes);
58
59 PngDecoder(const uint8_t* bytes, uint64_t num_bytes)
60 : BaseImageDecoder{bytes, num_bytes} {
61 ReadHeader();
62 }
63
64 virtual ~PngDecoder();
65
66 // ImageDecoder newDecoder() const CV_OVERRIDE;
67
68 private:
69 static void ReadDataFromBuf(void* png_ptr, uint8_t* dst, size_t size);
70
71 bool ReadHeader();
72 bool ReadData();
73
74 bool DecodeImpl(uint8_t* output, uint64_t out_bytes) override;
75
76 uint64_t cur_offset_{0}; // current read offset from bytes_
77
78 int bit_depth_{0};
79 int color_type_{0};
80
81 // TODO: These are opaque assumably to keep png.h from being included here. not sure it's worth it.
82 // Created in ReadHeder. Freed in dtor.
83 void* png_ptr_{nullptr}; // pointer to decompression structure
84 void* info_ptr_{nullptr}; // pointer to image information structure
85 void* end_info_{nullptr}; // pointer to one more image information structure
86};
87
88class PngEncoder : public BaseImageEncoder {
89 public:
90 PngEncoder(const uint8_t* bytes, const std::vector<int64_t>& shape)
91 : BaseImageEncoder(bytes, shape) {
92 }
93
94 private:
95 bool EncodeImpl() override;
96
97 static void WriteDataToBuf(void* png_ptr, uint8_t* src, size_t size);
98 static void FlushBuf(void* png_ptr);
99
100 uint64_t cur_offset_{0}; // current read offset from bytes_
101};
102
103} // namespace ort_extensions
104