1/**************************************************************************/
2/* gltf_document_extension_texture_webp.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 "gltf_document_extension_texture_webp.h"
32
33// Import process.
34Error GLTFDocumentExtensionTextureWebP::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
35 if (!p_extensions.has("EXT_texture_webp")) {
36 return ERR_SKIP;
37 }
38 return OK;
39}
40
41Vector<String> GLTFDocumentExtensionTextureWebP::get_supported_extensions() {
42 Vector<String> ret;
43 ret.push_back("EXT_texture_webp");
44 return ret;
45}
46
47Error GLTFDocumentExtensionTextureWebP::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
48 if (p_mime_type == "image/webp") {
49 return r_image->load_webp_from_buffer(p_image_data);
50 }
51 return OK;
52}
53
54String GLTFDocumentExtensionTextureWebP::get_image_file_extension() {
55 return ".webp";
56}
57
58Error GLTFDocumentExtensionTextureWebP::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
59 if (!p_texture_json.has("extensions")) {
60 return OK;
61 }
62 const Dictionary &extensions = p_texture_json["extensions"];
63 if (!extensions.has("EXT_texture_webp")) {
64 return OK;
65 }
66 const Dictionary &texture_webp = extensions["EXT_texture_webp"];
67 ERR_FAIL_COND_V(!texture_webp.has("source"), ERR_PARSE_ERROR);
68 r_gltf_texture->set_src_image(texture_webp["source"]);
69 return OK;
70}
71
72Vector<String> GLTFDocumentExtensionTextureWebP::get_saveable_image_formats() {
73 Vector<String> ret;
74 ret.push_back("Lossless WebP");
75 ret.push_back("Lossy WebP");
76 return ret;
77}
78
79PackedByteArray 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) {
80 if (p_image_format == "Lossless WebP") {
81 p_image_dict["mimeType"] = "image/webp";
82 return p_image->save_webp_to_buffer(false);
83 } else if (p_image_format == "Lossy WebP") {
84 p_image_dict["mimeType"] = "image/webp";
85 return p_image->save_webp_to_buffer(true, p_lossy_quality);
86 }
87 ERR_FAIL_V(PackedByteArray());
88}
89
90Error 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) {
91 if (p_image_format == "Lossless WebP") {
92 p_image->save_webp(p_file_path, false);
93 return OK;
94 } else if (p_image_format == "Lossy WebP") {
95 p_image->save_webp(p_file_path, true, p_lossy_quality);
96 return OK;
97 }
98 return ERR_INVALID_PARAMETER;
99}
100
101Error GLTFDocumentExtensionTextureWebP::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {
102 Dictionary ext_texture_webp;
103 ext_texture_webp["source"] = p_gltf_texture->get_src_image();
104 Dictionary texture_extensions;
105 texture_extensions["EXT_texture_webp"] = ext_texture_webp;
106 p_texture_json["extensions"] = texture_extensions;
107 p_state->add_used_extension("EXT_texture_webp", true);
108 return OK;
109}
110