Correzioni della revisione dei codec di casa (C1)

Correggo i difetti confermati dalla revisione di funzione/codec-immagini e
aggiungo le prove che li avrebbero trovati.

- JPEG: una tabella di Huffman con troppi codici corti dava
  ArgumentOutOfRangeException invece di ImageDecodeException, e un file
  rovinato faceva fallire importazione e ricostruzione; ora il controllo si
  fa prima di scrivere le voci. La prova che diceva di coprirlo cadeva invece
  su «tabella troncata»: ora tiene il totale giusto e controlla il messaggio.
- JPEG: il troncamento delle scansioni di Huffman si vede a ogni riga di MCU,
  non dopo l'ultimo MCU (un file tagliato dopo le intestazioni costava 6,7 s).
- JPEG: IDCT ridotta a 1/2 e 1/4 calcolata dai coefficienti; miniatura di una
  foto 4000x3000 da 126 a 59 ms in Release. Il predittore senza perdita non
  cerca più l'indice della componente a ogni campione.
- WebP: con livello del filtro del fotogramma 0 il filtro è spento, come nei
  decodificatori di riferimento; VP8L costruisce solo i gruppi nominati dai
  meta codici (prima fino a 335 MB di tabelle da 160 KB di file).
- BMP OS/2: altezza senza segno e sempre dal basso.
- Array di lavoro dei codec oltre 16 MiB fuori dal pool condiviso, che li
  avrebbe tenuti dopo l'uso.
- Conversione: profilo ICC copiato solo se RGB, profondità oltre 8 bit e
  blocchi gAMA/cHRM/sRGB dichiarati in NotCarried, Lossy vero quando si perde
  precisione.
- Impronta e misure di una foto da una sola lettura e decodifica; ReadSize
  legge solo l'inizio di JPEG, GIF e BMP.
- docs: decisioni (righe aggiunte, con la correzione dei 2,0 MB del set di
  riferimento) e windows.md (Febe.exe rimisurato: 85,6 MB, a capo sistemato).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 16:52:17 +01:00
1 parent d6a6a68496
commit 7e8296e790
32 files changed
+1120 -132

No files matched your search

+82 -19
View File
@@ -75,10 +75,17 @@ internal static class JpegDct
/// <summary>
/// Like <see cref="Inverse"/>, but writes the block reduced by 2^<paramref name="shift"/> (1 to 3) in each
/// direction: every output sample is the rounded mean of a square of the full block's samples. At an eighth that
/// mean is the DC term alone (S00 / 8 plus the level shift: the AC basis functions sum to zero over the block), so
/// no transform is computed.
/// direction: every output sample is the mean of a square of the full block's samples (before rounding and
/// clamping), computed directly from the coefficients without the full transform.
/// </summary>
/// <remarks>
/// Averaging two neighbours of the eight-point inverse x[n] = Σ C(k) X[k] cos((2n+1)kπ/16) gives
/// Σ C(k) X[k] cos(kπ/16) cos((2m+1)kπ/8). Coefficient 4 drops out (cos((2m+1)π/2) = 0) and coefficient 8 − k
/// folds onto k with weight −sin(kπ/16), so the half-size block is a four-point inverse of
/// Y0 = C(0) X0, Yk = Xk cos(kπ/16) − X(8−k) sin(kπ/16). Averaging pairs of that once more leaves
/// Y0 ± ((C2 + C6) Y1 + (C6 − C2) Y3) / 2, and at an eighth only the DC term remains (S00 / 8 plus the level
/// shift: the AC basis functions sum to zero over the block).
/// </remarks>
public static void InverseReduced(ReadOnlySpan<short> block, ReadOnlySpan<ushort> quantization, Span<byte> output, int stride, int precision, ReadOnlySpan<byte> to8Bit, int shift)
{
var max = precision == 12 ? 4095 : 255;
@@ -91,32 +98,88 @@ internal static class JpegDct
return;
}
Span<byte> full = stackalloc byte[64];
Inverse(block, quantization, full, 8, precision, to8Bit);
var n = 8 >> shift;
var side = 1 << shift;
var area = side * side;
var half = area >> 1;
for (var y = 0; y < n; y++)
Span<float> column = stackalloc float[8];
Span<float> work = stackalloc float[32];
Span<float> reduced = stackalloc float[4];
for (var c = 0; c < 8; c++)
{
var o = output.Slice(y * stride, n);
for (var x = 0; x < n; x++)
if (block[8 + c] == 0 && block[24 + c] == 0 && block[40 + c] == 0 && block[56 + c] == 0
&& (shift == 2 || (block[16 + c] == 0 && block[48 + c] == 0)))
{
var sum = 0;
for (var dy = 0; dy < side; dy++)
// Only the coefficients that reach the reduced samples are tested (rows 4, and 2 and 6 at a quarter, drop out).
var dc = block[c] * quantization[c] * C4;
for (var r = 0; r < n; r++)
{
var row = full.Slice((((y * side) + dy) * 8) + (x * side), side);
for (var dx = 0; dx < side; dx++)
{
sum += row[dx];
}
work[(r * 8) + c] = dc;
}
o[x] = (byte)((sum + half) / area);
continue;
}
for (var r = 0; r < 8; r++)
{
column[r] = block[(r * 8) + c] * quantization[(r * 8) + c];
}
Reduce1D(column, reduced, shift);
for (var r = 0; r < n; r++)
{
work[(r * 8) + c] = reduced[r];
}
}
// Each pass doubles the T.81 values, as in Inverse: a quarter brings them back.
var levelShift = precision == 12 ? 2048.5f : 128.5f;
for (var r = 0; r < n; r++)
{
Reduce1D(work.Slice(r * 8, 8), reduced, shift);
var o = output.Slice(r * stride, n);
for (var c = 0; c < n; c++)
{
var value = (int)((reduced[c] * 0.25f) + levelShift);
if ((uint)value > (uint)max)
{
value = value < 0 ? 0 : max;
}
o[c] = precision == 12 ? to8Bit[value] : (byte)value;
}
}
}
/// <summary>
/// The means of 2^<paramref name="shift"/> neighbouring points (shift 1 or 2) of the eight-point inverse of
/// <paramref name="x"/> (twice the T.81 inverse, like <see cref="Inverse1D"/>): four or two values.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Reduce1D(ReadOnlySpan<float> x, Span<float> output, int shift)
{
var y0 = C4 * x[0];
var y1 = (C1 * x[1]) - (C7 * x[7]);
var y3 = (C3 * x[3]) - (C5 * x[5]);
if (shift == 2)
{
var d = (HalfC2PlusC6 * y1) + (HalfC6MinusC2 * y3);
output[0] = y0 + d;
output[1] = y0 - d;
return;
}
var y2 = (C2 * x[2]) - (C6 * x[6]);
var e0 = y0 + (C4 * y2);
var e1 = y0 - (C4 * y2);
var o0 = (C2 * y1) + (C6 * y3);
var o1 = (C6 * y1) - (C2 * y3);
output[0] = e0 + o0;
output[1] = e1 + o1;
output[2] = e1 - o1;
output[3] = e0 - o0;
}
private const float HalfC2PlusC6 = (C2 + C6) / 2;
private const float HalfC6MinusC2 = (C6 - C2) / 2;
/// <summary>
/// The forward transform of a block of level-shifted samples (natural order) into DCT coefficients, natural order,
/// not yet quantized.