Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: tests/PathUtilsTest.cpp

Issue 16829003: Adding my Bitmap2Path sample for 1on1 meeting. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Added SkRegion mode to PathUtils and Test. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« src/utils/SkPathUtils.cpp ('K') | « src/utils/SkPathUtils.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1
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
15 #define NUM_IT 100000
16
17 class SkBitmap;
18
19 static void bin2SkBitmap(const char* bin_bmp, SkBitmap* sk_bmp,
20 int h, int w, int stride){
21 //init the SkBitmap
22 sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
23 sk_bmp->allocPixels();
24
25 for (int y = 0; y < h; ++y) { // for every row
26
27 const char* curLine = &bin_bmp[y * stride];
28 for (int x = 0; x < w; ++x) {// for every pixel
29 if (SkPathUtils::getBit(curLine, x)) {
30 *sk_bmp->getAddr32(x,y) = ON;
31 }
32 else {
33 *sk_bmp->getAddr32(x,y) = OFF;
34 }
35 }
36 }
37 }
38
39 static bool test_bmp(skiatest::Reporter* reporter,
40 const SkBitmap* bmp1, const SkBitmap* bmp2,
41 int h, int w) {
42 for (int y = 0; y < h; ++y) { // loop through all pixels
43 for (int x = 0; x < w; ++x) {
44 REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp1->getAddr32 (x,y) );
45 }
46 }
47 return true;
48 }
49
50 static void test_path(skiatest::Reporter* reporter, const SkPath* path,
51 const SkBitmap* truth, int h, int w){
52 // make paint
53 SkPaint bmpPaint;
54 bmpPaint.setAntiAlias(true); // Black paint for bitmap
55 bmpPaint.setStyle(SkPaint::kFill_Style);
56 bmpPaint.setColor(SK_ColorBLACK);
57
58 // make bmp
59 SkBitmap bmp;
60 bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h);
61 bmp.allocPixels();
62 SkCanvas(bmp).drawPath(*path, bmpPaint);
63
64 // test bmp
65 test_bmp(reporter, &bmp, truth, h, w);
66 }
67
68 static void test_pixel_path(skiatest::Reporter* reporter, SkBitmap* truth,
69 const char* bin_bmp, int h, int w, int stride){
70 //generate path
71 SkPath path;
72 SkPathUtils::bitmap2path(&path, bin_bmp, SkPathUtils::line2path_pixel,
73 h, w, stride);
74
75 //test for correctness
76 test_path(reporter, &path, truth, h, w);
77 }
78
79 static void test_span_path(skiatest::Reporter* reporter, const SkBitmap* truth,
80 const char* bin_bmp, int h, int w, int stride){
81 // make path
82 SkPath path;
83 SkPathUtils::bitmap2path(&path, bin_bmp, SkPathUtils::line2path_span,
84 h, w, stride);
85
86 //test for correctness
87 test_path(reporter, &path, truth, h, w);
88 }
89
90 static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
91 const char* bin_bmp, int h, int w, int stride){
92 //generate bitmap
93 SkPath path;
94 SkPathUtils::bitmap2path_region(&path, bin_bmp, h, w, stride);
95
96 //test for correctness
97 test_path(reporter, &path, truth, h, w);
98 }
99
100 static void TestPathUtils(skiatest::Reporter* reporter) {
101 const int w[4] = {8, 16, 32, 64};
102 int h = 16, stride = 8;
103 char bits[ h * stride ];
104 static const char* bin_bmp = &bits[0];
105
106 //loop to run randomized test lots of times
107 for (int it = 0; it < NUM_IT; ++it)
108 {
109 // generate a random binary bitmap
110 SkPathUtils::fillRandomBits( h * stride, bits ); // generate random bitm ap
111
112 // for each bitmap width, use subset of binary bitmap
113 for (int i = 0; i < 4; ++i) {
114 // generate truth bitmap
115 SkBitmap bmpTruth;
116 bin2SkBitmap(bin_bmp, &bmpTruth, h, w[i], stride);
117
118 test_pixel_path(reporter, &bmpTruth, bin_bmp, h, w[i], stride);
119 test_span_path(reporter, &bmpTruth, bin_bmp, h, w[i], stride);
120 test_region(reporter, &bmpTruth, bin_bmp, h, w[i], stride);
121 }
122 }
123 }
124
125 #include "TestClassDef.h"
126 DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils)
OLDNEW
« src/utils/SkPathUtils.cpp ('K') | « src/utils/SkPathUtils.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698