Example #1
0
        public static TBG FromBuffer(byte[] fileData)
        {
            if (Encoding.ASCII.GetString(fileData, 0, 4) != "tbg\0")
            {
                throw new InvalidDataException("Invalid magic number!");
            }
            uint  dataOffset = BitConverter.ToUInt32(fileData, 0x04);
            int   dataLength = BitConverter.ToInt32(fileData, 0x08);
            int   width      = BitConverter.ToInt32(fileData, 0x0C);
            int   height     = BitConverter.ToInt32(fileData, 0x10);
            byte  colorByte  = fileData[0x14 + 3];
            float resX       = BitConverter.ToSingle(fileData, 0x24);
            float resY       = BitConverter.ToSingle(fileData, 0x28);
            var   format     = TBGPixelFormats.FromBinaryRepresentation(colorByte);
            var   data       = new byte[dataLength];

            Array.Copy(fileData, dataOffset, data, 0, dataLength);

            var tbg = new TBG(new Size(width, height), format, data)
            {
                ResX = resX,
                ResY = resY
            };

            return(tbg);
        }
Example #2
0
        public static TBG FromFile(string filename)
        {
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
                using (BinaryReader file = new BinaryReader(fs))
                {
                    byte[] magic = file.ReadBytes(4);
                    if (Encoding.ASCII.GetString(magic) != "tbg\0")
                    {
                        throw new InvalidDataException("Invalid magic number!");
                    }
                    uint   dataOffset = file.ReadUInt32();
                    int    dataLength = file.ReadInt32();
                    int    width      = file.ReadInt32();
                    int    height     = file.ReadInt32();
                    byte[] flags      = file.ReadBytes(4);
                    int    imageCount = file.ReadInt32();
                    long   unknown    = file.ReadInt64();
                    float  resX       = file.ReadSingle();
                    float  resY       = file.ReadSingle();

                    var format = TBGPixelFormats.FromBinaryRepresentation(flags[3]);

                    file.BaseStream.Position = dataOffset;
                    byte[] data = file.ReadBytes(dataLength);
                    var    tbg  = new TBG(new Size(width, height), format, data)
                    {
                        ResX = resX,
                        ResY = resY
                    };
                    return(tbg);
                }
        }