testpixbuf-scale.c 5.16 KB
Newer Older
1 2 3 4 5
#include <gtk/gtk.h>
#include "gdk-pixbuf.h"

#include <stdio.h>

6
GdkInterpType interp_type = GDK_INTERP_BILINEAR;
7 8 9 10 11
int overall_alpha = 255;
GdkPixbuf *pixbuf;
GtkWidget *darea;
  
void
12
set_interp_type (GtkWidget *widget, gpointer data)
13
{
14
  interp_type = GPOINTER_TO_UINT (data);
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  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;
32 33 34
  GdkGC *gc;
  GdkColormap *colormap;
  GdkColor color;
35

36 37 38
  color.red = 0xffff;
  color.green = 0xffff;
  color.blue = 0xffff;
39
  
40 41 42 43 44 45 46 47 48 49 50
  gc = gdk_gc_new (widget->window);
  colormap = gdk_window_get_colormap (widget->window);
  gdk_colormap_alloc_color (colormap, &color, TRUE, TRUE);
  gdk_gc_set_foreground (gc, &color);
  gdk_draw_rectangle (widget->window, gc,	
		      TRUE,
		      event->area.x, 
		      event->area.y, 
		      event->area.width, event->area.height);

  gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
51 52


53 54 55 56 57 58 59 60 61 62 63 64 65 66
  dest = gdk_pixbuf_get_from_drawable(NULL,
				      widget->window,
				      gdk_window_get_colormap (widget->window),
				      event->area.x, 
				      event->area.y, 
				      0, 0, event->area.width, event->area.height);

  gdk_pixbuf_composite (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);
  
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  gdk_pixbuf_render_to_drawable (dest, widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
				 0, 0, event->area.x, event->area.y,
				 event->area.width, event->area.height,
				 GDK_RGB_DITHER_NORMAL, event->area.x, event->area.y);
  
  gdk_pixbuf_unref (dest);
  
  return TRUE;
}

int
main(int argc, char **argv)
{
	GtkWidget *window, *vbox;
	GtkWidget *menuitem, *optionmenu, *menu;
	GtkWidget *alignment;
	GtkWidget *hbox, *label, *hscale;
	GtkAdjustment *adjustment;
	GtkRequisition scratch_requisition;

	gtk_init (&argc, &argv);
	gdk_rgb_init ();

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

	pixbuf = gdk_pixbuf_new_from_file (argv[1]);
	if (!pixbuf) {
		fprintf (stderr, "Cannot load %s\n", argv[1]);
		exit(1);
	}

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_signal_connect (GTK_OBJECT (window), "destroy",
			    GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
	
	vbox = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (window), vbox);

	menu = gtk_menu_new ();
	
	menuitem = gtk_menu_item_new_with_label ("NEAREST");
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
112 113
			    GTK_SIGNAL_FUNC (set_interp_type),
			    GUINT_TO_POINTER (GDK_INTERP_NEAREST));
114 115 116 117 118
	gtk_widget_show (menuitem);
	gtk_container_add (GTK_CONTAINER (menu), menuitem);
	
	menuitem = gtk_menu_item_new_with_label ("BILINEAR");
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
119 120
			    GTK_SIGNAL_FUNC (set_interp_type),
			    GUINT_TO_POINTER (GDK_INTERP_BILINEAR));
121 122 123 124 125
	gtk_widget_show (menuitem);
	gtk_container_add (GTK_CONTAINER (menu), menuitem);
	
	menuitem = gtk_menu_item_new_with_label ("TILES");
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
126 127
			    GTK_SIGNAL_FUNC (set_interp_type),
			    GUINT_TO_POINTER (GDK_INTERP_TILES));
128 129 130 131
	gtk_container_add (GTK_CONTAINER (menu), menuitem);

	menuitem = gtk_menu_item_new_with_label ("HYPER");
	gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
132 133
			    GTK_SIGNAL_FUNC (set_interp_type),
			    GUINT_TO_POINTER (GDK_INTERP_HYPER));
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
	gtk_container_add (GTK_CONTAINER (menu), menuitem);

	optionmenu = gtk_option_menu_new ();
	gtk_option_menu_set_menu (GTK_OPTION_MENU (optionmenu), menu);
	gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu), 1);
	
	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));
	gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
			    GTK_SIGNAL_FUNC (overall_changed_cb), NULL);
	
	hscale = gtk_hscale_new (adjustment);
	gtk_scale_set_digits (GTK_SCALE (hscale), 0);
	gtk_box_pack_start (GTK_BOX (hbox), hscale, TRUE, TRUE, 0);

	gtk_container_add (GTK_CONTAINER (alignment), optionmenu);
	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);

	gtk_signal_connect (GTK_OBJECT (darea), "expose_event",
			    GTK_SIGNAL_FUNC (expose_cb), NULL);

	gtk_window_set_default_size (GTK_WINDOW (window),
170 171
				     gdk_pixbuf_get_width (pixbuf),
				     scratch_requisition.height + gdk_pixbuf_get_height (pixbuf));
172 173 174 175 176 177 178
	
	gtk_widget_show_all (window);

	gtk_main ();

	return 0;
}