Chromium Code Reviews| 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/image/image.h" | 10 #include "ui/gfx/image/image.h" |
| 11 #include "ui/gfx/image/image_skia.h" | 11 #include "ui/gfx/image/image_skia.h" |
| 12 | 12 |
| 13 namespace gfx { | 13 namespace gfx { |
| 14 | 14 |
| 15 // The iOS implementations of the JPEG functions are in image_util_ios.mm. | 15 // The iOS implementations of the JPEG functions are in image_util_ios.mm. |
| 16 #if !defined(OS_IOS) | 16 #if !defined(OS_IOS) |
| 17 Image ImageFrom1xJPEGEncodedData(const unsigned char* input, | 17 Image ImageFrom1xJPEGEncodedData(const unsigned char* input, |
| 18 size_t input_size) { | 18 size_t input_size) { |
| 19 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); | 19 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); |
| 20 if (bitmap.get()) | 20 if (bitmap.get()) |
| 21 return Image::CreateFrom1xBitmap(*bitmap); | 21 return Image::CreateFrom1xBitmap(*bitmap); |
| 22 | 22 |
| 23 return Image(); | 23 return Image(); |
| 24 } | 24 } |
| 25 | 25 |
| 26 bool JPEG1xEncodedDataFromImage(const Image& image, int quality, | 26 bool JPEG1xEncodedDataFromImage(const Image& image, int quality, |
| 27 std::vector<unsigned char>* dst) { | 27 std::vector<unsigned char>* dst) { |
| 28 const gfx::ImageSkiaRep& image_skia_rep = | 28 const gfx::ImageSkiaRep& image_skia_rep = |
| 29 image.AsImageSkia().GetRepresentation(1.0f); | 29 image.AsImageSkia().GetRepresentation(1.0f); |
| 30 if (image_skia_rep.scale() != 1.0f) | 30 if (image_skia_rep.scale() != 1.0f) |
| 31 return false; | 31 return false; |
| 32 | 32 |
| 33 const SkBitmap& bitmap = image_skia_rep.sk_bitmap(); | 33 const SkBitmap& bitmap = image_skia_rep.sk_bitmap(); |
| 34 SkAutoLockPixels bitmap_lock(bitmap); | 34 SkAutoLockPixels bitmap_lock(bitmap); |
| 35 | 35 |
| 36 if (!bitmap.readyToDraw()) | 36 if (!bitmap.readyToDraw()) |
| 37 return false; | 37 return false; |
| 38 | 38 |
| 39 return gfx::JPEGCodec::Encode( | 39 return gfx::JPEGCodec::Encode( |
| 40 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 40 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 41 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), | 41 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), |
| 42 bitmap.height(), | 42 bitmap.height(), |
| 43 static_cast<int>(bitmap.rowBytes()), quality, | 43 static_cast<int>(bitmap.rowBytes()), quality, |
| 44 dst); | 44 dst); |
| 45 } | 45 } |
| 46 #endif // !defined(OS_IOS) | 46 #endif // !defined(OS_IOS) |
| 47 | 47 |
| 48 bool VisibleMargins(const ImageSkia& image, int* leading, int* trailing) { | |
| 49 *leading = 0; | |
| 50 *trailing = std::max(1, image.width()) - 1; | |
| 51 if (!image.HasRepresentation(1.0)) | |
| 52 return false; | |
| 53 | |
| 54 const ImageSkiaRep& rep = image.GetRepresentation(1.0); | |
| 55 if (rep.is_null()) | |
| 56 return false; | |
| 57 | |
| 58 const SkBitmap& bitmap = rep.sk_bitmap(); | |
| 59 if (bitmap.isNull() || bitmap.width() == 0) | |
| 60 return false; | |
| 61 | |
| 62 if (bitmap.isOpaque()) | |
| 63 return true; | |
| 64 | |
| 65 SkAutoLockPixels l(bitmap); | |
| 66 int innerMin = bitmap.width(); | |
|
Robert Sesek
2014/01/30 20:41:38
naming: Use under_scores, not camelCase. Here and
Greg Billock
2014/01/30 23:39:19
Done.
| |
| 67 for (int x = 0; x < bitmap.width(); ++x) { | |
| 68 for (int y = 0; y < bitmap.height(); ++y) { | |
| 69 if (SkColorGetA(bitmap.getColor(x, y)) > 12) { | |
| 70 innerMin = x; | |
| 71 break; | |
| 72 } | |
| 73 } | |
| 74 if (innerMin < bitmap.width()) | |
| 75 break; | |
| 76 } | |
| 77 | |
| 78 int innerMax = -1; | |
| 79 for (int x = bitmap.width() - 1; x > innerMin; --x) { | |
| 80 for (int y = 0; y < bitmap.height(); ++y) { | |
| 81 if (SkColorGetA(bitmap.getColor(x, y)) > 12) { | |
| 82 innerMax = x; | |
| 83 break; | |
| 84 } | |
| 85 } | |
| 86 if (innerMax > -1) | |
| 87 break; | |
| 88 } | |
| 89 | |
| 90 if (innerMin == bitmap.width()) { | |
| 91 *leading = bitmap.width()/2; | |
| 92 *trailing = bitmap.width()/2 + 1; | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 *leading = innerMin; | |
| 97 *trailing = innerMax; | |
| 98 return true; | |
| 48 } | 99 } |
| 100 | |
| 101 } // namespace gfx | |
| OLD | NEW |