chili2chr.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. int main(int argc, char** argv) {
  6. if(argc<2) {
  7. printf("Usage: %s <input> <output>\nCurrently only 4-to-2-bit supported\n", argv[0]);
  8. }
  9. FILE *in, *out;
  10. size_t in_off = 0, out_off = 0;
  11. uint8_t pixperbyte, mask_shift, mask, depth, planeidx;
  12. uint8_t current_pixel, current_in_tile;
  13. int i,j;
  14. if((in=fopen(argv[1], "rb"))==NULL) {
  15. perror("Could not open input file");
  16. return 1;
  17. }
  18. if((out=fopen(argv[2], "wb"))==NULL) {
  19. perror("Could not open output file");
  20. return 1;
  21. }
  22. size_t fsize, dsize;
  23. fseek(in, 0, SEEK_END);
  24. fsize = ftell(in);
  25. fseek(in, 0, SEEK_SET);
  26. // pixperbyte = 2;
  27. // mask_shift = 4;
  28. // mask = 0x03;
  29. // depth = 2;
  30. // 4->2
  31. // pixperbyte = 2;
  32. // mask_shift = 4;
  33. // mask = 0x0f;
  34. // depth = 4;
  35. // 4->4?
  36. pixperbyte = 1;
  37. mask_shift = 0;
  38. mask = 0xff;
  39. depth = 8;
  40. // 8->8
  41. dsize = fsize * depth / (8/pixperbyte);
  42. uint16_t *obuf;
  43. if((obuf=malloc(dsize))==NULL) {
  44. perror("Could not reserve memory");
  45. fclose(out);
  46. fclose(in);
  47. return 1;
  48. }
  49. memset(obuf, 0, dsize);
  50. while (!feof(in)) {
  51. uint8_t chunk = fgetc(in);
  52. printf("%lX\n", out_off);
  53. for(i=0; i<pixperbyte; i++) {
  54. current_pixel = (in_off*pixperbyte+i)%8;
  55. current_in_tile = (in_off*pixperbyte+i)%64;
  56. if(!current_pixel && in_off) { // after 8 pixels:
  57. out_off++;
  58. }
  59. if(!current_in_tile && in_off) { // after 64 pixels:
  60. out_off += (depth/2-1) * 8;
  61. }
  62. uint8_t bits = (chunk&mask);
  63. chunk >>= mask_shift;
  64. for(planeidx=0; planeidx < depth/2; planeidx++) {
  65. for(j=0; j<2; j++) {
  66. obuf[out_off+planeidx*8] |= ((bits & (1<<(j+2*planeidx))) >> (j+2*planeidx) << ((8*j+7)-current_pixel));
  67. }
  68. }
  69. }
  70. in_off++;
  71. }
  72. free(obuf);
  73. fwrite(obuf, dsize, 1, out);
  74. fclose(out);
  75. fclose(in);
  76. }