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" |
10 #include "ui/gfx/image/image.h" | 11 #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 Image* image = new Image(input, input_size); | 16 SkBitmap bitmap; |
17 return image; | 17 if (gfx::PNGCodec::Decode(input, input_size, &bitmap)) |
| 18 return new Image(bitmap); |
| 19 |
| 20 return NULL; |
18 } | 21 } |
19 | 22 |
20 Image ImageFromJPEGEncodedData(const unsigned char* input, size_t input_size) { | 23 Image ImageFromJPEGEncodedData(const unsigned char* input, size_t input_size) { |
21 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); | 24 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); |
22 if (bitmap.get()) | 25 if (bitmap.get()) |
23 return Image(*bitmap); | 26 return Image(*bitmap); |
24 | 27 |
25 return Image(); | 28 return Image(); |
26 } | 29 } |
27 | 30 |
28 bool PNGEncodedDataFromImage(const Image& image, | 31 bool PNGEncodedDataFromImage(const Image& image, |
29 std::vector<unsigned char>* dst) { | 32 std::vector<unsigned char>* dst) { |
30 *dst = *image.ToImagePNG(); | 33 const SkBitmap& bitmap = *image.ToSkBitmap(); |
31 return !dst->empty(); | 34 return gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, dst); |
32 } | 35 } |
33 | 36 |
34 bool JPEGEncodedDataFromImage(const Image& image, int quality, | 37 bool JPEGEncodedDataFromImage(const Image& image, int quality, |
35 std::vector<unsigned char>* dst) { | 38 std::vector<unsigned char>* dst) { |
36 const SkBitmap& bitmap = *image.ToSkBitmap(); | 39 const SkBitmap& bitmap = *image.ToSkBitmap(); |
37 SkAutoLockPixels bitmap_lock(bitmap); | 40 SkAutoLockPixels bitmap_lock(bitmap); |
38 | 41 |
39 if (!bitmap.readyToDraw()) | 42 if (!bitmap.readyToDraw()) |
40 return false; | 43 return false; |
41 | 44 |
42 return gfx::JPEGCodec::Encode( | 45 return gfx::JPEGCodec::Encode( |
43 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 46 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
44 gfx::JPEGCodec::FORMAT_BGRA, bitmap.width(), | 47 gfx::JPEGCodec::FORMAT_BGRA, bitmap.width(), |
45 bitmap.height(), | 48 bitmap.height(), |
46 static_cast<int>(bitmap.rowBytes()), quality, | 49 static_cast<int>(bitmap.rowBytes()), quality, |
47 dst); | 50 dst); |
48 } | 51 } |
49 | 52 |
50 } | 53 } |
OLD | NEW |