Exemple #1
0
        public void loadFileAsGFNT(string filename)
        {
            #region typedef

            /*
             * typedef struct {
             *   char* magHeader; // GFNT
             *   DWORD fileSize; // best to ignore; is sometimes size up to first palette
             *   char* version; // '1.02', '1.01' or '1.00' best to ignore
             *   DWORD Imagebpp;
             *   DWORD imageWidthSc; // for real width; 8 << val
             *   DWORD imageHeightSc; // for real height; 8 << val
             *   DWORD nPals;
             *   byte* image;
             *   byte* palette; // seems to be 2Bppal always.
             * } GFNTFile
             */
            #endregion

            BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open));
            br.ReadInt32(); // we should already know the magic header is GFNT
            br.ReadInt32(); // we don't need to know the filesize, and it's not even correct for multipalettes

            br.ReadInt32(); // ignore version-string

            int imbpp    = br.ReadInt32();
            int imwidth  = 8 << br.ReadInt32();
            int imheight = 8 << br.ReadInt32();
            int nPals    = br.ReadInt32();

            int nBytesForIm = imwidth * imheight;

            switch (imbpp)
            {
            case 1: nBytesForIm /= 8; break;

            case 2: nBytesForIm /= 4; break;

            case 3: nBytesForIm /= 2; break;

            case 4: nBytesForIm *= 1; break;

            case 5: nBytesForIm *= 2; break;

            case 6: nBytesForIm *= 1; imbpp = 4; break;

            case 7: nBytesForIm *= 4; break;

            default: MainWindow.ShowError("Possibly invalid GFNT file: unknown GraphicsFormat " + imbpp); br.Close(); return;
            }

            this.Data = br.ReadBytes(nBytesForIm);

            graphFormat = (GraphicsFormat)imbpp;
            isBigEndian = true;
            tiled       = false;
            width       = (uint)imwidth;
            height      = (uint)imheight;

            br.Close();
        }
Exemple #2
0
        public void loadFileAsGFNT(string filename)
        {
            #region typedef

            /*
             * typedef struct {
             *   char* magHeader; // GFNT
             *   DWORD fileSize; // best to ignore; is sometimes size up to first palette
             *   char* version; // '1.02', '1.01' or '1.00' best to ignore
             *   DWORD Imagebpp;
             *   DWORD imageWidthSc; // for real width; 8 << val
             *   DWORD imageHeightSc; // for real height; 8 << val
             *   DWORD nPals;
             *   byte* image;
             *   byte* palette; // seems to be 2Bppal always.
             * } GFNTFile
             */
            #endregion

            BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open));
            br.ReadInt32(); // we should already know the magic header is GFNT
            br.ReadInt32(); // we don't need to know the filesize, and it's not even correct for multipalettes

            br.ReadInt32(); // ignore version-string

            int imbpp    = br.ReadInt32();
            int imwidth  = 8 << br.ReadInt32();
            int imheight = 8 << br.ReadInt32();
            int nPals    = br.ReadInt32();

            int nBytesForIm  = imwidth * imheight;
            int nBytesForPal = 2;

            switch (imbpp)
            {
            case 1: nBytesForIm /= 8; nBytesForPal *= 2; break;

            case 2: nBytesForIm /= 4; nBytesForPal *= 4; break;

            case 3: nBytesForIm /= 2; nBytesForPal *= 16; break;

            case 4: nBytesForIm *= 1; nBytesForPal *= 256; break;

            case 5: nBytesForIm *= 2; nBytesForPal = 0; break;

            case 6: nBytesForIm *= 1; imbpp = 4; nBytesForPal *= 256; break;

            case 7: nBytesForIm *= 4; nBytesForPal = 0; break;

            default: MainWindow.ShowError("Possibly invalid GFNT file: unknown GraphicsFormat " + imbpp); br.Close(); return;
            }

            br.BaseStream.Seek(nBytesForIm, SeekOrigin.Current);
            byte[] dt = br.ReadBytes(nBytesForPal);
            this.Data = dt;
            PalFormat = PaletteFormat.FORMAT_2BPP;
            alphaSettings.Location    = AlphaLocation.START;
            alphaSettings.IgnoreAlpha = true;
            PalOrder = PaletteOrder.BGR;

            br.Close();
        }
Exemple #3
0
        public void loadFileAsNCGR(String filename)
        {
            #region typedef

            /*
             * typedef struct {
             *  char* magHeader; // 4 bytes: RGCN
             *  DWORD magConst; // FF FE 01 01
             *  DWORD fileSize;
             *  WORD headerSize; // should be 10 (i.e. 10 00)
             *  WORD nSections; // should be 1 or 2 (i.e. 01 00 or 02 00)
             *  CHARSection charSection;
             * } NCGRFile
             *
             * typedef struct {
             *  char* magHeader; // 4 bytes: RAHC
             *  DWORD sectSize; // section size; should be fileSize - NCGRHeader.headerSize = fileSize - 0x10
             *  WORD height; // FF FF if tiled image. otherwise is height / 8
             *  WORD width; // FF FF if tiled image. otherwise is width / 8
             *  DWORD palType; // 04 00 -> 8bpp, 03 00 -> 4bpp
             *  WORD linearFlag; // 1 => linear, 0 => tiled
             *  WORD unkn2; // ???? part of linearFlag?
             *  WORD unkn3; // ???? Seen 01 01 here with elebits NPCs
             *  WORD unkn4; // ????
             *  DWORD imageDataSize; // length of imageData (in bytes)
             *  DWORD unkn5; // ???? seems to be constant 18 00 00 00
             *  byte* imageData;
             * } CHARSection
             *
             */
            #endregion
            BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open));
            // skip 4 bytes; they are the already checked magic header RGCN (or NCGR)
            br.ReadInt32();

            int val = br.ReadInt32();
            if (val != 0x0101FEFF && val != 0x0100FEFF)
            {
                MainWindow.ShowError("Given file " + filename + " is not a valid NCGR file.\n It does not have the magic constant 0x0101FEFF or 0100FEFF at 0x04");
                //return;
            }
            int fileSize   = br.ReadInt32();
            int headerSize = br.ReadInt16();
            int nSections  = br.ReadInt16();
            if (nSections > 2 || nSections == 0)
            {
                MainWindow.ShowError("Given file " + filename + " is not a valid NCGR file or of an unsupported type.\n The amount of sections is invalid (" + nSections + ")");
                return;
            }
            // should now be at CHAR section
            if (br.ReadChar() != 'R' || br.ReadChar() != 'A' || br.ReadChar() != 'H' || br.ReadChar() != 'C')
            {
                MainWindow.ShowError("Given file " + filename + " is not a valid NCGR file or of an unsupported type.\n The CHAR section does not follow the NCGR header");
                return;
            }

            int charSize = br.ReadInt32();
            int h = br.ReadInt16(), w = br.ReadInt16();

            int ptype = br.ReadInt32();
            switch (ptype)
            {
            case 6: GraphFormat = GraphicsFormat.FORMAT_32BPP; break;

            case 5: GraphFormat = GraphicsFormat.FORMAT_16BPP; break;

            case 4: GraphFormat = GraphicsFormat.FORMAT_8BPP; break;

            case 3: GraphFormat = GraphicsFormat.FORMAT_4BPP; break;

            case 2: GraphFormat = GraphicsFormat.FORMAT_2BPP; break;

            case 1: GraphFormat = GraphicsFormat.FORMAT_1BPP; break;

            default: MainWindow.ShowError("Unknown GraphicsFormat in NCGR file: " + ptype); break;
            }
            Tiled = br.ReadInt32() == 0;
            if (Tiled)
            {
                h *= 8; w *= 8; TileSize = new Point(8, 8);
            }

            if (h > 0 && w > 0)
            {
                PanelHeight = (uint)h; PanelWidth = (uint)w;
            }

            // 2*WORD = 8 bytes unknown => skip int32
            int t = br.ReadInt32();
#if DEBUG
            if (t > 1)
            {
                MainWindow.ShowError("Padding in NCGR>CHAR is not padding");
            }
#endif
            int imLength = br.ReadInt32();

#if DEBUG
            if (br.ReadInt32() != 0x18)
            {
                MainWindow.ShowError("Value at end of CHAR header isn't 0x18");
            }
#else
            br.ReadInt32();
#endif

            this.Data = br.ReadBytes(imLength);

            br.Close();
        }
Exemple #4
0
        public void loadFileAsNCLR(string filename)
        {
            #region typedef

            /*
             * typedef struct {
             *  char* magHeader; // 4 bytes: RLCN
             *  DWORD magConst; // FF FE 00 01
             *  DWORD fileSize;
             *  WORD headerSize; // = 0x10
             *  WORD nSections; // = 1
             *  PLTTSection plttSection;
             * } NCLRFile
             *
             * typedef struct {
             *  char* magHeader; // 4 bytes: TTLP
             *  DWORD sectionSize;
             *  DWORD paletteType; // could also be a WORD, but next 2 bytes seem to be always 0
             *  DWORD palStart; // start offset of data after header? seems to be 0 always
             *  DWORD palEnd; // end offset of data after header? seems to be sectionSize - 0x18 always
             *  DWORD unkn1; // seems to be 0x10 always
             *  WORD* palData;
             * } PLTTSection
             *
             */
            #endregion
            BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open));
            br.ReadInt32(); // skip magic header; it should be LRCN

            if (br.ReadInt32() != 0x0100FEFF)
            {
                MainWindow.ShowError("Given file " + filename + " is not a valid NCLR file.\n It does not have the magic constant 0x0100FEFF at 0x04");
                br.Close();
                return;
            }
            int fileSize = br.ReadInt32();

            int headerSize = br.ReadInt16();
            int nSections  = br.ReadInt16();

            // should now be at PLTT section
            if (br.ReadChar() != 'T' || br.ReadChar() != 'T' || br.ReadChar() != 'L' || br.ReadChar() != 'P')
            {
                MainWindow.ShowError("Given file " + filename + " is not a valid NCLR file or of an unsupported type.\n The PLTT section does not follow the NCLR header");
                br.Close();
                return;
            }

            int plttSize = br.ReadInt32();
            int pltype   = br.ReadInt32();
            int palStart = br.ReadInt32();
            int palEnd   = br.ReadInt32();
            int unkn     = br.ReadInt32();
            int palLen   = palEnd - palStart;
            if (palLen == 0)
            {
                palLen = plttSize * 0x18; // tbh: I cannot remember why this is actually * 0x18
            }
            this.Data = br.ReadBytes(Math.Min(palLen, plttSize - 0x18));

            switch (pltype)
            {
            case 3:
            case 4: PalFormat = PaletteFormat.FORMAT_2BPP; break;

            default: MainWindow.ShowError("Unknown palette format " + pltype); break;
            }

            br.Close();
        }