Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Texture Gif Frame Decoder


Dextrell
 Share

Recommended Posts

```

package com.pokefrontier.graphic;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;

/**
* Class GifDecoder - Decodes a GIF file into one or more frames.
*
* No copyright asserted on the source code of this class. May be used for
* libgdx based games, however, please give credits to Insenthium Gaming.
* Please forward any corrections to [email protected].
*
* @author Tony Rodriguez.
* @version 1.2 November 12, 2013
*
*/

public class GifDecoder
{

/**
* File read status: No errors.
*/
public static final int STATUS_GL_REDNERABLE = 0;

/**
* File read status: Error decoding file (may be partially decoded)
*/
public static final int STATUS_FORMAT_ERROR = 1;

/**
* File read status: Unable to open source.
*/
public static final int STATUS_GL_RENDER_ERROR = 2;

private int status;

/**
* Current frame from the gif in the texture.
*/
protected Texture currentFrame;

protected Texture targetGif;

/**
* Frames stored in an array.
*/
private Array frames;
protected int frameCount;
protected int delay = 0; // delay in milliseconds

public GifDecoder(Texture texture)
{
targetGif = texture;
intitGL();
}

class TextureFrame
{
Texture frame;
int delay;

private TextureFrame(Texture texture, int delay)
{
frame = texture;
this.delay = delay;
}
}

/**
* Main file parser. Reads GIF content blocks.
*/
protected void readContents()
{
// read GIF file content blocks
boolean done = false;
while (!done)
{
readGLTexture();

if(frameCount < 0)
status = STATUS_FORMAT_ERROR;
if(frameCount > 200)
done = true;
}
}

public void draw(SpriteBatch spriteBatch, int x, int y)
{
spriteBatch.draw(frames.get(1).frame, x, y);
}

/**
* Returns true if an error was encountered during reading/decoding
*/
protected boolean err()
{
return status != STATUS_GL_REDNERABLE;
}

/**
* Initializes or re-initializes reader
*/
protected void intitGL()
{
status = STATUS_GL_REDNERABLE;
frameCount = 0;
frames = new Array();
readContents();
}

/**
* Gets display duration for specified frame.
*
* @param n int index of frame
* @return delay in milliseconds
*/
public int getDelay(int n)
{
//
delay = -1;
if ((n >= 0) && (n < frameCount))
{
delay = ((TextureFrame) frames.get(n)).delay;
}
return delay;
}

/**
* Gets the image contents of frame n.
*
* @return Texture representation of frame, or null if n is invalid.
*/
public Texture getFrame(int n)
{
Texture im = null;
if ((n >= 0) && (n < frameCount))
{
im = ((TextureFrame) frames.get(n)).frame;
}
return im;
}

/**
* Gets the first (or only) image read.
*
* @return Texture containing first frame, or null if none.
*/
public Texture getImage()
{
return getFrame(0);
}

public void readGLTexture()
{
Texture image = null;
image = targetGif;
frameCount++;

frames.add(new TextureFrame(image, delay)); // add image to frame list
}

}

```
Supports openGl 2.0
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...