0

I am trying to take a certain section of a certain frame of a gif (we'll call this certain frame myFile1.gif), and replace that same area on the subsequent frames (myFile2.gif and so on) with the specified area from myFile1.gif.

Now, this code actually does modify the correct area on myFile2.gif and the subsequent frames, but for some reason, the specified area on myFile2.gif does not come out as a duplicate of that same area on myFile1.gif. Rather, the area comes out all "wonky". (It looks like it has inverted colors or something and it just looks very pixelated. You can also clearly see that modification was done on that specific area.) Why is this happening?

If it is relevant, the gif file is of the gif89a format, and myFile1.gif and myFile2.gif are both individual frames of the gif (not the whole animation in motion). Furthermore, myFile1.gif and myFile2.gif have the same dimensions (they are both 347 x 875).

File f = myFile1.gif;
BufferedImage img = ImageIO.read(f);
byte[] pixels1 = ((DataBufferByte)img.getRaster().getDataBuffer()).getData();
BufferedImage area = img.getSubimage(104, 361, 158, 85);
String filename = myFile2.gif;
File g = new File(filename);
temp = ImageIO.read(g);
byte[] pixels2 = ((DataBufferByte)temp.getRaster().getDataBuffer()).getData();
for(int j = 0; j < area.getHeight(); j++) {
    for(int k = 0; k < area.getWidth(); k++) {
        int index = (temp.getWidth() * (j + 361)) + (k + 104);
        pixels2[index] = pixels1[index];
    }
}
                
File outputfile = new File(filename);
ImageIO.write(temp, "gif", outputfile);
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Rikudou
  • 145
  • 6
  • `ImageIO` doesn't "really" work with animated gifs, just a FYI. You should also beware that gifs (AFAIK) work with a limited palette, so unless both Gifs are using the same palette, you're not going to get the same results – MadProgrammer Jun 01 '22 at 01:29
  • @MadProgrammer Is there something that you would recommend that does work with animated gifs? Also, all of my frames are coming from the same animation, so could I assume that all the frames are using the same pallette? – Rikudou Jun 01 '22 at 01:37
  • Fun with gifs, for [example](https://stackoverflow.com/questions/18117283/mirroring-animated-gif-on-load-in-java-imageicon/18117326#18117326); [example](https://stackoverflow.com/questions/36682135/java-animated-gifs-go-automatically-partially-transparent/36682907#36682907); [example](https://stackoverflow.com/questions/20318146/java-gif-creator-needs-tweaking/20318211#20318211); [example](https://stackoverflow.com/questions/36671767/java-swing-gif-partly-transparent-when-its-not-supposed-to-be/36683021#36683021) – MadProgrammer Jun 01 '22 at 01:39
  • You might need to do some searching for [`GifSequenceWriter`](https://www.google.com/search?q=GifSequenceWriter&oq=GifSequenceWriter&aqs=chrome.0.69i59j69i60l3.180j0j7&sourceid=chrome&ie=UTF-8) as my original source no longer seems to exist – MadProgrammer Jun 01 '22 at 01:44
  • You may also need to provide a copy of your source images – MadProgrammer Jun 01 '22 at 01:46
  • It's fully possible to have a different palette for each frame of a GIF, see [this fun example](https://en.wikipedia.org/wiki/GIF#/media/File:SmallFullColourGIF.gif) that exploits this to create a GIF with more than 256 colors... – Harald K Jun 02 '22 at 15:37

0 Answers0