uc::apng::loader is a header only C++11 APNG (Animated PNG) decoder.
- C++11 support compiler
- stb_image.h
// apng2pngs.cpp
#define STB_IMAGE_IMPLEMENTATION
#include "uc_apng_loader.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
int main(int argc, char** argv)
{
if (argc < 2) {
std::cerr << "Usage : " << argv[0] << " [APNG filename]" << std::endl;
return 1;
}
try {
auto loader = uc::apng::create_file_loader(argv[1]);
while (loader.has_frame()) {
auto frame = loader.next_frame();
std::ostringstream filename;
filename << "out" << std::setw(3) << std::setfill('0') << frame.index << ".png";
stbi_write_png(filename.str().c_str(), frame.image.width(), frame.image.height(),
4, frame.image.data(), frame.image.width() * 4);
}
} catch (std::exception& ex) {
std::cout << "failed : " << ex.what() << std::endl;
}
return 0;
}
$ g++ -std=c++11 apng2pngs.cpp
// from file
auto loader = uc::apng::create_file_loader("filename.apng");
// from memory (std::string stringdata)
auto loader = uc::apng::create_memory_loader(stringData);
// from memory (const char* buf, size_t buflen)
auto loader = uc::apng::create_memory_loader(buf, buflen);
// member
std::cout << "(" << loader.width() << "x" << loader.height() << "), "
<< loader.num_frames() << "frames, "
<< loader.num_plays() << " times to loop (0 indicates infinite looping).\n";
std::vector<uc::apng::frame> frames;
while (loader.has_frame()) {
frames.push_back(loader.next_frame());
}
for (auto&& frame : frames) {
auto filename = "out" + std::to_string(frame.index) + ".png";
stbi_write_png(filename.c_str(), frame.image.width(), frame.image.height(), 4,
frame.image.data(), frame.image.width() * 4);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frame.image.width(), frame.image.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, frame.image.data());
$ g++ -std=c++11 apng2pngs.cpp
$ ./a.out test_data/beach_ball.apng
$ emcc -std=c++11 -s USE_GLFW=3 emglsample.cpp -o glsample.html --preload-file test_data/beach_ball.apng
$ g++ -std=c++11 test.cpp
$ ./a.out
MIT License