-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfg.h
96 lines (68 loc) · 2.16 KB
/
bfg.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
/*
BFG - Block-Based Fast Graphics format for lossless image compression.
Seb Seager
## License
Copyright (c) 2022, Seb Seager.
<>
## Motivation
<>
## Format
<>
REMEMBER: block length is one more than the int recorded (so 0 = len 1)
TODO
- clear out unnecessary asserts after testing
ASSUMPTIONS
- lookback before beginning of the file all bytes are zeros
##
*/
#include <stdint.h>
#define BFG_MAGIC_TAG (0xBFBFBFBF)
#define BFG_VERSION (1)
#define BFG_MAX_BYTES (UINT32_MAX)
#define BFG_BIT_DEPTH (8)
#define BFG_TAG_BITS (3)
#define BFG_DIFF_BITS (4) /* must divide BFG_BIT_DEPTH */
/* Optionally provide custom malloc and free implementations. */
#ifndef BFG_MALLOC
#define BFG_MALLOC(sz) malloc(sz)
#define BFG_REALLOC(ptr, sz) realloc(ptr, sz)
#define BFG_FREE(ptr) free(ptr)
#endif
/* Tags appearing in the first BFG_TAG_BITS of each block header. No tag
* with a value < 0 should ever appear in a valid BFG file. These are provided
* for ease of implementation only. */
typedef enum {
BFG_BLOCK_FULL = 0,
BFG_BLOCK_RUN = 1,
BFG_BLOCK_DIFF = 2,
} bfg_block_type_t;
/* Internal image representation generated by any *_decode function. */
typedef struct bfg_raw {
uint32_t width;
uint32_t height;
uint8_t n_channels;
uint8_t *pixels;
} * bfg_raw_t;
/* Info struct for a BFG encoding. Filled out by bfg_encode and written to the
* beginning of a BFG file by bfg_write as the header block. */
typedef struct bfg_info {
uint32_t magic_tag;
uint32_t version;
uint32_t width;
uint32_t height;
uint32_t n_bytes;
uint8_t n_channels;
uint8_t color_mode;
uint8_t bit_depth;
} * bfg_info_t;
// TODO: MAKE THIS DIVISIBLE BY 32 BITS (add another 8 bits)
// TODO: fill out bit_depth
/* Encoded BFG image representation, always of length bfg_info->n_bytes.
* Generated by bfg_encode and written to a BFG file after the header block. */
typedef uint8_t *bfg_img_t;
void bfg_free(bfg_raw_t raw, bfg_img_t img);
/* Caller must free. */
bfg_img_t bfg_encode(bfg_raw_t raw, bfg_info_t info);
int bfg_decode(bfg_info_t info, bfg_img_t img, bfg_raw_t raw);
int bfg_write(char *fpath, bfg_info_t info, bfg_img_t img);
bfg_img_t bfg_read(char *fpath, bfg_info_t info);