| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/gfx/png_decoder.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | |
| 9 | |
| 10 extern "C" { | |
| 11 #if defined(USE_SYSTEM_LIBPNG) | |
| 12 #include <png.h> | |
| 13 #else | |
| 14 #include "third_party/libpng/png.h" | |
| 15 #endif | |
| 16 } | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Converts BGRA->RGBA and RGBA->BGRA. | |
| 21 void ConvertBetweenBGRAandRGBA(const unsigned char* input, int pixel_width, | |
| 22 unsigned char* output) { | |
| 23 for (int x = 0; x < pixel_width; x++) { | |
| 24 const unsigned char* pixel_in = &input[x * 4]; | |
| 25 unsigned char* pixel_out = &output[x * 4]; | |
| 26 pixel_out[0] = pixel_in[2]; | |
| 27 pixel_out[1] = pixel_in[1]; | |
| 28 pixel_out[2] = pixel_in[0]; | |
| 29 pixel_out[3] = pixel_in[3]; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 void ConvertRGBAtoRGB(const unsigned char* rgba, int pixel_width, | |
| 34 unsigned char* rgb) { | |
| 35 for (int x = 0; x < pixel_width; x++) { | |
| 36 const unsigned char* pixel_in = &rgba[x * 4]; | |
| 37 unsigned char* pixel_out = &rgb[x * 3]; | |
| 38 pixel_out[0] = pixel_in[0]; | |
| 39 pixel_out[1] = pixel_in[1]; | |
| 40 pixel_out[2] = pixel_in[2]; | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 // Decoder -------------------------------------------------------------------- | |
| 47 // | |
| 48 // This code is based on WebKit libpng interface (PNGImageDecoder), which is | |
| 49 // in turn based on the Mozilla png decoder. | |
| 50 | |
| 51 namespace { | |
| 52 | |
| 53 // Gamma constants: We assume we're on Windows which uses a gamma of 2.2. | |
| 54 const double kMaxGamma = 21474.83; // Maximum gamma accepted by png library. | |
| 55 const double kDefaultGamma = 2.2; | |
| 56 const double kInverseGamma = 1.0 / kDefaultGamma; | |
| 57 | |
| 58 class PngDecoderState { | |
| 59 public: | |
| 60 PngDecoderState(PNGDecoder::ColorFormat ofmt, std::vector<unsigned char>* o) | |
| 61 : output_format(ofmt), | |
| 62 output_channels(0), | |
| 63 output(o), | |
| 64 row_converter(NULL), | |
| 65 width(0), | |
| 66 height(0), | |
| 67 done(false) { | |
| 68 } | |
| 69 | |
| 70 PNGDecoder::ColorFormat output_format; | |
| 71 int output_channels; | |
| 72 | |
| 73 std::vector<unsigned char>* output; | |
| 74 | |
| 75 // Called to convert a row from the library to the correct output format. | |
| 76 // When NULL, no conversion is necessary. | |
| 77 void (*row_converter)(const unsigned char* in, int w, unsigned char* out); | |
| 78 | |
| 79 // Size of the image, set in the info callback. | |
| 80 int width; | |
| 81 int height; | |
| 82 | |
| 83 // Set to true when we've found the end of the data. | |
| 84 bool done; | |
| 85 | |
| 86 private: | |
| 87 DISALLOW_EVIL_CONSTRUCTORS(PngDecoderState); | |
| 88 }; | |
| 89 | |
| 90 void ConvertRGBtoRGBA(const unsigned char* rgb, int pixel_width, | |
| 91 unsigned char* rgba) { | |
| 92 for (int x = 0; x < pixel_width; x++) { | |
| 93 const unsigned char* pixel_in = &rgb[x * 3]; | |
| 94 unsigned char* pixel_out = &rgba[x * 4]; | |
| 95 pixel_out[0] = pixel_in[0]; | |
| 96 pixel_out[1] = pixel_in[1]; | |
| 97 pixel_out[2] = pixel_in[2]; | |
| 98 pixel_out[3] = 0xff; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void ConvertRGBtoBGRA(const unsigned char* rgb, int pixel_width, | |
| 103 unsigned char* bgra) { | |
| 104 for (int x = 0; x < pixel_width; x++) { | |
| 105 const unsigned char* pixel_in = &rgb[x * 3]; | |
| 106 unsigned char* pixel_out = &bgra[x * 4]; | |
| 107 pixel_out[0] = pixel_in[2]; | |
| 108 pixel_out[1] = pixel_in[1]; | |
| 109 pixel_out[2] = pixel_in[0]; | |
| 110 pixel_out[3] = 0xff; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 // Called when the png header has been read. This code is based on the WebKit | |
| 115 // PNGImageDecoder | |
| 116 void DecodeInfoCallback(png_struct* png_ptr, png_info* info_ptr) { | |
| 117 PngDecoderState* state = static_cast<PngDecoderState*>( | |
| 118 png_get_progressive_ptr(png_ptr)); | |
| 119 | |
| 120 int bit_depth, color_type, interlace_type, compression_type; | |
| 121 int filter_type, channels; | |
| 122 png_uint_32 w, h; | |
| 123 png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, | |
| 124 &interlace_type, &compression_type, &filter_type); | |
| 125 | |
| 126 // Bounds check. When the image is unreasonably big, we'll error out and | |
| 127 // end up back at the setjmp call when we set up decoding. "Unreasonably big" | |
| 128 // means "big enough that w * h * 32bpp might overflow an int"; we choose this | |
| 129 // threshold to match WebKit and because a number of places in code assume | |
| 130 // that an image's size (in bytes) fits in a (signed) int. | |
| 131 unsigned long long total_size = | |
| 132 static_cast<unsigned long long>(w) * static_cast<unsigned long long>(h); | |
| 133 if (total_size > ((1 << 29) - 1)) | |
| 134 longjmp(png_ptr->jmpbuf, 1); | |
| 135 state->width = static_cast<int>(w); | |
| 136 state->height = static_cast<int>(h); | |
| 137 | |
| 138 // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA. | |
| 139 if (color_type == PNG_COLOR_TYPE_PALETTE || | |
| 140 (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)) | |
| 141 png_set_expand(png_ptr); | |
| 142 | |
| 143 // Transparency for paletted images. | |
| 144 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) | |
| 145 png_set_expand(png_ptr); | |
| 146 | |
| 147 // Convert 16-bit to 8-bit. | |
| 148 if (bit_depth == 16) | |
| 149 png_set_strip_16(png_ptr); | |
| 150 | |
| 151 // Expand grayscale to RGB. | |
| 152 if (color_type == PNG_COLOR_TYPE_GRAY || | |
| 153 color_type == PNG_COLOR_TYPE_GRAY_ALPHA) | |
| 154 png_set_gray_to_rgb(png_ptr); | |
| 155 | |
| 156 // Deal with gamma and keep it under our control. | |
| 157 double gamma; | |
| 158 if (png_get_gAMA(png_ptr, info_ptr, &gamma)) { | |
| 159 if (gamma <= 0.0 || gamma > kMaxGamma) { | |
| 160 gamma = kInverseGamma; | |
| 161 png_set_gAMA(png_ptr, info_ptr, gamma); | |
| 162 } | |
| 163 png_set_gamma(png_ptr, kDefaultGamma, gamma); | |
| 164 } else { | |
| 165 png_set_gamma(png_ptr, kDefaultGamma, kInverseGamma); | |
| 166 } | |
| 167 | |
| 168 // Tell libpng to send us rows for interlaced pngs. | |
| 169 if (interlace_type == PNG_INTERLACE_ADAM7) | |
| 170 png_set_interlace_handling(png_ptr); | |
| 171 | |
| 172 // Update our info now | |
| 173 png_read_update_info(png_ptr, info_ptr); | |
| 174 channels = png_get_channels(png_ptr, info_ptr); | |
| 175 | |
| 176 // Pick our row format converter necessary for this data. | |
| 177 if (channels == 3) { | |
| 178 switch (state->output_format) { | |
| 179 case PNGDecoder::FORMAT_RGB: | |
| 180 state->row_converter = NULL; // no conversion necessary | |
| 181 state->output_channels = 3; | |
| 182 break; | |
| 183 case PNGDecoder::FORMAT_RGBA: | |
| 184 state->row_converter = &ConvertRGBtoRGBA; | |
| 185 state->output_channels = 4; | |
| 186 break; | |
| 187 case PNGDecoder::FORMAT_BGRA: | |
| 188 state->row_converter = &ConvertRGBtoBGRA; | |
| 189 state->output_channels = 4; | |
| 190 break; | |
| 191 default: | |
| 192 NOTREACHED() << "Unknown output format"; | |
| 193 break; | |
| 194 } | |
| 195 } else if (channels == 4) { | |
| 196 switch (state->output_format) { | |
| 197 case PNGDecoder::FORMAT_RGB: | |
| 198 state->row_converter = &ConvertRGBAtoRGB; | |
| 199 state->output_channels = 3; | |
| 200 break; | |
| 201 case PNGDecoder::FORMAT_RGBA: | |
| 202 state->row_converter = NULL; // no conversion necessary | |
| 203 state->output_channels = 4; | |
| 204 break; | |
| 205 case PNGDecoder::FORMAT_BGRA: | |
| 206 state->row_converter = &ConvertBetweenBGRAandRGBA; | |
| 207 state->output_channels = 4; | |
| 208 break; | |
| 209 default: | |
| 210 NOTREACHED() << "Unknown output format"; | |
| 211 break; | |
| 212 } | |
| 213 } else { | |
| 214 NOTREACHED() << "Unknown input channels"; | |
| 215 longjmp(png_ptr->jmpbuf, 1); | |
| 216 } | |
| 217 | |
| 218 state->output->resize(state->width * state->output_channels * state->height); | |
| 219 } | |
| 220 | |
| 221 void DecodeRowCallback(png_struct* png_ptr, png_byte* new_row, | |
| 222 png_uint_32 row_num, int pass) { | |
| 223 PngDecoderState* state = static_cast<PngDecoderState*>( | |
| 224 png_get_progressive_ptr(png_ptr)); | |
| 225 | |
| 226 DCHECK(pass == 0) << "We didn't turn on interlace handling, but libpng is " | |
| 227 "giving us interlaced data."; | |
| 228 if (static_cast<int>(row_num) > state->height) { | |
| 229 NOTREACHED() << "Invalid row"; | |
| 230 return; | |
| 231 } | |
| 232 | |
| 233 unsigned char* dest = &(*state->output)[ | |
| 234 state->width * state->output_channels * row_num]; | |
| 235 if (state->row_converter) | |
| 236 state->row_converter(new_row, state->width, dest); | |
| 237 else | |
| 238 memcpy(dest, new_row, state->width * state->output_channels); | |
| 239 } | |
| 240 | |
| 241 void DecodeEndCallback(png_struct* png_ptr, png_info* info) { | |
| 242 PngDecoderState* state = static_cast<PngDecoderState*>( | |
| 243 png_get_progressive_ptr(png_ptr)); | |
| 244 | |
| 245 // Mark the image as complete, this will tell the Decode function that we | |
| 246 // have successfully found the end of the data. | |
| 247 state->done = true; | |
| 248 } | |
| 249 | |
| 250 // Automatically destroys the given read structs on destruction to make | |
| 251 // cleanup and error handling code cleaner. | |
| 252 class PngReadStructDestroyer { | |
| 253 public: | |
| 254 PngReadStructDestroyer(png_struct** ps, png_info** pi) : ps_(ps), pi_(pi) { | |
| 255 } | |
| 256 ~PngReadStructDestroyer() { | |
| 257 png_destroy_read_struct(ps_, pi_, NULL); | |
| 258 } | |
| 259 private: | |
| 260 png_struct** ps_; | |
| 261 png_info** pi_; | |
| 262 }; | |
| 263 | |
| 264 } // namespace | |
| 265 | |
| 266 // static | |
| 267 bool PNGDecoder::Decode(const unsigned char* input, size_t input_size, | |
| 268 ColorFormat format, std::vector<unsigned char>* output, | |
| 269 int* w, int* h) { | |
| 270 if (input_size < 8) | |
| 271 return false; // Input data too small to be a png | |
| 272 | |
| 273 // Have libpng check the signature, it likes the first 8 bytes. | |
| 274 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0) | |
| 275 return false; | |
| 276 | |
| 277 png_struct* png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, | |
| 278 png_voidp_NULL, | |
| 279 png_error_ptr_NULL, | |
| 280 png_error_ptr_NULL); | |
| 281 if (!png_ptr) | |
| 282 return false; | |
| 283 | |
| 284 png_info* info_ptr = png_create_info_struct(png_ptr); | |
| 285 if (!info_ptr) { | |
| 286 png_destroy_read_struct(&png_ptr, NULL, NULL); | |
| 287 return false; | |
| 288 } | |
| 289 | |
| 290 PngReadStructDestroyer destroyer(&png_ptr, &info_ptr); | |
| 291 if (setjmp(png_jmpbuf(png_ptr))) { | |
| 292 // The destroyer will ensure that the structures are cleaned up in this | |
| 293 // case, even though we may get here as a jump from random parts of the | |
| 294 // PNG library called below. | |
| 295 return false; | |
| 296 } | |
| 297 | |
| 298 PngDecoderState state(format, output); | |
| 299 | |
| 300 png_set_progressive_read_fn(png_ptr, &state, &DecodeInfoCallback, | |
| 301 &DecodeRowCallback, &DecodeEndCallback); | |
| 302 png_process_data(png_ptr, | |
| 303 info_ptr, | |
| 304 const_cast<unsigned char*>(input), | |
| 305 input_size); | |
| 306 | |
| 307 if (!state.done) { | |
| 308 // Fed it all the data but the library didn't think we got all the data, so | |
| 309 // this file must be truncated. | |
| 310 output->clear(); | |
| 311 return false; | |
| 312 } | |
| 313 | |
| 314 *w = state.width; | |
| 315 *h = state.height; | |
| 316 return true; | |
| 317 } | |
| 318 | |
| 319 // static | |
| 320 bool PNGDecoder::Decode(const std::vector<unsigned char>* data, | |
| 321 SkBitmap* bitmap) { | |
| 322 DCHECK(bitmap); | |
| 323 if (!data || data->empty()) | |
| 324 return false; | |
| 325 int width, height; | |
| 326 std::vector<unsigned char> decoded_data; | |
| 327 if (PNGDecoder::Decode(&data->front(), data->size(), PNGDecoder::FORMAT_BGRA, | |
| 328 &decoded_data, &width, &height)) { | |
| 329 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 330 bitmap->allocPixels(); | |
| 331 unsigned char* bitmap_data = | |
| 332 reinterpret_cast<unsigned char*>(bitmap->getAddr32(0, 0)); | |
| 333 for (int i = width * height * 4 - 4; i >= 0; i -= 4) { | |
| 334 unsigned char alpha = decoded_data[i + 3]; | |
| 335 if (alpha != 0 && alpha != 255) { | |
| 336 SkColor premultiplied = SkPreMultiplyARGB(alpha, | |
| 337 decoded_data[i], decoded_data[i + 1], decoded_data[i + 2]); | |
| 338 bitmap_data[i + 3] = alpha; | |
| 339 bitmap_data[i] = SkColorGetR(premultiplied); | |
| 340 bitmap_data[i + 1] = SkColorGetG(premultiplied); | |
| 341 bitmap_data[i + 2] = SkColorGetB(premultiplied); | |
| 342 } else { | |
| 343 bitmap_data[i + 3] = alpha; | |
| 344 bitmap_data[i] = decoded_data[i]; | |
| 345 bitmap_data[i + 1] = decoded_data[i + 1]; | |
| 346 bitmap_data[i + 2] = decoded_data[i + 2]; | |
| 347 } | |
| 348 } | |
| 349 return true; | |
| 350 } | |
| 351 return false; | |
| 352 } | |
| 353 | |
| 354 //static | |
| 355 SkBitmap* PNGDecoder::CreateSkBitmapFromBGRAFormat( | |
| 356 std::vector<unsigned char>& bgra, int width, int height) { | |
| 357 SkBitmap* bitmap = new SkBitmap(); | |
| 358 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 359 bitmap->allocPixels(); | |
| 360 | |
| 361 bool opaque = false; | |
| 362 unsigned char* bitmap_data = | |
| 363 reinterpret_cast<unsigned char*>(bitmap->getAddr32(0, 0)); | |
| 364 for (int i = width * height * 4 - 4; i >= 0; i -= 4) { | |
| 365 unsigned char alpha = bgra[i + 3]; | |
| 366 if (!opaque && alpha != 255) { | |
| 367 opaque = false; | |
| 368 } | |
| 369 bitmap_data[i + 3] = alpha; | |
| 370 bitmap_data[i] = (bgra[i] * alpha) >> 8; | |
| 371 bitmap_data[i + 1] = (bgra[i + 1] * alpha) >> 8; | |
| 372 bitmap_data[i + 2] = (bgra[i + 2] * alpha) >> 8; | |
| 373 } | |
| 374 | |
| 375 bitmap->setIsOpaque(opaque); | |
| 376 return bitmap; | |
| 377 } | |
| OLD | NEW |