2015-04-06 21:11:15 +08:00
|
|
|
//! \file ImageAinos.cs
|
|
|
|
//! \date Sun Apr 05 04:16:27 2015
|
|
|
|
//! \brief Ainos 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;
|
2015-11-18 10:52:12 +08:00
|
|
|
using System.Linq;
|
2015-04-06 21:11:15 +08:00
|
|
|
using System.Text;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
using GameRes.Utility;
|
|
|
|
|
2015-11-18 10:52:12 +08:00
|
|
|
namespace GameRes.Formats.Ags
|
2015-04-06 21:11:15 +08:00
|
|
|
{
|
|
|
|
public class CgMetaData : ImageMetaData
|
|
|
|
{
|
|
|
|
public int Type;
|
|
|
|
public int Right, Bottom;
|
|
|
|
}
|
|
|
|
|
2015-11-18 10:52:12 +08:00
|
|
|
internal class AniEntry : Entry
|
|
|
|
{
|
|
|
|
public int FrameIndex;
|
|
|
|
public int FrameType;
|
|
|
|
public int KeyFrame;
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:11:15 +08:00
|
|
|
[Export(typeof(ArchiveFormat))]
|
|
|
|
public class AniOpener : ArchiveFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "ANI"; } }
|
2015-11-18 10:52:12 +08:00
|
|
|
public override string Description { get { return "Anime Game System animation resource"; } }
|
2015-04-06 21:11:15 +08:00
|
|
|
public override uint Signature { get { return 0; } }
|
|
|
|
public override bool IsHierarchic { get { return false; } }
|
2016-10-11 04:05:22 +08:00
|
|
|
public override bool CanWrite { get { return false; } }
|
2015-04-06 21:11:15 +08:00
|
|
|
|
|
|
|
public override ArcFile TryOpen (ArcView file)
|
|
|
|
{
|
2015-11-18 10:52:12 +08:00
|
|
|
uint first_offset = file.View.ReadUInt32 (0);
|
|
|
|
if (first_offset < 4 || file.MaxOffset > int.MaxValue || first_offset >= file.MaxOffset || 0 != (first_offset & 3))
|
|
|
|
return null;
|
|
|
|
int frame_count = (int)(first_offset / 4);
|
|
|
|
if (frame_count > 10000)
|
2015-04-06 21:11:15 +08:00
|
|
|
return null;
|
|
|
|
long index_offset = 4;
|
2015-11-18 10:52:12 +08:00
|
|
|
|
|
|
|
var frame_table = new uint[frame_count];
|
|
|
|
frame_table[0] = first_offset;
|
|
|
|
for (int i = 1; i < frame_count; ++i)
|
2015-04-06 21:11:15 +08:00
|
|
|
{
|
2015-11-18 10:52:12 +08:00
|
|
|
var offset = file.View.ReadUInt32 (index_offset);
|
|
|
|
index_offset += 4;
|
|
|
|
if (offset < first_offset || offset >= file.MaxOffset)
|
2015-04-06 21:11:15 +08:00
|
|
|
return null;
|
2015-11-18 10:52:12 +08:00
|
|
|
frame_table[i] = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
var frame_map = new Dictionary<uint, byte>();
|
|
|
|
foreach (var offset in frame_table)
|
|
|
|
{
|
|
|
|
if (!frame_map.ContainsKey (offset))
|
2015-04-06 21:11:15 +08:00
|
|
|
{
|
2015-11-18 10:52:12 +08:00
|
|
|
byte frame_type = file.View.ReadByte (offset);
|
|
|
|
if (frame_type >= 0x20)
|
|
|
|
return null;
|
|
|
|
frame_map[offset] = frame_type;
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
2015-11-18 10:52:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int last_key_frame = 0;
|
|
|
|
var dir = new List<Entry>();
|
|
|
|
for (int i = 0; i < frame_count; ++i)
|
|
|
|
{
|
|
|
|
var offset = frame_table[i];
|
|
|
|
int frame_type = frame_map[offset];
|
|
|
|
if (1 == frame_type)
|
|
|
|
continue;
|
|
|
|
frame_type &= 0xF;
|
|
|
|
if (0 == frame_type || 0xA == frame_type)
|
|
|
|
last_key_frame = dir.Count;
|
|
|
|
var entry = new AniEntry
|
|
|
|
{
|
|
|
|
Name = string.Format ("{0:D4}.tga", i),
|
|
|
|
Type = "image",
|
|
|
|
Offset = offset,
|
|
|
|
FrameType = frame_type,
|
|
|
|
KeyFrame = last_key_frame,
|
|
|
|
FrameIndex = dir.Count,
|
|
|
|
};
|
|
|
|
dir.Add (entry);
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
2015-04-19 19:21:41 +08:00
|
|
|
if (0 == dir.Count)
|
|
|
|
return null;
|
2015-11-18 10:52:12 +08:00
|
|
|
|
|
|
|
var ordered = dir.OrderBy (e => e.Offset).ToList();
|
|
|
|
for (int i = 0; i < ordered.Count; ++i)
|
|
|
|
{
|
|
|
|
var entry = ordered[i] as AniEntry;
|
|
|
|
long next_offset = file.MaxOffset;
|
|
|
|
for (int j = i+1; j <= ordered.Count; ++j)
|
|
|
|
{
|
|
|
|
next_offset = j == ordered.Count ? file.MaxOffset : ordered[j].Offset;
|
|
|
|
if (next_offset != entry.Offset)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
entry.Size = (uint)(next_offset - entry.Offset);
|
|
|
|
}
|
2015-04-06 21:11:15 +08:00
|
|
|
return new ArcFile (file, this, dir);
|
|
|
|
}
|
2015-11-18 10:52:12 +08:00
|
|
|
|
|
|
|
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
var ani = (AniEntry)entry;
|
|
|
|
byte[] key_frame = null;
|
|
|
|
if (ani.KeyFrame != ani.FrameIndex)
|
|
|
|
{
|
|
|
|
var dir = (List<Entry>)arc.Dir;
|
|
|
|
for (int i = ani.KeyFrame; i < ani.FrameIndex; ++i)
|
|
|
|
{
|
|
|
|
var frame = dir[i];
|
|
|
|
using (var s = arc.File.CreateStream (frame.Offset, frame.Size))
|
|
|
|
{
|
|
|
|
var frame_info = Cg.ReadMetaData (s) as CgMetaData;
|
|
|
|
if (null == frame_info)
|
|
|
|
break;
|
|
|
|
using (var reader = new CgFormat.Reader (s, frame_info, key_frame))
|
|
|
|
{
|
|
|
|
reader.Unpack();
|
|
|
|
key_frame = reader.Data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var input = arc.File.CreateStream (entry.Offset, entry.Size);
|
|
|
|
CgMetaData info = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
info = Cg.ReadMetaData (input) as CgMetaData;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
input.Dispose();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
if (null == info)
|
|
|
|
{
|
|
|
|
input.Position = 0;
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
using (input)
|
|
|
|
using (var reader = new CgFormat.Reader (input, info, key_frame))
|
|
|
|
{
|
|
|
|
reader.Unpack();
|
2015-11-30 07:50:03 +08:00
|
|
|
return TgaStream.Create (info, reader.Data);
|
2015-11-18 10:52:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Lazy<ImageFormat> s_Cg = new Lazy<ImageFormat> (() => ImageFormat.FindByTag ("CG"));
|
|
|
|
|
|
|
|
ImageFormat Cg { get { return s_Cg.Value; } }
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ImageFormat))]
|
|
|
|
public class CgFormat : ImageFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "CG"; } }
|
2015-11-18 10:52:12 +08:00
|
|
|
public override string Description { get { return "Anime Game System image format"; } }
|
2015-04-06 21:11:15 +08:00
|
|
|
public override uint Signature { get { return 0; } }
|
|
|
|
|
|
|
|
public override void Write (Stream file, ImageData image)
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException ("CgFormat.Write not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ImageMetaData ReadMetaData (Stream stream)
|
|
|
|
{
|
|
|
|
int sig = stream.ReadByte();
|
|
|
|
if (sig >= 0x20)
|
|
|
|
return null;
|
|
|
|
using (var input = new ArcView.Reader (stream))
|
|
|
|
{
|
|
|
|
int width = input.ReadInt16();
|
|
|
|
int height = input.ReadInt16();
|
|
|
|
if (width <= 0 || height <= 0 || width > 4096 || height > 4096)
|
|
|
|
return null;
|
|
|
|
var meta = new CgMetaData
|
|
|
|
{
|
|
|
|
Width = (uint)width,
|
|
|
|
Height = (uint)height,
|
|
|
|
BPP = 24,
|
|
|
|
Type = sig,
|
|
|
|
};
|
2016-02-15 03:12:11 +08:00
|
|
|
if (0 != (sig & 7))
|
2015-04-06 21:11:15 +08:00
|
|
|
{
|
|
|
|
meta.OffsetX = input.ReadInt16();
|
|
|
|
meta.OffsetY = input.ReadInt16();
|
|
|
|
meta.Right = input.ReadInt16();
|
|
|
|
meta.Bottom = input.ReadInt16();
|
|
|
|
if (meta.OffsetX > meta.Right || meta.OffsetY > meta.Bottom ||
|
|
|
|
meta.Right > width || meta.Bottom > height ||
|
|
|
|
meta.OffsetX < 0 || meta.OffsetY < 0)
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return meta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ImageData Read (Stream stream, ImageMetaData info)
|
|
|
|
{
|
2015-11-18 10:52:12 +08:00
|
|
|
var meta = (CgMetaData)info;
|
2015-04-06 21:11:15 +08:00
|
|
|
using (var reader = new Reader (stream, meta))
|
|
|
|
{
|
2015-11-18 10:52:12 +08:00
|
|
|
reader.Unpack();
|
2015-10-12 16:16:23 +08:00
|
|
|
return ImageData.Create (info, PixelFormats.Bgr24, null, reader.Data, (int)info.Width*3);
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class Reader : IDisposable
|
|
|
|
{
|
|
|
|
BinaryReader m_input;
|
|
|
|
byte[] m_output;
|
2015-11-18 10:52:12 +08:00
|
|
|
int m_type;
|
2015-04-06 21:11:15 +08:00
|
|
|
int m_width;
|
|
|
|
int m_height;
|
|
|
|
int m_left;
|
|
|
|
int m_top;
|
|
|
|
int m_right;
|
|
|
|
int m_bottom;
|
|
|
|
|
|
|
|
public byte[] Data { get { return m_output; } }
|
|
|
|
|
2015-11-18 10:52:12 +08:00
|
|
|
public Reader (Stream file, CgMetaData info, byte[] base_image = null)
|
2015-04-06 21:11:15 +08:00
|
|
|
{
|
2015-11-18 10:52:12 +08:00
|
|
|
m_type = info.Type;
|
2015-04-06 21:11:15 +08:00
|
|
|
m_width = (int)info.Width;
|
|
|
|
m_height = (int)info.Height;
|
|
|
|
m_left = info.OffsetX;
|
|
|
|
m_top = info.OffsetY;
|
|
|
|
m_right = info.Right == 0 ? m_width : info.Right;
|
|
|
|
m_bottom = info.Bottom == 0 ? m_height : info.Bottom;
|
2015-11-18 10:52:12 +08:00
|
|
|
m_output = base_image ?? new byte[3*m_width*m_height];
|
2015-04-06 21:11:15 +08:00
|
|
|
m_input = new BinaryReader (file, Encoding.ASCII, true);
|
|
|
|
ShiftTable = InitShiftTable();
|
2015-11-18 10:52:12 +08:00
|
|
|
|
2016-02-15 03:12:11 +08:00
|
|
|
if (0 != (info.Type & 7))
|
2015-11-18 10:52:12 +08:00
|
|
|
file.Position = 13;
|
|
|
|
else
|
|
|
|
file.Position = 5;
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static readonly short[] ShiftX = new short[] { // 409b6c
|
|
|
|
0, -1, -3, -2, -1, 0, 1, 2
|
|
|
|
};
|
|
|
|
static readonly short[] ShiftY = new short[] { // 409b7c
|
|
|
|
0, 0, -1, -1, -1, -1, -1, -1
|
|
|
|
};
|
|
|
|
readonly int[] ShiftTable;
|
|
|
|
|
|
|
|
private int[] InitShiftTable ()
|
|
|
|
{
|
|
|
|
var table = new int[8];
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
table[i] = 3 * (ShiftX[i] + ShiftY[i] * m_width);
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2015-11-18 10:52:12 +08:00
|
|
|
public void Unpack ()
|
|
|
|
{
|
|
|
|
if (0 != (m_type & 0x10))
|
|
|
|
UnpackRGB();
|
|
|
|
else
|
|
|
|
UnpackIndexed();
|
|
|
|
}
|
|
|
|
|
2015-04-06 21:11:15 +08:00
|
|
|
public void UnpackRGB ()
|
|
|
|
{
|
|
|
|
int right = 3 * (m_width * m_top + m_right);
|
|
|
|
int left = 3 * (m_width * m_top + m_left);
|
|
|
|
for (int i = m_top; i != m_bottom; ++i)
|
|
|
|
{
|
|
|
|
int dst = left;
|
|
|
|
while (dst != right)
|
|
|
|
{
|
|
|
|
byte v9 = m_input.ReadByte();
|
|
|
|
if (0 != (v9 & 0x80))
|
|
|
|
{
|
|
|
|
if (0 != (v9 & 0x40))
|
|
|
|
{
|
|
|
|
m_output[dst] = (byte)(m_output[dst - 3] + ((v9 >> 3) & 6) - 2);
|
|
|
|
m_output[dst + 1] = (byte)(m_output[dst - 2] + ((v9 >> 1) & 6) - 2);
|
|
|
|
m_output[dst + 2] = (byte)(m_output[dst - 1] + ((v9 & 3) + 127) * 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
byte v15 = m_input.ReadByte();
|
|
|
|
m_output[dst] = (byte)(((v9 << 1) + (v15 & 1)) << 1);
|
|
|
|
m_output[dst + 1] = (byte)(v15 & 0xfe);
|
|
|
|
m_output[dst + 2] = m_input.ReadByte();
|
|
|
|
}
|
|
|
|
dst += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint shift = (uint)v9 >> 4;
|
|
|
|
int count = v9 & 0xF;
|
|
|
|
if (0 == count)
|
|
|
|
{
|
|
|
|
count = (int)m_input.ReadByte() + 15;
|
|
|
|
if (270 == count)
|
|
|
|
{
|
|
|
|
int v12;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
v12 = m_input.ReadByte();
|
|
|
|
count += v12;
|
|
|
|
}
|
|
|
|
while (v12 == 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (0 != shift)
|
|
|
|
{
|
|
|
|
int src = dst + ShiftTable[shift];
|
2015-11-06 01:25:27 +08:00
|
|
|
Binary.CopyOverlapped (m_output, src, dst, count * 3);
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
2015-11-06 01:25:27 +08:00
|
|
|
dst += 3 * count;
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
|
|
|
left += m_width*3; //640*3;
|
|
|
|
right += m_width*3; //640*3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UnpackIndexed ()
|
|
|
|
{
|
2015-11-18 10:52:12 +08:00
|
|
|
byte[] palette = m_input.ReadBytes (0x180);
|
2015-04-06 21:11:15 +08:00
|
|
|
int right = 3 * (m_width * m_top + m_right);
|
|
|
|
int left = 3 * (m_width * m_top + m_left); // 3 * (Rect.left + 640 * Rect.top);
|
|
|
|
for (int i = m_top; i != m_bottom; ++i)
|
|
|
|
{
|
|
|
|
int dst = left;
|
|
|
|
while (dst != right)
|
|
|
|
{
|
|
|
|
byte v13 = m_input.ReadByte();
|
|
|
|
if (0 != (v13 & 0x80))
|
|
|
|
{
|
|
|
|
int color = 3 * (v13 & 0x7F);
|
|
|
|
m_output[dst] = palette[color];
|
|
|
|
m_output[dst+1] = palette[color+1];
|
|
|
|
m_output[dst+2] = palette[color+2];
|
|
|
|
dst += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint shift = (uint)v13 >> 4;
|
|
|
|
int count = v13 & 0xF;
|
|
|
|
if (0 == count)
|
|
|
|
{
|
|
|
|
count = m_input.ReadByte() + 15;
|
|
|
|
if (270 == count)
|
|
|
|
{
|
|
|
|
int v16;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
v16 = m_input.ReadByte();
|
|
|
|
count += v16;
|
|
|
|
}
|
|
|
|
while (v16 == 0xff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (0 != shift)
|
|
|
|
{
|
|
|
|
int src = dst + ShiftTable[shift];
|
2015-11-06 01:25:27 +08:00
|
|
|
Binary.CopyOverlapped (m_output, src, dst, count * 3);
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
2015-11-06 01:25:27 +08:00
|
|
|
dst += 3 * count;
|
2015-04-06 21:11:15 +08:00
|
|
|
}
|
|
|
|
right += m_width*3;
|
|
|
|
left += m_width*3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
bool disposed = false;
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
m_input.Dispose();
|
|
|
|
}
|
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|