4

I used the accepted answer's code from this SO question. Now, I want to convert in back to a BufferedImage (preferably not with setRGB()). I've tried this:

private BufferedImage createImage(int[][] pixelData, BufferedImage outputImage){
    final int[] outputImagePixelData = ((DataBufferInt) outputImage.getRaster().getDataBuffer()).getData();
    System.arraycopy(pixelData, 0, outputImagePixelData, 0, pixelData.length);
    return outputImage;
}

but it doesn't work because it takes a 1D array as a parameter, not a 2D array.

Community
  • 1
  • 1
ack
  • 1,181
  • 1
  • 17
  • 36

2 Answers2

2

If your outputImage has already the good type and format, then you can simply do a 2D to 1D conversion using loops (assuming your array encoding is [nbRows][RowLength]):

private BufferedImage createImage(int[][] pixelData, BufferedImage outputImage)
    {
    int[] outputImagePixelData = ((DataBufferInt) outputImage.getRaster().getDataBuffer()).getData() ;

    final int width = outputImage.getWidth() ;
    final int height = outputImage.getHeight() ;

    for (int y=0, pos=0 ; y < height ; y++)
        for (int x=0 ; x < width ; x++, pos++)
            outputImagePixelData[pos] = pixelData[y][x] ;

    return outputImage;
    }

But, the type INT is not really well defined in the BufferedImage. By default, you have TYPE_INT_RGB and TYPE_INT_ARGB, which concatenates the R, G, B, A values of a pixel encoding with a single INT. If you want to create a gray level BufferedImage of type INT with a single channel, then you should do:

private BufferedImage createImage(int[][] pixelData)
    {
    final int width = pixelData[0].length ;
    final int height = pixelData.length ;
    // First I create a BufferedImage with a DataBufferInt, with the appropriate dimensions and number of channels/bands/colors
    ColorSpace myColorSpace = new FloatCS(ColorSpace.TYPE_GRAY, channel) ;
    int[] bits = new int[]{32} ;
    ColorModel myColorModel = new ComponentColorModel(myColorSpace,bits,false,false,ColorModel.OPAQUE,DataBuffer.TYPE_INT) ;
    BufferedImage outputImage = new BufferedImage(myColorModel, myColorModel.createCompatibleWritableRaster(width, height), false, null) ;

    int[] outputImagePixelData = ((DataBufferInt) outputImage.getRaster().getDataBuffer()).getData() ;

    for (int y=0, pos=0 ; y < height ; y++)
        for (int x=0 ; x < width ; x++, pos++)
            outputImagePixelData[pos] = pixelData[y][x] ;

    return outputImage ;
    }

With FloatCS being the ColorSpace class. You have to create you own ColorSpace class when you want specific ColorSpace like Lab, HLS, etc.

public class FloatCS extends ColorSpace
{

private static final long serialVersionUID = -7713114653902159981L;

private ColorSpace rgb = ColorSpace.getInstance(ColorSpace.CS_sRGB) ;

public FloatCS(int type, int channel)
    {
    super(type, channel) ;
    }


@Override
public float[] fromCIEXYZ(float[] pixel)
    {
    return fromRGB(rgb.fromCIEXYZ(pixel)) ;
    }

@Override
public float[] fromRGB(float[] RGB)
    {   
    return RGB ;
    }

@Override
public float[] toCIEXYZ(float[] pixel)
    {
    return rgb.toCIEXYZ(toRGB(pixel)) ;
    }

@Override
public float[] toRGB(float[] nRGB)
    {
    return nRGB ;
    }
}
FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
1

Instead of using System.arraycopy, you can simply use a double for-loop and iterate over x/y, something like this:

private BufferedImage createImage(int[][] pixelData, BufferedImage outputImage) {
    int[] outputImagePixelData = ((DataBufferInt) outputImage.getRaster().getDataBuffer()).getData();

    int width = outputImage.getWidth();
    int height = outputImage.getHeight();

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            outputImagePixelData[y * width + x] = pixelData[x][y];
        }
    }

    return outputImage;
}

The code above assumes that one array element corresponds to one pixel element (which is the case for all of the BufferedImage.TYPE_INT_* types).

Harald K
  • 26,314
  • 7
  • 65
  • 111