4

In the following code, I'm trying to combine some 1024*1024 png into several larger pngs. The code fails with this exception:

Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to [I
    at sun.awt.image.IntegerInterleavedRaster.setDataElements(Unknown Source)
    at java.awt.image.BufferedImage.copyData(Unknown Source)
    at mloc.bs12.mapimagemerger.Merger.main(Merger.java:27)

It's probably something small and silly which I overlooked, but I can't find anything wrong with the code. Code:

import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
public class Merger {
    public static void main(String[] args) {
        String toX, toY, toZ;
        try {
        toX = args[0];
        toY = args[1];
        toZ = args[2];
        } catch(ArrayIndexOutOfBoundsException E) {
            //E.printStackTrace();
            toX = "3";
            toY = "5";
            toZ = "4";
        }
        int yproper = 1;
        for(int z = 1; z <= Integer.parseInt(toZ); z++) {
            BufferedImage img = new BufferedImage(Integer.parseInt(toX) * 1024, Integer.parseInt(toY) * 1024, BufferedImage.TYPE_INT_RGB);
            for(int x = 1; x <= Integer.parseInt(toX); x++) {
                for(int y = 1; y <= Integer.parseInt(toY); y++) {
                    BufferedImage simg = img.getSubimage(x*1024, y*1024, 1024, 1024);
                    BufferedImage tempimg = loadImage(x + "-" + y + "-" + z + ".png");
                    WritableRaster rsimg = simg.getRaster();
                    rsimg = tempimg.copyData(rsimg); <-- Error!
                    yproper++;
                }
            }
            saveImage(img, z + ".png");
        }
    }
    public static BufferedImage loadImage(String path) {
        BufferedImage bimg = null;
        try {

            bimg = ImageIO.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bimg;
    }
    public static void saveImage(BufferedImage img, String path) {
        try {

            ImageIO.write(img, "png", new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }
}

I think I have this figured out by now. The images I was loading were not the same type as the image I created. (I'm still not sure what type they are, what is the 13 type?) I have some more problems, but this error is fixed. (More problems, as in this.)

Mloc
  • 115
  • 1
  • 9
  • @Mloc: just use the `{}` button in the editor to format code. – Mat Apr 16 '11 at 10:17
  • It was all bunching up, for some reason. – Mloc Apr 16 '11 at 10:18
  • Hi @Mloc can you please re-edit the Exception it looks like there is only a part of it. Plus please do tell in the code snippet which line throws the exception. – Boro Apr 16 '11 at 10:20
  • @Mloc The first line of the exception also doesn't look right. Plus also in your code put something like //here we have exception – Boro Apr 16 '11 at 10:31
  • Look here, http://yourimg.in/m/lq63fcd.png – Mloc Apr 16 '11 at 10:34
  • @Mloc this is a weird exception then, because I cannot see B and I classes here. – Boro Apr 16 '11 at 10:37
  • @Boro I presume [B and [I mean byte and int, respectively. – Mloc Apr 16 '11 at 10:41
  • @Mloc yea I think it would have to mean that. Though someone should rewrite this exception's message. :) Sorry I am not sure I can help more here. Just +1 the question maybe someone will come with a solution. – Boro Apr 16 '11 at 10:44
  • @Mloc More guesing here then anything else, but what about using getData() instead of getRaster()? Maybe then it is returned in the same type. – Boro Apr 16 '11 at 10:47
  • This problem has been solved. – Mloc Apr 16 '11 at 11:09
  • @Mloc Please share how did you finally go about it? – Boro Apr 16 '11 at 11:24
  • @Boro The problem seemed to be that the loaded image used a byte format and the created image used a int format. – Mloc Apr 16 '11 at 11:46
  • 1
    @Mloc Ok. What about a solution in terms of coding. How did you code it? – Boro Apr 16 '11 at 11:55
  • @Boro The new code you posted broke it again, whoops. – Mloc Apr 16 '11 at 12:04
  • @Mloc It would be helpful to other readers if you could format your solution as an answer, post it on this question, and then accept it – NateW Jan 31 '18 at 18:18

1 Answers1

3

The library casts a byte array to an int array, which you cannot do.

I am unfamiliar with BufferedImage but a qualified guess would be that the PNG file you read in, is treated as byte values instead of integer values.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347