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