Advertisement
TheFastFish

kekeee

Jul 18th, 2015
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. //GRP Eater
  2. //(C) 2015 Alex Craik
  3. //A little project I made because who needs PTCUtilities?
  4. #include <iostream>
  5. #include <fstream>
  6. bool nopalette;
  7. char grp[49152];
  8. char col[256*3];
  9. char bin[49152];
  10. int j;
  11. int i;
  12. int v;
  13. int u;
  14. int y;
  15. int x;
  16. using namespace std;
  17.  
  18. int main(int argc, char* argv[]) {
  19.     //initial startup routines
  20.     //cout messages, argument processing, errors and whatnot
  21.     cout << "GRP Eater\n(C) 2015 Alex Craik\n";
  22.     if (argc == 1) {
  23.         cout << "Creates PNG images of GRP files.\n";
  24.         cout << "Accepts COL files as palettes (optional argument).\n";
  25.         cout << "Usage: grpeat InGRPFile [InCOLFile] OutPNGFile\n";
  26.         return 10;
  27.     }
  28.     if (argc == 2) {
  29.         cout << "Error 11\nNo output file specified.\n";
  30.         return 11;
  31.     }
  32.     char* ingrp = argv[1];
  33.     char* incol;
  34.     char* outpng;
  35.     if (argc == 3) {
  36.         outpng = argv[2];
  37.         nopalette = true;
  38.     } else {
  39.         incol = argv[2];
  40.         outpng = argv[3];
  41.     }
  42.  
  43.     fstream color;
  44.     if (nopalette) {
  45.         color.open("DEFLTCOL.PTC", ios::in | ios::binary);
  46.         if (!color.is_open()) {
  47.             cout << "Error 21\nWhere's DEFLTCOL.PTC? Did you delete it? I put it there for a reason, you know.\n";
  48.             return 21;
  49.         }
  50.     } else {
  51.         color.open(incol, ios::in | ios::binary);
  52.         if (!color.is_open()) {
  53.             cout << "Error 22\nCould not open given COL file.\n";
  54.             return 22;
  55.         }
  56.     }
  57.    
  58.     char four_bytes[5];
  59.     four_bytes[4] = '\0';
  60.     char colbytes[2];
  61.     char col_filename[8];
  62.     char md5_hash[16];
  63.     char type_string[13];
  64.     type_string[12] = '\0';
  65.     //data integrity check on COL
  66.     color.seekg(0);
  67.     color.read(four_bytes, 4);
  68.     if (strcmp(four_bytes, "PX01")) {
  69.         cout << "Error 30\nCOL file is of wrong type or damaged.\n";
  70.         cout << four_bytes;
  71.         return 30;
  72.     }
  73.     color.read(four_bytes, 4);
  74.     if (strcmp(four_bytes, "\x00\x00\x02\x0C")) {
  75.         cout << "Error 30\nCOL file is of wrong type or damaged.\n";
  76.         cout << four_bytes;
  77.         return 30;
  78.     }
  79.     color.read(four_bytes, 4);
  80.     if (strcmp(four_bytes, "\x05\x00\x00\x00")) {
  81.         cout << "Error 30\nCOL file is of wrong type or damaged.\n";
  82.         cout << four_bytes;
  83.         return 30;
  84.     }
  85.     color.read(col_filename, 8); //because why not, I might use it as metadata
  86.     color.read(md5_hash, 16); //won't be doing md5 checks yet
  87.     color.read(type_string, 12);
  88.     if (strcmp(type_string, "PETC0100RCOL")) {
  89.         cout << "Error 30\nCOL file is of wrong type or damaged.\n";
  90.         return 30;
  91.     }
  92.  
  93.     //get the colors
  94.     char red;
  95.     char green;
  96.     char blue;
  97.     for (i = 0; i < 256; i++) {
  98.         color.read(colbytes, 2);
  99.         red = colbytes[0] & 0x1f;
  100.         green = ((colbytes[0] & 0xe0) << 3) | ((colbytes[1] & 0x80) >> 2) | (colbytes[1] & 0x02); //finagle with this until colors come out right
  101.         blue = colbytes[1] & 0x7c;
  102.         //right-shift to get the 8-bit equivalents
  103.         col[i * 3] = red << 3;
  104.         col[i * 3 + 1] = green << 2;
  105.         col[i * 3 + 2] = blue << 3;
  106.     }
  107.     color.close();
  108.  
  109.     fstream input;
  110.     input.open(ingrp, ios::in | ios::binary);
  111.     if (!input.is_open()) {
  112.         cout << "Error 20\nCould not open given GRP file.\n";
  113.         return 20;
  114.     }
  115.  
  116.     //data integrity check on GRP
  117.     //do it later, just get to the meat-and-potatoes
  118.     input.seekg(48);
  119.     input.read(grp, 49152); //the whole GRP
  120.     int counter;
  121.     //now we eat the GRP data and turn it into something useable
  122.     //GRP has a crazy structure so I'm only doing this out of convenience
  123.     for (j = 0; j < 3; j++) {
  124.         for (i = 0; i < 4; i++) {
  125.             for (v = 0; v < 8; v++) {
  126.                 for (u = 0; u < 8; u++) {
  127.                     for (y = 0; y < 8; y++) {
  128.                         for (x = 0; x < 8; x++) {
  129.                             bin[x + (y * 8) + (u * 64) + (v * 512) + (i * 4096) + (j * 16384)] = grp[counter];
  130.                             counter++;
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     input.close();
  138.  
  139.     return 0;
  140. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement