Lettore e scrittore JPEG di casa (C1, passo 3)
Febe deve leggere e scrivere JPEG senza SkiaSharp né altri componenti esterni (decisione del 18 novembre 2026), quindi il codec è scritto dalla specifica ITU-T T.81. Lettore (JpegDecoder): sequenziale di base ed esteso, progressivo (selezione spettrale e approssimazioni successive), senza perdita (predittori 1-7), ognuno con Huffman e con codifica aritmetica; 8 e 12 bit (senza perdita da 2 a 16); qualunque campionamento, ripristino, DNL; grigio, YCbCr, RGB, CMYK e YCCK con il segmento Adobe. IDCT esatta e crominanza con il filtro triangolare. EXIF, ICC a pezzi, XMP e commenti; l'orientamento EXIF va in ImageMetadata.Orientation (anche per PNG) e non si applica ai pixel. I file gerarchici e quelli a 2 componenti si rifiutano come formato non letto; i dati troncati dentro una scansione sono un file danneggiato. Scrittore (JpegEncoder): sequenziale di base, qualità 1-100 sulle tabelle dell'allegato K, 4:2:0, 4:4:4 e grigio, intervallo di ripristino, EXIF, ICC, XMP e commenti. Prove: contro le letture di SkiaSharp i JPEG dell'insieme di riferimento stanno fra 59 e 67 dB di PSNR per canale, scarto massimo 3 (soglia 40 dB e 0,1% di pixel oltre 16). Un codificatore di prova scrive gli stessi coefficienti in progressivo, aritmetico, 12 bit, con ripristino, scansioni separate e DNL, e il lettore deve ridarli identici; senza perdita si confronta campione per campione. Più andata e ritorno dello scrittore, file danneggiati e mutazioni casuali con seme fisso. La facciata ImageDecoder ora legge i JPEG. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8bf76f18ff
commit
36196526bb
25 files changed
+6411
-14
No files matched your search
@@ -0,0 +1,106 @@
|
||||
using System.Globalization;
|
||||
using Febe.Core.Codecs.Images;
|
||||
|
||||
namespace Febe.Tests.Imaging;
|
||||
|
||||
/// <summary>How close a lossy decode is to its reference: PSNR per channel and the share of pixels far off.</summary>
|
||||
/// <param name="PsnrRed">PSNR of the red channel, dB (infinity when identical).</param>
|
||||
/// <param name="PsnrGreen">PSNR of the green channel, dB.</param>
|
||||
/// <param name="PsnrBlue">PSNR of the blue channel, dB.</param>
|
||||
/// <param name="PsnrAlpha">PSNR of the alpha channel, dB.</param>
|
||||
/// <param name="FarOff">Share (0-1) of pixels with some channel off by more than <see cref="ImageQuality.FarThreshold"/>.</param>
|
||||
/// <param name="MaxDifference">Largest difference of any channel.</param>
|
||||
internal sealed record ImageQualityResult(double PsnrRed, double PsnrGreen, double PsnrBlue, double PsnrAlpha, double FarOff, int MaxDifference)
|
||||
{
|
||||
/// <summary>The smallest PSNR of the colour channels.</summary>
|
||||
public double MinPsnr => Math.Min(PsnrRed, Math.Min(PsnrGreen, PsnrBlue));
|
||||
|
||||
public override string ToString() => string.Create(
|
||||
CultureInfo.InvariantCulture,
|
||||
$"PSNR R {PsnrRed:F2} G {PsnrGreen:F2} B {PsnrBlue:F2} A {PsnrAlpha:F2} dB, oltre {ImageQuality.FarThreshold}: {FarOff * 100:F3}%, scarto massimo {MaxDifference}");
|
||||
}
|
||||
|
||||
/// <summary>Measures and orientation helpers for comparing decoded images.</summary>
|
||||
internal static class ImageQuality
|
||||
{
|
||||
/// <summary>A pixel counts as far off when one of its channels differs by more than this.</summary>
|
||||
public const int FarThreshold = 16;
|
||||
|
||||
/// <summary>Lossy decodes must reach this PSNR on every colour channel (SPECIFICA §14.1, proposal of C1).</summary>
|
||||
public const double MinimumPsnr = 40;
|
||||
|
||||
/// <summary>Lossy decodes may have at most this share of pixels far off.</summary>
|
||||
public const double MaximumFarOff = 0.001;
|
||||
|
||||
public static ImageQualityResult Compare(RgbaImage expected, RgbaImage actual)
|
||||
{
|
||||
Assert.AreEqual((expected.Width, expected.Height), (actual.Width, actual.Height), "misura diversa");
|
||||
var e = expected.Pixels;
|
||||
var a = actual.Pixels;
|
||||
Span<double> squares = stackalloc double[4];
|
||||
var far = 0;
|
||||
var max = 0;
|
||||
for (var i = 0; i < e.Length; i += 4)
|
||||
{
|
||||
var isFar = false;
|
||||
for (var ch = 0; ch < 4; ch++)
|
||||
{
|
||||
var d = Math.Abs(e[i + ch] - a[i + ch]);
|
||||
squares[ch] += d * d;
|
||||
max = Math.Max(max, d);
|
||||
isFar |= d > FarThreshold;
|
||||
}
|
||||
|
||||
if (isFar)
|
||||
{
|
||||
far++;
|
||||
}
|
||||
}
|
||||
|
||||
var pixels = e.Length / 4;
|
||||
return new ImageQualityResult(Psnr(squares[0], pixels), Psnr(squares[1], pixels), Psnr(squares[2], pixels), Psnr(squares[3], pixels), (double)far / pixels, max);
|
||||
}
|
||||
|
||||
/// <summary>Asserts the thresholds of a lossy decode.</summary>
|
||||
public static void AssertClose(ImageQualityResult result, string what)
|
||||
{
|
||||
Assert.IsGreaterThanOrEqualTo(MinimumPsnr, result.MinPsnr, $"{what}: {result}");
|
||||
Assert.IsLessThanOrEqualTo(MaximumFarOff, result.FarOff, $"{what}: {result}");
|
||||
}
|
||||
|
||||
private static double Psnr(double squares, int pixels) =>
|
||||
squares == 0 ? double.PositiveInfinity : 10 * Math.Log10(255.0 * 255.0 / (squares / pixels));
|
||||
|
||||
/// <summary>
|
||||
/// The image as it should be shown for EXIF orientation <paramref name="orientation"/> (1-8): mirrored, rotated
|
||||
/// or transposed from the stored pixels.
|
||||
/// </summary>
|
||||
public static RgbaImage Orient(RgbaImage stored, int orientation)
|
||||
{
|
||||
var w = stored.Width;
|
||||
var h = stored.Height;
|
||||
var swap = orientation >= 5;
|
||||
var result = new RgbaImage(swap ? h : w, swap ? w : h);
|
||||
for (var dy = 0; dy < result.Height; dy++)
|
||||
{
|
||||
var row = result.GetRow(dy);
|
||||
for (var dx = 0; dx < result.Width; dx++)
|
||||
{
|
||||
var (sx, sy) = orientation switch
|
||||
{
|
||||
2 => (w - 1 - dx, dy),
|
||||
3 => (w - 1 - dx, h - 1 - dy),
|
||||
4 => (dx, h - 1 - dy),
|
||||
5 => (dy, dx),
|
||||
6 => (dy, h - 1 - dx),
|
||||
7 => (w - 1 - dy, h - 1 - dx),
|
||||
8 => (w - 1 - dy, dx),
|
||||
_ => (dx, dy),
|
||||
};
|
||||
stored.GetRow(sy).Slice(sx * 4, 4).CopyTo(row[(dx * 4)..]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user