comparison Core/Images/PngWriter.cpp @ 1612:96582230ddcb

Core/ImageFormats folder renamed as Core/Images
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Sep 2015 16:36:18 +0200
parents Core/ImageFormats/PngWriter.cpp@6e7e5ed91c2d
children b1291df2f780
comparison
equal deleted inserted replaced
1611:5e9b2aac8b89 1612:96582230ddcb
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, 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.
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.
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 "../PrecompiledHeaders.h"
34 #include "PngWriter.h"
35
36 #include <vector>
37 #include <stdint.h>
38 #include <png.h>
39 #include "../OrthancException.h"
40 #include "../ChunkedBuffer.h"
41 #include "../Toolbox.h"
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
75 namespace Orthanc
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 {
99 throw OrthancException(ErrorCode_NotEnoughMemory);
100 }
101
102 pimpl_->info_ = png_create_info_struct(pimpl_->png_);
103 if (!pimpl_->info_)
104 {
105 png_destroy_write_struct(&pimpl_->png_, NULL);
106 throw OrthancException(ErrorCode_NotEnoughMemory);
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 {
139 case PixelFormat_RGB24:
140 pimpl_->bitDepth_ = 8;
141 pimpl_->colorType_ = PNG_COLOR_TYPE_RGB;
142 break;
143
144 case PixelFormat_RGBA32:
145 pimpl_->bitDepth_ = 8;
146 pimpl_->colorType_ = PNG_COLOR_TYPE_RGBA;
147 break;
148
149 case PixelFormat_Grayscale8:
150 pimpl_->bitDepth_ = 8;
151 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY;
152 break;
153
154 case PixelFormat_Grayscale16:
155 case PixelFormat_SignedGrayscale16:
156 pimpl_->bitDepth_ = 16;
157 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY;
158 break;
159
160 default:
161 throw OrthancException(ErrorCode_NotImplemented);
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:
182 case PixelFormat_SignedGrayscale16:
183 {
184 int transforms = 0;
185 if (Toolbox::DetectEndianness() == Endianness_Little)
186 {
187 transforms = PNG_TRANSFORM_SWAP_ENDIAN;
188 }
189
190 png_set_rows(pimpl_->png_, pimpl_->info_, &pimpl_->rows_[0]);
191 png_write_png(pimpl_->png_, pimpl_->info_, transforms, NULL);
192
193 break;
194 }
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 {
217 throw OrthancException(ErrorCode_CannotWriteFile);
218 }
219
220 png_init_io(pimpl_->png_, fp);
221
222 if (setjmp(png_jmpbuf(pimpl_->png_)))
223 {
224 // Error during writing PNG
225 throw OrthancException(ErrorCode_CannotWriteFile);
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 {
240 ChunkedBuffer* buffer = reinterpret_cast<ChunkedBuffer*>(png_get_io_ptr(png_ptr));
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
260 throw OrthancException(ErrorCode_InternalError);
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 }