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,150 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Febe.Core.Codecs.Images;
|
||||
|
||||
/// <summary>
|
||||
/// The 8×8 discrete cosine transforms of T.81 (A.3.3), separable, in single precision. Each one-dimensional pass
|
||||
/// splits the eight points into an even half (a four-point transform on coefficients 0, 2, 4, 6) and an odd half
|
||||
/// (coefficients 1, 3, 5, 7), which is the textbook factorisation of the defining sums: accurate to well under one
|
||||
/// level, which is what the "accurate" integer IDCT of other decoders gives too.
|
||||
/// </summary>
|
||||
internal static class JpegDct
|
||||
{
|
||||
private const float C1 = 0.98078528040323044913f; // cos(1π/16)
|
||||
private const float C2 = 0.92387953251128675613f; // cos(2π/16)
|
||||
private const float C3 = 0.83146961230254523708f; // cos(3π/16)
|
||||
private const float C4 = 0.70710678118654752440f; // cos(4π/16)
|
||||
private const float C5 = 0.55557023301960222474f; // cos(5π/16)
|
||||
private const float C6 = 0.38268343236508977173f; // cos(6π/16)
|
||||
private const float C7 = 0.19509032201612826785f; // cos(7π/16)
|
||||
|
||||
/// <summary>
|
||||
/// Dequantizes a block (natural order) and writes its 8×8 samples to <paramref name="output"/>, row after row
|
||||
/// <paramref name="stride"/> bytes apart. With <paramref name="precision"/> 12 the samples are 12-bit and are
|
||||
/// reduced to 8 bits through <paramref name="to8Bit"/> (4096 entries); with 8 they are written as they are.
|
||||
/// </summary>
|
||||
public static void Inverse(ReadOnlySpan<short> block, ReadOnlySpan<ushort> quantization, Span<byte> output, int stride, int precision, ReadOnlySpan<byte> to8Bit)
|
||||
{
|
||||
Span<float> work = stackalloc float[64];
|
||||
for (var c = 0; c < 8; c++)
|
||||
{
|
||||
if (block[8 + c] == 0 && block[16 + c] == 0 && block[24 + c] == 0 && block[32 + c] == 0
|
||||
&& block[40 + c] == 0 && block[48 + c] == 0 && block[56 + c] == 0)
|
||||
{
|
||||
var dc = block[c] * quantization[c] * C4;
|
||||
for (var r = 0; r < 8; r++)
|
||||
{
|
||||
work[(r * 8) + c] = dc;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
float x0 = block[c] * quantization[c];
|
||||
float x1 = block[8 + c] * quantization[8 + c];
|
||||
float x2 = block[16 + c] * quantization[16 + c];
|
||||
float x3 = block[24 + c] * quantization[24 + c];
|
||||
float x4 = block[32 + c] * quantization[32 + c];
|
||||
float x5 = block[40 + c] * quantization[40 + c];
|
||||
float x6 = block[48 + c] * quantization[48 + c];
|
||||
float x7 = block[56 + c] * quantization[56 + c];
|
||||
Inverse1D(x0, x1, x2, x3, x4, x5, x6, x7, work, c, 8);
|
||||
}
|
||||
|
||||
// The column pass left every value doubled, the row pass doubles again: a quarter brings them back.
|
||||
var shift = precision == 12 ? 2048.5f : 128.5f;
|
||||
var max = precision == 12 ? 4095 : 255;
|
||||
Span<float> row = stackalloc float[8];
|
||||
for (var r = 0; r < 8; r++)
|
||||
{
|
||||
var w = work.Slice(r * 8, 8);
|
||||
Inverse1D(w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7], row, 0, 1);
|
||||
var o = output.Slice(r * stride, 8);
|
||||
for (var c = 0; c < 8; c++)
|
||||
{
|
||||
var value = (int)((row[c] * 0.25f) + shift);
|
||||
if ((uint)value > (uint)max)
|
||||
{
|
||||
value = value < 0 ? 0 : max;
|
||||
}
|
||||
|
||||
o[c] = precision == 12 ? to8Bit[value] : (byte)value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The forward transform of a block of level-shifted samples (natural order) into DCT coefficients, natural order,
|
||||
/// not yet quantized.
|
||||
/// </summary>
|
||||
public static void Forward(ReadOnlySpan<float> samples, Span<float> coefficients)
|
||||
{
|
||||
Span<float> work = stackalloc float[64];
|
||||
for (var r = 0; r < 8; r++)
|
||||
{
|
||||
var s = samples.Slice(r * 8, 8);
|
||||
Forward1D(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7], work, r * 8, 1);
|
||||
}
|
||||
|
||||
for (var c = 0; c < 8; c++)
|
||||
{
|
||||
Forward1D(work[c], work[8 + c], work[16 + c], work[24 + c], work[32 + c], work[40 + c], work[48 + c], work[56 + c], coefficients, c, 8);
|
||||
}
|
||||
|
||||
for (var i = 0; i < 64; i++)
|
||||
{
|
||||
coefficients[i] *= 0.25f;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Eight points of x[n] = Σ C(k) X[k] cos((2n+1)kπ/16), C(0) = 1/√2, C(k) = 1: twice the T.81 inverse.</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void Inverse1D(float x0, float x1, float x2, float x3, float x4, float x5, float x6, float x7, Span<float> output, int start, int step)
|
||||
{
|
||||
var t0 = C4 * (x0 + x4);
|
||||
var t1 = C4 * (x0 - x4);
|
||||
var t2 = (C2 * x2) + (C6 * x6);
|
||||
var t3 = (C6 * x2) - (C2 * x6);
|
||||
var e0 = t0 + t2;
|
||||
var e3 = t0 - t2;
|
||||
var e1 = t1 + t3;
|
||||
var e2 = t1 - t3;
|
||||
|
||||
var o0 = (C1 * x1) + (C3 * x3) + (C5 * x5) + (C7 * x7);
|
||||
var o1 = (C3 * x1) - (C7 * x3) - (C1 * x5) - (C5 * x7);
|
||||
var o2 = (C5 * x1) - (C1 * x3) + (C7 * x5) + (C3 * x7);
|
||||
var o3 = (C7 * x1) - (C5 * x3) + (C3 * x5) - (C1 * x7);
|
||||
|
||||
output[start] = e0 + o0;
|
||||
output[start + step] = e1 + o1;
|
||||
output[start + (2 * step)] = e2 + o2;
|
||||
output[start + (3 * step)] = e3 + o3;
|
||||
output[start + (4 * step)] = e3 - o3;
|
||||
output[start + (5 * step)] = e2 - o2;
|
||||
output[start + (6 * step)] = e1 - o1;
|
||||
output[start + (7 * step)] = e0 - o0;
|
||||
}
|
||||
|
||||
/// <summary>Eight points of X[k] = C(k) Σ x[n] cos((2n+1)kπ/16): twice the T.81 forward transform.</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void Forward1D(float x0, float x1, float x2, float x3, float x4, float x5, float x6, float x7, Span<float> output, int start, int step)
|
||||
{
|
||||
var s0 = x0 + x7;
|
||||
var s1 = x1 + x6;
|
||||
var s2 = x2 + x5;
|
||||
var s3 = x3 + x4;
|
||||
var d0 = x0 - x7;
|
||||
var d1 = x1 - x6;
|
||||
var d2 = x2 - x5;
|
||||
var d3 = x3 - x4;
|
||||
|
||||
output[start] = C4 * (s0 + s1 + s2 + s3);
|
||||
output[start + (4 * step)] = C4 * (s0 - s1 - s2 + s3);
|
||||
output[start + (2 * step)] = (C2 * (s0 - s3)) + (C6 * (s1 - s2));
|
||||
output[start + (6 * step)] = (C6 * (s0 - s3)) - (C2 * (s1 - s2));
|
||||
output[start + step] = (C1 * d0) + (C3 * d1) + (C5 * d2) + (C7 * d3);
|
||||
output[start + (3 * step)] = (C3 * d0) - (C7 * d1) - (C1 * d2) - (C5 * d3);
|
||||
output[start + (5 * step)] = (C5 * d0) - (C1 * d1) + (C7 * d2) + (C3 * d3);
|
||||
output[start + (7 * step)] = (C7 * d0) - (C5 * d1) + (C3 * d2) - (C1 * d3);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user