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