This repo contains short source code examples in C language using the awesone stb image 👍 for decoding animated GIF & PNG image files.
stb image is part of the great stb library 🤩 from nothings.
Enjoy!
Table of contents
- TL;DR
- Animated GIF short example
- Animated PNG short example (APNG)
- Any questions or comments are welcome 🐦
- gif I did not find a way for decoding animated gif frame by frame with stb image, that is an issue for big animated gif (too long time before displaying the first frame + too much memory consumption).
- apng stb image does not support animated png (apng). I thought apng was supported after the read of the jcredmond/stb_image-apng.c gist but this code requires a patch on top of stb image. You may use instead several png files.
- mjpeg stb image does not support mjpeg. You may use instead several jpeg files or search inside your mjpeg buffer each jpeg EOI before calling the decoder (idea from Creating a Videoplayer for ESP32).
- gif If you encounter a "segmentation fault" with a given gif, you can re-encode it with gifsicle (see stb image issue 1504 for details).
gifsicle -O3 gif_creating_seg_fault_with_stb_image.gif -o gif_ok_with_stb_image.gif
Despite above comments, I really like the awesome stb library😄.
stb image is easy to use for decoding an animated gif. After reading the entire gif file in a memory buffer (fopen/fread/fclose for instance, see src/helpers.c), give this buffer to the stbi function stbi_load_gif_from_memory() that will return a buffer containing on uncompressed gif (see stb_image_gif_example.c).
To build and test this example, use following instructions:
# Get latest stb_image.h from https://github.com/nothings/stb
wget https://raw.githubusercontent.com/nothings/stb/master/stb_image.h -O src/stb_image.h -q
# Download nice animated gif with transparency
# From https://commons.wikimedia.org/wiki/File:Wikipedia_logo_puzzle_globe_spins_horizontally_and_vertically,_revealing_the_contents_of_all_of_its_puzzle_pieces,_without_background.gif
# Credit: Wikipedia, License: Creative Commons Attribution-Share Alike 4.0 International
wget https://upload.wikimedia.org/wikipedia/commons/6/63/Wikipedia_logo_puzzle_globe_spins_horizontally_and_vertically%2C_revealing_the_contents_of_all_of_its_puzzle_pieces%2C_without_background.gif -O wikipedia_logo_puzzle_globe_without_background.gif -q
# From https://commons.wikimedia.org/wiki/File:Rotating_earth_(large)_transparent.gif
# Credit: Wikipedia, License: Creative Commons Attribution-Share Alike 3.0 International
wget https://upload.wikimedia.org/wikipedia/commons/a/a9/Rotating_earth_%28large%29_transparent.gif -O wikipedia_rotating_earth_transparent.gif -q
# Build in release (use "make debug" for step by step debug)
make clean release
build/stb_image_gif_example wikipedia_logo_puzzle_globe_without_background.gif
build/stb_image_gif_example wikipedia_rotating_earth_transparent.gif
IMPORTANT stb image does not support animated png (apng). I thought apng was supported after the read of the jcredmond/stb_image-apng.c gist but this code requires a patch on top of stb image. You may use instead several png files. Ie, in the stb_image_png_example.c, only the first png frame will be decoded and displayed.
To build and test this example, use following instructions:
# Get latest stb_image.h from https://github.com/nothings/stb
wget https://raw.githubusercontent.com/nothings/stb/master/stb_image.h -O src/stb_image.h -q
# Download nice animated png with transparency
# From https://commons.wikimedia.org/wiki/File:Animated_PNG_example_bouncing_beach_ball.png
# Credit: Holger Will, License: Public domain
wget https://upload.wikimedia.org/wikipedia/commons/1/14/Animated_PNG_example_bouncing_beach_ball.png -O wikipedia_bouncing_beach_ball.png -q
# Build in release (use "make debug" for step by step debug)
make clean release
build/stb_image_png_example wikipedia_bouncing_beach_ball.png
If you have any comments or questions, feel free to send me an email at coolcornucopia@outlook.com 📧.
--
Peace
coolcornucopia 😄