Rendering bitmaps



It's a lazy Friday, so I'm going to be lazy and ask if anyone knows a better way to render a 1bpp bitmap normally and inverted.

The code below works, but when rendering the inverted bitmap (background and foreground colors swapped) edge pixels of the bitmap are filled in when they shouldn't be. That may be a known "feature" of cairo that I just haven't read about yet.

This used to be trivial with the XOR operator in the GdkGC. Does it have to be so hard in Cairo?

FYI, the 1bpp bitmap was created using cairo_image_surface_create_for_data().

-------------------------------------------------

/*
 * The 'data' parameter is a GtkDrawingArea widget.
 */

static cairo_surface_t *s;

static void
invert_bitmap(GtkWidget *w, gpointer data)
{
cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(GTK_WIDGET(data)));
    GtkStyleContext *ctx = gtk_widget_get_style_context(GTK_WIDGET(data));
    GdkRGBA c;

    if (invert) {
        /*
* Display inverted bitmap: background of rectangle in foreground color
         * and bitmap in background color.
         */

gtk_style_context_get_background_color(ctx,GTK_STATE_FLAG_NORMAL,&c);
        cairo_set_operator(cr,CAIRO_OPERATOR_DIFFERENCE);
        cairo_set_source_surface(cr,s,2.0,1.0);
        cairo_set_source_rgb(cr,c.red,c.green,c.blue);
        cairo_rectangle(cr, 1, 1, 13, 22);
        cairo_fill(cr);
    } else {
        /*
         * Show normal bitmap while clearing the background behind it.
         */
        cairo_set_operator(cr,CAIRO_OPERATOR_DIFFERENCE);
        cairo_mask_surface(cr,s,2.0,1.0);
        gtk_render_background(ctx,cr,1,1,13,22);
    }

    cairo_destroy(cr);
    invert = !invert;
}

--
Mark Leisher


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]