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