chore: bootstrap repository foundation

Solution layout, build rules with zero-package guard, in-house test
framework, localization runtime, brand mark rasteriser with ICO/PNG
generation, Core contracts (imaging, video, domain model, TIFF reader),
Titano.Tools (make-icon, i18n, xaml-lint, package-guard) and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 08:59:40 +00:00
1 parent def3fadb6e
commit 83a54fb57b
109 files changed
+7523

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
using System.Buffers.Binary;
using Titano.Core.Codecs.Png;
namespace Titano.Core.Codecs.Ico;
/// <summary>
/// Writes Windows ICO files. Sizes ≥ 64 px are stored as PNG; smaller sizes as 32-bit DIBs with an AND
/// mask, which every shell version renders crisply.
/// </summary>
public static class IcoWriter
{
/// <param name="images">Straight-alpha RGBA8 square images keyed by size.</param>
public static byte[] Write(IReadOnlyList<(int Size, byte[] Rgba)> images)
{
var payloads = images.Select(i => i.Size >= 64
? PngEncoder.Encode(i.Rgba, i.Size, i.Size, PngColorType.Rgba)
: Dib(i.Rgba, i.Size)).ToList();
using var ms = new MemoryStream();
Span<byte> header = stackalloc byte[6];
BinaryPrimitives.WriteUInt16LittleEndian(header, 0);
BinaryPrimitives.WriteUInt16LittleEndian(header[2..], 1);
BinaryPrimitives.WriteUInt16LittleEndian(header[4..], (ushort)images.Count);
ms.Write(header);
var offset = 6 + 16 * images.Count;
Span<byte> entry = stackalloc byte[16];
for (var i = 0; i < images.Count; i++)
{
var size = images[i].Size;
entry.Clear();
entry[0] = (byte)(size >= 256 ? 0 : size);
entry[1] = (byte)(size >= 256 ? 0 : size);
BinaryPrimitives.WriteUInt16LittleEndian(entry[4..], 1);
BinaryPrimitives.WriteUInt16LittleEndian(entry[6..], 32);
BinaryPrimitives.WriteInt32LittleEndian(entry[8..], payloads[i].Length);
BinaryPrimitives.WriteInt32LittleEndian(entry[12..], offset);
ms.Write(entry);
offset += payloads[i].Length;
}
foreach (var p in payloads) ms.Write(p);
return ms.ToArray();
}
private static byte[] Dib(byte[] rgba, int size)
{
var maskStride = ((size + 31) / 32) * 4;
var data = new byte[40 + size * size * 4 + maskStride * size];
var span = data.AsSpan();
BinaryPrimitives.WriteInt32LittleEndian(span, 40);
BinaryPrimitives.WriteInt32LittleEndian(span[4..], size);
BinaryPrimitives.WriteInt32LittleEndian(span[8..], size * 2); // XOR + AND masks
BinaryPrimitives.WriteUInt16LittleEndian(span[12..], 1);
BinaryPrimitives.WriteUInt16LittleEndian(span[14..], 32);
BinaryPrimitives.WriteInt32LittleEndian(span[20..], size * size * 4);
var o = 40;
for (var y = size - 1; y >= 0; y--)
{
for (var x = 0; x < size; x++)
{
var s = (y * size + x) * 4;
data[o++] = rgba[s + 2];
data[o++] = rgba[s + 1];
data[o++] = rgba[s];
data[o++] = rgba[s + 3];
}
}
for (var y = size - 1; y >= 0; y--)
{
for (var x = 0; x < size; x++)
{
if (rgba[(y * size + x) * 4 + 3] == 0) data[o + (x >> 3)] |= (byte)(0x80 >> (x & 7));
}
o += maskStride;
}
return data;
}
}
+133
View File
@@ -0,0 +1,133 @@
using System.Buffers.Binary;
using System.IO.Compression;
using Titano.Core.Hashing;
namespace Titano.Core.Codecs.Png;
/// <summary>PNG colour types written by <see cref="PngEncoder"/>.</summary>
public enum PngColorType : byte
{
Gray = 0,
Rgb = 2,
GrayAlpha = 4,
Rgba = 6,
}
/// <summary>PNG encoder (8/16-bit, gray/RGB with or without alpha), adaptive filtering, zlib via the BCL.</summary>
public static class PngEncoder
{
private static ReadOnlySpan<byte> Signature => [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
/// <summary>Encodes interleaved pixels. 16-bit samples are big-endian in <paramref name="pixels"/>.</summary>
public static byte[] Encode(ReadOnlySpan<byte> pixels, int width, int height, PngColorType colorType, int bitDepth = 8, CompressionLevel level = CompressionLevel.SmallestSize)
{
using var ms = new MemoryStream();
Encode(ms, pixels, width, height, colorType, bitDepth, level);
return ms.ToArray();
}
public static void Encode(Stream output, ReadOnlySpan<byte> pixels, int width, int height, PngColorType colorType, int bitDepth = 8, CompressionLevel level = CompressionLevel.SmallestSize, IEnumerable<(string Type, byte[] Data)>? extraChunks = null)
{
if (bitDepth is not (8 or 16)) throw new ArgumentOutOfRangeException(nameof(bitDepth));
var channels = colorType switch
{
PngColorType.Gray => 1,
PngColorType.GrayAlpha => 2,
PngColorType.Rgb => 3,
_ => 4,
};
var bpp = channels * bitDepth / 8;
var stride = width * bpp;
if (pixels.Length < stride * height) throw new ArgumentException("Pixel buffer too small", nameof(pixels));
output.Write(Signature);
Span<byte> ihdr = stackalloc byte[13];
BinaryPrimitives.WriteInt32BigEndian(ihdr, width);
BinaryPrimitives.WriteInt32BigEndian(ihdr[4..], height);
ihdr[8] = (byte)bitDepth;
ihdr[9] = (byte)colorType;
ihdr[10] = 0;
ihdr[11] = 0;
ihdr[12] = 0;
WriteChunk(output, "IHDR", ihdr);
if (extraChunks is not null)
{
foreach (var (type, data) in extraChunks) WriteChunk(output, type, data);
}
WriteChunk(output, "IDAT", Compress(pixels, height, stride, bpp, level));
WriteChunk(output, "IEND", []);
}
/// <summary>Filters every row (adaptive, minimum sum of absolute differences) and deflates the result.</summary>
public static byte[] Compress(ReadOnlySpan<byte> pixels, int height, int stride, int bpp, CompressionLevel level)
{
using var ms = new MemoryStream();
using (var z = new ZLibStream(ms, level, leaveOpen: true))
{
var filtered = new byte[stride + 1];
var best = new byte[stride + 1];
for (var y = 0; y < height; y++)
{
var row = pixels.Slice(y * stride, stride);
var prev = y > 0 ? pixels.Slice((y - 1) * stride, stride) : default;
var bestScore = long.MaxValue;
for (byte f = 0; f <= 4; f++)
{
filtered[0] = f;
long score = 0;
for (var i = 0; i < stride; i++)
{
int a = i >= bpp ? row[i - bpp] : 0;
int b = y > 0 ? prev[i] : 0;
int c = i >= bpp && y > 0 ? prev[i - bpp] : 0;
var v = f switch
{
0 => row[i],
1 => row[i] - a,
2 => row[i] - b,
3 => row[i] - ((a + b) >> 1),
_ => row[i] - Paeth(a, b, c),
};
var bv = (byte)v;
filtered[i + 1] = bv;
score += bv < 128 ? bv : 256 - bv;
}
if (score < bestScore)
{
bestScore = score;
filtered.CopyTo(best, 0);
}
}
z.Write(best);
}
}
return ms.ToArray();
}
public static void WriteChunk(Stream output, string type, ReadOnlySpan<byte> data)
{
Span<byte> header = stackalloc byte[8];
BinaryPrimitives.WriteInt32BigEndian(header, data.Length);
for (var i = 0; i < 4; i++) header[4 + i] = (byte)type[i];
output.Write(header);
output.Write(data);
var crc = new Crc32();
crc.Append(header[4..]);
crc.Append(data);
Span<byte> trailer = stackalloc byte[4];
BinaryPrimitives.WriteUInt32BigEndian(trailer, crc.GetCurrentHashAsUInt32());
output.Write(trailer);
}
internal static int Paeth(int a, int b, int c)
{
var p = a + b - c;
var pa = Math.Abs(p - a);
var pb = Math.Abs(p - b);
var pc = Math.Abs(p - c);
if (pa <= pb && pa <= pc) return a;
return pb <= pc ? b : c;
}
}