gltf_document_extension_texture_webp.cpp 5.0 KB

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