blob: 15f981bb901a1a9919bd86b2102cc1993ec08536 [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
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +00009#include "SkPathUtils.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000010
11#include "SkBitmap.h"
12#include "SkCanvas.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000013#include "SkRandom.h"
14#include "SkTime.h"
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000015#include "Test.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000016
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000017const int kNumIt = 100;
18
commit-bot@chromium.org50df6312013-07-16 18:34:14 +000019static void fill_random_bits(int chars, char* bits){
robertphillips@google.comf8846502013-07-01 23:47:44 +000020 SkMWCRandom rand(SkTime::GetMSecs());
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000021
22 for (int i = 0; i < chars; ++i){
23 bits[i] = rand.nextU();
24 }
25}
26
commit-bot@chromium.org50df6312013-07-16 18:34:14 +000027static int get_bit(const char* buffer, int x) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000028 int byte = x >> 3;
29 int bit = x & 7;
30
robertphillips@google.com226d5082013-07-15 17:16:48 +000031 return buffer[byte] & (128 >> bit);
32}
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000033
robertphillips@google.com226d5082013-07-15 17:16:48 +000034/* // useful for debugging errors
35 #include <iostream>
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000036static void print_bits( const char* bits, int w, int h) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000037
38 for (int y = 0; y < h; ++y) {
39 for (int x = 0; x < w; ++x){
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000040 bool bit = get_bit(&bits[y], x)!=0;
robertphillips@google.com226d5082013-07-15 17:16:48 +000041 std::cout << bit;
42 }
43 std::cout << std::endl;
44 }
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000045}
46
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000047static void print_bmp( SkBitmap* bmp, int w, int h){
robertphillips@google.com226d5082013-07-15 17:16:48 +000048
49 for (int y = 0; y < h; ++y) {
50 for (int x = 0; x < w; ++x) {
51 int d = *bmp->getAddr32(x,y);
52 if (d == -1)
53 std::cout << 0;
54 else
55 std::cout << 1;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000056 }
robertphillips@google.com226d5082013-07-15 17:16:48 +000057 std::cout << std::endl;
58 }
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000059}
robertphillips@google.com226d5082013-07-15 17:16:48 +000060*/
61
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000062static void binary_to_skbitmap(const char* bin_bmp, SkBitmap* sk_bmp,
63 int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000064 //init the SkBitmap
65 sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
66 sk_bmp->allocPixels();
67
68 for (int y = 0; y < h; ++y) { // for every row
69
robertphillips@google.com226d5082013-07-15 17:16:48 +000070 const char* curLine = &bin_bmp[y * rowBytes];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000071 for (int x = 0; x < w; ++x) {// for every pixel
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000072 if (get_bit(curLine, x)) {
73 *sk_bmp->getAddr32(x,y) = SK_ColorBLACK;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000074 }
75 else {
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000076 *sk_bmp->getAddr32(x,y) = SK_ColorWHITE;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000077 }
78 }
79 }
80}
81
82static bool test_bmp(skiatest::Reporter* reporter,
83 const SkBitmap* bmp1, const SkBitmap* bmp2,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000084 int w, int h) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000085 for (int y = 0; y < h; ++y) { // loop through all pixels
86 for (int x = 0; x < w; ++x) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000087 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp2->getAddr32(x,y) );
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000088 }
89 }
90 return true;
91}
92
93static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path,
robertphillips@google.com226d5082013-07-15 17:16:48 +000094 const SkBitmap* truth, int w, int h){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000095 // make paint
96 SkPaint bmpPaint;
97 bmpPaint.setAntiAlias(true); // Black paint for bitmap
98 bmpPaint.setStyle(SkPaint::kFill_Style);
99 bmpPaint.setColor(SK_ColorBLACK);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000100
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000101 // make bmp
102 SkBitmap bmp;
103 bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h);
104 bmp.allocPixels();
robertphillips@google.com226d5082013-07-15 17:16:48 +0000105 SkCanvas canvas(bmp);
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000106 canvas.clear(SK_ColorWHITE);
robertphillips@google.com226d5082013-07-15 17:16:48 +0000107 canvas.drawPath(*path, bmpPaint);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000108
109 // test bmp
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000110 test_bmp(reporter, truth, &bmp, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000111}
112
113static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000114 const char* bin_bmp, int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000115 // make path
116 SkPath path;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000117 SkPathUtils::BitsToPath_Path(&path, bin_bmp, w, h, rowBytes);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000118
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000119 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000120 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000121}
122
123static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000124 const char* bin_bmp, int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000125 //generate bitmap
126 SkPath path;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000127 SkPathUtils::BitsToPath_Region(&path, bin_bmp, w, h, rowBytes);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000128
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000129 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000130 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000131}
132
133static void TestPathUtils(skiatest::Reporter* reporter) {
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000134 const int w[] = {4, 8, 12, 16};
robertphillips@google.com226d5082013-07-15 17:16:48 +0000135 const int h = 8, rowBytes = 4;
dierk@google.coma9505512013-07-01 20:36:31 +0000136
robertphillips@google.com226d5082013-07-15 17:16:48 +0000137 char bits[ h * rowBytes ];
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000138 static char* binBmp = &bits[0];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000139
140 //loop to run randomized test lots of times
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +0000141 for (int it = 0; it < kNumIt; ++it)
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000142 {
143 // generate a random binary bitmap
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000144 fill_random_bits( h * rowBytes, binBmp); // generate random bitmap
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000145
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000146 // for each bitmap width, use subset of binary bitmap
robertphillips@google.comb4a28552013-07-15 17:33:57 +0000147 for (unsigned int i = 0; i < SK_ARRAY_COUNT(w); ++i) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000148 // generate truth bitmap
149 SkBitmap bmpTruth;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000150 binary_to_skbitmap(binBmp, &bmpTruth, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000151
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000152 test_path(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
153 test_region(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000154 }
155 }
156}
157
158#include "TestClassDef.h"
159DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils)