image_loader.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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(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, "It's not a reference to a valid Image object.");
  74. Ref<FileAccess> f = p_custom;
  75. if (f.is_null()) {
  76. Error err;
  77. f = FileAccess::open(p_file, FileAccess::READ, &err);
  78. ERR_FAIL_COND_V_MSG(f.is_null(), err, "Error opening file '" + p_file + "'.");
  79. }
  80. String extension = p_file.get_extension();
  81. for (int i = 0; i < loader.size(); i++) {
  82. if (!loader[i]->recognize(extension)) {
  83. continue;
  84. }
  85. Error err = loader.write[i]->load_image(p_image, f, p_flags, p_scale);
  86. if (err != OK) {
  87. ERR_PRINT("Error loading image: " + p_file);
  88. }
  89. if (err != ERR_FILE_UNRECOGNIZED) {
  90. return err;
  91. }
  92. }
  93. return ERR_FILE_UNRECOGNIZED;
  94. }
  95. void ImageLoader::get_recognized_extensions(List<String> *p_extensions) {
  96. for (int i = 0; i < loader.size(); i++) {
  97. loader[i]->get_recognized_extensions(p_extensions);
  98. }
  99. }
  100. Ref<ImageFormatLoader> ImageLoader::recognize(const String &p_extension) {
  101. for (int i = 0; i < loader.size(); i++) {
  102. if (loader[i]->recognize(p_extension)) {
  103. return loader[i];
  104. }
  105. }
  106. return nullptr;
  107. }
  108. Vector<Ref<ImageFormatLoader>> ImageLoader::loader;
  109. void ImageLoader::add_image_format_loader(Ref<ImageFormatLoader> p_loader) {
  110. loader.push_back(p_loader);
  111. }
  112. void ImageLoader::remove_image_format_loader(Ref<ImageFormatLoader> p_loader) {
  113. loader.erase(p_loader);
  114. }
  115. const Vector<Ref<ImageFormatLoader>> &ImageLoader::get_image_format_loaders() {
  116. return loader;
  117. }
  118. void ImageLoader::cleanup() {
  119. while (loader.size()) {
  120. remove_image_format_loader(loader[0]);
  121. }
  122. }
  123. /////////////////
  124. 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) {
  125. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  126. if (f.is_null()) {
  127. if (r_error) {
  128. *r_error = ERR_CANT_OPEN;
  129. }
  130. return Ref<Resource>();
  131. }
  132. uint8_t header[4] = { 0, 0, 0, 0 };
  133. f->get_buffer(header, 4);
  134. bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M';
  135. if (unrecognized) {
  136. if (r_error) {
  137. *r_error = ERR_FILE_UNRECOGNIZED;
  138. }
  139. ERR_FAIL_V(Ref<Resource>());
  140. }
  141. String extension = f->get_pascal_string();
  142. int idx = -1;
  143. for (int i = 0; i < ImageLoader::loader.size(); i++) {
  144. if (ImageLoader::loader[i]->recognize(extension)) {
  145. idx = i;
  146. break;
  147. }
  148. }
  149. if (idx == -1) {
  150. if (r_error) {
  151. *r_error = ERR_FILE_UNRECOGNIZED;
  152. }
  153. ERR_FAIL_V(Ref<Resource>());
  154. }
  155. Ref<Image> image;
  156. image.instantiate();
  157. Error err = ImageLoader::loader.write[idx]->load_image(image, f);
  158. if (err != OK) {
  159. if (r_error) {
  160. *r_error = err;
  161. }
  162. return Ref<Resource>();
  163. }
  164. if (r_error) {
  165. *r_error = OK;
  166. }
  167. return image;
  168. }
  169. void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const {
  170. p_extensions->push_back("image");
  171. }
  172. bool ResourceFormatLoaderImage::handles_type(const String &p_type) const {
  173. return p_type == "Image";
  174. }
  175. String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {
  176. return p_path.get_extension().to_lower() == "image" ? "Image" : String();
  177. }