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

+26 -42
View File
@@ -1,5 +1,5 @@
using System.Numerics;
using SkiaSharp;
using Febe.Core.Codecs.Images;
namespace Febe.Core.Library;
@@ -38,6 +38,9 @@ public static class PerceptualHash
private const int Size = 32;
private const int Low = 8;
/// <summary>A JPEG is decoded reduced, but never below this long side.</summary>
private const int MinDecodedSide = 256;
private static readonly double[,] Cosines = BuildCosines();
/// <summary>Hamming distance between two hashes (0–64).</summary>
@@ -50,8 +53,8 @@ public static class PerceptualHash
public static ulong? Compute(Stream encodedImage)
{
ArgumentNullException.ThrowIfNull(encodedImage);
using var bitmap = ImageDecoding.DecodeUpright(encodedImage, minLongSide: 256);
return bitmap is null ? null : Compute(bitmap);
using var image = ImageDecoding.DecodeUpright(encodedImage, minLongSide: MinDecodedSide);
return image is null ? null : Compute(image);
}
/// <summary>Hash of an image file; null when it is not a decodable image.</summary>
@@ -61,17 +64,11 @@ public static class PerceptualHash
return Compute(stream);
}
/// <summary>Hash of a decoded bitmap (any size, premultiplied RGBA 8888 or convertible).</summary>
internal static ulong Compute(SKBitmap bitmap)
/// <summary>Hash of a decoded image (any size), upright as it is shown.</summary>
internal static ulong Compute(RgbaImage image)
{
ArgumentNullException.ThrowIfNull(bitmap);
using var rgba = bitmap.ColorType == SKColorType.Rgba8888 && bitmap.AlphaType == SKAlphaType.Premul
? null
: bitmap.Copy(SKColorType.Rgba8888);
var source = rgba ?? bitmap;
var grey = Luminance(source);
var small = AreaResample(grey, source.Width, source.Height, Size, Size);
return HashOf(small);
ArgumentNullException.ThrowIfNull(image);
return HashOf(AreaResample(image, Size, Size));
}
/// <summary>The hash of a 32×32 grey image (row-major).</summary>
@@ -123,50 +120,37 @@ public static class PerceptualHash
return hash;
}
/// <summary>Grey levels (0–255) of an RGBA premultiplied bitmap, composited over white.</summary>
private static double[] Luminance(SKBitmap bitmap)
/// <summary>
/// The grey levels (0–255, over white: <see cref="ImageGray.LumaOverWhite"/>) of an image reduced to dw × dh by area
/// averaging, one source row at a time (memory: one row and dw × source height values).
/// </summary>
internal static double[] AreaResample(RgbaImage image, int dw, int dh)
{
var width = bitmap.Width;
var height = bitmap.Height;
var result = new double[width * height];
var pixels = bitmap.GetPixelSpan();
var rowBytes = bitmap.RowBytes;
for (var y = 0; y < height; y++)
{
var row = pixels.Slice(y * rowBytes, width * 4);
for (var x = 0; x < width; x++)
{
var r = row[x * 4];
var g = row[(x * 4) + 1];
var b = row[(x * 4) + 2];
var a = row[(x * 4) + 3];
var white = 255 - a; // premultiplied: over white, each channel gains (255 − alpha)
result[(y * width) + x] = (0.299 * (r + white)) + (0.587 * (g + white)) + (0.114 * (b + white));
}
}
return result;
}
/// <summary>Area-averaging resample with exact fractional coverage (no aliasing when reducing).</summary>
internal static double[] AreaResample(double[] source, int sw, int sh, int dw, int dh)
{
var horizontal = new double[dw * sh];
var sw = image.Width;
var sh = image.Height;
var xs = Weights(sw, dw);
var horizontal = new double[dw * sh];
var grey = new double[sw];
for (var y = 0; y < sh; y++)
{
ImageGray.LumaOverWhite(image.GetRow(y), grey);
for (var dx = 0; dx < dw; dx++)
{
double sum = 0;
foreach (var (index, weight) in xs[dx])
{
sum += source[(y * sw) + index] * weight;
sum += grey[index] * weight;
}
horizontal[(y * dw) + dx] = sum;
}
}
return Vertical(horizontal, sh, dw, dh);
}
private static double[] Vertical(double[] horizontal, int sh, int dw, int dh)
{
var result = new double[dw * dh];
var ys = Weights(sh, dh);
for (var dy = 0; dy < dh; dy++)