blob: 3dec43943efb187bb91a168adc8e7b22fc7b3356 [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
robertphillips@google.com226d5082013-07-15 17:16:48 +000017#define NUM_IT 100
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000018#define ON 0xFF000000 // black pixel
robertphillips@google.com226d5082013-07-15 17:16:48 +000019#define OFF 0xFFFFFFFF // white pixel
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000020
21class SkBitmap;
22
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000023static void fillRandomBits( int chars, char* bits ){
robertphillips@google.comf8846502013-07-01 23:47:44 +000024 SkMWCRandom rand(SkTime::GetMSecs());
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000025
26 for (int i = 0; i < chars; ++i){
27 bits[i] = rand.nextU();
28 }
29}
30
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000031static int getBit( const char* buffer, int x ) {
32 int byte = x >> 3;
33 int bit = x & 7;
34
robertphillips@google.com226d5082013-07-15 17:16:48 +000035 return buffer[byte] & (128 >> bit);
36}
37/* // useful for debugging errors
38 #include <iostream>
39static void printBits( const char* bits, int w, int h) {
40
41 for (int y = 0; y < h; ++y) {
42 for (int x = 0; x < w; ++x){
43 bool bit = getBit(&bits[y], x)!=0;
44 std::cout << bit;
45 }
46 std::cout << std::endl;
47 }
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000048}
49
robertphillips@google.com226d5082013-07-15 17:16:48 +000050static void printBmp( SkBitmap* bmp, int w, int h){
51
52 for (int y = 0; y < h; ++y) {
53 for (int x = 0; x < w; ++x) {
54 int d = *bmp->getAddr32(x,y);
55 if (d == -1)
56 std::cout << 0;
57 else
58 std::cout << 1;
59 }
60 std::cout << std::endl;
61 }
62 }
63*/
64
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000065static void bin2SkBitmap(const char* bin_bmp, SkBitmap* sk_bmp,
robertphillips@google.com226d5082013-07-15 17:16:48 +000066 int h, int w, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000067 //init the SkBitmap
68 sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
69 sk_bmp->allocPixels();
70
71 for (int y = 0; y < h; ++y) { // for every row
72
robertphillips@google.com226d5082013-07-15 17:16:48 +000073 const char* curLine = &bin_bmp[y * rowBytes];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000074 for (int x = 0; x < w; ++x) {// for every pixel
75 if (getBit(curLine, x)) {
76 *sk_bmp->getAddr32(x,y) = ON;
77 }
78 else {
79 *sk_bmp->getAddr32(x,y) = OFF;
80 }
81 }
82 }
83}
84
85static bool test_bmp(skiatest::Reporter* reporter,
86 const SkBitmap* bmp1, const SkBitmap* bmp2,
87 int h, int w) {
88 for (int y = 0; y < h; ++y) { // loop through all pixels
89 for (int x = 0; x < w; ++x) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000090 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp2->getAddr32(x,y) );
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000091 }
92 }
93 return true;
94}
95
96static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path,
robertphillips@google.com226d5082013-07-15 17:16:48 +000097 const SkBitmap* truth, int w, int h){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000098 // make paint
99 SkPaint bmpPaint;
100 bmpPaint.setAntiAlias(true); // Black paint for bitmap
101 bmpPaint.setStyle(SkPaint::kFill_Style);
102 bmpPaint.setColor(SK_ColorBLACK);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000103
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000104 // make bmp
105 SkBitmap bmp;
106 bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h);
107 bmp.allocPixels();
robertphillips@google.com226d5082013-07-15 17:16:48 +0000108 SkCanvas canvas(bmp);
109 canvas.clear(0xFFFFFFFF);
110 canvas.drawPath(*path, bmpPaint);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000111
112 // test bmp
robertphillips@google.com226d5082013-07-15 17:16:48 +0000113 test_bmp(reporter, truth, &bmp, h, w);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000114}
115
116static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth,
robertphillips@google.com226d5082013-07-15 17:16:48 +0000117 const char* bin_bmp, int w, int h, int stride){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000118 // make path
119 SkPath path;
robertphillips@google.com226d5082013-07-15 17:16:48 +0000120 SkPathUtils::BitsToPath_Path(&path, bin_bmp, w, h, stride);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000121
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000122 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000123 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000124}
125
126static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
robertphillips@google.com226d5082013-07-15 17:16:48 +0000127 const char* bin_bmp, int w, int h, int stride){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000128 //generate bitmap
129 SkPath path;
robertphillips@google.com226d5082013-07-15 17:16:48 +0000130 SkPathUtils::BitsToPath_Region(&path, bin_bmp, w, h, stride);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000131
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000132 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000133 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000134}
135
136static void TestPathUtils(skiatest::Reporter* reporter) {
robertphillips@google.com226d5082013-07-15 17:16:48 +0000137 const int w[4] = {4, 8, 12, 16};
138// const int w[1] = {8};
139 const int h = 8, rowBytes = 4;
dierk@google.coma9505512013-07-01 20:36:31 +0000140
robertphillips@google.com226d5082013-07-15 17:16:48 +0000141 char bits[ h * rowBytes ];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000142 static char* bin_bmp = &bits[0];
143
144 //loop to run randomized test lots of times
145 for (int it = 0; it < NUM_IT; ++it)
146 {
147 // generate a random binary bitmap
robertphillips@google.com226d5082013-07-15 17:16:48 +0000148 fillRandomBits( h * rowBytes, bin_bmp); // generate random bitmap
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000149
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000150 // for each bitmap width, use subset of binary bitmap
robertphillips@google.comb4a28552013-07-15 17:33:57 +0000151 for (unsigned int i = 0; i < SK_ARRAY_COUNT(w); ++i) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000152 // generate truth bitmap
153 SkBitmap bmpTruth;
robertphillips@google.com226d5082013-07-15 17:16:48 +0000154 bin2SkBitmap(bin_bmp, &bmpTruth, h, w[i], rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000155
robertphillips@google.com226d5082013-07-15 17:16:48 +0000156 test_path(reporter, &bmpTruth, bin_bmp, w[i], h, rowBytes);
157 test_region(reporter, &bmpTruth, bin_bmp, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000158 }
159 }
160}
161
162#include "TestClassDef.h"
163DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils)