mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
Decode all slices in parallel
This commit is contained in:
@@ -23,15 +23,14 @@
|
|||||||
// IN THE SOFTWARE.
|
// IN THE SOFTWARE.
|
||||||
//
|
//
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.ComponentModel.Composition;
|
|
||||||
using System.IO;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Windows.Media;
|
|
||||||
using GameRes.Formats.QoiCodec;
|
using GameRes.Formats.QoiCodec;
|
||||||
using GameRes.Utility;
|
using GameRes.Utility;
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel.Composition;
|
||||||
|
using System.IO;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Media;
|
||||||
|
|
||||||
namespace GameRes.Formats.YuRis
|
namespace GameRes.Formats.YuRis
|
||||||
{
|
{
|
||||||
@@ -40,12 +39,14 @@ namespace GameRes.Formats.YuRis
|
|||||||
public int HeaderLength;
|
public int HeaderLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class YdgTile
|
internal class YdgSlice
|
||||||
{
|
{
|
||||||
public uint Offset;
|
public uint Offset;
|
||||||
public uint Size;
|
public uint Size;
|
||||||
public ushort X;
|
public byte[] Data;
|
||||||
public ushort Height;
|
public int X;
|
||||||
|
public int Y;
|
||||||
|
public int Height;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Export(typeof(ImageFormat))]
|
[Export(typeof(ImageFormat))]
|
||||||
@@ -67,7 +68,6 @@ namespace GameRes.Formats.YuRis
|
|||||||
return null;
|
return null;
|
||||||
if (!header.AsciiEqual (4, "YU-RIS"))
|
if (!header.AsciiEqual (4, "YU-RIS"))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return new YdgMetaData
|
return new YdgMetaData
|
||||||
{
|
{
|
||||||
Width = header.ToUInt16 (0x20),
|
Width = header.ToUInt16 (0x20),
|
||||||
@@ -79,9 +79,10 @@ namespace GameRes.Formats.YuRis
|
|||||||
|
|
||||||
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
|
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
|
||||||
{
|
{
|
||||||
var reader = new YdgReader(stream, (YdgMetaData)info);
|
var meta = (YdgMetaData) info;
|
||||||
|
var reader = new YdgReader (stream, meta);
|
||||||
reader.Unpack ();
|
reader.Unpack ();
|
||||||
return ImageData.Create (info, reader.Format, null, reader.Data);
|
return ImageData.Create (meta, reader.Format, null, reader.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Write (Stream file, ImageData image)
|
public override void Write (Stream file, ImageData image)
|
||||||
@@ -92,8 +93,9 @@ namespace GameRes.Formats.YuRis
|
|||||||
|
|
||||||
internal sealed class YdgReader
|
internal sealed class YdgReader
|
||||||
{
|
{
|
||||||
IBinaryStream m_input;
|
readonly IBinaryStream m_input;
|
||||||
YdgMetaData m_info;
|
readonly YdgMetaData m_info;
|
||||||
|
readonly byte[] m_output;
|
||||||
|
|
||||||
public PixelFormat Format { get; private set; }
|
public PixelFormat Format { get; private set; }
|
||||||
public byte[] Data { get { return m_output; } }
|
public byte[] Data { get { return m_output; } }
|
||||||
@@ -105,67 +107,70 @@ namespace GameRes.Formats.YuRis
|
|||||||
m_output = new byte[4*(int)info.Width*(int)info.Height];
|
m_output = new byte[4*(int)info.Width*(int)info.Height];
|
||||||
Format = PixelFormats.Bgra32;
|
Format = PixelFormats.Bgra32;
|
||||||
}
|
}
|
||||||
byte[] m_output;
|
|
||||||
|
|
||||||
public void Unpack ()
|
public void Unpack ()
|
||||||
{
|
{
|
||||||
var tiles = ReadIndex ();
|
var slices = ReadSlices ();
|
||||||
int width = (int)m_info.Width;
|
var options = new ParallelOptions { MaxDegreeOfParallelism = 4 };
|
||||||
int height = (int)m_info.Height;
|
Parallel.ForEach (slices, options, slice =>
|
||||||
int dst_y = 0;
|
|
||||||
foreach (var tile in tiles)
|
|
||||||
{
|
{
|
||||||
byte[] pixels = ReadTile (tile, out width, out height);
|
var pixels = DecodeSlice (slice.Data, out int slice_width, out int slice_height);
|
||||||
|
if (slice_height > slice.Height)
|
||||||
int tile_height = Math.Min(height, tile.Height);
|
throw new InvalidFormatException ();
|
||||||
CopyTile (pixels, width, tile_height, tile.X, dst_y);
|
var height = Math.Min (slice_height, slice.Height);
|
||||||
dst_y += tile_height;
|
CopySlice (pixels, slice_width, height, slice.X, slice.Y);
|
||||||
if (dst_y >= m_info.Height)
|
});
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
YdgTile[] ReadIndex ()
|
YdgSlice[] ReadSlices ()
|
||||||
{
|
{
|
||||||
m_input.Position = m_info.HeaderLength;
|
m_input.Position = m_info.HeaderLength;
|
||||||
int tileCount = m_input.ReadInt32();
|
var count = m_input.ReadInt32 ();
|
||||||
var dir = new YdgTile[tileCount];
|
if (count <= 0)
|
||||||
for (int i = 0; i < dir.Length; ++i)
|
throw new InvalidFormatException ();
|
||||||
|
var slices = new YdgSlice[count];
|
||||||
|
var y = 0;
|
||||||
|
for (var i = 0; i < slices.Length; i++)
|
||||||
{
|
{
|
||||||
dir[i] = new YdgTile
|
var slice = new YdgSlice
|
||||||
{
|
{
|
||||||
Offset = m_input.ReadUInt32 (),
|
Offset = m_input.ReadUInt32 (),
|
||||||
Size = m_input.ReadUInt32 (),
|
Size = m_input.ReadUInt32 (),
|
||||||
X = m_input.ReadUInt16 (),
|
X = m_input.ReadUInt16 (),
|
||||||
|
Y = y,
|
||||||
Height = m_input.ReadUInt16 (),
|
Height = m_input.ReadUInt16 (),
|
||||||
};
|
};
|
||||||
|
y += slice.Height;
|
||||||
m_input.ReadUInt32 ();
|
m_input.ReadUInt32 ();
|
||||||
|
slices[i] = slice;
|
||||||
}
|
}
|
||||||
return dir;
|
foreach (var slice in slices)
|
||||||
|
{
|
||||||
|
m_input.Position = slice.Offset;
|
||||||
|
slice.Data = m_input.ReadBytes ((int)slice.Size);
|
||||||
|
}
|
||||||
|
return slices;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] ReadTile (YdgTile tile, out int width, out int height)
|
byte[] DecodeSlice (byte[] data, out int width, out int height)
|
||||||
{
|
{
|
||||||
m_input.Position = tile.Offset;
|
|
||||||
var data = m_input.ReadBytes ((int)tile.Size);
|
|
||||||
if (Binary.AsciiEqual (data, 0, "RIFF") && Binary.AsciiEqual (data, 8, "WEBP"))
|
if (Binary.AsciiEqual (data, 0, "RIFF") && Binary.AsciiEqual (data, 8, "WEBP"))
|
||||||
return DecodeWebP (data, out width, out height);
|
return DecodeWebP (data, out width, out height);
|
||||||
return DecodeQoi (data, out width, out height);
|
return DecodeQoi (data, out width, out height);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CopyTile (byte[] pixels, int src_width, int src_height, int dst_x, int dst_y)
|
void CopySlice (byte[] pixels, int src_width, int src_height, int dst_x, int dst_y)
|
||||||
{
|
{
|
||||||
int copy_width = Math.Min (src_width, (int)m_info.Width - dst_x);
|
var copy_width = Math.Min (src_width, (int)m_info.Width-dst_x);
|
||||||
int copy_height = Math.Min (src_height, (int)m_info.Height - dst_y);
|
var copy_height = Math.Min (src_height, (int)m_info.Height-dst_y);
|
||||||
if (copy_width <= 0 || copy_height <= 0)
|
if (copy_width <= 0 || copy_height <= 0)
|
||||||
return;
|
return;
|
||||||
|
var src_stride = 4*src_width;
|
||||||
int src_stride = src_width * 4;
|
var dst_stride = 4*(int)m_info.Width;
|
||||||
int dst_stride = (int)m_info.Width * 4;
|
var dst = dst_y*dst_stride + 4*dst_x;
|
||||||
int dst = dst_y * dst_stride + dst_x * 4;
|
var row_bytes = 4*copy_width;
|
||||||
int row_bytes = copy_width * 4;
|
var src = 0;
|
||||||
int src = 0;
|
for (var y = 0; y < copy_height; y++)
|
||||||
for (int y = 0; y < copy_height; ++y)
|
|
||||||
{
|
{
|
||||||
Buffer.BlockCopy (pixels, src, m_output, dst, row_bytes);
|
Buffer.BlockCopy (pixels, src, m_output, dst, row_bytes);
|
||||||
src += src_stride;
|
src += src_stride;
|
||||||
@@ -175,24 +180,19 @@ namespace GameRes.Formats.YuRis
|
|||||||
|
|
||||||
byte[] DecodeWebP (byte[] data, out int width, out int height)
|
byte[] DecodeWebP (byte[] data, out int width, out int height)
|
||||||
{
|
{
|
||||||
WebPCodec.Load ();
|
|
||||||
|
|
||||||
width = 0;
|
width = 0;
|
||||||
height = 0;
|
height = 0;
|
||||||
|
WebPCodec.Load ();
|
||||||
if (1 != WebPCodec.WebPGetInfo (data, (UIntPtr)data.Length, ref width, ref height))
|
if (1 != WebPCodec.WebPGetInfo (data, (UIntPtr)data.Length, ref width, ref height))
|
||||||
throw new InvalidFormatException ("WebP image decoder failed.");
|
throw new InvalidFormatException ("WebP image decoder failed.");
|
||||||
|
var stride = 4*width;
|
||||||
int stride = width * 4;
|
|
||||||
var output = new byte[stride*height];
|
var output = new byte[stride*height];
|
||||||
var handle = GCHandle.Alloc (output, GCHandleType.Pinned);
|
var handle = GCHandle.Alloc (output, GCHandleType.Pinned);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (IntPtr.Zero == WebPCodec.WebPDecodeBGRAInto (data, (UIntPtr)data.Length,
|
if (IntPtr.Zero == WebPCodec.WebPDecodeBGRAInto (data, (UIntPtr)data.Length, handle.AddrOfPinnedObject (), (UIntPtr)output.Length, stride))
|
||||||
handle.AddrOfPinnedObject (), (UIntPtr)output.Length, stride))
|
|
||||||
{
|
|
||||||
throw new InvalidFormatException ("WebP image decoder failed.");
|
throw new InvalidFormatException ("WebP image decoder failed.");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
handle.Free ();
|
handle.Free ();
|
||||||
@@ -206,11 +206,10 @@ namespace GameRes.Formats.YuRis
|
|||||||
{
|
{
|
||||||
if (0x66696F71 != input.ReadInt32 ()) // "qoif"
|
if (0x66696F71 != input.ReadInt32 ()) // "qoif"
|
||||||
throw new InvalidFormatException ("Invalid QOI signature");
|
throw new InvalidFormatException ("Invalid QOI signature");
|
||||||
|
|
||||||
width = (int)Binary.BigEndian (input.ReadUInt32 ());
|
width = (int)Binary.BigEndian (input.ReadUInt32 ());
|
||||||
height = (int)Binary.BigEndian (input.ReadUInt32 ());
|
height = (int)Binary.BigEndian (input.ReadUInt32 ());
|
||||||
int channels = input.ReadByte();
|
var channels = input.ReadByte ();
|
||||||
int colorspace = input.ReadByte();
|
var colorspace = input.ReadByte ();
|
||||||
var count = 4*width*height;
|
var count = 4*width*height;
|
||||||
var output = new byte[count];
|
var output = new byte[count];
|
||||||
var qoi = new QoiDecodeStream (input);
|
var qoi = new QoiDecodeStream (input);
|
||||||
|
|||||||
Reference in New Issue
Block a user