Copy of TImage

Hello,
I am trying to do a copy of an image using the operator “=”. For example:

TImage *img = TImage::Create();
TImage *imgCopy = TImage::Create();
*imgCopy = *img;

This doesn’t work because if I write the two images in two files, the first file contains img but the second one is not created because I receive the error “No image loaded”. I know that this would work if I do img->FromPad(), but I need to avoid to do this because I would need to copy the img only knowing the pointer and not drawing it again.

In attachment there is an example of such a behaviour.
Can you tell if there are other ways to do it?
Thank you.
pad2pngCopy.C (544 Bytes)

This is a problem with the copy constructor when using the base class TImage.
In reality TImage::create is a factory creating a TASImage. You can simplify your example in doing as shown below.

Rene

[code]void pad2pngCopy() {
TCanvas *c = new TCanvas;
TH1F *h = new TH1F(“gaus”, “gaus”, 100, -5, 5);
h->FillRandom(“gaus”, 10000);
h->Draw();

gSystem->ProcessEvents();
TASImage img = (TASImage)TImage::Create();
img->FromPad©;

img->WriteImage("/tmp/canvas.png");
TASImage imgCopy(*img);
imgCopy.WriteImage("/tmp/canvasCopy.png");

delete h;
delete c;
delete img;
}[/code]

Thanks for the answer.
And how can I do if I want imgCopy to be a pointer to TASImage?

TASImage img = (TASImage)TImage::Create();
img->FromPad©;

img->WriteImage("/tmp/canvas.png");

TASImage* imgCopy(img); // this tells me error: cannot convert const TASImage' toTASImage’ in initialization

and
TASImage imgCopy = (TASImage)TImage::Create();
TASImage imgx(*img);
*imgCopy = imgx; / /this tells me: undefined reference to `TASImage::operator=(TASImage const&)’[/i]

Why do you want to complicate your problem and not follow my advice?
What you do is illegal C++

Rene

Hello,
probably there has been a misunderstanding. I put those lines of code to explain what I would like to do but, of course, I knew that they couldn’t work (illegal C++).
Following your advice, how can I get a pointer to the imgCopy instead of the raw one?
Thank you
Cheers,
Adriana

see my previous mail and use the copy constructor, ie instead of

TImage *img = TImage::Create(); TImage *imgCopy = TImage::Create(); *imgCopy = *img;
do

TASImage imgCopy(*img);
Rene