0
|
1 /**
|
59
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
0
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
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 {
|
|
137 case PixelFormat_Grayscale8:
|
|
138 pimpl_->bitDepth_ = 8;
|
|
139 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY;
|
|
140 break;
|
|
141
|
|
142 case PixelFormat_Grayscale16:
|
|
143 pimpl_->bitDepth_ = 16;
|
|
144 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY;
|
|
145 break;
|
|
146
|
|
147 default:
|
59
|
148 throw OrthancException(ErrorCode_NotImplemented);
|
0
|
149 }
|
|
150 }
|
|
151
|
|
152
|
|
153 void PngWriter::Compress(unsigned int width,
|
|
154 unsigned int height,
|
|
155 unsigned int pitch,
|
|
156 PixelFormat format)
|
|
157 {
|
|
158 png_set_IHDR(pimpl_->png_, pimpl_->info_, width, height,
|
|
159 pimpl_->bitDepth_, pimpl_->colorType_, PNG_INTERLACE_NONE,
|
|
160 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
161
|
|
162 png_write_info(pimpl_->png_, pimpl_->info_);
|
|
163
|
|
164 if (height > 0)
|
|
165 {
|
|
166 switch (format)
|
|
167 {
|
|
168 case PixelFormat_Grayscale16:
|
|
169 // Must swap the endianness!!
|
|
170 png_set_rows(pimpl_->png_, pimpl_->info_, &pimpl_->rows_[0]);
|
|
171 png_write_png(pimpl_->png_, pimpl_->info_, PNG_TRANSFORM_SWAP_ENDIAN, NULL);
|
|
172 break;
|
|
173
|
|
174 default:
|
|
175 png_write_image(pimpl_->png_, &pimpl_->rows_[0]);
|
|
176 }
|
|
177 }
|
|
178
|
|
179 png_write_end(pimpl_->png_, NULL);
|
|
180 }
|
|
181
|
|
182
|
|
183 void PngWriter::WriteToFile(const char* filename,
|
|
184 unsigned int width,
|
|
185 unsigned int height,
|
|
186 unsigned int pitch,
|
|
187 PixelFormat format,
|
|
188 const void* buffer)
|
|
189 {
|
|
190 Prepare(width, height, pitch, format, buffer);
|
|
191
|
|
192 FILE* fp = fopen(filename, "wb");
|
|
193 if (!fp)
|
|
194 {
|
59
|
195 throw OrthancException(ErrorCode_CannotWriteFile);
|
0
|
196 }
|
|
197
|
|
198 png_init_io(pimpl_->png_, fp);
|
|
199
|
|
200 if (setjmp(png_jmpbuf(pimpl_->png_)))
|
|
201 {
|
|
202 // Error during writing PNG
|
59
|
203 throw OrthancException(ErrorCode_CannotWriteFile);
|
0
|
204 }
|
|
205
|
|
206 Compress(width, height, pitch, format);
|
|
207
|
|
208 fclose(fp);
|
|
209 }
|
|
210
|
|
211
|
|
212
|
|
213
|
|
214 static void MemoryCallback(png_structp png_ptr,
|
|
215 png_bytep data,
|
|
216 png_size_t size)
|
|
217 {
|
|
218 ChunkedBuffer* buffer = (ChunkedBuffer*) png_get_io_ptr(png_ptr);
|
|
219 buffer->AddChunk(reinterpret_cast<const char*>(data), size);
|
|
220 }
|
|
221
|
|
222
|
|
223
|
|
224 void PngWriter::WriteToMemory(std::string& png,
|
|
225 unsigned int width,
|
|
226 unsigned int height,
|
|
227 unsigned int pitch,
|
|
228 PixelFormat format,
|
|
229 const void* buffer)
|
|
230 {
|
|
231 ChunkedBuffer chunks;
|
|
232
|
|
233 Prepare(width, height, pitch, format, buffer);
|
|
234
|
|
235 if (setjmp(png_jmpbuf(pimpl_->png_)))
|
|
236 {
|
|
237 // Error during writing PNG
|
59
|
238 throw OrthancException(ErrorCode_InternalError);
|
0
|
239 }
|
|
240
|
|
241 png_set_write_fn(pimpl_->png_, &chunks, MemoryCallback, NULL);
|
|
242
|
|
243 Compress(width, height, pitch, format);
|
|
244
|
|
245 chunks.Flatten(png);
|
|
246 }
|
|
247 }
|