image_loader.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*************************************************************************/
  2. /* image_loader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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.get_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_count; 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_count; i++) {
  69. loader[i]->get_recognized_extensions(p_extensions);
  70. }
  71. }
  72. bool ImageLoader::recognize(const String &p_extension) {
  73. for (int i = 0; i < loader_count; i++) {
  74. if (loader[i]->recognize(p_extension))
  75. return true;
  76. }
  77. return false;
  78. }
  79. ImageFormatLoader *ImageLoader::loader[MAX_LOADERS];
  80. int ImageLoader::loader_count = 0;
  81. void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) {
  82. ERR_FAIL_COND(loader_count >= MAX_LOADERS);
  83. loader[loader_count++] = p_loader;
  84. }