Decode all slices in parallel

This commit is contained in:
Crsky
2026-04-12 19:58:22 +08:00
parent bf8e146139
commit 71a938273f

View File

@@ -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,22 +39,24 @@ 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))]
public class YdgFormat : ImageFormat public class YdgFormat : ImageFormat
{ {
public override string Tag { get { return "YDG"; } } public override string Tag { get { return "YDG"; } }
public override string Description { get { return "YU-RIS compressed image format"; } } public override string Description { get { return "YU-RIS compressed image format"; } }
public override uint Signature { get { return 0x00474459; } } // 'YDG\0' public override uint Signature { get { return 0x00474459; } } // 'YDG\0'
public YdgFormat() public YdgFormat ()
{ {
Extensions = new string[] { "ydg" }; Extensions = new string[] { "ydg" };
} }
@@ -67,21 +68,21 @@ 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),
Height = header.ToUInt16 (0x22), Height = header.ToUInt16 (0x22),
BPP = 32, BPP = 32,
HeaderLength = header.ToInt32 (0x10), HeaderLength = header.ToInt32 (0x10),
}; };
} }
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;
reader.Unpack(); var reader = new YdgReader (stream, meta);
return ImageData.Create (info, reader.Format, null, reader.Data); reader.Unpack ();
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,80 +93,84 @@ 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; } }
public YdgReader(IBinaryStream input, YdgMetaData info) public YdgReader (IBinaryStream input, YdgMetaData info)
{ {
m_input = input; m_input = input;
m_info = info; m_info = info;
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 (),
Height = m_input.ReadUInt16(), Y = y,
Height = m_input.ReadUInt16 (),
}; };
m_input.ReadUInt32(); y += slice.Height;
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,23 +180,18 @@ 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;
if (1 != WebPCodec.WebPGetInfo(data, (UIntPtr)data.Length, ref width, ref height)) WebPCodec.Load ();
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
{ {
@@ -200,20 +200,19 @@ namespace GameRes.Formats.YuRis
return output; return output;
} }
byte[] DecodeQoi(byte[] data, out int width, out int height) byte[] DecodeQoi (byte[] data, out int width, out int height)
{ {
using (var input = new BinMemoryStream(data)) using (var input = new BinMemoryStream (data))
{ {
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()); var channels = input.ReadByte ();
int channels = input.ReadByte(); var colorspace = input.ReadByte ();
int 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);
uint pixel = 0; uint pixel = 0;
var run = 0; var run = 0;
var dst = 0; var dst = 0;
@@ -225,10 +224,10 @@ namespace GameRes.Formats.YuRis
{ {
run = qoi.Read (out pixel); run = qoi.Read (out pixel);
} }
output[dst ] = (byte)pixel; output[dst ] = (byte)pixel;
output[dst + 1] = (byte)(pixel >> 8); output[dst+1] = (byte)(pixel >> 8);
output[dst + 2] = (byte)(pixel >> 16); output[dst+2] = (byte)(pixel >> 16);
output[dst + 3] = (byte)(pixel >> 24); output[dst+3] = (byte)(pixel >> 24);
dst += 4; dst += 4;
} }
return output; return output;