Guest User

Untitled

a guest
May 24th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.88 KB | None | 0 0
  1. //lz11.h----------------------------------------------------------------------------------
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6. #include <stdlib.h>
  7. #include <cstdio>
  8. #include <squish.h>
  9. #ifdef _WIN32
  10.     #include <windows.h>
  11. #endif
  12. #include "FreeImage.h"
  13. #include <list>
  14. #include <cmath>
  15. #include <cstring>
  16. using namespace std;
  17.  
  18. typedef uint8_t byte;
  19. typedef uint32_t uint;
  20.  
  21.  
  22. class Stream
  23. {
  24. protected:
  25.     int curReadLoc;
  26.    
  27. public:
  28.     Stream();
  29.    
  30.     byte ReadByte();
  31.     void Read(byte* out, int unused, int amt);
  32.    
  33.     void WriteByte(byte b);
  34.    
  35.     vector<byte> data;
  36. };
  37.  
  38. long LZ11Decompress(Stream& instream, long inLength, Stream& outstream);
  39.  
  40. //lz11.cpp--------------------------------------------------------------------
  41. int ToNDSs32(byte* buffer, int offset)
  42.         {
  43.             return (int)(buffer[offset]
  44.                         | (buffer[offset + 1] << 8)
  45.                         | (buffer[offset + 2] << 16)
  46.                         | (buffer[offset + 3] << 24));
  47.         }
  48.  
  49.         int ToNDSu24(byte* buffer, int offset)
  50.         {
  51.             return (int)(buffer[offset]
  52.                         | (buffer[offset + 1] << 8)
  53.                         | (buffer[offset + 2] << 16));
  54.         }
  55.  
  56.        
  57.         long LZ11Decompress(Stream& instream, long inLength, Stream& outstream)
  58.         {
  59.             ////#region Format definition in NDSTEK style
  60.             /*  Data header (32bit)
  61.                   Bit 0-3   Reserved
  62.                   Bit 4-7   Compressed type (must be 1 for LZ77)
  63.                   Bit 8-31  Size of decompressed data. if 0, the next 4 bytes are decompressed length
  64.                 Repeat below. Each Flag Byte followed by eight Blocks.
  65.                 Flag data (8bit)
  66.                   Bit 0-7   Type Flags for next 8 Blocks, MSB first
  67.                 Block Type 0 - Uncompressed - Copy 1 Byte from Source to Dest
  68.                   Bit 0-7   One data byte to be copied to dest
  69.                 Block Type 1 - Compressed - Copy LEN Bytes from Dest-Disp-1 to Dest
  70.                     If Reserved is 0: - Default
  71.                       Bit 0-3   Disp MSBs
  72.                       Bit 4-7   LEN - 3
  73.                       Bit 8-15  Disp LSBs
  74.                     If Reserved is 1: - Higher compression rates for files with (lots of) long repetitions
  75.                       Bit 4-7   Indicator
  76.                         If Indicator > 1:
  77.                             Bit 0-3    Disp MSBs
  78.                             Bit 4-7    LEN - 1 (same bits as Indicator)
  79.                             Bit 8-15   Disp LSBs
  80.                         If Indicator is 1: A(B CD E)(F GH)
  81.                             Bit 0-3     (LEN - 0x111) MSBs
  82.                             Bit 4-7     Indicator; unused
  83.                             Bit 8-15    (LEN- 0x111) 'middle'-SBs
  84.                             Bit 16-19   Disp MSBs
  85.                             Bit 20-23   (LEN - 0x111) LSBs
  86.                             Bit 24-31   Disp LSBs
  87.                         If Indicator is 0:
  88.                             Bit 0-3     (LEN - 0x11) MSBs
  89.                             Bit 4-7     Indicator; unused
  90.                             Bit 8-11    Disp MSBs
  91.                             Bit 12-15   (LEN - 0x11) LSBs
  92.                             Bit 16-23   Disp LSBs
  93.              */
  94.             //#endregion
  95.  
  96.             long readBytes = 0;
  97.  
  98.             byte type = (byte)instream.ReadByte();
  99.             if (type != 0x11)
  100.             {
  101.                 //throw new InvalidDataException("The provided stream is not a valid LZ-0x11 "
  102.                 //            + "compressed stream (invalid type 0x" + type.ToString("X") + ")");
  103.                 cout << "The provided stream is not a valid LZ-0x11 compressed stream (invalid type " << type << ")" << endl;
  104.                 return 0;
  105.             }
  106.             byte* sizeBytes = new byte[3];
  107.             instream.Read(sizeBytes, 0, 3);
  108.             int decompressedSize = ToNDSu24(sizeBytes, 0);
  109.             readBytes += 4;
  110.             if (decompressedSize == 0)
  111.             {
  112.                 sizeBytes = new byte[4];
  113.                 instream.Read(sizeBytes, 0, 4);
  114.                 decompressedSize = ToNDSs32(sizeBytes, 0);
  115.                 readBytes += 4;
  116.             }
  117.  
  118.             // the maximum 'DISP-1' is still 0xFFF.
  119.             int bufferLength = 0x1000;
  120.             byte* buffer = new byte[bufferLength];
  121.             int bufferOffset = 0;
  122.            
  123.             int currentOutSize = 0;
  124.             int flags = 0, mask = 1;
  125.             while (currentOutSize < decompressedSize)
  126.             {
  127.                 // (throws when requested new flags byte is not available)
  128.                 //#region Update the mask. If all flag bits have been read, get a new set.
  129.                 // the current mask is the mask used in the previous run. So if it masks the
  130.                 // last flag bit, get a new flags byte.
  131.                 if (mask == 1)
  132.                 {
  133.                     if (readBytes >= inLength)
  134.                     {
  135.                         cout << "Not enough data" << endl;
  136.                         return 0;
  137.                         //throw new NotEnoughDataException(currentOutSize, decompressedSize);
  138.                     }
  139.                     flags = instream.ReadByte();
  140.                     readBytes++;
  141.                     if (flags < 0)
  142.                     {
  143.                         cout << "Stream too short" << endl;
  144.                         return 0;
  145.                         //throw new StreamTooShortException();
  146.                     }
  147.                     mask = 0x80;
  148.                 }
  149.                 else
  150.                 {
  151.                     mask >>= 1;
  152.                 }
  153.                 //#endregion
  154.  
  155.                 // bit = 1 <=> compressed.
  156.                 if ((flags & mask) > 0)
  157.                 {
  158.                     // (throws when not enough bytes are available)
  159.                     //#region Get length and displacement('disp') values from next 2, 3 or 4 bytes
  160.  
  161.                     // read the first byte first, which also signals the size of the compressed block
  162.                     if (readBytes >= inLength)
  163.                     {
  164.                         //throw new NotEnoughDataException(currentOutSize, decompressedSize);
  165.                         cout << "Not enough data " << currentOutSize << ", " << decompressedSize << endl;
  166.                         return 0;
  167.                     }
  168.                     int byte1 = instream.ReadByte(); readBytes++;
  169.                     if (byte1 < 0)
  170.                     {
  171.                         //throw new StreamTooShortException();
  172.                         cout << "Stream too short" << endl;
  173.                         return 0;
  174.                     }  
  175.  
  176.                     int length = byte1 >> 4;
  177.                     int disp = -1;
  178.                     if (length == 0)
  179.                     {
  180.                         //#region case 0; 0(B C)(D EF) + (0x11)(0x1) = (LEN)(DISP)
  181.  
  182.                         // case 0:
  183.                         // data = AB CD EF (with A=0)
  184.                         // LEN = ABC + 0x11 == BC + 0x11
  185.                         // DISP = DEF + 1
  186.  
  187.                         // we need two more bytes available
  188.                         if (readBytes + 1 >= inLength)
  189.                         {
  190.                             //throw new NotEnoughDataException(currentOutSize, decompressedSize);
  191.                             cout << "Not enough data " << currentOutSize << ", " << decompressedSize << endl;
  192.                             return 0;
  193.                         }
  194.                         int byte2 = instream.ReadByte(); readBytes++;
  195.                         int byte3 = instream.ReadByte(); readBytes++;
  196.                         if (byte3 < 0)
  197.                         {
  198.                             //throw new StreamTooShortException();
  199.                             cout << "Stream too short" << endl;
  200.                             return 0;
  201.                         }
  202.  
  203.                         length = (((byte1 & 0x0F) << 4) | (byte2 >> 4)) + 0x11;
  204.                         disp = (((byte2 & 0x0F) << 8) | byte3) + 0x1;
  205.  
  206.                         //#endregion
  207.                     }
  208.                     else if (length == 1)
  209.                     {
  210.                         //#region case 1: 1(B CD E)(F GH) + (0x111)(0x1) = (LEN)(DISP)
  211.  
  212.                         // case 1:
  213.                         // data = AB CD EF GH (with A=1)
  214.                         // LEN = BCDE + 0x111
  215.                         // DISP = FGH + 1
  216.  
  217.                         // we need three more bytes available
  218.                         if (readBytes + 2 >= inLength)
  219.                         {
  220.                             //throw new NotEnoughDataException(currentOutSize, decompressedSize);
  221.                             cout << "Not enough data " << currentOutSize << ", " << decompressedSize << endl;
  222.                             return 0;
  223.                         }
  224.                         int byte2 = instream.ReadByte(); readBytes++;
  225.                         int byte3 = instream.ReadByte(); readBytes++;
  226.                         int byte4 = instream.ReadByte(); readBytes++;
  227.                         if (byte4 < 0)
  228.                         {
  229.                             //throw new StreamTooShortException();
  230.                             cout << "Stream too short" << endl;
  231.                             return 0;
  232.                         }
  233.  
  234.                         length = (((byte1 & 0x0F) << 12) | (byte2 << 4) | (byte3 >> 4)) + 0x111;
  235.                         disp = (((byte3 & 0x0F) << 8) | byte4) + 0x1;
  236.  
  237.                         //#endregion
  238.                     }
  239.                     else
  240.                     {
  241.                         //#region case > 1: (A)(B CD) + (0x1)(0x1) = (LEN)(DISP)
  242.  
  243.                         // case other:
  244.                         // data = AB CD
  245.                         // LEN = A + 1
  246.                         // DISP = BCD + 1
  247.  
  248.                         // we need only one more byte available
  249.                         if (readBytes >= inLength)
  250.                         {
  251.                             //throw new NotEnoughDataException(currentOutSize, decompressedSize);
  252.                             cout << "Not enough data " << currentOutSize << ", " << decompressedSize << endl;
  253.                             return 0;
  254.                         }
  255.                         int byte2 = instream.ReadByte();
  256.                         readBytes++;
  257.                         if (byte2 < 0)
  258.                         {
  259.                             //throw new StreamTooShortException();
  260.                             cout << "Stream too short" << endl;
  261.                             return 0;
  262.                         }
  263.  
  264.                         length = ((byte1 & 0xF0) >> 4) + 0x1;
  265.                         disp = (((byte1 & 0x0F) << 8) | byte2) + 0x1;
  266.  
  267.                         //#endregion
  268.                     }
  269.  
  270.                     if (disp > currentOutSize)
  271.                     {
  272.                         //throw new InvalidDataException("Cannot go back more than already written. "
  273.                         //        + "DISP = " + disp + ", #written bytes = 0x" + currentOutSize.ToString("X")
  274.                         //        + " before 0x" + instream.Position.ToString("X") + " with indicator 0x"
  275.                         //        + (byte1 >> 4).ToString("X"));
  276.                         cout << "Invalid data: Cannot go back more than already written." << endl;
  277.                         return 0;
  278.                     }
  279.                     //#endregion
  280.  
  281.                     int bufIdx = bufferOffset + bufferLength - disp;
  282.                     for (int i = 0; i < length; i++)
  283.                     {
  284.                         byte next = buffer[bufIdx % bufferLength];
  285.                         bufIdx++;
  286.                         outstream.WriteByte(next);
  287.                         buffer[bufferOffset] = next;
  288.                         bufferOffset = (bufferOffset + 1) % bufferLength;
  289.                     }
  290.                     currentOutSize += length;
  291.                 }
  292.                 else
  293.                 {
  294.                     if (readBytes >= inLength)
  295.                     {
  296.                         //throw new NotEnoughDataException(currentOutSize, decompressedSize);
  297.                         cout << "Not enough data " << currentOutSize << ", " << decompressedSize << endl;
  298.                         return 0;
  299.                     }
  300.                     int next = instream.ReadByte(); readBytes++;
  301.                     if (next < 0)
  302.                     {
  303.                         //throw new StreamTooShortException();
  304.                         cout << "Stream too short" << endl;
  305.                         return 0;
  306.                     }
  307.  
  308.                     outstream.WriteByte((byte)next);
  309.                     currentOutSize++;
  310.                     buffer[bufferOffset] = (byte)next;
  311.                     bufferOffset = (bufferOffset + 1) % bufferLength;
  312.                 }
  313.             }
  314.  
  315.             if (readBytes < inLength)
  316.             {
  317.                 // the input may be 4-byte aligned.
  318.                 if ((readBytes ^ (readBytes & 3)) + 4 < inLength)
  319.                 {
  320.                     //throw new TooMuchInputException(readBytes, inLength);
  321.                     //cout << "Too much input " << readBytes << ", " << inLength << endl;
  322.                     //return 0;
  323.                 }
  324.             }
  325.  
  326.             return decompressedSize;
  327.         }
  328.  
  329. Stream::Stream()
  330. {
  331.     curReadLoc = 0;
  332. }
  333.  
  334. byte Stream::ReadByte()
  335. {
  336.     byte ret = data.data()[curReadLoc];
  337.     curReadLoc++;
  338.     return ret;
  339. }
  340.  
  341. void Stream::Read(byte* out, int unused, int amt)
  342. {
  343.     for(int i = 0; i < amt; i++)
  344.     {
  345.         out[i] = ReadByte();
  346.     }
  347. }
  348.  
  349. void Stream::WriteByte(byte b)
  350. {
  351.     data.push_back(b);
  352. }
  353.  
  354.  
  355.  
  356. //main.cpp-------------------------------------------------------------------
  357. #include <iostream>
  358. #include <vector>
  359. #include <string>
  360. #include <sstream>
  361. #include <stdlib.h>
  362. #include <cstdio>
  363. #include <squish.h>
  364. #ifdef _WIN32
  365.     #include <windows.h>
  366. #endif
  367. #include "FreeImage.h"
  368. #include <list>
  369. #include <cmath>
  370. #include <cstring>
  371. #include "lz11.h"
  372. using namespace std;
  373.  
  374. //extern unsigned char* LZX_Decode(char* input, int inputLength, int* outputLength);
  375. //extern void lz11Decompress(const uint8_t *src, uint8_t *dst, int size);
  376.  
  377. bool g_bPieceTogether;
  378.  
  379. typedef struct
  380. {
  381.     uint32_t numImages;
  382.     uint32_t _EOF;
  383.     uint32_t _pieceOffset;
  384.     uint32_t _imageOffset;
  385.     uint16_t imageW;
  386.     uint16_t imageH;
  387.     uint8_t unknown0[8];
  388.     uint16_t numPieces;
  389.     uint8_t unknown1[2];
  390.     uint32_t _unkOffset1;
  391.     uint32_t _unkOffset2;
  392.     uint32_t _unkOffset3;
  393. } anbHeader;
  394.  
  395. typedef struct
  396. {
  397.     float x;
  398.     float y;
  399. } Vec2;
  400.  
  401. typedef struct
  402. {
  403.     Vec2 topLeft;
  404.     Vec2 topLeftUV;
  405.     Vec2 topRight;
  406.     Vec2 topRightUV;
  407.     Vec2 bottomRight;
  408.     Vec2 bottomRightUV;
  409.     Vec2 bottomLeft;
  410.     Vec2 bottomLeftUV;
  411. } piece;
  412.  
  413. typedef struct
  414. {
  415.     uint32_t x;
  416.     uint32_t y;
  417. } fakeVec2;
  418.  
  419. typedef struct
  420. {
  421.     fakeVec2 topLeft;
  422.     fakeVec2 topLeftUV;
  423.     fakeVec2 topRight;
  424.     fakeVec2 topRightUV;
  425.     fakeVec2 bottomRight;
  426.     fakeVec2 bottomRightUV;
  427.     fakeVec2 bottomLeft;
  428.     fakeVec2 bottomLeftUV;
  429. } fakePiece;
  430.  
  431. uint16_t byteSwap16(uint16_t byte)
  432. {
  433.     uint16_t hibyte = (byte & 0xff00) >> 8;
  434.     uint16_t lobyte = (byte & 0x00ff);
  435.     return lobyte << 8 | hibyte;
  436. }
  437.  
  438. uint32_t byteSwap32(uint32_t val)
  439. {
  440.     val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
  441.     return (val << 16) | (val >> 16);
  442. }
  443.  
  444. float toFloat(uint32_t val)
  445. {
  446.     float f;
  447.     memcpy(&f, &val, sizeof(float));
  448.     return f;
  449. }
  450.  
  451. float byteSwapFloat(uint32_t val)
  452. {
  453.     val = byteSwap32(val);
  454.     return toFloat(val);
  455. }
  456.  
  457. Vec2 byteSwapVec2(fakeVec2 v)
  458. {
  459.     Vec2 ret;
  460.     ret.x = byteSwapFloat(v.x);
  461.     ret.y = byteSwapFloat(v.y);
  462.     return ret;
  463. }
  464.  
  465. piece byteSwapPiece(fakePiece p)
  466. {
  467.     piece ret;
  468.     ret.topLeft = byteSwapVec2(p.topLeft);
  469.     ret.topLeftUV = byteSwapVec2(p.topLeftUV);
  470.     ret.topRight = byteSwapVec2(p.topRight);
  471.     ret.topRightUV = byteSwapVec2(p.topRightUV);
  472.     ret.bottomRight = byteSwapVec2(p.bottomRight);
  473.     ret.bottomRightUV = byteSwapVec2(p.bottomRightUV);
  474.     ret.bottomLeft = byteSwapVec2(p.bottomLeft);
  475.     ret.bottomLeftUV = byteSwapVec2(p.bottomLeftUV);
  476.     return ret;
  477. }
  478.  
  479. FIBITMAP* imageFromPixels(uint8_t* imgData, uint32_t width, uint32_t height)
  480. {
  481.     //return FreeImage_ConvertFromRawBits(imgData, width, height, width*4, 32, 0xFF0000, 0x00FF00, 0x0000FF, true); //Doesn't seem to work
  482.     FIBITMAP* curImg = FreeImage_Allocate(width, height, 32);
  483.     FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(curImg);
  484.     if(image_type == FIT_BITMAP)
  485.     {
  486.         int curPos = 0;
  487.         unsigned pitch = FreeImage_GetPitch(curImg);
  488.         BYTE* bits = (BYTE*)FreeImage_GetBits(curImg);
  489.         bits += pitch * height - pitch;
  490.         for(int y = height-1; y >= 0; y--)
  491.         {
  492.             BYTE* pixel = (BYTE*)bits;
  493.             for(int x = 0; x < width; x++)
  494.             {
  495.                 pixel[FI_RGBA_RED] = imgData[curPos++];
  496.                 pixel[FI_RGBA_GREEN] = imgData[curPos++];
  497.                 pixel[FI_RGBA_BLUE] = imgData[curPos++];
  498.                 pixel[FI_RGBA_ALPHA] = imgData[curPos++];
  499.                 pixel += 4;
  500.             }
  501.             bits -= pitch;
  502.         }
  503.     }
  504.     return curImg;
  505. }
  506.  
  507. RGBQUAD makeColor(uint8_t a, uint8_t r, uint8_t b, uint8_t g)
  508. {
  509.     //RGBQUAD quad = { r, g, b, a };
  510.     RGBQUAD quad = { r, a, g, b };
  511.     return quad;
  512.     //return (g << 24) | (b << 16) | (a << 8) | (r);
  513. }
  514.  
  515. FIBITMAP* makeTexture(uint8_t* bytes, int byteslen, int width, int height)
  516. {
  517.     //byteslen = byteslen / 4;
  518.     uint8_t* buffer1 = new uint8_t[byteslen / 2];
  519.     uint8_t* buffer2 = new uint8_t[byteslen / 2];
  520.  
  521.     int i1 = 0, i2 = 0;
  522.     int pointer = 0;
  523.  
  524.     while(i1 + i2 < byteslen)
  525.     {
  526.         for(int j = 0; j < 32; j++)
  527.         {
  528.             buffer1[i1++] = bytes[pointer++];
  529.         }
  530.         for(int k = 0; k < 32; k++)
  531.         {
  532.             buffer2[i2++] = bytes[pointer++];
  533.         }
  534.     }
  535.     pointer = 0;
  536.  
  537.     //BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  538.     FIBITMAP* img = FreeImage_Allocate(width, height, 32);
  539.  
  540.     int x = 0, y = 0;
  541.     i1 = 0;
  542.     i2 = 0;
  543.  
  544.     while(i1 + i2 < byteslen)
  545.     {
  546.         for(int h = 0; h < 4; h++)
  547.         {
  548.             for(int w = 0; w < 4; w++)
  549.             {
  550.                 //img.setRGB(x + w, y + h, makeColor(buffer2[i2++], buffer2[i2++], buffer1[i1++ + 1], buffer1[i1++ - 1]));
  551.                 RGBQUAD colorQuad = makeColor(buffer2[i2++], buffer2[i2++], buffer1[i1++ + 1], buffer1[i1++ - 1]);
  552.                 FreeImage_SetPixelColor(img, x + w, y + h, &colorQuad);
  553.             }
  554.         }
  555.         x += 4;
  556.         if(x >= width)
  557.         {
  558.             x = 0;
  559.             y += 4;
  560.         }
  561.     }
  562.     delete[] buffer1;
  563.     delete[] buffer2;
  564.     return img;
  565. }
  566.  
  567. void splitImages(const char* cFilename)
  568. {
  569.     vector<uint8_t> vData;
  570.     FILE* fh = fopen( cFilename, "rb" );
  571.     if(fh == NULL)
  572.     {
  573.         cerr << "Unable to open input file " << cFilename << endl;
  574.         return;
  575.     }
  576.     fseek( fh, 0, SEEK_END );
  577.     size_t fileSize = ftell( fh );
  578.     fseek( fh, 0, SEEK_SET );
  579.     vData.reserve( fileSize );
  580.     size_t amt = fread( vData.data(), fileSize, 1, fh );
  581.     fclose( fh );
  582.     cout << "Splitting images from file " << cFilename << endl;
  583.    
  584.     //Figure out what we'll be naming the images
  585.     string sName = cFilename;
  586.     //First off, strip off filename extension
  587.     size_t namepos = sName.find(".anb");
  588.     if(namepos != string::npos)
  589.         sName.erase(namepos);
  590.     //Next, strip off any file path before it
  591.     namepos = sName.rfind('/');
  592.     if(namepos == string::npos)
  593.         namepos = sName.rfind('\\');
  594.     if(namepos != string::npos)
  595.         sName.erase(0, namepos+1);
  596.    
  597.     anbHeader ANBh;
  598.     memcpy(&ANBh, &(vData.data()[0]), sizeof(anbHeader));
  599.    
  600.     ANBh.numImages = byteSwap32(ANBh.numImages);
  601.     ANBh._EOF = byteSwap32(ANBh._EOF);
  602.     ANBh._pieceOffset = byteSwap32(ANBh._pieceOffset);
  603.     ANBh._imageOffset = byteSwap32(ANBh._imageOffset);
  604.     ANBh.imageH = byteSwap16(ANBh.imageH);
  605.     ANBh.imageW = byteSwap16(ANBh.imageW);
  606.     ANBh.numPieces = byteSwap16(ANBh.numPieces);
  607.    
  608.     //cout << ANBh.numImages << ", " << ANBh._EOF << ", " << ANBh._pieceOffset << ", " << ANBh._imageOffset << ", " << ANBh.imageW << ", " << ANBh.imageH << ", " << ANBh.numPieces << endl;
  609.    
  610.     //cout << toFloat(0xb0c10000) << endl;
  611.    
  612.     for(uint32_t j = 0; j < ANBh.numPieces*ANBh.numImages; j++)
  613.     {
  614.         piece p;
  615.         fakePiece f;
  616.         memcpy(&f, &(vData.data()[ANBh._pieceOffset+j*sizeof(fakePiece)]), sizeof(fakePiece));
  617.        
  618.         p = byteSwapPiece(f);
  619.         //Store our maximum values, so we know how large the image is
  620.         //cout << "Piece: "<< endl;
  621.         //cout << p.topLeft.x << ", " << p.topLeft.y << ", " << p.topLeftUV.x  << ", " << p.topLeftUV.y << ", " << p.bottomRight.x << ", " << p.bottomRight.y << ", " << p.bottomRightUV.x << ", " << p.bottomRightUV.y << endl;
  622.        
  623.         //Sanity check: Skip over piecing if there's only one piece total that fills the whole thing
  624.         //if(pd.numPieces == 1 && p.topLeftUV.x == 0.0 && p.topLeftUV.y == 0.0 && p.bottomRightUV.x == 1.0 && p.bottomRightUV.y == 1.0) bPieceTogether = false;
  625.        
  626.         //pieces.push_back(p);
  627.     }
  628.    
  629.     //Decompress image data
  630.     //int outputLength = ANBh.imageW * ANBh.imageH * 4;
  631.     //uint8_t img_data[outputLength];// = LZX_Decode((char*)&(vData.data()[ANBh._imageOffset]), ANBh._EOF - ANBh._imageOffset, &outputLength);
  632.     //lz11Decompress(&(vData.data()[ANBh._imageOffset]), img_data, ANBh._EOF - ANBh._imageOffset);
  633.     Stream in;
  634.     Stream out;
  635.     //in.data = vData;
  636.     in.data.reserve(ANBh.imageW * ANBh.imageH * 4);
  637.     for(int i = ANBh._imageOffset; i < fileSize; i++)
  638.     {
  639.         in.data.push_back(vData.data()[i]);
  640.     }
  641.     cout << in.data.size() << endl;
  642.     cout << ANBh._EOF - ANBh._imageOffset << endl;
  643.     int outputLength = LZ11Decompress(in, in.data.size(), out);
  644.     if(outputLength != ANBh.imageW * ANBh.imageH * 4)
  645.         cout << "Length mismatch" << endl;
  646.        
  647.     //FILE* fpp = fopen( "blob_anvil.anb.data", "rb" );
  648.     //if(fpp == NULL)
  649.     //{
  650.     //  cerr << "Unable to open input file " << cFilename << endl;
  651.     //  return;
  652.     //}
  653.     //fseek(fpp, 0, SEEK_END);
  654.     //size_t totalSz = ftell(fpp);
  655.     //fseek(fpp, 0, SEEK_SET);
  656.     //uint8_t* res = new uint8_t[ANBh.imageW * ANBh.imageH * 4];
  657.     //memset(res, 0, ANBh.imageW * ANBh.imageH * 4);
  658.     //vData.reserve(totalSz);
  659.     //fread(res, totalSz, 1, fpp);
  660.     //fclose(fpp);
  661.    
  662.     //cout << "Length: " << outputLength << endl;
  663.     //FIBITMAP* img = makeTexture(res, ANBh.imageW * ANBh.imageH * 4, ANBh.imageW, ANBh.imageH);
  664.     FIBITMAP* img = makeTexture((uint8_t*)out.data.data(), outputLength, ANBh.imageW, ANBh.imageH);
  665.     //FIBITMAP* img = imageFromPixels((uint8_t*)img_data, ANBh.imageW, ANBh.imageH);
  666.     ostringstream oss;
  667.     oss << "output/" << sName << '_' << 1 << ".png";
  668.     cout << "Saving " << oss.str() << endl;
  669.     FreeImage_Save(FIF_PNG, img, oss.str().c_str());
  670.     //FILE* fOut = fopen(oss.str().c_str(), "wb");
  671.     //fwrite(out.data.data(), outputLength, 1, fOut);
  672.     //fclose(fOut);
  673.     //delete[] res;
  674.     //free(img_data);
  675.     //FreeImage_Unload(img);
  676. }
  677.  
  678. int main(int argc, char** argv)
  679. {
  680.     g_bPieceTogether = true;
  681.     FreeImage_Initialise();
  682. #ifdef _WIN32
  683.     CreateDirectory(TEXT("output"), NULL);
  684. #else
  685.     int result = system("mkdir -p output");
  686. #endif
  687.     list<string> sFilenames;
  688.     //Parse commandline
  689.     for(int i = 1; i < argc; i++)
  690.     {
  691.         string s = argv[i];
  692.         if(s == "-nopiece")
  693.             g_bPieceTogether = false;
  694.         else
  695.             sFilenames.push_back(s);
  696.     }
  697.    
  698.     //Decompress ANB files
  699.     for(list<string>::iterator i = sFilenames.begin(); i != sFilenames.end(); i++)
  700.         splitImages((*i).c_str());
  701.     FreeImage_DeInitialise();
  702.     return 0;
  703. }
Advertisement
Add Comment
Please, Sign In to add comment