Blame view

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