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