# How to use mask to remove masked part of Image

**URL:** <https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983>\
**Category:** General JUCE discussion\
**Created:** [September 25, 2021, 1:21pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983 "2021-09-25T13:21:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [September 25, 2021, 1:21pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/1 "2021-09-25T13:21:49Z")

</div>

Hello,  
I know there is parameter `fillAlphaChannelWithCurrentBrush` in `Graphics::drawImage()` to use image as a mask.  
But actually it only uses current `Graphics` colour with alpha from image. But I need to remove all pixels which are under the mask.

How to achieve such kind of masking? Maybe I could use `Path` to create such mask?

For any help great thanks in advance.  
Best Regards

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [September 25, 2021, 1:43pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/2 "2021-09-25T13:43:41Z")

</div>

I’ve tried to use something like that:  
`graphics.reduceClipRegion(maskImage, AffineTransform());`

But it doesn’t work.  
When I just draw image like that:  
`graphics.drawImageAt(maskImage, 0, 0, true);`

Then everything is fine. The problem is it not removing masked pixels, but drawing shape of `maskImage` with current colour.

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [September 25, 2021, 1:49pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/3 "2021-09-25T13:49:22Z")

</div>

if i’m not mistaken, if your path is closed you can tell what’s within and outside

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [September 25, 2021, 2:31pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/4 "2021-09-25T14:31:38Z")

</div>

Sorry if my explanation is not clean. So I try to show it.  
My main `Image` is like that:

 ![01](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/9/906d9d3307e85cf113ed59e278962222094f379a.png)

And my masking `Image`, or `Path` is like that:

 ![02](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/1/1c1c697ce444326f52d08c02901f1846fefff4f3.png)

And I need to achieve that:

 ![03](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/0/00d8ba31e32cc4f09bcecabbe2c3ce4c70a88425.png)

Is it possible with `Juce::Graphics`?

---

<div class="post-metadata">

**Author:** ![matt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/matt/32/18340_2.png) [@matt](https://forum.juce.com/u/matt)\
**Post date:** [September 25, 2021, 5:12pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/5 "2021-09-25T17:12:28Z")

</div>

Try Graphics::reduceClipRegion with a Path.

You may need to set your Path to use non-zero winding.

Matt

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [September 25, 2021, 6:15pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/6 "2021-09-25T18:15:18Z")

</div>

Hmm… Great thanks for your answer. But as I told before I tried with `Graphics::reduceClipRegion(Rectangle<int>(0, 0, 20, 20))`  
So as I understand it should remove from my graph the rectangle 20x20.  
But it doesn’t work.  
So I thought that maybe `x y` coordinates are wrong, maybe `0, 0` is relative to some parent `Component` or anything like that. So to be sure that coordinates are OK, I tried `Graphics.fillRect(Rectangle<int>(0, 0, 20, 20))`  
And it works fine. It draws rectangle 20x20 as I expect.

But `reduceClipRegion` doesn’t work.

I’ve just tried also with `Path` and with no success. I tried both:  
`Path::setUsingNonZeroWinding(true)`  
and  
`Path::setUsingNonZeroWinding(false)`

And both don’t work. So I have no idea what I do wrong.

---

<div class="post-metadata">

**Author:** ![matt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/matt/32/18340_2.png) [@matt](https://forum.juce.com/u/matt)\
**Post date:** [September 25, 2021, 6:46pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/7 "2021-09-25T18:46:22Z")

</div>

> Hmm… Great thanks for your answer. But as I told before I tried with `Graphics::reduceClipRegion(Rectangle<int>(0, 0, 20, 20))`  
> So as I understand it should remove from my graph the rectangle 20x20.  
> But it doesn’t work.

`Graphics::reduceClipRegion(Rectangle<int>(0, 0, 20, 20))` will reduce your clip region to the specified rectangle. So it will not remove that rectangle from your clip region. After that call, your clip region will only be 0, 0, 20, 20.

If you want to remove that rectangle from your clip region, try Graphics::excludeClipRegion.

If you’re not sure, try calling Graphics::getClipRegion immediately after changing the clip region.

Matt

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [September 25, 2021, 7:14pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/8 "2021-09-25T19:14:32Z")

</div>

Great thanks, but all of that just don’t work for me. I don’t want to use `Rectangle`, It was just example.

I want to receive something like I showed on pictures. You’ve told to use `Graphics:: reduceClipRegion()` with path, but I can’t find the way how to use it.

My code is like that:

```auto
void MyComponent::paint(Graphics& g)
{
    g.fillAll(Colours::red);

    float width = getWidth(); // Which is 100
    float height = getHeight(); // Which is 100
    Path path;
    path.addEllipse((width/2) - 20.0f, (height/2) - 20.0f, 40.0f, 40.0f);

    g.reduceClipRegion(path);
}

```

So here I expect to have big red square (100x100), which is totally transparent in a circle in the center of square, and the circle radius is 20.

But `g.reduceClipRegion(path);` does nothing. It doesn’t matter if I use it or I delete it I still have square which is fully filled with red colour.

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [September 25, 2021, 7:25pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/9 "2021-09-25T19:25:31Z")

</div>

First you set the clip path, THEN you draw the square. Right now you fill first and then set a clip-path after. That’s the wrong way around.

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [September 25, 2021, 7:57pm UTC](https://forum.juce.com/t/how-to-use-mask-to-remove-masked-part-of-image/47983/10 "2021-09-25T19:57:11Z")

</div>

For me?  
You are genius. 😙  
To be honest I couldn’t find it works because I use:  
`Image myImage`  
`Graphics myGraphics(myImage)`  
And later in the code I use:  
`const Image::BitmapData destData (myImage, Image::BitmapData::writeOnly);`  
And then set all pixels to something.  
So it doesn’t matter if I use `myGraphics.reduceClipRegion` before or after `destData.setPixelColour()`

But now I know how to solve it. Thanks all of you.
