2016-01-24 08:10:39 +08:00
|
|
|
//! \file ArcANM.cs
|
|
|
|
//! \date Sat Jan 23 04:23:39 2016
|
|
|
|
//! \brief KaGuYa script engine animation resource.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2016 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.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
2016-10-26 09:10:30 +08:00
|
|
|
using System.Windows.Media;
|
2016-01-24 08:10:39 +08:00
|
|
|
|
|
|
|
namespace GameRes.Formats.Kaguya
|
|
|
|
{
|
|
|
|
internal class AnmArchive : ArcFile
|
|
|
|
{
|
|
|
|
public readonly ImageMetaData ImageInfo;
|
|
|
|
|
|
|
|
public AnmArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, ImageMetaData base_info)
|
|
|
|
: base (arc, impl, dir)
|
|
|
|
{
|
|
|
|
ImageInfo = base_info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 00:52:42 +08:00
|
|
|
internal class AnmEntry : Entry
|
|
|
|
{
|
|
|
|
public long ImageDataOffset;
|
|
|
|
public uint ImageDataSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal interface IAnmReader
|
|
|
|
{
|
|
|
|
List<Entry> GetFramesList (IBinaryStream input);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class AnmOpenerBase : ArchiveFormat, IAnmReader
|
2016-01-24 08:10:39 +08:00
|
|
|
{
|
|
|
|
public override string Description { get { return "KaGuYa script engine animation resource"; } }
|
|
|
|
public override bool IsHierarchic { get { return false; } }
|
2016-10-11 04:05:22 +08:00
|
|
|
public override bool CanWrite { get { return false; } }
|
2016-01-24 08:10:39 +08:00
|
|
|
|
2022-05-04 00:52:42 +08:00
|
|
|
public AnmOpenerBase ()
|
2016-01-24 08:10:39 +08:00
|
|
|
{
|
|
|
|
Extensions = new string[] { "anm" };
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ArcFile TryOpen (ArcView file)
|
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
using (var input = file.CreateStream())
|
|
|
|
{
|
|
|
|
var dir = GetFramesList (input);
|
|
|
|
if (null == dir)
|
|
|
|
return null;
|
|
|
|
var base_info = GetBaseInfo (input);
|
|
|
|
string base_name = Path.GetFileNameWithoutExtension (file.Name);
|
|
|
|
int i = 0;
|
|
|
|
foreach (var entry in dir)
|
|
|
|
{
|
|
|
|
entry.Name = string.Format ("{0}#{1:D2}", base_name, i++);
|
|
|
|
entry.Type = "image";
|
|
|
|
}
|
|
|
|
return new AnmArchive (file, this, dir, base_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override IImageDecoder OpenImage (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
var base_info = ((AnmArchive)arc).ImageInfo;
|
|
|
|
var input = arc.File.CreateStream (entry.Offset, entry.Size);
|
|
|
|
return CreateDecoder (input, base_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal virtual ImageMetaData GetBaseInfo (IBinaryStream input)
|
|
|
|
{
|
|
|
|
input.Position = 4;
|
|
|
|
return new ImageMetaData
|
2016-01-24 08:10:39 +08:00
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
OffsetX = input.ReadInt32(),
|
|
|
|
OffsetY = input.ReadInt32(),
|
|
|
|
Width = input.ReadUInt32(),
|
|
|
|
Height = input.ReadUInt32(),
|
2016-01-24 08:10:39 +08:00
|
|
|
BPP = 32,
|
|
|
|
};
|
2022-05-04 00:52:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract List<Entry> GetFramesList (IBinaryStream input);
|
|
|
|
|
|
|
|
public abstract IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(ArchiveFormat))]
|
|
|
|
public class AnmOpener : AnmOpenerBase
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "ANM/KAGUYA"; } }
|
|
|
|
public override uint Signature { get { return 0x30304E41; } } // 'AN00'
|
|
|
|
|
|
|
|
public override List<Entry> GetFramesList (IBinaryStream file)
|
|
|
|
{
|
|
|
|
file.Position = 0x14;
|
|
|
|
int frame_count = file.ReadInt16();
|
|
|
|
file.Position = 0x18 + frame_count * 4;
|
|
|
|
int count = file.ReadInt16();
|
|
|
|
if (!IsSaneCount (count))
|
|
|
|
return null;
|
|
|
|
var current_offset = file.Position;
|
2016-01-24 08:10:39 +08:00
|
|
|
var dir = new List<Entry> (count);
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
file.Position = current_offset + 8;
|
|
|
|
uint width = file.ReadUInt32();
|
|
|
|
uint height = file.ReadUInt32();
|
|
|
|
uint image_size = 4*width*height;
|
|
|
|
var entry = new AnmEntry
|
2016-01-24 08:10:39 +08:00
|
|
|
{
|
|
|
|
Offset = current_offset,
|
2022-05-04 00:52:42 +08:00
|
|
|
Size = 0x10 + image_size,
|
|
|
|
ImageDataOffset = current_offset + 0x10,
|
|
|
|
ImageDataSize = image_size,
|
2016-01-24 08:10:39 +08:00
|
|
|
};
|
|
|
|
dir.Add (entry);
|
|
|
|
current_offset += entry.Size;
|
|
|
|
}
|
2022-05-04 00:52:42 +08:00
|
|
|
return dir;
|
2016-01-24 08:10:39 +08:00
|
|
|
}
|
|
|
|
|
2022-05-04 00:52:42 +08:00
|
|
|
public override IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info)
|
2016-01-24 08:10:39 +08:00
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
return new An00Decoder (input, info);
|
2016-10-26 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class An00Decoder : BinaryImageDecoder
|
|
|
|
{
|
|
|
|
public An00Decoder (IBinaryStream input, ImageMetaData base_info) : base (input)
|
|
|
|
{
|
|
|
|
Info = new ImageMetaData
|
2016-01-24 08:10:39 +08:00
|
|
|
{
|
2016-10-26 09:10:30 +08:00
|
|
|
OffsetX = base_info.OffsetX + m_input.ReadInt32(),
|
|
|
|
OffsetY = base_info.OffsetY + m_input.ReadInt32(),
|
|
|
|
Width = m_input.ReadUInt32(),
|
|
|
|
Height = m_input.ReadUInt32(),
|
|
|
|
BPP = 32,
|
2016-01-24 08:10:39 +08:00
|
|
|
};
|
2016-10-26 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override ImageData GetImageData ()
|
|
|
|
{
|
|
|
|
m_input.Position = 0x10;
|
|
|
|
int stride = 4*(int)Info.Width;
|
|
|
|
var pixels = m_input.ReadBytes (stride*(int)Info.Height);
|
|
|
|
return ImageData.CreateFlipped (Info, PixelFormats.Bgra32, null, pixels, stride);
|
2016-01-24 08:10:39 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-22 18:22:14 +08:00
|
|
|
|
2022-04-29 17:25:49 +08:00
|
|
|
[Export(typeof(ArchiveFormat))]
|
2022-05-04 00:52:42 +08:00
|
|
|
public class An10Opener : AnmOpenerBase, IAnmReader
|
2022-04-29 17:25:49 +08:00
|
|
|
{
|
|
|
|
public override string Tag { get { return "AN10/KAGUYA"; } }
|
|
|
|
public override uint Signature { get { return 0x30314E41; } } // 'AN10'
|
|
|
|
|
2022-05-04 00:52:42 +08:00
|
|
|
public override List<Entry> GetFramesList (IBinaryStream file)
|
2022-04-29 17:25:49 +08:00
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
file.Position = 0x14;
|
|
|
|
int frame_count = file.ReadInt16();
|
|
|
|
file.Position = 0x18 + frame_count * 4;
|
|
|
|
int count = file.ReadInt16();
|
2022-04-29 17:25:49 +08:00
|
|
|
if (!IsSaneCount (count))
|
|
|
|
return null;
|
2022-05-04 00:52:42 +08:00
|
|
|
var current_offset = file.Position;
|
2022-04-29 17:25:49 +08:00
|
|
|
var dir = new List<Entry> (count);
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
file.Position = current_offset + 8;
|
|
|
|
uint width = file.ReadUInt32();
|
|
|
|
uint height = file.ReadUInt32();
|
|
|
|
uint channels = file.ReadUInt32();
|
|
|
|
uint image_size = channels*width*height;
|
|
|
|
var entry = new AnmEntry
|
2022-04-29 17:25:49 +08:00
|
|
|
{
|
|
|
|
Offset = current_offset,
|
2022-05-04 00:52:42 +08:00
|
|
|
Size = 0x14 + image_size,
|
|
|
|
ImageDataOffset = current_offset + 0x14,
|
|
|
|
ImageDataSize = image_size,
|
2022-04-29 17:25:49 +08:00
|
|
|
};
|
|
|
|
dir.Add (entry);
|
|
|
|
current_offset += entry.Size;
|
|
|
|
}
|
2022-05-04 00:52:42 +08:00
|
|
|
return dir;
|
2022-04-29 17:25:49 +08:00
|
|
|
}
|
|
|
|
|
2022-05-04 00:52:42 +08:00
|
|
|
public override IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info)
|
2022-04-29 17:25:49 +08:00
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
return new An10Decoder (input, info);
|
2022-04-29 17:25:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class An10Decoder : BinaryImageDecoder
|
|
|
|
{
|
|
|
|
public An10Decoder (IBinaryStream input, ImageMetaData base_info) : base (input)
|
|
|
|
{
|
|
|
|
Info = new ImageMetaData
|
|
|
|
{
|
|
|
|
OffsetX = base_info.OffsetX + m_input.ReadInt32(),
|
|
|
|
OffsetY = base_info.OffsetY + m_input.ReadInt32(),
|
|
|
|
Width = m_input.ReadUInt32(),
|
|
|
|
Height = m_input.ReadUInt32(),
|
|
|
|
BPP = m_input.ReadInt32() * 8,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override ImageData GetImageData ()
|
|
|
|
{
|
|
|
|
m_input.Position = 0x14;
|
2022-05-04 00:52:42 +08:00
|
|
|
int stride = Info.BPP / 8 * Info.iWidth;
|
|
|
|
var pixels = m_input.ReadBytes (stride*Info.iHeight);
|
2022-04-29 17:25:49 +08:00
|
|
|
PixelFormat format = 24 == Info.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgra32;
|
|
|
|
return ImageData.CreateFlipped (Info, format, null, pixels, stride);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:22:14 +08:00
|
|
|
[Export(typeof(ArchiveFormat))]
|
2022-05-04 00:52:42 +08:00
|
|
|
public class An20Opener : AnmOpenerBase
|
2016-10-22 18:22:14 +08:00
|
|
|
{
|
|
|
|
public override string Tag { get { return "AN20/KAGUYA"; } }
|
|
|
|
public override uint Signature { get { return 0x30324E41; } } // 'AN20'
|
|
|
|
|
2022-05-04 00:52:42 +08:00
|
|
|
public override List<Entry> GetFramesList (IBinaryStream file)
|
2016-10-22 18:22:14 +08:00
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
if (!SkipFrameTable (file))
|
|
|
|
return null;
|
|
|
|
int count = file.ReadInt16();
|
|
|
|
if (!IsSaneCount (count))
|
|
|
|
return null;
|
|
|
|
long current_offset = file.Position + 0x10;
|
|
|
|
var dir = new List<Entry> (count);
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
file.Position = current_offset + 8;
|
|
|
|
uint width = file.ReadUInt32();
|
|
|
|
uint height = file.ReadUInt32();
|
|
|
|
uint depth = file.ReadUInt32();
|
|
|
|
uint image_size = depth*width*height;
|
|
|
|
var entry = new AnmEntry
|
|
|
|
{
|
|
|
|
Offset = current_offset,
|
|
|
|
Size = 0x14 + image_size,
|
|
|
|
ImageDataOffset = current_offset + 0x14,
|
|
|
|
ImageDataSize = image_size,
|
|
|
|
};
|
|
|
|
dir.Add (entry);
|
|
|
|
current_offset += entry.Size;
|
|
|
|
}
|
|
|
|
return dir;
|
2016-10-22 18:22:14 +08:00
|
|
|
}
|
|
|
|
|
2022-05-04 00:52:42 +08:00
|
|
|
internal override ImageMetaData GetBaseInfo (IBinaryStream input)
|
2016-10-22 18:22:14 +08:00
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
SkipFrameTable (input);
|
|
|
|
input.ReadInt16();
|
|
|
|
return new ImageMetaData
|
|
|
|
{
|
|
|
|
OffsetX = input.ReadInt32(),
|
|
|
|
OffsetY = input.ReadInt32(),
|
|
|
|
Width = input.ReadUInt32(),
|
|
|
|
Height = input.ReadUInt32(),
|
|
|
|
BPP = 32,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkipFrameTable (IBinaryStream file)
|
|
|
|
{
|
|
|
|
file.Position = 4;
|
|
|
|
int table_count = file.ReadInt16();
|
|
|
|
file.Position = 8;
|
2016-10-22 18:22:14 +08:00
|
|
|
for (int i = 0; i < table_count; ++i)
|
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
switch (file.ReadByte())
|
2016-10-22 18:22:14 +08:00
|
|
|
{
|
|
|
|
case 0: break;
|
2022-05-04 00:52:42 +08:00
|
|
|
case 1: file.Seek (8, SeekOrigin.Current); break;
|
2016-10-22 18:22:14 +08:00
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 4:
|
2022-05-04 00:52:42 +08:00
|
|
|
case 5: file.Seek (4, SeekOrigin.Current); break;
|
|
|
|
default: return false;
|
2016-10-22 18:22:14 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-04 00:52:42 +08:00
|
|
|
int count = file.ReadUInt16();
|
|
|
|
file.Seek (count * 8, SeekOrigin.Current);
|
|
|
|
return true;
|
2016-10-22 18:22:14 +08:00
|
|
|
}
|
|
|
|
|
2022-05-04 00:52:42 +08:00
|
|
|
public override IImageDecoder CreateDecoder (IBinaryStream input, ImageMetaData info)
|
2016-10-22 18:22:14 +08:00
|
|
|
{
|
2022-05-04 00:52:42 +08:00
|
|
|
return new An20Decoder (input, info);
|
2016-10-26 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class An20Decoder : BinaryImageDecoder
|
|
|
|
{
|
|
|
|
public An20Decoder (IBinaryStream input, ImageMetaData base_info) : base (input)
|
|
|
|
{
|
|
|
|
Info = new ImageMetaData
|
2016-10-22 18:22:14 +08:00
|
|
|
{
|
2016-10-26 09:10:30 +08:00
|
|
|
OffsetX = base_info.OffsetX + m_input.ReadInt32(),
|
|
|
|
OffsetY = base_info.OffsetY + m_input.ReadInt32(),
|
|
|
|
Width = m_input.ReadUInt32(),
|
|
|
|
Height = m_input.ReadUInt32(),
|
|
|
|
BPP = m_input.ReadInt32() * 8,
|
2016-10-22 18:22:14 +08:00
|
|
|
};
|
2016-10-26 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override ImageData GetImageData ()
|
|
|
|
{
|
|
|
|
m_input.Position = 0x14;
|
2016-10-26 19:42:27 +08:00
|
|
|
int stride = Info.BPP/8*(int)Info.Width;
|
|
|
|
var pixels = m_input.ReadBytes (stride*(int)Info.Height);
|
2016-10-26 09:10:30 +08:00
|
|
|
return ImageData.CreateFlipped (Info, GetFormat(), null, pixels, stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
PixelFormat GetFormat ()
|
|
|
|
{
|
|
|
|
switch (Info.BPP)
|
|
|
|
{
|
|
|
|
case 8: return PixelFormats.Gray8;
|
|
|
|
case 24: return PixelFormats.Bgr24;
|
|
|
|
case 32: return PixelFormats.Bgra32;
|
|
|
|
default: throw new InvalidFormatException();
|
|
|
|
}
|
2016-10-22 18:22:14 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-24 08:10:39 +08:00
|
|
|
}
|