Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_image.h
10278 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 y = 0; y < image2->get_height(); y++) {
271
for (int x = 0; x < image2->get_width(); x++) {
272
CHECK_MESSAGE(
273
image2->get_pixel(x, y).r > 0.49,
274
"fill() should colorize all pixels of the image.");
275
}
276
}
277
278
// Fill rect with color
279
{
280
const int img_width = 3;
281
const int img_height = 3;
282
Vector<Rect2i> rects;
283
rects.push_back(Rect2i());
284
rects.push_back(Rect2i(-5, -5, 3, 3));
285
rects.push_back(Rect2i(img_width, 0, 12, 12));
286
rects.push_back(Rect2i(0, img_height, 12, 12));
287
rects.push_back(Rect2i(img_width + 1, img_height + 2, 12, 12));
288
rects.push_back(Rect2i(1, 1, 1, 1));
289
rects.push_back(Rect2i(0, 1, 2, 3));
290
rects.push_back(Rect2i(-5, 0, img_width + 10, 2));
291
rects.push_back(Rect2i(0, -5, 2, img_height + 10));
292
rects.push_back(Rect2i(-1, -1, img_width + 1, img_height + 1));
293
294
for (const Rect2i &rect : rects) {
295
Ref<Image> img = memnew(Image(img_width, img_height, false, Image::FORMAT_RGBA8));
296
img->fill_rect(rect, Color(1, 1, 1, 1));
297
for (int y = 0; y < img->get_height(); y++) {
298
for (int x = 0; x < img->get_width(); x++) {
299
if (rect.abs().has_point(Point2(x, y))) {
300
CHECK_MESSAGE(
301
img->get_pixel(x, y).is_equal_approx(Color(1, 1, 1, 1)),
302
"fill_rect() should colorize all image pixels within rect bounds.");
303
} else {
304
CHECK_MESSAGE(
305
!img->get_pixel(x, y).is_equal_approx(Color(1, 1, 1, 1)),
306
"fill_rect() shouldn't colorize any image pixel out of rect bounds.");
307
}
308
}
309
}
310
}
311
}
312
313
// Blend two images together
314
image->blend_rect(image2, Rect2i(Vector2i(0, 0), image2->get_size()), Vector2i(0, 0));
315
CHECK_MESSAGE(
316
image->get_pixel(0, 0).a > 0.7,
317
"blend_rect() should blend the alpha values of the two images.");
318
CHECK_MESSAGE(
319
image->get_used_rect().size == image->get_size(),
320
"get_used_rect() should return the expected value, its Rect size should be the same as get_size() if there are no transparent pixels.");
321
322
Ref<Image> image3 = memnew(Image(2, 2, false, Image::FORMAT_RGBA8));
323
image3->set_pixel(0, 0, Color(0, 1, 0, 1));
324
325
//blit_rect() two images together
326
image->blit_rect(image3, Rect2i(Vector2i(0, 0), image3->get_size()), Vector2i(0, 0));
327
CHECK_MESSAGE(
328
image->get_pixel(0, 0).is_equal_approx(Color(0, 1, 0, 1)),
329
"blit_rect() should replace old colors and not blend them.");
330
CHECK_MESSAGE(
331
!image->get_pixel(2, 2).is_equal_approx(Color(0, 1, 0, 1)),
332
"blit_rect() should not affect the area of the image that is outside src_rect.");
333
334
// Flip image
335
image3->flip_x();
336
CHECK(image3->get_pixel(1, 0).is_equal_approx(Color(0, 1, 0, 1)));
337
CHECK_MESSAGE(
338
image3->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 0)),
339
"flip_x() should not leave old pixels behind.");
340
image3->flip_y();
341
CHECK(image3->get_pixel(1, 1).is_equal_approx(Color(0, 1, 0, 1)));
342
CHECK_MESSAGE(
343
image3->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 0)),
344
"flip_y() should not leave old pixels behind.");
345
346
// Pre-multiply Alpha then Convert from RGBA to L8, checking alpha
347
{
348
Ref<Image> gray_image = memnew(Image(3, 3, false, Image::FORMAT_RGBA8));
349
gray_image->fill_rect(Rect2i(0, 0, 3, 3), Color(1, 1, 1, 0));
350
gray_image->set_pixel(1, 1, Color(1, 1, 1, 1));
351
gray_image->set_pixel(1, 2, Color(0.5, 0.5, 0.5, 0.5));
352
gray_image->set_pixel(2, 1, Color(0.25, 0.05, 0.5, 1.0));
353
gray_image->set_pixel(2, 2, Color(0.5, 0.25, 0.95, 0.75));
354
gray_image->premultiply_alpha();
355
gray_image->convert(Image::FORMAT_L8);
356
CHECK_MESSAGE(gray_image->get_pixel(0, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
357
CHECK_MESSAGE(gray_image->get_pixel(0, 1).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
358
CHECK_MESSAGE(gray_image->get_pixel(0, 2).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
359
CHECK_MESSAGE(gray_image->get_pixel(1, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
360
CHECK_MESSAGE(gray_image->get_pixel(1, 1).is_equal_approx(Color(1, 1, 1, 1)), "convert() RGBA to L8 should be white.");
361
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).");
362
CHECK_MESSAGE(gray_image->get_pixel(2, 0).is_equal_approx(Color(0, 0, 0, 1)), "convert() RGBA to L8 should be black.");
363
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).");
364
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).");
365
}
366
}
367
368
TEST_CASE("[Image] Custom mipmaps") {
369
Ref<Image> image = memnew(Image(100, 100, false, Image::FORMAT_RGBA8));
370
371
REQUIRE(!image->has_mipmaps());
372
image->generate_mipmaps();
373
REQUIRE(image->has_mipmaps());
374
375
const int mipmaps = image->get_mipmap_count() + 1;
376
REQUIRE(mipmaps == 7);
377
378
// Initialize reference mipmap data.
379
// Each byte is given value "mipmap_index * 5".
380
381
{
382
PackedByteArray data = image->get_data();
383
uint8_t *data_ptr = data.ptrw();
384
385
for (int mip = 0; mip < mipmaps; mip++) {
386
int64_t mip_offset = 0;
387
int64_t mip_size = 0;
388
image->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
389
390
for (int i = 0; i < mip_size; i++) {
391
data_ptr[mip_offset + i] = mip * 5;
392
}
393
}
394
image->set_data(image->get_width(), image->get_height(), image->has_mipmaps(), image->get_format(), data);
395
}
396
397
// Byte format conversion.
398
399
for (int format = Image::FORMAT_L8; format <= Image::FORMAT_RGBA8; format++) {
400
Ref<Image> image_bytes = memnew(Image());
401
image_bytes->copy_internals_from(image);
402
image_bytes->convert((Image::Format)format);
403
REQUIRE(image_bytes->has_mipmaps());
404
405
PackedByteArray data = image_bytes->get_data();
406
const uint8_t *data_ptr = data.ptr();
407
408
for (int mip = 0; mip < mipmaps; mip++) {
409
int64_t mip_offset = 0;
410
int64_t mip_size = 0;
411
image_bytes->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
412
413
for (int i = 0; i < mip_size; i++) {
414
if (data_ptr[mip_offset + i] != mip * 5) {
415
REQUIRE_MESSAGE(false, "Byte format conversion error.");
416
}
417
}
418
}
419
}
420
421
// Floating point format conversion.
422
423
for (int format = Image::FORMAT_RF; format <= Image::FORMAT_RGBAF; format++) {
424
Ref<Image> image_rgbaf = memnew(Image());
425
image_rgbaf->copy_internals_from(image);
426
image_rgbaf->convert((Image::Format)format);
427
REQUIRE(image_rgbaf->has_mipmaps());
428
429
PackedByteArray data = image_rgbaf->get_data();
430
const uint8_t *data_ptr = data.ptr();
431
432
for (int mip = 0; mip < mipmaps; mip++) {
433
int64_t mip_offset = 0;
434
int64_t mip_size = 0;
435
image_rgbaf->get_mipmap_offset_and_size(mip, mip_offset, mip_size);
436
437
for (int i = 0; i < mip_size; i += 4) {
438
float value = *(float *)(data_ptr + mip_offset + i);
439
if (!Math::is_equal_approx(value * 255.0f, mip * 5)) {
440
REQUIRE_MESSAGE(false, "Floating point conversion error.");
441
}
442
}
443
}
444
}
445
}
446
447
TEST_CASE("[Image] Convert image") {
448
for (int format = Image::FORMAT_RF; format < Image::FORMAT_RGBE9995; format++) {
449
for (int new_format = Image::FORMAT_RF; new_format < Image::FORMAT_RGBE9995; new_format++) {
450
Ref<Image> image = memnew(Image(4, 4, false, (Image::Format)format));
451
image->convert((Image::Format)new_format);
452
String format_string = Image::format_names[(Image::Format)format];
453
String new_format_string = Image::format_names[(Image::Format)new_format];
454
format_string = "Error converting from " + format_string + " to " + new_format_string + ".";
455
CHECK_MESSAGE(image->get_format() == new_format, format_string);
456
}
457
}
458
459
Ref<Image> image = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
460
PackedByteArray image_data = image->get_data();
461
ERR_PRINT_OFF;
462
image->convert((Image::Format)-1);
463
ERR_PRINT_ON;
464
CHECK_MESSAGE(image->get_data() == image_data, "Image conversion to invalid type (-1) should not alter image.");
465
Ref<Image> image2 = memnew(Image(4, 4, false, Image::FORMAT_RGBA8));
466
image_data = image2->get_data();
467
ERR_PRINT_OFF;
468
image2->convert((Image::Format)(Image::FORMAT_MAX + 1));
469
ERR_PRINT_ON;
470
CHECK_MESSAGE(image2->get_data() == image_data, "Image conversion to invalid type (Image::FORMAT_MAX + 1) should not alter image.");
471
}
472
473
} // namespace TestImage
474
475