Path: blob/master/modules/gltf/extensions/gltf_document_extension_texture_webp.cpp
10278 views
/**************************************************************************/1/* gltf_document_extension_texture_webp.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 "gltf_document_extension_texture_webp.h"3132// Import process.33Error GLTFDocumentExtensionTextureWebP::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {34if (!p_extensions.has("EXT_texture_webp")) {35return ERR_SKIP;36}37return OK;38}3940Vector<String> GLTFDocumentExtensionTextureWebP::get_supported_extensions() {41Vector<String> ret;42ret.push_back("EXT_texture_webp");43return ret;44}4546Error GLTFDocumentExtensionTextureWebP::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {47if (p_mime_type == "image/webp") {48return r_image->load_webp_from_buffer(p_image_data);49}50return OK;51}5253String GLTFDocumentExtensionTextureWebP::get_image_file_extension() {54return ".webp";55}5657Error GLTFDocumentExtensionTextureWebP::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {58if (!p_texture_json.has("extensions")) {59return OK;60}61const Dictionary &extensions = p_texture_json["extensions"];62if (!extensions.has("EXT_texture_webp")) {63return OK;64}65const Dictionary &texture_webp = extensions["EXT_texture_webp"];66ERR_FAIL_COND_V(!texture_webp.has("source"), ERR_PARSE_ERROR);67r_gltf_texture->set_src_image(texture_webp["source"]);68return OK;69}7071Vector<String> GLTFDocumentExtensionTextureWebP::get_saveable_image_formats() {72Vector<String> ret;73ret.push_back("Lossless WebP");74ret.push_back("Lossy WebP");75return ret;76}7778PackedByteArray GLTFDocumentExtensionTextureWebP::serialize_image_to_bytes(Ref<GLTFState> p_state, Ref<Image> p_image, Dictionary p_image_dict, const String &p_image_format, float p_lossy_quality) {79if (p_image_format == "Lossless WebP") {80p_image_dict["mimeType"] = "image/webp";81return p_image->save_webp_to_buffer(false);82} else if (p_image_format == "Lossy WebP") {83p_image_dict["mimeType"] = "image/webp";84return p_image->save_webp_to_buffer(true, p_lossy_quality);85}86ERR_FAIL_V(PackedByteArray());87}8889Error GLTFDocumentExtensionTextureWebP::save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_file_path, const String &p_image_format, float p_lossy_quality) {90if (p_image_format == "Lossless WebP") {91p_image->save_webp(p_file_path, false);92return OK;93} else if (p_image_format == "Lossy WebP") {94p_image->save_webp(p_file_path, true, p_lossy_quality);95return OK;96}97return ERR_INVALID_PARAMETER;98}99100Error GLTFDocumentExtensionTextureWebP::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {101Dictionary ext_texture_webp;102ext_texture_webp["source"] = p_gltf_texture->get_src_image();103Dictionary texture_extensions;104texture_extensions["EXT_texture_webp"] = ext_texture_webp;105p_texture_json["extensions"] = texture_extensions;106p_state->add_used_extension("EXT_texture_webp", true);107return OK;108}109110111