Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/io/image_resource_format.cpp
45997 views
1
/**************************************************************************/
2
/* image_resource_format.cpp */
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
#include "image_resource_format.h"
32
33
Ref<Resource> ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
34
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
35
if (f.is_null()) {
36
if (r_error) {
37
*r_error = ERR_CANT_OPEN;
38
}
39
return Ref<Resource>();
40
}
41
42
uint8_t header[4] = { 0, 0, 0, 0 };
43
f->get_buffer(header, 4);
44
45
bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M';
46
if (unrecognized) {
47
if (r_error) {
48
*r_error = ERR_FILE_UNRECOGNIZED;
49
}
50
ERR_FAIL_V(Ref<Resource>());
51
}
52
53
String extension = f->get_pascal_string();
54
55
int idx = -1;
56
57
for (int i = 0; i < ImageLoader::loader.size(); i++) {
58
if (ImageLoader::loader[i]->recognize(extension)) {
59
idx = i;
60
break;
61
}
62
}
63
64
if (idx == -1) {
65
if (r_error) {
66
*r_error = ERR_FILE_UNRECOGNIZED;
67
}
68
ERR_FAIL_V(Ref<Resource>());
69
}
70
71
Ref<Image> image;
72
image.instantiate();
73
74
Error err = ImageLoader::loader.write[idx]->load_image(image, f);
75
76
if (err != OK) {
77
if (r_error) {
78
*r_error = err;
79
}
80
return Ref<Resource>();
81
}
82
83
if (r_error) {
84
*r_error = OK;
85
}
86
87
return image;
88
}
89
90
void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const {
91
p_extensions->push_back("image");
92
}
93
94
bool ResourceFormatLoaderImage::handles_type(const String &p_type) const {
95
return p_type == "Image";
96
}
97
98
String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {
99
return p_path.get_extension().to_lower() == "image" ? "Image" : String();
100
}
101
102