Resizing PNG, saving as JPG with JAI

B

bangsi

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
 
O

Oliver Wong

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
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.

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
 
B

bthj

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
 
O

Oliver Wong

bthj said:
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.

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
 
B

bthj

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?


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top