summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@redhat.com>2012-11-13 22:21:28 -0500
committerSøren Sandmann Pedersen <ssp@redhat.com>2012-11-13 22:57:17 -0500
commit4c590b0fabf1d2118e752f002add79d89a88a14b (patch)
treecfae798208747b2131a2065358cb9c91e2fc4d11
parent742b3d203c555eacaaf6a27cb2ccabe0fda7061f (diff)
alias
-rw-r--r--Makefile5
-rw-r--r--alias.c136
2 files changed, 140 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 38d4b9b..665dd94 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CFLAGS=-Wall -g
LDFLAGS=-Wall -lm -g
GTKFLAGS=`pkg-config --cflags --libs gtk+-2.0`
-all: jinc filters fft-test bluenoise trans noise2 voidcluster
+all: jinc filters fft-test bluenoise trans noise2 voidcluster alias
filters: fft.c fft.h filters.c
$(CC) -o filters fft.c filters.c $(LDFLAGS)
@@ -24,3 +24,6 @@ noise2: noise2.c fft.c fft.h image.c image.h
voidcluster: voidcluster.c image.h fft.c
$(CC) $(GTKFLAGS) -o voidcluster fft.c voidcluster.c image.c $(LDFLAGS)
+
+alias: alias.c image.h image.c
+ $(CC) $(GTKFLAGS) -o alias alias.c fft.c image.c $(LDFLAGS)
diff --git a/alias.c b/alias.c
new file mode 100644
index 0000000..2c13ed8
--- /dev/null
+++ b/alias.c
@@ -0,0 +1,136 @@
+#include <math.h>
+#include "image.h"
+
+typedef double (* function_t) (double x);
+
+#define SCALE 32.0
+
+typedef struct
+{
+ double r, g, b;
+} color_t;
+
+static void
+plot (complex_image_t *image, color_t color, function_t f)
+{
+ int i;
+ double max, min;
+ int first = 1;
+ double a, b;
+
+ for (i = 0; i < image->width; ++i)
+ {
+ double v = f(i / SCALE);
+
+ if (first || v < min)
+ min = v;
+ if (first || v > max)
+ max = v;
+
+ first = 0;
+ }
+
+ if (max - min < DBL_EPSILON)
+ return;
+
+ max = 2;
+ min = -2;
+
+ printf ("min %f max %f\n", min, max);
+
+ a = (image->height - 1) / (max - min);
+ b = - (min * (image->height - 1)) / (max - min);
+
+ for (i = 0; i < image->width; ++i)
+ {
+ double v = f (i / SCALE);
+ int idx;
+ complex_t p;
+
+ v = a * v + b;
+
+ printf ("scaled: %f\n", v);
+
+ idx = v + 0.5;
+
+ if (idx > image->height - 1)
+ idx = image->height - 1;
+ if (idx < 0)
+ idx = 0;
+
+ idx = idx * image->width + i;
+
+ p.re = 0;
+ p.im = 0;
+
+ image->red[idx] = image->green[idx] = image->blue[idx] = p;
+
+ image->red[idx].re = color.r;
+ image->green[idx].re = color.g;
+ image->blue[idx].re = color.b;
+ }
+}
+
+static const color_t red =
+{
+ 1.0, 0.0, 0.0
+};
+
+static const color_t blue =
+{
+ 0.4, 0.4, 1.0
+};
+
+static const color_t green =
+{
+ 0.0, 1.0, 0.0
+};
+
+static const color_t yellow =
+{
+ 1.0, 1.0, 0.0
+};
+
+#define HIGH (2)
+#define ALIAS (1)
+
+static double
+orig (double x)
+{
+ double v = (0.4 * sin (HIGH * x) + sin (0.3 * x));
+
+ return v;
+}
+
+static double
+antialiased (double x)
+{
+ double v = sin (0.3 * x);
+
+ return v;
+}
+
+static double
+imperfect (double x)
+{
+ return sin (0.3 * x) + 0.1 * sin (ALIAS * x);
+}
+
+static double
+aliased (double x)
+{
+ return sin (0.3 * x) + 0.4 * sin (ALIAS * x);
+}
+
+int
+main ()
+{
+ complex_image_t *image = complex_image_new (1024, 256);
+
+ plot (image, red, orig);
+ plot (image, blue, antialiased);
+ plot (image, green, aliased);
+ plot (image, yellow, imperfect);
+
+ complex_image_show ("func", image, CONVERT_RE);
+}