• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on image assignment

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a buffered image:

BufferedImage origImg;
origImg = someImage.jpg

and I say:

BufferedImage workingImg;
workingImg = origImg;

Is workingImg an independent copy of someImage.jpg or does it simply point to the memory location of origImg?

To make an independent copy, do I have to say:

workingImg = new BufferedImage(origImg.getWidth(), origImg.getHeight(), origImg.getType());
workingImg = origImg;

or do I have to go back to the original source of the image file on the drive?

Thanks for any explanations.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, you can't get a BufferedImage from a filename, so maybe you should start here: http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

To make an independent copy, you can either read the image again from source, or create a new BufferedImage of the same size and draw the already loaded image onto it.
 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Although I did not go into it in my post, I am assigning the image to origImg as:

origImg = ImageIO.read(new File(fn));

My issue is that I need another instance of this image and following your response, I think I will get it by reading the file from disk a second time as:

workingImg = ImageIO.read(new File(fn));

I was concerned that this would be slow (which it isn't) or that there was a more professional way of doing it.

If there are any more comments or suggestions, I am all ears. Otherwise I will mark this topic as closed.

By the way, what does the statement workingImg = origImg actually do?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Sale wrote:what does the statement workingImg = origImg actually do?


It stores the same reference to the image in another variable. So now you have two references to the same image data. Since you said you wanted an independent copy of the image -which I understand to mean that you could change one without changing the other- that is not what you want.
 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. This clarifies a lot for me.
 
reply
    Bookmark Topic Watch Topic
  • New Topic