SkiaSharp e SkiaSharp.NativeAssets.Linux.NoDependencies escono da Directory.Packages.props e da ogni progetto, per la regola «niente componenti esterni». Tutto ciò che leggeva, ridimensionava o disegnava immagini con Skia ora usa codice scritto in casa sulla BCL: - ImageResampler: Lanczos3 separabile o media per area, alfa premoltiplicata, luce lineare facoltativa; ImageGray per l'impronta. - Il lettore JPEG decodifica ridotto a 1/2, 1/4, 1/8; le miniature chiedono almeno il doppio del lato lungo, così l'ultimo passo resta a Lanczos e la miniatura resta uguale alla lettura intera. - ImageOrientation applica l'EXIF in un punto solo (miniature, impronte, anteprime, dimensioni mostrate). - ImageConverter cambia formato fra JPEG, PNG e WebP portando EXIF, ICC, XMP e testi, con l'etichetta d'orientamento e i pixel non ruotati; PngEncoder scrive anche i testi (tEXt o iTXt). - Febe.Core.Drawing: rasterizzatore con antialiasing (tracciati, riempimenti, tratti, gradienti) per icona, immagini dimostrative, riquadro neutro dei video, foto del sito di prova e prove. Febe.ico rigenerata. - ThumbnailStore legge anche i BMP a 24 bit, come quelli del decoder video del ramo funzione/decoder-h264. Le impronte salvate restano confrontabili (390 su 393 campioni identiche, le altre a distanza 2, soglia 6): nessun ricalcolo. Un PNG o JPEG troncato ora non ha miniatura né impronta. Documenti, elenco dei componenti della pagina «Informazioni» e decisioni aggiornati; nuove prove per ridimensionamento, orientamento, lettura ridotta, conversione con metadati, testi PNG e rasterizzatore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
230 lines
9.5 KiB
C#
230 lines
9.5 KiB
C#
using System.Buffers.Binary;
|
|
using Febe.Core.Codecs.Images;
|
|
using Febe.Core.Drawing;
|
|
|
|
namespace Febe.Desktop.Core;
|
|
|
|
/// <summary>
|
|
/// 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 (<see cref="Canvas"/>) at any size, the same way the SVG describes it:
|
|
/// <code>
|
|
/// <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"/>
|
|
/// </code>
|
|
/// </summary>
|
|
public static class BrandMark
|
|
{
|
|
/// <summary>The sizes of the Windows icon: window, notification area, taskbar, Explorer, installer.</summary>
|
|
public static IReadOnlyList<int> IconSizes { get; } = [16, 24, 32, 48, 64, 128, 256];
|
|
|
|
private static readonly Color Accent = Color.FromRgb(0x0F5C8C);
|
|
|
|
/// <summary>The mark as a square image of <paramref name="size"/> pixels, straight RGBA, transparent corners.</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>The multi-size Windows icon (<see cref="IconSizes"/>) of the mark, as the bytes of an .ico file.</summary>
|
|
public static byte[] RenderIcon()
|
|
{
|
|
var images = new List<RgbaImage>();
|
|
foreach (var size in IconSizes)
|
|
{
|
|
images.Add(Render(size));
|
|
}
|
|
|
|
return IcoWriter.Write(images);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static class IcoWriter
|
|
{
|
|
/// <summary>Images at or above this size are stored as PNG (the Vista format); smaller ones as DIB.</summary>
|
|
public const int PngFrom = 256;
|
|
|
|
private const int DirectoryHeader = 6;
|
|
private const int DirectoryEntry = 16;
|
|
private const int BitmapInfoHeader = 40;
|
|
|
|
/// <summary>The .ico bytes of square <paramref name="images"/> (1 to 256 pixels), in the order given.</summary>
|
|
public static byte[] Write(IReadOnlyList<RgbaImage> 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<byte[]>(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<byte> 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<byte> 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();
|
|
}
|
|
|
|
/// <summary>Reads the directory of an .ico file: size, bytes, whether the image is PNG (tests, checks).</summary>
|
|
public static IReadOnlyList<IcoEntry> ReadDirectory(ReadOnlySpan<byte> 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<IcoEntry>(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;
|
|
}
|
|
|
|
/// <summary>The image of an entry (DIB or PNG) as straight RGBA, for comparisons.</summary>
|
|
public static RgbaImage Decode(ReadOnlySpan<byte> 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<byte> PngSignature => [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
|
|
|
private static byte[] Png(RgbaImage image) => PngEncoder.Encode(image, new PngEncoderOptions { ColorMode = PngColorMode.Rgba });
|
|
|
|
/// <summary>A 32-bit DIB: BITMAPINFOHEADER with twice the height (image + mask), BGRA rows bottom-up, AND mask.</summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>One image of an .ico file.</summary>
|
|
public sealed record IcoEntry(int Size, int BitsPerPixel, int Offset, int Length, bool IsPng);
|