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.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 **/
|
|
19
|
|
20
|
|
21 #include "PngWriter.h"
|
|
22
|
|
23 #include <vector>
|
|
24 #include <stdint.h>
|
|
25 #include <png.h>
|
59
|
26 #include "OrthancException.h"
|
0
|
27 #include "ChunkedBuffer.h"
|
|
28
|
|
29
|
|
30 // http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-4
|
|
31 // http://zarb.org/~gc/html/libpng.html
|
|
32 /*
|
|
33 void write_row_callback(png_ptr, png_uint_32 row, int pass)
|
|
34 {
|
|
35 }*/
|
|
36
|
|
37
|
|
38
|
|
39
|
|
40 /* bool isError_;
|
|
41
|
|
42 // http://www.libpng.org/pub/png/book/chapter14.html#png.ch14.div.2
|
|
43
|
|
44 static void ErrorHandler(png_structp png, png_const_charp message)
|
|
45 {
|
|
46 printf("** [%s]\n", message);
|
|
47
|
|
48 PngWriter* that = (PngWriter*) png_get_error_ptr(png);
|
|
49 that->isError_ = true;
|
|
50 printf("** %d\n", (int)that);
|
|
51
|
|
52 //((PngWriter*) payload)->isError_ = true;
|
|
53 }
|
|
54
|
|
55 static void WarningHandler(png_structp png, png_const_charp message)
|
|
56 {
|
|
57 printf("++ %d\n", (int)message);
|
|
58 }*/
|
|
59
|
|
60
|
59
|
61 namespace Orthanc
|
0
|
62 {
|
|
63 struct PngWriter::PImpl
|
|
64 {
|
|
65 png_structp png_;
|
|
66 png_infop info_;
|
|
67
|
|
68 // Filled by Prepare()
|
|
69 std::vector<uint8_t*> rows_;
|
|
70 int bitDepth_;
|
|
71 int colorType_;
|
|
72 };
|
|
73
|
|
74
|
|
75
|
|
76 PngWriter::PngWriter() : pimpl_(new PImpl)
|
|
77 {
|
|
78 pimpl_->png_ = NULL;
|
|
79 pimpl_->info_ = NULL;
|
|
80
|
|
81 pimpl_->png_ = png_create_write_struct
|
|
82 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); //this, ErrorHandler, WarningHandler);
|
|
83 if (!pimpl_->png_)
|
|
84 {
|
59
|
85 throw OrthancException(ErrorCode_NotEnoughMemory);
|
0
|
86 }
|
|
87
|
|
88 pimpl_->info_ = png_create_info_struct(pimpl_->png_);
|
|
89 if (!pimpl_->info_)
|
|
90 {
|
|
91 png_destroy_write_struct(&pimpl_->png_, NULL);
|
59
|
92 throw OrthancException(ErrorCode_NotEnoughMemory);
|
0
|
93 }
|
|
94 }
|
|
95
|
|
96 PngWriter::~PngWriter()
|
|
97 {
|
|
98 if (pimpl_->info_)
|
|
99 {
|
|
100 png_destroy_info_struct(pimpl_->png_, &pimpl_->info_);
|
|
101 }
|
|
102
|
|
103 if (pimpl_->png_)
|
|
104 {
|
|
105 png_destroy_write_struct(&pimpl_->png_, NULL);
|
|
106 }
|
|
107 }
|
|
108
|
|
109
|
|
110
|
|
111 void PngWriter::Prepare(unsigned int width,
|
|
112 unsigned int height,
|
|
113 unsigned int pitch,
|
|
114 PixelFormat format,
|
|
115 const void* buffer)
|
|
116 {
|
|
117 pimpl_->rows_.resize(height);
|
|
118 for (unsigned int y = 0; y < height; y++)
|
|
119 {
|
|
120 pimpl_->rows_[y] = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)) + y * pitch;
|
|
121 }
|
|
122
|
|
123 switch (format)
|
|
124 {
|
|
125 case PixelFormat_Grayscale8:
|
|
126 pimpl_->bitDepth_ = 8;
|
|
127 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY;
|
|
128 break;
|
|
129
|
|
130 case PixelFormat_Grayscale16:
|
|
131 pimpl_->bitDepth_ = 16;
|
|
132 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY;
|
|
133 break;
|
|
134
|
|
135 default:
|
59
|
136 throw OrthancException(ErrorCode_NotImplemented);
|
0
|
137 }
|
|
138 }
|
|
139
|
|
140
|
|
141 void PngWriter::Compress(unsigned int width,
|
|
142 unsigned int height,
|
|
143 unsigned int pitch,
|
|
144 PixelFormat format)
|
|
145 {
|
|
146 png_set_IHDR(pimpl_->png_, pimpl_->info_, width, height,
|
|
147 pimpl_->bitDepth_, pimpl_->colorType_, PNG_INTERLACE_NONE,
|
|
148 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
149
|
|
150 png_write_info(pimpl_->png_, pimpl_->info_);
|
|
151
|
|
152 if (height > 0)
|
|
153 {
|
|
154 switch (format)
|
|
155 {
|
|
156 case PixelFormat_Grayscale16:
|
|
157 // Must swap the endianness!!
|
|
158 png_set_rows(pimpl_->png_, pimpl_->info_, &pimpl_->rows_[0]);
|
|
159 png_write_png(pimpl_->png_, pimpl_->info_, PNG_TRANSFORM_SWAP_ENDIAN, NULL);
|
|
160 break;
|
|
161
|
|
162 default:
|
|
163 png_write_image(pimpl_->png_, &pimpl_->rows_[0]);
|
|
164 }
|
|
165 }
|
|
166
|
|
167 png_write_end(pimpl_->png_, NULL);
|
|
168 }
|
|
169
|
|
170
|
|
171 void PngWriter::WriteToFile(const char* filename,
|
|
172 unsigned int width,
|
|
173 unsigned int height,
|
|
174 unsigned int pitch,
|
|
175 PixelFormat format,
|
|
176 const void* buffer)
|
|
177 {
|
|
178 Prepare(width, height, pitch, format, buffer);
|
|
179
|
|
180 FILE* fp = fopen(filename, "wb");
|
|
181 if (!fp)
|
|
182 {
|
59
|
183 throw OrthancException(ErrorCode_CannotWriteFile);
|
0
|
184 }
|
|
185
|
|
186 png_init_io(pimpl_->png_, fp);
|
|
187
|
|
188 if (setjmp(png_jmpbuf(pimpl_->png_)))
|
|
189 {
|
|
190 // Error during writing PNG
|
59
|
191 throw OrthancException(ErrorCode_CannotWriteFile);
|
0
|
192 }
|
|
193
|
|
194 Compress(width, height, pitch, format);
|
|
195
|
|
196 fclose(fp);
|
|
197 }
|
|
198
|
|
199
|
|
200
|
|
201
|
|
202 static void MemoryCallback(png_structp png_ptr,
|
|
203 png_bytep data,
|
|
204 png_size_t size)
|
|
205 {
|
|
206 ChunkedBuffer* buffer = (ChunkedBuffer*) png_get_io_ptr(png_ptr);
|
|
207 buffer->AddChunk(reinterpret_cast<const char*>(data), size);
|
|
208 }
|
|
209
|
|
210
|
|
211
|
|
212 void PngWriter::WriteToMemory(std::string& png,
|
|
213 unsigned int width,
|
|
214 unsigned int height,
|
|
215 unsigned int pitch,
|
|
216 PixelFormat format,
|
|
217 const void* buffer)
|
|
218 {
|
|
219 ChunkedBuffer chunks;
|
|
220
|
|
221 Prepare(width, height, pitch, format, buffer);
|
|
222
|
|
223 if (setjmp(png_jmpbuf(pimpl_->png_)))
|
|
224 {
|
|
225 // Error during writing PNG
|
59
|
226 throw OrthancException(ErrorCode_InternalError);
|
0
|
227 }
|
|
228
|
|
229 png_set_write_fn(pimpl_->png_, &chunks, MemoryCallback, NULL);
|
|
230
|
|
231 Compress(width, height, pitch, format);
|
|
232
|
|
233 chunks.Flatten(png);
|
|
234 }
|
|
235 }
|