123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include <assert.h>
- #include <ctype.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
-
- int main(int argc, char** argv)
- {
- if (argc != 3)
- {
- fprintf(stderr, "Usage: %s <input image> <output header>\n", argv[0]);
-
- return 1;
- }
-
- const char* inFilename = argv[1];
- const char* outFilename = argv[2];
-
-
- int tileWidth;
- int tileHeight;
- uint8_t* tileBuffer;
- {
- FILE* inFile = fopen(inFilename, "rb");
- assert(inFile);
-
- #pragma pack(push,1)
- struct BmpHeader
- {
- char magic[2];
- uint32_t totalSize;
- uint32_t reserved;
- uint32_t offset;
- uint32_t headerSize;
- int32_t width;
- int32_t height;
- uint16_t planes;
- uint16_t depth;
- uint32_t compression;
- uint32_t imageSize;
- int32_t horizontalResolution;
- int32_t verticalResolution;
- uint32_t paletteColorCount;
- uint32_t importantColorcount;
- } bmpHeader;
- #pragma pack(pop)
-
- // Read the BMP header so we know where the image data is located
- fread(&bmpHeader, 1, sizeof(bmpHeader), inFile);
- assert(bmpHeader.magic[0] == 'B' && bmpHeader.magic[1] == 'M');
- assert(bmpHeader.depth == 8);
- assert(bmpHeader.headerSize == 40);
-
- // Go to location in file of image data
- fseek(inFile, bmpHeader.offset, SEEK_SET);
-
- // Read in the image data
- tileBuffer = malloc(bmpHeader.imageSize);
- assert(tileBuffer);
- fread(tileBuffer, 1, bmpHeader.imageSize, inFile);
-
- tileWidth = bmpHeader.width;
- tileHeight = bmpHeader.height;
-
- fclose(inFile);
- }
-
-
- printf("Input: %s\n", inFilename);
- printf("Output: %s\n", outFilename);
- printf("Width: %d\n", tileWidth);
- printf("Height: %d\n", tileHeight);
-
- FILE* outFile = fopen(outFilename, "w");
- assert(outFile);
-
-
- // Generate the preamble
- {
- fprintf(outFile, "// AUTOMATICALLY GENERATED. DO NOT EDIT.\n");
- fprintf(outFile, "\n");
- fprintf(outFile, "#pragma once\n");
- fprintf(outFile, "\n");
- fprintf(outFile, "#include <stdint.h>\n");
- fprintf(outFile, "\n");
- fprintf(outFile, "\n");
- }
-
-
- // Generate the font map with the calculated glyph bytes
- {
- fprintf(outFile, "static const uint8_t tile[%d][%d] =\n", tileHeight, tileWidth);
- fprintf(outFile, "{\n");
- fprintf(outFile, " ");
-
- int count = 0;
-
- for (int row = 0; row < tileHeight; ++row)
- {
- for (int col = 0; col < tileWidth; ++col)
- {
- // BMP is laid out bottom-to-top, but we want top-to-bottom (0-indexed)
- int y = tileHeight - row - 1;
-
- uint8_t paletteIndex = tileBuffer[y * tileWidth + col];
-
- fprintf(outFile, "%d,", paletteIndex);
- ++count;
-
- // Put a newline after sixteen values to keep it orderly
- if ((count % 16) == 0)
- {
- fprintf(outFile, "\n");
- fprintf(outFile, " ");
-
- count = 0;
- }
- }
- }
-
- fprintf(outFile, "};\n");
- }
-
- fclose(outFile);
-
- printf("\n");
- printf("DONE\n");
-
- return 0;
- }
|