Mercurial > hg > orthanc
annotate Core/Images/PngWriter.cpp @ 2147:8466aa60544a
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 09 Nov 2016 17:37:47 +0100 |
parents | fd5875662670 |
children | baf8dd89b4e0 |
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" | |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2140
diff
changeset
|
42 #include "../SystemToolbox.h" |
0 | 43 |
44 | |
45 // http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-4 | |
46 // http://zarb.org/~gc/html/libpng.html | |
47 /* | |
48 void write_row_callback(png_ptr, png_uint_32 row, int pass) | |
49 { | |
50 }*/ | |
51 | |
52 | |
53 | |
54 | |
55 /* bool isError_; | |
56 | |
57 // http://www.libpng.org/pub/png/book/chapter14.html#png.ch14.div.2 | |
58 | |
59 static void ErrorHandler(png_structp png, png_const_charp message) | |
60 { | |
61 printf("** [%s]\n", message); | |
62 | |
63 PngWriter* that = (PngWriter*) png_get_error_ptr(png); | |
64 that->isError_ = true; | |
65 printf("** %d\n", (int)that); | |
66 | |
67 //((PngWriter*) payload)->isError_ = true; | |
68 } | |
69 | |
70 static void WarningHandler(png_structp png, png_const_charp message) | |
71 { | |
72 printf("++ %d\n", (int)message); | |
73 }*/ | |
74 | |
75 | |
59 | 76 namespace Orthanc |
0 | 77 { |
78 struct PngWriter::PImpl | |
79 { | |
80 png_structp png_; | |
81 png_infop info_; | |
82 | |
83 // Filled by Prepare() | |
84 std::vector<uint8_t*> rows_; | |
85 int bitDepth_; | |
86 int colorType_; | |
87 }; | |
88 | |
89 | |
90 | |
91 PngWriter::PngWriter() : pimpl_(new PImpl) | |
92 { | |
93 pimpl_->png_ = NULL; | |
94 pimpl_->info_ = NULL; | |
95 | |
96 pimpl_->png_ = png_create_write_struct | |
97 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); //this, ErrorHandler, WarningHandler); | |
98 if (!pimpl_->png_) | |
99 { | |
59 | 100 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 101 } |
102 | |
103 pimpl_->info_ = png_create_info_struct(pimpl_->png_); | |
104 if (!pimpl_->info_) | |
105 { | |
106 png_destroy_write_struct(&pimpl_->png_, NULL); | |
59 | 107 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 108 } |
109 } | |
110 | |
111 PngWriter::~PngWriter() | |
112 { | |
113 if (pimpl_->info_) | |
114 { | |
115 png_destroy_info_struct(pimpl_->png_, &pimpl_->info_); | |
116 } | |
117 | |
118 if (pimpl_->png_) | |
119 { | |
120 png_destroy_write_struct(&pimpl_->png_, NULL); | |
121 } | |
122 } | |
123 | |
124 | |
125 | |
126 void PngWriter::Prepare(unsigned int width, | |
127 unsigned int height, | |
128 unsigned int pitch, | |
129 PixelFormat format, | |
130 const void* buffer) | |
131 { | |
132 pimpl_->rows_.resize(height); | |
133 for (unsigned int y = 0; y < height; y++) | |
134 { | |
135 pimpl_->rows_[y] = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)) + y * pitch; | |
136 } | |
137 | |
138 switch (format) | |
139 { | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
140 case PixelFormat_RGB24: |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
141 pimpl_->bitDepth_ = 8; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
142 pimpl_->colorType_ = PNG_COLOR_TYPE_RGB; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
143 break; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
144 |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
145 case PixelFormat_RGBA32: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
146 pimpl_->bitDepth_ = 8; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
147 pimpl_->colorType_ = PNG_COLOR_TYPE_RGBA; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
148 break; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
149 |
0 | 150 case PixelFormat_Grayscale8: |
151 pimpl_->bitDepth_ = 8; | |
152 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY; | |
153 break; | |
154 | |
155 case PixelFormat_Grayscale16: | |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
454
diff
changeset
|
156 case PixelFormat_SignedGrayscale16: |
0 | 157 pimpl_->bitDepth_ = 16; |
158 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY; | |
159 break; | |
160 | |
161 default: | |
59 | 162 throw OrthancException(ErrorCode_NotImplemented); |
0 | 163 } |
164 } | |
165 | |
166 | |
167 void PngWriter::Compress(unsigned int width, | |
168 unsigned int height, | |
169 unsigned int pitch, | |
170 PixelFormat format) | |
171 { | |
172 png_set_IHDR(pimpl_->png_, pimpl_->info_, width, height, | |
173 pimpl_->bitDepth_, pimpl_->colorType_, PNG_INTERLACE_NONE, | |
174 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); | |
175 | |
176 png_write_info(pimpl_->png_, pimpl_->info_); | |
177 | |
178 if (height > 0) | |
179 { | |
180 switch (format) | |
181 { | |
182 case PixelFormat_Grayscale16: | |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
454
diff
changeset
|
183 case PixelFormat_SignedGrayscale16: |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
184 { |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
185 int transforms = 0; |
453 | 186 if (Toolbox::DetectEndianness() == Endianness_Little) |
187 { | |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
188 transforms = PNG_TRANSFORM_SWAP_ENDIAN; |
453 | 189 } |
190 | |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
191 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
|
192 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
|
193 |
0 | 194 break; |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
195 } |
0 | 196 |
197 default: | |
198 png_write_image(pimpl_->png_, &pimpl_->rows_[0]); | |
199 } | |
200 } | |
201 | |
202 png_write_end(pimpl_->png_, NULL); | |
203 } | |
204 | |
205 | |
1916 | 206 void PngWriter::WriteToFileInternal(const std::string& filename, |
207 unsigned int width, | |
208 unsigned int height, | |
209 unsigned int pitch, | |
210 PixelFormat format, | |
211 const void* buffer) | |
0 | 212 { |
213 Prepare(width, height, pitch, format, buffer); | |
214 | |
2140 | 215 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
0 | 216 if (!fp) |
217 { | |
59 | 218 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 219 } |
220 | |
221 png_init_io(pimpl_->png_, fp); | |
222 | |
223 if (setjmp(png_jmpbuf(pimpl_->png_))) | |
224 { | |
225 // Error during writing PNG | |
59 | 226 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 227 } |
228 | |
229 Compress(width, height, pitch, format); | |
230 | |
231 fclose(fp); | |
232 } | |
233 | |
234 | |
235 | |
236 static void MemoryCallback(png_structp png_ptr, | |
237 png_bytep data, | |
238 png_size_t size) | |
239 { | |
656 | 240 ChunkedBuffer* buffer = reinterpret_cast<ChunkedBuffer*>(png_get_io_ptr(png_ptr)); |
0 | 241 buffer->AddChunk(reinterpret_cast<const char*>(data), size); |
242 } | |
243 | |
244 | |
245 | |
1916 | 246 void PngWriter::WriteToMemoryInternal(std::string& png, |
247 unsigned int width, | |
248 unsigned int height, | |
249 unsigned int pitch, | |
250 PixelFormat format, | |
251 const void* buffer) | |
0 | 252 { |
253 ChunkedBuffer chunks; | |
254 | |
255 Prepare(width, height, pitch, format, buffer); | |
256 | |
257 if (setjmp(png_jmpbuf(pimpl_->png_))) | |
258 { | |
259 // Error during writing PNG | |
59 | 260 throw OrthancException(ErrorCode_InternalError); |
0 | 261 } |
262 | |
263 png_set_write_fn(pimpl_->png_, &chunks, MemoryCallback, NULL); | |
264 | |
265 Compress(width, height, pitch, format); | |
266 | |
267 chunks.Flatten(png); | |
268 } | |
269 } |