Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/PngReader.cpp @ 4845:02d77189d8ba received-instance-callback
added ReceivedInstanceCallback + sample C++ plugin
author | Alain Mazy <am@osimis.io> |
---|---|
date | Thu, 09 Dec 2021 17:22:40 +0100 |
parents | 7053502fbf97 |
children | 43e613a7756b |
rev | line source |
---|---|
455 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4201
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
4831
7053502fbf97
added copyright UCLouvain
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
6 * Copyright (C) 2021-2021 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
455 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
455 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
455 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
455 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
24 #include "../PrecompiledHeaders.h" |
455 | 25 #include "PngReader.h" |
26 | |
27 #include "../OrthancException.h" | |
28 #include "../Toolbox.h" | |
29 | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
30 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
31 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
32 #endif |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
33 |
455 | 34 #include <png.h> |
492
f3d4193c571a
switch to jsoncpp-0.6.0-rc2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
455
diff
changeset
|
35 #include <string.h> // For memcpy() |
455 | 36 |
37 namespace Orthanc | |
38 { | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
39 #if ORTHANC_SANDBOXED == 0 |
455 | 40 namespace |
41 { | |
42 struct FileRabi | |
43 { | |
44 FILE* fp_; | |
45 | |
4201 | 46 explicit FileRabi(const char* filename) |
455 | 47 { |
2140 | 48 fp_ = SystemToolbox::OpenFile(filename, FileMode_ReadBinary); |
455 | 49 if (!fp_) |
50 { | |
51 throw OrthancException(ErrorCode_InexistentFile); | |
52 } | |
53 } | |
54 | |
55 ~FileRabi() | |
56 { | |
57 if (fp_) | |
2017 | 58 { |
455 | 59 fclose(fp_); |
2017 | 60 } |
455 | 61 } |
62 }; | |
63 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
64 #endif |
455 | 65 |
66 | |
67 struct PngReader::PngRabi | |
68 { | |
69 png_structp png_; | |
70 png_infop info_; | |
71 png_infop endInfo_; | |
72 | |
73 void Destruct() | |
74 { | |
75 if (png_) | |
76 { | |
77 png_destroy_read_struct(&png_, &info_, &endInfo_); | |
78 | |
79 png_ = NULL; | |
80 info_ = NULL; | |
81 endInfo_ = NULL; | |
82 } | |
83 } | |
84 | |
4201 | 85 PngRabi() : |
86 png_(NULL), | |
87 info_(NULL), | |
88 endInfo_(NULL) | |
455 | 89 { |
90 png_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | |
91 if (!png_) | |
92 { | |
93 throw OrthancException(ErrorCode_NotEnoughMemory); | |
94 } | |
95 | |
96 info_ = png_create_info_struct(png_); | |
97 if (!info_) | |
98 { | |
99 png_destroy_read_struct(&png_, NULL, NULL); | |
100 throw OrthancException(ErrorCode_NotEnoughMemory); | |
101 } | |
102 | |
103 endInfo_ = png_create_info_struct(png_); | |
104 if (!info_) | |
105 { | |
106 png_destroy_read_struct(&png_, &info_, NULL); | |
107 throw OrthancException(ErrorCode_NotEnoughMemory); | |
108 } | |
109 } | |
110 | |
111 ~PngRabi() | |
112 { | |
113 Destruct(); | |
114 } | |
115 | |
116 static void MemoryCallback(png_structp png_ptr, | |
117 png_bytep data, | |
118 png_size_t size); | |
119 }; | |
120 | |
121 | |
122 void PngReader::CheckHeader(const void* header) | |
123 { | |
124 int is_png = !png_sig_cmp((png_bytep) header, 0, 8); | |
125 if (!is_png) | |
126 { | |
127 throw OrthancException(ErrorCode_BadFileFormat); | |
128 } | |
129 } | |
130 | |
131 PngReader::PngReader() | |
132 { | |
133 } | |
134 | |
135 void PngReader::Read(PngRabi& rabi) | |
136 { | |
137 png_set_sig_bytes(rabi.png_, 8); | |
138 | |
139 png_read_info(rabi.png_, rabi.info_); | |
140 | |
141 png_uint_32 width, height; | |
142 int bit_depth, color_type, interlace_type; | |
143 int compression_type, filter_method; | |
144 // get size and bit-depth of the PNG-image | |
145 png_get_IHDR(rabi.png_, rabi.info_, | |
146 &width, &height, | |
147 &bit_depth, &color_type, &interlace_type, | |
148 &compression_type, &filter_method); | |
149 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
150 PixelFormat format; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
151 unsigned int pitch; |
455 | 152 |
153 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 8) | |
154 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
155 format = PixelFormat_Grayscale8; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
156 pitch = width; |
455 | 157 } |
158 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16) | |
159 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
160 format = PixelFormat_Grayscale16; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
161 pitch = 2 * width; |
455 | 162 |
163 if (Toolbox::DetectEndianness() == Endianness_Little) | |
164 { | |
165 png_set_swap(rabi.png_); | |
166 } | |
167 } | |
168 else if (color_type == PNG_COLOR_TYPE_RGB && bit_depth == 8) | |
169 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
170 format = PixelFormat_RGB24; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
171 pitch = 3 * width; |
455 | 172 } |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
173 else if (color_type == PNG_COLOR_TYPE_RGBA && bit_depth == 8) |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
174 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
175 format = PixelFormat_RGBA32; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
176 pitch = 4 * width; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
177 } |
455 | 178 else |
179 { | |
180 throw OrthancException(ErrorCode_NotImplemented); | |
181 } | |
182 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
183 data_.resize(height * pitch); |
455 | 184 |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
185 if (height == 0 || width == 0) |
455 | 186 { |
187 // Empty image, we are done | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
188 AssignEmpty(format); |
455 | 189 return; |
190 } | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
191 |
455 | 192 png_read_update_info(rabi.png_, rabi.info_); |
193 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
194 std::vector<png_bytep> rows(height); |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
195 for (size_t i = 0; i < height; i++) |
455 | 196 { |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
197 rows[i] = &data_[0] + i * pitch; |
455 | 198 } |
199 | |
200 png_read_image(rabi.png_, &rows[0]); | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
201 |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
202 AssignWritable(format, width, height, pitch, &data_[0]); |
455 | 203 } |
204 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
205 |
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
206 #if ORTHANC_SANDBOXED == 0 |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
207 void PngReader::ReadFromFile(const std::string& filename) |
455 | 208 { |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
209 FileRabi f(filename.c_str()); |
455 | 210 |
211 char header[8]; | |
212 if (fread(header, 1, 8, f.fp_) != 8) | |
213 { | |
214 throw OrthancException(ErrorCode_BadFileFormat); | |
215 } | |
216 | |
217 CheckHeader(header); | |
218 | |
219 PngRabi rabi; | |
220 | |
221 if (setjmp(png_jmpbuf(rabi.png_))) | |
222 { | |
223 rabi.Destruct(); | |
224 throw OrthancException(ErrorCode_BadFileFormat); | |
225 } | |
226 | |
227 png_init_io(rabi.png_, f.fp_); | |
228 | |
229 Read(rabi); | |
230 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
231 #endif |
455 | 232 |
233 | |
234 namespace | |
235 { | |
236 struct MemoryBuffer | |
237 { | |
238 const uint8_t* buffer_; | |
239 size_t size_; | |
240 size_t pos_; | |
241 bool ok_; | |
242 }; | |
243 } | |
244 | |
245 | |
246 void PngReader::PngRabi::MemoryCallback(png_structp png_ptr, | |
247 png_bytep outBytes, | |
248 png_size_t byteCountToRead) | |
249 { | |
656 | 250 MemoryBuffer* from = reinterpret_cast<MemoryBuffer*>(png_get_io_ptr(png_ptr)); |
455 | 251 |
252 if (!from->ok_) | |
253 { | |
254 return; | |
255 } | |
256 | |
257 if (from->pos_ + byteCountToRead > from->size_) | |
258 { | |
259 from->ok_ = false; | |
260 return; | |
261 } | |
262 | |
263 memcpy(outBytes, from->buffer_ + from->pos_, byteCountToRead); | |
264 | |
265 from->pos_ += byteCountToRead; | |
266 } | |
267 | |
268 | |
269 void PngReader::ReadFromMemory(const void* buffer, | |
270 size_t size) | |
271 { | |
272 if (size < 8) | |
273 { | |
274 throw OrthancException(ErrorCode_BadFileFormat); | |
275 } | |
276 | |
277 CheckHeader(buffer); | |
278 | |
279 PngRabi rabi; | |
280 | |
281 if (setjmp(png_jmpbuf(rabi.png_))) | |
282 { | |
283 rabi.Destruct(); | |
284 throw OrthancException(ErrorCode_BadFileFormat); | |
285 } | |
286 | |
287 MemoryBuffer tmp; | |
288 tmp.buffer_ = reinterpret_cast<const uint8_t*>(buffer) + 8; // We skip the header | |
289 tmp.size_ = size - 8; | |
290 tmp.pos_ = 0; | |
291 tmp.ok_ = true; | |
292 | |
293 png_set_read_fn(rabi.png_, &tmp, PngRabi::MemoryCallback); | |
294 | |
295 Read(rabi); | |
296 | |
297 if (!tmp.ok_) | |
298 { | |
299 throw OrthancException(ErrorCode_BadFileFormat); | |
300 } | |
301 } | |
302 | |
303 void PngReader::ReadFromMemory(const std::string& buffer) | |
304 { | |
305 if (buffer.size() != 0) | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
306 { |
455 | 307 ReadFromMemory(&buffer[0], buffer.size()); |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
308 } |
455 | 309 else |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
310 { |
455 | 311 ReadFromMemory(NULL, 0); |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
312 } |
455 | 313 } |
314 } |