2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkPathUtils.h"
15 const int kNumIt = 100;
17 static void fill_random_bits(int chars, char* bits){
18 SkRandom rand(SkTime::GetMSecs());
20 for (int i = 0; i < chars; ++i){
21 bits[i] = rand.nextU();
25 static int get_bit(const char* buffer, int x) {
29 return buffer[byte] & (128 >> bit);
32 /* // useful for debugging errors
34 static void print_bits( const char* bits, int w, int h) {
36 for (int y = 0; y < h; ++y) {
37 for (int x = 0; x < w; ++x){
38 bool bit = get_bit(&bits[y], x)!=0;
41 std::cout << std::endl;
45 static void print_bmp( SkBitmap* bmp, int w, int h){
47 for (int y = 0; y < h; ++y) {
48 for (int x = 0; x < w; ++x) {
49 int d = *bmp->getAddr32(x,y);
55 std::cout << std::endl;
60 static void binary_to_skbitmap(const char* bin_bmp, SkBitmap* sk_bmp,
61 int w, int h, int rowBytes){
63 sk_bmp->allocN32Pixels(w, h);
65 for (int y = 0; y < h; ++y) { // for every row
67 const char* curLine = &bin_bmp[y * rowBytes];
68 for (int x = 0; x < w; ++x) {// for every pixel
69 if (get_bit(curLine, x)) {
70 *sk_bmp->getAddr32(x,y) = SK_ColorBLACK;
73 *sk_bmp->getAddr32(x,y) = SK_ColorWHITE;
79 static bool test_bmp(skiatest::Reporter* reporter,
80 const SkBitmap* bmp1, const SkBitmap* bmp2,
82 for (int y = 0; y < h; ++y) { // loop through all pixels
83 for (int x = 0; x < w; ++x) {
84 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp2->getAddr32(x,y) );
90 static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path,
91 const SkBitmap* truth, int w, int h){
94 bmpPaint.setAntiAlias(true); // Black paint for bitmap
95 bmpPaint.setStyle(SkPaint::kFill_Style);
96 bmpPaint.setColor(SK_ColorBLACK);
100 bmp.allocN32Pixels(w, h);
101 SkCanvas canvas(bmp);
102 canvas.clear(SK_ColorWHITE);
103 canvas.drawPath(*path, bmpPaint);
106 test_bmp(reporter, truth, &bmp, w, h);
109 static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth,
110 const char* bin_bmp, int w, int h, int rowBytes){
113 SkPathUtils::BitsToPath_Path(&path, bin_bmp, w, h, rowBytes);
115 //test for correctness
116 test_path_eq(reporter, &path, truth, w, h);
119 static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
120 const char* bin_bmp, int w, int h, int rowBytes){
123 SkPathUtils::BitsToPath_Region(&path, bin_bmp, w, h, rowBytes);
125 //test for correctness
126 test_path_eq(reporter, &path, truth, w, h);
129 DEF_TEST(PathUtils, reporter) {
130 const int w[] = {4, 8, 12, 16};
131 const int h = 8, rowBytes = 4;
133 char bits[ h * rowBytes ];
134 static char* binBmp = &bits[0];
136 //loop to run randomized test lots of times
137 for (int it = 0; it < kNumIt; ++it)
139 // generate a random binary bitmap
140 fill_random_bits( h * rowBytes, binBmp); // generate random bitmap
142 // for each bitmap width, use subset of binary bitmap
143 for (unsigned int i = 0; i < SK_ARRAY_COUNT(w); ++i) {
144 // generate truth bitmap
146 binary_to_skbitmap(binBmp, &bmpTruth, w[i], h, rowBytes);
148 test_path(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
149 test_region(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);