Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

How to clone a java.awt.BufferedImage ?

Thread view: 
oliv@linuxmail.org - 11 Mar 2007 16:43 GMT
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
Tom Hawtin - 11 Mar 2007 19:20 GMT
> I want now to clone the "image" instance (different instance but same
> information).
[quoted text clipped - 3 lines]
>
> 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
a24900@googlemail.com - 11 Mar 2007 19:48 GMT
On Mar 11, 4:43 pm, o...@linuxmail.org wrote:
> hi,
> I have to use a function that returns a java.awt.BufferedImage :
[quoted text clipped - 3 lines]
> 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[i], image.getProperty(pnames[i]);
   }
}
WritableRaster wr = image.getRaster();
WritableRaster cwr = wr.createCompatibleWritableRaster();
cwr.setRect(wr);
BufferedImage cimage = BufferedImage(
   image.getColorModel(),  // should be immutable
   cwr,
   image.isRasterPremultiplied(),
   cproperties);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.