OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * 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 |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkRect.h" | 10 #include "SkRect.h" |
11 #include "SkRandom.h" | 11 #include "SkRandom.h" |
12 | 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_A1_A8(skiatest::Reporter* reporter, | |
29 const SkBitmap& bm1, const SkBitmap& bm8) { | |
30 SkASSERT(SkBitmap::kA1_Config == bm1.config()); | |
31 SkASSERT(SkBitmap::kA8_Config == bm8.config()); | |
32 | |
33 REPORTER_ASSERT(reporter, bm1.width() == bm8.width()); | |
34 REPORTER_ASSERT(reporter, bm1.height() == bm8.height()); | |
35 for (int y = 0; y < bm1.height(); ++y) { | |
36 for (int x = 0; x < bm1.width(); ++x) { | |
37 int p1 = *bm1.getAddr1(x, y) & (1 << (7 - (x & 7))); | |
38 SkASSERT(SkIsPow2(p1)); | |
39 p1 = p1 ? 0xFF : 0; | |
40 | |
41 int p8 = *bm8.getAddr8(x, y); | |
42 SkASSERT(0 == p8 || 0xFF == p8); | |
43 | |
44 REPORTER_ASSERT(reporter, p1 == p8); | |
45 } | |
46 } | |
47 } | |
48 | |
49 static void test_eraserect_A1(skiatest::Reporter* reporter) { | |
50 const int W = 43; | |
51 const int H = 13; | |
52 | |
53 SkBitmap bm1, bm8; | |
54 | |
55 bm1.setConfig(SkBitmap::kA1_Config, W, H); | |
56 bm1.allocPixels(); | |
57 bm8.setConfig(SkBitmap::kA8_Config, W, H); | |
58 bm8.allocPixels(); | |
59 | |
60 SkRandom rand; | |
61 for (int i = 0; i < 10000; ++i) { | |
62 SkIRect area; | |
63 rand_irect(&area, W, H, rand); | |
64 | |
65 bm1.eraseColor(0); | |
66 bm8.eraseColor(0); | |
67 | |
68 bm1.eraseArea(area, SK_ColorWHITE); | |
69 bm8.eraseArea(area, SK_ColorWHITE); | |
70 test_equal_A1_A8(reporter, bm1, bm8); | |
71 } | |
72 } | |
73 | |
74 static void TestGetColor(skiatest::Reporter* reporter) { | 13 static void TestGetColor(skiatest::Reporter* reporter) { |
75 static const struct Rec { | 14 static const struct Rec { |
76 SkBitmap::Config fConfig; | 15 SkBitmap::Config fConfig; |
77 SkColor fInColor; | 16 SkColor fInColor; |
78 SkColor fOutColor; | 17 SkColor fOutColor; |
79 } gRec[] = { | 18 } gRec[] = { |
80 // todo: add some tests that involve alpha, so we exercise the | 19 // todo: add some tests that involve alpha, so we exercise the |
81 // unpremultiply aspect of getColor() | 20 // unpremultiply aspect of getColor() |
82 { SkBitmap::kA8_Config, 0xFF000000, 0xFF000000 }, | 21 { SkBitmap::kA8_Config, 0xFF000000, 0xFF000000 }, |
83 { SkBitmap::kA8_Config, 0, 0 }, | 22 { SkBitmap::kA8_Config, 0, 0 }, |
(...skipping 14 matching lines...) Expand all Loading... |
98 uint32_t storage[4]; | 37 uint32_t storage[4]; |
99 bm.setConfig(gRec[i].fConfig, 2, 2); | 38 bm.setConfig(gRec[i].fConfig, 2, 2); |
100 bm.setPixels(storage); | 39 bm.setPixels(storage); |
101 | 40 |
102 bm.eraseColor(initColor); | 41 bm.eraseColor(initColor); |
103 bm.eraseArea(area, gRec[i].fInColor); | 42 bm.eraseArea(area, gRec[i].fInColor); |
104 | 43 |
105 SkColor c = bm.getColor(1, 1); | 44 SkColor c = bm.getColor(1, 1); |
106 REPORTER_ASSERT(reporter, c == gRec[i].fOutColor); | 45 REPORTER_ASSERT(reporter, c == gRec[i].fOutColor); |
107 } | 46 } |
108 | |
109 test_eraserect_A1(reporter); | |
110 } | 47 } |
111 | 48 |
112 #include "TestClassDef.h" | 49 #include "TestClassDef.h" |
113 DEFINE_TESTCLASS("GetColor", TestGetColorClass, TestGetColor) | 50 DEFINE_TESTCLASS("GetColor", TestGetColorClass, TestGetColor) |
OLD | NEW |