Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/dds/tests/test_dds.h
20997 views
1
/**************************************************************************/
2
/* test_dds.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 "../image_saver_dds.h"
34
35
#include "core/config/project_settings.h"
36
#include "core/io/dir_access.h"
37
#include "core/io/image.h"
38
#include "tests/core/config/test_project_settings.h"
39
#include "tests/test_macros.h"
40
#include "tests/test_utils.h"
41
42
namespace TestDDS {
43
String init(const String &p_test, const String &p_copy_target = String()) {
44
String old_resource_path = TestProjectSettingsInternalsAccessor::resource_path();
45
Error err;
46
// Setup project settings with `res://` set to a temporary path.
47
String project_folder = TestUtils::get_temp_path(p_test.get_file().get_basename());
48
TestProjectSettingsInternalsAccessor::resource_path() = project_folder;
49
ProjectSettings *ps = ProjectSettings::get_singleton();
50
err = ps->setup(project_folder, String(), true);
51
52
// Create the imported files folder as the editor import process expects it to exist.
53
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
54
da->make_dir_recursive(ps->globalize_path(ps->get_imported_files_path()));
55
56
if (p_copy_target.is_empty()) {
57
return old_resource_path;
58
}
59
60
// Copy all the necessary test data files to the res:// directory.
61
String test_data = String("tests/data").path_join(p_test);
62
da = DirAccess::open(test_data);
63
CHECK_MESSAGE(da.is_valid(), "Unable to open folder.");
64
da->list_dir_begin();
65
for (String item = da->get_next(); !item.is_empty(); item = da->get_next()) {
66
if (!FileAccess::exists(test_data.path_join(item))) {
67
continue;
68
}
69
Ref<FileAccess> output = FileAccess::open(p_copy_target.path_join(item), FileAccess::WRITE, &err);
70
CHECK_MESSAGE(err == OK, "Unable to open output file.");
71
output->store_buffer(FileAccess::get_file_as_bytes(test_data.path_join(item)));
72
output->close();
73
}
74
da->list_dir_end();
75
return old_resource_path;
76
}
77
78
TEST_CASE("[SceneTree][DDSSaver] Save DDS - Save valid image with mipmap" * doctest::skip(true)) {
79
String old_resource_path = init("save_dds_valid_image_with_mipmap");
80
Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
81
image->fill(Color(1, 0, 0)); // Fill with red color
82
image->generate_mipmaps();
83
image->compress_from_channels(Image::COMPRESS_S3TC, Image::USED_CHANNELS_RGBA);
84
Error err = save_dds("res://valid_image_with_mipmap.dds", image);
85
CHECK(err == OK);
86
87
Ref<Image> loaded_image;
88
loaded_image.instantiate();
89
Vector<uint8_t> buffer = FileAccess::get_file_as_bytes("res://valid_image_with_mipmap.dds", &err);
90
CHECK(err == OK);
91
err = loaded_image->load_dds_from_buffer(buffer);
92
CHECK(err == OK);
93
Dictionary metrics = image->compute_image_metrics(loaded_image, false);
94
CHECK(metrics.size() > 0);
95
CHECK_MESSAGE(metrics.has("root_mean_squared"), "Metrics dictionary contains 'root_mean_squared'.");
96
float rms = metrics["root_mean_squared"];
97
CHECK(rms == 0.0f);
98
TestProjectSettingsInternalsAccessor::resource_path() = old_resource_path;
99
}
100
101
TEST_CASE("[SceneTree][DDSSaver] Save DDS - Save valid image with BPTC and S3TC compression" * doctest::skip(true)) {
102
String old_resource_path = init("save_dds_valid_image_bptc_s3tc");
103
Ref<Image> image_bptc = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
104
image_bptc->fill(Color(0, 0, 1)); // Fill with blue color
105
image_bptc->compress_from_channels(Image::COMPRESS_BPTC, Image::USED_CHANNELS_RGBA);
106
Error err_bptc = image_bptc->save_dds("res://valid_image_bptc.dds");
107
CHECK(err_bptc == OK);
108
109
Ref<Image> image_s3tc = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
110
image_s3tc->fill(Color(1, 1, 1)); // Fill with white color
111
image_s3tc->compress_from_channels(Image::COMPRESS_S3TC, Image::USED_CHANNELS_RGBA);
112
Error err_s3tc = image_s3tc->save_dds("res://valid_image_s3tc_combined.dds");
113
CHECK(err_s3tc == OK);
114
115
// Validate BPTC image
116
Ref<Image> loaded_image_bptc;
117
loaded_image_bptc.instantiate();
118
Vector<uint8_t> buffer_bptc = FileAccess::get_file_as_bytes("res://valid_image_bptc.dds", &err_bptc);
119
CHECK(err_bptc == OK);
120
err_bptc = loaded_image_bptc->load_dds_from_buffer(buffer_bptc);
121
CHECK(err_bptc == OK);
122
Dictionary metrics_bptc = image_bptc->compute_image_metrics(loaded_image_bptc, false);
123
CHECK(metrics_bptc.size() > 0);
124
CHECK_MESSAGE(metrics_bptc.has("root_mean_squared"), "Metrics dictionary contains 'root_mean_squared' for BPTC.");
125
float rms_bptc = metrics_bptc["root_mean_squared"];
126
CHECK(rms_bptc == 0.0f);
127
128
// Validate S3TC image
129
Ref<Image> loaded_image_s3tc;
130
loaded_image_s3tc.instantiate();
131
Vector<uint8_t> buffer_s3tc = FileAccess::get_file_as_bytes("res://valid_image_s3tc_combined.dds", &err_s3tc);
132
CHECK(err_s3tc == OK);
133
err_s3tc = loaded_image_s3tc->load_dds_from_buffer(buffer_s3tc);
134
CHECK(err_s3tc == OK);
135
Dictionary metrics_s3tc = image_s3tc->compute_image_metrics(loaded_image_s3tc, false);
136
CHECK(metrics_s3tc.size() > 0);
137
CHECK_MESSAGE(metrics_s3tc.has("root_mean_squared"), "Metrics dictionary contains 'root_mean_squared' for S3TC.");
138
float rms_s3tc = metrics_s3tc["root_mean_squared"];
139
CHECK(rms_s3tc == 0.0f);
140
TestProjectSettingsInternalsAccessor::resource_path() = old_resource_path;
141
}
142
143
TEST_CASE("[SceneTree][DDSSaver] Save DDS - Save valid uncompressed image") {
144
String old_resource_path = init("save_dds_valid_uncompressed");
145
Ref<Image> image = Image::create_empty(4, 4, false, Image::FORMAT_RGBA8);
146
image->fill(Color(0, 0, 1)); // Fill with blue color
147
Error err = image->save_dds("res://valid_image_uncompressed.dds");
148
CHECK(err == OK);
149
Vector<uint8_t> buffer = FileAccess::get_file_as_bytes("res://valid_image_uncompressed.dds", &err);
150
CHECK(err == OK);
151
Ref<Image> loaded_image;
152
loaded_image.instantiate();
153
err = loaded_image->load_dds_from_buffer(buffer);
154
CHECK(err == OK);
155
Dictionary metrics = image->compute_image_metrics(loaded_image, false);
156
CHECK(metrics.size() > 0);
157
CHECK_MESSAGE(metrics.has("root_mean_squared"), "Metrics dictionary contains 'root_mean_squared' for uncompressed.");
158
float rms = metrics["root_mean_squared"];
159
CHECK(rms == 0.0f);
160
TestProjectSettingsInternalsAccessor::resource_path() = old_resource_path;
161
}
162
} //namespace TestDDS
163
164