blob: 5ec64cfc039054966c8aefd1db2e4237b32f9b29 [file] [log] [blame]
reed@android.com42263962009-05-01 04:00:01 +00001#include "Test.h"
2#include "SkBitmap.h"
3
4static const char* boolStr(bool value) {
5 return value ? "true" : "false";
6}
7
8// these are in the same order as the SkBitmap::Config enum
9static const char* gConfigName[] = {
10 "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8"
11};
12
13static void init_src(const SkBitmap& bitmap) {
14 SkAutoLockPixels lock(bitmap);
15 if (bitmap.getPixels()) {
16 memset(bitmap.getPixels(), 4, bitmap.getSize());
17 }
18}
19
20SkColorTable* init_ctable() {
21 static const SkColor colors[] = {
22 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
23 };
24 return new SkColorTable(colors, SK_ARRAY_COUNT(colors));
25}
26
27struct Pair {
28 SkBitmap::Config fConfig;
29 const char* fValid;
30};
31
32static void TestBitmapCopy(skiatest::Reporter* reporter) {
33 static const Pair gPairs[] = {
34 { SkBitmap::kNo_Config, "00000000" },
35 { SkBitmap::kA1_Config, "01101110" },
36 { SkBitmap::kA8_Config, "00101110" },
37 { SkBitmap::kIndex8_Config, "00111110" },
38 { SkBitmap::kRGB_565_Config, "00101110" },
39 { SkBitmap::kARGB_4444_Config, "00101110" },
40 { SkBitmap::kARGB_8888_Config, "00101110" },
41 { SkBitmap::kRLE_Index8_Config, "00000000" }
42 };
43
44 const int W = 20;
45 const int H = 33;
46
47 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
48 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
49 SkBitmap src, dst;
50 SkColorTable* ct = NULL;
51
52 src.setConfig(gPairs[i].fConfig, W, H);
53 if (SkBitmap::kIndex8_Config == src.config()) {
54 ct = init_ctable();
55 }
56 src.allocPixels(ct);
57 ct->safeRef();
58
59 init_src(src);
60 bool success = src.copyTo(&dst, gPairs[j].fConfig);
61 bool expected = gPairs[i].fValid[j] != '0';
62 if (success != expected) {
63 SkString str;
64 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
65 gConfigName[i], gConfigName[j], boolStr(expected),
66 boolStr(success));
67 reporter->reportFailed(str);
68 }
69
70 if (success) {
71 REPORTER_ASSERT(reporter, src.width() == dst.width());
72 REPORTER_ASSERT(reporter, src.height() == dst.height());
73 if (src.config() == dst.config()) {
74 SkAutoLockPixels srcLock(src);
75 SkAutoLockPixels dstLock(dst);
76 REPORTER_ASSERT(reporter, src.readyToDraw());
77 REPORTER_ASSERT(reporter, dst.readyToDraw());
78 const char* srcP = (const char*)src.getAddr(0, 0);
79 const char* dstP = (const char*)dst.getAddr(0, 0);
80 REPORTER_ASSERT(reporter, srcP != dstP);
81 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
82 src.getSize()));
83 }
84 } else {
85 // dst should be unchanged from its initial state
86 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
87 REPORTER_ASSERT(reporter, dst.width() == 0);
88 REPORTER_ASSERT(reporter, dst.height() == 0);
89 }
90 }
91 }
92}
93
94#include "TestClassDef.h"
95DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)