Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/PngReader.cpp @ 5640:f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 30 May 2024 21:19:57 +0200 |
parents | 48b8dae6dc77 |
children |
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 |
5640
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
455 | 8 * |
9 * 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
|
10 * 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
|
11 * 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
|
12 * the License, or (at your option) any later version. |
455 | 13 * |
14 * This program is distributed in the hope that it will be useful, but | |
15 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * 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
|
17 * Lesser General Public License for more details. |
455 | 18 * |
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
|
19 * 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
|
20 * 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
|
21 * <http://www.gnu.org/licenses/>. |
455 | 22 **/ |
23 | |
24 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
25 #include "../PrecompiledHeaders.h" |
455 | 26 #include "PngReader.h" |
27 | |
28 #include "../OrthancException.h" | |
29 #include "../Toolbox.h" | |
30 | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
31 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
32 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
33 #endif |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
34 |
455 | 35 #include <png.h> |
492
f3d4193c571a
switch to jsoncpp-0.6.0-rc2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
455
diff
changeset
|
36 #include <string.h> // For memcpy() |
455 | 37 |
38 namespace Orthanc | |
39 { | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
40 #if ORTHANC_SANDBOXED == 0 |
455 | 41 namespace |
42 { | |
43 struct FileRabi | |
44 { | |
45 FILE* fp_; | |
46 | |
4201 | 47 explicit FileRabi(const char* filename) |
455 | 48 { |
2140 | 49 fp_ = SystemToolbox::OpenFile(filename, FileMode_ReadBinary); |
455 | 50 if (!fp_) |
51 { | |
52 throw OrthancException(ErrorCode_InexistentFile); | |
53 } | |
54 } | |
55 | |
56 ~FileRabi() | |
57 { | |
58 if (fp_) | |
2017 | 59 { |
455 | 60 fclose(fp_); |
2017 | 61 } |
455 | 62 } |
63 }; | |
64 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
65 #endif |
455 | 66 |
67 | |
68 struct PngReader::PngRabi | |
69 { | |
70 png_structp png_; | |
71 png_infop info_; | |
72 png_infop endInfo_; | |
73 | |
74 void Destruct() | |
75 { | |
76 if (png_) | |
77 { | |
78 png_destroy_read_struct(&png_, &info_, &endInfo_); | |
79 | |
80 png_ = NULL; | |
81 info_ = NULL; | |
82 endInfo_ = NULL; | |
83 } | |
84 } | |
85 | |
4201 | 86 PngRabi() : |
87 png_(NULL), | |
88 info_(NULL), | |
89 endInfo_(NULL) | |
455 | 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 } | |
135 | |
136 void PngReader::Read(PngRabi& rabi) | |
137 { | |
138 png_set_sig_bytes(rabi.png_, 8); | |
139 | |
140 png_read_info(rabi.png_, rabi.info_); | |
141 | |
142 png_uint_32 width, height; | |
143 int bit_depth, color_type, interlace_type; | |
144 int compression_type, filter_method; | |
145 // get size and bit-depth of the PNG-image | |
146 png_get_IHDR(rabi.png_, rabi.info_, | |
147 &width, &height, | |
148 &bit_depth, &color_type, &interlace_type, | |
149 &compression_type, &filter_method); | |
150 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
151 PixelFormat format; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
152 unsigned int pitch; |
455 | 153 |
154 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 8) | |
155 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
156 format = PixelFormat_Grayscale8; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
157 pitch = width; |
455 | 158 } |
159 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16) | |
160 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
161 format = PixelFormat_Grayscale16; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
162 pitch = 2 * width; |
455 | 163 |
164 if (Toolbox::DetectEndianness() == Endianness_Little) | |
165 { | |
166 png_set_swap(rabi.png_); | |
167 } | |
168 } | |
169 else if (color_type == PNG_COLOR_TYPE_RGB && bit_depth == 8) | |
170 { | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
171 format = PixelFormat_RGB24; |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
172 pitch = 3 * width; |
455 | 173 } |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
174 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
|
175 { |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
176 format = PixelFormat_RGBA32; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
177 pitch = 4 * width; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
178 } |
5054
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
179 else if (color_type == PNG_COLOR_TYPE_RGBA && bit_depth == 16) |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
180 { |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
181 format = PixelFormat_RGBA64; |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
182 pitch = 8 * width; |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
183 |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
184 if (Toolbox::DetectEndianness() == Endianness_Little) |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
185 { |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
186 png_set_swap(rabi.png_); |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
187 } |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
188 } |
455 | 189 else |
190 { | |
191 throw OrthancException(ErrorCode_NotImplemented); | |
192 } | |
193 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
194 data_.resize(height * pitch); |
455 | 195 |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
196 if (height == 0 || width == 0) |
455 | 197 { |
198 // Empty image, we are done | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
199 AssignEmpty(format); |
455 | 200 return; |
201 } | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
202 |
455 | 203 png_read_update_info(rabi.png_, rabi.info_); |
204 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
205 std::vector<png_bytep> rows(height); |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
206 for (size_t i = 0; i < height; i++) |
455 | 207 { |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
208 rows[i] = &data_[0] + i * pitch; |
455 | 209 } |
210 | |
211 png_read_image(rabi.png_, &rows[0]); | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
212 |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
213 AssignWritable(format, width, height, pitch, &data_[0]); |
455 | 214 } |
215 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
216 |
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
217 #if ORTHANC_SANDBOXED == 0 |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
218 void PngReader::ReadFromFile(const std::string& filename) |
455 | 219 { |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
220 FileRabi f(filename.c_str()); |
455 | 221 |
222 char header[8]; | |
223 if (fread(header, 1, 8, f.fp_) != 8) | |
224 { | |
225 throw OrthancException(ErrorCode_BadFileFormat); | |
226 } | |
227 | |
228 CheckHeader(header); | |
229 | |
230 PngRabi rabi; | |
231 | |
232 if (setjmp(png_jmpbuf(rabi.png_))) | |
233 { | |
234 rabi.Destruct(); | |
235 throw OrthancException(ErrorCode_BadFileFormat); | |
236 } | |
237 | |
238 png_init_io(rabi.png_, f.fp_); | |
239 | |
240 Read(rabi); | |
241 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
242 #endif |
455 | 243 |
244 | |
245 namespace | |
246 { | |
247 struct MemoryBuffer | |
248 { | |
249 const uint8_t* buffer_; | |
250 size_t size_; | |
251 size_t pos_; | |
252 bool ok_; | |
253 }; | |
254 } | |
255 | |
256 | |
257 void PngReader::PngRabi::MemoryCallback(png_structp png_ptr, | |
258 png_bytep outBytes, | |
259 png_size_t byteCountToRead) | |
260 { | |
656 | 261 MemoryBuffer* from = reinterpret_cast<MemoryBuffer*>(png_get_io_ptr(png_ptr)); |
455 | 262 |
263 if (!from->ok_) | |
264 { | |
265 return; | |
266 } | |
267 | |
268 if (from->pos_ + byteCountToRead > from->size_) | |
269 { | |
270 from->ok_ = false; | |
271 return; | |
272 } | |
273 | |
274 memcpy(outBytes, from->buffer_ + from->pos_, byteCountToRead); | |
275 | |
276 from->pos_ += byteCountToRead; | |
277 } | |
278 | |
279 | |
280 void PngReader::ReadFromMemory(const void* buffer, | |
281 size_t size) | |
282 { | |
283 if (size < 8) | |
284 { | |
285 throw OrthancException(ErrorCode_BadFileFormat); | |
286 } | |
287 | |
288 CheckHeader(buffer); | |
289 | |
290 PngRabi rabi; | |
291 | |
292 if (setjmp(png_jmpbuf(rabi.png_))) | |
293 { | |
294 rabi.Destruct(); | |
295 throw OrthancException(ErrorCode_BadFileFormat); | |
296 } | |
297 | |
298 MemoryBuffer tmp; | |
299 tmp.buffer_ = reinterpret_cast<const uint8_t*>(buffer) + 8; // We skip the header | |
300 tmp.size_ = size - 8; | |
301 tmp.pos_ = 0; | |
302 tmp.ok_ = true; | |
303 | |
304 png_set_read_fn(rabi.png_, &tmp, PngRabi::MemoryCallback); | |
305 | |
306 Read(rabi); | |
307 | |
308 if (!tmp.ok_) | |
309 { | |
310 throw OrthancException(ErrorCode_BadFileFormat); | |
311 } | |
312 } | |
313 | |
314 void PngReader::ReadFromMemory(const std::string& buffer) | |
315 { | |
316 if (buffer.size() != 0) | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
317 { |
455 | 318 ReadFromMemory(&buffer[0], buffer.size()); |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
319 } |
455 | 320 else |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
321 { |
455 | 322 ReadFromMemory(NULL, 0); |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
323 } |
455 | 324 } |
325 } |