annotate Core/FileFormats/PngReader.cpp @ 689:2d0a347e8cfc

switch to 2014
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Feb 2014 16:06:58 +0100
parents 08eca5d86aad
children 37adac56017a
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
689
2d0a347e8cfc switch to 2014
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 656
diff changeset
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * 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
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "PngReader.h"
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "../OrthancException.h"
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include "../Toolbox.h"
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
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 namespace Orthanc
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 namespace
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 struct FileRabi
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 FILE* fp_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 FileRabi(const char* filename)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 fp_ = fopen(filename, "rb");
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 if (!fp_)
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 throw OrthancException(ErrorCode_InexistentFile);
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 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 ~FileRabi()
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 if (fp_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 fclose(fp_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 };
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 struct PngReader::PngRabi
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 png_structp png_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 png_infop info_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 png_infop endInfo_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 void Destruct()
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 if (png_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 png_destroy_read_struct(&png_, &info_, &endInfo_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 png_ = NULL;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 info_ = NULL;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 endInfo_ = NULL;
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 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 PngRabi()
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 png_ = NULL;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 info_ = NULL;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 endInfo_ = NULL;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 png_ = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 if (!png_)
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 throw OrthancException(ErrorCode_NotEnoughMemory);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 info_ = png_create_info_struct(png_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 if (!info_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 png_destroy_read_struct(&png_, NULL, NULL);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 throw OrthancException(ErrorCode_NotEnoughMemory);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 endInfo_ = png_create_info_struct(png_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 if (!info_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 png_destroy_read_struct(&png_, &info_, NULL);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 throw OrthancException(ErrorCode_NotEnoughMemory);
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 }
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 ~PngRabi()
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 Destruct();
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 static void MemoryCallback(png_structp png_ptr,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 png_bytep data,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 png_size_t size);
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
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 void PngReader::CheckHeader(const void* header)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 int is_png = !png_sig_cmp((png_bytep) header, 0, 8);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 if (!is_png)
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 throw OrthancException(ErrorCode_BadFileFormat);
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 }
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 PngReader::PngReader()
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 width_ = 0;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 height_ = 0;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 pitch_ = 0;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 format_ = PixelFormat_Grayscale8;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 }
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 void PngReader::Read(PngRabi& rabi)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 png_set_sig_bytes(rabi.png_, 8);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 png_read_info(rabi.png_, rabi.info_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 png_uint_32 width, height;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 int bit_depth, color_type, interlace_type;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 int compression_type, filter_method;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 // get size and bit-depth of the PNG-image
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 png_get_IHDR(rabi.png_, rabi.info_,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 &width, &height,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 &bit_depth, &color_type, &interlace_type,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 &compression_type, &filter_method);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 width_ = width;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 height_ = height;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 8)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 format_ = PixelFormat_Grayscale8;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 pitch_ = width_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth == 16)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 format_ = PixelFormat_Grayscale16;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 pitch_ = 2 * width_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 if (Toolbox::DetectEndianness() == Endianness_Little)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 png_set_swap(rabi.png_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 else if (color_type == PNG_COLOR_TYPE_RGB && bit_depth == 8)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 format_ = PixelFormat_Grayscale8;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 pitch_ = 3 * width_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 else
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 throw OrthancException(ErrorCode_NotImplemented);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 buffer_.resize(height_ * pitch_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 if (height_ == 0 || width_ == 0)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 // Empty image, we are done
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 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 std::vector<png_bytep> rows(height_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 for (size_t i = 0; i < height_; i++)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 rows[i] = &buffer_[0] + i * pitch_;
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]);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 void PngReader::ReadFromFile(const char* filename)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 FileRabi f(filename);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 char header[8];
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 if (fread(header, 1, 8, f.fp_) != 8)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 CheckHeader(header);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 PngRabi rabi;
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 if (setjmp(png_jmpbuf(rabi.png_)))
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 rabi.Destruct();
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 }
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 png_init_io(rabi.png_, f.fp_);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 Read(rabi);
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
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 namespace
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 struct MemoryBuffer
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 const uint8_t* buffer_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 size_t size_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 size_t pos_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 bool ok_;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 };
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 void PngReader::PngRabi::MemoryCallback(png_structp png_ptr,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 png_bytep outBytes,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 png_size_t byteCountToRead)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 {
656
08eca5d86aad fixes to cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 492
diff changeset
245 MemoryBuffer* from = reinterpret_cast<MemoryBuffer*>(png_get_io_ptr(png_ptr));
455
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 if (!from->ok_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 return;
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 if (from->pos_ + byteCountToRead > from->size_)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 from->ok_ = false;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 return;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 }
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 memcpy(outBytes, from->buffer_ + from->pos_, byteCountToRead);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 from->pos_ += byteCountToRead;
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
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 void PngReader::ReadFromMemory(const void* buffer,
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 size_t size)
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 if (size < 8)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 {
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 throw OrthancException(ErrorCode_BadFileFormat);
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
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 CheckHeader(buffer);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 PngRabi rabi;
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 if (setjmp(png_jmpbuf(rabi.png_)))
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 rabi.Destruct();
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 throw OrthancException(ErrorCode_BadFileFormat);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 }
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 MemoryBuffer tmp;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 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
284 tmp.size_ = size - 8;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 tmp.pos_ = 0;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 tmp.ok_ = true;
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 png_set_read_fn(rabi.png_, &tmp, PngRabi::MemoryCallback);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 Read(rabi);
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 if (!tmp.ok_)
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 throw OrthancException(ErrorCode_BadFileFormat);
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 }
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 void PngReader::ReadFromMemory(const std::string& buffer)
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 if (buffer.size() != 0)
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 ReadFromMemory(&buffer[0], buffer.size());
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 else
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 ReadFromMemory(NULL, 0);
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 }
238a0c99ced2 PNG reader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 }