BitmapTest.cpp 12 KB

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