image_loader.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*************************************************************************/
  2. /* image_loader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "image_loader.h"
  30. #include "print_string.h"
  31. bool ImageFormatLoader::recognize(const String& p_extension) const {
  32. List<String> extensions;
  33. get_recognized_extensions(&extensions);
  34. for (List<String>::Element *E=extensions.front();E;E=E->next()) {
  35. if (E->get().nocasecmp_to(p_extension.extension())==0)
  36. return true;
  37. }
  38. return false;
  39. }
  40. Error ImageLoader::load_image(String p_file,Image *p_image, FileAccess *p_custom) {
  41. FileAccess *f=p_custom;
  42. if (!f) {
  43. Error err;
  44. f=FileAccess::open(p_file,FileAccess::READ,&err);
  45. if (!f) {
  46. ERR_PRINTS("Error opening file: "+p_file);
  47. return err;
  48. }
  49. }
  50. String extension = p_file.extension();
  51. for (int i=0;i<loader_count;i++) {
  52. if (!loader[i]->recognize(extension))
  53. continue;
  54. Error err = loader[i]->load_image(p_image,f);
  55. if (err!=ERR_FILE_UNRECOGNIZED) {
  56. if (!p_custom)
  57. memdelete(f);
  58. return err;
  59. }
  60. }
  61. if (!p_custom)
  62. memdelete(f);
  63. return ERR_FILE_UNRECOGNIZED;
  64. }
  65. void ImageLoader::get_recognized_extensions(List<String> *p_extensions) {
  66. for (int i=0;i<loader_count;i++) {
  67. loader[i]->get_recognized_extensions(p_extensions);
  68. }
  69. }
  70. bool ImageLoader::recognize(const String& p_extension) {
  71. for (int i=0;i<loader_count;i++) {
  72. if (loader[i]->recognize(p_extension))
  73. return true;
  74. }
  75. return false;
  76. }
  77. ImageFormatLoader *ImageLoader::loader[MAX_LOADERS];
  78. int ImageLoader::loader_count=0;
  79. void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) {
  80. ERR_FAIL_COND(loader_count >=MAX_LOADERS );
  81. loader[loader_count++]=p_loader;
  82. }