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,19 +1,24 @@
|
||||
using System.Buffers.Binary;
|
||||
using Febe.Core.Codecs.Images;
|
||||
using Febe.Core.Library;
|
||||
using Febe.Tests.Imaging;
|
||||
using Febe.Tests.Support;
|
||||
using SkiaSharp;
|
||||
using TestImages = Febe.Tests.Support.TestImages;
|
||||
|
||||
namespace Febe.Tests.Library;
|
||||
|
||||
[TestClass]
|
||||
public sealed class ThumbnailStoreTests
|
||||
{
|
||||
private static SKImageInfo InfoOf(string path)
|
||||
private static ImageInfo InfoOf(string path)
|
||||
{
|
||||
using var codec = SKCodec.Create(path) ?? throw new AssertFailedException("Miniatura non leggibile.");
|
||||
Assert.AreEqual(SKEncodedImageFormat.Jpeg, codec.EncodedFormat);
|
||||
return codec.Info;
|
||||
var info = ImageDecoder.Identify(File.ReadAllBytes(path));
|
||||
Assert.AreEqual(ImageFormat.Jpeg, info.Format);
|
||||
return info;
|
||||
}
|
||||
|
||||
private static RgbaImage Read(string path) => ImageDecoder.Decode(File.ReadAllBytes(path));
|
||||
|
||||
[TestMethod]
|
||||
public void ThumbnailsAre480PixelsOnTheLongSideStoredOnceAndSharded()
|
||||
{
|
||||
@@ -68,17 +73,90 @@ public sealed class ThumbnailStoreTests
|
||||
Assert.AreEqual(360, info.Height);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UprightAndTaggedSidewaysGiveTheSameThumbnail()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
var store = new ThumbnailStore(temp.Path);
|
||||
using var scene = TestImages.Scene(4, 1200, 900);
|
||||
using var sideways = TestImages.RotateCounterClockwise(scene);
|
||||
Assert.IsTrue(store.Create(Make.Sha(9), new MemoryStream(TestImages.Png(scene))));
|
||||
Assert.IsTrue(store.Create(Make.Sha(10), new MemoryStream(TestImages.WithExifOrientation(TestImages.Jpeg(sideways, 95), 6))));
|
||||
using var upright = Read(store.PathFor(Make.Sha(9)));
|
||||
using var turned = Read(store.PathFor(Make.Sha(10)));
|
||||
var quality = ImageQuality.Compare(upright, turned);
|
||||
Assert.IsGreaterThan(30.0, quality.MinPsnr, quality.ToString());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TransparentImagesBecomeJpegOnWhite()
|
||||
{
|
||||
using var temp = new TempFolder();
|
||||
var store = new ThumbnailStore(temp.Path);
|
||||
using var transparent = new SKBitmap(new SKImageInfo(600, 600, SKColorType.Rgba8888, SKAlphaType.Premul));
|
||||
transparent.Erase(SKColors.Transparent);
|
||||
using var transparent = new RgbaImage(600, 600);
|
||||
Assert.IsTrue(store.Create(Make.Sha(4), new MemoryStream(TestImages.Png(transparent))));
|
||||
using var thumbnail = SKBitmap.Decode(store.PathFor(Make.Sha(4)));
|
||||
using var thumbnail = Read(store.PathFor(Make.Sha(4)));
|
||||
var pixel = thumbnail.GetPixel(10, 10);
|
||||
Assert.IsGreaterThan((byte)240, pixel.Red);
|
||||
Assert.IsGreaterThan((byte)240, pixel.R);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TheThumbnailShowsThePictureReducedFaithfully()
|
||||
{
|
||||
// Compared with the source reduced by area averaging (the most neutral reduction), what differs is only the
|
||||
// JPEG compression and the filter.
|
||||
using var temp = new TempFolder();
|
||||
var store = new ThumbnailStore(temp.Path);
|
||||
using var scene = TestImages.Scene(8, 2400, 1800);
|
||||
Assert.IsTrue(store.Create(Make.Sha(7), new MemoryStream(TestImages.Png(scene))));
|
||||
using var thumbnail = Read(store.PathFor(Make.Sha(7)));
|
||||
using var expected = ImageResampler.Resize(scene, 480, 360, new ResampleOptions { Filter = ResampleFilter.Area });
|
||||
var quality = ImageQuality.Compare(expected, thumbnail);
|
||||
Assert.IsGreaterThan(28.0, quality.MinPsnr, quality.ToString());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AJpegShrunkWhileDecodingGivesTheSameThumbnailAsAFullDecode()
|
||||
{
|
||||
// 4000 × 3000 is decoded at a quarter (1000 × 750) and then reduced to 480: compared with the full-size path.
|
||||
using var temp = new TempFolder();
|
||||
var store = new ThumbnailStore(temp.Path);
|
||||
using var scene = TestImages.Scene(9, 4000, 3000);
|
||||
var jpeg = TestImages.Jpeg(scene, 92);
|
||||
Assert.IsTrue(store.Create(Make.Sha(8), new MemoryStream(jpeg)));
|
||||
using var thumbnail = Read(store.PathFor(Make.Sha(8)));
|
||||
Assert.AreEqual((480, 360), (thumbnail.Width, thumbnail.Height));
|
||||
using var full = ImageDecoder.Decode(jpeg);
|
||||
using var reference = ImageResampler.Resize(full, 480, 360);
|
||||
var quality = ImageQuality.Compare(reference, thumbnail);
|
||||
Assert.IsGreaterThan(30.0, quality.MinPsnr, quality.ToString());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void VideoFramesAsBmpBecomeThumbnails()
|
||||
{
|
||||
// The in-house video decoder (branch funzione/decoder-h264) hands frames over as 24-bit BMP, rows bottom up.
|
||||
using var temp = new TempFolder();
|
||||
var store = new ThumbnailStore(temp.Path);
|
||||
using var frame = TestImages.Scene(11, 1280, 720);
|
||||
Assert.IsTrue(store.Create(Make.Sha(11), new MemoryStream(Bmp24(frame))));
|
||||
var info = InfoOf(store.PathFor(Make.Sha(11)));
|
||||
Assert.AreEqual((480, 270), (info.Width, info.Height));
|
||||
using var thumbnail = Read(store.PathFor(Make.Sha(11)));
|
||||
using var expected = ImageResampler.Resize(frame, 480, 270);
|
||||
var quality = ImageQuality.Compare(expected, thumbnail);
|
||||
Assert.IsGreaterThan(30.0, quality.MinPsnr, quality.ToString());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ThePhotoOfASearchIsASmallJpeg()
|
||||
{
|
||||
using var scene = TestImages.Scene(12, 1080, 1080);
|
||||
var small = ThumbnailStore.EncodeSmallJpeg(new MemoryStream(TestImages.Jpeg(scene)), 160);
|
||||
Assert.IsNotNull(small);
|
||||
var info = ImageDecoder.Identify(small);
|
||||
Assert.AreEqual((ImageFormat.Jpeg, 160, 160), (info.Format, info.Width, info.Height));
|
||||
Assert.IsNull(ThumbnailStore.EncodeSmallJpeg(new MemoryStream([1, 2, 3]), 160));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -100,8 +178,14 @@ public sealed class ThumbnailStoreTests
|
||||
var tile = store.NeutralVideoTile();
|
||||
Assert.AreEqual(Path.Combine(temp.Path, ThumbnailStore.NeutralVideoTileName), tile);
|
||||
var info = InfoOf(tile);
|
||||
Assert.AreEqual(480, info.Height);
|
||||
Assert.AreEqual((270, 480), (info.Width, info.Height));
|
||||
Assert.AreEqual(tile, store.NeutralVideoTile());
|
||||
|
||||
// Light grey, a darker disc in the middle, the play sign in the light grey again.
|
||||
using var image = Read(tile);
|
||||
AssertGrey(image.GetPixel(10, 10), 0xE4, "sfondo");
|
||||
AssertGrey(image.GetPixel(135 - 25, 240), 0x9A, "disco");
|
||||
AssertGrey(image.GetPixel(135 + 2, 240), 0xE4, "segno di riproduzione");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
@@ -140,4 +224,39 @@ public sealed class ThumbnailStoreTests
|
||||
|
||||
Assert.IsNull(await ffmpeg.ExtractJpegAsync(temp.Combine("non-esiste.mp4")));
|
||||
}
|
||||
|
||||
private static void AssertGrey((byte R, byte G, byte B, byte A) pixel, int level, string what) =>
|
||||
Assert.IsTrue(Math.Abs(pixel.R - level) <= 4 && Math.Abs(pixel.G - level) <= 4 && Math.Abs(pixel.B - level) <= 4, $"{what}: {pixel}");
|
||||
|
||||
/// <summary>A 24-bit BI_RGB BMP, rows bottom up, the way VideoImage.ToBmp writes it.</summary>
|
||||
private static byte[] Bmp24(RgbaImage image)
|
||||
{
|
||||
var stride = ((image.Width * 3) + 3) & ~3;
|
||||
var size = 54 + (stride * image.Height);
|
||||
var bmp = new byte[size];
|
||||
var span = bmp.AsSpan();
|
||||
span[0] = (byte)'B';
|
||||
span[1] = (byte)'M';
|
||||
BinaryPrimitives.WriteInt32LittleEndian(span[2..], size);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(span[10..], 54);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(span[14..], 40);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(span[18..], image.Width);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(span[22..], image.Height);
|
||||
BinaryPrimitives.WriteInt16LittleEndian(span[26..], 1);
|
||||
BinaryPrimitives.WriteInt16LittleEndian(span[28..], 24);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(span[34..], stride * image.Height);
|
||||
for (var y = 0; y < image.Height; y++)
|
||||
{
|
||||
var source = image.GetRow(y);
|
||||
var target = span.Slice(54 + ((image.Height - 1 - y) * stride), image.Width * 3);
|
||||
for (var x = 0; x < image.Width; x++)
|
||||
{
|
||||
target[x * 3] = source[(x * 4) + 2];
|
||||
target[(x * 3) + 1] = source[(x * 4) + 1];
|
||||
target[(x * 3) + 2] = source[x * 4];
|
||||
}
|
||||
}
|
||||
|
||||
return bmp;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user