Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/PngWriter.cpp @ 4664:f9eda86f9045
ImageProcessing::Convert() from Float32 to RGB24
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 May 2021 11:46:57 +0200 |
parents | e8967149d87a |
children | 7053502fbf97 |
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 { | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
65 printf("++ %d\n", (int)message); |
0 | 66 }*/ |
67 | |
68 | |
59 | 69 namespace Orthanc |
0 | 70 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
71 /** |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
72 * The "png_" structure cannot be safely reused if the bpp changes |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
73 * between successive invocations. This can lead to "Invalid reads" |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
74 * reported by valgrind if writing a 16bpp image, then a 8bpp image |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
75 * using the same "PngWriter" object. Starting with Orthanc 1.9.3, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
76 * we recreate a new "png_" context each time a PNG image must be |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
77 * written so as to prevent such invalid reads. |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
78 **/ |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
79 class PngWriter::Context : public boost::noncopyable |
0 | 80 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
81 private: |
0 | 82 png_structp png_; |
83 png_infop info_; | |
84 | |
85 // Filled by Prepare() | |
86 std::vector<uint8_t*> rows_; | |
87 int bitDepth_; | |
88 int colorType_; | |
89 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
90 public: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
91 Context() : |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
92 png_(NULL), |
4657 | 93 info_(NULL), |
94 bitDepth_(0), // Dummy initialization | |
95 colorType_(0) // Dummy initialization | |
0 | 96 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
97 png_ = png_create_write_struct |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
98 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); //this, ErrorHandler, WarningHandler); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
99 if (!png_) |
0 | 100 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
101 throw OrthancException(ErrorCode_NotEnoughMemory); |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
102 } |
0 | 103 |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
104 info_ = png_create_info_struct(png_); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
105 if (!info_) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
106 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
107 png_destroy_write_struct(&png_, NULL); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
108 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 109 } |
110 } | |
111 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
112 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
113 ~Context() |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
114 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
115 if (info_) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
116 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
117 png_destroy_info_struct(png_, &info_); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
118 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
119 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
120 if (png_) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
121 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
122 png_destroy_write_struct(&png_, NULL); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
123 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
124 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
125 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
126 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
127 png_structp GetObject() const |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
128 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
129 return png_; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
130 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
131 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
132 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
133 void Prepare(unsigned int width, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
134 unsigned int height, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
135 unsigned int pitch, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
136 PixelFormat format, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
137 const void* buffer) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
138 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
139 rows_.resize(height); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
140 for (unsigned int y = 0; y < height; y++) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
141 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
142 rows_[y] = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)) + y * pitch; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
143 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
144 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
145 switch (format) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
146 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
147 case PixelFormat_RGB24: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
148 bitDepth_ = 8; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
149 colorType_ = PNG_COLOR_TYPE_RGB; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
150 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
151 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
152 case PixelFormat_RGBA32: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
153 bitDepth_ = 8; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
154 colorType_ = PNG_COLOR_TYPE_RGBA; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
155 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
156 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
157 case PixelFormat_Grayscale8: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
158 bitDepth_ = 8; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
159 colorType_ = PNG_COLOR_TYPE_GRAY; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
160 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
161 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
162 case PixelFormat_Grayscale16: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
163 case PixelFormat_SignedGrayscale16: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
164 bitDepth_ = 16; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
165 colorType_ = PNG_COLOR_TYPE_GRAY; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
166 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
167 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
168 default: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
169 throw OrthancException(ErrorCode_NotImplemented); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
170 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
171 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
172 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
173 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
174 void Compress(unsigned int width, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
175 unsigned int height, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
176 unsigned int pitch, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
177 PixelFormat format) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
178 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
179 png_set_IHDR(png_, info_, width, height, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
180 bitDepth_, colorType_, PNG_INTERLACE_NONE, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
181 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
182 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
183 png_write_info(png_, info_); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
184 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
185 if (height > 0) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
186 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
187 switch (format) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
188 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
189 case PixelFormat_Grayscale16: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
190 case PixelFormat_SignedGrayscale16: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
191 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
192 int transforms = 0; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
193 if (Toolbox::DetectEndianness() == Endianness_Little) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
194 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
195 transforms = PNG_TRANSFORM_SWAP_ENDIAN; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
196 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
197 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
198 png_set_rows(png_, info_, &rows_[0]); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
199 png_write_png(png_, info_, transforms, NULL); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
200 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
201 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
202 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
203 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
204 default: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
205 png_write_image(png_, &rows_[0]); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
206 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
207 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
208 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
209 png_write_end(png_, NULL); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
210 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
211 }; |
0 | 212 |
213 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
214 #if ORTHANC_SANDBOXED == 0 |
1916 | 215 void PngWriter::WriteToFileInternal(const std::string& filename, |
216 unsigned int width, | |
217 unsigned int height, | |
218 unsigned int pitch, | |
219 PixelFormat format, | |
220 const void* buffer) | |
0 | 221 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
222 Context context; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
223 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
224 context.Prepare(width, height, pitch, format, buffer); |
0 | 225 |
2140 | 226 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
0 | 227 if (!fp) |
228 { | |
59 | 229 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 230 } |
231 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
232 png_init_io(context.GetObject(), fp); |
0 | 233 |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
234 if (setjmp(png_jmpbuf(context.GetObject()))) |
0 | 235 { |
236 // Error during writing PNG | |
59 | 237 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 238 } |
239 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
240 context.Compress(width, height, pitch, format); |
0 | 241 |
242 fclose(fp); | |
243 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
244 #endif |
0 | 245 |
246 | |
247 static void MemoryCallback(png_structp png_ptr, | |
248 png_bytep data, | |
249 png_size_t size) | |
250 { | |
656 | 251 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
|
252 buffer->AddChunk(data, size); |
0 | 253 } |
254 | |
255 | |
1916 | 256 void PngWriter::WriteToMemoryInternal(std::string& png, |
257 unsigned int width, | |
258 unsigned int height, | |
259 unsigned int pitch, | |
260 PixelFormat format, | |
261 const void* buffer) | |
0 | 262 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
263 Context context; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
264 |
0 | 265 ChunkedBuffer chunks; |
266 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
267 context.Prepare(width, height, pitch, format, buffer); |
0 | 268 |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
269 if (setjmp(png_jmpbuf(context.GetObject()))) |
0 | 270 { |
271 // Error during writing PNG | |
59 | 272 throw OrthancException(ErrorCode_InternalError); |
0 | 273 } |
274 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
275 png_set_write_fn(context.GetObject(), &chunks, MemoryCallback, NULL); |
0 | 276 |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
277 context.Compress(width, height, pitch, format); |
0 | 278 |
279 chunks.Flatten(png); | |
280 } | |
281 } |