blob: 872af0b3632234e4ba48f6a38e996388b2b1086c [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
commit-bot@chromium.org064779a2013-07-01 17:50:29 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000010#include "SkPathUtils.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000011#include "SkRandom.h"
12#include "SkTime.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000013#include "Test.h"
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000014
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000015const int kNumIt = 100;
16
commit-bot@chromium.org50df6312013-07-16 18:34:14 +000017static void fill_random_bits(int chars, char* bits){
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000018 SkRandom rand(SkTime::GetMSecs());
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000019
20 for (int i = 0; i < chars; ++i){
21 bits[i] = rand.nextU();
22 }
23}
24
commit-bot@chromium.org50df6312013-07-16 18:34:14 +000025static int get_bit(const char* buffer, int x) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000026 int byte = x >> 3;
27 int bit = x & 7;
28
robertphillips@google.com226d5082013-07-15 17:16:48 +000029 return buffer[byte] & (128 >> bit);
30}
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000031
robertphillips@google.com226d5082013-07-15 17:16:48 +000032/* // useful for debugging errors
33 #include <iostream>
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000034static void print_bits( const char* bits, int w, int h) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000035
36 for (int y = 0; y < h; ++y) {
37 for (int x = 0; x < w; ++x){
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000038 bool bit = get_bit(&bits[y], x)!=0;
robertphillips@google.com226d5082013-07-15 17:16:48 +000039 std::cout << bit;
40 }
41 std::cout << std::endl;
42 }
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000043}
44
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000045static void print_bmp( SkBitmap* bmp, int w, int h){
robertphillips@google.com226d5082013-07-15 17:16:48 +000046
47 for (int y = 0; y < h; ++y) {
48 for (int x = 0; x < w; ++x) {
49 int d = *bmp->getAddr32(x,y);
50 if (d == -1)
51 std::cout << 0;
52 else
53 std::cout << 1;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000054 }
robertphillips@google.com226d5082013-07-15 17:16:48 +000055 std::cout << std::endl;
56 }
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +000057}
robertphillips@google.com226d5082013-07-15 17:16:48 +000058*/
59
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000060static void binary_to_skbitmap(const char* bin_bmp, SkBitmap* sk_bmp,
61 int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000062 //init the SkBitmap
63 sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
64 sk_bmp->allocPixels();
65
66 for (int y = 0; y < h; ++y) { // for every row
67
robertphillips@google.com226d5082013-07-15 17:16:48 +000068 const char* curLine = &bin_bmp[y * rowBytes];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000069 for (int x = 0; x < w; ++x) {// for every pixel
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000070 if (get_bit(curLine, x)) {
71 *sk_bmp->getAddr32(x,y) = SK_ColorBLACK;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000072 }
73 else {
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000074 *sk_bmp->getAddr32(x,y) = SK_ColorWHITE;
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000075 }
76 }
77 }
78}
79
80static bool test_bmp(skiatest::Reporter* reporter,
81 const SkBitmap* bmp1, const SkBitmap* bmp2,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +000082 int w, int h) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000083 for (int y = 0; y < h; ++y) { // loop through all pixels
84 for (int x = 0; x < w; ++x) {
robertphillips@google.com226d5082013-07-15 17:16:48 +000085 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp2->getAddr32(x,y) );
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000086 }
87 }
88 return true;
89}
90
91static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path,
robertphillips@google.com226d5082013-07-15 17:16:48 +000092 const SkBitmap* truth, int w, int h){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000093 // make paint
94 SkPaint bmpPaint;
95 bmpPaint.setAntiAlias(true); // Black paint for bitmap
96 bmpPaint.setStyle(SkPaint::kFill_Style);
97 bmpPaint.setColor(SK_ColorBLACK);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +000098
commit-bot@chromium.org064779a2013-07-01 17:50:29 +000099 // make bmp
100 SkBitmap bmp;
101 bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h);
102 bmp.allocPixels();
robertphillips@google.com226d5082013-07-15 17:16:48 +0000103 SkCanvas canvas(bmp);
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000104 canvas.clear(SK_ColorWHITE);
robertphillips@google.com226d5082013-07-15 17:16:48 +0000105 canvas.drawPath(*path, bmpPaint);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000106
107 // test bmp
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000108 test_bmp(reporter, truth, &bmp, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000109}
110
111static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000112 const char* bin_bmp, int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000113 // make path
114 SkPath path;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000115 SkPathUtils::BitsToPath_Path(&path, bin_bmp, w, h, rowBytes);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000116
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000117 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000118 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000119}
120
121static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000122 const char* bin_bmp, int w, int h, int rowBytes){
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000123 //generate bitmap
124 SkPath path;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000125 SkPathUtils::BitsToPath_Region(&path, bin_bmp, w, h, rowBytes);
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000126
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000127 //test for correctness
robertphillips@google.com226d5082013-07-15 17:16:48 +0000128 test_path_eq(reporter, &path, truth, w, h);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000129}
130
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000131DEF_TEST(PathUtils, reporter) {
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000132 const int w[] = {4, 8, 12, 16};
robertphillips@google.com226d5082013-07-15 17:16:48 +0000133 const int h = 8, rowBytes = 4;
dierk@google.coma9505512013-07-01 20:36:31 +0000134
robertphillips@google.com226d5082013-07-15 17:16:48 +0000135 char bits[ h * rowBytes ];
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000136 static char* binBmp = &bits[0];
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000137
138 //loop to run randomized test lots of times
commit-bot@chromium.orgb92f9fb2013-07-16 17:39:08 +0000139 for (int it = 0; it < kNumIt; ++it)
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000140 {
141 // generate a random binary bitmap
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000142 fill_random_bits( h * rowBytes, binBmp); // generate random bitmap
skia.committer@gmail.com0d55dd72013-07-02 07:00:59 +0000143
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000144 // for each bitmap width, use subset of binary bitmap
robertphillips@google.comb4a28552013-07-15 17:33:57 +0000145 for (unsigned int i = 0; i < SK_ARRAY_COUNT(w); ++i) {
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000146 // generate truth bitmap
147 SkBitmap bmpTruth;
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000148 binary_to_skbitmap(binBmp, &bmpTruth, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000149
commit-bot@chromium.org40f960e2013-07-16 16:36:47 +0000150 test_path(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
151 test_region(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
commit-bot@chromium.org064779a2013-07-01 17:50:29 +0000152 }
153 }
154}