Initial texture replacement implementation

Unmerged PR #5632, originally by @gdkchan

* Add mod manager support - @KeatonTheBot

* Remove redundant code - @KeatonTheBot

* Optimize, modernize code to newer C# standards where appropriate - @KeatonTheBot

Co-authored-by: KeatonTheBot <keaton@ryujinx.app>
This commit is contained in:
gdkchanandKeatonTheBot authored and KeatonTheBot committed 2025-12-19 17:49:02 -06:00
1 parent d00bda7e53
commit 812cecd713
27 files changed
+3505 -67

No files matched your search

@@ -0,0 +1,859 @@
using Ryujinx.Common.Memory;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Texture.FileFormats
{
public static class DdsFileFormat
{
private const int StrideAlignment = 4;
[Flags]
private enum DdsFlags : uint
{
Caps = 1,
Height = 2,
Width = 4,
Pitch = 8,
PixelFormat = 0x1000,
MipMapCount = 0x20000,
LinearSize = 0x80000,
Depth = 0x800000,
}
[Flags]
private enum DdsCaps : uint
{
Complex = 8,
Texture = 0x1000,
MipMap = 0x400000,
}
[Flags]
private enum DdsCaps2 : uint
{
None = 0,
CubeMap = 0x200,
CubeMapPositiveX = 0x400,
CubeMapNegativeX = 0x800,
CubeMapPositiveY = 0x1000,
CubeMapNegativeY = 0x2000,
CubeMapPositiveZ = 0x4000,
CubeMapNegativeZ = 0x8000,
Volume = 0x200000,
}
[Flags]
private enum DdsPfFlags : uint
{
AlphaPixels = 1,
Alpha = 2,
FourCC = 4,
Rgb = 0x40,
Rgba = AlphaPixels | Rgb,
Yuv = 0x200,
Luminance = 0x20000,
BumpDuDv = 0x80000,
}
private struct DdsPixelFormat
{
public uint Size;
public DdsPfFlags Flags;
public uint FourCC;
public uint RGBBitCount;
public uint RBitMask;
public uint GBitMask;
public uint BBitMask;
public uint ABitMask;
}
private struct DdsHeader
{
public uint Size;
public DdsFlags Flags;
public uint Height;
public uint Width;
public uint PitchOrLinearSize;
public uint Depth;
public uint MipMapCount;
public Array11<uint> Reserved1;
public DdsPixelFormat DdsPf;
public DdsCaps Caps;
public DdsCaps2 Caps2;
public uint Caps3;
public uint Caps4;
public uint Reserved2;
}
private enum D3d10ResourceDimension : uint
{
Unknown = 0,
Buffer = 1,
Texture1D = 2,
Texture2D = 3,
Texture3D = 4,
}
private struct DdsHeaderDxt10
{
public DxgiFormat DxgiFormat;
public D3d10ResourceDimension ResourceDimension;
public uint MiscFlag;
public uint ArraySize;
public uint MiscFlags2;
}
private const uint DdsMagic = 0x20534444;
private const uint Dxt1FourCC = 'D' | ('X' << 8) | ('T' << 16) | ('1' << 24);
private const uint Dxt3FourCC = 'D' | ('X' << 8) | ('T' << 16) | ('3' << 24);
private const uint Dxt5FourCC = 'D' | ('X' << 8) | ('T' << 16) | ('5' << 24);
private const uint Dx10FourCC = 'D' | ('X' << 8) | ('1' << 16) | ('0' << 24);
private const uint Bc4UFourCC = 'B' | ('C' << 8) | ('4' << 16) | ('U' << 24);
private const uint Bc4SFourCC = 'B' | ('C' << 8) | ('4' << 16) | ('S' << 24);
private const uint Bc5SFourCC = 'B' | ('C' << 8) | ('5' << 16) | ('S' << 24);
private const uint Ati1FourCC = 'A' | ('T' << 8) | ('I' << 16) | ('1' << 24);
private const uint Ati2FourCC = 'A' | ('T' << 8) | ('I' << 16) | ('2' << 24);
public static ImageLoadResult TryLoadHeader(ReadOnlySpan<byte> ddsData, out ImageParameters parameters)
{
return TryLoadHeaderImpl(ddsData, out parameters, out _);
}
private static ImageLoadResult TryLoadHeaderImpl(ReadOnlySpan<byte> ddsData, out ImageParameters parameters, out int dataOffset)
{
parameters = default;
dataOffset = 0;
if (ddsData.Length < 4 + Unsafe.SizeOf<DdsHeader>())
{
return ImageLoadResult.DataTooShort;
}
uint magic = ddsData.Read<uint>();
DdsHeader header = ddsData[4..].Read<DdsHeader>();
if (magic != DdsMagic ||
header.Size != Unsafe.SizeOf<DdsHeader>() ||
header.DdsPf.Size != Unsafe.SizeOf<DdsPixelFormat>())
{
return ImageLoadResult.CorruptedHeader;
}
int depth = header.Flags.HasFlag(DdsFlags.Depth) ? (int)header.Depth : 1;
int levels = header.Flags.HasFlag(DdsFlags.MipMapCount) ? (int)header.MipMapCount : 1;
int layers = 1;
ImageDimensions dimensions = header.Flags.HasFlag(DdsFlags.Depth) ? ImageDimensions.Dim3D : ImageDimensions.Dim2D;
ImageFormat format = GetFormat(header.DdsPf);
if (header.Caps2.HasFlag(DdsCaps2.CubeMap))
{
layers = 6;
dimensions = ImageDimensions.DimCube;
}
dataOffset = 4 + Unsafe.SizeOf<DdsHeader>();
if (header.DdsPf.Flags.HasFlag(DdsPfFlags.FourCC) && header.DdsPf.FourCC == Dx10FourCC)
{
if (ddsData.Length < 4 + Unsafe.SizeOf<DdsHeader>() + Unsafe.SizeOf<DdsHeaderDxt10>())
{
return ImageLoadResult.DataTooShort;
}
DdsHeaderDxt10 headerDxt10 = ddsData[dataOffset..].Read<DdsHeaderDxt10>();
if (dimensions != ImageDimensions.Dim3D)
{
if (headerDxt10.MiscFlag == 4u)
{
// Cube array.
layers = (int)Math.Max(1, headerDxt10.ArraySize) * 6;
dimensions = headerDxt10.ArraySize > 1 ? ImageDimensions.DimCubeArray : ImageDimensions.DimCube;
}
else
{
// 2D array.
layers = (int)Math.Max(1, headerDxt10.ArraySize);
dimensions = headerDxt10.ArraySize > 1 ? ImageDimensions.Dim2DArray : ImageDimensions.Dim2D;
}
}
format = ConvertToImageFormat(headerDxt10.DxgiFormat);
dataOffset += Unsafe.SizeOf<DdsHeaderDxt10>();
}
parameters = new((int)header.Width, (int)header.Height, depth * layers, levels, format, dimensions);
return ImageLoadResult.Success;
}
public static int CalculateSize(in ImageParameters parameters)
{
return CalculateSizeInternal(parameters, StrideAlignment);
}
private static int CalculateSizeInternal(in ImageParameters parameters, int strideAlignment)
{
if (parameters.Format == ImageFormat.Unknown)
{
return 0;
}
int size = 0;
(int bw, int bh, int bpp) = GetBlockSizeAndBpp(parameters.Format);
for (int l = 0; l < parameters.Levels; l++)
{
int w = Math.Max(1, parameters.Width >> l);
int h = Math.Max(1, parameters.Height >> l);
int d = parameters.Dimensions == ImageDimensions.Dim3D ? Math.Max(1, parameters.DepthOrLayers >> l) : parameters.DepthOrLayers;
w = (w + bw - 1) / bw;
h = (h + bh - 1) / bh;
int stride = (w * bpp + strideAlignment - 1) & -strideAlignment;
size += stride * h * d;
}
return size;
}
private static int CalculateStride(in ImageParameters parameters, int level)
{
if (parameters.Format == ImageFormat.Unknown)
{
return 0;
}
(int bw, _, int bpp) = GetBlockSizeAndBpp(parameters.Format);
int w = Math.Max(1, parameters.Width >> level);
w = (w + bw - 1) / bw;
return w * bpp;
}
public static ImageLoadResult TryLoadData(ReadOnlySpan<byte> ddsData, Span<byte> output)
{
ImageLoadResult result = TryLoadHeaderImpl(ddsData, out ImageParameters parameters, out int dataOffset);
if (result != ImageLoadResult.Success)
{
return result;
}
if (parameters.Format == ImageFormat.Unknown)
{
return ImageLoadResult.UnsupportedFormat;
}
int size = CalculateSize(parameters);
int inSize = CalculateSizeInternal(parameters, 1);
// Some basic validation for completely bogus sizes.
if (inSize <= 0 || dataOffset + inSize <= 0)
{
return ImageLoadResult.CorruptedHeader;
}
if (dataOffset + inSize > ddsData.Length)
{
return ImageLoadResult.DataTooShort;
}
if (output.Length < size)
{
return ImageLoadResult.OutputTooShort;
}
if ((parameters.DepthOrLayers > 1 && parameters.Dimensions != ImageDimensions.Dim3D) || size != inSize)
{
int inOffset = dataOffset;
bool isArray = IsArray(parameters.Dimensions) || parameters.Dimensions == ImageDimensions.DimCube;
int layers = isArray ? parameters.DepthOrLayers : 1;
for (int z = 0; z < layers; z++)
{
for (int l = 0; l < parameters.Levels; l++)
{
(int sliceOffset, int sliceSize) = GetSlice(parameters, z, l);
inOffset += CopyData(output, ddsData, sliceOffset, inOffset, sliceSize, CalculateStride(parameters, l));
}
}
}
else
{
CopyData(output, ddsData, 0, dataOffset, size, CalculateStride(parameters, 0));
}
return ImageLoadResult.Success;
}
private static int CopyData(Span<byte> destination, ReadOnlySpan<byte> source, int dstOffset, int srcOffset, int size, int stride)
{
int strideAligned = (stride + StrideAlignment - 1) & -StrideAlignment;
if (stride != strideAligned)
{
int rows = size / strideAligned;
for (int y = 0; y < rows; y++)
{
source.Slice(srcOffset + y * stride, stride).CopyTo(destination.Slice(dstOffset + y * strideAligned, stride));
}
return rows * stride;
}
else
{
source.Slice(srcOffset, size).CopyTo(destination.Slice(dstOffset, size));
return size;
}
}
public static void Save(Stream output, ImageParameters parameters, ReadOnlySpan<byte> data)
{
DdsFlags flags = DdsFlags.Caps | DdsFlags.Height | DdsFlags.Width | DdsFlags.PixelFormat;
DdsCaps caps = DdsCaps.Texture;
DdsCaps2 caps2 = DdsCaps2.None;
if (parameters.Levels > 1)
{
flags |= DdsFlags.MipMapCount;
caps |= DdsCaps.MipMap | DdsCaps.Complex;
}
if (parameters.Dimensions == ImageDimensions.DimCube)
{
caps2 |= DdsCaps2.CubeMap | DdsCaps2.CubeMapPositiveX;
if (parameters.DepthOrLayers > 1)
{
caps2 |= DdsCaps2.CubeMapNegativeX;
}
if (parameters.DepthOrLayers > 2)
{
caps2 |= DdsCaps2.CubeMapPositiveY;
}
if (parameters.DepthOrLayers > 3)
{
caps2 |= DdsCaps2.CubeMapNegativeY;
}
if (parameters.DepthOrLayers > 4)
{
caps2 |= DdsCaps2.CubeMapPositiveZ;
}
if (parameters.DepthOrLayers > 5)
{
caps2 |= DdsCaps2.CubeMapNegativeZ;
}
}
else if (parameters.Dimensions == ImageDimensions.Dim3D)
{
flags |= DdsFlags.Depth;
caps2 |= DdsCaps2.Volume;
}
bool isArray = IsArray(parameters.Dimensions);
bool needsDxt10Header = isArray || !IsLegacyCompatibleFormat(parameters.Format);
DdsPixelFormat pixelFormat = needsDxt10Header ? CreateDx10PixelFormat() : CreatePixelFormat(parameters.Format);
(int bw, int bh, int bpp) = GetBlockSizeAndBpp(parameters.Format);
int pitch = (parameters.Width + bw - 1) / bw * bpp;
int pitchOrLinearSize = pitch;
if (bw > 1 || bh > 1)
{
flags |= DdsFlags.LinearSize;
pitchOrLinearSize *= (parameters.Height + bh - 1) / bh * parameters.DepthOrLayers;
}
else
{
flags |= DdsFlags.Pitch;
}
DdsHeader header = new()
{
Size = (uint)Unsafe.SizeOf<DdsHeader>(),
Flags = flags,
Height = (uint)parameters.Height,
Width = (uint)parameters.Width,
PitchOrLinearSize = (uint)pitchOrLinearSize,
Depth = (uint)parameters.DepthOrLayers,
MipMapCount = (uint)parameters.Levels,
Reserved1 = default,
DdsPf = pixelFormat,
Caps = caps,
Caps2 = caps2,
Caps3 = 0,
Caps4 = 0,
Reserved2 = 0,
};
output.Write(DdsMagic);
output.Write(header);
if (needsDxt10Header)
{
output.Write(CreateDxt10Header(parameters.Format, parameters.Dimensions, parameters.DepthOrLayers));
}
if ((parameters.DepthOrLayers > 1 && parameters.Dimensions != ImageDimensions.Dim3D) || bpp < StrideAlignment)
{
// On DDS, the order is:
// [Layer 0 Level 0] [Layer 0 Level 1] [Layer 1 Level 0] [Layer 1 Level 1]
// While on the input data, the order is:
// [Layer 0 Level 0] [Layer 1 Level 0] [Layer 0 Level 1] [Layer 1 Level 1]
int layers = isArray || parameters.Dimensions == ImageDimensions.DimCube ? parameters.DepthOrLayers : 1;
for (int z = 0; z < layers; z++)
{
for (int l = 0; l < parameters.Levels; l++)
{
(int sliceOffset, int sliceSize) = GetSlice(parameters, z, l);
pitch = (Math.Max(1, parameters.Width >> l) + bw - 1) / bw * bpp;
WriteData(output, data.Slice(sliceOffset, sliceSize), pitch);
}
}
}
else
{
WriteData(output, data, pitch);
}
}
private static void WriteData(Stream output, ReadOnlySpan<byte> data, int stride)
{
int strideAligned = (stride + StrideAlignment - 1) & -StrideAlignment;
if (stride != strideAligned)
{
for (int i = 0; i < data.Length; i += strideAligned)
{
output.Write(data.Slice(i, stride));
}
}
else
{
output.Write(data);
}
}
private static (int, int) GetSlice(ImageParameters parameters, int layer, int level)
{
int size = 0;
int sliceSize = 0;
int depth, layers;
if (parameters.Dimensions == ImageDimensions.Dim3D)
{
depth = parameters.DepthOrLayers;
layers = 1;
}
else
{
depth = 1;
layers = parameters.DepthOrLayers;
}
(int bw, int bh, int bpp) = GetBlockSizeAndBpp(parameters.Format);
for (int l = 0; l <= level; l++)
{
int w = Math.Max(1, parameters.Width >> l);
int h = Math.Max(1, parameters.Height >> l);
int d = Math.Max(1, depth >> l);
w = (w + bw - 1) / bw;
h = (h + bh - 1) / bh;
for (int z = 0; z < (l < level ? layers : layer + 1); z++)
{
int stride = (w * bpp + StrideAlignment - 1) & -StrideAlignment;
sliceSize = stride * h * d;
size += sliceSize;
}
}
return (size - sliceSize, sliceSize);
}
private static void Write<T>(this Stream stream, T value) where T : unmanaged
{
stream.Write(MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
}
private static T Read<T>(this ReadOnlySpan<byte> span) where T : unmanaged
{
return MemoryMarshal.Cast<byte, T>(span)[0];
}
private static bool IsArray(ImageDimensions dimensions)
{
return dimensions == ImageDimensions.Dim2DArray || dimensions == ImageDimensions.DimCubeArray;
}
private static DdsHeaderDxt10 CreateDxt10Header(ImageFormat format, ImageDimensions dimensions, int depthOrLayers)
{
D3d10ResourceDimension resourceDimension = dimensions == ImageDimensions.Dim3D
? D3d10ResourceDimension.Texture3D
: D3d10ResourceDimension.Texture2D;
uint arraySize = 1;
if (dimensions == ImageDimensions.Dim2DArray)
{
arraySize = (uint)depthOrLayers;
}
else if (dimensions == ImageDimensions.DimCubeArray)
{
arraySize = (uint)depthOrLayers / 6;
}
return new DdsHeaderDxt10()
{
DxgiFormat = ConvertToDxgiFormat(format),
ResourceDimension = resourceDimension,
MiscFlag = dimensions == ImageDimensions.DimCube || dimensions == ImageDimensions.DimCubeArray ? 4u : 0u,
ArraySize = arraySize,
MiscFlags2 = 1, // Straight alpha.
};
}
private static DdsPixelFormat CreateDx10PixelFormat()
{
return new DdsPixelFormat()
{
Size = (uint)Unsafe.SizeOf<DdsPixelFormat>(),
Flags = DdsPfFlags.FourCC,
FourCC = Dx10FourCC,
};
}
private static DdsPixelFormat CreatePixelFormat(ImageFormat format)
{
DdsPixelFormat pf = new()
{
Size = (uint)Unsafe.SizeOf<DdsPixelFormat>(),
};
switch (format)
{
case ImageFormat.Bc1RgbaUnorm:
pf.Flags = DdsPfFlags.FourCC;
pf.FourCC = Dxt1FourCC;
break;
case ImageFormat.Bc2Unorm:
pf.Flags = DdsPfFlags.FourCC;
pf.FourCC = Dxt3FourCC;
break;
case ImageFormat.Bc3Unorm:
pf.Flags = DdsPfFlags.FourCC;
pf.FourCC = Dxt5FourCC;
break;
case ImageFormat.Bc4Snorm:
pf.Flags = DdsPfFlags.FourCC;
pf.FourCC = Bc4SFourCC;
break;
case ImageFormat.Bc4Unorm:
pf.Flags = DdsPfFlags.FourCC;
pf.FourCC = Bc4UFourCC;
break;
case ImageFormat.Bc5Snorm:
pf.Flags = DdsPfFlags.FourCC;
pf.FourCC = Bc5SFourCC;
break;
case ImageFormat.Bc5Unorm:
pf.Flags = DdsPfFlags.FourCC;
pf.FourCC = Ati2FourCC;
break;
case ImageFormat.R8Unorm:
pf.Flags = DdsPfFlags.Luminance;
pf.RGBBitCount = 8;
pf.RBitMask = 0xffu;
pf.GBitMask = 0;
pf.BBitMask = 0;
pf.ABitMask = 0;
break;
case ImageFormat.R8G8Unorm:
pf.Flags = DdsPfFlags.BumpDuDv;
pf.RGBBitCount = 16;
pf.RBitMask = 0xffu;
pf.GBitMask = 0xffu << 8;
pf.BBitMask = 0;
pf.ABitMask = 0;
break;
case ImageFormat.R8G8B8A8Unorm:
pf.Flags = DdsPfFlags.Rgba;
pf.RGBBitCount = 32;
pf.RBitMask = 0xffu;
pf.GBitMask = 0xffu << 8;
pf.BBitMask = 0xffu << 16;
pf.ABitMask = 0xffu << 24;
break;
case ImageFormat.B8G8R8A8Unorm:
pf.Flags = DdsPfFlags.Rgba;
pf.RGBBitCount = 32;
pf.RBitMask = 0xffu << 16;
pf.GBitMask = 0xffu << 8;
pf.BBitMask = 0xffu;
pf.ABitMask = 0xffu << 24;
break;
case ImageFormat.R5G6B5Unorm:
pf.Flags = DdsPfFlags.Rgb;
pf.RGBBitCount = 16;
pf.RBitMask = 0x1fu << 11;
pf.GBitMask = 0x3fu << 5;
pf.BBitMask = 0x1fu;
break;
case ImageFormat.R5G5B5A1Unorm:
pf.Flags = DdsPfFlags.Rgba;
pf.RGBBitCount = 16;
pf.RBitMask = 0x1fu << 10;
pf.GBitMask = 0x1fu << 5;
pf.BBitMask = 0x1fu;
pf.ABitMask = 1u << 15;
break;
case ImageFormat.R4G4B4A4Unorm:
pf.Flags = DdsPfFlags.Rgba;
pf.RGBBitCount = 16;
pf.RBitMask = 0xfu << 8;
pf.GBitMask = 0xfu << 4;
pf.BBitMask = 0xfu;
pf.ABitMask = 0xfu << 12;
break;
default:
throw new ArgumentException($"Can't encode format \"{format}\" on legacy pixel format structure.");
}
return pf;
}
private static (int, int, int) GetBlockSizeAndBpp(ImageFormat format)
{
int bw = 1;
int bh = 1;
int bpp = 0;
switch (format)
{
case ImageFormat.Bc1RgbaSrgb:
case ImageFormat.Bc1RgbaUnorm:
case ImageFormat.Bc4Snorm:
case ImageFormat.Bc4Unorm:
bw = bh = 4;
bpp = 8;
break;
case ImageFormat.Bc2Srgb:
case ImageFormat.Bc2Unorm:
case ImageFormat.Bc3Srgb:
case ImageFormat.Bc3Unorm:
case ImageFormat.Bc5Snorm:
case ImageFormat.Bc5Unorm:
case ImageFormat.Bc7Srgb:
case ImageFormat.Bc7Unorm:
bw = bh = 4;
bpp = 16;
break;
case ImageFormat.R8Unorm:
bpp = 1;
break;
case ImageFormat.R8G8Unorm:
case ImageFormat.R5G6B5Unorm:
case ImageFormat.R5G5B5A1Unorm:
case ImageFormat.R4G4B4A4Unorm:
bpp = 2;
break;
case ImageFormat.R8G8B8A8Srgb:
case ImageFormat.R8G8B8A8Unorm:
case ImageFormat.B8G8R8A8Srgb:
case ImageFormat.B8G8R8A8Unorm:
bpp = 4;
break;
}
if (bpp == 0)
{
throw new ArgumentException($"Invalid format {format}.");
}
return (bw, bh, bpp);
}
private static bool IsLegacyCompatibleFormat(ImageFormat format)
{
switch (format)
{
case ImageFormat.Bc1RgbaUnorm:
case ImageFormat.Bc2Unorm:
case ImageFormat.Bc3Unorm:
case ImageFormat.Bc4Snorm:
case ImageFormat.Bc4Unorm:
case ImageFormat.Bc5Snorm:
case ImageFormat.Bc5Unorm:
case ImageFormat.R8G8B8A8Unorm:
case ImageFormat.B8G8R8A8Unorm:
case ImageFormat.R5G6B5Unorm:
case ImageFormat.R5G5B5A1Unorm:
case ImageFormat.R4G4B4A4Unorm:
return true;
}
return false;
}
private static DxgiFormat ConvertToDxgiFormat(ImageFormat format)
{
return format switch
{
ImageFormat.Bc1RgbaSrgb => DxgiFormat.FormatBC1UnormSrgb,
ImageFormat.Bc1RgbaUnorm => DxgiFormat.FormatBC1Unorm,
ImageFormat.Bc2Srgb => DxgiFormat.FormatBC2UnormSrgb,
ImageFormat.Bc2Unorm => DxgiFormat.FormatBC2Unorm,
ImageFormat.Bc3Srgb => DxgiFormat.FormatBC3UnormSrgb,
ImageFormat.Bc3Unorm => DxgiFormat.FormatBC3Unorm,
ImageFormat.Bc4Snorm => DxgiFormat.FormatBC4Snorm,
ImageFormat.Bc4Unorm => DxgiFormat.FormatBC4Unorm,
ImageFormat.Bc5Snorm => DxgiFormat.FormatBC5Snorm,
ImageFormat.Bc5Unorm => DxgiFormat.FormatBC5Unorm,
ImageFormat.Bc7Srgb => DxgiFormat.FormatBC7UnormSrgb,
ImageFormat.Bc7Unorm => DxgiFormat.FormatBC7Unorm,
ImageFormat.R8Unorm => DxgiFormat.FormatR8Unorm,
ImageFormat.R8G8Unorm => DxgiFormat.FormatR8G8Unorm,
ImageFormat.R8G8B8A8Srgb => DxgiFormat.FormatR8G8B8A8UnormSrgb,
ImageFormat.R8G8B8A8Unorm => DxgiFormat.FormatR8G8B8A8Unorm,
ImageFormat.B8G8R8A8Srgb => DxgiFormat.FormatB8G8R8A8UnormSrgb,
ImageFormat.B8G8R8A8Unorm => DxgiFormat.FormatB8G8R8A8Unorm,
ImageFormat.R5G6B5Unorm => DxgiFormat.FormatB5G6R5Unorm,
ImageFormat.R5G5B5A1Unorm => DxgiFormat.FormatB5G5R5A1Unorm,
ImageFormat.R4G4B4A4Unorm => DxgiFormat.FormatB4G4R4A4Unorm,
_ => DxgiFormat.FormatUnknown,
};
}
private static ImageFormat GetFormat(DdsPixelFormat pixelFormat)
{
if (pixelFormat.Flags.HasFlag(DdsPfFlags.FourCC))
{
return pixelFormat.FourCC switch
{
Dxt1FourCC => ImageFormat.Bc1RgbaUnorm,
Dxt3FourCC => ImageFormat.Bc2Unorm,
Dxt5FourCC => ImageFormat.Bc3Unorm,
Bc4SFourCC => ImageFormat.Bc4Snorm,
Bc4UFourCC or Ati1FourCC => ImageFormat.Bc4Unorm,
Bc5SFourCC => ImageFormat.Bc5Snorm,
Ati2FourCC => ImageFormat.Bc5Unorm,
_ => ImageFormat.Unknown,
};
}
else
{
if (pixelFormat.Flags == DdsPfFlags.Luminance &&
pixelFormat.RGBBitCount == 8 &&
pixelFormat.RBitMask == 0xffu &&
pixelFormat.GBitMask == 0 &&
pixelFormat.BBitMask == 0)
{
return ImageFormat.R8Unorm;
}
else if (pixelFormat.Flags == DdsPfFlags.BumpDuDv &&
pixelFormat.RGBBitCount == 16 &&
pixelFormat.RBitMask == 0xffu &&
pixelFormat.GBitMask == 0xffu << 8 &&
pixelFormat.BBitMask == 0)
{
return ImageFormat.R8G8Unorm;
}
else if ((pixelFormat.Flags & DdsPfFlags.Rgba) == DdsPfFlags.Rgba &&
pixelFormat.RGBBitCount == 32 &&
pixelFormat.RBitMask == 0xffu &&
pixelFormat.GBitMask == 0xffu << 8 &&
pixelFormat.BBitMask == 0xffu << 16 &&
pixelFormat.ABitMask == 0xffu << 24)
{
return ImageFormat.R8G8B8A8Unorm;
}
else if ((pixelFormat.Flags & DdsPfFlags.Rgba) == DdsPfFlags.Rgba &&
pixelFormat.RGBBitCount == 32 &&
pixelFormat.RBitMask == 0xffu << 16 &&
pixelFormat.GBitMask == 0xffu << 8 &&
pixelFormat.BBitMask == 0xffu &&
pixelFormat.ABitMask == 0xffu << 24)
{
return ImageFormat.B8G8R8A8Unorm;
}
else if ((pixelFormat.Flags & DdsPfFlags.Rgba) == DdsPfFlags.Rgb &&
pixelFormat.RGBBitCount == 16 &&
pixelFormat.RBitMask == 0x1fu << 11 &&
pixelFormat.GBitMask == 0x3fu << 5 &&
pixelFormat.BBitMask == 0x1fu)
{
return ImageFormat.R5G6B5Unorm;
}
else if ((pixelFormat.Flags & DdsPfFlags.Rgba) == DdsPfFlags.Rgba &&
pixelFormat.RGBBitCount == 16 &&
pixelFormat.RBitMask == 0x1fu << 10 &&
pixelFormat.GBitMask == 0x1fu << 5 &&
pixelFormat.BBitMask == 0x1fu &&
pixelFormat.ABitMask == 1u << 15)
{
return ImageFormat.R5G5B5A1Unorm;
}
else if ((pixelFormat.Flags & DdsPfFlags.Rgba) == DdsPfFlags.Rgba &&
pixelFormat.RGBBitCount == 16 &&
pixelFormat.RBitMask == 0xfu << 8 &&
pixelFormat.GBitMask == 0xfu << 4 &&
pixelFormat.BBitMask == 0xfu &&
pixelFormat.ABitMask == 0xfu << 12)
{
return ImageFormat.R4G4B4A4Unorm;
}
}
return ImageFormat.Unknown;
}
private static ImageFormat ConvertToImageFormat(DxgiFormat format)
{
return format switch
{
DxgiFormat.FormatBC1UnormSrgb => ImageFormat.Bc1RgbaSrgb,
DxgiFormat.FormatBC1Unorm => ImageFormat.Bc1RgbaUnorm,
DxgiFormat.FormatBC2UnormSrgb => ImageFormat.Bc2Srgb,
DxgiFormat.FormatBC2Unorm => ImageFormat.Bc2Unorm,
DxgiFormat.FormatBC3UnormSrgb => ImageFormat.Bc3Srgb,
DxgiFormat.FormatBC3Unorm => ImageFormat.Bc3Unorm,
DxgiFormat.FormatBC4Snorm => ImageFormat.Bc4Snorm,
DxgiFormat.FormatBC4Unorm => ImageFormat.Bc4Unorm,
DxgiFormat.FormatBC5Snorm => ImageFormat.Bc5Snorm,
DxgiFormat.FormatBC5Unorm => ImageFormat.Bc5Unorm,
DxgiFormat.FormatBC7UnormSrgb => ImageFormat.Bc7Srgb,
DxgiFormat.FormatBC7Unorm => ImageFormat.Bc7Unorm,
DxgiFormat.FormatR8Unorm => ImageFormat.R8Unorm,
DxgiFormat.FormatR8G8Unorm => ImageFormat.R8G8Unorm,
DxgiFormat.FormatR8G8B8A8UnormSrgb => ImageFormat.R8G8B8A8Srgb,
DxgiFormat.FormatR8G8B8A8Unorm => ImageFormat.R8G8B8A8Unorm,
DxgiFormat.FormatB8G8R8A8UnormSrgb => ImageFormat.B8G8R8A8Srgb,
DxgiFormat.FormatB8G8R8A8Unorm => ImageFormat.B8G8R8A8Unorm,
DxgiFormat.FormatB5G6R5Unorm => ImageFormat.R5G6B5Unorm,
DxgiFormat.FormatB5G5R5A1Unorm => ImageFormat.R5G5B5A1Unorm,
DxgiFormat.FormatB4G4R4A4Unorm => ImageFormat.R4G4B4A4Unorm,
_ => ImageFormat.Unknown,
};
}
}
}
@@ -0,0 +1,125 @@
namespace Ryujinx.Graphics.Texture.FileFormats
{
enum DxgiFormat
{
FormatUnknown = 0x0,
FormatR32G32B32A32Typeless = 0x1,
FormatR32G32B32A32Float = 0x2,
FormatR32G32B32A32Uint = 0x3,
FormatR32G32B32A32Sint = 0x4,
FormatR32G32B32Typeless = 0x5,
FormatR32G32B32Float = 0x6,
FormatR32G32B32Uint = 0x7,
FormatR32G32B32Sint = 0x8,
FormatR16G16B16A16Typeless = 0x9,
FormatR16G16B16A16Float = 0xA,
FormatR16G16B16A16Unorm = 0xB,
FormatR16G16B16A16Uint = 0xC,
FormatR16G16B16A16Snorm = 0xD,
FormatR16G16B16A16Sint = 0xE,
FormatR32G32Typeless = 0xF,
FormatR32G32Float = 0x10,
FormatR32G32Uint = 0x11,
FormatR32G32Sint = 0x12,
FormatR32G8X24Typeless = 0x13,
FormatD32FloatS8X24Uint = 0x14,
FormatR32FloatX8X24Typeless = 0x15,
FormatX32TypelessG8X24Uint = 0x16,
FormatR10G10B10A2Typeless = 0x17,
FormatR10G10B10A2Unorm = 0x18,
FormatR10G10B10A2Uint = 0x19,
FormatR11G11B10Float = 0x1A,
FormatR8G8B8A8Typeless = 0x1B,
FormatR8G8B8A8Unorm = 0x1C,
FormatR8G8B8A8UnormSrgb = 0x1D,
FormatR8G8B8A8Uint = 0x1E,
FormatR8G8B8A8Snorm = 0x1F,
FormatR8G8B8A8Sint = 0x20,
FormatR16G16Typeless = 0x21,
FormatR16G16Float = 0x22,
FormatR16G16Unorm = 0x23,
FormatR16G16Uint = 0x24,
FormatR16G16Snorm = 0x25,
FormatR16G16Sint = 0x26,
FormatR32Typeless = 0x27,
FormatD32Float = 0x28,
FormatR32Float = 0x29,
FormatR32Uint = 0x2A,
FormatR32Sint = 0x2B,
FormatR24G8Typeless = 0x2C,
FormatD24UnormS8Uint = 0x2D,
FormatR24UnormX8Typeless = 0x2E,
FormatX24TypelessG8Uint = 0x2F,
FormatR8G8Typeless = 0x30,
FormatR8G8Unorm = 0x31,
FormatR8G8Uint = 0x32,
FormatR8G8Snorm = 0x33,
FormatR8G8Sint = 0x34,
FormatR16Typeless = 0x35,
FormatR16Float = 0x36,
FormatD16Unorm = 0x37,
FormatR16Unorm = 0x38,
FormatR16Uint = 0x39,
FormatR16Snorm = 0x3A,
FormatR16Sint = 0x3B,
FormatR8Typeless = 0x3C,
FormatR8Unorm = 0x3D,
FormatR8Uint = 0x3E,
FormatR8Snorm = 0x3F,
FormatR8Sint = 0x40,
FormatA8Unorm = 0x41,
FormatR1Unorm = 0x42,
FormatR9G9B9E5Sharedexp = 0x43,
FormatR8G8B8G8Unorm = 0x44,
FormatG8R8G8B8Unorm = 0x45,
FormatBC1Typeless = 0x46,
FormatBC1Unorm = 0x47,
FormatBC1UnormSrgb = 0x48,
FormatBC2Typeless = 0x49,
FormatBC2Unorm = 0x4A,
FormatBC2UnormSrgb = 0x4B,
FormatBC3Typeless = 0x4C,
FormatBC3Unorm = 0x4D,
FormatBC3UnormSrgb = 0x4E,
FormatBC4Typeless = 0x4F,
FormatBC4Unorm = 0x50,
FormatBC4Snorm = 0x51,
FormatBC5Typeless = 0x52,
FormatBC5Unorm = 0x53,
FormatBC5Snorm = 0x54,
FormatB5G6R5Unorm = 0x55,
FormatB5G5R5A1Unorm = 0x56,
FormatB8G8R8A8Unorm = 0x57,
FormatB8G8R8X8Unorm = 0x58,
FormatR10G10B10XRBiasA2Unorm = 0x59,
FormatB8G8R8A8Typeless = 0x5A,
FormatB8G8R8A8UnormSrgb = 0x5B,
FormatB8G8R8X8Typeless = 0x5C,
FormatB8G8R8X8UnormSrgb = 0x5D,
FormatBC6HTypeless = 0x5E,
FormatBC6HUF16 = 0x5F,
FormatBC6HSF16 = 0x60,
FormatBC7Typeless = 0x61,
FormatBC7Unorm = 0x62,
FormatBC7UnormSrgb = 0x63,
FormatAyuv = 0x64,
FormatY410 = 0x65,
FormatY416 = 0x66,
FormatNV12 = 0x67,
FormatP010 = 0x68,
FormatP016 = 0x69,
Format420Opaque = 0x6A,
FormatYuy2 = 0x6B,
FormatY210 = 0x6C,
FormatY216 = 0x6D,
FormatNV11 = 0x6E,
FormatAI44 = 0x6F,
FormatIA44 = 0x70,
FormatP8 = 0x71,
FormatA8P8 = 0x72,
FormatB4G4R4A4Unorm = 0x73,
FormatP208 = 0x82,
FormatV208 = 0x83,
FormatV408 = 0x84,
}
}
@@ -0,0 +1,11 @@
namespace Ryujinx.Graphics.Texture.FileFormats
{
public enum ImageDimensions
{
Dim2D,
Dim2DArray,
Dim3D,
DimCube,
DimCubeArray,
}
}
@@ -0,0 +1,28 @@
namespace Ryujinx.Graphics.Texture.FileFormats
{
public enum ImageFormat
{
Unknown,
Bc1RgbaSrgb,
Bc1RgbaUnorm,
Bc2Srgb,
Bc2Unorm,
Bc3Srgb,
Bc3Unorm,
Bc4Unorm,
Bc4Snorm,
Bc5Unorm,
Bc5Snorm,
Bc7Srgb,
Bc7Unorm,
R8Unorm,
R8G8Unorm,
R8G8B8A8Srgb,
R8G8B8A8Unorm,
B8G8R8A8Srgb,
B8G8R8A8Unorm,
R5G6B5Unorm,
R5G5B5A1Unorm,
R4G4B4A4Unorm,
}
}
@@ -0,0 +1,12 @@
namespace Ryujinx.Graphics.Texture.FileFormats
{
public enum ImageLoadResult
{
Success,
CorruptedHeader,
CorruptedData,
DataTooShort,
OutputTooShort,
UnsupportedFormat,
}
}
@@ -0,0 +1,18 @@
namespace Ryujinx.Graphics.Texture.FileFormats
{
public readonly struct ImageParameters(
int width,
int height,
int depthOrLayers,
int levels,
ImageFormat format,
ImageDimensions dimensions)
{
public int Width { get; } = width;
public int Height { get; } = height;
public int DepthOrLayers { get; } = depthOrLayers;
public int Levels { get; } = levels;
public ImageFormat Format { get; } = format;
public ImageDimensions Dimensions { get; } = dimensions;
}
}
@@ -0,0 +1,755 @@
using System;
using System.Buffers.Binary;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Texture.FileFormats
{
public static class PngFileFormat
{
private const int ChunkOverheadSize = 12;
private const int MaxIdatChunkSize = 0x2000;
private static readonly uint[] _crcTable;
static PngFileFormat()
{
_crcTable = new uint[256];
for (int n = 0; n < _crcTable.Length; n++)
{
uint c = (uint)n;
for (int k = 0; k < 8; k++)
{
if ((c & 1) != 0)
{
c = 0xedb88320 ^ (c >> 1);
}
else
{
c >>= 1;
}
}
_crcTable[n] = c;
}
}
private ref struct PngChunk
{
public uint ChunkType;
public ReadOnlySpan<byte> Data;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct PngHeader
{
public int Width;
public int Height;
public byte BitDepth;
public byte ColorType;
public byte CompressionMethod;
public byte FilterMethod;
public byte InterlaceMethod;
}
private enum FilterType : byte
{
None = 0,
Sub = 1,
Up = 2,
Average = 3,
Paeth = 4,
}
private const uint IhdrMagic = ((byte)'I' << 24) | ((byte)'H' << 16) | ((byte)'D' << 8) | (byte)'R';
private const uint PlteMagic = ((byte)'P' << 24) | ((byte)'L' << 16) | ((byte)'T' << 8) | (byte)'E';
private const uint IdatMagic = ((byte)'I' << 24) | ((byte)'D' << 16) | ((byte)'A' << 8) | (byte)'T';
private const uint IendMagic = ((byte)'I' << 24) | ((byte)'E' << 16) | ((byte)'N' << 8) | (byte)'D';
private static readonly byte[] _pngSignature =
[
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a
];
public static ImageLoadResult TryLoadHeader(ReadOnlySpan<byte> pngData, out ImageParameters parameters)
{
parameters = default;
if (pngData.Length < 8)
{
return ImageLoadResult.DataTooShort;
}
if (!pngData[..8].SequenceEqual(_pngSignature))
{
return ImageLoadResult.CorruptedHeader;
}
pngData = pngData[8..];
ImageLoadResult result = TryParseChunk(pngData, out PngChunk ihdrChunk);
if (result != ImageLoadResult.Success)
{
return result;
}
if (ihdrChunk.ChunkType != IhdrMagic)
{
return ImageLoadResult.CorruptedHeader;
}
if (ihdrChunk.Data.Length < Unsafe.SizeOf<PngHeader>())
{
return ImageLoadResult.DataTooShort;
}
PngHeader header = MemoryMarshal.Cast<byte, PngHeader>(ihdrChunk.Data)[0];
if (!ValidateHeader(header))
{
return ImageLoadResult.CorruptedHeader;
}
parameters = new(
ReverseEndianness(header.Width),
ReverseEndianness(header.Height),
1,
1,
ImageFormat.R8G8B8A8Unorm,
ImageDimensions.Dim2D);
return ImageLoadResult.Success;
}
public static ImageLoadResult TryLoadData(ReadOnlySpan<byte> pngData, Span<byte> output)
{
if (pngData.Length < 8)
{
return ImageLoadResult.DataTooShort;
}
if (!pngData[..8].SequenceEqual(_pngSignature))
{
return ImageLoadResult.CorruptedHeader;
}
pngData = pngData[8..];
ImageLoadResult result = TryParseChunk(pngData, out PngChunk ihdrChunk);
if (result != ImageLoadResult.Success)
{
return result;
}
if (ihdrChunk.ChunkType != IhdrMagic)
{
return ImageLoadResult.CorruptedHeader;
}
if (ihdrChunk.Data.Length < Unsafe.SizeOf<PngHeader>())
{
return ImageLoadResult.DataTooShort;
}
PngHeader header = MemoryMarshal.Cast<byte, PngHeader>(ihdrChunk.Data)[0];
if (!ValidateHeader(header))
{
return ImageLoadResult.CorruptedHeader;
}
// We currently don't support Adam7 interlaced images.
if (header.InterlaceMethod != 0)
{
return ImageLoadResult.UnsupportedFormat;
}
// Make sure the output can fit the data.
if (output.Length < ReverseEndianness(header.Width) * ReverseEndianness(header.Height) * 4)
{
return ImageLoadResult.OutputTooShort;
}
pngData = pngData[(ihdrChunk.Data.Length + ChunkOverheadSize)..];
int outputOffset = 0;
int bpp = header.ColorType switch
{
0 => (header.BitDepth + 7) / 8,
2 => ((header.BitDepth + 7) / 8) * 3,
3 => 1,
4 => ((header.BitDepth + 7) / 8) * 2,
6 => ((header.BitDepth + 7) / 8) * 4,
_ => 0,
};
ReadOnlySpan<uint> palette = ReadOnlySpan<uint>.Empty;
using MemoryStream compressedStream = new();
using ZLibStream zLibStream = new(compressedStream, CompressionMode.Decompress);
int stride = ReverseEndianness(header.Width) * bpp;
Span<byte> tempOutput = header.ColorType == 6 && header.BitDepth <= 8 ? output : new byte[stride * ReverseEndianness(header.Height)];
byte[] scanline = new byte[stride];
int scanlineOffset = 0;
int filterType = -1;
while (pngData.Length > 0)
{
result = TryParseChunk(pngData, out PngChunk chunk);
if (result != ImageLoadResult.Success)
{
return result;
}
switch (chunk.ChunkType)
{
case IhdrMagic:
break;
case PlteMagic:
palette = DecodePalette(chunk.Data);
break;
case IdatMagic:
long position = compressedStream.Position;
compressedStream.Seek(0, SeekOrigin.End);
compressedStream.Write(chunk.Data);
compressedStream.Seek(position, SeekOrigin.Begin);
try
{
DecodeImageData(
zLibStream,
tempOutput,
ref outputOffset,
scanline,
ref scanlineOffset,
ref filterType,
ReverseEndianness(header.Width),
bpp);
}
catch (InvalidDataException)
{
return ImageLoadResult.CorruptedData;
}
break;
case IendMagic:
pngData = ReadOnlySpan<byte>.Empty;
break;
default:
bool isAncillary = char.IsAsciiLetterLower((char)(chunk.ChunkType >> 24));
if (!isAncillary)
{
return ImageLoadResult.CorruptedHeader;
}
break;
}
if (pngData.IsEmpty)
{
break;
}
pngData = pngData[(chunk.Data.Length + ChunkOverheadSize)..];
}
if (header.BitDepth == 16)
{
Convert16BitTo8Bit(tempOutput[..(tempOutput.Length / 2)], tempOutput);
tempOutput = tempOutput[..(tempOutput.Length / 2)];
}
switch (header.ColorType)
{
case 0:
CopyLToRgba(output, tempOutput);
break;
case 2:
CopyRgbToRgba(output, tempOutput);
break;
case 3:
CopyIndexedToRgba(output, tempOutput, palette);
break;
case 4:
CopyLaToRgba(output, tempOutput);
break;
case 6:
if (header.BitDepth == 16)
{
tempOutput.CopyTo(output);
}
break;
}
return ImageLoadResult.Success;
}
private static bool ValidateHeader(in PngHeader header)
{
// Width and height must be a non-zero positive value.
if (ReverseEndianness(header.Width) <= 0 || ReverseEndianness(header.Height) <= 0)
{
return false;
}
// Only compression and filter methods 0 were ever defined as part of the spec, everything else is invalid.
if ((header.CompressionMethod | header.FilterMethod) != 0)
{
return false;
}
// Only interlace methods 0 (None) and 1 (Adam7) are valid.
if ((header.InterlaceMethod | 1) != 1)
{
return false;
}
return header.ColorType switch
{
0 => header.BitDepth == 1 ||
header.BitDepth == 2 ||
header.BitDepth == 4 ||
header.BitDepth == 8 ||
header.BitDepth == 16,
2 or 4 or 6 => header.BitDepth == 8 || header.BitDepth == 16,
3 => header.BitDepth == 1 ||
header.BitDepth == 2 ||
header.BitDepth == 4 ||
header.BitDepth == 8,
_ => false,
};
}
private static ImageLoadResult TryParseChunk(ReadOnlySpan<byte> pngData, out PngChunk chunk)
{
if (pngData.Length < 8)
{
chunk = default;
return ImageLoadResult.DataTooShort;
}
uint length = BinaryPrimitives.ReadUInt32BigEndian(pngData);
uint chunkType = BinaryPrimitives.ReadUInt32BigEndian(pngData[4..]);
if (length + ChunkOverheadSize > pngData.Length)
{
chunk = default;
return ImageLoadResult.DataTooShort;
}
uint crc = BinaryPrimitives.ReadUInt32BigEndian(pngData[(8 + (int)length)..]);
ReadOnlySpan<byte> data = pngData.Slice(8, (int)length);
if (crc != ComputeCrc(chunkType, data))
{
chunk = default;
return ImageLoadResult.CorruptedData;
}
chunk = new()
{
ChunkType = chunkType,
Data = data,
};
return ImageLoadResult.Success;
}
private static uint[] DecodePalette(ReadOnlySpan<byte> input)
{
uint[] palette = new uint[input.Length / 3];
for (int i = 0; i < palette.Length; i++)
{
byte r = input[i * 3];
byte g = input[i * 3 + 1];
byte b = input[i * 3 + 2];
palette[i] = 0xff000000 | ((uint)b << 16) | ((uint)g << 8) | r;
if (!BitConverter.IsLittleEndian)
{
palette[i] = BinaryPrimitives.ReverseEndianness(palette[i]);
}
}
return palette;
}
private static void DecodeImageData(
Stream zLibStream,
Span<byte> output,
ref int outputOffset,
byte[] scanline,
ref int scanlineOffset,
ref int filterType,
int width,
int bpp)
{
int stride = width * bpp;
while (true)
{
if (filterType == -1)
{
filterType = zLibStream.ReadByte();
if (filterType == -1)
{
break;
}
}
while (scanlineOffset < scanline.Length)
{
int bytesRead = zLibStream.Read(scanline.AsSpan()[scanlineOffset..]);
if (bytesRead == 0)
{
return;
}
scanlineOffset += bytesRead;
if (scanlineOffset >= scanline.Length)
{
scanlineOffset = 0;
break;
}
}
if (scanlineOffset == 0)
{
switch ((FilterType)filterType)
{
case FilterType.None:
scanline.AsSpan().CopyTo(output[outputOffset..]);
break;
case FilterType.Sub:
for (int x = 0; x < scanline.Length; x++)
{
byte left = x < bpp ? (byte)0 : output[outputOffset + x - bpp];
output[outputOffset + x] = (byte)(left + scanline[x]);
}
break;
case FilterType.Up:
for (int x = 0; x < scanline.Length; x++)
{
byte above = outputOffset < stride ? (byte)0 : output[outputOffset + x - stride];
output[outputOffset + x] = (byte)(above + scanline[x]);
}
break;
case FilterType.Average:
for (int x = 0; x < scanline.Length; x++)
{
byte left = x < bpp ? (byte)0 : output[outputOffset + x - bpp];
byte above = outputOffset < stride ? (byte)0 : output[outputOffset + x - stride];
output[outputOffset + x] = (byte)(((left + above) >> 1) + scanline[x]);
}
break;
case FilterType.Paeth:
for (int x = 0; x < scanline.Length; x++)
{
byte left = x < bpp ? (byte)0 : output[outputOffset + x - bpp];
byte above = outputOffset < stride ? (byte)0 : output[outputOffset + x - stride];
byte leftAbove = outputOffset < stride || x < bpp ? (byte)0 : output[outputOffset + x - bpp - stride];
output[outputOffset + x] = (byte)(PaethPredict(left, above, leftAbove) + scanline[x]);
}
break;
}
outputOffset += scanline.Length;
filterType = -1;
}
}
}
public static void Save(Stream output, ImageParameters parameters, ReadOnlySpan<byte> data, bool fastMode = false)
{
output.Write(_pngSignature);
WriteChunk(output, IhdrMagic, new PngHeader()
{
Width = ReverseEndianness(parameters.Width),
Height = ReverseEndianness(parameters.Height),
BitDepth = 8,
ColorType = 6,
});
byte[] encoded = EncodeImageData(data, parameters.Width, parameters.Height, fastMode);
for (int encodedOffset = 0; encodedOffset < encoded.Length; encodedOffset += MaxIdatChunkSize)
{
int length = Math.Min(MaxIdatChunkSize, encoded.Length - encodedOffset);
WriteChunk(output, IdatMagic, encoded.AsSpan().Slice(encodedOffset, length));
}
WriteChunk(output, IendMagic, ReadOnlySpan<byte>.Empty);
}
private static byte[] EncodeImageData(ReadOnlySpan<byte> input, int width, int height, bool fastMode)
{
int bpp = 4;
int stride = width * bpp;
byte[] tempLine = new byte[stride];
using MemoryStream ms = new();
using (ZLibStream zLibStream = new(ms, fastMode ? CompressionLevel.Fastest : CompressionLevel.SmallestSize))
{
for (int y = 0; y < height; y++)
{
ReadOnlySpan<byte> scanline = input.Slice(y * stride, stride);
FilterType filterType = fastMode ? FilterType.None : SelectFilterType(input, scanline, y, width, bpp);
zLibStream.WriteByte((byte)filterType);
switch (filterType)
{
case FilterType.None:
zLibStream.Write(scanline);
break;
case FilterType.Sub:
for (int x = 0; x < scanline.Length; x++)
{
byte left = x < bpp ? (byte)0 : scanline[x - bpp];
tempLine[x] = (byte)(scanline[x] - left);
}
zLibStream.Write(tempLine);
break;
case FilterType.Up:
for (int x = 0; x < scanline.Length; x++)
{
byte above = y == 0 ? (byte)0 : input[y * stride + x - stride];
tempLine[x] = (byte)(scanline[x] - above);
}
zLibStream.Write(tempLine);
break;
case FilterType.Average:
for (int x = 0; x < scanline.Length; x++)
{
byte left = x < bpp ? (byte)0 : scanline[x - bpp];
byte above = y == 0 ? (byte)0 : input[y * stride + x - stride];
tempLine[x] = (byte)(scanline[x] - ((left + above) >> 1));
}
zLibStream.Write(tempLine);
break;
case FilterType.Paeth:
for (int x = 0; x < scanline.Length; x++)
{
byte left = x < bpp ? (byte)0 : scanline[x - bpp];
byte above = y == 0 ? (byte)0 : input[y * stride + x - stride];
byte leftAbove = y == 0 || x < bpp ? (byte)0 : input[y * stride + x - bpp - stride];
tempLine[x] = (byte)(scanline[x] - PaethPredict(left, above, leftAbove));
}
zLibStream.Write(tempLine);
break;
}
}
}
return ms.ToArray();
}
private static FilterType SelectFilterType(ReadOnlySpan<byte> input, ReadOnlySpan<byte> scanline, int y, int width, int bpp)
{
int stride = width * bpp;
Span<int> deltas = stackalloc int[4];
for (int x = 0; x < scanline.Length; x++)
{
byte left = x < bpp ? (byte)0 : scanline[x - bpp];
byte above = y == 0 ? (byte)0 : input[y * stride + x - stride];
byte leftAbove = y == 0 || x < bpp ? (byte)0 : input[y * stride + x - bpp - stride];
int value = scanline[x];
int valueSub = value - left;
int valueUp = value - above;
int valueAverage = value - ((left + above) >> 1);
int valuePaeth = value - PaethPredict(left, above, leftAbove);
deltas[0] += Math.Abs(valueSub);
deltas[1] += Math.Abs(valueUp);
deltas[2] += Math.Abs(valueAverage);
deltas[3] += Math.Abs(valuePaeth);
}
int lowestDelta = int.MaxValue;
FilterType bestCandidate = FilterType.None;
for (int i = 0; i < deltas.Length; i++)
{
if (deltas[i] < lowestDelta)
{
lowestDelta = deltas[i];
bestCandidate = (FilterType)(i + 1);
}
}
return bestCandidate;
}
private static void WriteChunk<T>(Stream output, uint chunkType, T data) where T : unmanaged
{
WriteChunk(output, chunkType, MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateSpan(ref data, 1)));
}
private static void WriteChunk(Stream output, uint chunkType, ReadOnlySpan<byte> data)
{
WriteUInt32BE(output, (uint)data.Length);
WriteUInt32BE(output, chunkType);
output.Write(data);
WriteUInt32BE(output, ComputeCrc(chunkType, data));
}
private static void WriteUInt32BE(Stream output, uint value)
{
output.WriteByte((byte)(value >> 24));
output.WriteByte((byte)(value >> 16));
output.WriteByte((byte)(value >> 8));
output.WriteByte((byte)value);
}
private static int PaethPredict(int a, int b, int c)
{
int p = a + b - c;
int pa = Math.Abs(p - a);
int pb = Math.Abs(p - b);
int pc = Math.Abs(p - c);
if (pa <= pb && pa <= pc)
{
return a;
}
else if (pb <= pc)
{
return b;
}
else
{
return c;
}
}
private static void Convert16BitTo8Bit(Span<byte> output, ReadOnlySpan<byte> input)
{
for (int i = 0; i < input.Length; i += 2)
{
output[i / 2] = input[i];
}
}
private static void CopyLToRgba(Span<byte> output, ReadOnlySpan<byte> input)
{
int width = input.Length;
for (int pixel = 0; pixel < width; pixel++)
{
byte luminance = input[pixel];
int dstX = pixel * 4;
output[dstX] = luminance;
output[dstX + 1] = luminance;
output[dstX + 2] = luminance;
output[dstX + 3] = 0xff;
}
}
private static void CopyRgbToRgba(Span<byte> output, ReadOnlySpan<byte> input)
{
int width = input.Length / 3;
for (int pixel = 0; pixel < width; pixel++)
{
int srcX = pixel * 3;
int dstX = pixel * 4;
output[dstX] = input[srcX];
output[dstX + 1] = input[srcX + 1];
output[dstX + 2] = input[srcX + 2];
output[dstX + 3] = 0xff;
}
}
private static void CopyIndexedToRgba(Span<byte> output, ReadOnlySpan<byte> input, ReadOnlySpan<uint> palette)
{
Span<uint> outputAsUint = MemoryMarshal.Cast<byte, uint>(output);
for (int pixel = 0; pixel < outputAsUint.Length; pixel++)
{
byte index = input[pixel];
if (index < palette.Length)
{
outputAsUint[pixel] = palette[index];
}
}
}
private static void CopyLaToRgba(Span<byte> output, ReadOnlySpan<byte> input)
{
int width = input.Length / 2;
for (int pixel = 0; pixel < width; pixel++)
{
int srcX = pixel * 2;
int dstX = pixel * 4;
byte luminance = input[srcX];
byte alpha = input[srcX + 1];
output[dstX] = luminance;
output[dstX + 1] = luminance;
output[dstX + 2] = luminance;
output[dstX + 3] = alpha;
}
}
private static uint ComputeCrc(uint chunkType, ReadOnlySpan<byte> input)
{
uint crc = UpdateCrc(uint.MaxValue, (byte)(chunkType >> 24));
crc = UpdateCrc(crc, (byte)(chunkType >> 16));
crc = UpdateCrc(crc, (byte)(chunkType >> 8));
crc = UpdateCrc(crc, (byte)chunkType);
crc = UpdateCrc(crc, input);
return ~crc;
}
private static uint UpdateCrc(uint crc, byte input)
{
return _crcTable[(byte)(crc ^ input)] ^ (crc >> 8);
}
private static uint UpdateCrc(uint crc, ReadOnlySpan<byte> input)
{
uint c = crc;
for (int n = 0; n < input.Length; n++)
{
c = _crcTable[(byte)(c ^ input[n])] ^ (c >> 8);
}
return c;
}
private static int ReverseEndianness(int value)
{
if (BitConverter.IsLittleEndian)
{
return BinaryPrimitives.ReverseEndianness(value);
}
return value;
}
}
}