| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/gfx/png_decoder.h" | 5 #include "base/gfx/png_decoder.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 | 9 |
| 10 extern "C" { | 10 extern "C" { |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 } | 254 } |
| 255 private: | 255 private: |
| 256 png_struct** ps_; | 256 png_struct** ps_; |
| 257 png_info** pi_; | 257 png_info** pi_; |
| 258 }; | 258 }; |
| 259 | 259 |
| 260 } // namespace | 260 } // namespace |
| 261 | 261 |
| 262 // static | 262 // static |
| 263 bool PNGDecoder::Decode(const unsigned char* input, size_t input_size, | 263 bool PNGDecoder::Decode(const unsigned char* input, size_t input_size, |
| 264 ColorFormat format, std::vector<unsigned char>* output, | 264 ColorFormat format, std::vector<unsigned char>* output, |
| 265 int* w, int* h) { | 265 int* w, int* h) { |
| 266 if (input_size < 8) | 266 if (input_size < 8) |
| 267 return false; // Input data too small to be a png | 267 return false; // Input data too small to be a png |
| 268 | 268 |
| 269 // Have libpng check the signature, it likes the first 8 bytes. | 269 // Have libpng check the signature, it likes the first 8 bytes. |
| 270 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0) | 270 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0) |
| 271 return false; | 271 return false; |
| 272 | 272 |
| 273 png_struct* png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, | 273 png_struct* png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, |
| 274 png_voidp_NULL, | 274 png_voidp_NULL, |
| 275 png_error_ptr_NULL, | 275 png_error_ptr_NULL, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 SkBitmap* bitmap) { | 317 SkBitmap* bitmap) { |
| 318 DCHECK(bitmap); | 318 DCHECK(bitmap); |
| 319 if (!data || data->empty()) | 319 if (!data || data->empty()) |
| 320 return false; | 320 return false; |
| 321 int width, height; | 321 int width, height; |
| 322 std::vector<unsigned char> decoded_data; | 322 std::vector<unsigned char> decoded_data; |
| 323 if (PNGDecoder::Decode(&data->front(), data->size(), PNGDecoder::FORMAT_BGRA, | 323 if (PNGDecoder::Decode(&data->front(), data->size(), PNGDecoder::FORMAT_BGRA, |
| 324 &decoded_data, &width, &height)) { | 324 &decoded_data, &width, &height)) { |
| 325 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | 325 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 326 bitmap->allocPixels(); | 326 bitmap->allocPixels(); |
| 327 memcpy(bitmap->getPixels(), &decoded_data.front(), width * height * 4); | 327 unsigned char* bitmap_data = |
| 328 reinterpret_cast<unsigned char*>(bitmap->getAddr32(0, 0)); |
| 329 for (int i = width * height * 4 - 4; i >= 0; i -= 4) { |
| 330 unsigned char alpha = decoded_data[i + 3]; |
| 331 if (alpha != 0 && alpha != 255) { |
| 332 bitmap_data[i + 3] = alpha; |
| 333 bitmap_data[i] = (decoded_data[i] * alpha) >> 8; |
| 334 bitmap_data[i + 1] = (decoded_data[i + 1] * alpha) >> 8; |
| 335 bitmap_data[i + 2] = (decoded_data[i + 2] * alpha) >> 8; |
| 336 } else { |
| 337 bitmap_data[i + 3] = alpha; |
| 338 bitmap_data[i] = decoded_data[i]; |
| 339 bitmap_data[i + 1] = decoded_data[i + 1]; |
| 340 bitmap_data[i + 2] = decoded_data[i + 2]; |
| 341 } |
| 342 } |
| 328 return true; | 343 return true; |
| 329 } | 344 } |
| 330 return false; | 345 return false; |
| 331 } | 346 } |
| 332 | 347 |
| 333 //static | 348 //static |
| 334 SkBitmap* PNGDecoder::CreateSkBitmapFromBGRAFormat( | 349 SkBitmap* PNGDecoder::CreateSkBitmapFromBGRAFormat( |
| 335 std::vector<unsigned char>& bgra, int width, int height) { | 350 std::vector<unsigned char>& bgra, int width, int height) { |
| 336 SkBitmap* bitmap = new SkBitmap(); | 351 SkBitmap* bitmap = new SkBitmap(); |
| 337 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | 352 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 338 bitmap->allocPixels(); | 353 bitmap->allocPixels(); |
| 339 | 354 |
| 340 bool opaque = false; | 355 bool opaque = false; |
| 341 unsigned char* bitmap_data = | 356 unsigned char* bitmap_data = |
| 342 reinterpret_cast<unsigned char*>(bitmap->getAddr32(0, 0)); | 357 reinterpret_cast<unsigned char*>(bitmap->getAddr32(0, 0)); |
| 343 for (int i = width * height * 4 - 4; i >= 0; i -= 4) { | 358 for (int i = width * height * 4 - 4; i >= 0; i -= 4) { |
| 344 unsigned char alpha = bgra[i + 3]; | 359 unsigned char alpha = bgra[i + 3]; |
| 345 if (!opaque && alpha != 255) { | 360 if (!opaque && alpha != 255) { |
| 346 opaque = false; | 361 opaque = false; |
| 347 } | 362 } |
| 348 bitmap_data[i + 3] = alpha; | 363 bitmap_data[i + 3] = alpha; |
| 349 bitmap_data[i] = (bgra[i] * alpha) >> 8; | 364 bitmap_data[i] = (bgra[i] * alpha) >> 8; |
| 350 bitmap_data[i + 1] = (bgra[i + 1] * alpha) >> 8; | 365 bitmap_data[i + 1] = (bgra[i + 1] * alpha) >> 8; |
| 351 bitmap_data[i + 2] = (bgra[i + 2] * alpha) >> 8; | 366 bitmap_data[i + 2] = (bgra[i + 2] * alpha) >> 8; |
| 352 } | 367 } |
| 353 | 368 |
| 354 bitmap->setIsOpaque(opaque); | 369 bitmap->setIsOpaque(opaque); |
| 355 return bitmap; | 370 return bitmap; |
| 356 } | 371 } |
| OLD | NEW |