11 GifBuilder(const std::string &fileName , const uint32_t width,
12 const uint32_t height, const uint32_t delay = 2)
14 GifBegin(&handle, fileName.c_str(), width, height, delay);
16 void addFrame(rlottie::Surface &s, uint32_t delay = 2)
19 GifWriteFrame(&handle,
20 reinterpret_cast<uint8_t *>(s.buffer()),
25 void argbTorgba(rlottie::Surface &s)
27 uint8_t *buffer = reinterpret_cast<uint8_t *>(s.buffer());
28 uint32_t totalBytes = s.height() * s.bytesPerLine();
30 for (uint32_t i = 0; i < totalBytes; i += 4) {
31 unsigned char a = buffer[i+3];
32 // compute only if alpha is non zero
34 unsigned char r = buffer[i+2];
35 unsigned char g = buffer[i+1];
36 unsigned char b = buffer[i];
38 if (a != 255) { //un premultiply
48 // only swizzle r and b
69 int render(uint32_t w, uint32_t h)
71 auto player = rlottie::Animation::loadFromFile(fileName);
72 if (!player) return help();
74 uint32_t* buffer = (uint32_t *) malloc(w * h * 4);
75 size_t frameCount = player->totalFrame();
77 GifBuilder builder(baseName.data(), w, h);
78 for (size_t i = 0; i < frameCount ; i++) {
79 rlottie::Surface surface(buffer, w, h, w * 4);
80 player->renderSync(i, surface);
81 builder.addFrame(surface);
89 int setup(int argc, char **argv)
91 if (argc > 1) fileName = argv[1];
93 if (!fileName) return help();
95 fileName = realpath(fileName, absoloutePath.data());
97 if (!fileName || !jsonFile(fileName) ) return help();
99 baseName = absoloutePath;
100 char *base = basename(baseName.data());
101 snprintf(baseName.data(), baseName.size(), "%s.gif",base);
107 bool jsonFile(const char *filename) {
108 const char *dot = strrchr(filename, '.');
109 if(!dot || dot == filename) return false;
110 return !strcmp(dot + 1, "json");
114 std::cout<<"Generated GIF file : "<<baseName.data()<<std::endl;
119 std::cout<<"Usage: \n lottie2gif [lottieFileName]\n";
124 char *fileName{nullptr};
125 std::array<char, 5000> absoloutePath;
126 std::array<char, 5000> baseName;
130 main(int argc, char **argv)
134 if (app.setup(argc, argv)) return 1;
136 app.render(200, 200);