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" }, |
| 42 | { SkBitmap::kRLE_Index8_Config, "00000000" } |
| 43 | }; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 44 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 45 | const int W = 20; |
| 46 | const int H = 33; |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 47 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 48 | 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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 52 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 53 | 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.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 70 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 71 | if (success) { |
| 72 | REPORTER_ASSERT(reporter, src.width() == dst.width()); |
| 73 | REPORTER_ASSERT(reporter, src.height() == dst.height()); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 75 | if (src.config() == dst.config()) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 76 | SkAutoLockPixels srcLock(src); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 77 | 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.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame^] | 86 | // 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(©, 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.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 110 | } 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" |
| 121 | DEFINE_TESTCLASS("BitmapCopy", TestBitmapCopyClass, TestBitmapCopy) |