summaryrefslogtreecommitdiff
path: root/pngtrans.c
blob: 4fb6b4cb031fdd799c129aa0d168e1c5a10fb352 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <stdlib.h>
#include <stdint.h>
#include <cairo.h>
#include <gtk/gtk.h>
#include <gdk/gdkpixbuf.h>
#include "fft.h"

typedef struct complex_image_t complex_image_t;
struct complex_image_t
{
    int width;
    int height;
    complex_t *red;
    complex_t *green;
    complex_t *blue;
};

static complex_image_t *
complex_image_new (int width, int height)
{
    complex_image_t *image = g_new0 (complex_image_t, 1);

    image->width = width;
    image->height = height;
    image->red = g_new0 (complex_t, width * height);
    image->green = g_new0 (complex_t, width * height);
    image->blue = g_new0 (complex_t, width * height);
    
    return image;
}

static complex_image_t *
complex_image_from_pixbuf (GdkPixbuf *pixbuf)
{
    complex_image_t *result;
    uint8_t *pdata;
    int w, h, s;
    int i, j;
    gboolean has_alpha;
    int n_channels;

    w = gdk_pixbuf_get_width (pixbuf);
    h = gdk_pixbuf_get_height (pixbuf);
    s = gdk_pixbuf_get_rowstride (pixbuf);
    pdata = gdk_pixbuf_get_pixels (pixbuf);
    has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
    n_channels = 3 + has_alpha;

    printf ("has alpha: %d\n", has_alpha);
    
    result = complex_image_new (w, h);

    for (i = 0; i < h; ++i)
    {
	for (j = 0; j < w; ++j)
	{
	    uint8_t *p = &pdata[i * s + j * n_channels];
	    int idx = i * w + j;

	    result->red[idx].re = p[0] / 255.0;
	    result->green[idx].re = p[1] / 255.0;
	    result->blue[idx].re = p[2] / 255.0;
	}
    }

    return result;
}

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

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

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)
{
    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);

    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);

#if 0
    low_pass (image, 2 * image->width);
#endif

    display ("test", image, DISPLAY_MAG);

#if 0
#endif
#if 0
#endif
    
    image_ifft (image);

    display ("test", image, DISPLAY_RE);
    
    return 0;
}