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

Unified Diff: dm/DMUtil.cpp

Issue 290043005: DM: find max component difference (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: tweaks Created 6 years, 7 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
« dm/DMUtil.h ('K') | « dm/DMUtil.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dm/DMUtil.cpp
diff --git a/dm/DMUtil.cpp b/dm/DMUtil.cpp
index bef903daf044cdf57655c0fbc38f3e664308651b..d7d6691550b2113b4991d157c68d9155f8a7e453 100644
--- a/dm/DMUtil.cpp
+++ b/dm/DMUtil.cpp
@@ -1,5 +1,6 @@
#include "DMUtil.h"
+#include "SkColorPriv.h"
#include "SkPicture.h"
#include "SkPictureRecorder.h"
@@ -38,9 +39,52 @@ void DrawPicture(SkPicture* picture, SkBitmap* bitmap) {
canvas.flush();
}
-bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
+static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) {
+ *r = SkGetPackedR16(pixel);
+ *g = SkGetPackedG16(pixel);
+ *b = SkGetPackedB16(pixel);
+}
+
+// Returns |a-b|.
+static unsigned abs_diff(unsigned a, unsigned b) {
+ return a > b ? a - b : b - a;
+}
+
+unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) {
+ if (a.info() != b.info()) {
+ SkFAIL("Can't compare bitmaps of different shapes.");
+ }
+
+ unsigned max = 0;
+
const SkAutoLockPixels lockA(a), lockB(b);
- return a.getSize() == b.getSize() && 0 == memcmp(a.getPixels(), b.getPixels(), b.getSize());
+ if (a.info().colorType() == kRGB_565_SkColorType) {
+ // 565 is special/annoying because its 3 components straddle 2 bytes.
+ const uint16_t* aPixels = (const uint16_t*)a.getPixels();
+ const uint16_t* bPixels = (const uint16_t*)b.getPixels();
+ for (size_t i = 0; i < a.getSize() / 2; i++) {
+ unsigned ar, ag, ab,
+ br, bg, bb;
+ unpack_565(aPixels[i], &ar, &ag, &ab);
+ unpack_565(bPixels[i], &br, &bg, &bb);
+ max = SkTMax(max, abs_diff(ar, br));
+ max = SkTMax(max, abs_diff(ag, bg));
+ max = SkTMax(max, abs_diff(ab, bb));
+ }
+ } else {
+ // Everything else we produce is byte aligned, so max component diff == max byte diff.
+ const uint8_t* aBytes = (const uint8_t*)a.getPixels();
+ const uint8_t* bBytes = (const uint8_t*)b.getPixels();
+ for (size_t i = 0; i < a.getSize(); i++) {
+ max = SkTMax(max, abs_diff(aBytes[i], bBytes[i]));
+ }
+ }
+
+ return max;
+}
+
+bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
+ return a.info() == b.info() && 0 == MaxComponentDifference(a, b);
reed1 2014/05/16 12:30:00 This seems like it will be slower than just memcmp
mtklein 2014/05/16 13:00:53 Definitely, but it's low enough down the profile I
}
} // namespace DM
« dm/DMUtil.h ('K') | « dm/DMUtil.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698