/**************************************************************************/1/* image_resource_format.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "image_resource_format.h"3132Ref<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) {33Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);34if (f.is_null()) {35if (r_error) {36*r_error = ERR_CANT_OPEN;37}38return Ref<Resource>();39}4041uint8_t header[4] = { 0, 0, 0, 0 };42f->get_buffer(header, 4);4344bool unrecognized = header[0] != 'G' || header[1] != 'D' || header[2] != 'I' || header[3] != 'M';45if (unrecognized) {46if (r_error) {47*r_error = ERR_FILE_UNRECOGNIZED;48}49ERR_FAIL_V(Ref<Resource>());50}5152String extension = f->get_pascal_string();5354int idx = -1;5556for (int i = 0; i < ImageLoader::loader.size(); i++) {57if (ImageLoader::loader[i]->recognize(extension)) {58idx = i;59break;60}61}6263if (idx == -1) {64if (r_error) {65*r_error = ERR_FILE_UNRECOGNIZED;66}67ERR_FAIL_V(Ref<Resource>());68}6970Ref<Image> image;71image.instantiate();7273Error err = ImageLoader::loader.write[idx]->load_image(image, f);7475if (err != OK) {76if (r_error) {77*r_error = err;78}79return Ref<Resource>();80}8182if (r_error) {83*r_error = OK;84}8586return image;87}8889void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const {90p_extensions->push_back("image");91}9293bool ResourceFormatLoaderImage::handles_type(const String &p_type) const {94return p_type == "Image";95}9697String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {98return p_path.get_extension().to_lower() == "image" ? "Image" : String();99}100101102