-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathResizeWebProcessor.cs
276 lines (235 loc) · 9.71 KB
/
ResizeWebProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms;
using SixLabors.ImageSharp.Web.Commands;
namespace SixLabors.ImageSharp.Web.Processors;
/// <summary>
/// Allows the resizing of images.
/// </summary>
public class ResizeWebProcessor : IImageWebProcessor
{
/// <summary>
/// The command constant for the resize width.
/// </summary>
public const string Width = "width";
/// <summary>
/// The command constant for the resize height.
/// </summary>
public const string Height = "height";
/// <summary>
/// The command constant for the resize focal point coordinates.
/// </summary>
public const string Xy = "rxy";
/// <summary>
/// The command constant for the resize mode.
/// </summary>
public const string Mode = "rmode";
/// <summary>
/// The command constant for the resize sampler.
/// </summary>
public const string Sampler = "rsampler";
/// <summary>
/// The command constant for the resize anchor position.
/// </summary>
public const string Anchor = "ranchor";
/// <summary>
/// The command constant for the resize padding background color.
/// </summary>
public const string Color = "rcolor";
/// <summary>
/// The command constant for the resize orientation handling mode.
/// </summary>
public const string Orient = "orient";
/// <summary>
/// The command constant for the resize compand mode.
/// </summary>
public const string Compand = "compand";
private static readonly IEnumerable<string> ResizeCommands
= new[]
{
Width,
Height,
Xy,
Mode,
Sampler,
Anchor,
Color,
Orient,
Compand
};
/// <inheritdoc/>
public IEnumerable<string> Commands { get; } = ResizeCommands;
/// <inheritdoc/>
public FormattedImage Process(
FormattedImage image,
ILogger logger,
CommandCollection commands,
CommandParser parser,
CultureInfo culture)
{
ResizeOptions? options = GetResizeOptions(image, commands, parser, culture);
if (options != null)
{
image.Image.Mutate(x => x.Resize(options));
}
return image;
}
/// <summary>
/// Parses the command collection returning the resize options.
/// </summary>
/// <param name="image">The image to process.</param>
/// <param name="commands">The ordered collection containing the processing commands.</param>
/// <param name="parser">The command parser use for parting commands.</param>
/// <param name="culture">
/// The <see cref="CultureInfo"/> to use as the current parsing culture.
/// </param>
/// <returns>The <see cref="ResizeOptions"/>.</returns>
internal static ResizeOptions? GetResizeOptions(
FormattedImage image,
CommandCollection commands,
CommandParser parser,
CultureInfo culture)
{
if (!commands.Contains(Width) && !commands.Contains(Height))
{
return null;
}
ushort orientation = GetExifOrientation(image, commands, parser, culture);
Size size = ParseSize(orientation, commands, parser, culture);
if (size.Width <= 0 && size.Height <= 0)
{
return null;
}
ResizeMode mode = GetMode(commands, parser, culture);
return new()
{
Size = size,
CenterCoordinates = GetCenter(image, orientation, commands, parser, culture),
Position = GetAnchor(orientation, commands, parser, culture),
Mode = mode,
Compand = GetCompandMode(commands, parser, culture),
Sampler = GetSampler(commands),
PadColor = parser.ParseValue<Color>(commands.GetValueOrDefault(Color), culture),
TargetRectangle = mode is ResizeMode.Manual ? new Rectangle(0, 0, size.Width, size.Height) : null
};
}
/// <inheritdoc/>
public bool RequiresTrueColorPixelFormat(CommandCollection commands, CommandParser parser, CultureInfo culture)
{
ResizeMode mode = parser.ParseValue<ResizeMode>(commands.GetValueOrDefault(Mode), culture);
return mode is ResizeMode.Pad or ResizeMode.BoxPad;
}
private static Size ParseSize(
ushort orientation,
CommandCollection commands,
CommandParser parser,
CultureInfo culture)
{
// The command parser will reject negative numbers as it clamps values to ranges.
int width = (int)parser.ParseValue<uint>(commands.GetValueOrDefault(Width), culture);
int height = (int)parser.ParseValue<uint>(commands.GetValueOrDefault(Height), culture);
return ExifOrientationUtilities.Transform(new Size(width, height), orientation);
}
private static PointF? GetCenter(
FormattedImage image,
ushort orientation,
CommandCollection commands,
CommandParser parser,
CultureInfo culture)
{
float[]? coordinates = parser.ParseValue<float[]>(commands.GetValueOrDefault(Xy), culture);
if (coordinates is null)
{
return null;
}
if (coordinates.Length != 2)
{
return null;
}
// Coordinates for the center point are given as a percentage.
// We must convert these to pixel values for transformation then convert back.
//
// Get the display size of the image after orientation is applied.
Size size = ExifOrientationUtilities.Transform(new Size(image.Image.Width, image.Image.Height), orientation);
Vector2 min = Vector2.Zero;
Vector2 max = new(size.Width, size.Height);
// Scale pixel values up to image height and transform.
Vector2 center = DeScale(new Vector2(coordinates[0], coordinates[1]), min, max);
Vector2 transformed = ExifOrientationUtilities.Transform(center, min, max, orientation);
// Now scale pixel values down as percentage of real image height.
max = new Vector2(image.Image.Width, image.Image.Height);
return Scale(transformed, min, max);
}
private static ResizeMode GetMode(
CommandCollection commands,
CommandParser parser,
CultureInfo culture)
=> parser.ParseValue<ResizeMode>(commands.GetValueOrDefault(Mode), culture);
private static AnchorPositionMode GetAnchor(
ushort orientation,
CommandCollection commands,
CommandParser parser,
CultureInfo culture)
{
AnchorPositionMode anchor = parser.ParseValue<AnchorPositionMode>(commands.GetValueOrDefault(Anchor), culture);
return ExifOrientationUtilities.Transform(anchor, orientation);
}
private static bool GetCompandMode(
CommandCollection commands,
CommandParser parser,
CultureInfo culture)
=> parser.ParseValue<bool>(commands.GetValueOrDefault(Compand), culture);
private static IResampler GetSampler(CommandCollection commands)
{
string? sampler = commands.GetValueOrDefault(Sampler);
if (sampler != null)
{
// No need to do a case test here. Parsed commands are automatically converted to lowercase.
return sampler switch
{
"nearest" or "nearestneighbor" => KnownResamplers.NearestNeighbor,
"box" => KnownResamplers.Box,
"mitchell" or "mitchellnetravali" => KnownResamplers.MitchellNetravali,
"catmull" or "catmullrom" => KnownResamplers.CatmullRom,
"lanczos2" => KnownResamplers.Lanczos2,
"lanczos3" => KnownResamplers.Lanczos3,
"lanczos5" => KnownResamplers.Lanczos5,
"lanczos8" => KnownResamplers.Lanczos8,
"welch" => KnownResamplers.Welch,
"robidoux" => KnownResamplers.Robidoux,
"robidouxsharp" => KnownResamplers.RobidouxSharp,
"spline" => KnownResamplers.Spline,
"triangle" => KnownResamplers.Triangle,
"hermite" => KnownResamplers.Hermite,
_ => KnownResamplers.Bicubic,
};
}
return KnownResamplers.Bicubic;
}
private static ushort GetExifOrientation(FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture)
{
// Browsers now implement 'image-orientation: from-image' by default.
// https://developer.mozilla.org/en-US/docs/web/css/image-orientation
// This makes orientation handling confusing for users who expect images to be resized in accordance
// to what they observe rather than pure (and correct) methods.
//
// To accomodate this we parse the dimensions to use based upon decoded EXIF orientation values.
// We default to 'true' for EXIF orientation handling. By passing 'false' it can be turned off.
if (commands.Contains(Orient) && !parser.ParseValue<bool>(commands.GetValueOrDefault(Orient), culture))
{
return ExifOrientationMode.Unknown;
}
image.TryGetExifOrientation(out ushort orientation);
return orientation;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector2 Scale(Vector2 x, Vector2 min, Vector2 max) => (x - min) / (max - min);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Vector2 DeScale(Vector2 x, Vector2 min, Vector2 max) => min + (x * (max - min));
}