1 | // This file is part of OpenCV project. |
2 | // It is subject to the license terms in the LICENSE file found in the top-level directory |
3 | // of this distribution and at http://opencv.org/license.html. |
4 | // |
5 | // Copyright (C) 2020, Stefan Brüns <stefan.bruens@rwth-aachen.de> |
6 | |
7 | #ifndef _GRFMT_OPENJPEG_H_ |
8 | #define _GRFMT_OPENJPEG_H_ |
9 | |
10 | #ifdef HAVE_OPENJPEG |
11 | |
12 | #include "grfmt_base.hpp" |
13 | #include <openjpeg.h> |
14 | |
15 | namespace cv { |
16 | namespace detail { |
17 | struct OpjStreamDeleter |
18 | { |
19 | void operator()(opj_stream_t* stream) const |
20 | { |
21 | opj_stream_destroy(p_stream: stream); |
22 | } |
23 | }; |
24 | |
25 | struct OpjCodecDeleter |
26 | { |
27 | void operator()(opj_codec_t* codec) const |
28 | { |
29 | opj_destroy_codec(p_codec: codec); |
30 | } |
31 | }; |
32 | |
33 | struct OpjImageDeleter |
34 | { |
35 | void operator()(opj_image_t* image) const |
36 | { |
37 | opj_image_destroy(image); |
38 | } |
39 | }; |
40 | |
41 | struct OpjMemoryBuffer { |
42 | OPJ_BYTE* pos{nullptr}; |
43 | OPJ_BYTE* begin{nullptr}; |
44 | OPJ_SIZE_T length{0}; |
45 | |
46 | OpjMemoryBuffer() = default; |
47 | |
48 | explicit OpjMemoryBuffer(cv::Mat& mat) |
49 | : pos{ mat.ptr() }, begin{ mat.ptr() }, length{ mat.rows * mat.cols * mat.elemSize() } |
50 | { |
51 | } |
52 | |
53 | OPJ_SIZE_T availableBytes() const CV_NOEXCEPT { |
54 | return begin + length - pos; |
55 | } |
56 | }; |
57 | |
58 | using StreamPtr = std::unique_ptr<opj_stream_t, detail::OpjStreamDeleter>; |
59 | using CodecPtr = std::unique_ptr<opj_codec_t, detail::OpjCodecDeleter>; |
60 | using ImagePtr = std::unique_ptr<opj_image_t, detail::OpjImageDeleter>; |
61 | |
62 | class Jpeg2KOpjDecoderBase : public BaseImageDecoder |
63 | { |
64 | public: |
65 | Jpeg2KOpjDecoderBase(OPJ_CODEC_FORMAT format); |
66 | |
67 | bool readData( Mat& img ) CV_OVERRIDE; |
68 | bool () CV_OVERRIDE; |
69 | |
70 | private: |
71 | detail::StreamPtr stream_{nullptr}; |
72 | detail::CodecPtr codec_{nullptr}; |
73 | detail::ImagePtr image_{nullptr}; |
74 | |
75 | detail::OpjMemoryBuffer opjBuf_; |
76 | |
77 | OPJ_UINT32 m_maxPrec = 0; |
78 | OPJ_CODEC_FORMAT format_; |
79 | }; |
80 | |
81 | } // namespace detail |
82 | |
83 | class Jpeg2KJP2OpjDecoder CV_FINAL : public detail::Jpeg2KOpjDecoderBase { |
84 | public: |
85 | Jpeg2KJP2OpjDecoder(); |
86 | |
87 | ImageDecoder newDecoder() const CV_OVERRIDE; |
88 | }; |
89 | |
90 | class Jpeg2KJ2KOpjDecoder CV_FINAL : public detail::Jpeg2KOpjDecoderBase { |
91 | public: |
92 | Jpeg2KJ2KOpjDecoder(); |
93 | |
94 | ImageDecoder newDecoder() const CV_OVERRIDE; |
95 | }; |
96 | |
97 | class Jpeg2KOpjEncoder CV_FINAL : public BaseImageEncoder |
98 | { |
99 | public: |
100 | Jpeg2KOpjEncoder(); |
101 | ~Jpeg2KOpjEncoder() CV_OVERRIDE = default; |
102 | |
103 | bool isFormatSupported( int depth ) const CV_OVERRIDE; |
104 | bool write( const Mat& img, const std::vector<int>& params ) CV_OVERRIDE; |
105 | ImageEncoder newEncoder() const CV_OVERRIDE; |
106 | }; |
107 | |
108 | } //namespace cv |
109 | |
110 | #endif |
111 | |
112 | #endif/*_GRFMT_OPENJPEG_H_*/ |
113 | |