testpixbuf-scale.c 4.43 KB
Newer Older
1
#include <config.h>
2 3 4
#include <gtk/gtk.h>

#include <stdio.h>
5
#include <stdlib.h>
6

7
GdkInterpType interp_type = GDK_INTERP_BILINEAR;
8 9 10 11 12
int overall_alpha = 255;
GdkPixbuf *pixbuf;
GtkWidget *darea;
  
void
13
set_interp_type (GtkWidget *widget, gpointer data)
14
{
15 16 17 18 19 20
  guint types[] = { GDK_INTERP_NEAREST,
                    GDK_INTERP_BILINEAR,
                    GDK_INTERP_TILES,
                    GDK_INTERP_HYPER };

  interp_type = types[gtk_combo_box_get_active (GTK_COMBO_BOX (widget))];
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  gtk_widget_queue_draw (darea);
}

void
overall_changed_cb (GtkAdjustment *adjustment, gpointer data)
{
  if (adjustment->value != overall_alpha)
    {
      overall_alpha = adjustment->value;
      gtk_widget_queue_draw (darea);
    }
}

gboolean
expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
  GdkPixbuf *dest;
38 39

  gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
40 41
  
  dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, event->area.width, event->area.height);
42

43 44 45 46 47 48 49
  gdk_pixbuf_composite_color (pixbuf, dest,
			      0, 0, event->area.width, event->area.height,
			      -event->area.x, -event->area.y,
			      (double) widget->allocation.width / gdk_pixbuf_get_width (pixbuf),
			      (double) widget->allocation.height / gdk_pixbuf_get_height (pixbuf),
			      interp_type, overall_alpha,
			      event->area.x, event->area.y, 16, 0xaaaaaa, 0x555555);
50

51 52 53 54
  gdk_draw_pixbuf (widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], dest,
		   0, 0, event->area.x, event->area.y,
		   event->area.width, event->area.height,
		   GDK_RGB_DITHER_NORMAL, event->area.x, event->area.y);
55
  
56
  g_object_unref (dest);
57 58 59 60
  
  return TRUE;
}

61 62
extern void pixbuf_init();

63 64 65 66
int
main(int argc, char **argv)
{
	GtkWidget *window, *vbox;
67
        GtkWidget *combo_box;
68 69 70 71
	GtkWidget *alignment;
	GtkWidget *hbox, *label, *hscale;
	GtkAdjustment *adjustment;
	GtkRequisition scratch_requisition;
72
        const gchar *creator;
Havoc Pennington's avatar
Havoc Pennington committed
73 74
        GError *error;
        
75 76
	pixbuf_init ();

77 78 79 80 81 82 83
	gtk_init (&argc, &argv);

	if (argc != 2) {
		fprintf (stderr, "Usage: testpixbuf-scale FILE\n");
		exit (1);
	}

Havoc Pennington's avatar
Havoc Pennington committed
84 85
        error = NULL;
	pixbuf = gdk_pixbuf_new_from_file (argv[1], &error);
86
	if (!pixbuf) {
Havoc Pennington's avatar
Havoc Pennington committed
87 88 89
		fprintf (stderr, "Cannot load image: %s\n",
                         error->message);
                g_error_free (error);
90 91 92
		exit(1);
	}

93 94 95 96
        creator = gdk_pixbuf_get_option (pixbuf, "tEXt::Software");
        if (creator)
                g_print ("%s was created by '%s'\n", argv[1], creator);

97
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
98 99
	g_signal_connect (window, "destroy",
			  G_CALLBACK (gtk_main_quit), NULL);
100 101 102 103
	
	vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (window), vbox);

104 105 106 107 108 109 110 111 112 113 114 115
        combo_box = gtk_combo_box_new_text ();

        gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "NEAREST");
        gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "BILINEAR");
        gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "TILES");
        gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "HYPER");

        gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 1);

        g_signal_connect (combo_box, "changed",
                          G_CALLBACK (set_interp_type),
                          NULL);
116 117 118 119 120 121 122 123 124 125 126
	
	alignment = gtk_alignment_new (0.0, 0.0, 0.0, 0.5);
	gtk_box_pack_start (GTK_BOX (vbox), alignment, FALSE, FALSE, 0);

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

	label = gtk_label_new ("Overall Alpha:");
	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

	adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (overall_alpha, 0, 255, 1, 10, 0));
127 128
	g_signal_connect (adjustment, "value_changed",
			  G_CALLBACK (overall_changed_cb), NULL);
129 130 131 132 133
	
	hscale = gtk_hscale_new (adjustment);
	gtk_scale_set_digits (GTK_SCALE (hscale), 0);
	gtk_box_pack_start (GTK_BOX (hbox), hscale, TRUE, TRUE, 0);

134
	gtk_container_add (GTK_CONTAINER (alignment), combo_box);
135 136 137 138 139 140 141 142
	gtk_widget_show_all (vbox);

	/* Compute the size without the drawing area, so we know how big to make the default size */
	gtk_widget_size_request (vbox, &scratch_requisition);

	darea = gtk_drawing_area_new ();
	gtk_box_pack_start (GTK_BOX (vbox), darea, TRUE, TRUE, 0);

143 144
	g_signal_connect (darea, "expose_event",
			  G_CALLBACK (expose_cb), NULL);
145 146

	gtk_window_set_default_size (GTK_WINDOW (window),
147 148
				     gdk_pixbuf_get_width (pixbuf),
				     scratch_requisition.height + gdk_pixbuf_get_height (pixbuf));
149 150 151 152 153 154 155
	
	gtk_widget_show_all (window);

	gtk_main ();

	return 0;
}