Files
Chronos/tests/Chronos.Data.Tests/Archiving/HeifFile.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout. The release cross-compiles the Windows programs, checks their
  icon, version and DPI manifest, packs installers and zips, publishes the
  Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only
  tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow) and
  pe-check.

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 11:34:48 +02:00

95 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Buffers.Binary;
using System.Text;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata;
namespace Chronos.Data.Tests.Archiving;
/// <summary>
/// HEIF files as phones and cameras write them, as far as a host without a HEVC decoder reads them (the server, BRIEF
/// §8.1): the primary image's size (<c>ispe</c>) and an Exif item with the capture time and, in IFD1, a small JPEG
/// thumbnail, the only pixels such a host finds in the file.
/// </summary>
internal static class HeifFile
{
/// <summary>A <paramref name="width"/> × <paramref name="height"/> HEIF captured at <paramref name="captured"/>, its Exif
/// thumbnail <paramref name="thumbnailWidth"/> × <paramref name="thumbnailHeight"/> (none when the width is 0: such a
/// host finds no pixels in it at all).</summary>
public static byte[] Create(DateTime captured, int width, int height, int thumbnailWidth = 160, int thumbnailHeight = 120)
{
var tiff = ExifWriter.BuildTiff(new FrameMetadata { Make = "TestCam", Model = "H-1", CapturedAt = captured });
if (thumbnailWidth > 0)
{
using var small = new ImageBuffer(thumbnailWidth, thumbnailHeight);
for (var c = 0; c < 3; c++) small.Plane(c).Fill(0.2f + c * 0.2f);
tiff = WithThumbnail(tiff, JpegEncoder.Encode(small, new JpegEncodeOptions { Quality = 80 }));
}
byte[] exif = [.. U32(6), .. "Exif\0\0"u8, .. tiff];
// The HEVC-coded primary image, which no managed decoder reads.
var coded = new byte[64];
byte[] Build(int mdat) =>
[
.. Box("ftyp", "heic"u8.ToArray(), U32(0), "mif1heic"u8.ToArray()),
.. FullBox("meta", 0,
FullBox("hdlr", 0, U32(0), "pict"u8.ToArray(), new byte[13]),
FullBox("pitm", 0, U16(1)),
FullBox("iinf", 0, U16(2), FullBox("infe", 2, U16(1), U16(0), "hvc1\0"u8.ToArray()), FullBox("infe", 2, U16(2), U16(0), "Exif\0"u8.ToArray())),
FullBox("iloc", 1, [0x44, 0x00], U16(2),
U16(1), U16(0), U16(0), U16(1), U32(mdat), U32(coded.Length),
U16(2), U16(0), U16(0), U16(1), U32(mdat + coded.Length), U32(exif.Length)),
Box("iprp", Box("ipco", FullBox("ispe", 0, U32(width), U32(height))), FullBox("ipma", 0, U32(1), U16(1), [1, 0x81]))),
];
var head = Build(0);
head = Build(head.Length + 8);
return [.. head, .. Box("mdat", coded, exif)];
}
/// <summary><paramref name="tiff"/> (a single IFD0 chain, as <see cref="ExifWriter.BuildTiff"/> writes it) with an IFD1 holding <paramref name="thumbnail"/>.</summary>
private static byte[] WithThumbnail(byte[] tiff, byte[] thumbnail)
{
const int Entries = 3;
var ifd1 = (tiff.Length + 1) & ~1;
var jpeg = ifd1 + 2 + 12 * Entries + 4;
var result = new byte[jpeg + thumbnail.Length];
tiff.CopyTo(result, 0);
var ifd0Entries = BinaryPrimitives.ReadUInt16LittleEndian(result.AsSpan(8));
BinaryPrimitives.WriteUInt32LittleEndian(result.AsSpan(8 + 2 + 12 * ifd0Entries), (uint)ifd1);
BinaryPrimitives.WriteUInt16LittleEndian(result.AsSpan(ifd1), Entries);
Entry(result, ifd1 + 2, 0x0103, 3, 6); // Compression: JPEG
Entry(result, ifd1 + 14, 0x0201, 4, (uint)jpeg); // JPEGInterchangeFormat
Entry(result, ifd1 + 26, 0x0202, 4, (uint)thumbnail.Length); // JPEGInterchangeFormatLength
thumbnail.CopyTo(result, jpeg);
return result;
}
/// <summary>A little-endian IFD entry with one value.</summary>
private static void Entry(byte[] data, int at, ushort tag, ushort type, uint value)
{
BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(at), tag);
BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(at + 2), type);
BinaryPrimitives.WriteUInt32LittleEndian(data.AsSpan(at + 4), 1);
BinaryPrimitives.WriteUInt32LittleEndian(data.AsSpan(at + 8), value);
}
private static byte[] Box(string type, params byte[][] payload) => [.. U32(8 + payload.Sum(p => p.Length)), .. Encoding.ASCII.GetBytes(type), .. payload.SelectMany(p => p)];
private static byte[] FullBox(string type, byte version, params byte[][] payload) => Box(type, [[version, 0, 0, 0], .. payload]);
private static byte[] U16(int value)
{
var bytes = new byte[2];
BinaryPrimitives.WriteUInt16BigEndian(bytes, (ushort)value);
return bytes;
}
private static byte[] U32(int value)
{
var bytes = new byte[4];
BinaryPrimitives.WriteUInt32BigEndian(bytes, (uint)value);
return bytes;
}
}