reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 1 | #include "Test.h" |
| 2 | #include "SkBitmap.h" |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 3 | #include "SkRect.h" |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 4 | |
| 5 | static const char* boolStr(bool value) { |
| 6 | return value ? "true" : "false"; |
| 7 | } |
| 8 | |
| 9 | // these are in the same order as the SkBitmap::Config enum |
| 10 | static const char* gConfigName[] = { |
| 11 | "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8" |
| 12 | }; |
| 13 | |
| 14 | static void init_src(const SkBitmap& bitmap) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 15 | SkAutoLockPixels lock(bitmap); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 16 | if (bitmap.getPixels()) { |
| 17 | memset(bitmap.getPixels(), 4, bitmap.getSize()); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | SkColorTable* 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 | |
| 28 | struct Pair { |
| 29 | SkBitmap::Config fConfig; |
| 30 | const char* fValid; |
| 31 | }; |
| 32 | |
| 33 | static void TestBitmapCopy(skiatest::Reporter* reporter) { |
| 34 | static const Pair gPairs[] = { |
| 35 | { SkBitmap::kNo_Config, "00000000" }, |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 36 | { SkBitmap::kA1_Config, "01000000" }, |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 37 | { 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.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 42 | // TODO: create valid RLE bitmap to test with |
| 43 | // { SkBitmap::kRLE_Index8_Config, "00101111" } |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 44 | }; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 45 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 46 | const int W = 20; |
| 47 | const int H = 33; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 48 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 49 | 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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 53 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 54 | src.setConfig(gPairs[i].fConfig, W, H); |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 55 | if (SkBitmap::kIndex8_Config == src.config() || |
| 56 | SkBitmap::kRLE_Index8_Config == src.config()) { |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 57 | 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.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 72 | |
| 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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 81 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 82 | if (success) { |
| 83 | REPORTER_ASSERT(reporter, src.width() == dst.width()); |
| 84 | REPORTER_ASSERT(reporter, src.height() == dst.height()); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 86 | if (src.config() == dst.config()) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 87 | SkAutoLockPixels srcLock(src); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 88 | 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.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 97 | // 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(©, 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.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 121 | } 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" |
| 132 | DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy) |