summaryrefslogtreecommitdiff
path: root/alias.c
blob: 2c13ed8f76ed50c81eca9355105353d43256706a (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
#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);
}