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

+93 -2
View File
@@ -40,6 +40,30 @@ public sealed class JpegMalformedTests
}
}
[TestMethod]
public void AFileCutAfterItsHeadersFailsAtTheFirstMcuRow()
{
// Headers of 8192×8192 (64 megapixels, within the default limits) and not one byte of the scan. The reader
// gives zero bits past the end, which are valid codes of the Annex K tables, so before the check per MCU row
// the decoder went through all 262,144 MCUs (seconds of IDCT) before failing; now it stops after one row.
foreach (var lossless in new[] { false, true })
{
var file = lossless ? JpegTestEncoder.WriteLossless(16, 16, 8, [(1, 1, 1, new int[256])], 1, 0, new JpegTestOptions()) : Baseline(16, 16);
var sof = SegmentOffset(file, lossless ? 0xC3 : 0xC0);
var sos = SegmentOffset(file, 0xDA);
var headers = file[..(sos + 2 + ((file[sos + 2] << 8) | file[sos + 3]))];
headers[sof + 5] = headers[sof + 7] = 0x20;
headers[sof + 6] = headers[sof + 8] = 0x00;
var clock = System.Diagnostics.Stopwatch.StartNew();
var error = Assert.Throws<ImageDecodeException>(() => JpegDecoder.Decode(headers), lossless ? "senza perdita" : "base");
clock.Stop();
StringAssert.Contains(error.Message, "troncato");
// Planes of 64 MB are still allocated; decoding every MCU took several seconds even in Release.
Assert.IsLessThan(2000, clock.ElapsedMilliseconds, $"{(lossless ? "senza perdita" : "base")}: {clock.ElapsedMilliseconds} ms");
}
}
[TestMethod]
public void BytesAfterTheEndAreIgnored()
{
@@ -141,11 +165,15 @@ public sealed class JpegMalformedTests
// No quantization table.
AssertDamaged(RemoveSegments(file, 0xDB), "senza DQT");
// A Huffman table with more codes than its lengths allow (two codes of length 1, then more).
// A Huffman table with more codes than its lengths allow; the total stays that of the segment's values, so
// the error is the oversubscription itself and not a truncated segment.
var dht = SegmentOffset(file, 0xC4);
var badTable = file.ToArray();
// The luminance DC table counts 0,1,5,1,1,...: three codes of length 1, none of length 2, three of length 3.
badTable[dht + 5] = 3;
AssertDamaged(badTable, "tabella di Huffman impossibile");
badTable[dht + 6] = 0;
badTable[dht + 7] = 3;
AssertOversubscribed(badTable, "tabella di Huffman impossibile");
// A segment length that runs past the end.
var longSegment = file.ToArray();
@@ -158,6 +186,63 @@ public sealed class JpegMalformedTests
AssertDamaged(file[..sos], "senza scansioni");
}
[TestMethod]
public void OversubscribedHuffmanTablesAreRefusedAtEveryLength()
{
// One code of each length below L leaves room for two codes of length L; three overflow it. The symbols
// match the counts, so only the oversubscription can be at fault. Lengths up to 9 used to index past the
// lookup table and threw ArgumentOutOfRangeException.
for (var length = 1; length <= 16; length++)
{
var counts = new byte[16];
for (var shorter = 1; shorter < length; shorter++)
{
counts[shorter - 1] = 1;
}
counts[length - 1] = 3;
Assert.IsTrue(Oversubscribed(counts), $"lunghezza {length}");
var values = new byte[length + 2];
for (var i = 0; i < values.Length; i++)
{
values[i] = (byte)i;
}
var error = Assert.ThrowsExactly<ImageDecodeException>(() => JpegHuffmanTable.Build(counts, values), $"lunghezza {length}");
StringAssert.Contains(error.Message, "non stanno nella loro lunghezza", $"lunghezza {length}");
// Two codes of length L fill the code space exactly and are valid.
counts[length - 1] = 2;
_ = JpegHuffmanTable.Build(counts, values.AsSpan(0, length + 1));
}
// Three codes of length 1 (the case of the review) and a full table that is still valid.
var three = new byte[16];
three[0] = 3;
Assert.ThrowsExactly<ImageDecodeException>(() => JpegHuffmanTable.Build(three, [0, 1, 2]));
var full = new byte[16];
full[0] = 1;
full[1] = 2;
_ = JpegHuffmanTable.Build(full, [0, 1, 2]);
}
private static bool Oversubscribed(byte[] counts)
{
long code = 0;
for (var length = 1; length <= 16; length++)
{
code += counts[length - 1];
if (code > 1L << length)
{
return true;
}
code <<= 1;
}
return false;
}
[TestMethod]
public void TheLimitsAreEnforcedBeforeDecoding()
{
@@ -306,6 +391,12 @@ public sealed class JpegMalformedTests
Assert.IsFalse(string.IsNullOrWhiteSpace(error.Message), what);
}
private static void AssertOversubscribed(byte[] file, string what)
{
var error = Assert.ThrowsExactly<ImageDecodeException>(() => JpegDecoder.Decode(file), what);
StringAssert.Contains(error.Message, "non stanno nella loro lunghezza", what);
}
/// <summary>Offset of the first marker segment of type <paramref name="marker"/> (the 0xFF byte).</summary>
private static int SegmentOffset(byte[] file, int marker) => SegmentOffsets(file, marker)[0];