[cairo] Converting to 8 bit black and white

Timothy Bolton ifoundthetao at gmail.com
Tue Sep 25 09:02:42 PDT 2012


Thanks!  I actually figured it out a while ago, and thought I emailed 
the list (but I only replied to the person who responded to my original 
post)! My apologies, as I would have liked to share this with everyone.

Here's most of the previous email:

I pulled in my png, put it on a surface.
I created a pattern
I set the color
I used the mask.
----
// Assume we already have a surface type
cairo_t *cr = cairo_create(cr);

//PNG to Surface.  filename is the location of the file you want to open
cairo_surface_t *png = cairo_image_surface_create_from_png(filename);

// Pattern pointer to use with the surface
cairo_pattern_t *pattern = cairo_pattern_create_for_surface(png);

//Set the source to black
cairo_set_source_rgb(cr, 0, 0, 0);

//Apply it
cairo_mask(cr, patter);

Hope that helps someone!


'We who cut mere stones must always be envisioning cathedrals.'
Quarry Worker's Creed

On 9/25/2012 10:52 AM, Ben wrote:
> May be too late...
>
> On Tue, 2012-09-18 at 13:36 -0500, Timothy Bolton wrote:
>
>> I have another question.  How do you convert a surface from color to 8
>> bit black and white?  I've been searching and I cannot seem to find a
>> way to convert a surface to black and white.
>>
>> I'm pulling in images, and I need them to be black and white (not gray
>> scale) for the rendering.
> If you use CAIRO_OPERATOR_HSL_COLOR with a grey source you get a
> grey-scale image.  You can then threshold it by adding and burning with
> the darkest non-black source you can construct:
>
>    cairo_t *cp = cairo_create(surf);
>    cairo_set_source_rgb(cp, t, t, t);
>    cairo_set_operator(cp, CAIRO_OPERATOR_HSL_COLOR);
>    cairo_paint(cp);
>    cairo_set_operator(cp, CAIRO_OPERATOR_ADD);
>    cairo_paint(cp);
>    cairo_set_source_rgb(cp, DBL_MIN, DBL_MIN, DBL_MIN);
>    cairo_set_operator(cp, CAIRO_OPERATOR_COLOR_BURN);
>    cairo_paint(cp);
>
> t is the level below which the result will be black.
>
> There may be a neater way to do this, but this method was the first that
> came to mind (I tend to stop once I have any solution).
>



More information about the cairo mailing list