-
Notifications
You must be signed in to change notification settings - Fork 12
/
bmp.h
1692 lines (1525 loc) · 54.3 KB
/
bmp.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
/**
* Bitmap API (bmp.h/bmp.c)
* ========================
* ![toc-]
*
* Low-level routines to manipulate bitmap graphic objects in memory and files on disk.
*
* Official repository: <https://github.com/wernsey/bitmap>
*
* * It supports BMP, GIF, PCX and TGA files without any third party dependencies.
* * PNG support is optional through [libpng][]. Use `-DUSEPNG` when compiling.
* * JPG support is optional through [libjpeg][]. Use `-DUSEJPG` when compiling.
* * Alternatively, JPG and PNG files can be loaded through [stb_image.h][stb_image].
* Put `stb_image.h` in the same directory as `bmp.c` and compile with `-DUSESTB`.
*
* [libpng]: http://www.libpng.org/pub/png/libpng.html
* [libjpeg]: http://www.ijg.org/
* [stb_image]: https://github.com/nothings/stb/blob/master/stb_image.h
*
* License
* -------
*
* ```
* MIT No Attribution
*
* Copyright (c) 2017 Werner Stoop <wstoop@gmail.com>
*
* 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.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ```
*
* API
* ---
*/
#ifndef BMP_H
#define BMP_H
#if defined(__cplusplus)
extern "C" {
#endif
/* See `bm_get_error()` for an explaination */
#ifndef BM_LAST_ERROR
# define BM_LAST_ERROR 1
#endif
/**
* ### Types
*/
typedef unsigned int bm_color_t;
/**
* #### `typedef struct bitmap Bitmap;`
*
* Structure containing a bitmap image.
*
* - Use `bm_width()` and `bm_height()` to get the dimensions of a bitmap
* - Use `bm_set_color()` and `bm_get_color()` to access the color used when drawing
* - Use `bm_clip()`, `bm_get_clip()` and `bm_set_clip()` to manipulate the clipping
* rectangle used when drawing
* - Use `bm_set_font()` and `bm_get_font()` to access the font used with `bm_puts()`
* and related routines
* - Use `bm_raw_data()` to access the raw pixel data in the bitmap.
*/
typedef struct bitmap Bitmap;
/**
* #### `typedef struct BmPoint BmPoint;`
* A point, with `x` and `y` coordinates
*/
typedef struct BmPoint {
int x, y;
} BmPoint;
/**
* #### `typedef struct BmRect BmRect;`
* Rectangle structure.
* `(x0,y0)` is inclusive.
* `(x1,y1)` is exclusive.
*/
typedef struct BmRect {
int x0, y0;
int x1, y1;
} BmRect;
/**
* #### `typedef struct bitmap_font BmFont;`
*
* Structure that represents the details about a font.
*
* See the section on [Font Routines](#font-routines) for more details.
*
* It has these members:
*
* * `const char *type` - a text description of the type of font
* * `ref_count` - a reference count for the font - This should be set
* to `1` when the font is created.
* * `int (*puts)(Bitmap *b, int x, int y, const char *text)` -
* Pointer to the function that will actually render the text.
* * `int (*width)(struct bitmap_font *font)` - Function that returns the
* width (in pixels) of a single character in the font.
* * `int (*height)(struct bitmap_font *font)` - Function that returns the
* height (in pixels) of a single character in the font.
* * `void (*dtor)(struct bitmap_font *font)` - Destructor function that
* deallocates all memory allocated to the `BmFont` object.
* * `void *data` - Additional data that may be required by the font.
*/
typedef struct bitmap_font {
const char *type;
unsigned int ref_count;
int (*puts)(Bitmap *b, int x, int y, const char *text);
int (*width)(struct bitmap_font *font, unsigned int codepoint);
int (*height)(struct bitmap_font *font, unsigned int codepoint);
void (*measure)(struct bitmap_font *font, const char *text, int *w, int* h, int* dx, int* dy);
void (*dtor)(struct bitmap_font *font);
void *data;
} BmFont;
/**
* #### `typedef struct bitmap_palette BmPalette;`
*
* Structure that contains a palette.
*
* See the section on [Palette Functions](#palette-functions) for more details.
*/
typedef struct bitmap_palette BmPalette;
/**
* ### Creating and Destroying bitmaps
*/
/**
* #### `Bitmap *bm_create(int w, int h)`
*
* Creates a bitmap of the specified dimensions `w` × `h`.
*/
Bitmap *bm_create(int w, int h);
/**
* #### `void bm_free(Bitmap *b)`
*
* Destroys a bitmap previously created with `bm_create()`
*/
void bm_free(Bitmap *b);
/**
* #### `Bitmap *bm_copy(Bitmap *b)`
*
* Creates a duplicate of the bitmap `b`.
*/
Bitmap *bm_copy(Bitmap *b);
/**
* #### `Bitmap *bm_crop(Bitmap *b, int x, int y, int w, int h)`
*
* Crops the bitmap `b` to the region defined by `{x,y,w,h}`
*/
Bitmap *bm_crop(Bitmap *b, int x, int y, int w, int h);
/**
* #### `Bitmap *bm_from_Xbm(int w, int h, unsigned char *data)`
*
* Creates a `Bitmap` object from [XBM data][XBM].
*
* The XBM image is imported into a program through a `#include "include.xbm"` directive.
*
* The width `w` and height `h` are the `_width` and `_height` variables at the top of the XBM file.
* The `data` parameter is the `_bits` variable in the XBM file.
*
* [XBM]: https://en.wikipedia.org/wiki/X_BitMap
*/
Bitmap *bm_from_Xbm(int w, int h, unsigned char *data);
/**
* #### `Bitmap *bm_from_Xpm(const char *xpm[])`
*
* Creates a `Bitmap` object from [X PixMap](https://en.wikipedia.org/wiki/X_PixMap)
* data in a source file.
*/
Bitmap *bm_from_Xpm(const char *xpm[]);
/**
* #### `int bm_width(Bitmap *b)`
*
* Retrieves the width of the bitmap `b`
*/
int bm_width(Bitmap *b);
/**
* #### `int bm_height(Bitmap *b)`
*
* Retrieves the height of the bitmap `b`
*/
int bm_height(Bitmap *b);
/**
* #### `unsigned char *bm_raw_data(Bitmap *b)`
*
* Retrieves the raw pixels in the bitmap `b`.
*
* The internal format is `0xAARRGGBB` little endian.
* Meaning that `p[0]` contains B, `p[1]` contains G,
* `p[2]` contains R and `p[3]` contains A
* and the data buffer is an array of bytes BGRABGRABGRABGRABGRA...
*/
unsigned char *bm_raw_data(Bitmap *b);
/**
* #### `int bm_pixel_count(Bitmap *b)`
*
* Returns the number of pixels in the bitmap `b`.
*
* Essentially `bm_width(b) * bm_height(b)`
*/
int bm_pixel_count(Bitmap *b);
/**
* ### File I/O Functions
* These functions are for reading graphics files into a `Bitmap` structure,
* or for writing a `Bitmap` structure to a file.
*/
/**
* #### `Bitmap *bm_load(const char *filename)`
*
* Loads a bitmap file `filename` into a bitmap structure.
*
* It tries to detect the file type from the first bytes in the file.
*
* BMP, GIF, PCX and TGA support is always enabled, while JPG and PNG support
* depends on how the library was compiled.
*
* Returns `NULL` if the file could not be loaded.
*/
Bitmap *bm_load(const char *filename);
/**
* #### `int bm_loadf(const char *fmt, ...)`
*
* Like `bm_load()`, but the filename is given as a `printf()`-style format string.
*/
Bitmap *bm_loadf(const char *fmt, ...);
#ifdef EOF /* <stdio.h> included? http://stackoverflow.com/q/29117606/115589 */
/**
* #### `Bitmap *bm_load_fp(FILE *f)`
*
* Loads a bitmap from a `FILE*` that's already open.
*
* BMP, GIF and PCX support is always enabled, while JPG and PNG support
* depends on how the library was compiled.
*
* Returns `NULL` if the file could not be loaded.
*/
Bitmap *bm_load_fp(FILE *f);
#endif
/**
* #### `Bitmap *bm_load_mem(const unsigned char *buffer, long len)`
*
* Loads a bitmap file from an array of bytes `buffer` of size `len`.
*
* It tries to detect the file type from the first bytes in the file.
*
* Only supports BMP, GIF, PCX and TGA at the moment.
* _Don't use it with untrusted input._
*
* Returns `NULL` if the file could not be loaded.
*/
Bitmap *bm_load_mem(const unsigned char *buffer, long len);
/**
* #### `Bitmap *bm_load_base64(const char *base64)`
*
* Loads a bitmap file from a [Base64][] encoded string. It uses
* `bm_load_mem()` internally, so the same caveats apply.
*
* Returns `NULL` if the bitmap could not be loaded.
*
* [Base64]: https://en.wikipedia.org/wiki/Base64
*/
Bitmap *bm_load_base64(const char *base64);
#if defined(USESDL) && defined(SDL_h_)
/**
* #### `Bitmap *bm_load_rw(SDL_RWops *file)`
*
* Loads a bitmap from a SDL `SDL_RWops*` structure,
* for use with the [SDL library](http://www.libsdl.org).
*
* BMP, GIF and PCX support is always enabled, while JPG and PNG support
* depends on how the library was compiled.
*
* Returns `NULL` if the file could not be loaded.
*
* **Note** This function is only available if the `USESDL` preprocessor macro
* is defined, and `SDL.h` is included before `bmp.h`.
*/
Bitmap *bm_load_rw(SDL_RWops *file);
#endif
#ifdef USESTB
/** #### `Bitmap *bm_load_stb(const char *filename)`
*
* Loads a `Bitmap` through the Sean Barrett's [stb_image][]
* image loader library (currently at v2.16).
*
* [stb_image][] provides support for loading JPEG and PNG files without
* relying on `libjpeg` and `libpng`. It also provides support for other
* file formats, such as PSD.
*
* To use this function, `stb_image.h` must be in the same directory
* as `bmp.c` and `-D USESTB` must be addeed to your compiler flags.
*/
Bitmap *bm_load_stb(const char *filename);
/** #### `Bitmap *bm_from_stb(int w, int h, unsigned char *data)`
*
* Creates a `Bitmap` object from the data returned by one of the
* `stbi_load*` functions of [stb_image][].
*
* The `desired_channels` parameter of the `stbi_load*` function
* _must_ be set to 4 for this function to work correctly.
*
*/
Bitmap *bm_from_stb(int w, int h, unsigned char *data);
#endif /* USESTB */
/**
* #### `int bm_save(Bitmap *b, const char *fname)`
*
* Saves the bitmap `b` to a bitmap file named `fname`.
*
* If the filename contains `".png"`, `".gif"`, `".pcx"`, `".tga"` or `".jpg"` the file is
* saved as a PNG, GIF, PCX or JPG, respectively, otherwise the BMP format is the default.
* It can only save to JPG or PNG if JPG or PNG support is enabled
* at compile time, otherwise it saves to a BMP file.
*
* Returns 1 on success, 0 on failure.
*/
int bm_save(Bitmap *b, const char *fname);
/**
* #### `int bm_savef(Bitmap *b, const char *fname, ...)`
*
* Like `bm_save()`, but the filename is given as a `printf()`-style format string.
*/
int bm_savef(Bitmap *b, const char *fname, ...);
/**
* #### `int bm_save_custom(Bitmap *b, bm_write_fun fun, void *context, const char *ext)`
*
* Saves a bitmap `b` using a custom function `fun` to output the individual bytes.
*
* `context` is a pointer to a structure that is passed directly to the callback function to
* receive the bytes.
*
* The `ext` parameter determines the file type: "bmp", "gif", "pcx", "tga", "pbm", "pgm", "png" or "jpg"
* (PNG and JPG support must be enabled through the `USEPNG` and `USEJPG` preprocessor definitions).
*
* The custom function `fun` has this prototype:
*
* ```
* int (*bm_write_fun)(void *data, int len, void *context);
* ```
*
* * `data` is the bytes to write,
* * `len` is the number of bytes to write, and
* * `context` is the pointer passed through directly from `bm_save_custom()`
*
* The `bm_write_fun` should return 1 on success, 0 on failure.
*/
typedef int (*bm_write_fun)(void *data, int len, void *context);
int bm_save_custom(Bitmap *b, bm_write_fun fun, void *context, const char *ext);
/**
* ### Reference Counting Functions
*
* These functions implement reference counting on `Bitmap` objects.
*
* Call `bm_retain()` on a bitmap at every location where a reference is held.
* Then call `bm_release()` on the bitmap when those references are no longer being held.
* When the last reference is released, the bitmap will be freed automatically.
*
* _Reference counting is optional:_ Bitmap objects are created with a reference
* count of zero to indicate they are not managed by the reference counter, and those
* should be destroyed through `bm_free()`.
*/
/**
* #### `Bitmap *bm_retain(Bitmap *b)`
*
* Increments the reference count of a `Bitmap` object `b`.
*
* It returns the object.
*/
Bitmap *bm_retain(Bitmap *b);
/**
* #### `void bm_release(Bitmap *b)`
*
* Decrements the reference count of a `Bitmap` object.
*
* If the reference count reaches 0, `bm_free()` is called on
* the object, and the pointer is no longer valid.
*/
void bm_release(Bitmap *b);
/**
* ### Binding Functions
* These functions are used to bind a `Bitmap` structure to
* an existing memory buffer such as an OpenGL texture, an
* SDL Surface or a Win32 GDI context.
*/
/**
* #### `Bitmap *bm_bind(int w, int h, unsigned char *data)`
*
* Creates a bitmap structure bound to an existing array
* of pixel data (for example, an OpenGL texture or a SDL surface). The
* `data` must be an array of `w` × `h` × 4 bytes of ARGB pixel data.
*
* ~~The returned `Bitmap*` must be destroyed with `bm_unbind()`
* rather than `bm_free()`.~~ In the latest versions, `bm_unbind()` just calls
* `bm_free()`
*/
Bitmap *bm_bind(int w, int h, unsigned char *data);
/**
* #### `void bm_rebind(Bitmap *b, unsigned char *data)`
*
* Changes the data referred to by a bitmap structure previously
* created with a call to `bm_bind()`.
*
* The new data must be of the same dimensions as specified
* in the original `bm_bind()` call.
*/
void bm_rebind(Bitmap *b, unsigned char *data);
/**
* #### `void bm_unbind(Bitmap *b)`
*
* Deallocates the memory of a bitmap structure previously created
* through `bm_bind()`.
*
* **Deprecated** - in the newest versions, this function just
* calls `bm_free()`
*/
void bm_unbind(Bitmap *b);
#if defined(USESDL) && defined(SDL_h_)
/**
* #### `SDL_Texture *bm_create_SDL_texture(Bitmap *b, SDL_Renderer *renderer)`
*
* Creates a
*
* **Note** This function is only available if the `USESDL` preprocessor macro
* is defined, and `SDL.h` is included before `bmp.h`.
*/
SDL_Texture *bm_create_SDL_texture(Bitmap *b, SDL_Renderer *renderer);
#endif
/**
* ### Clipping and Buffer Manipulation Functions
*/
/**
* #### `void bm_clip(Bitmap *b, int x0, int y0, int x1, int y1)`
*
* Sets the clipping rectangle on the bitmap from `x0,y0` (inclusive)
* to `x1,y1` exclusive.
*/
void bm_clip(Bitmap *b, int x0, int y0, int x1, int y1);
/**
* #### `void bm_unclip(Bitmap *b)`
*
* Resets the bitmap `b`'s clipping rectangle.
*/
void bm_unclip(Bitmap *b);
/**
* #### `int bm_inclip(Bitmap *b, int x, int y)`
*
* Tests whether the point `x,y` is in the bitmap `b`'s
* clipping region. Returns non-zero if it is, zero if it isn't.
*/
int bm_inclip(Bitmap *b, int x, int y);
/**
* #### `BmRect bm_get_clip(Bitmap *b)`
*
* Retrieves bitmap `b`'s clipping rectangle.
*/
BmRect bm_get_clip(Bitmap *b);
/**
* #### `void bm_set_clip(Bitmap *b, const BmRect rect)`
*
* Sets bitmap `b`'s clipping rectangle to `rect`.
*/
void bm_set_clip(Bitmap *b, const BmRect rect);
/**
* #### `void bm_flip_vertical(Bitmap *b)`
*
* Flips the bitmap vertically.
*/
void bm_flip_vertical(Bitmap *b);
/**
* ### Pixel Functions
*/
/**
* #### `bm_color_t bm_get(Bitmap *b, int x, int y)`
*
* Retrieves the value of the pixel at `x,y` as an integer.
*
* The return value is in the form `0xAABBGGRR`
*/
bm_color_t bm_get(Bitmap *b, int x, int y);
/**
* #### `void bm_set(Bitmap *b, int x, int y, bm_color_t c)`
*
* Sets the value of the pixel at `x,y` to the color `c`.
*
* `c` is in the form `0xAABBGGRR`.
*/
void bm_set(Bitmap *b, int x, int y, bm_color_t c);
/**
* ### Color functions
* Functions for manipulating colors in the image.
*/
/**
* #### `void bm_set_color(Bitmap *bm, bm_color_t col)`
*
* Sets the color of the pen to a color represented
* by an integer, like `0xAARRGGBB`
*/
void bm_set_color(Bitmap *bm, bm_color_t col);
/**
* #### `bm_color_t bm_get_color(Bitmap *bc)`
*
* Retrieves the pen color.
*/
bm_color_t bm_get_color(Bitmap *bm);
/**
* #### `void bm_set_alpha(Bitmap *bm, int a)`
*
* Sets the alpha value of the pen to `a`
*/
void bm_set_alpha(Bitmap *bm, int a);
/**
* #### `bm_color_t bm_picker(Bitmap *bm, int x, int y)`
*
* Sets the color of the pen to the color of the pixel at <x,y>
* on the bitmap.
*
* The pen color can then be retrieved through `bm_get_color()`.
*
* It returns the integer representation of the color.
*/
bm_color_t bm_picker(Bitmap *bm, int x, int y);
/**
* #### `bm_color_t bm_atoi(const char *text)`
*
* Converts a text string like "#FF00FF" or "white" to
* an integer of the form `0xFF00FF`.
* The `text` parameter is not case sensitive and spaces are
* ignored, so for example "darkred" and "Dark Red" are equivalent.
*
* The shorthand "#RGB" format is also supported
* (eg. "#0fb", which is the same as "#00FFBB").
*
* Additionally, it also supports the CSS syntax for "RGB(r,g,b)",
* "RGBA(r,g,b,a)", "HSL(h,s,l)" and "HSLA(h,s,l,a)".
*
* The list of supported colors are based on Wikipedia's
* list of HTML and X11 [Web colors](http://en.wikipedia.org/wiki/Web_colors).
*
* It returns 0 (black) if the string couldn't be parsed.
*/
bm_color_t bm_atoi(const char *text);
/**
* #### `bm_color_t bm_rgb(unsigned char R, unsigned char G, unsigned char B)`
*
* Builds a color from the specified `(R,G,B)` values
*/
bm_color_t bm_rgb(unsigned char R, unsigned char G, unsigned char B);
/**
* #### `bm_color_t bm_rgba(unsigned char R, unsigned char G, unsigned char B, unsigned char A)`
*
* Builds a color from the specified `(R,G,B,A)` values
*/
bm_color_t bm_rgba(unsigned char R, unsigned char G, unsigned char B, unsigned char A);
/**
* #### `void bm_get_rgb(bm_color_t col, unsigned char *R, unsigned char *G, unsigned char *B)`
*
* Decomposes a color `col` into its `R,G,B` components.
*/
void bm_get_rgb(bm_color_t col, unsigned char *R, unsigned char *G, unsigned char *B);
/**
* #### `bm_color_t bm_hsl(double H, double S, double L)`
*
* Creates a color from the given Hue/Saturation/Lightness values.
* See <https://en.wikipedia.org/wiki/HSL_and_HSV> for more information.
*
* Hue (`H`) is given as an angle in degrees from 0° to 360°.
* Saturation (`S`) and Lightness (`L`) are given as percentages from 0 to 100%.
*/
bm_color_t bm_hsl(double H, double S, double L);
/**
* #### `bm_color_t bm_hsla(double H, double S, double L, double A)`
*
* Creates a color from the given Hue/Saturation/Lightness and alpha values.
*
* Hue (`H`) is given as an angle in degrees from 0° to 360°.
* Saturation (`S`) and Lightness (`L`) and Alpha (`A`) are given as percentages from 0 to 100%.
*/
bm_color_t bm_hsla(double H, double S, double L, double A);
/**
* #### `bm_get_hsl(bm_color_t col, double *H, double *S, double *L)`
*
* Decomposes a color `col` into its Hue/Saturation/Lightness components.
*
* Hue (`H`) is given as an angle in degrees from 0° to 360°.
* Saturation (`S`) and Lightness (`L`) are given as percentages from 0 to 100%.
*/
void bm_get_hsl(bm_color_t col, double *H, double *S, double *L);
/**
* #### `int bm_colcmp(bm_color_t c1, bm_color_t c2)`
*
* Compares the RGB values of two colors, ignoring the alphas values
* (If the alpha values are important you can just use `==`).
*
* Returns non-zero if the RGB values of `c1` and `c2` are the same,
* zero otherwise.
*/
int bm_colcmp(bm_color_t c1, bm_color_t c2);
/**
* #### `bm_color_t bm_byte_order(bm_color_t col)`
*
* Fixes the input color to be in the proper byte order.
*
* The input color should be in the format `0xAARRGGBB`. The output
* will be in either `0xAARRGGBB` or `0xAABBGGRR` depending on how the
* library was compiled.
*/
bm_color_t bm_byte_order(bm_color_t col);
/**
* #### `bm_color_t bm_lerp(bm_color_t color1, bm_color_t color2, double t)`
*
* Computes the color that is a distance `t` along the line between
* `color1` and `color2`.
*
* If `t` is 0 it returns `color1`. If `t` is 1.0 it returns `color2`.
*/
bm_color_t bm_lerp(bm_color_t color1, bm_color_t color2, double t);
/**
* #### `bm_color_t bm_graypixel(bm_color_t c)`
*
* Converts a color to its grayscale value.
*
* See <https://en.wikipedia.org/wiki/Grayscale>
*/
bm_color_t bm_graypixel(bm_color_t c);
/**
* #### `void bm_swap_color(Bitmap *b, bm_color_t src, bm_color_t dest)`
*
* Replaces all pixels of color `src` in bitmap `b` with the color `dest`.
*/
void bm_swap_color(Bitmap *b, bm_color_t src, bm_color_t dest);
/**
* #### `Bitmap *bm_swap_rb(Bitmap *b)`
*
* Swaps the Red and Blue channels in a bitmap.
*
* (It is meant for certain use cases where a buffer is BGRA instead of RGBA)
*/
Bitmap *bm_swap_rb(Bitmap *b);
/**
* ### Blitting Functions
*/
/**
* #### `void bm_blit(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, int w, int h)`
*
* Blits an area of `w` × `h` pixels at `sx,sy` on the source bitmap `src` to
* `dx,dy` on the destination bitmap `dst`.
*/
void bm_blit(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, int w, int h);
/**
* #### `void bm_maskedblit(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, int w, int h)`
*
* Blits an area of `w` × `h` pixels at `sx,sy` on the `src` bitmap to
* `dx,dy` on the `dst` bitmap.
*
* Pixels on the `src` bitmap that matches the `src` bitmap color are not blitted.
* The alpha value of the pixels on the `src` bitmap is not taken into account.
*/
void bm_maskedblit(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, int w, int h);
/**
* #### `void bm_blit_ex(Bitmap *dst, int dx, int dy, int dw, int dh, Bitmap *src, int sx, int sy, int sw, int sh, int mask)`
*
* Extended blit function. Blits an area of `sw` × `sh` pixels at `sx,sy` from the `src` bitmap to
* `dx,dy` on the `dst` bitmap into an area of `dw` × `dh` pixels, stretching or shrinking the blitted area as neccessary.
*
* If `mask` is non-zero, pixels on the `src` bitmap that matches the `src` bitmap color are not blitted.
* Whether the alpha value of the pixels is taken into account depends on whether `IGNORE_ALPHA` is enabled.
*/
void bm_blit_ex(Bitmap *dst, int dx, int dy, int dw, int dh, Bitmap *src, int sx, int sy, int sw, int sh, int mask);
/**
* #### `void bm_blit_callback(Bitmap *dst, int dx, int dy, int dw, int dh, Bitmap *src, int sx, int sy, int sw, int sh, bm_sampler_function fun)`
*
* Blits a source bitmap to a destination similar to `bm_blit_ex()`, except
* that it calls a callback function for every pixel
*
* The callback function takes this form:
*
* typedef bm_color_t (*bm_sampler_function)(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, bm_color_t dest_color)
*
* Where
*
* * `dst` is the destination bitmap.
* * `dx`,`dy` is the pixel coordinates on the destination being blitted to.
* * `src` is the source bitmap being sampled from.
* * `sx`,`sy` is the coordinates of pixel on the source bitmap being sampled
* * `dest_color` is the current color of `dx`,`dy` on the destination, useful for blending.
*
* It will set the clipping region on `src` to the area defined by `sx,sy,sw,sh`
* before calling the callback, so that the callback can rely on it (The
* clipping region will be restored afterwards).
*/
typedef bm_color_t (*bm_sampler_function)(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, bm_color_t dest_color);
void bm_blit_callback(Bitmap *dst, int dx, int dy, int dw, int dh, Bitmap *src, int sx, int sy, int sw, int sh, bm_sampler_function fun);
/**
* Some built-in sampling functions for use with `bm_blit_callback()`:
*
* - `bm_smp_outline` - Highlights the outline of the `src` bitmap using
* the `dst->color`.
* - `bm_smp_border` - Highlights the border of the `src` bitmap using
* the `dst->color`.
* - `bm_smp_binary` - If the pixel on `src` matches `src->color`, set the
* pixel on `dst` to `dst->color`, otherwise leave it blank.
* - `bm_smp_blend50` - Uses a bit shift trick to do a 50/50 blend between
* the source and destination pixels
*/
bm_color_t bm_smp_outline(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, bm_color_t dest_color);
bm_color_t bm_smp_border(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, bm_color_t dest_color);
bm_color_t bm_smp_binary(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, bm_color_t dest_color);
bm_color_t bm_smp_blend50(Bitmap *dst, int dx, int dy, Bitmap *src, int sx, int sy, bm_color_t dest_color);
/**
* #### `void bm_rotate_blit(Bitmap *dst, int ox, int oy, Bitmap *src, int px, int py, double angle, double scale);`
*
* Rotates a source bitmap `src` around a pivot point `px,py` and blits it onto a destination bitmap `dst`.
*
* The bitmap is positioned such that the point `px,py` on the source is at the offset `ox,oy` on the destination.
*
* The `angle` is clockwise, in radians. The bitmap is also scaled by the factor `scale`.
*/
void bm_rotate_blit(Bitmap *dst, int ox, int oy, Bitmap *src, int px, int py, double angle, double scale);
/**
* #### `void bm_rotate_maskedblit(Bitmap *dst, int ox, int oy, Bitmap *src, int px, int py, double angle, double scale);`
*
* Rotates a source bitmap `src` around a pivot point `px,py` and blits it onto a destination bitmap `dst`.
*
* The bitmap is positioned such that the point `px,py` on the source is at the offset `ox,oy` on the destination.
*
* The `angle` is clockwise, in radians. The bitmap is also scaled by the factor `scale`.
*
* Pixels on the `src` bitmap that matches the `src` bitmap color are not blitted.
* The alpha value of the pixels on the `src` bitmap is not taken into account.
*/
void bm_rotate_maskedblit(Bitmap *dst, int ox, int oy, Bitmap *src, int px, int py, double angle, double scale);
/**
* #### `void bm_stretch(Bitmap *dst, Bitmap *src, BmPoint P[4])`
*
* Stretches a bitmap `src` onto the quadrilateral defined by the four points `P`
* on the destination `dst`.
*
* The clipping rectangle of `src` controls the region of `src` that should be stretched
* onto `dst`. It won't draw anything outside of `dst`'s clipping region.
*
* Vertices in `P` are in clockwise order. `P[0]` corresponds to the top left, `P[1]` to
* the top right, `P[2]` to the bottom right and `P[3]` to the bottom left of `src`.
*/
void bm_stretch(Bitmap *dst, Bitmap *src, BmPoint P[4]);
/**
* #### `void bm_destretch(Bitmap *dst, Bitmap *src, BmPoint P[4])`
*
* Fits the quadrilateral defined by the four points `P` on the bitmap `src` into the
* destination bitmap `dst`.
*
* It is the inverse operation of `bm_stretch()`.
*
* The clipping rectangle of `dst` defines the region into which the quadrilateral should be mapped.
* Pixels outside of `src`'s clipping rectangle won't be mapped.
*
* Vertices in `P` are in clockwise order. `P[0]` corresponds to the top left, `P[1]` to
* the top right, `P[2]` to the bottom right and `P[3]` to the bottom left of `dst`.
*/
void bm_destretch(Bitmap *dst, Bitmap *src, BmPoint P[4]);
/**
* #### `void bm_blit_xbm(Bitmap *dst, int dx, int dy, int sx, int sy, int w, int h, int xbm_w, int xbm_h, unsigned char xbm_data[]);`
*
* Blits an area of `w` × `h` pixels at `sx,sy` in [XBM image data][XBM] to
* `dx,dy` on the destination bitmap `dst`.
*
* It uses the color of `dst` as the foreground. Backdround pixels are unchanged.
*
* `xbm_w` and `xbm_h` is the width and height of the XBM image respectively. `xbm_data` is the XBM bytes.
*/
void bm_blit_xbm(Bitmap *dst, int dx, int dy, int sx, int sy, int w, int h, int xbm_w, int xbm_h, unsigned char xbm_data[]);
/**
* ### Filter Functions
*/
/** #### `void bm_grayscale(Bitmap *b)`
*
* Converts an image to grayscale.
*/
void bm_grayscale(Bitmap *b);
/**
* #### `void bm_smooth(Bitmap *b)`
*
* Smoothes the bitmap `b` by applying a 5×5 Gaussian filter.
*/
void bm_smooth(Bitmap *b);
/**
* #### `void bm_apply_kernel(Bitmap *b, int dim, float kernel[])`
*
* Applies a `dim` × `dim` kernel to the image.
*
* ```
* float smooth_kernel[] = { 0.0, 0.1, 0.0,
* 0.1, 0.6, 0.1,
* 0.0, 0.1, 0.0};
* bm_apply_kernel(screen, 3, smooth_kernel);
* ```
*/
void bm_apply_kernel(Bitmap *b, int dim, float kernel[]);
/**
* #### `Bitmap *bm_resample(const Bitmap *in, int nw, int nh)`
*
* Creates a new bitmap of dimensions `nw` × `nh` that is a scaled
* using the Nearest Neighbour method the input bitmap.
*
* The input bimap remains untouched.
*/
Bitmap *bm_resample(const Bitmap *in, int nw, int nh);
/**
* #### `Bitmap *bm_resample_blin(const Bitmap *in, int nw, int nh)`
*
* Creates a new bitmap of dimensions `nw` × `nh` that is a scaled
* using Bilinear Interpolation from the input bitmap.
*
* The input bimap remains untouched.
*
* _Bilinear Interpolation is better suited for making an image larger._
*/
Bitmap *bm_resample_blin(const Bitmap *in, int nw, int nh);
/**
* #### `Bitmap *bm_resample_bcub(const Bitmap *in, int nw, int nh)`
*
* Creates a new bitmap of dimensions `nw` × `nh` that is a scaled
* using Bicubic Interpolation from the input bitmap.
*
* The input bimap remains untouched.
*
* _Bicubic Interpolation is better suited for making an image smaller._
*/
Bitmap *bm_resample_bcub(const Bitmap *in, int nw, int nh);
/**
* #### `Bitmap *bm_resample_into(const Bitmap *in, Bitmap *out)`
*
* Resamples a bitmap `in` to fit into a bitmap `out` using nearest neighbour.
*/
Bitmap *bm_resample_into(const Bitmap *in, Bitmap *out);
/**
* #### `Bitmap *bm_resample_blin_into(const Bitmap *in, Bitmap *out)`
*
* Resamples a bitmap `in` to fit into a bitmap `out` using bilinear interpolation.
*/
Bitmap *bm_resample_blin_into(const Bitmap *in, Bitmap *out);
/**
* #### `Bitmap *bm_resample_bcub_into(const Bitmap *in, Bitmap *out)`
*
* Resamples a bitmap `in` to fit into a bitmap `out` using bicubic interpolation.
*/
Bitmap *bm_resample_bcub_into(const Bitmap *in, Bitmap *out);
/**
* #### `Bitmap *bm_rotate_cw(const Bitmap *in)`
*
* Creates a new bitmap from `in` that is rotated 90° clockwise.
*
* It takes the clipping region of `in` into account.
*/
Bitmap *bm_rotate_cw(const Bitmap *in);
/**
* #### `Bitmap *bm_rotate_ccw(const Bitmap *in)`
*
* Creates a new bitmap from `in` that is rotated 90° counter-clockwise.
*
* It takes the clipping region of `in` into account.
*/
Bitmap *bm_rotate_ccw(const Bitmap *in);
/**
* ### Palette Functions
*
* `bmp.h` provides these methods for manipulating color palettes.
*/
/**
* #### `BmPalette *bm_palette_create(unsigned int ncolors)`
*
* Creates a palette object with space for `ncolors`.
*
* This palette will be used to reduce the colors in the image when
* saving to an 8-bit format like GIF or PCX.
*
* The reference count of the palette is set to `1` initially.
* Call `bm_palette_release()` when the palette is no longer in use
* which will destro.
*
* It returns `NULL` on error.
*/
BmPalette *bm_palette_create(unsigned int ncolors);
/**
* #### `void bm_set_palette(Bitmap *b, BmPalette *pal)`
*
* Associates a palette `pal` with the bitmap `b`.
*
* `pal` may be `NULL` to dissociate a palette with the bitmap.
*
* It will increment the reference count of `pal` and decrement
* the reference counts of the previous palette assigned so that
* the palette's memory is reclaimed when it is no longer in use.
*/
void bm_set_palette(Bitmap *b, BmPalette *pal);
/**
* #### `BmPalette *bm_get_palette(const Bitmap *b)`
*