summaryrefslogtreecommitdiff
path: root/bluenoise.c
blob: 5d0b4c13f05cfca86ffaa0fab013b63161c87e5a (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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "fft.h"

#define N_SAMPLES 512

#define K   ((2 * M_PI) / N_SAMPLES)

static double
ssin (double f)
{
    return 0.5 * (sin (K * f) + 1.0);
}

static double
scos (double f)
{
    return 0.5 * (cos (K * f) + 1.0);
}

int
main ()
{
    complex_t *noise = malloc (sizeof (complex_t) * N_SAMPLES * N_SAMPLES);
    int i, j;
    double max, min;

    srand48 (time (0));
    
    for (i = 0; i < N_SAMPLES; ++i)
    {
	for (j = 0; j < N_SAMPLES; ++j)
	{
#if 0
	    noise[i * N_SAMPLES + j].re = 0.6 * (drand48() + drand48()) - 0.2 * drand48();
#endif
	    noise[i * N_SAMPLES + j].im = 0;
	    noise[i * N_SAMPLES + j].re = 0.5 * (drand48() + drand48());
	    noise[i * N_SAMPLES + j].re = 0.6 * (drand48() + drand48()) - 0.2 * drand48();
	}
    }

    fft_2d (noise, N_SAMPLES);
    
    shift_2d (noise, N_SAMPLES);

    max = noise[0].re;
    min = noise[0].re;

    for (i = 0; i < N_SAMPLES; ++i)
    {
	for (j = 0; j < N_SAMPLES; ++j)
	{
	    double d = sqrt ((i - N_SAMPLES/2) * (i - N_SAMPLES/2) +
			     (j - N_SAMPLES/2) * (j - N_SAMPLES/2));
	    double f = (d / (N_SAMPLES)); // * (d / N_SAMPLES);
	    

	    if (d != 0.0)
	    {
		double factor = 0;

#define CHECK(n)							\
		((i <= (n) || i >= N_SAMPLES - ((n) + 1))  ^		\
		 (j <= (n) || j >= N_SAMPLES - ((n) + 1)))
		
#if 0
		if (CHECK(1))
		{
		    factor = 1;
		}
		else if (CHECK(42))
		{
		    factor = 1;
		}
		else
		{
		    factor = 0.1;
		}
#endif
		if (f <= 0.5)
		{
		    factor = 0.5 * (cos (M_PI * (1 - 2 * (f))) + 1.0);
		}
		else
		{
		    factor = 1;
		}
#if 0
		noise[i * N_SAMPLES + j].re *= 2.4 * f;
		noise[i * N_SAMPLES + j].im *= 2.4 * f;
#endif
#if 0
		if (f < 0.4)
		    factor = 0;
		else
		    factor = 12 * (f - 0.4);
#endif

		noise[i * N_SAMPLES + j].re *= factor;
		noise[i * N_SAMPLES + j].im *= factor; // 8 * N_SAMPLES * N_SAMPLES * factor;
	    }
	}
    }

    printf ("%f %f\n", noise[i * (N_SAMPLES/2) + N_SAMPLES/2].re, noise[i * (N_SAMPLES/2) + N_SAMPLES/2].im);
    
    shift_2d (noise, N_SAMPLES);
    
    ifft_2d (noise, N_SAMPLES);

#define N_BUCKETS  128
    
    int hist[N_BUCKETS];
    int bad;
    bad = 0;
    memset (hist, 0, sizeof hist);
    for (i = 0; i < N_SAMPLES * N_SAMPLES; ++i)
    {
	int v = (noise[i].re * N_BUCKETS);
	if (v == N_BUCKETS)
	    v = N_BUCKETS - 1;
	else if (v >= N_BUCKETS || v < 0)
	{
	    printf ("bad: %f (%d)\n", noise[i].re, v);
	    bad++;
	}
	else
	    hist[v]++;
    }
    int total = 0;
    for (i = 0; i < N_BUCKETS; ++i)
    {
	printf ("%d: %d\n", i, hist[i]);
	total += hist[i];
    }
    printf ("bad: %d (%f %%)\n", bad, 100.0 * bad / (N_SAMPLES * N_SAMPLES));
    printf ("total: %d \n", total + bad);
    
    show_image ("blue noise", noise, N_SAMPLES);
    
    return 0;
}