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