test_image.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*************************************************************************/
  2. /* test_image.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/file_access_pack.h"
  33. #include "core/io/image.h"
  34. #include "test_utils.h"
  35. #include "thirdparty/doctest/doctest.h"
  36. namespace TestImage {
  37. TEST_CASE("[Image] Instantiation") {
  38. Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_RGBA8));
  39. CHECK_MESSAGE(
  40. !image->is_empty(),
  41. "An image created with specified size and format should not be empty at first.");
  42. CHECK_MESSAGE(
  43. image->is_invisible(),
  44. "A newly created image should be invisible.");
  45. CHECK_MESSAGE(
  46. !image->is_compressed(),
  47. "A newly created image should not be compressed.");
  48. CHECK(!image->has_mipmaps());
  49. Ref<Image> image_copy = memnew(Image());
  50. CHECK_MESSAGE(
  51. image_copy->is_empty(),
  52. "An image created without any specified size and format be empty at first.");
  53. image_copy->copy_internals_from(image);
  54. CHECK_MESSAGE(
  55. image->get_data() == image_copy->get_data(),
  56. "Duplicated images should have the same data.");
  57. PackedByteArray image_data = image->get_data();
  58. Ref<Image> image_from_data = memnew(Image(8, 4, false, Image::FORMAT_RGBA8, image_data));
  59. CHECK_MESSAGE(
  60. image->get_data() == image_from_data->get_data(),
  61. "An image created from data of another image should have the same data of the original image.");
  62. }
  63. TEST_CASE("[Image] Saving and loading") {
  64. Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
  65. const String save_path_png = OS::get_singleton()->get_cache_path().plus_file("image.png");
  66. const String save_path_exr = OS::get_singleton()->get_cache_path().plus_file("image.exr");
  67. // Save PNG
  68. Error err;
  69. err = image->save_png(save_path_png);
  70. CHECK_MESSAGE(
  71. err == OK,
  72. "The image should be saved successfully as a .png file.");
  73. // Save EXR
  74. err = image->save_exr(save_path_exr, false);
  75. CHECK_MESSAGE(
  76. err == OK,
  77. "The image should be saved successfully as an .exr file.");
  78. // Load using load()
  79. Ref<Image> image_load = memnew(Image());
  80. err = image_load->load(save_path_png);
  81. CHECK_MESSAGE(
  82. err == OK,
  83. "The image should load successfully using load().");
  84. CHECK_MESSAGE(
  85. image->get_data() == image_load->get_data(),
  86. "The loaded image should have the same data as the one that got saved.");
  87. // Load BMP
  88. Ref<Image> image_bmp = memnew(Image());
  89. FileAccessRef f_bmp = FileAccess::open(TestUtils::get_data_path("images/icon.bmp"), FileAccess::READ, &err);
  90. PackedByteArray data_bmp;
  91. data_bmp.resize(f_bmp->get_len() + 1);
  92. f_bmp->get_buffer(data_bmp.ptrw(), f_bmp->get_len());
  93. CHECK_MESSAGE(
  94. image_bmp->load_bmp_from_buffer(data_bmp) == OK,
  95. "The BMP image should load successfully.");
  96. // Load JPG
  97. Ref<Image> image_jpg = memnew(Image());
  98. FileAccessRef f_jpg = FileAccess::open(TestUtils::get_data_path("images/icon.jpg"), FileAccess::READ, &err);
  99. PackedByteArray data_jpg;
  100. data_jpg.resize(f_jpg->get_len() + 1);
  101. f_jpg->get_buffer(data_jpg.ptrw(), f_jpg->get_len());
  102. CHECK_MESSAGE(
  103. image_jpg->load_jpg_from_buffer(data_jpg) == OK,
  104. "The JPG image should load successfully.");
  105. // Load WEBP
  106. Ref<Image> image_webp = memnew(Image());
  107. FileAccessRef f_webp = FileAccess::open(TestUtils::get_data_path("images/icon.webp"), FileAccess::READ, &err);
  108. PackedByteArray data_webp;
  109. data_webp.resize(f_webp->get_len() + 1);
  110. f_webp->get_buffer(data_webp.ptrw(), f_webp->get_len());
  111. CHECK_MESSAGE(
  112. image_webp->load_webp_from_buffer(data_webp) == OK,
  113. "The WEBP image should load successfully.");
  114. // Load PNG
  115. Ref<Image> image_png = memnew(Image());
  116. FileAccessRef f_png = FileAccess::open(TestUtils::get_data_path("images/icon.png"), FileAccess::READ, &err);
  117. PackedByteArray data_png;
  118. data_png.resize(f_png->get_len() + 1);
  119. f_png->get_buffer(data_png.ptrw(), f_png->get_len());
  120. CHECK_MESSAGE(
  121. image_png->load_png_from_buffer(data_png) == OK,
  122. "The PNG image should load successfully.");
  123. // Load TGA
  124. Ref<Image> image_tga = memnew(Image());
  125. FileAccessRef f_tga = FileAccess::open(TestUtils::get_data_path("images/icon.tga"), FileAccess::READ, &err);
  126. PackedByteArray data_tga;
  127. data_tga.resize(f_tga->get_len() + 1);
  128. f_tga->get_buffer(data_tga.ptrw(), f_tga->get_len());
  129. CHECK_MESSAGE(
  130. image_tga->load_tga_from_buffer(data_tga) == OK,
  131. "The TGA image should load successfully.");
  132. }
  133. TEST_CASE("[Image] Basic getters") {
  134. Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_LA8));
  135. CHECK(image->get_width() == 8);
  136. CHECK(image->get_height() == 4);
  137. CHECK(image->get_size() == Vector2(8, 4));
  138. CHECK(image->get_format() == Image::FORMAT_LA8);
  139. CHECK(image->get_used_rect() == Rect2(0, 0, 0, 0));
  140. Ref<Image> image_get_rect = image->get_rect(Rect2(0, 0, 2, 1));
  141. CHECK(image_get_rect->get_size() == Vector2(2, 1));
  142. }
  143. TEST_CASE("[Image] Resizing") {
  144. Ref<Image> image = memnew(Image(8, 8, false, Image::FORMAT_RGBA8));
  145. // Crop
  146. image->crop(4, 4);
  147. CHECK_MESSAGE(
  148. image->get_size() == Vector2(4, 4),
  149. "get_size() should return the correct size after cropping.");
  150. image->set_pixel(0, 0, Color(1, 1, 1, 1));
  151. // Resize
  152. for (int i = 0; i < 5; i++) {
  153. Ref<Image> image_resized = memnew(Image());
  154. image_resized->copy_internals_from(image);
  155. Image::Interpolation interpolation = static_cast<Image::Interpolation>(i);
  156. image_resized->resize(8, 8, interpolation);
  157. CHECK_MESSAGE(
  158. image_resized->get_size() == Vector2(8, 8),
  159. "get_size() should return the correct size after resizing.");
  160. CHECK_MESSAGE(
  161. image_resized->get_pixel(1, 1).a > 0,
  162. "Resizing an image should also affect its content.");
  163. }
  164. // shrink_x2()
  165. image->shrink_x2();
  166. CHECK_MESSAGE(
  167. image->get_size() == Vector2(2, 2),
  168. "get_size() should return the correct size after shrink_x2().");
  169. // resize_to_po2()
  170. Ref<Image> image_po_2 = memnew(Image(14, 28, false, Image::FORMAT_RGBA8));
  171. image_po_2->resize_to_po2();
  172. CHECK_MESSAGE(
  173. image_po_2->get_size() == Vector2(16, 32),
  174. "get_size() should return the correct size after resize_to_po2().");
  175. }
  176. TEST_CASE("[Image] Modifying pixels of an image") {
  177. Ref<Image> image = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
  178. image->set_pixel(0, 0, Color(1, 1, 1, 1));
  179. CHECK_MESSAGE(
  180. !image->is_invisible(),
  181. "Image should not be invisible after drawing on it.");
  182. CHECK_MESSAGE(
  183. image->get_pixelv(Vector2(0, 0)).is_equal_approx(Color(1, 1, 1, 1)),
  184. "Image's get_pixel() should return the same color value as the one being set with set_pixel() in the same position.");
  185. CHECK_MESSAGE(
  186. image->get_used_rect() == Rect2(0, 0, 1, 1),
  187. "Image's get_used_rect should return the expected value, larger than Rect2(0, 0, 0, 0) if it's visible.");
  188. image->set_pixelv(Vector2(0, 0), Color(0.5, 0.5, 0.5, 0.5));
  189. Ref<Image> image2 = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
  190. // Fill image with color
  191. image2->fill(Color(0.5, 0.5, 0.5, 0.5));
  192. for (int x = 0; x < image2->get_width(); x++) {
  193. for (int y = 0; y < image2->get_height(); y++) {
  194. CHECK_MESSAGE(
  195. image2->get_pixel(x, y).r > 0.49,
  196. "fill() should colorize all pixels of the image.");
  197. }
  198. }
  199. // Blend two images together
  200. image->blend_rect(image2, Rect2(Vector2(0, 0), image2->get_size()), Vector2(0, 0));
  201. CHECK_MESSAGE(
  202. image->get_pixel(0, 0).a > 0.7,
  203. "blend_rect() should blend the alpha values of the two images.");
  204. CHECK_MESSAGE(
  205. image->get_used_rect().size == image->get_size(),
  206. "get_used_rect() should return the expected value, its Rect size should be the same as get_size() if there are no transparent pixels.");
  207. Ref<Image> image3 = memnew(Image(2, 2, false, Image::FORMAT_RGBA8));
  208. image3->set_pixel(0, 0, Color(0, 1, 0, 1));
  209. //blit_rect() two images together
  210. image->blit_rect(image3, Rect2(Vector2(0, 0), image3->get_size()), Vector2(0, 0));
  211. CHECK_MESSAGE(
  212. image->get_pixel(0, 0).is_equal_approx(Color(0, 1, 0, 1)),
  213. "blit_rect() should replace old colors and not blend them.");
  214. CHECK_MESSAGE(
  215. !image->get_pixel(2, 2).is_equal_approx(Color(0, 1, 0, 1)),
  216. "blit_rect() should not affect the area of the image that is outside src_rect.");
  217. // Flip image
  218. image3->flip_x();
  219. CHECK(image3->get_pixel(1, 0).is_equal_approx(Color(0, 1, 0, 1)));
  220. CHECK_MESSAGE(
  221. image3->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 0)),
  222. "flip_x() should not leave old pixels behind.");
  223. image3->flip_y();
  224. CHECK(image3->get_pixel(1, 1).is_equal_approx(Color(0, 1, 0, 1)));
  225. CHECK_MESSAGE(
  226. image3->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 0)),
  227. "flip_y() should not leave old pixels behind.");
  228. }
  229. } // namespace TestImage
  230. #endif // TEST_IMAGE_H