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>
263 lines
12 KiB
C#
263 lines
12 KiB
C#
using System.Buffers.Binary;
|
||
using Febe.Core.Codecs.Images;
|
||
using Febe.Core.Library;
|
||
using Febe.Tests.Imaging;
|
||
using Febe.Tests.Support;
|
||
using TestImages = Febe.Tests.Support.TestImages;
|
||
|
||
namespace Febe.Tests.Library;
|
||
|
||
[TestClass]
|
||
public sealed class ThumbnailStoreTests
|
||
{
|
||
private static ImageInfo InfoOf(string path)
|
||
{
|
||
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()
|
||
{
|
||
using var temp = new TempFolder();
|
||
var store = new ThumbnailStore(temp.Combine("miniature"));
|
||
using var scene = TestImages.Scene(1, 2400, 1600);
|
||
var key = Make.Sha(0xAB12);
|
||
|
||
Assert.IsTrue(store.Create(key, new MemoryStream(TestImages.Jpeg(scene))));
|
||
var path = store.PathFor(key);
|
||
Assert.AreEqual(Path.Combine(temp.Combine("miniature"), key[..2], key + ".jpg"), path);
|
||
var info = InfoOf(path);
|
||
Assert.AreEqual(480, info.Width);
|
||
Assert.AreEqual(320, info.Height);
|
||
|
||
var written = File.GetLastWriteTimeUtc(path);
|
||
File.SetLastWriteTimeUtc(path, written.AddDays(-1));
|
||
Assert.IsTrue(store.Create(key, new MemoryStream(TestImages.Jpeg(scene))));
|
||
Assert.AreEqual(written.AddDays(-1), File.GetLastWriteTimeUtc(path), "Una miniatura si genera una volta sola.");
|
||
Assert.IsTrue(store.Exists(key.ToUpperInvariant()));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void PortraitAndSmallImages()
|
||
{
|
||
using var temp = new TempFolder();
|
||
var store = new ThumbnailStore(temp.Path);
|
||
using var portrait = TestImages.Scene(2, 1080, 1920);
|
||
Assert.IsTrue(store.Create(Make.Sha(1), new MemoryStream(TestImages.Jpeg(portrait))));
|
||
var info = InfoOf(store.PathFor(Make.Sha(1)));
|
||
Assert.AreEqual(480, info.Height);
|
||
Assert.AreEqual(270, info.Width);
|
||
|
||
using var small = TestImages.Scene(3, 320, 200);
|
||
Assert.IsTrue(store.Create(Make.Sha(2), new MemoryStream(TestImages.Png(small))));
|
||
var smallInfo = InfoOf(store.PathFor(Make.Sha(2)));
|
||
Assert.AreEqual(320, smallInfo.Width, "Le immagini piccole non si ingrandiscono.");
|
||
}
|
||
|
||
[TestMethod]
|
||
public void ExifOrientationIsApplied()
|
||
{
|
||
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); // stored 900 × 1200
|
||
var tagged = TestImages.WithExifOrientation(TestImages.Jpeg(sideways), 6);
|
||
|
||
Assert.IsTrue(store.Create(Make.Sha(3), new MemoryStream(tagged)));
|
||
var info = InfoOf(store.PathFor(Make.Sha(3)));
|
||
Assert.AreEqual(480, info.Width, "Mostrata dritta: più larga che alta.");
|
||
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 RgbaImage(600, 600);
|
||
Assert.IsTrue(store.Create(Make.Sha(4), new MemoryStream(TestImages.Png(transparent))));
|
||
using var thumbnail = Read(store.PathFor(Make.Sha(4)));
|
||
var pixel = thumbnail.GetPixel(10, 10);
|
||
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]
|
||
public void InvalidContentAndKeysAreRefused()
|
||
{
|
||
using var temp = new TempFolder();
|
||
var store = new ThumbnailStore(temp.Path);
|
||
Assert.IsFalse(store.Create(Make.Sha(5), new MemoryStream([1, 2, 3])));
|
||
Assert.IsFalse(store.Exists(Make.Sha(5)));
|
||
Assert.ThrowsExactly<ArgumentException>(() => store.PathFor("../../etc/passwd"));
|
||
Assert.ThrowsExactly<ArgumentException>(() => store.PathFor("abc"));
|
||
}
|
||
|
||
[TestMethod]
|
||
public void VideosWithoutCoverUseTheNeutralTile()
|
||
{
|
||
using var temp = new TempFolder();
|
||
var store = new ThumbnailStore(temp.Path);
|
||
var tile = store.NeutralVideoTile();
|
||
Assert.AreEqual(Path.Combine(temp.Path, ThumbnailStore.NeutralVideoTileName), tile);
|
||
var info = InfoOf(tile);
|
||
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]
|
||
public async Task VideoFramesComeFromFfmpegWhenAvailable()
|
||
{
|
||
var ffmpeg = VideoFrameExtractor.TryCreate();
|
||
if (ffmpeg is null)
|
||
{
|
||
Assert.Inconclusive("ffmpeg non è installato: le miniature dei video senza copertina usano la tessera neutra.");
|
||
}
|
||
|
||
using var temp = new TempFolder();
|
||
var video = temp.Combine("prova.mp4");
|
||
var start = new System.Diagnostics.ProcessStartInfo(ffmpeg.Executable)
|
||
{
|
||
RedirectStandardError = true,
|
||
RedirectStandardOutput = true,
|
||
UseShellExecute = false,
|
||
};
|
||
foreach (var argument in new[] { "-hide_banner", "-nostdin", "-v", "error", "-f", "lavfi", "-i", "testsrc=duration=2:size=640x360:rate=10", "-pix_fmt", "yuv420p", video })
|
||
{
|
||
start.ArgumentList.Add(argument);
|
||
}
|
||
|
||
using (var process = System.Diagnostics.Process.Start(start)!)
|
||
{
|
||
await process.WaitForExitAsync();
|
||
Assert.AreEqual(0, process.ExitCode, "ffmpeg non ha creato il video di prova.");
|
||
}
|
||
|
||
var frame = await ffmpeg.ExtractJpegAsync(video);
|
||
Assert.IsNotNull(frame);
|
||
var store = new ThumbnailStore(temp.Combine("miniature"));
|
||
Assert.IsTrue(store.Create(Make.Sha(6), new MemoryStream(frame)));
|
||
Assert.AreEqual(480, InfoOf(store.PathFor(Make.Sha(6))).Width);
|
||
|
||
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;
|
||
}
|
||
}
|