Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | |
| 2 /* | 1 /* |
| 3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 4 * | 3 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 7 */ | 6 */ |
| 7 | |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkRect.h" | |
| 11 #include "SkRandom.h" | |
| 12 | |
| 13 static int nextRand(SkRandom& rand, int min, int max) { | |
| 14 return min + (int)rand.nextRangeU(0, max - min); | |
| 15 } | |
| 16 | |
| 17 static void rand_irect(SkIRect* rect, int W, int H, SkRandom& rand) { | |
| 18 const int DX = W / 2; | |
| 19 const int DY = H / 2; | |
| 20 | |
| 21 rect->fLeft = nextRand(rand, -DX, W + DX); | |
| 22 rect->fTop = nextRand(rand, -DY, H + DY); | |
| 23 rect->fRight = nextRand(rand, -DX, W + DX); | |
| 24 rect->fBottom = nextRand(rand, -DY, H + DY); | |
| 25 rect->sort(); | |
| 26 } | |
| 27 | |
| 28 static void test_equal(skiatest::Reporter* reporter, | |
|
scroggo
2013/06/28 19:02:35
Maybe add a comment explaining that this function
reed1
2013/06/28 19:31:54
Done.
| |
| 29 const SkBitmap& bm1, const SkBitmap& bm8) { | |
| 30 REPORTER_ASSERT(reporter, bm1.width() == bm8.width()); | |
| 31 REPORTER_ASSERT(reporter, bm1.height() == bm8.height()); | |
| 32 for (int y = 0; y < bm1.height(); ++y) { | |
| 33 for (int x = 0; x < bm1.width(); ++x) { | |
| 34 int p1 = *bm1.getAddr1(x, y) & (1 << (7 - (x & 7))); | |
| 35 SkASSERT(SkIsPow2(p1)); | |
| 36 p1 = p1 ? 0xFF : 0; | |
| 37 | |
| 38 int p8 = *bm8.getAddr8(x, y); | |
| 39 SkASSERT(0 == p8 || 0xFF == p8); | |
| 40 | |
| 41 REPORTER_ASSERT(reporter, p1 == p8); | |
| 42 } | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 static void test_eraserect_A1(skiatest::Reporter* reporter) { | |
| 47 const int W = 43; | |
| 48 const int H = 13; | |
| 49 | |
| 50 SkBitmap bm1, bm8; | |
| 51 | |
| 52 bm1.setConfig(SkBitmap::kA1_Config, W, H); | |
| 53 bm1.allocPixels(); | |
| 54 bm8.setConfig(SkBitmap::kA8_Config, W, H); | |
| 55 bm8.allocPixels(); | |
| 56 | |
| 57 SkRandom rand; | |
| 58 for (int i = 0; i < 10000; ++i) { | |
| 59 SkIRect area; | |
| 60 rand_irect(&area, W, H, rand); | |
| 61 | |
| 62 bm1.eraseColor(0); | |
| 63 bm8.eraseColor(0); | |
| 64 | |
| 65 bm1.eraseArea(area, SK_ColorWHITE); | |
| 66 bm8.eraseArea(area, SK_ColorWHITE); | |
| 67 test_equal(reporter, bm1, bm8); | |
| 68 } | |
| 69 } | |
| 10 | 70 |
| 11 static void TestGetColor(skiatest::Reporter* reporter) { | 71 static void TestGetColor(skiatest::Reporter* reporter) { |
| 12 static const struct Rec { | 72 static const struct Rec { |
| 13 SkBitmap::Config fConfig; | 73 SkBitmap::Config fConfig; |
| 14 SkColor fInColor; | 74 SkColor fInColor; |
| 15 SkColor fOutColor; | 75 SkColor fOutColor; |
| 16 } gRec[] = { | 76 } gRec[] = { |
| 17 // todo: add some tests that involve alpha, so we exercise the | 77 // todo: add some tests that involve alpha, so we exercise the |
| 18 // unpremultiply aspect of getColor() | 78 // unpremultiply aspect of getColor() |
| 19 { SkBitmap::kA8_Config, 0xFF000000, 0xFF000000 }, | 79 { SkBitmap::kA8_Config, 0xFF000000, 0xFF000000 }, |
| 20 { SkBitmap::kA8_Config, 0, 0 }, | 80 { SkBitmap::kA8_Config, 0, 0 }, |
| 21 { SkBitmap::kRGB_565_Config, 0xFF00FF00, 0xFF00FF00 }, | 81 { SkBitmap::kRGB_565_Config, 0xFF00FF00, 0xFF00FF00 }, |
| 22 { SkBitmap::kRGB_565_Config, 0xFFFF00FF, 0xFFFF00FF }, | 82 { SkBitmap::kRGB_565_Config, 0xFFFF00FF, 0xFFFF00FF }, |
| 23 { SkBitmap::kARGB_8888_Config, 0xFFFFFFFF, 0xFFFFFFFF }, | 83 { SkBitmap::kARGB_8888_Config, 0xFFFFFFFF, 0xFFFFFFFF }, |
| 24 { SkBitmap::kARGB_8888_Config, 0, 0 }, | 84 { SkBitmap::kARGB_8888_Config, 0, 0 }, |
| 25 { SkBitmap::kARGB_8888_Config, 0xFF224466, 0xFF224466 }, | 85 { SkBitmap::kARGB_8888_Config, 0xFF224466, 0xFF224466 }, |
| 26 }; | 86 }; |
| 27 | 87 |
| 88 // specify an area that doesn't touch (0,0) and may extend beyond the | |
| 89 // bitmap bounds (to test that we catch that in eraseArea | |
| 90 const SkColor initColor = 0xFF0000FF; | |
| 91 const SkIRect area = { 1, 1, 3, 3 }; | |
| 92 | |
| 28 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { | 93 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 29 SkBitmap bm; | 94 SkBitmap bm; |
| 30 uint32_t storage[1]; | 95 uint32_t storage[4]; |
| 31 bm.setConfig(gRec[i].fConfig, 1, 1); | 96 bm.setConfig(gRec[i].fConfig, 2, 2); |
| 32 bm.setPixels(storage); | 97 bm.setPixels(storage); |
| 33 bm.eraseColor(gRec[i].fInColor); | |
| 34 | 98 |
| 35 SkColor c = bm.getColor(0, 0); | 99 bm.eraseColor(initColor); |
| 100 bm.eraseArea(area, gRec[i].fInColor); | |
| 101 | |
| 102 SkColor c = bm.getColor(1, 1); | |
| 36 REPORTER_ASSERT(reporter, c == gRec[i].fOutColor); | 103 REPORTER_ASSERT(reporter, c == gRec[i].fOutColor); |
| 37 } | 104 } |
| 105 | |
| 106 test_eraserect_A1(reporter); | |
| 38 } | 107 } |
| 39 | 108 |
| 40 #include "TestClassDef.h" | 109 #include "TestClassDef.h" |
| 41 DEFINE_TESTCLASS("GetColor", TestGetColorClass, TestGetColor) | 110 DEFINE_TESTCLASS("GetColor", TestGetColorClass, TestGetColor) |
| OLD | NEW |