implemented IPF images.

This commit is contained in:
morkt 2017-01-15 15:08:08 +04:00
parent 5f1687efcd
commit cf1ce3c889
3 changed files with 206 additions and 0 deletions

View File

@ -334,6 +334,7 @@
<Compile Include="TamaSoft\ImageSUR.cs" /> <Compile Include="TamaSoft\ImageSUR.cs" />
<Compile Include="Taskforce\ArcDAT.cs" /> <Compile Include="Taskforce\ArcDAT.cs" />
<Compile Include="TechnoBrain\AudioWAPE.cs" /> <Compile Include="TechnoBrain\AudioWAPE.cs" />
<Compile Include="TechnoBrain\ImageIPF.cs" />
<Compile Include="TechnoBrain\ImageIPH.cs" /> <Compile Include="TechnoBrain\ImageIPH.cs" />
<Compile Include="DxLib\ArcDX.cs" /> <Compile Include="DxLib\ArcDX.cs" />
<Compile Include="ArcMiris.cs" /> <Compile Include="ArcMiris.cs" />

View File

@ -0,0 +1,203 @@
//! \file ImageIPF.cs
//! \date Sat Jan 14 13:26:13 2017
//! \brief TechnoBrain's "Inteligent Picture Format"
//
// Copyright (C) 2017 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.ComponentModel.Composition;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameRes.Utility;
namespace GameRes.Formats.TechnoBrain
{
internal class IpfMetaData : ImageMetaData
{
public bool HasPalette;
public bool IsCompressed;
public long PalOffset;
public int PalSize;
public long BmpOffset;
}
[Export(typeof(ImageFormat))]
public class IpfFormat : ImageFormat
{
public override string Tag { get { return "IPF"; } }
public override string Description { get { return "TechnoBrain's 'Inteligent Picture Format'"; } }
public override uint Signature { get { return 0; } } // 'RIFF'
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
// 'RIFF' isn't included into signature to avoid auto-detection of the WAV files as IPF images.
if (0x46464952 != file.Signature) // 'RIFF'
return null;
var header = file.ReadHeader (0x14);
if (!header.AsciiEqual (8, "IPF fmt "))
return null;
int fmt_size = header.ToInt32 (0x10);
if (fmt_size < 0x24)
return null;
header = file.ReadHeader (0x14 + fmt_size);
bool has_palette = header.ToInt32 (0x18) != 0;
bool has_bitmap = header.ToInt32 (0x28) != 0;
if (!has_bitmap)
return null;
var info = new IpfMetaData { BPP = 8, HasPalette = has_palette };
if (has_palette)
{
if (0x206C6170 != file.ReadInt32()) // 'pal '
return null;
info.PalSize = file.ReadInt32();
if (info.PalSize < 0x24)
return null;
info.PalOffset = file.Position;
file.Position = info.PalOffset + info.PalSize;
}
if (0x20706D62 != file.ReadInt32()) // 'bmp '
return null;
int bmp_size = file.ReadInt32();
if (bmp_size <= 0x20)
return null;
info.BmpOffset = file.Position + 0x18;
info.Width = file.ReadUInt16();
info.Height = file.ReadUInt16();
file.Seek (0xE, SeekOrigin.Current);
info.IsCompressed = 0 != (file.ReadByte() & 1);
return info;
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var reader = new IpfReader (file, (IpfMetaData)info);
var pixels = reader.Unpack();
return ImageData.Create (info, reader.Format, reader.Palette, pixels);
}
public override void Write (Stream file, ImageData image)
{
throw new System.NotImplementedException ("IpfFormat.Write not implemented");
}
}
internal sealed class IpfReader
{
IBinaryStream m_input;
IpfMetaData m_info;
byte[] m_output;
public BitmapPalette Palette { get; private set; }
public PixelFormat Format { get; private set; }
public byte[] Data { get { return m_output; } }
public IpfReader (IBinaryStream input, IpfMetaData info)
{
m_input = input;
m_info = info;
m_output = new byte[m_info.Width*m_info.Height];
Format = m_info.HasPalette ? PixelFormats.Indexed8 : PixelFormats.Gray8;
}
public byte[] Unpack ()
{
if (m_info.HasPalette)
{
UnpackPalette();
}
m_input.Position = m_info.BmpOffset;
if (m_info.IsCompressed)
UnpackBitmap();
else
m_input.Read (m_output, 0, m_output.Length);
return m_output;
}
void UnpackPalette ()
{
m_input.Position = m_info.PalOffset+4;
var header = m_input.ReadBytes (0x20);
var data = m_input.ReadBytes (m_info.PalSize - 0x24);
const int min_index = 0x0A;
const int max_index = 0xF6;
var color_map = new Color[0x100];
int src = 0;
for (int i = 0; i < 0x20; ++i)
{
var bits = header[i];
for (int j = 0; j < 8; ++j)
{
int dst = (i << 3) + j;
if (dst >= 0x0A && 0 != (bits & 0x80))
{
if (dst >= min_index && dst <= max_index)
{
color_map[dst] = Color.FromRgb (data[src], data[src+1], data[src+2]);
}
src += 3;
}
bits <<= 1;
}
}
Palette = new BitmapPalette (color_map);
}
void UnpackBitmap () // IPF_12
{
int dst = 0;
while (dst < m_output.Length)
{
byte ctl = m_input.ReadUInt8();
if (0xF == ctl)
{
break;
}
else if (0xE == ctl)
{
m_output[dst++] = ctl;
}
else if (ctl < 0x10)
{
int count = (ctl << 8 | m_input.ReadUInt8()) + 1;
byte b = m_input.ReadUInt8();
for (int i = 0; i < count; ++i)
m_output[dst++] = b;
}
else if (ctl < 0x20)
{
int offset = ((ctl - 0x10) << 8 | m_input.ReadUInt8()) + 1;
int count = m_input.ReadUInt8() + 1;
Binary.CopyOverlapped (m_output, dst - offset, dst, count);
dst += count;
}
else
{
m_output[dst++] = (byte)(ctl - 0x10);
}
}
}
}
}

View File

@ -975,12 +975,14 @@ Alfred Gakuen Mamono Daitai<br/>
Brown Doori Sanbanme<br/> Brown Doori Sanbanme<br/>
Daisounan<br/> Daisounan<br/>
Dancing Crazies<br/> Dancing Crazies<br/>
Hare nochi Tokidoki Munasawagi<br/>
Level Justice<br/> Level Justice<br/>
Mahiru ni Odoru Hanzaisha<br/> Mahiru ni Odoru Hanzaisha<br/>
Ouzoku<br/> Ouzoku<br/>
Planet Dragon<br/> Planet Dragon<br/>
</td></tr> </td></tr>
<tr><td>*.iph</td><td><tt>RIFF....IPH fmt</tt></td><td>No</td></tr> <tr><td>*.iph</td><td><tt>RIFF....IPH fmt</tt></td><td>No</td></tr>
<tr><td>*.ipf</td><td><tt>RIFF....IPF fmt</tt></td><td>No</td></tr>
<tr><td>*.aog</td><td><tt>AoiOgg</tt></td><td>No</td></tr> <tr><td>*.aog</td><td><tt>AoiOgg</tt></td><td>No</td></tr>
<tr><td>*.box</td><td><tt>AOIBOX4</tt><br/><tt>AOIBOX5</tt><br/><tt>AOIBOX7</tt><br/><tt>AOIBX10</tt><br/><tt>AOIBX12</tt><br/><tt>AOIMY01</tt></td><td>No</td></tr> <tr><td>*.box</td><td><tt>AOIBOX4</tt><br/><tt>AOIBOX5</tt><br/><tt>AOIBOX7</tt><br/><tt>AOIBX10</tt><br/><tt>AOIBX12</tt><br/><tt>AOIMY01</tt></td><td>No</td></tr>
<tr class="last"><td>*.agf</td><td><tt>AGF</tt></td><td>No</td></tr> <tr class="last"><td>*.agf</td><td><tt>AGF</tt></td><td>No</td></tr>