blob: a147487cfc11ed2b1aebec559d6d3206f041a495 [file] [log] [blame]
commit-bot@chromium.org064779a2013-07-01 17:50:29 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00008#include "Test.h"
9#include "TestClassDef.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000010#include "SkBitmap.h"
11#include "SkCanvas.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000012#include "SkPathUtils.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000013#include "SkRandom.h"
14#include "SkTime.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000015
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000016const int kNumIt = 100;
17
commit-bot@chromium.org50df6312013-07-16 18:34:14 +000018static void fill_random_bits(int chars, char* bits){
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000019 SkRandom rand(SkTime::GetMSecs());
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000020
21 for (int i = 0; i < chars; ++i){
22 bits[i] = rand.nextU();
23 }
24}
25
commit-bot@chromium.org50df6312013-07-16 18:34:14 +000026static int get_bit(const char* buffer, int x) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000027 int byte = x >> 3;
28 int bit = x & 7;
29
robertphillips@google.com226d5082013-07-15 17:16:48 +000030 return buffer[byte] & (128 >> bit);
31}
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000032
robertphillips@google.com226d5082013-07-15 17:16:48 +000033/* // useful for debugging errors
34 #include <iostream>
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000035static void print_bits( const char* bits, int w, int h) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000036
37 for (int y = 0; y < h; ++y) {
38 for (int x = 0; x < w; ++x){
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000039 bool bit = get_bit(&bits[y], x)!=0;
robertphillips@google.com226d5082013-07-15 17:16:48 +000040 std::cout << bit;
41 }
42 std::cout << std::endl;
43 }
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000044}
45
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000046static void print_bmp( SkBitmap* bmp, int w, int h){
robertphillips@google.com226d5082013-07-15 17:16:48 +000047
48 for (int y = 0; y < h; ++y) {
49 for (int x = 0; x < w; ++x) {
50 int d = *bmp->getAddr32(x,y);
51 if (d == -1)
52 std::cout << 0;
53 else
54 std::cout << 1;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000055 }
robertphillips@google.com226d5082013-07-15 17:16:48 +000056 std::cout << std::endl;
57 }
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000058}
robertphillips@google.com226d5082013-07-15 17:16:48 +000059*/
60
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000061static void binary_to_skbitmap(const char* bin_bmp, SkBitmap* sk_bmp,
62 int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000063 //init the SkBitmap
64 sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
65 sk_bmp->allocPixels();
66
67 for (int y = 0; y < h; ++y) { // for every row
68
robertphillips@google.com226d5082013-07-15 17:16:48 +000069 const char* curLine = &bin_bmp[y * rowBytes];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000070 for (int x = 0; x < w; ++x) {// for every pixel
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000071 if (get_bit(curLine, x)) {
72 *sk_bmp->getAddr32(x,y) = SK_ColorBLACK;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000073 }
74 else {
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000075 *sk_bmp->getAddr32(x,y) = SK_ColorWHITE;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000076 }
77 }
78 }
79}
80
81static bool test_bmp(skiatest::Reporter* reporter,
82 const SkBitmap* bmp1, const SkBitmap* bmp2,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000083 int w, int h) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000084 for (int y = 0; y < h; ++y) { // loop through all pixels
85 for (int x = 0; x < w; ++x) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000086 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp2->getAddr32(x,y) );
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000087 }
88 }
89 return true;
90}
91
92static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path,
robertphillips@google.com226d5082013-07-15 17:16:48 +000093 const SkBitmap* truth, int w, int h){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000094 // make paint
95 SkPaint bmpPaint;
96 bmpPaint.setAntiAlias(true); // Black paint for bitmap
97 bmpPaint.setStyle(SkPaint::kFill_Style);
98 bmpPaint.setColor(SK_ColorBLACK);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000099
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000100 // make bmp
101 SkBitmap bmp;
102 bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h);
103 bmp.allocPixels();
robertphillips@google.com226d5082013-07-15 17:16:48 +0000104 SkCanvas canvas(bmp);
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000105 canvas.clear(SK_ColorWHITE);
robertphillips@google.com226d5082013-07-15 17:16:48 +0000106 canvas.drawPath(*path, bmpPaint);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000107
108 // test bmp
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000109 test_bmp(reporter, truth, &bmp, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000110}
111
112static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000113 const char* bin_bmp, int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000114 // make path
115 SkPath path;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000116 SkPathUtils::BitsToPath_Path(&path, bin_bmp, w, h, rowBytes);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000117
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000118 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000119 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000120}
121
122static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000123 const char* bin_bmp, int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000124 //generate bitmap
125 SkPath path;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000126 SkPathUtils::BitsToPath_Region(&path, bin_bmp, w, h, rowBytes);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000127
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000128 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000129 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000130}
131
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000132DEF_TEST(PathUtils, reporter) {
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000133 const int w[] = {4, 8, 12, 16};
robertphillips@google.com226d5082013-07-15 17:16:48 +0000134 const int h = 8, rowBytes = 4;
dierk@google.coma9505512013-07-01 20:36:31 +0000135
robertphillips@google.com226d5082013-07-15 17:16:48 +0000136 char bits[ h * rowBytes ];
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000137 static char* binBmp = &bits[0];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000138
139 //loop to run randomized test lots of times
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +0000140 for (int it = 0; it < kNumIt; ++it)
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000141 {
142 // generate a random binary bitmap
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000143 fill_random_bits( h * rowBytes, binBmp); // generate random bitmap
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000144
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000145 // for each bitmap width, use subset of binary bitmap
robertphillips@google.comb4a28552013-07-15 17:33:57 +0000146 for (unsigned int i = 0; i < SK_ARRAY_COUNT(w); ++i) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000147 // generate truth bitmap
148 SkBitmap bmpTruth;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000149 binary_to_skbitmap(binBmp, &bmpTruth, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000150
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000151 test_path(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
152 test_region(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000153 }
154 }
155}