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