gtk-utils.c revision 9ad247e8
16ba797d6Smrg#include <gtk/gtk.h>
26ba797d6Smrg#include <config.h>
39ad247e8Sjmcneill#include "../test/utils.h"
46ba797d6Smrg#include "gtk-utils.h"
56ba797d6Smrg
66ba797d6SmrgGdkPixbuf *
76ba797d6Smrgpixbuf_from_argb32 (uint32_t *bits,
86ba797d6Smrg		    int width,
96ba797d6Smrg		    int height,
106ba797d6Smrg		    int stride)
116ba797d6Smrg{
126ba797d6Smrg    GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,
136ba797d6Smrg					8, width, height);
146ba797d6Smrg    int p_stride = gdk_pixbuf_get_rowstride (pixbuf);
156ba797d6Smrg    guint32 *p_bits = (guint32 *)gdk_pixbuf_get_pixels (pixbuf);
169ad247e8Sjmcneill    int i;
176ba797d6Smrg
189ad247e8Sjmcneill    for (i = 0; i < height; ++i)
199ad247e8Sjmcneill    {
209ad247e8Sjmcneill	uint32_t *src_row = &bits[i * (stride / 4)];
219ad247e8Sjmcneill	uint32_t *dst_row = p_bits + i * (p_stride / 4);
226ba797d6Smrg
239ad247e8Sjmcneill	a8r8g8b8_to_rgba_np (dst_row, src_row, width);
246ba797d6Smrg    }
259ad247e8Sjmcneill
266ba797d6Smrg    return pixbuf;
276ba797d6Smrg}
286ba797d6Smrg
296ba797d6Smrgstatic gboolean
306ba797d6Smrgon_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
316ba797d6Smrg{
326ba797d6Smrg    GdkPixbuf *pixbuf = data;
336ba797d6Smrg
346ba797d6Smrg    gdk_draw_pixbuf (widget->window, NULL,
356ba797d6Smrg		     pixbuf, 0, 0, 0, 0,
366ba797d6Smrg		     gdk_pixbuf_get_width (pixbuf),
376ba797d6Smrg		     gdk_pixbuf_get_height (pixbuf),
386ba797d6Smrg		     GDK_RGB_DITHER_NONE,
396ba797d6Smrg		     0, 0);
406ba797d6Smrg
416ba797d6Smrg    return TRUE;
426ba797d6Smrg}
436ba797d6Smrg
446ba797d6Smrgvoid
456ba797d6Smrgshow_image (pixman_image_t *image)
466ba797d6Smrg{
476ba797d6Smrg    GtkWidget *window;
486ba797d6Smrg    GdkPixbuf *pixbuf;
499ad247e8Sjmcneill    int width, height;
506ba797d6Smrg    int argc;
516ba797d6Smrg    char **argv;
526ba797d6Smrg    char *arg0 = g_strdup ("pixman-test-program");
536ba797d6Smrg    pixman_format_code_t format;
549ad247e8Sjmcneill    pixman_image_t *copy;
556ba797d6Smrg
566ba797d6Smrg    argc = 1;
576ba797d6Smrg    argv = (char **)&arg0;
586ba797d6Smrg
596ba797d6Smrg    gtk_init (&argc, &argv);
606ba797d6Smrg
616ba797d6Smrg    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
626ba797d6Smrg    width = pixman_image_get_width (image);
636ba797d6Smrg    height = pixman_image_get_height (image);
646ba797d6Smrg
656ba797d6Smrg    gtk_window_set_default_size (GTK_WINDOW (window), width, height);
669ad247e8Sjmcneill
679ad247e8Sjmcneill    format = pixman_image_get_format (image);
689ad247e8Sjmcneill
699ad247e8Sjmcneill    /* Three cases:
709ad247e8Sjmcneill     *
719ad247e8Sjmcneill     *  - image is a8r8g8b8_sRGB: we will display without modification
729ad247e8Sjmcneill     *    under the assumption that the monitor is sRGB
739ad247e8Sjmcneill     *
749ad247e8Sjmcneill     *  - image is a8r8g8b8: we will display without modification
759ad247e8Sjmcneill     *    under the assumption that whoever created the image
769ad247e8Sjmcneill     *    probably did it wrong by using sRGB inputs
779ad247e8Sjmcneill     *
789ad247e8Sjmcneill     *  - other: we will convert to a8r8g8b8 under the assumption that
799ad247e8Sjmcneill     *    whoever created the image probably did it wrong.
809ad247e8Sjmcneill     */
819ad247e8Sjmcneill    switch (format)
829ad247e8Sjmcneill    {
839ad247e8Sjmcneill    case PIXMAN_a8r8g8b8_sRGB:
849ad247e8Sjmcneill    case PIXMAN_a8r8g8b8:
859ad247e8Sjmcneill	copy = pixman_image_ref (image);
869ad247e8Sjmcneill	break;
879ad247e8Sjmcneill
889ad247e8Sjmcneill    default:
899ad247e8Sjmcneill	copy = pixman_image_create_bits (PIXMAN_a8r8g8b8,
909ad247e8Sjmcneill					 width, height, NULL, -1);
919ad247e8Sjmcneill	pixman_image_composite32 (PIXMAN_OP_SRC,
929ad247e8Sjmcneill				  image, NULL, copy,
939ad247e8Sjmcneill				  0, 0, 0, 0, 0, 0,
949ad247e8Sjmcneill				  width, height);
959ad247e8Sjmcneill	break;
969ad247e8Sjmcneill    }
979ad247e8Sjmcneill
989ad247e8Sjmcneill    pixbuf = pixbuf_from_argb32 (pixman_image_get_data (copy),
999ad247e8Sjmcneill				 width, height,
1009ad247e8Sjmcneill				 pixman_image_get_stride (copy));
1016ba797d6Smrg
1026ba797d6Smrg    g_signal_connect (window, "expose_event", G_CALLBACK (on_expose), pixbuf);
1036ba797d6Smrg    g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
1046ba797d6Smrg
1056ba797d6Smrg    gtk_widget_show (window);
1066ba797d6Smrg
1076ba797d6Smrg    gtk_main ();
1086ba797d6Smrg}
109