Skip to main content

[JAI] IndexColorModel and ComponentColorModel

2 replies [Last post]
Anonymous

Hi list,
I have a beginner question.
All i need is being able to read bmp and png and then writing them as gif.
I am able to read those file formats but i am not able to convert them
to gif. Simply using ImageIO.write with the gif format does not work.

I guess this is due to the different color model and pixel data
storage, thus can anyone suggest me how to overcome this annoying
problem?

Thanks a lot.
Simone

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

There are two aspects to this problem: 1) if your image is three-band RGB you
need to convert it to a 1-band image with an IndexColorModel; and 2) you need
to have JAI-Image I/O Tools installed.

As for item 1, you need to use either the ErrorDiffusion, OrderedDither, or
ColorQuantizer operation. The latter operation can also be used simply to
generate tke lookup table for ErrorDiffusion. Please see the jai-interest
mailing list archives for sample code (search for "Sample Code").

As for item 2, the only GIF writer supplied in Java or JAI distributions is
the one in JAI-Image I/O Tools. I think that this one will be integrated in
some future version of J2SE but I cannot make a statement as to when that will
be.

Brian

On Mon, 18 Apr 2005, simone giannecchini wrote:

> Hi list,
> I have a beginner question.
> All i need is being able to read bmp and png and then writing them as gif.
> I am able to read those file formats but i am not able to convert them
> to gif. Simply using ImageIO.write with the gif format does not work.
>
> I guess this is due to the different color model and pixel data
> storage, thus can anyone suggest me how to overcome this annoying
> problem?

----------------
Brian Burkhalter
Advanced Development, Graphics and Media
Software Chief Technology Office
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

simone giannecchini

Things are even more complicated than you said.

Images I am dealing with are 4 banded (they have transparency band). I
have widely used ImaIO to write down images in various formats, the
problem is always with GIFs.

I wrote the following code but the final image I get is empty and I
get the following exception

<---> Exception <--->
Caused by: java.lang.IllegalArgumentException: Bad value 216!
at com.sun.media.imageioimpl.plugins.gif.GIFImageWriter.getNumBits(GIFImageWriter.java:144)
at com.sun.media.imageioimpl.plugins.gif.GIFImageWriter.write(GIFImageWriter.java:605)
at com.sun.media.imageioimpl.plugins.gif.GIFImageWriter.write(GIFImageWriter.java:460)
at javax.imageio.ImageWriter.write(ImageWriter.java:591)
at javax.imageio.ImageIO.write(ImageIO.java:1434)
at javax.imageio.ImageIO.write(ImageIO.java:1508)
at org.geotools.gce.image.WorldImageWriter.encode(WorldImageWriter.java:482)
... 18 more

<----> Code <---->

/**
* WORKING ON A COLORED IMAGE
*/
surrogateImage = (PlanarImage) ((GridCoverage2D) sourceCoverage)
.getRenderedImage();

//trying to write a GIF
if (surrogateImage.getColorModel() instanceof
ComponentColorModel
&& (((String) (this.format.getWriteParameters()
.parameter("format")
.getValue()))
.compareToIgnoreCase("gif") == 0)) {
//parameter block
ParameterBlock pb = new ParameterBlock();

//check the number of bands looking for alpha band
if (surrogateImage.getSampleModel().getNumBands() > 3) {
//removing alpha band
int w = surrogateImage.getWidth();
int h = surrogateImage.getHeight();

BufferedImage bi = new BufferedImage(w, h,
BufferedImage.TYPE_3BYTE_BGR);

WritableRaster wr = bi.getWritableTile(0, 0);
WritableRaster wr3 = wr.createWritableChild(0, 0, w, h,
0, 0, new int[] { 0, 1, 2 });

wr3.setRect(surrogateImage.getData());
bi.releaseWritableTile(0, 0);
surrogateImage = PlanarImage.wrapRenderedImage(bi);

//PARAMETER BLOCK
pb.removeParameters();
pb.removeSources();

//color map
pb.addSource(surrogateImage);
pb.add(ColorQuantizerDescriptor.MEDIANCUT);
pb.add(255);
LookupTableJAI colorMap =
(LookupTableJAI)JAI.create("ColorQuantizer", pb).getProperty("LUT");
KernelJAI ditherMask =
KernelJAI.ERROR_FILTER_FLOYD_STEINBERG;

//building final color model
int bitsNum = 8;
ColorModel cm = new IndexColorModel(bitsNum,
colorMap.getByteData()[0].length,
colorMap.getByteData()[0],
colorMap.getByteData()[1], colorMap.getByteData()[2],
Transparency.OPAQUE);
PlanarImage op =
PlanarImage.wrapRenderedImage(new BufferedImage(
w, h,
BufferedImage.TYPE_BYTE_INDEXED,(IndexColorModel) cm));
//layout for the final image
ImageLayout layout = new ImageLayout();
layout.setTileWidth(512);
layout.setTileHeight(512);
layout.setColorModel(cm);

// SampleModel sampleModel = cm
// .createCompatibleSampleModel(surrogateImage
// .getWidth(), surrogateImage.getHeight());
layout.setSampleModel(op.getSampleModel());

RenderingHints rh = new
RenderingHints(JAI.KEY_IMAGE_LAYOUT,
layout);

//error diffusion
pb.removeParameters();
pb.removeSources();
pb.addSource(surrogateImage);
pb.add(colorMap);
pb.add(ditherMask);
op= JAI.create("errordiffusion",pb,rh);
surrogateImage=(PlanarImage)op;

. I hope you can tell me something about it:

On 4/18/05, Brian Burkhalter wrote:
> There are two aspects to this problem: 1) if your image is three-band RGB you
> need to convert it to a 1-band image with an IndexColorModel; and 2) you need
> to have JAI-Image I/O Tools installed.
>
> As for item 1, you need to use either the ErrorDiffusion, OrderedDither, or
> ColorQuantizer operation. The latter operation can also be used simply to
> generate tke lookup table for ErrorDiffusion. Please see the jai-interest
> mailing list archives for sample code (search for "Sample Code").
>
> As for item 2, the only GIF writer supplied in Java or JAI distributions is
> the one in JAI-Image I/O Tools. I think that this one will be integrated in
> some future version of J2SE but I cannot make a statement as to when that will
> be.
>
> Brian
>
> On Mon, 18 Apr 2005, simone giannecchini wrote:
>
> > Hi list,
> > I have a beginner question.
> > All i need is being able to read bmp and png and then writing them as gif.
> > I am able to read those file formats but i am not able to convert them
> > to gif. Simply using ImageIO.write with the gif format does not work.
> >
> > I guess this is due to the different color model and pixel data
> > storage, thus can anyone suggest me how to overcome this annoying
> > problem?
>
> ----------------
> Brian Burkhalter
> Advanced Development, Graphics and Media
> Software Chief Technology Office
> Sun Microsystems, Inc.
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> This email message is for the sole use of the intended recipient(s)
> and may contain confidential and privileged information. Any
> unauthorized review, use, disclosure or distribution is prohibited.
> If you are not the intended recipient, please contact the sender by
> reply email and destroy all copies of the original message.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net