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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
14 #include "ui/gfx/codec/jpeg_codec.h" | 14 #include "ui/gfx/codec/jpeg_codec.h" |
15 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
16 #include "ui/gfx/image/image_skia.h" | 16 #include "ui/gfx/image/image_skia.h" |
17 | 17 |
| 18 namespace { |
| 19 |
| 20 // Returns whether column |x| of |bitmap| has any "visible pixels", where |
| 21 // "visible" is defined as having an opactiy greater than an arbitrary small |
| 22 // value. |
| 23 bool ColumnHasVisiblePixels(const SkBitmap& bitmap, int x) { |
| 24 const SkAlpha kMinimumVisibleOpacity = 12; |
| 25 for (int y = 0; y < bitmap.height(); ++y) { |
| 26 if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) |
| 27 return true; |
| 28 } |
| 29 return false; |
| 30 } |
| 31 |
| 32 } // namespace |
| 33 |
18 namespace gfx { | 34 namespace gfx { |
19 | 35 |
20 const uint32_t kMinimumVisibleOpacity = 12; | |
21 | |
22 // The iOS implementations of the JPEG functions are in image_util_ios.mm. | 36 // The iOS implementations of the JPEG functions are in image_util_ios.mm. |
23 #if !defined(OS_IOS) | 37 #if !defined(OS_IOS) |
24 Image ImageFrom1xJPEGEncodedData(const unsigned char* input, | 38 Image ImageFrom1xJPEGEncodedData(const unsigned char* input, |
25 size_t input_size) { | 39 size_t input_size) { |
26 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); | 40 scoped_ptr<SkBitmap> bitmap(gfx::JPEGCodec::Decode(input, input_size)); |
27 if (bitmap.get()) | 41 if (bitmap.get()) |
28 return Image::CreateFrom1xBitmap(*bitmap); | 42 return Image::CreateFrom1xBitmap(*bitmap); |
29 | 43 |
30 return Image(); | 44 return Image(); |
31 } | 45 } |
(...skipping 13 matching lines...) Expand all Loading... |
45 | 59 |
46 return gfx::JPEGCodec::Encode( | 60 return gfx::JPEGCodec::Encode( |
47 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 61 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
48 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), | 62 gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), |
49 bitmap.height(), | 63 bitmap.height(), |
50 static_cast<int>(bitmap.rowBytes()), quality, | 64 static_cast<int>(bitmap.rowBytes()), quality, |
51 dst); | 65 dst); |
52 } | 66 } |
53 #endif // !defined(OS_IOS) | 67 #endif // !defined(OS_IOS) |
54 | 68 |
55 bool VisibleMargins(const ImageSkia& image, int* leading, int* trailing) { | 69 void GetVisibleMargins(const ImageSkia& image, int* left, int* right) { |
56 *leading = 0; | 70 *left = 0; |
57 *trailing = std::max(1, image.width()) - 1; | 71 *right = 0; |
58 if (!image.HasRepresentation(1.0)) | 72 if (!image.HasRepresentation(1.f)) |
59 return false; | 73 return; |
| 74 const SkBitmap& bitmap = image.GetRepresentation(1.f).sk_bitmap(); |
| 75 if (bitmap.drawsNothing() || bitmap.isOpaque()) |
| 76 return; |
60 | 77 |
61 const ImageSkiaRep& rep = image.GetRepresentation(1.0); | 78 SkAutoLockPixels lock(bitmap); |
62 if (rep.is_null()) | 79 int x = 0; |
63 return false; | 80 for (; x < bitmap.width(); ++x) { |
| 81 if (ColumnHasVisiblePixels(bitmap, x)) { |
| 82 *left = x; |
| 83 break; |
| 84 } |
| 85 } |
64 | 86 |
65 const SkBitmap& bitmap = rep.sk_bitmap(); | 87 if (x == bitmap.width()) { |
66 if (bitmap.isNull() || bitmap.width() == 0) | 88 // Image is fully transparent. Divide the width in half, giving the leading |
67 return false; | 89 // region the extra pixel for odd widths. |
| 90 *left = (bitmap.width() + 1) / 2; |
| 91 *right = bitmap.width() - *left; |
| 92 return; |
| 93 } |
68 | 94 |
69 if (bitmap.isOpaque()) | 95 // Since we already know column *left is non-transparent, we can avoid |
70 return true; | 96 // rechecking that column; hence the '>' here. |
71 | 97 for (x = bitmap.width() - 1; x > *left; --x) { |
72 SkAutoLockPixels l(bitmap); | 98 if (ColumnHasVisiblePixels(bitmap, x)) |
73 int inner_min = bitmap.width(); | |
74 for (int x = 0; x < bitmap.width(); ++x) { | |
75 for (int y = 0; y < bitmap.height(); ++y) { | |
76 if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) { | |
77 inner_min = x; | |
78 break; | |
79 } | |
80 } | |
81 if (inner_min < bitmap.width()) | |
82 break; | 99 break; |
83 } | 100 } |
84 | 101 *right = bitmap.width() - 1 - x; |
85 int inner_max = -1; | |
86 for (int x = bitmap.width() - 1; x > inner_min; --x) { | |
87 for (int y = 0; y < bitmap.height(); ++y) { | |
88 if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) { | |
89 inner_max = x; | |
90 break; | |
91 } | |
92 } | |
93 if (inner_max > -1) | |
94 break; | |
95 } | |
96 | |
97 if (inner_min == bitmap.width()) { | |
98 *leading = bitmap.width()/2; | |
99 *trailing = bitmap.width()/2 + 1; | |
100 return true; | |
101 } | |
102 | |
103 *leading = inner_min; | |
104 *trailing = inner_max; | |
105 return true; | |
106 } | 102 } |
107 | 103 |
108 } // namespace gfx | 104 } // namespace gfx |
OLD | NEW |