blob: 2c403e3743db768634b3a958352db6619a0c8514 [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
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000017#define SK_NUM_IT 100
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000018
19class SkBitmap;
20
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000021static void fill_random_bits( int chars, char* bits ){
robertphillips@google.comf8846502013-07-01 23:47:44 +000022 SkMWCRandom rand(SkTime::GetMSecs());
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000023
24 for (int i = 0; i < chars; ++i){
25 bits[i] = rand.nextU();
26 }
27}
28
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000029static int get_bit( const char* buffer, int x ) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000030 int byte = x >> 3;
31 int bit = x & 7;
32
robertphillips@google.com226d5082013-07-15 17:16:48 +000033 return buffer[byte] & (128 >> bit);
34}
35/* // useful for debugging errors
36 #include <iostream>
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000037static void print_bits( const char* bits, int w, int h) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000038
39 for (int y = 0; y < h; ++y) {
40 for (int x = 0; x < w; ++x){
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000041 bool bit = get_bit(&bits[y], x)!=0;
robertphillips@google.com226d5082013-07-15 17:16:48 +000042 std::cout << bit;
43 }
44 std::cout << std::endl;
45 }
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000046}
47
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000048static void print_bmp( SkBitmap* bmp, int w, int h){
robertphillips@google.com226d5082013-07-15 17:16:48 +000049
50 for (int y = 0; y < h; ++y) {
51 for (int x = 0; x < w; ++x) {
52 int d = *bmp->getAddr32(x,y);
53 if (d == -1)
54 std::cout << 0;
55 else
56 std::cout << 1;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000057 }
robertphillips@google.com226d5082013-07-15 17:16:48 +000058 std::cout << std::endl;
59 }
60 }
61*/
62
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000063static void binary_to_skbitmap(const char* bin_bmp, SkBitmap* sk_bmp,
64 int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000065 //init the SkBitmap
66 sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
67 sk_bmp->allocPixels();
68
69 for (int y = 0; y < h; ++y) { // for every row
70
robertphillips@google.com226d5082013-07-15 17:16:48 +000071 const char* curLine = &bin_bmp[y * rowBytes];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000072 for (int x = 0; x < w; ++x) {// for every pixel
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000073 if (get_bit(curLine, x)) {
74 *sk_bmp->getAddr32(x,y) = SK_ColorBLACK;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000075 }
76 else {
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000077 *sk_bmp->getAddr32(x,y) = SK_ColorWHITE;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000078 }
79 }
80 }
81}
82
83static bool test_bmp(skiatest::Reporter* reporter,
84 const SkBitmap* bmp1, const SkBitmap* bmp2,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000085 int w, int h) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000086 for (int y = 0; y < h; ++y) { // loop through all pixels
87 for (int x = 0; x < w; ++x) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000088 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp2->getAddr32(x,y) );
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000089 }
90 }
91 return true;
92}
93
94static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path,
robertphillips@google.com226d5082013-07-15 17:16:48 +000095 const SkBitmap* truth, int w, int h){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000096 // make paint
97 SkPaint bmpPaint;
98 bmpPaint.setAntiAlias(true); // Black paint for bitmap
99 bmpPaint.setStyle(SkPaint::kFill_Style);
100 bmpPaint.setColor(SK_ColorBLACK);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000101
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000102 // make bmp
103 SkBitmap bmp;
104 bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h);
105 bmp.allocPixels();
robertphillips@google.com226d5082013-07-15 17:16:48 +0000106 SkCanvas canvas(bmp);
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000107 canvas.clear(SK_ColorWHITE);
robertphillips@google.com226d5082013-07-15 17:16:48 +0000108 canvas.drawPath(*path, bmpPaint);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000109
110 // test bmp
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000111 test_bmp(reporter, truth, &bmp, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000112}
113
114static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000115 const char* bin_bmp, int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000116 // make path
117 SkPath path;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000118 SkPathUtils::BitsToPath_Path(&path, bin_bmp, w, h, rowBytes);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000119
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000120 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000121 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000122}
123
124static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000125 const char* bin_bmp, int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000126 //generate bitmap
127 SkPath path;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000128 SkPathUtils::BitsToPath_Region(&path, bin_bmp, w, h, rowBytes);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000129
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000130 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000131 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000132}
133
134static void TestPathUtils(skiatest::Reporter* reporter) {
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000135 const int w[] = {4, 8, 12, 16};
robertphillips@google.com226d5082013-07-15 17:16:48 +0000136 const int h = 8, rowBytes = 4;
dierk@google.coma9505512013-07-01 20:36:31 +0000137
robertphillips@google.com226d5082013-07-15 17:16:48 +0000138 char bits[ h * rowBytes ];
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000139 static char* binBmp = &bits[0];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000140
141 //loop to run randomized test lots of times
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000142 for (int it = 0; it < SK_NUM_IT; ++it)
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000143 {
144 // generate a random binary bitmap
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000145 fill_random_bits( h * rowBytes, binBmp); // generate random bitmap
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000146
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000147 // for each bitmap width, use subset of binary bitmap
robertphillips@google.comb4a28552013-07-15 17:33:57 +0000148 for (unsigned int i = 0; i < SK_ARRAY_COUNT(w); ++i) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000149 // generate truth bitmap
150 SkBitmap bmpTruth;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000151 binary_to_skbitmap(binBmp, &bmpTruth, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000152
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000153 test_path(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
154 test_region(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000155 }
156 }
157}
158
159#include "TestClassDef.h"
160DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils)