Skip to content

Instantly share code, notes, and snippets.

Created October 31, 2012 11:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3986620 to your computer and use it in GitHub Desktop.
Save anonymous/3986620 to your computer and use it in GitHub Desktop.
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