-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotmini.h
More file actions
3990 lines (3693 loc) · 165 KB
/
plotmini.h
File metadata and controls
3990 lines (3693 loc) · 165 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
/*
plotmini.h -- v0.1 -- public domain, single-header plotting library
Plot the data you have. Software-rasterised, zero dependencies beyond libc.
Output is always a raw pixel framebuffer -- attach any display backend you like.
====== USAGE ======
1. In *one* C file write:
#define PLOTMINI_IMPLEMENTATION
#include "plotmini.h"
2. Everywhere else just:
#include "plotmini.h"
====== CONFIGURATION MACROS ======
Define these *before* the include if you want to override defaults:
PLOTMINI_MALLOC / PLOTMINI_FREE
Custom allocator. Defaults to <stdlib.h> malloc / free.
PLOTMINI_ASSERT(cond)
Custom assertion. Defaults to <assert.h>.
PLOTMINI_NO_FONT
Strip the embedded bitmap font (~1.5 KB saved). Text functions
become no-ops.
PLOTMINI_GRAYSCALE_ONLY
Drop RGBA8 support. Halves the implementation size. The library
only handles 1-byte-per-pixel luminance framebuffers.
====== LICENSE ======
This is free and unencumbered software released into the public domain.
See the end of the file for the full Unlicense text.
*/
#ifndef PLOTMINI_H
#define PLOTMINI_H
#include <stddef.h> /* size_t */
#ifdef __cplusplus
extern "C" {
#endif
/* ------------------------------------------------------------------ */
/* TYPES */
/* ------------------------------------------------------------------ */
/* ---- pixel format ------------------------------------------------- */
typedef enum plm_pixelfmt {
PLM_GRAY8 = 1, /* 1 byte per pixel, luminance 0..255 */
PLM_RGBA8 = 4 /* 4 bytes per pixel, R G B A 0..255 */
} plm_pixelfmt;
/* ---- framebuffer -------------------------------------------------- */
typedef struct plm_fb {
unsigned char *pixels; /* raw pixel data, top-left origin */
int width;
int height;
int stride; /* bytes between rows (>= width * bpp) */
int bpp; /* bytes per pixel (1 = GRAY, 4 = RGBA) */
} plm_fb;
/* ---- colour ------------------------------------------------------- */
typedef struct plm_color {
unsigned char r, g, b, a;
} plm_color;
#define PLM_RGBA(rr,gg,bb,aa) ((plm_color){(rr),(gg),(bb),(aa)})
#define PLM_BLACK PLM_RGBA(0x00,0x00,0x00,0xFF)
#define PLM_WHITE PLM_RGBA(0xFF,0xFF,0xFF,0xFF)
#define PLM_RED PLM_RGBA(0xFF,0x00,0x00,0xFF)
#define PLM_GREEN PLM_RGBA(0x00,0xFF,0x00,0xFF)
#define PLM_BLUE PLM_RGBA(0x00,0x00,0xFF,0xFF)
#define PLM_YELLOW PLM_RGBA(0xFF,0xFF,0x00,0xFF)
#define PLM_CYAN PLM_RGBA(0x00,0xFF,0xFF,0xFF)
#define PLM_MAGENTA PLM_RGBA(0xFF,0x00,0xFF,0xFF)
#define PLM_GREY(x) PLM_RGBA(x,x,x,0xFF)
/* ---- theme colours (overridable via PLOTMINI_DARK_THEME) ---------- */
#ifdef PLOTMINI_DARK_THEME
#define PLM_BG_COLOR PLM_RGBA(0x1E,0x1E,0x1E,0xFF) /* dark bg */
#define PLM_FG_COLOR PLM_RGBA(0xCC,0xCC,0xCC,0xFF) /* light fg */
#define PLM_GRID_COLOR PLM_RGBA(0x3C,0x3C,0x3C,0xFF) /* subtle grid */
#else
#define PLM_BG_COLOR PLM_WHITE
#define PLM_FG_COLOR PLM_BLACK
#define PLM_GRID_COLOR PLM_GREY(220)
#endif
/* ---- 2d point (data space) ---------------------------------------- */
typedef struct plm_vec2 {
double x, y;
} plm_vec2;
/* ---- rectangle (pixel space, integer) ------------------------------ */
typedef struct plm_irect {
int x0, y0, x1, y1; /* (x0,y0) inclusive top-left,
(x1,y1) exclusive bottom-right */
} plm_irect;
/* ---- axis scale ---------------------------------------------------- */
typedef enum plm_scale {
PLM_LINEAR,
PLM_LOG,
PLM_CATEGORICAL
} plm_scale;
/* ---- tick label formatter callback (forward) ----------------------- */
typedef void (*plm_tick_formatter)(char *buf, int buf_size, double value);
/* ---- axis description ---------------------------------------------- */
typedef struct plm_axis {
plm_scale scale;
double min, max; /* data range; if min==max -> auto-range */
int tick_hint; /* desired number of major ticks (0 = auto) */
int grid; /* 0 = none, 1 = major grid lines,
2 = major+minor grid lines */
const char *label; /* axis label, may be NULL */
int label_font; /* reserved (font id) */
/* categorical axis (PLM_CATEGORICAL) */
const char **categories; /* NULL-terminated array of labels, or NULL */
int num_categories; /* number of category labels */
/* custom tick label formatting */
plm_tick_formatter tick_formatter; /* if non-NULL, called to format tick labels */
int font_scale; /* 0 = use PLOTMINI_TEXT_SCALE; >0 = override */
} plm_axis;
/* ---- step mode (for line plots) ----------------------------------- */
typedef enum plm_step_mode {
PLM_STEP_NONE = 0, /* normal straight-line connection */
PLM_STEP_PRE, /* step before: horizontal then vertical */
PLM_STEP_POST, /* step after: vertical then horizontal */
PLM_STEP_MID /* step mid: vertical-horizontal-vertical */
} plm_step_mode;
/* ---- line style ---------------------------------------------------- */
typedef struct plm_line_style {
plm_color color;
float width; /* 0.0..N pixel width (0 = hairline) */
const char *legend; /* optional legend label */
plm_step_mode step_mode; /* PLM_STEP_NONE = normal line */
unsigned short dash_pattern; /* 16-bit mask: 1=draw,0=skip; 0=solid */
} plm_line_style;
/* common dash patterns (16-bit, repeats every 16 px along the line) */
#define PLM_DASH_SOLID 0x0000 /* solid (default) */
#define PLM_DASH_DOTTED 0xAAAA /* 1 on, 1 off (dotted) */
#define PLM_DASH_DASHED 0xFF00 /* 8 on, 8 off (dashed) */
#define PLM_DASH_DASHDOT 0xE0E0 /* 3 on, 1 off, 3 on, 9 off */
#define PLM_DASH_LONGDASH 0xFFF0 /* 12 on, 4 off (long dash) */
#define PLM_DASH_FINEDOT 0x8888 /* 1 on, 3 off (fine dot) */
/* ---- line series --------------------------------------------------- */
typedef struct plm_line_series {
const float *x_data; /* internally-allocated copy (from add_line) */
const float *y_data;
int count;
plm_line_style style;
} plm_line_series;
/* ---- bar / histogram style ----------------------------------------- */
typedef struct plm_bar_style {
plm_color fill_color;
plm_color stroke_color;
float stroke_width; /* 0 = no stroke */
const char *legend;
} plm_bar_style;
/* ---- histogram series ---------------------------------------------- */
typedef struct plm_hist_series {
const float *data; /* raw samples */
int count;
int bins; /* number of bins (0 = auto) */
int density; /* 0 = counts, 1 = probability density */
plm_bar_style style;
} plm_hist_series;
/* ---- bar series (categorical or numeric x) ------------------------- */
typedef struct plm_bar_series {
const float *x_data; /* category indices or x positions */
const float *y_data; /* bar heights */
int count;
float width; /* bar width in data units (0 = auto) */
plm_bar_style style;
} plm_bar_series;
/* ---- error bar style ----------------------------------------------- */
typedef struct plm_errorbar_style {
plm_color line_color; /* color for error lines and connecting line */
float line_width; /* 0 = hairline */
float cap_size; /* cap width in pixels (0 = no caps) */
plm_color marker_color; /* marker fill color */
float marker_radius; /* 0 = no markers */
const char *legend;
} plm_errorbar_style;
/* ---- error bar series ---------------------------------------------- */
typedef struct plm_errorbar_series {
const float *x_data;
const float *y_data;
const float *x_err; /* symmetric x error, or NULL = no x error */
const float *y_err; /* symmetric y error, or NULL = no y error */
const float *x_err_low; /* asymmetric lower x error (NULL → use x_err) */
const float *x_err_high; /* asymmetric upper x error (NULL → use x_err) */
const float *y_err_low; /* asymmetric lower y error (NULL → use y_err) */
const float *y_err_high; /* asymmetric upper y error (NULL → use y_err) */
int count;
plm_errorbar_style style;
} plm_errorbar_series;
/* ---- band style ---------------------------------------------------- */
typedef struct plm_band_style {
plm_color fill_color;
plm_color stroke_color;
float stroke_width; /* 0 = no stroke */
const char *legend;
} plm_band_style;
/* ---- band series (fill between two curves) ------------------------- */
typedef struct plm_band_series {
const float *x_data;
const float *y_lower; /* lower bound */
const float *y_upper; /* upper bound */
int count;
plm_band_style style;
} plm_band_series;
/* ---- scatter series ------------------------------------------------ */
typedef struct plm_scatter_style {
plm_color color;
float radius; /* dot radius in pixels, default 2.0 */
const char *legend;
} plm_scatter_style;
typedef struct plm_scatter_series {
const float *x_data;
const float *y_data;
int count;
plm_scatter_style style;
} plm_scatter_series;
/* ---- stem style ---------------------------------------------------- */
typedef struct plm_stem_style {
plm_color line_color; /* color for the vertical stem line */
float line_width; /* 0 = hairline */
plm_color marker_color; /* marker fill color */
float marker_radius; /* 0 = no markers */
const char *legend;
} plm_stem_style;
/* ---- stem series --------------------------------------------------- */
typedef struct plm_stem_series {
const float *x_data;
const float *y_data;
int count;
double baseline; /* y-value of stem base (default = 0) */
plm_stem_style style;
} plm_stem_series;
/* ---- legend position ----------------------------------------------- */
typedef enum plm_legend_pos {
PLM_LEGEND_NONE = 0, /* no legend */
PLM_LEGEND_TOP_RIGHT, /* inside plot area, top-right corner */
PLM_LEGEND_TOP_LEFT, /* inside plot area, top-left corner */
PLM_LEGEND_BOTTOM_RIGHT, /* inside plot area, bottom-right corner */
PLM_LEGEND_BOTTOM_LEFT, /* inside plot area, bottom-left corner */
PLM_LEGEND_OUTSIDE_RIGHT /* outside plot area, to the right */
} plm_legend_pos;
/* ---- a complete plot description (hybrid retained model) ----------- */
typedef struct plm_plot {
/* axes */
plm_axis x_axis;
plm_axis y_axis;
plm_axis y_axis_right; /* secondary (right-side) y-axis; min==max disables it */
/* layout (pixels) -- margins can be set or auto-computed (-1) */
int margin_left;
int margin_top;
int margin_right;
int margin_bottom;
/* labels */
const char *title;
const char *x_label;
const char *y_label;
const char *y_label_right; /* label for the right y-axis */
/* legend */
plm_legend_pos legend_position; /* PLM_LEGEND_NONE = no legend */
/* series -- these are filled by plm_plot_add_*() */
plm_line_series *lines;
int line_count;
int line_cap; /* internal: allocated capacity */
plm_hist_series *hists;
int hist_count;
int hist_cap; /* internal */
plm_scatter_series *scatters;
int scatter_count;
int scatter_cap; /* internal */
plm_bar_series *bars;
int bar_count;
int bar_cap; /* internal */
plm_errorbar_series *errorbars;
int errorbar_count;
int errorbar_cap; /* internal */
plm_band_series *bands;
int band_count;
int band_cap; /* internal */
plm_stem_series *stems;
int stem_count;
int stem_cap; /* internal */
/* internal state (set during render, read-only for user) */
plm_irect plot_area; /* pixel rect of the data region */
int fb_w; /* framebuffer width at last render */
int fb_h; /* framebuffer height at last render */
} plm_plot;
/* Opaque 3D-plot handle. plotmini3d.h fills in the stride and
callbacks so plm_figure can dispatch without knowing the full type. */
typedef void (*plm_render_3d_fn)(void *p, plm_fb *fb,
int x0, int y0, int x1, int y1);
typedef void (*plm_reset_3d_fn)(void *p);
/* A grid of subplots. Each cell holds an independent plm_plot
(2D) or, when plotmini3d.h is included, a plm3d_plot (3D).
Create with plm_figure_init(), fill each cell via plm_figure_plot()
or plm_figure_plot_3d(), then render with plm_figure_render(). */
typedef struct plm_figure {
int nrows;
int ncols;
int hgap; /* horizontal gap between cells (pixels) */
int vgap; /* vertical gap between cells (pixels) */
const char *title; /* overall figure title, drawn at top */
plm_plot *plots; /* array[nrows*ncols], 2D plots, owned */
void *plots3d; /* array[nrows*ncols], 3D plots (NULL=off)*/
int plots3d_stride; /* sizeof(plm3d_plot), set by 3D module */
int *cell_types; /* 0 = 2D, 1 = 3D; NULL = all 2D */
plm_render_3d_fn render_3d; /* callback set by plotmini3d.h */
plm_reset_3d_fn reset_3d; /* callback set by plotmini3d.h */
} plm_figure;
/* ------------------------------------------------------------------ */
/* PUBLIC API (declarations) */
/* ------------------------------------------------------------------ */
/* ---- framebuffer -------------------------------------------------- */
/* Create a framebuffer. The memory is owned by the caller;
pass `pixels` already allocated or NULL to let the library allocate. */
plm_fb plm_fb_create(unsigned char *pixels, int w, int h, plm_pixelfmt fmt);
/* Free framebuffer internal storage (call when pixels==NULL was passed). */
void plm_fb_free(plm_fb *fb);
/* Fill entire framebuffer with a solid colour. */
void plm_fb_clear(plm_fb *fb, plm_color c);
/* Fill an integer rectangle. Clipped to fb bounds. */
void plm_fb_fill_rect(plm_fb *fb, plm_irect r, plm_color c);
/* Swap R<->B channels in-place (RGBA8 <-> BGRA8).
Useful before feeding the framebuffer to a BGRA-native display backend. */
void plm_fb_swizzle_rgba_bgra(plm_fb *fb);
/* ---- colormap ----------------------------------------------------- */
typedef enum plm_cmap {
PLM_CMAP_NONE = 0,
PLM_CMAP_HEAT,
PLM_CMAP_VIRIDIS,
PLM_CMAP_GRAY,
PLM_CMAP_PLASMA,
PLM_CMAP_INFERNO,
PLM_CMAP_MAGMA,
PLM_CMAP_TURBO,
PLM_CMAP_CIVIDIS,
PLM_CMAP_COOLWARM,
PLM_CMAP_JET
} plm_cmap;
plm_color plm_cmap_lookup(float t, plm_cmap cmap);
void plm_imshow(plm_fb *fb, plm_irect rect,
const float *data, int data_w, int data_h,
double vmin, double vmax, plm_cmap cmap);
/* ---- colorbar ----------------------------------------------------- */
typedef enum plm_cbar_orient {
PLM_CBAR_VERTICAL = 0, /* vertical bar (default), left→right gradient */
PLM_CBAR_HORIZONTAL = 1 /* horizontal bar, top→bottom gradient */
} plm_cbar_orient;
/* Draw a colorbar (gradient + tick labels + optional label).
rect: bounding box for the entire colorbar (including labels).
vmin/vmax: data range mapped to the gradient.
tick_hint: desired number of ticks (0 = auto ~5).
orient: PLM_CBAR_VERTICAL or PLM_CBAR_HORIZONTAL.
label: optional title placed adjacent to the bar. */
void plm_colorbar(plm_fb *fb, plm_irect rect,
double vmin, double vmax, plm_cmap cmap,
plm_cbar_orient orient, const char *label,
int tick_hint);
/* ---- plot lifecycle ------------------------------------------------ */
plm_irect plm_fit_into(plm_irect bounds, int inner_w, int inner_h);
/* Initialise a plot to defaults. */
void plm_plot_init(plm_plot *p);
/* Free all internally-allocated series arrays. plot can be reused. */
void plm_plot_reset(plm_plot *p);
/* ---- adding data series -------------------------------------------- */
/* Append a line series. Data is *copied* internally. */
void plm_plot_add_line(plm_plot *p,
const float *x, const float *y, int count,
plm_line_style style);
/* Append a histogram series from raw samples. */
void plm_plot_add_hist(plm_plot *p,
const float *samples, int count,
int bins, int density,
plm_bar_style style);
/* Append a scatter series. Data is *copied* internally. */
void plm_plot_add_scatter(plm_plot *p,
const float *x, const float *y, int count,
plm_scatter_style style);
/* Append a bar series. Data is *copied* internally.
For categorical axes, x_data should be category indices (0, 1, ...).
width is in data units; 0 = auto (0.8 for categorical). */
void plm_plot_add_bar(plm_plot *p,
const float *x, const float *y, int count,
float width, plm_bar_style style);
/* Append an error bar series. Data is *copied* internally.
x_err and y_err may be NULL (no error in that direction).
If cap_size > 0, caps are drawn at the end of error bars.
If marker_radius > 0, markers are drawn at each data point. */
void plm_plot_add_errorbar(plm_plot *p,
const float *x, const float *y,
const float *x_err, const float *y_err,
int count,
plm_errorbar_style style);
/* Append a band (filled region) between two curves.
Data is *copied* internally. y_lower and y_upper define the
lower and upper boundaries of the filled region. */
void plm_plot_add_band(plm_plot *p,
const float *x, const float *y_lower,
const float *y_upper,
int count,
plm_band_style style);
/* Append an error bar series with asymmetric errors.
Data is *copied* internally.
Pass NULL for any of x_err_low/x_err_high to skip x error bars;
pass NULL for any of y_err_low/y_err_high to skip y error bars.
To get symmetric errors in a direction, pass the same array for
low and high. */
void plm_plot_add_errorbar_asym(plm_plot *p,
const float *x, const float *y,
const float *x_err_low, const float *x_err_high,
const float *y_err_low, const float *y_err_high,
int count,
plm_errorbar_style style);
/* Append a stem series. Data is *copied* internally.
Stems are vertical lines from a baseline (default 0) to each
(x, y) data point, with an optional marker at the top. */
void plm_plot_add_stem(plm_plot *p,
const float *x, const float *y, int count,
double baseline, plm_stem_style style);
/* ---- rendering ----------------------------------------------------- */
/* Save framebuffer as 24-bit BMP. Returns 0 on success, non-zero on error. */
int plm_fb_save_bmp(const plm_fb *fb, const char *path);
/* Render the current plot description into a framebuffer.
Layout (margins, tick labels) is recomputed every call, so you can
call this repeatedly with different fb sizes or updated data. */
void plm_render(const plm_plot *p, plm_fb *fb);
/* ---- built-in tick formatters -------------------------------------- */
/* Default: "%.4g" */
void plm_tick_fmt_default(char *buf, int sz, double v);
/* Scientific: "%.2e" */
void plm_tick_fmt_sci(char *buf, int sz, double v);
/* Integer: "%.0f" */
void plm_tick_fmt_int(char *buf, int sz, double v);
/* Percentage: "%.0f%%" (multiplies v by 100) */
void plm_tick_fmt_pct(char *buf, int sz, double v);
/* Log10: "10^n" for exact powers of 10, else "%.4g" */
void plm_tick_fmt_log10(char *buf, int sz, double v);
/* Currency: "$%.2f" */
void plm_tick_fmt_usd(char *buf, int sz, double v);
/* ---- subplot / figure ---------------------------------------------- */
/* Initialise a figure with `nrows` x `ncols` grid of subplots.
All plots are pre-allocated; access them with plm_figure_plot().
Default gaps: 20 px horizontal, 20 px vertical. */
void plm_figure_init(plm_figure *fig, int nrows, int ncols);
/* Return a pointer to the plot at (row, col). Both 0-based.
Fill the returned plm_plot with series, labels, styles as usual. */
plm_plot *plm_figure_plot(plm_figure *fig, int row, int col);
/* Switch cell to 3D mode and return an opaque 3D plot handle.
Requires plotmini3d.h to be included. Cast the return to
plm3d_plot*. Lazy-allocates 3D storage on first call. */
void *plm_figure_plot_3d(plm_figure *fig, int row, int col);
/* Render all subplots (2D and 3D) into one framebuffer.
Clears fb once, then renders each cell. Margins stay within cells. */
void plm_figure_render(const plm_figure *fig, plm_fb *fb);
/* Free all internal storage; figure can be re-init'd or discarded. */
void plm_figure_reset(plm_figure *fig);
/* ---- text helpers (optional, only if PLOTMINI_NO_FONT is not set) -- */
/* Returns pixel advance after the string. Height is fixed per font. */
int plm_text_width(const char *s);
int plm_text_height(void);
/* Draw a null-terminated string at pixel (x, baseline_y). */
void plm_draw_text(plm_fb *fb, int x, int y, const char *s, plm_color c);
#ifdef __cplusplus
}
#endif
#endif /* PLOTMINI_H */
/* ================================================================== */
/* IMPLEMENTATION */
/* ================================================================== */
#ifdef PLOTMINI_IMPLEMENTATION
/* ---- standard library includes ------------------------------------- */
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <stdio.h>
/* ---- allocator overrides ------------------------------------------- */
#ifndef PLOTMINI_MALLOC
#define PLOTMINI_MALLOC(sz) malloc(sz)
#endif
#ifndef PLOTMINI_FREE
#define PLOTMINI_FREE(p) free(p)
#endif
#ifndef PLOTMINI_ASSERT
#include <assert.h>
#define PLOTMINI_ASSERT(cond) assert(cond)
#endif
/* ---- internal math helpers ----------------------------------------- */
static int plm__isnan(float x) {
#ifdef _MSC_VER
return _isnan(x);
#else
return isnan(x); /* C99; most compilers have it as a macro */
#endif
}
#define plm__clamp(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x))
#define plm__max(a, b) ((a) > (b) ? (a) : (b))
#define plm__min(a, b) ((a) < (b) ? (a) : (b))
/* ---- pixel ops ----------------------------------------------------- */
static void plm__set_pixel(plm_fb *fb, int x, int y, plm_color c) {
if (x < 0 || x >= fb->width || y < 0 || y >= fb->height) return;
if (fb->bpp == 1) {
/* luminance: take perceptual average of R G B */
fb->pixels[y * fb->stride + x] =
(unsigned char)((unsigned int)(c.r * 77 + c.g * 150 + c.b * 29) >> 8);
} else {
unsigned char *p = fb->pixels + y * fb->stride + x * 4;
p[0] = c.r; p[1] = c.g; p[2] = c.b; p[3] = c.a;
}
}
static void plm__blend_pixel(plm_fb *fb, int x, int y, plm_color c, unsigned char alpha) {
if (x < 0 || x >= fb->width || y < 0 || y >= fb->height) return;
if (alpha == 0) return;
if (alpha == 255) { plm__set_pixel(fb, x, y, c); return; }
if (fb->bpp == 1) {
unsigned char *dst = fb->pixels + y * fb->stride + x;
unsigned char lum = (unsigned char)((unsigned int)(c.r * 77 + c.g * 150 + c.b * 29) >> 8);
unsigned int inv_a = 255 - alpha;
*dst = (unsigned char)(((unsigned int)lum * alpha + (unsigned int)*dst * inv_a) >> 8);
} else {
unsigned char *dst = fb->pixels + y * fb->stride + x * 4;
unsigned int inv_a = 255 - alpha;
dst[0] = (unsigned char)(((unsigned int)c.r * alpha + (unsigned int)dst[0] * inv_a) >> 8);
dst[1] = (unsigned char)(((unsigned int)c.g * alpha + (unsigned int)dst[1] * inv_a) >> 8);
dst[2] = (unsigned char)(((unsigned int)c.b * alpha + (unsigned int)dst[2] * inv_a) >> 8);
/* leave dst[3] (alpha) unchanged -- premultiplied blending */
}
}
/* Draw a filled, anti-aliased circle. Used for scatter dots.
Uses smoothstep falloff over 1.5 px for soft edges. */
static void plm__fill_circle(plm_fb *fb, int cx, int cy, float radius, plm_color color) {
int r = (int)(radius + 1.5f); /* extra pixels for the fade band */
if (r < 1) r = 1;
int y;
float r2 = radius * radius;
float fade = 1.5f; /* width of the fade band */
for (y = -r; y <= r; y++) {
float wy = (float)(y * y);
if (wy >= (radius + fade) * (radius + fade)) continue;
float half_wf = sqrtf(r2 - wy);
if (wy >= r2) half_wf = 0.0f; /* purely in fade zone */
int x0 = (int)((float)cx - half_wf - fade);
int x1 = (int)((float)cx + half_wf + fade + 0.9999f);
int x;
for (x = x0; x <= x1; x++) {
float dx = (float)(x - cx);
float dy = (float)y;
float dist = sqrtf(dx * dx + dy * dy);
if (dist <= radius - 0.5f) {
plm__set_pixel(fb, x, cy + y, color);
} else if (dist < radius + fade) {
/* smoothstep: t = 1 - (dist - (r-0.5)) / (fade+0.5) */
float t = (radius + fade - dist) / (fade + 0.5f);
if (t < 0.0f) t = 0.0f;
if (t > 1.0f) t = 1.0f;
/* smoothstep: t*t*(3 - 2*t) */
t = t * t * (3.0f - 2.0f * t);
unsigned char alpha = (unsigned char)(t * 255.0f);
plm__blend_pixel(fb, x, cy + y, color, alpha);
}
}
}
}
/* ---- framebuffer ops ----------------------------------------------- */
plm_fb plm_fb_create(unsigned char *pixels, int w, int h, plm_pixelfmt fmt) {
plm_fb fb;
fb.width = w;
fb.height = h;
fb.bpp = (int)fmt;
fb.stride = w * fb.bpp;
if (pixels) {
fb.pixels = pixels;
} else {
fb.pixels = (unsigned char *)PLOTMINI_MALLOC((size_t)(fb.stride * h));
}
return fb;
}
void plm_fb_free(plm_fb *fb) {
if (fb && fb->pixels) {
PLOTMINI_FREE(fb->pixels);
fb->pixels = NULL;
}
}
void plm_fb_clear(plm_fb *fb, plm_color c) {
int y;
if (fb->bpp == 4) {
/* 4-byte fill -- use memset for speed */
unsigned char row[4] = { c.r, c.g, c.b, c.a };
for (y = 0; y < fb->height; y++) {
unsigned char *dst = fb->pixels + y * fb->stride;
int x;
for (x = 0; x < fb->width; x++) {
dst[0] = row[0]; dst[1] = row[1];
dst[2] = row[2]; dst[3] = row[3];
dst += 4;
}
}
} else {
unsigned char lum = (unsigned char)((unsigned int)(c.r * 77 + c.g * 150 + c.b * 29) >> 8);
for (y = 0; y < fb->height; y++) {
memset(fb->pixels + y * fb->stride, lum, (size_t)fb->width);
}
}
}
void plm_fb_fill_rect(plm_fb *fb, plm_irect r, plm_color c) {
/* clip to framebuffer */
if (r.x0 < 0) r.x0 = 0;
if (r.y0 < 0) r.y0 = 0;
if (r.x1 > fb->width) r.x1 = fb->width;
if (r.y1 > fb->height) r.y1 = fb->height;
if (r.x0 >= r.x1 || r.y0 >= r.y1) return;
if (fb->bpp == 4) {
unsigned char row[4] = { c.r, c.g, c.b, c.a };
int x, y;
for (y = r.y0; y < r.y1; y++) {
unsigned char *dst = fb->pixels + y * fb->stride + r.x0 * 4;
for (x = r.x0; x < r.x1; x++) {
dst[0] = row[0]; dst[1] = row[1];
dst[2] = row[2]; dst[3] = row[3];
dst += 4;
}
}
} else {
unsigned char lum = (unsigned char)((unsigned int)(c.r * 77 + c.g * 150 + c.b * 29) >> 8);
int y;
for (y = r.y0; y < r.y1; y++) {
memset(fb->pixels + y * fb->stride + r.x0, lum,
(size_t)(r.x1 - r.x0));
}
}
}
void plm_fb_swizzle_rgba_bgra(plm_fb *fb) {
if (fb->bpp != 4) return;
int i;
for (i = 0; i < fb->width * fb->height; i++) {
unsigned char *p = fb->pixels + i * 4;
unsigned char r = p[0];
p[0] = p[2];
p[2] = r;
}
}
int plm_fb_save_bmp(const plm_fb *fb, const char *path) {
if (!fb || !fb->pixels || fb->width <= 0 || fb->height <= 0) return 1;
FILE *f = fopen(path, "wb");
if (!f) return 1;
int w = fb->width, h = fb->height, row_size = (w * 3 + 3) & ~3;
unsigned char hdr[54] = {0};
hdr[0] = 'B'; hdr[1] = 'M';
int fsize = 54 + row_size * h;
hdr[2] = (unsigned char)fsize; hdr[3] = (unsigned char)(fsize>>8);
hdr[4] = (unsigned char)(fsize>>16); hdr[5] = (unsigned char)(fsize>>24);
hdr[10] = 54;
hdr[14] = 40;
hdr[18] = (unsigned char)w; hdr[19] = (unsigned char)(w>>8);
hdr[22] = (unsigned char)h; hdr[23] = (unsigned char)(h>>8);
hdr[26] = 1;
hdr[28] = 24;
fwrite(hdr, 54, 1, f);
unsigned char *row = (unsigned char *)PLOTMINI_MALLOC((size_t)row_size);
if (!row) { fclose(f); return 1; }
int y;
for (y = h - 1; y >= 0; y--) {
const unsigned char *src = fb->pixels + y * fb->stride;
int x;
if (fb->bpp == 4) {
for (x = 0; x < w; x++) {
row[x*3+0] = src[x*4+2];
row[x*3+1] = src[x*4+1];
row[x*3+2] = src[x*4+0];
}
} else {
for (x = 0; x < w; x++) {
row[x*3+0] = row[x*3+1] = row[x*3+2] = src[x];
}
}
fwrite(row, (size_t)row_size, 1, f);
}
PLOTMINI_FREE(row);
fclose(f);
return 0;
}
/* ---- colormap helpers ---------------------------------------------- */
/* ---- generic piecewise-linear colormap helper ---------------------- */
/* ctrl is [t0,r0,g0,b0, t1,r1,g1,b1, ...] * npts points.
Values clamped to [0,1] before lookup. */
static plm_color plm__cmap_lerp(float t, const float *ctrl, int npts) {
if (t <= ctrl[0]) {
return PLM_RGBA((unsigned char)(ctrl[1]*255.0f),
(unsigned char)(ctrl[2]*255.0f),
(unsigned char)(ctrl[3]*255.0f), 0xFF);
}
int i;
for (i = 1; i < npts; i++) {
if (t <= ctrl[i*4]) {
float s = (t - ctrl[(i-1)*4]) / (ctrl[i*4] - ctrl[(i-1)*4]);
float r = ctrl[(i-1)*4+1] + s * (ctrl[i*4+1] - ctrl[(i-1)*4+1]);
float g = ctrl[(i-1)*4+2] + s * (ctrl[i*4+2] - ctrl[(i-1)*4+2]);
float b = ctrl[(i-1)*4+3] + s * (ctrl[i*4+3] - ctrl[(i-1)*4+3]);
return PLM_RGBA((unsigned char)(r*255.0f),
(unsigned char)(g*255.0f),
(unsigned char)(b*255.0f), 0xFF);
}
}
/* past last point */
return PLM_RGBA((unsigned char)(ctrl[(npts-1)*4+1]*255.0f),
(unsigned char)(ctrl[(npts-1)*4+2]*255.0f),
(unsigned char)(ctrl[(npts-1)*4+3]*255.0f), 0xFF);
}
/* ---- individual colormaps (control points from matplotlib) --------- */
static plm_color plm__cmap_heat(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.00f, 0.00f, 0.00f, 0.00f,
0.25f, 0.25f, 0.00f, 0.40f,
0.50f, 0.00f, 0.25f, 1.00f,
0.75f, 0.25f, 1.00f, 0.75f,
1.00f, 1.00f, 1.00f, 0.25f };
return plm__cmap_lerp(t, c, 5);
}
static plm_color plm__cmap_viridis(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.000f, 0.267f, 0.004f, 0.329f,
0.125f, 0.282f, 0.141f, 0.458f,
0.250f, 0.253f, 0.266f, 0.530f,
0.375f, 0.206f, 0.372f, 0.554f,
0.500f, 0.164f, 0.471f, 0.558f,
0.625f, 0.128f, 0.567f, 0.550f,
0.750f, 0.134f, 0.659f, 0.514f,
0.875f, 0.281f, 0.748f, 0.408f,
1.000f, 0.477f, 0.821f, 0.318f };
return plm__cmap_lerp(t, c, 9);
}
static plm_color plm__cmap_gray(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
unsigned char v = (unsigned char)(t * 255.0f);
return PLM_RGBA(v, v, v, 0xFF);
}
static plm_color plm__cmap_plasma(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.000f, 0.050f, 0.030f, 0.528f,
0.125f, 0.249f, 0.004f, 0.645f,
0.250f, 0.497f, 0.036f, 0.661f,
0.375f, 0.704f, 0.104f, 0.562f,
0.500f, 0.861f, 0.244f, 0.355f,
0.625f, 0.950f, 0.415f, 0.153f,
0.750f, 0.979f, 0.602f, 0.037f,
0.875f, 0.969f, 0.799f, 0.045f,
1.000f, 0.940f, 0.975f, 0.131f };
return plm__cmap_lerp(t, c, 9);
}
static plm_color plm__cmap_inferno(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.000f, 0.001f, 0.000f, 0.014f,
0.125f, 0.170f, 0.030f, 0.280f,
0.250f, 0.389f, 0.113f, 0.436f,
0.375f, 0.611f, 0.192f, 0.375f,
0.500f, 0.792f, 0.295f, 0.218f,
0.625f, 0.922f, 0.424f, 0.074f,
0.750f, 0.981f, 0.569f, 0.029f,
0.875f, 0.996f, 0.740f, 0.110f,
1.000f, 0.988f, 0.998f, 0.645f };
return plm__cmap_lerp(t, c, 9);
}
static plm_color plm__cmap_magma(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.000f, 0.001f, 0.000f, 0.014f,
0.125f, 0.161f, 0.031f, 0.365f,
0.250f, 0.361f, 0.095f, 0.493f,
0.375f, 0.568f, 0.161f, 0.449f,
0.500f, 0.752f, 0.252f, 0.287f,
0.625f, 0.895f, 0.380f, 0.100f,
0.750f, 0.975f, 0.528f, 0.137f,
0.875f, 0.994f, 0.745f, 0.370f,
1.000f, 0.988f, 0.991f, 0.750f };
return plm__cmap_lerp(t, c, 9);
}
static plm_color plm__cmap_turbo(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.000f, 0.190f, 0.072f, 0.232f,
0.067f, 0.322f, 0.168f, 0.542f,
0.133f, 0.291f, 0.355f, 0.749f,
0.200f, 0.135f, 0.524f, 0.798f,
0.267f, 0.006f, 0.635f, 0.706f,
0.333f, 0.030f, 0.690f, 0.538f,
0.400f, 0.201f, 0.716f, 0.348f,
0.467f, 0.447f, 0.723f, 0.172f,
0.533f, 0.664f, 0.695f, 0.066f,
0.600f, 0.820f, 0.623f, 0.058f,
0.667f, 0.920f, 0.513f, 0.147f,
0.733f, 0.966f, 0.386f, 0.250f,
0.800f, 0.962f, 0.248f, 0.322f,
0.867f, 0.904f, 0.116f, 0.319f,
0.933f, 0.800f, 0.016f, 0.223f,
1.000f, 0.659f, 0.000f, 0.100f };
return plm__cmap_lerp(t, c, 16);
}
static plm_color plm__cmap_cividis(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.000f, 0.000f, 0.126f, 0.302f,
0.125f, 0.169f, 0.310f, 0.436f,
0.250f, 0.312f, 0.443f, 0.446f,
0.375f, 0.448f, 0.553f, 0.397f,
0.500f, 0.589f, 0.653f, 0.319f,
0.625f, 0.735f, 0.743f, 0.227f,
0.750f, 0.871f, 0.826f, 0.153f,
0.875f, 0.949f, 0.894f, 0.168f,
1.000f, 0.995f, 0.949f, 0.291f };
return plm__cmap_lerp(t, c, 9);
}
static plm_color plm__cmap_coolwarm(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.000f, 0.230f, 0.299f, 0.754f,
0.200f, 0.553f, 0.692f, 0.920f,
0.400f, 0.820f, 0.860f, 0.876f,
0.500f, 0.866f, 0.866f, 0.866f,
0.600f, 0.876f, 0.820f, 0.765f,
0.800f, 0.920f, 0.553f, 0.519f,
1.000f, 0.706f, 0.016f, 0.150f };
return plm__cmap_lerp(t, c, 7);
}
static plm_color plm__cmap_jet(float t) {
if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f;
static const float c[] = {
0.000f, 0.000f, 0.000f, 0.515f,
0.125f, 0.000f, 0.000f, 1.000f,
0.250f, 0.000f, 0.500f, 1.000f,
0.375f, 0.000f, 1.000f, 1.000f,
0.500f, 0.500f, 1.000f, 0.500f,
0.625f, 1.000f, 1.000f, 0.000f,
0.750f, 1.000f, 0.500f, 0.000f,
0.875f, 1.000f, 0.000f, 0.000f,
1.000f, 0.500f, 0.000f, 0.000f };
return plm__cmap_lerp(t, c, 9);
}
plm_color plm_cmap_lookup(float t, plm_cmap cmap) {
if (t < 0.0f) t = 0.0f;
if (t > 1.0f) t = 1.0f;
switch (cmap) {
default:
case PLM_CMAP_HEAT: return plm__cmap_heat(t);
case PLM_CMAP_VIRIDIS: return plm__cmap_viridis(t);
case PLM_CMAP_GRAY: return plm__cmap_gray(t);
case PLM_CMAP_PLASMA: return plm__cmap_plasma(t);
case PLM_CMAP_INFERNO: return plm__cmap_inferno(t);
case PLM_CMAP_MAGMA: return plm__cmap_magma(t);
case PLM_CMAP_TURBO: return plm__cmap_turbo(t);
case PLM_CMAP_CIVIDIS: return plm__cmap_cividis(t);
case PLM_CMAP_COOLWARM: return plm__cmap_coolwarm(t);
case PLM_CMAP_JET: return plm__cmap_jet(t);
case PLM_CMAP_NONE: return PLM_RGBA(128, 128, 128, 255);
}
}
void plm_imshow(plm_fb *fb, plm_irect rect,
const float *data, int data_w, int data_h,
double vmin, double vmax, plm_cmap cmap) {
if (!data || data_w <= 0 || data_h <= 0) return;
if (fb->bpp != 4 && fb->bpp != 1) return;
if (rect.x0 < 0) rect.x0 = 0;
if (rect.y0 < 0) rect.y0 = 0;
if (rect.x1 > fb->width) rect.x1 = fb->width;
if (rect.y1 > fb->height) rect.y1 = fb->height;
if (rect.x0 >= rect.x1 || rect.y0 >= rect.y1) return;
int rw = rect.x1 - rect.x0;
int rh = rect.y1 - rect.y0;
if (vmin == vmax) {
double lo = DBL_MAX, hi = -DBL_MAX;
int i;
for (i = 0; i < data_w * data_h; i++) {
double v = (double)data[i];
if (!plm__isnan((float)v)) {
if (v < lo) lo = v;
if (v > hi) hi = v;