test_image.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /**************************************************************************/
  2. /* test_image.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. #ifndef TEST_IMAGE_H
  31. #define TEST_IMAGE_H
  32. #include "core/io/image.h"
  33. #include "core/os/os.h"
  34. #include "tests/test_utils.h"
  35. #include "thirdparty/doctest/doctest.h"
  36. #include "modules/modules_enabled.gen.h"
  37. namespace TestImage {
  38. TEST_CASE("[Image] Instantiation") {
  39. Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_RGBA8));
  40. CHECK_MESSAGE(
  41. !image->is_empty(),
  42. "An image created with specified size and format should not be empty at first.");
  43. CHECK_MESSAGE(
  44. image->is_invisible(),
  45. "A newly created image should be invisible.");
  46. CHECK_MESSAGE(
  47. !image->is_compressed(),
  48. "A newly created image should not be compressed.");
  49. CHECK(!image->has_mipmaps());
  50. PackedByteArray image_data = image->get_data();
  51. for (int i = 0; i < image_data.size(); i++) {
  52. CHECK_MESSAGE(
  53. image_data[i] == 0,
  54. "An image created without data specified should have its data zeroed out.");
  55. }
  56. Ref<Image> image_copy = memnew(Image());
  57. CHECK_MESSAGE(
  58. image_copy->is_empty(),
  59. "An image created without any specified size and format be empty at first.");
  60. image_copy->copy_internals_from(image);
  61. CHECK_MESSAGE(
  62. image->get_data() == image_copy->get_data(),
  63. "Duplicated images should have the same data.");
  64. image_data = image->get_data();
  65. Ref<Image> image_from_data = memnew(Image(8, 4, false, Image::FORMAT_RGBA8, image_data));
  66. CHECK_MESSAGE(
  67. image->get_data() == image_from_data->get_data(),
  68. "An image created from data of another image should have the same data of the original image.");
  69. }
  70. TEST_CASE("[Image] Saving and loading") {
  71. Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
  72. const String save_path_png = TestUtils::get_temp_path("image.png");
  73. const String save_path_exr = TestUtils::get_temp_path("image.exr");
  74. // Save PNG
  75. Error err;
  76. err = image->save_png(save_path_png);
  77. CHECK_MESSAGE(
  78. err == OK,
  79. "The image should be saved successfully as a .png file.");
  80. // Only available on editor builds.
  81. #ifdef TOOLS_ENABLED
  82. // Save EXR
  83. err = image->save_exr(save_path_exr, false);
  84. CHECK_MESSAGE(
  85. err == OK,
  86. "The image should be saved successfully as an .exr file.");
  87. #endif // TOOLS_ENABLED
  88. // Load using load()
  89. Ref<Image> image_load = memnew(Image());
  90. err = image_load->load(save_path_png);
  91. CHECK_MESSAGE(
  92. err == OK,
  93. "The image should load successfully using load().");
  94. CHECK_MESSAGE(
  95. image->get_data() == image_load->get_data(),
  96. "The loaded image should have the same data as the one that got saved.");
  97. #ifdef MODULE_BMP_ENABLED
  98. // Load BMP
  99. Ref<Image> image_bmp = memnew(Image());
  100. Ref<FileAccess> f_bmp = FileAccess::open(TestUtils::get_data_path("images/icon.bmp"), FileAccess::READ, &err);
  101. REQUIRE(!f_bmp.is_null());
  102. PackedByteArray data_bmp;
  103. data_bmp.resize(f_bmp->get_length() + 1);
  104. f_bmp->get_buffer(data_bmp.ptrw(), f_bmp->get_length());
  105. CHECK_MESSAGE(
  106. image_bmp->load_bmp_from_buffer(data_bmp) == OK,
  107. "The BMP image should load successfully.");
  108. #endif // MODULE_BMP_ENABLED
  109. #ifdef MODULE_JPG_ENABLED
  110. // Load JPG
  111. Ref<Image> image_jpg = memnew(Image());
  112. Ref<FileAccess> f_jpg = FileAccess::open(TestUtils::get_data_path("images/icon.jpg"), FileAccess::READ, &err);
  113. REQUIRE(!f_jpg.is_null());
  114. PackedByteArray data_jpg;
  115. data_jpg.resize(f_jpg->get_length() + 1);
  116. f_jpg->get_buffer(data_jpg.ptrw(), f_jpg->get_length());
  117. CHECK_MESSAGE(
  118. image_jpg->load_jpg_from_buffer(data_jpg) == OK,
  119. "The JPG image should load successfully.");
  120. #endif // MODULE_JPG_ENABLED
  121. #ifdef MODULE_WEBP_ENABLED
  122. // Load WebP
  123. Ref<Image> image_webp = memnew(Image());
  124. Ref<FileAccess> f_webp = FileAccess::open(TestUtils::get_data_path("images/icon.webp"), FileAccess::READ, &err);
  125. REQUIRE(!f_webp.is_null());
  126. PackedByteArray data_webp;
  127. data_webp.resize(f_webp->get_length() + 1);
  128. f_webp->get_buffer(data_webp.ptrw(), f_webp->get_length());
  129. CHECK_MESSAGE(
  130. image_webp->load_webp_from_buffer(data_webp) == OK,
  131. "The WebP image should load successfully.");
  132. #endif // MODULE_WEBP_ENABLED
  133. // Load PNG
  134. Ref<Image> image_png = memnew(Image());
  135. Ref<FileAccess> f_png = FileAccess::open(TestUtils::get_data_path("images/icon.png"), FileAccess::READ, &err);
  136. REQUIRE(!f_png.is_null());
  137. PackedByteArray data_png;
  138. data_png.resize(f_png->get_length() + 1);
  139. f_png->get_buffer(data_png.ptrw(), f_png->get_length());
  140. CHECK_MESSAGE(
  141. image_png->load_png_from_buffer(data_png) == OK,
  142. "The PNG image should load successfully.");
  143. #ifdef MODULE_TGA_ENABLED
  144. // Load TGA
  145. Ref<Image> image_tga = memnew(Image());
  146. Ref<FileAccess> f_tga = FileAccess::open(TestUtils::get_data_path("images/icon.tga"), FileAccess::READ, &err);
  147. REQUIRE(!f_tga.is_null());
  148. PackedByteArray data_tga;
  149. data_tga.resize(f_tga->get_length() + 1);
  150. f_tga->get_buffer(data_tga.ptrw(), f_tga->get_length());
  151. CHECK_MESSAGE(
  152. image_tga->load_tga_from_buffer(data_tga) == OK,
  153. "The TGA image should load successfully.");
  154. #endif // MODULE_TGA_ENABLED
  155. }
  156. TEST_CASE("[Image] Basic getters") {
  157. Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_LA8));
  158. CHECK(image->get_width() == 8);
  159. CHECK(image->get_height() == 4);
  160. CHECK(image->get_size() == Vector2(8, 4));
  161. CHECK(image->get_format() == Image::FORMAT_LA8);
  162. CHECK(image->get_used_rect() == Rect2i(0, 0, 0, 0));
  163. Ref<Image> image_get_rect = image->get_region(Rect2i(0, 0, 2, 1));
  164. CHECK(image_get_rect->get_size() == Vector2(2, 1));
  165. }
  166. TEST_CASE("[Image] Resizing") {
  167. Ref<Image> image = memnew(Image(8, 8, false, Image::FORMAT_RGBA8));
  168. // Crop
  169. image->crop(4, 4);
  170. CHECK_MESSAGE(
  171. image->get_size() == Vector2(4, 4),
  172. "get_size() should return the correct size after cropping.");
  173. image->set_pixel(0, 0, Color(1, 1, 1, 1));
  174. // Resize
  175. for (int i = 0; i < 5; i++) {
  176. Ref<Image> image_resized = memnew(Image());
  177. image_resized->copy_internals_from(image);
  178. Image::Interpolation interpolation = static_cast<Image::Interpolation>(i);
  179. image_resized->resize(8, 8, interpolation);
  180. CHECK_MESSAGE(
  181. image_resized->get_size() == Vector2(8, 8),
  182. "get_size() should return the correct size after resizing.");
  183. CHECK_MESSAGE(
  184. image_resized->get_pixel(1, 1).a > 0,
  185. "Resizing an image should also affect its content.");
  186. }
  187. // shrink_x2()
  188. image->shrink_x2();
  189. CHECK_MESSAGE(
  190. image->get_size() == Vector2(2, 2),
  191. "get_size() should return the correct size after shrink_x2().");
  192. // resize_to_po2()
  193. Ref<Image> image_po_2 = memnew(Image(14, 28, false, Image::FORMAT_RGBA8));
  194. image_po_2->resize_to_po2();
  195. CHECK_MESSAGE(
  196. image_po_2->get_size() == Vector2(16, 32),
  197. "get_size() should return the correct size after resize_to_po2().");
  198. }
  199. TEST_CASE("[Image] Modifying pixels of an image") {
  200. Ref<Image> image = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
  201. image->set_pixel(0, 0, Color(1, 1, 1, 1));
  202. CHECK_MESSAGE(
  203. !image->is_invisible(),
  204. "Image should not be invisible after drawing on it.");
  205. CHECK_MESSAGE(
  206. image->get_pixelv(Vector2(0, 0)).is_equal_approx(Color(1, 1, 1, 1)),
  207. "Image's get_pixel() should return the same color value as the one being set with set_pixel() in the same position.");
  208. CHECK_MESSAGE(
  209. image->get_used_rect() == Rect2i(0, 0, 1, 1),
  210. "Image's get_used_rect should return the expected value, larger than Rect2i(0, 0, 0, 0) if it's visible.");
  211. image->set_pixelv(Vector2(0, 0), Color(0.5, 0.5, 0.5, 0.5));
  212. Ref<Image> image2 = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
  213. // Fill image with color
  214. image2->fill(Color(0.5, 0.5, 0.5, 0.5));
  215. for (int y = 0; y < image2->get_height(); y++) {
  216. for (int x = 0; x < image2->get_width(); x++) {
  217. CHECK_MESSAGE(
  218. image2->get_pixel(x, y).r > 0.49,
  219. "fill() should colorize all pixels of the image.");
  220. }
  221. }
  222. // Fill rect with color
  223. {
  224. const int img_width = 3;
  225. const int img_height = 3;
  226. Vector<Rect2i> rects;
  227. rects.push_back(Rect2i());
  228. rects.push_back(Rect2i(-5, -5, 3, 3));
  229. rects.push_back(Rect2i(img_width, 0, 12, 12));
  230. rects.push_back(Rect2i(0, img_height, 12, 12));
  231. rects.push_back(Rect2i(img_width + 1, img_height + 2, 12, 12));
  232. rects.push_back(Rect2i(1, 1, 1, 1));
  233. rects.push_back(Rect2i(0, 1, 2, 3));
  234. rects.push_back(Rect2i(-5, 0, img_width + 10, 2));
  235. rects.push_back(Rect2i(0, -5, 2, img_height + 10));
  236. rects.push_back(Rect2i(-1, -1, img_width + 1, img_height + 1));
  237. for (const Rect2i &rect : rects) {
  238. Ref<Image> img = memnew(Image(img_width, img_height, false, Image::FORMAT_RGBA8));
  239. img->fill_rect(rect, Color(1, 1, 1, 1));
  240. for (int y = 0; y < img->get_height(); y++) {
  241. for (int x = 0; x < img->get_width(); x++) {
  242. if (rect.abs().has_point(Point2(x, y))) {
  243. CHECK_MESSAGE(
  244. img->get_pixel(x, y).is_equal_approx(Color(1, 1, 1, 1)),
  245. "fill_rect() should colorize all image pixels within rect bounds.");
  246. } else {
  247. CHECK_MESSAGE(
  248. !img->get_pixel(x, y).is_equal_approx(Color(1, 1, 1, 1)),
  249. "fill_rect() shouldn't colorize any image pixel out of rect bounds.");
  250. }
  251. }
  252. }
  253. }
  254. }
  255. // Blend two images together
  256. image->blend_rect(image2, Rect2i(Vector2i(0, 0), image2->get_size()), Vector2i(0, 0));
  257. CHECK_MESSAGE(
  258. image->get_pixel(0, 0).a > 0.7,
  259. "blend_rect() should blend the alpha values of the two images.");
  260. CHECK_MESSAGE(
  261. image->get_used_rect().size == image->get_size(),
  262. "get_used_rect() should return the expected value, its Rect size should be the same as get_size() if there are no transparent pixels.");
  263. Ref<Image> image3 = memnew(Image(2, 2, false, Image::FORMAT_RGBA8));
  264. image3->set_pixel(0, 0, Color(0, 1, 0, 1));
  265. //blit_rect() two images together
  266. image->blit_rect(image3, Rect2i(Vector2i(0, 0), image3->get_size()), Vector2i(0, 0));
  267. CHECK_MESSAGE(
  268. image->get_pixel(0, 0).is_equal_approx(Color(0, 1, 0, 1)),
  269. "blit_rect() should replace old colors and not blend them.");
  270. CHECK_MESSAGE(
  271. !image->get_pixel(2, 2).is_equal_approx(Color(0, 1, 0, 1)),
  272. "blit_rect() should not affect the area of the image that is outside src_rect.");
  273. // Flip image
  274. image3->flip_x();
  275. CHECK(image3->get_pixel(1, 0).is_equal_approx(Color(0, 1, 0, 1)));
  276. CHECK_MESSAGE(
  277. image3->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 0)),
  278. "flip_x() should not leave old pixels behind.");
  279. image3->flip_y();
  280. CHECK(image3->get_pixel(1, 1).is_equal_approx(Color(0, 1, 0, 1)));
  281. CHECK_MESSAGE(
  282. image3->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 0)),
  283. "flip_y() should not leave old pixels behind.");
  284. // Pre-multiply Alpha then Convert from RGBA to L8, checking alpha
  285. {
  286. Ref<Image> gray_image = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
  287. gray_image->fill_rect(Rect2i(0, 0, 3, 3), Color(1, 1, 1, 0));
  288. gray_image->set_pixel(1, 1, Color(1, 1, 1, 1));
  289. gray_image->set_pixel(1, 2, Color(0.5, 0.5, 0.5, 0.5));
  290. gray_image->set_pixel(2, 1, Color(0.25, 0.05, 0.5, 1.0));
  291. gray_image->set_pixel(2, 2, Color(0.5, 0.25, 0.95, 0.75));
  292. gray_image->premultiply_alpha();
  293. gray_image->convert(Image::FORMAT_L8);
  294. CHECK_MESSAGE(gray_image->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
  295. CHECK_MESSAGE(gray_image->get_pixel(0, 1).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
  296. CHECK_MESSAGE(gray_image->get_pixel(0, 2).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
  297. CHECK_MESSAGE(gray_image->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
  298. CHECK_MESSAGE(gray_image->get_pixel(1, 1).is_equal_approx(Color(1, 1, 1, 1)), "convert() RGBA to L8 should be white.");
  299. CHECK_MESSAGE(gray_image->get_pixel(1, 2).is_equal_approx(Color(0.250980407, 0.250980407, 0.250980407, 1)), "convert() RGBA to L8 should be around 0.250980407 (64).");
  300. CHECK_MESSAGE(gray_image->get_pixel(2, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
  301. CHECK_MESSAGE(gray_image->get_pixel(2, 1).is_equal_approx(Color(0.121568628, 0.121568628, 0.121568628, 1)), "convert() RGBA to L8 should be around 0.121568628 (31).");
  302. CHECK_MESSAGE(gray_image->get_pixel(2, 2).is_equal_approx(Color(0.266666681, 0.266666681, 0.266666681, 1)), "convert() RGBA to L8 should be around 0.266666681 (68).");
  303. }
  304. }
  305. TEST_CASE("[Image] Custom mipmaps") {
  306. Ref<Image> image = memnew(Image(100, 100, false, Image::FORMAT_RGBA8));
  307. REQUIRE(!image->has_mipmaps());
  308. image->generate_mipmaps();
  309. REQUIRE(image->has_mipmaps());
  310. const int mipmaps = image->get_mipmap_count() + 1;
  311. REQUIRE(mipmaps == 7);
  312. // Initialize reference mipmap data.
  313. // Each byte is given value "mipmap_index * 5".
  314. {
  315. PackedByteArray data = image->get_data();
  316. uint8_t *data_ptr = data.ptrw();
  317. for (int mip = 0; mip < mipmaps; mip++) {
  318. int64_t mip_offset = 0;
  319. int64_t mip_size = 0;
  320. image->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
  321. for (int i = 0; i < mip_size; i++) {
  322. data_ptr[mip_offset + i] = mip * 5;
  323. }
  324. }
  325. image->set_data(image->get_width(), image->get_height(), image->has_mipmaps(), image->get_format(), data);
  326. }
  327. // Byte format conversion.
  328. for (int format = Image::FORMAT_L8; format <= Image::FORMAT_RGBA8; format++) {
  329. Ref<Image> image_bytes = memnew(Image());
  330. image_bytes->copy_internals_from(image);
  331. image_bytes->convert((Image::Format)format);
  332. REQUIRE(image_bytes->has_mipmaps());
  333. PackedByteArray data = image_bytes->get_data();
  334. const uint8_t *data_ptr = data.ptr();
  335. for (int mip = 0; mip < mipmaps; mip++) {
  336. int64_t mip_offset = 0;
  337. int64_t mip_size = 0;
  338. image_bytes->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
  339. for (int i = 0; i < mip_size; i++) {
  340. if (data_ptr[mip_offset + i] != mip * 5) {
  341. REQUIRE_MESSAGE(false, "Byte format conversion error.");
  342. }
  343. }
  344. }
  345. }
  346. // Floating point format conversion.
  347. for (int format = Image::FORMAT_RF; format <= Image::FORMAT_RGBAF; format++) {
  348. Ref<Image> image_rgbaf = memnew(Image());
  349. image_rgbaf->copy_internals_from(image);
  350. image_rgbaf->convert((Image::Format)format);
  351. REQUIRE(image_rgbaf->has_mipmaps());
  352. PackedByteArray data = image_rgbaf->get_data();
  353. const uint8_t *data_ptr = data.ptr();
  354. for (int mip = 0; mip < mipmaps; mip++) {
  355. int64_t mip_offset = 0;
  356. int64_t mip_size = 0;
  357. image_rgbaf->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
  358. for (int i = 0; i < mip_size; i += 4) {
  359. float value = *(float *)(data_ptr + mip_offset + i);
  360. if (!Math::is_equal_approx(value * 255.0f, mip * 5)) {
  361. REQUIRE_MESSAGE(false, "Floating point conversion error.");
  362. }
  363. }
  364. }
  365. }
  366. }
  367. TEST_CASE("[Image] Convert image") {
  368. for (int format = Image::FORMAT_RF; format < Image::FORMAT_RGBE9995; format++) {
  369. for (int new_format = Image::FORMAT_RF; new_format < Image::FORMAT_RGBE9995; new_format++) {
  370. Ref<Image> image = memnew(Image(4, 4, false, (Image::Format)format));
  371. image->convert((Image::Format)new_format);
  372. String format_string = Image::format_names[(Image::Format)format];
  373. String new_format_string = Image::format_names[(Image::Format)new_format];
  374. format_string = "Error converting from " + format_string + " to " + new_format_string + ".";
  375. CHECK_MESSAGE(image->get_format() == new_format, format_string);
  376. }
  377. }
  378. Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
  379. PackedByteArray image_data = image->get_data();
  380. ERR_PRINT_OFF;
  381. image->convert((Image::Format)-1);
  382. ERR_PRINT_ON;
  383. CHECK_MESSAGE(image->get_data() == image_data, "Image conversion to invalid type (-1) should not alter image.");
  384. Ref<Image> image2 = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
  385. image_data = image2->get_data();
  386. ERR_PRINT_OFF;
  387. image2->convert((Image::Format)(Image::FORMAT_MAX + 1));
  388. ERR_PRINT_ON;
  389. CHECK_MESSAGE(image2->get_data() == image_data, "Image conversion to invalid type (Image::FORMAT_MAX + 1) should not alter image.");
  390. }
  391. } // namespace TestImage
  392. #endif // TEST_IMAGE_H