palreorder.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. int main(int argc, char **argv) {
  4. if(argc<3) {
  5. fprintf(stderr, "Usage: %s <infile> <outfile>\n", argv[0]);
  6. return 1;
  7. }
  8. FILE *in, *out;
  9. if((in=fopen(argv[1], "rb"))==NULL) {
  10. perror("Could not open input file");
  11. return 1;
  12. }
  13. if((out=fopen(argv[2], "wb"))==NULL) {
  14. perror("Could not open output file");
  15. return 1;
  16. }
  17. uint16_t palette_src[256];
  18. uint16_t palette_tgt[256];
  19. uint16_t i=0;
  20. fread(palette_src, 2, 256, in);
  21. for(i=0; i<256; i++) {
  22. uint8_t tgt_index=i;
  23. if(tgt_index) {
  24. if(tgt_index>224) { // move upper colors to start
  25. tgt_index-=224;
  26. } else if(tgt_index==224) { // remap 224 to 32, not 0
  27. tgt_index=32;
  28. } else { // shift colors, leave gap 176-191
  29. // relocate 0xd0-0xdf (which would be
  30. // remapped to 0x00-0x0f) to 0xb0-0xbf
  31. tgt_index+=32;
  32. if(tgt_index>=176) {
  33. tgt_index+=16;
  34. if(tgt_index<16) tgt_index=0xb0+tgt_index;
  35. }
  36. }
  37. }
  38. palette_tgt[tgt_index] = palette_src[i];
  39. }
  40. fwrite(palette_tgt, 2, 256, out);
  41. fclose(out);
  42. fclose(in);
  43. return 0;
  44. }