~ubuntu-branches/ubuntu/saucy/pixman/saucy-security

« back to all changes in this revision

Viewing changes to test/utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Cristau
  • Date: 2009-09-28 18:12:47 UTC
  • mfrom: (1.1.8 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090928181247-3iehog63i50htejf
Tags: 0.16.2-1
* New upstream release (closes: #546849).
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <gtk/gtk.h>
 
2
#include <config.h>
 
3
#include "pixman-private.h"     /* For image->bits.format
 
4
                                 * FIXME: there should probably be public API for this
 
5
                                 */
 
6
#include "utils.h"
 
7
 
 
8
GdkPixbuf *
 
9
pixbuf_from_argb32 (uint32_t *bits,
 
10
                    gboolean has_alpha,
 
11
                    int width,
 
12
                    int height,
 
13
                    int stride)
 
14
{
 
15
    GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
 
16
                                        8, width, height);
 
17
    int p_stride = gdk_pixbuf_get_rowstride (pixbuf);
 
18
    guint32 *p_bits = (guint32 *)gdk_pixbuf_get_pixels (pixbuf);
 
19
    int w, h;
 
20
    
 
21
    for (h = 0; h < height; ++h)
 
22
    {
 
23
        for (w = 0; w < width; ++w)
 
24
        {
 
25
            uint32_t argb = bits[h * (stride / 4) + w];
 
26
            guint r, g, b, a;
 
27
            char *pb = (char *)p_bits;
 
28
 
 
29
            pb += h * p_stride + w * 4;
 
30
 
 
31
            r = (argb & 0x00ff0000) >> 16;
 
32
            g = (argb & 0x0000ff00) >> 8;
 
33
            b = (argb & 0x000000ff) >> 0;
 
34
            a = has_alpha? (argb & 0xff000000) >> 24 : 0xff;
 
35
 
 
36
            if (a)
 
37
            {
 
38
                r = (r * 255) / a;
 
39
                g = (g * 255) / a;
 
40
                b = (b * 255) / a;
 
41
            }
 
42
 
 
43
            if (r > 255) r = 255;
 
44
            if (g > 255) g = 255;
 
45
            if (b > 255) b = 255;
 
46
            
 
47
            pb[0] = r;
 
48
            pb[1] = g;
 
49
            pb[2] = b;
 
50
            pb[3] = a;
 
51
        }
 
52
    }
 
53
    
 
54
    return pixbuf;
 
55
}
 
56
 
 
57
 
 
58
static gboolean
 
59
on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
 
60
{
 
61
    GdkPixbuf *pixbuf = data;
 
62
    
 
63
    gdk_draw_pixbuf (widget->window, NULL,
 
64
                     pixbuf, 0, 0, 0, 0,
 
65
                     gdk_pixbuf_get_width (pixbuf),
 
66
                     gdk_pixbuf_get_height (pixbuf),
 
67
                     GDK_RGB_DITHER_NONE,
 
68
                     0, 0);
 
69
    
 
70
    return TRUE;
 
71
}
 
72
 
 
73
void
 
74
show_image (pixman_image_t *image)
 
75
{
 
76
    GtkWidget *window;
 
77
    GdkPixbuf *pixbuf;
 
78
    int width, height, stride;
 
79
    int argc;
 
80
    char **argv;
 
81
    char *arg0 = g_strdup ("pixman-test-program");
 
82
    gboolean has_alpha;
 
83
    pixman_format_code_t format;
 
84
 
 
85
    argc = 1;
 
86
    argv = (char **)&arg0;
 
87
 
 
88
    gtk_init (&argc, &argv);
 
89
    
 
90
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
91
    width = pixman_image_get_width (image);
 
92
    height = pixman_image_get_height (image);
 
93
    stride = pixman_image_get_stride (image);
 
94
 
 
95
    format = image->bits.format;
 
96
    
 
97
    if (format == PIXMAN_a8r8g8b8)
 
98
        has_alpha = TRUE;
 
99
    else if (format == PIXMAN_x8r8g8b8)
 
100
        has_alpha = FALSE;
 
101
    else
 
102
        g_error ("Can't deal with this format: %x\n", format);
 
103
    
 
104
    pixbuf = pixbuf_from_argb32 (pixman_image_get_data (image), has_alpha,
 
105
                                 width, height, stride);
 
106
    
 
107
    g_signal_connect (window, "expose_event", G_CALLBACK (on_expose), pixbuf);
 
108
    g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
 
109
    
 
110
    gtk_widget_show (window);
 
111
    
 
112
    gtk_main ();
 
113
}