| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using Anichron.Worker.Settings; |
| | | 3 | | using Microsoft.Extensions.Options; |
| | | 4 | | using SixLabors.ImageSharp; |
| | | 5 | | using SixLabors.ImageSharp.PixelFormats; |
| | | 6 | | |
| | | 7 | | namespace Anichron.Worker.Ingestion.Proxy; |
| | | 8 | | |
| | | 9 | | internal interface IImageProxyGenerator |
| | | 10 | | { |
| | | 11 | | string FileName { get; } |
| | | 12 | | ProxyType ProxyType { get; } |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// The most pixels this generator uses. It receives its own copy of the upright source, already shrunk |
| | | 16 | | /// to fit this bound, and may modify that copy freely. |
| | | 17 | | /// </summary> |
| | | 18 | | ProxyBound Bound { get; } |
| | | 19 | | |
| | | 20 | | Task<byte[]> GenerateAsync(Image<Rgb24> image, CancellationToken ct); |
| | | 21 | | } |
| | | 22 | | |
| | 64 | 23 | | internal sealed class ThumbnailGenerator(IImageProcessor imageProcessor, IOptions<WorkerSettings> options) |
| | | 24 | | : IImageProxyGenerator |
| | | 25 | | { |
| | 54 | 26 | | public string FileName => "thumbnail.jpg"; |
| | 54 | 27 | | public ProxyType ProxyType => ProxyType.Thumbnail; |
| | 54 | 28 | | public ProxyBound Bound => new(ProxyEdge.ShortEdge, options.Value.ThumbnailShortEdge); |
| | | 29 | | public Task<byte[]> GenerateAsync(Image<Rgb24> image, CancellationToken ct) |
| | 54 | 30 | | => imageProcessor.EncodeThumbnailAsync(image, ct); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | internal sealed class FullPreviewGenerator(IImageProcessor imageProcessor, IOptions<WorkerSettings> options) |
| | | 34 | | : IImageProxyGenerator |
| | | 35 | | { |
| | | 36 | | public string FileName => "preview.jpg"; |
| | | 37 | | public ProxyType ProxyType => ProxyType.FullPreview; |
| | | 38 | | public ProxyBound Bound => new(ProxyEdge.LongEdge, options.Value.PreviewLongEdge); |
| | | 39 | | public Task<byte[]> GenerateAsync(Image<Rgb24> image, CancellationToken ct) |
| | | 40 | | => imageProcessor.EncodeFullPreviewAsync(image, ct); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | internal sealed class BlurhashEncoder(IImageProcessor imageProcessor, IOptions<WorkerSettings> options) |
| | | 44 | | { |
| | | 45 | | public ProxyBound Bound => new(ProxyEdge.LongEdge, options.Value.BlurhashSampleLongEdge); |
| | | 46 | | public Task<string> EncodeAsync(Image<Rgb24> image, CancellationToken ct) |
| | | 47 | | => imageProcessor.ComputeBlurhashAsync(image, ct); |
| | | 48 | | } |