Jan Tosovsky
Jan Tosovsky

Reputation: 532

Affine transformation in linear RGB space

If the source raster in linear RGB color space is transformed using the following Java code, the java.awt.image.ImagingOpException: Unable to transform src image error is thrown when the filter is applied (the last line).

ColorModel linearRGBColorModel = new DirectColorModel(
        ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB), 32,
        0xff0000, 0xff00, 0xff, 0xff000000, true, DataBuffer.TYPE_INT);

WritableRaster srcRaster = linearRGBColorModel.createCompatibleWritableRaster(100, 100);
WritableRaster dstRaster = linearRGBColorModel.createCompatibleWritableRaster(200, 200);

BufferedImage srcImage = new BufferedImage(linearRGBColorModel, srcRaster, false, null);
BufferedImage dstImage = new BufferedImage(linearRGBColorModel, dstRaster, false, null);

AffineTransform aff = new AffineTransform();
aff.scale(2.0, 2.0);

AffineTransformOp op = new AffineTransformOp(aff, null);
op.filter(srcImage, dstImage);

When ColorSpace.CS_sRGB is used instead, it works properly.

In real case I manipulate image with gray blurred line. Is transformation of such source just missing JDK feature or it doesn't make sense at all?

Anyway, I plan to recalculate pixels to sRGB and make the transformation afterwards.

Upvotes: 1

Views: 699

Answers (1)

Harald K
Harald K

Reputation: 27094

Not really an explanation of why you code doesn't work*, but at least you can easily work around the issue. Instead of filtering the BufferedImages:

op.filter(srcImage, dstImage);

...you could filter the Rasters:

op.filter(srcRaster, dstRaster);

Which will produce the same result (as using filter(BufferedImage, BufferedImage) on two images in sRGB color space).

As long as the color spaces and raster layouts are the same, the type of color space doesn't really matter.


*) I strongly believe this is a Java (JRE) bug, and should be reported to Oracle/OpenJDK.

Upvotes: 2

Related Questions