refactor(codecs): pool luminance rows and gate timing on optimised builds

EncodeRow8/16 reuse one pooled scratch row for RGB-to-gray output
instead of allocating per row; drop unused JPEG marker constants; the
24 MP timing test skips itself when the JIT optimiser is disabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:12:49 +00:00
1 parent 78dad093ab
commit f2c0709ef2
3 files changed
+28 -31

No files matched your search

+22 -29
View File
@@ -1,3 +1,4 @@
using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices;
@@ -132,57 +133,48 @@ public static class PixelConvert
/// <summary>Encodes row <paramref name="y"/> into interleaved 8-bit samples (codec edge helper).</summary>
internal static void EncodeRow8(ImageBuffer image, int y, Span<byte> row, int channels, TransferFunction transfer, bool dither)
{
var w = image.Width;
Span<float> offsets = stackalloc float[8];
if (dither)
{
for (var i = 0; i < 8; i++) offsets[i] = 0.5f + Bayer[(y & 7) * 8 + i];
}
else
{
offsets.Fill(0.5f);
}
for (var i = 0; i < 8; i++) offsets[i] = dither ? 0.5f + Bayer[(y & 7) * 8 + i] : 0.5f;
var srgb = transfer == TransferFunction.Srgb;
var colour = channels is 1 or 2 ? 1 : 3;
var scratch = channels <= 2 && image.Channels >= 3 ? ArrayPool<float>.Shared.Rent(image.Width) : null;
for (var c = 0; c < channels; c++)
{
if (c == colour)
if (c == (channels <= 2 ? 1 : 3))
{
var a = image.Channels == 4 ? image.Row(3, y) : default;
for (var x = 0; x < w; x++) row[x * channels + c] = a.IsEmpty ? (byte)255 : (byte)(Math.Clamp(a[x], 0f, 1f) * 255f + 0.5f);
for (var x = 0; x < image.Width; x++) row[x * channels + c] = a.IsEmpty ? (byte)255 : (byte)(Math.Clamp(a[x], 0f, 1f) * 255f + 0.5f);
continue;
}
var src = SourceRow(image, y, c, colour, out var luminance);
for (var x = 0; x < w; x++)
var src = ColourRow(image, y, c, channels, scratch);
for (var x = 0; x < src.Length; x++)
{
var v = luminance is null ? src[x] : luminance[x];
var e = srgb ? ColorSpace.EncodeFast(v) : Math.Clamp(v, 0f, 1f);
var e = srgb ? ColorSpace.EncodeFast(src[x]) : Math.Clamp(src[x], 0f, 1f);
row[x * channels + c] = (byte)Math.Clamp(e * 255f + offsets[x & 7], 0f, 255f);
}
}
if (scratch is not null) ArrayPool<float>.Shared.Return(scratch);
}
/// <summary>Encodes row <paramref name="y"/> into interleaved 16-bit samples (codec edge helper).</summary>
internal static void EncodeRow16(ImageBuffer image, int y, Span<ushort> row, int channels, TransferFunction transfer)
{
var w = image.Width;
var srgb = transfer == TransferFunction.Srgb;
var colour = channels is 1 or 2 ? 1 : 3;
var scratch = channels <= 2 && image.Channels >= 3 ? ArrayPool<float>.Shared.Rent(image.Width) : null;
for (var c = 0; c < channels; c++)
{
if (c == colour)
if (c == (channels <= 2 ? 1 : 3))
{
var a = image.Channels == 4 ? image.Row(3, y) : default;
for (var x = 0; x < w; x++) row[x * channels + c] = a.IsEmpty ? (ushort)65535 : (ushort)(Math.Clamp(a[x], 0f, 1f) * 65535f + 0.5f);
for (var x = 0; x < image.Width; x++) row[x * channels + c] = a.IsEmpty ? (ushort)65535 : (ushort)(Math.Clamp(a[x], 0f, 1f) * 65535f + 0.5f);
continue;
}
var src = SourceRow(image, y, c, colour, out var luminance);
for (var x = 0; x < w; x++)
var src = ColourRow(image, y, c, channels, scratch);
for (var x = 0; x < src.Length; x++)
{
var v = luminance is null ? src[x] : luminance[x];
row[x * channels + c] = srgb ? ColorSpace.Encode16(v) : (ushort)(Math.Clamp(v, 0f, 1f) * 65535f + 0.5f);
row[x * channels + c] = srgb ? ColorSpace.Encode16(src[x]) : (ushort)(Math.Clamp(src[x], 0f, 1f) * 65535f + 0.5f);
}
}
if (scratch is not null) ArrayPool<float>.Shared.Return(scratch);
}
/// <summary>Single-channel buffer replicated into three channels.</summary>
@@ -302,17 +294,18 @@ public static class PixelConvert
return result;
}
private static ReadOnlySpan<float> SourceRow(ImageBuffer image, int y, int c, int colourChannels, out float[]? luminance)
/// <summary>Linear row feeding output colour channel <paramref name="c"/>: gray is replicated, and a gray output
/// from RGB takes Rec.709 luminance computed into <paramref name="scratch"/>.</summary>
private static ReadOnlySpan<float> ColourRow(ImageBuffer image, int y, int c, int channels, float[]? scratch)
{
luminance = null;
if (image.Channels == 1) return image.Row(0, y);
if (colourChannels == 3) return image.Row(c, y);
if (scratch is null) return image.Row(c, y);
var r = image.Row(0, y);
var g = image.Row(1, y);
var b = image.Row(2, y);
luminance = new float[image.Width];
var luminance = scratch.AsSpan(0, image.Width);
for (var x = 0; x < luminance.Length; x++) luminance[x] = ColorSpace.Luminance(r[x], g[x], b[x]);
return default;
return luminance;
}
/// <summary>De-interleaves one row: colour through <paramref name="lut"/> (indices clamped to its range), alpha scaled.</summary>