summaryrefslogtreecommitdiff
path: root/render.c
blob: e78d91b5c709bc7d3f82ae5c23cc3209b24e92d8 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>

#include "config.h"
#include "color.h"
#include "bmp.h"

/* Adding submap i to all submaps > i to reconstruct the result of single buddhabrot calculations. */
static void
add_submaps(config_t* conf, uint32_t* map) {
	int    i, j;
	size_t k, mapsize, off_i, off_j;

	mapsize = conf->width * conf->height;

	for(i = conf->iters_n - 1; i <= 0; i--) {
		off_i = mapsize * i;
		for(j = 0; j < i; j++) {
			off_j = mapsize * j;
			for(k = 0; k < mapsize; k++) {
				map[off_i + k] += map[off_j + k];
			}
		}
	}
}

typedef struct lookup_elem_t {
	uint32_t              val;
	struct lookup_elem_t* prev;
	struct lookup_elem_t* next;
} lookup_elem_t;

static lookup_elem_t*
new_lookup_elem(uint32_t val, lookup_elem_t* prev, lookup_elem_t* next) {
	lookup_elem_t* rv = malloc(sizeof(lookup_elem_t));
	if(!rv) {
		return NULL;
	}

	rv->val  = val;
	rv->prev = prev;
	rv->next = next;

	return rv;
}

static int
maybe_insert(uint32_t val, lookup_elem_t* a, lookup_elem_t* b) {
	lookup_elem_t* new;

	if(!(new = new_lookup_elem(val, a, b))) {
		return 0;
	}

	if(!a) {
		b->prev = new;
		return 1;
	}
	if(!b) {
		a->next = new;
		return 1;
	}

	if((a->val < val) && (val < b->val)) {
		a->next = new;
		b->prev = new;
	} else {
		free(new);
	}
	return 1;
}

static int
build_lookup(uint32_t* map, size_t mapsize, lookup_elem_t** e) {
	size_t   i;
	uint32_t val;

	for(i = 0; i < mapsize; i++) {
		val = map[i];

		if(!*e) {
			if(!(*e = new_lookup_elem(val, NULL, NULL))) {
				return 0;
			}
			continue;
		}

		for(;; ) {
			if((*e)->val == val) {
				break;
			}

			if(val > (*e)->val) {
				if(!maybe_insert(val, *e, (*e)->next)) {
					return 0;
				}
				*e = (*e)->next;
			} else {
				if(!maybe_insert(val, (*e)->prev, *e)) {
					return 0;
				}
				*e = (*e)->prev;
			}
		}
	}

	while((*e)->prev) {
		*e = (*e)->prev;
	}

	return 1;
}

static size_t
lookup_len(lookup_elem_t* e) {
	size_t len;

	if(!e) {
		return 0;
	}

	while(e->prev) {
		e = e->prev;
	}

	len = 0;
	while(e) {
		len++;
		e = e->next;
	}
	return len;
}

static void
lookup_destroy(lookup_elem_t* e) {
	lookup_elem_t* next;
	while(e->prev) {
		e = e->prev;
	}
	while(e) {
		next = e->next;
		free(e);
		e = next;
	}
}

static size_t
lookup(uint32_t val, lookup_elem_t** e, size_t* pos) {
	while((*e)->val != val) {
		if(val < (*e)->val) {
			*e = (*e)->prev;
			(*pos)--;
		} else {
			*e = (*e)->next;
			(*pos)++;
		}
	}
	return *pos;
}

int
render(config_t* conf, uint32_t* map) {
	int                 rv = 0;
	size_t              i, j;
	color_t             col;
	double              factor;
	bmp_write_handle_t* bmph    = NULL;
	size_t              mapsize = conf->width * conf->height;

	lookup_elem_t** lookups = NULL;
	size_t*         poss    = NULL;
	size_t*         lens    = NULL;

	lookups = calloc(sizeof(lookup_elem_t*), conf->iters_n);
	poss    = calloc(sizeof(size_t), conf->iters_n);
	lens    = calloc(sizeof(size_t), conf->iters_n);
	if((!lookups) || (!poss) || (!lens)) {
		fputs("Could not allocate memory for lookup tables.\n", stderr);
		goto tidyup;
	}

	if(!(bmph = bmp_create(conf->output, conf->width, conf->height))) {
		fprintf(stderr, "Could not create BMP.\n");
		/* TODO: More details? */
		goto tidyup;
	}

	add_submaps(conf, map);

	for(i = 0; i < conf->iters_n; i++) {
		if(!build_lookup(map + (mapsize * i), mapsize, &(lookups[i]))) {
			fputs("Could not build lookup table.\n", stderr);
			goto tidyup;
		}
		lens[i] = lookup_len(lookups[i]);
	}

	for(i = 0; i < mapsize; i++) {
		col.r = 0;
		col.g = 0;
		col.b = 0;

		for(j = 0; j < conf->iters_n; j++) {
			factor = (double) lookup(map[i + mapsize * j], &(lookups[j]), &(poss[j])) / (double) lens[j];
			col    = color_add(col, color_mul(conf->colors[j], factor));
		}
		if(!bmp_write_pixel(bmph, color_fix(col))) {
			fputs("Could not write pixel data.\n", stderr);
			goto tidyup;
		}
	}

	rv = 1;

tidyup:
	if(bmph) {
		bmp_destroy(bmph);
	}
	if(lookups) {
		for(i = 0; i < conf->iters_n; i++) {
			if(lookups[i]) {
				lookup_destroy(lookups[i]);
			}
		}
		free(lookups);
	}
	if(poss) {
		free(poss);
	}
	if(lens) {
		free(lens);
	}

	return rv;
}