Resizing PNG, saving as JPG with JAI

Discussion in 'Java' started by bangsi@gmail.com, Jul 6, 2006.

  1. Guest

    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
     
    , Jul 6, 2006
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    > 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
     
    Oliver Wong, Jul 6, 2006
    #2
    1. Advertising

  3. bthj Guest

    > 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
     
    bthj, Jul 6, 2006
    #3
  4. Oliver Wong Guest

    "bthj" <> wrote in message
    news:...
    > > 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.
    > >

    >
    > 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
     
    Oliver Wong, Jul 6, 2006
    #4
  5. bthj Guest

    wrote:
    > 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
     
    bthj, Jul 18, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Al
    Replies:
    2
    Views:
    546
  2. Roland
    Replies:
    0
    Views:
    1,074
    Roland
    May 12, 2004
  3. Armin Gajda

    JAI - How to convert PNG to TIFF

    Armin Gajda, Mar 6, 2006, in forum: Java
    Replies:
    2
    Views:
    7,922
    Thomas Weidenfeller
    Mar 6, 2006
  4. Armin Gajda
    Replies:
    1
    Views:
    1,528
    Andrey Kuznetsov
    Jun 13, 2006
  5. Replies:
    0
    Views:
    475
Loading...

Share This Page