image_loader.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. bool ImageFormatLoader::recognize(const String &p_extension) const {
  33. List<String> extensions;
  34. get_recognized_extensions(&extensions);
  35. for (const String &E : extensions) {
  36. if (E.nocasecmp_to(p_extension) == 0) {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. Error ImageLoader::load_image(String p_file, Ref<Image> p_image, Ref<FileAccess> p_custom, bool p_force_linear, float p_scale) {
  43. ERR_FAIL_COND_V_MSG(p_image.is_null(), ERR_INVALID_PARAMETER, "It's not a reference to a valid Image object.");
  44. Ref<FileAccess> f = p_custom;
  45. if (f.is_null()) {
  46. Error err;
  47. f = FileAccess::open(p_file, FileAccess::READ, &err);
  48. ERR_FAIL_COND_V_MSG(f.is_null(), err, "Error opening file '" + p_file + "'.");
  49. }
  50. String extension = p_file.get_extension();
  51. for (int i = 0; i < loader.size(); i++) {
  52. if (!loader[i]->recognize(extension)) {
  53. continue;
  54. }
  55. Error err = loader[i]->load_image(p_image, f, p_force_linear, p_scale);
  56. if (err != OK) {
  57. ERR_PRINT("Error loading image: " + p_file);
  58. }
  59. if (err != ERR_FILE_UNRECOGNIZED) {
  60. return err;
  61. }
  62. }
  63. return ERR_FILE_UNRECOGNIZED;
  64. }
  65. void ImageLoader::get_recognized_extensions(List<String> *p_extensions) {
  66. for (int i = 0; i < loader.size(); i++) {
  67. loader[i]->get_recognized_extensions(p_extensions);
  68. }
  69. }
  70. ImageFormatLoader *ImageLoader::recognize(const String &p_extension) {
  71. for (int i = 0; i < loader.size(); i++) {
  72. if (loader[i]->recognize(p_extension)) {
  73. return loader[i];
  74. }
  75. }
  76. return nullptr;
  77. }
  78. Vector<ImageFormatLoader *> ImageLoader::loader;
  79. void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) {
  80. loader.push_back(p_loader);
  81. }
  82. void ImageLoader::remove_image_format_loader(ImageFormatLoader *p_loader) {
  83. loader.erase(p_loader);
  84. }
  85. const Vector<ImageFormatLoader *> &ImageLoader::get_image_format_loaders() {
  86. return loader;
  87. }
  88. void ImageLoader::cleanup() {
  89. while (loader.size()) {
  90. remove_image_format_loader(loader[0]);
  91. }
  92. }
  93. /////////////////
  94. 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) {
  95. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  96. if (f.is_null()) {
  97. if (r_error) {
  98. *r_error = ERR_CANT_OPEN;
  99. }
  100. return Ref<Resource>();
  101. }
  102. uint8_t header[4] = { 0, 0, 0, 0 };
  103. f->get_buffer(header, 4);
  104. bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M';
  105. if (unrecognized) {
  106. if (r_error) {
  107. *r_error = ERR_FILE_UNRECOGNIZED;
  108. }
  109. ERR_FAIL_V(Ref<Resource>());
  110. }
  111. String extension = f->get_pascal_string();
  112. int idx = -1;
  113. for (int i = 0; i < ImageLoader::loader.size(); i++) {
  114. if (ImageLoader::loader[i]->recognize(extension)) {
  115. idx = i;
  116. break;
  117. }
  118. }
  119. if (idx == -1) {
  120. if (r_error) {
  121. *r_error = ERR_FILE_UNRECOGNIZED;
  122. }
  123. ERR_FAIL_V(Ref<Resource>());
  124. }
  125. Ref<Image> image;
  126. image.instantiate();
  127. Error err = ImageLoader::loader[idx]->load_image(image, f, false, 1.0);
  128. if (err != OK) {
  129. if (r_error) {
  130. *r_error = err;
  131. }
  132. return Ref<Resource>();
  133. }
  134. if (r_error) {
  135. *r_error = OK;
  136. }
  137. return image;
  138. }
  139. void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const {
  140. p_extensions->push_back("image");
  141. }
  142. bool ResourceFormatLoaderImage::handles_type(const String &p_type) const {
  143. return p_type == "Image";
  144. }
  145. String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {
  146. return p_path.get_extension().to_lower() == "image" ? "Image" : String();
  147. }