comparison Resources/Orthanc/Core/Images/PngReader.cpp @ 59:7a3853d51c45

Move "Framework/Orthanc/" as "Resources/Orthanc/"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Nov 2016 16:34:34 +0100
parents Framework/Orthanc/Core/Images/PngReader.cpp@06847108819c
children ff0ef01c332c
comparison
equal deleted inserted replaced
58:35468714a38e 59:7a3853d51c45
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
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
33 #include "../PrecompiledHeaders.h"
34 #include "PngReader.h"
35
36 #include "../OrthancException.h"
37 #include "../Toolbox.h"
38
39 #if ORTHANC_SANDBOXED == 0
40 # include "../SystemToolbox.h"
41 #endif
42
43 #include <png.h>
44 #include <string.h> // For memcpy()
45
46 namespace Orthanc
47 {
48 #if ORTHANC_SANDBOXED == 0
49 namespace
50 {
51 struct FileRabi
52 {
53 FILE* fp_;
54
55 FileRabi(const char* filename)
56 {
57 fp_ = SystemToolbox::OpenFile(filename, FileMode_ReadBinary);
58 if (!fp_)
59 {
60 throw OrthancException(ErrorCode_InexistentFile);
61 }
62 }
63
64 ~FileRabi()
65 {
66 if (fp_)
67 {
68 fclose(fp_);
69 }
70 }
71 };
72 }
73 #endif
74
75
76 struct PngReader::PngRabi
77 {
78 png_structp png_;
79 png_infop info_;
80 png_infop endInfo_;
81
82 void Destruct()
83 {
84 if (png_)
85 {
86 png_destroy_read_struct(&png_, &info_, &endInfo_);
87
88 png_ = NULL;
89 info_ = NULL;
90 endInfo_ = NULL;
91 }
92 }
93
94 PngRabi()
95 {
96 png_ = NULL;
97 info_ = NULL;
98 endInfo_ = NULL;
99
100 png_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
101 if (!png_)
102 {
103 throw OrthancException(ErrorCode_NotEnoughMemory);
104 }
105
106 info_ = png_create_info_struct(png_);
107 if (!info_)
108 {
109 png_destroy_read_struct(&png_, NULL, NULL);
110 throw OrthancException(ErrorCode_NotEnoughMemory);
111 }
112
113 endInfo_ = png_create_info_struct(png_);
114 if (!info_)
115 {
116 png_destroy_read_struct(&png_, &info_, NULL);
117 throw OrthancException(ErrorCode_NotEnoughMemory);
118 }
119 }
120
121 ~PngRabi()
122 {
123 Destruct();
124 }
125
126 static void MemoryCallback(png_structp png_ptr,
127 png_bytep data,
128 png_size_t size);
129 };
130
131
132 void PngReader::CheckHeader(const void* header)
133 {
134 int is_png = !png_sig_cmp((png_bytep) header, 0, 8);
135 if (!is_png)
136 {
137 throw OrthancException(ErrorCode_BadFileFormat);
138 }
139 }
140
141 PngReader::PngReader()
142 {
143 }
144
145 void PngReader::Read(PngRabi& rabi)
146 {
147 png_set_sig_bytes(rabi.png_, 8);
148
149 png_read_info(rabi.png_, rabi.info_);
150
151 png_uint_32 width, height;
152 int bit_depth, color_type, interlace_type;
153 int compression_type, filter_method;
154 // get size and bit-depth of the PNG-image
155 png_get_IHDR(rabi.png_, rabi.info_,
156 &width, &height,
157 &bit_depth, &color_type, &interlace_type,
158 &compression_type, &filter_method);
159
160 PixelFormat format;
161 unsigned int pitch;
162
163 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 8)
164 {
165 format = PixelFormat_Grayscale8;
166 pitch = width;
167 }
168 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16)
169 {
170 format = PixelFormat_Grayscale16;
171 pitch = 2 * width;
172
173 if (Toolbox::DetectEndianness() == Endianness_Little)
174 {
175 png_set_swap(rabi.png_);
176 }
177 }
178 else if (color_type == PNG_COLOR_TYPE_RGB && bit_depth == 8)
179 {
180 format = PixelFormat_RGB24;
181 pitch = 3 * width;
182 }
183 else if (color_type == PNG_COLOR_TYPE_RGBA && bit_depth == 8)
184 {
185 format = PixelFormat_RGBA32;
186 pitch = 4 * width;
187 }
188 else
189 {
190 throw OrthancException(ErrorCode_NotImplemented);
191 }
192
193 data_.resize(height * pitch);
194
195 if (height == 0 || width == 0)
196 {
197 // Empty image, we are done
198 AssignEmpty(format);
199 return;
200 }
201
202 png_read_update_info(rabi.png_, rabi.info_);
203
204 std::vector<png_bytep> rows(height);
205 for (size_t i = 0; i < height; i++)
206 {
207 rows[i] = &data_[0] + i * pitch;
208 }
209
210 png_read_image(rabi.png_, &rows[0]);
211
212 AssignWritable(format, width, height, pitch, &data_[0]);
213 }
214
215
216 #if ORTHANC_SANDBOXED == 0
217 void PngReader::ReadFromFile(const std::string& filename)
218 {
219 FileRabi f(filename.c_str());
220
221 char header[8];
222 if (fread(header, 1, 8, f.fp_) != 8)
223 {
224 throw OrthancException(ErrorCode_BadFileFormat);
225 }
226
227 CheckHeader(header);
228
229 PngRabi rabi;
230
231 if (setjmp(png_jmpbuf(rabi.png_)))
232 {
233 rabi.Destruct();
234 throw OrthancException(ErrorCode_BadFileFormat);
235 }
236
237 png_init_io(rabi.png_, f.fp_);
238
239 Read(rabi);
240 }
241 #endif
242
243
244 namespace
245 {
246 struct MemoryBuffer
247 {
248 const uint8_t* buffer_;
249 size_t size_;
250 size_t pos_;
251 bool ok_;
252 };
253 }
254
255
256 void PngReader::PngRabi::MemoryCallback(png_structp png_ptr,
257 png_bytep outBytes,
258 png_size_t byteCountToRead)
259 {
260 MemoryBuffer* from = reinterpret_cast<MemoryBuffer*>(png_get_io_ptr(png_ptr));
261
262 if (!from->ok_)
263 {
264 return;
265 }
266
267 if (from->pos_ + byteCountToRead > from->size_)
268 {
269 from->ok_ = false;
270 return;
271 }
272
273 memcpy(outBytes, from->buffer_ + from->pos_, byteCountToRead);
274
275 from->pos_ += byteCountToRead;
276 }
277
278
279 void PngReader::ReadFromMemory(const void* buffer,
280 size_t size)
281 {
282 if (size < 8)
283 {
284 throw OrthancException(ErrorCode_BadFileFormat);
285 }
286
287 CheckHeader(buffer);
288
289 PngRabi rabi;
290
291 if (setjmp(png_jmpbuf(rabi.png_)))
292 {
293 rabi.Destruct();
294 throw OrthancException(ErrorCode_BadFileFormat);
295 }
296
297 MemoryBuffer tmp;
298 tmp.buffer_ = reinterpret_cast<const uint8_t*>(buffer) + 8; // We skip the header
299 tmp.size_ = size - 8;
300 tmp.pos_ = 0;
301 tmp.ok_ = true;
302
303 png_set_read_fn(rabi.png_, &tmp, PngRabi::MemoryCallback);
304
305 Read(rabi);
306
307 if (!tmp.ok_)
308 {
309 throw OrthancException(ErrorCode_BadFileFormat);
310 }
311 }
312
313 void PngReader::ReadFromMemory(const std::string& buffer)
314 {
315 if (buffer.size() != 0)
316 {
317 ReadFromMemory(&buffer[0], buffer.size());
318 }
319 else
320 {
321 ReadFromMemory(NULL, 0);
322 }
323 }
324 }