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:
Alby96andClaude Opus 5.5 committed 2026-11-18 16:02:03 +01:00
1 parent b4383a6110
commit d6a6a68496
47 files changed
+3391 -562

No files matched your search

+83 -66
View File
@@ -1,8 +1,10 @@
using System.Buffers.Binary;
using System.Numerics;
using Febe.Core.Abstractions;
using Febe.Core.Codecs.Images;
using Febe.Core.Domain;
using Febe.Core.Drawing;
using Febe.Data;
using SkiaSharp;
namespace Febe.Tests.Support;
@@ -116,37 +118,34 @@ internal static class TestDatabase
public static Task<FebeDatabase> OpenAsync(TempFolder folder, string name = "dati") => FebeDatabase.OpenAsync(folder.Combine(name));
}
/// <summary>Generated images: «photos» made of gradients, shapes, lines and grain, reproducible from a seed.</summary>
/// <summary>
/// Generated images: «photos» made of gradients, shapes, lines and grain, reproducible from a seed, drawn and encoded
/// with Febe's own code (Drawing, Codecs/Images).
/// </summary>
internal static class TestImages
{
public static SKBitmap Scene(int seed, int width = 1200, int height = 900)
public static RgbaImage Scene(int seed, int width = 1200, int height = 900)
{
var random = new Random(seed);
var bitmap = new SKBitmap(new SKImageInfo(width, height, SKColorType.Rgba8888, SKAlphaType.Premul));
using var canvas = new SKCanvas(bitmap);
SKColor RandomColor() => new((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256));
var image = new RgbaImage(width, height);
using var canvas = new Canvas(image);
Color RandomColor() => new((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256));
using (var background = new SKPaint())
{
var angle = random.NextDouble() * Math.PI * 2;
var dx = (float)Math.Cos(angle) * width / 2;
var dy = (float)Math.Sin(angle) * height / 2;
background.Shader = SKShader.CreateLinearGradient(
new SKPoint((width / 2f) - dx, (height / 2f) - dy),
new SKPoint((width / 2f) + dx, (height / 2f) + dy),
[RandomColor(), RandomColor(), RandomColor()],
null,
SKShaderTileMode.Clamp);
canvas.DrawRect(0, 0, width, height, background);
}
var angle = random.NextDouble() * Math.PI * 2;
var dx = (float)Math.Cos(angle) * width / 2;
var dy = (float)Math.Sin(angle) * height / 2;
var background = new LinearGradientBrush(
new Vector2((width / 2f) - dx, (height / 2f) - dy),
new Vector2((width / 2f) + dx, (height / 2f) + dy),
[RandomColor(), RandomColor(), RandomColor()]);
canvas.FillRect(0, 0, width, height, background);
using var paint = new SKPaint { IsAntialias = true };
var shapes = 10 + random.Next(10);
for (var i = 0; i < shapes; i++)
{
paint.Color = RandomColor().WithAlpha((byte)(160 + random.Next(96)));
paint.Style = random.Next(4) == 0 ? SKPaintStyle.Stroke : SKPaintStyle.Fill;
paint.StrokeWidth = 4 + random.Next(30);
var color = RandomColor().WithAlpha((byte)(160 + random.Next(96)));
var stroke = random.Next(4) == 0;
float strokeWidth = 4 + random.Next(30);
var x = (float)(random.NextDouble() * width);
var y = (float)(random.NextDouble() * height);
var w = (float)((0.05 + (random.NextDouble() * 0.4)) * width);
@@ -154,76 +153,94 @@ internal static class TestImages
switch (random.Next(4))
{
case 0:
canvas.DrawRect(x - (w / 2), y - (h / 2), w, h, paint);
if (stroke)
{
canvas.StrokeRect(x - (w / 2), y - (h / 2), w, h, strokeWidth, color);
}
else
{
canvas.FillRect(x - (w / 2), y - (h / 2), w, h, color);
}
break;
case 1:
canvas.DrawCircle(x, y, Math.Min(w, h) / 2, paint);
if (stroke)
{
canvas.StrokeCircle(x, y, Math.Min(w, h) / 2, strokeWidth, color);
}
else
{
canvas.FillCircle(x, y, Math.Min(w, h) / 2, color);
}
break;
case 2:
canvas.DrawOval(new SKRect(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2)), paint);
if (stroke)
{
canvas.StrokeEllipse(x, y, w / 2, h / 2, strokeWidth, color);
}
else
{
canvas.FillEllipse(x, y, w / 2, h / 2, color);
}
break;
default:
paint.StrokeWidth = 6 + random.Next(20);
canvas.DrawLine(x, y, (float)(random.NextDouble() * width), (float)(random.NextDouble() * height), paint);
strokeWidth = 6 + random.Next(20);
canvas.DrawLine(x, y, (float)(random.NextDouble() * width), (float)(random.NextDouble() * height), strokeWidth, color);
break;
}
}
// Fine grain, as in a photo.
using var grain = new SKPaint { IsAntialias = false };
for (var i = 0; i < width * height / 60; i++)
{
var v = (byte)random.Next(256);
grain.Color = new SKColor(v, v, v, 28);
canvas.DrawPoint(random.Next(width), random.Next(height), grain);
canvas.BlendPixel(random.Next(width), random.Next(height), new Color(v, v, v, 28));
}
return bitmap;
return image;
}
public static byte[] Jpeg(SKBitmap bitmap, int quality = 90)
{
using var image = SKImage.FromBitmap(bitmap);
using var data = image.Encode(SKEncodedImageFormat.Jpeg, quality);
return data.ToArray();
}
public static byte[] Jpeg(RgbaImage image, int quality = 90) => JpegEncoder.Encode(image, new JpegEncoderOptions { Quality = quality });
public static byte[] Png(SKBitmap bitmap)
{
using var image = SKImage.FromBitmap(bitmap);
using var data = image.Encode(SKEncodedImageFormat.Png, 100);
return data.ToArray();
}
public static byte[] Png(RgbaImage image) => PngEncoder.Encode(image);
public static SKBitmap Resize(SKBitmap source, int width, int height)
public static RgbaImage Resize(RgbaImage source, int width, int height) => ImageResampler.Resize(source, width, height);
/// <summary>The pixels of <paramref name="width"/> × <paramref name="height"/> from (<paramref name="left"/>, <paramref name="top"/>).</summary>
public static RgbaImage Crop(RgbaImage source, int left, int top, int width, int height)
{
var result = new SKBitmap(new SKImageInfo(width, height, SKColorType.Rgba8888, SKAlphaType.Premul));
using var canvas = new SKCanvas(result);
using var image = SKImage.FromBitmap(source);
canvas.DrawImage(image, new SKRect(0, 0, width, height), new SKSamplingOptions(SKCubicResampler.Mitchell), null);
var result = new RgbaImage(width, height);
for (var y = 0; y < height; y++)
{
source.GetRow(top + y).Slice(left * 4, width * 4).CopyTo(result.GetRow(y));
}
return result;
}
public static SKBitmap Crop(SKBitmap source, SKRectI area)
{
var result = new SKBitmap(new SKImageInfo(area.Width, area.Height, SKColorType.Rgba8888, SKAlphaType.Premul));
using var canvas = new SKCanvas(result);
using var image = SKImage.FromBitmap(source);
canvas.DrawImage(image, area, new SKRect(0, 0, area.Width, area.Height), new SKSamplingOptions(SKFilterMode.Nearest), null);
return result;
}
/// <summary>Decodes a JPEG/PNG back to a bitmap.</summary>
public static SKBitmap Decode(byte[] encoded) => SKBitmap.Decode(encoded) ?? throw new InvalidOperationException("decode");
/// <summary>Decodes a JPEG/PNG back to pixels (as stored: the orientation is not applied).</summary>
public static RgbaImage Decode(byte[] encoded) => ImageDecoder.Decode(encoded);
/// <summary>Rotates pixels 90° counter-clockwise (what a camera stores before tagging orientation 6).</summary>
public static SKBitmap RotateCounterClockwise(SKBitmap source)
public static RgbaImage RotateCounterClockwise(RgbaImage source)
{
var result = new SKBitmap(new SKImageInfo(source.Height, source.Width, SKColorType.Rgba8888, SKAlphaType.Premul));
using var canvas = new SKCanvas(result);
canvas.Translate(0, source.Width);
canvas.RotateDegrees(-90);
canvas.DrawBitmap(source, 0, 0, new SKSamplingOptions(SKFilterMode.Nearest), null);
var result = new RgbaImage(source.Height, source.Width);
for (var y = 0; y < result.Height; y++)
{
var row = result.GetRow(y);
for (var x = 0; x < result.Width; x++)
{
// Target (x, y) shows source (w − 1 − y, x).
var (r, g, b, a) = source.GetPixel(source.Width - 1 - y, x);
row[x * 4] = r;
row[(x * 4) + 1] = g;
row[(x * 4) + 2] = b;
row[(x * 4) + 3] = a;
}
}
return result;
}