Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make average GIF FPS approximately equal to lottie one #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion example/lottie2gif.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gif.h"
#include <rlottie.h>

#include<cmath>
#include<iostream>
#include<string>
#include<vector>
Expand Down Expand Up @@ -82,14 +83,24 @@ class App {
auto player = rlottie::Animation::loadFromFile(fileName);
if (!player) return help();

const double averageDelay = 100 / player->frameRate();
const double averageDelayFractional = averageDelay - (int)averageDelay;
const int longFrameFrequency = averageDelayFractional > 0 ? (int)round(1 / averageDelayFractional) : 1;

// the most of gif viewers does not properly play gif with duration = 1
const size_t longFrameDuration = (size_t)std::max(2.0, ceil(averageDelay));
const size_t shortFrameDuration = (size_t)std::max(2.0, floor(averageDelay));

auto buffer = std::unique_ptr<uint32_t[]>(new uint32_t[w * h]);
size_t frameCount = player->totalFrame();

GifBuilder builder(gifName.data(), w, h, bgColor);
for (size_t i = 0; i < frameCount ; i++) {
rlottie::Surface surface(buffer.get(), w, h, w * 4);
player->renderSync(i, surface);
builder.addFrame(surface);

const auto currentDelay = i % longFrameFrequency ? shortFrameDuration : longFrameDuration;
builder.addFrame(surface, currentDelay);
}
return result();
}
Expand Down