image_loader.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**************************************************************************/
  2. /* image_loader.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef IMAGE_LOADER_H
  31. #define IMAGE_LOADER_H
  32. #include "core/core_bind.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/image.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/object/gdvirtual.gen.inc"
  37. #include "core/string/ustring.h"
  38. #include "core/templates/list.h"
  39. #include "core/variant/binder_common.h"
  40. class ImageLoader;
  41. class ImageFormatLoader : public RefCounted {
  42. GDCLASS(ImageFormatLoader, RefCounted);
  43. friend class ImageLoader;
  44. friend class ResourceFormatLoaderImage;
  45. public:
  46. enum LoaderFlags {
  47. FLAG_NONE = 0,
  48. FLAG_FORCE_LINEAR = 1,
  49. FLAG_CONVERT_COLORS = 2,
  50. };
  51. protected:
  52. static void _bind_methods();
  53. virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags = FLAG_NONE, float p_scale = 1.0) = 0;
  54. virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
  55. bool recognize(const String &p_extension) const;
  56. public:
  57. virtual ~ImageFormatLoader() {}
  58. };
  59. VARIANT_BITFIELD_CAST(ImageFormatLoader::LoaderFlags);
  60. class ImageFormatLoaderExtension : public ImageFormatLoader {
  61. GDCLASS(ImageFormatLoaderExtension, ImageFormatLoader);
  62. protected:
  63. static void _bind_methods();
  64. public:
  65. virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags = FLAG_NONE, float p_scale = 1.0) override;
  66. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  67. void add_format_loader();
  68. void remove_format_loader();
  69. GDVIRTUAL0RC(PackedStringArray, _get_recognized_extensions);
  70. GDVIRTUAL4R(Error, _load_image, Ref<Image>, Ref<FileAccess>, BitField<ImageFormatLoader::LoaderFlags>, float);
  71. };
  72. class ImageLoader {
  73. static Vector<Ref<ImageFormatLoader>> loader;
  74. friend class ResourceFormatLoaderImage;
  75. protected:
  76. public:
  77. static Error load_image(const String &p_file, Ref<Image> p_image, Ref<FileAccess> p_custom = Ref<FileAccess>(), BitField<ImageFormatLoader::LoaderFlags> p_flags = ImageFormatLoader::FLAG_NONE, float p_scale = 1.0);
  78. static void get_recognized_extensions(List<String> *p_extensions);
  79. static Ref<ImageFormatLoader> recognize(const String &p_extension);
  80. static void add_image_format_loader(Ref<ImageFormatLoader> p_loader);
  81. static void remove_image_format_loader(Ref<ImageFormatLoader> p_loader);
  82. static void cleanup();
  83. };
  84. class ResourceFormatLoaderImage : public ResourceFormatLoader {
  85. public:
  86. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  87. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  88. virtual bool handles_type(const String &p_type) const override;
  89. virtual String get_resource_type(const String &p_path) const override;
  90. };
  91. #endif // IMAGE_LOADER_H