blob: 95b49ce4a099226541bd6d90adf4f96f307b6a99 [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" },
42 { SkBitmap::kRLE_Index8_Config, "00000000" }
43 };
weita@google.comf9ab99a2009-05-03 18:23:30 +000044
reed@android.com42263962009-05-01 04:00:01 +000045 const int W = 20;
46 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +000047
reed@android.com42263962009-05-01 04:00:01 +000048 for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) {
49 for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) {
50 SkBitmap src, dst;
51 SkColorTable* ct = NULL;
weita@google.comf9ab99a2009-05-03 18:23:30 +000052
reed@android.com42263962009-05-01 04:00:01 +000053 src.setConfig(gPairs[i].fConfig, W, H);
54 if (SkBitmap::kIndex8_Config == src.config()) {
55 ct = init_ctable();
56 }
57 src.allocPixels(ct);
58 ct->safeRef();
59
60 init_src(src);
61 bool success = src.copyTo(&dst, gPairs[j].fConfig);
62 bool expected = gPairs[i].fValid[j] != '0';
63 if (success != expected) {
64 SkString str;
65 str.printf("SkBitmap::copyTo from %s to %s. expected %s returned %s",
66 gConfigName[i], gConfigName[j], boolStr(expected),
67 boolStr(success));
68 reporter->reportFailed(str);
69 }
weita@google.comf9ab99a2009-05-03 18:23:30 +000070
reed@android.com42263962009-05-01 04:00:01 +000071 if (success) {
72 REPORTER_ASSERT(reporter, src.width() == dst.width());
73 REPORTER_ASSERT(reporter, src.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +000074 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +000075 if (src.config() == dst.config()) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000076 SkAutoLockPixels srcLock(src);
reed@android.com42263962009-05-01 04:00:01 +000077 SkAutoLockPixels dstLock(dst);
78 REPORTER_ASSERT(reporter, src.readyToDraw());
79 REPORTER_ASSERT(reporter, dst.readyToDraw());
80 const char* srcP = (const char*)src.getAddr(0, 0);
81 const char* dstP = (const char*)dst.getAddr(0, 0);
82 REPORTER_ASSERT(reporter, srcP != dstP);
83 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
84 src.getSize()));
85 }
reed@android.com311c82d2009-05-05 23:13:23 +000086 // test extractSubset
87 {
88 SkBitmap subset;
89 SkIRect r;
90 r.set(1, 1, 2, 2);
91 if (src.extractSubset(&subset, r)) {
92 REPORTER_ASSERT(reporter, subset.width() == 1);
93 REPORTER_ASSERT(reporter, subset.height() == 1);
94
95 SkBitmap copy;
96 REPORTER_ASSERT(reporter,
97 subset.copyTo(&copy, subset.config()));
98 REPORTER_ASSERT(reporter, copy.width() == 1);
99 REPORTER_ASSERT(reporter, copy.height() == 1);
100 REPORTER_ASSERT(reporter, copy.rowBytes() <= 4);
101
102 SkAutoLockPixels alp0(subset);
103 SkAutoLockPixels alp1(copy);
104 // they should both have, or both not-have, a colortable
105 bool hasCT = subset.getColorTable() != NULL;
106 REPORTER_ASSERT(reporter,
107 (copy.getColorTable() != NULL) == hasCT);
108 }
109 }
reed@android.com42263962009-05-01 04:00:01 +0000110 } else {
111 // dst should be unchanged from its initial state
112 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
113 REPORTER_ASSERT(reporter, dst.width() == 0);
114 REPORTER_ASSERT(reporter, dst.height() == 0);
115 }
116 }
117 }
118}
119
120#include "TestClassDef.h"
121DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)