skbitmap_operations_unittest.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gfx/skbitmap_operations.h"
  5. #include <stdint.h>
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "third_party/skia/include/core/SkBitmap.h"
  8. #include "third_party/skia/include/core/SkCanvas.h"
  9. #include "third_party/skia/include/core/SkColorPriv.h"
  10. #include "third_party/skia/include/core/SkRect.h"
  11. #include "third_party/skia/include/core/SkRegion.h"
  12. #include "third_party/skia/include/core/SkUnPreMultiply.h"
  13. namespace {
  14. // Returns true if each channel of the given two colors are "close." This is
  15. // used for comparing colors where rounding errors may cause off-by-one.
  16. inline bool ColorsClose(uint32_t a, uint32_t b) {
  17. return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) <= 2 &&
  18. abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) <= 2 &&
  19. abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) <= 2 &&
  20. abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) <= 2;
  21. }
  22. inline bool MultipliedColorsClose(uint32_t a, uint32_t b) {
  23. return ColorsClose(SkUnPreMultiply::PMColorToColor(a),
  24. SkUnPreMultiply::PMColorToColor(b));
  25. }
  26. bool BitmapsClose(const SkBitmap& a, const SkBitmap& b) {
  27. for (int y = 0; y < a.height(); y++) {
  28. for (int x = 0; x < a.width(); x++) {
  29. SkColor a_pixel = *a.getAddr32(x, y);
  30. SkColor b_pixel = *b.getAddr32(x, y);
  31. if (!ColorsClose(a_pixel, b_pixel))
  32. return false;
  33. }
  34. }
  35. return true;
  36. }
  37. void FillDataToBitmap(int w, int h, SkBitmap* bmp) {
  38. bmp->allocN32Pixels(w, h);
  39. unsigned char* src_data =
  40. reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0));
  41. for (int i = 0; i < w * h; i++) {
  42. const int alpha = i % 256;
  43. src_data[i * 4 + 0] = static_cast<unsigned char>(alpha);
  44. src_data[i * 4 + 1] = static_cast<unsigned char>((i + 16) % (alpha + 1));
  45. src_data[i * 4 + 2] = static_cast<unsigned char>((i + 32) % (alpha + 1));
  46. src_data[i * 4 + 3] = static_cast<unsigned char>((i + 64) % (alpha + 1));
  47. }
  48. }
  49. // The reference (i.e., old) implementation of |CreateHSLShiftedBitmap()|.
  50. SkBitmap ReferenceCreateHSLShiftedBitmap(
  51. const SkBitmap& bitmap,
  52. color_utils::HSL hsl_shift) {
  53. SkBitmap shifted;
  54. shifted.allocN32Pixels(bitmap.width(), bitmap.height());
  55. shifted.eraseARGB(0, 0, 0, 0);
  56. // Loop through the pixels of the original bitmap.
  57. for (int y = 0; y < bitmap.height(); ++y) {
  58. SkPMColor* pixels = bitmap.getAddr32(0, y);
  59. SkPMColor* tinted_pixels = shifted.getAddr32(0, y);
  60. for (int x = 0; x < bitmap.width(); ++x) {
  61. tinted_pixels[x] = SkPreMultiplyColor(color_utils::HSLShift(
  62. SkUnPreMultiply::PMColorToColor(pixels[x]), hsl_shift));
  63. }
  64. }
  65. return shifted;
  66. }
  67. } // namespace
  68. // Invert bitmap and verify the each pixel is inverted and the alpha value is
  69. // not changed.
  70. TEST(SkBitmapOperationsTest, CreateInvertedBitmap) {
  71. int src_w = 16, src_h = 16;
  72. SkBitmap src;
  73. src.allocN32Pixels(src_w, src_h);
  74. for (int y = 0; y < src_h; y++) {
  75. for (int x = 0; x < src_w; x++) {
  76. int i = y * src_w + x;
  77. *src.getAddr32(x, y) =
  78. SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0);
  79. }
  80. }
  81. SkBitmap inverted = SkBitmapOperations::CreateInvertedBitmap(src);
  82. for (int y = 0; y < src_h; y++) {
  83. for (int x = 0; x < src_w; x++) {
  84. int i = y * src_w + x;
  85. EXPECT_EQ(static_cast<unsigned int>((255 - i) % 255),
  86. SkColorGetA(*inverted.getAddr32(x, y)));
  87. EXPECT_EQ(static_cast<unsigned int>(255 - (i % 255)),
  88. SkColorGetR(*inverted.getAddr32(x, y)));
  89. EXPECT_EQ(static_cast<unsigned int>(255 - (i * 4 % 255)),
  90. SkColorGetG(*inverted.getAddr32(x, y)));
  91. EXPECT_EQ(static_cast<unsigned int>(255),
  92. SkColorGetB(*inverted.getAddr32(x, y)));
  93. }
  94. }
  95. }
  96. // Blend two bitmaps together at 50% alpha and verify that the result
  97. // is the middle-blend of the two.
  98. TEST(SkBitmapOperationsTest, CreateBlendedBitmap) {
  99. int src_w = 16, src_h = 16;
  100. SkBitmap src_a;
  101. src_a.allocN32Pixels(src_w, src_h);
  102. SkBitmap src_b;
  103. src_b.allocN32Pixels(src_w, src_h);
  104. for (int y = 0, i = 0; y < src_h; y++) {
  105. for (int x = 0; x < src_w; x++) {
  106. *src_a.getAddr32(x, y) = SkColorSetARGB(255, 0, i * 2 % 255, i % 255);
  107. *src_b.getAddr32(x, y) =
  108. SkColorSetARGB((255 - i) % 255, i % 255, i * 4 % 255, 0);
  109. i++;
  110. }
  111. }
  112. // Shift to red.
  113. SkBitmap blended = SkBitmapOperations::CreateBlendedBitmap(
  114. src_a, src_b, 0.5);
  115. for (int y = 0; y < src_h; y++) {
  116. for (int x = 0; x < src_w; x++) {
  117. int i = y * src_w + x;
  118. EXPECT_EQ(static_cast<unsigned int>((255 + ((255 - i) % 255)) / 2),
  119. SkColorGetA(*blended.getAddr32(x, y)));
  120. EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2),
  121. SkColorGetR(*blended.getAddr32(x, y)));
  122. EXPECT_EQ((static_cast<unsigned int>((i * 2) % 255 + (i * 4) % 255) / 2),
  123. SkColorGetG(*blended.getAddr32(x, y)));
  124. EXPECT_EQ(static_cast<unsigned int>(i % 255 / 2),
  125. SkColorGetB(*blended.getAddr32(x, y)));
  126. }
  127. }
  128. }
  129. // Test our masking functions.
  130. TEST(SkBitmapOperationsTest, CreateMaskedBitmap) {
  131. const int src_w = 16, src_h = 16;
  132. SkBitmap src;
  133. FillDataToBitmap(src_w, src_h, &src);
  134. SkBitmap alpha;
  135. alpha.allocN32Pixels(src_w, src_h);
  136. for (int y = 0, i = 0; y < src_h; y++) {
  137. for (int x = 0; x < src_w; x++) {
  138. *alpha.getAddr32(x, y) = SkPackARGB32(i % 256, 0, 0, 0);
  139. i++;
  140. }
  141. }
  142. SkBitmap masked = SkBitmapOperations::CreateMaskedBitmap(src, alpha);
  143. for (int y = 0; y < src_h; y++) {
  144. for (int x = 0; x < src_w; x++) {
  145. int alpha_pixel = *alpha.getAddr32(x, y);
  146. int src_pixel = *src.getAddr32(x, y);
  147. int masked_pixel = *masked.getAddr32(x, y);
  148. int scale = SkAlpha255To256(SkGetPackedA32(alpha_pixel));
  149. int src_a = (src_pixel >> SK_A32_SHIFT) & 0xFF;
  150. int src_r = (src_pixel >> SK_R32_SHIFT) & 0xFF;
  151. int src_g = (src_pixel >> SK_G32_SHIFT) & 0xFF;
  152. int src_b = (src_pixel >> SK_B32_SHIFT) & 0xFF;
  153. int masked_a = (masked_pixel >> SK_A32_SHIFT) & 0xFF;
  154. int masked_r = (masked_pixel >> SK_R32_SHIFT) & 0xFF;
  155. int masked_g = (masked_pixel >> SK_G32_SHIFT) & 0xFF;
  156. int masked_b = (masked_pixel >> SK_B32_SHIFT) & 0xFF;
  157. EXPECT_EQ((src_a * scale) >> 8, masked_a);
  158. EXPECT_EQ((src_r * scale) >> 8, masked_r);
  159. EXPECT_EQ((src_g * scale) >> 8, masked_g);
  160. EXPECT_EQ((src_b * scale) >> 8, masked_b);
  161. }
  162. }
  163. }
  164. // Make sure that when shifting a bitmap without any shift parameters,
  165. // the end result is close enough to the original (rounding errors
  166. // notwithstanding).
  167. TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapToSame) {
  168. int src_w = 16, src_h = 16;
  169. SkBitmap src;
  170. src.allocN32Pixels(src_w, src_h);
  171. for (int y = 0, i = 0; y < src_h; y++) {
  172. for (int x = 0; x < src_w; x++) {
  173. *src.getAddr32(x, y) = SkPreMultiplyColor(SkColorSetARGB((i + 128) % 255,
  174. (i + 128) % 255, (i + 64) % 255, (i + 0) % 255));
  175. i++;
  176. }
  177. }
  178. color_utils::HSL hsl = { -1, -1, -1 };
  179. SkBitmap shifted = ReferenceCreateHSLShiftedBitmap(src, hsl);
  180. for (int y = 0; y < src_h; y++) {
  181. for (int x = 0; x < src_w; x++) {
  182. SkColor src_pixel = *src.getAddr32(x, y);
  183. SkColor shifted_pixel = *shifted.getAddr32(x, y);
  184. EXPECT_TRUE(MultipliedColorsClose(src_pixel, shifted_pixel)) <<
  185. "source: (a,r,g,b) = (" << SkColorGetA(src_pixel) << "," <<
  186. SkColorGetR(src_pixel) << "," <<
  187. SkColorGetG(src_pixel) << "," <<
  188. SkColorGetB(src_pixel) << "); " <<
  189. "shifted: (a,r,g,b) = (" << SkColorGetA(shifted_pixel) << "," <<
  190. SkColorGetR(shifted_pixel) << "," <<
  191. SkColorGetG(shifted_pixel) << "," <<
  192. SkColorGetB(shifted_pixel) << ")";
  193. }
  194. }
  195. }
  196. // Shift a blue bitmap to red.
  197. TEST(SkBitmapOperationsTest, CreateHSLShiftedBitmapHueOnly) {
  198. int src_w = 16, src_h = 16;
  199. SkBitmap src;
  200. src.allocN32Pixels(src_w, src_h);
  201. for (int y = 0, i = 0; y < src_h; y++) {
  202. for (int x = 0; x < src_w; x++) {
  203. *src.getAddr32(x, y) = SkColorSetARGB(255, 0, 0, i % 255);
  204. i++;
  205. }
  206. }
  207. // Shift to red.
  208. color_utils::HSL hsl = { 0, -1, -1 };
  209. SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl);
  210. for (int y = 0, i = 0; y < src_h; y++) {
  211. for (int x = 0; x < src_w; x++) {
  212. EXPECT_TRUE(ColorsClose(shifted.getColor(x, y),
  213. SkColorSetARGB(255, i % 255, 0, 0)));
  214. i++;
  215. }
  216. }
  217. }
  218. // Validate HSL shift.
  219. TEST(SkBitmapOperationsTest, ValidateHSLShift) {
  220. // Note: 255/51 = 5 (exactly) => 6 including 0!
  221. const int inc = 51;
  222. const int dim = 255 / inc + 1;
  223. SkBitmap src;
  224. src.allocN32Pixels(dim*dim, dim*dim);
  225. for (int a = 0, y = 0; a <= 255; a += inc) {
  226. for (int r = 0; r <= 255; r += inc, y++) {
  227. for (int g = 0, x = 0; g <= 255; g += inc) {
  228. for (int b = 0; b <= 255; b+= inc, x++) {
  229. *src.getAddr32(x, y) =
  230. SkPreMultiplyColor(SkColorSetARGB(a, r, g, b));
  231. }
  232. }
  233. }
  234. }
  235. // Shhhh. The spec says I should set things to -1 for "no change", but
  236. // actually -0.1 will do. Don't tell anyone I did this.
  237. for (double h = -0.1; h <= 1.0001; h += 0.1) {
  238. for (double s = -0.1; s <= 1.0001; s += 0.1) {
  239. for (double l = -0.1; l <= 1.0001; l += 0.1) {
  240. color_utils::HSL hsl = { h, s, l };
  241. SkBitmap ref_shifted = ReferenceCreateHSLShiftedBitmap(src, hsl);
  242. SkBitmap shifted = SkBitmapOperations::CreateHSLShiftedBitmap(src, hsl);
  243. EXPECT_TRUE(BitmapsClose(ref_shifted, shifted))
  244. << "h = " << h << ", s = " << s << ", l = " << l;
  245. }
  246. }
  247. }
  248. }
  249. // Test our cropping.
  250. TEST(SkBitmapOperationsTest, CreateCroppedBitmap) {
  251. int src_w = 16, src_h = 16;
  252. SkBitmap src;
  253. FillDataToBitmap(src_w, src_h, &src);
  254. SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(src, 4, 4,
  255. 8, 8);
  256. ASSERT_EQ(8, cropped.width());
  257. ASSERT_EQ(8, cropped.height());
  258. for (int y = 4; y < 12; y++) {
  259. for (int x = 4; x < 12; x++) {
  260. EXPECT_EQ(*src.getAddr32(x, y),
  261. *cropped.getAddr32(x - 4, y - 4));
  262. }
  263. }
  264. }
  265. // Test whether our cropping correctly wraps across image boundaries.
  266. TEST(SkBitmapOperationsTest, CreateCroppedBitmapWrapping) {
  267. int src_w = 16, src_h = 16;
  268. SkBitmap src;
  269. FillDataToBitmap(src_w, src_h, &src);
  270. SkBitmap cropped = SkBitmapOperations::CreateTiledBitmap(
  271. src, src_w / 2, src_h / 2, src_w, src_h);
  272. ASSERT_EQ(src_w, cropped.width());
  273. ASSERT_EQ(src_h, cropped.height());
  274. for (int y = 0; y < src_h; y++) {
  275. for (int x = 0; x < src_w; x++) {
  276. EXPECT_EQ(*src.getAddr32(x, y),
  277. *cropped.getAddr32((x + src_w / 2) % src_w,
  278. (y + src_h / 2) % src_h));
  279. }
  280. }
  281. }
  282. TEST(SkBitmapOperationsTest, DownsampleByTwo) {
  283. // Use an odd-sized bitmap to make sure the edge cases where there isn't a
  284. // 2x2 block of pixels is handled correctly.
  285. // Here's the ARGB example
  286. //
  287. // 50% transparent green opaque 50% blue white
  288. // 80008000 FF000080 FFFFFFFF
  289. //
  290. // 50% transparent red opaque 50% gray black
  291. // 80800000 80808080 FF000000
  292. //
  293. // black white 50% gray
  294. // FF000000 FFFFFFFF FF808080
  295. //
  296. // The result of this computation should be:
  297. // A0404040 FF808080
  298. // FF808080 FF808080
  299. SkBitmap input;
  300. input.allocN32Pixels(3, 3);
  301. // The color order may be different, but we don't care (the channels are
  302. // trated the same).
  303. *input.getAddr32(0, 0) = 0x80008000;
  304. *input.getAddr32(1, 0) = 0xFF000080;
  305. *input.getAddr32(2, 0) = 0xFFFFFFFF;
  306. *input.getAddr32(0, 1) = 0x80800000;
  307. *input.getAddr32(1, 1) = 0x80808080;
  308. *input.getAddr32(2, 1) = 0xFF000000;
  309. *input.getAddr32(0, 2) = 0xFF000000;
  310. *input.getAddr32(1, 2) = 0xFFFFFFFF;
  311. *input.getAddr32(2, 2) = 0xFF808080;
  312. SkBitmap result = SkBitmapOperations::DownsampleByTwo(input);
  313. EXPECT_EQ(2, result.width());
  314. EXPECT_EQ(2, result.height());
  315. // Some of the values are off-by-one due to rounding.
  316. EXPECT_EQ(0x9f404040, *result.getAddr32(0, 0));
  317. EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(1, 0));
  318. EXPECT_EQ(0xFF7f7f7f, *result.getAddr32(0, 1));
  319. EXPECT_EQ(0xFF808080, *result.getAddr32(1, 1));
  320. }
  321. // Test edge cases for DownsampleByTwo.
  322. TEST(SkBitmapOperationsTest, DownsampleByTwoSmall) {
  323. SkPMColor reference = 0xFF4080FF;
  324. // Test a 1x1 bitmap.
  325. SkBitmap one_by_one;
  326. one_by_one.allocN32Pixels(1, 1);
  327. *one_by_one.getAddr32(0, 0) = reference;
  328. SkBitmap result = SkBitmapOperations::DownsampleByTwo(one_by_one);
  329. EXPECT_EQ(1, result.width());
  330. EXPECT_EQ(1, result.height());
  331. EXPECT_EQ(reference, *result.getAddr32(0, 0));
  332. // Test an n by 1 bitmap.
  333. SkBitmap one_by_n;
  334. one_by_n.allocN32Pixels(300, 1);
  335. result = SkBitmapOperations::DownsampleByTwo(one_by_n);
  336. EXPECT_EQ(300, result.width());
  337. EXPECT_EQ(1, result.height());
  338. // Test a 1 by n bitmap.
  339. SkBitmap n_by_one;
  340. n_by_one.allocN32Pixels(1, 300);
  341. result = SkBitmapOperations::DownsampleByTwo(n_by_one);
  342. EXPECT_EQ(1, result.width());
  343. EXPECT_EQ(300, result.height());
  344. // Test an empty bitmap
  345. SkBitmap empty;
  346. result = SkBitmapOperations::DownsampleByTwo(empty);
  347. EXPECT_TRUE(result.isNull());
  348. EXPECT_EQ(0, result.width());
  349. EXPECT_EQ(0, result.height());
  350. }
  351. // Here we assume DownsampleByTwo works correctly (it's tested above) and
  352. // just make sure that the wrapper function does the right thing.
  353. TEST(SkBitmapOperationsTest, DownsampleByTwoUntilSize) {
  354. // First make sure a "too small" bitmap doesn't get modified at all.
  355. SkBitmap too_small;
  356. too_small.allocN32Pixels(10, 10);
  357. SkBitmap result = SkBitmapOperations::DownsampleByTwoUntilSize(
  358. too_small, 16, 16);
  359. EXPECT_EQ(10, result.width());
  360. EXPECT_EQ(10, result.height());
  361. // Now make sure giving it a 0x0 target returns something reasonable.
  362. result = SkBitmapOperations::DownsampleByTwoUntilSize(too_small, 0, 0);
  363. EXPECT_EQ(1, result.width());
  364. EXPECT_EQ(1, result.height());
  365. // Test multiple steps of downsampling.
  366. SkBitmap large;
  367. large.allocN32Pixels(100, 43);
  368. result = SkBitmapOperations::DownsampleByTwoUntilSize(large, 6, 6);
  369. // The result should be divided in half 100x43 -> 50x22 -> 25x11
  370. EXPECT_EQ(25, result.width());
  371. EXPECT_EQ(11, result.height());
  372. }
  373. TEST(SkBitmapOperationsTest, UnPreMultiply) {
  374. SkBitmap input;
  375. input.allocN32Pixels(2, 2);
  376. EXPECT_EQ(input.alphaType(), kPremul_SkAlphaType);
  377. // Set PMColors into the bitmap
  378. *input.getAddr32(0, 0) = SkPackARGB32NoCheck(0x80, 0x00, 0x00, 0x00);
  379. *input.getAddr32(1, 0) = SkPackARGB32NoCheck(0x80, 0x80, 0x80, 0x80);
  380. *input.getAddr32(0, 1) = SkPackARGB32NoCheck(0xFF, 0x00, 0xCC, 0x88);
  381. *input.getAddr32(1, 1) = SkPackARGB32NoCheck(0x00, 0x00, 0xCC, 0x88);
  382. SkBitmap result = SkBitmapOperations::UnPreMultiply(input);
  383. EXPECT_EQ(result.alphaType(), kUnpremul_SkAlphaType);
  384. EXPECT_EQ(2, result.width());
  385. EXPECT_EQ(2, result.height());
  386. EXPECT_NE(result.getPixels(), input.getPixels());
  387. EXPECT_EQ(0x80000000, *result.getAddr32(0, 0));
  388. EXPECT_EQ(0x80FFFFFF, *result.getAddr32(1, 0));
  389. EXPECT_EQ(0xFF00CC88, *result.getAddr32(0, 1));
  390. EXPECT_EQ(0x00000000u, *result.getAddr32(1, 1)); // "Division by zero".
  391. }
  392. TEST(SkBitmapOperationsTest, UnPreMultiplyOpaque) {
  393. SkBitmap input;
  394. input.allocN32Pixels(2, 2, true);
  395. EXPECT_EQ(input.alphaType(), kOpaque_SkAlphaType);
  396. SkBitmap result = SkBitmapOperations::UnPreMultiply(input);
  397. EXPECT_EQ(result.alphaType(), kOpaque_SkAlphaType);
  398. EXPECT_EQ(result.getPixels(), input.getPixels());
  399. }
  400. TEST(SkBitmapOperationsTest, UnPreMultiplyAlreadyUnPreMultiplied) {
  401. SkBitmap input;
  402. input.allocN32Pixels(2, 2);
  403. input.setAlphaType(kUnpremul_SkAlphaType);
  404. EXPECT_EQ(input.alphaType(), kUnpremul_SkAlphaType);
  405. SkBitmap result = SkBitmapOperations::UnPreMultiply(input);
  406. EXPECT_EQ(result.alphaType(), kUnpremul_SkAlphaType);
  407. EXPECT_EQ(result.getPixels(), input.getPixels());
  408. }
  409. TEST(SkBitmapOperationsTest, CreateTransposedBitmap) {
  410. SkBitmap input;
  411. input.allocN32Pixels(2, 3);
  412. for (int x = 0; x < input.width(); ++x) {
  413. for (int y = 0; y < input.height(); ++y) {
  414. *input.getAddr32(x, y) = x * input.width() + y;
  415. }
  416. }
  417. SkBitmap result = SkBitmapOperations::CreateTransposedBitmap(input);
  418. EXPECT_EQ(3, result.width());
  419. EXPECT_EQ(2, result.height());
  420. for (int x = 0; x < input.width(); ++x) {
  421. for (int y = 0; y < input.height(); ++y) {
  422. EXPECT_EQ(*input.getAddr32(x, y), *result.getAddr32(y, x));
  423. }
  424. }
  425. }
  426. void DrawRectWithColor(SkCanvas* canvas,
  427. int left,
  428. int top,
  429. int right,
  430. int bottom,
  431. SkColor color) {
  432. SkPaint paint;
  433. paint.setColor(color);
  434. paint.setBlendMode(SkBlendMode::kSrc);
  435. canvas->drawRect(
  436. SkRect::MakeLTRB(SkIntToScalar(left), SkIntToScalar(top),
  437. SkIntToScalar(right), SkIntToScalar(bottom)),
  438. paint);
  439. }
  440. // Check that Rotate provides the desired results
  441. TEST(SkBitmapOperationsTest, RotateImage) {
  442. const int src_w = 6, src_h = 4;
  443. SkBitmap src;
  444. // Create a simple 4 color bitmap:
  445. // RRRBBB
  446. // RRRBBB
  447. // GGGYYY
  448. // GGGYYY
  449. src.allocN32Pixels(src_w, src_h);
  450. SkCanvas canvas(src, SkSurfaceProps{});
  451. src.eraseARGB(0, 0, 0, 0);
  452. // This region is a semi-transparent red to test non-opaque pixels.
  453. DrawRectWithColor(&canvas, 0, 0, src_w / 2, src_h / 2, 0x1FFF0000);
  454. DrawRectWithColor(&canvas, src_w / 2, 0, src_w, src_h / 2, SK_ColorBLUE);
  455. DrawRectWithColor(&canvas, 0, src_h / 2, src_w / 2, src_h, SK_ColorGREEN);
  456. DrawRectWithColor(&canvas, src_w / 2, src_h / 2, src_w, src_h,
  457. SK_ColorYELLOW);
  458. SkBitmap rotate90, rotate180, rotate270;
  459. rotate90 = SkBitmapOperations::Rotate(src,
  460. SkBitmapOperations::ROTATION_90_CW);
  461. rotate180 = SkBitmapOperations::Rotate(src,
  462. SkBitmapOperations::ROTATION_180_CW);
  463. rotate270 = SkBitmapOperations::Rotate(src,
  464. SkBitmapOperations::ROTATION_270_CW);
  465. ASSERT_EQ(rotate90.width(), src.height());
  466. ASSERT_EQ(rotate90.height(), src.width());
  467. ASSERT_EQ(rotate180.width(), src.width());
  468. ASSERT_EQ(rotate180.height(), src.height());
  469. ASSERT_EQ(rotate270.width(), src.height());
  470. ASSERT_EQ(rotate270.height(), src.width());
  471. for (int x=0; x < src_w; ++x) {
  472. for (int y=0; y < src_h; ++y) {
  473. ASSERT_EQ(*src.getAddr32(x,y), *rotate90.getAddr32(src_h - (y+1),x));
  474. ASSERT_EQ(*src.getAddr32(x,y), *rotate270.getAddr32(y, src_w - (x+1)));
  475. ASSERT_EQ(*src.getAddr32(x,y),
  476. *rotate180.getAddr32(src_w - (x+1), src_h - (y+1)));
  477. }
  478. }
  479. }