Path: blob/master/editor/import/resource_importer_image.cpp
9896 views
/**************************************************************************/1/* resource_importer_image.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 "resource_importer_image.h"3132#include "core/io/file_access.h"33#include "core/io/image_loader.h"3435String ResourceImporterImage::get_importer_name() const {36return "image";37}3839String ResourceImporterImage::get_visible_name() const {40return "Image";41}4243void ResourceImporterImage::get_recognized_extensions(List<String> *p_extensions) const {44ImageLoader::get_recognized_extensions(p_extensions);45}4647String ResourceImporterImage::get_save_extension() const {48return "image";49}5051String ResourceImporterImage::get_resource_type() const {52return "Image";53}5455bool ResourceImporterImage::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {56return true;57}5859int ResourceImporterImage::get_preset_count() const {60return 0;61}6263String ResourceImporterImage::get_preset_name(int p_idx) const {64return String();65}6667void ResourceImporterImage::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {68}6970Error ResourceImporterImage::import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {71Ref<FileAccess> f = FileAccess::open(p_source_file, FileAccess::READ);7273ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, "Cannot open file from path '" + p_source_file + "'.");74uint64_t len = f->get_length();7576Vector<uint8_t> data;77data.resize(len);7879f->get_buffer(data.ptrw(), len);8081f = FileAccess::open(p_save_path + ".image", FileAccess::WRITE);82ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_CREATE, "Cannot create file in path '" + p_save_path + ".image'.");8384//save the header GDIM85const uint8_t header[4] = { 'G', 'D', 'I', 'M' };86f->store_buffer(header, 4);87//SAVE the extension (so it can be recognized by the loader later88f->store_pascal_string(p_source_file.get_extension().to_lower());89//SAVE the actual image90f->store_buffer(data.ptr(), len);9192return OK;93}949596