Hi,
I'm having trouble resizing PNG images and saving the resized versions
as JPGs, to be displayed as thumbnails in a webapp.
For some PNGs the method I use works fine but for others it doesn't.
Here is what I use:
...
FileSeekableStream fss = new FileSeekableStream(inFile);
RenderedOp image = JAI.create("stream", fss);
...
ParameterBlock params = new ParameterBlock();
params.addSource(image);
params.add(modifier);//x scale factor
params.add(modifier);//y scale factor
params.add(0.0F);//x translate
params.add(0.0F);//y translate
params.add(new InterpolationNearest());
RenderedOp thumb = JAI.create("scale", params);
File outFile = new File("some/filename");
FileOutputStream os = new FileOutputStream(outFile);
ImageIO.write(thumb, "JPEG", os);
os.close();
thumb.dispose();
image.dispose();
fss.close();
...
This works great for converting / resizing other formats to JPGs, like
TIFF, BMP and the same, JPG (didn't have luck with GIFs though, handled
that case separately as GIF -> GIF thumbs, made sense).
Some of the JPG files produced from PNGs by this method don't display
in web browsers but do in other image viewers (though with distorted
colors), some are just zero size and some are just fine.
Any ideas how this can be handled?
/Björn
Oliver Wong - 06 Jul 2006 20:41 GMT
> Hi,
>
> I'm having trouble resizing PNG images and saving the resized versions
> as JPGs, to be displayed as thumbnails in a webapp.
>
> For some PNGs the method I use works fine but for others it doesn't.
[code snipped]
> This works great for converting / resizing other formats to JPGs, like
> TIFF, BMP and the same, JPG (didn't have luck with GIFs though, handled
[quoted text clipped - 3 lines]
> in web browsers but do in other image viewers (though with distorted
> colors), some are just zero size and some are just fine.
Check for a common characteristics in all the failing PNGs that the
non-failing ones don't have. I'd bet it has something to do with alpha
channel being present.
- Oliver
bthj - 06 Jul 2006 21:05 GMT
> Check for a common characteristics in all the failing PNGs that the
> non-failing ones don't have. I'd bet it has something to do with alpha
> channel being present.
>
> - Oliver
Thanks, Oliver.
The common characteristic in the failing PNGs seems to be that their
bit depth is above 24; 24 bit PNGs seem to work fine, 32 and 48 bit
fail.
Are there any parameters or orther settings that could handle this?
/Björn
Oliver Wong - 06 Jul 2006 22:43 GMT
> > Check for a common characteristics in all the failing PNGs that the
> > non-failing ones don't have. I'd bet it has something to do with alpha
[quoted text clipped - 3 lines]
> bit depth is above 24; 24 bit PNGs seem to work fine, 32 and 48 bit
> fail.
Downsample to 24 bits then. There are many algorithms ways to do this,
and I'm not that familiar with JAI, so I don't know if the JAI library comes
with downsampling algorithms.
See http://en.wikipedia.org/wiki/Dither
- Oliver
bthj - 18 Jul 2006 19:57 GMT
> Hi,
>
[quoted text clipped - 38 lines]
>
> Any ideas how this can be handled?
Converting to the sRGB color space before writing solved it for me,
thus:
private static BufferedImage convertToRgb(RenderedOp inImg) {
BufferedImage bufImage = inImg.getAsBufferedImage();
ICC_Profile iccProfile =
ICC_Profile.getInstance(ICC_ColorSpace.CS_sRGB);
ColorSpace cSpace = new ICC_ColorSpace(iccProfile);
ColorConvertOp op = new
ColorConvertOp(bufImage.getColorModel().getColorSpace(), cSpace, null);
BufferedImage newImage = new BufferedImage(bufImage.getWidth(),
bufImage.getHeight(), BufferedImage.OPAQUE);
op.filter(bufImage, newImage);
return newImage;
}
Then instead of
ImageIO.write(thumb, "JPEG", os);
in the code snippet above, check for PNGs before writing and call the
new function:
...
if( bIsFilePng ) {
ImageIO.write( convertToRgb(thumb), "JPEG", os);
} else {
ImageIO.write(thumb, "JPEG", os);
}
/Björn