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

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

Issue 148093013: [gfx] Add a utility to calculate visible margins of an image. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 11 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
« no previous file with comments | « ui/gfx/image/image_util.h ('k') | ui/gfx/image/image_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/image/image_util.cc
diff --git a/ui/gfx/image/image_util.cc b/ui/gfx/image/image_util.cc
index 59d631d171df572b7912e4119a2df1966b74798f..7f8ac1c85dad5a151867ae95668a81f282f0d94b 100644
--- a/ui/gfx/image/image_util.cc
+++ b/ui/gfx/image/image_util.cc
@@ -24,7 +24,7 @@ Image ImageFrom1xJPEGEncodedData(const unsigned char* input,
}
bool JPEG1xEncodedDataFromImage(const Image& image, int quality,
- std::vector<unsigned char>* dst) {
+ std::vector<unsigned char>* dst) {
const gfx::ImageSkiaRep& image_skia_rep =
image.AsImageSkia().GetRepresentation(1.0f);
if (image_skia_rep.scale() != 1.0f)
@@ -45,4 +45,57 @@ 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 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.
+ for (int x = 0; x < bitmap.width(); ++x) {
+ for (int y = 0; y < bitmap.height(); ++y) {
+ if (SkColorGetA(bitmap.getColor(x, y)) > 12) {
+ innerMin = x;
+ break;
+ }
+ }
+ if (innerMin < bitmap.width())
+ break;
+ }
+
+ int innerMax = -1;
+ for (int x = bitmap.width() - 1; x > innerMin; --x) {
+ for (int y = 0; y < bitmap.height(); ++y) {
+ if (SkColorGetA(bitmap.getColor(x, y)) > 12) {
+ innerMax = x;
+ break;
+ }
+ }
+ if (innerMax > -1)
+ break;
+ }
+
+ if (innerMin == bitmap.width()) {
+ *leading = bitmap.width()/2;
+ *trailing = bitmap.width()/2 + 1;
+ return true;
+ }
+
+ *leading = innerMin;
+ *trailing = innerMax;
+ return true;
}
+
+} // namespace gfx
« no previous file with comments | « ui/gfx/image/image_util.h ('k') | ui/gfx/image/image_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698