Author Topic: Gif to pixel data help  (Read 9104 times)

0 Members and 2 Guests are viewing this topic.

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Gif to pixel data help
« on: August 02, 2012, 07:37:58 pm »
I'm trying to add gif support to a certain secret project. Due to the way I'm handling drawing to the screen, I need to convert the gif image data into an array of pixels as well as get the data for how many frames, time between frames, etc. I was wondering if anyone here knows the gif file format and could help me.

Here is the current way I load pixels:

Code: [Select]
private void loadTiles(String name) {
    for (int i = 0; i < tileNames.length; i++) {
try {
    BufferedImage image = ImageIO.read(TileSet.class.getResource("/tiles/" + name + "/" + tileNames[i] + ".gif"));
    Render result = new Render(xPix, yPix);//Render contains the pixel data in result.pixels
    System.out.println(result.width);
    image.getRGB(0, 0, xPix, yPix, result.pixels, 0, xPix);
    tileRender[i] = result;
} catch (Exception e) {
    System.err.println("File: \"" + "tiles/" + name + "/" + tileNames[i] + ".gif" + "\" does not exist");
    throw new RuntimeException(e);
}
    }
}

It seems I could use image.getRGB(), but how would I do so?

Thanks!

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline SpiroH

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 729
  • Rating: +153/-23
    • View Profile
Re: Gif to pixel data help
« Reply #1 on: August 02, 2012, 07:49:44 pm »

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Gif to pixel data help
« Reply #2 on: August 02, 2012, 08:43:43 pm »
Thanks, though is there a way to get the data using BufferedImage? I'd rather use something like that to easily handle everything instead of having to dig out everything directly. I know how I could get all the data into an array, but how do I get the number of frames for each? Or is there a better way to get animation?

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Gif to pixel data help
« Reply #3 on: August 04, 2012, 01:33:02 pm »
Okay, here's an example I made that shows loading a gif and displaying it as an animation.  You should be able to look at this and adapt it for what you need.  If you need help, just ask, or if you want me to try and adapt it for you, I could try, you'll just need to provide a little more info on the code structure of the project =P

Code: [Select]
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import org.w3c.dom.*;

public class GIFTEST {
    public static void main(String[] args) throws IOException {
        File input = new File("cat.gif");
        ImageInputStream stream = ImageIO.createImageInputStream(input);
        Iterator readers = ImageIO.getImageReaders(stream);
        if (!readers.hasNext()) throw new RuntimeException("No image reader found");
        ImageReader reader = (ImageReader)readers.next();
        reader.setInput(stream);
        int num = reader.getNumImages(true);
        GifFrame[] frames = new GifFrame[num];
        for (int i = 0; i < num; i++) {
            frames[i] = new GifFrame();
            BufferedImage image = reader.read(i);
            frames[i].i = image;
            IIOMetadata meta = reader.getImageMetadata(i);
            NodeList nodes = meta.getAsTree(meta.getNativeMetadataFormatName()).getChildNodes();
            for (int x = 0; x < nodes.getLength(); x++) {
                Node n = nodes.item(x);
                if (n.getLocalName().equals("GraphicControlExtension")) {
                    NamedNodeMap atts = n.getAttributes();
                    frames[i].time = Integer.parseInt(atts.getNamedItem("delayTime").getNodeValue());
                    frames[i].dispose = atts.getNamedItem("disposalMethod").getNodeValue();
                    frames[i].wait = Boolean.parseBoolean(atts.getNamedItem("userInputFlag").getNodeValue());
                }
            }
        }
        stream.close();
       
        Frame f = new Frame("GIF Test");
        GifAnimator ga = new GifAnimator(frames);
        ga.setSize(400, 400);
        f.add(ga);
        f.pack();
        f.setVisible(true);
        ga.start();
    }
}

class GifFrame {
    public Image i;
    public int time;
    public String dispose;
    public boolean wait;
}

class GifAnimator extends Canvas {
    private GifFrame[] frames;
    private Thread animator;
    private boolean run = false;
    private int f = 0;
   
    public GifAnimator(GifFrame[] frames) {
        this.frames = frames;
    }
   
    public void start() {
        run = true;
        animator = new Thread() {

            @Override
            public void run() {
                while (run) {
                    for (int i = 0; i < frames.length; i++) {
                        f = i;
                        repaint();
                        try {sleep(frames[i].time * 10);} catch (Exception e) {}
                    }
                }
            }
           
        };
        animator.start();
    }
   
    public void stop() {
        run = false;
    }
   
    @Override
    public void paint(Graphics g) {
        g.drawImage(frames[f].i, 0, 0, null);
    }
   
    @Override
    public void update(Graphics g) {
        paint(g);
    }
}
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Gif to pixel data help
« Reply #4 on: August 04, 2012, 01:46:19 pm »
Okay, here's an example I made that shows loading a gif and displaying it as an animation.  You should be able to look at this and adapt it for what you need.  If you need help, just ask, or if you want me to try and adapt it for you, I could try, you'll just need to provide a little more info on the code structure of the project =P

Code: [Select]
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import org.w3c.dom.*;

public class GIFTEST {
    public static void main(String[] args) throws IOException {
        File input = new File("cat.gif");
        ImageInputStream stream = ImageIO.createImageInputStream(input);
        Iterator readers = ImageIO.getImageReaders(stream);
        if (!readers.hasNext()) throw new RuntimeException("No image reader found");
        ImageReader reader = (ImageReader)readers.next();
        reader.setInput(stream);
        int num = reader.getNumImages(true);
        GifFrame[] frames = new GifFrame[num];
        for (int i = 0; i < num; i++) {
            frames[i] = new GifFrame();
            BufferedImage image = reader.read(i);
            frames[i].i = image;
            IIOMetadata meta = reader.getImageMetadata(i);
            NodeList nodes = meta.getAsTree(meta.getNativeMetadataFormatName()).getChildNodes();
            for (int x = 0; x < nodes.getLength(); x++) {
                Node n = nodes.item(x);
                if (n.getLocalName().equals("GraphicControlExtension")) {
                    NamedNodeMap atts = n.getAttributes();
                    frames[i].time = Integer.parseInt(atts.getNamedItem("delayTime").getNodeValue());
                    frames[i].dispose = atts.getNamedItem("disposalMethod").getNodeValue();
                    frames[i].wait = Boolean.parseBoolean(atts.getNamedItem("userInputFlag").getNodeValue());
                }
            }
        }
        stream.close();
       
        Frame f = new Frame("GIF Test");
        GifAnimator ga = new GifAnimator(frames);
        ga.setSize(400, 400);
        f.add(ga);
        f.pack();
        f.setVisible(true);
        ga.start();
    }
}

class GifFrame {
    public Image i;
    public int time;
    public String dispose;
    public boolean wait;
}

class GifAnimator extends Canvas {
    private GifFrame[] frames;
    private Thread animator;
    private boolean run = false;
    private int f = 0;
   
    public GifAnimator(GifFrame[] frames) {
        this.frames = frames;
    }
   
    public void start() {
        run = true;
        animator = new Thread() {

            @Override
            public void run() {
                while (run) {
                    for (int i = 0; i < frames.length; i++) {
                        f = i;
                        repaint();
                        try {sleep(frames[i].time * 10);} catch (Exception e) {}
                    }
                }
            }
           
        };
        animator.start();
    }
   
    public void stop() {
        run = false;
    }
   
    @Override
    public void paint(Graphics g) {
        g.drawImage(frames[f].i, 0, 0, null);
    }
   
    @Override
    public void update(Graphics g) {
        paint(g);
    }
}

This is perfect, I know exactly how to modify it, I just need to take each frame for each BufferedImage and get the pixel data array with getRGB, get the animation times, and number of frames like you did, and use those to display each frame for however long it needs to be.

Beyond that I'll just need to modify my code to support each drawable entity to have an array of arrays of ints to hold each frame.

I'll modify it to my code tonight, thanks!

Btw, I've been looking all over Google for the solution to this issue and found nothing. Where did you learn the stuff for gif rendering and animation?

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Gif to pixel data help
« Reply #5 on: August 04, 2012, 02:36:30 pm »
http://www.daniweb.com/software-development/java/threads/177666/imageobserver-and-animated-gifs#post811177 Is where I found the code I used in the first part to actually load the frame data, then I saw a mention of metadata somewhere, so I googled that and came up with http://docs.oracle.com/javase/1.4.2/docs/api/javax/imageio/metadata/package-summary.html and therefore http://docs.oracle.com/javase/1.4.2/docs/api/javax/imageio/metadata/doc-files/gif_metadata.html which I used to write the metadata extractor, then I just added a simple animator to use all the read data.
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)