Copying BufferedImage

Discussion in 'Java' started by Eel, Jul 9, 2003.

  1. Eel

    Eel Guest

    Should the following work? Is there any trick to using getRGB /
    setRGB in BufferedImage? I'm just trying to copy one BufferedImage to
    another. The ultimate goal is to resize, but I just want to copy for
    now.

    BufferedInputStream in = new BufferedInputStream(new
    FileInputStream(input));

    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);

    BufferedImage bi = decoder.decodeAsBufferedImage();

    int imageWidth = bi.getWidth();
    int imageHeight = bi.getHeight();

    BufferedOutputStream out = new BufferedOutputStream(new
    FileOutputStream(output));

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

    BufferedImage bi1 =
    new BufferedImage(imageWidth, imageHeight, bi.getType());
    for (int y = 0; y < imageHeight; y++)
    {
    for (int x = 0; x < imageWidth; x++)
    {
    int rgb = bi.getRGB(x, y);
    bi1.setRGB(x, y, rgb);
    }
    }
    encoder.encode(bi1);
     
    Eel, Jul 9, 2003
    #1
    1. Advertising

  2. Eel:

    >Should the following work?


    Why don't you try? The setRGB/getRGB combination loses some precision
    if the BufferedImage in question contains more than eight bits per
    component, or uses some color model that cannot be perfectly mapped
    from or to RGB.

    [...]

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Jul 9, 2003
    #2
    1. Advertising

  3. Eel

    Eel Guest

    Thanks for responding. Actually the code I posted worked. The reason
    I thought it didn't was that I was using ascii ftp to get test images
    to view. Sorry for the silly error.

    Thanks.

    Marco Schmidt <> wrote in message news:<>...
    > Eel:
    >
    > >Should the following work?

    >
    > Why don't you try? The setRGB/getRGB combination loses some precision
    > if the BufferedImage in question contains more than eight bits per
    > component, or uses some color model that cannot be perfectly mapped
    > from or to RGB.
    >
    > [...]
    >
    > Regards,
    > Marco
     
    Eel, Jul 10, 2003
    #3
    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. Peter Szymanski
    Replies:
    0
    Views:
    903
    Peter Szymanski
    Jul 9, 2003
  2. hz010c1877

    Read PPM images as BufferedImage

    hz010c1877, Aug 14, 2003, in forum: Java
    Replies:
    1
    Views:
    6,990
    John C. Bollinger
    Aug 14, 2003
  3. Morten Nørgaard
    Replies:
    0
    Views:
    3,459
    Morten Nørgaard
    Aug 29, 2003
  4. Mike Westerfield

    Transparent BufferedImage

    Mike Westerfield, Nov 1, 2003, in forum: Java
    Replies:
    1
    Views:
    4,727
    Andrew Thompson
    Nov 1, 2003
  5. Larry Coon

    Creating BufferedImage

    Larry Coon, Nov 9, 2003, in forum: Java
    Replies:
    7
    Views:
    7,216
    Larry Coon
    Nov 11, 2003
Loading...

Share This Page