summaryrefslogtreecommitdiff
path: root/gtk-utils.c
blob: fa129f16bfac66349a993850f14ef70fd275bfd5 (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
#include <math.h>
#include <stdlib.h>
#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, total, set;

    total = 0;
    set = 0;

    for (h = 0; h < n; ++h)
    {
	for (w = 0; w < n; ++w)
	{
	    unsigned char *pb = (unsigned char *)p_bits;
	    complex_t c = buffer[h * n + w];
	    double v;
	    int vi;
	    int g;
	    double no;

	    pb += h * p_stride + w * 4;

	    no = c.re;
	    
#define THRESHOLD   (0.5)

	    total++;
	    
	    if (THRESHOLD + 2 * (no - 0.5) >= 0.5)
	    {
		g = 0xff;
		set++;
	    }
	    else if (THRESHOLD + 2 * (no - 0.5) >= -0.5)
	    {
		g = 0x80;
	    }
	    else if (THRESHOLD + 2 * (no - 0.5) >= -1.5)
	    {
		g = 0x00;
		set--;
	    }
	    else
	    {
		printf ("asdf\n");
		g = 100;
	    }
	    
#if 0
	    vi *= 0x0f;
#endif

#define SADD(v,a)   (((v) + (a) > 0xff)? 0xff : ((v) + (a) < 0)? 0 : ((v) + (a)))

	    g = ((double)w / n) * 255;

#if 0
	    if (vi + g > 0xff)
		pb[0] = pb[1] = pb[2] = 0xff;
	    else
		pb[0] = pb[1] = pb[2] = 0x00;
#endif
#if 0
	    vi = (v - 0x80);
	    vi >>= 3;

	    int rr, gg, bb;

	    rr = 0x20 + 0.5 * g;
	    gg = 0x20 + 0.5 * g;
	    bb = 0 + 0.3 * g;

	    pb[0] = SADD (rr, vi);
	    pb[1] = SADD (gg, - (vi));
	    pb[2] = SADD (bb, vi);
	    pb[3] = 0xff;

	    pb[0] = SADD (rr, 0.5 * vi);
	    pb[1] = SADD (gg, - 0.5 * vi);
	    pb[2] = SADD (bb, 2 * vi);
	    pb[3] = 0xff;
#endif

	    pb[0] = g;
	    pb[1] = g;
	    pb[2] = g;
	    pb[3] = 0xff;

#define TRUNC(x, n)							\
	    x >>= (8 - n);						\
	    x <<= (8 - n);						\
	    x |= x >> n;						\
	    x |= x >> (2 * n)

#if 0
	    TRUNC(pb[0], 5);
	    TRUNC(pb[1], 6);
	    TRUNC(pb[2], 5);
#endif
	}

    }
    printf ("%d of %d (%f)\n", set, total, (double)set / total);

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

void
show_image (const char *name, complex_t *image, int n)
{
    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_buffer (image, n);

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