Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/PngWriter.cpp @ 4446:19e8540064d8
ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 13 Jan 2021 17:13:42 +0100 |
parents | d9473bd5ed43 |
children | 69bbb4bd35cb |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4119
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * the License, or (at your option) any later version. |
136 | 11 * |
0 | 12 * This program is distributed in the hope that it will be useful, but |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * Lesser General Public License for more details. |
0 | 16 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
0 | 20 **/ |
21 | |
22 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
23 #include "../PrecompiledHeaders.h" |
0 | 24 #include "PngWriter.h" |
25 | |
26 #include <vector> | |
27 #include <stdint.h> | |
28 #include <png.h> | |
454 | 29 #include "../OrthancException.h" |
30 #include "../ChunkedBuffer.h" | |
31 #include "../Toolbox.h" | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
32 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
33 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
34 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
35 #endif |
0 | 36 |
37 | |
38 // http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-4 | |
39 // http://zarb.org/~gc/html/libpng.html | |
40 /* | |
41 void write_row_callback(png_ptr, png_uint_32 row, int pass) | |
42 { | |
43 }*/ | |
44 | |
45 | |
46 | |
47 | |
48 /* bool isError_; | |
49 | |
50 // http://www.libpng.org/pub/png/book/chapter14.html#png.ch14.div.2 | |
51 | |
52 static void ErrorHandler(png_structp png, png_const_charp message) | |
53 { | |
54 printf("** [%s]\n", message); | |
55 | |
56 PngWriter* that = (PngWriter*) png_get_error_ptr(png); | |
57 that->isError_ = true; | |
58 printf("** %d\n", (int)that); | |
59 | |
60 //((PngWriter*) payload)->isError_ = true; | |
61 } | |
62 | |
63 static void WarningHandler(png_structp png, png_const_charp message) | |
64 { | |
65 printf("++ %d\n", (int)message); | |
66 }*/ | |
67 | |
68 | |
59 | 69 namespace Orthanc |
0 | 70 { |
71 struct PngWriter::PImpl | |
72 { | |
73 png_structp png_; | |
74 png_infop info_; | |
75 | |
76 // Filled by Prepare() | |
77 std::vector<uint8_t*> rows_; | |
78 int bitDepth_; | |
79 int colorType_; | |
80 }; | |
81 | |
82 | |
83 | |
84 PngWriter::PngWriter() : pimpl_(new PImpl) | |
85 { | |
86 pimpl_->png_ = NULL; | |
87 pimpl_->info_ = NULL; | |
88 | |
89 pimpl_->png_ = png_create_write_struct | |
90 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); //this, ErrorHandler, WarningHandler); | |
91 if (!pimpl_->png_) | |
92 { | |
59 | 93 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 94 } |
95 | |
96 pimpl_->info_ = png_create_info_struct(pimpl_->png_); | |
97 if (!pimpl_->info_) | |
98 { | |
99 png_destroy_write_struct(&pimpl_->png_, NULL); | |
59 | 100 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 101 } |
102 } | |
103 | |
104 PngWriter::~PngWriter() | |
105 { | |
106 if (pimpl_->info_) | |
107 { | |
108 png_destroy_info_struct(pimpl_->png_, &pimpl_->info_); | |
109 } | |
110 | |
111 if (pimpl_->png_) | |
112 { | |
113 png_destroy_write_struct(&pimpl_->png_, NULL); | |
114 } | |
115 } | |
116 | |
117 | |
118 | |
119 void PngWriter::Prepare(unsigned int width, | |
120 unsigned int height, | |
121 unsigned int pitch, | |
122 PixelFormat format, | |
123 const void* buffer) | |
124 { | |
125 pimpl_->rows_.resize(height); | |
126 for (unsigned int y = 0; y < height; y++) | |
127 { | |
128 pimpl_->rows_[y] = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)) + y * pitch; | |
129 } | |
130 | |
131 switch (format) | |
132 { | |
368
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
133 case PixelFormat_RGB24: |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
134 pimpl_->bitDepth_ = 8; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
135 pimpl_->colorType_ = PNG_COLOR_TYPE_RGB; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
136 break; |
80011cd589e6
support of rgb images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
136
diff
changeset
|
137 |
800
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
138 case PixelFormat_RGBA32: |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
139 pimpl_->bitDepth_ = 8; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
140 pimpl_->colorType_ = PNG_COLOR_TYPE_RGBA; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
141 break; |
ecedd89055db
generation of DICOM images from PNG files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
799
diff
changeset
|
142 |
0 | 143 case PixelFormat_Grayscale8: |
144 pimpl_->bitDepth_ = 8; | |
145 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY; | |
146 break; | |
147 | |
148 case PixelFormat_Grayscale16: | |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
454
diff
changeset
|
149 case PixelFormat_SignedGrayscale16: |
0 | 150 pimpl_->bitDepth_ = 16; |
151 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY; | |
152 break; | |
153 | |
154 default: | |
59 | 155 throw OrthancException(ErrorCode_NotImplemented); |
0 | 156 } |
157 } | |
158 | |
159 | |
160 void PngWriter::Compress(unsigned int width, | |
161 unsigned int height, | |
162 unsigned int pitch, | |
163 PixelFormat format) | |
164 { | |
165 png_set_IHDR(pimpl_->png_, pimpl_->info_, width, height, | |
166 pimpl_->bitDepth_, pimpl_->colorType_, PNG_INTERLACE_NONE, | |
167 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); | |
168 | |
169 png_write_info(pimpl_->png_, pimpl_->info_); | |
170 | |
171 if (height > 0) | |
172 { | |
173 switch (format) | |
174 { | |
175 case PixelFormat_Grayscale16: | |
465
7a966b440f19
signed images to PNG
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
454
diff
changeset
|
176 case PixelFormat_SignedGrayscale16: |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
177 { |
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
178 int transforms = 0; |
453 | 179 if (Toolbox::DetectEndianness() == Endianness_Little) |
180 { | |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
181 transforms = PNG_TRANSFORM_SWAP_ENDIAN; |
453 | 182 } |
183 | |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
184 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
|
185 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
|
186 |
0 | 187 break; |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
188 } |
0 | 189 |
190 default: | |
191 png_write_image(pimpl_->png_, &pimpl_->rows_[0]); | |
192 } | |
193 } | |
194 | |
195 png_write_end(pimpl_->png_, NULL); | |
196 } | |
197 | |
198 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
199 #if ORTHANC_SANDBOXED == 0 |
1916 | 200 void PngWriter::WriteToFileInternal(const std::string& filename, |
201 unsigned int width, | |
202 unsigned int height, | |
203 unsigned int pitch, | |
204 PixelFormat format, | |
205 const void* buffer) | |
0 | 206 { |
207 Prepare(width, height, pitch, format, buffer); | |
208 | |
2140 | 209 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
0 | 210 if (!fp) |
211 { | |
59 | 212 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 213 } |
214 | |
215 png_init_io(pimpl_->png_, fp); | |
216 | |
217 if (setjmp(png_jmpbuf(pimpl_->png_))) | |
218 { | |
219 // Error during writing PNG | |
59 | 220 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 221 } |
222 | |
223 Compress(width, height, pitch, format); | |
224 | |
225 fclose(fp); | |
226 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
227 #endif |
0 | 228 |
229 | |
230 static void MemoryCallback(png_structp png_ptr, | |
231 png_bytep data, | |
232 png_size_t size) | |
233 { | |
656 | 234 ChunkedBuffer* buffer = reinterpret_cast<ChunkedBuffer*>(png_get_io_ptr(png_ptr)); |
3402
cf31b5bacce3
removing unneeded casts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
235 buffer->AddChunk(data, size); |
0 | 236 } |
237 | |
238 | |
239 | |
1916 | 240 void PngWriter::WriteToMemoryInternal(std::string& png, |
241 unsigned int width, | |
242 unsigned int height, | |
243 unsigned int pitch, | |
244 PixelFormat format, | |
245 const void* buffer) | |
0 | 246 { |
247 ChunkedBuffer chunks; | |
248 | |
249 Prepare(width, height, pitch, format, buffer); | |
250 | |
251 if (setjmp(png_jmpbuf(pimpl_->png_))) | |
252 { | |
253 // Error during writing PNG | |
59 | 254 throw OrthancException(ErrorCode_InternalError); |
0 | 255 } |
256 | |
257 png_set_write_fn(pimpl_->png_, &chunks, MemoryCallback, NULL); | |
258 | |
259 Compress(width, height, pitch, format); | |
260 | |
261 chunks.Flatten(png); | |
262 } | |
263 } |