Question

Basically, I am loading spritesheets for a game, but I've hit a bug where the sheets can't be bigger than 256x256, this is obviously a problem for large animation sheets.

The main problem is when I load the image as a BufferedImage through the ImageIO.read() method, then use the getRGB() method from BufferedImage, it outputs to an int[]. This is fine, except for when the int array needs to be bigger than 256x256(as that's the maximum integer value).

In the end the values are being converted from the int[] to an int[][], so the final size of the array isn't an issue, but BufferedImage.getRGB() only outputs to a one dimensional int array, so how can I do this with something bigger than that? Here's a rundown of my code.

This is when I tried to analyze the image in two different pieces with different int[] then put them both into the int[][].

private void load() {
        int width = 0, height = 0;
        int[] pixelsr = null, pixelsrr = null;
        try {
            BufferedImage image = ImageIO.read(new FileInputStream(path));
            width = image.getWidth();
            height = image.getHeight();
            pixels = new int[width][height];
            if (width >= 256 || height >= 256) {
                pixelsr = new int[256 * 256];
                image.getRGB(0, 0, w, h, pixelsr, 0, 256);
                pixelsrr = new int[(width - 256) * (height - 256)];
                image.getRGB(256, 256, width, height, pixelsr, 0, width - 256);
                for (int i = 0; i < pixelsr.length; i++) {
                    for (int ii = 0; ii < 256; ii++) {
                        for (int iii = 0; iii < 256; iii++) {
                            pixels[ii][iii] = pixelsr[ii + iii * 256];
                        }
                    }
                }
                for (int i = 0; i < pixelsrr.length; i++) {
                    for (int ii = 256; ii < width - 256; ii++) {
                        for (int iii = 256; iii < height - 256; iii++) {
                            pixels[ii][iii] = pixelsr[ii + iii * (width - 256)];
                        }
                    }
                }
            } else {
                pixelsr = new int[width * height];
                image.getRGB(0, 0, w, h, pixelsr, 0, width);
                for (int i = 0; i < pixelsr.length; i++) {
                    for (int ii = 0; ii < width; ii++) {
                        for (int iii = 0; iii < height; iii++) {
                            pixels[ii][iii] = pixelsr[ii + iii * width];
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

I've been stuck on this for hours, can't figure out a way to do it, and so far haven't seen anyone else with this problem.

Was it helpful?

Solution

From the doc:

public int[] getRGB(int startX,
           int startY,
           int w,
           int h,
           int[] rgbArray,
           int offset,
           int scansize)

Returns an array of integer pixels in the default RGB color model (TYPE_INT_ARGB) and default sRGB color space, from a portion of the image data.

The w and h parameters let you specify the size of the region you want to retrieve.

Using your two-dimensional array, you can store each column of the image in a different one-dimensional array.

I haven't tested this, but something similar should work.

for(int i = 0; i < width; i++){
    image.getRGB(i, 0, 1, height, pixels[i], 0, stride);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top