Hi!

I´m working with multiband TIF files and some of them have double data type.

I need to modify some pixels from int (0-255) values to double values; problem is that my original image has int values, so it always rounds new values to int. That´s why I want to change to double type.

ParameterBlock pbConvert = new ParameterBlock();
        pbConvert.addSource(myPlanarImage);
        pbConvert.add(DataBuffer.TYPE_DOUBLE);
        PlanarImage im = JAI.create("format", pbConvert);

        WritableRaster wr = (WritableRaster) im.getData();

//make some modifications to wr so it contains double data values
//now I would like to wrap it into a new BufferedImage

              ComponentColorModel colorModel =
	new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_sRGB),
				 null,
				 true,
				 false,
				 ComponentColorModel.TRANSLUCENT,
				 DataBuffer.TYPE_DOUBLE );
      BufferedImage bufImg =
	new BufferedImage (colorModel,
			   wr,
			   false, 
			   null);

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Raster sun.awt.image.SunWritableRaster@1195c2b is incompatible with ColorModel ColorModel: #pixelBits = 256 numComponents = 4 color space = java.awt.color.ICC_ColorSpace@1f217ec transparency = 3 has alpha = true isAlphaPre = false


Please, can you help me to set my raster into a new BufferedImage please?

Thank you very much!!

need to modify some pixels from int (0-255) values to double values

Are pixel values whole numbers in the range 0-255?

Can you edit your post and reenter the text of the error message to preserve the line ends?
What line of code in your program caused the error?

I want to change to double type

Use casting to change int to double:

double dd = (double)intValue;

Finally I made a new BufferedImage step by step:

First, I created a DataBuffer, a SampleModel ant wrapped them into a new Raster.

//pi is my original PlanarImage
DataBuffer data = new DataBufferDouble(datos,
                        pi.getData().getDataBuffer().getSize(),
                        pi.getData().getDataBuffer().getOffsets()[0]);
                int[] bandOffsets = new int[nbands];
                for (int i = 0; i < bandOffsets.length; i++) {
                    bandOffsets[i] = i;
                }
                SampleModel sample = new PixelInterleavedSampleModel(
                        DataBuffer.TYPE_DOUBLE, width, height, nbands, width * nbands, bandOffsets);
                WritableRaster raster = RasterFactory.createWritableRaster(sample, data, null);

Then I worked whith this new WritableRaster filling data with double values.

Finally, created a new ColorModel and a new BufferedImage bi.

ComponentColorModel colorModel =
                        new ComponentColorModel(pi.getColorModel().getColorSpace(),
                        null,
                        pi.getColorModel().hasAlpha(),
                        pi.getColorModel().isAlphaPremultiplied(),
                        pi.getColorModel().getTransparency(),
                        DataBuffer.TYPE_DOUBLE);
                bi =
                        new BufferedImage(colorModel,
                        raster,
                        false, // isRasterPremultiplied
                        null); // properties

The last point is to transform to PlanarImage:

return PlanarImage.wrapRenderedImage(bi);

Well, I had a lot of problems creating the databuffer and samplemodel. I have entered some values just by observing what values my images had in its PlanarImage´s object attributes.

This Web helped me to understand each parameter:

http://javaboutique.internet.com/tutorials/rasters/index2.html

Thanks for your interest. I would appreciate any comment which would give a better alternative and more generic way of passing parameters to the databuffer or samplemodel (bandOffsets, pixelStride, ScanlineStride, numbanks..).

Thank you very much!

commented: Thanks for the explanation +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.