|
|
|
@ -10,132 +10,8 @@ |
|
|
|
<#@ import namespace="System.Runtime.InteropServices" #> |
|
|
|
<#@ output extension=".cs" #> |
|
|
|
<# |
|
|
|
void GenerateToDestFormatMethods(string pixelType) |
|
|
|
{ |
|
|
|
#> |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Converts 'count' pixels in 'sourcePixels` span to a span of <see cref="<#=pixelType#>"/>-s. |
|
|
|
/// Bulk version of <see cref="IPixel.To<#=pixelType#>(ref <#=pixelType#>)"/>. |
|
|
|
/// </summary> |
|
|
|
/// <param name="sourcePixels">The span of source pixels</param> |
|
|
|
/// <param name="dest">The destination span of <see cref="<#=pixelType#>"/> data.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
internal virtual void To<#=pixelType#>(ReadOnlySpan<TPixel> sourcePixels, Span<<#=pixelType#>> dest, int count) |
|
|
|
{ |
|
|
|
GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); |
|
|
|
|
|
|
|
ref TPixel sourceBaseRef = ref MemoryMarshal.GetReference(sourcePixels); |
|
|
|
ref <#=pixelType#> destBaseRef = ref MemoryMarshal.GetReference(dest); |
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) |
|
|
|
{ |
|
|
|
ref TPixel sp = ref Unsafe.Add(ref sourceBaseRef, i); |
|
|
|
ref <#=pixelType#> dp = ref Unsafe.Add(ref destBaseRef, i); |
|
|
|
sp.To<#=pixelType#>(ref dp); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// A helper for <see cref="To<#=pixelType#>(ReadOnlySpan{TPixel}, Span{<#=pixelType#>}, int)"/> that expects a byte span as destination. |
|
|
|
/// The layout of the data in 'destBytes' must be compatible with <see cref="<#=pixelType#>"/> layout. |
|
|
|
/// </summary> |
|
|
|
/// <param name="sourceColors">The <see cref="Span{T}"/> to the source colors.</param> |
|
|
|
/// <param name="destBytes">The <see cref="Span{T}"/> to the destination bytes.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
internal void To<#=pixelType#>Bytes(ReadOnlySpan<TPixel> sourceColors, Span<byte> destBytes, int count) |
|
|
|
{ |
|
|
|
this.To<#=pixelType#>(sourceColors, MemoryMarshal.Cast<byte, <#=pixelType#>>(destBytes), count); |
|
|
|
} |
|
|
|
<# |
|
|
|
} |
|
|
|
|
|
|
|
void GeneratePackFromMethodUsingPackFromRgba64(string pixelType, string rgbaOperationCode) |
|
|
|
{ |
|
|
|
#> |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Converts 'count' elements in 'source` span of <see cref="<#=pixelType#>"/> data to a span of <typeparamref name="TPixel"/>-s. |
|
|
|
/// </summary> |
|
|
|
/// <param name="source">The source <see cref="Span{T}"/> of <see cref="<#=pixelType#>"/> data.</param> |
|
|
|
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
internal virtual void PackFrom<#=pixelType#>(ReadOnlySpan<<#=pixelType#>> source, Span<TPixel> destPixels, int count) |
|
|
|
{ |
|
|
|
GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); |
|
|
|
|
|
|
|
ref <#=pixelType#> sourceRef = ref MemoryMarshal.GetReference(source); |
|
|
|
ref TPixel destRef = ref MemoryMarshal.GetReference(destPixels); |
|
|
|
|
|
|
|
var rgba = new Rgba64(0, 0, 0, 65535); |
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) |
|
|
|
{ |
|
|
|
ref TPixel dp = ref Unsafe.Add(ref destRef, i); |
|
|
|
<#=rgbaOperationCode#> |
|
|
|
dp.PackFromRgba64(rgba); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// A helper for <see cref="PackFrom<#=pixelType#>(ReadOnlySpan{<#=pixelType#>}, Span{TPixel}, int)"/> that expects a byte span. |
|
|
|
/// The layout of the data in 'sourceBytes' must be compatible with <see cref="<#=pixelType#>"/> layout. |
|
|
|
/// </summary> |
|
|
|
/// <param name="sourceBytes">The <see cref="ReadOnlySpan{T}"/> to the source bytes.</param> |
|
|
|
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
internal void PackFrom<#=pixelType#>Bytes(ReadOnlySpan<byte> sourceBytes, Span<TPixel> destPixels, int count) |
|
|
|
{ |
|
|
|
this.PackFrom<#=pixelType#>(MemoryMarshal.Cast<byte, <#=pixelType#>>(sourceBytes), destPixels, count); |
|
|
|
} |
|
|
|
<# |
|
|
|
} |
|
|
|
|
|
|
|
void GeneratePackFromMethodUsingPackFromRgb48(string pixelType, string rgbaOperationCode) |
|
|
|
{ |
|
|
|
#> |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Converts 'count' elements in 'source` span of <see cref="<#=pixelType#>"/> data to a span of <typeparamref name="TPixel"/>-s. |
|
|
|
/// </summary> |
|
|
|
/// <param name="source">The source <see cref="Span{T}"/> of <see cref="<#=pixelType#>"/> data.</param> |
|
|
|
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
internal virtual void PackFrom<#=pixelType#>(ReadOnlySpan<<#=pixelType#>> source, Span<TPixel> destPixels, int count) |
|
|
|
{ |
|
|
|
GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); |
|
|
|
|
|
|
|
ref <#=pixelType#> sourceRef = ref MemoryMarshal.GetReference(source); |
|
|
|
ref TPixel destRef = ref MemoryMarshal.GetReference(destPixels); |
|
|
|
|
|
|
|
var rgb = default(Rgb48); |
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) |
|
|
|
{ |
|
|
|
ref TPixel dp = ref Unsafe.Add(ref destRef, i); |
|
|
|
<#=rgbaOperationCode#> |
|
|
|
dp.PackFromRgb48(rgb); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// A helper for <see cref="PackFrom<#=pixelType#>(ReadOnlySpan{<#=pixelType#>}, Span{TPixel}, int)"/> that expects a byte span. |
|
|
|
/// The layout of the data in 'sourceBytes' must be compatible with <see cref="<#=pixelType#>"/> layout. |
|
|
|
/// </summary> |
|
|
|
/// <param name="sourceBytes">The <see cref="ReadOnlySpan{T}"/> to the source bytes.</param> |
|
|
|
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
internal void PackFrom<#=pixelType#>Bytes(ReadOnlySpan<byte> sourceBytes, Span<TPixel> destPixels, int count) |
|
|
|
{ |
|
|
|
this.PackFrom<#=pixelType#>(MemoryMarshal.Cast<byte, <#=pixelType#>>(sourceBytes), destPixels, count); |
|
|
|
} |
|
|
|
<# |
|
|
|
} |
|
|
|
|
|
|
|
void GeneratePackFromMethodUsingPackFromRgba32(string pixelType, string rgbaOperationCode) |
|
|
|
void GeneratePackFromMethods(string pixelType, string tempPixelType, string assignToTempCode) |
|
|
|
{ |
|
|
|
#> |
|
|
|
|
|
|
|
@ -152,13 +28,14 @@ |
|
|
|
ref <#=pixelType#> sourceRef = ref MemoryMarshal.GetReference(source); |
|
|
|
ref TPixel destRef = ref MemoryMarshal.GetReference(destPixels); |
|
|
|
|
|
|
|
var rgba = new Rgba32(0, 0, 0, 255); |
|
|
|
// For conversion methods writing only to RGB channels, we need to keep the alpha channel opaque! |
|
|
|
var temp = NamedColors<<#=tempPixelType#>>.Black; |
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) |
|
|
|
{ |
|
|
|
ref TPixel dp = ref Unsafe.Add(ref destRef, i); |
|
|
|
<#=rgbaOperationCode#> |
|
|
|
dp.PackFromRgba32(rgba); |
|
|
|
<#=assignToTempCode#> |
|
|
|
dp.PackFrom<#=tempPixelType#>(temp); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -177,86 +54,43 @@ |
|
|
|
<# |
|
|
|
} |
|
|
|
|
|
|
|
void GeneratePackFromMethodUsingPackFromArgb32(string pixelType, string argbOperationCode) |
|
|
|
{ |
|
|
|
#> |
|
|
|
void GenerateToDestFormatMethods(string pixelType) |
|
|
|
{ |
|
|
|
#> |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Converts 'count' elements in 'source` span of <see cref="<#=pixelType#>"/> data to a span of <typeparamref name="TPixel"/>-s. |
|
|
|
/// Converts 'count' pixels in 'sourcePixels` span to a span of <see cref="<#=pixelType#>"/>-s. |
|
|
|
/// Bulk version of <see cref="IPixel.To<#=pixelType#>(ref <#=pixelType#>)"/>. |
|
|
|
/// </summary> |
|
|
|
/// <param name="source">The source <see cref="Span{T}"/> of <see cref="<#=pixelType#>"/> data.</param> |
|
|
|
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param> |
|
|
|
/// <param name="sourcePixels">The span of source pixels</param> |
|
|
|
/// <param name="dest">The destination span of <see cref="<#=pixelType#>"/> data.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
internal virtual void PackFrom<#=pixelType#>(ReadOnlySpan<<#=pixelType#>> source, Span<TPixel> destPixels, int count) |
|
|
|
internal virtual void To<#=pixelType#>(ReadOnlySpan<TPixel> sourcePixels, Span<<#=pixelType#>> dest, int count) |
|
|
|
{ |
|
|
|
GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); |
|
|
|
|
|
|
|
ref <#=pixelType#> sourceRef = ref MemoryMarshal.GetReference(source); |
|
|
|
ref TPixel destRef = ref MemoryMarshal.GetReference(destPixels); |
|
|
|
GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); |
|
|
|
|
|
|
|
var argb = new Argb32(0, 0, 0, 255); |
|
|
|
ref TPixel sourceBaseRef = ref MemoryMarshal.GetReference(sourcePixels); |
|
|
|
ref <#=pixelType#> destBaseRef = ref MemoryMarshal.GetReference(dest); |
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) |
|
|
|
{ |
|
|
|
ref TPixel dp = ref Unsafe.Add(ref destRef, i); |
|
|
|
<#=argbOperationCode#> |
|
|
|
dp.PackFromArgb32(argb); |
|
|
|
ref TPixel sp = ref Unsafe.Add(ref sourceBaseRef, i); |
|
|
|
ref <#=pixelType#> dp = ref Unsafe.Add(ref destBaseRef, i); |
|
|
|
sp.To<#=pixelType#>(ref dp); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// A helper for <see cref="PackFrom<#=pixelType#>(ReadOnlySpan{<#=pixelType#>}, Span{TPixel}, int)"/> that expects a byte span. |
|
|
|
/// The layout of the data in 'sourceBytes' must be compatible with <see cref="<#=pixelType#>"/> layout. |
|
|
|
/// </summary> |
|
|
|
/// <param name="sourceBytes">The <see cref="ReadOnlySpan{T}"/> to the source bytes.</param> |
|
|
|
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
internal void PackFrom<#=pixelType#>Bytes(ReadOnlySpan<byte> sourceBytes, Span<TPixel> destPixels, int count) |
|
|
|
{ |
|
|
|
this.PackFrom<#=pixelType#>(MemoryMarshal.Cast<byte, <#=pixelType#>>(sourceBytes), destPixels, count); |
|
|
|
} |
|
|
|
<# |
|
|
|
} |
|
|
|
|
|
|
|
void GeneratePackFromMethodUsingPackFromBgra32(string pixelType, string bgraOperationCode) |
|
|
|
{ |
|
|
|
#> |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// Converts 'count' elements in 'source` span of <see cref="<#=pixelType#>"/> data to a span of <typeparamref name="TPixel"/>-s. |
|
|
|
/// </summary> |
|
|
|
/// <param name="source">The source <see cref="Span{T}"/> of <see cref="<#=pixelType#>"/> data.</param> |
|
|
|
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
internal virtual void PackFrom<#=pixelType#>(ReadOnlySpan<<#=pixelType#>> source, Span<TPixel> destPixels, int count) |
|
|
|
{ |
|
|
|
GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); |
|
|
|
|
|
|
|
ref <#=pixelType#> sourceRef = ref MemoryMarshal.GetReference(source); |
|
|
|
ref TPixel destRef = ref MemoryMarshal.GetReference(destPixels); |
|
|
|
|
|
|
|
var bgra = new Bgra32(0, 0, 0, 255); |
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) |
|
|
|
{ |
|
|
|
ref TPixel dp = ref Unsafe.Add(ref destRef, i); |
|
|
|
<#=bgraOperationCode#> |
|
|
|
dp.PackFromBgra32(bgra); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// A helper for <see cref="PackFrom<#=pixelType#>(ReadOnlySpan{<#=pixelType#>}, Span{TPixel}, int)"/> that expects a byte span. |
|
|
|
/// The layout of the data in 'sourceBytes' must be compatible with <see cref="<#=pixelType#>"/> layout. |
|
|
|
/// A helper for <see cref="To<#=pixelType#>(ReadOnlySpan{TPixel}, Span{<#=pixelType#>}, int)"/> that expects a byte span as destination. |
|
|
|
/// The layout of the data in 'destBytes' must be compatible with <see cref="<#=pixelType#>"/> layout. |
|
|
|
/// </summary> |
|
|
|
/// <param name="sourceBytes">The <see cref="ReadOnlySpan{T}"/> to the source bytes.</param> |
|
|
|
/// <param name="destPixels">The <see cref="Span{T}"/> to the destination pixels.</param> |
|
|
|
/// <param name="sourceColors">The <see cref="Span{T}"/> to the source colors.</param> |
|
|
|
/// <param name="destBytes">The <see cref="Span{T}"/> to the destination bytes.</param> |
|
|
|
/// <param name="count">The number of pixels to convert.</param> |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
internal void PackFrom<#=pixelType#>Bytes(ReadOnlySpan<byte> sourceBytes, Span<TPixel> destPixels, int count) |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
internal void To<#=pixelType#>Bytes(ReadOnlySpan<TPixel> sourceColors, Span<byte> destBytes, int count) |
|
|
|
{ |
|
|
|
this.PackFrom<#=pixelType#>(MemoryMarshal.Cast<byte, <#=pixelType#>>(sourceBytes), destPixels, count); |
|
|
|
this.To<#=pixelType#>(sourceColors, MemoryMarshal.Cast<byte, <#=pixelType#>>(destBytes), count); |
|
|
|
} |
|
|
|
<# |
|
|
|
} |
|
|
|
@ -275,29 +109,28 @@ namespace SixLabors.ImageSharp.PixelFormats |
|
|
|
public partial class PixelOperations<TPixel> |
|
|
|
{ |
|
|
|
<# |
|
|
|
|
|
|
|
GeneratePackFromMethodUsingPackFromRgba64("Rgba64", "rgba = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GeneratePackFromMethods("Rgba64", "Rgba64", "temp = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GenerateToDestFormatMethods("Rgba64"); |
|
|
|
|
|
|
|
GeneratePackFromMethodUsingPackFromRgb48("Rgb48", "rgb = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GeneratePackFromMethods("Rgb48", "Rgb48", "temp = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GenerateToDestFormatMethods("Rgb48"); |
|
|
|
|
|
|
|
GeneratePackFromMethodUsingPackFromRgba32("Rgba32", "rgba = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GeneratePackFromMethods("Rgba32", "Rgba32", "temp = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GenerateToDestFormatMethods("Rgba32"); |
|
|
|
|
|
|
|
GeneratePackFromMethodUsingPackFromBgra32("Bgra32", "bgra = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GeneratePackFromMethods("Bgra32", "Bgra32", "temp = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GenerateToDestFormatMethods("Bgra32"); |
|
|
|
|
|
|
|
GeneratePackFromMethodUsingPackFromRgba32("Rgb24", "rgba.Rgb = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GeneratePackFromMethods("Rgb24", "Rgba32", "temp.Rgb = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GenerateToDestFormatMethods("Rgb24"); |
|
|
|
|
|
|
|
GeneratePackFromMethodUsingPackFromRgba32("Bgr24", "rgba.Bgr = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GeneratePackFromMethods("Bgr24", "Rgba32", "temp.Bgr = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GenerateToDestFormatMethods("Bgr24"); |
|
|
|
|
|
|
|
GeneratePackFromMethodUsingPackFromArgb32("Argb32", "argb = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GeneratePackFromMethods("Argb32", "Argb32", "temp = Unsafe.Add(ref sourceRef, i);"); |
|
|
|
GenerateToDestFormatMethods("Argb32"); |
|
|
|
|
|
|
|
#> |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |