image_loader.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*************************************************************************/
  2. /* image_loader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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;
  49. if (GDVIRTUAL_CALL(_load_image, p_image, p_fileaccess, p_flags, p_scale, err)) {
  50. return err;
  51. }
  52. return ERR_UNAVAILABLE;
  53. }
  54. void ImageFormatLoaderExtension::get_recognized_extensions(List<String> *p_extension) const {
  55. PackedStringArray ext;
  56. if (GDVIRTUAL_CALL(_get_recognized_extensions, ext)) {
  57. for (int i = 0; i < ext.size(); i++) {
  58. p_extension->push_back(ext[i]);
  59. }
  60. }
  61. }
  62. void ImageFormatLoaderExtension::add_format_loader() {
  63. ImageLoader::add_image_format_loader(this);
  64. }
  65. void ImageFormatLoaderExtension::remove_format_loader() {
  66. ImageLoader::remove_image_format_loader(this);
  67. }
  68. void ImageFormatLoaderExtension::_bind_methods() {
  69. GDVIRTUAL_BIND(_get_recognized_extensions);
  70. GDVIRTUAL_BIND(_load_image, "image", "fileaccess", "flags", "scale");
  71. ClassDB::bind_method(D_METHOD("add_format_loader"), &ImageFormatLoaderExtension::add_format_loader);
  72. ClassDB::bind_method(D_METHOD("remove_format_loader"), &ImageFormatLoaderExtension::remove_format_loader);
  73. }
  74. Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
  75. ERR_FAIL_COND_V_MSG(p_image.is_null(), ERR_INVALID_PARAMETER, "It's not a reference to a valid Image object.");
  76. Ref<FileAccess> f = p_custom;
  77. if (f.is_null()) {
  78. Error err;
  79. f = FileAccess::open(p_file, FileAccess::READ, &err);
  80. ERR_FAIL_COND_V_MSG(f.is_null(), err, "Error opening file '" + p_file + "'.");
  81. }
  82. String extension = p_file.get_extension();
  83. for (int i = 0; i < loader.size(); i++) {
  84. if (!loader[i]->recognize(extension)) {
  85. continue;
  86. }
  87. Error err = loader.write[i]->load_image(p_image, f, p_flags, p_scale);
  88. if (err != OK) {
  89. ERR_PRINT("Error loading image: " + p_file);
  90. }
  91. if (err != ERR_FILE_UNRECOGNIZED) {
  92. return err;
  93. }
  94. }
  95. return ERR_FILE_UNRECOGNIZED;
  96. }
  97. void ImageLoader::get_recognized_extensions(List<String> *p_extensions) {
  98. for (int i = 0; i < loader.size(); i++) {
  99. loader[i]->get_recognized_extensions(p_extensions);
  100. }
  101. }
  102. Ref<ImageFormatLoader> ImageLoader::recognize(const String &p_extension) {
  103. for (int i = 0; i < loader.size(); i++) {
  104. if (loader[i]->recognize(p_extension)) {
  105. return loader[i];
  106. }
  107. }
  108. return nullptr;
  109. }
  110. Vector<Ref<ImageFormatLoader>> ImageLoader::loader;
  111. void ImageLoader::add_image_format_loader(Ref<ImageFormatLoader> p_loader) {
  112. loader.push_back(p_loader);
  113. }
  114. void ImageLoader::remove_image_format_loader(Ref<ImageFormatLoader> p_loader) {
  115. loader.erase(p_loader);
  116. }
  117. const Vector<Ref<ImageFormatLoader>> &ImageLoader::get_image_format_loaders() {
  118. return loader;
  119. }
  120. void ImageLoader::cleanup() {
  121. while (loader.size()) {
  122. remove_image_format_loader(loader[0]);
  123. }
  124. }
  125. /////////////////
  126. 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) {
  127. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  128. if (f.is_null()) {
  129. if (r_error) {
  130. *r_error = ERR_CANT_OPEN;
  131. }
  132. return Ref<Resource>();
  133. }
  134. uint8_t header[4] = { 0, 0, 0, 0 };
  135. f->get_buffer(header, 4);
  136. bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M';
  137. if (unrecognized) {
  138. if (r_error) {
  139. *r_error = ERR_FILE_UNRECOGNIZED;
  140. }
  141. ERR_FAIL_V(Ref<Resource>());
  142. }
  143. String extension = f->get_pascal_string();
  144. int idx = -1;
  145. for (int i = 0; i < ImageLoader::loader.size(); i++) {
  146. if (ImageLoader::loader[i]->recognize(extension)) {
  147. idx = i;
  148. break;
  149. }
  150. }
  151. if (idx == -1) {
  152. if (r_error) {
  153. *r_error = ERR_FILE_UNRECOGNIZED;
  154. }
  155. ERR_FAIL_V(Ref<Resource>());
  156. }
  157. Ref<Image> image;
  158. image.instantiate();
  159. Error err = ImageLoader::loader.write[idx]->load_image(image, f);
  160. if (err != OK) {
  161. if (r_error) {
  162. *r_error = err;
  163. }
  164. return Ref<Resource>();
  165. }
  166. if (r_error) {
  167. *r_error = OK;
  168. }
  169. return image;
  170. }
  171. void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const {
  172. p_extensions->push_back("image");
  173. }
  174. bool ResourceFormatLoaderImage::handles_type(const String &p_type) const {
  175. return p_type == "Image";
  176. }
  177. String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {
  178. return p_path.get_extension().to_lower() == "image" ? "Image" : String();
  179. }