annotate Orthanc/ImageFormats/PngWriter.cpp @ 15:51b727a268f4

do not remove the preview button
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Apr 2015 15:07:42 +0200
parents 02f7a0400a91
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "../PrecompiledHeaders.h"
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "PngWriter.h"
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include <vector>
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <stdint.h>
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include <png.h>
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include "../OrthancException.h"
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include "../ChunkedBuffer.h"
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include "../Toolbox.h"
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 // http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-4
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 // http://zarb.org/~gc/html/libpng.html
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 /*
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 void write_row_callback(png_ptr, png_uint_32 row, int pass)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 }*/
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 /* bool isError_;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 // http://www.libpng.org/pub/png/book/chapter14.html#png.ch14.div.2
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 static void ErrorHandler(png_structp png, png_const_charp message)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 printf("** [%s]\n", message);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 PngWriter* that = (PngWriter*) png_get_error_ptr(png);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 that->isError_ = true;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 printf("** %d\n", (int)that);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 //((PngWriter*) payload)->isError_ = true;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 static void WarningHandler(png_structp png, png_const_charp message)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 printf("++ %d\n", (int)message);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }*/
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 namespace Orthanc
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 struct PngWriter::PImpl
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 png_structp png_;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 png_infop info_;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 // Filled by Prepare()
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 std::vector<uint8_t*> rows_;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 int bitDepth_;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 int colorType_;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 };
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 PngWriter::PngWriter() : pimpl_(new PImpl)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 pimpl_->png_ = NULL;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 pimpl_->info_ = NULL;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 pimpl_->png_ = png_create_write_struct
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); //this, ErrorHandler, WarningHandler);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if (!pimpl_->png_)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 throw OrthancException(ErrorCode_NotEnoughMemory);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 pimpl_->info_ = png_create_info_struct(pimpl_->png_);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 if (!pimpl_->info_)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 png_destroy_write_struct(&pimpl_->png_, NULL);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 throw OrthancException(ErrorCode_NotEnoughMemory);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 PngWriter::~PngWriter()
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 if (pimpl_->info_)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 png_destroy_info_struct(pimpl_->png_, &pimpl_->info_);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 if (pimpl_->png_)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 png_destroy_write_struct(&pimpl_->png_, NULL);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 void PngWriter::Prepare(unsigned int width,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 unsigned int height,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 unsigned int pitch,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 PixelFormat format,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 const void* buffer)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 pimpl_->rows_.resize(height);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 for (unsigned int y = 0; y < height; y++)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 pimpl_->rows_[y] = const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(buffer)) + y * pitch;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 switch (format)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 case PixelFormat_RGB24:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 pimpl_->bitDepth_ = 8;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 pimpl_->colorType_ = PNG_COLOR_TYPE_RGB;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 break;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 case PixelFormat_RGBA32:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 pimpl_->bitDepth_ = 8;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 pimpl_->colorType_ = PNG_COLOR_TYPE_RGBA;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 break;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 case PixelFormat_Grayscale8:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 pimpl_->bitDepth_ = 8;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 break;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 case PixelFormat_Grayscale16:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 case PixelFormat_SignedGrayscale16:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 pimpl_->bitDepth_ = 16;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 pimpl_->colorType_ = PNG_COLOR_TYPE_GRAY;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 break;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 default:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 throw OrthancException(ErrorCode_NotImplemented);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 void PngWriter::Compress(unsigned int width,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 unsigned int height,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 unsigned int pitch,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 PixelFormat format)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 png_set_IHDR(pimpl_->png_, pimpl_->info_, width, height,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 pimpl_->bitDepth_, pimpl_->colorType_, PNG_INTERLACE_NONE,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 png_write_info(pimpl_->png_, pimpl_->info_);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 if (height > 0)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 switch (format)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 case PixelFormat_Grayscale16:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 case PixelFormat_SignedGrayscale16:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 int transforms = 0;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 if (Toolbox::DetectEndianness() == Endianness_Little)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 transforms = PNG_TRANSFORM_SWAP_ENDIAN;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 png_set_rows(pimpl_->png_, pimpl_->info_, &pimpl_->rows_[0]);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 png_write_png(pimpl_->png_, pimpl_->info_, transforms, NULL);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 break;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 default:
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 png_write_image(pimpl_->png_, &pimpl_->rows_[0]);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 png_write_end(pimpl_->png_, NULL);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 void PngWriter::WriteToFile(const char* filename,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 unsigned int width,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 unsigned int height,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 unsigned int pitch,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 PixelFormat format,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 const void* buffer)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 Prepare(width, height, pitch, format, buffer);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 FILE* fp = fopen(filename, "wb");
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 if (!fp)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 throw OrthancException(ErrorCode_CannotWriteFile);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 png_init_io(pimpl_->png_, fp);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 if (setjmp(png_jmpbuf(pimpl_->png_)))
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 // Error during writing PNG
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 throw OrthancException(ErrorCode_CannotWriteFile);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 Compress(width, height, pitch, format);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 fclose(fp);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 static void MemoryCallback(png_structp png_ptr,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 png_bytep data,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 png_size_t size)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 ChunkedBuffer* buffer = reinterpret_cast<ChunkedBuffer*>(png_get_io_ptr(png_ptr));
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 buffer->AddChunk(reinterpret_cast<const char*>(data), size);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 void PngWriter::WriteToMemory(std::string& png,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 unsigned int width,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 unsigned int height,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 unsigned int pitch,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 PixelFormat format,
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 const void* buffer)
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 ChunkedBuffer chunks;
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 Prepare(width, height, pitch, format, buffer);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 if (setjmp(png_jmpbuf(pimpl_->png_)))
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 {
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 // Error during writing PNG
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 throw OrthancException(ErrorCode_InternalError);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 png_set_write_fn(pimpl_->png_, &chunks, MemoryCallback, NULL);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 Compress(width, height, pitch, format);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 chunks.Flatten(png);
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 }
02f7a0400a91 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 }