blob: f9048591560448510997989b769c92c8bc778a36 [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) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000014 SkAutoLockPixels lock(bitmap);
reed@android.com42263962009-05-01 04:00:01 +000015 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" },
weita@google.comf9ab99a2009-05-03 18:23:30 +000035 { SkBitmap::kA1_Config, "01000000" },
reed@android.com42263962009-05-01 04:00:01 +000036 { 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 };
weita@google.comf9ab99a2009-05-03 18:23:30 +000043
reed@android.com42263962009-05-01 04:00:01 +000044 const int W = 20;
45 const int H = 33;
weita@google.comf9ab99a2009-05-03 18:23:30 +000046
reed@android.com42263962009-05-01 04:00:01 +000047 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;
weita@google.comf9ab99a2009-05-03 18:23:30 +000051
reed@android.com42263962009-05-01 04:00:01 +000052 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 }
weita@google.comf9ab99a2009-05-03 18:23:30 +000069
reed@android.com42263962009-05-01 04:00:01 +000070 if (success) {
71 REPORTER_ASSERT(reporter, src.width() == dst.width());
72 REPORTER_ASSERT(reporter, src.height() == dst.height());
weita@google.comf9ab99a2009-05-03 18:23:30 +000073 REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig);
reed@android.com42263962009-05-01 04:00:01 +000074 if (src.config() == dst.config()) {
weita@google.comf9ab99a2009-05-03 18:23:30 +000075 SkAutoLockPixels srcLock(src);
reed@android.com42263962009-05-01 04:00:01 +000076 SkAutoLockPixels dstLock(dst);
77 REPORTER_ASSERT(reporter, src.readyToDraw());
78 REPORTER_ASSERT(reporter, dst.readyToDraw());
79 const char* srcP = (const char*)src.getAddr(0, 0);
80 const char* dstP = (const char*)dst.getAddr(0, 0);
81 REPORTER_ASSERT(reporter, srcP != dstP);
82 REPORTER_ASSERT(reporter, !memcmp(srcP, dstP,
83 src.getSize()));
84 }
85 } else {
86 // dst should be unchanged from its initial state
87 REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config);
88 REPORTER_ASSERT(reporter, dst.width() == 0);
89 REPORTER_ASSERT(reporter, dst.height() == 0);
90 }
91 }
92 }
93}
94
95#include "TestClassDef.h"
96DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy)