Owner review 2, phase 4b, fix round 3 (codecs). Chronos's decoders never read an ICC profile: they take code values through the sRGB curve and float samples as they are. Round 2 wrote a float TIFF's values with its profile's own curves, so after a Replace the renders, thumbnails, analyses and deflicker of an Adobe RGB, ProPhoto or gamma-1.0 float TIFF saw other pixels (up to 2.7 EV darker, shadows lifted). Every converted file now holds the values the decoders read from the original, as they read them back, and says truthfully what they are: - A JPEG or TIFF-16 keeps the codes of code values and carries the original's profile. Float values are written as sRGB codes with a profile made for them: the original's colorants and media white with the sRGB curve. A gray profile's codes are kept, its curve carried as an RGB profile (they were converted to sRGB before, which changed them too). - A Linear DNG, whose matrices Chronos develops it with as well, takes only values that are sRGB's as the decoders read them; any other profile is refused as color_profile instead of converted to colours Chronos would then show differently. - A profile with colorant tags that convert nothing is never sRGB by its name. - A float file with NaN or infinite samples fails as damaged data instead of being written black (and spending a DNG's headroom). The plan and the decoders behind it: - TooLarge and WorstBytes answer any size, int.MaxValue sides included, without a product that wraps around; the worst case saturates. - A frame another host flagged unreadable (the server, for a CMYK or CIELab TIFF its reader refuses) is asked again with a small decode where the conversion has other decoders (WIC on a PC), so a PC still converts it. The too_large sentences say "or Chronos reads back" (en, it). - WIC's TIFF decoder decodes an unknown compression as zeros, does not undo the floating-point predictor of Photoshop's 32-bit files and reads 32-bit integers as floats, all without an error: such TIFFs are left to the managed reader, on the desktop as in conversions, never black or noise. - A conversion's WIC probe reads the headers (decoder, first frame's oriented size, pixel format), so the size bound applies to what only WIC reads; a read error stays a read error (inaccessible), never a verdict. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
6.3 KiB
C#
138 lines
6.3 KiB
C#
using System.Buffers.Binary;
|
||
using System.IO.Compression;
|
||
using Chronos.Core.Imaging;
|
||
|
||
namespace Chronos.Core.Windows.Tests.Wic;
|
||
|
||
/// <summary>Little-endian TIFFs of one strip, built tag by tag: what WIC and the managed reader are asked about.</summary>
|
||
internal static class TestTiffs
|
||
{
|
||
private const ushort Short = 3, Long = 4, Undefined = 7;
|
||
|
||
/// <summary>
|
||
/// A TIFF of <paramref name="width"/> × <paramref name="height"/> pixels of <paramref name="bits"/>-bit samples,
|
||
/// <paramref name="spp"/> a pixel, in one strip holding <paramref name="strip"/> as it is (already compressed or predicted).
|
||
/// </summary>
|
||
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;
|
||
|
||
/// <summary>An 8-bit RGB TIFF of <paramref name="image"/>'s sRGB codes, uncompressed, with its strip labelled <paramref name="compression"/>.</summary>
|
||
public static byte[] Rgb8(ImageBuffer image, int compression = 1) =>
|
||
Build(image.Width, image.Height, 3, 8, PixelConvert.ToInterleaved8(image, 3), compression);
|
||
|
||
/// <summary>
|
||
/// A 32-bit float RGB TIFF of <paramref name="image"/> (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.
|
||
/// </summary>
|
||
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);
|
||
}
|
||
|
||
/// <summary>A 32-bit unsigned integer RGB TIFF of <paramref name="image"/>'s codes, scaled to 32 bits.</summary>
|
||
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();
|
||
}
|
||
}
|