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,95 @@
|
||||
namespace Febe.Core.Codecs.Images;
|
||||
|
||||
/// <summary>
|
||||
/// Reads JPEG files (ITU-T T.81): baseline and extended sequential DCT, progressive DCT (spectral selection and
|
||||
/// successive approximation), lossless, each with Huffman or arithmetic coding, 8 and 12 bits (lossless 2-16), any
|
||||
/// sampling factors, restart intervals and DNL. Grey, YCbCr (JFIF), RGB, and with an Adobe APP14 segment CMYK and
|
||||
/// YCCK, all converted to straight RGBA8 (alpha 255). The IDCT is accurate (single precision), chroma is upsampled
|
||||
/// with the triangular filter where the ratio is 2 and replicated otherwise. Metadata: EXIF (with its orientation,
|
||||
/// reported and not applied), the ICC profile from its APP2 chunks, XMP, comments.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Strictness: a file whose entropy-coded data ends before the last MCU of a scan is damaged
|
||||
/// (<see cref="ImageDecodeException"/>), as is a Huffman code the table does not have; a missing EOI after complete
|
||||
/// scans, bytes after EOI and stray restart markers are accepted. Hierarchical files (SOF5-7, SOF13-15) are refused
|
||||
/// with <see cref="ImageFormatNotSupportedException"/>.
|
||||
/// </remarks>
|
||||
public static class JpegDecoder
|
||||
{
|
||||
/// <summary>Whether the bytes start like a JPEG file (SOI and the start of a marker).</summary>
|
||||
public static bool IsJpeg(ReadOnlySpan<byte> data) => data.Length >= 3 && data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF;
|
||||
|
||||
/// <summary>Size and metadata of a JPEG, without decoding the pixels.</summary>
|
||||
/// <exception cref="ImageDecodeException">The file is damaged or not a JPEG.</exception>
|
||||
public static ImageInfo Identify(ReadOnlyMemory<byte> data, ImageLimits? limits = null)
|
||||
{
|
||||
var file = new JpegFile(data, limits ?? ImageLimits.Default, JpegFile.Mode.Identify, readMetadata: true);
|
||||
file.Run();
|
||||
return file.ToInfo();
|
||||
}
|
||||
|
||||
/// <summary>Decodes a JPEG to straight RGBA8.</summary>
|
||||
/// <exception cref="ImageDecodeException">The file is damaged, truncated or not a JPEG.</exception>
|
||||
/// <exception cref="ImageFormatNotSupportedException">The file uses the hierarchical process.</exception>
|
||||
/// <exception cref="ImageLimitException">The image is larger than <paramref name="limits"/> allow.</exception>
|
||||
public static RgbaImage Decode(ReadOnlyMemory<byte> data, ImageLimits? limits = null, bool pooled = false)
|
||||
{
|
||||
using var file = new JpegFile(data, limits ?? ImageLimits.Default, JpegFile.Mode.Pixels, readMetadata: false);
|
||||
file.Run();
|
||||
return file.Render(pooled);
|
||||
}
|
||||
|
||||
/// <summary>Decodes a JPEG to straight RGBA8 and returns what <see cref="Identify"/> returns as well.</summary>
|
||||
public static RgbaImage Decode(ReadOnlyMemory<byte> data, out ImageInfo info, ImageLimits? limits = null, bool pooled = false)
|
||||
{
|
||||
using var file = new JpegFile(data, limits ?? ImageLimits.Default, JpegFile.Mode.Pixels, readMetadata: true);
|
||||
file.Run();
|
||||
var image = file.Render(pooled);
|
||||
info = file.ToInfo();
|
||||
return image;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The quantized DCT coefficients of every block of a DCT-based JPEG, as they stand after the last scan, with the
|
||||
/// frame parameters: what tests compare between encodings of the same image (sequential, progressive, restart).
|
||||
/// </summary>
|
||||
internal static JpegCoefficients DecodeCoefficients(ReadOnlyMemory<byte> data, ImageLimits? limits = null)
|
||||
{
|
||||
var file = new JpegFile(data, limits ?? ImageLimits.Default, JpegFile.Mode.Coefficients, readMetadata: false);
|
||||
file.Run();
|
||||
return file.ToCoefficients();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The coefficients of one component, natural order inside each block, blocks row after row.</summary>
|
||||
/// <param name="Id">Component identifier.</param>
|
||||
/// <param name="H">Horizontal sampling factor.</param>
|
||||
/// <param name="V">Vertical sampling factor.</param>
|
||||
/// <param name="BlocksWide">Blocks that cover the component's samples horizontally.</param>
|
||||
/// <param name="BlocksHigh">Blocks that cover the component's samples vertically.</param>
|
||||
/// <param name="Stride">Blocks per row in <paramref name="Coefficients"/> (the MCU grid, at least <paramref name="BlocksWide"/>).</param>
|
||||
/// <param name="Quantization">The quantization table the component used, natural order.</param>
|
||||
/// <param name="Coefficients">64 coefficients per block.</param>
|
||||
internal sealed record JpegComponentCoefficients(int Id, int H, int V, int BlocksWide, int BlocksHigh, int Stride, ushort[] Quantization, short[] Coefficients)
|
||||
{
|
||||
/// <summary>The DQT table the component refers to.</summary>
|
||||
public int QuantizationTable { get; init; }
|
||||
|
||||
/// <summary>Rows of blocks in <see cref="Coefficients"/>.</summary>
|
||||
public int StoredRows => Coefficients.Length / 64 / Stride;
|
||||
|
||||
/// <summary>The 64 coefficients of block (<paramref name="x"/>, <paramref name="y"/>).</summary>
|
||||
public ReadOnlySpan<short> Block(int x, int y) => Coefficients.AsSpan(((y * Stride) + x) * 64, 64);
|
||||
}
|
||||
|
||||
/// <summary>What <see cref="JpegDecoder.DecodeCoefficients"/> returns.</summary>
|
||||
internal sealed record JpegCoefficients(
|
||||
int FrameMarker,
|
||||
int Precision,
|
||||
int Width,
|
||||
int Height,
|
||||
int RestartInterval,
|
||||
int AdobeTransform,
|
||||
bool Jfif,
|
||||
int ScanCount,
|
||||
IReadOnlyList<JpegComponentCoefficients> Components);
|
||||
Reference in new issue
Block a user