1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "private/qpnghandler_p.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_IMAGEFORMAT_PNG
|
---|
45 | #include <qcoreapplication.h>
|
---|
46 | #include <qiodevice.h>
|
---|
47 | #include <qimage.h>
|
---|
48 | #include <qlist.h>
|
---|
49 | #include <qtextcodec.h>
|
---|
50 | #include <qvariant.h>
|
---|
51 | #include <qvector.h>
|
---|
52 |
|
---|
53 | #include <png.h>
|
---|
54 | #include <pngconf.h>
|
---|
55 |
|
---|
56 | #ifdef Q_OS_WINCE
|
---|
57 | #define CALLBACK_CALL_TYPE __cdecl
|
---|
58 | #else
|
---|
59 | #define CALLBACK_CALL_TYPE
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | QT_BEGIN_NAMESPACE
|
---|
63 |
|
---|
64 | #if defined(Q_OS_WINCE) && defined(STANDARDSHELL_UI_MODEL)
|
---|
65 | # define Q_INTERNAL_WIN_NO_THROW __declspec(nothrow)
|
---|
66 | #else
|
---|
67 | # define Q_INTERNAL_WIN_NO_THROW
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | /*
|
---|
71 | All PNG files load to the minimal QImage equivalent.
|
---|
72 |
|
---|
73 | All QImage formats output to reasonably efficient PNG equivalents.
|
---|
74 | Never to grayscale.
|
---|
75 | */
|
---|
76 |
|
---|
77 | #if defined(Q_C_CALLBACKS)
|
---|
78 | extern "C" {
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | class QPNGImageWriter {
|
---|
82 | public:
|
---|
83 | explicit QPNGImageWriter(QIODevice*);
|
---|
84 | ~QPNGImageWriter();
|
---|
85 |
|
---|
86 | enum DisposalMethod { Unspecified, NoDisposal, RestoreBackground, RestoreImage };
|
---|
87 | void setDisposalMethod(DisposalMethod);
|
---|
88 | void setLooping(int loops=0); // 0 == infinity
|
---|
89 | void setFrameDelay(int msecs);
|
---|
90 | void setGamma(float);
|
---|
91 |
|
---|
92 | bool writeImage(const QImage& img, int x, int y);
|
---|
93 | bool writeImage(const QImage& img, int quality, const QString &description, int x, int y);
|
---|
94 | bool writeImage(const QImage& img)
|
---|
95 | { return writeImage(img, 0, 0); }
|
---|
96 | bool writeImage(const QImage& img, int quality, const QString &description)
|
---|
97 | { return writeImage(img, quality, description, 0, 0); }
|
---|
98 |
|
---|
99 | QIODevice* device() { return dev; }
|
---|
100 |
|
---|
101 | private:
|
---|
102 | QIODevice* dev;
|
---|
103 | int frames_written;
|
---|
104 | DisposalMethod disposal;
|
---|
105 | int looping;
|
---|
106 | int ms_delay;
|
---|
107 | float gamma;
|
---|
108 | };
|
---|
109 |
|
---|
110 | static
|
---|
111 | void CALLBACK_CALL_TYPE iod_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
|
---|
112 | {
|
---|
113 | QIODevice *in = (QIODevice *)png_get_io_ptr(png_ptr);
|
---|
114 |
|
---|
115 | while (length) {
|
---|
116 | int nr = in->read((char*)data, length);
|
---|
117 | if (nr <= 0) {
|
---|
118 | png_error(png_ptr, "Read Error");
|
---|
119 | return;
|
---|
120 | }
|
---|
121 | length -= nr;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | static
|
---|
127 | void CALLBACK_CALL_TYPE qpiw_write_fn(png_structp png_ptr, png_bytep data, png_size_t length)
|
---|
128 | {
|
---|
129 | QPNGImageWriter* qpiw = (QPNGImageWriter*)png_get_io_ptr(png_ptr);
|
---|
130 | QIODevice* out = qpiw->device();
|
---|
131 |
|
---|
132 | uint nr = out->write((char*)data, length);
|
---|
133 | if (nr != length) {
|
---|
134 | png_error(png_ptr, "Write Error");
|
---|
135 | return;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | static
|
---|
141 | void CALLBACK_CALL_TYPE qpiw_flush_fn(png_structp /* png_ptr */)
|
---|
142 | {
|
---|
143 | }
|
---|
144 |
|
---|
145 | #if defined(Q_C_CALLBACKS)
|
---|
146 | }
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | static
|
---|
150 | void setup_qt(QImage& image, png_structp png_ptr, png_infop info_ptr, float screen_gamma=0.0)
|
---|
151 | {
|
---|
152 | if (screen_gamma != 0.0 && png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
|
---|
153 | double file_gamma;
|
---|
154 | png_get_gAMA(png_ptr, info_ptr, &file_gamma);
|
---|
155 | png_set_gamma(png_ptr, screen_gamma, file_gamma);
|
---|
156 | }
|
---|
157 |
|
---|
158 | png_uint_32 width;
|
---|
159 | png_uint_32 height;
|
---|
160 | int bit_depth;
|
---|
161 | int color_type;
|
---|
162 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
|
---|
163 |
|
---|
164 | if (color_type == PNG_COLOR_TYPE_GRAY) {
|
---|
165 | // Black & White or 8-bit grayscale
|
---|
166 | if (bit_depth == 1 && info_ptr->channels == 1) {
|
---|
167 | png_set_invert_mono(png_ptr);
|
---|
168 | png_read_update_info(png_ptr, info_ptr);
|
---|
169 | if (image.size() != QSize(width, height) || image.format() != QImage::Format_Mono) {
|
---|
170 | image = QImage(width, height, QImage::Format_Mono);
|
---|
171 | if (image.isNull())
|
---|
172 | return;
|
---|
173 | }
|
---|
174 | image.setNumColors(2);
|
---|
175 | image.setColor(1, qRgb(0,0,0));
|
---|
176 | image.setColor(0, qRgb(255,255,255));
|
---|
177 | } else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
---|
178 | png_set_expand(png_ptr);
|
---|
179 | png_set_strip_16(png_ptr);
|
---|
180 | png_set_gray_to_rgb(png_ptr);
|
---|
181 | if (image.size() != QSize(width, height) || image.format() != QImage::Format_ARGB32) {
|
---|
182 | image = QImage(width, height, QImage::Format_ARGB32);
|
---|
183 | if (image.isNull())
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
|
---|
187 | png_set_swap_alpha(png_ptr);
|
---|
188 |
|
---|
189 | png_read_update_info(png_ptr, info_ptr);
|
---|
190 | } else {
|
---|
191 | if (bit_depth == 16)
|
---|
192 | png_set_strip_16(png_ptr);
|
---|
193 | else if (bit_depth < 8)
|
---|
194 | png_set_packing(png_ptr);
|
---|
195 | int ncols = bit_depth < 8 ? 1 << bit_depth : 256;
|
---|
196 | png_read_update_info(png_ptr, info_ptr);
|
---|
197 | if (image.size() != QSize(width, height) || image.format() != QImage::Format_Indexed8) {
|
---|
198 | image = QImage(width, height, QImage::Format_Indexed8);
|
---|
199 | if (image.isNull())
|
---|
200 | return;
|
---|
201 | }
|
---|
202 | image.setNumColors(ncols);
|
---|
203 | for (int i=0; i<ncols; i++) {
|
---|
204 | int c = i*255/(ncols-1);
|
---|
205 | image.setColor(i, qRgba(c,c,c,0xff));
|
---|
206 | }
|
---|
207 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
---|
208 | const int g = info_ptr->trans_values.gray;
|
---|
209 | if (g < ncols) {
|
---|
210 | image.setColor(g, 0);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 | } else if (color_type == PNG_COLOR_TYPE_PALETTE
|
---|
215 | && png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)
|
---|
216 | && info_ptr->num_palette <= 256)
|
---|
217 | {
|
---|
218 | // 1-bit and 8-bit color
|
---|
219 | if (bit_depth != 1)
|
---|
220 | png_set_packing(png_ptr);
|
---|
221 | png_read_update_info(png_ptr, info_ptr);
|
---|
222 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
|
---|
223 | QImage::Format format = bit_depth == 1 ? QImage::Format_Mono : QImage::Format_Indexed8;
|
---|
224 | if (image.size() != QSize(width, height) || image.format() != format) {
|
---|
225 | image = QImage(width, height, format);
|
---|
226 | if (image.isNull())
|
---|
227 | return;
|
---|
228 | }
|
---|
229 | image.setNumColors(info_ptr->num_palette);
|
---|
230 | int i = 0;
|
---|
231 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
---|
232 | while (i < info_ptr->num_trans) {
|
---|
233 | image.setColor(i, qRgba(
|
---|
234 | info_ptr->palette[i].red,
|
---|
235 | info_ptr->palette[i].green,
|
---|
236 | info_ptr->palette[i].blue,
|
---|
237 | info_ptr->trans[i]
|
---|
238 | )
|
---|
239 | );
|
---|
240 | i++;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | while (i < info_ptr->num_palette) {
|
---|
244 | image.setColor(i, qRgba(
|
---|
245 | info_ptr->palette[i].red,
|
---|
246 | info_ptr->palette[i].green,
|
---|
247 | info_ptr->palette[i].blue,
|
---|
248 | 0xff
|
---|
249 | )
|
---|
250 | );
|
---|
251 | i++;
|
---|
252 | }
|
---|
253 | } else {
|
---|
254 | // 32-bit
|
---|
255 | if (bit_depth == 16)
|
---|
256 | png_set_strip_16(png_ptr);
|
---|
257 |
|
---|
258 | png_set_expand(png_ptr);
|
---|
259 |
|
---|
260 | if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
---|
261 | png_set_gray_to_rgb(png_ptr);
|
---|
262 |
|
---|
263 | QImage::Format format = QImage::Format_ARGB32;
|
---|
264 | // Only add filler if no alpha, or we can get 5 channel data.
|
---|
265 | if (!(color_type & PNG_COLOR_MASK_ALPHA)
|
---|
266 | && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
---|
267 | png_set_filler(png_ptr, 0xff, QSysInfo::ByteOrder == QSysInfo::BigEndian ?
|
---|
268 | PNG_FILLER_BEFORE : PNG_FILLER_AFTER);
|
---|
269 | // We want 4 bytes, but it isn't an alpha channel
|
---|
270 | format = QImage::Format_RGB32;
|
---|
271 | }
|
---|
272 | if (image.size() != QSize(width, height) || image.format() != format) {
|
---|
273 | image = QImage(width, height, format);
|
---|
274 | if (image.isNull())
|
---|
275 | return;
|
---|
276 | }
|
---|
277 |
|
---|
278 | if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
|
---|
279 | png_set_swap_alpha(png_ptr);
|
---|
280 |
|
---|
281 | png_read_update_info(png_ptr, info_ptr);
|
---|
282 | }
|
---|
283 |
|
---|
284 | // Qt==ARGB==Big(ARGB)==Little(BGRA)
|
---|
285 | if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
|
---|
286 | png_set_bgr(png_ptr);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | #if defined(Q_C_CALLBACKS)
|
---|
292 | extern "C" {
|
---|
293 | #endif
|
---|
294 | static void CALLBACK_CALL_TYPE qt_png_warning(png_structp /*png_ptr*/, png_const_charp message)
|
---|
295 | {
|
---|
296 | qWarning("libpng warning: %s", message);
|
---|
297 | }
|
---|
298 |
|
---|
299 | #if defined(Q_C_CALLBACKS)
|
---|
300 | }
|
---|
301 | #endif
|
---|
302 |
|
---|
303 | class QPngHandlerPrivate
|
---|
304 | {
|
---|
305 | public:
|
---|
306 | enum State {
|
---|
307 | Ready,
|
---|
308 | ReadHeader,
|
---|
309 | Error
|
---|
310 | };
|
---|
311 |
|
---|
312 | QPngHandlerPrivate(QPngHandler *qq)
|
---|
313 | : gamma(0.0), quality(2), png_ptr(0), info_ptr(0),
|
---|
314 | end_info(0), row_pointers(0), state(Ready), q(qq)
|
---|
315 | { }
|
---|
316 |
|
---|
317 | float gamma;
|
---|
318 | int quality;
|
---|
319 | QString description;
|
---|
320 |
|
---|
321 | png_struct *png_ptr;
|
---|
322 | png_info *info_ptr;
|
---|
323 | png_info *end_info;
|
---|
324 | png_byte **row_pointers;
|
---|
325 |
|
---|
326 | bool readPngHeader();
|
---|
327 | bool readPngImage(QImage *image);
|
---|
328 |
|
---|
329 | QImage::Format readImageFormat();
|
---|
330 |
|
---|
331 | State state;
|
---|
332 |
|
---|
333 | QPngHandler *q;
|
---|
334 | };
|
---|
335 |
|
---|
336 | /*!
|
---|
337 | \internal
|
---|
338 | */
|
---|
339 | bool Q_INTERNAL_WIN_NO_THROW QPngHandlerPrivate::readPngHeader()
|
---|
340 | {
|
---|
341 | state = Error;
|
---|
342 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
|
---|
343 | if (!png_ptr)
|
---|
344 | return false;
|
---|
345 |
|
---|
346 | png_set_error_fn(png_ptr, 0, 0, qt_png_warning);
|
---|
347 |
|
---|
348 | info_ptr = png_create_info_struct(png_ptr);
|
---|
349 | if (!info_ptr) {
|
---|
350 | png_destroy_read_struct(&png_ptr, 0, 0);
|
---|
351 | png_ptr = 0;
|
---|
352 | return false;
|
---|
353 | }
|
---|
354 |
|
---|
355 | end_info = png_create_info_struct(png_ptr);
|
---|
356 | if (!end_info) {
|
---|
357 | png_destroy_read_struct(&png_ptr, &info_ptr, 0);
|
---|
358 | png_ptr = 0;
|
---|
359 | return false;
|
---|
360 | }
|
---|
361 |
|
---|
362 | if (setjmp(png_jmpbuf(png_ptr))) {
|
---|
363 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
---|
364 | png_ptr = 0;
|
---|
365 | return false;
|
---|
366 | }
|
---|
367 |
|
---|
368 | png_set_read_fn(png_ptr, q->device(), iod_read_fn);
|
---|
369 | png_read_info(png_ptr, info_ptr);
|
---|
370 |
|
---|
371 | #ifndef QT_NO_IMAGE_TEXT
|
---|
372 | png_textp text_ptr;
|
---|
373 | int num_text=0;
|
---|
374 | png_get_text(png_ptr,info_ptr,&text_ptr,&num_text);
|
---|
375 |
|
---|
376 | while (num_text--) {
|
---|
377 | QString key, value;
|
---|
378 | #if defined(PNG_iTXt_SUPPORTED)
|
---|
379 | if (text_ptr->lang) {
|
---|
380 | QTextCodec *codec = QTextCodec::codecForName(text_ptr->lang);
|
---|
381 | if (codec) {
|
---|
382 | key = codec->toUnicode(text_ptr->lang_key);
|
---|
383 | value = codec->toUnicode(QByteArray(text_ptr->text, text_ptr->itxt_length));
|
---|
384 | } else {
|
---|
385 | key = QString::fromLatin1(text_ptr->key);
|
---|
386 | value = QString::fromLatin1(QByteArray(text_ptr->text, int(text_ptr->text_length)));
|
---|
387 | }
|
---|
388 | } else
|
---|
389 | #endif
|
---|
390 | {
|
---|
391 | key = QString::fromLatin1(text_ptr->key);
|
---|
392 | value = QString::fromLatin1(QByteArray(text_ptr->text, int(text_ptr->text_length)));
|
---|
393 | }
|
---|
394 | if (!description.isEmpty())
|
---|
395 | description += QLatin1String("\n\n");
|
---|
396 | description += key + QLatin1String(": ") + value.simplified();
|
---|
397 | text_ptr++;
|
---|
398 | }
|
---|
399 | #endif
|
---|
400 |
|
---|
401 | state = ReadHeader;
|
---|
402 | return true;
|
---|
403 | }
|
---|
404 |
|
---|
405 | /*!
|
---|
406 | \internal
|
---|
407 | */
|
---|
408 | bool Q_INTERNAL_WIN_NO_THROW QPngHandlerPrivate::readPngImage(QImage *outImage)
|
---|
409 | {
|
---|
410 | if (state == Error)
|
---|
411 | return false;
|
---|
412 |
|
---|
413 | if (state == Ready && !readPngHeader()) {
|
---|
414 | state = Error;
|
---|
415 | return false;
|
---|
416 | }
|
---|
417 |
|
---|
418 | row_pointers = 0;
|
---|
419 | if (setjmp(png_jmpbuf(png_ptr))) {
|
---|
420 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
---|
421 | delete [] row_pointers;
|
---|
422 | png_ptr = 0;
|
---|
423 | state = Error;
|
---|
424 | return false;
|
---|
425 | }
|
---|
426 |
|
---|
427 | setup_qt(*outImage, png_ptr, info_ptr, gamma);
|
---|
428 |
|
---|
429 | if (outImage->isNull()) {
|
---|
430 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
---|
431 | delete [] row_pointers;
|
---|
432 | png_ptr = 0;
|
---|
433 | state = Error;
|
---|
434 | return false;
|
---|
435 | }
|
---|
436 |
|
---|
437 | png_uint_32 width;
|
---|
438 | png_uint_32 height;
|
---|
439 | int bit_depth;
|
---|
440 | int color_type;
|
---|
441 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
|
---|
442 | 0, 0, 0);
|
---|
443 |
|
---|
444 | uchar *data = outImage->bits();
|
---|
445 | int bpl = outImage->bytesPerLine();
|
---|
446 | row_pointers = new png_bytep[height];
|
---|
447 |
|
---|
448 | for (uint y = 0; y < height; y++)
|
---|
449 | row_pointers[y] = data + y * bpl;
|
---|
450 |
|
---|
451 | png_read_image(png_ptr, row_pointers);
|
---|
452 |
|
---|
453 | #if 0 // libpng takes care of this.
|
---|
454 | png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)
|
---|
455 | if (outImage->depth()==32 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
---|
456 | QRgb trans = 0xFF000000 | qRgb(
|
---|
457 | (info_ptr->trans_values.red << 8 >> bit_depth)&0xff,
|
---|
458 | (info_ptr->trans_values.green << 8 >> bit_depth)&0xff,
|
---|
459 | (info_ptr->trans_values.blue << 8 >> bit_depth)&0xff);
|
---|
460 | for (uint y=0; y<height; y++) {
|
---|
461 | for (uint x=0; x<info_ptr->width; x++) {
|
---|
462 | if (((uint**)jt)[y][x] == trans) {
|
---|
463 | ((uint**)jt)[y][x] &= 0x00FFFFFF;
|
---|
464 | } else {
|
---|
465 | }
|
---|
466 | }
|
---|
467 | }
|
---|
468 | }
|
---|
469 | #endif
|
---|
470 |
|
---|
471 | outImage->setDotsPerMeterX(png_get_x_pixels_per_meter(png_ptr,info_ptr));
|
---|
472 | outImage->setDotsPerMeterY(png_get_y_pixels_per_meter(png_ptr,info_ptr));
|
---|
473 |
|
---|
474 | #ifndef QT_NO_IMAGE_TEXT
|
---|
475 | png_textp text_ptr;
|
---|
476 | int num_text=0;
|
---|
477 | png_get_text(png_ptr,info_ptr,&text_ptr,&num_text);
|
---|
478 | while (num_text--) {
|
---|
479 | outImage->setText(text_ptr->key,0,QString::fromAscii(text_ptr->text));
|
---|
480 | text_ptr++;
|
---|
481 | }
|
---|
482 |
|
---|
483 | foreach (const QString &pair, description.split(QLatin1String("\n\n"))) {
|
---|
484 | int index = pair.indexOf(QLatin1Char(':'));
|
---|
485 | if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) {
|
---|
486 | outImage->setText(QLatin1String("Description"), pair.simplified());
|
---|
487 | } else {
|
---|
488 | QString key = pair.left(index);
|
---|
489 | outImage->setText(key, pair.mid(index + 2).simplified());
|
---|
490 | }
|
---|
491 | }
|
---|
492 | #endif
|
---|
493 |
|
---|
494 | png_read_end(png_ptr, end_info);
|
---|
495 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
---|
496 | delete [] row_pointers;
|
---|
497 | png_ptr = 0;
|
---|
498 | state = Ready;
|
---|
499 |
|
---|
500 | // sanity check palette entries
|
---|
501 | if (color_type == PNG_COLOR_TYPE_PALETTE
|
---|
502 | && outImage->format() == QImage::Format_Indexed8) {
|
---|
503 | int color_table_size = outImage->numColors();
|
---|
504 | for (int y=0; y<(int)height; ++y) {
|
---|
505 | uchar *p = outImage->scanLine(y);
|
---|
506 | uchar *end = p + width;
|
---|
507 | while (p < end) {
|
---|
508 | if (*p >= color_table_size)
|
---|
509 | *p = 0;
|
---|
510 | ++p;
|
---|
511 | }
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | return true;
|
---|
516 | }
|
---|
517 |
|
---|
518 | QImage::Format QPngHandlerPrivate::readImageFormat()
|
---|
519 | {
|
---|
520 | QImage::Format format = QImage::Format_Invalid;
|
---|
521 | png_uint_32 width, height;
|
---|
522 | int bit_depth, color_type;
|
---|
523 | if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
|
---|
524 | // Black & White or 8-bit grayscale
|
---|
525 | if (info_ptr->bit_depth == 1 && info_ptr->channels == 1) {
|
---|
526 | format = QImage::Format_Mono;
|
---|
527 | } else if (info_ptr->bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
---|
528 | format = QImage::Format_ARGB32;
|
---|
529 | } else {
|
---|
530 | format = QImage::Format_Indexed8;
|
---|
531 | }
|
---|
532 | } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE
|
---|
533 | && png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)
|
---|
534 | && info_ptr->num_palette <= 256)
|
---|
535 | {
|
---|
536 | // 1-bit and 8-bit color
|
---|
537 | if (info_ptr->bit_depth != 1)
|
---|
538 | png_set_packing(png_ptr);
|
---|
539 | png_read_update_info(png_ptr, info_ptr);
|
---|
540 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
|
---|
541 | format = bit_depth == 1 ? QImage::Format_Mono : QImage::Format_Indexed8;
|
---|
542 | } else {
|
---|
543 | // 32-bit
|
---|
544 | if (info_ptr->bit_depth == 16)
|
---|
545 | png_set_strip_16(png_ptr);
|
---|
546 |
|
---|
547 | format = QImage::Format_ARGB32;
|
---|
548 | // Only add filler if no alpha, or we can get 5 channel data.
|
---|
549 | if (!(info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
|
---|
550 | && !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
---|
551 | // We want 4 bytes, but it isn't an alpha channel
|
---|
552 | format = QImage::Format_RGB32;
|
---|
553 | }
|
---|
554 | }
|
---|
555 |
|
---|
556 | return format;
|
---|
557 | }
|
---|
558 |
|
---|
559 | QPNGImageWriter::QPNGImageWriter(QIODevice* iod) :
|
---|
560 | dev(iod),
|
---|
561 | frames_written(0),
|
---|
562 | disposal(Unspecified),
|
---|
563 | looping(-1),
|
---|
564 | ms_delay(-1),
|
---|
565 | gamma(0.0)
|
---|
566 | {
|
---|
567 | }
|
---|
568 |
|
---|
569 | QPNGImageWriter::~QPNGImageWriter()
|
---|
570 | {
|
---|
571 | }
|
---|
572 |
|
---|
573 | void QPNGImageWriter::setDisposalMethod(DisposalMethod dm)
|
---|
574 | {
|
---|
575 | disposal = dm;
|
---|
576 | }
|
---|
577 |
|
---|
578 | void QPNGImageWriter::setLooping(int loops)
|
---|
579 | {
|
---|
580 | looping = loops;
|
---|
581 | }
|
---|
582 |
|
---|
583 | void QPNGImageWriter::setFrameDelay(int msecs)
|
---|
584 | {
|
---|
585 | ms_delay = msecs;
|
---|
586 | }
|
---|
587 |
|
---|
588 | void QPNGImageWriter::setGamma(float g)
|
---|
589 | {
|
---|
590 | gamma = g;
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | #ifndef QT_NO_IMAGE_TEXT
|
---|
595 | static void set_text(const QImage &image, png_structp png_ptr, png_infop info_ptr,
|
---|
596 | const QString &description)
|
---|
597 | {
|
---|
598 | QMap<QString, QString> text;
|
---|
599 | foreach (const QString &key, image.textKeys()) {
|
---|
600 | if (!key.isEmpty())
|
---|
601 | text.insert(key, image.text(key));
|
---|
602 | }
|
---|
603 | foreach (const QString &pair, description.split(QLatin1String("\n\n"))) {
|
---|
604 | int index = pair.indexOf(QLatin1Char(':'));
|
---|
605 | if (index >= 0 && pair.indexOf(QLatin1Char(' ')) < index) {
|
---|
606 | QString s = pair.simplified();
|
---|
607 | if (!s.isEmpty())
|
---|
608 | text.insert(QLatin1String("Description"), s);
|
---|
609 | } else {
|
---|
610 | QString key = pair.left(index);
|
---|
611 | if (!key.simplified().isEmpty())
|
---|
612 | text.insert(key, pair.mid(index + 2).simplified());
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | if (text.isEmpty())
|
---|
617 | return;
|
---|
618 |
|
---|
619 | png_textp text_ptr = new png_text[text.size()];
|
---|
620 |
|
---|
621 | QMap<QString, QString>::ConstIterator it = text.constBegin();
|
---|
622 | int i = 0;
|
---|
623 | while (it != text.constEnd()) {
|
---|
624 | QString t = it.value();
|
---|
625 | if (t.length() < 40)
|
---|
626 | text_ptr[i].compression = PNG_TEXT_COMPRESSION_NONE;
|
---|
627 | else
|
---|
628 | text_ptr[i].compression = PNG_TEXT_COMPRESSION_zTXt;
|
---|
629 | text_ptr[i].key = qstrdup(it.key().left(79).toLatin1().constData());
|
---|
630 |
|
---|
631 | #ifndef PNG_iTXt_SUPPORTED
|
---|
632 | QByteArray value = it.value().toLatin1();
|
---|
633 | text_ptr[i].text = qstrdup(value.constData());
|
---|
634 | text_ptr[i].text_length = value.size();
|
---|
635 | #else
|
---|
636 | QByteArray value = it.value().toUtf8();
|
---|
637 | text_ptr[i].text = qstrdup(value.constData());
|
---|
638 | text_ptr[i].text_length = 0;
|
---|
639 | text_ptr[i].itxt_length = value.size();
|
---|
640 | text_ptr[i].lang = "UTF-8";
|
---|
641 | text_ptr[i].lang_key = qstrdup(it.key().toUtf8().constData());
|
---|
642 | #endif
|
---|
643 | ++i;
|
---|
644 | ++it;
|
---|
645 | }
|
---|
646 |
|
---|
647 | png_set_text(png_ptr, info_ptr, text_ptr, i);
|
---|
648 | for (i = 0; i < text.size(); ++i) {
|
---|
649 | delete [] text_ptr[i].key;
|
---|
650 | delete [] text_ptr[i].text;
|
---|
651 | #ifdef PNG_iTXt_SUPPORTED
|
---|
652 | delete [] text_ptr[i].lang_key;
|
---|
653 | #endif
|
---|
654 | }
|
---|
655 | delete [] text_ptr;
|
---|
656 | }
|
---|
657 | #endif
|
---|
658 |
|
---|
659 | bool QPNGImageWriter::writeImage(const QImage& image, int off_x, int off_y)
|
---|
660 | {
|
---|
661 | return writeImage(image, -1, QString(), off_x, off_y);
|
---|
662 | }
|
---|
663 |
|
---|
664 | bool Q_INTERNAL_WIN_NO_THROW QPNGImageWriter::writeImage(const QImage& image_in, int quality_in, const QString &description,
|
---|
665 | int off_x_in, int off_y_in)
|
---|
666 | {
|
---|
667 | #ifdef QT_NO_IMAGE_TEXT
|
---|
668 | Q_UNUSED(description);
|
---|
669 | #endif
|
---|
670 |
|
---|
671 | QImage image;
|
---|
672 | switch (image_in.format()) {
|
---|
673 | case QImage::Format_ARGB32_Premultiplied:
|
---|
674 | case QImage::Format_ARGB4444_Premultiplied:
|
---|
675 | case QImage::Format_ARGB8555_Premultiplied:
|
---|
676 | case QImage::Format_ARGB8565_Premultiplied:
|
---|
677 | case QImage::Format_ARGB6666_Premultiplied:
|
---|
678 | image = image_in.convertToFormat(QImage::Format_ARGB32);
|
---|
679 | break;
|
---|
680 | case QImage::Format_RGB16:
|
---|
681 | case QImage::Format_RGB444:
|
---|
682 | case QImage::Format_RGB555:
|
---|
683 | case QImage::Format_RGB666:
|
---|
684 | case QImage::Format_RGB888:
|
---|
685 | image = image_in.convertToFormat(QImage::Format_RGB32);
|
---|
686 | break;
|
---|
687 | default:
|
---|
688 | image = image_in;
|
---|
689 | break;
|
---|
690 | }
|
---|
691 |
|
---|
692 | QPoint offset = image.offset();
|
---|
693 | int off_x = off_x_in + offset.x();
|
---|
694 | int off_y = off_y_in + offset.y();
|
---|
695 |
|
---|
696 | png_structp png_ptr;
|
---|
697 | png_infop info_ptr;
|
---|
698 | png_bytep* row_pointers;
|
---|
699 |
|
---|
700 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0);
|
---|
701 | if (!png_ptr) {
|
---|
702 | return false;
|
---|
703 | }
|
---|
704 |
|
---|
705 | png_set_error_fn(png_ptr, 0, 0, qt_png_warning);
|
---|
706 |
|
---|
707 | info_ptr = png_create_info_struct(png_ptr);
|
---|
708 | if (!info_ptr) {
|
---|
709 | png_destroy_write_struct(&png_ptr, 0);
|
---|
710 | return false;
|
---|
711 | }
|
---|
712 |
|
---|
713 | if (setjmp(png_jmpbuf(png_ptr))) {
|
---|
714 | png_destroy_write_struct(&png_ptr, &info_ptr);
|
---|
715 | return false;
|
---|
716 | }
|
---|
717 |
|
---|
718 | int quality = quality_in;
|
---|
719 | if (quality >= 0) {
|
---|
720 | if (quality > 9) {
|
---|
721 | qWarning("PNG: Quality %d out of range", quality);
|
---|
722 | quality = 9;
|
---|
723 | }
|
---|
724 | png_set_compression_level(png_ptr, quality);
|
---|
725 | }
|
---|
726 |
|
---|
727 | if (gamma != 0.0) {
|
---|
728 | png_set_gAMA(png_ptr, info_ptr, 1.0/gamma);
|
---|
729 | }
|
---|
730 |
|
---|
731 | png_set_write_fn(png_ptr, (void*)this, qpiw_write_fn, qpiw_flush_fn);
|
---|
732 |
|
---|
733 | info_ptr->channels =
|
---|
734 | (image.depth() == 32)
|
---|
735 | ? (image.format() == QImage::Format_RGB32 ? 3 : 4)
|
---|
736 | : 1;
|
---|
737 |
|
---|
738 | png_set_IHDR(png_ptr, info_ptr, image.width(), image.height(),
|
---|
739 | image.depth() == 1 ? 1 : 8 /* per channel */,
|
---|
740 | image.depth() == 32
|
---|
741 | ? image.format() == QImage::Format_RGB32
|
---|
742 | ? PNG_COLOR_TYPE_RGB
|
---|
743 | : PNG_COLOR_TYPE_RGB_ALPHA
|
---|
744 | : PNG_COLOR_TYPE_PALETTE, 0, 0, 0);
|
---|
745 |
|
---|
746 |
|
---|
747 | //png_set_sBIT(png_ptr, info_ptr, 8);
|
---|
748 | info_ptr->sig_bit.red = 8;
|
---|
749 | info_ptr->sig_bit.green = 8;
|
---|
750 | info_ptr->sig_bit.blue = 8;
|
---|
751 |
|
---|
752 | if (image.format() == QImage::Format_MonoLSB)
|
---|
753 | png_set_packswap(png_ptr);
|
---|
754 |
|
---|
755 | png_colorp palette = 0;
|
---|
756 | png_bytep copy_trans = 0;
|
---|
757 | if (image.numColors()) {
|
---|
758 | // Paletted
|
---|
759 | int num_palette = image.numColors();
|
---|
760 | palette = new png_color[num_palette];
|
---|
761 | png_set_PLTE(png_ptr, info_ptr, palette, num_palette);
|
---|
762 | int* trans = new int[num_palette];
|
---|
763 | int num_trans = 0;
|
---|
764 | for (int i=0; i<num_palette; i++) {
|
---|
765 | QRgb rgb=image.color(i);
|
---|
766 | info_ptr->palette[i].red = qRed(rgb);
|
---|
767 | info_ptr->palette[i].green = qGreen(rgb);
|
---|
768 | info_ptr->palette[i].blue = qBlue(rgb);
|
---|
769 | trans[i] = rgb >> 24;
|
---|
770 | if (trans[i] < 255) {
|
---|
771 | num_trans = i+1;
|
---|
772 | }
|
---|
773 | }
|
---|
774 | if (num_trans) {
|
---|
775 | copy_trans = new png_byte[num_trans];
|
---|
776 | for (int i=0; i<num_trans; i++)
|
---|
777 | copy_trans[i] = trans[i];
|
---|
778 | png_set_tRNS(png_ptr, info_ptr, copy_trans, num_trans, 0);
|
---|
779 | }
|
---|
780 | delete [] trans;
|
---|
781 | }
|
---|
782 |
|
---|
783 | if (image.format() != QImage::Format_RGB32) {
|
---|
784 | info_ptr->sig_bit.alpha = 8;
|
---|
785 | }
|
---|
786 |
|
---|
787 | // Swap ARGB to RGBA (normal PNG format) before saving on
|
---|
788 | // BigEndian machines
|
---|
789 | if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
|
---|
790 | png_set_swap_alpha(png_ptr);
|
---|
791 | }
|
---|
792 |
|
---|
793 | // Qt==ARGB==Big(ARGB)==Little(BGRA)
|
---|
794 | if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
|
---|
795 | png_set_bgr(png_ptr);
|
---|
796 | }
|
---|
797 |
|
---|
798 | if (off_x || off_y) {
|
---|
799 | png_set_oFFs(png_ptr, info_ptr, off_x, off_y, PNG_OFFSET_PIXEL);
|
---|
800 | }
|
---|
801 |
|
---|
802 | if (frames_written > 0)
|
---|
803 | png_set_sig_bytes(png_ptr, 8);
|
---|
804 |
|
---|
805 | if (image.dotsPerMeterX() > 0 || image.dotsPerMeterY() > 0) {
|
---|
806 | png_set_pHYs(png_ptr, info_ptr,
|
---|
807 | image.dotsPerMeterX(), image.dotsPerMeterY(),
|
---|
808 | PNG_RESOLUTION_METER);
|
---|
809 | }
|
---|
810 |
|
---|
811 | #ifndef QT_NO_IMAGE_TEXT
|
---|
812 | set_text(image, png_ptr, info_ptr, description);
|
---|
813 | #endif
|
---|
814 | png_write_info(png_ptr, info_ptr);
|
---|
815 |
|
---|
816 | if (image.depth() != 1)
|
---|
817 | png_set_packing(png_ptr);
|
---|
818 |
|
---|
819 | if (image.format() == QImage::Format_RGB32)
|
---|
820 | png_set_filler(png_ptr, 0,
|
---|
821 | QSysInfo::ByteOrder == QSysInfo::BigEndian ?
|
---|
822 | PNG_FILLER_BEFORE : PNG_FILLER_AFTER);
|
---|
823 |
|
---|
824 | if (looping >= 0 && frames_written == 0) {
|
---|
825 | uchar data[13] = "NETSCAPE2.0";
|
---|
826 | // 0123456789aBC
|
---|
827 | data[0xB] = looping%0x100;
|
---|
828 | data[0xC] = looping/0x100;
|
---|
829 | png_write_chunk(png_ptr, (png_byte*)"gIFx", data, 13);
|
---|
830 | }
|
---|
831 | if (ms_delay >= 0 || disposal!=Unspecified) {
|
---|
832 | uchar data[4];
|
---|
833 | data[0] = disposal;
|
---|
834 | data[1] = 0;
|
---|
835 | data[2] = (ms_delay/10)/0x100; // hundredths
|
---|
836 | data[3] = (ms_delay/10)%0x100;
|
---|
837 | png_write_chunk(png_ptr, (png_byte*)"gIFg", data, 4);
|
---|
838 | }
|
---|
839 |
|
---|
840 | png_uint_32 width;
|
---|
841 | png_uint_32 height;
|
---|
842 | int bit_depth;
|
---|
843 | int color_type;
|
---|
844 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
|
---|
845 | 0, 0, 0);
|
---|
846 |
|
---|
847 | const uchar *data = image.bits();
|
---|
848 | int bpl = image.bytesPerLine();
|
---|
849 | row_pointers = new png_bytep[height];
|
---|
850 | uint y;
|
---|
851 | for (y=0; y<height; y++) {
|
---|
852 | row_pointers[y] = (png_bytep)(data + y * bpl);
|
---|
853 | }
|
---|
854 | png_write_image(png_ptr, row_pointers);
|
---|
855 | delete [] row_pointers;
|
---|
856 |
|
---|
857 | png_write_end(png_ptr, info_ptr);
|
---|
858 | frames_written++;
|
---|
859 |
|
---|
860 | if (palette)
|
---|
861 | delete [] palette;
|
---|
862 | if (copy_trans)
|
---|
863 | delete [] copy_trans;
|
---|
864 |
|
---|
865 | png_destroy_write_struct(&png_ptr, &info_ptr);
|
---|
866 |
|
---|
867 | return true;
|
---|
868 | }
|
---|
869 |
|
---|
870 | static bool write_png_image(const QImage &image, QIODevice *device,
|
---|
871 | int quality, float gamma, const QString &description)
|
---|
872 | {
|
---|
873 | QPNGImageWriter writer(device);
|
---|
874 | if (quality >= 0) {
|
---|
875 | quality = qMin(quality, 100);
|
---|
876 | quality = (100-quality) * 9 / 91; // map [0,100] -> [9,0]
|
---|
877 | }
|
---|
878 | writer.setGamma(gamma);
|
---|
879 | return writer.writeImage(image, quality, description);
|
---|
880 | }
|
---|
881 |
|
---|
882 | QPngHandler::QPngHandler()
|
---|
883 | : d(new QPngHandlerPrivate(this))
|
---|
884 | {
|
---|
885 | }
|
---|
886 |
|
---|
887 | QPngHandler::~QPngHandler()
|
---|
888 | {
|
---|
889 | if (d->png_ptr)
|
---|
890 | png_destroy_read_struct(&d->png_ptr, &d->info_ptr, &d->end_info);
|
---|
891 | delete d;
|
---|
892 | }
|
---|
893 |
|
---|
894 | bool QPngHandler::canRead() const
|
---|
895 | {
|
---|
896 | if (d->state == QPngHandlerPrivate::Ready) {
|
---|
897 | if (!canRead(device()))
|
---|
898 | return false;
|
---|
899 | setFormat("png");
|
---|
900 | return true;
|
---|
901 | }
|
---|
902 | return d->state != QPngHandlerPrivate::Error;
|
---|
903 | }
|
---|
904 |
|
---|
905 | bool QPngHandler::canRead(QIODevice *device)
|
---|
906 | {
|
---|
907 | if (!device) {
|
---|
908 | qWarning("QPngHandler::canRead() called with no device");
|
---|
909 | return false;
|
---|
910 | }
|
---|
911 |
|
---|
912 | return device->peek(8) == "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
|
---|
913 | }
|
---|
914 |
|
---|
915 | bool QPngHandler::read(QImage *image)
|
---|
916 | {
|
---|
917 | if (!canRead())
|
---|
918 | return false;
|
---|
919 | return d->readPngImage(image);
|
---|
920 | }
|
---|
921 |
|
---|
922 | bool QPngHandler::write(const QImage &image)
|
---|
923 | {
|
---|
924 | return write_png_image(image, device(), d->quality, d->gamma, d->description);
|
---|
925 | }
|
---|
926 |
|
---|
927 | bool QPngHandler::supportsOption(ImageOption option) const
|
---|
928 | {
|
---|
929 | return option == Gamma
|
---|
930 | || option == Description
|
---|
931 | || option == ImageFormat
|
---|
932 | || option == Quality
|
---|
933 | || option == Size;
|
---|
934 | }
|
---|
935 |
|
---|
936 | QVariant QPngHandler::option(ImageOption option) const
|
---|
937 | {
|
---|
938 | if (d->state == QPngHandlerPrivate::Error)
|
---|
939 | return QVariant();
|
---|
940 | if (d->state == QPngHandlerPrivate::Ready && !d->readPngHeader())
|
---|
941 | return QVariant();
|
---|
942 |
|
---|
943 | if (option == Gamma)
|
---|
944 | return d->gamma;
|
---|
945 | else if (option == Quality)
|
---|
946 | return d->quality;
|
---|
947 | else if (option == Description)
|
---|
948 | return d->description;
|
---|
949 | else if (option == Size)
|
---|
950 | return QSize(d->info_ptr->width, d->info_ptr->height);
|
---|
951 | else if (option == ImageFormat)
|
---|
952 | return d->readImageFormat();
|
---|
953 | return 0;
|
---|
954 | }
|
---|
955 |
|
---|
956 | void QPngHandler::setOption(ImageOption option, const QVariant &value)
|
---|
957 | {
|
---|
958 | if (option == Gamma)
|
---|
959 | d->gamma = value.toDouble();
|
---|
960 | else if (option == Quality)
|
---|
961 | d->quality = value.toInt();
|
---|
962 | else if (option == Description)
|
---|
963 | d->description = value.toString();
|
---|
964 | }
|
---|
965 |
|
---|
966 | QByteArray QPngHandler::name() const
|
---|
967 | {
|
---|
968 | return "png";
|
---|
969 | }
|
---|
970 |
|
---|
971 | QT_END_NAMESPACE
|
---|
972 |
|
---|
973 | #endif // QT_NO_IMAGEFORMAT_PNG
|
---|