1+ /* *************************************************************************/
2+ /* streaming_image_loader.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 " streaming_image_loader.h"
32+ #include " core/io/file_access.h"
33+
34+ Ref<Resource> StreamingImageLoader::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) {
35+ Ref<FileAccess> f = FileAccess::open (p_path, FileAccess::READ );
36+ if (f.is_null ()) {
37+ if (r_error) {
38+ *r_error = ERR_CANT_OPEN ;
39+ }
40+ return Ref<Resource>();
41+ }
42+
43+ uint8_t header[4 ] = { 0 , 0 , 0 , 0 };
44+ f->get_buffer (header, 4 );
45+
46+ bool unrecognized = header[0 ] != ' G' || header[1 ] != ' D' || header[2 ] != ' S' || header[3 ] != ' I' ;
47+ if (unrecognized) {
48+ if (r_error) {
49+ *r_error = ERR_FILE_UNRECOGNIZED ;
50+ }
51+ ERR_FAIL_V (Ref<Resource>());
52+ }
53+
54+ Ref<Image> image;
55+ image.instantiate ();
56+
57+ image->create_empty (2 , 2 , false , Image::FORMAT_RGB8 );
58+ image->set_pixel (0 , 0 , Color (1.0 , 0.0 , 0.0 ));
59+ image->set_pixel (1 , 0 , Color (0.0 , 1.0 , 0.0 ));
60+ image->set_pixel (0 , 1 , Color (0.0 , 0.0 , 1.0 ));
61+ image->set_pixel (1 , 1 , Color (1.0 , 1.0 , 1.0 ));
62+
63+ if (r_error) {
64+ *r_error = OK ;
65+ }
66+
67+ return image;
68+ }
69+
70+ void StreamingImageLoader::get_recognized_extensions (List<String> *p_extensions) const {
71+ p_extensions->push_back (" streaming_image" );
72+ }
73+
74+ bool StreamingImageLoader::handles_type (const String &p_type) const {
75+ return p_type == " Image" ;
76+ }
77+
78+ String StreamingImageLoader::get_resource_type (const String &p_path) const {
79+ return p_path.get_extension ().to_lower () == " streaming_image" ? " Image" : String ();
80+ }
0 commit comments