Example #1
0
        public static byte[] ReadBytes(GTFS fs, long offset, int length, bool flip)
        {
            byte[] bytes = new byte[length];

            long original = fs.Position;

            fs.Position = offset;

            //for (int i = 0; i < length; i++)
            //bytes[i] = (byte)fs.ReadByte();

            fs.Read(bytes, 0, length);

            if (fs.GetType() == typeof(GTFS) && EnableGTFSView)
            {
                ((GTFS)fs).AddTrack(offset, length);
            }

            fs.Position = original;

            if (flip)
            {
                Array.Reverse(bytes);
            }

            return(bytes);
        }
Example #2
0
        public static string ReadASCIItoNull(GTFS fs, long offset, bool flip, byte terminator = 0x00)
        {
            long previous = fs.Position;

            int end = 0;

            fs.Position = offset;
            for (int k = 0; k < 100; k++)
            {
                byte test = (byte)fs.ReadByte();
                if (test == terminator)
                {
                    end = k;
                    break;
                }
            }

            if (end == 0)
            {
                throw new Exception();
            }

            fs.Position = previous;

            byte[] bString = ReadBytes(fs, offset, end, flip);

            return(Encoding.ASCII.GetString(bString));
        }
Example #3
0
        public static byte[] ReadBytes(GTFS fs, int length, bool flip)
        {
            if (length < 0)
            {
                throw new Exception();
            }

            byte[] bytes = new byte[length];
            fs.Read(bytes, 0, length);

            //for (int i = 0; i < length i++)
            //bytes[i] = (byte)fs.ReadByte();

            if (fs.GetType() == typeof(GTFS) && EnableGTFSView)
            {
                ((GTFS)fs).AddTrack(fs.Position - length, length);
            }

            if (flip)
            {
                Array.Reverse(bytes);
            }

            return(bytes);
        }
Example #4
0
        /*
         * public enum Format {
         *  DXT1, DXT, RGBA32, RGBA24
         * }
         */

        public static Color[][] ReadCMPR(GTFS fs, bool flip, int width, int height)
        {
            int total = width * height;

            Color[][] blocks = ReadDXT1(fs, flip, width, height);

            //The blocks require reordering...
            //int rows = (int)Math.Sqrt(blocks.Length);
            int rows = height / 2;
            int cols = width / 2;

            Color[][] reorder = new Color[total / 16][];
            int       i       = 0;

            for (int y = 0; y < rows; y += 4)
            {
                for (int x = 0; x < cols; x += 4)
                {
                    reorder[i++] = blocks[y * rows / 4 + x + 0];
                    reorder[i++] = blocks[y * rows / 4 + x + 1];
                }
                for (int x = 0; x < cols; x += 4)
                {
                    reorder[i++] = blocks[y * rows / 4 + x + 2];
                    reorder[i++] = blocks[y * rows / 4 + x + 3];
                }
            }

            for (int z = i; z < blocks.Length; z++)
            {
                reorder[i++] = blocks[0];
            }

            return(reorder);
        }
Example #5
0
        public static void WriteSubFile(GTFS fs, string newfile, long length)
        {
            FileStream nf = File.Create(newfile);

            byte[] buffer = new byte[length];
            fs.Read(buffer, 0, buffer.Length);
            nf.Write(buffer, 0, buffer.Length);
            nf.Close();
        }
Example #6
0
        public static byte ReadByte(GTFS fs)
        {
            if (fs.GetType() == typeof(GTFS) && EnableGTFSView)
            {
                ((GTFS)fs).AddTrack(fs.Position, 1);
            }

            return((byte)fs.ReadByte());
        }
Example #7
0
        public static void WriteSubFile(GTFS fs, string newfile, long length, long offset)
        {
            long last = fs.Position;

            FileStream nf = File.Create(newfile);

            byte[] buffer = new byte[length];
            fs.Seek(offset, SeekOrigin.Begin);
            fs.Read(buffer, 0, buffer.Length);
            nf.Write(buffer, 0, buffer.Length);
            nf.Close();

            fs.Position = last;
        }
Example #8
0
        public static byte ReadByteAt(GTFS fs, long offset)
        {
            if (fs.GetType() == typeof(GTFS) && EnableGTFSView)
            {
                ((GTFS)fs).AddTrack(fs.Position, 1);
            }

            long remember = fs.Position;

            fs.Position = offset;
            byte b = (byte)fs.ReadByte();

            fs.Position = remember;

            return(b);
        }
Example #9
0
        public void WriteOut(GTFS fs, string outdir)
        {
            if (!Directory.Exists(outdir))
            {
                Directory.CreateDirectory(outdir);
            }

            Filename = Filename.Replace('/', '_'); //From old PAK4

            if (Offset < 0)
            {
                throw new Exception(); //Probably invalid for GT.WriteSubFile, use the other one.
            }
            string newfile = outdir + "\\" + Filename;

            GT.WriteSubFile(fs, newfile, Size, Offset);
        }
Example #10
0
        public static Color[][] ReadDXT3(GTFS fs, bool flip, int width, int height)
        {
            int total = width * height;

            Color[][] blocks = new Color[total / 16][];

            byte[] compare = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };

            for (int i = 0; i < total / 16; i++)
            {
                byte[] rawalpha = GT.ReadBytes(fs, 8, flip); //Each pixel gets 4 bits for alpha

                byte[] raw = GT.ReadBytes(fs, 4, flip);

                int[] alpha = new int[rawalpha.Length * 2];

                for (int a = 0; a < rawalpha.Length; a++)
                {
                    alpha[a * 2]     = (rawalpha[a] & 0x0F) * 17;
                    alpha[a * 2 + 1] = ((rawalpha[a] & 0xF0) >> 4) * 17;
                }

                //http://www.igeekstudio.com/blog/dtx1-decompression-explained
                ushort c0 = (ushort)(raw[0] | raw[1] << 8);
                ushort c1 = (ushort)(raw[2] | raw[3] << 8);

                // Color 0 RGB
                byte r0 = (byte)((c0 >> 11) << 3 | 0x04);
                byte g0 = (byte)((c0 & 0x07E0) >> 3 | 0x02);
                byte b0 = (byte)((c0 & 0x001F) << 3 | 0x04);
                // Color 1 RBG
                byte r1 = (byte)((c1 >> 11) << 3);
                byte g1 = (byte)((c1 & 0x07E0) >> 3);
                byte b1 = (byte)((c1 & 0x001F) << 3);
                //--

                Color[] cx = new Color[] {
                    Color.FromArgb(Byte.MaxValue, r0, g0, b0),
                    Color.FromArgb(Byte.MaxValue, r1, g1, b1),
                    (c0 > c1) ? Color.FromArgb(byte.MaxValue, (2 * r0 + r1) / 3, (2 * g0 + g1) / 3, (2 * b0 + b1) / 3) : Color.FromArgb(byte.MaxValue, (2 * r1 + r0) / 3, (2 * g1 + g0) / 3, (2 * b1 + b0) / 3),
                    (c0 <= c1) ? Color.FromArgb(byte.MaxValue, (2 * r0 + r1) / 3, (2 * g0 + g1) / 3, (2 * b0 + b1) / 3) : Color.FromArgb(byte.MaxValue, (2 * r1 + r0) / 3, (2 * g1 + g0) / 3, (2 * b1 + b0) / 3)
                };

                uint pixels = GT.ReadUInt32(fs, 4, flip);

                int p15 = (int)((pixels & 0xC0000000) >> 30);
                int p14 = (int)((pixels & 0x30000000) >> 28);
                int p13 = (int)((pixels & 0xC000000) >> 26);
                int p12 = (int)((pixels & 0x3000000) >> 24);
                int p11 = (int)((pixels & 0xC00000) >> 22);
                int p10 = (int)((pixels & 0x300000) >> 20);
                int p9  = (int)((pixels & 0xC0000) >> 18);
                int p8  = (int)((pixels & 0x30000) >> 16);
                int p7  = (int)((pixels & 0xC000) >> 14);
                int p6  = (int)((pixels & 0x3000) >> 12);
                int p5  = (int)((pixels & 0xC00) >> 10);
                int p4  = (int)((pixels & 0x300) >> 8);
                int p3  = (int)((pixels & 0xC0) >> 6);
                int p2  = (int)((pixels & 0x30) >> 4);
                int p1  = (int)((pixels & 0xC) >> 2);
                int p0  = (int)((pixels & 0x3));

                blocks[i] = new Color[] { cx[p0], cx[p1], cx[p2], cx[p3],
                                          cx[p4], cx[p5], cx[p6], cx[p7],
                                          cx[p8], cx[p9], cx[p10], cx[p11],
                                          cx[p12], cx[p13], cx[p14], cx[p15] };

                for (int k = 0; k < blocks[i].Length; k++)
                {
                    blocks[i][k] = Color.FromArgb(alpha[k], blocks[i][k]);
                }
            }

            return(blocks);
        }
Example #11
0
        public static ushort ReadUInt16(GTFS fs, int length, bool flip)
        {
            byte[] bytes = ReadBytes(fs, length, flip);

            return(BitConverter.ToUInt16(bytes, 0));
        }
Example #12
0
        public static int ReadInt32(GTFS fs, int length, bool flip)
        {
            byte[] bytes = ReadBytes(fs, length, flip);

            return(BitConverter.ToInt32(bytes, 0));
        }
Example #13
0
        public static uint ReadUInt32(GTFS fs, long offset, int length, bool flip)
        {
            byte[] bytes = ReadBytes(fs, offset, length, flip);

            return(BitConverter.ToUInt32(bytes, 0));
        }
Example #14
0
 public static string ReadASCII(GTFS fs, int length, bool flip)
 {
     return(Encoding.ASCII.GetString(ReadBytes(fs, length, flip)));
 }
Example #15
0
        public static Color[][] ReadDXT1(GTFS fs, bool flip, int width, int height)
        {
            int total = width * height;

            Color[][] blocks = new Color[total / 16][];

            for (int i = 0; i < total / 16; i++)
            {
                byte[] raw = GT.ReadBytes(fs, 4, flip);

                ushort c0, c1;
                if (flip)
                {
                    c0 = (ushort)(raw[2] | raw[3] << 8);
                    c1 = (ushort)(raw[0] | raw[1] << 8);
                }
                else
                {
                    c0 = (ushort)(raw[0] | raw[1] << 8);
                    c1 = (ushort)(raw[2] | raw[3] << 8);
                }

                // Color 0 RGB
                byte r0 = (byte)((c0 >> 11) << 3 | 0x04);
                byte g0 = (byte)((c0 & 0x07E0) >> 3 | 0x02);
                byte b0 = (byte)((c0 & 0x001F) << 3 | 0x04);
                // Color 1 RBG
                byte r1 = (byte)((c1 >> 11) << 3);
                byte g1 = (byte)((c1 & 0x07E0) >> 3);
                byte b1 = (byte)((c1 & 0x001F) << 3);
                //--

                Color[] cx = new Color[] {
                    Color.FromArgb(Byte.MaxValue, r0, g0, b0),
                    Color.FromArgb(Byte.MaxValue, r1, g1, b1),
                    (c0 > c1) ? Color.FromArgb(byte.MaxValue, (2 * r0 + r1) / 3, (2 * g0 + g1) / 3, (2 * b0 + b1) / 3) : Color.FromArgb(byte.MaxValue, (2 * r1 + r0) / 3, (2 * g1 + g0) / 3, (2 * b1 + b0) / 3),
                    (c0 <= c1) ? Color.FromArgb(byte.MaxValue, (2 * r0 + r1) / 3, (2 * g0 + g1) / 3, (2 * b0 + b1) / 3) : Color.FromArgb(byte.MaxValue, (2 * r1 + r0) / 3, (2 * g1 + g0) / 3, (2 * b1 + b0) / 3)
                };

                uint pixels = GT.ReadUInt32(fs, 4, flip);

                int p15 = (int)((pixels & 0xC0000000) >> 30);
                int p14 = (int)((pixels & 0x30000000) >> 28);
                int p13 = (int)((pixels & 0xC000000) >> 26);
                int p12 = (int)((pixels & 0x3000000) >> 24);
                int p11 = (int)((pixels & 0xC00000) >> 22);
                int p10 = (int)((pixels & 0x300000) >> 20);
                int p9  = (int)((pixels & 0xC0000) >> 18);
                int p8  = (int)((pixels & 0x30000) >> 16);
                int p7  = (int)((pixels & 0xC000) >> 14);
                int p6  = (int)((pixels & 0x3000) >> 12);
                int p5  = (int)((pixels & 0xC00) >> 10);
                int p4  = (int)((pixels & 0x300) >> 8);
                int p3  = (int)((pixels & 0xC0) >> 6);
                int p2  = (int)((pixels & 0x30) >> 4);
                int p1  = (int)((pixels & 0xC) >> 2);
                int p0  = (int)((pixels & 0x3));

                if (flip)
                {
                    blocks[i] = new Color[] {
                        cx[p15], cx[p14], cx[p13], cx[p12],
                        cx[p11], cx[p10], cx[p9], cx[p8],
                        cx[p7], cx[p6], cx[p5], cx[p4],
                        cx[p3], cx[p2], cx[p1], cx[p0]
                    };
                }
                else
                {
                    blocks[i] = new Color[] { cx[p0], cx[p1], cx[p2], cx[p3],
                                              cx[p4], cx[p5], cx[p6], cx[p7],
                                              cx[p8], cx[p9], cx[p10], cx[p11],
                                              cx[p12], cx[p13], cx[p14], cx[p15] };
                }
            }

            return(blocks);
        }
Example #16
0
        public static float ReadFloat(GTFS fs, int length, bool flip)
        {
            byte[] bytes = ReadBytes(fs, length, flip);

            return(BitConverter.ToSingle(bytes, 0));
        }
Example #17
0
 public GTFSView(GTFS fs)
 {
     this.fs = fs;
     InitializeComponent();
 }