Skip to content

Commit 17ef18c

Browse files
Use TryGetExifOrientation
1 parent f2b3594 commit 17ef18c

2 files changed

Lines changed: 40 additions & 29 deletions

File tree

src/ImageSharp.Web/FormattedImage.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,39 +119,38 @@ public static async Task<FormattedImage> LoadAsync(Configuration configuration,
119119
public Task SaveAsync(Stream destination) => this.Image.SaveAsync(destination, this.encoder);
120120

121121
/// <summary>
122-
/// Returns a value indicating whether the source image contains EXIF metadata for <see cref="ExifTag.Orientation"/>
123-
/// indicating that the image is rotated (not flipped).
122+
/// Gets the EXIF orientation metata for the <see cref="FormattedImage"/>.
124123
/// </summary>
125-
/// <param name="orientation">The decoded orientation. Use <see cref="ExifOrientationMode"/> for comparison.</param>
126-
/// <returns>The <see cref="bool"/> indicating whether the image contains EXIF metadata indicating that the image is rotated.</returns>
127-
public bool IsExifRotated(out ushort orientation)
124+
/// <param name="value">
125+
/// When this method returns, contains the value parsed from decoded EXIF metadata; otherwise,
126+
/// the default value for the type of the <paramref name="value"/> parameter.
127+
/// This parameter is passed uninitialized. Use <see cref="ExifOrientationMode"/> for comparison.
128+
/// </param>
129+
/// <returns>
130+
/// <see langword="true"/> if the <see cref="FormattedImage"/> contains EXIF orientation metadata
131+
/// for <see cref="ExifTag.Orientation"/>; otherwise, <see langword="false"/>.
132+
/// </returns>
133+
public bool TryGetExifOrientation(out ushort value)
128134
{
129-
orientation = ExifOrientationMode.Unknown;
135+
value = ExifOrientationMode.Unknown;
130136
if (this.Image.Metadata.ExifProfile != null)
131137
{
132-
IExifValue<ushort> value = this.Image.Metadata.ExifProfile.GetValue(ExifTag.Orientation);
133-
if (value is null)
138+
IExifValue<ushort> orientation = this.Image.Metadata.ExifProfile.GetValue(ExifTag.Orientation);
139+
if (orientation is null)
134140
{
135141
return false;
136142
}
137143

138-
if (value.DataType == ExifDataType.Short)
144+
if (orientation.DataType == ExifDataType.Short)
139145
{
140-
orientation = value.Value;
146+
value = orientation.Value;
141147
}
142148
else
143149
{
144-
orientation = Convert.ToUInt16(value.Value);
150+
value = Convert.ToUInt16(orientation.Value);
145151
}
146152

147-
return orientation switch
148-
{
149-
ExifOrientationMode.LeftTop
150-
or ExifOrientationMode.RightTop
151-
or ExifOrientationMode.RightBottom
152-
or ExifOrientationMode.LeftBottom => true,
153-
_ => false,
154-
};
153+
return true;
155154
}
156155

157156
return false;

src/ImageSharp.Web/Processors/ResizeWebProcessor.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ internal static ResizeOptions GetResizeOptions(
112112
return null;
113113
}
114114

115-
bool rotated = ShouldHandleExifRotation(image, commands, parser, culture, out ushort orientation);
115+
ushort orientation = GetExifOrientation(image, commands, parser, culture);
116116

117-
Size size = ParseSize(rotated, commands, parser, culture);
117+
Size size = ParseSize(orientation, commands, parser, culture);
118118

119119
if (size.Width <= 0 && size.Height <= 0)
120120
{
@@ -137,15 +137,18 @@ internal static ResizeOptions GetResizeOptions(
137137
}
138138

139139
private static Size ParseSize(
140-
bool rotated,
140+
ushort orientation,
141141
CommandCollection commands,
142142
CommandParser parser,
143143
CultureInfo culture)
144144
{
145145
// The command parser will reject negative numbers as it clamps values to ranges.
146146
int width = (int)parser.ParseValue<uint>(commands.GetValueOrDefault(Width), culture);
147147
int height = (int)parser.ParseValue<uint>(commands.GetValueOrDefault(Height), culture);
148-
return rotated ? new Size(height, width) : new Size(width, height);
148+
149+
return IsExifOrientationRotated(orientation)
150+
? new Size(height, width)
151+
: new Size(width, height);
149152
}
150153

151154
private static PointF? GetCenter(
@@ -345,23 +348,32 @@ private static IResampler GetSampler(CommandCollection commands)
345348
return KnownResamplers.Bicubic;
346349
}
347350

348-
private static bool ShouldHandleExifRotation(FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture, out ushort orientation)
351+
private static ushort GetExifOrientation(FormattedImage image, CommandCollection commands, CommandParser parser, CultureInfo culture)
349352
{
350353
// Browsers now implement 'image-orientation: from-image' by default.
351354
// https://developer.mozilla.org/en-US/docs/web/css/image-orientation
352355
// This makes orientation handling confusing for users who expect images to be resized in accordance
353356
// to what they observe rather than pure (and correct) methods.
354357
//
355-
// To accomodate this we parse the dimensions to use based upon decoded EXIF orientation values, switching
356-
// the width/height parameters when images are rotated (not flipped).
358+
// To accomodate this we parse the dimensions to use based upon decoded EXIF orientation values.
357359
// We default to 'true' for EXIF orientation handling. By passing 'false' it can be turned off.
358360
if (commands.Contains(Orient) && !parser.ParseValue<bool>(commands.GetValueOrDefault(Orient), culture))
359361
{
360-
orientation = ExifOrientationMode.Unknown;
361-
return false;
362+
return ExifOrientationMode.Unknown;
362363
}
363364

364-
return image.IsExifRotated(out orientation);
365+
image.TryGetExifOrientation(out ushort orientation);
366+
return orientation;
365367
}
368+
369+
private static bool IsExifOrientationRotated(ushort orientation)
370+
=> orientation switch
371+
{
372+
ExifOrientationMode.LeftTop
373+
or ExifOrientationMode.RightTop
374+
or ExifOrientationMode.RightBottom
375+
or ExifOrientationMode.LeftBottom => true,
376+
_ => false,
377+
};
366378
}
367379
}

0 commit comments

Comments
 (0)