1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkPathUtils.h"
#include "SkRandom.h"
#include "SkTime.h"
#define NUM_IT 1000
#define ON 0xFF000000 // black pixel
#define OFF 0x00000000 // transparent pixel
class SkBitmap;
//this function is redefined for sample, test, and bench. is there anywhere
// I can put it to avoid code duplcation?
static void fillRandomBits( int chars, char* bits ){
SkMWCRandom rand(SkTime::GetMSecs());
for (int i = 0; i < chars; ++i){
bits[i] = rand.nextU();
}
}
//also defined within PathUtils.cpp, but not in scope here. Anyway to call it
// without re-defining it?
static int getBit( const char* buffer, int x ) {
int byte = x >> 3;
int bit = x & 7;
return buffer[byte] & (1 << bit);
}
static void bin2SkBitmap(const char* bin_bmp, SkBitmap* sk_bmp,
int h, int w, int stride){
//init the SkBitmap
sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
sk_bmp->allocPixels();
for (int y = 0; y < h; ++y) { // for every row
const char* curLine = &bin_bmp[y * stride];
for (int x = 0; x < w; ++x) {// for every pixel
if (getBit(curLine, x)) {
*sk_bmp->getAddr32(x,y) = ON;
}
else {
*sk_bmp->getAddr32(x,y) = OFF;
}
}
}
}
static bool test_bmp(skiatest::Reporter* reporter,
const SkBitmap* bmp1, const SkBitmap* bmp2,
int h, int w) {
for (int y = 0; y < h; ++y) { // loop through all pixels
for (int x = 0; x < w; ++x) {
REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp1->getAddr32(x,y) );
}
}
return true;
}
static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path,
const SkBitmap* truth, int h, int w){
// make paint
SkPaint bmpPaint;
bmpPaint.setAntiAlias(true); // Black paint for bitmap
bmpPaint.setStyle(SkPaint::kFill_Style);
bmpPaint.setColor(SK_ColorBLACK);
// make bmp
SkBitmap bmp;
bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h);
bmp.allocPixels();
SkCanvas(bmp).drawPath(*path, bmpPaint);
// test bmp
test_bmp(reporter, &bmp, truth, h, w);
}
static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth,
const char* bin_bmp, int h, int w, int stride){
// make path
SkPath path;
SkPathUtils::BitsToPath_Path(&path, bin_bmp, h, w, stride);
//test for correctness
test_path_eq(reporter, &path, truth, h, w);
}
static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
const char* bin_bmp, int h, int w, int stride){
//generate bitmap
SkPath path;
SkPathUtils::BitsToPath_Region(&path, bin_bmp, h, w, stride);
//test for correctness
test_path_eq(reporter, &path, truth, h, w);
}
#define W_tests 4
static void TestPathUtils(skiatest::Reporter* reporter) {
const int w[W_tests] = {4, 8, 12, 16};
const int h = 8, stride = 4;
char bits[ h * stride ];
static char* bin_bmp = &bits[0];
//loop to run randomized test lots of times
for (int it = 0; it < NUM_IT; ++it)
{
// generate a random binary bitmap
fillRandomBits( h * stride, bin_bmp); // generate random bitmap
// for each bitmap width, use subset of binary bitmap
for (int i = 0; i < W_tests; ++i) {
// generate truth bitmap
SkBitmap bmpTruth;
bin2SkBitmap(bin_bmp, &bmpTruth, h, w[i], stride);
test_path(reporter, &bmpTruth, bin_bmp, h, w[i], stride);
test_region(reporter, &bmpTruth, bin_bmp, h, w[i], stride);
}
}
}
#include "TestClassDef.h"
DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils)
|