-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathimage.c
More file actions
1174 lines (1068 loc) · 33.4 KB
/
image.c
File metadata and controls
1174 lines (1068 loc) · 33.4 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
// Copyright (c) 2025 Nicolas Schodet
#include <pbio/config.h>
#if PBIO_CONFIG_IMAGE
#include <pbio/image.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
/**
* Clip range between 0 and given length, return if out.
* @param [in,out] c1 First coordinate, included.
* @param [in,out] c2 Second coordinate, excluded.
* @param [in] l Length.
*
* First coordinate must not be greater than the second one.
*/
#define clip_or_return(c1, c2, l) \
do { \
if ((c1) >= (l) || (c2) <= 0) { \
return; \
} \
if ((c1) < 0) { \
(c1) = 0; \
} \
if ((c2) > (l)) { \
(c2) = (l); \
} \
} while (0)
/**
* Initialize an image, using external storage.
* @param [out] image Uninitialized image to initialize.
* @param [in] pixels Buffer storing the pixels values, not changed.
* @param [in] width Number of columns.
* @param [in] height Number of rows.
* @param [in] stride Distance in bytes inside the pixel buffer to go from
* one row to the next one.
*
* No memory allocation is done. This function takes an uninitialized image
* structure. The pixel buffer is left untouched, pbio_image_fill() can be
* used to clear it.
*/
void pbio_image_init(pbio_image_t *image, uint8_t *pixels, int width,
int height, int stride) {
image->pixels = pixels;
image->width = width;
image->height = height;
image->stride = stride;
image->print_font = NULL;
image->print_x_left = 0;
image->print_y_top = 0;
image->print_value = 0;
}
/**
* Initialize an image as a viewport into another bigger image.
* @param [out] image Uninitialized image to initialize.
* @param [in] source Source image.
* @param [in] x X coordinate of the top-left point of the viewport in
* source.
* @param [in] y Y coordinate of the top-left point of the viewport in
* source.
* @param [in] width Width of the viewport.
* @param [in] height Height of the viewport.
*
* After this call, the two images shares the same buffer, modifying a pixel
* inside the viewport in one image will change the same pixel in the other
* image.
*
* Clipping: viewport is clipped to source image dimensions. If completely out
* of the source image, the destination image may be empty.
*/
void pbio_image_init_sub(pbio_image_t *image, const pbio_image_t *source,
int x, int y, int width, int height) {
// Start with an empty image in case of early return.
pbio_image_init(image, source->pixels, 0, 0, 0);
// Eliminate weird cases.
if (width <= 0 || height <= 0) {
return;
}
// Clipping, may result in an empty image.
int x2 = x + width;
int y2 = y + height;
clip_or_return(x, x2, source->width);
clip_or_return(y, y2, source->height);
// Select the right part of source image.
pbio_image_init(image, source->pixels + y * source->stride + x, x2 - x,
y2 - y, source->stride);
// Reuse the same font and value.
image->print_font = source->print_font;
image->print_value = source->print_value;
}
/**
* Fill an image with a value.
* @param [in] image Image to fill.
* @param [in] value Pixel value.
*
* This also resets the text printing position.
*/
void pbio_image_fill(pbio_image_t *image, uint8_t value) {
uint8_t *p = image->pixels;
for (int h = image->height; h; h--) {
memset(p, value, image->width);
p += image->stride;
}
image->print_x_left = 0;
image->print_y_top = 0;
}
/**
* Draw an image inside another image.
* @param [in] image Destination image to draw into.
* @param [in] source Source image.
* @param [in] x X coordinate of the top-left point in destination
* image.
* @param [in] y Y coordinate of the top-left point in destination
* image.
*
* Source image pixels are copied into destination image.
*
* Clipping: drawing is clipped to destination image dimensions.
*/
void pbio_image_draw_image(pbio_image_t *image, const pbio_image_t *source,
int x, int y) {
// Clipping.
int ox = x;
int oy = y;
int x2 = x + source->width;
int y2 = y + source->height;
clip_or_return(x, x2, image->width);
clip_or_return(y, y2, image->height);
// Copy pixels.
uint8_t *src = source->pixels + (y - oy) * source->stride + (x - ox);
uint8_t *dst = image->pixels + y * image->stride + x;
int w = x2 - x;
for (int h = y2 - y; h; h--) {
memcpy(dst, src, w);
dst += image->stride;
src += source->stride;
}
}
/**
* Draw an image inside another image with transparency.
* @param [in] image Destination image to draw into.
* @param [in] source Source image.
* @param [in] x X coordinate of the top-left point in destination
* image.
* @param [in] y Y coordinate of the top-left point in destination
* image.
* @param [in] value Pixel value in source image considered transparent.
*
* Source image pixels are copied into destination image. When a source pixel
* matches the transparent value, the corresponding destination pixel is left
* untouched.
*
* Clipping: drawing is clipped to destination image dimensions.
*/
void pbio_image_draw_image_transparent(pbio_image_t *image,
const pbio_image_t *source, int x, int y, uint8_t value) {
// Clipping.
int ox = x;
int oy = y;
int x2 = x + source->width;
int y2 = y + source->height;
clip_or_return(x, x2, image->width);
clip_or_return(y, y2, image->height);
// Draw pixels.
uint8_t *src = source->pixels + (y - oy) * source->stride + (x - ox);
uint8_t *dst = image->pixels + y * image->stride + x;
int w = x2 - x;
for (int h = y2 - y; h; h--) {
for (int i = w; i; i--) {
uint8_t c = *src;
if (c != value) {
*dst = c;
}
src++;
dst++;
}
dst += image->stride - w;
src += source->stride - w;
}
}
/**
* Draw an image inside another image with transparency. The source is
* a compressed monochrome image.
*
* @param [in] image Destination image to draw into.
* @param [in] source Source image.
* @param [in] x X coordinate of the top-left point in destination
* image.
* @param [in] y Y coordinate of the top-left point in destination
* image.
* @param [in] value Pixel value in destination for black.
*
* Source image pixels are copied into destination image. When a source pixel
* matches the transparent value, the corresponding destination pixel is left
* untouched.
*
* Clipping: drawing is clipped to destination image dimensions.
*/
void pbio_image_draw_image_transparent_from_monochrome(pbio_image_t *image,
const pbio_image_monochrome_t *source, int x, int y, uint8_t value) {
// Clipping.
int ox = x;
int oy = y;
int x2 = x + source->width;
int y2 = y + source->height;
clip_or_return(x, x2, image->width);
clip_or_return(y, y2, image->height);
// Initial index in source.
size_t index = (y - oy) * source->width + (x - ox);
// Draw pixels.
uint8_t *dst = image->pixels + y * image->stride + x;
int w = x2 - x;
for (int h = y2 - y; h; h--) {
for (int i = w; i; i--) {
if (source->data[index / 8] & (1 << (7 - index % 8))) {
*dst = value;
}
index++;
dst++;
}
dst += image->stride - w;
index += source->width - w;
}
}
/**
* Draw a single pixel.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the pixel.
* @param [in] y Y coordinate of the pixel.
* @param [in] value New pixel value.
*
* Clipping: pixel is not drawn if coordinate is outside of the image.
*/
void pbio_image_draw_pixel(pbio_image_t *image, int x, int y, uint8_t value) {
// Clipping.
if (x < 0 || x >= image->width || y < 0 || y >= image->height) {
return;
}
// Draw pixel.
uint8_t *p = image->pixels + y * image->stride + x;
*p = value;
}
/**
* Draw a horizontal line.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the leftmost pixel.
* @param [in] y Y coordinate of the line.
* @param [in] l Length of the line.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_draw_hline(pbio_image_t *image, int x, int y, int l,
uint8_t value) {
// Eliminate weird cases.
if (l <= 0) {
return;
}
// Clipping.
int x2 = x + l;
clip_or_return(x, x2, image->width);
if (y < 0 || y >= image->height) {
return;
}
// Draw line.
uint8_t *p = image->pixels + y * image->stride + x;
memset(p, value, x2 - x);
}
/**
* Draw a vertical line.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the line.
* @param [in] y Y coordinate of the topmost pixel.
* @param [in] l Length of the line.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_draw_vline(pbio_image_t *image, int x, int y, int l,
uint8_t value) {
// Eliminate weird cases.
if (l <= 0) {
return;
}
// Clipping.
int y2 = y + l;
if (x < 0 || x >= image->width) {
return;
}
clip_or_return(y, y2, image->height);
// Draw line.
uint8_t *p = image->pixels + y * image->stride + x;
for (int h = y2 - y; h; h--) {
*p = value;
p += image->stride;
}
}
/**
* Draw a line with a flat slope (less or equal to 1).
* @param [in] image Image to draw into.
* @param [in] x1 X coordinate of the first end.
* @param [in] y1 Y coordinate of the first end.
* @param [in] x2 X coordinate of the second end.
* @param [in] y2 Y coordinate of the second end.
* @param [in] value Pixel value.
*
* This is an internal function, x2 must be greater or equal to x1.
*
* Clipping: drawing is clipped to image dimensions.
*/
static void pbio_image_draw_line_flat(pbio_image_t *image, int x1, int y1,
int x2, int y2, uint8_t value) {
int dx = x2 - x1;
int dy = y2 - y1;
int ydir = 1;
int x, y, err;
// Fall back to horizontal line, much faster.
if (dy == 0) {
pbio_image_draw_hline(image, x1, y1, dx + 1, value);
return;
}
// Clipping, X out of image.
if (x1 >= image->width || x2 < 0) {
return;
}
// Check Y direction.
if (dy < 0) {
dy = -dy;
ydir = -1;
}
// Error is scaled by 2 * dx, offset with one half to look at mid point.
err = -dx;
y = y1;
// Skip pixels left of image.
if (x1 < 0) {
err += -x1 * dy * 2;
int yskip = (err + dx * 2) / (dx * 2);
err -= yskip * dx * 2;
y += yskip * ydir;
x1 = 0;
}
// Skip pixels right of image.
if (x2 >= image->width) {
x2 = image->width - 1;
}
// Draw.
x = x1;
do {
pbio_image_draw_pixel(image, x, y, value);
err += dy * 2;
if (err >= 0) {
err -= dx * 2;
y += ydir;
}
x++;
} while (x <= x2);
}
/**
* Draw a line with a steep slope (more than 1).
* @param [in] image Image to draw into.
* @param [in] x1 X coordinate of the first end.
* @param [in] y1 Y coordinate of the first end.
* @param [in] x2 X coordinate of the second end.
* @param [in] y2 Y coordinate of the second end.
* @param [in] value Pixel value.
*
* This is an internal function, y2 must be greater or equal to y1.
*
* Clipping: drawing is clipped to image dimensions.
*/
static void pbio_image_draw_line_steep(pbio_image_t *image, int x1, int y1,
int x2, int y2, uint8_t value) {
int dx = x2 - x1;
int dy = y2 - y1;
int xdir = 1;
int x, y, err;
// Fall back to vertical line, much faster.
if (dx == 0) {
pbio_image_draw_vline(image, x1, y1, dy + 1, value);
return;
}
// Clipping, Y out of image.
if (y1 >= image->height || y2 < 0) {
return;
}
// Check X direction.
if (dx < 0) {
dx = -dx;
xdir = -1;
}
// Error is scaled by 2 * dy, offset with one half to look at mid point.
err = -dy;
x = x1;
// Skip pixels above image.
if (y1 < 0) {
err += -y1 * dx * 2;
int xskip = (err + dy * 2) / (dy * 2);
err -= xskip * dy * 2;
x += xskip * xdir;
y1 = 0;
}
// Skip pixels bellow image.
if (y2 >= image->height) {
y2 = image->height - 1;
}
// Draw.
y = y1;
do {
pbio_image_draw_pixel(image, x, y, value);
err += dx * 2;
if (err >= 0) {
err -= dy * 2;
x += xdir;
}
y++;
} while (y <= y2);
}
/**
* Draw a line.
* @param [in] image Image to draw into.
* @param [in] x1 X coordinate of the first end.
* @param [in] y1 Y coordinate of the first end.
* @param [in] x2 X coordinate of the second end.
* @param [in] y2 Y coordinate of the second end.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_draw_line(pbio_image_t *image, int x1, int y1, int x2, int y2,
uint8_t value) {
int dx = x2 - x1;
int dy = y2 - y1;
int abs_dx = dx < 0 ? -dx : dx;
int abs_dy = dy < 0 ? -dy : dy;
if (abs_dx >= abs_dy) {
// Flat slope, X always increasing.
if (dx > 0) {
pbio_image_draw_line_flat(image, x1, y1, x2, y2, value);
} else {
pbio_image_draw_line_flat(image, x2, y2, x1, y1, value);
}
} else {
// Steep slope, Y always increasing.
if (dy > 0) {
pbio_image_draw_line_steep(image, x1, y1, x2, y2, value);
} else {
pbio_image_draw_line_steep(image, x2, y2, x1, y1, value);
}
}
}
/**
* Draw a thick line.
* @param [in] image Image to draw into.
* @param [in] x1 X coordinate of the first end.
* @param [in] y1 Y coordinate of the first end.
* @param [in] x2 X coordinate of the second end.
* @param [in] y2 Y coordinate of the second end.
* @param [in] thickness Line thickness.
* @param [in] value Pixel value.
*
* When line thickness is odd, pixels are centered on the line. When even,
* line is thicker on left side, when looking from first end to second end.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_draw_thick_line(pbio_image_t *image, int x1, int y1, int x2,
int y2, int thickness, uint8_t value) {
// Eliminate weird cases.
if (thickness <= 0) {
return;
}
// Fall back to regular line.
if (thickness <= 1) {
pbio_image_draw_line(image, x1, y1, x2, y2, value);
return;
}
int dx = x2 - x1;
int dy = y2 - y1;
int abs_dx = dx < 0 ? -dx : dx;
int abs_dy = dy < 0 ? -dy : dy;
int i;
int offset = thickness / 2;
if (abs_dx >= abs_dy) {
// Flat slope, X always increasing.
if (dx > 0) {
for (i = 0; i < thickness; i++) {
pbio_image_draw_line_flat(image, x1, y1 + i - offset,
x2, y2 + i - offset, value);
}
} else {
for (i = 0; i < thickness; i++) {
pbio_image_draw_line_flat(image, x2, y2 - i + offset,
x1, y1 - i + offset, value);
}
}
} else {
// Steep slope, Y always increasing.
if (dy > 0) {
for (i = 0; i < thickness; i++) {
pbio_image_draw_line_steep(image, x1 - i + offset, y1,
x2 - i + offset, y2, value);
}
} else {
for (i = 0; i < thickness; i++) {
pbio_image_draw_line_steep(image, x2 + i - offset, y2,
x1 + i - offset, y1, value);
}
}
}
}
/**
* Draw a rectangle.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the top-left corner.
* @param [in] y Y coordinate of the top-left corner.
* @param [in] width Rectangle width.
* @param [in] height Rectangle height.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_draw_rect(pbio_image_t *image, int x, int y, int width,
int height, uint8_t value) {
// Draw.
pbio_image_draw_hline(image, x, y, width, value);
if (height > 1) {
pbio_image_draw_hline(image, x, y + height - 1, width, value);
}
pbio_image_draw_vline(image, x, y + 1, height - 2, value);
if (width > 1) {
pbio_image_draw_vline(image, x + width - 1, y + 1, height - 2, value);
}
}
/**
* Draw a filled rectangle.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the top-left corner.
* @param [in] y Y coordinate of the top-left corner.
* @param [in] width Rectangle width.
* @param [in] height Rectangle height.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_fill_rect(pbio_image_t *image, int x, int y, int width,
int height, uint8_t value) {
// Eliminate weird cases.
if (width <= 0 || height <= 0) {
return;
}
// Clipping.
int x2 = x + width;
int y2 = y + height;
clip_or_return(x, x2, image->width);
clip_or_return(y, y2, image->height);
// Draw.
uint8_t *p = image->pixels + y * image->stride + x;
for (int h = y2 - y; h; h--) {
memset(p, value, x2 - x);
p += image->stride;
}
}
/**
* Draw a rounded rectangle.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the top-left corner.
* @param [in] y Y coordinate of the top-left corner.
* @param [in] width Rectangle width.
* @param [in] height Rectangle height.
* @param [in] r Corner radius.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_draw_rounded_rect(pbio_image_t *image, int x, int y,
int width, int height, int r, uint8_t value) {
// Eliminate weird cases.
if (width <= 0 || height <= 0) {
return;
}
if (r > (width - 1) / 2) {
r = (width - 1) / 2;
}
if (r > (height - 1) / 2) {
r = (height - 1) / 2;
}
// Fall back to regular rectangle.
if (r < 1) {
pbio_image_draw_rect(image, x, y, width, height, value);
return;
}
// Draw corners.
int dx = 0, dy = r;
int r2 = r * r;
int xr1 = x + r;
int yr1 = y + r;
int xr2 = x + width - 1 - r;
int yr2 = y + height - 1 - r;
while (dx < dy) {
dx++;
if (dx * dx + dy * dy > r2) {
dy--;
}
pbio_image_draw_pixel(image, xr1 - dx, yr1 - dy, value);
pbio_image_draw_pixel(image, xr1 - dy, yr1 - dx, value);
pbio_image_draw_pixel(image, xr2 + dx, yr1 - dy, value);
pbio_image_draw_pixel(image, xr2 + dy, yr1 - dx, value);
pbio_image_draw_pixel(image, xr1 - dx, yr2 + dy, value);
pbio_image_draw_pixel(image, xr1 - dy, yr2 + dx, value);
pbio_image_draw_pixel(image, xr2 + dx, yr2 + dy, value);
pbio_image_draw_pixel(image, xr2 + dy, yr2 + dx, value);
}
// Draw sides.
int wr = width - 2 * r;
int hr = height - 2 * r;
pbio_image_draw_hline(image, xr1, y, wr, value);
pbio_image_draw_hline(image, xr1, y + height - 1, wr, value);
pbio_image_draw_vline(image, x, yr1, hr, value);
pbio_image_draw_vline(image, x + width - 1, yr1, hr, value);
}
/**
* Draw a filled rounded rectangle.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the top-left corner.
* @param [in] y Y coordinate of the top-left corner.
* @param [in] width Rectangle width.
* @param [in] height Rectangle height.
* @param [in] r Corner radius.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_fill_rounded_rect(pbio_image_t *image, int x, int y,
int width, int height, int r, uint8_t value) {
// Eliminate weird cases.
if (width <= 0 || height <= 0) {
return;
}
if (r > (width - 1) / 2) {
r = (width - 1) / 2;
}
if (r > (height - 1) / 2) {
r = (height - 1) / 2;
}
// Fall back to regular rectangle.
if (r < 1) {
pbio_image_fill_rect(image, x, y, width, height, value);
return;
}
// Draw top and bottom.
int dx = 0, dy = r;
int r2 = r * r;
int xr1 = x + r;
int yr1 = y + r;
int yr2 = y + height - 1 - r;
int wr = width - 2 * r;
while (dx <= dy) {
// Optimization opportunity: when dy is not moving, the same pixels
// are drawn again and again.
pbio_image_draw_hline(image, xr1 - dx, yr1 - dy, wr + 2 * dx, value);
pbio_image_draw_hline(image, xr1 - dx, yr2 + dy, wr + 2 * dx, value);
if (dx != dy) {
pbio_image_draw_hline(image, xr1 - dy, yr1 - dx, wr + 2 * dy,
value);
pbio_image_draw_hline(image, xr1 - dy, yr2 + dx, wr + 2 * dy,
value);
}
dx++;
if (dx * dx + dy * dy > r2) {
dy--;
}
}
// Draw middle section.
pbio_image_fill_rect(image, x, yr1 + 1, width, height - 2 - 2 * r, value);
}
/**
* Draw a circle.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the circle center.
* @param [in] y Y coordinate of the circle center.
* @param [in] r Circle radius.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_draw_circle(pbio_image_t *image, int x, int y, int r,
uint8_t value) {
// Eliminate weird cases.
if (r <= 0) {
return;
}
// Draw.
int dx = 0, dy = r;
int err = 5 - r * 4;
while (dx <= dy) {
pbio_image_draw_pixel(image, x + dx, y + dy, value);
pbio_image_draw_pixel(image, x - dx, y + dy, value);
pbio_image_draw_pixel(image, x + dx, y - dy, value);
pbio_image_draw_pixel(image, x - dx, y - dy, value);
pbio_image_draw_pixel(image, x + dy, y + dx, value);
pbio_image_draw_pixel(image, x - dy, y + dx, value);
pbio_image_draw_pixel(image, x + dy, y - dx, value);
pbio_image_draw_pixel(image, x - dy, y - dx, value);
if (err > 0) {
dy--;
err -= dy * 8;
}
dx++;
err += 4 + dx * 8;
}
}
/**
* Draw a disc.
* @param [in] image Image to draw into.
* @param [in] x X coordinate of the disc center.
* @param [in] y Y coordinate of the disc center.
* @param [in] r Disc radius.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*/
void pbio_image_fill_circle(pbio_image_t *image, int x, int y, int r,
uint8_t value) {
// Eliminate weird cases.
if (r <= 0) {
return;
}
// Draw.
int dx = 0, dy = r;
int err = 5 - r * 4;
while (dx <= dy) {
// Optimization opportunity: when dy is not moving, the same pixels
// are drawn again and again.
pbio_image_draw_hline(image, x - dx, y + dy, 1 + 2 * dx, value);
pbio_image_draw_hline(image, x - dx, y - dy, 1 + 2 * dx, value);
if (dx != dy) {
pbio_image_draw_hline(image, x - dy, y + dx, 1 + 2 * dy, value);
pbio_image_draw_hline(image, x - dy, y - dx, 1 + 2 * dy, value);
}
if (err > 0) {
dy--;
err -= dy * 8;
}
dx++;
err += 4 + dx * 8;
}
}
/**
* Draw a single glyph.
* @param [in] image Image to draw into.
* @param [in] font Font to use for drawing.
* @param [in] glyph Glyph to draw.
* @param [in] x X coordinate of the baseline.
* @param [in] y Y coordinate of the baseline.
* @param [in] value Pixel value.
*/
static void pbio_image_draw_text_glyph(pbio_image_t *image,
const pbio_font_t *font, const pbio_font_glyph_t *glyph, int x, int y,
uint8_t value) {
const uint8_t *src = &font->data[glyph->data_index];
uint8_t bits;
for (int dy = 0; dy < glyph->height; dy++) {
for (int dx = 0; dx < glyph->width; dx++) {
if ((dx & 7) == 0) {
bits = *src++;
}
if (bits & 128) {
pbio_image_draw_pixel(image, x + glyph->left + dx,
y - glyph->top + dy, value);
}
bits <<= 1;
}
}
}
/**
* Draw text.
* @param [in] image Image to draw into.
* @param [in] font Font to use for drawing.
* @param [in] x X coordinate of the baseline.
* @param [in] y Y coordinate of the baseline.
* @param [in] text Text string.
* @param [in] text_len Text length.
* @param [in] value Pixel value.
*
* Clipping: drawing is clipped to image dimensions.
*
* Text uses ASCII encoding. Newlines are handled, text is flush left.
*/
void pbio_image_draw_text(pbio_image_t *image, const pbio_font_t *font, int x,
int y, const char *text, size_t text_len, uint8_t value) {
int x_left = x;
const char *p;
char c, prev = 0;
const pbio_font_glyph_t *g;
const pbio_font_kerning_t *k;
for (p = text; p != text + text_len; p++) {
c = *p;
if (c == '\n') {
x = x_left;
y += font->line_height;
} else if (c >= font->first && c <= font->last) {
g = &font->glyphs[c - font->first];
/* Apply kerning. */
if (font->kernings && prev) {
for (k = &font->kernings[g->kerning_index];
k != &font->kernings[(g + 1)->kerning_index]; k++) {
if (prev == k->previous) {
x += k->kerning;
break;
}
}
}
/* Draw glyph. */
pbio_image_draw_text_glyph(image, font, g, x, y, value);
/* Advance pen. */
x += g->advance;
}
prev = c;
}
}
/**
* Determine the bounding box of a text to be drawn.
* @param [in] font Font to use for drawing.
* @param [in] text Text string.
* @param [in] text_len Text length.
* @param [out] rect Rectangle covering the drawn text.
*
* Text uses ASCII encoding. Newlines are handled, text is flush left.
*/
void pbio_image_bbox_text(const pbio_font_t *font, const char *text,
size_t text_len, pbio_image_rect_t *rect) {
int x, y;
int xmin, ymin, xmax, ymax;
const char *p;
char c, prev = 0;
const pbio_font_glyph_t *g;
const pbio_font_kerning_t *k;
x = 0;
y = 0;
xmin = ymin = INT_MAX;
xmax = ymax = -INT_MAX;
for (p = text; p != text + text_len; p++) {
c = *p;
if (c == '\n') {
x = 0;
y += font->line_height;
} else if (c >= font->first && c <= font->last) {
g = &font->glyphs[c - font->first];
/* Apply kerning. */
if (font->kernings && prev) {
for (k = &font->kernings[g->kerning_index];
k != &font->kernings[(g + 1)->kerning_index]; k++) {
if (prev == k->previous) {
x += k->kerning;
break;
}
}
}
/* Update min/max. */
if (xmin > x + g->left) {
xmin = x + g->left;
}
if (xmax < x + g->left + g->width) {
xmax = x + g->left + g->width;
}
if (ymin > y - g->top) {
ymin = y - g->top;
}
if (ymax < y - g->top + g->height) {
ymax = y - g->top + g->height;
}
/* Advance pen. */
x += g->advance;
}
prev = c;
}
if (xmin > xmax) {
xmin = ymin = xmax = ymax = 0;
}
rect->x = xmin;
rect->y = ymin;
rect->width = xmax - xmin;
rect->height = ymax - ymin;
}
/**
* Scroll the whole image up by a number of pixels.
* @param [in] image Image to scroll.
* @param [in] n Number of pixels to scroll.
*/
static void pbio_image_scroll_up(pbio_image_t *image, int n) {
uint8_t *dst = image->pixels;
uint8_t *src = image->pixels + n * image->stride;
int y;
for (y = 0; y < image->height - n; y++) {
memcpy(dst, src, image->width);
src += image->stride;
dst += image->stride;
}
for (; y < image->height; y++) {
memset(dst, 0, image->width);
dst += image->stride;
}
}
/**
* Print text inside image, like in a terminal, scroll when bottom is reached.
* @param [in] image Image to draw into.
* @param [in] text Text string.
* @param [in] text_len Text length.
*
* Clipping: drawing is clipped to image dimensions.
*
* Text uses ASCII encoding. Newlines are handled, text is flush left.
*/
void pbio_image_print(pbio_image_t *image, const char *text, size_t text_len) {
const char *p;
int x, y_top;
char c, prev = 0;
const pbio_font_t *font;