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