using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using Titano.Core.Imaging;
namespace Titano.Core.Codecs.Jpeg;
/// Chroma subsampling written by .
public enum JpegSubsampling
{
Yuv444,
Yuv422,
Yuv420,
}
/// Options for .
public sealed record JpegEncodeOptions
{
public static JpegEncodeOptions Default { get; } = new();
/// IJG quality 1–100 (scales the Annex K tables).
public int Quality { get; init; } = 90;
public JpegSubsampling Subsampling { get; init; } = JpegSubsampling.Yuv420;
/// Two-pass encoding with Huffman tables built from the image's statistics (smaller files).
public bool OptimizeHuffman { get; init; }
/// Restart interval in MCUs (0 = none).
public int RestartInterval { get; init; }
/// APP1 payloads written after SOI, each including its identifier (e.g. "Exif\0\0" + TIFF, or the XMP
/// namespace + packet); at most 65533 bytes each. When present the JFIF APP0 segment is omitted (Exif layout).
public IReadOnlyList? App1Segments { get; init; }
/// Ordered dither when quantising linear float input to 8-bit sRGB.
public bool Dither { get; init; }
/// Worker limit for colour conversion and forward DCT (-1 = all cores).
public int MaxDegreeOfParallelism { get; init; } = -1;
}
///
/// Baseline JPEG encoder (8-bit, Huffman, 1 or 3 components). Colour conversion and float forward DCT run in
/// parallel per MCU row; entropy coding is sequential.
///
public static class JpegEncoder
{
private static readonly float[] BasisT = Transpose(JpegTables.Basis);
/// Encodes a linear-light buffer (1 channel → gray JPEG, 3/4 → YCbCr; alpha is dropped).
public static byte[] Encode(ImageBuffer image, JpegEncodeOptions? options = null)
{
using var ms = new MemoryStream();
Encode(ms, image, options);
return ms.ToArray();
}
public static void Encode(Stream output, ImageBuffer image, JpegEncodeOptions? options = null)
{
var o = options ?? JpegEncodeOptions.Default;
var channels = image.Channels == 1 ? 1 : 3;
EncodeCore(output, image.Width, image.Height, channels, (y, row) => PixelConvert.EncodeRow8(image, y, row, channels, TransferFunction.Srgb, o.Dither), o);
}
/// Encodes interleaved 8-bit sRGB pixels: 1 = gray, 3 = RGB, 4 = RGBA (alpha ignored).
public static byte[] Encode(ReadOnlySpan pixels, int width, int height, int channels, JpegEncodeOptions? options = null)
{
using var ms = new MemoryStream();
Encode(ms, pixels, width, height, channels, options);
return ms.ToArray();
}
public static unsafe void Encode(Stream output, ReadOnlySpan pixels, int width, int height, int channels, JpegEncodeOptions? options = null)
{
if (channels is not (1 or 3 or 4)) throw new ArgumentOutOfRangeException(nameof(channels));
if (pixels.Length < (long)width * height * channels) throw new ArgumentException("Pixel buffer too small", nameof(pixels));
var outChannels = channels == 1 ? 1 : 3;
fixed (byte* p = pixels)
{
var ptr = (nint)p;
EncodeCore(output, width, height, outChannels, (y, row) =>
{
var src = new ReadOnlySpan((byte*)ptr + (long)y * width * channels, width * channels);
if (channels == outChannels)
{
src.CopyTo(row);
return;
}
for (var x = 0; x < width; x++)
{
row[3 * x] = src[4 * x];
row[3 * x + 1] = src[4 * x + 1];
row[3 * x + 2] = src[4 * x + 2];
}
}, options ?? JpegEncodeOptions.Default);
}
}
/// IJG quality scaling of a base table (jcparam.c), clamped to baseline 1..255.
internal static int[] ScaleTable(ReadOnlySpan table, int quality)
{
quality = Math.Clamp(quality, 1, 100);
var scale = quality < 50 ? 5000 / quality : 200 - quality * 2;
var result = new int[64];
for (var i = 0; i < 64; i++) result[i] = Math.Clamp((table[i] * scale + 50) / 100, 1, 255);
return result;
}
private delegate void RowReader(int y, Span row);
private sealed class Plan
{
public required int Width, Height, Components, HY, VY, McusX, McusY;
public required int[][] Quant;
public required short[][] Coefficients;
public int BlocksPerLine(int c) => c == 0 ? McusX * HY : McusX;
}
private static void EncodeCore(Stream output, int width, int height, int components, RowReader read, JpegEncodeOptions o)
{
if (width is < 1 or > 65535 || height is < 1 or > 65535) throw new ArgumentOutOfRangeException(nameof(width), "JPEG dimensions are 1..65535");
if (o.RestartInterval is < 0 or > 65535) throw new ArgumentOutOfRangeException(nameof(o), "Restart interval is 0..65535");
var (hy, vy) = components == 1 ? (1, 1) : o.Subsampling switch
{
JpegSubsampling.Yuv444 => (1, 1),
JpegSubsampling.Yuv422 => (2, 1),
_ => (2, 2),
};
var mcusX = JpegParser.DivUp(width, 8 * hy);
var mcusY = JpegParser.DivUp(height, 8 * vy);
var plan = new Plan
{
Width = width, Height = height, Components = components, HY = hy, VY = vy, McusX = mcusX, McusY = mcusY,
Quant = [ScaleTable(JpegTables.LumaQuant, o.Quality), ScaleTable(JpegTables.ChromaQuant, o.Quality)],
Coefficients = new short[components][],
};
plan.Coefficients[0] = new short[checked(mcusX * hy * mcusY * vy * 64)];
for (var c = 1; c < components; c++) plan.Coefficients[c] = new short[checked(mcusX * mcusY * 64)];
Transform(plan, read, o.MaxDegreeOfParallelism);
JpegHuffmanCode dcY, acY, dcC, acC;
if (o.OptimizeHuffman)
{
var freq = new long[4][];
for (var i = 0; i < 4; i++) freq[i] = new long[256];
Entropy(plan, o.RestartInterval, null, freq, null);
dcY = JpegHuffmanCode.Optimal(freq[0]);
acY = JpegHuffmanCode.Optimal(freq[1]);
dcC = components > 1 ? JpegHuffmanCode.Optimal(freq[2]) : dcY;
acC = components > 1 ? JpegHuffmanCode.Optimal(freq[3]) : acY;
}
else
{
dcY = new JpegHuffmanCode(JpegTables.DcLumaBits, JpegTables.DcLumaValues);
acY = new JpegHuffmanCode(JpegTables.AcLumaBits, JpegTables.AcLumaValues);
dcC = new JpegHuffmanCode(JpegTables.DcChromaBits, JpegTables.DcChromaValues);
acC = new JpegHuffmanCode(JpegTables.AcChromaBits, JpegTables.AcChromaValues);
}
var writer = new JpegBitWriter((int)Math.Min(1 << 30, (long)width * height * components / 4));
Entropy(plan, o.RestartInterval, writer, null, [dcY, acY, dcC, acC]);
WriteHeaders(output, plan, o, dcY, acY, dcC, acC);
output.Write(writer.Written);
output.Write([0xFF, JpegTables.Eoi]);
}
/// Colour conversion, chroma downsampling (box average), forward DCT and quantisation per MCU row.
private static void Transform(Plan p, RowReader read, int parallelism)
{
int hy = p.HY, vy = p.VY, mcuH = 8 * vy, paddedW = p.McusX * 8 * hy, nc = p.Components;
var recip = new float[2][];
for (var t = 0; t < 2; t++)
{
recip[t] = new float[64];
for (var i = 0; i < 64; i++) recip[t][i] = 1f / p.Quant[t][i];
}
Parallel.For(0, p.McusY, new ParallelOptions { MaxDegreeOfParallelism = parallelism }, my =>
{
var rgb = new byte[p.Width * nc];
var planes = new float[nc][];
for (var c = 0; c < nc; c++) planes[c] = new float[paddedW * mcuH];
for (var r = 0; r < mcuH; r++)
{
var y = Math.Min(my * mcuH + r, p.Height - 1);
read(y, rgb);
var row = r * paddedW;
if (nc == 1)
{
var g = planes[0];
for (var x = 0; x < p.Width; x++) g[row + x] = rgb[x] - 128f;
}
else
{
float[] yp = planes[0], cb = planes[1], cr = planes[2];
for (var x = 0; x < p.Width; x++)
{
float rr = rgb[3 * x], gg = rgb[3 * x + 1], bb = rgb[3 * x + 2];
yp[row + x] = 0.299f * rr + 0.587f * gg + 0.114f * bb - 128f;
cb[row + x] = -0.168736f * rr - 0.331264f * gg + 0.5f * bb;
cr[row + x] = 0.5f * rr - 0.418688f * gg - 0.081312f * bb;
}
}
for (var c = 0; c < nc; c++)
{
var plane = planes[c];
plane.AsSpan(row + p.Width, paddedW - p.Width).Fill(plane[row + p.Width - 1]);
}
}
Span block = stackalloc float[64];
Span coef = stackalloc float[64];
var bpl = p.BlocksPerLine(0);
for (var by = 0; by < vy; by++)
{
for (var bx = 0; bx < bpl; bx++)
{
for (var r = 0; r < 8; r++) planes[0].AsSpan((by * 8 + r) * paddedW + bx * 8, 8).CopyTo(block.Slice(r * 8, 8));
ForwardDct(block, coef);
Quantize(coef, recip[0], p.Coefficients[0].AsSpan(((my * vy + by) * bpl + bx) * 64, 64));
}
}
var area = 1f / (hy * vy);
for (var c = 1; c < nc; c++)
{
var plane = planes[c];
for (var bx = 0; bx < p.McusX; bx++)
{
for (var r = 0; r < 8; r++)
{
for (var i = 0; i < 8; i++)
{
var sum = 0f;
var x0 = (bx * 8 + i) * hy;
for (var dy = 0; dy < vy; dy++)
{
var at = (r * vy + dy) * paddedW + x0;
for (var dx = 0; dx < hy; dx++) sum += plane[at + dx];
}
block[r * 8 + i] = sum * area;
}
}
ForwardDct(block, coef);
Quantize(coef, recip[1], p.Coefficients[c].AsSpan((my * p.McusX + bx) * 64, 64));
}
}
});
}
/// F = B·f·Bᵀ with the orthonormal basis (DC = 8 × mean), Vector256 rows when available.
private static void ForwardDct(ReadOnlySpan f, Span output)
{
var basis = JpegTables.Basis;
if (Vector256.IsHardwareAccelerated)
{
ref var bt = ref MemoryMarshal.GetArrayDataReference(BasisT);
Span> t = stackalloc Vector256[8];
for (var y = 0; y < 8; y++)
{
var acc = Vector256.Zero;
for (var x = 0; x < 8; x++) acc += Vector256.Create(f[y * 8 + x]) * Vector256.LoadUnsafe(ref bt, (nuint)(x * 8));
t[y] = acc;
}
for (var v = 0; v < 8; v++)
{
var acc = Vector256.Zero;
for (var y = 0; y < 8; y++) acc += Vector256.Create(basis[v * 8 + y]) * t[y];
acc.CopyTo(output.Slice(v * 8, 8));
}
return;
}
Span tmp = stackalloc float[64];
for (var y = 0; y < 8; y++)
{
for (var u = 0; u < 8; u++)
{
var s = 0f;
for (var x = 0; x < 8; x++) s += f[y * 8 + x] * basis[u * 8 + x];
tmp[y * 8 + u] = s;
}
}
for (var v = 0; v < 8; v++)
{
for (var u = 0; u < 8; u++)
{
var s = 0f;
for (var y = 0; y < 8; y++) s += basis[v * 8 + y] * tmp[y * 8 + u];
output[v * 8 + u] = s;
}
}
}
private static void Quantize(ReadOnlySpan coef, float[] recip, Span destination)
{
destination[0] = (short)Math.Clamp(MathF.Round(coef[0] * recip[0]), -1024f, 1023f);
for (var i = 1; i < 64; i++) destination[i] = (short)Math.Clamp(MathF.Round(coef[i] * recip[i]), -1023f, 1023f);
}
/// Walks every MCU in order: counts symbol frequencies () or emits codes.
private static void Entropy(Plan p, int restartInterval, JpegBitWriter? writer, long[][]? freq, JpegHuffmanCode[]? codes)
{
var preds = new int[p.Components];
var total = p.McusX * p.McusY;
var rst = 0;
for (var mcu = 0; mcu < total; mcu++)
{
if (restartInterval > 0 && mcu > 0 && mcu % restartInterval == 0)
{
Array.Clear(preds);
if (writer is not null)
{
writer.Flush();
writer.WriteMarker((byte)(JpegTables.Rst0 + rst));
}
rst = (rst + 1) & 7;
}
writer?.Reserve(4096);
int mx = mcu % p.McusX, my = mcu / p.McusX;
for (var c = 0; c < p.Components; c++)
{
int h = c == 0 ? p.HY : 1, v = c == 0 ? p.VY : 1, bpl = p.BlocksPerLine(c), table = c == 0 ? 0 : 2;
for (var by = 0; by < v; by++)
{
for (var bx = 0; bx < h; bx++)
{
var block = p.Coefficients[c].AsSpan(((my * v + by) * bpl + mx * h + bx) * 64, 64);
if (writer is null) CountBlock(block, ref preds[c], freq![table], freq[table + 1]);
else EncodeBlock(writer, block, ref preds[c], codes![table], codes[table + 1]);
}
}
}
}
writer?.Flush();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int Category(int v) => v == 0 ? 0 : 32 - BitOperations.LeadingZeroCount((uint)Math.Abs(v));
private static void CountBlock(ReadOnlySpan block, ref int pred, long[] dc, long[] ac)
{
dc[Category(block[0] - pred)]++;
pred = block[0];
var run = 0;
var order = JpegTables.NaturalOrder;
for (var k = 1; k < 64; k++)
{
int v = block[order[k]];
if (v == 0)
{
run++;
continue;
}
for (; run >= 16; run -= 16) ac[0xF0]++;
ac[run << 4 | Category(v)]++;
run = 0;
}
if (run > 0) ac[0]++;
}
private static void EncodeBlock(JpegBitWriter w, ReadOnlySpan block, ref int pred, JpegHuffmanCode dc, JpegHuffmanCode ac)
{
var diff = block[0] - pred;
pred = block[0];
var s = Category(diff);
w.Write((uint)(dc.Code[s] << s | ((diff < 0 ? diff - 1 : diff) & ((1 << s) - 1))), dc.Length[s] + s);
var run = 0;
var order = JpegTables.NaturalOrder;
for (var k = 1; k < 64; k++)
{
int v = block[order[k]];
if (v == 0)
{
run++;
continue;
}
for (; run >= 16; run -= 16) w.Write(ac.Code[0xF0], ac.Length[0xF0]);
s = Category(v);
var symbol = run << 4 | s;
w.Write((uint)(ac.Code[symbol] << s | ((v < 0 ? v - 1 : v) & ((1 << s) - 1))), ac.Length[symbol] + s);
run = 0;
}
if (run > 0) w.Write(ac.Code[0], ac.Length[0]);
}
private static void WriteHeaders(Stream output, Plan p, JpegEncodeOptions o, JpegHuffmanCode dcY, JpegHuffmanCode acY, JpegHuffmanCode dcC, JpegHuffmanCode acC)
{
var h = new MemoryStream();
h.Write([0xFF, JpegTables.Soi]);
if (o.App1Segments is { Count: > 0 } apps)
{
foreach (var app in apps)
{
if (app.Length > 65533) throw new ArgumentException("APP1 payload exceeds 65533 bytes", nameof(o));
Segment(h, JpegTables.App1, app);
}
}
else
{
Segment(h, JpegTables.App0, [(byte)'J', (byte)'F', (byte)'I', (byte)'F', 0, 1, 1, 0, 0, 1, 0, 1, 0, 0]);
}
var tables = p.Components == 1 ? 1 : 2;
var dqt = new byte[65 * tables];
for (var t = 0; t < tables; t++)
{
dqt[65 * t] = (byte)t;
for (var k = 0; k < 64; k++) dqt[65 * t + 1 + k] = (byte)p.Quant[t][JpegTables.NaturalOrder[k]];
}
Segment(h, JpegTables.Dqt, dqt);
var sof = new byte[6 + 3 * p.Components];
sof[0] = 8;
sof[1] = (byte)(p.Height >> 8);
sof[2] = (byte)p.Height;
sof[3] = (byte)(p.Width >> 8);
sof[4] = (byte)p.Width;
sof[5] = (byte)p.Components;
for (var c = 0; c < p.Components; c++)
{
sof[6 + 3 * c] = (byte)(c + 1);
sof[7 + 3 * c] = c == 0 ? (byte)(p.HY << 4 | p.VY) : (byte)0x11;
sof[8 + 3 * c] = (byte)(c == 0 ? 0 : 1);
}
Segment(h, JpegTables.Sof0, sof);
var dht = new MemoryStream();
Huffman(dht, 0x00, dcY);
Huffman(dht, 0x10, acY);
if (p.Components > 1)
{
Huffman(dht, 0x01, dcC);
Huffman(dht, 0x11, acC);
}
Segment(h, JpegTables.Dht, dht.ToArray());
if (o.RestartInterval > 0) Segment(h, JpegTables.Dri, [(byte)(o.RestartInterval >> 8), (byte)o.RestartInterval]);
var sos = new byte[4 + 2 * p.Components];
sos[0] = (byte)p.Components;
for (var c = 0; c < p.Components; c++)
{
sos[1 + 2 * c] = (byte)(c + 1);
sos[2 + 2 * c] = c == 0 ? (byte)0x00 : (byte)0x11;
}
sos[^3] = 0;
sos[^2] = 63;
sos[^1] = 0;
Segment(h, JpegTables.Sos, sos);
h.Position = 0;
h.CopyTo(output);
}
private static void Huffman(Stream s, byte classAndId, JpegHuffmanCode code)
{
s.WriteByte(classAndId);
s.Write(code.Bits);
s.Write(code.Values);
}
internal static void Segment(Stream s, byte marker, ReadOnlySpan payload)
{
var length = payload.Length + 2;
s.Write([0xFF, marker, (byte)(length >> 8), (byte)length]);
s.Write(payload);
}
private static float[] Transpose(float[] m)
{
var t = new float[64];
for (var i = 0; i < 8; i++)
{
for (var j = 0; j < 8; j++) t[j * 8 + i] = m[i * 8 + j];
}
return t;
}
}