Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/PngReader.cpp @ 4127:5be586c6a23e framework-lgpl
merge
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 20:28:09 +0200 |
parents | bf7b9edf6b81 |
children | 2d5209153b32 |
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 |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 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 | |
45 FileRabi(const char* filename) | |
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 | |
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 } | |
134 | |
135 void PngReader::Read(PngRabi& rabi) | |
136 { | |
137 png_set_sig_bytes(rabi.png_, 8); | |
138 | |
139 png_read_info(rabi.png_, rabi.info_); | |
140 | |
141 png_uint_32 width, height; | |
142 int bit_depth, color_type, interlace_type; | |
143 int compression_type, filter_method; | |
144 // get size and bit-depth of the PNG-image | |
145 png_get_IHDR(rabi.png_, rabi.info_, | |
146 &width, &height, | |
147 &bit_depth, &color_type, &interlace_type, | |
148 &compression_type, &filter_method); | |
149 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
150 PixelFormat format; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
151 unsigned int pitch; |
455 | 152 |
153 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 8) | |
154 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
155 format = PixelFormat_Grayscale8; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
156 pitch = width; |
455 | 157 } |
158 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16) | |
159 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
160 format = PixelFormat_Grayscale16; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
161 pitch = 2 * width; |
455 | 162 |
163 if (Toolbox::DetectEndianness() == Endianness_Little) | |
164 { | |
165 png_set_swap(rabi.png_); | |
166 } | |
167 } | |
168 else if (color_type == PNG_COLOR_TYPE_RGB && bit_depth == 8) | |
169 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
170 format = PixelFormat_RGB24; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
171 pitch = 3 * width; |
455 | 172 } |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
173 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
|
174 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
175 format = PixelFormat_RGBA32; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
176 pitch = 4 * width; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
177 } |
455 | 178 else |
179 { | |
180 throw OrthancException(ErrorCode_NotImplemented); | |
181 } | |
182 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
183 data_.resize(height * pitch); |
455 | 184 |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
185 if (height == 0 || width == 0) |
455 | 186 { |
187 // Empty image, we are done | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
188 AssignEmpty(format); |
455 | 189 return; |
190 } | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
191 |
455 | 192 png_read_update_info(rabi.png_, rabi.info_); |
193 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
194 std::vector<png_bytep> rows(height); |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
195 for (size_t i = 0; i < height; i++) |
455 | 196 { |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
197 rows[i] = &data_[0] + i * pitch; |
455 | 198 } |
199 | |
200 png_read_image(rabi.png_, &rows[0]); | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
201 |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
202 AssignWritable(format, width, height, pitch, &data_[0]); |
455 | 203 } |
204 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
205 |
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
206 #if ORTHANC_SANDBOXED == 0 |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
207 void PngReader::ReadFromFile(const std::string& filename) |
455 | 208 { |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
209 FileRabi f(filename.c_str()); |
455 | 210 |
211 char header[8]; | |
212 if (fread(header, 1, 8, f.fp_) != 8) | |
213 { | |
214 throw OrthancException(ErrorCode_BadFileFormat); | |
215 } | |
216 | |
217 CheckHeader(header); | |
218 | |
219 PngRabi rabi; | |
220 | |
221 if (setjmp(png_jmpbuf(rabi.png_))) | |
222 { | |
223 rabi.Destruct(); | |
224 throw OrthancException(ErrorCode_BadFileFormat); | |
225 } | |
226 | |
227 png_init_io(rabi.png_, f.fp_); | |
228 | |
229 Read(rabi); | |
230 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
231 #endif |
455 | 232 |
233 | |
234 namespace | |
235 { | |
236 struct MemoryBuffer | |
237 { | |
238 const uint8_t* buffer_; | |
239 size_t size_; | |
240 size_t pos_; | |
241 bool ok_; | |
242 }; | |
243 } | |
244 | |
245 | |
246 void PngReader::PngRabi::MemoryCallback(png_structp png_ptr, | |
247 png_bytep outBytes, | |
248 png_size_t byteCountToRead) | |
249 { | |
656 | 250 MemoryBuffer* from = reinterpret_cast<MemoryBuffer*>(png_get_io_ptr(png_ptr)); |
455 | 251 |
252 if (!from->ok_) | |
253 { | |
254 return; | |
255 } | |
256 | |
257 if (from->pos_ + byteCountToRead > from->size_) | |
258 { | |
259 from->ok_ = false; | |
260 return; | |
261 } | |
262 | |
263 memcpy(outBytes, from->buffer_ + from->pos_, byteCountToRead); | |
264 | |
265 from->pos_ += byteCountToRead; | |
266 } | |
267 | |
268 | |
269 void PngReader::ReadFromMemory(const void* buffer, | |
270 size_t size) | |
271 { | |
272 if (size < 8) | |
273 { | |
274 throw OrthancException(ErrorCode_BadFileFormat); | |
275 } | |
276 | |
277 CheckHeader(buffer); | |
278 | |
279 PngRabi rabi; | |
280 | |
281 if (setjmp(png_jmpbuf(rabi.png_))) | |
282 { | |
283 rabi.Destruct(); | |
284 throw OrthancException(ErrorCode_BadFileFormat); | |
285 } | |
286 | |
287 MemoryBuffer tmp; | |
288 tmp.buffer_ = reinterpret_cast<const uint8_t*>(buffer) + 8; // We skip the header | |
289 tmp.size_ = size - 8; | |
290 tmp.pos_ = 0; | |
291 tmp.ok_ = true; | |
292 | |
293 png_set_read_fn(rabi.png_, &tmp, PngRabi::MemoryCallback); | |
294 | |
295 Read(rabi); | |
296 | |
297 if (!tmp.ok_) | |
298 { | |
299 throw OrthancException(ErrorCode_BadFileFormat); | |
300 } | |
301 } | |
302 | |
303 void PngReader::ReadFromMemory(const std::string& buffer) | |
304 { | |
305 if (buffer.size() != 0) | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
306 { |
455 | 307 ReadFromMemory(&buffer[0], buffer.size()); |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
308 } |
455 | 309 else |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
310 { |
455 | 311 ReadFromMemory(NULL, 0); |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
312 } |
455 | 313 } |
314 } |