testpixbuf-scale.c 4.27 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
  gtk_widget_queue_draw (darea);
}

void
overall_changed_cb (GtkAdjustment *adjustment, gpointer data)
{
27
  if (gtk_adjustment_get_value (adjustment) != overall_alpha)
28
    {
29
      overall_alpha = gtk_adjustment_get_value (adjustment);
30 31 32 33 34
      gtk_widget_queue_draw (darea);
    }
}

gboolean
35
draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
36 37
{
  GdkPixbuf *dest;
38
  int width, height;
39

40 41 42 43
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);

  dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
44

45
  gdk_pixbuf_composite_color (pixbuf, dest,
46 47 48 49
			      0, 0, width, height,
			      0, 0,
                              (double) width / gdk_pixbuf_get_width (pixbuf),
                              (double) height / gdk_pixbuf_get_height (pixbuf),
50
			      interp_type, overall_alpha,
51
			      0, 0, 16, 0xaaaaaa, 0x555555);
52 53

  gdk_cairo_set_source_pixbuf (cr, dest, 0, 0);
54
  cairo_paint (cr);
55

56
  g_object_unref (dest);
57 58 59 60 61 62 63 64
  
  return TRUE;
}

int
main(int argc, char **argv)
{
	GtkWidget *window, *vbox;
65
        GtkWidget *combo_box;
66 67 68
	GtkWidget *hbox, *label, *hscale;
	GtkAdjustment *adjustment;
	GtkRequisition scratch_requisition;
69
        const gchar *creator;
Havoc Pennington's avatar
Havoc Pennington committed
70
        GError *error;
Owen Taylor's avatar
Owen Taylor committed
71

72 73 74 75 76 77 78
	gtk_init (&argc, &argv);

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

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

88 89 90 91
        creator = gdk_pixbuf_get_option (pixbuf, "tEXt::Software");
        if (creator)
                g_print ("%s was created by '%s'\n", argv[1], creator);

92
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
93 94
	g_signal_connect (window, "destroy",
			  G_CALLBACK (gtk_main_quit), NULL);
95
	
96
	vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
97 98
	gtk_container_add (GTK_CONTAINER (window), vbox);

99
        combo_box = gtk_combo_box_text_new ();
100

101 102 103 104
        gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), "NEAREST");
        gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), "BILINEAR");
        gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), "TILES");
        gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), "HYPER");
105 106 107 108 109 110

        gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 1);

        g_signal_connect (combo_box, "changed",
                          G_CALLBACK (set_interp_type),
                          NULL);
111
	
112 113
        gtk_widget_set_halign (combo_box, GTK_ALIGN_START);
	gtk_box_pack_start (GTK_BOX (vbox), combo_box, FALSE, FALSE, 0);
114

115
	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);
116 117 118 119 120
	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);

Javier Jardón's avatar
Javier Jardón committed
121
	adjustment = gtk_adjustment_new (overall_alpha, 0, 255, 1, 10, 0);
122 123
	g_signal_connect (adjustment, "value_changed",
			  G_CALLBACK (overall_changed_cb), NULL);
124
	
125
	hscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment);
126 127 128 129 130 131
	gtk_scale_set_digits (GTK_SCALE (hscale), 0);
	gtk_box_pack_start (GTK_BOX (hbox), hscale, TRUE, TRUE, 0);

	gtk_widget_show_all (vbox);

	/* Compute the size without the drawing area, so we know how big to make the default size */
132
        gtk_widget_get_preferred_size ( (vbox),
133
                                   &scratch_requisition, NULL);
134 135 136 137

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

138 139
	g_signal_connect (darea, "draw",
			  G_CALLBACK (draw_cb), NULL);
140 141

	gtk_window_set_default_size (GTK_WINDOW (window),
142 143
				     gdk_pixbuf_get_width (pixbuf),
				     scratch_requisition.height + gdk_pixbuf_get_height (pixbuf));
144 145 146 147 148 149 150
	
	gtk_widget_show_all (window);

	gtk_main ();

	return 0;
}