DMUtil.cpp revision 9b14f26d0f3a974f3dd626c8354e1db1cfcd322f
1#include "DMUtil.h"
2
3#include "SkColorPriv.h"
4#include "SkPicture.h"
5#include "SkPictureRecorder.h"
6
7namespace DM {
8
9SkString UnderJoin(const char* a, const char* b) {
10    SkString s;
11    s.appendf("%s_%s", a, b);
12    return s;
13}
14
15SkPicture* RecordPicture(skiagm::GM* gm, uint32_t recordFlags, SkBBHFactory* factory) {
16    const SkISize size = gm->getISize();
17    SkPictureRecorder recorder;
18    SkCanvas* canvas = recorder.beginRecording(size.width(), size.height(), factory, recordFlags);
19    canvas->concat(gm->getInitialTransform());
20    gm->draw(canvas);
21    canvas->flush();
22    return recorder.endRecording();
23}
24
25void AllocatePixels(SkColorType ct, int width, int height, SkBitmap* bitmap) {
26    bitmap->allocPixels(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType));
27    bitmap->eraseColor(0x00000000);
28}
29
30void AllocatePixels(const SkBitmap& reference, SkBitmap* bitmap) {
31    AllocatePixels(reference.colorType(), reference.width(), reference.height(), bitmap);
32}
33
34void DrawPicture(SkPicture* picture, SkBitmap* bitmap) {
35    SkASSERT(picture != NULL);
36    SkASSERT(bitmap != NULL);
37    SkCanvas canvas(*bitmap);
38    canvas.drawPicture(picture);
39    canvas.flush();
40}
41
42static 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|.
49static unsigned abs_diff(unsigned a, unsigned b) {
50    return a > b ? a - b : b - a;
51}
52
53unsigned 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
86bool BitmapsEqual(const SkBitmap& a, const SkBitmap& b) {
87    if (a.info() != b.info()) {
88        return false;
89    }
90    const SkAutoLockPixels lockA(a), lockB(b);
91    return 0 == memcmp(a.getPixels(), b.getPixels(), a.getSize());
92}
93
94}  // namespace DM
95