|
|
@ -1,15 +1,11 @@ |
|
|
// Copyright (c) Six Labors.
|
|
|
// Copyright (c) Six Labors.
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
using System.IO; |
|
|
using System.IO; |
|
|
using System.Threading; |
|
|
using System.Threading; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using SixLabors.ImageSharp.Formats.Jpeg; |
|
|
using SixLabors.ImageSharp.Formats.Jpeg; |
|
|
using SixLabors.ImageSharp.Metadata; |
|
|
using SixLabors.ImageSharp.Metadata; |
|
|
using SixLabors.ImageSharp.Metadata.Profiles.Exif; |
|
|
|
|
|
using SixLabors.ImageSharp.Metadata.Profiles.Icc; |
|
|
|
|
|
using SixLabors.ImageSharp.Metadata.Profiles.Iptc; |
|
|
|
|
|
using SixLabors.ImageSharp.PixelFormats; |
|
|
using SixLabors.ImageSharp.PixelFormats; |
|
|
using SixLabors.ImageSharp.Processing; |
|
|
using SixLabors.ImageSharp.Processing; |
|
|
using SixLabors.ImageSharp.Tests.TestUtilities; |
|
|
using SixLabors.ImageSharp.Tests.TestUtilities; |
|
|
@ -63,6 +59,29 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg |
|
|
{ TestImages.Jpeg.Baseline.GammaDalaiLamaGray, 72, 72, PixelResolutionUnit.PixelsPerInch } |
|
|
{ TestImages.Jpeg.Baseline.GammaDalaiLamaGray, 72, 72, PixelResolutionUnit.PixelsPerInch } |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Quality_1_And_100_Are_Not_Identical() |
|
|
|
|
|
{ |
|
|
|
|
|
var options = new JpegEncoder |
|
|
|
|
|
{ |
|
|
|
|
|
Quality = 1 |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var testFile = TestFile.Create(TestImages.Jpeg.Baseline.Calliphora); |
|
|
|
|
|
|
|
|
|
|
|
using (Image<Rgba32> input = testFile.CreateRgba32Image()) |
|
|
|
|
|
using (var memStream0 = new MemoryStream()) |
|
|
|
|
|
using (var memStream1 = new MemoryStream()) |
|
|
|
|
|
{ |
|
|
|
|
|
input.SaveAsJpeg(memStream0, options); |
|
|
|
|
|
|
|
|
|
|
|
options.Quality = 100; |
|
|
|
|
|
input.SaveAsJpeg(memStream1, options); |
|
|
|
|
|
|
|
|
|
|
|
Assert.NotEqual(memStream0.ToArray(), memStream1.ToArray()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
[Theory] |
|
|
[Theory] |
|
|
[WithFile(TestImages.Jpeg.Baseline.Floorplan, PixelTypes.Rgba32, JpegEncodingColor.Luminance)] |
|
|
[WithFile(TestImages.Jpeg.Baseline.Floorplan, PixelTypes.Rgba32, JpegEncodingColor.Luminance)] |
|
|
[WithFile(TestImages.Jpeg.Baseline.Jpeg444, PixelTypes.Rgba32, JpegEncodingColor.YCbCrRatio444)] |
|
|
[WithFile(TestImages.Jpeg.Baseline.Jpeg444, PixelTypes.Rgba32, JpegEncodingColor.YCbCrRatio444)] |
|
|
@ -169,36 +188,21 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg |
|
|
TestJpegEncoderCore(provider, colorType, 100, comparer); |
|
|
TestJpegEncoderCore(provider, colorType, 100, comparer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
[Theory] |
|
|
/// Anton's SUPER-SCIENTIFIC tolerance threshold calculation
|
|
|
[WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.L8, JpegEncodingColor.Luminance)] |
|
|
/// </summary>
|
|
|
[WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgb24, JpegEncodingColor.Rgb)] |
|
|
private static ImageComparer GetComparer(int quality, JpegEncodingColor? colorType) |
|
|
[WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgb24, JpegEncodingColor.Cmyk)] |
|
|
{ |
|
|
[WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio444)] |
|
|
float tolerance = 0.015f; // ~1.5%
|
|
|
[WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio422)] |
|
|
|
|
|
[WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio420)] |
|
|
if (quality < 50) |
|
|
[WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio411)] |
|
|
{ |
|
|
[WithFile(TestImages.Jpeg.Baseline.Calliphora, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio410)] |
|
|
tolerance *= 4.5f; |
|
|
public void EncodeBaseline_WorksWithAllColorTypes<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType) |
|
|
} |
|
|
|
|
|
else if (quality < 75 || colorType == JpegEncodingColor.YCbCrRatio420) |
|
|
|
|
|
{ |
|
|
|
|
|
tolerance *= 2.0f; |
|
|
|
|
|
if (colorType == JpegEncodingColor.YCbCrRatio420) |
|
|
|
|
|
{ |
|
|
|
|
|
tolerance *= 2.0f; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ImageComparer.Tolerant(tolerance); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void TestJpegEncoderCore<TPixel>( |
|
|
|
|
|
TestImageProvider<TPixel> provider, |
|
|
|
|
|
JpegEncodingColor colorType = JpegEncodingColor.YCbCrRatio420, |
|
|
|
|
|
int quality = 100, |
|
|
|
|
|
ImageComparer comparer = null) |
|
|
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
{ |
|
|
{ |
|
|
|
|
|
// all reference output images are saved with quality=100
|
|
|
|
|
|
const int quality = 100; |
|
|
|
|
|
|
|
|
using Image<TPixel> image = provider.GetImage(); |
|
|
using Image<TPixel> image = provider.GetImage(); |
|
|
|
|
|
|
|
|
// There is no alpha in Jpeg!
|
|
|
// There is no alpha in Jpeg!
|
|
|
@ -211,33 +215,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg |
|
|
}; |
|
|
}; |
|
|
string info = $"{colorType}-Q{quality}"; |
|
|
string info = $"{colorType}-Q{quality}"; |
|
|
|
|
|
|
|
|
comparer ??= GetComparer(quality, colorType); |
|
|
ImageComparer comparer = GetComparer(quality, colorType); |
|
|
|
|
|
|
|
|
// Does DebugSave & load reference CompareToReferenceInput():
|
|
|
// Does DebugSave & load reference CompareToReferenceInput():
|
|
|
image.VerifyEncoder(provider, "jpeg", info, encoder, comparer, referenceImageExtension: "png"); |
|
|
image.VerifyEncoder(provider, "jpeg", info, encoder, comparer, referenceImageExtension: "jpg"); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Quality_1_And_100_Are_Not_Identical() |
|
|
|
|
|
{ |
|
|
|
|
|
var options = new JpegEncoder |
|
|
|
|
|
{ |
|
|
|
|
|
Quality = 1 |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var testFile = TestFile.Create(TestImages.Jpeg.Baseline.Calliphora); |
|
|
|
|
|
|
|
|
|
|
|
using (Image<Rgba32> input = testFile.CreateRgba32Image()) |
|
|
|
|
|
using (var memStream0 = new MemoryStream()) |
|
|
|
|
|
using (var memStream1 = new MemoryStream()) |
|
|
|
|
|
{ |
|
|
|
|
|
input.SaveAsJpeg(memStream0, options); |
|
|
|
|
|
|
|
|
|
|
|
options.Quality = 100; |
|
|
|
|
|
input.SaveAsJpeg(memStream1, options); |
|
|
|
|
|
|
|
|
|
|
|
Assert.NotEqual(memStream0.ToArray(), memStream1.ToArray()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Theory] |
|
|
[Theory] |
|
|
@ -263,68 +244,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Encode_PreservesIptcProfile() |
|
|
|
|
|
{ |
|
|
|
|
|
// arrange
|
|
|
|
|
|
using var input = new Image<Rgba32>(1, 1); |
|
|
|
|
|
input.Metadata.IptcProfile = new IptcProfile(); |
|
|
|
|
|
input.Metadata.IptcProfile.SetValue(IptcTag.Byline, "unit_test"); |
|
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
|
using var memStream = new MemoryStream(); |
|
|
|
|
|
input.Save(memStream, JpegEncoder); |
|
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
|
memStream.Position = 0; |
|
|
|
|
|
using var output = Image.Load<Rgba32>(memStream); |
|
|
|
|
|
IptcProfile actual = output.Metadata.IptcProfile; |
|
|
|
|
|
Assert.NotNull(actual); |
|
|
|
|
|
IEnumerable<IptcValue> values = input.Metadata.IptcProfile.Values; |
|
|
|
|
|
Assert.Equal(values, actual.Values); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Encode_PreservesExifProfile() |
|
|
|
|
|
{ |
|
|
|
|
|
// arrange
|
|
|
|
|
|
using var input = new Image<Rgba32>(1, 1); |
|
|
|
|
|
input.Metadata.ExifProfile = new ExifProfile(); |
|
|
|
|
|
input.Metadata.ExifProfile.SetValue(ExifTag.Software, "unit_test"); |
|
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
|
using var memStream = new MemoryStream(); |
|
|
|
|
|
input.Save(memStream, JpegEncoder); |
|
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
|
memStream.Position = 0; |
|
|
|
|
|
using var output = Image.Load<Rgba32>(memStream); |
|
|
|
|
|
ExifProfile actual = output.Metadata.ExifProfile; |
|
|
|
|
|
Assert.NotNull(actual); |
|
|
|
|
|
IReadOnlyList<IExifValue> values = input.Metadata.ExifProfile.Values; |
|
|
|
|
|
Assert.Equal(values, actual.Values); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Encode_PreservesIccProfile() |
|
|
|
|
|
{ |
|
|
|
|
|
// arrange
|
|
|
|
|
|
using var input = new Image<Rgba32>(1, 1); |
|
|
|
|
|
input.Metadata.IccProfile = new IccProfile(IccTestDataProfiles.Profile_Random_Array); |
|
|
|
|
|
|
|
|
|
|
|
// act
|
|
|
|
|
|
using var memStream = new MemoryStream(); |
|
|
|
|
|
input.Save(memStream, JpegEncoder); |
|
|
|
|
|
|
|
|
|
|
|
// assert
|
|
|
|
|
|
memStream.Position = 0; |
|
|
|
|
|
using var output = Image.Load<Rgba32>(memStream); |
|
|
|
|
|
IccProfile actual = output.Metadata.IccProfile; |
|
|
|
|
|
Assert.NotNull(actual); |
|
|
|
|
|
IccProfile values = input.Metadata.IccProfile; |
|
|
|
|
|
Assert.Equal(values.Entries, actual.Entries); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Theory] |
|
|
[Theory] |
|
|
[InlineData(JpegEncodingColor.YCbCrRatio420)] |
|
|
[InlineData(JpegEncodingColor.YCbCrRatio420)] |
|
|
[InlineData(JpegEncodingColor.YCbCrRatio444)] |
|
|
[InlineData(JpegEncodingColor.YCbCrRatio444)] |
|
|
@ -354,5 +273,53 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg |
|
|
await image.SaveAsync(pausedStream, encoder, cts.Token); |
|
|
await image.SaveAsync(pausedStream, encoder, cts.Token); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Anton's SUPER-SCIENTIFIC tolerance threshold calculation
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static ImageComparer GetComparer(int quality, JpegEncodingColor? colorType) |
|
|
|
|
|
{ |
|
|
|
|
|
float tolerance = 0.015f; // ~1.5%
|
|
|
|
|
|
|
|
|
|
|
|
if (quality < 50) |
|
|
|
|
|
{ |
|
|
|
|
|
tolerance *= 4.5f; |
|
|
|
|
|
} |
|
|
|
|
|
else if (quality < 75 || colorType == JpegEncodingColor.YCbCrRatio420) |
|
|
|
|
|
{ |
|
|
|
|
|
tolerance *= 2.0f; |
|
|
|
|
|
if (colorType == JpegEncodingColor.YCbCrRatio420) |
|
|
|
|
|
{ |
|
|
|
|
|
tolerance *= 2.0f; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ImageComparer.Tolerant(tolerance); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static void TestJpegEncoderCore<TPixel>( |
|
|
|
|
|
TestImageProvider<TPixel> provider, |
|
|
|
|
|
JpegEncodingColor colorType = JpegEncodingColor.YCbCrRatio420, |
|
|
|
|
|
int quality = 100, |
|
|
|
|
|
ImageComparer comparer = null) |
|
|
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
|
|
{ |
|
|
|
|
|
using Image<TPixel> image = provider.GetImage(); |
|
|
|
|
|
|
|
|
|
|
|
// There is no alpha in Jpeg!
|
|
|
|
|
|
image.Mutate(c => c.MakeOpaque()); |
|
|
|
|
|
|
|
|
|
|
|
var encoder = new JpegEncoder |
|
|
|
|
|
{ |
|
|
|
|
|
Quality = quality, |
|
|
|
|
|
ColorType = colorType |
|
|
|
|
|
}; |
|
|
|
|
|
string info = $"{colorType}-Q{quality}"; |
|
|
|
|
|
|
|
|
|
|
|
comparer ??= GetComparer(quality, colorType); |
|
|
|
|
|
|
|
|
|
|
|
// Does DebugSave & load reference CompareToReferenceInput():
|
|
|
|
|
|
image.VerifyEncoder(provider, "jpeg", info, encoder, comparer, referenceImageExtension: "png"); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|