GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Draw onto GdkPixbuf using cairo

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Thu Feb 18, 2010 4:56 pm    Post subject: Draw onto GdkPixbuf using cairo Reply with quote

Hello.

I wrote down a relatively simple application that demonstrates some advanced usage of GdkPixbuf and cairo. Two most interesting functions are documented inside sources. Information about in-memory representation of various images are available:

Hopefully someone will find this useful.

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <gtk/gtk.h>

typedef struct _Data Data;
struct _Data
{
    GtkWidget *box,
              *image,
              *entry;
    GdkPixbuf *pixbuf;
};


static cairo_t   *pixbuf_cairo_create ( GdkPixbuf *pixbuf );
static GdkPixbuf *pixbuf_cairo_destroy( cairo_t   *cr,
                                        gboolean   create_new_pixbuf );
static void       cb_new_image        ( GtkButton *button,
                                        Data      *data );
static void       cb_update_image     ( GtkButton *button,
                                        Data      *data );

/* Key for automated pixbuf updating and destruction */
static const cairo_user_data_key_t pixbuf_key;


int
main( int    argc,
      char **argv )
{
    GtkWidget *window,
              *vbox,
              *swindow,
              *hbox,
              *image,
              *entry,
              *button;
    GdkPixbuf *pixbuf;
    Data      *data;

    gtk_init( &argc, &argv );

    data = g_slice_new( Data );

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );

    vbox = gtk_vbox_new( FALSE, 6 );
    gtk_container_add( GTK_CONTAINER( window ), vbox );

    swindow = gtk_scrolled_window_new( NULL, NULL );
    gtk_box_pack_start( GTK_BOX( vbox ), swindow, TRUE, TRUE, 0 );

    hbox = gtk_hbox_new( TRUE, 6 );
    data->box = hbox;
    gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW( swindow ),
                                           hbox );

    pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, FALSE, 8, 200, 200 );
    gdk_pixbuf_fill( pixbuf, 0xffff00ff );
    data->pixbuf = pixbuf;
    image = gtk_image_new_from_pixbuf( pixbuf );
    data->image = image;
    g_object_unref( G_OBJECT( pixbuf ) );
    gtk_box_pack_start( GTK_BOX( hbox ), image, FALSE, FALSE, 0 );

    hbox = gtk_hbox_new( FALSE, 6 );
    gtk_box_pack_start( GTK_BOX( vbox ), hbox, FALSE, FALSE, 0 );

    entry = gtk_entry_new();
    data->entry = entry;
    gtk_box_pack_start( GTK_BOX( hbox ), entry, TRUE, TRUE, 0 );

    button = gtk_button_new_with_label( "Create new image" );
    g_signal_connect( G_OBJECT( button ), "clicked",
                      G_CALLBACK( cb_new_image ), data );
    gtk_box_pack_start( GTK_BOX( hbox ), button, FALSE, FALSE, 0 );

    button = gtk_button_new_with_label( "Update original image" );
    g_signal_connect( G_OBJECT( button ), "clicked",
                      G_CALLBACK( cb_update_image ), data );
    gtk_box_pack_start( GTK_BOX( hbox ), button, FALSE, FALSE, 0 );

    gtk_widget_show_all( window );

    gtk_main();

    g_slice_free( Data, data );

    return( 0 );
}

/**
 * pixbuf_cairo_create:
 * @pixbuf: GdkPixbuf that you wish to wrap with cairo context
 *
 * This function will initialize new cairo context with contents of @pixbuf. You
 * can then draw using returned context. When finished drawing, you must call
 * pixbuf_cairo_destroy() or your pixbuf will not be updated with new contents!
 *
 * Return value: New cairo_t context. When you're done with it, call
 * pixbuf_cairo_destroy() to update your pixbuf and free memory.
 */
static cairo_t *
pixbuf_cairo_create( GdkPixbuf *pixbuf )
{
    gint             width,        /* Width of both pixbuf and surface */
                     
height,       /* Height of both pixbuf and surface */
                     
p_stride,     /* Pixbuf stride value */
                     
p_n_channels, /* RGB -> 3, RGBA -> 4 */
                     
s_stride;     /* Surface stride value */
   
guchar          *p_pixels,     /* Pixbuf's pixel data */
                   
*s_pixels;     /* Surface's pixel data */
   
cairo_surface_t *surface;      /* Temporary image surface */
   
cairo_t         *cr;           /* Final context */

   
g_object_ref( G_OBJECT( pixbuf ) );

    /* Inspect input pixbuf and create compatible cairo surface */
   
g_object_get( G_OBJECT( pixbuf ), "width",           &width,
                                      "height",          &height,
                                      "rowstride",       &p_stride,
                                      "n-channels",      &p_n_channels,
                                      "pixels",          &p_pixels,
                                      NULL );
    surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, width, height );
    s_stride = cairo_image_surface_get_stride( surface );
    s_pixels = cairo_image_surface_get_data( surface );

    /* Copy pixel data from pixbuf to surface */
   
while( height-- )
    {
        gint    i;
        guchar *p_iter = p_pixels,
               *s_iter = s_pixels;

        for( i = 0; i < width; i++ )
        {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
            /* Pixbuf:  RGB(A)
             * Surface: BGRA */
           
if( p_n_channels == 3 )
            {
                s_iter[0] = p_iter[2];
                s_iter[1] = p_iter[1];
                s_iter[2] = p_iter[0];
                s_iter[3] = 0xff;
            }
            else /* p_n_channels == 4 */
           
{
                gdouble alpha_factor = p_iter[3] / (gdouble)0xff;

                s_iter[0] = (guchar)( p_iter[2] * alpha_factor + .5 );
                s_iter[1] = (guchar)( p_iter[1] * alpha_factor + .5 );
                s_iter[2] = (guchar)( p_iter[0] * alpha_factor + .5 );
                s_iter[3] =           p_iter[3];
            }
#elif G_BYTE_ORDER == G_BIG_ENDIAN
            /* Pixbuf:  RGB(A)
             * Surface: ARGB */
           
if( p_n_channels == 3 )
            {
                s_iter[3] = p_iter[2];
                s_iter[2] = p_iter[1];
                s_iter[1] = p_iter[0];
                s_iter[0] = 0xff;
            }
            else /* p_n_channels == 4 */
           
{
                gdouble alpha_factor = p_iter[3] / (gdouble)0xff;

                s_iter[3] = (guchar)( p_iter[2] * alpha_factor + .5 );
                s_iter[2] = (guchar)( p_iter[1] * alpha_factor + .5 );
                s_iter[1] = (guchar)( p_iter[0] * alpha_factor + .5 );
                s_iter[0] =           p_iter[3];
            }
#else /* PDP endianness */
            /* Pixbuf:  RGB(A)
             * Surface: RABG */
           
if( p_n_channels == 3 )
            {
                s_iter[0] = p_iter[0];
                s_iter[1] = 0xff;
                s_iter[2] = p_iter[2];
                s_iter[3] = p_iter[1];
            }
            else /* p_n_channels == 4 */
           
{
                gdouble alpha_factor = p_iter[3] / (gdouble)0xff;

                s_iter[0] = (guchar)( p_iter[0] * alpha_factor + .5 );
                s_iter[1] =           p_iter[3];
                s_iter[1] = (guchar)( p_iter[2] * alpha_factor + .5 );
                s_iter[2] = (guchar)( p_iter[1] * alpha_factor + .5 );
            }
#endif
           
s_iter += 4;
            p_iter += p_n_channels;
        }
        s_pixels += s_stride;
        p_pixels += p_stride;
    }

    /* Create context and set user data */
   
cr = cairo_create( surface );
    cairo_surface_destroy( surface );
    cairo_set_user_data( cr, &pixbuf_key, pixbuf, g_object_unref );

    /* Return context */
   
return( cr );
}

/**
 * pixbuf_cairo_destroy:
 * @cr: Cairo context that you wish to destroy
 * @create_new_pixbuf: If TRUE, new pixbuf will be created and returned. If
 *                     FALSE, input pixbuf will be updated in place.
 *
 * This function will destroy cairo context, created with pixbuf_cairo_create().
 *
 * Return value: New or updated GdkPixbuf. You own a new reference on return
 * value, so you need to call g_object_unref() on returned pixbuf when you don't
 * need it anymore.
 */
static GdkPixbuf *
pixbuf_cairo_destroy( cairo_t  *cr,
                         gboolean  create_new_pixbuf )
{
    gint             width,        /* Width of both pixbuf and surface */
                     
height,       /* Height of both pixbuf and surface */
                     
p_stride,     /* Pixbuf stride value */
                     
p_n_channels, /* RGB -> 3, RGBA -> 4 */
                     
s_stride;     /* Surface stride value */
   
guchar          *p_pixels,     /* Pixbuf's pixel data */
                   
*s_pixels;     /* Surface's pixel data */
   
cairo_surface_t *surface;      /* Temporary image surface */
   
GdkPixbuf       *pixbuf,       /* Pixbuf to be returned */
                   
*tmp_pix;      /* Temporary storage */

    /* Obtain pixbuf to be returned */
   
tmp_pix = cairo_get_user_data( cr, &pixbuf_key );
    if( create_new_pixbuf )
        pixbuf = gdk_pixbuf_copy( tmp_pix );
    else
       
pixbuf = g_object_ref( G_OBJECT( tmp_pix ) );

    /* Obtain surface from where pixel values will be copied */
   
surface = cairo_get_target( cr );

    /* Inspect pixbuf and surface */
   
g_object_get( G_OBJECT( pixbuf ), "width",           &width,
                                      "height",          &height,
                                      "rowstride",       &p_stride,
                                      "n-channels",      &p_n_channels,
                                      "pixels",          &p_pixels,
                                      NULL );
    s_stride = cairo_image_surface_get_stride( surface );
    s_pixels = cairo_image_surface_get_data( surface );

    /* Copy pixel data from surface to pixbuf */
   
while( height-- )
    {
        gint    i;
        guchar *p_iter = p_pixels,
               *s_iter = s_pixels;

        for( i = 0; i < width; i++ )
        {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
            /* Pixbuf:  RGB(A)
             * Surface: BGRA */
           
gdouble alpha_factor = (gdouble)0xff / s_iter[3];

            p_iter[0] = (guchar)( s_iter[2] * alpha_factor + .5 );
            p_iter[1] = (guchar)( s_iter[1] * alpha_factor + .5 );
            p_iter[2] = (guchar)( s_iter[0] * alpha_factor + .5 );
            if( p_n_channels == 4 )
                p_iter[3] = s_iter[3];
#elif G_BYTE_ORDER == G_BIG_ENDIAN
            /* Pixbuf:  RGB(A)
             * Surface: ARGB */
           
gdouble alpha_factor = (gdouble)0xff / s_iter[0];

            p_iter[0] = (guchar)( s_iter[1] * alpha_factor + .5 );
            p_iter[1] = (guchar)( s_iter[2] * alpha_factor + .5 );
            p_iter[2] = (guchar)( s_iter[3] * alpha_factor + .5 );
            if( p_n_channels == 4 )
                p_iter[3] = s_iter[0];
#else /* PDP endianness */
            /* Pixbuf:  RGB(A)
             * Surface: RABG */
           
gdouble alpha_factor = (gdouble)0xff / s_iter[1];

            p_iter[0] = (guchar)( s_iter[0] * alpha_factor + .5 );
            p_iter[1] = (guchar)( s_iter[3] * alpha_factor + .5 );
            p_iter[2] = (guchar)( s_iter[2] * alpha_factor + .5 );
            if( p_n_channels == 4 )
                p_iter[3] = s_iter[1];
#endif
           
s_iter += 4;
            p_iter += p_n_channels;
        }
        s_pixels += s_stride;
        p_pixels += p_stride;
    }

    /* Destroy context */
   
cairo_destroy( cr );

    /* Return pixbuf */
   
return( pixbuf );
}

static void
cb_new_image( GtkButton *button,
              Data      *data )
{
    GtkWidget   *image;
    GdkPixbuf   *new_pix;
    PangoLayout *layout;
    cairo_t     *cr;

    cr = pixbuf_cairo_create( data->pixbuf );
    cairo_set_source_rgb( cr, 0, 0, 0 );
    cairo_move_to( cr, 0, 0 );
    layout = pango_cairo_create_layout( cr );
    pango_layout_set_text( layout,
                           gtk_entry_get_text( GTK_ENTRY( data->entry ) ), -1 );
    pango_cairo_show_layout( cr, layout );
    g_object_unref( G_OBJECT( layout ) );
    new_pix = pixbuf_cairo_destroy( cr, TRUE );

    image = gtk_image_new_from_pixbuf( new_pix );
    g_object_unref( G_OBJECT( new_pix ) );
    gtk_box_pack_start( GTK_BOX( data->box ), image, FALSE, FALSE, 0 );
    gtk_widget_show( image );
}

static void
cb_update_image( GtkButton *button,
                 Data      *data )
{
    GdkPixbuf   *new_pix;
    PangoLayout *layout;
    cairo_t     *cr;

    cr = pixbuf_cairo_create( data->pixbuf );
    cairo_set_source_rgb( cr, 0, 0, 0 );
    cairo_move_to( cr, 0, 0 );
    layout = pango_cairo_create_layout( cr );
    pango_layout_set_text( layout,
                           gtk_entry_get_text( GTK_ENTRY( data->entry ) ), -1 );
    pango_cairo_show_layout( cr, layout );
    g_object_unref( G_OBJECT( layout ) );
    new_pix = pixbuf_cairo_destroy( cr, FALSE );
    g_object_unref( G_OBJECT( new_pix ) );

    gtk_widget_queue_draw( data->image );
}

Tadej
Back to top
virgoptrex@yahoo.com
Familiar Face


Joined: 03 Feb 2010
Posts: 21

PostPosted: Sat Jun 12, 2010 2:31 pm    Post subject: I tried your code but gave me error Reply with quote

make
g++ -g -Wall `pkg-config --cflags --libs gtk+-2.0` test_cairo_gdk_pixbuf.c -o test_cairo_gdk_pixbuf
test_cairo_gdk_pixbuf.c:22: error: uninitialized const ‘pixbuf_key’
test_cairo_gdk_pixbuf.c: In function ‘GdkPixbuf* pixbuf_cairo_destroy(cairo_t*, gboolean)’:
test_cairo_gdk_pixbuf.c:240: error: invalid conversion from ‘void*’ to ‘GdkPixbuf*’
test_cairo_gdk_pixbuf.c:244: error: invalid conversion from ‘void*’ to ‘GdkPixbuf*’
make: *** [sample] Error 1
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Sat Jun 12, 2010 6:06 pm    Post subject: Reply with quote

Hello.

I compiled this using C compiler and it works just fine. Maybe C++ compiler is causing troubles because it enforces stricter rules? Try compiling using gcc instead of g++ and report back.

Tadej
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP