Codec di casa al posto di SkiaSharp (C1, passo 5)
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>
This commit is contained in:
1 parent
b4383a6110
commit
d6a6a68496
47 files changed
+3391
-562
No files matched your search
@@ -1,11 +1,12 @@
|
||||
using System.Buffers.Binary;
|
||||
using SkiaSharp;
|
||||
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 SkiaSharp at any size, the same way the SVG describes it:
|
||||
/// 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"/>
|
||||
@@ -17,57 +18,32 @@ 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 SKColor Accent = new(0x0F, 0x5C, 0x8C);
|
||||
private static readonly Color Accent = Color.FromRgb(0x0F5C8C);
|
||||
|
||||
/// <summary>The mark as a square bitmap of <paramref name="size"/> pixels, premultiplied BGRA, transparent corners.</summary>
|
||||
public static SKBitmap Render(int size)
|
||||
/// <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 bitmap = new SKBitmap(new SKImageInfo(size, size, SKColorType.Bgra8888, SKAlphaType.Premul));
|
||||
using var canvas = new SKCanvas(bitmap);
|
||||
canvas.Clear(SKColors.Transparent);
|
||||
var scale = size / 32f;
|
||||
canvas.Scale(scale);
|
||||
using (var square = new SKPaint { Color = Accent, IsAntialias = true, Style = SKPaintStyle.Fill })
|
||||
{
|
||||
canvas.DrawRoundRect(new SKRect(0, 0, 32, 32), 8, 8, square);
|
||||
}
|
||||
|
||||
using (var ring = new SKPaint { Color = SKColors.White, IsAntialias = true, Style = SKPaintStyle.Stroke, StrokeWidth = 2.6f })
|
||||
{
|
||||
canvas.DrawCircle(14.5f, 17.5f, 7.4f, ring);
|
||||
}
|
||||
|
||||
using (var moon = new SKPaint { Color = SKColors.White, IsAntialias = true, Style = SKPaintStyle.Fill })
|
||||
{
|
||||
canvas.DrawCircle(23.2f, 8.8f, 3.1f, moon);
|
||||
}
|
||||
|
||||
canvas.Flush();
|
||||
return bitmap;
|
||||
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<SKBitmap>();
|
||||
try
|
||||
var images = new List<RgbaImage>();
|
||||
foreach (var size in IconSizes)
|
||||
{
|
||||
foreach (var size in IconSizes)
|
||||
{
|
||||
images.Add(Render(size));
|
||||
}
|
||||
images.Add(Render(size));
|
||||
}
|
||||
|
||||
return IcoWriter.Write(images);
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var image in images)
|
||||
{
|
||||
image.Dispose();
|
||||
}
|
||||
}
|
||||
return IcoWriter.Write(images);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +62,7 @@ public static class IcoWriter
|
||||
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<SKBitmap> images)
|
||||
public static byte[] Write(IReadOnlyList<RgbaImage> images)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(images);
|
||||
if (images.Count is 0 or > ushort.MaxValue)
|
||||
@@ -167,41 +143,48 @@ public static class IcoWriter
|
||||
return entries;
|
||||
}
|
||||
|
||||
/// <summary>The image of an entry as a bitmap (DIB or PNG), for comparisons.</summary>
|
||||
public static SKBitmap Decode(ReadOnlySpan<byte> ico, IcoEntry entry)
|
||||
/// <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)
|
||||
{
|
||||
return SKBitmap.Decode(data.ToArray()) ?? throw new InvalidDataException("PNG dell'icona non leggibile.");
|
||||
try
|
||||
{
|
||||
return PngDecoder.Decode(data.ToArray());
|
||||
}
|
||||
catch (ImageDecodeException ex)
|
||||
{
|
||||
throw new InvalidDataException("PNG dell'icona non leggibile.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
var size = entry.Size;
|
||||
var bitmap = new SKBitmap(new SKImageInfo(size, size, SKColorType.Bgra8888, SKAlphaType.Unpremul));
|
||||
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++)
|
||||
{
|
||||
bitmap.SetPixel(x, y, new SKColor(row[(x * 4) + 2], row[(x * 4) + 1], row[x * 4], row[(x * 4) + 3]));
|
||||
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 bitmap;
|
||||
return image;
|
||||
}
|
||||
|
||||
private static ReadOnlySpan<byte> PngSignature => [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
|
||||
private static byte[] Png(SKBitmap image)
|
||||
{
|
||||
using var data = image.Encode(SKEncodedImageFormat.Png, 100) ?? throw new InvalidOperationException("Codifica PNG non riuscita.");
|
||||
return data.ToArray();
|
||||
}
|
||||
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(SKBitmap image)
|
||||
private static byte[] Dib(RgbaImage image)
|
||||
{
|
||||
var size = image.Width;
|
||||
var maskStride = ((size + 31) / 32) * 4;
|
||||
@@ -223,13 +206,13 @@ public static class IcoWriter
|
||||
var row = pixels.Slice((size - 1 - y) * size * 4, size * 4);
|
||||
for (var x = 0; x < size; x++)
|
||||
{
|
||||
// GetPixel gives straight (not premultiplied) alpha, as icons want it.
|
||||
// Straight (not premultiplied) alpha, as icons want it.
|
||||
var c = image.GetPixel(x, y);
|
||||
row[x * 4] = c.Blue;
|
||||
row[(x * 4) + 1] = c.Green;
|
||||
row[(x * 4) + 2] = c.Red;
|
||||
row[(x * 4) + 3] = c.Alpha;
|
||||
if (c.Alpha == 0)
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user