Graphics::drawImage problem

I think I discovered problem with Graphics::drawImage() method. It doesn’t always respect a given destination rectangle.

Test case:

void MainWinComponent::paint (Graphics& g)
{
    Image testImage (Image::ARGB, 30, 30, false);
    Graphics imgGr (testImage);
    imgGr.setColour (Colours::red);
    imgGr.fillRect (testImage.getBounds())

    // this works
    //g.drawImage(testImage, 0, 0, 20, 20, 0, 0, 10, 10);

    // this also works
    // g.drawImage(testImage, 0, 0, 20, 20, 10, 10, 20, 20);

    // bug: it doesn't draw into "0, 0, 20, 20" rectangle but into somehow shifted one
    g.drawImage(testImage, 0, 0, 20, 20, 10, 10, 10, 10);
}

Sounds like something I fixed a while ago - what juce version is this?

It’s the latest juce from git on WinXP/Win7.

Good catch! That was a bug, and in fixing it, I’ve done something I’ve been meaning to do for a long time, and have added an Image::getCroppedImage() method, so you can create Images that refer to subimages of other ones. That replaces some of the confusing clip rectangle parameters in some graphics methods, and simplifies everything quite nicely. New code is checked in if you want to try it…

Awesome! I can confirm that this bug is fixed.

getCroppedImage() method is also an extremely useful bonus.
BTW: Will duplicateIfShared() method also correctly detach cropped image from the original if needed?

Thanks! Yes, the duplication method should do the right thing, I think!