namespace Febe.Core.Codecs.Images;
///
/// 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.
///
///
/// Strictness: a file whose entropy-coded data ends before the last MCU of a scan is damaged
/// (), 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 .
///
public static class JpegDecoder
{
/// Whether the bytes start like a JPEG file (SOI and the start of a marker).
public static bool IsJpeg(ReadOnlySpan data) => data.Length >= 3 && data[0] == 0xFF && data[1] == 0xD8 && data[2] == 0xFF;
/// Size and metadata of a JPEG, without decoding the pixels.
/// The file is damaged or not a JPEG.
public static ImageInfo Identify(ReadOnlyMemory data, ImageLimits? limits = null)
{
var file = new JpegFile(data, limits ?? ImageLimits.Default, JpegFile.Mode.Identify, readMetadata: true);
file.Run();
return file.ToInfo();
}
/// Decodes a JPEG to straight RGBA8.
/// The file is damaged, truncated or not a JPEG.
/// The file uses the hierarchical process.
/// The image is larger than allow.
public static RgbaImage Decode(ReadOnlyMemory 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);
}
/// Decodes a JPEG to straight RGBA8 and returns what returns as well.
public static RgbaImage Decode(ReadOnlyMemory 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;
}
///
/// Decodes a JPEG reduced by 2^ in each direction (0 full size, 1 half, 2 quarter,
/// 3 eighth; sizes rounded up), with what returns (the full size): each 8×8 block gives the
/// means of its squares of samples, which saves memory and, at an eighth, the whole inverse transform. Lossless
/// files are always decoded at full size: check the size of the result.
///
public static RgbaImage DecodeReduced(ReadOnlyMemory data, int reduction, out ImageInfo info, ImageLimits? limits = null, bool pooled = false)
{
ArgumentOutOfRangeException.ThrowIfNegative(reduction);
ArgumentOutOfRangeException.ThrowIfGreaterThan(reduction, 3);
using var file = new JpegFile(data, limits ?? ImageLimits.Default, JpegFile.Mode.Pixels, readMetadata: true) { ScaleShift = reduction };
file.Run();
var image = file.Render(pooled);
info = file.ToInfo();
return image;
}
///
/// 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).
///
internal static JpegCoefficients DecodeCoefficients(ReadOnlyMemory data, ImageLimits? limits = null)
{
var file = new JpegFile(data, limits ?? ImageLimits.Default, JpegFile.Mode.Coefficients, readMetadata: false);
file.Run();
return file.ToCoefficients();
}
}
/// The coefficients of one component, natural order inside each block, blocks row after row.
/// Component identifier.
/// Horizontal sampling factor.
/// Vertical sampling factor.
/// Blocks that cover the component's samples horizontally.
/// Blocks that cover the component's samples vertically.
/// Blocks per row in (the MCU grid, at least ).
/// The quantization table the component used, natural order.
/// 64 coefficients per block.
internal sealed record JpegComponentCoefficients(int Id, int H, int V, int BlocksWide, int BlocksHigh, int Stride, ushort[] Quantization, short[] Coefficients)
{
/// The DQT table the component refers to.
public int QuantizationTable { get; init; }
/// Rows of blocks in .
public int StoredRows => Coefficients.Length / 64 / Stride;
/// The 64 coefficients of block (, ).
public ReadOnlySpan Block(int x, int y) => Coefficients.AsSpan(((y * Stride) + x) * 64, 64);
}
/// What returns.
internal sealed record JpegCoefficients(
int FrameMarker,
int Precision,
int Width,
int Height,
int RestartInterval,
int AdobeTransform,
bool Jfif,
int ScanCount,
IReadOnlyList Components);