using System.Buffers.Binary;
using System.IO.Compression;
using Chronos.Core.Imaging;
namespace Chronos.Core.Windows.Tests.Wic;
/// Little-endian TIFFs of one strip, built tag by tag: what WIC and the managed reader are asked about.
internal static class TestTiffs
{
private const ushort Short = 3, Long = 4, Undefined = 7;
///
/// A TIFF of × pixels of -bit samples,
/// a pixel, in one strip holding as it is (already compressed or predicted).
///
public static byte[] Build(int width, int height, int spp, int bits, byte[] strip, int compression = 1, int photometric = 2, int sampleFormat = 1,
int predictor = 1, byte[]? icc = null)
{
var entries = new List<(ushort Tag, ushort Type, uint[] Values, byte[]? Bytes)>
{
(256, Long, [(uint)width], null),
(257, Long, [(uint)height], null),
(258, Short, [.. Enumerable.Repeat((uint)bits, spp)], null),
(259, Short, [(uint)compression], null),
(262, Short, [(uint)photometric], null),
(273, Long, [0], null),
(277, Short, [(uint)spp], null),
(278, Long, [(uint)height], null),
(279, Long, [(uint)strip.Length], null),
(284, Short, [1], null),
};
if (predictor != 1) entries.Add((317, Short, [(uint)predictor], null));
if (sampleFormat != 1) entries.Add((339, Short, [.. Enumerable.Repeat((uint)sampleFormat, spp)], null));
if (icc is not null) entries.Add((34675, Undefined, [], icc));
entries.Sort((a, b) => a.Tag.CompareTo(b.Tag));
const int ifd = 8;
var position = ifd + 2 + entries.Count * 12 + 4;
var outOfLine = new int[entries.Count];
for (var i = 0; i < entries.Count; i++)
{
var size = Size(entries[i]);
if (size <= 4) continue;
outOfLine[i] = position;
position += size + (size & 1);
}
var data = position;
var bytes = new byte[data + strip.Length];
"II*\0"u8.CopyTo(bytes);
BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(4), ifd);
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(ifd), (ushort)entries.Count);
for (var i = 0; i < entries.Count; i++)
{
var (tag, type, values, raw) = entries[i];
var at = ifd + 2 + 12 * i;
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(at), tag);
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(at + 2), type);
BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(at + 4), (uint)(raw?.Length ?? values.Length));
var target = outOfLine[i] > 0 ? outOfLine[i] : at + 8;
if (outOfLine[i] > 0) BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(at + 8), (uint)outOfLine[i]);
if (raw is not null)
{
raw.CopyTo(bytes, target);
continue;
}
for (var k = 0; k < values.Length; k++)
{
var value = tag == 273 ? (uint)data : values[k];
if (type == Short) BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(target + 2 * k), (ushort)value);
else BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(target + 4 * k), value);
}
}
strip.CopyTo(bytes, data);
return bytes;
}
private static int Size((ushort Tag, ushort Type, uint[] Values, byte[]? Bytes) entry) =>
entry.Bytes?.Length ?? (entry.Type == Short ? 2 : 4) * entry.Values.Length;
/// An 8-bit RGB TIFF of 's sRGB codes, uncompressed, with its strip labelled .
public static byte[] Rgb8(ImageBuffer image, int compression = 1) =>
Build(image.Width, image.Height, 3, 8, PixelConvert.ToInterleaved8(image, 3), compression);
///
/// A 32-bit float RGB TIFF of (linear light, as it is), uncompressed, or Deflate with the
/// floating-point predictor (3) as Photoshop writes its 32-bit files: byte planes most significant first, each byte less
/// the one a pixel before it.
///
public static byte[] FloatRgb(ImageBuffer image, byte[]? icc = null, bool predicted = false)
{
const int spp = 3;
var count = image.Width * spp;
var strip = new byte[image.PlaneLength * spp * 4];
var row = new byte[count * 4];
for (var y = 0; y < image.Height; y++)
{
for (var x = 0; x < image.Width; x++)
{
for (var c = 0; c < spp; c++)
{
var bits = BitConverter.SingleToUInt32Bits(image[c, x, y]);
var i = x * spp + c;
if (!predicted)
{
BinaryPrimitives.WriteUInt32LittleEndian(row.AsSpan(i * 4), bits);
continue;
}
for (var k = 0; k < 4; k++) row[k * count + i] = (byte)(bits >> (8 * (3 - k)));
}
}
if (predicted)
{
for (var i = row.Length - 1; i >= spp; i--) row[i] -= row[i - spp];
}
row.CopyTo(strip, y * row.Length);
}
return predicted
? Build(image.Width, image.Height, spp, 32, Deflate(strip), compression: 8, sampleFormat: 3, predictor: 3, icc: icc)
: Build(image.Width, image.Height, spp, 32, strip, sampleFormat: 3, icc: icc);
}
/// A 32-bit unsigned integer RGB TIFF of 's codes, scaled to 32 bits.
public static byte[] Uint32Rgb(ImageBuffer image)
{
var codes = PixelConvert.ToInterleaved8(image, 3);
var strip = new byte[codes.Length * 4];
for (var i = 0; i < codes.Length; i++) BinaryPrimitives.WriteUInt32LittleEndian(strip.AsSpan(i * 4), codes[i] * 16843009u);
return Build(image.Width, image.Height, 3, 32, strip);
}
private static byte[] Deflate(byte[] raw)
{
using var output = new MemoryStream();
using (var z = new ZLibStream(output, CompressionLevel.Optimal, leaveOpen: true)) z.Write(raw);
return output.ToArray();
}
}