Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Unified Diff: ui/gfx/image/image_util.cc

Issue 1836483002: Fix up and rename VisibleMargins() in preparation for making use of it. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698