using System.Buffers.Binary;
using Titano.Core.Imaging;
using Titano.Core.Metadata;
using Titano.Core.Metadata.Tiff;
using Titano.Core.Tests.Metadata.Builders;
using Titano.Testing;
using static Titano.Core.Tests.Metadata.MetadataTestHelpers;
namespace Titano.Core.Tests.Metadata;
/// RAF, WebP, PNG and the minimal formats (GIF, BMP, JPEG XL, JPEG XR).
public sealed class OtherFormatTests
{
[Test]
public void Raf()
{
var m = Read(Fixtures.Raf(), "DSCF0001.RAF");
AssertNoFault(m);
Assert.Empty(m.Warnings);
Assert.Equal(ImageFileFormat.Raf, m.Format);
Assert.Equal("FUJIFILM", m.Make);
Assert.Equal("X-T5", m.Model);
Assert.Equal((7752, 5178), (m.Width, m.Height));
Assert.Equal(14, m.BitsPerSample);
AssertPreviews(m, (1920, 1280, PreviewKind.Preview), (160, 120, PreviewKind.Thumbnail));
Assert.Equal("FF02B1234567", m.BodySerial);
Assert.Equal(6100, m.ColorTemperature);
Assert.Equal(true, m.ImageStabilization);
Assert.Equal(Fixtures.Captured, m.CapturedAt);
}
[Test]
public void RafHeaderModelIsTheFallback()
{
var raf = Fixtures.Raf();
BinaryPrimitives.WriteUInt32BigEndian(raf.AsSpan(0x54), 0);
var m = Read(raf);
Assert.Equal("FUJIFILM", m.Make);
Assert.Equal("X-T5", m.Model);
Assert.Equal((7752, 5178), (m.Width, m.Height));
Assert.Contains("RAF embedded JPEG missing", m.Warnings);
}
[Test]
public void WebPWithVp8xExifAndXmp()
{
var m = Read(Fixtures.WebP(), "a.webp");
AssertNoFault(m);
Assert.Empty(m.Warnings);
Assert.Equal(ImageFileFormat.WebP, m.Format);
Assert.Equal((3000, 2000), (m.Width, m.Height));
Assert.Equal(8, m.BitsPerSample);
Assert.Equal(Fixtures.Make, m.Make);
Assert.Equal(Fixtures.Captured, m.CapturedAt);
Assert.Equal(4, m.Rating);
Assert.Equal(6, m.Orientation);
}
[Test]
public void WebPSimpleFormats()
{
Assert.Equal((640, 480), Size(Read(Fixtures.WebPLossy(640, 480))));
var body = (byte[])[.. "WEBP"u8, .. Box.Riff("VP8L", Fixtures.Vp8L(400, 300))];
Assert.Equal((400, 300), Size(Read([.. "RIFF"u8, .. LittleEndian(body.Length), .. body])));
}
[Test]
public void WebPExifWithPrefix()
{
var body = (byte[])[.. "WEBP"u8, .. Box.Riff("VP8L", Fixtures.Vp8L(8, 8)), .. Box.Riff("EXIF", [.. "Exif\0\0"u8, .. Fixtures.ExifTiff()])];
Assert.Equal(Fixtures.Model, Read([.. "RIFF"u8, .. LittleEndian(body.Length), .. body]).Model);
}
[Theory]
[Case(false)]
[Case(true)]
public void Png(bool compressedXmp)
{
var m = Read(Fixtures.Png(compressedXmp), "a.png");
AssertNoFault(m);
Assert.Empty(m.Warnings);
Assert.Equal(ImageFileFormat.Png, m.Format);
Assert.Equal((2048, 1536), (m.Width, m.Height));
Assert.Equal(16, m.BitsPerSample);
Assert.Equal(Fixtures.Model, m.Model);
Assert.Equal(400, m.Iso);
Assert.Equal("Red", m.Label);
}
[Test]
public void Gif() => Assert.Equal((320, 200), Size(Read([.. "GIF89a"u8, 0x40, 0x01, 0xC8, 0x00, 0, 0, 0])));
[Theory]
[Case(40, 640, -480)]
[Case(124, 1024, 768)]
public void Bmp(int headerSize, int width, int height)
{
var bmp = new byte[54];
"BM"u8.CopyTo(bmp);
BinaryPrimitives.WriteInt32LittleEndian(bmp.AsSpan(14), headerSize);
BinaryPrimitives.WriteInt32LittleEndian(bmp.AsSpan(18), width);
BinaryPrimitives.WriteInt32LittleEndian(bmp.AsSpan(22), height);
Assert.Equal((width, Math.Abs(height)), Size(Read(bmp)));
}
[Test]
public void BmpCoreHeader()
{
var bmp = new byte[26];
"BM"u8.CopyTo(bmp);
bmp[14] = 12;
BinaryPrimitives.WriteUInt16LittleEndian(bmp.AsSpan(18), 300);
BinaryPrimitives.WriteUInt16LittleEndian(bmp.AsSpan(20), 200);
Assert.Equal((300, 200), Size(Read(bmp)));
}
[Theory]
[Case(256, 128)]
[Case(1000, 750, Name = "4:3 ratio")]
[Case(1921, 1081)]
[Case(300000, 270000, Name = "two 30-bit sizes (68-bit header)")]
public void JpegXlCodestream(int width, int height)
{
var m = Read(BuildJpegXlCodestream(width, height), "a.jxl");
Assert.Equal(ImageFileFormat.JpegXl, m.Format);
Assert.Equal((width, height), Size(m));
}
[Test]
public void JpegXlContainer()
{
byte[] file = [.. Box.Make("JXL ", [0x0D, 0x0A, 0x87, 0x0A]), .. Box.Make("ftyp", Box.Ascii("jxl "), Box.U32(0), Box.Ascii("jxl ")),
.. Box.Make("Exif", Box.U32(0), Fixtures.ExifTiff()), .. Box.Make("jxlp", Box.U32(0), BuildJpegXlCodestream(512, 384))];
var m = Read(file);
Assert.Equal(ImageFileFormat.JpegXl, m.Format);
Assert.Equal((512, 384), Size(m));
Assert.Equal(Fixtures.Make, m.Make);
}
[Test]
public void JpegXr()
{
var ifd = new IfdSpec().Ascii(TiffTags.Make, "Microsoft").Long(0xBC80, 1600).Long(0xBC81, 900);
var m = Read(TiffFile.Build(ifd, magic: "II¼\u0001"), "a.jxr");
Assert.Equal(ImageFileFormat.JpegXr, m.Format);
Assert.Equal((1600, 900), Size(m));
Assert.Equal("Microsoft", m.Make);
}
[Test]
public void UnknownFilesYieldAWarning()
{
var m = Read("hello world, not an image"u8.ToArray(), "notes.jpg");
Assert.Equal(ImageFileFormat.Unknown, m.Format);
Assert.Contains("Unrecognised file signature", m.Warnings);
}
private static (int, int) Size(FrameMetadata m) => (m.Width, m.Height);
private static byte[] LittleEndian(int v)
{
var b = new byte[4];
BinaryPrimitives.WriteInt32LittleEndian(b, v);
return b;
}
/// FF 0A + SizeHeader (ISO/IEC 18181-1): small multiples of 8 or U32 distributions; a 4:3 ratio when exact.
private static byte[] BuildJpegXlCodestream(int width, int height)
{
var bits = new List();
void Put(ulong value, int count)
{
for (var i = 0; i < count; i++) bits.Add(((value >> i) & 1) != 0);
}
void PutU32(int value)
{
var v = (ulong)(value - 1);
var (selector, n) = v < 1 << 9 ? (0, 9) : v < 1 << 13 ? (1, 13) : v < 1 << 18 ? (2, 18) : (3, 30);
Put((ulong)selector, 2);
Put(v, n);
}
var small = width % 8 == 0 && height % 8 == 0 && width <= 256 && height <= 256;
Put(small ? 1UL : 0UL, 1);
if (small) Put((ulong)(height / 8 - 1), 5);
else PutU32(height);
var ratio = height * 4 / 3 == width && height % 3 == 0 ? 3 : 0;
Put((ulong)ratio, 3);
if (ratio == 0)
{
if (small) Put((ulong)(width / 8 - 1), 5);
else PutU32(width);
}
var bytes = new byte[2 + (bits.Count + 7) / 8 + 8];
bytes[0] = 0xFF;
bytes[1] = 0x0A;
for (var i = 0; i < bits.Count; i++)
{
if (bits[i]) bytes[2 + i / 8] |= (byte)(1 << (i % 8));
}
return bytes;
}
}