I need to convert JPG images provided by customers to sRGB format (sRGB IEC61966-2.1) to make them ready for web.
I can do it successfully with ImageIO
and BufferedImage
but this operation is really slow:
val srgbSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB)
val colorConversionOperation = new ColorConvertOp(srgbSpace, null)
val converted = colorConversionOperation.filter(inputImage, null)
(already tried providing RenderingHints
to ColorConvertOp
- it doesn't help)
As far as I understand, the culprit is BufferedImage
and I need to work on Raster
s to speed things up:
val iccProfile = ICC_Profile.getInstance(ColorSpace.CS_sRGB)
val iccColorSpace = new ICC_ColorSpace(iccProfile)
val sourceColorSpace = inputImage.getColorModel.getColorSpace
val colorConversionOperation = new ColorConvertOp(sourceColorSpace, iccColorSpace, null)
val converted = colorConversionOperation.filter(inputImage.getRaster, null)
This indeed gives a huge performance boost, but I don't know how to save the Raster
so that the final image contain appropriate Color Space and Color Profile information.
Currently I create the BufferedImage
from Raster
in the following way:
val outImage = new BufferedImage(ColorModel.getRGBdefault, converted, false, null);
When I work with BufferedImage
, the final JPG is saved as:
Color space: RGB
Color profile: sRGB IEC61966-2.1
which is correct in this case.
When I work with Raster
s, the final JPG is saved just as:
Color space: RGB
Which means I have lost Color profile
information. I think it's because I do ColorModel.getRGBdefault
when transforming Raster
to BufferedImage
but I have no idea how to get an instance of ColorModel
for sRGB IEC61966-2.1.