Esempio n. 1
0
        private void Readmetadata(MetadataReader metadata, out TIM2Segment.TIM2SegmentParameters parameters, out string basename, out int palCount)
        {
            metadata.EnterSection("TIM2Texture");

            basename = metadata.GetAttribute <string>("Basename");
            palCount = metadata.GetAttribute <int>("Cluts");

            parameters = new TIM2Segment.TIM2SegmentParameters();

            parameters.swizzled = swizzled;

            parameters.linearPalette = metadata.GetAttribute <bool>("LinearClut");

            parameters.width       = metadata.Get <int>("Width");
            parameters.height      = metadata.Get <int>("Height");
            parameters.bpp         = metadata.Get <byte>("Bpp");
            parameters.colorSize   = metadata.Get <int>("ColorSize");
            parameters.mipmapCount = metadata.Get <byte>("MipmapCount");

            parameters.format = metadata.Get <byte>("Format");

            parameters.GsTEX0 = metadata.Get <byte[]>("GsTEX0");
            parameters.GsTEX1 = metadata.Get <byte[]>("GsTEX1");

            parameters.GsRegs    = metadata.Get <uint>("GsRegs");
            parameters.GsTexClut = metadata.Get <uint>("GsTexClut");

            parameters.userdata = metadata.Get <byte[]>("UserData");

            metadata.ExitSection();
        }
Esempio n. 2
0
        private void WriteHeader(TIM2Segment.TIM2SegmentParameters parameters, Stream outFormatData, byte[] imageData, byte[] paletteData)
        {
            BinaryWriter writer    = new BinaryWriter(outFormatData);
            uint         totalSize = (uint)(0x30 + parameters.userdata.Length + imageData.Length + paletteData.Length);

            writer.Write(totalSize);
            writer.Write((uint)paletteData.Length);
            writer.Write((uint)imageData.Length);
            writer.Write((ushort)(0x30 + parameters.userdata.Length));


            ushort colorEntries = (ushort)(parameters.bpp > 8 ? 0 : 1 << parameters.bpp);

            writer.Write(colorEntries);

            writer.Write(parameters.format);
            writer.Write(parameters.mipmapCount);

            byte clutFormat = (byte)(parameters.bpp > 8 ? 0 : parameters.colorSize - 1);

            clutFormat |= parameters.linearPalette ? (byte)0x80 : (byte)0;

            writer.Write(clutFormat);
            byte depth;

            switch (parameters.bpp)
            {
            case 4:
                depth = 4;
                break;

            case 8:
                depth = 5;
                break;

            case 16:
                depth = 1;
                break;

            case 24:
                depth = 2;
                break;

            case 32:
                depth = 3;
                break;

            default:
                throw new ArgumentException("Should never happen");
            }
            writer.Write(depth);
            writer.Write((ushort)parameters.width);
            writer.Write((ushort)parameters.height);
            writer.Write(parameters.GsTEX0);
            writer.Write(parameters.GsTEX1);
            writer.Write(parameters.GsRegs);
            writer.Write(parameters.GsTexClut);
            writer.Write(parameters.userdata);
        }
Esempio n. 3
0
        public void Save(TextureFormat texture, Stream outFormatData)
        {
            TIM2Segment segment = texture as TIM2Segment;

            if (segment == null)
            {
                throw new TextureFormatException("Not A valid TIM2Segment!");
            }

            byte[] imageData   = segment.GetImageData();
            byte[] paletteData = segment.GetPaletteData();
            TIM2Segment.TIM2SegmentParameters parameters = segment.GetParameters();

            //write header
            WriteHeader(parameters, outFormatData, imageData, paletteData);
            outFormatData.Write(imageData, 0, imageData.Length);
            outFormatData.Write(paletteData, 0, paletteData.Length);
        }
Esempio n. 4
0
        private void AcquireInfoFromHeader(Stream formatData, out TIM2Segment.TIM2SegmentParameters parameters, out uint dataSize, out uint paletteSize, out uint colorEntries)
        {
            byte[] fullHeader = new byte[0x30];
            formatData.Read(fullHeader, 0, fullHeader.Length);

            BinaryReader reader = new BinaryReader(new MemoryStream(fullHeader));

#pragma warning disable 219
            uint totalSize = reader.ReadUInt32();
#pragma warning restore 219

            paletteSize = reader.ReadUInt32();
            dataSize    = reader.ReadUInt32();
            ushort headerSize = reader.ReadUInt16();

            int userDataSize = headerSize - 0x30;

            colorEntries = reader.ReadUInt16();

            parameters          = new TIM2Segment.TIM2SegmentParameters();
            parameters.swizzled = swizzled;

            parameters.format = reader.ReadByte();

            parameters.mipmapCount = reader.ReadByte();

            if (parameters.mipmapCount > 1)
            {
                throw new TextureFormatException("Mipmapped images not supported yet!");
            }

            byte clutFormat = reader.ReadByte();

            byte depth = reader.ReadByte();

            switch (depth)
            {
            case 01:
                parameters.bpp = 16;
                break;

            case 02:
                parameters.bpp = 24;
                break;

            case 03:
                parameters.bpp = 32;
                break;

            case 04:
                parameters.bpp = 4;
                break;

            case 05:
                parameters.bpp = 8;
                break;

            default:
                throw new TextureFormatException("Illegal bit depth!");
            }

            parameters.width  = reader.ReadUInt16();
            parameters.height = reader.ReadUInt16();

            parameters.GsTEX0 = reader.ReadBytes(8);
            parameters.GsTEX1 = reader.ReadBytes(8);

            parameters.GsRegs    = reader.ReadUInt32();
            parameters.GsTexClut = reader.ReadUInt32();

            reader.Close();

            parameters.linearPalette = (clutFormat & 0x80) != 0;
            clutFormat &= 0x7F;

            parameters.colorSize = parameters.bpp > 8 ? parameters.bpp / 8 : (clutFormat & 0x07) + 1;

            if (userDataSize > 0)
            {
                byte[] data = new byte[userDataSize];
                formatData.Read(data, 0, userDataSize);
                parameters.userdata = data;
            }
        }