Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split GIF into frames

Tags:

android

Trying to play animated GIFs on Android here (see Android: How do a display a large animated gif given a url?). It's anything but smooth. On Android 1.5, the Movie.decodeStream()/decodeFile() returns a null. On Android 1.6, with the GIF files I've got, it returns a zero-sized movie with zero duration - clearly not adequate.

The referenced question suggests splitting the GIF into frames (and then rendering these). How exactly do i accomplish that, please?

EDIT: tried opening one of my GIFs with GNU giflib in Windows. It choked on the very first frame. The GIF itself is all right, all browsers (save Android's) display and animate it fine.

EDIT2: worked with giflib 4.1.4. Not sure what was broken in 4.1.6, but there you go. Next step: build giflib for Android via NDK, somehow integrate with the Bitmap class.

EDIT3: giflib it is. In conjuction with Bitmap.copyPixelsFromBuffer. The colors are all askew right now, but the basic design seems workable.

like image 666
Seva Alekseyev Avatar asked Jul 22 '10 19:07

Seva Alekseyev


1 Answers

EDIT: I've migrated my project to Google GifFileDecoder, slightly patched. The MovieView class from the legacy solution remained mostly intact, but the GIF parsing logic is in Java.

Couple points of interest: GifFileDecoder does its own looping. If the animation is looped and you do readFrame() until there are no more frames, this loop will be infinite; the decoder has its own wrap-around logic. Also, the buffer that readFrame() returns is being reused over and over, so don't store the result of readFrame as it is, next time you call readFrame it will be overwritten. Make a deep copy instead. Also, the decoder assumes reading from a file, but can be trivially modified to read from any other stream, sacrificing the looping logic.

Here's the 10 year old answer, for old times' sake:

giflib 4.1.4 and NDK. You have to manually resolve the palette colors in the GIF to match one of Android's ARGBxxxx formats, then pass the pixel array to a Bitmap object via copyPixelsFromBuffer(). If you want transparency and/or animation, you have to go through extension blocks and look for the one with code 0xf9.

Shared my code, along with guidance, here: http://rathertech.blogspot.com/2014/04/splitting-gif-into-frames-on-android.html

like image 91
Seva Alekseyev Avatar answered Oct 06 '22 00:10

Seva Alekseyev