test_bit_map.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**************************************************************************/
  2. /* test_bit_map.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/os/memory.h"
  32. #include "scene/resources/bit_map.h"
  33. #include "tests/test_macros.h"
  34. namespace TestBitmap {
  35. void reset_bit_map(BitMap &p_bm) {
  36. Size2i size = p_bm.get_size();
  37. p_bm.set_bit_rect(Rect2i(0, 0, size.width, size.height), false);
  38. }
  39. TEST_CASE("[BitMap] Create bit map") {
  40. Size2i dim{ 256, 512 };
  41. BitMap bit_map{};
  42. bit_map.create(dim);
  43. CHECK(bit_map.get_size() == Size2i(256, 512));
  44. CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "This will go through the entire bitmask inside of bitmap, thus hopefully checking if the bitmask was correctly set up.");
  45. ERR_PRINT_OFF
  46. dim = Size2i(0, 256);
  47. bit_map.create(dim);
  48. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 512), "We should still have the same dimensions as before, because the new dimension is invalid.");
  49. dim = Size2i(512, 0);
  50. bit_map.create(dim);
  51. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 512), "We should still have the same dimensions as before, because the new dimension is invalid.");
  52. dim = Size2i(46341, 46341);
  53. bit_map.create(dim);
  54. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 512), "We should still have the same dimensions as before, because the new dimension is too large (46341*46341=2147488281).");
  55. ERR_PRINT_ON
  56. }
  57. TEST_CASE("[BitMap] Create bit map from image alpha") {
  58. const Size2i dim{ 256, 256 };
  59. BitMap bit_map{};
  60. bit_map.create(dim);
  61. ERR_PRINT_OFF
  62. const Ref<Image> null_img = nullptr;
  63. bit_map.create_from_image_alpha(null_img);
  64. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "Bitmap should have its old values because bitmap creation from a nullptr should fail.");
  65. Ref<Image> empty_img;
  66. empty_img.instantiate();
  67. bit_map.create_from_image_alpha(empty_img);
  68. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "Bitmap should have its old values because bitmap creation from an empty image should fail.");
  69. Ref<Image> wrong_format_img = Image::create_empty(3, 3, false, Image::Format::FORMAT_DXT1);
  70. bit_map.create_from_image_alpha(wrong_format_img);
  71. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "Bitmap should have its old values because converting from a compressed image should fail.");
  72. ERR_PRINT_ON
  73. Ref<Image> img = Image::create_empty(3, 3, false, Image::Format::FORMAT_RGBA8);
  74. img->set_pixel(0, 0, Color(0, 0, 0, 0));
  75. img->set_pixel(0, 1, Color(0, 0, 0, 0.09f));
  76. img->set_pixel(0, 2, Color(0, 0, 0, 0.25f));
  77. img->set_pixel(1, 0, Color(0, 0, 0, 0.5f));
  78. img->set_pixel(1, 1, Color(0, 0, 0, 0.75f));
  79. img->set_pixel(1, 2, Color(0, 0, 0, 0.99f));
  80. img->set_pixel(2, 0, Color(0, 0, 0, 1.f));
  81. // Check different threshold values.
  82. bit_map.create_from_image_alpha(img);
  83. CHECK_MESSAGE(bit_map.get_true_bit_count() == 5, "There are 5 values in the image that are smaller than the default threshold of 0.1.");
  84. bit_map.create_from_image_alpha(img, 0.08f);
  85. CHECK_MESSAGE(bit_map.get_true_bit_count() == 6, "There are 6 values in the image that are smaller than the threshold of 0.08.");
  86. bit_map.create_from_image_alpha(img, 1);
  87. CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "There are no values in the image that are smaller than the threshold of 1, there is one value equal to 1, but we check for inequality only.");
  88. }
  89. TEST_CASE("[BitMap] Set bit") {
  90. Size2i dim{ 256, 256 };
  91. BitMap bit_map{};
  92. // Setting a point before a bit map is created should not crash, because there are checks to see if we are out of bounds.
  93. ERR_PRINT_OFF
  94. bit_map.set_bitv(Point2i(128, 128), true);
  95. bit_map.create(dim);
  96. CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "All values should be initialized to false.");
  97. bit_map.set_bitv(Point2i(128, 128), true);
  98. CHECK_MESSAGE(bit_map.get_true_bit_count() == 1, "One bit should be set to true.");
  99. CHECK_MESSAGE(bit_map.get_bitv(Point2i(128, 128)) == true, "The bit at (128,128) should be set to true");
  100. bit_map.set_bitv(Point2i(128, 128), false);
  101. CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "The bit should now be set to false again");
  102. CHECK_MESSAGE(bit_map.get_bitv(Point2i(128, 128)) == false, "The bit at (128,128) should now be set to false again");
  103. bit_map.create(dim);
  104. bit_map.set_bitv(Point2i(512, 512), true);
  105. CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "Nothing should change as we were trying to edit a bit outside of the correct range.");
  106. ERR_PRINT_ON
  107. }
  108. TEST_CASE("[BitMap] Get bit") {
  109. const Size2i dim{ 256, 256 };
  110. BitMap bit_map{};
  111. ERR_PRINT_OFF
  112. CHECK_MESSAGE(bit_map.get_bitv(Point2i(128, 128)) == false, "Trying to access a bit outside of the BitMap's range should always return false");
  113. bit_map.create(dim);
  114. CHECK(bit_map.get_bitv(Point2i(128, 128)) == false);
  115. bit_map.set_bit_rect(Rect2i(-1, -1, 257, 257), true);
  116. // Checking that range is [0, 256).
  117. CHECK(bit_map.get_bitv(Point2i(-1, 0)) == false);
  118. CHECK(bit_map.get_bitv(Point2i(0, 0)) == true);
  119. CHECK(bit_map.get_bitv(Point2i(128, 128)) == true);
  120. CHECK(bit_map.get_bitv(Point2i(255, 255)) == true);
  121. CHECK(bit_map.get_bitv(Point2i(256, 256)) == false);
  122. CHECK(bit_map.get_bitv(Point2i(257, 257)) == false);
  123. ERR_PRINT_ON
  124. }
  125. TEST_CASE("[BitMap] Set bit rect") {
  126. const Size2i dim{ 256, 256 };
  127. BitMap bit_map{};
  128. // Although we have not setup the BitMap yet, this should not crash because we get an empty intersection inside of the method.
  129. bit_map.set_bit_rect(Rect2i{ 0, 0, 128, 128 }, true);
  130. bit_map.create(dim);
  131. CHECK(bit_map.get_true_bit_count() == 0);
  132. bit_map.set_bit_rect(Rect2i{ 0, 0, 256, 256 }, true);
  133. CHECK(bit_map.get_true_bit_count() == 65536);
  134. reset_bit_map(bit_map);
  135. // Checking out of bounds handling.
  136. ERR_PRINT_OFF
  137. bit_map.set_bit_rect(Rect2i{ 128, 128, 256, 256 }, true);
  138. CHECK(bit_map.get_true_bit_count() == 16384);
  139. reset_bit_map(bit_map);
  140. bit_map.set_bit_rect(Rect2i{ -128, -128, 256, 256 }, true);
  141. CHECK(bit_map.get_true_bit_count() == 16384);
  142. reset_bit_map(bit_map);
  143. bit_map.set_bit_rect(Rect2i{ -128, -128, 512, 512 }, true);
  144. CHECK(bit_map.get_true_bit_count() == 65536);
  145. ERR_PRINT_ON
  146. }
  147. TEST_CASE("[BitMap] Get true bit count") {
  148. const Size2i dim{ 256, 256 };
  149. BitMap bit_map{};
  150. CHECK(bit_map.get_true_bit_count() == 0);
  151. bit_map.create(dim);
  152. CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "Uninitialized bit map should have no true bits");
  153. bit_map.set_bit_rect(Rect2i{ 0, 0, 256, 256 }, true);
  154. CHECK(bit_map.get_true_bit_count() == 65536);
  155. bit_map.set_bitv(Point2i{ 0, 0 }, false);
  156. CHECK(bit_map.get_true_bit_count() == 65535);
  157. bit_map.set_bit_rect(Rect2i{ 0, 0, 256, 256 }, false);
  158. CHECK(bit_map.get_true_bit_count() == 0);
  159. }
  160. TEST_CASE("[BitMap] Get size") {
  161. const Size2i dim{ 256, 256 };
  162. BitMap bit_map{};
  163. CHECK_MESSAGE(bit_map.get_size() == Size2i(0, 0), "Uninitialized bit map should have a size of 0x0");
  164. bit_map.create(dim);
  165. CHECK(bit_map.get_size() == Size2i(256, 256));
  166. ERR_PRINT_OFF
  167. bit_map.create(Size2i(-1, 0));
  168. ERR_PRINT_ON
  169. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "Invalid size should not be accepted by create");
  170. bit_map.create(Size2i(256, 128));
  171. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 128), "Bitmap should have updated size");
  172. }
  173. TEST_CASE("[BitMap] Resize") {
  174. const Size2i dim{ 128, 128 };
  175. BitMap bit_map{};
  176. bit_map.resize(dim);
  177. CHECK(bit_map.get_size() == dim);
  178. bit_map.create(dim);
  179. bit_map.set_bit_rect(Rect2i(0, 0, 10, 10), true);
  180. bit_map.set_bit_rect(Rect2i(118, 118, 10, 10), true);
  181. CHECK_MESSAGE(bit_map.get_true_bit_count() == 200, "There should be 100 bits in the top left corner, and 100 bits in the bottom right corner");
  182. bit_map.resize(Size2i(64, 64));
  183. CHECK_MESSAGE(bit_map.get_true_bit_count() == 50, "There should be 25 bits in the top left corner, and 25 bits in the bottom right corner");
  184. bit_map.create(dim);
  185. ERR_PRINT_OFF
  186. bit_map.resize(Size2i(-1, 128));
  187. ERR_PRINT_ON
  188. CHECK_MESSAGE(bit_map.get_size() == Size2i(128, 128), "When an invalid size is given the bit map will keep its size");
  189. bit_map.create(dim);
  190. bit_map.set_bit_rect(Rect2i(0, 0, 10, 10), true);
  191. bit_map.set_bit_rect(Rect2i(118, 118, 10, 10), true);
  192. CHECK_MESSAGE(bit_map.get_true_bit_count() == 200, "There should be 100 bits in the top left corner, and 100 bits in the bottom right corner");
  193. bit_map.resize(Size2i(256, 256));
  194. CHECK_MESSAGE(bit_map.get_true_bit_count() == 800, "There should still be 100 bits in the bottom right corner, and all new bits should be initialized to false");
  195. CHECK_MESSAGE(bit_map.get_size() == Size2i(256, 256), "The bitmap should now be 256x256");
  196. }
  197. TEST_CASE("[BitMap] Grow and shrink mask") {
  198. const Size2i dim{ 256, 256 };
  199. BitMap bit_map{};
  200. ERR_PRINT_OFF
  201. bit_map.grow_mask(100, Rect2i(0, 0, 128, 128)); // Check if method does not crash when working with an uninitialized bit map.
  202. ERR_PRINT_ON
  203. CHECK_MESSAGE(bit_map.get_size() == Size2i(0, 0), "Size should still be equal to 0x0");
  204. bit_map.create(dim);
  205. bit_map.set_bit_rect(Rect2i(96, 96, 64, 64), true);
  206. CHECK_MESSAGE(bit_map.get_true_bit_count() == 4096, "Creating a square of 64x64 should be 4096 bits");
  207. bit_map.grow_mask(0, Rect2i(0, 0, 256, 256));
  208. CHECK_MESSAGE(bit_map.get_true_bit_count() == 4096, "Growing with size of 0 should not change any bits");
  209. reset_bit_map(bit_map);
  210. bit_map.set_bit_rect(Rect2i(96, 96, 64, 64), true);
  211. CHECK_MESSAGE(bit_map.get_bitv(Point2i(95, 128)) == false, "Bits just outside of the square should not be set");
  212. CHECK_MESSAGE(bit_map.get_bitv(Point2i(160, 128)) == false, "Bits just outside of the square should not be set");
  213. CHECK_MESSAGE(bit_map.get_bitv(Point2i(128, 95)) == false, "Bits just outside of the square should not be set");
  214. CHECK_MESSAGE(bit_map.get_bitv(Point2i(128, 160)) == false, "Bits just outside of the square should not be set");
  215. bit_map.grow_mask(1, Rect2i(0, 0, 256, 256));
  216. CHECK_MESSAGE(bit_map.get_true_bit_count() == 4352, "We should have 4*64 (perimeter of square) more bits set to true");
  217. CHECK_MESSAGE(bit_map.get_bitv(Point2i(95, 128)) == true, "Bits that were just outside of the square should now be set to true");
  218. CHECK_MESSAGE(bit_map.get_bitv(Point2i(160, 128)) == true, "Bits that were just outside of the square should now be set to true");
  219. CHECK_MESSAGE(bit_map.get_bitv(Point2i(128, 95)) == true, "Bits that were just outside of the square should now be set to true");
  220. CHECK_MESSAGE(bit_map.get_bitv(Point2i(128, 160)) == true, "Bits that were just outside of the square should now be set to true");
  221. reset_bit_map(bit_map);
  222. bit_map.set_bit_rect(Rect2i(127, 127, 1, 1), true);
  223. CHECK(bit_map.get_true_bit_count() == 1);
  224. bit_map.grow_mask(32, Rect2i(0, 0, 256, 256));
  225. CHECK_MESSAGE(bit_map.get_true_bit_count() == 3209, "Creates a circle around the initial bit with a radius of 32 bits. Any bit that has a distance within this radius will be set to true");
  226. reset_bit_map(bit_map);
  227. bit_map.set_bit_rect(Rect2i(127, 127, 1, 1), true);
  228. for (int i = 0; i < 32; i++) {
  229. bit_map.grow_mask(1, Rect2i(0, 0, 256, 256));
  230. }
  231. CHECK_MESSAGE(bit_map.get_true_bit_count() == 2113, "Creates a diamond around the initial bit with diagonals that are 65 bits long.");
  232. reset_bit_map(bit_map);
  233. bit_map.set_bit_rect(Rect2i(123, 123, 10, 10), true);
  234. CHECK(bit_map.get_true_bit_count() == 100);
  235. bit_map.grow_mask(-11, Rect2i(0, 0, 256, 256));
  236. CHECK_MESSAGE(bit_map.get_true_bit_count() == 0, "Shrinking by more than the width of the square should totally remove it.");
  237. reset_bit_map(bit_map);
  238. bit_map.set_bit_rect(Rect2i(96, 96, 64, 64), true);
  239. CHECK_MESSAGE(bit_map.get_bitv(Point2i(96, 129)) == true, "Bits on the edge of the square should be true");
  240. CHECK_MESSAGE(bit_map.get_bitv(Point2i(159, 129)) == true, "Bits on the edge of the square should be true");
  241. CHECK_MESSAGE(bit_map.get_bitv(Point2i(129, 96)) == true, "Bits on the edge of the square should be true");
  242. CHECK_MESSAGE(bit_map.get_bitv(Point2i(129, 159)) == true, "Bits on the edge of the square should be true");
  243. bit_map.grow_mask(-1, Rect2i(0, 0, 256, 256));
  244. CHECK_MESSAGE(bit_map.get_true_bit_count() == 3844, "Shrinking by 1 should set 4*63=252 bits to false");
  245. CHECK_MESSAGE(bit_map.get_bitv(Point2i(96, 129)) == false, "Bits that were on the edge of the square should now be set to false");
  246. CHECK_MESSAGE(bit_map.get_bitv(Point2i(159, 129)) == false, "Bits that were on the edge of the square should now be set to false");
  247. CHECK_MESSAGE(bit_map.get_bitv(Point2i(129, 96)) == false, "Bits that were on the edge of the square should now be set to false");
  248. CHECK_MESSAGE(bit_map.get_bitv(Point2i(129, 159)) == false, "Bits that were on the edge of the square should now be set to false");
  249. reset_bit_map(bit_map);
  250. bit_map.set_bit_rect(Rect2i(125, 125, 1, 6), true);
  251. bit_map.set_bit_rect(Rect2i(130, 125, 1, 6), true);
  252. bit_map.set_bit_rect(Rect2i(125, 130, 6, 1), true);
  253. CHECK(bit_map.get_true_bit_count() == 16);
  254. CHECK_MESSAGE(bit_map.get_bitv(Point2i(125, 131)) == false, "Bits that are on the edge of the shape should be set to false");
  255. CHECK_MESSAGE(bit_map.get_bitv(Point2i(131, 131)) == false, "Bits that are on the edge of the shape should be set to false");
  256. CHECK_MESSAGE(bit_map.get_bitv(Point2i(125, 124)) == false, "Bits that are on the edge of the shape should be set to false");
  257. CHECK_MESSAGE(bit_map.get_bitv(Point2i(130, 124)) == false, "Bits that are on the edge of the shape should be set to false");
  258. bit_map.grow_mask(1, Rect2i(0, 0, 256, 256));
  259. CHECK(bit_map.get_true_bit_count() == 48);
  260. CHECK_MESSAGE(bit_map.get_bitv(Point2i(125, 131)) == true, "Bits that were on the edge of the shape should now be set to true");
  261. CHECK_MESSAGE(bit_map.get_bitv(Point2i(131, 130)) == true, "Bits that were on the edge of the shape should now be set to true");
  262. CHECK_MESSAGE(bit_map.get_bitv(Point2i(125, 124)) == true, "Bits that were on the edge of the shape should now be set to true");
  263. CHECK_MESSAGE(bit_map.get_bitv(Point2i(130, 124)) == true, "Bits that were on the edge of the shape should now be set to true");
  264. CHECK_MESSAGE(bit_map.get_bitv(Point2i(124, 124)) == false, "Bits that are on the edge of the shape should be set to false");
  265. CHECK_MESSAGE(bit_map.get_bitv(Point2i(126, 124)) == false, "Bits that are on the edge of the shape should be set to false");
  266. CHECK_MESSAGE(bit_map.get_bitv(Point2i(124, 131)) == false, "Bits that are on the edge of the shape should be set to false");
  267. CHECK_MESSAGE(bit_map.get_bitv(Point2i(131, 131)) == false, "Bits that are on the edge of the shape should be set to false");
  268. }
  269. TEST_CASE("[BitMap] Blit") {
  270. Point2i blit_pos{ 128, 128 };
  271. Point2i bit_map_size{ 256, 256 };
  272. Point2i blit_size{ 32, 32 };
  273. BitMap bit_map{};
  274. Ref<BitMap> blit_bit_map{};
  275. // Testing null reference to blit bit map.
  276. ERR_PRINT_OFF
  277. bit_map.blit(blit_pos, blit_bit_map);
  278. ERR_PRINT_ON
  279. blit_bit_map.instantiate();
  280. // Testing if uninitialized blit bit map and uninitialized bit map does not crash
  281. bit_map.blit(blit_pos, blit_bit_map);
  282. // Testing if uninitialized bit map does not crash
  283. blit_bit_map->create(blit_size);
  284. bit_map.blit(blit_pos, blit_bit_map);
  285. // Testing if uninitialized bit map does not crash
  286. blit_bit_map.unref();
  287. blit_bit_map.instantiate();
  288. CHECK_MESSAGE(blit_bit_map->get_size() == Point2i(0, 0), "Size should be cleared by unref and instance calls.");
  289. bit_map.create(bit_map_size);
  290. bit_map.blit(Point2i(128, 128), blit_bit_map);
  291. // Testing if both initialized does not crash.
  292. blit_bit_map->create(blit_size);
  293. bit_map.blit(blit_pos, blit_bit_map);
  294. bit_map.set_bit_rect(Rect2i{ 127, 127, 3, 3 }, true);
  295. CHECK(bit_map.get_true_bit_count() == 9);
  296. bit_map.blit(Point2i(112, 112), blit_bit_map);
  297. CHECK_MESSAGE(bit_map.get_true_bit_count() == 9, "No bits should have been changed, as the blit bit map only contains falses");
  298. bit_map.create(bit_map_size);
  299. blit_bit_map->create(blit_size);
  300. blit_bit_map->set_bit_rect(Rect2i(15, 15, 3, 3), true);
  301. CHECK(blit_bit_map->get_true_bit_count() == 9);
  302. CHECK(bit_map.get_true_bit_count() == 0);
  303. bit_map.blit(Point2i(112, 112), blit_bit_map);
  304. CHECK_MESSAGE(bit_map.get_true_bit_count() == 9, "All true bits should have been moved to the bit map");
  305. for (int x = 127; x < 129; ++x) {
  306. for (int y = 127; y < 129; ++y) {
  307. CHECK_MESSAGE(bit_map.get_bitv(Point2i(x, y)) == true, "All true bits should have been moved to the bit map");
  308. }
  309. }
  310. }
  311. TEST_CASE("[BitMap] Convert to image") {
  312. const Size2i dim{ 256, 256 };
  313. BitMap bit_map{};
  314. Ref<Image> img;
  315. ERR_PRINT_OFF
  316. img = bit_map.convert_to_image();
  317. ERR_PRINT_ON
  318. CHECK_MESSAGE(img.is_valid(), "We should receive a valid Image Object even if BitMap is not created yet");
  319. CHECK_MESSAGE(img->get_format() == Image::FORMAT_L8, "We should receive a valid Image Object even if BitMap is not created yet");
  320. CHECK_MESSAGE(img->get_size() == (Size2i(0, 0)), "Image should have no width or height, because BitMap has not yet been created");
  321. bit_map.create(dim);
  322. img = bit_map.convert_to_image();
  323. CHECK_MESSAGE(img->get_size() == dim, "Image should have the same dimensions as the BitMap");
  324. CHECK_MESSAGE(img->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0)), "BitMap is initialized to all 0's, so Image should be all black");
  325. reset_bit_map(bit_map);
  326. bit_map.set_bit_rect(Rect2i(0, 0, 128, 128), true);
  327. img = bit_map.convert_to_image();
  328. CHECK_MESSAGE(img->get_pixel(0, 0).is_equal_approx(Color(1, 1, 1)), "BitMap's top-left quadrant is all 1's, so Image should be white");
  329. CHECK_MESSAGE(img->get_pixel(255, 255).is_equal_approx(Color(0, 0, 0)), "All other quadrants were 0's, so these should be black");
  330. }
  331. TEST_CASE("[BitMap] Clip to polygon") {
  332. const Size2i dim{ 256, 256 };
  333. BitMap bit_map{};
  334. Vector<Vector<Vector2>> polygons;
  335. ERR_PRINT_OFF
  336. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 128, 128));
  337. ERR_PRINT_ON
  338. CHECK_MESSAGE(polygons.size() == 0, "We should have no polygons, because the BitMap was not initialized");
  339. bit_map.create(dim);
  340. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 128, 128));
  341. CHECK_MESSAGE(polygons.size() == 0, "We should have no polygons, because the BitMap was all 0's");
  342. reset_bit_map(bit_map);
  343. bit_map.set_bit_rect(Rect2i(0, 0, 64, 64), true);
  344. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 128, 128));
  345. CHECK_MESSAGE(polygons.size() == 1, "We should have exactly 1 polygon");
  346. CHECK_MESSAGE(polygons[0].size() == 4, "The polygon should have exactly 4 points");
  347. reset_bit_map(bit_map);
  348. bit_map.set_bit_rect(Rect2i(0, 0, 32, 32), true);
  349. bit_map.set_bit_rect(Rect2i(64, 64, 32, 32), true);
  350. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 128, 128));
  351. CHECK_MESSAGE(polygons.size() == 2, "We should have exactly 2 polygons");
  352. CHECK_MESSAGE(polygons[0].size() == 4, "The polygon should have exactly 4 points");
  353. CHECK_MESSAGE(polygons[1].size() == 4, "The polygon should have exactly 4 points");
  354. reset_bit_map(bit_map);
  355. bit_map.set_bit_rect(Rect2i(124, 112, 8, 32), true);
  356. bit_map.set_bit_rect(Rect2i(112, 124, 32, 8), true);
  357. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 256, 256));
  358. CHECK_MESSAGE(polygons.size() == 1, "We should have exactly 1 polygon");
  359. CHECK_MESSAGE(polygons[0].size() == 12, "The polygon should have exactly 12 points");
  360. reset_bit_map(bit_map);
  361. bit_map.set_bit_rect(Rect2i(124, 112, 8, 32), true);
  362. bit_map.set_bit_rect(Rect2i(112, 124, 32, 8), true);
  363. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 128, 128));
  364. CHECK_MESSAGE(polygons.size() == 1, "We should have exactly 1 polygon");
  365. CHECK_MESSAGE(polygons[0].size() == 6, "The polygon should have exactly 6 points");
  366. reset_bit_map(bit_map);
  367. bit_map.set_bit_rect(Rect2i(0, 0, 64, 64), true);
  368. bit_map.set_bit_rect(Rect2i(64, 64, 64, 64), true);
  369. bit_map.set_bit_rect(Rect2i(192, 128, 64, 64), true);
  370. bit_map.set_bit_rect(Rect2i(128, 192, 64, 64), true);
  371. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 256, 256));
  372. CHECK_MESSAGE(polygons.size() == 4, "We should have exactly 4 polygons");
  373. CHECK_MESSAGE(polygons[0].size() == 4, "The polygon should have exactly 4 points");
  374. CHECK_MESSAGE(polygons[1].size() == 4, "The polygon should have exactly 4 points");
  375. CHECK_MESSAGE(polygons[2].size() == 4, "The polygon should have exactly 4 points");
  376. CHECK_MESSAGE(polygons[3].size() == 4, "The polygon should have exactly 4 points");
  377. reset_bit_map(bit_map);
  378. bit_map.set_bit(0, 0, true);
  379. bit_map.set_bit(2, 0, true);
  380. bit_map.set_bit_rect(Rect2i(1, 1, 1, 2), true);
  381. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 3, 3));
  382. CHECK_MESSAGE(polygons.size() == 3, "We should have exactly 3 polygons");
  383. CHECK_MESSAGE(polygons[0].size() == 4, "The polygon should have exactly 4 points");
  384. CHECK_MESSAGE(polygons[1].size() == 4, "The polygon should have exactly 4 points");
  385. CHECK_MESSAGE(polygons[2].size() == 4, "The polygon should have exactly 4 points");
  386. reset_bit_map(bit_map);
  387. bit_map.set_bit_rect(Rect2i(0, 0, 2, 1), true);
  388. bit_map.set_bit_rect(Rect2i(0, 2, 3, 1), true);
  389. bit_map.set_bit(0, 1, true);
  390. bit_map.set_bit(2, 1, true);
  391. polygons = bit_map.clip_opaque_to_polygons(Rect2i(0, 0, 4, 4));
  392. CHECK_MESSAGE(polygons.size() == 1, "We should have exactly 1 polygon");
  393. CHECK_MESSAGE(polygons[0].size() == 6, "The polygon should have exactly 6 points");
  394. }
  395. } // namespace TestBitmap