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.

palette_processor.c 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <assert.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. int main(int argc, char** argv)
  7. {
  8. if (argc != 3)
  9. {
  10. fprintf(stderr, "Usage: %s <input palette> <output header>\n", argv[0]);
  11. return 1;
  12. }
  13. const char* inFilename = argv[1];
  14. const char* outFilename = argv[2];
  15. // Read the file
  16. uint16_t* palette;
  17. int paletteSize;
  18. {
  19. FILE* inFile = fopen(inFilename, "r");
  20. assert(inFile);
  21. char* line;
  22. size_t len = 0;
  23. getline(&line, &len, inFile);
  24. assert(strncmp(line, "JASC-PAL", 8) == 0);
  25. getline(&line, &len, inFile);
  26. assert(strncmp(line, "0100", 4) == 0);
  27. getline(&line, &len, inFile);
  28. paletteSize = atoi(line);
  29. palette = malloc(sizeof(*palette) * paletteSize);
  30. assert(palette);
  31. // Each line is of form R G B
  32. for (int i = 0; i < paletteSize; ++i)
  33. {
  34. getline(&line, &len, inFile);
  35. char* tok = strtok(line, " ");
  36. int red = atoi(tok);
  37. tok = strtok(NULL, " ");
  38. int green = atoi(tok);
  39. tok = strtok(NULL, " ");
  40. int blue = atoi(tok);
  41. uint16_t rgb565 =
  42. ((red >> 3u) << 11u)
  43. | ((green >> 2u) << 5u)
  44. | (blue >> 3u);
  45. uint16_t endianSwap = ((rgb565 & 0xFFu) << 8u) | (rgb565 >> 8u);
  46. palette[i] = endianSwap;
  47. }
  48. fclose(inFile);
  49. }
  50. printf("Input: %s\n", inFilename);
  51. printf("Output: %s\n", outFilename);
  52. printf("Palette Size: %d\n", paletteSize);
  53. // Output to the file
  54. {
  55. FILE* outFile = fopen(outFilename, "w");
  56. assert(outFile);
  57. fprintf(outFile, "// AUTOMATICALLY GENERATED. DO NOT EDIT.\n");
  58. fprintf(outFile, "\n");
  59. fprintf(outFile, "#pragma once\n");
  60. fprintf(outFile, "\n");
  61. fprintf(outFile, "\n");
  62. fprintf(outFile, "uint16_t palette[%d] =\n", paletteSize);
  63. fprintf(outFile, "{\n");
  64. for (int i = 0; i < paletteSize; ++i)
  65. {
  66. fprintf(outFile, " 0x%04X,\n", palette[i]);
  67. }
  68. fprintf(outFile, "};\n");
  69. fprintf(outFile, "\n");
  70. fclose(outFile);
  71. }
  72. printf("\n");
  73. printf("DONE\n");
  74. return 0;
  75. }