-
Notifications
You must be signed in to change notification settings - Fork 7
/
r1b.h
3234 lines (2857 loc) · 106 KB
/
r1b.h
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
/* ===================================================================== *
* _|_ *
* (E|3) r1b.h *
* v v *
* A thermal-printer-oriented, 1 bit graphics renderer for 2D and 3D *
* A Header-only C99 library *
* MIT LICENSE (c) Lingdong Huang 2020 *
* ===================================================================== */
#ifndef R1B_HDR
#define R1B_HDR
// ---------------
// CONFIGS
// ---------------
#ifndef R1B_CONFIG_LINE3D_EPSILON
#define R1B_CONFIG_LINE3D_EPSILON 0.015
#endif
#ifndef R1B_CONFIG_UP2X_FLOAT_EPSILON
#define R1B_CONFIG_UP2X_FLOAT_EPSILON 0.1
#endif
#ifndef R1B_CONFIG_STBI_PATH
#define R1B_CONFIG_STBI_PATH "external/stb_image.h"
#endif
#ifndef R1B_CONFIG_STBIW_PATH
#define R1B_CONFIG_STBIW_PATH "external/stb_image_write.h"
#endif
// ---------------
// MORE CONFIGS
// ---------------
// R1B_CONFIG_NO_FG8X12
// R1B_CONFIG_NO_STBI
#define _GNU_SOURCE // for getline
// TODO: cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/pkgtools/libnbcompat/files/getdelim.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#define STB_IMAGE_IMPLEMENTATION
#include R1B_CONFIG_STBI_PATH
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include R1B_CONFIG_STBIW_PATH
// ---------------
// ENUMS
// ---------------
#define R1B_INFER -42
#define R1B_DTHR_ORD 1
#define R1B_DTHR_FS 2
#define R1B_SMPL_NN 11
#define R1B_SMPL_BILINEAR 12
#define R1B_BRDR_COPY 21
#define R1B_BRDR_ZERO 22
#define R1B_BRDR_NONE 23
#define R1B_BRDR_WRAP 24
#define R1B_FONT_HEX 31
#define R1B_BLIT_OR 41
#define R1B_BLIT_ADD 42
#define R1B_BLIT_FLIP 43
#define R1B_BLIT_SET 44
#define R1B_POLY_CONVEX 51
#define R1B_POLY_CONCAVE 52
#define R1B_WIRE_NONE 61
#define R1B_WIRE_FRONT 62
#define R1B_WIRE_ALL 63
#define R1B_SHDR_NONE 70
#define R1B_SHDR_FLAT 71
#define R1B_SHDR_NDOTL 72
#define R1B_SHDR_NDOTLF 73
#define R1B_UP2X_SAA5050 81
#define R1B_UP2X_EPX 82
#define R1B_UP2X_EAGLE 83
#define R1B_UP2X_HQX 84
#define R1B_KERN_ELLIPSE 91
#define R1B_KERN_GAUSS 92
#define R1B_KERN_GAUSS1D 93
#define R1B_KERN_CROSS 94
#define R1B_KERN_RECT 95
#define R1B_BLUR_GAUSS 111
#define R1B_BLUR_BOX 112
#define R1B_FLAG_SORTED 1
/**
* @brief datatype for a (1-channel grayscale or binary) image
*/
typedef struct {
int w; /**< width */
int h; /**< height */
float* data; /**< image data: row-major array of floats */
} r1b_im_t;
/**
* @brief datatype for a bitmap font
*/
typedef struct {
int h; /**< height of font */
char* glyphs; /**< number of glyphs */
int n; /**< pointer to data for glyphs */
uint32_t* offsets; /**< array of data offsets for each glyph */
uint8_t* sizes; /**< array of sizes (width/4) for each glyph */
uint16_t* cmap; /**< array of unicode code points for each glyph */
int flags; /**< bitwise OR of flags:
R1B_FLAG_SORTED: whether the glyphs are sorted by codepoint
*/
} r1b_font_t;
/**
* @brief datatype for a 3D mesh
*/
typedef struct {
float* X; /**< array of x coordinate of vertices */
float* Y; /**< array of y coordinate of vertices */
float* Z; /**< array of z coordinate of vertices */
int* tris; /**< array of triangles (using vertex index) */
float* norms; /**< array of vertex normals (optional, can be NULL) */
int n_vtx; /**< number of vertices */
int n_tri; /**< number of triangles */
} r1b_mesh_t;
static const float r1b_dither_ord_idx[16] = {
0.0/16.0,8.0/16.0,2.0/16.0,10.0/16.0,
12.0/16.0,4.0/16.0,14.0/16.0,6.0/16.0,
3.0/16.0,11.0/16.0,1.0/16.0,9.0/16.0,
15.0/16.0,7.0/16.0,13.0/16.0,5.0/16.0};
#define R1B_MAX(x, y) (((x) > (y)) ? (x) : (y))
#define R1B_MIN(x, y) (((x) < (y)) ? (x) : (y))
static float r1b_pttn_data_SOLID[] = {1};
static float r1b_pttn_data_GRAY5[] = {1,1,0,1, 1,1,1,1, 0,1,1,1, 1,1,1,1, };
static float r1b_pttn_data_GRAY4[] = {1,1,0,1, 0,1,1,1, 1,1,0,1, 0,1,1,1, };
static float r1b_pttn_data_GRAY3[] = {1,0, 0,1, };
static float r1b_pttn_data_GRAY2[] = {0,0,1,0, 1,0,0,0, 0,0,1,0, 1,0,0,0, };
static float r1b_pttn_data_GRAY1[] = {0,0,1,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, };
static float r1b_pttn_data_EMPTY[] = {0};
static float r1b_pttn_data_GRID1[] = {0,1, 1,1, };
static float r1b_pttn_data_GRID2[] = {0,0,0,1, 0,0,0,1, 0,0,0,1, 1,1,1,1, };
static float r1b_pttn_data_DOTS1[] = {1,0, 0,0, };
static float r1b_pttn_data_DOTS2[] = {1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, };
static float r1b_pttn_data_DOTSR[] = {0,0,0,0,0,1,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,1,0,0,0,0,0,0,0,0,0,1, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,1,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,1,0,0,0,0,0,1, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0, 0,0,1,0,0,0,0,0,0,1,0,0, };
static float r1b_pttn_data_HRZL1[] = {1, 0, };
static float r1b_pttn_data_VRTL1[] = {1,0, };
static float r1b_pttn_data_HRZL2[] = {1, 0, 0, 0, };
static float r1b_pttn_data_VRTL2[] = {1,0,0,0, };
static float r1b_pttn_data_DGNLL[] = {0,0,0,1, 0,0,1,0, 0,1,0,0, 1,0,0,0, };
static float r1b_pttn_data_DGNLR[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1, };
static float r1b_pttn_data_CROSS[] = {1,0,0,0, 0,1,0,1, 0,0,1,0, 0,1,0,1, };
static float r1b_pttn_data_BRICK[] = {1,1,1,1,1,1,1,1, 0,0,0,0,1,0,0,0, 0,0,0,0,1,0,0,0, 0,0,0,0,1,0,0,0, 1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0, };
static float r1b_pttn_data_SCALE[] = {0,0,0,0,1,0,0,0, 0,0,0,1,0,1,0,0, 1,1,1,0,0,0,1,1, 1,0,0,0,0,0,0,0, 1,0,0,0,0,0,0,0, 0,1,0,0,0,0,0,1, 0,0,1,1,1,1,1,0, 0,0,0,0,1,0,0,0, };
static float r1b_pttn_data_WAVES[] = {0,0,0,0,0,0,1,0, 0,0,0,0,0,1,0,1, 1,0,0,0,1,0,0,0, 1,0,0,0,1,0,0,0, 1,0,0,0,1,0,0,0, 1,0,0,0,1,0,0,0, 0,1,0,1,0,0,0,0, 0,0,1,0,0,0,0,0, };
static float r1b_pttn_data_CHESS[] = {1,1,0,0, 1,1,0,0, 0,0,1,1, 0,0,1,1, };
static float r1b_pttn_data_DMOND[] = {0,0,0,0,1,0,0,0, 0,0,0,1,0,1,0,0, 0,0,1,0,1,0,1,0, 0,1,0,1,0,1,0,1, 0,0,1,0,1,0,1,0, 0,0,0,1,0,1,0,0, 0,0,0,0,1,0,0,0, 0,0,0,0,0,0,0,0, };
static const int r1b_pttn_w_SOLID = 1; static const int r1b_pttn_h_SOLID = 1;
static const int r1b_pttn_w_GRAY5 = 4; static const int r1b_pttn_h_GRAY5 = 4;
static const int r1b_pttn_w_GRAY4 = 4; static const int r1b_pttn_h_GRAY4 = 4;
static const int r1b_pttn_w_GRAY3 = 2; static const int r1b_pttn_h_GRAY3 = 2;
static const int r1b_pttn_w_GRAY2 = 4; static const int r1b_pttn_h_GRAY2 = 4;
static const int r1b_pttn_w_GRAY1 = 4; static const int r1b_pttn_h_GRAY1 = 4;
static const int r1b_pttn_w_EMPTY = 1; static const int r1b_pttn_h_EMPTY = 1;
static const int r1b_pttn_w_GRID1 = 2; static const int r1b_pttn_h_GRID1 = 2;
static const int r1b_pttn_w_GRID2 = 4; static const int r1b_pttn_h_GRID2 = 4;
static const int r1b_pttn_w_DOTS1 = 2; static const int r1b_pttn_h_DOTS1 = 2;
static const int r1b_pttn_w_DOTS2 = 4; static const int r1b_pttn_h_DOTS2 = 4;
static const int r1b_pttn_w_DOTSR =12; static const int r1b_pttn_h_DOTSR =12;
static const int r1b_pttn_w_HRZL1 = 1; static const int r1b_pttn_h_HRZL1 = 2;
static const int r1b_pttn_w_VRTL1 = 2; static const int r1b_pttn_h_VRTL1 = 1;
static const int r1b_pttn_w_HRZL2 = 1; static const int r1b_pttn_h_HRZL2 = 4;
static const int r1b_pttn_w_VRTL2 = 4; static const int r1b_pttn_h_VRTL2 = 1;
static const int r1b_pttn_w_DGNLL = 4; static const int r1b_pttn_h_DGNLL = 4;
static const int r1b_pttn_w_DGNLR = 4; static const int r1b_pttn_h_DGNLR = 4;
static const int r1b_pttn_w_CROSS = 4; static const int r1b_pttn_h_CROSS = 4;
static const int r1b_pttn_w_BRICK = 8; static const int r1b_pttn_h_BRICK = 8;
static const int r1b_pttn_w_SCALE = 8; static const int r1b_pttn_h_SCALE = 8;
static const int r1b_pttn_w_WAVES = 8; static const int r1b_pttn_h_WAVES = 8;
static const int r1b_pttn_w_CHESS = 4; static const int r1b_pttn_h_CHESS = 4;
static const int r1b_pttn_w_DMOND = 8; static const int r1b_pttn_h_DMOND = 8;
r1b_im_t* r1b_pttn_SOLID = NULL; r1b_im_t* r1b_pttn_GRAY5 = NULL;
r1b_im_t* r1b_pttn_GRAY4 = NULL; r1b_im_t* r1b_pttn_GRAY3 = NULL;
r1b_im_t* r1b_pttn_GRAY2 = NULL; r1b_im_t* r1b_pttn_GRAY1 = NULL;
r1b_im_t* r1b_pttn_EMPTY = NULL; r1b_im_t* r1b_pttn_GRID1 = NULL;
r1b_im_t* r1b_pttn_GRID2 = NULL; r1b_im_t* r1b_pttn_DOTS1 = NULL;
r1b_im_t* r1b_pttn_DOTS2 = NULL; r1b_im_t* r1b_pttn_DOTSR = NULL;
r1b_im_t* r1b_pttn_HRZL1 = NULL; r1b_im_t* r1b_pttn_VRTL1 = NULL;
r1b_im_t* r1b_pttn_HRZL2 = NULL; r1b_im_t* r1b_pttn_VRTL2 = NULL;
r1b_im_t* r1b_pttn_DGNLL = NULL; r1b_im_t* r1b_pttn_DGNLR = NULL;
r1b_im_t* r1b_pttn_CROSS = NULL; r1b_im_t* r1b_pttn_BRICK = NULL;
r1b_im_t* r1b_pttn_SCALE = NULL; r1b_im_t* r1b_pttn_WAVES = NULL;
r1b_im_t* r1b_pttn_CHESS = NULL; r1b_im_t* r1b_pttn_DMOND = NULL;
#define R1B_PATTERN(x) \
((r1b_pttn_ ## x) ? (r1b_pttn_ ## x) : (\
r1b_pttn_ ## x = (r1b_im_t*)malloc(sizeof(r1b_im_t)),\
r1b_pttn_ ## x -> data = r1b_pttn_data_ ## x,\
r1b_pttn_ ## x -> w = r1b_pttn_w_ ## x,\
r1b_pttn_ ## x -> h = r1b_pttn_h_ ## x,\
r1b_pttn_ ## x \
))
#define R1B_DESTROY_PATTERN(x) if (r1b_pttn_ ## x) {free(r1b_pttn_ ## x); r1b_pttn_ ## x = NULL;};
#ifndef R1B_CONFIG_NO_FG8X12
// converted from freeglut/freeglut_font_data.c
// ==============================================================
// this license below is from freeGlut, (freeglut_font_data.c)
/*
* The legal status of this file is a bit vague. The font glyphs
* themselves come from XFree86 v4.3.0 (as of this writing), and as
* part of the X server may be subject to the XFree86 copyrights.
* The original freeglut fonts were extracted by a utility written
* by Pawel W. Olszta (see below) and the generated fonts contained
* his copyright exclusively. Steve Baker asserts that Pawel
* assigned intellectual property rights to Steve Baker. Steve
* Baker also asserts that fonts cannot be copyrighted. He has
* neither stripped the copyright from the freeglut fonts nor
* formally retitled anything in his name. Since that time, the
* OpenGLUT project has branched from freeglut, and has made
* necessary modifications to Pawel's ``genfonts'' utility.
* To that extent, OpenGLUT may have some title to this file.
* What is fairly clear is that the font data is licensed under
* the XFree86 license (which is variously termed ``XFree'' and
* ``MIT'' by the freeglut project). It is believed that all
* title holders wish this file to be as useful as possible, and
* that either the ``XFree'' or ``MIT'' license works.
*
* Portions copyright (c) 2004, the OpenGLUT project contributors.
* OpenGLUT branched from freeglut in February, 2004.
*
* Copyright (c) 1999-2000 by Pawel W. Olszta
* Written by Pawel W. Olszta, <olszta@sourceforge.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Sotware.
*/
// ==============================================================
static char R1B_FG8X12_GLYPHS [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,16,16,16,16,16,16,16,0,16,0,0,0,36,36,36,0,0,0,0,0,0,0,0,0,0,36,36,126,36,126,36,36,0,0,0,0,16,60,80,80,56,20,20,120,16,0,0,0,34,82,36,8,8,16,36,42,68,0,0,0,0,0,48,72,72,48,74,68,58,0,0,0,56,48,64,0,0,0,0,0,0,0,0,0,4,8,8,16,16,16,8,8,4,0,0,0,32,16,16,8,8,8,16,16,32,0,0,0,0,0,36,24,126,24,36,0,0,0,0,0,0,0,16,16,124,16,16,0,0,0,0,0,0,0,0,0,0,0,0,56,48,64,0,0,0,0,0,0,126,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,56,16,0,0,2,2,4,8,16,32,64,128,128,0,0,0,24,36,66,66,66,66,66,36,24,0,0,0,16,48,80,16,16,16,16,16,124,0,0,0,60,66,66,2,4,24,32,64,126,0,0,0,126,2,4,8,28,2,2,66,60,0,0,0,4,12,20,36,68,68,126,4,4,0,0,0,126,64,64,92,98,2,2,66,60,0,0,0,28,32,64,64,92,98,66,66,60,0,0,0,126,2,4,8,8,16,16,32,32,0,0,0,60,66,66,66,60,66,66,66,60,0,0,0,60,66,66,70,58,2,2,4,56,0,0,0,0,0,16,56,16,0,0,16,56,16,0,0,0,0,16,56,16,0,0,56,48,64,0,0,2,4,8,16,32,16,8,4,2,0,0,0,0,0,0,126,0,0,126,0,0,0,0,0,64,32,16,8,4,8,16,32,64,0,0,0,60,66,66,2,4,8,8,0,8,0,0,0,60,66,66,78,82,86,74,64,60,0,0,0,24,36,66,66,66,126,66,66,66,0,0,0,252,66,66,66,124,66,66,66,252,0,0,0,60,66,64,64,64,64,64,66,60,0,0,0,252,66,66,66,66,66,66,66,252,0,0,0,126,64,64,64,120,64,64,64,126,0,0,0,126,64,64,64,120,64,64,64,64,0,0,0,60,66,64,64,64,78,66,70,58,0,0,0,66,66,66,66,126,66,66,66,66,0,0,0,124,16,16,16,16,16,16,16,124,0,0,0,31,4,4,4,4,4,4,68,56,0,0,0,66,68,72,80,96,80,72,68,66,0,0,0,64,64,64,64,64,64,64,64,126,0,0,0,130,130,198,170,146,146,130,130,130,0,0,0,66,66,98,82,74,70,66,66,66,0,0,0,60,66,66,66,66,66,66,66,60,0,0,0,124,66,66,66,124,64,64,64,64,0,0,0,60,66,66,66,66,66,82,74,60,2,0,0,124,66,66,66,124,80,72,68,66,0,0,0,60,66,64,64,60,2,2,66,60,0,0,0,254,16,16,16,16,16,16,16,16,0,0,0,66,66,66,66,66,66,66,66,60,0,0,0,130,130,68,68,68,40,40,40,16,0,0,0,130,130,130,130,146,146,146,170,68,0,0,0,130,130,68,40,16,40,68,130,130,0,0,0,130,130,68,40,16,16,16,16,16,0,0,0,126,2,4,8,16,32,64,64,126,0,0,0,60,32,32,32,32,32,32,32,60,0,0,0,128,128,64,32,16,8,4,2,2,0,0,0,120,8,8,8,8,8,8,8,120,0,0,0,16,40,68,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,254,0,0,56,24,4,0,0,0,0,0,0,0,0,0,0,0,0,60,2,62,66,70,58,0,0,0,64,64,64,92,98,66,66,98,92,0,0,0,0,0,0,60,66,64,64,66,60,0,0,0,2,2,2,58,70,66,66,70,58,0,0,0,0,0,0,60,66,126,64,66,60,0,0,0,28,34,32,32,124,32,32,32,32,0,0,0,0,0,0,58,68,68,56,64,60,66,60,0,64,64,64,92,98,66,66,66,66,0,0,0,0,16,0,48,16,16,16,16,124,0,0,0,0,4,0,12,4,4,4,4,68,68,56,0,64,64,64,68,72,112,72,68,66,0,0,0,48,16,16,16,16,16,16,16,124,0,0,0,0,0,0,236,146,146,146,146,130,0,0,0,0,0,0,92,98,66,66,66,66,0,0,0,0,0,0,60,66,66,66,66,60,0,0,0,0,0,0,92,98,66,98,92,64,64,64,0,0,0,0,58,70,66,70,58,2,2,2,0,0,0,0,92,34,32,32,32,32,0,0,0,0,0,0,60,66,48,12,66,60,0,0,0,0,32,32,124,32,32,32,34,28,0,0,0,0,0,0,68,68,68,68,68,58,0,0,0,0,0,0,68,68,68,40,40,16,0,0,0,0,0,0,130,130,146,146,170,68,0,0,0,0,0,0,66,36,24,24,36,66,0,0,0,0,0,0,66,66,66,70,58,2,66,60,0,0,0,0,126,4,8,16,32,126,0,0,0,14,16,16,8,48,8,16,16,14,0,0,0,16,16,16,16,16,16,16,16,16,0,0,0,112,8,8,16,12,16,8,8,112,0,0,0,36,84,72,0,0,0,0,0,0,0,0};
static uint32_t R1B_FG8X12_OFFSETS[] = {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188 };
static uint8_t R1B_FG8X12_SIZES [] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
static uint16_t R1B_FG8X12_CMAP [] = {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};
r1b_font_t* r1b_font_fg8x12 = NULL;
#define R1B_FONT_FG8X12\
((r1b_font_fg8x12) ? (r1b_font_fg8x12) : (\
r1b_font_fg8x12 = (r1b_font_t*)malloc(sizeof(r1b_font_t)),\
r1b_font_fg8x12 -> glyphs = R1B_FG8X12_GLYPHS ,\
r1b_font_fg8x12 -> offsets= R1B_FG8X12_OFFSETS,\
r1b_font_fg8x12 -> sizes = R1B_FG8X12_SIZES ,\
r1b_font_fg8x12 -> cmap = R1B_FG8X12_CMAP ,\
r1b_font_fg8x12 -> flags = R1B_FLAG_SORTED ,\
r1b_font_fg8x12 -> h = 12,\
r1b_font_fg8x12 -> n = 95,\
r1b_font_fg8x12 \
))
// non-macro wrapper for swig etc.
r1b_font_t* r1b_get_font_fg8x12(){{
return R1B_FONT_FG8X12;
}}
#endif /*R1B_CONFIG_NO_FG8X12*/
float* r1b_tmp0 = NULL;
int r1b_tmp0_size = 0;
float* r1b_tmp1 = NULL;
int r1b_tmp1_size = 0;
/**
* @brief (internal use) allocate temporary buffer for certain operations
* @param w width
* @param h height
*/
void r1b_make_tmp0(int w, int h){
int l = w*h;
if (!r1b_tmp0){
r1b_tmp0_size = l;
r1b_tmp0 = (float*)malloc(r1b_tmp0_size*sizeof(float));
}else if (r1b_tmp0_size < l){
r1b_tmp0_size = l;
r1b_tmp0 = (float*)realloc(r1b_tmp0, r1b_tmp0_size*sizeof(float));
}
}
/**
* @brief (internal use) allocate temporary buffer for certain operations
* @param w width
* @param h height
*/
void r1b_make_tmp1(int w, int h){
int l = w*h;
if (!r1b_tmp1){
r1b_tmp1_size = l;
r1b_tmp1 = (float*)malloc(r1b_tmp1_size*sizeof(float));
}else if (r1b_tmp1_size < l){
r1b_tmp1_size = l;
r1b_tmp1 = (float*)realloc(r1b_tmp1, r1b_tmp1_size*sizeof(float));
}
}
/**
* @brief free all resources allocated internally by the library
*/
void r1b_cleanup(){
if (r1b_tmp0){
r1b_tmp0_size = 0;
free(r1b_tmp0);
}
if (r1b_tmp1){
r1b_tmp1_size = 0;
free(r1b_tmp1);
}
#ifndef R1B_CONFIG_NO_FG8X12
if (r1b_font_fg8x12){
free(r1b_font_fg8x12);
}
#endif
R1B_DESTROY_PATTERN(SOLID); R1B_DESTROY_PATTERN(GRAY5);
R1B_DESTROY_PATTERN(GRAY4); R1B_DESTROY_PATTERN(GRAY3);
R1B_DESTROY_PATTERN(GRAY2); R1B_DESTROY_PATTERN(GRAY1);
R1B_DESTROY_PATTERN(EMPTY); R1B_DESTROY_PATTERN(GRID1);
R1B_DESTROY_PATTERN(GRID2); R1B_DESTROY_PATTERN(DOTS1);
R1B_DESTROY_PATTERN(DOTS2); R1B_DESTROY_PATTERN(DOTSR);
R1B_DESTROY_PATTERN(HRZL1); R1B_DESTROY_PATTERN(VRTL1);
R1B_DESTROY_PATTERN(HRZL2); R1B_DESTROY_PATTERN(VRTL2);
R1B_DESTROY_PATTERN(DGNLL); R1B_DESTROY_PATTERN(DGNLR);
R1B_DESTROY_PATTERN(CROSS); R1B_DESTROY_PATTERN(BRICK);
R1B_DESTROY_PATTERN(SCALE); R1B_DESTROY_PATTERN(WAVES);
R1B_DESTROY_PATTERN(CHESS); R1B_DESTROY_PATTERN(DMOND);
}
#ifndef R1B_CONFIG_NO_STBI
/**
* @brief read an image from disk as grayscale
* @param path the file path
* @return an image
*/
r1b_im_t r1b_read(const char* path){
int width, height, channels;
unsigned char *image = stbi_load(path,
&width,
&height,
&channels,
STBI_grey);
float* data = (float*)malloc(width*height*sizeof(float));
int i; for (i= 0; i < height; i++){
int j; for (j= 0; j < width; j++){
data[i*width+j] = 1.0-(float)image[i*width+j]/255.0;
}
}
stbi_image_free(image);
r1b_im_t im;
im.w = width;
im.h = height;
im.data = data;
return im;
}
/**
* @brief write image to file, supported: jpg, png, bmp
* @param path the file path
* @param im an image
*/
void r1b_snapshot(const char* path, r1b_im_t* im){
unsigned char *image = (unsigned char*)malloc(sizeof(unsigned char)*im->w*im->h);
int i; for (i= 0; i < im->h; i++ ){
int j; for (j= 0; j < im->w; j++ ){
image[i*im->w+j] = (unsigned char)(int) ((1.0-fmin(fmax(im->data[i*im->w+j],0.0),1.0)) * 255.0);
}
}
char* ext = (char*)path + strlen(path);
while (ext > (char*)path && *(char*)ext != '.') {
ext--;
}
ext ++;
if (strcmp(ext,"jpg")==0 || strcmp(ext,"jpeg")==0){
stbi_write_jpg(path, im->w, im->h, 1, image, 90);
}else if (strcmp(ext,"png")==0){
stbi_write_png(path, im->w, im->h, 1, image, im->w);
}else if (strcmp(ext,"bmp")==0){
stbi_write_bmp(path, im->w, im->h, 1, image);
}
}
#endif /*R1B_CONFIG_NO_STBI*/
/**
* @brief create image filled with zeros
* @param w width
* @param h height
* @return an image
*/
r1b_im_t r1b_zeros(int w, int h){
r1b_im_t im;
im.w = w;
im.h = h;
im.data = (float*)calloc(w*h,sizeof(float));
return im;
}
/**
* @brief create image filled with ones
* @param w width
* @param h height
* @return an image
*/
r1b_im_t r1b_ones(int w, int h){
int l = w*h;
r1b_im_t im;
im.w = w;
im.h = h;
im.data = (float*)malloc(l*sizeof(float));
int i; for (i= 0; i < l; i++){
im.data[i] = 1.0;
}
return im;
}
/**
* @brief create image from data pointed by array pointer, copying the data over
* @param w width
* @param h height
* @param arr an array that is at least w x h in size
* @return an image
*/
r1b_im_t r1b_from_array(int w, int h, float* arr){
int l = w*h;
r1b_im_t im;
im.w = w;
im.h = h;
im.data = (float*)malloc(l*sizeof(float));
memcpy(im.data, arr, l*sizeof(float));
return im;
}
/**
* @brief create image filled with FLT_MAX
* @param w width
* @param h height
* @return an image
*/
r1b_im_t r1b_infs(int w, int h){
int l = w*h;
r1b_im_t im;
im.w = w;
im.h = h;
im.data = (float*)malloc(l*sizeof(float));
int i; for (i= 0; i < l; i++){
im.data[i] = FLT_MAX;
}
return im;
}
/**
* @brief free data allocated in an image; does not free the pointer itself
* @param im pointer to image
*/
void r1b_free(r1b_im_t* im){
free(im->data);
im->data = NULL;
}
/**
* @brief transpose an image
* @param im pointer to image
*/
void r1b_transpose(r1b_im_t* im){
// TODO: cache friendly
r1b_make_tmp0(im->w,im->h);
int i; for (i= 0; i < im->h; i++){
int j; for (j= 0; j < im->w; j++){
r1b_tmp0[j*im->h+i] = im->data[i*im->w+j];
}
}
int tmp = im->w;
im->w = im->h;
im->h = tmp;
memcpy(im->data, r1b_tmp0, im->w*im->h*sizeof(float));
}
/**
* @brief transpose and flip an image, effectively rotating it by 90 degrees.
* @param im pointer to image
*/
void r1b_transpose_flip(r1b_im_t* im){
// TODO: cache friendly
r1b_make_tmp0(im->w,im->h);
int i; for (i= 0; i < im->h; i++){
int j; for (j= 0; j < im->w; j++){
r1b_tmp0[j*im->h+i] = im->data[i*im->w+im->w-1-j];
}
}
int tmp = im->w;
im->w = im->h;
im->h = tmp;
memcpy(im->data, r1b_tmp0, im->w*im->h*sizeof(float));
}
/**
* @brief normalize an image to a given interval
* @param im pointer to image
* @param lo new min value
* @param hi new max value
*/
void r1b_normalize(r1b_im_t* im, float lo, float hi){
float m = FLT_MAX;
float M = -FLT_MAX;
int i;
for (i = 0; i < im->h*im->w; i++){
if (im->data[i] == FLT_MAX || im->data[i] == -FLT_MAX){
continue;
}
m = fmin(im->data[i],m);
M = fmax(im->data[i],M);
}
float hi_lo = hi - lo;
for (i = 0; i < im->h*im->w; i++){
im->data[i] = fmin(fmax( ((im->data[i]-m)/M) * hi_lo + lo ,lo),hi);
}
}
/**
* @brief duplicates an image
* @param im pointer to image
* @return the clone
*/
r1b_im_t r1b_copy_of(r1b_im_t* im){
int s = im->w*im->h*sizeof(float);
r1b_im_t dst;
dst.w = im->w;
dst.h = im->h;
dst.data = (float*)malloc(s);
memcpy(dst.data, im->data, s);
return dst;
}
/**
* @brief copy data from one image to another
* the images should have the same width, and destination is at least
* the same height as source
* @param im source: pointer to image to be copied from
* @param dst destination: pointer to image to be copied into
*/
void r1b_copy_to(r1b_im_t* im, r1b_im_t* dst){
memcpy(dst->data, im->data, im->w*im->h*sizeof(float));
}
/**
* @brief (internal use) apply floyd-steinberg dithering to a grayscale image
* @param im pointer to image
*/
void r1b_dither_fs(r1b_im_t* im) {
r1b_make_tmp0(im->w,im->h);
r1b_make_tmp1(im->w,im->h);
memset(r1b_tmp1,0,im->w*im->h*sizeof(float));
int i; for (i= 0; i < im->h; i++) {
int j; for (j= 0; j < im->w; j++) {
float o = im->data[i*im->w+j] + r1b_tmp1[i*im->w+j];
float n = round(o);
float qe = o - n;
if(j<im->w-1){ r1b_tmp1[ i *im->w+ j+1] += qe * 7.0/16.0; }
if(i<im->h-1){if(j!=0){r1b_tmp1[(i+1)*im->w+ j-1] += qe * 3.0/16.0; }
r1b_tmp1[(i+1)*im->w+ j ] += qe * 5.0/16.0;
if(j<im->w-1){ r1b_tmp1[(i+1)*im->w+ j+1] += qe * 1.0/16.0; }}
r1b_tmp0[i*im->w+j] = n;
}
}
memcpy(im->data, r1b_tmp0, im->w*im->h*sizeof(float));
}
/**
* @brief (internal use) apply ordered dithering to a grayscale image
* @param im pointer to image
*/
void r1b_dither_ord(r1b_im_t* im){
r1b_make_tmp0(im->w,im->h);
int i; for (i= 0; i < im->h; i++ ){
int j; for (j= 0; j < im->w; j++ ){
float o = im->data[i*im->w+j];
float n = round(o);
float qe = fabs(o - n);
float d = r1b_dither_ord_idx[(i%4)*4+j%4];
if (qe < d) {
r1b_tmp0[i*im->w+j] = n;
}else{
r1b_tmp0[i*im->w+j] = 1.0-n;
}
}
}
memcpy(im->data, r1b_tmp0, im->w*im->h*sizeof(float));
}
/**
* @brief apply dithering to a grayscale image
* turning it into binary image using specified algorithm
* @param im pointer to image
* @param mode the algorithm to use, either R1B_DTHR_ORD or R1B_DTHR_FS
*/
void r1b_dither(r1b_im_t* im, int mode){
switch(mode){
case R1B_DTHR_ORD:
r1b_dither_ord(im);
break;
case R1B_DTHR_FS:
default:
r1b_dither_fs(im);
}
}
/**
* @brief print an image to stdout for debugging
* @param im pointer to image
*/
void r1b_log(r1b_im_t* im){
int i; for (i= 0; i < im->h; i++) {
int j; for (j= 0; j < im->w; j++) {
if (im->data[i*im->w+j]>=0.5){
printf("* ");
}else{
printf(" ");
}
}
printf("\n");
}
}
#define R1B_PUT8(x) bytes[cnt_bytes] = x; cnt_bytes ++;
/**
* @brief encode an image to ESC/POS commands
* @param im pointer to image
* @param n_bytes_written size of the returned buffer in bytes
* @return an array of bytes
*/
char* r1b_encode(r1b_im_t* im, int* n_bytes_written){
int l0 = im->w%256;
int ll = (int)(im->w/256);
int num_bytes = ((im->h+23)/24)*8 + 2 + im->w*3*((im->h+23)/24);
int cnt_bytes = 0;
char* bytes = (char*) malloc(sizeof(char)*num_bytes);
R1B_PUT8(0x1b); R1B_PUT8(0x40);
int r; for (r= 0; r < im->h; r+= 24){
//ESC * m nL nH
R1B_PUT8(0x1b);R1B_PUT8(0x2a);R1B_PUT8(33);R1B_PUT8((char)l0);R1B_PUT8((char)ll);
int c; for (c= 0; c < im->w; c ++ ){
int n; for (n= 0; n < 3; n++ ){
char x = 0;
int h; for (h= 0; h < 8; h++ ){
x = x << 1;
if ( (r+n*8+h)<=im->h-1 && im->data[R1B_MIN(r+n*8+h,im->h-1)*im->w+c]>=0.5 ){
x = x | 1;
}
}
R1B_PUT8(x);
}
}
R1B_PUT8(0x1b);R1B_PUT8(0x4a);R1B_PUT8(0);
}
// printf("%d %d\n",num_bytes,cnt_bytes);
*n_bytes_written = cnt_bytes;
return bytes;
}
/**
* @brief encode an image to ESC/POS commands and write it to a file
* @param path file path to write to
* @param im pointer to image
*/
void r1b_encode2file(const char* path, r1b_im_t* im){
FILE * fp;
fp = fopen(path, "wb");
int n;
char* buf = r1b_encode(im,&n);
fwrite(buf , 1, n, fp);
fclose(fp);
}
/**
* @brief send an image to a thermal printer for printing using LPR command
* @param printer the name of the printer
* @param im pointer to image
*/
void r1b_lpr(const char* printer, r1b_im_t* im){
char buf[256];
r1b_encode2file("r1b_tmp",im);
sprintf(buf,"lpr -P%s -o raw r1b_tmp",printer);
system(buf);
}
/**
* @brief (internal use) nearest neighbor resampling
* @param im pointer to image
* @param w new width
* @param h new height
*/
void r1b_resample_nearest(r1b_im_t* im, int w, int h){
r1b_make_tmp0(w,h);
float hs = (float)im->h/(float)h;
float ws = (float)im->w/(float)w;
int i; for (i= 0; i < h; i++){
int j; for (j= 0; j < w; j++){
int ii = R1B_MIN( im->h-1, (int)round((float)i*hs) );
int jj = R1B_MIN( im->w-1, (int)round((float)j*ws) );
r1b_tmp0[i*w+j] = im->data[ii*im->w+jj];
}
}
// swap
r1b_tmp0_size = im->w*im->h;
float* tmp = im->data;
im->w = w;
im->h = h;
im->data = r1b_tmp0;
r1b_tmp0 = tmp;
}
/**
* @brief (internal use) bilinear resampling
* @param im pointer to image
* @param w new width
* @param h new height
*/
void r1b_resample_bilinear(r1b_im_t* im, int w, int h){
r1b_make_tmp0(w,h);
float hs = (float)im->h/(float)h;
float ws = (float)im->w/(float)w;
int i; for (i= 0; i < h; i++){
int j; for (j= 0; j < w; j++){
float ii = (float)i*hs;
float jj = (float)j*ws;
int i0 = (int)ii;
int i1 = R1B_MIN(im->h-1,(int)ceil(ii));
int j0 = (int)jj;
int j1 = R1B_MIN(im->w-1,(int)ceil(jj));
float it = (float)ii-(float)i0;
float jt = (float)jj-(float)j0;
float x00 = im->data[i0*im->w+j0];
float x01 = im->data[i0*im->w+j1];
float x10 = im->data[i1*im->w+j0];
float x11 = im->data[i1*im->w+j1];
r1b_tmp0[i*w+j] = (x00*(1-jt)+x01*jt)*(1-it)+(x10*(1-jt)+x11*jt)*it;
}
}
// swap
r1b_tmp0_size = im->w*im->h;
float* tmp = im->data;
im->w = w;
im->h = h;
im->data = r1b_tmp0;
r1b_tmp0 = tmp;
}
/**
* @brief resample image to a different size
* @param im pointer to image
* @param w new width, pass R1B_INFER to scale proportionally per new height
* @param h new height, pass R1B_INFER to scale proportionally per new width
* @param mode the alogirthm, either R1B_SMPL_NN or R1B_SMPL_BILINEAR
*/
void r1b_resample(r1b_im_t* im, int w, int h, int mode){
if (w == R1B_INFER){
w = (int)round((float)h*(float)(im->w)/(float)(im->h));
}
if (h == R1B_INFER){
h = (int)round((float)w*(float)(im->h)/(float)(im->w));
}
switch(mode){
case R1B_SMPL_BILINEAR:
r1b_resample_bilinear(im,w,h);
break;
case R1B_SMPL_NN:
default:
r1b_resample_nearest(im,w,h);
}
}
/**
* @brief retrieve the value of a pixel at given coordinate
* @param im pointer to image
* @param x x coordinate
* @param y y coordinate
* @param mode border handling when coordinate is outside image:
* R1B_BRDR_ZERO: zero padded;
* R1B_BRDR_COPY: copy padded;
* R1B_BRDR_WRAP: wrap around;
* R1B_BRDR_NONE: no boundry checking (expect funny result or segfault when out of range).
*/
float r1b_get(r1b_im_t* im, int x, int y, int mode){
if (mode == R1B_BRDR_ZERO){
if (x< 0 || x >= im->w || y < 0 || y >= im->h){
return 0;
}
}else if (mode == R1B_BRDR_COPY){
x = R1B_MIN(R1B_MAX(x,0),im->w-1);
y = R1B_MIN(R1B_MAX(y,0),im->h-1);
}else if (mode == R1B_BRDR_WRAP){
while (x < 0 && im->w > 0) {
x += im->w;
}
while (y < 0 && im->h > 0) {
y += im->h;
}
x = x % im->w;
y = y % im->h;
}
return im->data[y*im->w+x];
}
/**
* @brief set the value of a pixel at given coordinate
* @param im pointer to image
* @param x x coordinate
* @param y y coordinate
* @param val the value of the pixel
* @param mode blit mode, one of:
* R1B_BLIT_SET: overwrite the pixel;
* R1B_BLIT_FLIP: flip the value of the pixel;
* R1B_BLIT_OR: turn on the pixel if it's off;
* R1B_BLIT_ADD: add the value to the original.
*/
void r1b_set(r1b_im_t* im, int x, int y, float val, int mode){
if (x< 0 || x >= im->w || y < 0 || y >= im->h){
return;
}
float o = im->data[y*im->w+x];
switch (mode) {
case R1B_BLIT_SET:
im->data[y*im->w+x] = val;
break;
case R1B_BLIT_FLIP:
if (val > 0.5){
im->data[y*im->w+x] = 1-o;
}
break;
case R1B_BLIT_OR:
im->data[y*im->w+x] = fmax(val,o);
break;
case R1B_BLIT_ADD:
im->data[y*im->w+x] = val+o;
break;
default:
im->data[y*im->w+x] = val;
}
}
/**
* @brief (internal use) upsample image by 2x using saa5050 algorithm
* @param im pointer to image
*/
void r1b_upsample2x_saa5050(r1b_im_t* im){
//wikipedia.org/wiki/Pixel-art_scaling_algorithms#SAA5050_'diagonal_smoothing'
r1b_make_tmp0(im->w*2,im->h*2);
int i; for (i= 0; i < im->h; i++) {
int _i = (i-1)*im->w;
int i_ = i*im->w;
int j; for (j= 0; j < im->w; j++) {
// readable version:
// int A = 0.5 < r1b_get(im,j-1,i-1,R1B_BRDR_ZERO);
// int B = 0.5 < r1b_get(im,j, i-1,R1B_BRDR_ZERO);
// int C = 0.5 < r1b_get(im,j+1,i-1,R1B_BRDR_ZERO);
// int D = 0.5 < r1b_get(im,j-1,i, R1B_BRDR_ZERO);
// int E = 0.5 < r1b_get(im,j, i, R1B_BRDR_ZERO);
// int F = 0.5 < r1b_get(im,j+1,i, R1B_BRDR_ZERO);
int _j = j-1;
int j_ = j+1;
int bi = i == 0;
int bj0 = j == 0;
int bj1 = j == im->w-1;
int A = 0.5<((bi||bj0)?0:im->data[_i + _j ]);
int B = 0.5<((bi) ?0:im->data[_i + j ]);
int C = 0.5<((bi||bj1)?0:im->data[_i + j_]);
int D = 0.5<(( bj0)?0:im->data[ i_+ _j ]);
int E = 0.5<( im->data[ i_+ j ]);
int F = 0.5<(( bj1)?0:im->data[ i_+ j_]);
r1b_tmp0[i*2 *(im->w*2)+j*2 ] = B | (A & E & !B & !D);
r1b_tmp0[i*2 *(im->w*2)+j*2+1] = B | (C & E & !B & !F);
r1b_tmp0[(i*2+1)*(im->w*2)+j*2 ] = E | (!A & !E & B & D);
r1b_tmp0[(i*2+1)*(im->w*2)+j*2+1] = E | (!C & !E & B & F);
}
}
// swap
r1b_tmp0_size = im->w*im->h;
float* tmp = im->data;
im->w *=2;
im->h *=2;
im->data = r1b_tmp0;
r1b_tmp0 = tmp;
}
/**
* @brief (internal use) upsample image by 2x using EPX algorithm
* @param im pointer to image
*/
void r1b_upsample2x_epx(r1b_im_t* im){
//wikipedia.org/wiki/Pixel-art_scaling_algorithms#EPX
r1b_make_tmp0(im->w*2,im->h*2);
float ep = R1B_CONFIG_UP2X_FLOAT_EPSILON;
int i; for (i= 0; i < im->h; i++) {
int j; for (j= 0; j < im->w; j++) {
float P = im->data[i*im->w+j];
float A = (i==0 )?P:im->data[(i-1)*im->w+j ];
float B = (j==im->w-1)?P:im->data[(i )*im->w+j+1];
float C = (j==0 )?P:im->data[(i )*im->w+j-1];
float D = (i==im->h-1)?P:im->data[(i+1)*im->w+j ];
int AeqC = fabs(C-A)<ep;
int AeqB = fabs(A-B)<ep;
int CeqD = fabs(D-C)<ep;
int BeqD = fabs(B-D)<ep;
r1b_tmp0[ i*2 *(im->w*2)+j*2 ] = (AeqC & (!CeqD) & (!AeqB)) ? A : P;
r1b_tmp0[ i*2 *(im->w*2)+j*2+1] = (AeqB & (!AeqC) & (!BeqD)) ? B : P;
r1b_tmp0[(i*2+1)*(im->w*2)+j*2 ] = (CeqD & (!BeqD) & (!AeqC)) ? C : P;
r1b_tmp0[(i*2+1)*(im->w*2)+j*2+1] = (BeqD & (!AeqB) & (!CeqD)) ? D : P;
}
}
// swap
r1b_tmp0_size = im->w*im->h;
float* tmp = im->data;
im->w *=2;
im->h *=2;
im->data = r1b_tmp0;
r1b_tmp0 = tmp;
}
/**
* @brief (internal use) upsample image by 2x using Eagle algorithm
* @param im pointer to image
*/
void r1b_upsample2x_eagle(r1b_im_t* im){
//wikipedia.org/wiki/Pixel-art_scaling_algorithms#Eagle
r1b_make_tmp0(im->w*2,im->h*2);
float ep = R1B_CONFIG_UP2X_FLOAT_EPSILON;
int i; for (i= 0; i < im->h; i++) {
int _i = (i-1)*im->w;