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);