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