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
15namespace cv {
16namespace detail {
17struct OpjStreamDeleter
18{
19 void operator()(opj_stream_t* stream) const
20 {
21 opj_stream_destroy(p_stream: stream);
22 }
23};
24
25struct OpjCodecDeleter
26{
27 void operator()(opj_codec_t* codec) const
28 {
29 opj_destroy_codec(p_codec: codec);
30 }
31};
32
33struct OpjImageDeleter
34{
35 void operator()(opj_image_t* image) const
36 {
37 opj_image_destroy(image);
38 }
39};
40
41struct 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
58using StreamPtr = std::unique_ptr<opj_stream_t, detail::OpjStreamDeleter>;
59using CodecPtr = std::unique_ptr<opj_codec_t, detail::OpjCodecDeleter>;
60using ImagePtr = std::unique_ptr<opj_image_t, detail::OpjImageDeleter>;
61
62class Jpeg2KOpjDecoderBase : public BaseImageDecoder
63{
64public:
65 Jpeg2KOpjDecoderBase(OPJ_CODEC_FORMAT format);
66
67 bool readData( Mat& img ) CV_OVERRIDE;
68 bool readHeader() CV_OVERRIDE;
69
70private:
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
83class Jpeg2KJP2OpjDecoder CV_FINAL : public detail::Jpeg2KOpjDecoderBase {
84public:
85 Jpeg2KJP2OpjDecoder();
86
87 ImageDecoder newDecoder() const CV_OVERRIDE;
88};
89
90class Jpeg2KJ2KOpjDecoder CV_FINAL : public detail::Jpeg2KOpjDecoderBase {
91public:
92 Jpeg2KJ2KOpjDecoder();
93
94 ImageDecoder newDecoder() const CV_OVERRIDE;
95};
96
97class Jpeg2KOpjEncoder CV_FINAL : public BaseImageEncoder
98{
99public:
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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of opencv/modules/imgcodecs/src/grfmt_jpeg2000_openjpeg.hpp