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