How do I convert an image from color to grayscale in JAI?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, September 26, 2001 10:32 AM

My code look something like: ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); RenderedOp ro = JAI.create("fileload", fileName); ColorModel cmRo = ro.getColorModel(); ColorModel cm = RasterFactory.createComponentColorModel( ro.getSampleModel().getDataType(), cs, cmRo.hasAlpha(), cmRo.isAlphaPremultiplied(), cmRo.getTransparency()); // ParameterBlock pb = new ParameterBlock(); pb.addSource(ro); pb.add(cm); ImageLayout layout = new ImageLayout(); layout.setColorModel(cm); layout.setSampleModel(ro.getSampleModel()); RenderingHints rh = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout); ro = JAI.create("col   More>>

My code look something like:

			
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
RenderedOp ro = JAI.create("fileload", fileName);
ColorModel cmRo = ro.getColorModel();
ColorModel cm = RasterFactory.createComponentColorModel(
ro.getSampleModel().getDataType(),
cs, cmRo.hasAlpha(),
cmRo.isAlphaPremultiplied(), cmRo.getTransparency());
//
ParameterBlock pb = new ParameterBlock();
pb.addSource(ro);
pb.add(cm);
ImageLayout layout = new ImageLayout();
layout.setColorModel(cm);
layout.setSampleModel(ro.getSampleModel());
RenderingHints rh = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
ro = JAI.create("colorconvert", pb, rh);
//
for (int x=0; x

and I get:
			
Exception in thread "main" java.lang.IllegalArgumentException: The specified
ColorModel is incompatible with the image SampleModel.
at javax.media.jai.PlanarImage.setImageLayout(PlanarImage.java:493)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:853)
at javax.media.jai.RenderedOp.getWidth(RenderedOp.java:2135)
at Splitter.main(Splitter.java:37)

What do I do wrong? How do I ensure compatibility of ColorModel and SampleModel ?    <<Less

Re: How do I convert an image from color to grayscale in JAI?

Posted By:   Anonymous  
Posted On:   Thursday, September 27, 2001 04:08 AM

I found the solution. Now the code looks like this:

ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
RenderedOp ro = JAI.create("fileload", "../orlandoradar.jpg");
ColorModel cmRo = ro.getColorModel();
ColorModel cm = RasterFactory.createComponentColorModel(
ro.getSampleModel().getDataType(),
cs, cmRo.hasAlpha(),
cmRo.isAlphaPremultiplied(), cmRo.getTransparency());
//
ParameterBlock pb = new ParameterBlock();
pb.addSource(ro);
pb.add(cm);
// changed code:
ImageLayout layout = new ImageLayout();
layout.setColorModel(cm);
layout.setSampleModel(cm.createCompatibleSampleModel(
ro.getWidth(), ro.getHeight()));
RenderingHints rh = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
ro.setRenderingHints(rh);
// end of changed code
ro = JAI.create("colorconvert", pb, rh);

Maybe it can be simplified, especially the ColorModel creation method, but this works fine - I got grayscale image as a result.

Re: How do I convert an image from color to grayscale in JAI?

Posted By:   Reza_Nazarian  
Posted On:   Wednesday, September 26, 2001 01:34 PM

Hi. I had the same problem. I think that the ColorModel of the loaded image is the DirectColorModel. I don't know why, but JAI isn't able to make the colorconvert-operation on such kind of image. i didn't had enough time to solve this problem. Try to change the ColorModel to IndexColorModel or similar before converting. Good Luck, Reza.
About | Sitemap | Contact