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

+64 -10
View File
@@ -27,7 +27,10 @@ public sealed record ImageConversionOptions
/// <param name="Target">The format written.</param>
/// <param name="Width">Width as stored (the orientation is not applied: the tag is copied).</param>
/// <param name="Height">Height as stored.</param>
/// <param name="Lossy">Whether pixels may differ from the source's (a JPEG target).</param>
/// <param name="Lossy">
/// Whether pixels may differ from the source's: a JPEG target, or a source with more than 8 bits per sample (the
/// decoders keep 8).
/// </param>
/// <param name="Metadata">The metadata written.</param>
/// <param name="NotCarried">What of the source's metadata the target could not take, in Italian, for the interface.</param>
public sealed record ImageConversionResult(
@@ -42,12 +45,15 @@ public sealed record ImageConversionResult(
/// <summary>
/// Turns a picture into another format with the same pixels and the same metadata, so that the converted file is
/// the same photo with another extension (exports, decision of 18 November 2026): PNG and WebP losslessly, JPEG at
/// the quality of the options. The EXIF block (dates, camera, the orientation tag), the ICC profile and the XMP
/// packet are copied as they are; the pixels stay as stored, not turned by the orientation, which the copied tag
/// still describes. Texts go where the target can hold them: PNG text chunks, JPEG comments; WebP has none, and
/// what cannot be carried is listed in <see cref="ImageConversionResult.NotCarried"/>. Animations keep their first
/// frame only, and say so.
/// the same photo with another extension (exports, decision of 18 November 2026): PNG and WebP without further loss,
/// JPEG at the quality of the options. The pixels written are always 8-bit RGB(A): a source with more bits per
/// sample (16-bit PNG, 12-bit or lossless JPEG) loses that precision, which the result reports as lossy. The EXIF
/// block (dates, camera, the orientation tag) and the XMP packet are copied as they are, the ICC profile only when it
/// describes RGB data (a CMYK or grey profile would no longer fit the RGB pixels); the pixels stay as stored, not
/// turned by the orientation, which the copied tag still describes. Texts go where the target can hold them: PNG
/// text chunks, JPEG comments; WebP has none. The PNG colour chunks (gAMA, cHRM, sRGB) are not written. Whatever
/// cannot be carried is listed in <see cref="ImageConversionResult.NotCarried"/>. Animations keep their first frame
/// only, and say so.
/// </summary>
public static class ImageConverter
{
@@ -72,9 +78,34 @@ public static class ImageConverter
notCarried.Add($"l'animazione: resta solo il primo fotogramma di {info.FrameCount}");
}
var lossy = target == ImageFormat.Jpeg;
if (info.BitsPerSample > 8)
{
notCarried.Add($"la profondità di {info.BitsPerSample} bit per campione (i pixel sono scritti a 8 bit)");
lossy = true;
}
var metadata = info.Metadata;
var exif = metadata.Exif;
var icc = metadata.IccProfile;
var iccName = metadata.IccProfileName;
if (icc is not null && IccColorSpace(icc) != "RGB ")
{
notCarried.Add(IccColorSpace(icc) switch
{
"CMYK" => "il profilo colore CMYK (i pixel sono stati convertiti in RGB)",
"GRAY" => "il profilo colore in scala di grigi (i pixel sono scritti in RGB)",
_ => "il profilo colore (non descrive pixel RGB)",
});
icc = null;
iccName = null;
}
foreach (var chunk in metadata.ColorChunks)
{
notCarried.Add($"il blocco di colore {chunk} del PNG (non viene scritto)");
}
var xmp = metadata.Xmp;
var texts = metadata.Texts;
byte[] data;
@@ -85,7 +116,7 @@ public static class ImageConverter
{
Exif = exif,
IccProfile = icc,
IccProfileName = ValidIccName(metadata.IccProfileName) ?? PngEncoderOptions.Default.IccProfileName,
IccProfileName = ValidIccName(iccName) ?? PngEncoderOptions.Default.IccProfileName,
Xmp = xmp,
Texts = PngTexts(texts, notCarried),
});
@@ -147,11 +178,34 @@ public static class ImageConverter
Exif = exif,
Orientation = metadata.Orientation,
IccProfile = icc,
IccProfileName = metadata.IccProfileName,
IccProfileName = icc is null ? null : iccName,
Xmp = xmp,
Texts = target == ImageFormat.WebP ? [] : texts,
};
return new ImageConversionResult(data, info.Format, target, image.Width, image.Height, target == ImageFormat.Jpeg, written, notCarried);
return new ImageConversionResult(data, info.Format, target, image.Width, image.Height, lossy, written, notCarried);
}
/// <summary>The data colour space of an ICC profile (bytes 16-19 of its header: «RGB », «GRAY», «CMYK»...), or null.</summary>
internal static string? IccColorSpace(ReadOnlySpan<byte> profile)
{
if (profile.Length < 20)
{
return null;
}
Span<char> name = stackalloc char[4];
for (var i = 0; i < 4; i++)
{
var b = profile[16 + i];
if (b is < 32 or > 126)
{
return null;
}
name[i] = (char)b;
}
return new string(name);
}
/// <summary>Bytes of payload in one JPEG marker segment (65535 less the two of the length).</summary>