Question

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 javax.media.jai.PlanarImage.setImageLayout(PlanarImage.java:535) at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867) at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:888) at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:799) at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867) at javax.media.jai.RenderedOp.copyData(RenderedOp.java:2284)

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

ParameterBlock pb = new ParameterBlock();
pb.addSource(input);
ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] {8}, 
    false, 
    false, 
    Transparency.OPAQUE, 
    DataBuffer.TYPE_BYTE);
pb.add(cm);
PlanarImage src = JAI.create("ColorConvert", pb);

pb = new ParameterBlock();
pb.addSource(src);
String opName = null;
opName = "errordiffusion";
LookupTableJAI lut =  new LookupTableJAI(new byte[] {(byte)0x00, (byte)0xff});
pb.add(lut);
pb.add(KernelJAI.ERROR_FILTER_FLOYD_STEINBERG);


// 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);
layout.setColorModel(cm);

// 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 javax.media.jai.PlanarImage.setImageLayout(PlanarImage.java:535) at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867) at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:888) at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:799) at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867) at javax.media.jai.RenderedOp.getSampleModel(RenderedOp.java:2233) at javax.media.jai.PlanarImage.getNumBands(PlanarImage.java:678)

Was it helpful?

Solution

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();
  pb.addSource(image);
  pb.add(matrix1);

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



  BufferedImage img = dst.getAsBufferedImage();
  display(img);

 }

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.

THIS METHORD WORKS ONLY FOR .JPG FILES.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top