title: java try to convert planarimage to bufferedimage but colormodel is incompatible
body: <p>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:</p>
  
  <p>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)</p>
  
  <p>Here is the code I found (Sun example, I think) that converts to black and white:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Here is what I have tried to convert it to a BufferedImage, in the order I have tried:</p>
  
  <pre><code>//doesn't work
  BufferedImage image = dst.getAsBufferedImage();
  
  //thought of using the color model the b&amp;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);
  </code></pre>
  
  <p>Can anyone tell me what I am doing wrong?  </p>
  
  <p>I have found that even calling <code>dst.getNumBands()</code> will throw this error.  Clearly I don't know what I am doing here. More precisely, calling <code>dst.betNumbBands();</code> will cause this:</p>
  
  <p>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)</p>