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