_convert.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. int main(int argc, char* argv[]) {
  6. const unsigned char* pp = NULL;
  7. uint32_t pix = 0;
  8. int bit = 0;
  9. uint8_t b = 0;
  10. uint8_t bcnt = 0;
  11. unsigned int lcnt = 0;
  12. static const int lmax = 16; // max hex values per line
  13. uint8_t* buf = NULL;
  14. uint8_t* bp = NULL;
  15. unsigned int blen = 0;
  16. uint8_t* cmp = NULL;
  17. uint8_t* cp = NULL;
  18. unsigned int clen = 0;
  19. uint8_t ctag = 0xFF;
  20. uint32_t tag[256] = {0};
  21. uint32_t tmax = UINT32_MAX;
  22. unsigned int x, y, z;
  23. const char* name = argv[1];
  24. FILE* fh = fopen(argv[2], "wb");
  25. uint32_t white = 0xFF;
  26. int rv = 0; // assume success
  27. // allocate buffers
  28. blen = ((img.w * img.h) + 0x7) >> 3;
  29. bp = (buf = calloc(blen + 1, 1));
  30. cp = (cmp = calloc(blen + 4, 1));
  31. // sanity check
  32. if(!fh || !buf || !cmp) {
  33. printf("! fopen() or malloc() fail.\n");
  34. rv = 255;
  35. goto bail;
  36. }
  37. // Find white value
  38. for(x = 1; x < img.bpp; x++) white = (white << 8) | 0xFF;
  39. // build bit pattern
  40. // create the comment as we go
  41. for(pp = img.b, y = 0; y < img.h; y++) {
  42. fprintf(fh, "// ");
  43. for(x = 0; x < img.w; x++) {
  44. // read pixel
  45. for(pix = 0, z = 0; z < img.bpp; pix = (pix << 8) | *pp++, z++)
  46. ;
  47. // get bit and draw
  48. if(pix < white) {
  49. b = (b << 1) | 1;
  50. fprintf(fh, "##");
  51. } else {
  52. b <<= 1;
  53. fprintf(fh, "..");
  54. }
  55. // got byte
  56. if((++bcnt) == 8) {
  57. *bp++ = b;
  58. tag[b]++;
  59. bcnt = (b = 0);
  60. }
  61. }
  62. fprintf(fh, "\n");
  63. }
  64. fprintf(fh, "\n");
  65. // padding
  66. if(bcnt) {
  67. b <<= (bcnt = 8 - bcnt);
  68. *bp++ = b;
  69. tag[b]++;
  70. }
  71. // Kill the compression
  72. *bp = ~bp[-1]; // https://youtube.com/clip/Ugkx-JZIr16hETy7hz_H6yIdKPtxVe8C5w_V
  73. // Byte run length compression
  74. // Find a good tag
  75. for(x = 0; tmax && (x < 256); x++) {
  76. if(tag[x] < tmax) {
  77. tmax = tag[x];
  78. ctag = x;
  79. }
  80. }
  81. // compress the data
  82. for(bp = buf, x = 0; (clen < blen) && (x < blen); x++) {
  83. // need at least 4 the same to be worth it
  84. // must compress tag (if it occurs)
  85. if((bp[x] == bp[x + 1]) && (bp[x] == bp[x + 2]) && (bp[x] == bp[x + 3]) ||
  86. (bp[x] == ctag)) {
  87. for(y = 1; (y < 255) && (bp[x] == bp[x + y]); y++)
  88. ;
  89. *cp++ = ctag; // tag
  90. *cp++ = y; // length
  91. *cp++ = bp[x]; // byte
  92. x += y - 1;
  93. clen += 3;
  94. } else {
  95. *cp++ = bp[x];
  96. clen++;
  97. }
  98. }
  99. // create struct
  100. fprintf(fh, "#include \"images.h\"\n\n");
  101. fprintf(fh, "const image_t img_%s = { %d, %d, ", name, img.w, img.h);
  102. if(clen < blen) { // dump compressed?
  103. fprintf(
  104. fh,
  105. "true, %d, 0x%02X, { // orig:%d, comp:%.2f%%\n\t",
  106. clen,
  107. ctag,
  108. blen,
  109. 100.0 - ((clen * 100.0) / blen));
  110. for(x = 0; x < clen; x++)
  111. if(x == clen - 1)
  112. fprintf(fh, "0x%02X\n}};\n", cmp[x]);
  113. else
  114. fprintf(fh, "0x%02X%s", cmp[x], (!((x + 1) % 16)) ? ",\n\t" : ", ");
  115. } else { // dump UNcompressed
  116. fprintf(fh, "false, %d, 0, {\n\t", blen);
  117. for(x = 0; x < blen; x++)
  118. if(x == blen - 1)
  119. fprintf(fh, "0x%02X\n}};\n", buf[x]);
  120. else
  121. fprintf(fh, "0x%02X%s", buf[x], (!((x + 1) % 16)) ? ",\n\t" : ", ");
  122. }
  123. bail:
  124. if(fh) fclose(fh);
  125. if(buf) free(buf);
  126. if(cmp) free(cmp);
  127. return rv;
  128. }