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

Side by Side Diff: tests/BitmapGetColorTest.cpp

Issue 18029021: add bitmap::eraseArea (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 5 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
« no previous file with comments | « src/core/SkBitmap.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
1
2 /* 1 /*
3 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
4 * 3 *
5 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 5 * found in the LICENSE file.
7 */ 6 */
7
8 #include "Test.h" 8 #include "Test.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkRect.h"
11 #include "SkRandom.h"
12
13 static int nextRand(SkRandom& rand, int min, int max) {
14 return min + (int)rand.nextRangeU(0, max - min);
15 }
16
17 static void rand_irect(SkIRect* rect, int W, int H, SkRandom& rand) {
18 const int DX = W / 2;
19 const int DY = H / 2;
20
21 rect->fLeft = nextRand(rand, -DX, W + DX);
22 rect->fTop = nextRand(rand, -DY, H + DY);
23 rect->fRight = nextRand(rand, -DX, W + DX);
24 rect->fBottom = nextRand(rand, -DY, H + DY);
25 rect->sort();
26 }
27
28 static void test_equal_A1_A8(skiatest::Reporter* reporter,
29 const SkBitmap& bm1, const SkBitmap& bm8) {
30 SkASSERT(SkBitmap::kA1_Config == bm1.config());
31 SkASSERT(SkBitmap::kA8_Config == bm8.config());
32
33 REPORTER_ASSERT(reporter, bm1.width() == bm8.width());
34 REPORTER_ASSERT(reporter, bm1.height() == bm8.height());
35 for (int y = 0; y < bm1.height(); ++y) {
36 for (int x = 0; x < bm1.width(); ++x) {
37 int p1 = *bm1.getAddr1(x, y) & (1 << (7 - (x & 7)));
38 SkASSERT(SkIsPow2(p1));
39 p1 = p1 ? 0xFF : 0;
40
41 int p8 = *bm8.getAddr8(x, y);
42 SkASSERT(0 == p8 || 0xFF == p8);
43
44 REPORTER_ASSERT(reporter, p1 == p8);
45 }
46 }
47 }
48
49 static void test_eraserect_A1(skiatest::Reporter* reporter) {
50 const int W = 43;
51 const int H = 13;
52
53 SkBitmap bm1, bm8;
54
55 bm1.setConfig(SkBitmap::kA1_Config, W, H);
56 bm1.allocPixels();
57 bm8.setConfig(SkBitmap::kA8_Config, W, H);
58 bm8.allocPixels();
59
60 SkRandom rand;
61 for (int i = 0; i < 10000; ++i) {
62 SkIRect area;
63 rand_irect(&area, W, H, rand);
64
65 bm1.eraseColor(0);
66 bm8.eraseColor(0);
67
68 bm1.eraseArea(area, SK_ColorWHITE);
69 bm8.eraseArea(area, SK_ColorWHITE);
70 test_equal_A1_A8(reporter, bm1, bm8);
71 }
72 }
10 73
11 static void TestGetColor(skiatest::Reporter* reporter) { 74 static void TestGetColor(skiatest::Reporter* reporter) {
12 static const struct Rec { 75 static const struct Rec {
13 SkBitmap::Config fConfig; 76 SkBitmap::Config fConfig;
14 SkColor fInColor; 77 SkColor fInColor;
15 SkColor fOutColor; 78 SkColor fOutColor;
16 } gRec[] = { 79 } gRec[] = {
17 // todo: add some tests that involve alpha, so we exercise the 80 // todo: add some tests that involve alpha, so we exercise the
18 // unpremultiply aspect of getColor() 81 // unpremultiply aspect of getColor()
19 { SkBitmap::kA8_Config, 0xFF000000, 0xFF000000 }, 82 { SkBitmap::kA8_Config, 0xFF000000, 0xFF000000 },
20 { SkBitmap::kA8_Config, 0, 0 }, 83 { SkBitmap::kA8_Config, 0, 0 },
21 { SkBitmap::kRGB_565_Config, 0xFF00FF00, 0xFF00FF00 }, 84 { SkBitmap::kRGB_565_Config, 0xFF00FF00, 0xFF00FF00 },
22 { SkBitmap::kRGB_565_Config, 0xFFFF00FF, 0xFFFF00FF }, 85 { SkBitmap::kRGB_565_Config, 0xFFFF00FF, 0xFFFF00FF },
23 { SkBitmap::kARGB_8888_Config, 0xFFFFFFFF, 0xFFFFFFFF }, 86 { SkBitmap::kARGB_8888_Config, 0xFFFFFFFF, 0xFFFFFFFF },
24 { SkBitmap::kARGB_8888_Config, 0, 0 }, 87 { SkBitmap::kARGB_8888_Config, 0, 0 },
25 { SkBitmap::kARGB_8888_Config, 0xFF224466, 0xFF224466 }, 88 { SkBitmap::kARGB_8888_Config, 0xFF224466, 0xFF224466 },
26 }; 89 };
27 90
91 // specify an area that doesn't touch (0,0) and may extend beyond the
92 // bitmap bounds (to test that we catch that in eraseArea
93 const SkColor initColor = 0xFF0000FF;
94 const SkIRect area = { 1, 1, 3, 3 };
95
28 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { 96 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) {
29 SkBitmap bm; 97 SkBitmap bm;
30 uint32_t storage[1]; 98 uint32_t storage[4];
31 bm.setConfig(gRec[i].fConfig, 1, 1); 99 bm.setConfig(gRec[i].fConfig, 2, 2);
32 bm.setPixels(storage); 100 bm.setPixels(storage);
33 bm.eraseColor(gRec[i].fInColor);
34 101
35 SkColor c = bm.getColor(0, 0); 102 bm.eraseColor(initColor);
103 bm.eraseArea(area, gRec[i].fInColor);
104
105 SkColor c = bm.getColor(1, 1);
36 REPORTER_ASSERT(reporter, c == gRec[i].fOutColor); 106 REPORTER_ASSERT(reporter, c == gRec[i].fOutColor);
37 } 107 }
108
109 test_eraserect_A1(reporter);
38 } 110 }
39 111
40 #include "TestClassDef.h" 112 #include "TestClassDef.h"
41 DEFINE_TESTCLASS("GetColor", TestGetColorClass, TestGetColor) 113 DEFINE_TESTCLASS("GetColor", TestGetColorClass, TestGetColor)
OLDNEW
« no previous file with comments | « src/core/SkBitmap.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698