Index: ui/gfx/image/image_util.cc |
diff --git a/ui/gfx/image/image_util.cc b/ui/gfx/image/image_util.cc |
index e991da955e4982ad14861d3e0bc6cbed9a87ac74..fd808036fc77c91dd83a6da057db0659903c17c0 100644 |
--- a/ui/gfx/image/image_util.cc |
+++ b/ui/gfx/image/image_util.cc |
@@ -15,9 +15,22 @@ |
#include "ui/gfx/image/image.h" |
#include "ui/gfx/image/image_skia.h" |
-namespace gfx { |
+namespace { |
+ |
+// Returns whether column |x| of |bitmap| has any pixels with opacity > min, |
danakj
2016/03/25 23:35:28
min looks like it's hardcoded, maybe it was a para
Peter Kasting
2016/03/26 01:01:57
I actually wrote the comment to describe this hard
|
+// where min is a small number representing "pretty much trasparent". |
+bool ColumnHasVisiblePixels(const SkBitmap& bitmap, int x) { |
+ const SkAlpha kMinimumVisibleOpacity = 12; |
+ for (int y = 0; y < bitmap.height(); ++y) { |
+ if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) |
+ return true; |
+ } |
+ return false; |
+} |
-const uint32_t kMinimumVisibleOpacity = 12; |
+} // namespace |
+ |
+namespace gfx { |
// The iOS implementations of the JPEG functions are in image_util_ios.mm. |
#if !defined(OS_IOS) |
@@ -52,57 +65,37 @@ bool JPEG1xEncodedDataFromImage(const Image& image, int quality, |
} |
#endif // !defined(OS_IOS) |
-bool VisibleMargins(const ImageSkia& image, int* leading, int* trailing) { |
- *leading = 0; |
- *trailing = std::max(1, image.width()) - 1; |
- if (!image.HasRepresentation(1.0)) |
- return false; |
- |
- const ImageSkiaRep& rep = image.GetRepresentation(1.0); |
- if (rep.is_null()) |
- return false; |
- |
- const SkBitmap& bitmap = rep.sk_bitmap(); |
- if (bitmap.isNull() || bitmap.width() == 0) |
- return false; |
- |
- if (bitmap.isOpaque()) |
- return true; |
- |
- SkAutoLockPixels l(bitmap); |
- int inner_min = bitmap.width(); |
- for (int x = 0; x < bitmap.width(); ++x) { |
- for (int y = 0; y < bitmap.height(); ++y) { |
- if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) { |
- inner_min = x; |
- break; |
- } |
- } |
- if (inner_min < bitmap.width()) |
+void GetVisibleMargins(const ImageSkia& image, int* left, int* right) { |
+ *left = 0; |
+ *right = 0; |
+ const SkBitmap& bitmap = image.GetRepresentation(1.0).sk_bitmap(); |
+ if (bitmap.drawsNothing() || bitmap.isOpaque()) |
+ return; |
+ |
+ SkAutoLockPixels lock(bitmap); |
+ int x = 0; |
+ for (; x < bitmap.width(); ++x) { |
+ if (ColumnHasVisiblePixels(bitmap, x)) { |
+ *left = x; |
break; |
- } |
- |
- int inner_max = -1; |
- for (int x = bitmap.width() - 1; x > inner_min; --x) { |
- for (int y = 0; y < bitmap.height(); ++y) { |
- if (SkColorGetA(bitmap.getColor(x, y)) > kMinimumVisibleOpacity) { |
- inner_max = x; |
- break; |
- } |
} |
- if (inner_max > -1) |
- break; |
} |
- if (inner_min == bitmap.width()) { |
- *leading = bitmap.width()/2; |
- *trailing = bitmap.width()/2 + 1; |
- return true; |
+ if (x == bitmap.width()) { |
+ // Image is fully transparent. Divide the width in half, giving the leading |
+ // region the extra pixel for odd widths. |
+ *left = (bitmap.width() + 1) / 2; |
+ *right = bitmap.width() - *left; |
+ return; |
} |
- *leading = inner_min; |
- *trailing = inner_max; |
- return true; |
+ // Since we already know column *leading is non-transparent, we can avoid |
danakj
2016/03/25 23:35:28
leading->left ?
Peter Kasting
2016/03/26 01:01:57
Yeah, oops.
|
+ // rechecking that column; hence the '>' here. |
+ for (x = bitmap.width() - 1; x > *left; --x) { |
+ if (ColumnHasVisiblePixels(bitmap, x)) |
+ break; |
+ } |
+ *right = bitmap.width() - 1 - x; |
} |
} // namespace gfx |