image_loader.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* image_loader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/print_string.h"
  32. bool ImageFormatLoader::recognize(const String &p_extension) const {
  33. List<String> extensions;
  34. get_recognized_extensions(&extensions);
  35. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  36. if (E->get().nocasecmp_to(p_extension) == 0)
  37. return true;
  38. }
  39. return false;
  40. }
  41. Error ImageLoader::load_image(String p_file, Ref<Image> p_image, FileAccess *p_custom, bool p_force_linear, float p_scale) {
  42. ERR_FAIL_COND_V(p_image.is_null(), ERR_INVALID_PARAMETER);
  43. FileAccess *f = p_custom;
  44. if (!f) {
  45. Error err;
  46. f = FileAccess::open(p_file, FileAccess::READ, &err);
  47. if (!f) {
  48. ERR_PRINTS("Error opening file: " + p_file);
  49. return err;
  50. }
  51. }
  52. String extension = p_file.get_extension();
  53. for (int i = 0; i < loader.size(); i++) {
  54. if (!loader[i]->recognize(extension))
  55. continue;
  56. Error err = loader[i]->load_image(p_image, f, p_force_linear, p_scale);
  57. if (err != ERR_FILE_UNRECOGNIZED) {
  58. if (!p_custom)
  59. memdelete(f);
  60. return err;
  61. }
  62. }
  63. if (!p_custom)
  64. memdelete(f);
  65. return ERR_FILE_UNRECOGNIZED;
  66. }
  67. void ImageLoader::get_recognized_extensions(List<String> *p_extensions) {
  68. for (int i = 0; i < loader.size(); i++) {
  69. loader[i]->get_recognized_extensions(p_extensions);
  70. }
  71. }
  72. ImageFormatLoader *ImageLoader::recognize(const String &p_extension) {
  73. for (int i = 0; i < loader.size(); i++) {
  74. if (loader[i]->recognize(p_extension))
  75. return loader[i];
  76. }
  77. return NULL;
  78. }
  79. Vector<ImageFormatLoader *> ImageLoader::loader;
  80. void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) {
  81. loader.push_back(p_loader);
  82. }
  83. void ImageLoader::remove_image_format_loader(ImageFormatLoader *p_loader) {
  84. loader.erase(p_loader);
  85. }
  86. const Vector<ImageFormatLoader *> &ImageLoader::get_image_format_loaders() {
  87. return loader;
  88. }
  89. void ImageLoader::cleanup() {
  90. while (loader.size()) {
  91. remove_image_format_loader(loader[0]);
  92. }
  93. }
  94. /////////////////
  95. RES ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error) {
  96. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  97. if (!f) {
  98. if (r_error) {
  99. *r_error = ERR_CANT_OPEN;
  100. }
  101. return RES();
  102. }
  103. uint8_t header[4] = { 0, 0, 0, 0 };
  104. f->get_buffer(header, 4);
  105. bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M';
  106. if (unrecognized) {
  107. memdelete(f);
  108. if (r_error) {
  109. *r_error = ERR_FILE_UNRECOGNIZED;
  110. }
  111. ERR_FAIL_V(RES());
  112. }
  113. String extension = f->get_pascal_string();
  114. int idx = -1;
  115. for (int i = 0; i < ImageLoader::loader.size(); i++) {
  116. if (ImageLoader::loader[i]->recognize(extension)) {
  117. idx = i;
  118. break;
  119. }
  120. }
  121. if (idx == -1) {
  122. memdelete(f);
  123. if (r_error) {
  124. *r_error = ERR_FILE_UNRECOGNIZED;
  125. }
  126. ERR_FAIL_V(RES());
  127. }
  128. Ref<Image> image;
  129. image.instance();
  130. Error err = ImageLoader::loader[idx]->load_image(image, f, false, 1.0);
  131. memdelete(f);
  132. if (err != OK) {
  133. if (r_error) {
  134. *r_error = err;
  135. }
  136. return RES();
  137. }
  138. if (r_error) {
  139. *r_error = OK;
  140. }
  141. return image;
  142. }
  143. void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const {
  144. p_extensions->push_back("image");
  145. }
  146. bool ResourceFormatLoaderImage::handles_type(const String &p_type) const {
  147. return p_type == "Image";
  148. }
  149. String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {
  150. return p_path.get_extension().to_lower() == "image" ? "Image" : String();
  151. }