summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@redhat.com>2010-09-25 00:58:37 -0400
committerSøren Sandmann Pedersen <ssp@redhat.com>2010-09-25 00:58:37 -0400
commit9b14b53493320ce38e00b5b687e3ad30b389cac1 (patch)
tree62fb62ac97fe8f88ed80db205861b92464af9d1b
parent6f2cfe869efbc2d15dda6949013f499c3389a019 (diff)
noise
-rw-r--r--Makefile5
-rw-r--r--bluenoise.c45
-rw-r--r--fft.h5
-rw-r--r--gtk-utils.c79
4 files changed, 133 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 519e05d..b91a5c2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,8 @@
CFLAGS=-Wall
LDFLAGS=-Wall -lm
+GTKFLAGS=`pkg-config --cflags --libs gtk+-2.0`
-all: filters fft-test
+all: filters fft-test bluenoise
filters: fft.c fft.h filters.c
$(CC) -o filters fft.c filters.c $(LDFLAGS)
@@ -9,3 +10,5 @@ filters: fft.c fft.h filters.c
fft-test: fft.c fft.h fft-test.c
$(CC) -o fft-test fft.c fft-test.c $(LDFLAGS)
+bluenoise: fft.c fft.h bluenoise.c gtk-utils.c
+ $(CC) $(GTKFLAGS) -o bluenoise fft.c bluenoise.c gtk-utils.c $(LDFLAGS)
diff --git a/bluenoise.c b/bluenoise.c
new file mode 100644
index 0000000..ed57192
--- /dev/null
+++ b/bluenoise.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+
+#include "fft.h"
+
+#define N_SAMPLES 512
+
+#define K ((2 * M_PI) / N_SAMPLES)
+
+static double
+ssin (double f)
+{
+ return 0.5 * (sin (K * f) + 1.0);
+}
+
+static double
+scos (double f)
+{
+ return 0.5 * (cos (K * f) + 1.0);
+}
+
+int
+main ()
+{
+ complex_t *noise = malloc (sizeof (complex_t) * N_SAMPLES * N_SAMPLES);
+ int i, j;
+
+ for (i = 0; i < N_SAMPLES; ++i)
+ {
+ for (j = 0; j < N_SAMPLES; ++j)
+ {
+ noise[i * N_SAMPLES + j].re = drand48();
+ noise[i * N_SAMPLES + j].im = 0;
+ }
+ }
+
+ fft_2d (noise, N_SAMPLES);
+
+ shift_2d (noise, N_SAMPLES);
+
+ show_image (noise, N_SAMPLES);
+
+ return 0;
+}
diff --git a/fft.h b/fft.h
index f98e059..d82a992 100644
--- a/fft.h
+++ b/fft.h
@@ -1,3 +1,5 @@
+#include <math.h>
+
#ifndef FALSE
#define FALSE (0)
#endif
@@ -78,3 +80,6 @@ shift (complex_t *buffer, int n);
void
shift_2d (complex_t *buffer, int n);
+
+void
+show_image (complex_t *image, int n);
diff --git a/gtk-utils.c b/gtk-utils.c
new file mode 100644
index 0000000..39f697a
--- /dev/null
+++ b/gtk-utils.c
@@ -0,0 +1,79 @@
+#include <stdint.h>
+#include <gtk/gtk.h>
+#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;
+
+ for (h = 0; h < n; ++h)
+ {
+ for (w = 0; w < n; ++w)
+ {
+ char *pb = (char *)p_bits;
+ complex_t c = buffer[h * n + w];
+ double v;
+
+ pb += h * p_stride + w * 4;
+
+ v = c.re * 255.0 + 0.5;
+ if (v > 255.0)
+ v = 255.0;
+ if (v < 0)
+ v = 0;
+
+ pb[0] = pb[1] = pb[2] = (int)v;
+ pb[3] = 0xff;
+ }
+ }
+
+ return pixbuf;
+}
+
+static gboolean
+on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
+{
+ GdkPixbuf *pixbuf = data;
+
+ gdk_draw_pixbuf (widget->window, NULL,
+ pixbuf, 0, 0, 0, 0,
+ gdk_pixbuf_get_width (pixbuf),
+ gdk_pixbuf_get_height (pixbuf),
+ GDK_RGB_DITHER_NONE,
+ 0, 0);
+
+ return TRUE;
+}
+
+void
+show_image (complex_t *image, int n)
+{
+ GtkWidget *window;
+ GdkPixbuf *pixbuf;
+ int argc;
+ char **argv;
+ char *arg0 = g_strdup ("pixman-test-program");
+
+ argc = 1;
+ argv = (char **)&arg0;
+
+ gtk_init (&argc, &argv);
+
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+
+ gtk_window_set_default_size (GTK_WINDOW (window), n, n);
+
+ pixbuf = pixbuf_from_buffer (image, n);
+
+ g_signal_connect (window, "expose_event", G_CALLBACK (on_expose), pixbuf);
+ g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
+
+ gtk_widget_show (window);
+
+ gtk_main ();
+}