aboutsummaryrefslogtreecommitdiff
path: root/bmpconv.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2023-05-02 21:02:04 +1000
committerquou <quou@disroot.org>2023-05-02 21:02:04 +1000
commitc1efdf9b0875f2a39488a86cd838947a24fab9fc (patch)
treeb459d024fa99029758f8d2f8630470fe6060122e /bmpconv.c
Initial commit.
Diffstat (limited to 'bmpconv.c')
-rw-r--r--bmpconv.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/bmpconv.c b/bmpconv.c
new file mode 100644
index 0000000..446af30
--- /dev/null
+++ b/bmpconv.c
@@ -0,0 +1,90 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "render.h"
+
+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, i, s;
+ unsigned short bmp_bits;
+ char magic[4] = { 'I', 'M', 'A', 'G' };
+ Colour pixel;
+ 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;
+ }
+
+ outfile = fopen(argv[2], "wb");
+ if (!outfile) {
+ fprintf(stderr, "Failed to open %s.\n", argv[2]);
+ fclose(infile);
+ 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);
+
+ fwrite(magic, 1, 4, outfile);
+ fwrite(&bmp_w, 1, 4, outfile);
+ fwrite(&bmp_h, 1, 4, outfile);
+
+ s = bmp_w * bmp_h;
+ /* Allocate space. */
+ for (i = 0; i < s; i++) {
+ fwrite(&pixel, 1, 4, outfile);
+ }
+
+ fseek(outfile, 12, SEEK_SET);
+ 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;
+ /* Do a lil flip. */
+ fseek(
+ outfile,
+ 12 + (x + (bmp_h - y - 1) * bmp_w) * 4,
+ SEEK_SET
+ );
+ fwrite(&pixel, 1, 4, outfile);
+ }
+ }
+
+end:
+ fclose(infile);
+ fclose(outfile);
+ return ret;
+}