| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/image.h" |
| 34 |
#include "core/os/os.h" |
| 35 |
|
| 36 |
#include "tests/test_utils.h" |
| 37 |
|
| 38 |
#include "modules/modules_enabled.gen.h" |
| 39 |
|
| 40 |
#include "thirdparty/doctest/doctest.h" |
| 41 |
|
| 42 |
namespace TestImage { |
| 43 |
|
| 44 |
TEST_CASE("[Image] Instantiation") { |
| 45 |
Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_RGBA8)); |
| 46 |
CHECK_MESSAGE( |
| 47 |
!image->is_empty(), |
| 48 |
"An image created with specified size and format should not be empty at first."); |
| 49 |
CHECK_MESSAGE( |
| 50 |
image->is_invisible(), |
| 51 |
"A newly created image should be invisible."); |
| 52 |
CHECK_MESSAGE( |
| 53 |
!image->is_compressed(), |
| 54 |
"A newly created image should not be compressed."); |
| 55 |
CHECK(!image->has_mipmaps()); |
| 56 |
|
| 57 |
PackedByteArray image_data = image->get_data(); |
| 58 |
for (int i = 0; i < image_data.size(); i++) { |
| 59 |
CHECK_MESSAGE( |
| 60 |
image_data[i] == 0, |
| 61 |
"An image created without data specified should have its data zeroed out."); |
| 62 |
} |
| 63 |
|
| 64 |
Ref<Image> image_copy = memnew(Image()); |
| 65 |
CHECK_MESSAGE( |
| 66 |
image_copy->is_empty(), |
| 67 |
"An image created without any specified size and format be empty at first."); |
| 68 |
image_copy->copy_internals_from(image); |
| 69 |
|
| 70 |
CHECK_MESSAGE( |
| 71 |
image->get_data() == image_copy->get_data(), |
| 72 |
"Duplicated images should have the same data."); |
| 73 |
|
| 74 |
image_data = image->get_data(); |
| 75 |
Ref<Image> image_from_data = memnew(Image(8, 4, false, Image::FORMAT_RGBA8, image_data)); |
| 76 |
CHECK_MESSAGE( |
| 77 |
image->get_data() == image_from_data->get_data(), |
| 78 |
"An image created from data of another image should have the same data of the original image."); |
| 79 |
} |
| 80 |
|
| 81 |
TEST_CASE("[Image] Saving and loading") { |
| 82 |
Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8)); |
| 83 |
const String save_path_png = TestUtils::get_temp_path("image.png"); |
| 84 |
const String save_path_exr = TestUtils::get_temp_path("image.exr"); |
| 85 |
|
| 86 |
|
| 87 |
Error err; |
| 88 |
err = image->save_png(save_path_png); |
| 89 |
CHECK_MESSAGE( |
| 90 |
err == OK, |
| 91 |
"The image should be saved successfully as a .png file."); |
| 92 |
|
| 93 |
|
| 94 |
#ifdef TOOLS_ENABLED |
| 95 |
|
| 96 |
err = image->save_exr(save_path_exr, false); |
| 97 |
CHECK_MESSAGE( |
| 98 |
err == OK, |
| 99 |
"The image should be saved successfully as an .exr file."); |
| 100 |
#endif |
| 101 |
|
| 102 |
|
| 103 |
Ref<Image> image_load = memnew(Image()); |
| 104 |
err = image_load->load(save_path_png); |
| 105 |
CHECK_MESSAGE( |
| 106 |
err == OK, |
| 107 |
"The image should load successfully using load()."); |
| 108 |
CHECK_MESSAGE( |
| 109 |
image->get_data() == image_load->get_data(), |
| 110 |
"The loaded image should have the same data as the one that got saved."); |
| 111 |
|
| 112 |
#ifdef MODULE_BMP_ENABLED |
| 113 |
|
| 114 |
Ref<Image> image_bmp = memnew(Image()); |
| 115 |
Ref<FileAccess> f_bmp = FileAccess::open(TestUtils::get_data_path("images/icon.bmp"), FileAccess::READ, &err); |
| 116 |
REQUIRE(f_bmp.is_valid()); |
| 117 |
PackedByteArray data_bmp; |
| 118 |
data_bmp.resize(f_bmp->get_length() + 1); |
| 119 |
f_bmp->get_buffer(data_bmp.ptrw(), f_bmp->get_length()); |
| 120 |
CHECK_MESSAGE( |
| 121 |
image_bmp->load_bmp_from_buffer(data_bmp) == OK, |
| 122 |
"The BMP image should load successfully."); |
| 123 |
#endif |
| 124 |
|
| 125 |
#ifdef MODULE_EXR_ENABLED |
| 126 |
|
| 127 |
Ref<Image> image_exr; |
| 128 |
image_exr.instantiate(); |
| 129 |
Ref<FileAccess> f_exr = FileAccess::open(TestUtils::get_data_path("images/icon.exr"), FileAccess::READ, &err); |
| 130 |
REQUIRE(f_exr.is_valid()); |
| 131 |
PackedByteArray data_exr; |
| 132 |
data_exr.resize(f_exr->get_length() + 1); |
| 133 |
f_exr->get_buffer(data_exr.ptrw(), f_exr->get_length()); |
| 134 |
CHECK_MESSAGE( |
| 135 |
image_exr->load_exr_from_buffer(data_exr) == OK, |
| 136 |
"The EXR image should load successfully."); |
| 137 |
#endif |
| 138 |
|
| 139 |
#ifdef MODULE_JPG_ENABLED |
| 140 |
|
| 141 |
Ref<Image> image_jpg = memnew(Image()); |
| 142 |
Ref<FileAccess> f_jpg = FileAccess::open(TestUtils::get_data_path("images/icon.jpg"), FileAccess::READ, &err); |
| 143 |
REQUIRE(f_jpg.is_valid()); |
| 144 |
PackedByteArray data_jpg; |
| 145 |
data_jpg.resize(f_jpg->get_length() + 1); |
| 146 |
f_jpg->get_buffer(data_jpg.ptrw(), f_jpg->get_length()); |
| 147 |
CHECK_MESSAGE( |
| 148 |
image_jpg->load_jpg_from_buffer(data_jpg) == OK, |
| 149 |
"The JPG image should load successfully."); |
| 150 |
|
| 151 |
Ref<Image> image_grayscale_jpg = memnew(Image()); |
| 152 |
Ref<FileAccess> f_grayscale_jpg = FileAccess::open(TestUtils::get_data_path("images/grayscale.jpg"), FileAccess::READ, &err); |
| 153 |
REQUIRE(f_grayscale_jpg.is_valid()); |
| 154 |
PackedByteArray data_grayscale_jpg; |
| 155 |
data_grayscale_jpg.resize(f_grayscale_jpg->get_length() + 1); |
| 156 |
f_grayscale_jpg->get_buffer(data_grayscale_jpg.ptrw(), f_grayscale_jpg->get_length()); |
| 157 |
CHECK_MESSAGE( |
| 158 |
image_jpg->load_jpg_from_buffer(data_grayscale_jpg) == OK, |
| 159 |
"The grayscale JPG image should load successfully."); |
| 160 |
|
| 161 |
|
| 162 |
const String save_path_jpg = TestUtils::get_temp_path("image.jpg"); |
| 163 |
CHECK_MESSAGE(image->save_jpg(save_path_jpg) == OK, |
| 164 |
"The image should be saved successfully as a .jpg file."); |
| 165 |
|
| 166 |
#ifdef MODULE_SVG_ENABLED |
| 167 |
|
| 168 |
Ref<Image> image_svg = memnew(Image()); |
| 169 |
Ref<FileAccess> f_svg = FileAccess::open(TestUtils::get_data_path("images/embedded_jpg.svg"), FileAccess::READ, &err); |
| 170 |
REQUIRE(f_svg.is_valid()); |
| 171 |
PackedByteArray data_svg; |
| 172 |
data_svg.resize(f_svg->get_length() + 1); |
| 173 |
f_svg->get_buffer(data_svg.ptrw(), f_svg->get_length()); |
| 174 |
CHECK_MESSAGE( |
| 175 |
image_svg->load_svg_from_buffer(data_svg) == OK, |
| 176 |
"The SVG image should load successfully."); |
| 177 |
#endif |
| 178 |
#endif |
| 179 |
|
| 180 |
#ifdef MODULE_WEBP_ENABLED |
| 181 |
|
| 182 |
Ref<Image> image_webp = memnew(Image()); |
| 183 |
Ref<FileAccess> f_webp = FileAccess::open(TestUtils::get_data_path("images/icon.webp"), FileAccess::READ, &err); |
| 184 |
REQUIRE(f_webp.is_valid()); |
| 185 |
PackedByteArray data_webp; |
| 186 |
data_webp.resize(f_webp->get_length() + 1); |
| 187 |
f_webp->get_buffer(data_webp.ptrw(), f_webp->get_length()); |
| 188 |
CHECK_MESSAGE( |
| 189 |
image_webp->load_webp_from_buffer(data_webp) == OK, |
| 190 |
"The WebP image should load successfully."); |
| 191 |
#endif |
| 192 |
|
| 193 |
|
| 194 |
Ref<Image> image_png = memnew(Image()); |
| 195 |
Ref<FileAccess> f_png = FileAccess::open(TestUtils::get_data_path("images/icon.png"), FileAccess::READ, &err); |
| 196 |
REQUIRE(f_png.is_valid()); |
| 197 |
PackedByteArray data_png; |
| 198 |
data_png.resize(f_png->get_length() + 1); |
| 199 |
f_png->get_buffer(data_png.ptrw(), f_png->get_length()); |
| 200 |
CHECK_MESSAGE( |
| 201 |
image_png->load_png_from_buffer(data_png) == OK, |
| 202 |
"The PNG image should load successfully."); |
| 203 |
|
| 204 |
#ifdef MODULE_TGA_ENABLED |
| 205 |
|
| 206 |
Ref<Image> image_tga = memnew(Image()); |
| 207 |
Ref<FileAccess> f_tga = FileAccess::open(TestUtils::get_data_path("images/icon.tga"), FileAccess::READ, &err); |
| 208 |
REQUIRE(f_tga.is_valid()); |
| 209 |
PackedByteArray data_tga; |
| 210 |
data_tga.resize(f_tga->get_length() + 1); |
| 211 |
f_tga->get_buffer(data_tga.ptrw(), f_tga->get_length()); |
| 212 |
CHECK_MESSAGE( |
| 213 |
image_tga->load_tga_from_buffer(data_tga) == OK, |
| 214 |
"The TGA image should load successfully."); |
| 215 |
#endif |
| 216 |
} |
| 217 |
|
| 218 |
TEST_CASE("[Image] Basic getters") { |
| 219 |
Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_LA8)); |
| 220 |
CHECK(image->get_width() == 8); |
| 221 |
CHECK(image->get_height() == 4); |
| 222 |
CHECK(image->get_size() == Vector2(8, 4)); |
| 223 |
CHECK(image->get_format() == Image::FORMAT_LA8); |
| 224 |
CHECK(image->get_used_rect() == Rect2i(0, 0, 0, 0)); |
| 225 |
Ref<Image> image_get_rect = image->get_region(Rect2i(0, 0, 2, 1)); |
| 226 |
CHECK(image_get_rect->get_size() == Vector2(2, 1)); |
| 227 |
} |
| 228 |
|
| 229 |
TEST_CASE("[Image] Resizing") { |
| 230 |
Ref<Image> image = memnew(Image(8, 8, false, Image::FORMAT_RGBA8)); |
| 231 |
|
| 232 |
image->crop(4, 4); |
| 233 |
CHECK_MESSAGE( |
| 234 |
image->get_size() == Vector2(4, 4), |
| 235 |
"get_size() should return the correct size after cropping."); |
| 236 |
image->set_pixel(0, 0, Color(1, 1, 1, 1)); |
| 237 |
|
| 238 |
|
| 239 |
for (int i = 0; i < 5; i++) { |
| 240 |
Ref<Image> image_resized = memnew(Image()); |
| 241 |
image_resized->copy_internals_from(image); |
| 242 |
Image::Interpolation interpolation = static_cast<Image::Interpolation>(i); |
| 243 |
image_resized->resize(8, 8, interpolation); |
| 244 |
CHECK_MESSAGE( |
| 245 |
image_resized->get_size() == Vector2(8, 8), |
| 246 |
"get_size() should return the correct size after resizing."); |
| 247 |
CHECK_MESSAGE( |
| 248 |
image_resized->get_pixel(1, 1).a > 0, |
| 249 |
"Resizing an image should also affect its content."); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
image->shrink_x2(); |
| 254 |
CHECK_MESSAGE( |
| 255 |
image->get_size() == Vector2(2, 2), |
| 256 |
"get_size() should return the correct size after shrink_x2()."); |
| 257 |
|
| 258 |
|
| 259 |
Ref<Image> image_po_2 = memnew(Image(14, 28, false, Image::FORMAT_RGBA8)); |
| 260 |
image_po_2->resize_to_po2(); |
| 261 |
CHECK_MESSAGE( |
| 262 |
image_po_2->get_size() == Vector2(16, 32), |
| 263 |
"get_size() should return the correct size after resize_to_po2()."); |
| 264 |
} |
| 265 |
|
| 266 |
TEST_CASE("[Image] Modifying pixels of an image") { |
| 267 |
Ref<Image> image = memnew(Image(3, 3, false, Image::FORMAT_RGBA8)); |
| 268 |
image->set_pixel(0, 0, Color(1, 1, 1, 1)); |
| 269 |
CHECK_MESSAGE( |
| 270 |
!image->is_invisible(), |
| 271 |
"Image should not be invisible after drawing on it."); |
| 272 |
CHECK_MESSAGE( |
| 273 |
image->get_pixelv(Vector2(0, 0)).is_equal_approx(Color(1, 1, 1, 1)), |
| 274 |
"Image's get_pixel() should return the same color value as the one being set with set_pixel() in the same position."); |
| 275 |
CHECK_MESSAGE( |
| 276 |
image->get_used_rect() == Rect2i(0, 0, 1, 1), |
| 277 |
"Image's get_used_rect should return the expected value, larger than Rect2i(0, 0, 0, 0) if it's visible."); |
| 278 |
|
| 279 |
image->set_pixelv(Vector2(0, 0), Color(0.5, 0.5, 0.5, 0.5)); |
| 280 |
Ref<Image> image2 = memnew(Image(3, 3, false, Image::FORMAT_RGBA8)); |
| 281 |
|
| 282 |
|
| 283 |
image2->fill(Color(0.5, 0.5, 0.5, 0.5)); |
| 284 |
for (int m = 0; m < image2->get_mipmap_count(); m++) { |
| 285 |
Ref<Image> mip_image = image2->get_image_from_mipmap(m); |
| 286 |
|
| 287 |
for (int y = 0; y < mip_image->get_height(); y++) { |
| 288 |
for (int x = 0; x < mip_image->get_width(); x++) { |
| 289 |
CHECK_MESSAGE( |
| 290 |
mip_image->get_pixel(x, y).r > 0.49, |
| 291 |
"fill() should colorize all pixels of the image."); |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
{ |
| 298 |
const int img_width = 3; |
| 299 |
const int img_height = 3; |
| 300 |
Vector<Rect2i> rects; |
| 301 |
rects.push_back(Rect2i()); |
| 302 |
rects.push_back(Rect2i(-5, -5, 3, 3)); |
| 303 |
rects.push_back(Rect2i(img_width, 0, 12, 12)); |
| 304 |
rects.push_back(Rect2i(0, img_height, 12, 12)); |
| 305 |
rects.push_back(Rect2i(img_width + 1, img_height + 2, 12, 12)); |
| 306 |
rects.push_back(Rect2i(1, 1, 1, 1)); |
| 307 |
rects.push_back(Rect2i(0, 1, 2, 3)); |
| 308 |
rects.push_back(Rect2i(-5, 0, img_width + 10, 2)); |
| 309 |
rects.push_back(Rect2i(0, -5, 2, img_height + 10)); |
| 310 |
rects.push_back(Rect2i(-1, -1, img_width + 1, img_height + 1)); |
| 311 |
|
| 312 |
for (const Rect2i &rect : rects) { |
| 313 |
Ref<Image> img = memnew(Image(img_width, img_height, false, Image::FORMAT_RGBA8)); |
| 314 |
img->fill_rect(rect, Color(1, 1, 1, 1)); |
| 315 |
for (int y = 0; y < img->get_height(); y++) { |
| 316 |
for (int x = 0; x < img->get_width(); x++) { |
| 317 |
if (rect.abs().has_point(Point2(x, y))) { |
| 318 |
CHECK_MESSAGE( |
| 319 |
img->get_pixel(x, y).is_equal_approx(Color(1, 1, 1, 1)), |
| 320 |
"fill_rect() should colorize all image pixels within rect bounds."); |
| 321 |
} else { |
| 322 |
CHECK_MESSAGE( |
| 323 |
!img->get_pixel(x, y).is_equal_approx(Color(1, 1, 1, 1)), |
| 324 |
"fill_rect() shouldn't colorize any image pixel out of rect bounds."); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
image->blend_rect(image2, Rect2i(Vector2i(0, 0), image2->get_size()), Vector2i(0, 0)); |
| 333 |
CHECK_MESSAGE( |
| 334 |
image->get_pixel(0, 0).a > 0.7, |
| 335 |
"blend_rect() should blend the alpha values of the two images."); |
| 336 |
CHECK_MESSAGE( |
| 337 |
image->get_used_rect().size == image->get_size(), |
| 338 |
"get_used_rect() should return the expected value, its Rect size should be the same as get_size() if there are no transparent pixels."); |
| 339 |
|
| 340 |
Ref<Image> image3 = memnew(Image(2, 2, false, Image::FORMAT_RGBA8)); |
| 341 |
image3->set_pixel(0, 0, Color(0, 1, 0, 1)); |
| 342 |
|
| 343 |
|
| 344 |
image->blit_rect(image3, Rect2i(Vector2i(0, 0), image3->get_size()), Vector2i(0, 0)); |
| 345 |
CHECK_MESSAGE( |
| 346 |
image->get_pixel(0, 0).is_equal_approx(Color(0, 1, 0, 1)), |
| 347 |
"blit_rect() should replace old colors and not blend them."); |
| 348 |
CHECK_MESSAGE( |
| 349 |
!image->get_pixel(2, 2).is_equal_approx(Color(0, 1, 0, 1)), |
| 350 |
"blit_rect() should not affect the area of the image that is outside src_rect."); |
| 351 |
|
| 352 |
|
| 353 |
image3->flip_x(); |
| 354 |
CHECK(image3->get_pixel(1, 0).is_equal_approx(Color(0, 1, 0, 1))); |
| 355 |
CHECK_MESSAGE( |
| 356 |
image3->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 0)), |
| 357 |
"flip_x() should not leave old pixels behind."); |
| 358 |
image3->flip_y(); |
| 359 |
CHECK(image3->get_pixel(1, 1).is_equal_approx(Color(0, 1, 0, 1))); |
| 360 |
CHECK_MESSAGE( |
| 361 |
image3->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 0)), |
| 362 |
"flip_y() should not leave old pixels behind."); |
| 363 |
|
| 364 |
|
| 365 |
{ |
| 366 |
Ref<Image> gray_image = memnew(Image(3, 3, false, Image::FORMAT_RGBA8)); |
| 367 |
gray_image->fill_rect(Rect2i(0, 0, 3, 3), Color(1, 1, 1, 0)); |
| 368 |
gray_image->set_pixel(1, 1, Color(1, 1, 1, 1)); |
| 369 |
gray_image->set_pixel(1, 2, Color(0.5, 0.5, 0.5, 0.5)); |
| 370 |
gray_image->set_pixel(2, 1, Color(0.25, 0.05, 0.5, 1.0)); |
| 371 |
gray_image->set_pixel(2, 2, Color(0.5, 0.25, 0.95, 0.75)); |
| 372 |
gray_image->premultiply_alpha(); |
| 373 |
gray_image->convert(Image::FORMAT_L8); |
| 374 |
CHECK_MESSAGE(gray_image->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black."); |
| 375 |
CHECK_MESSAGE(gray_image->get_pixel(0, 1).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black."); |
| 376 |
CHECK_MESSAGE(gray_image->get_pixel(0, 2).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black."); |
| 377 |
CHECK_MESSAGE(gray_image->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black."); |
| 378 |
CHECK_MESSAGE(gray_image->get_pixel(1, 1).is_equal_approx(Color(1, 1, 1, 1)), "convert() RGBA to L8 should be white."); |
| 379 |
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)."); |
| 380 |
CHECK_MESSAGE(gray_image->get_pixel(2, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black."); |
| 381 |
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)."); |
| 382 |
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)."); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
TEST_CASE("[Image] Custom mipmaps") { |
| 387 |
Ref<Image> image = memnew(Image(100, 100, false, Image::FORMAT_RGBA8)); |
| 388 |
|
| 389 |
REQUIRE(!image->has_mipmaps()); |
| 390 |
image->generate_mipmaps(); |
| 391 |
REQUIRE(image->has_mipmaps()); |
| 392 |
|
| 393 |
const int mipmaps = image->get_mipmap_count() + 1; |
| 394 |
REQUIRE(mipmaps == 7); |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
{ |
| 400 |
PackedByteArray data = image->get_data(); |
| 401 |
uint8_t *data_ptr = data.ptrw(); |
| 402 |
|
| 403 |
for (int mip = 0; mip < mipmaps; mip++) { |
| 404 |
int64_t mip_offset = 0; |
| 405 |
int64_t mip_size = 0; |
| 406 |
image->get_mipmap_offset_and_size(mip, mip_offset, mip_size); |
| 407 |
|
| 408 |
for (int i = 0; i < mip_size; i++) { |
| 409 |
data_ptr[mip_offset + i] = mip * 5; |
| 410 |
} |
| 411 |
} |
| 412 |
image->set_data(image->get_width(), image->get_height(), image->has_mipmaps(), image->get_format(), data); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
for (int format = Image::FORMAT_L8; format <= Image::FORMAT_RGBA8; format++) { |
| 418 |
Ref<Image> image_bytes = memnew(Image()); |
| 419 |
image_bytes->copy_internals_from(image); |
| 420 |
image_bytes->convert((Image::Format)format); |
| 421 |
REQUIRE(image_bytes->has_mipmaps()); |
| 422 |
|
| 423 |
PackedByteArray data = image_bytes->get_data(); |
| 424 |
const uint8_t *data_ptr = data.ptr(); |
| 425 |
|
| 426 |
for (int mip = 0; mip < mipmaps; mip++) { |
| 427 |
int64_t mip_offset = 0; |
| 428 |
int64_t mip_size = 0; |
| 429 |
image_bytes->get_mipmap_offset_and_size(mip, mip_offset, mip_size); |
| 430 |
|
| 431 |
for (int i = 0; i < mip_size; i++) { |
| 432 |
if (data_ptr[mip_offset + i] != mip * 5) { |
| 433 |
REQUIRE_MESSAGE(false, "Byte format conversion error."); |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
for (int format = Image::FORMAT_RF; format <= Image::FORMAT_RGBAF; format++) { |
| 442 |
Ref<Image> image_rgbaf = memnew(Image()); |
| 443 |
image_rgbaf->copy_internals_from(image); |
| 444 |
image_rgbaf->convert((Image::Format)format); |
| 445 |
REQUIRE(image_rgbaf->has_mipmaps()); |
| 446 |
|
| 447 |
PackedByteArray data = image_rgbaf->get_data(); |
| 448 |
const uint8_t *data_ptr = data.ptr(); |
| 449 |
|
| 450 |
for (int mip = 0; mip < mipmaps; mip++) { |
| 451 |
int64_t mip_offset = 0; |
| 452 |
int64_t mip_size = 0; |
| 453 |
image_rgbaf->get_mipmap_offset_and_size(mip, mip_offset, mip_size); |
| 454 |
|
| 455 |
for (int i = 0; i < mip_size; i += 4) { |
| 456 |
float value = *(float *)(data_ptr + mip_offset + i); |
| 457 |
if (!Math::is_equal_approx(value * 255.0f, mip * 5)) { |
| 458 |
REQUIRE_MESSAGE(false, "Floating point conversion error."); |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
TEST_CASE("[Image] Convert image") { |
| 466 |
for (int format = Image::FORMAT_RF; format < Image::FORMAT_RGBE9995; format++) { |
| 467 |
for (int new_format = Image::FORMAT_RF; new_format < Image::FORMAT_RGBE9995; new_format++) { |
| 468 |
Ref<Image> image = memnew(Image(4, 4, false, (Image::Format)format)); |
| 469 |
image->convert((Image::Format)new_format); |
| 470 |
String format_string = Image::format_names[(Image::Format)format]; |
| 471 |
String new_format_string = Image::format_names[(Image::Format)new_format]; |
| 472 |
format_string = "Error converting from " + format_string + " to " + new_format_string + "."; |
| 473 |
CHECK_MESSAGE(image->get_format() == new_format, format_string); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8)); |
| 478 |
PackedByteArray image_data = image->get_data(); |
| 479 |
ERR_PRINT_OFF; |
| 480 |
image->convert((Image::Format)-1); |
| 481 |
ERR_PRINT_ON; |
| 482 |
CHECK_MESSAGE(image->get_data() == image_data, "Image conversion to invalid type (-1) should not alter image."); |
| 483 |
Ref<Image> image2 = memnew(Image(4, 4, false, Image::FORMAT_RGBA8)); |
| 484 |
image_data = image2->get_data(); |
| 485 |
ERR_PRINT_OFF; |
| 486 |
image2->convert((Image::Format)(Image::FORMAT_MAX + 1)); |
| 487 |
ERR_PRINT_ON; |
| 488 |
CHECK_MESSAGE(image2->get_data() == image_data, "Image conversion to invalid type (Image::FORMAT_MAX + 1) should not alter image."); |
| 489 |
} |
| 490 |
|
| 491 |
} |