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