public SpriteFrame ReadFrame(int Index) { BinaryReader Reader = new BinaryReader(new MemoryStream(m_ChunkData)); Reader.BaseStream.Seek(m_FrameOffsets[Index], SeekOrigin.Begin); SpriteFrame Frame = new SpriteFrame(); Frame.FrameIndex = (uint)Index; Frame.Width = Reader.ReadUInt16(); Frame.Height = Reader.ReadUInt16(); Frame.Flag = Reader.ReadUInt32(); Frame.PaletteID = Reader.ReadUInt16(); Frame.TransparentPixel = m_PMap.GetColorAtIndex(Reader.ReadUInt16()); Frame.YLocation = Reader.ReadUInt16(); Frame.XLocation = Reader.ReadUInt16(); if (Frame.Flag == 0x07) { Frame.Init(true, true); } else { if ((SPR2Flags)Frame.Flag == SPR2Flags.HasZBufferChannel) { Frame.Init(false, true); } else { Frame.Init(false, false); } } DecompressFrame2(ref Frame, ref Reader); Frame.BitmapData.Unlock(true); //The bitmapdata is locked when the frame is created. if (Frame.HasZBuffer) { Frame.ZBuffer.Unlock(true); //The bitmapdata is locked when the frame is created. } Reader.Close(); //Store the frame to avoid having to decompress in the future. m_Frames.Add(Frame); return(Frame); }
private SpriteFrame ReadFrame(int Index) { MemoryStream MemStream = new MemoryStream(m_ChunkData); BinaryReader Reader = new BinaryReader(MemStream); Reader.BaseStream.Position = m_OffsetTable[Index]; SpriteFrame Frame = new SpriteFrame(); if (!m_IsBigEndian) { Reader.ReadBytes(4); //Reserved. Frame.Height = Reader.ReadUInt16(); Frame.Width = Reader.ReadUInt16(); Frame.PaletteID = (ushort)m_PaletteID; } else { Reader.ReadBytes(4); //Reserved. Frame.Height = Endian.SwapUInt16(Reader.ReadUInt16()); Frame.Width = Endian.SwapUInt16(Reader.ReadUInt16()); Frame.PaletteID = (ushort)m_PaletteID; } Frame.Init(true, false); //SPR#s don't have alpha channels, but alpha is used to plot transparent pixels. DecompressFrame2(ref Frame, ref Reader); Frame.BitmapData.Unlock(true); //The bitmapdata is locked when the frame is created. //Store the frame to avoid having to decompress in the future. m_Frames.Add(Frame); return(Frame); }
public SPRParser(IffChunk Chunk, List <PaletteMap> PaletteMaps) : base(Chunk) { MemoryStream MemStream = new MemoryStream(Chunk.Data); BinaryReader Reader = new BinaryReader(MemStream); //The two first bytes aren't used by the version... ushort FirstBytes = Reader.ReadUInt16(); if (FirstBytes == 0) { m_IsBigEndian = true; m_Version = (uint)Endian.SwapUInt16(Reader.ReadUInt16()); } else { m_Version = (uint)FirstBytes; Reader.ReadUInt16(); } //In version 1001, all frames are decompressed from the beginning, and there's no point in storing //the compressed data AS WELL as the decompressed frames...! if (m_Version != 1001) { m_ChunkData = Chunk.Data; } if (m_IsBigEndian) { if (m_Version != 1001) { m_FrameCount = Endian.SwapUInt32(Reader.ReadUInt32()); m_PaletteID = Endian.SwapUInt32(Reader.ReadUInt32()); for (uint i = 0; i < m_FrameCount; i++) { m_OffsetTable.Add(Endian.SwapUInt32(Reader.ReadUInt32())); } //Find and set the correct palettemap... if (PaletteMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PaletteMaps[0]; } else { m_PMap = PaletteMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return(true); } return(false); }); } } else { m_FrameCount = Endian.SwapUInt32(Reader.ReadUInt32()); m_PaletteID = Endian.SwapUInt32(Reader.ReadUInt32()); //Find and set the correct palettemap... if (PaletteMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PaletteMaps[0]; } else { m_PMap = PaletteMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return(true); } return(false); }); } } } else { if (m_Version != 1001) { m_FrameCount = Reader.ReadUInt32(); m_PaletteID = Reader.ReadUInt32(); for (uint i = 0; i < m_FrameCount; i++) { m_OffsetTable.Add(Reader.ReadUInt32()); } //Find and set the correct palettemap... if (PaletteMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PaletteMaps[0]; } else { m_PMap = PaletteMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return(true); } return(false); }); } } else { m_FrameCount = Reader.ReadUInt32(); m_PaletteID = Reader.ReadUInt32(); //Find and set the correct palettemap... if (PaletteMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PaletteMaps[0]; } else { m_PMap = PaletteMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return(true); } return(false); }); } } } if (m_Version == 1001) { //Framecount may be set to -1 and should be ignored... while (true) { SpriteFrame Frame = new SpriteFrame(); Frame.Version = Reader.ReadUInt32(); Frame.Size = Reader.ReadUInt32(); Reader.ReadBytes(4); //Reserved. Frame.Height = Reader.ReadUInt16(); Frame.Width = Reader.ReadUInt16(); Frame.Init(true, false); //SPR#s don't have alpha channels, but alpha is used to plot transparent pixels. DecompressFrame2(ref Frame, ref Reader); Frame.BitmapData.Unlock(true); //The bitmapdata is locked when the frame is created. m_Frames.Add(Frame); if ((Reader.BaseStream.Position == Reader.BaseStream.Length) || (Reader.BaseStream.Position - Reader.BaseStream.Length < 14)) { break; } } } Reader.Close(); }
private SpriteFrame ReadFrame(int Index) { MemoryStream MemStream = new MemoryStream(m_ChunkData); BinaryReader Reader = new BinaryReader(MemStream); Reader.BaseStream.Position = m_OffsetTable[Index]; SpriteFrame Frame = new SpriteFrame(); if (!m_IsBigEndian) { Reader.ReadBytes(4); //Reserved. Frame.Height = Reader.ReadUInt16(); Frame.Width = Reader.ReadUInt16(); Frame.PaletteID = (ushort)m_PaletteID; } else { Reader.ReadBytes(4); //Reserved. Frame.Height = Endian.SwapUInt16(Reader.ReadUInt16()); Frame.Width = Endian.SwapUInt16(Reader.ReadUInt16()); Frame.PaletteID = (ushort)m_PaletteID; } Frame.Init(true); //SPR#s don't have alpha channels, but alpha is used to plot transparent pixels. DecompressFrame2(ref Frame, ref Reader); Frame.BitmapData.Unlock(true); //The bitmapdata is locked when the frame is created. //Store the frame to avoid having to decompress in the future. m_Frames.Add(Frame); return Frame; }
public SPRParser(IffChunk Chunk, List<PaletteMap> PaletteMaps) : base(Chunk) { MemoryStream MemStream = new MemoryStream(Chunk.Data); BinaryReader Reader = new BinaryReader(MemStream); //The two first bytes aren't used by the version... ushort FirstBytes = Reader.ReadUInt16(); if (FirstBytes == 0) { m_IsBigEndian = true; m_Version = (uint)Endian.SwapUInt16(Reader.ReadUInt16()); } else { m_Version = (uint)FirstBytes; Reader.ReadUInt16(); } //In version 1001, all frames are decompressed from the beginning, and there's no point in storing //the compressed data AS WELL as the decompressed frames...! if (m_Version != 1001) m_ChunkData = Chunk.Data; if (m_IsBigEndian) { if (m_Version != 1001) { m_FrameCount = Endian.SwapUInt32(Reader.ReadUInt32()); m_PaletteID = Endian.SwapUInt32(Reader.ReadUInt32()); for (uint i = 0; i < m_FrameCount; i++) m_OffsetTable.Add(Endian.SwapUInt32(Reader.ReadUInt32())); //Find and set the correct palettemap... if (PaletteMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PaletteMaps[0]; } else m_PMap = PaletteMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return true; } return false; }); } else { m_FrameCount = Endian.SwapUInt32(Reader.ReadUInt32()); m_PaletteID = Endian.SwapUInt32(Reader.ReadUInt32()); //Find and set the correct palettemap... if (PaletteMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PaletteMaps[0]; } else m_PMap = PaletteMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return true; } return false; }); } } else { if (m_Version != 1001) { m_FrameCount = Reader.ReadUInt32(); m_PaletteID = Reader.ReadUInt32(); for (uint i = 0; i < m_FrameCount; i++) m_OffsetTable.Add(Reader.ReadUInt32()); //Find and set the correct palettemap... if (PaletteMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PaletteMaps[0]; } else m_PMap = PaletteMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return true; } return false; }); } else { m_FrameCount = Reader.ReadUInt32(); m_PaletteID = Reader.ReadUInt32(); //Find and set the correct palettemap... if (PaletteMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PaletteMaps[0]; } else m_PMap = PaletteMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return true; } return false; }); } } if (m_Version == 1001) { //Framecount may be set to -1 and should be ignored... while(true) { SpriteFrame Frame = new SpriteFrame(); Frame.Version = Reader.ReadUInt32(); Frame.Size = Reader.ReadUInt32(); Reader.ReadBytes(4); //Reserved. Frame.Height = Reader.ReadUInt16(); Frame.Width = Reader.ReadUInt16(); Frame.Init(true); //SPR#s don't have alpha channels, but alpha is used to plot transparent pixels. DecompressFrame2(ref Frame, ref Reader); Frame.BitmapData.Unlock(true); //The bitmapdata is locked when the frame is created. m_Frames.Add(Frame); if ((Reader.BaseStream.Position == Reader.BaseStream.Length) || (Reader.BaseStream.Position - Reader.BaseStream.Length < 14)) break; } } Reader.Close(); }
public SpriteParser(byte[] ChunkData, List <PaletteMap> PMaps, int ChunkID) { m_ChunkID = ChunkID; MemoryStream MemStream = new MemoryStream(ChunkData); BinaryReader Reader = new BinaryReader(MemStream); Reader.BaseStream.Position = INDEX_SPRID; m_ID = Reader.ReadByte(); Reader.BaseStream.Position = INDEX_SPRVERSION; m_Version = Reader.ReadUInt32(); if (m_Version != 1000 && m_Version != 1001) { throw new Exception("Version was: " + m_Version); } m_FrameCount = Reader.ReadUInt32(); if (m_Version == 1000) { m_PaletteID = Reader.ReadUInt32(); } bool PaletteFound = false; foreach (PaletteMap Map in PMaps) { if (Map.ID == m_PaletteID) { m_PMap = Map; PaletteFound = true; } } //This is guesswork, but when the PaletteID is 1, it seems to typically indicate //that there's only one palette to choose from... if (m_PaletteID == 1) { m_PMap = PMaps[0]; PaletteFound = true; } if (!PaletteFound) { throw new Exception("No palette found!"); } for (int i = 0; i < m_FrameCount; i++) { SpriteFrame Frame = new SpriteFrame(); Reader.BaseStream.Position = INDEX_SPROFFSETTABLE + 4 * i; Reader.BaseStream.Position = Reader.ReadUInt32() + INDEX_SPRVERSION; Frame.Width = Reader.ReadUInt16(); Frame.Height = Reader.ReadUInt16(); Frame.Flag = Reader.ReadUInt16(); //Zero value skipped. Reader.BaseStream.Position += 2; Frame.PaletteID = Reader.ReadUInt16(); Frame.PalMap = PMaps[0]; foreach (PaletteMap PMap in PMaps) { if (PMap.ID == m_PaletteID) { Frame.PalMap = PMap; } } Frame.TransparentPixel = m_PMap.GetColorAtIndex(Reader.ReadUInt16()); Frame.XLocation = Reader.ReadUInt16(); Frame.YLocation = Reader.ReadUInt16(); Frame.Init(); EncryptedRowHeader RowHeader = new EncryptedRowHeader(); int RowID = 0; for (int j = 0; j < Frame.Height; j++) { long InitPos = Reader.BaseStream.Position; RowHeader = GetDecryptedValues(Reader.ReadUInt16()); if (RowHeader.Code == 0) { //byte[] RowSegmentData = Reader.ReadBytes(RowHeader.Count - 2); ReadRowSegment(Reader, RowID, ref Frame); Reader.BaseStream.Position = InitPos + RowHeader.Count; RowID++; } else if (RowHeader.Code == 4) { RowID += RowHeader.Count; } else if (RowHeader.Code == 5) { break; } } m_Frames.Add(Frame); } Reader.Close(); }
public SPR2Parser(IffChunk Chunk, List<PaletteMap> PMaps) : base(Chunk) { m_Name = Name; int[] offsets; MemoryStream MemStream = new MemoryStream(Chunk.Data); BinaryReader Reader = new BinaryReader(MemStream); m_Version = Reader.ReadUInt32(); //In version 1001, all frames are decompressed from the beginning, and there's no point in storing //the compressed data AS WELL as the decompressed frames...! if (m_Version == 1000) m_ChunkData = Chunk.Data; if (m_Version == 1001) { m_PaletteID = Reader.ReadUInt32(); m_FrameCount = Reader.ReadUInt32(); } else { m_FrameCount = Reader.ReadUInt32(); m_PaletteID = Reader.ReadUInt32(); } if (PMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PMaps[0]; } else m_PMap = PMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return true; } return false; }); //Some SPR2s blatantly specify the wrong ID because there's only one palettemap... if (m_PMap == null) { m_PMap = PMaps[0]; } offsets = new int[m_FrameCount]; if (m_Version == 1000) { for (int i = 0; i < m_FrameCount; i++) { offsets[i] = Reader.ReadInt32(); m_FrameOffsets.Add(offsets[i]); } } if (m_Version == 1001) { for (int l = 0; l < m_FrameCount; l++) { SpriteFrame Frame = new SpriteFrame(); Frame.Version = Reader.ReadUInt32(); Frame.Size = Reader.ReadUInt32(); Frame.Width = Reader.ReadUInt16(); Frame.Height = Reader.ReadUInt16(); Frame.Flag = Reader.ReadUInt32(); Frame.PaletteID = Reader.ReadUInt16(); Frame.TransparentPixel = m_PMap.GetColorAtIndex(Reader.ReadUInt16()); Frame.YLocation = Reader.ReadUInt16(); Frame.XLocation = Reader.ReadUInt16(); if ((SPR2Flags)Frame.Flag == SPR2Flags.HasAlphaChannel) Frame.Init(true, true); else { if ((SPR2Flags)Frame.Flag == SPR2Flags.HasZBufferChannel) Frame.Init(false, true); else Frame.Init(false, false); } DecompressFrame2(ref Frame, ref Reader); Frame.BitmapData.Unlock(true); //The bitmapdata is locked when the frame is created. if (Frame.HasZBuffer) Frame.ZBuffer.Unlock(true); //The bitmapdata is locked when the frame is created. m_Frames.Add(Frame); } } Reader.Close(); }
public SpriteFrame ReadFrame(int Index) { BinaryReader Reader = new BinaryReader(new MemoryStream(m_ChunkData)); Reader.BaseStream.Seek(m_FrameOffsets[Index], SeekOrigin.Begin); SpriteFrame Frame = new SpriteFrame(); Frame.FrameIndex = (uint)Index; Frame.Width = Reader.ReadUInt16(); Frame.Height = Reader.ReadUInt16(); Frame.Flag = Reader.ReadUInt32(); Frame.PaletteID = Reader.ReadUInt16(); Frame.TransparentPixel = m_PMap.GetColorAtIndex(Reader.ReadUInt16()); Frame.YLocation = Reader.ReadUInt16(); Frame.XLocation = Reader.ReadUInt16(); if (Frame.Flag == 0x07) Frame.Init(true, true); else { if ((SPR2Flags)Frame.Flag == SPR2Flags.HasZBufferChannel) Frame.Init(false, true); else Frame.Init(false, false); } DecompressFrame2(ref Frame, ref Reader); Frame.BitmapData.Unlock(true); //The bitmapdata is locked when the frame is created. if (Frame.HasZBuffer) Frame.ZBuffer.Unlock(true); //The bitmapdata is locked when the frame is created. Reader.Close(); //Store the frame to avoid having to decompress in the future. m_Frames.Add(Frame); return Frame; }
public SPR2Parser(IffChunk Chunk, List <PaletteMap> PMaps) : base(Chunk) { m_Name = Name; int[] offsets; MemoryStream MemStream = new MemoryStream(Chunk.Data); BinaryReader Reader = new BinaryReader(MemStream); m_Version = Reader.ReadUInt32(); //In version 1001, all frames are decompressed from the beginning, and there's no point in storing //the compressed data AS WELL as the decompressed frames...! if (m_Version == 1000) { m_ChunkData = Chunk.Data; } if (m_Version == 1001) { m_PaletteID = Reader.ReadUInt32(); m_FrameCount = Reader.ReadUInt32(); } else { m_FrameCount = Reader.ReadUInt32(); m_PaletteID = Reader.ReadUInt32(); } if (PMaps.Count == 1 && m_PaletteID == 1) { m_PMap = PMaps[0]; } else { m_PMap = PMaps.Find(delegate(PaletteMap PMap) { if (PMap.ID == m_PaletteID) { return(true); } return(false); }); } //Some SPR2s blatantly specify the wrong ID because there's only one palettemap... if (m_PMap == null) { m_PMap = PMaps[0]; } offsets = new int[m_FrameCount]; if (m_Version == 1000) { for (int i = 0; i < m_FrameCount; i++) { offsets[i] = Reader.ReadInt32(); m_FrameOffsets.Add(offsets[i]); } } if (m_Version == 1001) { for (int l = 0; l < m_FrameCount; l++) { SpriteFrame Frame = new SpriteFrame(); Frame.Version = Reader.ReadUInt32(); Frame.Size = Reader.ReadUInt32(); Frame.Width = Reader.ReadUInt16(); Frame.Height = Reader.ReadUInt16(); Frame.Flag = Reader.ReadUInt32(); Frame.PaletteID = Reader.ReadUInt16(); Frame.TransparentPixel = m_PMap.GetColorAtIndex(Reader.ReadUInt16()); Frame.YLocation = Reader.ReadUInt16(); Frame.XLocation = Reader.ReadUInt16(); if ((SPR2Flags)Frame.Flag == SPR2Flags.HasAlphaChannel) { Frame.Init(true, true); } else { if ((SPR2Flags)Frame.Flag == SPR2Flags.HasZBufferChannel) { Frame.Init(false, true); } else { Frame.Init(false, false); } } DecompressFrame2(ref Frame, ref Reader); Frame.BitmapData.Unlock(true); //The bitmapdata is locked when the frame is created. if (Frame.HasZBuffer) { Frame.ZBuffer.Unlock(true); //The bitmapdata is locked when the frame is created. } m_Frames.Add(Frame); } } Reader.Close(); }
public SpriteParser(byte[] ChunkData, List<PaletteMap> PMaps, int ChunkID) { m_ChunkID = ChunkID; MemoryStream MemStream = new MemoryStream(ChunkData); BinaryReader Reader = new BinaryReader(MemStream); Reader.BaseStream.Position = INDEX_SPRID; m_ID = Reader.ReadByte(); Reader.BaseStream.Position = INDEX_SPRVERSION; m_Version = Reader.ReadUInt32(); if (m_Version != 1000 && m_Version != 1001) throw new Exception("Version was: " + m_Version); m_FrameCount = Reader.ReadUInt32(); if (m_Version == 1000) m_PaletteID = Reader.ReadUInt32(); bool PaletteFound = false; foreach (PaletteMap Map in PMaps) { if (Map.ID == m_PaletteID) { m_PMap = Map; PaletteFound = true; } } //This is guesswork, but when the PaletteID is 1, it seems to typically indicate //that there's only one palette to choose from... if (m_PaletteID == 1) { m_PMap = PMaps[0]; PaletteFound = true; } if (!PaletteFound) throw new Exception("No palette found!"); for (int i = 0; i < m_FrameCount; i++) { SpriteFrame Frame = new SpriteFrame(); Reader.BaseStream.Position = INDEX_SPROFFSETTABLE + 4 * i; Reader.BaseStream.Position = Reader.ReadUInt32() + INDEX_SPRVERSION; Frame.Width = Reader.ReadUInt16(); Frame.Height = Reader.ReadUInt16(); Frame.Flag = Reader.ReadUInt16(); //Zero value skipped. Reader.BaseStream.Position += 2; Frame.PaletteID = Reader.ReadUInt16(); Frame.PalMap = PMaps[0]; foreach (PaletteMap PMap in PMaps) { if (PMap.ID == m_PaletteID) Frame.PalMap = PMap; } Frame.TransparentPixel = m_PMap.GetColorAtIndex(Reader.ReadUInt16()); Frame.XLocation = Reader.ReadUInt16(); Frame.YLocation = Reader.ReadUInt16(); Frame.Init(); EncryptedRowHeader RowHeader = new EncryptedRowHeader(); int RowID = 0; for (int j = 0; j < Frame.Height; j++) { long InitPos = Reader.BaseStream.Position; RowHeader = GetDecryptedValues(Reader.ReadUInt16()); if (RowHeader.Code == 0) { //byte[] RowSegmentData = Reader.ReadBytes(RowHeader.Count - 2); ReadRowSegment(Reader, RowID, ref Frame); Reader.BaseStream.Position = InitPos + RowHeader.Count; RowID++; } else if (RowHeader.Code == 4) { RowID += RowHeader.Count; } else if (RowHeader.Code == 5) { break; } } m_Frames.Add(Frame); } Reader.Close(); }