image_loader.cpp 6.9 KB

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