[cairo] Converting to 8 bit black and white

Ben ben.lists at bsb.me.uk
Tue Sep 25 08:52:02 PDT 2012

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_set_operator(cp, CAIRO_OPERATOR_ADD);
  cairo_set_source_rgb(cp, DBL_MIN, DBL_MIN, DBL_MIN);
  cairo_set_operator(cp, CAIRO_OPERATOR_COLOR_BURN);

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