0

I'm creating a mask based on DeviceGray color space based image.

What basically I want to do is to change all sorts of gray (beside black) pixels into white and leave black pixels as they are.

So I want my image to be consisted with black and white pixels.

Any idea how to achieve that using CoreGraphics means ?

Please dont offer running all over the pixels in the loop

deimus
  • 9,565
  • 12
  • 63
  • 107

1 Answers1

2

Use CGImageCreateWithMaskingColors and CGContextSetRGBFillColor together like this:

CGImageRef myMaskedImage;
const CGFloat myMaskingColors[6] = { 0, 124, 0, 68, 0, 0 };
myColorMaskedImage = CGImageCreateWithMaskingColors (image,
                                        myMaskingColors);
CGContextSetRGBFillColor (myContext, 0.6373,0.6373, 0, 1);
CGContextFillRect(context, rect);
CGContextDrawImage(context, rect, myColorMaskedImage);

By the way, the fill color is mentioned at the CGImageCreateWithMaskingColors discussion.

A-Live
  • 8,904
  • 2
  • 39
  • 74
  • I've applied the code provided here on my picture still i get the gray pixels, I need to keep black pixels and convert all sorts on gray pixels into white. – deimus May 23 '12 at 19:25
  • @deimus You should change the color range and the fill color in the snippet, did you do that ? – A-Live May 23 '12 at 19:52
  • hah sorry I'm just too noob yet in Quartz :), Just to confirm is following range right to get all gray colors beside black {1, 255, 1, 255, 1, 255} – deimus May 23 '12 at 20:03
  • That must be correct for RGB, see the formula at the documentation link. – A-Live May 23 '12 at 20:53
  • CGImageCreateWithMaskingColors : what does this actually stands for ? – Shailesh Dec 19 '14 at 14:11
  • @Shailesh Always check [the documentation](https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGImage/index.html) when you have such questions. Actually the answer already has the link where you could find the same information. – A-Live Dec 19 '14 at 14:44