4

I have a geotiff raster image. I have estracted the WritableRaster, the envelope, and the gridsampleDimension I need to create a new geotiff using Geotools.

GridCoverageFactory gcf = new GridCoverageFactory();
GridCoverage2D image2 = gcf.create(UUID.randomUUID().toString(), raster2, envelope, gridSampleDimensions);
String url = "D:\\ImmaginiRaster\\2015\\" + UUID.randomUUID().toString() + "--.tif";
File file = new File(url);
GeoTiffWriter writer = new GeoTiffWriter(file);
writer.write(image2, null);

And this works perfectly well. Also if I set a random valure for the pixel using

 raster2.setPixel(i, j, [RANDOM]);

But this image is in b/w. I want to create a new image in RGB.

I've tried using and IndexColorModel

int nColors = 8;
Random rn = new Random();
byte b000 = (byte) 0;
byte b255 = (byte) 255;
byte[] reds = new byte[]{b255, b255, b000, b000, b255, b255, b000, b000};
byte[] greens = new byte[]{b255, b255, b255, b255, b000, b000, b000, b000};
byte[] blues = new byte[]{b255, b000, b255, b000, b255, b000, b255, b000};;

IndexColorModel colorModel = new IndexColorModel(3, nColors, reds, greens, blues);
raster2 = colorModel.createCompatibleWritableRaster(w, h);
for (int i = 0; i < w; i++) {//width...

        for (int j = 0; j < h; j++) {
                raster2.setSample(i, j, 0, rn.nextInt(200));
                raster2.setSample(i, j, 1, rn.nextInt(200));
                raster2.setSample(i, j, 2, rn.nextInt(200));
        }
}

I've also tried with

BufferedImage imagex = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
TiledImage tiledImage = new TiledImage(0, 0, w, h, 0, 0, imagex.getSampleModel(), imagex.getColorModel() );
raster2 = tiledImage.getWritableTile(0, 0);

But I receive always a message like this

width = 1304 height = 567 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0 is incompatible with ColorModel ColorModel: #pixelBits = 32 numComponents = 1 color space = ScaledColorSpace[32767.0, 32767.0] transparency = 1 has alpha = false isAlphaPre = false

Where am i wrong ?

  • 1
    try TYPE_INT_RGBA to include transparency – Ian Turton Oct 28 '15 at 15:12
  • Nothing, quite same error IntegerInterleavedRaster: width = 1304 height = 567 #Bands = 4 xOff = 0 yOff = 0 dataOffset[0] 0 is incompatible with ColorModel ColorModel: #pixelBits = 32 numComponents = 1 color space = ScaledColorSpace[32767.0, 32767.0] transparency = 1 has alpha = false isAlphaPre = false I've added also : raster2.setSample(i, j, 3, 100); BufferedImage.TYPE_INT_ARGB – Carnauser Oct 28 '15 at 16:01
1

I can't get my head around IndexColorModel but for a use case like your second one I see the same error with your code but if I create the WritableRaster using this line all works as expected.

WritableRaster raster2 = 
    RasterFactory.createBandedRaster(java.awt.image.DataBuffer.TYPE_INT, w,
    h, 3, null);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.