-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbezier.c
295 lines (263 loc) · 9.16 KB
/
bezier.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//#include "main.h"
#include "bezier.h"
#include "2dvector.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define WIDTH 320
#define HEIGHT 200
/*
void bezier_draw_path_partial(Bitmap *dest, bezier_t *data, int len, int percent){
for(int i=0; i<len; ++i){
point_t a,b,p;
int x,y;
point_t* bez = (point_t*) (data + i);
for(int t=0; t<BEZ_STEPS; ++t){
a = padd(bez[0], pscale_bez(psub(bez[1],bez[0]),t));
b = padd(bez[1], pscale_bez(psub(bez[2],bez[1]),t));
p = padd(a, pscale_bez(psub(b,a),t));
bezier_setpixel(dest, p,255);
}
#ifdef SHOW_CONTROL_POINTS
bezier_setpixel(dest, bez.p[2],7<<5);
bezier_setpixel(dest, bez.p[1],7<<2);
#endif //SHOW_CONTROL_POINTS
}
}*/
void bezier_draw_partial(Bitmap *dest, bezier_t bez, int ratio){
// ratio is between 0 and 128. Or negative for drawing only the end, not the beginning.
if(ratio<0){
// turn around bez
ratio = -ratio;
point_t tmp;
tmp = bez.p[0];
bez.p[0] = bez.p[2];
bez.p[2] = tmp;
}
if(ratio>=128){
bezier_draw(dest, bez);
return;
}
// calculate sub-curve
point_t p1 = padd(pscale(bez.p[0], 128-ratio, 7), pscale(bez.p[1], ratio, 7));
point_t p2 = padd(pscale(bez.p[1], 128-ratio, 7), pscale(bez.p[2], ratio, 7));
point_t pm = padd(pscale(p1, 128-ratio, 7), pscale(p2, ratio, 7));
bezier_t bez2;
bez.p[1] = p1;
bez.p[2] = pm;
bezier_draw(dest, bez);
}
void bezier_draw(Bitmap *dest, bezier_t bez){
static bezier_t stack[15];
int stackpos = 0;
stack[0] = bez;
//TODO: draw last point of curve
while(stackpos >=0){
bezier_t *b = &stack[stackpos--];
int len = plen(b->p[0],b->p[1])+plen(b->p[1],b->p[2]);
if(len>BEZ_SCALEDOWN*2){
point_t p1, p2, pm, bp2;
p1 = pavg(b->p[0], b->p[1]);
p2 = pavg(b->p[1], b->p[2]);
pm = pavg(p1,p2);
bezier_t *t;
t = &stack[++stackpos];
t->p[0] = b->p[0];
t->p[1] = p1;
bp2 = b->p[2]; // rescue value before writing there
t->p[2] = pm;
t = &stack[++stackpos];
t->p[0] = pm;
t->p[1] = p2;
t->p[2] = bp2;
} else {
bezier_setpixel(dest, b->p[0], 0xff);// RawRGB(5,5,2));
bezier_setpixel(dest, b->p[1], 0xff);//RawRGB(5,5,2));
}
}
// bezier_setpixel(dest, bez.p[0],255);
// bezier_setpixel(dest, bez.p[2],255);
}
void bezier_draw_colored(Bitmap *dest, bezier_t bez, uint8_t color){
static bezier_t stack[15];
int stackpos = 0;
stack[0] = bez;
//TODO: draw last point of curve
while(stackpos >=0){
bezier_t *b = &stack[stackpos--];
int len = plen(b->p[0],b->p[1])+plen(b->p[1],b->p[2]);
if(len>BEZ_SCALEDOWN*2){
point_t p1, p2, pm, bp2;
p1 = pavg(b->p[0], b->p[1]);
p2 = pavg(b->p[1], b->p[2]);
pm = pavg(p1,p2);
bezier_t *t;
t = &stack[++stackpos];
t->p[0] = b->p[0];
t->p[1] = p1;
bp2 = b->p[2]; // rescue value before writing there
t->p[2] = pm;
t = &stack[++stackpos];
t->p[0] = pm;
t->p[1] = p2;
t->p[2] = bp2;
} else {
bezier_setpixel(dest, b->p[0], color);
bezier_setpixel(dest, b->p[1], color);
}
}
}
void bezier_setpixel(Bitmap *dest, point_t p, uint8_t colour){
int x,y, distance;
x = p.x / BEZ_SCALEDOWN;
y = p.y / BEZ_SCALEDOWN;
// printf("draw: (%i,%i)\n", (int)x, (int)y);
if(x>=WIDTH || x<0){
// printf("x: %i \n",(int)x);
return;
}
if(y>=HEIGHT|| y<0){
// printf("y: %i \n",(int)y);
return;
}
distance = abs((p.x % BEZ_SCALEDOWN)-BEZ_SCALEDOWN/2);
distance += abs((p.y % BEZ_SCALEDOWN)-BEZ_SCALEDOWN/2);
/* static int ctr;
ctr++;
if(ctr>10000){
printf("%i\n",distance);
ctr=0;
}*/
int intensity = 9-(distance * 4) / BEZ_SCALEDOWN;
if(intensity<0)
intensity=0;
if(intensity>8)
intensity=8;
// image[p.x+(p.y)*WIDTH]=colour;
uint8_t oldpixel = dest->pixels[x+(HEIGHT-y-1)*WIDTH];
int colour_decomposed = (colour * ((1<<(8+16))|(1<<(6+8))|(1<<3))) & 0x03070700;
int oldpixel_decomposed = (oldpixel * ((1<<(8+16))|(1<<(6+8))|(1<<3))) & 0x03070700;
int colour_scaled = (colour_decomposed*intensity+0x03010100)>>3;
int newcolour = (colour_scaled) & 0x03070700; //FIXME: add oldpixel here or something
int newcolour2 = ((newcolour * ((1)|(1<<(2+8))|(1<<(5+16)))) & 0xff000000)>>24;
// printf("%x %x %x %x\n",colour, newcolour, colour_scaled, newcolour);
dest->pixels[x+(HEIGHT-y-1)*WIDTH] = newcolour2;
#ifdef DEBUG
callcount_setpixel++;
#endif //DEBUG
}
/*int bezier_len(bezier_t b){
int r = pdist(p.b[0],b.p[1]);
r += pdist(p.b[1],b.p[2]);
return(r);
}*/
inline void bezier_setpixel_additive(Bitmap* dest, int x, int y, int color)
{
uint8_t a = dest->pixels[x+(HEIGHT-y-1)*WIDTH];
uint8_t b = color;
// add
uint8_t halfa=(a>>1)&(PixelAllButHighBits);
uint8_t halfb=(b>>1)&(PixelAllButHighBits);
uint8_t carry=a&b&(PixelLowBits);
dest->pixels[x+(HEIGHT-y-1)*WIDTH] = halfa+halfb+carry + RawRGB(0,0,0);
}
fillpixel_t* bezier_fill_writepixels(Bitmap *dest, bezier_t bez, fillpixel_t* pixelptr, uint8_t color){
// TODO: maxpixels
static bezier_t stack[15];
int stackpos = 0;
stack[0] = bez;
//TODO: draw last point of curve
while(stackpos >=0){
bezier_t *b = &stack[stackpos--];
int len = plen(b->p[0],b->p[1])+plen(b->p[1],b->p[2]);
if(len>BEZ_SCALEDOWN*2){
point_t p1, p2, pm, bp2;
p1 = pavg(b->p[0], b->p[1]);
p2 = pavg(b->p[1], b->p[2]);
pm = pavg(p1,p2);
bezier_t *t;
t = &stack[++stackpos];
t->p[0] = b->p[0];
t->p[1] = p1;
bp2 = b->p[2]; // rescue value before writing there
t->p[2] = pm;
t = &stack[++stackpos];
t->p[0] = pm;
t->p[1] = p2;
t->p[2] = bp2;
} else {
static int lastx, lasty;
bool rising = ( b->p[2].y > (b->p[0].y));
for(int i=0; i<2; ++i){
int x = b->p[i].x / BEZ_SCALEDOWN;
int y = b->p[i].y / BEZ_SCALEDOWN;
if(x>=WIDTH || x<0){
continue;
}
if(y>=HEIGHT|| y<0){
continue;
}
if(x==lastx && y==lasty){
continue;
}
lastx = x; lasty = y;
pixelptr->y = y;
pixelptr->x = x;
pixelptr->action = rising ? 0 : 1;
pixelptr++;
// paint border pixels
//bezier_setpixel(dest, b->p[i], color);
bezier_setpixel_additive(dest, x, y, color);
//dest->pixels[x+(HEIGHT-y-1)*WIDTH] = color;
if(len<BEZ_SCALEDOWN){
break; // don't draw second pixel when bezier is tiny.
}
}
// bezier_setpixel(dest, b->p[0], color);
// bezier_setpixel(dest, b->p[1], color);
}
}
return pixelptr;
}
static int cmp_fillpixel(const void* p1, const void* p2){
fillpixel_t* fp1 = (fillpixel_t*)p1;
fillpixel_t* fp2 = (fillpixel_t*)p2;
//return ((fp1->y*65536 + fp1->x - 2*fp1->action) - (fp2->y*65536 + fp2->x - 2*fp2->action));
return ((fp1->y*65536 | fp1->x*4) - 1*fp1->action) - (((fp2->y*65536 | fp2->x*4) - 1*fp2->action));
// return ((fp1->y*65536 | fp1->x)) - (((fp2->y*65536 | fp2->x)));
// return *((int32_t*)p1) - *((int32_t*)p2);
}
void floodfill(Bitmap *dest, fillpixel_t* start, fillpixel_t* end, uint8_t color){
// sort
qsort(start, end-start, sizeof(fillpixel_t), cmp_fillpixel);
// sentinel
end->x=WIDTH;
end->y=HEIGHT;
end->action=1; // (the loop will stop/iterate on every endpixel, but not on every startpixel)
fillpixel_t* pptr = start;
while((pptr)<=end){
// require a start followed by end, on the same line
if( (pptr->action != 0) || ((pptr+1)->action != 1) || (pptr->y != (pptr+1)->y) ){
pptr++;
continue;
}
int xstart = pptr->x + 1; // xstart indexes the first pixel to be touched
int y = pptr->y;
pptr++;
int xend = pptr->x; // the pixel pointed to by xend will not be touched
// memset(dest->pixels+xstart+(HEIGHT-y-1)*WIDTH, color, xend-xstart);
if(xend-xstart >= 10){ // quick-and-dirty hack for greet floodfill
pptr++;
continue;
}
for(int i=xstart; i<xend; i++){
bezier_setpixel_additive(dest, i, y, color);
// dest->pixels[i+(HEIGHT-y-1)*WIDTH] = color; // do floodfill
}
if(xend-xstart > 10)
{
printf("%i %i %i\n",xstart, xend, y);
}
pptr++;
}
}