image_loader.cpp 7.0 KB

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