comparison Core/FileFormats/PngReader.cpp @ 455:238a0c99ced2

PNG reader
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 04 Jul 2013 15:25:15 +0200
parents
children f3d4193c571a
comparison
equal deleted inserted replaced
454:6f47a4262618 455:238a0c99ced2
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
4 * 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 "PngReader.h"
34
35 #include "../OrthancException.h"
36 #include "../Toolbox.h"
37
38 #include <png.h>
39
40 namespace Orthanc
41 {
42 namespace
43 {
44 struct FileRabi
45 {
46 FILE* fp_;
47
48 FileRabi(const char* filename)
49 {
50 fp_ = fopen(filename, "rb");
51 if (!fp_)
52 {
53 throw OrthancException(ErrorCode_InexistentFile);
54 }
55 }
56
57 ~FileRabi()
58 {
59 if (fp_)
60 fclose(fp_);
61 }
62 };
63 }
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
84 PngRabi()
85 {
86 png_ = NULL;
87 info_ = NULL;
88 endInfo_ = NULL;
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 width_ = 0;
134 height_ = 0;
135 pitch_ = 0;
136 format_ = PixelFormat_Grayscale8;
137 }
138
139 void PngReader::Read(PngRabi& rabi)
140 {
141 png_set_sig_bytes(rabi.png_, 8);
142
143 png_read_info(rabi.png_, rabi.info_);
144
145 png_uint_32 width, height;
146 int bit_depth, color_type, interlace_type;
147 int compression_type, filter_method;
148 // get size and bit-depth of the PNG-image
149 png_get_IHDR(rabi.png_, rabi.info_,
150 &width, &height,
151 &bit_depth, &color_type, &interlace_type,
152 &compression_type, &filter_method);
153
154 width_ = width;
155 height_ = height;
156
157 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 8)
158 {
159 format_ = PixelFormat_Grayscale8;
160 pitch_ = width_;
161 }
162 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16)
163 {
164 format_ = PixelFormat_Grayscale16;
165 pitch_ = 2 * width_;
166
167 if (Toolbox::DetectEndianness() == Endianness_Little)
168 {
169 png_set_swap(rabi.png_);
170 }
171 }
172 else if (color_type == PNG_COLOR_TYPE_RGB && bit_depth == 8)
173 {
174 format_ = PixelFormat_Grayscale8;
175 pitch_ = 3 * width_;
176 }
177 else
178 {
179 throw OrthancException(ErrorCode_NotImplemented);
180 }
181
182 buffer_.resize(height_ * pitch_);
183
184 if (height_ == 0 || width_ == 0)
185 {
186 // Empty image, we are done
187 return;
188 }
189
190 png_read_update_info(rabi.png_, rabi.info_);
191
192 std::vector<png_bytep> rows(height_);
193 for (size_t i = 0; i < height_; i++)
194 {
195 rows[i] = &buffer_[0] + i * pitch_;
196 }
197
198 png_read_image(rabi.png_, &rows[0]);
199 }
200
201 void PngReader::ReadFromFile(const char* filename)
202 {
203 FileRabi f(filename);
204
205 char header[8];
206 if (fread(header, 1, 8, f.fp_) != 8)
207 {
208 throw OrthancException(ErrorCode_BadFileFormat);
209 }
210
211 CheckHeader(header);
212
213 PngRabi rabi;
214
215 if (setjmp(png_jmpbuf(rabi.png_)))
216 {
217 rabi.Destruct();
218 throw OrthancException(ErrorCode_BadFileFormat);
219 }
220
221 png_init_io(rabi.png_, f.fp_);
222
223 Read(rabi);
224 }
225
226
227
228 namespace
229 {
230 struct MemoryBuffer
231 {
232 const uint8_t* buffer_;
233 size_t size_;
234 size_t pos_;
235 bool ok_;
236 };
237 }
238
239
240 void PngReader::PngRabi::MemoryCallback(png_structp png_ptr,
241 png_bytep outBytes,
242 png_size_t byteCountToRead)
243 {
244 MemoryBuffer* from = (MemoryBuffer*) png_get_io_ptr(png_ptr);
245
246 if (!from->ok_)
247 {
248 return;
249 }
250
251 if (from->pos_ + byteCountToRead > from->size_)
252 {
253 from->ok_ = false;
254 return;
255 }
256
257 memcpy(outBytes, from->buffer_ + from->pos_, byteCountToRead);
258
259 from->pos_ += byteCountToRead;
260 }
261
262
263 void PngReader::ReadFromMemory(const void* buffer,
264 size_t size)
265 {
266 if (size < 8)
267 {
268 throw OrthancException(ErrorCode_BadFileFormat);
269 }
270
271 CheckHeader(buffer);
272
273 PngRabi rabi;
274
275 if (setjmp(png_jmpbuf(rabi.png_)))
276 {
277 rabi.Destruct();
278 throw OrthancException(ErrorCode_BadFileFormat);
279 }
280
281 MemoryBuffer tmp;
282 tmp.buffer_ = reinterpret_cast<const uint8_t*>(buffer) + 8; // We skip the header
283 tmp.size_ = size - 8;
284 tmp.pos_ = 0;
285 tmp.ok_ = true;
286
287 png_set_read_fn(rabi.png_, &tmp, PngRabi::MemoryCallback);
288
289 Read(rabi);
290
291 if (!tmp.ok_)
292 {
293 throw OrthancException(ErrorCode_BadFileFormat);
294 }
295 }
296
297 void PngReader::ReadFromMemory(const std::string& buffer)
298 {
299 if (buffer.size() != 0)
300 ReadFromMemory(&buffer[0], buffer.size());
301 else
302 ReadFromMemory(NULL, 0);
303 }
304 }