| | | 1 | | using SixLabors.ImageSharp; |
| | | 2 | | |
| | | 3 | | namespace Anichron.Worker.Ingestion.Proxy; |
| | | 4 | | |
| | | 5 | | public enum ProxyEdge |
| | | 6 | | { |
| | | 7 | | LongEdge = 0, |
| | | 8 | | ShortEdge = 1, |
| | | 9 | | } |
| | | 10 | | |
| | | 11 | | internal readonly record struct ProxyBound(ProxyEdge Edge, int Pixels) |
| | | 12 | | { |
| | | 13 | | public Size FitTo(Size source) |
| | 177 | 14 | | { |
| | 177 | 15 | | var widthIsLongEdge = source.Width == Math.Max(source.Width, source.Height); |
| | 177 | 16 | | var boundedIsWidth = Edge == ProxyEdge.LongEdge == widthIsLongEdge; |
| | 177 | 17 | | var (bounded, other) = boundedIsWidth |
| | 177 | 18 | | ? (source.Width, source.Height) |
| | 177 | 19 | | : (source.Height, source.Width); |
| | 177 | 20 | | var target = Math.Min(bounded, Pixels); |
| | | 21 | | |
| | 177 | 22 | | var scaledOther = Math.Max(1, (int)Math.Round((double)other * target / bounded, MidpointRounding.AwayFromZero)); |
| | 177 | 23 | | return boundedIsWidth ? new Size(target, scaledOther) : new Size(scaledOther, target); |
| | 177 | 24 | | } |
| | | 25 | | } |