annotate OrthancFramework/Sources/Images/PngReader.cpp @ 4313:91554aecff9a

removed a friend method for better abi
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Nov 2020 16:09:05 +0100
parents 2d5209153b32
children d9473bd5ed43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1612
diff changeset
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 800
diff changeset
23 #include "../PrecompiledHeaders.h"
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "PngReader.h"
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "../OrthancException.h"
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 #include "../Toolbox.h"
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 namespace Orthanc
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
2170
baf8dd89b4e0 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
38 #if ORTHANC_SANDBOXED == 0
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 namespace
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 struct FileRabi
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 FILE* fp_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
45 explicit FileRabi(const char* filename)
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2017
diff changeset
47 fp_ = SystemToolbox::OpenFile(filename, FileMode_ReadBinary);
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 if (!fp_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 throw OrthancException(ErrorCode_InexistentFile);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 ~FileRabi()
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 if (fp_)
2017
08ce34cfacad refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1925
diff changeset
57 {
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 fclose(fp_);
2017
08ce34cfacad refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1925
diff changeset
59 }
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 };
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
2170
baf8dd89b4e0 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
63 #endif
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 struct PngReader::PngRabi
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 png_structp png_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 png_infop info_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 png_infop endInfo_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 void Destruct()
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 if (png_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 png_destroy_read_struct(&png_, &info_, &endInfo_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 png_ = NULL;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 info_ = NULL;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 endInfo_ = NULL;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
84 PngRabi() :
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
85 png_(NULL),
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
86 info_(NULL),
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
87 endInfo_(NULL)
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 png_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 if (!png_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 throw OrthancException(ErrorCode_NotEnoughMemory);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 info_ = png_create_info_struct(png_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 if (!info_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 png_destroy_read_struct(&png_, NULL, NULL);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 throw OrthancException(ErrorCode_NotEnoughMemory);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 endInfo_ = png_create_info_struct(png_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 if (!info_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 png_destroy_read_struct(&png_, &info_, NULL);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 throw OrthancException(ErrorCode_NotEnoughMemory);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 ~PngRabi()
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 Destruct();
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 static void MemoryCallback(png_structp png_ptr,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 png_bytep data,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 png_size_t size);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 };
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 void PngReader::CheckHeader(const void* header)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 int is_png = !png_sig_cmp((png_bytep) header, 0, 8);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 if (!is_png)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 PngReader::PngReader()
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 void PngReader::Read(PngRabi& rabi)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 png_set_sig_bytes(rabi.png_, 8);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 png_read_info(rabi.png_, rabi.info_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 png_uint_32 width, height;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 int bit_depth, color_type, interlace_type;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 int compression_type, filter_method;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 // get size and bit-depth of the PNG-image
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 png_get_IHDR(rabi.png_, rabi.info_,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 &width, &height,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 &bit_depth, &color_type, &interlace_type,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 &compression_type, &filter_method);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
149 PixelFormat format;
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
150 unsigned int pitch;
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 8)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 {
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
154 format = PixelFormat_Grayscale8;
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
155 pitch = width;
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 {
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
159 format = PixelFormat_Grayscale16;
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
160 pitch = 2 * width;
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 if (Toolbox::DetectEndianness() == Endianness_Little)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 png_set_swap(rabi.png_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 else if (color_type == PNG_COLOR_TYPE_RGB && bit_depth == 8)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 {
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
169 format = PixelFormat_RGB24;
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
170 pitch = 3 * width;
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 }
800
ecedd89055db generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 799
diff changeset
172 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
173 {
ecedd89055db generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 799
diff changeset
174 format = PixelFormat_RGBA32;
ecedd89055db generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 799
diff changeset
175 pitch = 4 * width;
ecedd89055db generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 799
diff changeset
176 }
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 else
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 throw OrthancException(ErrorCode_NotImplemented);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
182 data_.resize(height * pitch);
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
184 if (height == 0 || width == 0)
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 // Empty image, we are done
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
187 AssignEmpty(format);
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 return;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 }
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
190
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 png_read_update_info(rabi.png_, rabi.info_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
193 std::vector<png_bytep> rows(height);
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
194 for (size_t i = 0; i < height; i++)
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 {
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
196 rows[i] = &data_[0] + i * pitch;
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 png_read_image(rabi.png_, &rows[0]);
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
200
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
201 AssignWritable(format, width, height, pitch, &data_[0]);
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
2170
baf8dd89b4e0 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
204
baf8dd89b4e0 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
205 #if ORTHANC_SANDBOXED == 0
1925
56276bad7e42 removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
206 void PngReader::ReadFromFile(const std::string& filename)
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 {
1925
56276bad7e42 removal of two overloads making few sense
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
208 FileRabi f(filename.c_str());
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 char header[8];
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 if (fread(header, 1, 8, f.fp_) != 8)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 CheckHeader(header);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 PngRabi rabi;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 if (setjmp(png_jmpbuf(rabi.png_)))
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 rabi.Destruct();
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 png_init_io(rabi.png_, f.fp_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 Read(rabi);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 }
2170
baf8dd89b4e0 improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2143
diff changeset
230 #endif
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 namespace
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 struct MemoryBuffer
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 const uint8_t* buffer_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 size_t size_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 size_t pos_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 bool ok_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 };
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 void PngReader::PngRabi::MemoryCallback(png_structp png_ptr,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 png_bytep outBytes,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 png_size_t byteCountToRead)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 {
656
08eca5d86aad fixes to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 492
diff changeset
249 MemoryBuffer* from = reinterpret_cast<MemoryBuffer*>(png_get_io_ptr(png_ptr));
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 if (!from->ok_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 return;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 if (from->pos_ + byteCountToRead > from->size_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 from->ok_ = false;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 return;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 memcpy(outBytes, from->buffer_ + from->pos_, byteCountToRead);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 from->pos_ += byteCountToRead;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 void PngReader::ReadFromMemory(const void* buffer,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 size_t size)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 if (size < 8)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 CheckHeader(buffer);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 PngRabi rabi;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 if (setjmp(png_jmpbuf(rabi.png_)))
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 rabi.Destruct();
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 MemoryBuffer tmp;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 tmp.buffer_ = reinterpret_cast<const uint8_t*>(buffer) + 8; // We skip the header
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 tmp.size_ = size - 8;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 tmp.pos_ = 0;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 tmp.ok_ = true;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 png_set_read_fn(rabi.png_, &tmp, PngRabi::MemoryCallback);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 Read(rabi);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 if (!tmp.ok_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 void PngReader::ReadFromMemory(const std::string& buffer)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 if (buffer.size() != 0)
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
305 {
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 ReadFromMemory(&buffer[0], buffer.size());
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
307 }
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 else
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
309 {
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 ReadFromMemory(NULL, 0);
797
37adac56017a ImageAccessor abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
311 }
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 }