blob: 70cc1b9f759ea59d8947c910d8bbf54b21f6805e [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkImageDecoder.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +00009#include "SkImageEncoder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkColor.h"
11#include "SkColorPriv.h"
12#include "SkDither.h"
13#include "SkMath.h"
halcanary@google.com2a103182013-10-14 12:49:15 +000014#include "SkRTConf.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkScaledBitmapSampler.h"
16#include "SkStream.h"
17#include "SkTemplates.h"
18#include "SkUtils.h"
epoger@google.com4ce738b2012-11-16 18:44:18 +000019#include "transform_scanline.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020extern "C" {
21#include "png.h"
22}
23
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +000024/* These were dropped in libpng >= 1.4 */
25#ifndef png_infopp_NULL
26#define png_infopp_NULL NULL
27#endif
28
29#ifndef png_bytepp_NULL
30#define png_bytepp_NULL NULL
31#endif
32
33#ifndef int_p_NULL
34#define int_p_NULL NULL
35#endif
36
37#ifndef png_flush_ptr_NULL
38#define png_flush_ptr_NULL NULL
39#endif
40
halcanary@google.comfed30372013-10-04 12:46:45 +000041#if defined(SK_DEBUG)
halcanary@google.com2a103182013-10-14 12:49:15 +000042#define DEFAULT_FOR_SUPPRESS_PNG_IMAGE_DECODER_WARNINGS false
43#else // !defined(SK_DEBUG)
44#define DEFAULT_FOR_SUPPRESS_PNG_IMAGE_DECODER_WARNINGS true
halcanary@google.comfed30372013-10-04 12:46:45 +000045#endif // defined(SK_DEBUG)
halcanary@google.com2a103182013-10-14 12:49:15 +000046SK_CONF_DECLARE(bool, c_suppressPNGImageDecoderWarnings,
47 "images.png.suppressDecoderWarnings",
48 DEFAULT_FOR_SUPPRESS_PNG_IMAGE_DECODER_WARNINGS,
49 "Suppress most PNG warnings when calling image decode "
50 "functions.");
51
halcanary@google.comfed30372013-10-04 12:46:45 +000052
53
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000054class SkPNGImageIndex {
55public:
scroggo@google.comb5571b32013-09-25 21:34:24 +000056 SkPNGImageIndex(SkStreamRewindable* stream, png_structp png_ptr, png_infop info_ptr)
scroggo@google.comc70a3aa2013-07-18 20:03:15 +000057 : fStream(stream)
58 , fPng_ptr(png_ptr)
59 , fInfo_ptr(info_ptr)
reed6c225732014-06-09 19:52:07 -070060 , fColorType(kUnknown_SkColorType) {
scroggo@google.comc70a3aa2013-07-18 20:03:15 +000061 SkASSERT(stream != NULL);
62 stream->ref();
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000063 }
64 ~SkPNGImageIndex() {
scroggo@google.comc70a3aa2013-07-18 20:03:15 +000065 if (NULL != fPng_ptr) {
66 png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, png_infopp_NULL);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000067 }
68 }
69
scroggo@google.comb5571b32013-09-25 21:34:24 +000070 SkAutoTUnref<SkStreamRewindable> fStream;
71 png_structp fPng_ptr;
72 png_infop fInfo_ptr;
reed6c225732014-06-09 19:52:07 -070073 SkColorType fColorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000074};
75
reed@android.com8a1c16f2008-12-17 15:59:43 +000076class SkPNGImageDecoder : public SkImageDecoder {
77public:
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000078 SkPNGImageDecoder() {
79 fImageIndex = NULL;
80 }
81 virtual Format getFormat() const SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 return kPNG_Format;
83 }
scroggo@google.com39edf4c2013-04-25 17:33:51 +000084
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000085 virtual ~SkPNGImageDecoder() {
86 SkDELETE(fImageIndex);
87 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089protected:
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000090#ifdef SK_BUILD_FOR_ANDROID
scroggo@google.comb5571b32013-09-25 21:34:24 +000091 virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *height) SK_OVERRIDE;
scroggo@google.com7e6fcee2013-05-03 20:14:28 +000092 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& region) SK_OVERRIDE;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +000093#endif
94 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
95
96private:
97 SkPNGImageIndex* fImageIndex;
98
99 bool onDecodeInit(SkStream* stream, png_structp *png_ptrp, png_infop *info_ptrp);
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000100 bool decodePalette(png_structp png_ptr, png_infop info_ptr,
101 bool * SK_RESTRICT hasAlphap, bool *reallyHasAlphap,
102 SkColorTable **colorTablep);
reed6c225732014-06-09 19:52:07 -0700103 bool getBitmapConfig(png_structp, png_infop, SkColorType*, bool* hasAlpha,
104 SkPMColor* theTranspColor);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000105
106 typedef SkImageDecoder INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107};
108
109#ifndef png_jmpbuf
110# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
111#endif
112
113#define PNG_BYTES_TO_CHECK 4
114
115/* Automatically clean up after throwing an exception */
116struct PNGAutoClean {
117 PNGAutoClean(png_structp p, png_infop i): png_ptr(p), info_ptr(i) {}
118 ~PNGAutoClean() {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000119 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 }
121private:
122 png_structp png_ptr;
123 png_infop info_ptr;
124};
125
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126static void sk_read_fn(png_structp png_ptr, png_bytep data, png_size_t length) {
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000127 SkStream* sk_stream = (SkStream*) png_get_io_ptr(png_ptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 size_t bytes = sk_stream->read(data, length);
129 if (bytes != length) {
130 png_error(png_ptr, "Read Error!");
131 }
132}
133
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000134#ifdef SK_BUILD_FOR_ANDROID
135static void sk_seek_fn(png_structp png_ptr, png_uint_32 offset) {
scroggo@google.comb5571b32013-09-25 21:34:24 +0000136 SkStreamRewindable* sk_stream = (SkStreamRewindable*) png_get_io_ptr(png_ptr);
scroggo@google.com4d213ab2013-08-28 13:08:54 +0000137 if (!sk_stream->rewind()) {
138 png_error(png_ptr, "Failed to rewind stream!");
139 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000140 (void)sk_stream->skip(offset);
141}
142#endif
143
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) {
145 SkImageDecoder::Peeker* peeker =
146 (SkImageDecoder::Peeker*)png_get_user_chunk_ptr(png_ptr);
147 // peek() returning true means continue decoding
148 return peeker->peek((const char*)chunk->name, chunk->data, chunk->size) ?
149 1 : -1;
150}
151
152static void sk_error_fn(png_structp png_ptr, png_const_charp msg) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000153 SkDEBUGF(("------ png error %s\n", msg));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 longjmp(png_jmpbuf(png_ptr), 1);
155}
156
157static void skip_src_rows(png_structp png_ptr, uint8_t storage[], int count) {
158 for (int i = 0; i < count; i++) {
159 uint8_t* tmp = storage;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000160 png_read_rows(png_ptr, &tmp, png_bytepp_NULL, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 }
162}
163
164static bool pos_le(int value, int max) {
165 return value > 0 && value <= max;
166}
167
168static bool substituteTranspColor(SkBitmap* bm, SkPMColor match) {
169 SkASSERT(bm->config() == SkBitmap::kARGB_8888_Config);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000170
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 bool reallyHasAlpha = false;
172
173 for (int y = bm->height() - 1; y >= 0; --y) {
174 SkPMColor* p = bm->getAddr32(0, y);
175 for (int x = bm->width() - 1; x >= 0; --x) {
176 if (match == *p) {
177 *p = 0;
178 reallyHasAlpha = true;
179 }
180 p += 1;
181 }
182 }
183 return reallyHasAlpha;
184}
185
reed6c225732014-06-09 19:52:07 -0700186static bool canUpscalePaletteToConfig(SkColorType dstColorType, bool srcHasAlpha) {
187 switch (dstColorType) {
188 case kN32_SkColorType:
189 case kARGB_4444_SkColorType:
reed@android.comb6137c32009-07-29 20:56:52 +0000190 return true;
reed6c225732014-06-09 19:52:07 -0700191 case kRGB_565_SkColorType:
reed@android.comb6137c32009-07-29 20:56:52 +0000192 // only return true if the src is opaque (since 565 is opaque)
193 return !srcHasAlpha;
194 default:
195 return false;
196 }
197}
198
199// call only if color_type is PALETTE. Returns true if the ctable has alpha
200static bool hasTransparencyInPalette(png_structp png_ptr, png_infop info_ptr) {
201 png_bytep trans;
202 int num_trans;
203
204 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
205 png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL);
206 return num_trans > 0;
207 }
208 return false;
reed@android.com11344262009-07-08 20:09:23 +0000209}
210
halcanary@google.comfed30372013-10-04 12:46:45 +0000211void do_nothing_warning_fn(png_structp, png_const_charp) {
212 /* do nothing */
213}
214
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000215bool SkPNGImageDecoder::onDecodeInit(SkStream* sk_stream, png_structp *png_ptrp,
216 png_infop *info_ptrp) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217 /* Create and initialize the png_struct with the desired error handler
218 * functions. If you want to use the default stderr and longjump method,
219 * you can supply NULL for the last three parameters. We also supply the
220 * the compiler header file version, so that we know if the application
221 * was compiled with a compatible version of the library. */
halcanary@google.comfed30372013-10-04 12:46:45 +0000222
halcanary@google.comfed30372013-10-04 12:46:45 +0000223 png_error_ptr user_warning_fn =
224 (c_suppressPNGImageDecoderWarnings) ? (&do_nothing_warning_fn) : NULL;
225 /* NULL means to leave as default library behavior. */
halcanary@google.com2a103182013-10-14 12:49:15 +0000226 /* c_suppressPNGImageDecoderWarnings default depends on SK_DEBUG. */
halcanary@google.comfed30372013-10-04 12:46:45 +0000227 /* To suppress warnings with a SK_DEBUG binary, set the
228 * environment variable "skia_images_png_suppressDecoderWarnings"
229 * to "true". Inside a program that links to skia:
230 * SK_CONF_SET("images.png.suppressDecoderWarnings", true); */
halcanary@google.comfed30372013-10-04 12:46:45 +0000231
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
halcanary@google.comfed30372013-10-04 12:46:45 +0000233 NULL, sk_error_fn, user_warning_fn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 // png_voidp user_error_ptr, user_error_fn, user_warning_fn);
235 if (png_ptr == NULL) {
236 return false;
237 }
halcanary@google.comfed30372013-10-04 12:46:45 +0000238
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000239 *png_ptrp = png_ptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240
241 /* Allocate/initialize the memory for image information. */
242 png_infop info_ptr = png_create_info_struct(png_ptr);
243 if (info_ptr == NULL) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000244 png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 return false;
246 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000247 *info_ptrp = info_ptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248
249 /* Set error handling if you are using the setjmp/longjmp method (this is
250 * the normal method of doing things with libpng). REQUIRED unless you
251 * set up your own error handlers in the png_create_read_struct() earlier.
252 */
253 if (setjmp(png_jmpbuf(png_ptr))) {
scroggo@google.com5401cd02013-11-12 15:30:06 +0000254 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 return false;
256 }
257
258 /* If you are using replacement read functions, instead of calling
259 * png_init_io() here you would call:
260 */
261 png_set_read_fn(png_ptr, (void *)sk_stream, sk_read_fn);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000262#ifdef SK_BUILD_FOR_ANDROID
263 png_set_seek_fn(png_ptr, sk_seek_fn);
264#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 /* where user_io_ptr is a structure you want available to the callbacks */
266 /* If we have already read some of the signature */
267// png_set_sig_bytes(png_ptr, 0 /* sig_read */ );
268
269 // hookup our peeker so we can see any user-chunks the caller may be interested in
270 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"", 0);
271 if (this->getPeeker()) {
272 png_set_read_user_chunk_fn(png_ptr, (png_voidp)this->getPeeker(), sk_read_user_chunk);
273 }
274
275 /* The call to png_read_info() gives us all of the information from the
276 * PNG file before the first IDAT (image data chunk). */
277 png_read_info(png_ptr, info_ptr);
278 png_uint_32 origWidth, origHeight;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000279 int bitDepth, colorType;
280 png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth,
281 &colorType, int_p_NULL, int_p_NULL, int_p_NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282
283 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000284 if (bitDepth == 16) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 png_set_strip_16(png_ptr);
286 }
287 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
288 * byte into separate bytes (useful for paletted and grayscale images). */
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000289 if (bitDepth < 8) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 png_set_packing(png_ptr);
291 }
292 /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000293 if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8) {
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000294 png_set_expand_gray_1_2_4_to_8(png_ptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000296
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000297 return true;
298}
299
300bool SkPNGImageDecoder::onDecode(SkStream* sk_stream, SkBitmap* decodedBitmap,
301 Mode mode) {
302 png_structp png_ptr;
303 png_infop info_ptr;
304
305 if (!onDecodeInit(sk_stream, &png_ptr, &info_ptr)) {
306 return false;
307 }
308
scroggo@google.comfeeca3c2013-11-12 14:38:41 +0000309 PNGAutoClean autoClean(png_ptr, info_ptr);
310
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000311 if (setjmp(png_jmpbuf(png_ptr))) {
312 return false;
313 }
314
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000315 png_uint_32 origWidth, origHeight;
reed6c225732014-06-09 19:52:07 -0700316 int bitDepth, pngColorType, interlaceType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000317 png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth,
reed6c225732014-06-09 19:52:07 -0700318 &pngColorType, &interlaceType, int_p_NULL, int_p_NULL);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000319
reed6c225732014-06-09 19:52:07 -0700320 SkColorType colorType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 bool hasAlpha = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000322 SkPMColor theTranspColor = 0; // 0 tells us not to try to match
rmistry@google.comd6176b02012-08-23 18:14:13 +0000323
reed6c225732014-06-09 19:52:07 -0700324 if (!this->getBitmapConfig(png_ptr, info_ptr, &colorType, &hasAlpha, &theTranspColor)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000325 return false;
326 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000327
reed6c225732014-06-09 19:52:07 -0700328 SkAlphaType alphaType = this->getRequireUnpremultipliedColors() ?
329 kUnpremul_SkAlphaType : kPremul_SkAlphaType;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 const int sampleSize = this->getSampleSize();
331 SkScaledBitmapSampler sampler(origWidth, origHeight, sampleSize);
reed6c225732014-06-09 19:52:07 -0700332 decodedBitmap->setInfo(SkImageInfo::Make(sampler.scaledWidth(), sampler.scaledHeight(),
333 colorType, alphaType));
djsollen@google.com446cf712014-02-19 21:45:35 +0000334
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
336 return true;
337 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000338
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 // from here down we are concerned with colortables and pixels
340
341 // we track if we actually see a non-opaque pixels, since sometimes a PNG sets its colortype
342 // to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We care, since we
343 // draw lots faster if we can flag the bitmap has being opaque
344 bool reallyHasAlpha = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000345 SkColorTable* colorTable = NULL;
346
reed6c225732014-06-09 19:52:07 -0700347 if (pngColorType == PNG_COLOR_TYPE_PALETTE) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000348 decodePalette(png_ptr, info_ptr, &hasAlpha, &reallyHasAlpha, &colorTable);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000349 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000350
reed@android.com8a1c16f2008-12-17 15:59:43 +0000351 SkAutoUnref aur(colorTable);
352
scroggo@google.combc69ce92013-07-09 15:45:14 +0000353 if (!this->allocPixelRef(decodedBitmap,
reed6c225732014-06-09 19:52:07 -0700354 kIndex_8_SkColorType == colorType ? colorTable : NULL)) {
scroggo@google.combc69ce92013-07-09 15:45:14 +0000355 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000357
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358 SkAutoLockPixels alp(*decodedBitmap);
359
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 /* Turn on interlace handling. REQUIRED if you are not using
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000361 * png_read_image(). To see how to handle interlacing passes,
362 * see the png_read_row() method below:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363 */
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000364 const int number_passes = (interlaceType != PNG_INTERLACE_NONE) ?
365 png_set_interlace_handling(png_ptr) : 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366
367 /* Optional call to gamma correct and add the background to the palette
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000368 * and update info structure. REQUIRED if you are expecting libpng to
369 * update the palette for you (ie you selected such a transform above).
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370 */
371 png_read_update_info(png_ptr, info_ptr);
372
reed6c225732014-06-09 19:52:07 -0700373 if ((kAlpha_8_SkColorType == colorType || kIndex_8_SkColorType == colorType) &&
374 1 == sampleSize) {
375 if (kAlpha_8_SkColorType == colorType) {
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000376 // For an A8 bitmap, we assume there is an alpha for speed. It is
377 // possible the bitmap is opaque, but that is an unlikely use case
378 // since it would not be very interesting.
379 reallyHasAlpha = true;
380 // A8 is only allowed if the original was GRAY.
reed6c225732014-06-09 19:52:07 -0700381 SkASSERT(PNG_COLOR_TYPE_GRAY == pngColorType);
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000382 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000383 for (int i = 0; i < number_passes; i++) {
384 for (png_uint_32 y = 0; y < origHeight; y++) {
385 uint8_t* bmRow = decodedBitmap->getAddr8(0, y);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000386 png_read_rows(png_ptr, &bmRow, png_bytepp_NULL, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000387 }
388 }
389 } else {
390 SkScaledBitmapSampler::SrcConfig sc;
391 int srcBytesPerPixel = 4;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000392
reed@android.com11344262009-07-08 20:09:23 +0000393 if (colorTable != NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394 sc = SkScaledBitmapSampler::kIndex;
395 srcBytesPerPixel = 1;
reed6c225732014-06-09 19:52:07 -0700396 } else if (kAlpha_8_SkColorType == colorType) {
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000397 // A8 is only allowed if the original was GRAY.
reed6c225732014-06-09 19:52:07 -0700398 SkASSERT(PNG_COLOR_TYPE_GRAY == pngColorType);
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000399 sc = SkScaledBitmapSampler::kGray;
400 srcBytesPerPixel = 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000401 } else if (hasAlpha) {
402 sc = SkScaledBitmapSampler::kRGBA;
403 } else {
404 sc = SkScaledBitmapSampler::kRGBX;
405 }
reed@android.com11344262009-07-08 20:09:23 +0000406
407 /* We have to pass the colortable explicitly, since we may have one
408 even if our decodedBitmap doesn't, due to the request that we
409 upscale png's palette to a direct model
410 */
411 SkAutoLockColors ctLock(colorTable);
scroggo@google.com8d239242013-10-01 17:27:15 +0000412 if (!sampler.begin(decodedBitmap, sc, *this, ctLock.colors())) {
reed@android.com862e91b2009-04-28 15:27:07 +0000413 return false;
414 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000415 const int height = decodedBitmap->height();
416
reed@android.com862e91b2009-04-28 15:27:07 +0000417 if (number_passes > 1) {
418 SkAutoMalloc storage(origWidth * origHeight * srcBytesPerPixel);
419 uint8_t* base = (uint8_t*)storage.get();
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000420 size_t rowBytes = origWidth * srcBytesPerPixel;
reed@android.coma8a8b8b2009-05-04 15:00:11 +0000421
reed@android.com862e91b2009-04-28 15:27:07 +0000422 for (int i = 0; i < number_passes; i++) {
423 uint8_t* row = base;
424 for (png_uint_32 y = 0; y < origHeight; y++) {
425 uint8_t* bmRow = row;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000426 png_read_rows(png_ptr, &bmRow, png_bytepp_NULL, 1);
427 row += rowBytes;
reed@android.com862e91b2009-04-28 15:27:07 +0000428 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 }
reed@android.com862e91b2009-04-28 15:27:07 +0000430 // now sample it
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000431 base += sampler.srcY0() * rowBytes;
reed@android.com862e91b2009-04-28 15:27:07 +0000432 for (int y = 0; y < height; y++) {
433 reallyHasAlpha |= sampler.next(base);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000434 base += sampler.srcDY() * rowBytes;
reed@android.com862e91b2009-04-28 15:27:07 +0000435 }
436 } else {
437 SkAutoMalloc storage(origWidth * srcBytesPerPixel);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 uint8_t* srcRow = (uint8_t*)storage.get();
439 skip_src_rows(png_ptr, srcRow, sampler.srcY0());
440
441 for (int y = 0; y < height; y++) {
442 uint8_t* tmp = srcRow;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000443 png_read_rows(png_ptr, &tmp, png_bytepp_NULL, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000444 reallyHasAlpha |= sampler.next(srcRow);
445 if (y < height - 1) {
446 skip_src_rows(png_ptr, srcRow, sampler.srcDY() - 1);
447 }
448 }
reed@android.com862e91b2009-04-28 15:27:07 +0000449
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 // skip the rest of the rows (if any)
451 png_uint_32 read = (height - 1) * sampler.srcDY() +
452 sampler.srcY0() + 1;
453 SkASSERT(read <= origHeight);
454 skip_src_rows(png_ptr, srcRow, origHeight - read);
455 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456 }
457
458 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
459 png_read_end(png_ptr, info_ptr);
460
461 if (0 != theTranspColor) {
462 reallyHasAlpha |= substituteTranspColor(decodedBitmap, theTranspColor);
463 }
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000464 if (reallyHasAlpha && this->getRequireUnpremultipliedColors()) {
465 switch (decodedBitmap->config()) {
466 case SkBitmap::kIndex8_Config:
467 // Fall through.
468 case SkBitmap::kARGB_4444_Config:
469 // We have chosen not to support unpremul for these configs.
470 return false;
471 default: {
472 // Fall through to finish the decode. This config either
473 // supports unpremul or it is irrelevant because it has no
474 // alpha (or only alpha).
475 // These brackets prevent a warning.
476 }
477 }
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000478 }
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000479
djsollen@google.com446cf712014-02-19 21:45:35 +0000480 if (!reallyHasAlpha) {
481 decodedBitmap->setAlphaType(kOpaque_SkAlphaType);
reed@google.com383a6972013-10-21 14:00:07 +0000482 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483 return true;
484}
485
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000486
487
488bool SkPNGImageDecoder::getBitmapConfig(png_structp png_ptr, png_infop info_ptr,
reed6c225732014-06-09 19:52:07 -0700489 SkColorType* colorTypep,
490 bool* hasAlphap,
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000491 SkPMColor* SK_RESTRICT theTranspColorp) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000492 png_uint_32 origWidth, origHeight;
493 int bitDepth, colorType;
494 png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth,
495 &colorType, int_p_NULL, int_p_NULL, int_p_NULL);
496
497 // check for sBIT chunk data, in case we should disable dithering because
498 // our data is not truely 8bits per component
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000499 png_color_8p sig_bit;
scroggo@google.com8d239242013-10-01 17:27:15 +0000500 if (this->getDitherImage() && png_get_sBIT(png_ptr, info_ptr, &sig_bit)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000501#if 0
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000502 SkDebugf("----- sBIT %d %d %d %d\n", sig_bit->red, sig_bit->green,
503 sig_bit->blue, sig_bit->alpha);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000504#endif
505 // 0 seems to indicate no information available
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000506 if (pos_le(sig_bit->red, SK_R16_BITS) &&
507 pos_le(sig_bit->green, SK_G16_BITS) &&
508 pos_le(sig_bit->blue, SK_B16_BITS)) {
scroggo@google.com8d239242013-10-01 17:27:15 +0000509 this->setDitherImage(false);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000510 }
511 }
512
513 if (colorType == PNG_COLOR_TYPE_PALETTE) {
514 bool paletteHasAlpha = hasTransparencyInPalette(png_ptr, info_ptr);
reed6c225732014-06-09 19:52:07 -0700515 *colorTypep = this->getPrefColorType(kIndex_SrcDepth, paletteHasAlpha);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000516 // now see if we can upscale to their requested config
reed6c225732014-06-09 19:52:07 -0700517 if (!canUpscalePaletteToConfig(*colorTypep, paletteHasAlpha)) {
518 *colorTypep = kIndex_8_SkColorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000519 }
520 } else {
521 png_color_16p transpColor = NULL;
522 int numTransp = 0;
523
524 png_get_tRNS(png_ptr, info_ptr, NULL, &numTransp, &transpColor);
525
526 bool valid = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS);
527
528 if (valid && numTransp == 1 && transpColor != NULL) {
529 /* Compute our transparent color, which we'll match against later.
530 We don't really handle 16bit components properly here, since we
531 do our compare *after* the values have been knocked down to 8bit
532 which means we will find more matches than we should. The real
533 fix seems to be to see the actual 16bit components, do the
534 compare, and then knock it down to 8bits ourselves.
535 */
536 if (colorType & PNG_COLOR_MASK_COLOR) {
537 if (16 == bitDepth) {
538 *theTranspColorp = SkPackARGB32(0xFF, transpColor->red >> 8,
539 transpColor->green >> 8,
540 transpColor->blue >> 8);
541 } else {
halcanary@google.comfed30372013-10-04 12:46:45 +0000542 /* We apply the mask because in a very small
543 number of corrupt PNGs, (transpColor->red > 255)
544 and (bitDepth == 8), for certain versions of libpng. */
545 *theTranspColorp = SkPackARGB32(0xFF,
546 0xFF & (transpColor->red),
547 0xFF & (transpColor->green),
548 0xFF & (transpColor->blue));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000549 }
550 } else { // gray
551 if (16 == bitDepth) {
552 *theTranspColorp = SkPackARGB32(0xFF, transpColor->gray >> 8,
553 transpColor->gray >> 8,
554 transpColor->gray >> 8);
555 } else {
halcanary@google.comfed30372013-10-04 12:46:45 +0000556 /* We apply the mask because in a very small
557 number of corrupt PNGs, (transpColor->red >
558 255) and (bitDepth == 8), for certain versions
559 of libpng. For safety we assume the same could
560 happen with a grayscale PNG. */
561 *theTranspColorp = SkPackARGB32(0xFF,
562 0xFF & (transpColor->gray),
563 0xFF & (transpColor->gray),
564 0xFF & (transpColor->gray));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000565 }
566 }
567 }
568
569 if (valid ||
570 PNG_COLOR_TYPE_RGB_ALPHA == colorType ||
571 PNG_COLOR_TYPE_GRAY_ALPHA == colorType) {
572 *hasAlphap = true;
573 }
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000574
575 SrcDepth srcDepth = k32Bit_SrcDepth;
576 if (PNG_COLOR_TYPE_GRAY == colorType) {
577 srcDepth = k8BitGray_SrcDepth;
scroggo@google.com8e2ef012013-07-18 20:14:45 +0000578 // Remove this assert, which fails on desk_pokemonwiki.skp
579 //SkASSERT(!*hasAlphap);
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000580 }
581
reed6c225732014-06-09 19:52:07 -0700582 *colorTypep = this->getPrefColorType(srcDepth, *hasAlphap);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000583 // now match the request against our capabilities
584 if (*hasAlphap) {
reed6c225732014-06-09 19:52:07 -0700585 if (*colorTypep != kARGB_4444_SkColorType) {
586 *colorTypep = kN32_SkColorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000587 }
588 } else {
reed6c225732014-06-09 19:52:07 -0700589 if (kAlpha_8_SkColorType == *colorTypep) {
scroggo@google.com354fd972013-10-02 15:50:19 +0000590 if (k8BitGray_SrcDepth != srcDepth) {
591 // Converting a non grayscale image to A8 is not currently supported.
reed6c225732014-06-09 19:52:07 -0700592 *colorTypep = kN32_SkColorType;
scroggo@google.com354fd972013-10-02 15:50:19 +0000593 }
reed6c225732014-06-09 19:52:07 -0700594 } else if (*colorTypep != kRGB_565_SkColorType &&
595 *colorTypep != kARGB_4444_SkColorType) {
596 *colorTypep = kN32_SkColorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000597 }
598 }
599 }
600
601 // sanity check for size
602 {
reed@google.com57212f92013-12-30 14:40:38 +0000603 int64_t size = sk_64_mul(origWidth, origHeight);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000604 // now check that if we are 4-bytes per pixel, we also don't overflow
reed@google.com57212f92013-12-30 14:40:38 +0000605 if (size < 0 || size > (0x7FFFFFFF >> 2)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000606 return false;
607 }
608 }
609
reed6c225732014-06-09 19:52:07 -0700610 if (!this->chooseFromOneChoice(*colorTypep, origWidth, origHeight)) {
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000611 return false;
612 }
613
614 // If the image has alpha and the decoder wants unpremultiplied
615 // colors, the only supported config is 8888.
616 if (this->getRequireUnpremultipliedColors() && *hasAlphap) {
reed6c225732014-06-09 19:52:07 -0700617 *colorTypep = kN32_SkColorType;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000618 }
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000619
620 if (fImageIndex != NULL) {
reed6c225732014-06-09 19:52:07 -0700621 if (kUnknown_SkColorType == fImageIndex->fColorType) {
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000622 // This is the first time for this subset decode. From now on,
623 // all decodes must be in the same config.
reed6c225732014-06-09 19:52:07 -0700624 fImageIndex->fColorType = *colorTypep;
625 } else if (fImageIndex->fColorType != *colorTypep) {
626 // Requesting a different colortype for a subsequent decode is not
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000627 // supported. Report failure before we make changes to png_ptr.
628 return false;
629 }
630 }
631
reed6c225732014-06-09 19:52:07 -0700632 bool convertGrayToRGB = PNG_COLOR_TYPE_GRAY == colorType && *colorTypep != kAlpha_8_SkColorType;
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000633
634 // Unless the user is requesting A8, convert a grayscale image into RGB.
635 // GRAY_ALPHA will always be converted to RGB
636 if (convertGrayToRGB || colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
637 png_set_gray_to_rgb(png_ptr);
638 }
639
640 // Add filler (or alpha) byte (after each RGB triplet) if necessary.
641 if (colorType == PNG_COLOR_TYPE_RGB || convertGrayToRGB) {
642 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
643 }
644
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000645 return true;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000646}
647
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000648typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
649
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000650bool SkPNGImageDecoder::decodePalette(png_structp png_ptr, png_infop info_ptr,
651 bool *hasAlphap, bool *reallyHasAlphap,
652 SkColorTable **colorTablep) {
653 int numPalette;
654 png_colorp palette;
655 png_bytep trans;
656 int numTrans;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000657
658 png_get_PLTE(png_ptr, info_ptr, &palette, &numPalette);
659
660 /* BUGGY IMAGE WORKAROUND
661
662 We hit some images (e.g. fruit_.png) who contain bytes that are == colortable_count
663 which is a problem since we use the byte as an index. To work around this we grow
664 the colortable by 1 (if its < 256) and duplicate the last color into that slot.
665 */
666 int colorCount = numPalette + (numPalette < 256);
reed@google.com0a6151d2013-10-10 14:44:56 +0000667 SkPMColor colorStorage[256]; // worst-case storage
668 SkPMColor* colorPtr = colorStorage;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000669
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000670 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
671 png_get_tRNS(png_ptr, info_ptr, &trans, &numTrans, NULL);
672 *hasAlphap = (numTrans > 0);
673 } else {
674 numTrans = 0;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000675 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000676
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000677 // check for bad images that might make us crash
678 if (numTrans > numPalette) {
679 numTrans = numPalette;
680 }
681
682 int index = 0;
683 int transLessThanFF = 0;
684
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000685 // Choose which function to use to create the color table. If the final destination's
686 // config is unpremultiplied, the color table will store unpremultiplied colors.
687 PackColorProc proc;
688 if (this->getRequireUnpremultipliedColors()) {
689 proc = &SkPackARGB32NoCheck;
690 } else {
691 proc = &SkPreMultiplyARGB;
692 }
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000693 for (; index < numTrans; index++) {
694 transLessThanFF |= (int)*trans - 0xFF;
scroggo@google.com2bbc2c92013-06-14 15:33:20 +0000695 *colorPtr++ = proc(*trans++, palette->red, palette->green, palette->blue);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000696 palette++;
697 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000698 bool reallyHasAlpha = (transLessThanFF < 0);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000699
700 for (; index < numPalette; index++) {
701 *colorPtr++ = SkPackARGB32(0xFF, palette->red, palette->green, palette->blue);
702 palette++;
703 }
704
705 // see BUGGY IMAGE WORKAROUND comment above
706 if (numPalette < 256) {
707 *colorPtr = colorPtr[-1];
708 }
reed@google.com0a6151d2013-10-10 14:44:56 +0000709
710 SkAlphaType alphaType = kOpaque_SkAlphaType;
711 if (reallyHasAlpha) {
712 if (this->getRequireUnpremultipliedColors()) {
713 alphaType = kUnpremul_SkAlphaType;
714 } else {
715 alphaType = kPremul_SkAlphaType;
716 }
717 }
718
719 *colorTablep = SkNEW_ARGS(SkColorTable,
720 (colorStorage, colorCount, alphaType));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000721 *reallyHasAlphap = reallyHasAlpha;
722 return true;
723}
724
725#ifdef SK_BUILD_FOR_ANDROID
726
scroggo@google.comb5571b32013-09-25 21:34:24 +0000727bool SkPNGImageDecoder::onBuildTileIndex(SkStreamRewindable* sk_stream, int *width, int *height) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000728 png_structp png_ptr;
729 png_infop info_ptr;
730
731 if (!onDecodeInit(sk_stream, &png_ptr, &info_ptr)) {
732 return false;
733 }
734
735 if (setjmp(png_jmpbuf(png_ptr)) != 0) {
736 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
737 return false;
738 }
739
740 png_uint_32 origWidth, origHeight;
741 int bitDepth, colorType;
742 png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth,
743 &colorType, int_p_NULL, int_p_NULL, int_p_NULL);
744
745 *width = origWidth;
746 *height = origHeight;
747
748 png_build_index(png_ptr);
749
750 if (fImageIndex) {
751 SkDELETE(fImageIndex);
752 }
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000753 fImageIndex = SkNEW_ARGS(SkPNGImageIndex, (sk_stream, png_ptr, info_ptr));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000754
755 return true;
756}
757
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000758bool SkPNGImageDecoder::onDecodeSubset(SkBitmap* bm, const SkIRect& region) {
759 if (NULL == fImageIndex) {
760 return false;
761 }
762
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000763 png_structp png_ptr = fImageIndex->fPng_ptr;
764 png_infop info_ptr = fImageIndex->fInfo_ptr;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000765 if (setjmp(png_jmpbuf(png_ptr))) {
766 return false;
767 }
768
769 png_uint_32 origWidth, origHeight;
reed6c225732014-06-09 19:52:07 -0700770 int bitDepth, pngColorType, interlaceType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000771 png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth,
reed6c225732014-06-09 19:52:07 -0700772 &pngColorType, &interlaceType, int_p_NULL, int_p_NULL);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000773
774 SkIRect rect = SkIRect::MakeWH(origWidth, origHeight);
775
776 if (!rect.intersect(region)) {
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000777 // If the requested region is entirely outside the image, just
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000778 // returns false
779 return false;
780 }
781
reed6c225732014-06-09 19:52:07 -0700782 SkColorType colorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000783 bool hasAlpha = false;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000784 SkPMColor theTranspColor = 0; // 0 tells us not to try to match
785
reed6c225732014-06-09 19:52:07 -0700786 if (!this->getBitmapConfig(png_ptr, info_ptr, &colorType, &hasAlpha, &theTranspColor)) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000787 return false;
788 }
789
790 const int sampleSize = this->getSampleSize();
791 SkScaledBitmapSampler sampler(origWidth, rect.height(), sampleSize);
792
793 SkBitmap decodedBitmap;
reed6c225732014-06-09 19:52:07 -0700794 decodedBitmap.setInfo(SkImageInfo::Make(sampler.scaledWidth(), sampler.scaledHeight(),
795 colorType, kPremul_SkAlphaType));
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000796
797 // from here down we are concerned with colortables and pixels
798
799 // we track if we actually see a non-opaque pixels, since sometimes a PNG sets its colortype
800 // to |= PNG_COLOR_MASK_ALPHA, but all of its pixels are in fact opaque. We care, since we
801 // draw lots faster if we can flag the bitmap has being opaque
802 bool reallyHasAlpha = false;
803 SkColorTable* colorTable = NULL;
804
reed6c225732014-06-09 19:52:07 -0700805 if (pngColorType == PNG_COLOR_TYPE_PALETTE) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000806 decodePalette(png_ptr, info_ptr, &hasAlpha, &reallyHasAlpha, &colorTable);
807 }
808
809 SkAutoUnref aur(colorTable);
810
811 // Check ahead of time if the swap(dest, src) is possible.
812 // If yes, then we will stick to AllocPixelRef since it's cheaper with the swap happening.
813 // If no, then we will use alloc to allocate pixels to prevent garbage collection.
814 int w = rect.width() / sampleSize;
815 int h = rect.height() / sampleSize;
816 const bool swapOnly = (rect == region) && (w == decodedBitmap.width()) &&
817 (h == decodedBitmap.height()) && bm->isNull();
reed6c225732014-06-09 19:52:07 -0700818 const bool needColorTable = kIndex_8_SkColorType == colorType;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000819 if (swapOnly) {
820 if (!this->allocPixelRef(&decodedBitmap, needColorTable ? colorTable : NULL)) {
821 return false;
822 }
823 } else {
824 if (!decodedBitmap.allocPixels(NULL, needColorTable ? colorTable : NULL)) {
825 return false;
826 }
827 }
828 SkAutoLockPixels alp(decodedBitmap);
829
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000830 /* Turn on interlace handling. REQUIRED if you are not using
831 * png_read_image(). To see how to handle interlacing passes,
832 * see the png_read_row() method below:
833 */
834 const int number_passes = (interlaceType != PNG_INTERLACE_NONE) ?
835 png_set_interlace_handling(png_ptr) : 1;
836
837 /* Optional call to gamma correct and add the background to the palette
838 * and update info structure. REQUIRED if you are expecting libpng to
839 * update the palette for you (ie you selected such a transform above).
840 */
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000841
842 // Direct access to png_ptr fields is deprecated in libpng > 1.2.
843#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000844 png_ptr->pass = 0;
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000845#else
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000846 // FIXME: This sets pass as desired, but also sets iwidth. Is that ok?
847 png_set_interlaced_pass(png_ptr, 0);
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000848#endif
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000849 png_read_update_info(png_ptr, info_ptr);
850
851 int actualTop = rect.fTop;
852
reed6c225732014-06-09 19:52:07 -0700853 if ((kAlpha_8_SkColorType == colorType || kIndex_8_SkColorType == colorType)
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000854 && 1 == sampleSize) {
reed6c225732014-06-09 19:52:07 -0700855 if (kAlpha_8_SkColorType == colorType) {
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000856 // For an A8 bitmap, we assume there is an alpha for speed. It is
857 // possible the bitmap is opaque, but that is an unlikely use case
858 // since it would not be very interesting.
859 reallyHasAlpha = true;
860 // A8 is only allowed if the original was GRAY.
reed6c225732014-06-09 19:52:07 -0700861 SkASSERT(PNG_COLOR_TYPE_GRAY == pngColorType);
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000862 }
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000863
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000864 for (int i = 0; i < number_passes; i++) {
865 png_configure_decoder(png_ptr, &actualTop, i);
866 for (int j = 0; j < rect.fTop - actualTop; j++) {
867 uint8_t* bmRow = decodedBitmap.getAddr8(0, 0);
868 png_read_rows(png_ptr, &bmRow, png_bytepp_NULL, 1);
869 }
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000870 png_uint_32 bitmapHeight = (png_uint_32) decodedBitmap.height();
871 for (png_uint_32 y = 0; y < bitmapHeight; y++) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000872 uint8_t* bmRow = decodedBitmap.getAddr8(0, y);
873 png_read_rows(png_ptr, &bmRow, png_bytepp_NULL, 1);
874 }
875 }
876 } else {
877 SkScaledBitmapSampler::SrcConfig sc;
878 int srcBytesPerPixel = 4;
879
880 if (colorTable != NULL) {
881 sc = SkScaledBitmapSampler::kIndex;
882 srcBytesPerPixel = 1;
reed6c225732014-06-09 19:52:07 -0700883 } else if (kAlpha_8_SkColorType == colorType) {
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000884 // A8 is only allowed if the original was GRAY.
reed6c225732014-06-09 19:52:07 -0700885 SkASSERT(PNG_COLOR_TYPE_GRAY == pngColorType);
scroggo@google.comc70a3aa2013-07-18 20:03:15 +0000886 sc = SkScaledBitmapSampler::kGray;
887 srcBytesPerPixel = 1;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000888 } else if (hasAlpha) {
889 sc = SkScaledBitmapSampler::kRGBA;
890 } else {
891 sc = SkScaledBitmapSampler::kRGBX;
892 }
893
894 /* We have to pass the colortable explicitly, since we may have one
895 even if our decodedBitmap doesn't, due to the request that we
896 upscale png's palette to a direct model
897 */
898 SkAutoLockColors ctLock(colorTable);
scroggo@google.com8d239242013-10-01 17:27:15 +0000899 if (!sampler.begin(&decodedBitmap, sc, *this, ctLock.colors())) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000900 return false;
901 }
902 const int height = decodedBitmap.height();
903
904 if (number_passes > 1) {
905 SkAutoMalloc storage(origWidth * origHeight * srcBytesPerPixel);
906 uint8_t* base = (uint8_t*)storage.get();
907 size_t rb = origWidth * srcBytesPerPixel;
908
909 for (int i = 0; i < number_passes; i++) {
910 png_configure_decoder(png_ptr, &actualTop, i);
911 for (int j = 0; j < rect.fTop - actualTop; j++) {
912 uint8_t* bmRow = (uint8_t*)decodedBitmap.getPixels();
913 png_read_rows(png_ptr, &bmRow, png_bytepp_NULL, 1);
914 }
915 uint8_t* row = base;
916 for (int32_t y = 0; y < rect.height(); y++) {
917 uint8_t* bmRow = row;
918 png_read_rows(png_ptr, &bmRow, png_bytepp_NULL, 1);
919 row += rb;
920 }
921 }
922 // now sample it
923 base += sampler.srcY0() * rb;
924 for (int y = 0; y < height; y++) {
925 reallyHasAlpha |= sampler.next(base);
926 base += sampler.srcDY() * rb;
927 }
928 } else {
929 SkAutoMalloc storage(origWidth * srcBytesPerPixel);
930 uint8_t* srcRow = (uint8_t*)storage.get();
931
932 png_configure_decoder(png_ptr, &actualTop, 0);
933 skip_src_rows(png_ptr, srcRow, sampler.srcY0());
934
935 for (int i = 0; i < rect.fTop - actualTop; i++) {
936 uint8_t* bmRow = (uint8_t*)decodedBitmap.getPixels();
937 png_read_rows(png_ptr, &bmRow, png_bytepp_NULL, 1);
938 }
939 for (int y = 0; y < height; y++) {
940 uint8_t* tmp = srcRow;
941 png_read_rows(png_ptr, &tmp, png_bytepp_NULL, 1);
942 reallyHasAlpha |= sampler.next(srcRow);
943 if (y < height - 1) {
944 skip_src_rows(png_ptr, srcRow, sampler.srcDY() - 1);
945 }
946 }
947 }
948 }
949
950 if (0 != theTranspColor) {
951 reallyHasAlpha |= substituteTranspColor(&decodedBitmap, theTranspColor);
952 }
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000953 if (reallyHasAlpha && this->getRequireUnpremultipliedColors()) {
reed6c225732014-06-09 19:52:07 -0700954 switch (decodedBitmap.colorType()) {
955 case kIndex_8_SkColorType:
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000956 // Fall through.
reed6c225732014-06-09 19:52:07 -0700957 case kARGB_4444_SkColorType:
958 // We have chosen not to support unpremul for these colortypess.
scroggo@google.com5ee18dd2013-10-21 20:47:31 +0000959 return false;
960 default: {
961 // Fall through to finish the decode. This config either
962 // supports unpremul or it is irrelevant because it has no
963 // alpha (or only alpha).
964 // These brackets prevent a warning.
965 }
966 }
commit-bot@chromium.org546f70c2013-10-03 17:13:38 +0000967 }
reed@google.com383a6972013-10-21 14:00:07 +0000968 SkAlphaType alphaType = kOpaque_SkAlphaType;
969 if (reallyHasAlpha) {
970 if (this->getRequireUnpremultipliedColors()) {
971 alphaType = kUnpremul_SkAlphaType;
972 } else {
973 alphaType = kPremul_SkAlphaType;
974 }
975 }
976 decodedBitmap.setAlphaType(alphaType);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000977
978 if (swapOnly) {
979 bm->swap(decodedBitmap);
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000980 return true;
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000981 }
scroggo@google.com7e6fcee2013-05-03 20:14:28 +0000982 return this->cropBitmap(bm, &decodedBitmap, sampleSize, region.x(), region.y(),
983 region.width(), region.height(), 0, rect.y());
commit-bot@chromium.orga936e372013-03-14 14:42:18 +0000984}
985#endif
986
reed@android.com8a1c16f2008-12-17 15:59:43 +0000987///////////////////////////////////////////////////////////////////////////////
988
reed@android.com8a1c16f2008-12-17 15:59:43 +0000989#include "SkColorPriv.h"
990#include "SkUnPreMultiply.h"
991
992static void sk_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) {
commit-bot@chromium.orgc1c56952013-05-02 13:41:34 +0000993 SkWStream* sk_stream = (SkWStream*)png_get_io_ptr(png_ptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000994 if (!sk_stream->write(data, len)) {
995 png_error(png_ptr, "sk_write_fn Error!");
996 }
997}
998
reed@android.com8a1c16f2008-12-17 15:59:43 +0000999static transform_scanline_proc choose_proc(SkBitmap::Config config,
1000 bool hasAlpha) {
1001 // we don't care about search on alpha if we're kIndex8, since only the
1002 // colortable packing cares about that distinction, not the pixels
1003 if (SkBitmap::kIndex8_Config == config) {
1004 hasAlpha = false; // we store false in the table entries for kIndex8
1005 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001006
reed@android.com8a1c16f2008-12-17 15:59:43 +00001007 static const struct {
1008 SkBitmap::Config fConfig;
1009 bool fHasAlpha;
1010 transform_scanline_proc fProc;
1011 } gMap[] = {
1012 { SkBitmap::kRGB_565_Config, false, transform_scanline_565 },
1013 { SkBitmap::kARGB_8888_Config, false, transform_scanline_888 },
1014 { SkBitmap::kARGB_8888_Config, true, transform_scanline_8888 },
1015 { SkBitmap::kARGB_4444_Config, false, transform_scanline_444 },
1016 { SkBitmap::kARGB_4444_Config, true, transform_scanline_4444 },
epoger@google.com4ce738b2012-11-16 18:44:18 +00001017 { SkBitmap::kIndex8_Config, false, transform_scanline_memcpy },
reed@android.com8a1c16f2008-12-17 15:59:43 +00001018 };
1019
1020 for (int i = SK_ARRAY_COUNT(gMap) - 1; i >= 0; --i) {
1021 if (gMap[i].fConfig == config && gMap[i].fHasAlpha == hasAlpha) {
1022 return gMap[i].fProc;
1023 }
1024 }
1025 sk_throw();
1026 return NULL;
1027}
1028
1029// return the minimum legal bitdepth (by png standards) for this many colortable
1030// entries. SkBitmap always stores in 8bits per pixel, but for colorcount <= 16,
1031// we can use fewer bits per in png
1032static int computeBitDepth(int colorCount) {
1033#if 0
1034 int bits = SkNextLog2(colorCount);
1035 SkASSERT(bits >= 1 && bits <= 8);
1036 // now we need bits itself to be a power of 2 (e.g. 1, 2, 4, 8)
1037 return SkNextPow2(bits);
1038#else
1039 // for the moment, we don't know how to pack bitdepth < 8
1040 return 8;
1041#endif
1042}
1043
1044/* Pack palette[] with the corresponding colors, and if hasAlpha is true, also
1045 pack trans[] and return the number of trans[] entries written. If hasAlpha
1046 is false, the return value will always be 0.
rmistry@google.comd6176b02012-08-23 18:14:13 +00001047
reed@android.com8a1c16f2008-12-17 15:59:43 +00001048 Note: this routine takes care of unpremultiplying the RGB values when we
1049 have alpha in the colortable, since png doesn't support premul colors
1050*/
reed@android.com6f252972009-01-14 16:46:16 +00001051static inline int pack_palette(SkColorTable* ctable,
reed@android.comb50a60c2009-01-14 17:51:08 +00001052 png_color* SK_RESTRICT palette,
1053 png_byte* SK_RESTRICT trans, bool hasAlpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001054 SkAutoLockColors alc(ctable);
1055 const SkPMColor* SK_RESTRICT colors = alc.colors();
1056 const int ctCount = ctable->count();
1057 int i, num_trans = 0;
1058
1059 if (hasAlpha) {
1060 /* first see if we have some number of fully opaque at the end of the
1061 ctable. PNG allows num_trans < num_palette, but all of the trans
1062 entries must come first in the palette. If I was smarter, I'd
1063 reorder the indices and ctable so that all non-opaque colors came
1064 first in the palette. But, since that would slow down the encode,
1065 I'm leaving the indices and ctable order as is, and just looking
1066 at the tail of the ctable for opaqueness.
1067 */
1068 num_trans = ctCount;
1069 for (i = ctCount - 1; i >= 0; --i) {
1070 if (SkGetPackedA32(colors[i]) != 0xFF) {
1071 break;
1072 }
1073 num_trans -= 1;
1074 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001075
reed@android.com8a1c16f2008-12-17 15:59:43 +00001076 const SkUnPreMultiply::Scale* SK_RESTRICT table =
1077 SkUnPreMultiply::GetScaleTable();
1078
1079 for (i = 0; i < num_trans; i++) {
1080 const SkPMColor c = *colors++;
1081 const unsigned a = SkGetPackedA32(c);
1082 const SkUnPreMultiply::Scale s = table[a];
1083 trans[i] = a;
1084 palette[i].red = SkUnPreMultiply::ApplyScale(s, SkGetPackedR32(c));
1085 palette[i].green = SkUnPreMultiply::ApplyScale(s,SkGetPackedG32(c));
1086 palette[i].blue = SkUnPreMultiply::ApplyScale(s, SkGetPackedB32(c));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001087 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001088 // now fall out of this if-block to use common code for the trailing
1089 // opaque entries
1090 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001091
reed@android.com8a1c16f2008-12-17 15:59:43 +00001092 // these (remaining) entries are opaque
1093 for (i = num_trans; i < ctCount; i++) {
1094 SkPMColor c = *colors++;
1095 palette[i].red = SkGetPackedR32(c);
1096 palette[i].green = SkGetPackedG32(c);
1097 palette[i].blue = SkGetPackedB32(c);
1098 }
1099 return num_trans;
1100}
1101
1102class SkPNGImageEncoder : public SkImageEncoder {
1103protected:
commit-bot@chromium.orga936e372013-03-14 14:42:18 +00001104 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK_OVERRIDE;
tomhudson@google.com5c210c72011-07-28 21:06:40 +00001105private:
1106 bool doEncode(SkWStream* stream, const SkBitmap& bm,
1107 const bool& hasAlpha, int colorType,
1108 int bitDepth, SkBitmap::Config config,
1109 png_color_8& sig_bit);
commit-bot@chromium.orga936e372013-03-14 14:42:18 +00001110
1111 typedef SkImageEncoder INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001112};
1113
1114bool SkPNGImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap,
1115 int /*quality*/) {
reed@google.com44699382013-10-31 17:28:30 +00001116 SkBitmap::Config config = bitmap.config();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001117
1118 const bool hasAlpha = !bitmap.isOpaque();
1119 int colorType = PNG_COLOR_MASK_COLOR;
1120 int bitDepth = 8; // default for color
1121 png_color_8 sig_bit;
1122
1123 switch (config) {
1124 case SkBitmap::kIndex8_Config:
1125 colorType |= PNG_COLOR_MASK_PALETTE;
1126 // fall through to the ARGB_8888 case
1127 case SkBitmap::kARGB_8888_Config:
1128 sig_bit.red = 8;
1129 sig_bit.green = 8;
1130 sig_bit.blue = 8;
1131 sig_bit.alpha = 8;
1132 break;
1133 case SkBitmap::kARGB_4444_Config:
1134 sig_bit.red = 4;
1135 sig_bit.green = 4;
1136 sig_bit.blue = 4;
1137 sig_bit.alpha = 4;
1138 break;
1139 case SkBitmap::kRGB_565_Config:
1140 sig_bit.red = 5;
1141 sig_bit.green = 6;
1142 sig_bit.blue = 5;
1143 sig_bit.alpha = 0;
1144 break;
1145 default:
1146 return false;
1147 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001148
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149 if (hasAlpha) {
1150 // don't specify alpha if we're a palette, even if our ctable has alpha
1151 if (!(colorType & PNG_COLOR_MASK_PALETTE)) {
1152 colorType |= PNG_COLOR_MASK_ALPHA;
1153 }
1154 } else {
1155 sig_bit.alpha = 0;
1156 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001157
reed@android.com8a1c16f2008-12-17 15:59:43 +00001158 SkAutoLockPixels alp(bitmap);
1159 // readyToDraw checks for pixels (and colortable if that is required)
1160 if (!bitmap.readyToDraw()) {
1161 return false;
1162 }
1163
1164 // we must do this after we have locked the pixels
1165 SkColorTable* ctable = bitmap.getColorTable();
1166 if (NULL != ctable) {
1167 if (ctable->count() == 0) {
1168 return false;
1169 }
1170 // check if we can store in fewer than 8 bits
1171 bitDepth = computeBitDepth(ctable->count());
1172 }
1173
tomhudson@google.com5c210c72011-07-28 21:06:40 +00001174 return doEncode(stream, bitmap, hasAlpha, colorType,
1175 bitDepth, config, sig_bit);
1176}
1177
1178bool SkPNGImageEncoder::doEncode(SkWStream* stream, const SkBitmap& bitmap,
1179 const bool& hasAlpha, int colorType,
1180 int bitDepth, SkBitmap::Config config,
1181 png_color_8& sig_bit) {
1182
reed@android.com8a1c16f2008-12-17 15:59:43 +00001183 png_structp png_ptr;
1184 png_infop info_ptr;
1185
1186 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, sk_error_fn,
1187 NULL);
1188 if (NULL == png_ptr) {
1189 return false;
1190 }
1191
1192 info_ptr = png_create_info_struct(png_ptr);
1193 if (NULL == info_ptr) {
commit-bot@chromium.orga936e372013-03-14 14:42:18 +00001194 png_destroy_write_struct(&png_ptr, png_infopp_NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001195 return false;
1196 }
1197
1198 /* Set error handling. REQUIRED if you aren't supplying your own
1199 * error handling functions in the png_create_write_struct() call.
1200 */
1201 if (setjmp(png_jmpbuf(png_ptr))) {
1202 png_destroy_write_struct(&png_ptr, &info_ptr);
1203 return false;
1204 }
1205
commit-bot@chromium.orga936e372013-03-14 14:42:18 +00001206 png_set_write_fn(png_ptr, (void*)stream, sk_write_fn, png_flush_ptr_NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001207
1208 /* Set the image information here. Width and height are up to 2^31,
1209 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
1210 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
1211 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
1212 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
1213 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
1214 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
1215 */
1216
1217 png_set_IHDR(png_ptr, info_ptr, bitmap.width(), bitmap.height(),
1218 bitDepth, colorType,
1219 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
1220 PNG_FILTER_TYPE_BASE);
1221
reed@android.com61898772009-07-07 19:38:01 +00001222 // set our colortable/trans arrays if needed
1223 png_color paletteColors[256];
1224 png_byte trans[256];
1225 if (SkBitmap::kIndex8_Config == config) {
1226 SkColorTable* ct = bitmap.getColorTable();
1227 int numTrans = pack_palette(ct, paletteColors, trans, hasAlpha);
1228 png_set_PLTE(png_ptr, info_ptr, paletteColors, ct->count());
1229 if (numTrans > 0) {
1230 png_set_tRNS(png_ptr, info_ptr, trans, numTrans, NULL);
1231 }
1232 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001233
1234 png_set_sBIT(png_ptr, info_ptr, &sig_bit);
1235 png_write_info(png_ptr, info_ptr);
1236
1237 const char* srcImage = (const char*)bitmap.getPixels();
1238 SkAutoSMalloc<1024> rowStorage(bitmap.width() << 2);
1239 char* storage = (char*)rowStorage.get();
1240 transform_scanline_proc proc = choose_proc(config, hasAlpha);
1241
1242 for (int y = 0; y < bitmap.height(); y++) {
1243 png_bytep row_ptr = (png_bytep)storage;
1244 proc(srcImage, bitmap.width(), storage);
1245 png_write_rows(png_ptr, &row_ptr, 1);
1246 srcImage += bitmap.rowBytes();
1247 }
1248
1249 png_write_end(png_ptr, info_ptr);
1250
1251 /* clean up after the write, and free any memory allocated */
1252 png_destroy_write_struct(&png_ptr, &info_ptr);
1253 return true;
1254}
1255
reed@android.com00bf85a2009-01-22 13:04:56 +00001256///////////////////////////////////////////////////////////////////////////////
robertphillips@google.comec51cb82012-03-23 18:13:47 +00001257DEFINE_DECODER_CREATOR(PNGImageDecoder);
1258DEFINE_ENCODER_CREATOR(PNGImageEncoder);
1259///////////////////////////////////////////////////////////////////////////////
reed@android.com00bf85a2009-01-22 13:04:56 +00001260
scroggo@google.comb5571b32013-09-25 21:34:24 +00001261static bool is_png(SkStreamRewindable* stream) {
reed@android.com00bf85a2009-01-22 13:04:56 +00001262 char buf[PNG_BYTES_TO_CHECK];
1263 if (stream->read(buf, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK &&
1264 !png_sig_cmp((png_bytep) buf, (png_size_t)0, PNG_BYTES_TO_CHECK)) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +00001265 return true;
1266 }
1267 return false;
1268}
1269
scroggo@google.comb5571b32013-09-25 21:34:24 +00001270SkImageDecoder* sk_libpng_dfactory(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +00001271 if (is_png(stream)) {
reed@android.com00bf85a2009-01-22 13:04:56 +00001272 return SkNEW(SkPNGImageDecoder);
1273 }
1274 return NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001275}
1276
scroggo@google.comb5571b32013-09-25 21:34:24 +00001277static SkImageDecoder::Format get_format_png(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +00001278 if (is_png(stream)) {
1279 return SkImageDecoder::kPNG_Format;
1280 }
1281 return SkImageDecoder::kUnknown_Format;
1282}
1283
reed@android.comdfee5792010-04-15 14:24:50 +00001284SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) {
reed@android.com00bf85a2009-01-22 13:04:56 +00001285 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL;
1286}
1287
mtklein@google.combd6343b2013-09-04 17:20:18 +00001288static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory);
1289static SkImageDecoder_FormatReg gFormatReg(get_format_png);
1290static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory);