summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@redhat.com>2010-09-25 19:38:27 -0400
committerSøren Sandmann Pedersen <ssp@redhat.com>2010-09-25 19:38:27 -0400
commit597d2c9472425f2feed5a02a7a3ee360a984bd51 (patch)
tree4c4e371e9d186dea77239affdd4e4bff4c8f5c47
parentf921166c883f8c02e7d23c5a85ce0c691138d0fe (diff)
Histogram plus gradients
-rw-r--r--bluenoise.c82
-rw-r--r--gtk-utils.c48
2 files changed, 116 insertions, 14 deletions
diff --git a/bluenoise.c b/bluenoise.c
index 25ab43a..5d0b4c1 100644
--- a/bluenoise.c
+++ b/bluenoise.c
@@ -2,10 +2,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
+#include <string.h>
#include "fft.h"
-#define N_SAMPLES 128
+#define N_SAMPLES 512
#define K ((2 * M_PI) / N_SAMPLES)
@@ -34,8 +35,12 @@ main ()
{
for (j = 0; j < N_SAMPLES; ++j)
{
- noise[i * N_SAMPLES + j].re = 0.5 * (drand48() + drand48());
+#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();
}
}
@@ -57,27 +62,84 @@ main ()
if (d != 0.0)
{
- noise[i * N_SAMPLES + j].re *= 0.5 * (cos (M_PI * (1 - 2 * f)) + 1.0);
- noise[i * N_SAMPLES + j].im *= 0.5 * (cos (M_PI * (1 - 2 * f)) + 1.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);
-#if 0
- noise[i * (N_SAMPLES / 2) + N_SAMPLES / 2].re = 1.5;
- noise[i * (N_SAMPLES / 2) + N_SAMPLES / 2].im = 0;
-#endif
-
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);
diff --git a/gtk-utils.c b/gtk-utils.c
index e1eca8d..e8a8d63 100644
--- a/gtk-utils.c
+++ b/gtk-utils.c
@@ -15,9 +15,11 @@ pixbuf_from_buffer (complex_t *buffer, int n)
{
for (w = 0; w < n; ++w)
{
- char *pb = (char *)p_bits;
+ unsigned char *pb = (unsigned char *)p_bits;
complex_t c = buffer[h * n + w];
double v;
+ int vi;
+ int g;
pb += h * p_stride + w * 4;
@@ -26,11 +28,49 @@ pixbuf_from_buffer (complex_t *buffer, int n)
v = 255.0;
if (v < 0)
v = 0;
+
+#if 0
+ vi *= 0x0f;
+#endif
+
+#define SADD(v,a) (((v) + (a) > 0xff)? 0xff : ((v) + (a) < 0)? 0 : ((v) + (a)))
+
+ g = ((double)h / n) * 255;
+
+#if 0
+ if (vi + g > 0xff)
+ pb[0] = pb[1] = pb[2] = 0xff;
+ else
+ pb[0] = pb[1] = pb[2] = 0x00;
+#endif
+ vi = (v - 0x80);
+ vi >>= 5;
+
+ int rr, gg, bb;
+
+ rr = 0x3;
+ gg = 0x80 - 0x80/255.0 * g;
+ bb = 0x3;
- pb[0] = (int)(v);
- pb[1] = (int)(v);
- pb[2] = (int)(v);
+ pb[0] = SADD (rr, vi);
+ pb[1] = SADD (gg, - vi);
+ pb[2] = SADD (bb, vi);
+ pb[3] = 0xff;
+
+ pb[0] = rr;
+ pb[1] = gg;
+ pb[2] = bb;
pb[3] = 0xff;
+
+#define TRUNC(x, n) \
+ x >>= (8 - n); \
+ x <<= (8 - n); \
+ x |= x >> n; \
+ x |= x >> (2 * n)
+
+ TRUNC(pb[0], 8);
+ TRUNC(pb[1], 8);
+ TRUNC(pb[2], 8);
}
}