How to clone a java.awt.BufferedImage ?

Discussion in 'Java' started by oliv@linuxmail.org, Mar 11, 2007.

  1. Guest

    hi,
    I have to use a function that returns a java.awt.BufferedImage :

    BufferedImage image=function.method(..);

    I want now to clone the "image" instance (different instance but same
    information).

    // of course it does not work because clone method is not public
    BufferedImage clone=(BufferedImage)image.clone();

    What is the fatest way to do that ?

    I tried this:
    public class MyBufferedImage extends BufferedImage implements
    Cloneable {

    /* I don't know how to do the constructor ?? */
    MyBufferedImage() {
    super(null,null,true,null);
    }
    public Object clone() throws CloneNotSupportedException
    {
    return super.clone();
    }
    }

    and I don't know what to do next with this class
    thanks
    , Mar 11, 2007
    #1
    1. Advertising

  2. Tom Hawtin Guest

    wrote:
    >
    > I want now to clone the "image" instance (different instance but same
    > information).
    >
    > // of course it does not work because clone method is not public
    > BufferedImage clone=(BufferedImage)image.clone();
    >
    > What is the fatest way to do that ?


    > public class MyBufferedImage extends BufferedImage implements
    > Cloneable {


    Cloneable is seriously broken. Shallow copying field for field in a
    class is almost certainly the wrong thing to do for anything more
    complicated than Rectangle.

    A close approximation to clone for BufferedImage is:

    BufferedImage clone = image.getSubimage(
    0, 0, image.getWidth(), image.getHeight()
    );

    That is a shallow clone - it shares image data with the original. Update
    one and the other follows.

    I guess you could create a BufferedImage of the same size, getWraster
    and then copyData on the original. I really don't know the relative
    performance of different ways of using BufferedImage, which probably
    depend upon version of Java, graphics card and drivers anyway.

    Tom Hawtin
    Tom Hawtin, Mar 11, 2007
    #2
    1. Advertising

  3. Guest

    On Mar 11, 4:43 pm, wrote:
    > hi,
    > I have to use a function that returns a java.awt.BufferedImage :
    >
    > BufferedImage image=function.method(..);
    >
    > I want now to clone the "image" instance (different instance but same
    > information).


    String[] pnames = image.getPropertyNames();
    Hashtable<String, Object> cproperties = new Hashtable<String,
    Object>();
    if(pnames != null) {
    for(int i = 0; i < pnames.length; i++) {
    cproperties.put(pnames, image.getProperty(pnames);
    }
    }
    WritableRaster wr = image.getRaster();
    WritableRaster cwr = wr.createCompatibleWritableRaster();
    cwr.setRect(wr);
    BufferedImage cimage = BufferedImage(
    image.getColorModel(), // should be immutable
    cwr,
    image.isRasterPremultiplied(),
    cproperties);
    , Mar 11, 2007
    #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. Thang Nguyen
    Replies:
    0
    Views:
    608
    Thang Nguyen
    Aug 7, 2003
  2. Robert Mark Bram
    Replies:
    2
    Views:
    5,943
    Robert Mark Bram
    Dec 11, 2003
  3. M Smith

    Clone A BufferedImage ?

    M Smith, Apr 3, 2004, in forum: Java
    Replies:
    1
    Views:
    1,948
    Ryan Stewart
    Apr 4, 2004
  4. MaoXuePeng
    Replies:
    1
    Views:
    964
    Andrew Thompson
    Nov 24, 2004
  5. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    706
    lordy
    Jul 7, 2006
Loading...

Share This Page