mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
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().
This commit is contained in:
parent
059939f2ed
commit
d37ed535d1
1 changed files with 40 additions and 3 deletions
|
@ -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<SwCanvas> canvas = nullptr;
|
||||
unique_ptr<Animation> animation = nullptr;
|
||||
string data;
|
||||
uint8_t* buffer = nullptr;
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
|
|
Loading…
Add table
Reference in a new issue