1 | /**************************************************************************/ |
2 | /* image_loader.cpp */ |
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 | #include "image_loader.h" |
32 | |
33 | #include "core/string/print_string.h" |
34 | |
35 | void ImageFormatLoader::_bind_methods() { |
36 | BIND_BITFIELD_FLAG(FLAG_NONE); |
37 | BIND_BITFIELD_FLAG(FLAG_FORCE_LINEAR); |
38 | BIND_BITFIELD_FLAG(FLAG_CONVERT_COLORS); |
39 | } |
40 | |
41 | bool ImageFormatLoader::recognize(const String &p_extension) const { |
42 | List<String> extensions; |
43 | get_recognized_extensions(&extensions); |
44 | for (const String &E : extensions) { |
45 | if (E.nocasecmp_to(p_extension) == 0) { |
46 | return true; |
47 | } |
48 | } |
49 | |
50 | return false; |
51 | } |
52 | |
53 | Error ImageFormatLoaderExtension::load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) { |
54 | Error err = ERR_UNAVAILABLE; |
55 | GDVIRTUAL_CALL(_load_image, p_image, p_fileaccess, p_flags, p_scale, err); |
56 | return err; |
57 | } |
58 | |
59 | void ImageFormatLoaderExtension::get_recognized_extensions(List<String> *p_extension) const { |
60 | PackedStringArray ext; |
61 | if (GDVIRTUAL_CALL(_get_recognized_extensions, ext)) { |
62 | for (int i = 0; i < ext.size(); i++) { |
63 | p_extension->push_back(ext[i]); |
64 | } |
65 | } |
66 | } |
67 | |
68 | void ImageFormatLoaderExtension::add_format_loader() { |
69 | ImageLoader::add_image_format_loader(this); |
70 | } |
71 | |
72 | void ImageFormatLoaderExtension::remove_format_loader() { |
73 | ImageLoader::remove_image_format_loader(this); |
74 | } |
75 | |
76 | void ImageFormatLoaderExtension::_bind_methods() { |
77 | GDVIRTUAL_BIND(_get_recognized_extensions); |
78 | GDVIRTUAL_BIND(_load_image, "image" , "fileaccess" , "flags" , "scale" ); |
79 | ClassDB::bind_method(D_METHOD("add_format_loader" ), &ImageFormatLoaderExtension::add_format_loader); |
80 | ClassDB::bind_method(D_METHOD("remove_format_loader" ), &ImageFormatLoaderExtension::remove_format_loader); |
81 | } |
82 | |
83 | Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) { |
84 | ERR_FAIL_COND_V_MSG(p_image.is_null(), ERR_INVALID_PARAMETER, "It's not a reference to a valid Image object." ); |
85 | |
86 | Ref<FileAccess> f = p_custom; |
87 | if (f.is_null()) { |
88 | Error err; |
89 | f = FileAccess::open(p_file, FileAccess::READ, &err); |
90 | ERR_FAIL_COND_V_MSG(f.is_null(), err, "Error opening file '" + p_file + "'." ); |
91 | } |
92 | |
93 | String extension = p_file.get_extension(); |
94 | |
95 | for (int i = 0; i < loader.size(); i++) { |
96 | if (!loader[i]->recognize(extension)) { |
97 | continue; |
98 | } |
99 | Error err = loader.write[i]->load_image(p_image, f, p_flags, p_scale); |
100 | if (err != OK) { |
101 | ERR_PRINT("Error loading image: " + p_file); |
102 | } |
103 | |
104 | if (err != ERR_FILE_UNRECOGNIZED) { |
105 | return err; |
106 | } |
107 | } |
108 | |
109 | return ERR_FILE_UNRECOGNIZED; |
110 | } |
111 | |
112 | void ImageLoader::get_recognized_extensions(List<String> *p_extensions) { |
113 | for (int i = 0; i < loader.size(); i++) { |
114 | loader[i]->get_recognized_extensions(p_extensions); |
115 | } |
116 | } |
117 | |
118 | Ref<ImageFormatLoader> ImageLoader::recognize(const String &p_extension) { |
119 | for (int i = 0; i < loader.size(); i++) { |
120 | if (loader[i]->recognize(p_extension)) { |
121 | return loader[i]; |
122 | } |
123 | } |
124 | |
125 | return nullptr; |
126 | } |
127 | |
128 | Vector<Ref<ImageFormatLoader>> ImageLoader::loader; |
129 | |
130 | void ImageLoader::add_image_format_loader(Ref<ImageFormatLoader> p_loader) { |
131 | loader.push_back(p_loader); |
132 | } |
133 | |
134 | void ImageLoader::remove_image_format_loader(Ref<ImageFormatLoader> p_loader) { |
135 | loader.erase(p_loader); |
136 | } |
137 | |
138 | void ImageLoader::cleanup() { |
139 | while (loader.size()) { |
140 | remove_image_format_loader(loader[0]); |
141 | } |
142 | } |
143 | |
144 | ///////////////// |
145 | |
146 | Ref<Resource> ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { |
147 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
148 | if (f.is_null()) { |
149 | if (r_error) { |
150 | *r_error = ERR_CANT_OPEN; |
151 | } |
152 | return Ref<Resource>(); |
153 | } |
154 | |
155 | uint8_t [4] = { 0, 0, 0, 0 }; |
156 | f->get_buffer(header, 4); |
157 | |
158 | bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M'; |
159 | if (unrecognized) { |
160 | if (r_error) { |
161 | *r_error = ERR_FILE_UNRECOGNIZED; |
162 | } |
163 | ERR_FAIL_V(Ref<Resource>()); |
164 | } |
165 | |
166 | String extension = f->get_pascal_string(); |
167 | |
168 | int idx = -1; |
169 | |
170 | for (int i = 0; i < ImageLoader::loader.size(); i++) { |
171 | if (ImageLoader::loader[i]->recognize(extension)) { |
172 | idx = i; |
173 | break; |
174 | } |
175 | } |
176 | |
177 | if (idx == -1) { |
178 | if (r_error) { |
179 | *r_error = ERR_FILE_UNRECOGNIZED; |
180 | } |
181 | ERR_FAIL_V(Ref<Resource>()); |
182 | } |
183 | |
184 | Ref<Image> image; |
185 | image.instantiate(); |
186 | |
187 | Error err = ImageLoader::loader.write[idx]->load_image(image, f); |
188 | |
189 | if (err != OK) { |
190 | if (r_error) { |
191 | *r_error = err; |
192 | } |
193 | return Ref<Resource>(); |
194 | } |
195 | |
196 | if (r_error) { |
197 | *r_error = OK; |
198 | } |
199 | |
200 | return image; |
201 | } |
202 | |
203 | void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const { |
204 | p_extensions->push_back("image" ); |
205 | } |
206 | |
207 | bool ResourceFormatLoaderImage::handles_type(const String &p_type) const { |
208 | return p_type == "Image" ; |
209 | } |
210 | |
211 | String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const { |
212 | return p_path.get_extension().to_lower() == "image" ? "Image" : String(); |
213 | } |
214 | |