1 /*
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
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkMallocPixelRef.h"
12 #include "include/core/SkPixelRef.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkTypes.h"
17 #include "include/private/SkFloatingPoint.h"
18 #include "include/utils/SkRandom.h"
19 #include "tests/Test.h"
20 #include "tools/ToolUtils.h"
21
22 #include <initializer_list>
23
test_peekpixels(skiatest::Reporter * reporter)24 static void test_peekpixels(skiatest::Reporter* reporter) {
25 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
26
27 SkPixmap pmap;
28 SkBitmap bm;
29
30 // empty should return false
31 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
32 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
33
34 // no pixels should return false
35 bm.setInfo(SkImageInfo::MakeN32Premul(10, 10));
36 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
37 REPORTER_ASSERT(reporter, !bm.peekPixels(&pmap));
38
39 // real pixels should return true
40 bm.allocPixels(info);
41 REPORTER_ASSERT(reporter, bm.peekPixels(nullptr));
42 REPORTER_ASSERT(reporter, bm.peekPixels(&pmap));
43 REPORTER_ASSERT(reporter, pmap.info() == bm.info());
44 REPORTER_ASSERT(reporter, pmap.addr() == bm.getPixels());
45 REPORTER_ASSERT(reporter, pmap.rowBytes() == bm.rowBytes());
46 }
47
48 // https://code.google.com/p/chromium/issues/detail?id=446164
test_bigalloc(skiatest::Reporter * reporter)49 static void test_bigalloc(skiatest::Reporter* reporter) {
50 const int width = 0x40000001;
51 const int height = 0x00000096;
52 const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
53
54 SkBitmap bm;
55 REPORTER_ASSERT(reporter, !bm.tryAllocPixels(info));
56
57 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, info.minRowBytes());
58 REPORTER_ASSERT(reporter, !pr);
59 }
60
test_allocpixels(skiatest::Reporter * reporter)61 static void test_allocpixels(skiatest::Reporter* reporter) {
62 const int width = 10;
63 const int height = 10;
64 const SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
65 const size_t explicitRowBytes = info.minRowBytes() + 24;
66
67 SkBitmap bm;
68 bm.setInfo(info);
69 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
70 bm.allocPixels();
71 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
72 bm.reset();
73 bm.allocPixels(info);
74 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
75
76 bm.setInfo(info, explicitRowBytes);
77 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
78 bm.allocPixels();
79 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
80 bm.reset();
81 bm.allocPixels(info, explicitRowBytes);
82 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
83
84 bm.reset();
85 bm.setInfo(info, 0);
86 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
87 bm.reset();
88 bm.allocPixels(info, 0);
89 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
90
91 bm.reset();
92 bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit
93 REPORTER_ASSERT(reporter, !success);
94 REPORTER_ASSERT(reporter, bm.isNull());
95 }
96
test_bigwidth(skiatest::Reporter * reporter)97 static void test_bigwidth(skiatest::Reporter* reporter) {
98 SkBitmap bm;
99 int width = 1 << 29; // *4 will be the high-bit of 32bit int
100
101 SkImageInfo info = SkImageInfo::MakeA8(width, 1);
102 REPORTER_ASSERT(reporter, bm.setInfo(info));
103 REPORTER_ASSERT(reporter, bm.setInfo(info.makeColorType(kRGB_565_SkColorType)));
104
105 // for a 4-byte config, this width will compute a rowbytes of 0x80000000,
106 // which does not fit in a int32_t. setConfig should detect this, and fail.
107
108 // TODO: perhaps skia can relax this, and only require that rowBytes fit
109 // in a uint32_t (or larger), but for now this is the constraint.
110
111 REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType)));
112 }
113
114 /**
115 * This test contains basic sanity checks concerning bitmaps.
116 */
DEF_TEST(Bitmap,reporter)117 DEF_TEST(Bitmap, reporter) {
118 // Zero-sized bitmaps are allowed
119 for (int width = 0; width < 2; ++width) {
120 for (int height = 0; height < 2; ++height) {
121 SkBitmap bm;
122 bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height));
123 REPORTER_ASSERT(reporter, setConf);
124 if (setConf) {
125 bm.allocPixels();
126 }
127 REPORTER_ASSERT(reporter, SkToBool(width & height) != bm.empty());
128 }
129 }
130
131 test_bigwidth(reporter);
132 test_allocpixels(reporter);
133 test_bigalloc(reporter);
134 test_peekpixels(reporter);
135 }
136
137 /**
138 * This test checks that getColor works for both swizzles.
139 */
DEF_TEST(Bitmap_getColor_Swizzle,r)140 DEF_TEST(Bitmap_getColor_Swizzle, r) {
141 SkBitmap source;
142 source.allocN32Pixels(1,1);
143 source.eraseColor(SK_ColorRED);
144 SkColorType colorTypes[] = {
145 kRGBA_8888_SkColorType,
146 kBGRA_8888_SkColorType,
147 };
148 for (SkColorType ct : colorTypes) {
149 SkBitmap copy;
150 if (!ToolUtils::copy_to(©, ct, source)) {
151 ERRORF(r, "SkBitmap::copy failed %d", (int)ct);
152 continue;
153 }
154 REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
155 }
156 }
157
test_erasecolor_premul(skiatest::Reporter * reporter,SkColorType ct,SkColor input,SkColor expected)158 static void test_erasecolor_premul(skiatest::Reporter* reporter, SkColorType ct, SkColor input,
159 SkColor expected) {
160 SkBitmap bm;
161 bm.allocPixels(SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType));
162 bm.eraseColor(input);
163 INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0));
164 REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
165 }
166
167 /**
168 * This test checks that eraseColor premultiplies the color correctly.
169 */
DEF_TEST(Bitmap_eraseColor_Premul,r)170 DEF_TEST(Bitmap_eraseColor_Premul, r) {
171 SkColor color = 0x80FF0080;
172 test_erasecolor_premul(r, kAlpha_8_SkColorType, color, 0x80000000);
173 test_erasecolor_premul(r, kRGB_565_SkColorType, color, 0xFF840042);
174 test_erasecolor_premul(r, kARGB_4444_SkColorType, color, 0x88FF0080);
175 test_erasecolor_premul(r, kRGBA_8888_SkColorType, color, color);
176 test_erasecolor_premul(r, kBGRA_8888_SkColorType, color, color);
177 }
178
179 // Test that SkBitmap::ComputeOpaque() is correct for various colortypes.
DEF_TEST(Bitmap_compute_is_opaque,r)180 DEF_TEST(Bitmap_compute_is_opaque, r) {
181 SkColorType colorTypes[] = {
182 kAlpha_8_SkColorType,
183 kRGB_565_SkColorType,
184 kARGB_4444_SkColorType,
185 kRGBA_8888_SkColorType,
186 kRGB_888x_SkColorType,
187 kBGRA_8888_SkColorType,
188 kRGBA_1010102_SkColorType,
189 kRGB_101010x_SkColorType,
190 kGray_8_SkColorType,
191 kRGBA_F16_SkColorType,
192 kRGBA_F32_SkColorType,
193 };
194 for (auto ct : colorTypes) {
195 SkBitmap bm;
196 SkAlphaType at = SkColorTypeIsAlwaysOpaque(ct) ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
197 bm.allocPixels(SkImageInfo::Make(13, 17, ct, at));
198 bm.eraseColor(SkColorSetARGB(255, 10, 20, 30));
199 REPORTER_ASSERT(r, SkBitmap::ComputeIsOpaque(bm));
200
201 bm.eraseColor(SkColorSetARGB(128, 255, 255, 255));
202 bool isOpaque = SkBitmap::ComputeIsOpaque(bm);
203 bool shouldBeOpaque = (at == kOpaque_SkAlphaType);
204 REPORTER_ASSERT(r, isOpaque == shouldBeOpaque);
205 }
206 }
207
208 // Test that erase+getColor round trips with RGBA_F16 pixels.
DEF_TEST(Bitmap_erase_f16_erase_getColor,r)209 DEF_TEST(Bitmap_erase_f16_erase_getColor, r) {
210 SkRandom random;
211 SkPixmap pm;
212 SkBitmap bm;
213 bm.allocPixels(SkImageInfo::Make(1, 1, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
214 REPORTER_ASSERT(r, bm.peekPixels(&pm));
215 for (unsigned i = 0; i < 0x100; ++i) {
216 // Test all possible values of blue component.
217 SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i);
218 // Test all possible values of alpha component.
219 SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24));
220 for (SkColor color : {color1, color2}) {
221 pm.erase(color);
222 if (SkColorGetA(color) != 0) {
223 REPORTER_ASSERT(r, color == pm.getColor(0, 0));
224 } else {
225 REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0)));
226 }
227 }
228 }
229 }
230
231 // Make sure that the bitmap remains valid when pixelref is removed.
DEF_TEST(Bitmap_clear_pixelref_keep_info,r)232 DEF_TEST(Bitmap_clear_pixelref_keep_info, r) {
233 SkBitmap bm;
234 bm.allocPixels(SkImageInfo::MakeN32Premul(100,100));
235 bm.setPixelRef(nullptr, 0, 0);
236 SkDEBUGCODE(bm.validate();)
237 }
238
239 // At the time of writing, SkBitmap::erase() works when the color is zero for all formats,
240 // but some formats failed when the color is non-zero!
DEF_TEST(Bitmap_erase,r)241 DEF_TEST(Bitmap_erase, r) {
242 SkColorType colorTypes[] = {
243 kRGB_565_SkColorType,
244 kARGB_4444_SkColorType,
245 kRGB_888x_SkColorType,
246 kRGBA_8888_SkColorType,
247 kBGRA_8888_SkColorType,
248 kRGB_101010x_SkColorType,
249 kRGBA_1010102_SkColorType,
250 };
251
252 for (SkColorType ct : colorTypes) {
253 SkImageInfo info = SkImageInfo::Make(1,1, (SkColorType)ct, kPremul_SkAlphaType);
254
255 SkBitmap bm;
256 bm.allocPixels(info);
257
258 bm.eraseColor(0x00000000);
259 if (SkColorTypeIsAlwaysOpaque(ct)) {
260 REPORTER_ASSERT(r, bm.getColor(0,0) == 0xff000000);
261 } else {
262 REPORTER_ASSERT(r, bm.getColor(0,0) == 0x00000000);
263 }
264
265 bm.eraseColor(0xaabbccdd);
266 REPORTER_ASSERT(r, bm.getColor(0,0) != 0xff000000);
267 REPORTER_ASSERT(r, bm.getColor(0,0) != 0x00000000);
268 }
269 }
270
check_alphas(skiatest::Reporter * reporter,const SkBitmap & bm,bool (* pred)(float expected,float actual))271 static void check_alphas(skiatest::Reporter* reporter, const SkBitmap& bm,
272 bool (*pred)(float expected, float actual)) {
273 SkASSERT(bm.width() == 16);
274 SkASSERT(bm.height() == 16);
275
276 int alpha = 0;
277 for (int y = 0; y < 16; ++y) {
278 for (int x = 0; x < 16; ++x) {
279 float expected = alpha / 255.0f;
280 float actual = bm.getAlphaf(x, y);
281 if (!pred(expected, actual)) {
282 ERRORF(reporter, "got %g, want %g\n", actual, expected);
283 }
284 alpha += 1;
285 }
286 }
287 }
288
unit_compare(float expected,float actual,float tol=1.0f/(1<<12))289 static bool unit_compare(float expected, float actual, float tol = 1.0f/(1<<12)) {
290 SkASSERT(expected >= 0 && expected <= 1);
291 SkASSERT( actual >= 0 && actual <= 1);
292 if (expected == 0 || expected == 1) {
293 return actual == expected;
294 } else {
295 return SkScalarNearlyEqual(expected, actual, tol);
296 }
297 }
298
unit_discretize(float value,float scale)299 static float unit_discretize(float value, float scale) {
300 SkASSERT(value >= 0 && value <= 1);
301 if (value == 1) {
302 return 1;
303 } else {
304 return sk_float_floor(value * scale + 0.5f) / scale;
305 }
306 }
307
DEF_TEST(getalphaf,reporter)308 DEF_TEST(getalphaf, reporter) {
309 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
310 SkBitmap bm;
311 bm.allocPixels(info);
312
313 int alpha = 0;
314 for (int y = 0; y < 16; ++y) {
315 for (int x = 0; x < 16; ++x) {
316 *bm.getAddr32(x, y) = alpha++ << 24;
317 }
318 }
319
320 auto nearly = [](float expected, float actual) -> bool {
321 return unit_compare(expected, actual);
322 };
323 auto nearly4bit = [](float expected, float actual) -> bool {
324 expected = unit_discretize(expected, 15);
325 return unit_compare(expected, actual);
326 };
327 auto nearly2bit = [](float expected, float actual) -> bool {
328 expected = unit_discretize(expected, 3);
329 return unit_compare(expected, actual);
330 };
331 auto opaque = [](float expected, float actual) -> bool {
332 return actual == 1.0f;
333 };
334
335 auto nearly_half = [](float expected, float actual) -> bool {
336 return unit_compare(expected, actual, 1.0f/(1<<10));
337 };
338
339 const struct {
340 SkColorType fColorType;
341 bool (*fPred)(float, float);
342 } recs[] = {
343 { kRGB_565_SkColorType, opaque },
344 { kGray_8_SkColorType, opaque },
345 { kRGB_888x_SkColorType, opaque },
346 { kRGB_101010x_SkColorType, opaque },
347
348 { kAlpha_8_SkColorType, nearly },
349 { kRGBA_8888_SkColorType, nearly },
350 { kBGRA_8888_SkColorType, nearly },
351 { kRGBA_F16_SkColorType, nearly_half },
352 { kRGBA_F32_SkColorType, nearly },
353
354 { kRGBA_1010102_SkColorType, nearly2bit },
355
356 { kARGB_4444_SkColorType, nearly4bit },
357 };
358
359 for (const auto& rec : recs) {
360 SkBitmap tmp;
361 tmp.allocPixels(bm.info().makeColorType(rec.fColorType));
362 if (bm.readPixels(tmp.pixmap())) {
363 check_alphas(reporter, tmp, rec.fPred);
364 } else {
365 SkDebugf("can't readpixels\n");
366 }
367 }
368 }
369
370