2013-07-01 17:50:29 +00:00
|
|
|
/*
|
|
|
|
* 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 "SkBitmap.h"
|
|
|
|
#include "SkCanvas.h"
|
2013-12-12 21:11:12 +00:00
|
|
|
#include "SkPathUtils.h"
|
2013-07-01 17:50:29 +00:00
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkTime.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2013-07-01 17:50:29 +00:00
|
|
|
|
2013-07-16 17:39:08 +00:00
|
|
|
const int kNumIt = 100;
|
|
|
|
|
2013-07-16 18:34:14 +00:00
|
|
|
static void fill_random_bits(int chars, char* bits){
|
2013-09-09 20:09:12 +00:00
|
|
|
SkRandom rand(SkTime::GetMSecs());
|
2013-07-01 17:50:29 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < chars; ++i){
|
|
|
|
bits[i] = rand.nextU();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 18:34:14 +00:00
|
|
|
static int get_bit(const char* buffer, int x) {
|
2013-07-01 17:50:29 +00:00
|
|
|
int byte = x >> 3;
|
|
|
|
int bit = x & 7;
|
|
|
|
|
2013-07-15 17:16:48 +00:00
|
|
|
return buffer[byte] & (128 >> bit);
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
2013-07-16 17:39:08 +00:00
|
|
|
|
2013-07-15 17:16:48 +00:00
|
|
|
/* // useful for debugging errors
|
|
|
|
#include <iostream>
|
2013-07-16 16:36:47 +00:00
|
|
|
static void print_bits( const char* bits, int w, int h) {
|
2013-07-15 17:16:48 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
for (int x = 0; x < w; ++x){
|
2013-07-16 16:36:47 +00:00
|
|
|
bool bit = get_bit(&bits[y], x)!=0;
|
2013-07-15 17:16:48 +00:00
|
|
|
std::cout << bit;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 16:36:47 +00:00
|
|
|
static void print_bmp( SkBitmap* bmp, int w, int h){
|
2013-07-15 17:16:48 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < h; ++y) {
|
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
int d = *bmp->getAddr32(x,y);
|
|
|
|
if (d == -1)
|
|
|
|
std::cout << 0;
|
|
|
|
else
|
|
|
|
std::cout << 1;
|
2013-07-16 16:36:47 +00:00
|
|
|
}
|
2013-07-15 17:16:48 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2013-07-16 17:39:08 +00:00
|
|
|
}
|
2013-07-15 17:16:48 +00:00
|
|
|
*/
|
2013-07-01 17:50:29 +00:00
|
|
|
|
2013-07-16 16:36:47 +00:00
|
|
|
static void binary_to_skbitmap(const char* bin_bmp, SkBitmap* sk_bmp,
|
|
|
|
int w, int h, int rowBytes){
|
2013-07-01 17:50:29 +00:00
|
|
|
//init the SkBitmap
|
2014-02-13 14:41:43 +00:00
|
|
|
sk_bmp->allocN32Pixels(w, h);
|
2013-07-01 17:50:29 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < h; ++y) { // for every row
|
|
|
|
|
2013-07-15 17:16:48 +00:00
|
|
|
const char* curLine = &bin_bmp[y * rowBytes];
|
2013-07-01 17:50:29 +00:00
|
|
|
for (int x = 0; x < w; ++x) {// for every pixel
|
2013-07-16 16:36:47 +00:00
|
|
|
if (get_bit(curLine, x)) {
|
|
|
|
*sk_bmp->getAddr32(x,y) = SK_ColorBLACK;
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-07-16 16:36:47 +00:00
|
|
|
*sk_bmp->getAddr32(x,y) = SK_ColorWHITE;
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_bmp(skiatest::Reporter* reporter,
|
|
|
|
const SkBitmap* bmp1, const SkBitmap* bmp2,
|
2013-07-16 16:36:47 +00:00
|
|
|
int w, int h) {
|
2013-07-01 17:50:29 +00:00
|
|
|
for (int y = 0; y < h; ++y) { // loop through all pixels
|
|
|
|
for (int x = 0; x < w; ++x) {
|
2013-07-15 17:16:48 +00:00
|
|
|
REPORTER_ASSERT( reporter, *bmp1->getAddr32(x,y) == *bmp2->getAddr32(x,y) );
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_path_eq(skiatest::Reporter* reporter, const SkPath* path,
|
2013-07-15 17:16:48 +00:00
|
|
|
const SkBitmap* truth, int w, int h){
|
2013-07-01 17:50:29 +00:00
|
|
|
// make paint
|
|
|
|
SkPaint bmpPaint;
|
|
|
|
bmpPaint.setAntiAlias(true); // Black paint for bitmap
|
|
|
|
bmpPaint.setStyle(SkPaint::kFill_Style);
|
|
|
|
bmpPaint.setColor(SK_ColorBLACK);
|
2013-07-02 07:00:59 +00:00
|
|
|
|
2013-07-01 17:50:29 +00:00
|
|
|
// make bmp
|
|
|
|
SkBitmap bmp;
|
2014-02-13 14:41:43 +00:00
|
|
|
bmp.allocN32Pixels(w, h);
|
2013-07-15 17:16:48 +00:00
|
|
|
SkCanvas canvas(bmp);
|
2013-07-16 16:36:47 +00:00
|
|
|
canvas.clear(SK_ColorWHITE);
|
2013-07-15 17:16:48 +00:00
|
|
|
canvas.drawPath(*path, bmpPaint);
|
2013-07-01 17:50:29 +00:00
|
|
|
|
|
|
|
// test bmp
|
2013-07-16 16:36:47 +00:00
|
|
|
test_bmp(reporter, truth, &bmp, w, h);
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_path(skiatest::Reporter* reporter, const SkBitmap* truth,
|
2013-07-16 16:36:47 +00:00
|
|
|
const char* bin_bmp, int w, int h, int rowBytes){
|
2013-07-01 17:50:29 +00:00
|
|
|
// make path
|
|
|
|
SkPath path;
|
2013-07-16 16:36:47 +00:00
|
|
|
SkPathUtils::BitsToPath_Path(&path, bin_bmp, w, h, rowBytes);
|
2013-07-02 07:00:59 +00:00
|
|
|
|
2013-07-01 17:50:29 +00:00
|
|
|
//test for correctness
|
2013-07-15 17:16:48 +00:00
|
|
|
test_path_eq(reporter, &path, truth, w, h);
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_region(skiatest::Reporter* reporter, const SkBitmap* truth,
|
2013-07-16 16:36:47 +00:00
|
|
|
const char* bin_bmp, int w, int h, int rowBytes){
|
2013-07-01 17:50:29 +00:00
|
|
|
//generate bitmap
|
|
|
|
SkPath path;
|
2013-07-16 16:36:47 +00:00
|
|
|
SkPathUtils::BitsToPath_Region(&path, bin_bmp, w, h, rowBytes);
|
2013-07-02 07:00:59 +00:00
|
|
|
|
2013-07-01 17:50:29 +00:00
|
|
|
//test for correctness
|
2013-07-15 17:16:48 +00:00
|
|
|
test_path_eq(reporter, &path, truth, w, h);
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(PathUtils, reporter) {
|
2013-07-16 16:36:47 +00:00
|
|
|
const int w[] = {4, 8, 12, 16};
|
2013-07-15 17:16:48 +00:00
|
|
|
const int h = 8, rowBytes = 4;
|
2013-07-01 20:36:31 +00:00
|
|
|
|
2013-07-15 17:16:48 +00:00
|
|
|
char bits[ h * rowBytes ];
|
2013-07-16 16:36:47 +00:00
|
|
|
static char* binBmp = &bits[0];
|
2013-07-01 17:50:29 +00:00
|
|
|
|
|
|
|
//loop to run randomized test lots of times
|
2013-07-16 17:39:08 +00:00
|
|
|
for (int it = 0; it < kNumIt; ++it)
|
2013-07-01 17:50:29 +00:00
|
|
|
{
|
|
|
|
// generate a random binary bitmap
|
2013-07-16 16:36:47 +00:00
|
|
|
fill_random_bits( h * rowBytes, binBmp); // generate random bitmap
|
2013-07-02 07:00:59 +00:00
|
|
|
|
2013-07-01 17:50:29 +00:00
|
|
|
// for each bitmap width, use subset of binary bitmap
|
2013-07-15 17:33:57 +00:00
|
|
|
for (unsigned int i = 0; i < SK_ARRAY_COUNT(w); ++i) {
|
2013-07-01 17:50:29 +00:00
|
|
|
// generate truth bitmap
|
|
|
|
SkBitmap bmpTruth;
|
2013-07-16 16:36:47 +00:00
|
|
|
binary_to_skbitmap(binBmp, &bmpTruth, w[i], h, rowBytes);
|
2013-07-01 17:50:29 +00:00
|
|
|
|
2013-07-16 16:36:47 +00:00
|
|
|
test_path(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
|
|
|
|
test_region(reporter, &bmpTruth, binBmp, w[i], h, rowBytes);
|
2013-07-01 17:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|