-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro-draw.h
More file actions
1781 lines (1612 loc) · 40.7 KB
/
micro-draw.h
File metadata and controls
1781 lines (1612 loc) · 40.7 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////
// SPDX-License-Identifier: MIT
//
// micro-draw.h
// ============
//
// Header-only software renderer in C99.
//
// Author: Giovanni Santini
// Mail: giovanni.santini@proton.me
// License: MIT
//
//
// Features
// --------
//
// - lines
// - rectangles
// - circles
// - triangles
// - grids
// - text
// - color RGBA, Black&White, easily add more formats
// - PPM file reading and writing
// - resize
// - overlap
//
// Usage
// -----
//
// Do this:
//
// #define MICRO_DRAW_IMPLEMENTATION
//
// before you include this file in *one* C or C++ file to create the
// implementation.
//
// i.e. it should look like this:
//
// #include ...
// #include ...
// #include ...
// #define MICRO_DRAW_IMPLEMENTATION
// #include "micro-draw.h"
//
// You can tune the library by #defining certain values. See the
// "Config" comments under "Configuration" below.
//
// To enable PPM related functions, you need to #define MIRO_DRAW_PPM.
// More info: https://en.wikipedia.org/wiki/Netpbm
//
// The usage is quite straight forward: you supply a data buffer to a
// micro-draw.h function which will fill the pixels accordingly. For
// example, you can use this buffer to render a frame on screen, or to
// save it as an image file, or do whatever you want.
//
// Check out some examples in the `test` directory.
//
//
// Code
// ----
//
// The official git repository of micro-draw.h is hosted at:
//
// https://github.com/San7o/micro-draw.h
//
// This is part of a bigger collection of header-only C99 libraries
// called "micro-headers", contributions are welcome:
//
// https://github.com/San7o/micro-headers
//
//
// TODO
// ----
//
// - Implement micro_draw_from_ppm
//
#ifndef MICRO_DRAW
#define MICRO_DRAW
#define MICRO_DRAW_MAJOR 0
#define MICRO_DRAW_MINOR 1
#ifdef __cplusplus
extern "C" {
#endif
//
// Configuration
//
// Config: enable PPM format reading / writing with MICRO_DRAW_PPM
#if 0
#define MICRO_DRAW_PPM
#endif
// Config: Prefix for all functions
// For function inlining, set this to `static inline` and then define
// the implementation in all the files
#ifndef MICRO_DRAW_DEF
#define MICRO_DRAW_DEF extern
#endif
// I/O operations
#ifndef MICRO_DRAW_FOPEN
#include <stdio.h>
#define MICRO_DRAW_FOPEN fopen
#define MICRO_FILE_MODE_READ "r"
#define MICRO_FILE_MODE_WRITE "w+"
#endif
#ifndef MICRO_DRAW_FCLOSE
#define MICRO_DRAW_FCLOSE fclose
#endif
#ifndef MICRO_DRAW_FREAD
#define MICRO_DRAW_FREAD fread
#endif
#ifndef MICRO_DRAW_FWRITE
#define MICRO_DRAW_FWRITE fwrite
#endif
#ifndef MICRO_DRAW_OUT
#define MICRO_DRAW_OUT printf
#endif
// Memory operations
#ifndef MICRO_DRAW_MALLOC
#define MICRO_DRAW_MALLOC malloc
#endif
#ifndef MICRO_DRAW_FREE
#define MICRO_DRAW_FREE free
#endif
//
// Types
//
typedef enum {
MICRO_DRAW_RGBA8 = 0,
MICRO_DRAW_BLACK_WHITE,
_MICRO_DRAW_PIXEL_MAX,
} MicroDrawPixel;
typedef enum {
MICRO_DRAW_OK = 0,
MICRO_DRAW_ERROR_OPEN_FILE,
MICRO_DRAW_ERROR_INVALID_MAGIC_NUMBER,
MICRO_DRAW_ERROR_INVALID_IMAGE_SIZE,
_MICRO_DRAW_ERROR_MAX,
} MicroDrawError;
#define MICRO_DRAW_FONT_HEIGHT 6
#define MICRO_DRAW_FONT_WIDTH 5
extern unsigned char
micro_draw_font[128][MICRO_DRAW_FONT_HEIGHT][MICRO_DRAW_FONT_WIDTH];
// Default character size in pixel
#define MICRO_DRAW_CHARACTER_PIXELS_X 50
#define MICRO_DRAW_CHARACTER_PIXELS_Y 50
typedef struct {
unsigned char* data;
int width;
int height;
MicroDrawPixel pixel;
} MicroDrawCanvas;
typedef struct {
char* text;
int x;
int y;
float scale;
} MicroDrawText;
#ifndef MICRO_LA
typedef struct {
int c_x; int c_y;
int r;
} Circle;
// Vertexes are in clockwise order
typedef struct {
int a_x; int a_y;
int b_x; int b_y;
int c_x; int c_y;
int d_x; int d_y;
} Rect;
typedef struct {
int a_x; int a_y;
int b_x; int b_y;
int c_x; int c_y;
} Triangle;
typedef struct {
int x;
int y;
} Vec2;
#else
typedef Recti Rect;
typedef Circlei Circle;
typedef Trianglei Triangle;
typedef Vec2i Vec2;
#endif // MICRO_LA
//
// Function declarations
//
// Canvas ------------------------------------------------------------
MICRO_DRAW_DEF void
micro_draw_canvas_init(MicroDrawCanvas *canvas,
int width, int height,
MicroDrawPixel pixel);
MICRO_DRAW_DEF void micro_draw_canvas_free(MicroDrawCanvas *canvas);
// Color -------------------------------------------------------------
// Return the number of channels of a pixel type
MICRO_DRAW_DEF unsigned int micro_draw_get_channels(MicroDrawPixel pixel);
// Returns the number of bytes of a single channel if a pixel type
MICRO_DRAW_DEF unsigned int micro_draw_get_channel_size(MicroDrawPixel pixel);
MICRO_DRAW_DEF void
micro_draw_color_to_rgba8(unsigned char* color_src, MicroDrawPixel pixel_src,
unsigned char color_dest[4]);
MICRO_DRAW_DEF void
micro_draw_color_from_rgba8(unsigned char color_src[4],
unsigned char *color_dest, MicroDrawPixel pixel_dest);
// Set the memory pointed by [color_dest] with the converted [pixel_dest]
// color values from [pixel_src] in [color_src].
// The user must ensure that enough memory is available for storing
// the colors.
MICRO_DRAW_DEF void
micro_draw_color_convert(unsigned char *color_src, MicroDrawPixel pixel_src,
unsigned char *color_dest, MicroDrawPixel pixel_dest);
MICRO_DRAW_DEF unsigned char*
micro_draw_color_from_rgba(unsigned char* color,
MicroDrawPixel pixel);
MICRO_DRAW_DEF void
micro_draw_get_color(MicroDrawCanvas *canvas,
Vec2 pos,
unsigned char** color);
// Primitives --------------------------------------------------------
MICRO_DRAW_DEF void
micro_draw_pixel(MicroDrawCanvas *canvas,
Vec2 pos,
unsigned char *color);
MICRO_DRAW_DEF void
micro_draw_line(MicroDrawCanvas *canvas,
Vec2 a,
Vec2 b,
unsigned char* color);
MICRO_DRAW_DEF void
micro_draw_fill_rect(MicroDrawCanvas *canvas,
Rect rect,
unsigned char *color);
MICRO_DRAW_DEF void
micro_draw_fill_circle(MicroDrawCanvas *canvas,
Circle circle,
unsigned char *color);
MICRO_DRAW_DEF void
micro_draw_fill_triangle(MicroDrawCanvas *canvas,
Triangle triangle,
unsigned char *color);
MICRO_DRAW_DEF void
micro_draw_grid(MicroDrawCanvas *canvas,
int columns, int rows,
unsigned char* color);
MICRO_DRAW_DEF void
micro_draw_clear(MicroDrawCanvas *canvas,
unsigned char *color);
// Transformations ---------------------------------------------------
MICRO_DRAW_DEF void
micro_draw_scaled(MicroDrawCanvas *source_canvas,
MicroDrawCanvas *dest_canvas);
MICRO_DRAW_DEF void
micro_draw_overlap(MicroDrawCanvas *source_canvas,
MicroDrawCanvas *dest_canvas,
Vec2 offset);
// Text --------------------------------------------------------------
MICRO_DRAW_DEF void
micro_draw_text(MicroDrawCanvas *canvas,
MicroDrawText text,
unsigned char* color);
// PPM ---------------------------------------------------------------
#ifdef MICRO_DRAW_PPM
MICRO_DRAW_DEF MicroDrawError
micro_draw_to_ppm(const char *filename, MicroDrawCanvas *canvas);
MICRO_DRAW_DEF MicroDrawError
micro_draw_from_ppm(const char *filename, MicroDrawCanvas **canvas);
#endif // MICRO_DRAW_PPM
//
// Implementation
//
#ifdef MICRO_DRAW_IMPLEMENTATION
#define micro_assert(cond) \
do { \
if (!(cond)) { \
__builtin_trap(); \
} \
} while (0)
#ifndef MICRO_STATIC_ASSERT
#define MICRO_STATIC_ASSERT(cond, msg) \
typedef char static_assertion_##msg##__LINE__[(cond) ? 1 : -1]
#endif
void micro_draw_canvas_init(MicroDrawCanvas *canvas,
int width, int height,
MicroDrawPixel pixel)
{
if (!canvas) return;
canvas->width = width;
canvas->height = height;
canvas->pixel = pixel;
canvas->data = MICRO_DRAW_MALLOC(width *
height *
micro_draw_get_channels(pixel) *
micro_draw_get_channel_size(pixel));
}
void micro_draw_canvas_free(MicroDrawCanvas *canvas)
{
MICRO_DRAW_FREE(canvas->data);
}
MICRO_STATIC_ASSERT(_MICRO_DRAW_PIXEL_MAX == 2,
should_also_update_micro_draw_get_channels);
MICRO_DRAW_DEF unsigned int micro_draw_get_channels(MicroDrawPixel pixel)
{
switch(pixel)
{
case MICRO_DRAW_RGBA8:
return 4;
case MICRO_DRAW_BLACK_WHITE:
return 1;
default:
break;
}
return 0;
}
MICRO_STATIC_ASSERT(_MICRO_DRAW_PIXEL_MAX == 2,
should_also_update_micro_draw_get_channel_size);
MICRO_DRAW_DEF unsigned int micro_draw_get_channel_size(MicroDrawPixel pixel)
{
switch(pixel)
{
case MICRO_DRAW_RGBA8:
return 1;
case MICRO_DRAW_BLACK_WHITE:
return 1;
default:
break;
}
return 0;
}
MICRO_STATIC_ASSERT(_MICRO_DRAW_PIXEL_MAX == 2,
should_also_update_micro_draw_color_to_rgba8);
MICRO_DRAW_DEF void
micro_draw_color_to_rgba8(unsigned char* color_src, MicroDrawPixel pixel_src,
unsigned char color_dest[4])
{
micro_assert(micro_draw_get_channels(MICRO_DRAW_RGBA8) == 4);
micro_assert(micro_draw_get_channel_size(MICRO_DRAW_RGBA8) == 1);
switch(pixel_src)
{
case MICRO_DRAW_RGBA8:
for (int i = 0; i < 4; ++i)
color_dest[i] = color_src[i];
break;
case MICRO_DRAW_BLACK_WHITE:
color_dest[0] = color_src[0] * 255;
color_dest[1] = color_src[0] * 255;
color_dest[2] = color_src[0] * 255;
color_dest[3] = 255;
break;
default:
break;
}
}
MICRO_STATIC_ASSERT(_MICRO_DRAW_PIXEL_MAX == 2,
should_also_update_micro_draw_color_from_rgba8);
MICRO_DRAW_DEF void
micro_draw_color_from_rgba8(unsigned char color_src[4],
unsigned char *color_dest, MicroDrawPixel pixel_dest)
{
micro_assert(micro_draw_get_channels(MICRO_DRAW_RGBA8) == 4);
micro_assert(micro_draw_get_channel_size(MICRO_DRAW_RGBA8) == 1);
switch(pixel_dest)
{
case MICRO_DRAW_RGBA8:
for (int i = 0; i < 4; ++i)
color_dest[i] = color_src[i];
break;
case MICRO_DRAW_BLACK_WHITE:
color_dest[0] = color_src[0] == 255 ? 1 : 0;
break;
default:
break;
}
return;
}
MICRO_DRAW_DEF void
micro_draw_color_convert(unsigned char *color_src, MicroDrawPixel pixel_src,
unsigned char *color_dest, MicroDrawPixel pixel_dest)
{
micro_assert(micro_draw_get_channels(MICRO_DRAW_RGBA8) == 4);
micro_assert(micro_draw_get_channel_size(MICRO_DRAW_RGBA8) == 1);
unsigned char color_rgba[4] = {0};
micro_draw_color_to_rgba8(color_src, pixel_src, color_rgba);
micro_draw_color_from_rgba8(color_rgba, color_dest, pixel_dest);
return;
}
static inline void *
_micro_draw_memcpy(void *dest, const void *src, unsigned int n)
{
for (unsigned int i = 0; i < n; ++i)
((char*)dest)[i] = ((char*)src)[i];
return dest;
}
MICRO_DRAW_DEF void
micro_draw_pixel(MicroDrawCanvas *canvas,
Vec2 pos,
unsigned char* color)
{
if (!canvas || pos.x >= canvas->width || pos.x < 0
|| pos.y >= canvas->height || pos.y < 0) return;
int channel_size = micro_draw_get_channel_size(canvas->pixel); // bytes
unsigned int channels = micro_draw_get_channels(canvas->pixel);
int index = (pos.y * canvas->width + pos.x) * channels * channel_size;
_micro_draw_memcpy(&canvas->data[index], color, channels * channel_size);
return;
}
MICRO_DRAW_DEF void
micro_draw_line(MicroDrawCanvas *canvas,
Vec2 a, Vec2 b,
unsigned char* color)
{
if (!canvas) return;
// Line equation
double m = (a.y - b.y) / (double)(a.x - b.x);
double q = a.y - m * a.x;
int is_steep = (m > 1 || m < -1);
if (is_steep)
{
// Transpose the image
int tmp = a.x;
a.x = a.y;
a.y = tmp;
tmp = b.x;
b.x = b.y;
b.y = tmp;
m = (a.y - b.y) / (double)(a.x - b.x);
q = a.y - m * a.x;
}
int start, end;
if (a.x > b.x)
{
start = b.x;
end = a.x;
}
else
{
start = a.x;
end = b.x;
}
if (is_steep)
{
// Iterate by columns
for (int p_x = start; p_x < end; ++p_x)
{
int p_y = m * p_x + q;
// Retranspose the image
micro_draw_pixel(canvas, (Vec2){ .x = p_y, .y = p_x }, color);
}
}
else
{
// Transposed
for (int p_x = start; p_x < end; ++p_x)
{
int p_y = m * p_x + q;
micro_draw_pixel(canvas, (Vec2) { .x = p_x, .y = p_y }, color);
}
}
return;
}
MICRO_DRAW_DEF void
micro_draw_clear(MicroDrawCanvas *canvas,
unsigned char *color)
{
if (!canvas) return;
for (int row = 0; row < canvas->height; ++row)
for (int col = 0; col < canvas->width; ++col)
micro_draw_pixel(canvas, (Vec2) { .x = col, .y = row }, color);
}
MICRO_STATIC_ASSERT(_MICRO_DRAW_PIXEL_MAX == 2,
make_sure_that_color_dest_in_micro_draw_overlap_is_enough);
MICRO_DRAW_DEF void
micro_draw_overlap(MicroDrawCanvas *src_canvas,
MicroDrawCanvas *dest_canvas,
Vec2 offset)
{
int channel_size = micro_draw_get_channel_size(src_canvas->pixel); // bytes
unsigned int channels = micro_draw_get_channels(src_canvas->pixel);
for (int row = 0; row < src_canvas->height; ++row)
{
for (int col = 0; col < src_canvas->width; ++col)
{
if (row < 0 || col < 0) continue;
int index = (row * src_canvas->width + col) * channels * channel_size;
unsigned char color_dest[4] = {0}; // 4 is enough for now
micro_draw_color_convert(src_canvas->data + index, src_canvas->pixel,
color_dest, dest_canvas->pixel);
micro_draw_pixel(dest_canvas,
(Vec2) { .x = col + offset.x, .y = row + offset.y },
color_dest);
}
}
return;
}
MICRO_DRAW_DEF void
micro_draw_fill_rect(MicroDrawCanvas *canvas,
Rect rect,
unsigned char *color)
{
micro_draw_fill_triangle(canvas,
(Triangle) {
.a_x = rect.a_x, .a_y = rect.a_y,
.b_x = rect.b_x, .b_y = rect.b_y,
.c_x = rect.c_x, .c_y = rect.c_y,
}, color);
micro_draw_fill_triangle(canvas,
(Triangle) {
.a_x = rect.a_x, .a_y = rect.a_y,
.b_x = rect.c_x, .b_y = rect.c_y,
.c_x = rect.d_x, .c_y = rect.d_y,
}, color);
return;
}
#define MICRO_DRAW_ABS(x) (((x) >= 0) ? (x) : -(x))
MICRO_DRAW_DEF void
micro_draw_fill_circle(MicroDrawCanvas *canvas,
Circle circle,
unsigned char *color)
{
for (int row = circle.c_y - circle.r; row < circle.c_y + circle.r && row < canvas->height; ++row)
{
for (int col = circle.c_x - circle.r; col < circle.c_x + circle.r && col < canvas->width; ++col)
{
if (row < 0 || col < 0) continue;
int dx = MICRO_DRAW_ABS(col - circle.c_x);
int dy = MICRO_DRAW_ABS(row - circle.c_y);
if (dx*dx + dy*dy > circle.r*circle.r) continue;
micro_draw_pixel(canvas, (Vec2) { .x = col, .y = row }, color);
}
}
return;
}
// Get the orientation of three 2D points (a, b, c).
//
// This computes the determinant:
//
// det( [[ a_x, a_y, 1 ],
// [ b_x, b_y, 1 ],
// [ c_x, c_y, 1 ]] )
//
// Algebraically, this is equal to
//
// (b_x - a_x) * (c_y - a_y) - (b_y - a_y) * (c_x - a_x)
//
// which is twice the signed area of the triangle (a, b, c).
//
// The sign of the result indicates the orientation:
// > 0 : counterclockwise
// < 0 : clockwise
// = 0 : collinear
//
// This orientation test is important for rasterization. Suppose we
// want to test whether a point P lies inside the triangle (a, b, c).
// We form the three sub-triangles (P, b, c), (a, P, c), and (a, b, P).
// If P is inside, all of these orientations will have the same sign
// as the orientation of the original triangle. If P is outside, at
// least one orientation will differ.
//
// See also: Fabian Giesen,
// “The barycentric conspiracies”
// https://fgiesen.wordpress.com/2013/02/06/the-barycentric-conspirac/
#define _micro_draw_orient2D(a_x, a_y, b_x, b_y, c_x, c_y) \
( ((b_x) - (a_x)) * ((c_y) - (a_y)) - ((b_y) - (a_y)) * ((c_x) - (a_x)) )
#define _micro_draw_min(a, b) ((a) < (b) ? (a) : (b))
#define _micro_draw_max(a, b) ((a) > (b) ? (a) : (b))
#define _micro_draw_min3(a, b, c) (_micro_draw_min(_micro_draw_min((a), (b)), (c)))
#define _micro_draw_max3(a, b, c) (_micro_draw_max(_micro_draw_max((a), (b)), (c)))
// https://fgiesen.wordpress.com/2013/02/08/triangle-rasterization-in-practice/
MICRO_DRAW_DEF void
micro_draw_fill_triangle(MicroDrawCanvas *canvas,
Triangle triangle,
unsigned char *color)
{
// Compute triangle bounding box
int minX = _micro_draw_min3(triangle.a_x, triangle.b_x, triangle.c_x);
int minY = _micro_draw_min3(triangle.a_y, triangle.b_y, triangle.c_y);
int maxX = _micro_draw_max3(triangle.a_x, triangle.b_x, triangle.c_x);
int maxY = _micro_draw_max3(triangle.a_y, triangle.b_y, triangle.c_y);
// Clip against screen bounds
minX = _micro_draw_max(minX, 0);
minY = _micro_draw_max(minY, 0);
maxX = _micro_draw_min(maxX, canvas->width - 1);
maxY = _micro_draw_min(maxY, canvas->height - 1);
for (int row = minY; row <= maxY; ++row)
{
for (int col = minX; col <= maxX; ++col)
{
// Determine barycentric coordinates
int w0 = _micro_draw_orient2D(triangle.b_x, triangle.b_y, triangle.c_x, triangle.c_y, col, row);
int w1 = _micro_draw_orient2D(triangle.c_x, triangle.c_y, triangle.a_x, triangle.a_y, col, row);
int w2 = _micro_draw_orient2D(triangle.a_x, triangle.a_y, triangle.b_x, triangle.b_y, col, row);
// If p is on or inside all edges, render pixel.
if (w0 >= 0 && w1 >= 0 && w2 >= 0)
micro_draw_pixel(canvas, (Vec2) { .x = col, .y = row }, color);
}
}
return;
}
MICRO_DRAW_DEF void
micro_draw_grid(MicroDrawCanvas *canvas,
int columns, int rows,
unsigned char* color)
{
if (!canvas) return;
// Draw columns
for (int x = 0; x < canvas->width; x += canvas->width / columns)
{
micro_draw_line(canvas,
(Vec2) { .x = x, .y = 0 },
(Vec2) { .x = x, .y = canvas->height},
color);
}
// Draw rows
for (int y = 0; y < canvas->height; y += canvas->height / rows)
{
micro_draw_line(canvas,
(Vec2) { .x = 0, .y = y },
(Vec2) { .x = canvas->width, .y = y },
color);
}
return;
}
MICRO_DRAW_DEF void
micro_draw_get_color(MicroDrawCanvas *canvas,
Vec2 pos,
unsigned char** color)
{
if (!canvas || pos.x >= canvas->width || pos.x < 0
|| pos.y >= canvas->height || pos.y < 0) return;
int channel_size = micro_draw_get_channel_size(canvas->pixel); // bytes
unsigned int channels = micro_draw_get_channels(canvas->pixel);
int index = (pos.y * canvas->width + pos.x) * channels * channel_size;
*color = canvas->data + index;
return;
}
MICRO_STATIC_ASSERT(_MICRO_DRAW_PIXEL_MAX == 2,
make_sure_that_color_dest_in_micro_draw_scaled_is_enough);
MICRO_DRAW_DEF void
micro_draw_scaled(MicroDrawCanvas *src_canvas,
MicroDrawCanvas *dest_canvas)
{
for (int y = 0; y < dest_canvas->height; ++y)
{
for (int x = 0; x < dest_canvas->width; ++x)
{
int x_frame = (x * src_canvas->width) / (double)dest_canvas->width;
int y_frame = (y * src_canvas->height) / (double)dest_canvas->height;
unsigned char *color = NULL;
micro_draw_get_color(src_canvas,
(Vec2) { .x = x_frame, .y = y_frame },
&color);
unsigned char color_dest[4] = {0}; // 4 is enough for now
micro_draw_color_convert(color, src_canvas->pixel,
color_dest, dest_canvas->pixel);
micro_draw_pixel(dest_canvas,
(Vec2) { .x = x, .y = y },
color_dest);
}
}
return;
}
static inline int _micro_draw_get_horizontal_characters(char* str)
{
int max_num = 0;
int num = 0;
while (*str != '\0')
{
if (*str == '\n')
{
max_num = (num > max_num) ? num : max_num;
num = 0;
}
else
{
num++;
}
str++;
}
return (num > max_num) ? num : max_num;
}
static inline int _micro_draw_get_vertical_characters(char* str)
{
int num = 1;
while (*str != '\0')
{
if (*str == '\n') num++;
str++;
}
return num;
}
static inline int _micro_draw_strlen(char* str)
{
int len = 0;
while (str[len] != '\0') len++;
return len;
}
MICRO_STATIC_ASSERT(_MICRO_DRAW_PIXEL_MAX == 2,
make_sure_that_color_dest_in_micro_draw_text_is_enough);
MICRO_DRAW_DEF void
micro_draw_text(MicroDrawCanvas *canvas,
MicroDrawText text,
unsigned char* color)
{
int channel_size = micro_draw_get_channel_size(canvas->pixel); // bytes
unsigned int channels = micro_draw_get_channels(canvas->pixel);
int char_x = MICRO_DRAW_CHARACTER_PIXELS_X * text.scale;
int char_y = MICRO_DRAW_CHARACTER_PIXELS_Y * text.scale;
int text_row = 0;
int text_col = 0;
int text_len = _micro_draw_strlen(text.text);
for (int c = 0; c < text_len; ++c)
{
if (text.text[c] == '\n')
{
text_row++;
text_col = 0;
continue;
}
for (int y = 0; y < char_y; ++y)
{
for (int x = 0; x < char_x; ++x)
{
// Rescale the pixel
int font_x = (x * MICRO_DRAW_FONT_WIDTH) / (double)char_x;
int font_y = (y * MICRO_DRAW_FONT_HEIGHT) / (double)char_y;
if (micro_draw_font[(int)text.text[c]][font_y][font_x] == 0)
continue;
// Calculate color
unsigned char color_dest[4] = {0};
for (unsigned int i = 0; i < channels * channel_size; ++i)
color_dest[i] =
color[i] *
micro_draw_font[(int)text.text[c]][font_y][font_x];
// TODO: implement transparency by multiplying color_dest by
// the color in data
// Draw with translation
micro_draw_pixel(canvas,
(Vec2) {
.x = x + text.x + text_col * char_x,
.y = y + text.y + text_row * char_y
},
color_dest);
}
}
text_col++;
}
return;
}
#ifdef MICRO_DRAW_PPM
typedef __builtin_va_list _micro_draw_va_list;
#define _micro_draw_va_start(v, l) __builtin_va_start(v, l)
#define _micro_draw_va_end(v) __builtin_va_end(v)
#define _micro_draw_va_arg(v, l) __builtin_va_arg(v, l)
#define _micro_draw_va_copy(d, s) __builtin_va_copy(d, s)
static char* _micro_draw_strcat_and_advance(char* dest, const char* src)
{
while (*src)
{
*dest++ = *src++;
}
return dest;
}
static char* _micro_draw_itoa(long val, char* buf, int base)
{
char* p = buf;
char* p1, *p2;
unsigned long uval = (val < 0 && base == 10) ? -val : val;
do {
*p++ = "0123456789abcdef"[uval % base];
} while (uval /= base);
if (val < 0 && base == 10) *p++ = '-';
*p = '\0';
p1 = buf; p2 = p - 1;
while (p1 < p2)
{
char tmp = *p1; *p1++ = *p2; *p2-- = tmp;
}
return buf;
}
static void _micro_draw_format(char* out, const char* fmt,
_micro_draw_va_list args)
{
while (*fmt)
{
if (*fmt == '%')
{
fmt++;
switch (*fmt)
{
case 'd':
case 'i': {
char buf[32];
_micro_draw_itoa(_micro_draw_va_arg(args, int), buf, 10);
out = _micro_draw_strcat_and_advance(out, buf);
break;
}
case 'x': {
char buf[32];
_micro_draw_itoa(_micro_draw_va_arg(args, unsigned int), buf, 16);
out = _micro_draw_strcat_and_advance(out, buf);
break;
}
case 's': {
char* s = _micro_draw_va_arg(args, char*);
if (!s) s = "(null)";
out = _micro_draw_strcat_and_advance(out, s);
break;
}
case 'f': {
double f = _micro_draw_va_arg(args, double);
char buf[64];
// Handle negative
if (f < 0) { *out++ = '-'; f = -f; }
// Integer part
long i_part = (long)f;
_micro_draw_itoa(i_part, buf, 10);
out = _micro_draw_strcat_and_advance(out, buf);
*out++ = '.';
// Fractional part (6 decimal places)
long f_part = (long)((f - (double)i_part) * 1000000.0 + 0.5);
_micro_draw_itoa(f_part, buf, 10);
// Add leading zeros to fraction if necessary
int len = 0; while(buf[len]) len++;
for(int z = 0; z < (6 - len); z++) *out++ = '0';
out = _micro_draw_strcat_and_advance(out, buf);
break;
}
case '%': {
*out++ = '%';
break;
}
default: {
*out++ = '%';
*out++ = *fmt;
break;
}
}
}
else
{
*out++ = *fmt;
}
fmt++;
}
*out = '\0';
}
static int _micro_draw_sprintf(char* buf, const char* fmt, ...)
{
_micro_draw_va_list args;
_micro_draw_va_start(args, fmt);
// We reuse the formatter you just built
_micro_draw_format(buf, fmt, args);
_micro_draw_va_end(args);
// To be fully compliant, we should return the number of characters written
int len = 0;
while (buf[len]) len++;
return len;
}
// The PPM header starts with 4 values speareted by either space or
// newline: ID, WIDTH, HEIGHT, MAX_COLOR_VALUE.
// Where ID is either:
// - P1: ASCII Bitmap (.pbm)
// - P2: ASCII Graymap (.pgm)
// - P3: ASCII PixMap (.ppm)