Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/PngWriter.cpp @ 5334:5b2a5cc64cb1
upgraded anonymization to Basic Profile of PS 3.15-2023b Table E.1-1
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 27 Jun 2023 15:12:39 +0200 |
parents | 0ea402b4d901 |
children | 48b8dae6dc77 |
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 |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5054
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5054
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * 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
|
9 * 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
|
10 * 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
|
11 * the License, or (at your option) any later version. |
136 | 12 * |
0 | 13 * This program is distributed in the hope that it will be useful, but |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * 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
|
16 * Lesser General Public License for more details. |
0 | 17 * |
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
|
18 * 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
|
19 * 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
|
20 * <http://www.gnu.org/licenses/>. |
0 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
800
diff
changeset
|
24 #include "../PrecompiledHeaders.h" |
0 | 25 #include "PngWriter.h" |
26 | |
27 #include <vector> | |
28 #include <stdint.h> | |
29 #include <png.h> | |
454 | 30 #include "../OrthancException.h" |
31 #include "../ChunkedBuffer.h" | |
32 #include "../Toolbox.h" | |
2171
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
33 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
34 #if ORTHANC_SANDBOXED == 0 |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
35 # include "../SystemToolbox.h" |
35febe19e874
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2170
diff
changeset
|
36 #endif |
0 | 37 |
38 | |
39 // http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-4 | |
40 // http://zarb.org/~gc/html/libpng.html | |
41 /* | |
42 void write_row_callback(png_ptr, png_uint_32 row, int pass) | |
43 { | |
44 }*/ | |
45 | |
46 | |
47 | |
48 | |
49 /* bool isError_; | |
50 | |
51 // http://www.libpng.org/pub/png/book/chapter14.html#png.ch14.div.2 | |
52 | |
53 static void ErrorHandler(png_structp png, png_const_charp message) | |
54 { | |
55 printf("** [%s]\n", message); | |
56 | |
57 PngWriter* that = (PngWriter*) png_get_error_ptr(png); | |
58 that->isError_ = true; | |
59 printf("** %d\n", (int)that); | |
60 | |
61 //((PngWriter*) payload)->isError_ = true; | |
62 } | |
63 | |
64 static void WarningHandler(png_structp png, png_const_charp message) | |
65 { | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
66 printf("++ %d\n", (int)message); |
0 | 67 }*/ |
68 | |
69 | |
59 | 70 namespace Orthanc |
0 | 71 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
72 /** |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
73 * 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
|
74 * 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
|
75 * 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
|
76 * 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
|
77 * 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
|
78 * 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
|
79 **/ |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
80 class PngWriter::Context : public boost::noncopyable |
0 | 81 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
82 private: |
0 | 83 png_structp png_; |
84 png_infop info_; | |
85 | |
86 // Filled by Prepare() | |
87 std::vector<uint8_t*> rows_; | |
88 int bitDepth_; | |
89 int colorType_; | |
90 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
91 public: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
92 Context() : |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
93 png_(NULL), |
4657 | 94 info_(NULL), |
95 bitDepth_(0), // Dummy initialization | |
96 colorType_(0) // Dummy initialization | |
0 | 97 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
98 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
|
99 (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
|
100 if (!png_) |
0 | 101 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
102 throw OrthancException(ErrorCode_NotEnoughMemory); |
639
51892be15618
fix for big endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
465
diff
changeset
|
103 } |
0 | 104 |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
105 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
|
106 if (!info_) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
107 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
108 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
|
109 throw OrthancException(ErrorCode_NotEnoughMemory); |
0 | 110 } |
111 } | |
112 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
113 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
114 ~Context() |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
115 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
116 if (info_) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
117 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
118 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
|
119 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
120 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
121 if (png_) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
122 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
123 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
|
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 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
128 png_structp GetObject() const |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
129 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
130 return png_; |
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 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
134 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
|
135 unsigned int height, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
136 unsigned int pitch, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
137 PixelFormat format, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
138 const void* buffer) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
139 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
140 rows_.resize(height); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
141 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
|
142 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
143 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
|
144 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
145 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
146 switch (format) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
147 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
148 case PixelFormat_RGB24: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
149 bitDepth_ = 8; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
150 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
|
151 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
152 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
153 case PixelFormat_RGBA32: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
154 bitDepth_ = 8; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
155 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
|
156 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
157 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
158 case PixelFormat_Grayscale8: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
159 bitDepth_ = 8; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
160 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
|
161 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
162 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
163 case PixelFormat_Grayscale16: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
164 case PixelFormat_SignedGrayscale16: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
165 bitDepth_ = 16; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
166 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
|
167 break; |
5054
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
168 |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
169 case PixelFormat_RGBA64: |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
170 bitDepth_ = 16; |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
171 colorType_ = PNG_COLOR_TYPE_RGBA; |
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
172 break; |
4642
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 default: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
175 throw OrthancException(ErrorCode_NotImplemented); |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
176 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
177 } |
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 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
180 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
|
181 unsigned int height, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
182 unsigned int pitch, |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
183 PixelFormat format) |
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 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
|
186 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
|
187 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
|
188 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
189 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
|
190 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
191 if (height > 0) |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
192 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
193 switch (format) |
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 case PixelFormat_Grayscale16: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
196 case PixelFormat_SignedGrayscale16: |
5054
255b02c68908
Added support for RGBA64 images in tools/create-dicom and /preview (Contribution from James Manners - Pliny)
Alain Mazy <am@osimis.io>
parents:
4870
diff
changeset
|
197 case PixelFormat_RGBA64: |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
198 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
199 int transforms = 0; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
200 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
|
201 { |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
202 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
|
203 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
204 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
205 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
|
206 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
|
207 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
208 break; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
209 } |
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 default: |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
212 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
|
213 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
214 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
215 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
216 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
|
217 } |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
218 }; |
0 | 219 |
220 | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
221 #if ORTHANC_SANDBOXED == 0 |
1916 | 222 void PngWriter::WriteToFileInternal(const std::string& filename, |
223 unsigned int width, | |
224 unsigned int height, | |
225 unsigned int pitch, | |
226 PixelFormat format, | |
227 const void* buffer) | |
0 | 228 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
229 Context context; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
230 |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
231 context.Prepare(width, height, pitch, format, buffer); |
0 | 232 |
2140 | 233 FILE* fp = SystemToolbox::OpenFile(filename, FileMode_WriteBinary); |
0 | 234 if (!fp) |
235 { | |
59 | 236 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 237 } |
238 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
239 png_init_io(context.GetObject(), fp); |
0 | 240 |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
241 if (setjmp(png_jmpbuf(context.GetObject()))) |
0 | 242 { |
243 // Error during writing PNG | |
59 | 244 throw OrthancException(ErrorCode_CannotWriteFile); |
0 | 245 } |
246 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
247 context.Compress(width, height, pitch, format); |
0 | 248 |
249 fclose(fp); | |
250 } | |
2170
baf8dd89b4e0
improved support for sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
251 #endif |
0 | 252 |
253 | |
254 static void MemoryCallback(png_structp png_ptr, | |
255 png_bytep data, | |
256 png_size_t size) | |
257 { | |
656 | 258 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
|
259 buffer->AddChunk(data, size); |
0 | 260 } |
261 | |
262 | |
1916 | 263 void PngWriter::WriteToMemoryInternal(std::string& png, |
264 unsigned int width, | |
265 unsigned int height, | |
266 unsigned int pitch, | |
267 PixelFormat format, | |
268 const void* buffer) | |
0 | 269 { |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
270 Context context; |
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
271 |
0 | 272 ChunkedBuffer chunks; |
273 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
274 context.Prepare(width, height, pitch, format, buffer); |
0 | 275 |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
276 if (setjmp(png_jmpbuf(context.GetObject()))) |
0 | 277 { |
278 // Error during writing PNG | |
59 | 279 throw OrthancException(ErrorCode_InternalError); |
0 | 280 } |
281 | |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
282 png_set_write_fn(context.GetObject(), &chunks, MemoryCallback, NULL); |
0 | 283 |
4642
69bbb4bd35cb
PngWriter recreates a new libpng context for each encoding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
284 context.Compress(width, height, pitch, format); |
0 | 285 |
286 chunks.Flatten(png); | |
287 } | |
288 } |