Commit 79f8b8001554f8597265fcf3d4de6c73d0cf2018

Authored by henyxia
0 parents

Initial verson

Showing 4 changed files with 106 additions and 0 deletions   Show diff stats
.gitignore 0 → 100644
  1 +++ a/.gitignore
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +bmp2map
Makefile 0 → 100644
  1 +++ a/Makefile
@@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
  1 +CC=gcc
  2 +CC_FLAGS=-c -Wall -Werror -std=gnu99 -g
  3 +CC_LIBS=
  4 +INCLUDES=
  5 +
  6 +SOURCES=main.c
  7 +OBJECTS=$(SOURCES:.c=.o)
  8 +OUTPUT=bmp2map
  9 +
  10 +all: $(SOURCES) $(OUTPUT)
  11 +
  12 +$(OUTPUT): $(OBJECTS)
  13 + $(CC) $(OBJECTS) $(CC_LIBS) -o $@
  14 +
  15 +%.o: %.c
  16 + $(CC) $(INCLUDES) $(CC_FLAGS) $< -o $@
  17 +
  18 +clear:
  19 + rm -f $(OUTPUT) $(OBJECTS)
README.md 0 → 100644
  1 +++ a/README.md
@@ -0,0 +1 @@ @@ -0,0 +1 @@
  1 +# bmp2map
main.c 0 → 100644
  1 +++ a/main.c
@@ -0,0 +1,85 @@ @@ -0,0 +1,85 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +
  4 +int main(int argc, char *argv[])
  5 +{
  6 + if(argc != 3)
  7 + {
  8 + printf("bmp2map [BMP_SRC] [MAP_DEST]\n");
  9 + return 1;
  10 + }
  11 +
  12 + FILE* src = NULL;
  13 + FILE* dst = NULL;
  14 + unsigned char buffer[5];
  15 + int initOffsetBMP;
  16 +
  17 + src = fopen(argv[1], "r");
  18 + dst = fopen(argv[2], "w");
  19 +
  20 + if(src == NULL)
  21 + {
  22 + printf("Unable to open the BMP file\n");
  23 + return 2;
  24 + }
  25 +
  26 + if(dst == NULL)
  27 + {
  28 + printf("Unable to open the MAP file\n");
  29 + return 3;
  30 + }
  31 +
  32 + //printf("Reading BMP\n");
  33 +
  34 + fseek(src, 0x0A, SEEK_SET);
  35 + buffer[0] = getc(src);
  36 + buffer[1] = getc(src);
  37 + buffer[2] = getc(src);
  38 + buffer[3] = getc(src);
  39 + //printf("Pixel array offset %02x %02x %02x %02x\n", buffer[3], buffer[2], buffer[1], buffer[0]);
  40 + initOffsetBMP = buffer[0] + ((int)buffer[1] * 256);
  41 +
  42 + fseek(src, 0x12, SEEK_SET);
  43 + buffer[0] = getc(src);
  44 + buffer[1] = getc(src);
  45 + buffer[2] = getc(src);
  46 + buffer[3] = getc(src);
  47 + //printf("Width %02x %02x %02x %02x\n", buffer[3], buffer[2], buffer[1], buffer[0]);
  48 +
  49 + //fseek(src, 0x12, SEEK_SET);
  50 + buffer[0] = getc(src);
  51 + buffer[1] = getc(src);
  52 + buffer[2] = getc(src);
  53 + buffer[3] = getc(src);
  54 + //printf("Height %02x %02x %02x %02x\n", buffer[3], buffer[2], buffer[1], buffer[0]);
  55 +
  56 + fseek(src, 0x1C, SEEK_SET);
  57 + buffer[0] = getc(src);
  58 + buffer[1] = getc(src);
  59 + //printf("Bits Per Pixel %02x %02x\n", buffer[0], buffer[1]);
  60 +
  61 + if(buffer[1] != 0 || buffer[0] != 0x08)
  62 + {
  63 + printf("Could not read BMP as it's in %d bits per pixel\n", buffer[0]);
  64 + return 1;
  65 + }
  66 +
  67 + fseek(src, initOffsetBMP, SEEK_SET);
  68 +
  69 + //printf("Now reading BMP pixels and printing them to the MAP file\n");
  70 + while(1)
  71 + {
  72 + buffer[0] = getc(src);
  73 + if(feof(src))
  74 + break;
  75 + //printf("Pixel BMP 24 bits Grey %02X\n", buffer[0]);
  76 + fprintf(dst, "%c", buffer[0]);
  77 + }
  78 +
  79 + //printf("Done !\n");
  80 +
  81 + fclose(src);
  82 + fclose(dst);
  83 +
  84 + return 0;
  85 +}