blob: e54cde33192d7b50e94a6ae04bd8b732417697c6 [file] [log] [blame]
reed@android.com42263962009-05-01 04:00:01 +00001#include "Test.h"
2#include "SkBitmap.h"
reed@android.com311c82d2009-05-05 23:13:23 +00003#include "SkRect.h"
reed@android.com42263962009-05-01 04:00:01 +00004
5static const char* boolStr(bool value) {
6 return value ? "true" : "false";
7}
8
9// these are in the same order as the SkBitmap::Config enum
10static const char* gConfigName[] = {
11 "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8"
12};
13
14static void init_src(const SkBitmap& bitmap) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000015 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000016 if (bitmap.getPixels()) {
17 memset(bitmap.getPixels(), 4, bitmap.getSize());
18 }
19}
20
21SkColorTable* init_ctable() {
22 static const SkColor colors[] = {
23 SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE
24 };
25 return new SkColorTable(colors, SK_ARRAY_COUNT(colors));
26}
27
28struct Pair {
29 SkBitmap::Config fConfig;
30 const char* fValid;
31};
32
33static void TestBitmapCopy(skiatest::Reporter* reporter) {
34 static const Pair gPairs[] = {
35 { SkBitmap::kNo_Config, "00000000" },
weita@google.comf9ab99a2009-05-03 18:23:30 +000036 { SkBitmap::kA1_Config, "01000000" },
reed@android.com42263962009-05-01 04:00:01 +000037 { SkBitmap::kA8_Config, "00101110" },
38 { SkBitmap::kIndex8_Config, "00111110" },
39 { SkBitmap::kRGB_565_Config, "00101110" },
40 { SkBitmap::kARGB_4444_Config, "00101110" },
41 { SkBitmap::kARGB_8888_Config, "00101110" },
reed@android.comfbaa88d2009-05-06 17:44:34 +000042// TODO: create valid RLE bitmap to test with
43 // { SkBitmap::kRLE_Index8_Config, "00101111" }
reed@android.com42263962009-05-01 04:00:01 +000044 };
weita@google.comf9ab99a2009-05-03 18:23:30 +000045
reed@android.com42263962009-05-01 04:00:01 +000046 const int W = 20;
47 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +000048
reed@android.com42263962009-05-01 04:00:01 +000049 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
50 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
51 SkBitmap src, dst;
52 SkColorTable* ct = NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +000053
reed@android.com42263962009-05-01 04:00:01 +000054 src.setConfig(gPairs[i].fConfig, W, H);
reed@android.comfbaa88d2009-05-06 17:44:34 +000055 if (SkBitmap::kIndex8_Config == src.config() ||
56 SkBitmap::kRLE_Index8_Config == src.config()) {
reed@android.com42263962009-05-01 04:00:01 +000057 ct = init_ctable();
58 }
59 src.allocPixels(ct);
60 ct->safeRef();
61
62 init_src(src);
63 bool success = src.copyTo(&dst, gPairs[j].fConfig);
64 bool expected = gPairs[i].fValid[j] != '0';
65 if (success != expected) {
66 SkString str;
67 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
68 gConfigName[i], gConfigName[j], boolStr(expected),
69 boolStr(success));
70 reporter->reportFailed(str);
71 }
reed@android.comfbaa88d2009-05-06 17:44:34 +000072
73 bool canSucceed = src.canCopyTo(gPairs[j].fConfig);
74 if (success != canSucceed) {
75 SkString str;
76 str.printf("SkBitmap::copyTo from %s to %s. returned %s canCopyTo %s",
77 gConfigName[i], gConfigName[j], boolStr(success),
78 boolStr(canSucceed));
79 reporter->reportFailed(str);
80 }
weita@google.comf9ab99a2009-05-03 18:23:30 +000081
reed@android.com42263962009-05-01 04:00:01 +000082 if (success) {
83 REPORTER_ASSERT(reporter, src.width() == dst.width());
84 REPORTER_ASSERT(reporter, src.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +000085 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +000086 if (src.config() == dst.config()) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000087 SkAutoLockPixels srcLock(src);
reed@android.com42263962009-05-01 04:00:01 +000088 SkAutoLockPixels dstLock(dst);
89 REPORTER_ASSERT(reporter, src.readyToDraw());
90 REPORTER_ASSERT(reporter, dst.readyToDraw());
91 const char* srcP = (const char*)src.getAddr(0, 0);
92 const char* dstP = (const char*)dst.getAddr(0, 0);
93 REPORTER_ASSERT(reporter, srcP != dstP);
94 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
95 src.getSize()));
96 }
reed@android.com311c82d2009-05-05 23:13:23 +000097 // test extractSubset
98 {
99 SkBitmap subset;
100 SkIRect r;
101 r.set(1, 1, 2, 2);
102 if (src.extractSubset(&subset, r)) {
103 REPORTER_ASSERT(reporter, subset.width() == 1);
104 REPORTER_ASSERT(reporter, subset.height() == 1);
105
106 SkBitmap copy;
107 REPORTER_ASSERT(reporter,
108 subset.copyTo(&copy, subset.config()));
109 REPORTER_ASSERT(reporter, copy.width() == 1);
110 REPORTER_ASSERT(reporter, copy.height() == 1);
111 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
112
113 SkAutoLockPixels alp0(subset);
114 SkAutoLockPixels alp1(copy);
115 // they should both have, or both not-have, a colortable
116 bool hasCT = subset.getColorTable() != NULL;
117 REPORTER_ASSERT(reporter,
118 (copy.getColorTable() != NULL) == hasCT);
119 }
120 }
reed@android.com42263962009-05-01 04:00:01 +0000121 } else {
122 // dst should be unchanged from its initial state
123 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
124 REPORTER_ASSERT(reporter, dst.width() == 0);
125 REPORTER_ASSERT(reporter, dst.height() == 0);
126 }
127 }
128 }
129}
130
131#include "TestClassDef.h"
132DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)