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 | |
31 | #ifndef IMAGE_LOADER_H |
32 | #define IMAGE_LOADER_H |
33 | |
34 | #include "core/core_bind.h" |
35 | #include "core/io/file_access.h" |
36 | #include "core/io/image.h" |
37 | #include "core/io/resource_loader.h" |
38 | #include "core/object/gdvirtual.gen.inc" |
39 | #include "core/string/ustring.h" |
40 | #include "core/templates/list.h" |
41 | #include "core/variant/binder_common.h" |
42 | |
43 | class ImageLoader; |
44 | |
45 | class ImageFormatLoader : public RefCounted { |
46 | GDCLASS(ImageFormatLoader, RefCounted); |
47 | |
48 | friend class ImageLoader; |
49 | friend class ResourceFormatLoaderImage; |
50 | |
51 | public: |
52 | enum LoaderFlags { |
53 | FLAG_NONE = 0, |
54 | FLAG_FORCE_LINEAR = 1, |
55 | FLAG_CONVERT_COLORS = 2, |
56 | }; |
57 | |
58 | protected: |
59 | static void _bind_methods(); |
60 | |
61 | 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; |
62 | virtual void get_recognized_extensions(List<String> *p_extensions) const = 0; |
63 | bool recognize(const String &p_extension) const; |
64 | |
65 | public: |
66 | virtual ~ImageFormatLoader() {} |
67 | }; |
68 | |
69 | VARIANT_BITFIELD_CAST(ImageFormatLoader::LoaderFlags); |
70 | |
71 | class ImageFormatLoaderExtension : public ImageFormatLoader { |
72 | GDCLASS(ImageFormatLoaderExtension, ImageFormatLoader); |
73 | |
74 | protected: |
75 | static void _bind_methods(); |
76 | |
77 | public: |
78 | 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; |
79 | virtual void get_recognized_extensions(List<String> *p_extensions) const override; |
80 | |
81 | void add_format_loader(); |
82 | void remove_format_loader(); |
83 | |
84 | GDVIRTUAL0RC(PackedStringArray, _get_recognized_extensions); |
85 | GDVIRTUAL4R(Error, _load_image, Ref<Image>, Ref<FileAccess>, BitField<ImageFormatLoader::LoaderFlags>, float); |
86 | }; |
87 | |
88 | class ImageLoader { |
89 | static Vector<Ref<ImageFormatLoader>> loader; |
90 | friend class ResourceFormatLoaderImage; |
91 | |
92 | protected: |
93 | public: |
94 | static Error load_image(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); |
95 | static void get_recognized_extensions(List<String> *p_extensions); |
96 | static Ref<ImageFormatLoader> recognize(const String &p_extension); |
97 | |
98 | static void add_image_format_loader(Ref<ImageFormatLoader> p_loader); |
99 | static void remove_image_format_loader(Ref<ImageFormatLoader> p_loader); |
100 | |
101 | static void cleanup(); |
102 | }; |
103 | |
104 | class ResourceFormatLoaderImage : public ResourceFormatLoader { |
105 | public: |
106 | 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); |
107 | virtual void get_recognized_extensions(List<String> *p_extensions) const; |
108 | virtual bool handles_type(const String &p_type) const; |
109 | virtual String get_resource_type(const String &p_path) const; |
110 | }; |
111 | |
112 | #endif // IMAGE_LOADER_H |
113 | |