| Index: tests/PathUtilsTest.cpp
|
| diff --git a/tests/PathUtilsTest.cpp b/tests/PathUtilsTest.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..aea311c66d4f29208d1cd298a1e71288b6566b59
|
| --- /dev/null
|
| +++ b/tests/PathUtilsTest.cpp
|
| @@ -0,0 +1,126 @@
|
| +
|
| +/*
|
| + * Copyright 2013 Google Inc.
|
| + *
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + */
|
| +
|
| +#include "Test.h"
|
| +
|
| +#include "SkBitmap.h"
|
| +#include "SkCanvas.h"
|
| +#include "SkPathUtils.h"
|
| +
|
| +#define NUM_IT 100000
|
| +
|
| +class SkBitmap;
|
| +
|
| +static void bin2SkBitmap(const char* bin_bmp, SkBitmap* sk_bmp,
|
| + int h, int w, int stride){
|
| + //init the SkBitmap
|
| + sk_bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
|
| + sk_bmp->allocPixels();
|
| +
|
| + for (int y = 0; y < h; ++y) { // for every row
|
| +
|
| + const char* curLine = &bin_bmp[y * stride];
|
| + for (int x = 0; x < w; ++x) {// for every pixel
|
| + if (SkPathUtils::getBit(curLine, x)) {
|
| + *sk_bmp->getAddr32(x,y) = ON;
|
| + }
|
| + else {
|
| + *sk_bmp->getAddr32(x,y) = OFF;
|
| + }
|
| + }
|
| + }
|
| +}
|
| +
|
| +static bool test_bmp(skiatest::Reporter* reporter,
|
| + const SkBitmap* bmp1, const SkBitmap* bmp2,
|
| + int h, int w) {
|
| + for (int y = 0; y < h; ++y) { // loop through all pixels
|
| + for (int x = 0; x < w; ++x) {
|
| + REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp1->getAddr32(x,y) );
|
| + }
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +static void test_path(skiatest::Reporter* reporter, const SkPath* path,
|
| + const SkBitmap* truth, int h, int w){
|
| + // make paint
|
| + SkPaint bmpPaint;
|
| + bmpPaint.setAntiAlias(true); // Black paint for bitmap
|
| + bmpPaint.setStyle(SkPaint::kFill_Style);
|
| + bmpPaint.setColor(SK_ColorBLACK);
|
| +
|
| + // make bmp
|
| + SkBitmap bmp;
|
| + bmp.setConfig(SkBitmap::kARGB_8888_Config, w, h);
|
| + bmp.allocPixels();
|
| + SkCanvas(bmp).drawPath(*path, bmpPaint);
|
| +
|
| + // test bmp
|
| + test_bmp(reporter, &bmp, truth, h, w);
|
| +}
|
| +
|
| +static void test_pixel_path(skiatest::Reporter* reporter, SkBitmap* truth,
|
| + const char* bin_bmp, int h, int w, int stride){
|
| + //generate path
|
| + SkPath path;
|
| + SkPathUtils::bitmap2path(&path, bin_bmp, SkPathUtils::line2path_pixel,
|
| + h, w, stride);
|
| +
|
| + //test for correctness
|
| + test_path(reporter, &path, truth, h, w);
|
| +}
|
| +
|
| +static void test_span_path(skiatest::Reporter* reporter, const SkBitmap* truth,
|
| + const char* bin_bmp, int h, int w, int stride){
|
| + // make path
|
| + SkPath path;
|
| + SkPathUtils::bitmap2path(&path, bin_bmp, SkPathUtils::line2path_span,
|
| + h, w, stride);
|
| +
|
| + //test for correctness
|
| + test_path(reporter, &path, truth, h, w);
|
| +}
|
| +
|
| +static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
|
| + const char* bin_bmp, int h, int w, int stride){
|
| + //generate bitmap
|
| + SkPath path;
|
| + SkPathUtils::bitmap2path_region(&path, bin_bmp, h, w, stride);
|
| +
|
| + //test for correctness
|
| + test_path(reporter, &path, truth, h, w);
|
| +}
|
| +
|
| +static void TestPathUtils(skiatest::Reporter* reporter) {
|
| + const int w[4] = {8, 16, 32, 64};
|
| + int h = 16, stride = 8;
|
| + char bits[ h * stride ];
|
| + static const char* bin_bmp = &bits[0];
|
| +
|
| + //loop to run randomized test lots of times
|
| + for (int it = 0; it < NUM_IT; ++it)
|
| + {
|
| + // generate a random binary bitmap
|
| + SkPathUtils::fillRandomBits( h * stride, bits ); // generate random bitmap
|
| +
|
| + // for each bitmap width, use subset of binary bitmap
|
| + for (int i = 0; i < 4; ++i) {
|
| + // generate truth bitmap
|
| + SkBitmap bmpTruth;
|
| + bin2SkBitmap(bin_bmp, &bmpTruth, h, w[i], stride);
|
| +
|
| + test_pixel_path(reporter, &bmpTruth, bin_bmp, h, w[i], stride);
|
| + test_span_path(reporter, &bmpTruth, bin_bmp, h, w[i], stride);
|
| + test_region(reporter, &bmpTruth, bin_bmp, h, w[i], stride);
|
| + }
|
| + }
|
| +}
|
| +
|
| +#include "TestClassDef.h"
|
| +DEFINE_TESTCLASS("PathUtils", PathUtils, TestPathUtils)
|
|
|