#include #include #include #include #include "fft.h" static GdkPixbuf * pixbuf_from_buffer (complex_t *buffer, int n) { GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, n, n); guint32 *p_bits = (guint32 *)gdk_pixbuf_get_pixels (pixbuf); int p_stride = gdk_pixbuf_get_rowstride (pixbuf); int w, h, total, set; total = 0; set = 0; for (h = 0; h < n; ++h) { for (w = 0; w < n; ++w) { unsigned char *pb = (unsigned char *)p_bits; complex_t c = buffer[h * n + w]; double v; int vi; int g; double no; pb += h * p_stride + w * 4; no = c.re; #define THRESHOLD (0.5) total++; if (THRESHOLD + 2 * (no - 0.5) >= 0.5) { g = 0xff; set++; } else if (THRESHOLD + 2 * (no - 0.5) >= -0.5) { g = 0x80; } else if (THRESHOLD + 2 * (no - 0.5) >= -1.5) { g = 0x00; set--; } else { printf ("asdf\n"); g = 100; } #if 0 vi *= 0x0f; #endif #define SADD(v,a) (((v) + (a) > 0xff)? 0xff : ((v) + (a) < 0)? 0 : ((v) + (a))) g = ((double)w / n) * 255; #if 0 if (vi + g > 0xff) pb[0] = pb[1] = pb[2] = 0xff; else pb[0] = pb[1] = pb[2] = 0x00; #endif #if 0 vi = (v - 0x80); vi >>= 3; int rr, gg, bb; rr = 0x20 + 0.5 * g; gg = 0x20 + 0.5 * g; bb = 0 + 0.3 * g; pb[0] = SADD (rr, vi); pb[1] = SADD (gg, - (vi)); pb[2] = SADD (bb, vi); pb[3] = 0xff; pb[0] = SADD (rr, 0.5 * vi); pb[1] = SADD (gg, - 0.5 * vi); pb[2] = SADD (bb, 2 * vi); pb[3] = 0xff; #endif pb[0] = g; pb[1] = g; pb[2] = g; pb[3] = 0xff; #define TRUNC(x, n) \ x >>= (8 - n); \ x <<= (8 - n); \ x |= x >> n; \ x |= x >> (2 * n) #if 0 TRUNC(pb[0], 5); TRUNC(pb[1], 6); TRUNC(pb[2], 5); #endif } } printf ("%d of %d (%f)\n", set, total, (double)set / total); return pixbuf; } #define SIZE 1024 #define SIZE 1024 static gboolean on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data) { GdkPixbuf *pixbuf = data; int i, j; int pwidth = gdk_pixbuf_get_width (pixbuf); int pheight = gdk_pixbuf_get_height (pixbuf); int dwidth, dheight; gdk_drawable_get_size (widget->window, &dwidth, &dheight); for (i = 0; i < dheight; i += pheight) { for (j = 0; j < dwidth; j += pwidth) { gdk_draw_pixbuf (widget->window, NULL, pixbuf, 0, 0, j, i, pwidth, pheight, GDK_RGB_DITHER_NONE, 0, 0); } } return TRUE; } void show_image (const char *name, complex_t *image, int n) { GtkWidget *window, *da; GdkPixbuf *pixbuf; int argc; char **argv; char *arg0 = g_strdup (name); argc = 1; argv = (char **)&arg0; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); da = gtk_drawing_area_new (); gtk_container_add (GTK_CONTAINER (window), da); gtk_window_set_default_size (GTK_WINDOW (window), SIZE, SIZE); pixbuf = pixbuf_from_buffer (image, n); g_signal_connect (da, "expose_event", G_CALLBACK (on_expose), pixbuf); g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL); gtk_widget_show_all (window); gtk_main (); }