Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/PngReader.cpp @ 5577:9e74e761b108 find-refactoring
integration mainline->find-refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 26 Apr 2024 17:43:22 +0200 |
parents | 48b8dae6dc77 |
children | f7adfb22e20e |
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 |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
455 | 7 * |
8 * 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
|
9 * 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
|
10 * 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
|
11 * the License, or (at your option) any later version. |
455 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * 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
|
16 * Lesser General Public License for more details. |
455 | 17 * |
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
|
18 * 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
|
19 * 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
|
20 * <http://www.gnu.org/licenses/>. |
455 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
24 #include "../PrecompiledHeaders.h" |
455 | 25 #include "PngReader.h" |
26 | |
27 #include "../OrthancException.h" | |
28 #include "../Toolbox.h" | |
29 | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
30 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
31 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
32 #endif |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
33 |
455 | 34 #include <png.h> |
492
f3d4193c571a
switch to jsoncpp-0.6.0-rc2
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
455
diff
changeset
|
35 #include <string.h> // For memcpy() |
455 | 36 |
37 namespace Orthanc | |
38 { | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
39 #if ORTHANC_SANDBOXED == 0 |
455 | 40 namespace |
41 { | |
42 struct FileRabi | |
43 { | |
44 FILE* fp_; | |
45 | |
4201 | 46 explicit FileRabi(const char* filename) |
455 | 47 { |
2140 | 48 fp_ = SystemToolbox::OpenFile(filename, FileMode_ReadBinary); |
455 | 49 if (!fp_) |
50 { | |
51 throw OrthancException(ErrorCode_InexistentFile); | |
52 } | |
53 } | |
54 | |
55 ~FileRabi() | |
56 { | |
57 if (fp_) | |
2017 | 58 { |
455 | 59 fclose(fp_); |
2017 | 60 } |
455 | 61 } |
62 }; | |
63 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
64 #endif |
455 | 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 | |
4201 | 85 PngRabi() : |
86 png_(NULL), | |
87 info_(NULL), | |
88 endInfo_(NULL) | |
455 | 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 } |
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
|
178 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
|
179 { |
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 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
|
181 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
|
182 |
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 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
|
184 { |
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 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
|
186 } |
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 } |
455 | 188 else |
189 { | |
190 throw OrthancException(ErrorCode_NotImplemented); | |
191 } | |
192 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
193 data_.resize(height * pitch); |
455 | 194 |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
195 if (height == 0 || width == 0) |
455 | 196 { |
197 // Empty image, we are done | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
198 AssignEmpty(format); |
455 | 199 return; |
200 } | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
201 |
455 | 202 png_read_update_info(rabi.png_, rabi.info_); |
203 | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
204 std::vector<png_bytep> rows(height); |
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
205 for (size_t i = 0; i < height; i++) |
455 | 206 { |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
207 rows[i] = &data_[0] + i * pitch; |
455 | 208 } |
209 | |
210 png_read_image(rabi.png_, &rows[0]); | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
211 |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
212 AssignWritable(format, width, height, pitch, &data_[0]); |
455 | 213 } |
214 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
215 |
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
216 #if ORTHANC_SANDBOXED == 0 |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
217 void PngReader::ReadFromFile(const std::string& filename) |
455 | 218 { |
1925
56276bad7e42
removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
219 FileRabi f(filename.c_str()); |
455 | 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 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
241 #endif |
455 | 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 { | |
656 | 260 MemoryBuffer* from = reinterpret_cast<MemoryBuffer*>(png_get_io_ptr(png_ptr)); |
455 | 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) | |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
316 { |
455 | 317 ReadFromMemory(&buffer[0], buffer.size()); |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
318 } |
455 | 319 else |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
320 { |
455 | 321 ReadFromMemory(NULL, 0); |
797
37adac56017a
ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
322 } |
455 | 323 } |
324 } |