Example #1
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);
                }
        }
Example #2
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 #3
0
        public TBG(Size size, TBGPixelFormat format)
        {
            this.Size        = size;
            this.PixelFormat = format;
            int pixelSize = TBGPixelFormats.GetSize(format);

            this._data = new byte[size.Width * size.Height * pixelSize];
        }
Example #4
0
        /// <summary>
        /// Writes the TBG file to a stream using the provided BinaryWriter.
        /// See https://github.com/Anonym271/7scarlet-tools/wiki/TBG-Images-%28%2A.tbg%29 for details.
        /// </summary>
        public void Save(BinaryWriter file)
        {
            var startPos = file.BaseStream.Position;

            file.Write(Encoding.ASCII.GetBytes("tbg\0"));
            file.Write(HEADER_SIZE);
            file.Write(_data.Length);
            file.Write(Size.Width);
            file.Write(Size.Height);
            file.Write(new byte[] { 0x00, 0x10, 0x00 });
            file.Write(TBGPixelFormats.GetBinaryRepresentation(PixelFormat));
            file.Write(1);
            file.Write((long)0);
            file.Write(ResX);
            file.Write(ResY);

            file.BaseStream.Position = startPos + HEADER_SIZE;
            file.Write(_data);
        }
Example #5
0
        public static TBG FromBitmap(Bitmap bmp)
        {
            var tbgFormat = TBGPixelFormat.BGRA;
            var bmpFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;

            if (bmp.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
            {
                tbgFormat = TBGPixelFormat.BGR;
                bmpFormat = System.Drawing.Imaging.PixelFormat.Format32bppRgb;
            }
            int pixelSize = TBGPixelFormats.GetSize(tbgFormat);

            var data    = new byte[bmp.Width * bmp.Height * pixelSize];
            var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmpFormat);

            Marshal.Copy(bmpData.Scan0, data, 0, data.Length);
            bmp.UnlockBits(bmpData);

            return(new TBG(bmp.Size, tbgFormat, data));
        }