Mercurial > hg > orthanc
annotate Core/FileFormats/PngReader.cpp @ 633:e207b33216db
merge
author | jodogne |
---|---|
date | Tue, 29 Oct 2013 09:04:25 +0100 |
parents | f3d4193c571a |
children | 08eca5d86aad |
rev | line source |
---|---|
455 | 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> | |
492
f3d4193c571a
switch to jsoncpp-0.6.0-rc2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
455
diff
changeset
|
39 #include <string.h> // For memcpy() |
455 | 40 |
41 namespace Orthanc | |
42 { | |
43 namespace | |
44 { | |
45 struct FileRabi | |
46 { | |
47 FILE* fp_; | |
48 | |
49 FileRabi(const char* filename) | |
50 { | |
51 fp_ = fopen(filename, "rb"); | |
52 if (!fp_) | |
53 { | |
54 throw OrthancException(ErrorCode_InexistentFile); | |
55 } | |
56 } | |
57 | |
58 ~FileRabi() | |
59 { | |
60 if (fp_) | |
61 fclose(fp_); | |
62 } | |
63 }; | |
64 } | |
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 | |
85 PngRabi() | |
86 { | |
87 png_ = NULL; | |
88 info_ = NULL; | |
89 endInfo_ = NULL; | |
90 | |
91 png_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | |
92 if (!png_) | |
93 { | |
94 throw OrthancException(ErrorCode_NotEnoughMemory); | |
95 } | |
96 | |
97 info_ = png_create_info_struct(png_); | |
98 if (!info_) | |
99 { | |
100 png_destroy_read_struct(&png_, NULL, NULL); | |
101 throw OrthancException(ErrorCode_NotEnoughMemory); | |
102 } | |
103 | |
104 endInfo_ = png_create_info_struct(png_); | |
105 if (!info_) | |
106 { | |
107 png_destroy_read_struct(&png_, &info_, NULL); | |
108 throw OrthancException(ErrorCode_NotEnoughMemory); | |
109 } | |
110 } | |
111 | |
112 ~PngRabi() | |
113 { | |
114 Destruct(); | |
115 } | |
116 | |
117 static void MemoryCallback(png_structp png_ptr, | |
118 png_bytep data, | |
119 png_size_t size); | |
120 }; | |
121 | |
122 | |
123 void PngReader::CheckHeader(const void* header) | |
124 { | |
125 int is_png = !png_sig_cmp((png_bytep) header, 0, 8); | |
126 if (!is_png) | |
127 { | |
128 throw OrthancException(ErrorCode_BadFileFormat); | |
129 } | |
130 } | |
131 | |
132 PngReader::PngReader() | |
133 { | |
134 width_ = 0; | |
135 height_ = 0; | |
136 pitch_ = 0; | |
137 format_ = PixelFormat_Grayscale8; | |
138 } | |
139 | |
140 void PngReader::Read(PngRabi& rabi) | |
141 { | |
142 png_set_sig_bytes(rabi.png_, 8); | |
143 | |
144 png_read_info(rabi.png_, rabi.info_); | |
145 | |
146 png_uint_32 width, height; | |
147 int bit_depth, color_type, interlace_type; | |
148 int compression_type, filter_method; | |
149 // get size and bit-depth of the PNG-image | |
150 png_get_IHDR(rabi.png_, rabi.info_, | |
151 &width, &height, | |
152 &bit_depth, &color_type, &interlace_type, | |
153 &compression_type, &filter_method); | |
154 | |
155 width_ = width; | |
156 height_ = height; | |
157 | |
158 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 8) | |
159 { | |
160 format_ = PixelFormat_Grayscale8; | |
161 pitch_ = width_; | |
162 } | |
163 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16) | |
164 { | |
165 format_ = PixelFormat_Grayscale16; | |
166 pitch_ = 2 * width_; | |
167 | |
168 if (Toolbox::DetectEndianness() == Endianness_Little) | |
169 { | |
170 png_set_swap(rabi.png_); | |
171 } | |
172 } | |
173 else if (color_type == PNG_COLOR_TYPE_RGB && bit_depth == 8) | |
174 { | |
175 format_ = PixelFormat_Grayscale8; | |
176 pitch_ = 3 * width_; | |
177 } | |
178 else | |
179 { | |
180 throw OrthancException(ErrorCode_NotImplemented); | |
181 } | |
182 | |
183 buffer_.resize(height_ * pitch_); | |
184 | |
185 if (height_ == 0 || width_ == 0) | |
186 { | |
187 // Empty image, we are done | |
188 return; | |
189 } | |
190 | |
191 png_read_update_info(rabi.png_, rabi.info_); | |
192 | |
193 std::vector<png_bytep> rows(height_); | |
194 for (size_t i = 0; i < height_; i++) | |
195 { | |
196 rows[i] = &buffer_[0] + i * pitch_; | |
197 } | |
198 | |
199 png_read_image(rabi.png_, &rows[0]); | |
200 } | |
201 | |
202 void PngReader::ReadFromFile(const char* filename) | |
203 { | |
204 FileRabi f(filename); | |
205 | |
206 char header[8]; | |
207 if (fread(header, 1, 8, f.fp_) != 8) | |
208 { | |
209 throw OrthancException(ErrorCode_BadFileFormat); | |
210 } | |
211 | |
212 CheckHeader(header); | |
213 | |
214 PngRabi rabi; | |
215 | |
216 if (setjmp(png_jmpbuf(rabi.png_))) | |
217 { | |
218 rabi.Destruct(); | |
219 throw OrthancException(ErrorCode_BadFileFormat); | |
220 } | |
221 | |
222 png_init_io(rabi.png_, f.fp_); | |
223 | |
224 Read(rabi); | |
225 } | |
226 | |
227 | |
228 | |
229 namespace | |
230 { | |
231 struct MemoryBuffer | |
232 { | |
233 const uint8_t* buffer_; | |
234 size_t size_; | |
235 size_t pos_; | |
236 bool ok_; | |
237 }; | |
238 } | |
239 | |
240 | |
241 void PngReader::PngRabi::MemoryCallback(png_structp png_ptr, | |
242 png_bytep outBytes, | |
243 png_size_t byteCountToRead) | |
244 { | |
245 MemoryBuffer* from = (MemoryBuffer*) png_get_io_ptr(png_ptr); | |
246 | |
247 if (!from->ok_) | |
248 { | |
249 return; | |
250 } | |
251 | |
252 if (from->pos_ + byteCountToRead > from->size_) | |
253 { | |
254 from->ok_ = false; | |
255 return; | |
256 } | |
257 | |
258 memcpy(outBytes, from->buffer_ + from->pos_, byteCountToRead); | |
259 | |
260 from->pos_ += byteCountToRead; | |
261 } | |
262 | |
263 | |
264 void PngReader::ReadFromMemory(const void* buffer, | |
265 size_t size) | |
266 { | |
267 if (size < 8) | |
268 { | |
269 throw OrthancException(ErrorCode_BadFileFormat); | |
270 } | |
271 | |
272 CheckHeader(buffer); | |
273 | |
274 PngRabi rabi; | |
275 | |
276 if (setjmp(png_jmpbuf(rabi.png_))) | |
277 { | |
278 rabi.Destruct(); | |
279 throw OrthancException(ErrorCode_BadFileFormat); | |
280 } | |
281 | |
282 MemoryBuffer tmp; | |
283 tmp.buffer_ = reinterpret_cast<const uint8_t*>(buffer) + 8; // We skip the header | |
284 tmp.size_ = size - 8; | |
285 tmp.pos_ = 0; | |
286 tmp.ok_ = true; | |
287 | |
288 png_set_read_fn(rabi.png_, &tmp, PngRabi::MemoryCallback); | |
289 | |
290 Read(rabi); | |
291 | |
292 if (!tmp.ok_) | |
293 { | |
294 throw OrthancException(ErrorCode_BadFileFormat); | |
295 } | |
296 } | |
297 | |
298 void PngReader::ReadFromMemory(const std::string& buffer) | |
299 { | |
300 if (buffer.size() != 0) | |
301 ReadFromMemory(&buffer[0], buffer.size()); | |
302 else | |
303 ReadFromMemory(NULL, 0); | |
304 } | |
305 } |