diff options
author | Søren Sandmann Pedersen <ssp@redhat.com> | 2011-08-01 20:20:33 -0400 |
---|---|---|
committer | Søren Sandmann Pedersen <ssp@redhat.com> | 2011-08-01 20:20:33 -0400 |
commit | 1f6ee9c805c75b76fcea166e7bae92b544340e6f (patch) | |
tree | 7417df0fe2ae1d1769073c6aa3730f950d506953 | |
parent | 9f052b706178d591a4fff0ca6a6c304dde154143 (diff) |
Refactor pixbuf conversion
-rw-r--r-- | pngtrans.c | 178 |
1 files changed, 89 insertions, 89 deletions
@@ -6,32 +6,80 @@ #include "fft.h" #include "image.h" -typedef uint8_t (* convert_t) (complex_t c); +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_t convert) +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] = convert (image->red[idx]); - p[1] = convert (image->green[idx]); - p[2] = convert (image->blue[idx]); + + 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; } @@ -46,9 +94,9 @@ on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data) 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) @@ -62,83 +110,35 @@ 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 + 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 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; - + 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); - - 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); - + gtk_widget_show_all (window); - + gtk_main (); } @@ -163,7 +163,7 @@ filter (complex_t c, double dist) { double m = complex_mag (c); double arg = complex_arg (c); - + return complex_from_mag_arg (m * (1/(pow (sqrt (dist) / 16.0, 4) + 1)) , arg); } @@ -175,7 +175,7 @@ low_pass (complex_image_t *image, double d) int i, j; double d2 = d * d; int c = h / 2; - + for (i = 0; i < h; ++i) { for (j = 0; j < w; ++j) @@ -183,7 +183,7 @@ low_pass (complex_image_t *image, double d) int idx = i * w + j; double dist = (c - i) * (c - i) + (c - j) * (c -j); double t; - + image->red[idx] = filter (image->red[idx], dist); image->green[idx] = filter (image->green[idx], dist); image->blue[idx] = filter (image->blue[idx], dist); @@ -197,39 +197,39 @@ main (int argc, char **argv) char *input, *output; GdkPixbuf *pb; complex_image_t *image; - + g_type_init (); - + if (argc < 3) { printf ("Usage: %s <input name> <output name>\n\n", argv[0]); return 1; } - + input = argv[1]; output = argv[2]; - + if (!(pb = gdk_pixbuf_new_from_file (input, NULL))) { printf ("Could not open %s\n", input); return -1; } - + image = complex_image_from_pixbuf (pb); - + g_print ("width, height %d %d\n", image->width, image->height); - + image_fft (image); - - display ("test", image, DISPLAY_MAG); - + + display ("test", image, CONVERT_MAG); + low_pass (image, 2 * image->width); - - display ("test", image, DISPLAY_MAG); - + + display ("test", image, CONVERT_MAG); + image_ifft (image); - - display ("test", image, DISPLAY_RE); - + + display ("test", image, CONVERT_RE); + return 0; } |