OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "Test.h" | 9 #include "Test.h" |
10 | 10 |
11 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
12 #include "SkCanvas.h" | 12 #include "SkCanvas.h" |
13 #include "SkPathUtils.h" | 13 #include "SkPathUtils.h" |
14 #include "SkRandom.h" | 14 #include "SkRandom.h" |
15 #include "SkTime.h" | 15 #include "SkTime.h" |
16 | 16 |
17 #define NUM_IT 1000 | 17 #define SK_NUM_IT 100 |
18 #define ON 0xFF000000 // black pixel | |
19 #define OFF 0x00000000 // transparent pixel | |
20 | 18 |
21 class SkBitmap; | 19 class SkBitmap; |
22 | 20 |
23 //this function is redefined for sample, test, and bench. is there anywhere | 21 static void fill_random_bits( int chars, char* bits ){ |
24 // I can put it to avoid code duplcation? | |
25 static void fillRandomBits( int chars, char* bits ){ | |
26 SkMWCRandom rand(SkTime::GetMSecs()); | 22 SkMWCRandom rand(SkTime::GetMSecs()); |
27 | 23 |
28 for (int i = 0; i < chars; ++i){ | 24 for (int i = 0; i < chars; ++i){ |
29 bits[i] = rand.nextU(); | 25 bits[i] = rand.nextU(); |
30 } | 26 } |
31 } | 27 } |
32 | 28 |
33 //also defined within PathUtils.cpp, but not in scope here. Anyway to call it | 29 static int get_bit( const char* buffer, int x ) { |
34 // without re-defining it? | |
35 static int getBit( const char* buffer, int x ) { | |
36 int byte = x >> 3; | 30 int byte = x >> 3; |
37 int bit = x & 7; | 31 int bit = x & 7; |
38 | 32 |
39 return buffer[byte] & (1 << bit); | 33 return buffer[byte] & (128 >> bit); |
| 34 } |
| 35 /* // useful for debugging errors |
| 36 #include <iostream> |
| 37 static void print_bits( const char* bits, int w, int h) { |
| 38 |
| 39 for (int y = 0; y < h; ++y) { |
| 40 for (int x = 0; x < w; ++x){ |
| 41 bool bit = getBit(&bits[y], x)!=0; |
| 42 std::cout << bit; |
| 43 } |
| 44 std::cout << std::endl; |
| 45 } |
40 } | 46 } |
41 | 47 |
42 static void bin2SkBitmap(const char* bin_bmp, SkBitmap* sk_bmp, | 48 static void print_bmp( SkBitmap* bmp, int w, int h){ |
43 int h, int w, int stride){ | 49 |
| 50 for (int y = 0; y < h; ++y) { |
| 51 for (int x = 0; x < w; ++x) { |
| 52 int d = *bmp->getAddr32(x,y); |
| 53 if (d == -1) |
| 54 std::cout << 0; |
| 55 else |
| 56 std::cout << 1; |
| 57 } |
| 58 std::cout << std::endl; |
| 59 } |
| 60 } |
| 61 */ |
| 62 |
| 63 static void binary_to_skbitmap(const char* bin_bmp, SkBitmap* sk_bmp, |
| 64 int h, int w, int rowBytes){ |
44 //init the SkBitmap | 65 //init the SkBitmap |
45 sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | 66 sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
46 sk_bmp->allocPixels(); | 67 sk_bmp->allocPixels(); |
47 | 68 |
48 for (int y = 0; y < h; ++y) { // for every row | 69 for (int y = 0; y < h; ++y) { // for every row |
49 | 70 |
50 const char* curLine = &bin_bmp[y * stride]; | 71 const char* curLine = &bin_bmp[y * rowBytes]; |
51 for (int x = 0; x < w; ++x) {// for every pixel | 72 for (int x = 0; x < w; ++x) {// for every pixel |
52 if (getBit(curLine, x)) { | 73 if (get_bit(curLine, x)) { |
53 *sk_bmp->getAddr32(x,y) = ON; | 74 *sk_bmp->getAddr32(x,y) = SK_ColorBLACK; |
54 } | 75 } |
55 else { | 76 else { |
56 *sk_bmp->getAddr32(x,y) = OFF; | 77 *sk_bmp->getAddr32(x,y) = SK_ColorWHITE; |
57 } | 78 } |
58 } | 79 } |
59 } | 80 } |
60 } | 81 } |
61 | 82 |
62 static bool test_bmp(skiatest::Reporter* reporter, | 83 static bool test_bmp(skiatest::Reporter* reporter, |
63 const SkBitmap* bmp1, const SkBitmap* bmp2, | 84 const SkBitmap* bmp1, const SkBitmap* bmp2, |
64 int h, int w) { | 85 int h, int w) { |
65 for (int y = 0; y < h; ++y) { // loop through all pixels | 86 for (int y = 0; y < h; ++y) { // loop through all pixels |
66 for (int x = 0; x < w; ++x) { | 87 for (int x = 0; x < w; ++x) { |
67 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp1->getAddr32
(x,y) ); | 88 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp2->getAddr32
(x,y) ); |
68 } | 89 } |
69 } | 90 } |
70 return true; | 91 return true; |
71 } | 92 } |
72 | 93 |
73 static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path, | 94 static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path, |
74 const SkBitmap* truth, int h, int w){ | 95 const SkBitmap* truth, int w, int h){ |
75 // make paint | 96 // make paint |
76 SkPaint bmpPaint; | 97 SkPaint bmpPaint; |
77 bmpPaint.setAntiAlias(true); // Black paint for bitmap | 98 bmpPaint.setAntiAlias(true); // Black paint for bitmap |
78 bmpPaint.setStyle(SkPaint::kFill_Style); | 99 bmpPaint.setStyle(SkPaint::kFill_Style); |
79 bmpPaint.setColor(SK_ColorBLACK); | 100 bmpPaint.setColor(SK_ColorBLACK); |
80 | 101 |
81 // make bmp | 102 // make bmp |
82 SkBitmap bmp; | 103 SkBitmap bmp; |
83 bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h); | 104 bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
84 bmp.allocPixels(); | 105 bmp.allocPixels(); |
85 SkCanvas(bmp).drawPath(*path, bmpPaint); | 106 SkCanvas canvas(bmp); |
| 107 canvas.clear(SK_ColorWHITE); |
| 108 canvas.drawPath(*path, bmpPaint); |
86 | 109 |
87 // test bmp | 110 // test bmp |
88 test_bmp(reporter, &bmp, truth, h, w); | 111 test_bmp(reporter, truth, &bmp, h, w); |
89 } | 112 } |
90 | 113 |
91 static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth, | 114 static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth, |
92 const char* bin_bmp, int h, int w, int stride){ | 115 const char* bin_bmp, int w, int h, int stride){ |
93 // make path | 116 // make path |
94 SkPath path; | 117 SkPath path; |
95 SkPathUtils::BitsToPath_Path(&path, bin_bmp, h, w, stride); | 118 SkPathUtils::BitsToPath_Path(&path, bin_bmp, w, h, stride); |
96 | 119 |
97 //test for correctness | 120 //test for correctness |
98 test_path_eq(reporter, &path, truth, h, w); | 121 test_path_eq(reporter, &path, truth, w, h); |
99 } | 122 } |
100 | 123 |
101 static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth, | 124 static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth, |
102 const char* bin_bmp, int h, int w, int stride){ | 125 const char* bin_bmp, int w, int h, int stride){ |
103 //generate bitmap | 126 //generate bitmap |
104 SkPath path; | 127 SkPath path; |
105 SkPathUtils::BitsToPath_Region(&path, bin_bmp, h, w, stride); | 128 SkPathUtils::BitsToPath_Region(&path, bin_bmp, w, h, stride); |
106 | 129 |
107 //test for correctness | 130 //test for correctness |
108 test_path_eq(reporter, &path, truth, h, w); | 131 test_path_eq(reporter, &path, truth, w, h); |
109 } | 132 } |
110 | 133 |
111 #define W_tests 4 | 134 static void TestPathUtils(skiatest::Reporter* reporter) { |
| 135 const int w[] = {4, 8, 12, 16}; |
| 136 const int h = 8, rowBytes = 4; |
112 | 137 |
113 static void TestPathUtils(skiatest::Reporter* reporter) { | 138 char bits[ h * rowBytes ]; |
114 const int w[W_tests] = {4, 8, 12, 16}; | |
115 const int h = 8, stride = 4; | |
116 | |
117 char bits[ h * stride ]; | |
118 static char* bin_bmp = &bits[0]; | 139 static char* bin_bmp = &bits[0]; |
119 | 140 |
120 //loop to run randomized test lots of times | 141 //loop to run randomized test lots of times |
121 for (int it = 0; it < NUM_IT; ++it) | 142 for (int it = 0; it < SK_NUM_IT; ++it) |
122 { | 143 { |
123 // generate a random binary bitmap | 144 // generate a random binary bitmap |
124 fillRandomBits( h * stride, bin_bmp); // generate random bitmap | 145 fill_random_bits( h * rowBytes, bin_bmp); // generate random bitmap |
125 | 146 |
126 // for each bitmap width, use subset of binary bitmap | 147 // for each bitmap width, use subset of binary bitmap |
127 for (int i = 0; i < W_tests; ++i) { | 148 for (uint i = 0; i < SK_ARRAY_COUNT(w); ++i) { |
128 // generate truth bitmap | 149 // generate truth bitmap |
129 SkBitmap bmpTruth; | 150 SkBitmap bmpTruth; |
130 bin2SkBitmap(bin_bmp, &bmpTruth, h, w[i], stride); | 151 binary_to_skbitmap(bin_bmp, &bmpTruth, h, w[i], rowBytes); |
131 | 152 |
132 test_path(reporter, &bmpTruth, bin_bmp, h, w[i], stride); | 153 test_path(reporter, &bmpTruth, bin_bmp, w[i], h, rowBytes); |
133 test_region(reporter, &bmpTruth, bin_bmp, h, w[i], stride); | 154 test_region(reporter, &bmpTruth, bin_bmp, w[i], h, rowBytes); |
134 } | 155 } |
135 } | 156 } |
136 } | 157 } |
137 | 158 |
138 #include "TestClassDef.h" | 159 #include "TestClassDef.h" |
139 DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils) | 160 DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils) |
OLD | NEW |