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