forked from sysprog21/rv32emu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.c
422 lines (366 loc) · 12.7 KB
/
line.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Copyright (c) 2023 National Cheng Kung University. All rights reserved.
* Copyright (C) 2017 Milo Yip. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of pngout nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Source: https://github.com/miloyip/line
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
/* Write a byte */
#define SVPNG_PUT(u) fputc(u, fp)
/*!
* \brief Save a RGB/RGBA image in PNG format.
* \param FILE *fp Output stream (by default using file descriptor).
* \param w Width of the image. (<16383)
* \param h Height of the image.
* \param img Image pixel data in 24-bit RGB or 32-bit RGBA format.
* \param alpha Whether the image contains alpha channel.
*/
void svpng(FILE *fp, unsigned w, unsigned h, const uint8_t *img, bool alpha)
{
static const unsigned t[] = {
0,
0x1db71064,
0x3b6e20c8,
0x26d930ac,
0x76dc4190,
0x6b6b51f4,
0x4db26158,
0x5005713c,
/* CRC32 Table */ 0xedb88320,
0xf00f9344,
0xd6d6a3e8,
0xcb61b38c,
0x9b64c2b0,
0x86d3d2d4,
0xa00ae278,
0xbdbdf21c,
};
unsigned a = 1, b = 0, c, p = w * (alpha ? 4 : 3) + 1, x, y;
#define SVPNG_U8A(ua, l) \
for (size_t i = 0; i < l; i++) \
SVPNG_PUT((ua)[i]);
#define SVPNG_U32(u) \
do { \
SVPNG_PUT((u) >> 24); \
SVPNG_PUT(((u) >> 16) & 255); \
SVPNG_PUT(((u) >> 8) & 255); \
SVPNG_PUT((u) &255); \
} while (0)
#define SVPNG_U8C(u) \
do { \
SVPNG_PUT(u); \
c ^= (u); \
c = (c >> 4) ^ t[c & 15]; \
c = (c >> 4) ^ t[c & 15]; \
} while (0)
#define SVPNG_U8AC(ua, l) \
for (size_t i = 0; i < l; i++) \
SVPNG_U8C((ua)[i])
#define SVPNG_U16LC(u) \
do { \
SVPNG_U8C((u) &255); \
SVPNG_U8C(((u) >> 8) & 255); \
} while (0)
#define SVPNG_U32C(u) \
do { \
SVPNG_U8C((u) >> 24); \
SVPNG_U8C(((u) >> 16) & 255); \
SVPNG_U8C(((u) >> 8) & 255); \
SVPNG_U8C((u) &255); \
} while (0)
#define SVPNG_U8ADLER(u) \
do { \
SVPNG_U8C(u); \
a = (a + (u)) % 65521; \
b = (b + a) % 65521; \
} while (0)
#define SVPNG_BEGIN(s, l) \
do { \
SVPNG_U32(l); \
c = ~0U; \
SVPNG_U8AC(s, 4); \
} while (0)
#define SVPNG_END() SVPNG_U32(~c)
SVPNG_U8A("\x89PNG\r\n\32\n", 8); /* Magic */
SVPNG_BEGIN("IHDR", 13); /* IHDR chunk { */
SVPNG_U32C(w);
SVPNG_U32C(h); /* Width & Height (8 bytes) */
SVPNG_U8C(8);
/* Depth=8, Color=True color with/without alpha (2 bytes) */
SVPNG_U8C(alpha ? 6 : 2);
/* Compression=Deflate, Filter=No, Interlace=No (3 bytes) */
SVPNG_U8AC("\0\0\0", 3);
SVPNG_END(); /* } */
SVPNG_BEGIN("IDAT", 2 + h * (5 + p) + 4); /* IDAT chunk { */
SVPNG_U8AC("\x78\1", 2); /* Deflate block begin (2 bytes) */
/* Each horizontal line makes a block for simplicity */
for (y = 0; y < h; y++) {
/* 1 for the last block, 0 for others (1 byte) */
SVPNG_U8C(y == h - 1);
SVPNG_U16LC(p);
/* Size of block in little endian and its 1's complement (4 bytes) */
SVPNG_U16LC(~p);
SVPNG_U8ADLER(0); /* No filter prefix (1 byte) */
for (x = 0; x < p - 1; x++, img++)
SVPNG_U8ADLER(*img); /* Image pixel data */
}
SVPNG_U32C((b << 16) | a); /* Deflate block end with adler (4 bytes) */
SVPNG_END(); /* } */
SVPNG_BEGIN("IEND", 0);
SVPNG_END(); /* IEND chunk {} */
}
#include <string.h> // memset()
#define Q (20)
#define Q_PI (1686629713 >> (29 - Q))
/* 32-bit Q notation to specify a fixed point number format. */
typedef int32_t qfixed_t;
/* 64-bit Q format buffer, for handling overflow cases */
typedef int64_t qbuf_t;
/* format convertion: float to Q format */
#define f2Q(x) ((qfixed_t) ((x) * (1 << Q)))
/* format convertion: Q format to float */
#define Q2f(x) (((float) (x)) / (1 << Q))
/* format convertion: Q format to int */
#define Q2I(x) ((int) ((x) >> Q))
/* format convertion: int to Q format */
#define I2Q(x) ((qfixed_t) ((x) << Q))
/* maximum value of Q format */
#define QFMT_MAX 0x7FFFFFFF
/* minimum value of Q format */
#define QFMT_MIN 0x80000000
/* addition of Q format value */
static inline qfixed_t q_add(qfixed_t a, qfixed_t b)
{
qbuf_t tmp = (qbuf_t) a + (qbuf_t) b;
if (tmp > (qbuf_t) QFMT_MAX)
return (qfixed_t) QFMT_MAX;
if (~tmp + 1 >= (qbuf_t) QFMT_MIN)
return (qfixed_t) QFMT_MIN;
return (qfixed_t) tmp;
};
/* multiplication of Q format value */
static inline qfixed_t q_mul(qfixed_t a, qfixed_t b)
{
qbuf_t tmp = (qbuf_t) a * (qbuf_t) b;
/* rounding and resize */
tmp += (qbuf_t) (1 << (Q - 1));
tmp >>= Q;
/* check overflow */
if (tmp > (qbuf_t) QFMT_MAX)
return (qfixed_t) QFMT_MAX;
if (tmp * -1 >= (qbuf_t) QFMT_MIN)
return (qfixed_t) QFMT_MIN;
return (qfixed_t) tmp;
}
/* division of Q format value */
static inline qfixed_t q_div(qfixed_t a, qfixed_t b)
{
qbuf_t tmp = (qbuf_t) a << Q;
if ((tmp >= 0 && b >= 0) || (tmp < 0 && b < 0)) {
tmp += (b >> 1);
} else {
tmp -= (b >> 1);
}
return (qfixed_t) (tmp / b);
}
/* return the largest integral value that is not greater than x */
static inline qfixed_t q_floor(qfixed_t x)
{
qfixed_t mask = (0xFFFFFFFF >> Q) << Q;
return x & mask;
}
/* return the smallest integral value that is not less than x */
static inline qfixed_t q_ceil(qfixed_t x)
{
qfixed_t mask = (0xFFFFFFFF >> Q) << Q;
qfixed_t delta = x & ~mask;
x = x & mask;
return delta ? q_add(x, 1 << Q) : x;
}
/* return the nonnegative square root of x */
static inline qfixed_t q_sqrt(qfixed_t x)
{
if (x <= 0)
return 0;
if (x < I2Q(1) + (1 << (Q / 2 - 1)) && x > I2Q(1) - (1 << (Q / 2 - 1)))
return I2Q(1);
qfixed_t res = 0;
qfixed_t bit = 1 << 15;
/* left shift x as more as possible */
int offset = 0;
for (qfixed_t i = x; !(0x40000000 & i); i <<= 1)
++offset;
offset = (offset & ~1);
x <<= offset;
/* shift bit to the highest bit 1' in x */
while (bit > x)
bit >>= 1;
for (; bit > 0; bit >>= 1) {
int tmp = bit + res;
/* check overflow: 46341^2 > 2^31 - 1, which is the maximun value */
if (tmp > 46340)
continue;
int square = tmp * tmp;
if (square <= x) {
res = tmp;
if (square == x)
break;
}
} /* iter: goto next lower bit to get more precise sqrt value */
offset >>= 1;
offset -= (Q >> 1);
return (offset >= 0) ? res >> offset : res << (-offset);
}
/* get both sin and cos value in the same radius */
static inline void q_sincos(qfixed_t radius, qfixed_t *sin_t, qfixed_t *cos_t)
{
int region = (radius / (Q_PI >> 1)) & 0b11;
/* theta must be pi/2 to 0 and start from x-axis */
qfixed_t theta = radius % (Q_PI >> 1);
if (region & 0b1)
theta = (Q_PI >> 1) - theta;
/* start from cos(pi/2) and sin(pi/2) */
radius = Q_PI >> 1;
qfixed_t cos_rad = 0;
qfixed_t sin_rad = I2Q(1);
/* start from cos(0) and sin(0) */
*cos_t = I2Q(1);
*sin_t = 0;
while (radius > 0) {
if (radius <= theta) {
theta -= radius;
/*
* Trigonometric Identities:
* cos(a + b) = cos(a)*cos(b) - sin(a)sin(b)
* sin(a + b) = sin(a)*cos(b) + cos(a)sin(b)
*/
qfixed_t tmp = q_mul(*cos_t, cos_rad) - q_mul(*sin_t, sin_rad);
*sin_t = q_mul(*sin_t, cos_rad) + q_mul(*cos_t, sin_rad);
*cos_t = tmp;
}
if (theta == 0)
break;
/* Half angle formula to approach the value */
radius >>= 1;
sin_rad = q_sqrt((I2Q(1) - cos_rad) >> 1);
cos_rad = q_sqrt((I2Q(1) + cos_rad) >> 1);
}
if (region == 0b10 || region == 0b01)
*cos_t = ~*cos_t + 1;
if (region & 0b10)
*sin_t = ~*sin_t + 1;
}
#define W 512
#define H 512
static uint8_t img[W * H * 3];
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
/*
* Using signed distnace field (SDF) of capsule shape to perform anti-aliasing
* with single sample per pixel.
*/
qfixed_t capsuleSDF(qfixed_t px,
qfixed_t py,
qfixed_t ax,
qfixed_t ay,
qfixed_t bx,
qfixed_t by,
qfixed_t r)
{
qfixed_t pax = q_add(px, -ax);
qfixed_t pay = q_add(py, -ay);
qfixed_t bax = q_add(bx, -ax);
qfixed_t bay = q_add(by, -ay);
qfixed_t t0 = q_add(q_mul(pax, bax), q_mul(pay, bay));
qfixed_t t1 = q_add(q_mul(bax, bax), q_mul(bay, bay));
qfixed_t tmp = min(q_div(t0, t1), I2Q(1));
qfixed_t h = max(tmp, 0);
qfixed_t dx = q_add(pax, -q_mul(bax, h));
qfixed_t dy = q_add(pay, -q_mul(bay, h));
tmp = q_add(q_mul(dx, dx), q_mul(dy, dy));
qfixed_t res = q_add(q_sqrt(tmp), -r);
return res;
}
#define PUT(v) \
p[v] = (uint8_t) Q2I( \
q_add(p[v] * q_add((1 << Q), -alpha), q_mul(g, alpha) * 255))
/* Render shapes into the buffer individually with alpha blending. */
void alphablend(int x,
int y,
qfixed_t alpha,
qfixed_t r,
qfixed_t g,
qfixed_t b)
{
uint8_t *p = img + (y * W + x) * 3;
PUT(0);
PUT(1);
PUT(2);
}
#undef PUT
/* Use AABB of capsule to reduce the number of samples. */
void lineSDFAABB(qfixed_t ax, qfixed_t ay, qfixed_t bx, qfixed_t by, qfixed_t r)
{
int x0 = Q2I(q_floor(q_add(min(ax, bx), -r)));
int x1 = Q2I(q_ceil(q_add(max(ax, bx), r)));
int y0 = Q2I(q_floor(q_add(min(ay, by), -r)));
int y1 = Q2I(q_ceil(q_add(max(ay, by), r)));
for (int y = y0; y <= y1; y++) {
for (int x = x0; x <= x1; x++)
alphablend(x, y,
max(min((1 << (Q - 1)) - capsuleSDF(I2Q(x), I2Q(y), ax,
ay, bx, by, r),
I2Q(1)),
0),
0, 0, 0);
}
}
int main()
{
memset(img, 255, sizeof(img));
/* center of the line drawing, (1 << (Q - 1)) is equal to 0.5 */
qfixed_t cx = W * (1 << (Q - 1)), cy = H * (1 << (Q - 1));
/* cos(theta) and sin(theta) value for each line */
qfixed_t ct, st;
for (int j = 0; j < 5; j++) {
qfixed_t r1 = min(W, H) * q_mul((I2Q(j) + (1 << (Q - 1))), f2Q(0.085f));
qfixed_t r2 = min(W, H) * q_mul((I2Q(j) + (3 << (Q - 1))), f2Q(0.085f));
qfixed_t t = j * q_div(Q_PI, I2Q(64));
qfixed_t r = (j + 1) * (1 << (Q - 1));
for (int i = 1; i <= 64; i++) {
t = q_add(t, q_mul(I2Q(2), q_div(Q_PI, I2Q(64))));
q_sincos(t, &st, &ct);
lineSDFAABB(q_add(cx, q_mul(r1, ct)), q_add(cy, -q_mul(r1, st)),
q_add(cx, q_mul(r2, ct)), q_add(cy, -q_mul(r2, st)), r);
}
}
svpng(fopen("line.png", "wb"), W, H, img, false);
}