image_loader.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*************************************************************************/
  2. /* image_loader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "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.extension()) == 0)
  37. return true;
  38. }
  39. return false;
  40. }
  41. Error ImageLoader::load_image(String p_file, Image *p_image, FileAccess *p_custom) {
  42. FileAccess *f = p_custom;
  43. if (!f) {
  44. Error err;
  45. f = FileAccess::open(p_file, FileAccess::READ, &err);
  46. if (!f) {
  47. ERR_PRINTS("Error opening file: " + p_file);
  48. return err;
  49. }
  50. }
  51. String extension = p_file.extension();
  52. for (int i = 0; i < loader_count; i++) {
  53. if (!loader[i]->recognize(extension))
  54. continue;
  55. Error err = loader[i]->load_image(p_image, f);
  56. if (err != ERR_FILE_UNRECOGNIZED) {
  57. if (!p_custom)
  58. memdelete(f);
  59. return err;
  60. }
  61. }
  62. if (!p_custom)
  63. memdelete(f);
  64. return ERR_FILE_UNRECOGNIZED;
  65. }
  66. void ImageLoader::get_recognized_extensions(List<String> *p_extensions) {
  67. for (int i = 0; i < loader_count; i++) {
  68. loader[i]->get_recognized_extensions(p_extensions);
  69. }
  70. }
  71. bool ImageLoader::recognize(const String &p_extension) {
  72. for (int i = 0; i < loader_count; i++) {
  73. if (loader[i]->recognize(p_extension))
  74. return true;
  75. }
  76. return false;
  77. }
  78. ImageFormatLoader *ImageLoader::loader[MAX_LOADERS];
  79. int ImageLoader::loader_count = 0;
  80. void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) {
  81. ERR_FAIL_COND(loader_count >= MAX_LOADERS);
  82. loader[loader_count++] = p_loader;
  83. }