|
|
|
@ -6,27 +6,16 @@ using SixLabors.ImageSharp.Metadata.Profiles.Exif; |
|
|
|
using SixLabors.ImageSharp.PixelFormats; |
|
|
|
using SixLabors.ImageSharp.Processing; |
|
|
|
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; |
|
|
|
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison; |
|
|
|
|
|
|
|
using Xunit; |
|
|
|
|
|
|
|
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms |
|
|
|
{ |
|
|
|
public class AutoOrientTests : FileTestBase |
|
|
|
[GroupOutput("Transforms")] |
|
|
|
public class AutoOrientTests |
|
|
|
{ |
|
|
|
public static readonly string[] FlipFiles = { TestImages.Bmp.F }; |
|
|
|
|
|
|
|
public static readonly TheoryData<RotateMode, FlipMode, ushort> OrientationValues |
|
|
|
= new TheoryData<RotateMode, FlipMode, ushort> |
|
|
|
{ |
|
|
|
{ RotateMode.None, FlipMode.None, 0 }, |
|
|
|
{ RotateMode.None, FlipMode.None, 1 }, |
|
|
|
{ RotateMode.None, FlipMode.Horizontal, 2 }, |
|
|
|
{ RotateMode.Rotate180, FlipMode.None, 3 }, |
|
|
|
{ RotateMode.Rotate180, FlipMode.Horizontal, 4 }, |
|
|
|
{ RotateMode.Rotate90, FlipMode.Horizontal, 5 }, |
|
|
|
{ RotateMode.Rotate270, FlipMode.None, 6 }, |
|
|
|
{ RotateMode.Rotate90, FlipMode.Vertical, 7 }, |
|
|
|
{ RotateMode.Rotate90, FlipMode.None, 8 }, |
|
|
|
}; |
|
|
|
public const string FlipTestFile = TestImages.Bmp.F; |
|
|
|
|
|
|
|
public static readonly TheoryData<ExifDataType, byte[]> InvalidOrientationValues |
|
|
|
= new TheoryData<ExifDataType, byte[]> |
|
|
|
@ -38,27 +27,38 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms |
|
|
|
{ ExifDataType.SignedLong, BitConverter.GetBytes((int) 5) } |
|
|
|
}; |
|
|
|
|
|
|
|
public static readonly TheoryData<ushort> ExifOrientationValues = new TheoryData<ushort>() |
|
|
|
{ |
|
|
|
0, |
|
|
|
1, |
|
|
|
2, |
|
|
|
3, |
|
|
|
4, |
|
|
|
5, |
|
|
|
6, |
|
|
|
7, |
|
|
|
8 |
|
|
|
}; |
|
|
|
|
|
|
|
[Theory] |
|
|
|
[WithFileCollection(nameof(FlipFiles), nameof(OrientationValues), DefaultPixelType)] |
|
|
|
public void ImageShouldAutoRotate<TPixel>(TestImageProvider<TPixel> provider, RotateMode rotateType, FlipMode flipType, ushort orientation) |
|
|
|
[WithFile(FlipTestFile, nameof(ExifOrientationValues), PixelTypes.Rgba32)] |
|
|
|
public void AutoOrient_WorksForAllExifOrientations<TPixel>(TestImageProvider<TPixel> provider, ushort orientation) |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
{ |
|
|
|
using (Image<TPixel> image = provider.GetImage()) |
|
|
|
{ |
|
|
|
image.Metadata.ExifProfile = new ExifProfile(); |
|
|
|
image.Metadata.ExifProfile.SetValue(ExifTag.Orientation, orientation); |
|
|
|
|
|
|
|
image.Mutate(x => x.RotateFlip(rotateType, flipType)); |
|
|
|
image.DebugSave(provider, string.Join("_", rotateType, flipType, orientation, "1_before")); |
|
|
|
|
|
|
|
|
|
|
|
image.Mutate(x => x.AutoOrient()); |
|
|
|
image.DebugSave(provider, string.Join("_", rotateType, flipType, orientation, "2_after")); |
|
|
|
image.DebugSave(provider, orientation, appendPixelTypeToFileName: false); |
|
|
|
image.CompareToReferenceOutput(provider, orientation, appendPixelTypeToFileName: false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
[Theory] |
|
|
|
[WithFileCollection(nameof(FlipFiles), nameof(InvalidOrientationValues), DefaultPixelType)] |
|
|
|
public void ImageShouldAutoRotateInvalidValues<TPixel>(TestImageProvider<TPixel> provider, ExifDataType dataType, byte[] orientation) |
|
|
|
[WithFile(FlipTestFile, nameof(InvalidOrientationValues), PixelTypes.Rgba32)] |
|
|
|
public void AutoOrient_WorksWithCorruptExifData<TPixel>(TestImageProvider<TPixel> provider, ExifDataType dataType, byte[] orientation) |
|
|
|
where TPixel : struct, IPixel<TPixel> |
|
|
|
{ |
|
|
|
var profile = new ExifProfile(); |
|
|
|
@ -72,11 +72,19 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Transforms |
|
|
|
bytes[18] = (byte)dataType; |
|
|
|
// Change the number of components
|
|
|
|
bytes[20] = 1; |
|
|
|
|
|
|
|
byte[] orientationCodeData = new byte[8]; |
|
|
|
Array.Copy(orientation, orientationCodeData, orientation.Length); |
|
|
|
|
|
|
|
ulong orientationCode = BitConverter.ToUInt64(orientationCodeData, 0); |
|
|
|
|
|
|
|
using (Image<TPixel> image = provider.GetImage()) |
|
|
|
using (Image<TPixel> reference = image.Clone()) |
|
|
|
{ |
|
|
|
image.Metadata.ExifProfile = new ExifProfile(bytes); |
|
|
|
image.Mutate(x => x.AutoOrient()); |
|
|
|
image.DebugSave(provider, $"{dataType}-{orientationCode}", appendPixelTypeToFileName: false); |
|
|
|
ImageComparer.Exact.VerifySimilarity(image, reference); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|