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