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