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