SlideIO 2.0.0
Open-source library for reading of medical images
Loading...
Searching...
No Matches
jp2kcodec.hpp
1// This file is part of slideio 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://slideio.com/license.html.
4#pragma once
5#include <cstring>
6#include <openjpeg.h>
7
8
9class wopj_cparameters : public opj_cparameters_t
10{
11public:
12 wopj_cparameters() {
13 opj_cparameters_t* str = (opj_cparameters_t*)this;
14 memset(str, 0, sizeof(opj_cparameters_t));
15 opj_set_default_encoder_parameters(this);
16 }
17 ~wopj_cparameters() {
18 if (cp_comment) {
19 free(cp_comment);
20 }
21 if (cp_matrice) {
22 free(cp_matrice);
23 }
24 }
25};
26
27class CodecPtr {
28public:
29 CodecPtr(opj_codec_t* codec = nullptr) : m_codec(codec) {}
30
31 CodecPtr(const CodecPtr& other) {
32 m_codec = other.m_codec;
33 const_cast<CodecPtr&>(other).m_codec = nullptr;
34 }
35
36 CodecPtr& operator=(const CodecPtr& other) {
37 if (this != &other) {
38 if (m_codec) {
39 opj_destroy_codec(m_codec);
40 }
41 m_codec = other.m_codec;
42 const_cast<CodecPtr&>(other).m_codec = nullptr;
43 }
44 return *this;
45 }
46
47 ~CodecPtr() {
48 if (m_codec) {
49 opj_destroy_codec(m_codec);
50 }
51 }
52
53 opj_codec_t* get() const {
54 return m_codec;
55 }
56
57 operator opj_codec_t* () const {
58 return m_codec;
59 }
60
61private:
62 opj_codec_t* m_codec;
63};
64
65class ImagePtr {
66public:
67 ImagePtr(opj_image_t* image = nullptr) : m_image(image) {}
68
69 ImagePtr(const ImagePtr& other) {
70 m_image = other.m_image;
71 const_cast<ImagePtr&>(other).m_image = nullptr;
72 }
73
74 ImagePtr& operator=(const ImagePtr& other) {
75 if (this != &other) {
76 if (m_image) {
77 opj_image_destroy(m_image);
78 }
79 m_image = other.m_image;
80 const_cast<ImagePtr&>(other).m_image = nullptr;
81 }
82 return *this;
83 }
84
85 ~ImagePtr() {
86 if (m_image) {
87 opj_image_destroy(m_image);
88 }
89 }
90
91 opj_image_t* get() const {
92 return m_image;
93 }
94
95 operator opj_image_t* () const {
96 return m_image;
97 }
98
99private:
100 opj_image_t* m_image;
101};
102
103class StreamPtr {
104public:
105 StreamPtr(opj_stream_t* codec = nullptr) : m_stream(codec) {}
106
107 StreamPtr(const StreamPtr& other) {
108 m_stream = other.m_stream;
109 const_cast<StreamPtr&>(other).m_stream = nullptr;
110 }
111
112 StreamPtr& operator=(const StreamPtr& other) {
113 if (this != &other) {
114 if (m_stream) {
115 opj_stream_destroy(m_stream);
116 }
117 m_stream = other.m_stream;
118 const_cast<StreamPtr&>(other).m_stream = nullptr;
119 }
120 return *this;
121 }
122
123 ~StreamPtr() {
124 if (m_stream) {
125 opj_stream_destroy(m_stream);
126 }
127 }
128
129 opj_stream_t* get() const {
130 return m_stream;
131 }
132
133 operator opj_stream_t* () const {
134 return m_stream;
135 }
136
137private:
138 opj_stream_t* m_stream;
139};