commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 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 "Test.h" |
| 10 | |
| 11 | #include "SkBitmap.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkPathUtils.h" |
| 14 | #include "SkRandom.h" |
| 15 | #include "SkTime.h" |
| 16 | |
dierk@google.com | a950551 | 2013-07-01 20:36:31 +0000 | [diff] [blame] | 17 | #define NUM_IT 1000 |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 18 | #define ON 0xFF000000 // black pixel |
| 19 | #define OFF 0x00000000 // transparent pixel |
| 20 | |
| 21 | class SkBitmap; |
| 22 | |
| 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 ){ |
| 26 | SkTime time; |
| 27 | SkMWCRandom rand = SkMWCRandom( time.GetMSecs() ); |
| 28 | |
| 29 | for (int i = 0; i < chars; ++i){ |
| 30 | bits[i] = rand.nextU(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | //also defined within PathUtils.cpp, but not in scope here. Anyway to call it |
| 35 | // without re-defining it? |
| 36 | static int getBit( const char* buffer, int x ) { |
| 37 | int byte = x >> 3; |
| 38 | int bit = x & 7; |
| 39 | |
| 40 | return buffer[byte] & (1 << bit); |
| 41 | } |
| 42 | |
| 43 | static void bin2SkBitmap(const char* bin_bmp, SkBitmap* sk_bmp, |
| 44 | int h, int w, int stride){ |
| 45 | //init the SkBitmap |
| 46 | sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 47 | sk_bmp->allocPixels(); |
| 48 | |
| 49 | for (int y = 0; y < h; ++y) { // for every row |
| 50 | |
| 51 | const char* curLine = &bin_bmp[y * stride]; |
| 52 | for (int x = 0; x < w; ++x) {// for every pixel |
| 53 | if (getBit(curLine, x)) { |
| 54 | *sk_bmp->getAddr32(x,y) = ON; |
| 55 | } |
| 56 | else { |
| 57 | *sk_bmp->getAddr32(x,y) = OFF; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static bool test_bmp(skiatest::Reporter* reporter, |
| 64 | const SkBitmap* bmp1, const SkBitmap* bmp2, |
| 65 | int h, int w) { |
| 66 | for (int y = 0; y < h; ++y) { // loop through all pixels |
| 67 | for (int x = 0; x < w; ++x) { |
| 68 | REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp1->getAddr32(x,y) ); |
| 69 | } |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path, |
| 75 | const SkBitmap* truth, int h, int w){ |
| 76 | // make paint |
| 77 | SkPaint bmpPaint; |
| 78 | bmpPaint.setAntiAlias(true); // Black paint for bitmap |
| 79 | bmpPaint.setStyle(SkPaint::kFill_Style); |
| 80 | bmpPaint.setColor(SK_ColorBLACK); |
| 81 | |
| 82 | // make bmp |
| 83 | SkBitmap bmp; |
| 84 | bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 85 | bmp.allocPixels(); |
| 86 | SkCanvas(bmp).drawPath(*path, bmpPaint); |
| 87 | |
| 88 | // test bmp |
| 89 | test_bmp(reporter, &bmp, truth, h, w); |
| 90 | } |
| 91 | |
| 92 | static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth, |
| 93 | const char* bin_bmp, int h, int w, int stride){ |
| 94 | // make path |
| 95 | SkPath path; |
| 96 | SkPathUtils::BitsToPath_Path(&path, bin_bmp, h, w, stride); |
| 97 | |
| 98 | //test for correctness |
| 99 | test_path_eq(reporter, &path, truth, h, w); |
| 100 | } |
| 101 | |
| 102 | static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth, |
| 103 | const char* bin_bmp, int h, int w, int stride){ |
| 104 | //generate bitmap |
| 105 | SkPath path; |
| 106 | SkPathUtils::BitsToPath_Region(&path, bin_bmp, h, w, stride); |
| 107 | |
| 108 | //test for correctness |
| 109 | test_path_eq(reporter, &path, truth, h, w); |
| 110 | } |
| 111 | |
dierk@google.com | a950551 | 2013-07-01 20:36:31 +0000 | [diff] [blame] | 112 | #define W_tests 4 |
| 113 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 114 | static void TestPathUtils(skiatest::Reporter* reporter) { |
dierk@google.com | a950551 | 2013-07-01 20:36:31 +0000 | [diff] [blame] | 115 | const int w[W_tests] = {4, 8, 12, 16}; |
| 116 | const int h = 8, stride = 4; |
| 117 | |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 118 | char bits[ h * stride ]; |
| 119 | static char* bin_bmp = &bits[0]; |
| 120 | |
| 121 | //loop to run randomized test lots of times |
| 122 | for (int it = 0; it < NUM_IT; ++it) |
| 123 | { |
| 124 | // generate a random binary bitmap |
| 125 | fillRandomBits( h * stride, bin_bmp); // generate random bitmap |
| 126 | |
| 127 | // for each bitmap width, use subset of binary bitmap |
dierk@google.com | a950551 | 2013-07-01 20:36:31 +0000 | [diff] [blame] | 128 | for (int i = 0; i < W_tests; ++i) { |
commit-bot@chromium.org | 064779a | 2013-07-01 17:50:29 +0000 | [diff] [blame] | 129 | // generate truth bitmap |
| 130 | SkBitmap bmpTruth; |
| 131 | bin2SkBitmap(bin_bmp, &bmpTruth, h, w[i], stride); |
| 132 | |
| 133 | test_path(reporter, &bmpTruth, bin_bmp, h, w[i], stride); |
| 134 | test_region(reporter, &bmpTruth, bin_bmp, h, w[i], stride); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #include "TestClassDef.h" |
| 140 | DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils) |