Copy Image with its transformated Graphics context

Hi there!

Is there a way I can create a copy of an Image maintaining the transformations I applied to its Graphics context?

For example:

auto image = Image(Image::ARGB, width, height, true);
Graphics context(image);
auto& internalContext = context.getInternalContext();
internalContext.addTransform(AffineTransformation::scale(2.0f));

auto newImage = image; // something to mantain internalContext transformation
Graphics newContext(newImage);
// newContext stuff...

Would Image::Image(const Image&) noexcept maintain this transformation?

Thank you!

Image is by default sharing the pixel data, so calling the copy constructor is just creating the Image with the same pixel data.

If you want a copy you need to call createCopy() or on the copy duplicateIfNeeded, which unlinks it from other images, that share the pixel data.

auto image = original.createCopy();
// or
juce::Image image (original);
image.duplicateIfNeeded();

For applying the transform you need to actually render it:

auto image = juce::Image (original.getFormat(), original.getWidth() * 2, original.getHeight() * 2, true);
juce::Graphics g (image);
g.drawImageTransformed (original, juce::AffineTransform::scale (2.0f));

And last but not least for this particular case there is a special function:

auto image = original.rescaled (original.getWidth() * 2, original.getHeight() * 2);

Hope that helps

Thank you for answering!

After some checks I realized that if a duplicate is made with createCopy() or duplicateIfShared() (and it’s shared) the transformations applied to the original image context are not maintained. If I use the copy constructor or the operator=() no duplicate is created (it just shares the pixel data, as you said) so these transformations are maintained. Shouldn’t the duplicates also maintain these transformations?

The transformations are handled and stored in the Graphics object, not the Image itself.
Think of the Graphics as a plotter, and the Image is the sheet of paper it is drawing onto.