Unione di funzione/codec-immagini in funzione/decoder-h264 (catena della 0.3.0)
Porta nel decodificatore H.264 le regole senza componenti esterni, la sessione S4b e i codec di immagini di casa (C1), così la richiesta di unione di questo ramo contiene già tutta la catena. Conflitti risolti tenendo le due parti: in decisioni.md tutte le righe, prima quelle della catena e poi quelle del decodificatore; in docker.md, processo.md e Sviluppo.md né ffmpeg (uscito con questo ramo) né SkiaSharp (uscito con C1), con l'eccezione «da fare» delle prove di conformità; nelle prove di Febe.Tests entrambe le cartelle di file copiati e, nelle prove delle miniature, sia i video sia le immagini di riferimento. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
8bcc86e496
279 files changed
+25617
-816
No files matched your search
@@ -1,4 +1,5 @@
|
||||
using SkiaSharp;
|
||||
using Febe.Core.Codecs.Images;
|
||||
using Febe.Core.Drawing;
|
||||
|
||||
namespace Febe.Core.Library;
|
||||
|
||||
@@ -54,13 +55,14 @@ public sealed class ThumbnailStore
|
||||
return true;
|
||||
}
|
||||
|
||||
using var bitmap = ImageDecoding.DecodeUpright(encodedImage, LongSide);
|
||||
if (bitmap is null)
|
||||
using var image = DecodeFor(encodedImage, LongSide);
|
||||
if (image is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
WriteJpeg(target, Render(bitmap, LongSide));
|
||||
using var thumbnail = Render(image, LongSide);
|
||||
WriteJpeg(target, thumbnail);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -94,26 +96,20 @@ public sealed class ThumbnailStore
|
||||
}
|
||||
|
||||
const int width = 270, height = LongSide; // 9:16, like stories and reels
|
||||
using var surface = SKSurface.Create(new SKImageInfo(width, height, SKColorType.Rgba8888, SKAlphaType.Premul));
|
||||
var canvas = surface.Canvas;
|
||||
canvas.Clear(new SKColor(0xE4, 0xE4, 0xE4));
|
||||
using (var paint = new SKPaint { Color = new SKColor(0x9A, 0x9A, 0x9A), IsAntialias = true, Style = SKPaintStyle.Fill })
|
||||
using var tile = new RgbaImage(width, height);
|
||||
using (var canvas = new Canvas(tile))
|
||||
{
|
||||
canvas.DrawCircle(width / 2f, height / 2f, 34, paint);
|
||||
}
|
||||
var background = new Color(0xE4, 0xE4, 0xE4);
|
||||
canvas.Clear(background);
|
||||
canvas.FillCircle(width / 2f, height / 2f, 34, new Color(0x9A, 0x9A, 0x9A));
|
||||
|
||||
using (var paint = new SKPaint { Color = new SKColor(0xE4, 0xE4, 0xE4), IsAntialias = true, Style = SKPaintStyle.Fill })
|
||||
{
|
||||
// A play triangle drawn as three thin rectangles would be blurry; vertices keep it crisp.
|
||||
var cx = width / 2f + 4;
|
||||
// The play sign: a triangle pointing right, a little right of the centre to look centred.
|
||||
var cx = (width / 2f) + 4;
|
||||
var cy = height / 2f;
|
||||
SKPoint[] triangle = [new(cx - 10, cy - 15), new(cx - 10, cy + 15), new(cx + 15, cy)];
|
||||
canvas.DrawVertices(SKVertexMode.Triangles, triangle, null, null, paint);
|
||||
canvas.Fill(new PathBuilder().AddPolygon([cx - 10, cy - 15, cx - 10, cy + 15, cx + 15, cy]), background);
|
||||
}
|
||||
|
||||
using var image = surface.Snapshot();
|
||||
using var bitmap = SKBitmap.FromImage(image);
|
||||
WriteJpeg(path, bitmap.Copy());
|
||||
WriteJpeg(path, tile);
|
||||
return path;
|
||||
}
|
||||
|
||||
@@ -125,67 +121,66 @@ public sealed class ThumbnailStore
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(source);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(longSide, 16);
|
||||
using var decoded = ImageDecoding.DecodeUpright(source, longSide);
|
||||
using var decoded = DecodeFor(source, longSide);
|
||||
if (decoded is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var small = Render(decoded, longSide);
|
||||
using var pixmap = small.PeekPixels();
|
||||
if (pixmap is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var data = pixmap.Encode(new SKJpegEncoderOptions(JpegQuality));
|
||||
return data?.ToArray();
|
||||
return Jpeg(small);
|
||||
}
|
||||
|
||||
private static SKBitmap Render(SKBitmap source, int longSide)
|
||||
/// <summary>
|
||||
/// Decodes an image upright for a picture of <paramref name="longSide"/> pixels. A JPEG may be shrunk while decoding,
|
||||
/// but only down to twice that size: the last step is left to Lanczos, since the codec's 8 × 8 block averages would
|
||||
/// show at almost 1:1. Null when the content cannot be decoded.
|
||||
/// </summary>
|
||||
internal static RgbaImage? DecodeFor(Stream encodedImage, int longSide) =>
|
||||
ImageDecoding.DecodeUpright(encodedImage, 2 * longSide);
|
||||
|
||||
/// <summary>
|
||||
/// The image reduced to <paramref name="longSide"/> on its long side (Lanczos, never enlarged) and seen over white,
|
||||
/// since JPEG has no alpha. Always a new pooled image, which the caller disposes.
|
||||
/// </summary>
|
||||
internal static RgbaImage Render(RgbaImage source, int longSide)
|
||||
{
|
||||
var scale = Math.Min(1.0, (double)longSide / Math.Max(source.Width, source.Height));
|
||||
var width = Math.Max(1, (int)Math.Round(source.Width * scale));
|
||||
var height = Math.Max(1, (int)Math.Round(source.Height * scale));
|
||||
var result = new SKBitmap(new SKImageInfo(width, height, SKColorType.Rgba8888, SKAlphaType.Premul));
|
||||
using var canvas = new SKCanvas(result);
|
||||
canvas.Clear(SKColors.White); // JPEG has no alpha: transparent areas become white
|
||||
using var image = SKImage.FromBitmap(source);
|
||||
canvas.DrawImage(image, new SKRect(0, 0, width, height), new SKSamplingOptions(SKCubicResampler.Mitchell), null);
|
||||
var (width, height) = ImageResampler.FitWithin(source.Width, source.Height, longSide);
|
||||
var result = ImageResampler.Resize(source, width, height, pooled: true);
|
||||
ImageBackground.Flatten(result, Color.White);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void WriteJpeg(string target, SKBitmap bitmap)
|
||||
/// <summary>The JPEG of a thumbnail: quality <see cref="JpegQuality"/>, chroma 4:2:0.</summary>
|
||||
internal static byte[] Jpeg(RgbaImage image) =>
|
||||
JpegEncoder.Encode(image, new JpegEncoderOptions { Quality = JpegQuality, Subsampling = JpegSubsampling.Ratio420 });
|
||||
|
||||
private static void WriteJpeg(string target, RgbaImage image)
|
||||
{
|
||||
using (bitmap)
|
||||
var data = Jpeg(image);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
|
||||
var temp = target + "." + Guid.NewGuid().ToString("N")[..8] + ".tmp";
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
|
||||
var temp = target + "." + Guid.NewGuid().ToString("N")[..8] + ".tmp";
|
||||
using (var file = new FileStream(temp, FileMode.CreateNew, FileAccess.Write))
|
||||
{
|
||||
file.Write(data);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using (var pixmap = bitmap.PeekPixels() ?? throw new InvalidOperationException("Miniatura senza pixel."))
|
||||
using (var data = pixmap.Encode(new SKJpegEncoderOptions(JpegQuality))
|
||||
?? throw new InvalidOperationException("Codifica JPEG della miniatura non riuscita."))
|
||||
using (var file = new FileStream(temp, FileMode.CreateNew, FileAccess.Write))
|
||||
{
|
||||
data.SaveTo(file);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
File.Move(temp, target, overwrite: false);
|
||||
}
|
||||
catch (IOException) when (File.Exists(target))
|
||||
{
|
||||
// Created meanwhile by another task: same content.
|
||||
}
|
||||
File.Move(temp, target, overwrite: false);
|
||||
}
|
||||
finally
|
||||
catch (IOException) when (File.Exists(target))
|
||||
{
|
||||
if (File.Exists(temp))
|
||||
{
|
||||
File.Delete(temp);
|
||||
}
|
||||
// Created meanwhile by another task: same content.
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (File.Exists(temp))
|
||||
{
|
||||
File.Delete(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user