C++ gif encoder with good quality!
Use Neural-Net quantization algorithm by Anthony Dekker for generating colormap.
Use giflib for encoding gif.
Use stb_image for image loading in demo code.
Just copy gif directory to your project, and include GifEncoder.h
, that's all.
#include "GifEncoder.h"
int main() {
// Suppose that you have three frame to be encoded
const int w = 40;
const int h = 40;
uint32_t frame0[w * h];
uint32_t frame1[w * h];
uint32_t frame2[w * h];
for (int i = 0; i < w * h; ++i) {
frame0[i] = 0xFF0000FF; // red
frame1[i] = 0xFF00FF00; // green
frame2[i] = 0xFFFF0000; // blue
}
int quality = 10;
bool useGlobalColorMap = true;
int loop = 0;
int preAllocSize = useGlobalColorMap ? w * h * 3 * 3 : w * h * 3;
int delay = 20;
GifEncoder gifEncoder;
if (!gifEncoder.open("test.gif", w, h, quality, useGlobalColorMap, loop, preAllocSize)) {
fprintf(stderr, "Error open gif file\n");
return 1;
}
gifEncoder.push(GifEncoder::PIXEL_FORMAT_RGBA, (uint8_t *) frame0, w, h, delay);
gifEncoder.push(GifEncoder::PIXEL_FORMAT_RGBA, (uint8_t *) frame1, w, h, delay);
gifEncoder.push(GifEncoder::PIXEL_FORMAT_RGBA, (uint8_t *) frame2, w, h, delay);
if (!gifEncoder.close()) {
fprintf(stderr, "Error close gif file\n");
return 1;
}
}
More complex demo code is in demo.cpp
.
Use cmake to build demo.
cmake -DCMAKE_BUILD_TYPE=Release -B build .
cmake --build build -- all
./build/egif_demo
Open Visual Studio Command Prompt and run these command
cmake -DCMAKE_BUILD_TYPE=Release -G "NMake Makefiles" -B build .
cmake --build build -- all
.\build\egif_demo.exe
Build it by yourself