No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tile_processor.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <assert.h>
  2. #include <ctype.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. int main(int argc, char** argv)
  8. {
  9. if (argc != 3)
  10. {
  11. fprintf(stderr, "Usage: %s <input image> <output header>\n", argv[0]);
  12. return 1;
  13. }
  14. const char* inFilename = argv[1];
  15. const char* outFilename = argv[2];
  16. int tileWidth;
  17. int tileHeight;
  18. uint8_t* tileBuffer;
  19. {
  20. FILE* inFile = fopen(inFilename, "rb");
  21. assert(inFile);
  22. #pragma pack(push,1)
  23. struct BmpHeader
  24. {
  25. char magic[2];
  26. uint32_t totalSize;
  27. uint32_t reserved;
  28. uint32_t offset;
  29. uint32_t headerSize;
  30. int32_t width;
  31. int32_t height;
  32. uint16_t planes;
  33. uint16_t depth;
  34. uint32_t compression;
  35. uint32_t imageSize;
  36. int32_t horizontalResolution;
  37. int32_t verticalResolution;
  38. uint32_t paletteColorCount;
  39. uint32_t importantColorcount;
  40. } bmpHeader;
  41. #pragma pack(pop)
  42. // Read the BMP header so we know where the image data is located
  43. fread(&bmpHeader, 1, sizeof(bmpHeader), inFile);
  44. assert(bmpHeader.magic[0] == 'B' && bmpHeader.magic[1] == 'M');
  45. assert(bmpHeader.depth == 8);
  46. assert(bmpHeader.headerSize == 40);
  47. // Go to location in file of image data
  48. fseek(inFile, bmpHeader.offset, SEEK_SET);
  49. // Read in the image data
  50. tileBuffer = malloc(bmpHeader.imageSize);
  51. assert(tileBuffer);
  52. fread(tileBuffer, 1, bmpHeader.imageSize, inFile);
  53. tileWidth = bmpHeader.width;
  54. tileHeight = bmpHeader.height;
  55. fclose(inFile);
  56. }
  57. printf("Input: %s\n", inFilename);
  58. printf("Output: %s\n", outFilename);
  59. printf("Width: %d\n", tileWidth);
  60. printf("Height: %d\n", tileHeight);
  61. FILE* outFile = fopen(outFilename, "w");
  62. assert(outFile);
  63. // Generate the preamble
  64. {
  65. fprintf(outFile, "// AUTOMATICALLY GENERATED. DO NOT EDIT.\n");
  66. fprintf(outFile, "\n");
  67. fprintf(outFile, "#pragma once\n");
  68. fprintf(outFile, "\n");
  69. fprintf(outFile, "#include <stdint.h>\n");
  70. fprintf(outFile, "\n");
  71. fprintf(outFile, "\n");
  72. }
  73. // Generate the font map with the calculated glyph bytes
  74. {
  75. fprintf(outFile, "static const uint8_t tile[%d][%d] =\n", tileHeight, tileWidth);
  76. fprintf(outFile, "{\n");
  77. fprintf(outFile, " ");
  78. int count = 0;
  79. for (int row = 0; row < tileHeight; ++row)
  80. {
  81. for (int col = 0; col < tileWidth; ++col)
  82. {
  83. // BMP is laid out bottom-to-top, but we want top-to-bottom (0-indexed)
  84. int y = tileHeight - row - 1;
  85. uint8_t paletteIndex = tileBuffer[y * tileWidth + col];
  86. fprintf(outFile, "%d,", paletteIndex);
  87. ++count;
  88. // Put a newline after sixteen values to keep it orderly
  89. if ((count % 16) == 0)
  90. {
  91. fprintf(outFile, "\n");
  92. fprintf(outFile, " ");
  93. count = 0;
  94. }
  95. }
  96. }
  97. fprintf(outFile, "};\n");
  98. }
  99. fclose(outFile);
  100. printf("\n");
  101. printf("DONE\n");
  102. return 0;
  103. }