summaryrefslogtreecommitdiff
path: root/pngtrans.c
blob: e3558a0f059e960037e8feb787bf880a19edcf7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdlib.h>
#include <stdint.h>
#include <cairo.h>
#include <gtk/gtk.h>
#include <gdk/gdkpixbuf.h>
#include "fft.h"
#include "image.h"

#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;
}

static void
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);
    
    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_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 ();
}

static void
image_fft (complex_image_t *image)
{
    fft_shift_2d (image->red, image->width);
    fft_shift_2d (image->green, image->width);
    fft_shift_2d (image->blue, image->width);
}

static void
image_ifft (complex_image_t *image)
{
    ifft_shift_2d (image->red, image->width);
    ifft_shift_2d (image->green, image->width);
    ifft_shift_2d (image->blue, image->width);
}

static complex_t
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);
}

static void
low_pass (complex_image_t *image, double d)
{
    int w = image->width;
    int h = image->height;
    int i, j;
    double d2 = d * d;
    int c = h / 2;
    
    for (i = 0; i < h; ++i)
    {
	for (j = 0; j < w; ++j)
	{
	    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);
	}
    }
}

int
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, CONVERT_MAG);
    
    low_pass (image, 2 * image->width);
    
    display ("test", image, CONVERT_MAG);
    
    image_ifft (image);
    
    display ("test", image, CONVERT_RE);
    
    return 0;
}