summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@redhat.com>2011-08-01 20:23:23 -0400
committerSøren Sandmann Pedersen <ssp@redhat.com>2011-08-01 20:23:23 -0400
commitf1457fcec94aac20d458218021902d64089e93ab (patch)
tree2b4f23d843bb44b859468ebadddc7671a779e65e
parent1f6ee9c805c75b76fcea166e7bae92b544340e6f (diff)
Move more pixbuf conversion to image.[ch]
-rw-r--r--image.c72
-rw-r--r--image.h9
-rw-r--r--noise2.c86
-rw-r--r--pngtrans.c77
4 files changed, 87 insertions, 157 deletions
diff --git a/image.c b/image.c
index d0f868e..59ee9d7 100644
--- a/image.c
+++ b/image.c
@@ -85,3 +85,75 @@ complex_image_from_pixbuf (GdkPixbuf *pixbuf)
return result;
}
+
+typedef uint8_t (* converter_t) (complex_t c);
+
+static uint8_t
+convert_mag (complex_t d)
+{
+ double m = complex_mag (d);
+
+ m = log (m + 1) / 12.0;
+
+ if (m > 1.0)
+ {
+ printf ("%f\n", m);
+ m = 1.0;
+ }
+
+ if (m < 0)
+ {
+ printf ("%f\n", m);
+ m = 0;
+ }
+
+ return (uint8_t) (m * 255.0 + 0.5);
+}
+
+static uint8_t
+convert_re (complex_t c)
+{
+ if (c.re > 1.0)
+ c.re = 1.0;
+ if (c.re < 0)
+ c.re = 0;
+
+ return c.re * 255.0 + 0.5;
+}
+
+GdkPixbuf *
+pixbuf_from_complex_image (complex_image_t *image, convert_type_t convert)
+{
+ int w = image->width;
+ int h = image->height;
+ GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
+ uint8_t *p_bits = (uint8_t *)gdk_pixbuf_get_pixels (pixbuf);
+ int s = gdk_pixbuf_get_rowstride (pixbuf);
+ converter_t converter;
+ int i, j;
+
+ switch (convert)
+ {
+ case CONVERT_MAG: converter = convert_mag; break;
+ case CONVERT_RE: converter = convert_re; break;
+ default:
+ g_assert_not_reached();
+ }
+
+ for (i = 0; i < h; ++i)
+ {
+ for (j = 0; j < w; ++j)
+ {
+ uint8_t *p = &(p_bits[i * s + j * 4]);
+ int idx = i * w + j;
+
+ p[0] = converter (image->red[idx]);
+ p[1] = converter (image->green[idx]);
+ p[2] = converter (image->blue[idx]);
+ p[3] = 0xff; // convert (image->alpha[idx]);
+ }
+ }
+
+ return pixbuf;
+}
+
diff --git a/image.h b/image.h
index c5baebc..c2aee71 100644
--- a/image.h
+++ b/image.h
@@ -24,3 +24,12 @@ complex_image_subtract (complex_image_t *image,
complex_image_t *
complex_image_from_pixbuf (GdkPixbuf *pixbuf);
+
+typedef enum
+{
+ CONVERT_MAG, /* display the log magnitude */
+ CONVERT_RE, /* display the real value */
+} convert_type_t;
+
+GdkPixbuf *
+pixbuf_from_complex_image (complex_image_t *image, convert_type_t convert);
diff --git a/noise2.c b/noise2.c
index 4cea4a2..7e87fec 100644
--- a/noise2.c
+++ b/noise2.c
@@ -6,35 +6,6 @@
#include "fft.h"
#include "image.h"
-typedef uint8_t (* convert_t) (complex_t c);
-
-static GdkPixbuf *
-pixbuf_from_complex_image (complex_image_t *image, convert_t convert)
-{
- int w = image->width;
- int h = image->height;
- GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
- uint8_t *p_bits = (uint8_t *)gdk_pixbuf_get_pixels (pixbuf);
- int s = gdk_pixbuf_get_rowstride (pixbuf);
- int i, j;
-
- for (i = 0; i < h; ++i)
- {
- for (j = 0; j < w; ++j)
- {
- uint8_t *p = &(p_bits[i * s + j * 4]);
- int idx = i * w + j;
-
- p[0] = convert (image->red[idx]);
- p[1] = convert (image->green[idx]);
- p[2] = convert (image->blue[idx]);
- p[3] = 0xff; // convert (image->alpha[idx]);
- }
- }
-
- return pixbuf;
-}
-
#define SIZE 1024
#define SIZE 1024
@@ -62,51 +33,14 @@ on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
return TRUE;
}
-typedef enum
-{
- DISPLAY_MAG,
- DISPLAY_RE
-} display_type_t;
-
-static uint8_t
-convert_mag (complex_t d)
-{
- double m = complex_mag (d);
-
- m = log (m) / 10.0;
-
- if (m > 1.0)
- {
- printf ("%f\n", m);
- m = 1.0;
- }
-
- if (m < 0)
- m = 0;
-
- return (uint8_t) (m * 255.0 + 0.5);
-}
-
-static uint8_t
-convert_re (complex_t c)
-{
- if (c.re > 1.0)
- c.re = 1.0;
- if (c.re < 0)
- c.re = 0;
-
- return c.re * 255.0 + 0.5;
-}
-
static void
-display (const char *name, complex_image_t *image, display_type_t type)
+display (const char *name, complex_image_t *image, convert_type_t type)
{
GtkWidget *window, *da;
GdkPixbuf *pixbuf;
int argc;
char **argv;
char *arg0 = g_strdup (name);
- convert_t convert;
argc = 1;
argv = (char **)&arg0;
@@ -121,15 +55,7 @@ display (const char *name, complex_image_t *image, display_type_t type)
gtk_window_set_default_size (GTK_WINDOW (window), SIZE, SIZE);
- switch (type)
- {
- case DISPLAY_MAG: convert = convert_mag; break;
- case DISPLAY_RE: convert = convert_re; break;
- default:
- g_assert_not_reached();
- }
-
- pixbuf = pixbuf_from_complex_image (image, convert);
+ pixbuf = pixbuf_from_complex_image (image, type);
g_signal_connect (da, "expose_event", G_CALLBACK (on_expose), pixbuf);
g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL);
@@ -234,7 +160,7 @@ main (int argc, char **argv)
make_noise (image);
- display ("test", image, DISPLAY_RE);
+ display ("test", image, CONVERT_RE);
image_fft (image);
@@ -242,15 +168,15 @@ main (int argc, char **argv)
low_pass (low_passed, 2 * image->width);
- display ("low passed", low_passed, DISPLAY_MAG);
+ display ("low passed", low_passed, CONVERT_MAG);
complex_image_subtract (image, low_passed);
- display ("ssubtracted", image, DISPLAY_MAG);
+ display ("ssubtracted", image, CONVERT_MAG);
image_ifft (image);
- display ("test", image, DISPLAY_RE);
+ display ("test", image, CONVERT_RE);
return 0;
}
diff --git a/pngtrans.c b/pngtrans.c
index 6a6c529..e3558a0 100644
--- a/pngtrans.c
+++ b/pngtrans.c
@@ -6,83 +6,6 @@
#include "fft.h"
#include "image.h"
-typedef enum
-{
- CONVERT_MAG, /* display the log magnitude */
- CONVERT_RE, /* display the real value */
-} convert_type_t;
-
-typedef uint8_t (* converter_t) (complex_t c);
-
-static uint8_t
-convert_mag (complex_t d)
-{
- double m = complex_mag (d);
-
- m = log (m + 1) / 12.0;
-
- if (m > 1.0)
- {
- printf ("%f\n", m);
- m = 1.0;
- }
-
- if (m < 0)
- {
- printf ("%f\n", m);
- m = 0;
- }
-
- return (uint8_t) (m * 255.0 + 0.5);
-}
-
-static uint8_t
-convert_re (complex_t c)
-{
- if (c.re > 1.0)
- c.re = 1.0;
- if (c.re < 0)
- c.re = 0;
-
- return c.re * 255.0 + 0.5;
-}
-
-static GdkPixbuf *
-pixbuf_from_complex_image (complex_image_t *image, convert_type_t convert)
-{
- int w = image->width;
- int h = image->height;
- GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
- uint8_t *p_bits = (uint8_t *)gdk_pixbuf_get_pixels (pixbuf);
- int s = gdk_pixbuf_get_rowstride (pixbuf);
- converter_t converter;
- int i, j;
-
- switch (convert)
- {
- case CONVERT_MAG: converter = convert_mag; break;
- case CONVERT_RE: converter = convert_re; break;
- default:
- g_assert_not_reached();
- }
-
- for (i = 0; i < h; ++i)
- {
- for (j = 0; j < w; ++j)
- {
- uint8_t *p = &(p_bits[i * s + j * 4]);
- int idx = i * w + j;
-
- p[0] = converter (image->red[idx]);
- p[1] = converter (image->green[idx]);
- p[2] = converter (image->blue[idx]);
- p[3] = 0xff; // convert (image->alpha[idx]);
- }
- }
-
- return pixbuf;
-}
-
#define SIZE 1024
#define SIZE 1024