Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_image.h
11353 views
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
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
// Save PNG
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
// Only available on editor builds.
94
#ifdef TOOLS_ENABLED
95
// Save EXR
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 // TOOLS_ENABLED
101
102
// Load using load()
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
// Load BMP
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 // MODULE_BMP_ENABLED
124
125
#ifdef MODULE_JPG_ENABLED
126
// Load JPG
127
Ref<Image> image_jpg = memnew(Image());
128
Ref<FileAccess> f_jpg = FileAccess::open(TestUtils::get_data_path("images/icon.jpg"), FileAccess::READ, &err);
129
REQUIRE(f_jpg.is_valid());
130
PackedByteArray data_jpg;
131
data_jpg.resize(f_jpg->get_length() + 1);
132
f_jpg->get_buffer(data_jpg.ptrw(), f_jpg->get_length());
133
CHECK_MESSAGE(
134
image_jpg->load_jpg_from_buffer(data_jpg) == OK,
135
"The JPG image should load successfully.");
136
137
Ref<Image> image_grayscale_jpg = memnew(Image());
138
Ref<FileAccess> f_grayscale_jpg = FileAccess::open(TestUtils::get_data_path("images/grayscale.jpg"), FileAccess::READ, &err);
139
REQUIRE(f_grayscale_jpg.is_valid());
140
PackedByteArray data_grayscale_jpg;
141
data_grayscale_jpg.resize(f_grayscale_jpg->get_length() + 1);
142
f_grayscale_jpg->get_buffer(data_grayscale_jpg.ptrw(), f_grayscale_jpg->get_length());
143
CHECK_MESSAGE(
144
image_jpg->load_jpg_from_buffer(data_grayscale_jpg) == OK,
145
"The grayscale JPG image should load successfully.");
146
147
// Save JPG
148
const String save_path_jpg = TestUtils::get_temp_path("image.jpg");
149
CHECK_MESSAGE(image->save_jpg(save_path_jpg) == OK,
150
"The image should be saved successfully as a .jpg file.");
151
152
#ifdef MODULE_SVG_ENABLED
153
// Load SVG with embedded jpg image
154
Ref<Image> image_svg = memnew(Image());
155
Ref<FileAccess> f_svg = FileAccess::open(TestUtils::get_data_path("images/embedded_jpg.svg"), FileAccess::READ, &err);
156
REQUIRE(f_svg.is_valid());
157
PackedByteArray data_svg;
158
data_svg.resize(f_svg->get_length() + 1);
159
f_svg->get_buffer(data_svg.ptrw(), f_svg->get_length());
160
CHECK_MESSAGE(
161
image_svg->load_svg_from_buffer(data_svg) == OK,
162
"The SVG image should load successfully.");
163
#endif // MODULE_SVG_ENABLED
164
#endif // MODULE_JPG_ENABLED
165
166
#ifdef MODULE_WEBP_ENABLED
167
// Load WebP
168
Ref<Image> image_webp = memnew(Image());
169
Ref<FileAccess> f_webp = FileAccess::open(TestUtils::get_data_path("images/icon.webp"), FileAccess::READ, &err);
170
REQUIRE(f_webp.is_valid());
171
PackedByteArray data_webp;
172
data_webp.resize(f_webp->get_length() + 1);
173
f_webp->get_buffer(data_webp.ptrw(), f_webp->get_length());
174
CHECK_MESSAGE(
175
image_webp->load_webp_from_buffer(data_webp) == OK,
176
"The WebP image should load successfully.");
177
#endif // MODULE_WEBP_ENABLED
178
179
// Load PNG
180
Ref<Image> image_png = memnew(Image());
181
Ref<FileAccess> f_png = FileAccess::open(TestUtils::get_data_path("images/icon.png"), FileAccess::READ, &err);
182
REQUIRE(f_png.is_valid());
183
PackedByteArray data_png;
184
data_png.resize(f_png->get_length() + 1);
185
f_png->get_buffer(data_png.ptrw(), f_png->get_length());
186
CHECK_MESSAGE(
187
image_png->load_png_from_buffer(data_png) == OK,
188
"The PNG image should load successfully.");
189
190
#ifdef MODULE_TGA_ENABLED
191
// Load TGA
192
Ref<Image> image_tga = memnew(Image());
193
Ref<FileAccess> f_tga = FileAccess::open(TestUtils::get_data_path("images/icon.tga"), FileAccess::READ, &err);
194
REQUIRE(f_tga.is_valid());
195
PackedByteArray data_tga;
196
data_tga.resize(f_tga->get_length() + 1);
197
f_tga->get_buffer(data_tga.ptrw(), f_tga->get_length());
198
CHECK_MESSAGE(
199
image_tga->load_tga_from_buffer(data_tga) == OK,
200
"The TGA image should load successfully.");
201
#endif // MODULE_TGA_ENABLED
202
}
203
204
TEST_CASE("[Image] Basic getters") {
205
Ref<Image> image = memnew(Image(8, 4, false, Image::FORMAT_LA8));
206
CHECK(image->get_width() == 8);
207
CHECK(image->get_height() == 4);
208
CHECK(image->get_size() == Vector2(8, 4));
209
CHECK(image->get_format() == Image::FORMAT_LA8);
210
CHECK(image->get_used_rect() == Rect2i(0, 0, 0, 0));
211
Ref<Image> image_get_rect = image->get_region(Rect2i(0, 0, 2, 1));
212
CHECK(image_get_rect->get_size() == Vector2(2, 1));
213
}
214
215
TEST_CASE("[Image] Resizing") {
216
Ref<Image> image = memnew(Image(8, 8, false, Image::FORMAT_RGBA8));
217
// Crop
218
image->crop(4, 4);
219
CHECK_MESSAGE(
220
image->get_size() == Vector2(4, 4),
221
"get_size() should return the correct size after cropping.");
222
image->set_pixel(0, 0, Color(1, 1, 1, 1));
223
224
// Resize
225
for (int i = 0; i < 5; i++) {
226
Ref<Image> image_resized = memnew(Image());
227
image_resized->copy_internals_from(image);
228
Image::Interpolation interpolation = static_cast<Image::Interpolation>(i);
229
image_resized->resize(8, 8, interpolation);
230
CHECK_MESSAGE(
231
image_resized->get_size() == Vector2(8, 8),
232
"get_size() should return the correct size after resizing.");
233
CHECK_MESSAGE(
234
image_resized->get_pixel(1, 1).a > 0,
235
"Resizing an image should also affect its content.");
236
}
237
238
// shrink_x2()
239
image->shrink_x2();
240
CHECK_MESSAGE(
241
image->get_size() == Vector2(2, 2),
242
"get_size() should return the correct size after shrink_x2().");
243
244
// resize_to_po2()
245
Ref<Image> image_po_2 = memnew(Image(14, 28, false, Image::FORMAT_RGBA8));
246
image_po_2->resize_to_po2();
247
CHECK_MESSAGE(
248
image_po_2->get_size() == Vector2(16, 32),
249
"get_size() should return the correct size after resize_to_po2().");
250
}
251
252
TEST_CASE("[Image] Modifying pixels of an image") {
253
Ref<Image> image = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
254
image->set_pixel(0, 0, Color(1, 1, 1, 1));
255
CHECK_MESSAGE(
256
!image->is_invisible(),
257
"Image should not be invisible after drawing on it.");
258
CHECK_MESSAGE(
259
image->get_pixelv(Vector2(0, 0)).is_equal_approx(Color(1, 1, 1, 1)),
260
"Image's get_pixel() should return the same color value as the one being set with set_pixel() in the same position.");
261
CHECK_MESSAGE(
262
image->get_used_rect() == Rect2i(0, 0, 1, 1),
263
"Image's get_used_rect should return the expected value, larger than Rect2i(0, 0, 0, 0) if it's visible.");
264
265
image->set_pixelv(Vector2(0, 0), Color(0.5, 0.5, 0.5, 0.5));
266
Ref<Image> image2 = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
267
268
// Fill image with color
269
image2->fill(Color(0.5, 0.5, 0.5, 0.5));
270
for (int m = 0; m < image2->get_mipmap_count(); m++) {
271
Ref<Image> mip_image = image2->get_image_from_mipmap(m);
272
273
for (int y = 0; y < mip_image->get_height(); y++) {
274
for (int x = 0; x < mip_image->get_width(); x++) {
275
CHECK_MESSAGE(
276
mip_image->get_pixel(x, y).r > 0.49,
277
"fill() should colorize all pixels of the image.");
278
}
279
}
280
}
281
282
// Fill rect with color
283
{
284
const int img_width = 3;
285
const int img_height = 3;
286
Vector<Rect2i> rects;
287
rects.push_back(Rect2i());
288
rects.push_back(Rect2i(-5, -5, 3, 3));
289
rects.push_back(Rect2i(img_width, 0, 12, 12));
290
rects.push_back(Rect2i(0, img_height, 12, 12));
291
rects.push_back(Rect2i(img_width + 1, img_height + 2, 12, 12));
292
rects.push_back(Rect2i(1, 1, 1, 1));
293
rects.push_back(Rect2i(0, 1, 2, 3));
294
rects.push_back(Rect2i(-5, 0, img_width + 10, 2));
295
rects.push_back(Rect2i(0, -5, 2, img_height + 10));
296
rects.push_back(Rect2i(-1, -1, img_width + 1, img_height + 1));
297
298
for (const Rect2i &rect : rects) {
299
Ref<Image> img = memnew(Image(img_width, img_height, false, Image::FORMAT_RGBA8));
300
img->fill_rect(rect, Color(1, 1, 1, 1));
301
for (int y = 0; y < img->get_height(); y++) {
302
for (int x = 0; x < img->get_width(); x++) {
303
if (rect.abs().has_point(Point2(x, y))) {
304
CHECK_MESSAGE(
305
img->get_pixel(x, y).is_equal_approx(Color(1, 1, 1, 1)),
306
"fill_rect() should colorize all image pixels within rect bounds.");
307
} else {
308
CHECK_MESSAGE(
309
!img->get_pixel(x, y).is_equal_approx(Color(1, 1, 1, 1)),
310
"fill_rect() shouldn't colorize any image pixel out of rect bounds.");
311
}
312
}
313
}
314
}
315
}
316
317
// Blend two images together
318
image->blend_rect(image2, Rect2i(Vector2i(0, 0), image2->get_size()), Vector2i(0, 0));
319
CHECK_MESSAGE(
320
image->get_pixel(0, 0).a > 0.7,
321
"blend_rect() should blend the alpha values of the two images.");
322
CHECK_MESSAGE(
323
image->get_used_rect().size == image->get_size(),
324
"get_used_rect() should return the expected value, its Rect size should be the same as get_size() if there are no transparent pixels.");
325
326
Ref<Image> image3 = memnew(Image(2, 2, false, Image::FORMAT_RGBA8));
327
image3->set_pixel(0, 0, Color(0, 1, 0, 1));
328
329
//blit_rect() two images together
330
image->blit_rect(image3, Rect2i(Vector2i(0, 0), image3->get_size()), Vector2i(0, 0));
331
CHECK_MESSAGE(
332
image->get_pixel(0, 0).is_equal_approx(Color(0, 1, 0, 1)),
333
"blit_rect() should replace old colors and not blend them.");
334
CHECK_MESSAGE(
335
!image->get_pixel(2, 2).is_equal_approx(Color(0, 1, 0, 1)),
336
"blit_rect() should not affect the area of the image that is outside src_rect.");
337
338
// Flip image
339
image3->flip_x();
340
CHECK(image3->get_pixel(1, 0).is_equal_approx(Color(0, 1, 0, 1)));
341
CHECK_MESSAGE(
342
image3->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 0)),
343
"flip_x() should not leave old pixels behind.");
344
image3->flip_y();
345
CHECK(image3->get_pixel(1, 1).is_equal_approx(Color(0, 1, 0, 1)));
346
CHECK_MESSAGE(
347
image3->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 0)),
348
"flip_y() should not leave old pixels behind.");
349
350
// Pre-multiply Alpha then Convert from RGBA to L8, checking alpha
351
{
352
Ref<Image> gray_image = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
353
gray_image->fill_rect(Rect2i(0, 0, 3, 3), Color(1, 1, 1, 0));
354
gray_image->set_pixel(1, 1, Color(1, 1, 1, 1));
355
gray_image->set_pixel(1, 2, Color(0.5, 0.5, 0.5, 0.5));
356
gray_image->set_pixel(2, 1, Color(0.25, 0.05, 0.5, 1.0));
357
gray_image->set_pixel(2, 2, Color(0.5, 0.25, 0.95, 0.75));
358
gray_image->premultiply_alpha();
359
gray_image->convert(Image::FORMAT_L8);
360
CHECK_MESSAGE(gray_image->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
361
CHECK_MESSAGE(gray_image->get_pixel(0, 1).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
362
CHECK_MESSAGE(gray_image->get_pixel(0, 2).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
363
CHECK_MESSAGE(gray_image->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
364
CHECK_MESSAGE(gray_image->get_pixel(1, 1).is_equal_approx(Color(1, 1, 1, 1)), "convert() RGBA to L8 should be white.");
365
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).");
366
CHECK_MESSAGE(gray_image->get_pixel(2, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
367
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).");
368
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).");
369
}
370
}
371
372
TEST_CASE("[Image] Custom mipmaps") {
373
Ref<Image> image = memnew(Image(100, 100, false, Image::FORMAT_RGBA8));
374
375
REQUIRE(!image->has_mipmaps());
376
image->generate_mipmaps();
377
REQUIRE(image->has_mipmaps());
378
379
const int mipmaps = image->get_mipmap_count() + 1;
380
REQUIRE(mipmaps == 7);
381
382
// Initialize reference mipmap data.
383
// Each byte is given value "mipmap_index * 5".
384
385
{
386
PackedByteArray data = image->get_data();
387
uint8_t *data_ptr = data.ptrw();
388
389
for (int mip = 0; mip < mipmaps; mip++) {
390
int64_t mip_offset = 0;
391
int64_t mip_size = 0;
392
image->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
393
394
for (int i = 0; i < mip_size; i++) {
395
data_ptr[mip_offset + i] = mip * 5;
396
}
397
}
398
image->set_data(image->get_width(), image->get_height(), image->has_mipmaps(), image->get_format(), data);
399
}
400
401
// Byte format conversion.
402
403
for (int format = Image::FORMAT_L8; format <= Image::FORMAT_RGBA8; format++) {
404
Ref<Image> image_bytes = memnew(Image());
405
image_bytes->copy_internals_from(image);
406
image_bytes->convert((Image::Format)format);
407
REQUIRE(image_bytes->has_mipmaps());
408
409
PackedByteArray data = image_bytes->get_data();
410
const uint8_t *data_ptr = data.ptr();
411
412
for (int mip = 0; mip < mipmaps; mip++) {
413
int64_t mip_offset = 0;
414
int64_t mip_size = 0;
415
image_bytes->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
416
417
for (int i = 0; i < mip_size; i++) {
418
if (data_ptr[mip_offset + i] != mip * 5) {
419
REQUIRE_MESSAGE(false, "Byte format conversion error.");
420
}
421
}
422
}
423
}
424
425
// Floating point format conversion.
426
427
for (int format = Image::FORMAT_RF; format <= Image::FORMAT_RGBAF; format++) {
428
Ref<Image> image_rgbaf = memnew(Image());
429
image_rgbaf->copy_internals_from(image);
430
image_rgbaf->convert((Image::Format)format);
431
REQUIRE(image_rgbaf->has_mipmaps());
432
433
PackedByteArray data = image_rgbaf->get_data();
434
const uint8_t *data_ptr = data.ptr();
435
436
for (int mip = 0; mip < mipmaps; mip++) {
437
int64_t mip_offset = 0;
438
int64_t mip_size = 0;
439
image_rgbaf->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
440
441
for (int i = 0; i < mip_size; i += 4) {
442
float value = *(float *)(data_ptr + mip_offset + i);
443
if (!Math::is_equal_approx(value * 255.0f, mip * 5)) {
444
REQUIRE_MESSAGE(false, "Floating point conversion error.");
445
}
446
}
447
}
448
}
449
}
450
451
TEST_CASE("[Image] Convert image") {
452
for (int format = Image::FORMAT_RF; format < Image::FORMAT_RGBE9995; format++) {
453
for (int new_format = Image::FORMAT_RF; new_format < Image::FORMAT_RGBE9995; new_format++) {
454
Ref<Image> image = memnew(Image(4, 4, false, (Image::Format)format));
455
image->convert((Image::Format)new_format);
456
String format_string = Image::format_names[(Image::Format)format];
457
String new_format_string = Image::format_names[(Image::Format)new_format];
458
format_string = "Error converting from " + format_string + " to " + new_format_string + ".";
459
CHECK_MESSAGE(image->get_format() == new_format, format_string);
460
}
461
}
462
463
Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
464
PackedByteArray image_data = image->get_data();
465
ERR_PRINT_OFF;
466
image->convert((Image::Format)-1);
467
ERR_PRINT_ON;
468
CHECK_MESSAGE(image->get_data() == image_data, "Image conversion to invalid type (-1) should not alter image.");
469
Ref<Image> image2 = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
470
image_data = image2->get_data();
471
ERR_PRINT_OFF;
472
image2->convert((Image::Format)(Image::FORMAT_MAX + 1));
473
ERR_PRINT_ON;
474
CHECK_MESSAGE(image2->get_data() == image_data, "Image conversion to invalid type (Image::FORMAT_MAX + 1) should not alter image.");
475
}
476
477
} // namespace TestImage
478
479