using System.Buffers.Binary; using Febe.Core.Codecs.Images; using Febe.Core.Drawing; namespace Febe.Desktop.Core; /// /// The brand mark of appendix C — a rounded square in the accent colour with a ring and a dot, a body and its moon — /// drawn with Febe's own rasteriser () at any size, the same way the SVG describes it: /// /// <rect width="32" height="32" rx="8" fill="#0F5C8C"/> /// <circle cx="14.5" cy="17.5" r="7.4" fill="none" stroke="#FFFFFF" stroke-width="2.6"/> /// <circle cx="23.2" cy="8.8" r="3.1" fill="#FFFFFF"/> /// /// public static class BrandMark { /// The sizes of the Windows icon: window, notification area, taskbar, Explorer, installer. public static IReadOnlyList IconSizes { get; } = [16, 24, 32, 48, 64, 128, 256]; private static readonly Color Accent = Color.FromRgb(0x0F5C8C); /// The mark as a square image of pixels, straight RGBA, transparent corners. public static RgbaImage Render(int size) { ArgumentOutOfRangeException.ThrowIfLessThan(size, 1); ArgumentOutOfRangeException.ThrowIfGreaterThan(size, 1024); var image = new RgbaImage(size, size); using var canvas = new Canvas(image); canvas.Scale(size / 32f); canvas.FillRoundRect(0, 0, 32, 32, 8, Accent); canvas.StrokeCircle(14.5f, 17.5f, 7.4f, 2.6f, Color.White); canvas.FillCircle(23.2f, 8.8f, 3.1f, Color.White); return image; } /// The multi-size Windows icon () of the mark, as the bytes of an .ico file. public static byte[] RenderIcon() { var images = new List(); foreach (var size in IconSizes) { images.Add(Render(size)); } return IcoWriter.Write(images); } } /// /// Writes Windows .ico files by hand (no package for it, spec §3): the 256-pixel image as PNG, the smaller ones as /// 32-bit DIBs (BGRA with alpha, bottom-up, plus the 1-bit AND mask), which every part of Windows reads — window icons, /// the notification area, Explorer, NSIS. /// public static class IcoWriter { /// Images at or above this size are stored as PNG (the Vista format); smaller ones as DIB. public const int PngFrom = 256; private const int DirectoryHeader = 6; private const int DirectoryEntry = 16; private const int BitmapInfoHeader = 40; /// The .ico bytes of square (1 to 256 pixels), in the order given. public static byte[] Write(IReadOnlyList images) { ArgumentNullException.ThrowIfNull(images); if (images.Count is 0 or > ushort.MaxValue) { throw new ArgumentException("Un'icona contiene da 1 a 65535 immagini.", nameof(images)); } var payloads = new List(images.Count); foreach (var image in images) { if (image.Width != image.Height || image.Width is < 1 or > 256) { throw new ArgumentException("Le immagini di un'icona sono quadrate, da 1 a 256 pixel.", nameof(images)); } payloads.Add(image.Width >= PngFrom ? Png(image) : Dib(image)); } using var output = new MemoryStream(); Span header = stackalloc byte[DirectoryHeader]; BinaryPrimitives.WriteUInt16LittleEndian(header, 0); // reserved BinaryPrimitives.WriteUInt16LittleEndian(header[2..], 1); // 1 = icon BinaryPrimitives.WriteUInt16LittleEndian(header[4..], (ushort)images.Count); output.Write(header); var offset = DirectoryHeader + (DirectoryEntry * images.Count); Span entry = stackalloc byte[DirectoryEntry]; for (var i = 0; i < images.Count; i++) { var size = images[i].Width; entry.Clear(); entry[0] = (byte)(size >= 256 ? 0 : size); // 0 means 256 entry[1] = (byte)(size >= 256 ? 0 : size); entry[2] = 0; // no palette entry[3] = 0; // reserved BinaryPrimitives.WriteUInt16LittleEndian(entry[4..], 1); // colour planes BinaryPrimitives.WriteUInt16LittleEndian(entry[6..], 32); // bits per pixel BinaryPrimitives.WriteUInt32LittleEndian(entry[8..], (uint)payloads[i].Length); BinaryPrimitives.WriteUInt32LittleEndian(entry[12..], (uint)offset); output.Write(entry); offset += payloads[i].Length; } foreach (var payload in payloads) { output.Write(payload); } return output.ToArray(); } /// Reads the directory of an .ico file: size, bytes, whether the image is PNG (tests, checks). public static IReadOnlyList ReadDirectory(ReadOnlySpan ico) { if (ico.Length < DirectoryHeader || BinaryPrimitives.ReadUInt16LittleEndian(ico) != 0 || BinaryPrimitives.ReadUInt16LittleEndian(ico[2..]) != 1) { throw new InvalidDataException("Non è un file .ico."); } var count = BinaryPrimitives.ReadUInt16LittleEndian(ico[4..]); var entries = new List(count); for (var i = 0; i < count; i++) { var e = ico.Slice(DirectoryHeader + (i * DirectoryEntry), DirectoryEntry); var size = e[0] == 0 ? 256 : e[0]; var length = (int)BinaryPrimitives.ReadUInt32LittleEndian(e[8..]); var offset = (int)BinaryPrimitives.ReadUInt32LittleEndian(e[12..]); if (offset < 0 || length < 0 || offset + length > ico.Length) { throw new InvalidDataException("Immagine fuori dal file .ico."); } var data = ico.Slice(offset, length); var png = data.Length >= 8 && data[..8].SequenceEqual(PngSignature); entries.Add(new IcoEntry(size, BinaryPrimitives.ReadUInt16LittleEndian(e[6..]), offset, length, png)); } return entries; } /// The image of an entry (DIB or PNG) as straight RGBA, for comparisons. public static RgbaImage Decode(ReadOnlySpan ico, IcoEntry entry) { ArgumentNullException.ThrowIfNull(entry); var data = ico.Slice(entry.Offset, entry.Length); if (entry.IsPng) { try { return PngDecoder.Decode(data.ToArray()); } catch (ImageDecodeException ex) { throw new InvalidDataException("PNG dell'icona non leggibile.", ex); } } var size = entry.Size; var image = new RgbaImage(size, size); var pixels = data[BitmapInfoHeader..]; for (var y = 0; y < size; y++) { var row = pixels.Slice((size - 1 - y) * size * 4, size * 4); var target = image.GetRow(y); for (var x = 0; x < size; x++) { target[x * 4] = row[(x * 4) + 2]; target[(x * 4) + 1] = row[(x * 4) + 1]; target[(x * 4) + 2] = row[x * 4]; target[(x * 4) + 3] = row[(x * 4) + 3]; } } return image; } private static ReadOnlySpan PngSignature => [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]; private static byte[] Png(RgbaImage image) => PngEncoder.Encode(image, new PngEncoderOptions { ColorMode = PngColorMode.Rgba }); /// A 32-bit DIB: BITMAPINFOHEADER with twice the height (image + mask), BGRA rows bottom-up, AND mask. private static byte[] Dib(RgbaImage image) { var size = image.Width; var maskStride = ((size + 31) / 32) * 4; var pixelBytes = size * size * 4; var result = new byte[BitmapInfoHeader + pixelBytes + (maskStride * size)]; var span = result.AsSpan(); BinaryPrimitives.WriteUInt32LittleEndian(span, BitmapInfoHeader); BinaryPrimitives.WriteInt32LittleEndian(span[4..], size); BinaryPrimitives.WriteInt32LittleEndian(span[8..], size * 2); BinaryPrimitives.WriteUInt16LittleEndian(span[12..], 1); // planes BinaryPrimitives.WriteUInt16LittleEndian(span[14..], 32); // bits per pixel BinaryPrimitives.WriteUInt32LittleEndian(span[16..], 0); // BI_RGB BinaryPrimitives.WriteUInt32LittleEndian(span[20..], (uint)(pixelBytes + (maskStride * size))); var pixels = span[BitmapInfoHeader..]; var mask = span[(BitmapInfoHeader + pixelBytes)..]; for (var y = 0; y < size; y++) { var row = pixels.Slice((size - 1 - y) * size * 4, size * 4); for (var x = 0; x < size; x++) { // Straight (not premultiplied) alpha, as icons want it. var c = image.GetPixel(x, y); row[x * 4] = c.B; row[(x * 4) + 1] = c.G; row[(x * 4) + 2] = c.R; row[(x * 4) + 3] = c.A; if (c.A == 0) { // Transparent pixels are also set in the AND mask, for the readers that ignore the alpha channel. var maskRow = mask.Slice((size - 1 - y) * maskStride, maskStride); maskRow[x / 8] |= (byte)(0x80 >> (x % 8)); } } } return result; } } /// One image of an .ico file. public sealed record IcoEntry(int Size, int BitsPerPixel, int Offset, int Length, bool IsPng);