mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-11-20 23:46:02 +00:00
lottie2gif: support set background color as an extra argument.
If lottie contents were designed in white color, gif result is invisible. we can generate gif with a specified bg color. lottie2gif [lottieFileName] lottie2gif [lottieFileName] [bgColor] example: lottie2gif sample.json //white bg color in default. lottie2gif sample.json ff0000 //red bg color
This commit is contained in:
parent
991d231f2d
commit
682bf0c560
@ -9,9 +9,12 @@
|
|||||||
class GifBuilder {
|
class GifBuilder {
|
||||||
public:
|
public:
|
||||||
explicit GifBuilder(const std::string &fileName , const uint32_t width,
|
explicit GifBuilder(const std::string &fileName , const uint32_t width,
|
||||||
const uint32_t height, const uint32_t delay = 2)
|
const uint32_t height, const int bgColor=0xffffffff, const uint32_t delay = 2)
|
||||||
{
|
{
|
||||||
GifBegin(&handle, fileName.c_str(), width, height, delay);
|
GifBegin(&handle, fileName.c_str(), width, height, delay);
|
||||||
|
bgColorR = (uint8_t) ((bgColor & 0xff0000) >> 16);
|
||||||
|
bgColorG = (uint8_t) ((bgColor & 0x00ff00) >> 8);
|
||||||
|
bgColorB = (uint8_t) ((bgColor & 0x0000ff));
|
||||||
}
|
}
|
||||||
~GifBuilder()
|
~GifBuilder()
|
||||||
{
|
{
|
||||||
@ -40,13 +43,12 @@ public:
|
|||||||
unsigned char b = buffer[i];
|
unsigned char b = buffer[i];
|
||||||
|
|
||||||
if (a != 255) { //un premultiply
|
if (a != 255) { //un premultiply
|
||||||
r = (r * 255) / a;
|
unsigned char r2 = (unsigned char) ((float) bgColorR * ((float) (255 - a) / 255));
|
||||||
g = (g * 255) / a;
|
unsigned char g2 = (unsigned char) ((float) bgColorG * ((float) (255 - a) / 255));
|
||||||
b = (b * 255) / a;
|
unsigned char b2 = (unsigned char) ((float) bgColorB * ((float) (255 - a) / 255));
|
||||||
|
buffer[i] = r + r2;
|
||||||
buffer[i] = r;
|
buffer[i+1] = g + g2;
|
||||||
buffer[i+1] = g;
|
buffer[i+2] = b + b2;
|
||||||
buffer[i+2] = b;
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// only swizzle r and b
|
// only swizzle r and b
|
||||||
@ -54,15 +56,16 @@ public:
|
|||||||
buffer[i+2] = b;
|
buffer[i+2] = b;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
buffer[i+2] = 255;
|
buffer[i+2] = bgColorB;
|
||||||
buffer[i+1] = 255;
|
buffer[i+1] = bgColorG;
|
||||||
buffer[i] = 255;
|
buffer[i] = bgColorR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GifWriter handle;
|
GifWriter handle;
|
||||||
|
uint8_t bgColorR, bgColorG, bgColorB;
|
||||||
};
|
};
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
@ -75,7 +78,7 @@ public:
|
|||||||
auto buffer = std::unique_ptr<uint32_t[]>(new uint32_t[w * h]);
|
auto buffer = std::unique_ptr<uint32_t[]>(new uint32_t[w * h]);
|
||||||
size_t frameCount = player->totalFrame();
|
size_t frameCount = player->totalFrame();
|
||||||
|
|
||||||
GifBuilder builder(baseName.data(), w, h);
|
GifBuilder builder(baseName.data(), w, h, bgColor);
|
||||||
for (size_t i = 0; i < frameCount ; i++) {
|
for (size_t i = 0; i < frameCount ; i++) {
|
||||||
rlottie::Surface surface(buffer.get(), w, h, w * 4);
|
rlottie::Surface surface(buffer.get(), w, h, w * 4);
|
||||||
player->renderSync(i, surface);
|
player->renderSync(i, surface);
|
||||||
@ -87,6 +90,7 @@ public:
|
|||||||
int setup(int argc, char **argv)
|
int setup(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if (argc > 1) fileName = argv[1];
|
if (argc > 1) fileName = argv[1];
|
||||||
|
if (argc > 2) bgColor = strtol(argv[2], NULL, 16);
|
||||||
|
|
||||||
if (!fileName) return help();
|
if (!fileName) return help();
|
||||||
|
|
||||||
@ -114,12 +118,13 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int help() {
|
int help() {
|
||||||
std::cout<<"Usage: \n lottie2gif [lottieFileName]\n";
|
std::cout<<"Usage: \n lottie2gif [lottieFileName] [bgColor]\n\nExamples: \n $ lottie2gif input.json\n $ lottie2gif input.json ff00ff\n\n";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
char *fileName{nullptr};
|
char *fileName{nullptr};
|
||||||
|
int bgColor = 0xffffffff;
|
||||||
std::array<char, 5000> absoloutePath;
|
std::array<char, 5000> absoloutePath;
|
||||||
std::array<char, 5000> baseName;
|
std::array<char, 5000> baseName;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user