CI / Build and test (push) Failing after 3m1s
CI / PostgreSQL 16 integration (push) Skipped
CI / PostgreSQL 17 integration (push) Skipped
CI / Docker image (push) Skipped
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
Release / Build, test and publish (push) Successful in 15m31s
Release / Server image (push) Successful in 1m13s
Convert photos already in the archive to JPEG, TIFF-16 or Linear DNG, from the panel's Library and the desktop's Archive and Library. Each run asks Replace (same frames, originals to the trash, recoverable) or New sequence. A dry run lists what converts, what is skipped and why, size before and after, and free space; the job converts exactly the confirmed photos, resumes, and commits all or nothing per sequence. A conversion never changes what Chronos reads from a photo. Replaced originals are erased only on the archive's own word that their conversion is gone. Desktop TIFFs that WIC showed black or as noise now go to the managed reader; the Edit picker no longer spins a core. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.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();
|
||
}
|
||
}
|