| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/image/image_util.h" | 5 #include "ui/gfx/image/image_util.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "ui/gfx/codec/jpeg_codec.h" | 9 #include "ui/gfx/codec/jpeg_codec.h" |
| 10 #include "ui/gfx/codec/png_codec.h" | |
| 11 #include "ui/gfx/image/image.h" | 10 #include "ui/gfx/image/image.h" |
| 11 #include "ui/gfx/image/image_skia.h" |
| 12 | 12 |
| 13 namespace gfx { | 13 namespace gfx { |
| 14 | 14 |
| 15 Image* ImageFromPNGEncodedData(const unsigned char* input, size_t input_size) { | 15 Image* ImageFromPNGEncodedData(const unsigned char* input, size_t input_size) { |
| 16 SkBitmap bitmap; | 16 Image* image = new Image(input, input_size); |
| 17 if (gfx::PNGCodec::Decode(input, input_size, &bitmap)) | 17 // Check if image can be converted to an ImageSkia and return NULL if |
| 18 return new Image(bitmap); | 18 // conversion fails. |
| 19 | 19 if (image->ToImageSkia()->empty()) |
| 20 return NULL; | 20 return NULL; |
| 21 return image; |
| 21 } | 22 } |
| 22 | 23 |
| 23 Image ImageFromJPEGEncodedData(const unsigned char* input, size_t input_size) { | 24 Image ImageFromJPEGEncodedData(const unsigned char* input, size_t input_size) { |
| 24 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); | 25 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); |
| 25 if (bitmap.get()) | 26 if (bitmap.get()) |
| 26 return Image(*bitmap); | 27 return Image(*bitmap); |
| 27 | 28 |
| 28 return Image(); | 29 return Image(); |
| 29 } | 30 } |
| 30 | 31 |
| 31 bool PNGEncodedDataFromImage(const Image& image, | 32 bool PNGEncodedDataFromImage(const Image& image, |
| 32 std::vector<unsigned char>* dst) { | 33 std::vector<unsigned char>* dst) { |
| 33 const SkBitmap& bitmap = *image.ToSkBitmap(); | 34 *dst = *image.ToImagePNG(); |
| 34 return gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, dst); | 35 return !dst->empty(); |
| 35 } | 36 } |
| 36 | 37 |
| 37 bool JPEGEncodedDataFromImage(const Image& image, int quality, | 38 bool JPEGEncodedDataFromImage(const Image& image, int quality, |
| 38 std::vector<unsigned char>* dst) { | 39 std::vector<unsigned char>* dst) { |
| 39 const SkBitmap& bitmap = *image.ToSkBitmap(); | 40 const SkBitmap& bitmap = *image.ToSkBitmap(); |
| 40 SkAutoLockPixels bitmap_lock(bitmap); | 41 SkAutoLockPixels bitmap_lock(bitmap); |
| 41 | 42 |
| 42 if (!bitmap.readyToDraw()) | 43 if (!bitmap.readyToDraw()) |
| 43 return false; | 44 return false; |
| 44 | 45 |
| 45 return gfx::JPEGCodec::Encode( | 46 return gfx::JPEGCodec::Encode( |
| 46 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 47 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 47 gfx::JPEGCodec::FORMAT_BGRA, bitmap.width(), | 48 gfx::JPEGCodec::FORMAT_BGRA, bitmap.width(), |
| 48 bitmap.height(), | 49 bitmap.height(), |
| 49 static_cast<int>(bitmap.rowBytes()), quality, | 50 static_cast<int>(bitmap.rowBytes()), quality, |
| 50 dst); | 51 dst); |
| 51 } | 52 } |
| 52 | 53 |
| 53 } | 54 } |
| OLD | NEW |