~ubuntu-branches/debian/squeeze/pixman/squeeze

« back to all changes in this revision

Viewing changes to test/gradient-test.c

  • Committer: Bazaar Package Importer
  • Author(s): David Nusinow
  • Date: 2007-08-09 22:15:45 UTC
  • Revision ID: james.westby@ubuntu.com-20070809221545-b3rj83wnluotrybv
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <gtk/gtk.h>
 
4
#include "pixman.h"
 
5
 
 
6
GdkPixbuf *
 
7
pixbuf_from_argb32 (uint32_t *bits,
 
8
                    int width,
 
9
                    int height,
 
10
                    int stride)
 
11
{
 
12
    GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
 
13
                                        8, width, height);
 
14
    int p_stride = gdk_pixbuf_get_rowstride (pixbuf);
 
15
    guint32 *p_bits = (guint32 *)gdk_pixbuf_get_pixels (pixbuf);
 
16
    int w, h;
 
17
    
 
18
    for (h = 0; h < height; ++h)
 
19
    {
 
20
        for (w = 0; w < width; ++w)
 
21
        {
 
22
            uint32_t argb = bits[h * stride + w];
 
23
            guint32 abgr;
 
24
            
 
25
            abgr = (argb & 0xff000000) |
 
26
                (argb & 0xff) << 16 |
 
27
                (argb & 0x00ff00) |
 
28
                (argb & 0xff0000) >> 16;
 
29
            
 
30
            p_bits[h * (p_stride / 4) + w] = abgr;
 
31
        }
 
32
    }
 
33
    
 
34
    return pixbuf;
 
35
}
 
36
 
 
37
static gboolean
 
38
on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
 
39
{
 
40
    GdkPixbuf *pixbuf = data;
 
41
    
 
42
    gdk_draw_pixbuf (widget->window, NULL,
 
43
                     pixbuf, 0, 0, 0, 0,
 
44
                     gdk_pixbuf_get_width (pixbuf),
 
45
                     gdk_pixbuf_get_height (pixbuf),
 
46
                     GDK_RGB_DITHER_NONE,
 
47
                     0, 0);
 
48
    
 
49
    return TRUE;
 
50
}
 
51
 
 
52
static void
 
53
show_window (uint32_t *bits, int w, int h, int stride)
 
54
{
 
55
    GdkPixbuf *pixbuf;
 
56
    
 
57
    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
58
    
 
59
    pixbuf = pixbuf_from_argb32 (bits, w, h, stride);
 
60
    
 
61
    g_signal_connect (window, "expose_event", G_CALLBACK (on_expose), pixbuf);
 
62
    g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
 
63
    
 
64
    gtk_widget_show (window);
 
65
    
 
66
    gtk_main ();
 
67
}
 
68
 
 
69
int
 
70
main (int argc, char **argv)
 
71
{
 
72
#define WIDTH 200
 
73
#define HEIGHT 200
 
74
    
 
75
    uint32_t *dest = malloc (WIDTH * HEIGHT * 4);
 
76
    pixman_image_t *src_img;
 
77
    pixman_image_t *dest_img;
 
78
    int i;
 
79
    pixman_gradient_stop_t stops[2] =
 
80
        {
 
81
            { pixman_int_to_fixed (0), { 0xffff, 0x0000, 0x0000, 0xffff } },
 
82
            { pixman_int_to_fixed (1), { 0xffff, 0xffff, 0x0000, 0xffff } }
 
83
        };
 
84
    pixman_point_fixed_t p1 = { 0, 0 };
 
85
    pixman_point_fixed_t p2 = { pixman_int_to_fixed (WIDTH),
 
86
                                pixman_int_to_fixed (HEIGHT) };
 
87
    pixman_transform_t trans = {
 
88
        { { pixman_double_to_fixed (2), pixman_double_to_fixed (0.5), pixman_double_to_fixed (-100), },
 
89
          { pixman_double_to_fixed (0), pixman_double_to_fixed (2), pixman_double_to_fixed (0), },
 
90
          { pixman_double_to_fixed (0), pixman_double_to_fixed (0.004990), pixman_double_to_fixed (1.0) } 
 
91
        }
 
92
    };
 
93
 
 
94
    pixman_transform_t id = {
 
95
        { { pixman_fixed_1, 0, 0 },
 
96
          { 0, pixman_fixed_1, 0 },
 
97
          { 0, 0, pixman_fixed_1 } }
 
98
    };
 
99
 
 
100
    pixman_point_fixed_t c_inner;
 
101
    pixman_point_fixed_t c_outer;
 
102
    pixman_fixed_t r_inner;
 
103
    pixman_fixed_t r_outer;
 
104
    
 
105
    gtk_init (&argc, &argv);
 
106
    
 
107
    for (i = 0; i < WIDTH * HEIGHT; ++i)
 
108
        dest[i] = 0x3f0000ff; /* pale blue */
 
109
    
 
110
    dest_img = pixman_image_create_bits (PIXMAN_a8r8g8b8,
 
111
                                         WIDTH, HEIGHT, 
 
112
                                         dest,
 
113
                                         WIDTH * 4);
 
114
 
 
115
    c_inner.x = pixman_double_to_fixed (50.0);
 
116
    c_inner.y = pixman_double_to_fixed (50.0);
 
117
    c_outer.x = pixman_double_to_fixed (50.0);
 
118
    c_outer.y = pixman_double_to_fixed (50.0);
 
119
    r_inner = 0;
 
120
    r_outer = pixman_double_to_fixed (50.0);
 
121
    
 
122
    src_img = pixman_image_create_radial_gradient (&c_inner, &c_outer,
 
123
                                                   r_inner, r_outer,
 
124
                                                   stops, 2);
 
125
    
 
126
#if 0
 
127
    src_img = pixman_image_create_linear_gradient  (&p1, &p2,
 
128
                                                    stops, 2);
 
129
    
 
130
#endif
 
131
    pixman_image_set_transform (src_img, &trans);
 
132
    
 
133
    pixman_image_composite (PIXMAN_OP_OVER, src_img, NULL, dest_img,
 
134
                            0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
 
135
    
 
136
    printf ("0, 0: %x\n", dest[0]);
 
137
    printf ("10, 10: %x\n", dest[10 * 10 + 10]);
 
138
    printf ("w, h: %x\n", dest[(HEIGHT - 1) * 100 + (WIDTH - 1)]);
 
139
    
 
140
    show_window (dest, WIDTH, HEIGHT, WIDTH);
 
141
    
 
142
    pixman_image_unref (src_img);
 
143
    pixman_image_unref (dest_img);
 
144
    free (dest);
 
145
    
 
146
    return 0;
 
147
}