implemented IFL archives and GRD images.

This commit is contained in:
morkt 2015-04-12 23:02:29 +04:00
parent 3d4b74e34a
commit 8b6e7d869c
2 changed files with 245 additions and 0 deletions

69
ArcFormats/ArcIFL.cs Normal file
View File

@ -0,0 +1,69 @@
//! \file ArcIFL.cs
//! \date Sun Apr 12 20:47:04 2015
//! \brief IFLS archive implementation.
//
// Copyright (C) 2015 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using GameRes.Utility;
namespace GameRes.Formats.Silky
{
[Export(typeof(ArchiveFormat))]
public class IfsOpener : ArchiveFormat
{
public override string Tag { get { return "IFL"; } }
public override string Description { get { return "Silky's engine resource archive"; } }
public override uint Signature { get { return 0x534c4649; } } // 'IFLS'
public override bool IsHierarchic { get { return false; } }
public override bool CanCreate { get { return false; } }
public override ArcFile TryOpen (ArcView file)
{
uint data_offset = file.View.ReadUInt32 (4);
int count = file.View.ReadInt32 (8);
if (data_offset <= 12 || data_offset >= file.MaxOffset
|| count <= 0 || count > 0xfffff)
return null;
var dir = new List<Entry> (count);
long index_offset = 12;
for (int i = 0; i < count; ++i)
{
string name = file.View.ReadString (index_offset, 0x10);
if (0 == name.Length)
return null;
var entry = FormatCatalog.Instance.CreateEntry (name);
entry.Offset = file.View.ReadUInt32 (index_offset+0x10);
entry.Size = file.View.ReadUInt32 (index_offset+0x14);
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
index_offset += 0x18;
}
return new ArcFile (file, this, dir);
}
}
}

176
ArcFormats/ImageGRD.cs Normal file
View File

@ -0,0 +1,176 @@
//! \file ImageGRD.cs
//! \date Sun Apr 12 21:05:44 2015
//! \brief Silky's GRD image format.
//
// Copyright (C) 2015 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameRes.Utility;
namespace GameRes.Formats.Silky
{
internal class GrdMetaData : ImageMetaData
{
public int DataSize;
}
[Export(typeof(ImageFormat))]
public class GrdFormat : ImageFormat
{
public override string Tag { get { return "GRD"; } }
public override string Description { get { return "Silky's RGB image format"; } }
public override uint Signature { get { return 0x5f504d43u; } } // 'CMP_'
public override void Write (Stream file, ImageData image)
{
throw new NotImplementedException ("GrdFormat.Write not implemented");
}
public override ImageMetaData ReadMetaData (Stream stream)
{
stream.Seek (4, SeekOrigin.Current);
int data_size = stream.ReadByte() | stream.ReadByte() << 8 | stream.ReadByte() << 16 | stream.ReadByte() << 24;
stream.Seek (4, SeekOrigin.Current);
using (var reader = new Reader (stream, 0x22)) // BMP header
{
reader.Unpack();
var bmp = reader.Data;
if (bmp[0] != 'B' || bmp[1] != 'M')
return null;
int width = LittleEndian.ToInt32 (bmp, 0x12);
int height = LittleEndian.ToInt32 (bmp, 0x16);
int bpp = LittleEndian.ToInt16 (bmp, 0x1c);
return new GrdMetaData
{
Width = (uint)width,
Height = (uint)height,
BPP = bpp,
DataSize = data_size,
};
}
}
public override ImageData Read (Stream stream, ImageMetaData info)
{
var meta = info as GrdMetaData;
if (null == meta)
throw new ArgumentException ("GrdFormat.Read should be supplied with GrdMetaData", "info");
stream.Position = 12;
using (var reader = new Reader (stream, meta.DataSize))
{
reader.Unpack();
byte[] pixels = reader.Data;
using (var bmp = new MemoryStream (reader.Data, false))
{
var decoder = new BmpBitmapDecoder (bmp,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
BitmapSource frame = decoder.Frames[0];
frame.Freeze();
return new ImageData (frame, info);
}
}
}
}
// custom LZSS reader
//
internal class Reader : IDisposable
{
Stream m_input;
byte[] m_output;
public byte[] Data { get { return m_output; } }
public Reader (Stream file, int dst_size)
{
m_input = file;
m_output = new byte[dst_size];
}
public void Unpack ()
{
var frame = new byte[0x1000];
int frame_pos = 0xfee;
for (int i = 0; i < frame_pos; ++i)
frame[i] = 32;
int ctl = 0;
int dst = 0;
while (dst < m_output.Length)
{
ctl >>= 1;
if (0 == (ctl & 0x100))
{
int b = m_input.ReadByte();
if (-1 == b)
break;
ctl = b | 0xff00;
}
if (0 != (ctl & 1))
{
int b = m_input.ReadByte();
if (-1 == b)
break;
m_output[dst++] = (byte)b;
frame[frame_pos++] = (byte)b;
frame_pos &= 0xfff;
}
else
{
int lo = m_input.ReadByte();
if (-1 == lo)
break;
int hi = m_input.ReadByte();
if (-1 == hi)
break;
int offset = (hi & 0xF0) << 4 | lo;
int count = (hi & 0xF) + 3;
for (int i = 0; i < count && dst < m_output.Length; ++i)
{
byte b = frame[(offset + i) & 0xfff];
m_output[dst++] = b;
frame[frame_pos++] = b;
frame_pos &= 0xfff;
}
}
}
}
#region IDisposable Members
public void Dispose ()
{
if (null != m_input)
{
m_input = null;
}
GC.SuppressFinalize (this);
}
#endregion
}
}