Compare commits

...

2 Commits

Author SHA1 Message Date
itycodes a89f992623 Improve the CLI interface of decode.py
2 weeks ago
itycodes c92c83bacd Add a C encoder
2 weeks ago

@ -26,6 +26,9 @@ from PIL import Image
import sys
if not len(sys.argv) in [2, 3]:
print(f"Usage: {sys.argv[0]} <input.rif> [output.png]")
# RGB, RGBA
frm_sizes = [3, 4]
frm_names = ["RGB", "RGBA"]
@ -41,4 +44,7 @@ height = (len(data)//frm_size)//width
print(width, height)
img = Image.frombuffer(frm_names[formt], (width, height), data, "raw", frm_names[formt], 0, 1)
if len(sys.argv) == 2:
img.save('.'.join(sys.argv[1].split(".")[:1])+".png")
elif len(sys.argv) == 3:
img.save(sys.argv[2])

30
rif.h

@ -0,0 +1,30 @@
typedef struct rif {
// "lifV0001"
// "rifV0001"
char magic_num[8]; // Big Endian. UTF-8 Encoded. 6C 69 66 56 30 30 30 31
uint64_t width; // Big Endian
uint8_t format;
uint8_t data[];
} rif_t;
#define RIF_MAGIC_LITTLE {'l', 'i', 'f', 'V', '0', '0', '0', '1'}
#define RIF_MAGIC_BIG {'r', 'i', 'f', 'V', '0', '0', '0', '1'}
#define RIF_FORMAT_R8G8B8 0
#define RIF_FORMAT_R8G8B8A8 1
// TODO detect endianess - this breaks on big endian systems.
void write_rif_little(char* path, uint32_t width, uint32_t size, uint8_t format, void* data) {
// TODO is this better than just hardcoding 17?
const uint32_t header_size = sizeof(rif_t) - sizeof(uint8_t*);
const uint64_t img_size = header_size + size;
rif_t* img = malloc(img_size);
char magic[] = RIF_MAGIC_LITTLE;
memcpy(&img->magic_num, magic, 8);
img->width = width;
img->format = format;
memcpy(&img->data, data, size);
FILE* output_file = fopen(path, "w");
fwrite(img, 1, img_size, output_file);
free(img);
}
Loading…
Cancel
Save