Re: CAIRO_FORMAT_A1 image source color

Uli Schlachter <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
Hi,

On 28.03.2014 07:01, sthustfo wrote:
> I am using cairopango library to render text on monochrome image on Ubuntu
> 13.10. Now before rendering text, I create a surface of type
> CAIRO_FORMAT_A1, create a cairo context and set the source to solid white.
> However, the color of the image remains black even though I have set the
> color source to solid white. I want to render black color text on white
> background.
> 
> surface = cairo_image_surface_create (CAIRO_FORMAT_A1, 320, 280);
> cr = cairo_create (surface);
> cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
> cairo_paint (cr);
> 
> cairo_surface_write_to_png (surface, filename);
> 
> The saved PNG image always is black color. How do I achieve an image of
> solid white color for a monochrome image (CAIRO_FORMAT_A1)? If suppose, the
> surface is created using CAIRO_FORMAT_ARGB32, then everyrhing is fine.

Please note that CAIRO_FORMAT_A1 and CAIRO_FORMAT_A8 are *not* monochrome
images. They describe images without any colors at all which only contain an
alpha channel. Only when generating a PNG are these images interpreted as
monochrome. The contents of the color components in your call to
cairo_set_source_rgba() won't make any difference at all.

Also note that the default operator (CAIRO_OPERATOR_OVER) doesn't allow to
"remove transparency", it only ever is added.

Attached is a program which generates a PNG with a white and a black pixel. It
uses the operator "SOURCE" to set the alpha-value of the target pixel. I hope
this can help you.

Cheers,
Uli
-- 
No matter how much cats fight, there always seem to be plenty of kittens.

-- 
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
t.c (text/x-csrc, 506 B)
#include <cairo.h>

int main() {
	cairo_surface_t *s = cairo_image_surface_create (CAIRO_FORMAT_A1, 1, 2);
	cairo_t *cr = cairo_create (s);

	cairo_surface_destroy (s);

	cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
	cairo_set_source_rgba (cr, 1, 1, 1, 1);
	cairo_rectangle (cr, 0, 0, 1, 1);
	cairo_fill (cr);

	cairo_set_source_rgba (cr, 1, 1, 1, 0);
	cairo_rectangle (cr, 0, 1, 1, 1);
	cairo_fill (cr);

	cairo_surface_write_to_png (cairo_get_target(cr), "out.png");
	cairo_destroy (cr);
	return 0;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.