From d37ed535d10a7c889d0121f24e55ea48a3dcd27e Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Sat, 2 Mar 2024 05:45:12 +0900 Subject: [PATCH] wasm: update the gif conversion function re-implement the gif conversion function with the correct approach. The input data is not reusable as it undergoes modifications during parsing. To address this, the function now creates a backup of the original data for use in GIF conversion. This also resolves issues where the GIF viewport was incorrectly matched. This implementation may be revisited upon the availability of Animation::duplicate(). --- src/bindings/wasm/tvgWasmLottieAnimation.cpp | 43 ++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/bindings/wasm/tvgWasmLottieAnimation.cpp b/src/bindings/wasm/tvgWasmLottieAnimation.cpp index 8058ed00..3cdb25dd 100644 --- a/src/bindings/wasm/tvgWasmLottieAnimation.cpp +++ b/src/bindings/wasm/tvgWasmLottieAnimation.cpp @@ -85,6 +85,9 @@ public: return false; } + //back up for saving + this->data = data; + canvas->clear(true); animation = Animation::gen(); @@ -206,7 +209,10 @@ public: { errorMsg = NoError; - if (!animation) return false; + if (data.empty()) { + errorMsg = "Invalid data"; + return false; + } auto saver = Saver::gen(); if (!saver) { @@ -214,12 +220,42 @@ public: return false; } + //animation to save + auto animation = Animation::gen(); + if (!animation) { + errorMsg = "Invalid animation"; + return false; + } + + if (animation->picture()->load(data.c_str(), data.size(), "lottie", nullptr, false) != Result::Success) { + errorMsg = "load() fail"; + return false; + } + + //gif resolution (600x600) + constexpr float GIF_SIZE = 600; + + //transform + float width, height; + animation->picture()->size(&width, &height); + float scale; + if (width > height) scale = GIF_SIZE / width; + else scale = GIF_SIZE / height; + animation->picture()->size(width * scale, height * scale); + //set a white opaque background auto bg = tvg::Shape::gen(); + if (!bg) { + errorMsg = "Invalid bg"; + return false; + } bg->fill(255, 255, 255, 255); - bg->appendRect(0, 0, width, height); + bg->appendRect(0, 0, GIF_SIZE, GIF_SIZE); - saver->background(std::move(bg)); + if (saver->background(std::move(bg)) != Result::Success) { + errorMsg = "background() fail"; + return false; + } if (saver->save(std::move(animation), "output.gif", 100, 30) != tvg::Result::Success) { errorMsg = "save(), fail"; @@ -282,6 +318,7 @@ private: string errorMsg; unique_ptr canvas = nullptr; unique_ptr animation = nullptr; + string data; uint8_t* buffer = nullptr; uint32_t width = 0; uint32_t height = 0;