コード例 #1
0
ファイル: ParseIMGD.cs プロジェクト: weimingtom/OpenKH
        /// <summary>
        /// Function that will parse an IMGD file
        /// </summary>
        /// <param name="si">MemoryStream of the file</param>
        /// <returns></returns>
        public static PicIMGD TakeIMGD(MemoryStream si)
        {
            si.Position = 0L;
            if (si.ReadByte() != 73)
            {
                throw new NotSupportedException("!IMGD");
            }
            if (si.ReadByte() != 77)
            {
                throw new NotSupportedException("!IMGD");
            }
            if (si.ReadByte() != 71)
            {
                throw new NotSupportedException("!IMGD");
            }
            if (si.ReadByte() != 68)
            {
                throw new NotSupportedException("!IMGD");
            }
            var binaryReader = new BinaryReader(si);

            binaryReader.ReadInt32();
            int num    = binaryReader.ReadInt32();
            int count  = binaryReader.ReadInt32();
            int num2   = binaryReader.ReadInt32();
            int count2 = binaryReader.ReadInt32();

            si.Position = 28L;
            int num3 = binaryReader.ReadUInt16();
            int num4 = binaryReader.ReadUInt16();

            si.Position = 38L;
            int num5 = binaryReader.ReadUInt16();

            si.Position = 60L;
            int  num6 = binaryReader.ReadByte();
            bool flag = num6 == 7;

            si.Position = num;
            byte[] array = binaryReader.ReadBytes(count);
            si.Position = num2;
            byte[] array2 = binaryReader.ReadBytes(count2);
            int    num7   = num3;
            int    num8   = num4;

            if (num5 == 19)
            {
                int        num9       = num3 / 128;
                int        num10      = num4 / 64;
                int        bw         = num9;
                int        bh         = num10;
                byte[]     array3     = flag ? Reform8.Decode8(Reform32.Encode32(array, num9, num10), bw, bh) : array;
                var        bitmap     = new Bitmap(num7, num8, PixelFormat.Format8bppIndexed);
                BitmapData bitmapData = bitmap.LockBits(Rectangle.FromLTRB(0, 0, num7, num8), ImageLockMode.WriteOnly,
                                                        PixelFormat.Format8bppIndexed);
                try
                {
                    Marshal.Copy(array3, 0, bitmapData.Scan0, array3.Length);
                }
                finally
                {
                    bitmap.UnlockBits(bitmapData);
                }
                var array4 = new byte[8192];
                Array.Copy(array2, 0, array4, 0, Math.Min(8192, array2.Length));
                byte[]       array5  = array4;
                ColorPalette palette = bitmap.Palette;
                for (int i = 0; i < 256; i++)
                {
                    int num11 = i;
                    int num12 = KHcv8pal_swap34.repl(i);
                    palette.Entries[num12] = Color.FromArgb(Math.Min(255, array5[4 * num11 + 3] * 2), array5[4 * num11],
                                                            array5[4 * num11 + 1], array5[4 * num11 + 2]);
                }
                bitmap.Palette = palette;
                return(new PicIMGD(bitmap));
            }
            if (num5 == 20)
            {
                int    num13  = num3 / 128;
                int    num14  = num4 / 128;
                int    bw2    = num13;
                int    bh2    = num14;
                byte[] array6 = flag
                    ? Reform4.Decode4(Reform32.Encode32(array, num13, num14), bw2, bh2)
                    : HLUt.Swap(array);
                var        bitmap2     = new Bitmap(num7, num8, PixelFormat.Format4bppIndexed);
                BitmapData bitmapData2 = bitmap2.LockBits(Rectangle.FromLTRB(0, 0, num7, num8), ImageLockMode.WriteOnly,
                                                          PixelFormat.Format4bppIndexed);
                try
                {
                    Marshal.Copy(array6, 0, bitmapData2.Scan0, array6.Length);
                }
                finally
                {
                    bitmap2.UnlockBits(bitmapData2);
                }
                var array7 = new byte[8192];
                Array.Copy(array2, 0, array7, 0, Math.Min(8192, array2.Length));
                byte[]       array8   = array7;
                ColorPalette palette2 = bitmap2.Palette;
                for (int j = 0; j < 16; j++)
                {
                    int num15 = j;
                    int num16 = j;
                    palette2.Entries[num16] = Color.FromArgb(Math.Min(255, array8[4 * num15 + 3] * 2), array8[4 * num15],
                                                             array8[4 * num15 + 1], array8[4 * num15 + 2]);
                }
                bitmap2.Palette = palette2;
                return(new PicIMGD(bitmap2));
            }
            throw new NotSupportedException("v26 = " + num5.ToString("X"));
        }
コード例 #2
0
        public static Texex2 Load(Stream fs)
        {
            var texex        = new Texex2();
            var binaryReader = new BinaryReader(fs);

            fs.Position = 8L;
            binaryReader.ReadInt32();
            int num  = binaryReader.ReadInt32();
            int num2 = binaryReader.ReadInt32();
            int num3 = binaryReader.ReadInt32();
            int num4 = binaryReader.ReadInt32();

            fs.Position = num2;
            byte[] array = binaryReader.ReadBytes(num);
            fs.Position = 28L;
            int num5 = binaryReader.ReadInt32();
            int num6 = binaryReader.ReadInt32();

            fs.Position = num3;
            binaryReader.ReadBytes(num4 - num3);
            fs.Position = num4;
            binaryReader.ReadBytes(num5 - num4);
            fs.Position = num5;
            binaryReader.ReadBytes(num6 - num5);
            binaryReader.ReadBytes(Convert.ToInt32(fs.Length) - 4 - num6);
            var array2 = new byte[4194304];

            for (int i = 0; i < num; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    int num7 = (j == 0) ? 0 : (1 + array[i]);
                    fs.Position = num3 + 144 * num7 + 32;
                    ulong num8  = binaryReader.ReadUInt64();
                    int   num9  = (int)(num8 >> 32) & 16383;
                    int   num10 = (int)(num8 >> 48) & 63;
                    int   num11 = (int)(num8 >> 56) & 63;
                    Trace.Assert(binaryReader.ReadUInt64() == 80uL, "Unexpected texture format");
                    fs.Position = num3 + 144 * num7 + 64;
                    ulong num12 = binaryReader.ReadUInt64();
                    Trace.Assert(binaryReader.ReadUInt64() == 82uL, "Unexpected texture format");
                    fs.Position = num3 + 144 * num7 + 96;
                    ulong num13 = binaryReader.ReadUInt64();
                    int   num14 = (int)num13 & 16383;
                    fs.Position = num3 + 144 * num7 + 112;
                    ulong num15 = binaryReader.ReadUInt64();
                    int   num16 = (int)num15 & 16383;
                    int   num17 = (int)(num15 >> 32) & 2147483647;
                    Trace.Assert(num14 == num16, "Unexpected texture format");
                    fs.Position = num17;
                    var array3 = new byte[16 * num16];
                    fs.Read(array3, 0, 16 * num16);
                    Trace.Assert(num11 == 0, "Unexpected texture format");
                    int bh = Convert.ToInt32(array3.Length) / 8192 / num10;
                    array3 = Reform32.Encode32(array3, num10, bh);
                    Array.Copy(array3, 0, array2, 256 * num9, 16 * num16);
                    Console.Write("");
                }
                fs.Position = num4 + 160 * i + 32;
                ulong num18 = binaryReader.ReadUInt64();
                Trace.Assert(num18 == 0uL, "Unexpected texture format");
                Trace.Assert(binaryReader.ReadUInt64() == 63uL, "Unexpected texture format");
                fs.Position = num4 + 160 * i + 48;
                ulong num19 = binaryReader.ReadUInt64();
                Trace.Assert(num19 == 0uL, "Unexpected texture format");
                Trace.Assert(binaryReader.ReadUInt64() == 52uL, "Unexpected texture format");
                fs.Position = num4 + 160 * i + 64;
                ulong num20 = binaryReader.ReadUInt64();
                Trace.Assert(num20 == 0uL, "Unexpected texture format");
                Trace.Assert(binaryReader.ReadUInt64() == 54uL, "Unexpected texture format");
                fs.Position = num4 + 160 * i + 80;
                ulong num21 = binaryReader.ReadUInt64();
                Trace.Assert(binaryReader.ReadUInt64() == 22uL, "Unexpected texture format");
                fs.Position = num4 + 160 * i + 112;
                ulong num22 = binaryReader.ReadUInt64();
                int   num23 = (int)num22 & 16383;
                int   tbw   = (int)(num22 >> 14) & 63;
                int   num24 = (int)(num22 >> 20) & 63;
                int   num25 = (int)(num22 >> 26) & 15;
                int   num26 = (int)(num22 >> 30) & 15;
                int   num27 = (int)(num22 >> 37) & 16383;
                Trace.Assert(binaryReader.ReadUInt64() == 6uL, "Unexpected texture format");
                int num28  = (1 << num25) * (1 << num26);
                var array4 = new byte[num28];
                Array.Copy(array2, 256 * num23, array4, 0, array4.Length);
                var array5 = new byte[8192];
                Array.Copy(array2, 256 * num27, array5, 0, array5.Length);
                STim item = null;
                if (num24 == 19)
                {
                    item = TexUtil.Decode8(array4, array5, tbw, 1 << num25, 1 << num26);
                }
                if (num24 == 20)
                {
                    item = TexUtil.Decode4(array4, array5, tbw, 1 << num25, 1 << num26);
                }
                texex.alt.Add(item);
            }
            int num29 = 0;

            while (num29 < fs.Length)
            {
                fs.Position = num29;
                byte[] array6 = binaryReader.ReadBytes(16);
                if (array6[0] == 95 && array6[1] == 68 && array6[2] == 77 && array6[3] == 89 && array6[8] == 84 &&
                    array6[9] == 69 && array6[10] == 88 && array6[11] == 65)
                {
                    fs.Position = num29 + 16 + 2;
                    int texi = binaryReader.ReadUInt16();
                    fs.Position = num29 + 16 + 12;
                    binaryReader.ReadUInt16();
                    int num30 = binaryReader.ReadUInt16();
                    int px    = binaryReader.ReadUInt16();
                    int py    = binaryReader.ReadUInt16();
                    int num31 = binaryReader.ReadUInt16();
                    int num32 = binaryReader.ReadUInt16();
                    int num33 = binaryReader.ReadInt32();
                    int num34 = binaryReader.ReadInt32();
                    int num35 = binaryReader.ReadInt32();
                    fs.Position = num29 + 16 + num35;
                    byte[] bits = binaryReader.ReadBytes(num31 * num32 * num30);
                    Patc   patc;
                    texex.alp.Add(patc = new Patc(bits, px, py, num31, num32, num30, texi));
                    int num36 = num29 + 16;
                    int num37 = (num34 - num33) / 2;
                    var list  = new List <Texfac>();
                    for (int k = 0; k < num37; k++)
                    {
                        fs.Position = num36 + num33 + 2 * k;
                        int num38 = binaryReader.ReadInt16() - 1;
                        if (num38 >= 0)
                        {
                            fs.Position = num36 + num34 + 4 * num38;
                            int num39 = binaryReader.ReadInt32();
                            fs.Position = num36 + num39;
                            int    num40 = 0;
                            Texfac texfac;
                            do
                            {
                                texfac    = new Texfac();
                                texfac.i0 = k;
                                texfac.i1 = num38;
                                texfac.i2 = num40;
                                texfac.v0 = binaryReader.ReadInt16();
                                texfac.v2 = binaryReader.ReadInt16();
                                texfac.v4 = binaryReader.ReadInt16();
                                texfac.v6 = binaryReader.ReadInt16();
                                list.Add(texfac);
                                num40++;
                            } while (texfac.v0 >= 0);
                        }
                    }
                    patc.altf = list.ToArray();
                }
                num29 += 16;
            }
            return(texex);
        }