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