Compare commits

...

3 Commits

Author SHA1 Message Date
Crsky
71a938273f Decode all slices in parallel 2026-04-12 19:58:22 +08:00
Crsky
bf8e146139 Extract QoiCodec 2026-04-12 18:58:42 +08:00
Crsky
6cff1b96d4 Extract WebP Codec 2026-04-12 18:49:53 +08:00
6 changed files with 274 additions and 331 deletions

View File

@@ -268,6 +268,7 @@
<Compile Include="Ps1\ImageTIM.cs" />
<Compile Include="Psp\ArcGim.cs" />
<Compile Include="Psp\ArcQPK.cs" />
<Compile Include="QoiCodec.cs" />
<Compile Include="ScrPlayer\ImageIMG.cs" />
<Compile Include="SingleFileArchive.cs" />
<Compile Include="Software House Parsley\ArcCG3.cs" />
@@ -278,6 +279,7 @@
<Compile Include="VnMaker\AudioMP3.cs" />
<Compile Include="VnMaker\AudioOGG.cs" />
<Compile Include="VnMaker\ImagePNG.cs" />
<Compile Include="WebPCodec.cs" />
<Compile Include="Xuse\ArcNT.cs" />
<Compile Include="Xuse\ArcWVB.cs" />
<Compile Include="YuRis\ImageYDG.cs" />

View File

@@ -65,10 +65,10 @@ namespace GameRes.Formats.Artemis
data[j] -= (byte)(0x77 * j);
var slice_width = 0;
var slice_height = 0;
if (1 != WebPGetInfo (data, (UIntPtr)data.Length, ref slice_width, ref slice_height))
if (1 != WebPCodec.WebPGetInfo(data, (UIntPtr)data.Length, ref slice_width, ref slice_height))
throw new InvalidFormatException("WebP image decoder failed.");
var slice_size = stride * slice_height;
if (IntPtr.Zero == WebPDecodeBGRAInto (data, (UIntPtr)data.Length, output, (UIntPtr)slice_size, stride))
if (IntPtr.Zero == WebPCodec.WebPDecodeBGRAInto (data, (UIntPtr)data.Length, output, (UIntPtr)slice_size, stride))
throw new InvalidFormatException("WebP image decoder failed.");
output += slice_size;
}
@@ -94,7 +94,7 @@ namespace GameRes.Formats.Artemis
offset[i] = file.ReadInt32();
for (var i = 0; i < 8; i++)
length[i] = file.ReadInt32();
LibWebPLoader.Load();
WebPCodec.Load();
var image_width = 0;
var image_height = 0;
for (var i = 0; i < 8; i++)
@@ -111,7 +111,7 @@ namespace GameRes.Formats.Artemis
webp_header[j] -= (byte)(0x77 * j);
var slice_width = 0;
var slice_height = 0;
if (1 != WebPGetInfo (webp_header, (UIntPtr)webp_header.Length, ref slice_width, ref slice_height))
if (1 != WebPCodec.WebPGetInfo (webp_header, (UIntPtr)webp_header.Length, ref slice_width, ref slice_height))
throw new InvalidFormatException ("WebP image decoder failed.");
image_width = Math.Max (image_width, slice_width);
image_height += slice_height;
@@ -130,36 +130,5 @@ namespace GameRes.Formats.Artemis
{
throw new NotImplementedException("ImageNekoPNG.Write not implemented");
}
[DllImport("libwebp.dll", EntryPoint = "WebPGetInfo", CallingConvention = CallingConvention.Cdecl)]
public static extern int WebPGetInfo ([MarshalAs(UnmanagedType.LPArray)] byte[] data, UIntPtr data_size, ref int width, ref int height);
[DllImport("libwebp.dll", EntryPoint = "WebPDecodeBGRAInto", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr WebPDecodeBGRAInto ([MarshalAs(UnmanagedType.LPArray)] byte[] data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride);
}
internal static class LibWebPLoader
{
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
static extern IntPtr LoadLibraryEx (string lpFileName, IntPtr hReservedNull, uint dwFlags);
static bool loaded = false;
const uint LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x00000100;
const uint LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800;
public static void Load ()
{
if (loaded)
return;
var folder = Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location);
folder = Path.Combine (folder, (IntPtr.Size == 4) ? "x86" : "x64");
var fullPath = Path.Combine (folder, "libwebp.dll");
fullPath = Path.GetFullPath (fullPath);
var handle = LoadLibraryEx (fullPath, IntPtr.Zero, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (IntPtr.Zero == handle)
throw new Win32Exception (Marshal.GetLastWin32Error());
loaded = true;
}
}
}

View File

@@ -8,6 +8,7 @@
// C# port by morkt
//
using GameRes.Formats.QoiCodec;
using GameRes.Utility;
using K4os.Compression.LZ4;
using System;
@@ -1220,95 +1221,6 @@ namespace GameRes.Formats.KiriKiri
}
}
static class QoiCodec
{
public const int Index = 0x00;
public const int Diff = 0x40;
public const int Luma = 0x80;
public const int Run = 0xC0;
public const int Rgb = 0xFE;
public const int Rgba = 0xFF;
public const int Mask2 = 0xC0;
public const int HashTableSize = 64;
}
class QoiDecodeStream
{
readonly IBinaryStream m_input;
readonly byte[] m_table;
uint m_pixel;
public QoiDecodeStream (IBinaryStream input)
{
m_input = input;
m_table = new byte [4*QoiCodec.HashTableSize];
m_pixel = 0xFF000000;
}
public int Read (out uint output)
{
var r = (byte)m_pixel;
var g = (byte)(m_pixel >> 8);
var b = (byte)(m_pixel >> 16);
var a = (byte)(m_pixel >> 24);
var run = 1;
var b1 = m_input.ReadByte ();
if (-1 == b1)
throw new EndOfStreamException ();
if (QoiCodec.Rgb == b1)
{
var rgb = m_input.ReadInt24 ();
r = (byte)rgb;
g = (byte)(rgb >> 8);
b = (byte)(rgb >> 16);
}
else if (QoiCodec.Rgba == b1)
{
var rgba = m_input.ReadInt32 ();
r = (byte)rgba;
g = (byte)(rgba >> 8);
b = (byte)(rgba >> 16);
a = (byte)(rgba >> 24);
}
else if (QoiCodec.Index == (b1 & QoiCodec.Mask2))
{
var p1 = (b1 & ~QoiCodec.Mask2) * 4;
r = m_table[p1 ];
g = m_table[p1+1];
b = m_table[p1+2];
a = m_table[p1+3];
}
else if (QoiCodec.Diff == (b1 & QoiCodec.Mask2))
{
r += (byte)(((b1 >> 4) & 0x03) - 2);
g += (byte)(((b1 >> 2) & 0x03) - 2);
b += (byte)((b1 & 0x03) - 2);
}
else if (QoiCodec.Luma == (b1 & QoiCodec.Mask2))
{
var b2 = m_input.ReadByte ();
if (-1 == b2)
throw new EndOfStreamException ();
var vg = (b1 & 0x3F) - 32;
r += (byte)(vg - 8 + ((b2 >> 4) & 0x0F));
g += (byte)vg;
b += (byte)(vg - 8 + (b2 & 0x0F));
}
else if (QoiCodec.Run == (b1 & QoiCodec.Mask2))
{
run = (b1 & 0x3F) + 1;
}
var p2 = (r*3 + g*5 + b*7 + a*11) % QoiCodec.HashTableSize*4;
m_table[p2 ] = r;
m_table[p2+1] = g;
m_table[p2+2] = b;
m_table[p2+3] = a;
m_pixel = (uint)(r | (g << 8) | (b << 16) | (a << 24));
output = (uint)(b | (g << 8) | (r << 16) | (a << 24));
return run;
}
}
class QoiBlockDecoder
{
readonly QoiDecodeStream m_qoi;

118
ArcFormats/QoiCodec.cs Normal file
View File

@@ -0,0 +1,118 @@
//! \file QoiCodec.cs
//! \date Sun Apr 12 2026
//! \brief Quite OK Image Format
//
// 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.IO;
namespace GameRes.Formats.QoiCodec
{
static class QoiConst
{
public const int Index = 0x00;
public const int Diff = 0x40;
public const int Luma = 0x80;
public const int Run = 0xC0;
public const int Rgb = 0xFE;
public const int Rgba = 0xFF;
public const int Mask2 = 0xC0;
public const int HashTableSize = 64;
}
public class QoiDecodeStream
{
readonly IBinaryStream m_input;
readonly byte[] m_table;
uint m_pixel;
public QoiDecodeStream (IBinaryStream input)
{
m_input = input;
m_table = new byte [4*QoiConst.HashTableSize];
m_pixel = 0xFF000000;
}
public int Read (out uint output)
{
var r = (byte)m_pixel;
var g = (byte)(m_pixel >> 8);
var b = (byte)(m_pixel >> 16);
var a = (byte)(m_pixel >> 24);
var run = 1;
var b1 = m_input.ReadByte ();
if (-1 == b1)
throw new EndOfStreamException ();
if (QoiConst.Rgb == b1)
{
var rgb = m_input.ReadInt24 ();
r = (byte)rgb;
g = (byte)(rgb >> 8);
b = (byte)(rgb >> 16);
}
else if (QoiConst.Rgba == b1)
{
var rgba = m_input.ReadInt32 ();
r = (byte)rgba;
g = (byte)(rgba >> 8);
b = (byte)(rgba >> 16);
a = (byte)(rgba >> 24);
}
else if (QoiConst.Index == (b1 & QoiConst.Mask2))
{
var p1 = (b1 & ~QoiConst.Mask2) * 4;
r = m_table[p1 ];
g = m_table[p1+1];
b = m_table[p1+2];
a = m_table[p1+3];
}
else if (QoiConst.Diff == (b1 & QoiConst.Mask2))
{
r += (byte)(((b1 >> 4) & 0x03) - 2);
g += (byte)(((b1 >> 2) & 0x03) - 2);
b += (byte)((b1 & 0x03) - 2);
}
else if (QoiConst.Luma == (b1 & QoiConst.Mask2))
{
var b2 = m_input.ReadByte ();
if (-1 == b2)
throw new EndOfStreamException ();
var vg = (b1 & 0x3F) - 32;
r += (byte)(vg - 8 + ((b2 >> 4) & 0x0F));
g += (byte)vg;
b += (byte)(vg - 8 + (b2 & 0x0F));
}
else if (QoiConst.Run == (b1 & QoiConst.Mask2))
{
run = (b1 & 0x3F) + 1;
}
var p2 = (r*3 + g*5 + b*7 + a*11) % QoiConst.HashTableSize*4;
m_table[p2 ] = r;
m_table[p2+1] = g;
m_table[p2+2] = b;
m_table[p2+3] = a;
m_pixel = (uint)(r | (g << 8) | (b << 16) | (a << 24));
output = (uint)(b | (g << 8) | (r << 16) | (a << 24));
return run;
}
}
}

64
ArcFormats/WebPCodec.cs Normal file
View File

@@ -0,0 +1,64 @@
//! \file WebPCodec.cs
//! \date Sun Apr 12 2026
//! \brief Google WebP image format.
//
// 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;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace GameRes.Formats
{
public class WebPCodec
{
[DllImport("libwebp.dll", EntryPoint = "WebPGetInfo", CallingConvention = CallingConvention.Cdecl)]
public static extern int WebPGetInfo ([MarshalAs(UnmanagedType.LPArray)] byte[] data, UIntPtr data_size, ref int width, ref int height);
[DllImport("libwebp.dll", EntryPoint = "WebPDecodeBGRAInto", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr WebPDecodeBGRAInto ([MarshalAs(UnmanagedType.LPArray)] byte[] data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride);
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
static extern IntPtr LoadLibraryEx (string lpFileName, IntPtr hReservedNull, uint dwFlags);
static bool loaded = false;
const uint LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x00000100;
const uint LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800;
public static void Load ()
{
if (loaded)
return;
var dir = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
dir = Path.Combine (dir, (IntPtr.Size == 4) ? "x86" : "x64");
var path = Path.Combine (dir, "libwebp.dll");
path = Path.GetFullPath (path);
var lib = LoadLibraryEx (path, IntPtr.Zero, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (IntPtr.Zero == lib)
throw new Win32Exception (Marshal.GetLastWin32Error ());
loaded = true;
}
}
}

View File

@@ -23,14 +23,14 @@
// IN THE SOFTWARE.
//
using GameRes.Formats.QoiCodec;
using GameRes.Utility;
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Media;
using GameRes.Utility;
namespace GameRes.Formats.YuRis
{
@@ -39,22 +39,24 @@ namespace GameRes.Formats.YuRis
public int HeaderLength;
}
internal class YdgTile
internal class YdgSlice
{
public uint Offset;
public uint Size;
public ushort X;
public ushort Height;
public uint Offset;
public uint Size;
public byte[] Data;
public int X;
public int Y;
public int Height;
}
[Export(typeof(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 uint Signature { get { return 0x00474459; } } // 'YDG\0'
public override uint Signature { get { return 0x00474459; } } // 'YDG\0'
public YdgFormat()
public YdgFormat ()
{
Extensions = new string[] { "ydg" };
}
@@ -66,21 +68,21 @@ namespace GameRes.Formats.YuRis
return null;
if (!header.AsciiEqual (4, "YU-RIS"))
return null;
return new YdgMetaData
{
Width = header.ToUInt16 (0x20),
Height = header.ToUInt16 (0x22),
BPP = 32,
Width = header.ToUInt16 (0x20),
Height = header.ToUInt16 (0x22),
BPP = 32,
HeaderLength = header.ToInt32 (0x10),
};
}
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var reader = new YdgReader(stream, (YdgMetaData)info);
reader.Unpack();
return ImageData.Create (info, reader.Format, null, reader.Data);
var meta = (YdgMetaData) info;
var reader = new YdgReader (stream, meta);
reader.Unpack ();
return ImageData.Create (meta, reader.Format, null, reader.Data);
}
public override void Write (Stream file, ImageData image)
@@ -91,80 +93,84 @@ namespace GameRes.Formats.YuRis
internal sealed class YdgReader
{
IBinaryStream m_input;
YdgMetaData m_info;
readonly IBinaryStream m_input;
readonly YdgMetaData m_info;
readonly byte[] m_output;
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_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;
}
byte[] m_output;
public void Unpack ()
{
var tiles = ReadIndex ();
int width = (int)m_info.Width;
int height = (int)m_info.Height;
int dst_y = 0;
foreach (var tile in tiles)
var slices = ReadSlices ();
var options = new ParallelOptions { MaxDegreeOfParallelism = 4 };
Parallel.ForEach (slices, options, slice =>
{
byte[] pixels = ReadTile (tile, out width, out height);
int tile_height = Math.Min(height, tile.Height);
CopyTile (pixels, width, tile_height, tile.X, dst_y);
dst_y += tile_height;
if (dst_y >= m_info.Height)
break;
}
var pixels = DecodeSlice (slice.Data, out int slice_width, out int slice_height);
if (slice_height > slice.Height)
throw new InvalidFormatException ();
var height = Math.Min (slice_height, slice.Height);
CopySlice (pixels, slice_width, height, slice.X, slice.Y);
});
}
YdgTile[] ReadIndex ()
YdgSlice[] ReadSlices ()
{
m_input.Position = m_info.HeaderLength;
int tileCount = m_input.ReadInt32();
var dir = new YdgTile[tileCount];
for (int i = 0; i < dir.Length; ++i)
var count = m_input.ReadInt32 ();
if (count <= 0)
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(),
Size = m_input.ReadUInt32(),
X = m_input.ReadUInt16(),
Height = m_input.ReadUInt16(),
Offset = m_input.ReadUInt32 (),
Size = m_input.ReadUInt32 (),
X = 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"))
return DecodeWebP (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);
int copy_height = Math.Min (src_height, (int)m_info.Height - dst_y);
var copy_width = Math.Min (src_width, (int)m_info.Width-dst_x);
var copy_height = Math.Min (src_height, (int)m_info.Height-dst_y);
if (copy_width <= 0 || copy_height <= 0)
return;
int src_stride = src_width * 4;
int dst_stride = (int)m_info.Width * 4;
int dst = dst_y * dst_stride + dst_x * 4;
int row_bytes = copy_width * 4;
int src = 0;
for (int y = 0; y < copy_height; ++y)
var src_stride = 4*src_width;
var dst_stride = 4*(int)m_info.Width;
var dst = dst_y*dst_stride + 4*dst_x;
var row_bytes = 4*copy_width;
var src = 0;
for (var y = 0; y < copy_height; y++)
{
Buffer.BlockCopy (pixels, src, m_output, dst, row_bytes);
src += src_stride;
@@ -174,23 +180,18 @@ namespace GameRes.Formats.YuRis
byte[] DecodeWebP (byte[] data, out int width, out int height)
{
LibWebPLoader.Load ();
width = 0;
height = 0;
if (1 != 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.");
int stride = width * 4;
var output = new byte[stride * height];
var stride = 4*width;
var output = new byte[stride*height];
var handle = GCHandle.Alloc (output, GCHandleType.Pinned);
try
{
if (IntPtr.Zero == WebPDecodeBGRAInto (data, (UIntPtr)data.Length,
handle.AddrOfPinnedObject (), (UIntPtr)output.Length, stride))
{
if (IntPtr.Zero == WebPCodec.WebPDecodeBGRAInto (data, (UIntPtr)data.Length, handle.AddrOfPinnedObject (), (UIntPtr)output.Length, stride))
throw new InvalidFormatException ("WebP image decoder failed.");
}
}
finally
{
@@ -199,20 +200,19 @@ namespace GameRes.Formats.YuRis
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");
width = (int)Binary.BigEndian(input.ReadUInt32());
height = (int)Binary.BigEndian(input.ReadUInt32());
int channels = input.ReadByte();
int colorspace = input.ReadByte();
var count = 4 * width * height;
width = (int)Binary.BigEndian (input.ReadUInt32 ());
height = (int)Binary.BigEndian (input.ReadUInt32 ());
var channels = input.ReadByte ();
var colorspace = input.ReadByte ();
var count = 4*width*height;
var output = new byte[count];
var qoi = new QoiDecodeStream(input);
var qoi = new QoiDecodeStream (input);
uint pixel = 0;
var run = 0;
var dst = 0;
@@ -224,136 +224,14 @@ namespace GameRes.Formats.YuRis
{
run = qoi.Read (out pixel);
}
output[dst ] = (byte)pixel;
output[dst + 1] = (byte)(pixel >> 8);
output[dst + 2] = (byte)(pixel >> 16);
output[dst + 3] = (byte)(pixel >> 24);
output[dst ] = (byte)pixel;
output[dst+1] = (byte)(pixel >> 8);
output[dst+2] = (byte)(pixel >> 16);
output[dst+3] = (byte)(pixel >> 24);
dst += 4;
}
return output;
}
}
static class QoiCodec
{
public const int Index = 0x00;
public const int Diff = 0x40;
public const int Luma = 0x80;
public const int Run = 0xC0;
public const int Rgb = 0xFE;
public const int Rgba = 0xFF;
public const int Mask2 = 0xC0;
public const int HashTableSize = 64;
}
class QoiDecodeStream
{
readonly IBinaryStream m_input;
readonly byte[] m_table;
uint m_pixel;
public QoiDecodeStream(IBinaryStream input)
{
m_input = input;
m_table = new byte[4 * QoiCodec.HashTableSize];
m_pixel = 0xFF000000;
}
public int Read(out uint output)
{
var r = (byte)m_pixel;
var g = (byte)(m_pixel >> 8);
var b = (byte)(m_pixel >> 16);
var a = (byte)(m_pixel >> 24);
var run = 1;
var b1 = m_input.ReadByte();
if (-1 == b1)
throw new EndOfStreamException();
if (QoiCodec.Rgb == b1)
{
var rgb = m_input.ReadInt24();
r = (byte)rgb;
g = (byte)(rgb >> 8);
b = (byte)(rgb >> 16);
}
else if (QoiCodec.Rgba == b1)
{
var rgba = m_input.ReadInt32();
r = (byte)rgba;
g = (byte)(rgba >> 8);
b = (byte)(rgba >> 16);
a = (byte)(rgba >> 24);
}
else if (QoiCodec.Index == (b1 & QoiCodec.Mask2))
{
var p1 = (b1 & ~QoiCodec.Mask2) * 4;
r = m_table[p1];
g = m_table[p1 + 1];
b = m_table[p1 + 2];
a = m_table[p1 + 3];
}
else if (QoiCodec.Diff == (b1 & QoiCodec.Mask2))
{
r += (byte)(((b1 >> 4) & 0x03) - 2);
g += (byte)(((b1 >> 2) & 0x03) - 2);
b += (byte)((b1 & 0x03) - 2);
}
else if (QoiCodec.Luma == (b1 & QoiCodec.Mask2))
{
var b2 = m_input.ReadByte();
if (-1 == b2)
throw new EndOfStreamException();
var vg = (b1 & 0x3F) - 32;
r += (byte)(vg - 8 + ((b2 >> 4) & 0x0F));
g += (byte)vg;
b += (byte)(vg - 8 + (b2 & 0x0F));
}
else if (QoiCodec.Run == (b1 & QoiCodec.Mask2))
{
run = (b1 & 0x3F) + 1;
}
var p2 = (r * 3 + g * 5 + b * 7 + a * 11) % QoiCodec.HashTableSize * 4;
m_table[p2 ] = r;
m_table[p2 + 1] = g;
m_table[p2 + 2] = b;
m_table[p2 + 3] = a;
m_pixel = (uint)(r | (g << 8) | (b << 16) | (a << 24));
output = (uint)(b | (g << 8) | (r << 16) | (a << 24));
return run;
}
}
[DllImport("libwebp.dll", EntryPoint = "WebPGetInfo", CallingConvention = CallingConvention.Cdecl)]
public static extern int WebPGetInfo([MarshalAs(UnmanagedType.LPArray)] byte[] data, UIntPtr data_size, ref int width, ref int height);
[DllImport("libwebp.dll", EntryPoint = "WebPDecodeBGRAInto", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr WebPDecodeBGRAInto([MarshalAs(UnmanagedType.LPArray)] byte[] data, UIntPtr data_size, IntPtr output_buffer, UIntPtr output_buffer_size, int output_stride);
}
internal static class LibWebPLoader
{
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, uint dwFlags);
static bool loaded = false;
const uint LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x00000100;
const uint LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800;
public static void Load()
{
if (loaded)
return;
var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
folder = Path.Combine(folder, (IntPtr.Size == 4) ? "x86" : "x64");
var fullPath = Path.Combine(folder, "libwebp.dll");
fullPath = Path.GetFullPath(fullPath);
var handle = LoadLibraryEx(fullPath, IntPtr.Zero, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (IntPtr.Zero == handle)
throw new Win32Exception(Marshal.GetLastWin32Error());
loaded = true;
}
}
}