Mercurial > hg > orthanc
annotate Core/Images/PngWriter.cpp @ 2217:4f39ab2cb453
flush
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 12 Dec 2016 11:56:26 +0100 |
parents | 35febe19e874 |
children | a3a65de1840f |
rev | line source |
---|---|
0 | 1 /** |
59 | 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 |
0 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
0 | 34 #include "PngWriter.h" |
35 | |
36 #include <vector> | |
37 #include <stdint.h> | |
38 #include <png.h> | |
454 | 39 #include "../OrthancException.h" |
40 #include "../ChunkedBuffer.h" | |
41 #include "../Toolbox.h" | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
42 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
43 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
44 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
45 #endif |
0 | 46 |
47 | |
48 // http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-4 | |
49 // http://zarb.org/~gc/html/libpng.html | |
50 /* | |
51 void write_row_callback(png_ptr, png_uint_32 row, int pass) | |
52 { | |
53 }*/ | |
54 | |
55 | |
56 | |
57 | |
58 /* bool isError_; | |
59 | |
60 // http://www.libpng.org/pub/png/book/chapter14.html#png.ch14.div.2 | |
61 | |
62 static void ErrorHandler(png_structp png, png_const_charp message) | |
63 { | |
64 printf("** [%s]\n", message); | |
65 | |
66 PngWriter* that = (PngWriter*) png_get_error_ptr(png); | |
67 that->isError_ = true; | |
68 printf("** %d\n", (int)that); | |
69 | |
70 //((PngWriter*) payload)->isError_ = true; | |
71 } | |
72 | |
73 static void WarningHandler(png_structp png, png_const_charp message) | |
74 { | |
75 printf("++ %d\n", (int)message); | |
76 }*/ | |
77 | |
78 | |
59 | 79 namespace Orthanc |
0 | 80 { |
81 struct PngWriter::PImpl | |
82 { | |
83 png_structp png_; | |
84 png_infop info_; | |
85 | |
86 // Filled by Prepare() | |
87 std::vector<uint8_t*> rows_; | |
88 int bitDepth_; | |
89 int colorType_; | |
90 }; | |
91 | |
92 | |
93 | |
94 PngWriter::PngWriter() : pimpl_(new PImpl) | |
95 { | |
96 pimpl_->png_ = NULL; | |
97 pimpl_->info_ = NULL; | |
98 | |
99 pimpl_->png_ = png_create_write_struct | |
100 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); //this, ErrorHandler, WarningHandler); | |
101 if (!pimpl_->png_) | |
102 { | |
59 | 103 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 104 } |
105 | |
106 pimpl_->info_ = png_create_info_struct(pimpl_->png_); | |
107 if (!pimpl_->info_) | |
108 { | |
109 png_destroy_write_struct(&pimpl_->png_, NULL); | |
59 | 110 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 111 } |
112 } | |
113 | |
114 PngWriter::~PngWriter() | |
115 { | |
116 if (pimpl_->info_) | |
117 { | |
118 png_destroy_info_struct(pimpl_->png_, &pimpl_->info_); | |
119 } | |
120 | |
121 if (pimpl_->png_) | |
122 { | |
123 png_destroy_write_struct(&pimpl_->png_, NULL); | |
124 } | |
125 } | |
126 | |
127 | |
128 | |
129 void PngWriter::Prepare(unsigned int width, | |
130 unsigned int height, | |
131 unsigned int pitch, | |
132 PixelFormat format, | |
133 const void* buffer) | |
134 { | |
135 pimpl_->rows_.resize(height); | |
136 for (unsigned int y = 0; y < height; y++) | |
137 { | |
138 pimpl_->rows_[y] = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)) + y * pitch; | |
139 } | |
140 | |
141 switch (format) | |
142 { | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
143 case PixelFormat_RGB24: |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
144 pimpl_->bitDepth_ = 8; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
145 pimpl_->colorType_ = PNG_COLOR_TYPE_RGB; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
146 break; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
147 |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
148 case PixelFormat_RGBA32: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
149 pimpl_->bitDepth_ = 8; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
150 pimpl_->colorType_ = PNG_COLOR_TYPE_RGBA; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
151 break; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
152 |
0 | 153 case PixelFormat_Grayscale8: |
154 pimpl_->bitDepth_ = 8; | |
155 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY; | |
156 break; | |
157 | |
158 case PixelFormat_Grayscale16: | |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
454
diff
changeset
|
159 case PixelFormat_SignedGrayscale16: |
0 | 160 pimpl_->bitDepth_ = 16; |
161 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY; | |
162 break; | |
163 | |
164 default: | |
59 | 165 throw OrthancException(ErrorCode_NotImplemented); |
0 | 166 } |
167 } | |
168 | |
169 | |
170 void PngWriter::Compress(unsigned int width, | |
171 unsigned int height, | |
172 unsigned int pitch, | |
173 PixelFormat format) | |
174 { | |
175 png_set_IHDR(pimpl_->png_, pimpl_->info_, width, height, | |
176 pimpl_->bitDepth_, pimpl_->colorType_, PNG_INTERLACE_NONE, | |
177 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); | |
178 | |
179 png_write_info(pimpl_->png_, pimpl_->info_); | |
180 | |
181 if (height > 0) | |
182 { | |
183 switch (format) | |
184 { | |
185 case PixelFormat_Grayscale16: | |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
454
diff
changeset
|
186 case PixelFormat_SignedGrayscale16: |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
187 { |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
188 int transforms = 0; |
453 | 189 if (Toolbox::DetectEndianness() == Endianness_Little) |
190 { | |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
191 transforms = PNG_TRANSFORM_SWAP_ENDIAN; |
453 | 192 } |
193 | |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
194 png_set_rows(pimpl_->png_, pimpl_->info_, &pimpl_->rows_[0]); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
195 png_write_png(pimpl_->png_, pimpl_->info_, transforms, NULL); |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
196 |
0 | 197 break; |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
198 } |
0 | 199 |
200 default: | |
201 png_write_image(pimpl_->png_, &pimpl_->rows_[0]); | |
202 } | |
203 } | |
204 | |
205 png_write_end(pimpl_->png_, NULL); | |
206 } | |
207 | |
208 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
209 #if ORTHANC_SANDBOXED == 0 |
1916 | 210 void PngWriter::WriteToFileInternal(const std::string& filename, |
211 unsigned int width, | |
212 unsigned int height, | |
213 unsigned int pitch, | |
214 PixelFormat format, | |
215 const void* buffer) | |
0 | 216 { |
217 Prepare(width, height, pitch, format, buffer); | |
218 | |
2140 | 219 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
0 | 220 if (!fp) |
221 { | |
59 | 222 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 223 } |
224 | |
225 png_init_io(pimpl_->png_, fp); | |
226 | |
227 if (setjmp(png_jmpbuf(pimpl_->png_))) | |
228 { | |
229 // Error during writing PNG | |
59 | 230 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 231 } |
232 | |
233 Compress(width, height, pitch, format); | |
234 | |
235 fclose(fp); | |
236 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
237 #endif |
0 | 238 |
239 | |
240 static void MemoryCallback(png_structp png_ptr, | |
241 png_bytep data, | |
242 png_size_t size) | |
243 { | |
656 | 244 ChunkedBuffer* buffer = reinterpret_cast<ChunkedBuffer*>(png_get_io_ptr(png_ptr)); |
0 | 245 buffer->AddChunk(reinterpret_cast<const char*>(data), size); |
246 } | |
247 | |
248 | |
249 | |
1916 | 250 void PngWriter::WriteToMemoryInternal(std::string& png, |
251 unsigned int width, | |
252 unsigned int height, | |
253 unsigned int pitch, | |
254 PixelFormat format, | |
255 const void* buffer) | |
0 | 256 { |
257 ChunkedBuffer chunks; | |
258 | |
259 Prepare(width, height, pitch, format, buffer); | |
260 | |
261 if (setjmp(png_jmpbuf(pimpl_->png_))) | |
262 { | |
263 // Error during writing PNG | |
59 | 264 throw OrthancException(ErrorCode_InternalError); |
0 | 265 } |
266 | |
267 png_set_write_fn(pimpl_->png_, &chunks, MemoryCallback, NULL); | |
268 | |
269 Compress(width, height, pitch, format); | |
270 | |
271 chunks.Flatten(png); | |
272 } | |
273 } |