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