OLD | NEW |
---|---|
1 #include "DMUtil.h" | 1 #include "DMUtil.h" |
2 | 2 |
3 #include "SkColorPriv.h" | |
3 #include "SkPicture.h" | 4 #include "SkPicture.h" |
4 #include "SkPictureRecorder.h" | 5 #include "SkPictureRecorder.h" |
5 | 6 |
6 namespace DM { | 7 namespace DM { |
7 | 8 |
8 SkString UnderJoin(const char* a, const char* b) { | 9 SkString UnderJoin(const char* a, const char* b) { |
9 SkString s; | 10 SkString s; |
10 s.appendf("%s_%s", a, b); | 11 s.appendf("%s_%s", a, b); |
11 return s; | 12 return s; |
12 } | 13 } |
(...skipping 18 matching lines...) Expand all Loading... | |
31 } | 32 } |
32 | 33 |
33 void DrawPicture(SkPicture* picture, SkBitmap* bitmap) { | 34 void DrawPicture(SkPicture* picture, SkBitmap* bitmap) { |
34 SkASSERT(picture != NULL); | 35 SkASSERT(picture != NULL); |
35 SkASSERT(bitmap != NULL); | 36 SkASSERT(bitmap != NULL); |
36 SkCanvas canvas(*bitmap); | 37 SkCanvas canvas(*bitmap); |
37 canvas.drawPicture(*picture); | 38 canvas.drawPicture(*picture); |
38 canvas.flush(); | 39 canvas.flush(); |
39 } | 40 } |
40 | 41 |
42 static void unpack_565(uint16_t pixel, unsigned* r, unsigned* g, unsigned* b) { | |
43 *r = SkGetPackedR16(pixel); | |
44 *g = SkGetPackedG16(pixel); | |
45 *b = SkGetPackedB16(pixel); | |
46 } | |
47 | |
48 // Returns |a-b|. | |
49 static unsigned abs_diff(unsigned a, unsigned b) { | |
50 return a > b ? a - b : b - a; | |
51 } | |
52 | |
53 unsigned MaxComponentDifference(const SkBitmap& a, const SkBitmap& b) { | |
54 if (a.info() != b.info()) { | |
55 SkFAIL("Can't compare bitmaps of different shapes."); | |
56 } | |
57 | |
58 unsigned max = 0; | |
59 | |
60 const SkAutoLockPixels lockA(a), lockB(b); | |
61 if (a.info().colorType() == kRGB_565_SkColorType) { | |
62 // 565 is special/annoying because its 3 components straddle 2 bytes. | |
63 const uint16_t* aPixels = (const uint16_t*)a.getPixels(); | |
64 const uint16_t* bPixels = (const uint16_t*)b.getPixels(); | |
65 for (size_t i = 0; i < a.getSize() / 2; i++) { | |
66 unsigned ar, ag, ab, | |
67 br, bg, bb; | |
68 unpack_565(aPixels[i], &ar, &ag, &ab); | |
69 unpack_565(bPixels[i], &br, &bg, &bb); | |
70 max = SkTMax(max, abs_diff(ar, br)); | |
71 max = SkTMax(max, abs_diff(ag, bg)); | |
72 max = SkTMax(max, abs_diff(ab, bb)); | |
73 } | |
74 } else { | |
75 // Everything else we produce is byte aligned, so max component diff == max byte diff. | |
76 const uint8_t* aBytes = (const uint8_t*)a.getPixels(); | |
77 const uint8_t* bBytes = (const uint8_t*)b.getPixels(); | |
78 for (size_t i = 0; i < a.getSize(); i++) { | |
79 max = SkTMax(max, abs_diff(aBytes[i], bBytes[i])); | |
80 } | |
81 } | |
82 | |
83 return max; | |
84 } | |
85 | |
41 bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) { | 86 bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) { |
42 const SkAutoLockPixels lockA(a), lockB(b); | 87 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
| |
43 return a.getSize() == b.getSize() && 0 == memcmp(a.getPixels(), b.getPixels( ), b.getSize()); | |
44 } | 88 } |
45 | 89 |
46 } // namespace DM | 90 } // namespace DM |
OLD | NEW |