aboutsummaryrefslogtreecommitdiff
path: root/convimg.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-09-23 21:11:29 +1000
committerquou <quou@disroot.org>2024-09-23 21:11:29 +1000
commit259237fae792e8f19d9b7920b2354f75bc1789e2 (patch)
treeaf079430f30f7df1b76efc4f96f520b0b6eee0af /convimg.c
initial commit, basic platform and rendering code.
Diffstat (limited to 'convimg.c')
-rw-r--r--convimg.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/convimg.c b/convimg.c
new file mode 100644
index 0000000..631a8b1
--- /dev/null
+++ b/convimg.c
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct {
+ unsigned char r, g, b, a;
+} Colour;
+
+int main(int argc, char** argv) {
+ FILE* outfile, * infile;
+ char bmp_magic[2];
+ int ret = 0;
+ unsigned bmp_offset;
+ int bmp_w, bmp_h, x, y, s, bs;
+ unsigned short bmp_bits;
+ unsigned* bitmap;
+ Colour pixel, * buffer;
+ unsigned char t;
+
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s infile outfile.\n", argv[0]);
+ return 1;
+ }
+
+ infile = fopen(argv[1], "rb");
+ if (!infile) {
+ fprintf(stderr, "Failed to open %s.\n", argv[1]);
+ return 2;
+ }
+
+ fread(bmp_magic, 2, 1, infile);
+ if (bmp_magic[0] != 'B' || bmp_magic[1] != 'M') {
+ ret = 3;
+ fprintf(stderr, "Not a valid bitmap file.\n");
+ goto end;
+ }
+
+ fseek(infile, 10, SEEK_SET);
+ fread(&bmp_offset, 4, 1, infile);
+
+ fseek(infile, 18, SEEK_SET);
+ fread(&bmp_w, 4, 1, infile);
+ fread(&bmp_h, 4, 1, infile);
+ fseek(infile, 28, SEEK_SET);
+ fread(&bmp_bits, 2, 1, infile);
+
+ if (bmp_bits != 32) {
+ ret = 4;
+ fprintf(stderr, "Bitmap must have 32 bit pixels. %d\n", bmp_bits);
+ goto end;
+ }
+
+ fseek(infile, bmp_offset, SEEK_SET);
+
+ s = bmp_w * bmp_h;
+ buffer = malloc(s * 4);
+ /* Read & flip */
+ for (y = 0; y < bmp_h; y++) {
+ for (x = 0; x < bmp_w; x++) {
+ fread(&pixel, 1, 4, infile);
+ t = pixel.r;
+ pixel.r = pixel.b;
+ pixel.b = t;
+ if (pixel.a == 0) {
+ pixel.r = 0;
+ pixel.g = 0;
+ pixel.b = 0;
+ }
+ buffer[x + (bmp_h - y - 1) * bmp_w] = pixel;
+ }
+ }
+
+ outfile = fopen(argv[2], "wb");
+ if (!outfile) {
+ fprintf(stderr, "Failed to open %s.\n", argv[2]);
+ fclose(infile);
+ return 2;
+ }
+ bs = (bmp_w * bmp_h + 32) / 32 * 4;
+ bitmap = calloc(bs, 1);
+ for (y = 0; y < bmp_h; y++) {
+ for (x = 0; x < bmp_w; x++) {
+ int idx = x + y * bmp_w;
+ if (buffer[idx].r) {
+ bitmap[idx / 32] |= 1 << (idx % 32);
+ }
+ }
+ }
+ fwrite(&bmp_w, 1, 2, outfile);
+ fwrite(&bmp_h, 1, 2, outfile);
+ fwrite(bitmap, 1, bs, outfile);
+ free(buffer);
+
+end:
+ fclose(infile);
+ fclose(outfile);
+ return ret;
+}