Skip to content

Instantly share code, notes, and snippets.

/gist:3986620

Created Oct 31, 2012
Embed
What would you like to do?
pixel plotting template
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
void put_pixel(GdkPixbuf *pixbuf, int x, int y, guchar red, guchar green, guchar blue, guchar alpha);
struct widgets
{
GtkWidget *window, *image;
GdkPixbuf *pixbuf;
};
int main (int argc, char *argv[])
{
struct widgets w;
gtk_init(&argc, &argv);
w.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
w.pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, 0, 8, 640, 480);
w.image = gtk_image_new_from_pixbuf(w.pixbuf);
gtk_container_add(GTK_CONTAINER(w.window), GTK_WIDGET(w.image));
gtk_widget_show_all(w.window);
g_signal_connect_swapped(G_OBJECT(w.window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
/*Put pixel array (w.pixbuf) manipulation code here*/
/*EXAMPLE*/
int x,y;
for (x = 0; x < 640; x++)
{
for (y = 0; y < 480; y++)
{
put_pixel(w.pixbuf, (int)x, (int)y, (guchar)x, (guchar)y, (guchar)x+y, 255);
}
}
/*END OF EXAMPLE*/
gtk_image_set_from_pixbuf((GtkImage *)w.image, w.pixbuf);
gtk_main();
return 0;
}
void put_pixel(GdkPixbuf *pixbuf, int x, int y, guchar red, guchar green, guchar blue, guchar alpha)
{
guchar *pixels, *p;
int rowstride, numchannels;
numchannels = gdk_pixbuf_get_n_channels(pixbuf);
rowstride = gdk_pixbuf_get_rowstride(pixbuf);
pixels = gdk_pixbuf_get_pixels(pixbuf);
p = pixels + y * rowstride + x * numchannels;
p[0] = red;
p[1] = green;
p[2] = blue;
p[3] = alpha;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.