Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
BitmapTest.cpp
Go to the documentation of this file.
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
24#include "src/base/SkRandom.h"
25#include "tests/Test.h"
26#include "tools/ToolUtils.h"
27
28#include <cstddef>
29#include <initializer_list>
30
33
34 SkPixmap pmap;
35 SkBitmap bm;
36
37 // empty should return false
38 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
40
41 // no pixels should return false
43 REPORTER_ASSERT(reporter, !bm.peekPixels(nullptr));
45
46 // real pixels should return true
47 bm.allocPixels(info);
50 REPORTER_ASSERT(reporter, pmap.info() == bm.info());
51 REPORTER_ASSERT(reporter, pmap.addr() == bm.getPixels());
53}
54
55// https://code.google.com/p/chromium/issues/detail?id=446164
57 const int width = 0x40000001;
58 const int height = 0x00000096;
60
61 SkBitmap bm;
63
66}
67
69 const int width = 10;
70 const int height = 10;
72 const size_t explicitRowBytes = info.minRowBytes() + 24;
73
74 SkBitmap bm;
75 bm.setInfo(info);
76 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
77 bm.allocPixels();
78 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
79 bm.reset();
80 bm.allocPixels(info);
81 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
82
83 bm.setInfo(info, explicitRowBytes);
84 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
85 bm.allocPixels();
86 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
87 bm.reset();
88 bm.allocPixels(info, explicitRowBytes);
89 REPORTER_ASSERT(reporter, explicitRowBytes == bm.rowBytes());
90
91 bm.reset();
92 bm.setInfo(info, 0);
93 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
94 bm.reset();
95 bm.allocPixels(info, 0);
96 REPORTER_ASSERT(reporter, info.minRowBytes() == bm.rowBytes());
97
98 bm.reset();
99 bool success = bm.setInfo(info, info.minRowBytes() - 1); // invalid for 32bit
100 REPORTER_ASSERT(reporter, !success);
102
103 for (SkColorType ct : {
122 }) {
123 SkImageInfo imageInfo = info.makeColorType(ct);
124 for (int rowBytesPadding = 1; rowBytesPadding <= 17; rowBytesPadding++) {
125 bm.reset();
126 success = bm.setInfo(imageInfo, imageInfo.minRowBytes() + rowBytesPadding);
127 if (rowBytesPadding % imageInfo.bytesPerPixel() == 0) {
128 REPORTER_ASSERT(reporter, success);
129 success = bm.tryAllocPixels();
130 REPORTER_ASSERT(reporter, success);
131 } else {
132 // Not pixel aligned.
133 REPORTER_ASSERT(reporter, !success);
135 }
136 }
137 }
138}
139
141 SkBitmap bm;
142 int width = 1 << 29; // *4 will be the high-bit of 32bit int
143
147
148 // for a 4-byte config, this width will compute a rowbytes of 0x80000000,
149 // which does not fit in a int32_t. setConfig should detect this, and fail.
150
151 // TODO: perhaps skia can relax this, and only require that rowBytes fit
152 // in a uint32_t (or larger), but for now this is the constraint.
153
154 REPORTER_ASSERT(reporter, !bm.setInfo(info.makeColorType(kN32_SkColorType)));
155}
156
158 // Zero-sized bitmaps are allowed
159 for (int width = 0; width < 2; ++width) {
160 for (int height = 0; height < 2; ++height) {
161 SkBitmap bm;
162 bool setConf = bm.setInfo(SkImageInfo::MakeN32Premul(width, height));
163 REPORTER_ASSERT(reporter, setConf);
164 if (setConf) {
165 bm.allocPixels();
166 }
168 }
169 }
170
175}
176
177DEF_TEST(Bitmap_setColorSpace, r) {
178 // Make a 1x1 bitmap holding 50% gray in default colorspace.
181 source.eraseColor(SkColorSetARGB(0xFF, 0x80, 0x80, 0x80));
182
183 // Readback should use the normal sRGB colorspace.
184 const SkImageInfo kReadbackInfo = SkImageInfo::Make(/*width=*/1,
185 /*height=*/1,
186 kN32_SkColorType,
189 // Do readback and verify that the color is gray.
190 uint8_t pixelData[4];
191 REPORTER_ASSERT(r, source.readPixels(kReadbackInfo,
192 pixelData,
193 /*dstRowBytes=*/4,
194 /*srcX=*/0,
195 /*srcY=*/0));
196 REPORTER_ASSERT(r, pixelData[0] == 0x80);
197 REPORTER_ASSERT(r, pixelData[1] == 0x80);
198 REPORTER_ASSERT(r, pixelData[2] == 0x80);
199
200 // Also check the color with getColor4f, which does not honor colorspaces.
201 uint32_t colorRGBA = source.getColor4f(0, 0).toBytes_RGBA();
202 REPORTER_ASSERT(r, colorRGBA == 0xFF808080, "RGBA=%08X", colorRGBA);
203
204 // Convert the SkBitmap's colorspace to linear.
206
207 // Readback again and verify that the color is interpreted differently.
208 REPORTER_ASSERT(r, source.readPixels(kReadbackInfo,
209 pixelData,
210 /*dstRowBytes=*/4,
211 /*srcX=*/0,
212 /*srcY=*/0));
213 REPORTER_ASSERT(r, pixelData[0] == 0xBC, "R:%02X", pixelData[0]);
214 REPORTER_ASSERT(r, pixelData[1] == 0xBC, "G:%02X", pixelData[1]);
215 REPORTER_ASSERT(r, pixelData[2] == 0xBC, "B:%02X", pixelData[2]);
216
217 // Since getColor4f does not honor colorspaces, this should still contain 50% gray.
218 colorRGBA = source.getColor4f(0, 0).toBytes_RGBA();
219 REPORTER_ASSERT(r, colorRGBA == 0xFF808080, "RGBA=%08X", colorRGBA);
220}
221
222/**
223 * This test checks that getColor works for both swizzles.
224 */
225DEF_TEST(Bitmap_getColor_Swizzle, r) {
229 SkColorType colorTypes[] = {
232 };
233 for (SkColorType ct : colorTypes) {
235 if (!ToolUtils::copy_to(&copy, ct, source)) {
236 ERRORF(r, "SkBitmap::copy failed %d", (int)ct);
237 continue;
238 }
239 REPORTER_ASSERT(r, source.getColor(0, 0) == copy.getColor(0, 0));
240 }
241}
242
244 SkColor expected) {
245 SkBitmap bm;
247 bm.eraseColor(input);
248 INFOF(reporter, "expected: %x actual: %x\n", expected, bm.getColor(0, 0));
249 REPORTER_ASSERT(reporter, bm.getColor(0, 0) == expected);
250}
251
252/**
253 * This test checks that eraseColor premultiplies the color correctly.
254 */
263
264// Test that SkBitmap::ComputeOpaque() is correct for various colortypes.
265DEF_TEST(Bitmap_compute_is_opaque, r) {
266
267 for (int i = 1; i <= kLastEnum_SkColorType; ++i) {
268 SkColorType ct = (SkColorType) i;
269 SkBitmap bm;
271 bm.allocPixels(SkImageInfo::Make(13, 17, ct, at));
272 bm.eraseColor(SkColorSetARGB(255, 10, 20, 30));
274
275 bm.eraseColor(SkColorSetARGB(128, 255, 255, 255));
276 bool isOpaque = SkBitmap::ComputeIsOpaque(bm);
277 bool shouldBeOpaque = (at == kOpaque_SkAlphaType);
278 REPORTER_ASSERT(r, isOpaque == shouldBeOpaque);
279 }
280}
281
282// Test that erase+getColor round trips with RGBA_F16 pixels.
283DEF_TEST(Bitmap_erase_f16_erase_getColor, r) {
284 SkRandom random;
285 SkPixmap pm;
286 SkBitmap bm;
288 REPORTER_ASSERT(r, bm.peekPixels(&pm));
289 for (unsigned i = 0; i < 0x100; ++i) {
290 // Test all possible values of blue component.
291 SkColor color1 = (SkColor)((random.nextU() & 0xFFFFFF00) | i);
292 // Test all possible values of alpha component.
293 SkColor color2 = (SkColor)((random.nextU() & 0x00FFFFFF) | (i << 24));
294 for (SkColor color : {color1, color2}) {
295 pm.erase(color);
296 if (SkColorGetA(color) != 0) {
297 REPORTER_ASSERT(r, color == pm.getColor(0, 0));
298 } else {
299 REPORTER_ASSERT(r, 0 == SkColorGetA(pm.getColor(0, 0)));
300 }
301 }
302 }
303}
304
305// Verify that SkBitmap::erase erases in SRGB, regardless of the SkColorSpace of the
306// SkBitmap.
307DEF_TEST(Bitmap_erase_srgb, r) {
308 SkBitmap bm;
309 // Use a color spin from SRGB.
310 bm.allocPixels(SkImageInfo::Make(1, 1, kN32_SkColorType, kPremul_SkAlphaType,
311 SkColorSpace::MakeSRGB()->makeColorSpin()));
312 // RED will be converted into the spun color space.
314 // getColor doesn't take the color space into account, so the returned color
315 // is different due to the color spin.
316 REPORTER_ASSERT(r, bm.getColor(0, 0) == SK_ColorBLUE);
317}
318
319// Make sure that the bitmap remains valid when pixelref is removed.
320DEF_TEST(Bitmap_clear_pixelref_keep_info, r) {
321 SkBitmap bm;
323 bm.setPixelRef(nullptr, 0, 0);
324 SkDEBUGCODE(bm.validate();)
325}
326
327// At the time of writing, SkBitmap::erase() works when the color is zero for all formats,
328// but some formats failed when the color is non-zero!
329DEF_TEST(Bitmap_erase, r) {
330 SkColorType colorTypes[] = {
338 };
339
340 for (SkColorType ct : colorTypes) {
342
343 SkBitmap bm;
344 bm.allocPixels(info);
345
346 bm.eraseColor(0x00000000);
348 REPORTER_ASSERT(r, bm.getColor(0,0) == 0xff000000);
349 } else {
350 REPORTER_ASSERT(r, bm.getColor(0,0) == 0x00000000);
351 }
352
353 bm.eraseColor(0xaabbccdd);
354 REPORTER_ASSERT(r, bm.getColor(0,0) != 0xff000000);
355 REPORTER_ASSERT(r, bm.getColor(0,0) != 0x00000000);
356 }
357}
358
360 bool (*pred)(float expected, float actual), SkColorType ct) {
361 SkASSERT(bm.width() == 16);
362 SkASSERT(bm.height() == 16);
363
364 int alpha = 0;
365 for (int y = 0; y < 16; ++y) {
366 for (int x = 0; x < 16; ++x) {
367 float expected = alpha / 255.0f;
368 float actual = bm.getAlphaf(x, y);
369 if (!pred(expected, actual)) {
370 ERRORF(reporter, "%s: got %g, want %g\n",
371 ToolUtils::colortype_name(ct), actual, expected);
372 }
373 alpha += 1;
374 }
375 }
376}
377
378static bool unit_compare(float expected, float actual, float tol = 1.0f/(1<<12)) {
379 SkASSERT(expected >= 0 && expected <= 1);
380 SkASSERT( actual >= 0 && actual <= 1);
381 if (expected == 0 || expected == 1) {
382 return actual == expected;
383 } else {
384 return SkScalarNearlyEqual(expected, actual, tol);
385 }
386}
387
388static float unit_discretize(float value, float scale) {
389 SkASSERT(value >= 0 && value <= 1);
390 if (value == 1) {
391 return 1;
392 } else {
393 return std::floor(value * scale + 0.5f) / scale;
394 }
395}
396
397DEF_TEST(getalphaf, reporter) {
399 SkBitmap bm;
400 bm.allocPixels(info);
401
402 int alpha = 0;
403 for (int y = 0; y < 16; ++y) {
404 for (int x = 0; x < 16; ++x) {
405 *bm.getAddr32(x, y) = alpha++ << 24;
406 }
407 }
408
409 auto nearly = [](float expected, float actual) -> bool {
410 return unit_compare(expected, actual);
411 };
412 auto nearly4bit = [](float expected, float actual) -> bool {
413 expected = unit_discretize(expected, 15);
414 return unit_compare(expected, actual);
415 };
416 auto nearly2bit = [](float expected, float actual) -> bool {
417 expected = unit_discretize(expected, 3);
418 return unit_compare(expected, actual);
419 };
420 auto opaque = [](float expected, float actual) -> bool {
421 return actual == 1.0f;
422 };
423
424 auto nearly_half = [](float expected, float actual) -> bool {
425 return unit_compare(expected, actual, 1.0f/(1<<10));
426 };
427
428 const struct {
430 bool (*fPred)(float, float);
431 } recs[] = {
432 { kRGB_565_SkColorType, opaque },
433 { kGray_8_SkColorType, opaque },
434 { kR8G8_unorm_SkColorType, opaque },
435 { kR16G16_unorm_SkColorType, opaque },
436 { kR16G16_float_SkColorType, opaque },
437 { kRGB_888x_SkColorType, opaque },
438 { kRGB_101010x_SkColorType, opaque },
439
440 { kAlpha_8_SkColorType, nearly },
441 { kA16_unorm_SkColorType, nearly },
442 { kA16_float_SkColorType, nearly_half },
443 { kRGBA_8888_SkColorType, nearly },
444 { kBGRA_8888_SkColorType, nearly },
446 { kRGBA_F16_SkColorType, nearly_half },
447 { kRGBA_F32_SkColorType, nearly },
448
449 { kRGBA_1010102_SkColorType, nearly2bit },
450
451 { kARGB_4444_SkColorType, nearly4bit },
452 };
453
454 for (const auto& rec : recs) {
455 SkBitmap tmp;
456 tmp.allocPixels(bm.info().makeColorType(rec.fColorType));
457 if (bm.readPixels(tmp.pixmap())) {
458 check_alphas(reporter, tmp, rec.fPred, rec.fColorType);
459 } else {
460 SkDebugf("can't readpixels\n");
461 }
462 }
463}
464
465/* computeByteSize() is documented to return 0 if height is zero, but does not
466 * special-case width==0, so computeByteSize() can return non-zero for that
467 * (since it is defined to return (height-1)*rb + ...
468 *
469 * Test that allocPixels() respects this, and allocates a buffer as large as
470 * computeByteSize()... even though the bitmap is logicallly empty.
471 */
472DEF_TEST(bitmap_zerowidth_crbug_1103827, reporter) {
473 const size_t big_rb = 1 << 16;
474
475 struct {
476 int width, height;
477 size_t rowbytes, expected_size;
478 } rec[] = {
479 { 2, 0, big_rb, 0 }, // zero-height means zero-size
480 { 0, 2, big_rb, big_rb }, // zero-width is computed normally
481 };
482
483 for (const auto& r : rec) {
484 auto info = SkImageInfo::Make(r.width, r.height,
486 size_t size = info.computeByteSize(r.rowbytes);
487 REPORTER_ASSERT(reporter, size == r.expected_size);
488
489 SkBitmap bm;
490 bm.setInfo(info, r.rowbytes);
492
493 // Be sure we can actually write to that much memory. If the bitmap underallocated
494 // the buffer, this should trash memory and crash (we hope).
495 bm.allocPixels();
496 sk_bzero(bm.getPixels(), size);
497 }
498}
static void test_peekpixels(skiatest::Reporter *reporter)
static bool unit_compare(float expected, float actual, float tol=1.0f/(1<< 12))
static void check_alphas(skiatest::Reporter *reporter, const SkBitmap &bm, bool(*pred)(float expected, float actual), SkColorType ct)
static void test_bigwidth(skiatest::Reporter *reporter)
static float unit_discretize(float value, float scale)
static void test_erasecolor_premul(skiatest::Reporter *reporter, SkColorType ct, SkColor input, SkColor expected)
static void test_bigalloc(skiatest::Reporter *reporter)
static void test_allocpixels(skiatest::Reporter *reporter)
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
reporter
SkColor4f color
SkColorType fColorType
SkAlphaType
Definition SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkASSERT(cond)
Definition SkAssert.h:116
SkColorType
Definition SkColorType.h:19
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition SkColorType.h:50
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition SkColorType.h:23
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition SkColorType.h:43
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition SkColorType.h:48
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition SkColorType.h:21
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:29
@ kLastEnum_SkColorType
last valid value
Definition SkColorType.h:56
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:25
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition SkColorType.h:45
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition SkColorType.h:27
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition SkColorType.h:49
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition SkColorType.h:36
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition SkColorType.h:46
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
#define SkColorGetA(color)
Definition SkColor.h:61
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define SkDEBUGCODE(...)
Definition SkDebug.h:23
SK_API bool SkColorTypeIsAlwaysOpaque(SkColorType ct)
static void sk_bzero(void *buffer, size_t size)
Definition SkMalloc.h:105
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition SkScalar.h:107
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define INFOF(REPORTER,...)
Definition Test.h:298
#define ERRORF(r,...)
Definition Test.h:293
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
SkColor getColor(int x, int y) const
Definition SkBitmap.h:874
bool empty() const
Definition SkBitmap.h:210
size_t computeByteSize() const
Definition SkBitmap.h:293
int width() const
Definition SkBitmap.h:149
const SkPixmap & pixmap() const
Definition SkBitmap.h:133
SkColor4f getColor4f(int x, int y) const
Definition SkBitmap.h:893
void setColorSpace(sk_sp< SkColorSpace > colorSpace)
Definition SkBitmap.cpp:160
size_t rowBytes() const
Definition SkBitmap.h:238
bool isNull() const
Definition SkBitmap.h:219
void reset()
Definition SkBitmap.cpp:92
void * getPixels() const
Definition SkBitmap.h:283
const SkImageInfo & info() const
Definition SkBitmap.h:139
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Definition SkBitmap.cpp:488
float getAlphaf(int x, int y) const
Definition SkBitmap.h:903
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
bool peekPixels(SkPixmap *pixmap) const
Definition SkBitmap.cpp:635
bool setInfo(const SkImageInfo &imageInfo, size_t rowBytes=0)
Definition SkBitmap.cpp:114
static bool ComputeIsOpaque(const SkBitmap &bm)
Definition SkBitmap.h:358
int height() const
Definition SkBitmap.h:158
uint32_t * getAddr32(int x, int y) const
Definition SkBitmap.h:1260
void setPixelRef(sk_sp< SkPixelRef > pixelRef, int dx, int dy)
Definition SkBitmap.cpp:182
bool tryAllocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:271
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkColorSpace > MakeSRGBLinear()
SkColor getColor(int x, int y) const
Definition SkPixmap.cpp:187
bool erase(SkColor color, const SkIRect &subset) const
Definition SkPixmap.cpp:742
size_t rowBytes() const
Definition SkPixmap.h:145
const SkImageInfo & info() const
Definition SkPixmap.h:135
const void * addr() const
Definition SkPixmap.h:153
uint32_t nextU()
Definition SkRandom.h:42
SkBitmap source
Definition examples.cpp:28
uint8_t value
double y
double x
SK_API sk_sp< SkPixelRef > MakeAllocate(const SkImageInfo &, size_t rowBytes)
bool copy_to(SkBitmap *dst, SkColorType dstColorType, const SkBitmap &src)
const char * colortype_name(SkColorType ct)
Definition ToolUtils.cpp:65
Definition copy.py:1
int32_t height
int32_t width
const Scalar scale
static SkImageInfo MakeN32Premul(int width, int height)
size_t minRowBytes() const
int bytesPerPixel() const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkImageInfo makeColorType(SkColorType newColorType) const
static SkImageInfo MakeA8(int width, int height)