java try to convert planarimage to bufferedimage but colormodel is incompatible

Go To


I have a planarimage that I convert to black and white via some example code I found. I then need to convert it into a BufferedImage for the next code section. But I get the following exception:

java.lang.IllegalArgumentException: The specified ColorModel is incompatible with the image SampleModel. at at at at at at

Here is the code I found (Sun example, I think) that converts to black and white:

ParameterBlock pb = new ParameterBlock();
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {8}, 
PlanarImage src = JAI.create("ColorConvert", pb);

pb = new ParameterBlock();
String opName = null;
opName = "errordiffusion";
LookupTableJAI lut =  new LookupTableJAI(new byte[] {(byte)0x00, (byte)0xff});

// Create a layout containing an IndexColorModel which maps
// zero to zero and unity to 255.
ImageLayout layout = new ImageLayout();
byte[] map = new byte[] {(byte)0x00, (byte)0xff};
cm = new IndexColorModel(1, 2, map, map, map);

// Create a hint containing the layout.
RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT,layout);

// Dither the image.
PlanarImage dst = JAI.create(opName, pb, hints);

Here is what I have tried to convert it to a BufferedImage, in the order I have tried:

//doesn't work
BufferedImage image = dst.getAsBufferedImage();

//thought of using the color model the b&w process did,also doesn't work
byte[] map = new byte[] {(byte)0x00, (byte)0xff};
ColorModel cm = new IndexColorModel(1, 2, map, map, map);
BufferedImage image = new BufferedImage(cm, dst.copyData(), false, null);

//I had the most hope for this one,but same error again
WritableRaster wr = dst.copyData();
ColorModel cm = PlanarImage.createColorModel(wr.getSampleModel());
BufferedImage image = new BufferedImage(cm, wr, false, null);

Can anyone tell me what I am doing wrong?

I have found that even calling dst.getNumBands() will throw this error. Clearly I don't know what I am doing here. More precisely, calling dst.betNumbBands(); will cause this:

java.lang.IllegalArgumentException: The specified ColorModel is incompatible with the image SampleModel. at at at at at at at

2012-04-04 02:20
by Andrew


I think ur problem might be with the colorSpace parameter which dose not match with the source img's colorSpace parameter. U can try this code which implements "bandCombine" opName.

 public void imageToGrayScale()
  double[][] matrix1 = {{ 1./3, 1./3, 1./3, 0 }};

  ParameterBlock pb = new ParameterBlock();

  PlanarImage dst = (PlanarImage) JAI.create("bandCombine",pb,null);

  BufferedImage img = dst.getAsBufferedImage();


Here what happens is ,v r just taking the avg of the three color components (RGB) and putting it in a matrix then adding it to a parameterBlock,which gives u the grayscaleImage.

display() method just displays the image inside a JFrame.

matrix explanation:- The matrix is in the form of :- double[][] matrix = new double[destBands][sourceBands + 1]; sourceBands is important where it contains RGB values and the extra '+1' is for the constant values each of which is added to the respective band of the destination.


2014-06-10 18:19
by Swami
Error : BandCombine: a row of the matrix does not have the correct number of entries . - Mr. P 2016-07-27 10:55