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