namespace Titano.Core.Codecs.Jpeg;
///
/// Inverse DCT. Full-size blocks use libjpeg's accurate integer algorithm (jidctint ISLOW: Loeffler–Ligtenberg–
/// Moschytz with 13-bit constants), so output is bit-identical to libjpeg/djpeg. Reduced sizes (4, 2, 1) evaluate
/// the low-frequency n×n coefficients on an n-point grid in float (DCT-domain scaling for thumbnails).
///
internal static class JpegIdct
{
private const int ConstBits = 13;
private const int Fix0298631336 = 2446, Fix0390180644 = 3196, Fix0541196100 = 4433, Fix0765366865 = 6270;
private const int Fix0899976223 = 7373, Fix1175875602 = 9633, Fix1501321110 = 12299, Fix1847759065 = 15137;
private const int Fix1961570560 = 16069, Fix2053119869 = 16819, Fix2562915447 = 20995, Fix3072711026 = 25172;
private static readonly float[] Basis4 = JpegTables.BuildBasis(4);
private static readonly float[] Basis2 = JpegTables.BuildBasis(2);
/// Transforms one block into n·n samples, level-shifted and clamped to 0..2^precision−1.
/// 64 quantised coefficients, natural order.
/// 64 dequantisation factors, natural order.
/// Output block size: 8, 4, 2 or 1.
/// Sample precision (8 or 12).
public static void Transform(ReadOnlySpan coefficients, ReadOnlySpan quant, int size, int precision, Span output)
{
var max = (1 << precision) - 1;
var center = 1 << (precision - 1);
if (size == 8)
{
Islow(coefficients, quant, precision == 8 ? 2 : 1, center, max, output);
return;
}
if (size == 1)
{
output[0] = Math.Clamp((int)MathF.Round(coefficients[0] * quant[0] * 0.125f) + center, 0, max);
return;
}
Reduced(coefficients, quant, size, size == 4 ? Basis4 : Basis2, center, max, output);
}
/// Port of libjpeg-turbo jpeg_idct_islow (columns, then rows; PASS1_BITS 2 for 8-bit, 1 for 12-bit).
private static void Islow(ReadOnlySpan c, ReadOnlySpan q, int pass1Bits, int center, int max, Span output)
{
Span ws = stackalloc int[64];
_ = c[63];
_ = q[63];
for (var col = 0; col < 8; col++)
{
if (c[8 + col] == 0 && c[16 + col] == 0 && c[24 + col] == 0 && c[32 + col] == 0 &&
c[40 + col] == 0 && c[48 + col] == 0 && c[56 + col] == 0)
{
var dc = c[col] * q[col] << pass1Bits;
for (var r = 0; r < 8; r++) ws[r * 8 + col] = dc;
continue;
}
var shift = ConstBits - pass1Bits;
Butterfly(
c[col] * q[col], c[8 + col] * q[8 + col], c[16 + col] * q[16 + col], c[24 + col] * q[24 + col],
c[32 + col] * q[32 + col], c[40 + col] * q[40 + col], c[48 + col] * q[48 + col], c[56 + col] * q[56 + col],
out var o0, out var o1, out var o2, out var o3, out var o4, out var o5, out var o6, out var o7);
var round = 1 << (shift - 1);
ws[col] = (o0 + round) >> shift;
ws[8 + col] = (o1 + round) >> shift;
ws[16 + col] = (o2 + round) >> shift;
ws[24 + col] = (o3 + round) >> shift;
ws[32 + col] = (o4 + round) >> shift;
ws[40 + col] = (o5 + round) >> shift;
ws[48 + col] = (o6 + round) >> shift;
ws[56 + col] = (o7 + round) >> shift;
}
for (var row = 0; row < 8; row++)
{
var w = ws.Slice(row * 8, 8);
var o = output.Slice(row * 8, 8);
if (w[1] == 0 && w[2] == 0 && w[3] == 0 && w[4] == 0 && w[5] == 0 && w[6] == 0 && w[7] == 0)
{
var dcShift = pass1Bits + 3;
o.Fill(Math.Clamp(((w[0] + (1 << (dcShift - 1))) >> dcShift) + center, 0, max));
continue;
}
var shift = ConstBits + pass1Bits + 3;
var round = 1 << (shift - 1);
Butterfly(w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7],
out var o0, out var o1, out var o2, out var o3, out var o4, out var o5, out var o6, out var o7);
o[0] = Math.Clamp(((o0 + round) >> shift) + center, 0, max);
o[1] = Math.Clamp(((o1 + round) >> shift) + center, 0, max);
o[2] = Math.Clamp(((o2 + round) >> shift) + center, 0, max);
o[3] = Math.Clamp(((o3 + round) >> shift) + center, 0, max);
o[4] = Math.Clamp(((o4 + round) >> shift) + center, 0, max);
o[5] = Math.Clamp(((o5 + round) >> shift) + center, 0, max);
o[6] = Math.Clamp(((o6 + round) >> shift) + center, 0, max);
o[7] = Math.Clamp(((o7 + round) >> shift) + center, 0, max);
}
}
/// One 8-point ISLOW pass; outputs are scaled by 2^13 (CONST_BITS) before descaling.
private static void Butterfly(int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7,
out int o0, out int o1, out int o2, out int o3, out int o4, out int o5, out int o6, out int o7)
{
// Even part: the rotator is sqrt(2)·c(−6).
var z1 = (i2 + i6) * Fix0541196100;
var tmp2 = z1 - i6 * Fix1847759065;
var tmp3 = z1 + i2 * Fix0765366865;
var tmp0 = (i0 + i4) << ConstBits;
var tmp1 = (i0 - i4) << ConstBits;
int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
// Odd part (figure 8 of the LL&M paper).
tmp0 = i7;
tmp1 = i5;
tmp2 = i3;
tmp3 = i1;
z1 = tmp0 + tmp3;
var z2 = tmp1 + tmp2;
var z3 = tmp0 + tmp2;
var z4 = tmp1 + tmp3;
var z5 = (z3 + z4) * Fix1175875602;
tmp0 *= Fix0298631336;
tmp1 *= Fix2053119869;
tmp2 *= Fix3072711026;
tmp3 *= Fix1501321110;
z1 *= -Fix0899976223;
z2 *= -Fix2562915447;
z3 = z3 * -Fix1961570560 + z5;
z4 = z4 * -Fix0390180644 + z5;
tmp0 += z1 + z3;
tmp1 += z2 + z4;
tmp2 += z2 + z3;
tmp3 += z1 + z4;
o0 = tmp10 + tmp3;
o7 = tmp10 - tmp3;
o1 = tmp11 + tmp2;
o6 = tmp11 - tmp2;
o2 = tmp12 + tmp1;
o5 = tmp12 - tmp1;
o3 = tmp13 + tmp0;
o4 = tmp13 - tmp0;
}
/// n-point IDCT of the low-frequency n×n coefficients, evaluated at the centres of n output samples.
private static void Reduced(ReadOnlySpan coefficients, ReadOnlySpan quant, int n, float[] basis, int center, int max, Span output)
{
Span g = stackalloc float[n * n];
Span f = stackalloc float[n];
g.Clear();
for (var v = 0; v < n; v++)
{
var row = g.Slice(v * n, n);
for (var u = 0; u < n; u++)
{
int coef = coefficients[v * 8 + u];
if (coef == 0) continue;
var value = (float)(coef * quant[v * 8 + u]);
for (var x = 0; x < n; x++) row[x] += value * basis[u * n + x];
}
}
for (var y = 0; y < n; y++)
{
f.Clear();
for (var v = 0; v < n; v++)
{
var w = basis[v * n + y];
var row = g.Slice(v * n, n);
for (var x = 0; x < n; x++) f[x] += w * row[x];
}
for (var x = 0; x < n; x++) output[y * n + x] = Math.Clamp((int)MathF.Round(f[x]) + center, 0, max);
}
}
}