Browse Source

Rename to JpegColorType

pull/2751/head
James Jackson-South 2 years ago
parent
commit
3d80dfcbac
  1. 4
      src/ImageSharp/Formats/Jpeg/Components/Encoder/EncodingConfigs/JpegFrameConfig.cs
  2. 6
      src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs
  3. 2
      src/ImageSharp/Formats/Jpeg/JpegColorType.cs
  4. 24
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  5. 2
      src/ImageSharp/Formats/Jpeg/JpegEncoder.cs
  6. 18
      src/ImageSharp/Formats/Jpeg/JpegEncoderCore.FrameConfig.cs
  7. 2
      src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
  8. 24
      src/ImageSharp/Formats/Jpeg/JpegMetadata.cs
  9. 2
      src/ImageSharp/Formats/Tiff/Compression/Compressors/TiffJpegCompressor.cs
  10. 2
      tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegComparison.cs
  11. 12
      tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegFeatures.cs
  12. 28
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs
  13. 10
      tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs
  14. 112
      tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs
  15. 4
      tests/ImageSharp.Tests/Formats/Jpg/JpegMetadataTests.cs

4
src/ImageSharp/Formats/Jpeg/Components/Encoder/EncodingConfigs/JpegFrameConfig.cs

@ -5,7 +5,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Encoder;
internal class JpegFrameConfig
{
public JpegFrameConfig(JpegColorSpace colorType, JpegEncodingColor encodingColor, JpegComponentConfig[] components, JpegHuffmanTableConfig[] huffmanTables, JpegQuantizationTableConfig[] quantTables)
public JpegFrameConfig(JpegColorSpace colorType, JpegColorType encodingColor, JpegComponentConfig[] components, JpegHuffmanTableConfig[] huffmanTables, JpegQuantizationTableConfig[] quantTables)
{
this.ColorType = colorType;
this.EncodingColor = encodingColor;
@ -25,7 +25,7 @@ internal class JpegFrameConfig
public JpegColorSpace ColorType { get; }
public JpegEncodingColor EncodingColor { get; }
public JpegColorType EncodingColor { get; }
public JpegComponentConfig[] Components { get; }

6
src/ImageSharp/Formats/Jpeg/Components/Encoder/HuffmanScanEncoder.cs

@ -139,13 +139,13 @@ internal class HuffmanScanEncoder
/// <param name="frame">Frame to encode.</param>
/// <param name="converter">Converter from color to spectral.</param>
/// <param name="cancellationToken">The token to request cancellation.</param>
public void EncodeScanBaselineInterleaved<TPixel>(JpegEncodingColor color, JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken)
public void EncodeScanBaselineInterleaved<TPixel>(JpegColorType color, JpegFrame frame, SpectralConverter<TPixel> converter, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
switch (color)
{
case JpegEncodingColor.YCbCrRatio444:
case JpegEncodingColor.Rgb:
case JpegColorType.YCbCrRatio444:
case JpegColorType.Rgb:
this.EncodeThreeComponentBaselineInterleavedScanNoSubsampling(frame, converter, cancellationToken);
break;
default:

2
src/ImageSharp/Formats/Jpeg/JpegEncodingColor.cs → src/ImageSharp/Formats/Jpeg/JpegColorType.cs

@ -6,7 +6,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg;
/// <summary>
/// Provides enumeration of available JPEG color types.
/// </summary>
public enum JpegEncodingColor : byte
public enum JpegColorType : byte
{
/// <summary>
/// YCbCr (luminance, blue chroma, red chroma) color as defined in the ITU-T T.871 specification.

24
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -603,58 +603,58 @@ internal sealed class JpegDecoderCore : IRawJpegData, IImageDecoderInternals
/// Returns the jpeg color type based on the colorspace and subsampling used.
/// </summary>
/// <returns>Jpeg color type.</returns>
private JpegEncodingColor DeduceJpegColorType()
private JpegColorType DeduceJpegColorType()
{
switch (this.ColorSpace)
{
case JpegColorSpace.Grayscale:
return JpegEncodingColor.Luminance;
return JpegColorType.Luminance;
case JpegColorSpace.RGB:
return JpegEncodingColor.Rgb;
return JpegColorType.Rgb;
case JpegColorSpace.YCbCr:
if (this.Frame.Components[0].HorizontalSamplingFactor == 1 && this.Frame.Components[0].VerticalSamplingFactor == 1 &&
this.Frame.Components[1].HorizontalSamplingFactor == 1 && this.Frame.Components[1].VerticalSamplingFactor == 1 &&
this.Frame.Components[2].HorizontalSamplingFactor == 1 && this.Frame.Components[2].VerticalSamplingFactor == 1)
{
return JpegEncodingColor.YCbCrRatio444;
return JpegColorType.YCbCrRatio444;
}
else if (this.Frame.Components[0].HorizontalSamplingFactor == 2 && this.Frame.Components[0].VerticalSamplingFactor == 1 &&
this.Frame.Components[1].HorizontalSamplingFactor == 1 && this.Frame.Components[1].VerticalSamplingFactor == 1 &&
this.Frame.Components[2].HorizontalSamplingFactor == 1 && this.Frame.Components[2].VerticalSamplingFactor == 1)
{
return JpegEncodingColor.YCbCrRatio422;
return JpegColorType.YCbCrRatio422;
}
else if (this.Frame.Components[0].HorizontalSamplingFactor == 2 && this.Frame.Components[0].VerticalSamplingFactor == 2 &&
this.Frame.Components[1].HorizontalSamplingFactor == 1 && this.Frame.Components[1].VerticalSamplingFactor == 1 &&
this.Frame.Components[2].HorizontalSamplingFactor == 1 && this.Frame.Components[2].VerticalSamplingFactor == 1)
{
return JpegEncodingColor.YCbCrRatio420;
return JpegColorType.YCbCrRatio420;
}
else if (this.Frame.Components[0].HorizontalSamplingFactor == 4 && this.Frame.Components[0].VerticalSamplingFactor == 1 &&
this.Frame.Components[1].HorizontalSamplingFactor == 1 && this.Frame.Components[1].VerticalSamplingFactor == 1 &&
this.Frame.Components[2].HorizontalSamplingFactor == 1 && this.Frame.Components[2].VerticalSamplingFactor == 1)
{
return JpegEncodingColor.YCbCrRatio411;
return JpegColorType.YCbCrRatio411;
}
else if (this.Frame.Components[0].HorizontalSamplingFactor == 4 && this.Frame.Components[0].VerticalSamplingFactor == 2 &&
this.Frame.Components[1].HorizontalSamplingFactor == 1 && this.Frame.Components[1].VerticalSamplingFactor == 1 &&
this.Frame.Components[2].HorizontalSamplingFactor == 1 && this.Frame.Components[2].VerticalSamplingFactor == 1)
{
return JpegEncodingColor.YCbCrRatio410;
return JpegColorType.YCbCrRatio410;
}
else
{
return JpegEncodingColor.YCbCrRatio420;
return JpegColorType.YCbCrRatio420;
}
case JpegColorSpace.Cmyk:
return JpegEncodingColor.Cmyk;
return JpegColorType.Cmyk;
case JpegColorSpace.Ycck:
return JpegEncodingColor.Ycck;
return JpegColorType.Ycck;
default:
return JpegEncodingColor.YCbCrRatio420;
return JpegColorType.YCbCrRatio420;
}
}

2
src/ImageSharp/Formats/Jpeg/JpegEncoder.cs

@ -45,7 +45,7 @@ public sealed class JpegEncoder : ImageEncoder
/// <summary>
/// Gets the jpeg color for encoding.
/// </summary>
public JpegEncodingColor? ColorType { get; init; }
public JpegColorType? ColorType { get; init; }
/// <inheritdoc/>
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken)

18
src/ImageSharp/Formats/Jpeg/JpegEncoderCore.FrameConfig.cs

@ -40,7 +40,7 @@ internal sealed unsafe partial class JpegEncoderCore
// YCbCr 4:4:4
new JpegFrameConfig(
JpegColorSpace.YCbCr,
JpegEncodingColor.YCbCrRatio444,
JpegColorType.YCbCrRatio444,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 1, hsf: 1, vsf: 1, quantIndex: 0, dcIndex: 0, acIndex: 0),
@ -53,7 +53,7 @@ internal sealed unsafe partial class JpegEncoderCore
// YCbCr 4:2:2
new JpegFrameConfig(
JpegColorSpace.YCbCr,
JpegEncodingColor.YCbCrRatio422,
JpegColorType.YCbCrRatio422,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 1, hsf: 2, vsf: 1, quantIndex: 0, dcIndex: 0, acIndex: 0),
@ -66,7 +66,7 @@ internal sealed unsafe partial class JpegEncoderCore
// YCbCr 4:2:0
new JpegFrameConfig(
JpegColorSpace.YCbCr,
JpegEncodingColor.YCbCrRatio420,
JpegColorType.YCbCrRatio420,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 1, hsf: 2, vsf: 2, quantIndex: 0, dcIndex: 0, acIndex: 0),
@ -79,7 +79,7 @@ internal sealed unsafe partial class JpegEncoderCore
// YCbCr 4:1:1
new JpegFrameConfig(
JpegColorSpace.YCbCr,
JpegEncodingColor.YCbCrRatio411,
JpegColorType.YCbCrRatio411,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 1, hsf: 4, vsf: 1, quantIndex: 0, dcIndex: 0, acIndex: 0),
@ -92,7 +92,7 @@ internal sealed unsafe partial class JpegEncoderCore
// YCbCr 4:1:0
new JpegFrameConfig(
JpegColorSpace.YCbCr,
JpegEncodingColor.YCbCrRatio410,
JpegColorType.YCbCrRatio410,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 1, hsf: 4, vsf: 2, quantIndex: 0, dcIndex: 0, acIndex: 0),
@ -105,7 +105,7 @@ internal sealed unsafe partial class JpegEncoderCore
// Luminance
new JpegFrameConfig(
JpegColorSpace.Grayscale,
JpegEncodingColor.Luminance,
JpegColorType.Luminance,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 0, hsf: 1, vsf: 1, quantIndex: 0, dcIndex: 0, acIndex: 0),
@ -123,7 +123,7 @@ internal sealed unsafe partial class JpegEncoderCore
// Rgb
new JpegFrameConfig(
JpegColorSpace.RGB,
JpegEncodingColor.Rgb,
JpegColorType.Rgb,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 82, hsf: 1, vsf: 1, quantIndex: 0, dcIndex: 0, acIndex: 0),
@ -146,7 +146,7 @@ internal sealed unsafe partial class JpegEncoderCore
// Cmyk
new JpegFrameConfig(
JpegColorSpace.Cmyk,
JpegEncodingColor.Cmyk,
JpegColorType.Cmyk,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 1, hsf: 1, vsf: 1, quantIndex: 0, dcIndex: 0, acIndex: 0),
@ -170,7 +170,7 @@ internal sealed unsafe partial class JpegEncoderCore
// YccK
new JpegFrameConfig(
JpegColorSpace.Ycck,
JpegEncodingColor.Ycck,
JpegColorType.Ycck,
new JpegComponentConfig[]
{
new JpegComponentConfig(id: 1, hsf: 1, vsf: 1, quantIndex: 0, dcIndex: 0, acIndex: 0),

2
src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

@ -780,7 +780,7 @@ internal sealed unsafe partial class JpegEncoderCore : IImageEncoderInternals
private JpegFrameConfig GetFrameConfig(JpegMetadata metadata)
{
JpegEncodingColor color = this.encoder.ColorType ?? metadata.ColorType ?? JpegEncodingColor.YCbCrRatio420;
JpegColorType color = this.encoder.ColorType ?? metadata.ColorType;
JpegFrameConfig frameConfig = Array.Find(
FrameConfigs,
cfg => cfg.EncodingColor == color);

24
src/ImageSharp/Formats/Jpeg/JpegMetadata.cs

@ -82,7 +82,7 @@ public class JpegMetadata : IFormatMetadata<JpegMetadata>
/// <summary>
/// Gets or sets the color type.
/// </summary>
public JpegEncodingColor ColorType { get; set; } = JpegEncodingColor.YCbCrRatio420;
public JpegColorType ColorType { get; set; } = JpegColorType.YCbCrRatio420;
/// <summary>
/// Gets or sets a value indicating whether the component encoding mode should be interleaved.
@ -109,29 +109,29 @@ public class JpegMetadata : IFormatMetadata<JpegMetadata>
/// <inheritdoc/>
public static JpegMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
{
JpegEncodingColor color;
JpegColorType color;
PixelColorType colorType = metadata.PixelTypeInfo.ColorType ?? PixelColorType.YCbCr;
switch (colorType)
{
case PixelColorType.Luminance:
color = JpegEncodingColor.Luminance;
color = JpegColorType.Luminance;
break;
case PixelColorType.CMYK:
color = JpegEncodingColor.Cmyk;
color = JpegColorType.Cmyk;
break;
case PixelColorType.YCCK:
color = JpegEncodingColor.Ycck;
color = JpegColorType.Ycck;
break;
default:
if (colorType.HasFlag(PixelColorType.RGB) || colorType.HasFlag(PixelColorType.BGR))
{
color = JpegEncodingColor.Rgb;
color = JpegColorType.Rgb;
}
else
{
color = metadata.Quality <= Quantization.DefaultQualityFactor
? JpegEncodingColor.YCbCrRatio420
: JpegEncodingColor.YCbCrRatio444;
? JpegColorType.YCbCrRatio420
: JpegColorType.YCbCrRatio444;
}
break;
@ -153,22 +153,22 @@ public class JpegMetadata : IFormatMetadata<JpegMetadata>
PixelComponentInfo info;
switch (this.ColorType)
{
case JpegEncodingColor.Luminance:
case JpegColorType.Luminance:
bpp = 8;
colorType = PixelColorType.Luminance;
info = PixelComponentInfo.Create(1, bpp, 8);
break;
case JpegEncodingColor.Cmyk:
case JpegColorType.Cmyk:
bpp = 32;
colorType = PixelColorType.CMYK;
info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8);
break;
case JpegEncodingColor.Ycck:
case JpegColorType.Ycck:
bpp = 32;
colorType = PixelColorType.YCCK;
info = PixelComponentInfo.Create(4, bpp, 8, 8, 8, 8);
break;
case JpegEncodingColor.Rgb:
case JpegColorType.Rgb:
bpp = 24;
colorType = PixelColorType.RGB;
info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);

2
src/ImageSharp/Formats/Tiff/Compression/Compressors/TiffJpegCompressor.cs

@ -33,7 +33,7 @@ internal class TiffJpegCompressor : TiffBaseCompressor
var image = Image.LoadPixelData<Rgb24>(rows, width, height);
image.Save(memoryStream, new JpegEncoder()
{
ColorType = JpegEncodingColor.Rgb
ColorType = JpegColorType.Rgb
});
memoryStream.Position = 0;
memoryStream.WriteTo(this.Output);

2
tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegComparison.cs

@ -39,7 +39,7 @@ public class EncodeJpegComparison
using FileStream imageBinaryStream = File.OpenRead(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImage));
this.imageImageSharp = Image.Load<Rgba32>(imageBinaryStream);
this.encoderImageSharp = new JpegEncoder { Quality = this.Quality, ColorType = JpegEncodingColor.YCbCrRatio420 };
this.encoderImageSharp = new JpegEncoder { Quality = this.Quality, ColorType = JpegColorType.YCbCrRatio420 };
this.destinationStream = new MemoryStream();
}

12
tests/ImageSharp.Benchmarks/Codecs/Jpeg/EncodeJpegFeatures.cs

@ -20,19 +20,19 @@ public class EncodeJpegFeatures
// No metadata
private const string TestImage = TestImages.Jpeg.Baseline.Calliphora;
public static IEnumerable<JpegEncodingColor> ColorSpaceValues => new[]
public static IEnumerable<JpegColorType> ColorSpaceValues => new[]
{
JpegEncodingColor.Luminance,
JpegEncodingColor.Rgb,
JpegEncodingColor.YCbCrRatio420,
JpegEncodingColor.YCbCrRatio444,
JpegColorType.Luminance,
JpegColorType.Rgb,
JpegColorType.YCbCrRatio420,
JpegColorType.YCbCrRatio444,
};
[Params(75, 90, 100)]
public int Quality;
[ParamsSource(nameof(ColorSpaceValues), Priority = -100)]
public JpegEncodingColor TargetColorSpace;
public JpegColorType TargetColorSpace;
private Image<Rgb24> bmpCore;
private JpegEncoder encoder;

28
tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs

@ -145,14 +145,14 @@ public partial class JpegDecoderTests
}
[Theory]
[InlineData(TestImages.Jpeg.Baseline.Floorplan, JpegEncodingColor.Luminance)]
[InlineData(TestImages.Jpeg.Baseline.Jpeg420Small, JpegEncodingColor.YCbCrRatio420)]
[InlineData(TestImages.Jpeg.Baseline.Jpeg444, JpegEncodingColor.YCbCrRatio444)]
[InlineData(TestImages.Jpeg.Baseline.JpegRgb, JpegEncodingColor.Rgb)]
[InlineData(TestImages.Jpeg.Baseline.Cmyk, JpegEncodingColor.Cmyk)]
[InlineData(TestImages.Jpeg.Baseline.Jpeg410, JpegEncodingColor.YCbCrRatio410)]
[InlineData(TestImages.Jpeg.Baseline.Jpeg411, JpegEncodingColor.YCbCrRatio411)]
public void Identify_DetectsCorrectColorType(string imagePath, JpegEncodingColor expectedColorType)
[InlineData(TestImages.Jpeg.Baseline.Floorplan, JpegColorType.Luminance)]
[InlineData(TestImages.Jpeg.Baseline.Jpeg420Small, JpegColorType.YCbCrRatio420)]
[InlineData(TestImages.Jpeg.Baseline.Jpeg444, JpegColorType.YCbCrRatio444)]
[InlineData(TestImages.Jpeg.Baseline.JpegRgb, JpegColorType.Rgb)]
[InlineData(TestImages.Jpeg.Baseline.Cmyk, JpegColorType.Cmyk)]
[InlineData(TestImages.Jpeg.Baseline.Jpeg410, JpegColorType.YCbCrRatio410)]
[InlineData(TestImages.Jpeg.Baseline.Jpeg411, JpegColorType.YCbCrRatio411)]
public void Identify_DetectsCorrectColorType(string imagePath, JpegColorType expectedColorType)
{
TestFile testFile = TestFile.Create(imagePath);
using MemoryStream stream = new(testFile.Bytes, false);
@ -162,12 +162,12 @@ public partial class JpegDecoderTests
}
[Theory]
[WithFile(TestImages.Jpeg.Baseline.Floorplan, PixelTypes.Rgb24, JpegEncodingColor.Luminance)]
[WithFile(TestImages.Jpeg.Baseline.Jpeg420Small, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio420)]
[WithFile(TestImages.Jpeg.Baseline.Jpeg444, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio444)]
[WithFile(TestImages.Jpeg.Baseline.JpegRgb, PixelTypes.Rgb24, JpegEncodingColor.Rgb)]
[WithFile(TestImages.Jpeg.Baseline.Cmyk, PixelTypes.Rgb24, JpegEncodingColor.Cmyk)]
public void Decode_DetectsCorrectColorType<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor expectedColorType)
[WithFile(TestImages.Jpeg.Baseline.Floorplan, PixelTypes.Rgb24, JpegColorType.Luminance)]
[WithFile(TestImages.Jpeg.Baseline.Jpeg420Small, PixelTypes.Rgb24, JpegColorType.YCbCrRatio420)]
[WithFile(TestImages.Jpeg.Baseline.Jpeg444, PixelTypes.Rgb24, JpegColorType.YCbCrRatio444)]
[WithFile(TestImages.Jpeg.Baseline.JpegRgb, PixelTypes.Rgb24, JpegColorType.Rgb)]
[WithFile(TestImages.Jpeg.Baseline.Cmyk, PixelTypes.Rgb24, JpegColorType.Cmyk)]
public void Decode_DetectsCorrectColorType<TPixel>(TestImageProvider<TPixel> provider, JpegColorType expectedColorType)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);

10
tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs

@ -208,11 +208,11 @@ public partial class JpegEncoderTests
}
[Theory]
[WithFile(TestImages.Jpeg.Baseline.Floorplan, PixelTypes.Rgb24, JpegEncodingColor.Luminance)]
[WithFile(TestImages.Jpeg.Baseline.Jpeg444, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio444)]
[WithFile(TestImages.Jpeg.Baseline.Jpeg420Small, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio420)]
[WithFile(TestImages.Jpeg.Baseline.JpegRgb, PixelTypes.Rgb24, JpegEncodingColor.Rgb)]
public void Encode_PreservesColorType<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor expectedColorType)
[WithFile(TestImages.Jpeg.Baseline.Floorplan, PixelTypes.Rgb24, JpegColorType.Luminance)]
[WithFile(TestImages.Jpeg.Baseline.Jpeg444, PixelTypes.Rgb24, JpegColorType.YCbCrRatio444)]
[WithFile(TestImages.Jpeg.Baseline.Jpeg420Small, PixelTypes.Rgb24, JpegColorType.YCbCrRatio420)]
[WithFile(TestImages.Jpeg.Baseline.JpegRgb, PixelTypes.Rgb24, JpegColorType.Rgb)]
public void Encode_PreservesColorType<TPixel>(TestImageProvider<TPixel> provider, JpegColorType expectedColorType)
where TPixel : unmanaged, IPixel<TPixel>
{
// arrange

112
tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs

@ -21,51 +21,51 @@ public partial class JpegEncoderTests
100,
};
public static readonly TheoryData<JpegEncodingColor, int, float> NonSubsampledEncodingSetups = new()
public static readonly TheoryData<JpegColorType, int, float> NonSubsampledEncodingSetups = new()
{
{ JpegEncodingColor.Rgb, 100, 0.0238f / 100 },
{ JpegEncodingColor.Rgb, 80, 1.3044f / 100 },
{ JpegEncodingColor.Rgb, 40, 2.9879f / 100 },
{ JpegEncodingColor.YCbCrRatio444, 100, 0.0780f / 100 },
{ JpegEncodingColor.YCbCrRatio444, 80, 1.4585f / 100 },
{ JpegEncodingColor.YCbCrRatio444, 40, 3.1413f / 100 },
{ JpegColorType.Rgb, 100, 0.0238f / 100 },
{ JpegColorType.Rgb, 80, 1.3044f / 100 },
{ JpegColorType.Rgb, 40, 2.9879f / 100 },
{ JpegColorType.YCbCrRatio444, 100, 0.0780f / 100 },
{ JpegColorType.YCbCrRatio444, 80, 1.4585f / 100 },
{ JpegColorType.YCbCrRatio444, 40, 3.1413f / 100 },
};
public static readonly TheoryData<JpegEncodingColor, int, float> SubsampledEncodingSetups = new()
public static readonly TheoryData<JpegColorType, int, float> SubsampledEncodingSetups = new()
{
{ JpegEncodingColor.YCbCrRatio422, 100, 0.4895f / 100 },
{ JpegEncodingColor.YCbCrRatio422, 80, 1.6043f / 100 },
{ JpegEncodingColor.YCbCrRatio422, 40, 3.1996f / 100 },
{ JpegEncodingColor.YCbCrRatio420, 100, 0.5790f / 100 },
{ JpegEncodingColor.YCbCrRatio420, 80, 1.6692f / 100 },
{ JpegEncodingColor.YCbCrRatio420, 40, 3.2324f / 100 },
{ JpegEncodingColor.YCbCrRatio411, 100, 0.6868f / 100 },
{ JpegEncodingColor.YCbCrRatio411, 80, 1.7139f / 100 },
{ JpegEncodingColor.YCbCrRatio411, 40, 3.2634f / 100 },
{ JpegEncodingColor.YCbCrRatio410, 100, 0.7357f / 100 },
{ JpegEncodingColor.YCbCrRatio410, 80, 1.7495f / 100 },
{ JpegEncodingColor.YCbCrRatio410, 40, 3.2911f / 100 },
{ JpegColorType.YCbCrRatio422, 100, 0.4895f / 100 },
{ JpegColorType.YCbCrRatio422, 80, 1.6043f / 100 },
{ JpegColorType.YCbCrRatio422, 40, 3.1996f / 100 },
{ JpegColorType.YCbCrRatio420, 100, 0.5790f / 100 },
{ JpegColorType.YCbCrRatio420, 80, 1.6692f / 100 },
{ JpegColorType.YCbCrRatio420, 40, 3.2324f / 100 },
{ JpegColorType.YCbCrRatio411, 100, 0.6868f / 100 },
{ JpegColorType.YCbCrRatio411, 80, 1.7139f / 100 },
{ JpegColorType.YCbCrRatio411, 40, 3.2634f / 100 },
{ JpegColorType.YCbCrRatio410, 100, 0.7357f / 100 },
{ JpegColorType.YCbCrRatio410, 80, 1.7495f / 100 },
{ JpegColorType.YCbCrRatio410, 40, 3.2911f / 100 },
};
public static readonly TheoryData<JpegEncodingColor, int, float> CmykEncodingSetups = new()
public static readonly TheoryData<JpegColorType, int, float> CmykEncodingSetups = new()
{
{ JpegEncodingColor.Cmyk, 100, 0.0159f / 100 },
{ JpegEncodingColor.Cmyk, 80, 0.3922f / 100 },
{ JpegEncodingColor.Cmyk, 40, 0.6488f / 100 },
{ JpegColorType.Cmyk, 100, 0.0159f / 100 },
{ JpegColorType.Cmyk, 80, 0.3922f / 100 },
{ JpegColorType.Cmyk, 40, 0.6488f / 100 },
};
public static readonly TheoryData<JpegEncodingColor, int, float> YcckEncodingSetups = new()
public static readonly TheoryData<JpegColorType, int, float> YcckEncodingSetups = new()
{
{ JpegEncodingColor.Ycck, 100, 0.0356f / 100 },
{ JpegEncodingColor.Ycck, 80, 0.1245f / 100 },
{ JpegEncodingColor.Ycck, 40, 0.2663f / 100 },
{ JpegColorType.Ycck, 100, 0.0356f / 100 },
{ JpegColorType.Ycck, 80, 0.1245f / 100 },
{ JpegColorType.Ycck, 40, 0.2663f / 100 },
};
public static readonly TheoryData<JpegEncodingColor, int, float> LuminanceEncodingSetups = new()
public static readonly TheoryData<JpegColorType, int, float> LuminanceEncodingSetups = new()
{
{ JpegEncodingColor.Luminance, 100, 0.0175f / 100 },
{ JpegEncodingColor.Luminance, 80, 0.6730f / 100 },
{ JpegEncodingColor.Luminance, 40, 0.9943f / 100 },
{ JpegColorType.Luminance, 100, 0.0175f / 100 },
{ JpegColorType.Luminance, 80, 0.6730f / 100 },
{ JpegColorType.Luminance, 40, 0.9943f / 100 },
};
[Theory]
@ -74,7 +74,7 @@ public partial class JpegEncoderTests
[WithFile(TestImages.Png.BikeGrayscale, nameof(LuminanceEncodingSetups), PixelTypes.L8)]
[WithFile(TestImages.Jpeg.Baseline.Cmyk, nameof(CmykEncodingSetups), PixelTypes.Rgb24)]
[WithFile(TestImages.Jpeg.Baseline.Ycck, nameof(YcckEncodingSetups), PixelTypes.Rgb24)]
public void EncodeBaseline_Interleaved<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality, float tolerance)
public void EncodeBaseline_Interleaved<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality, float tolerance)
where TPixel : unmanaged, IPixel<TPixel> => TestJpegEncoderCore(provider, colorType, quality, tolerance);
[Theory]
@ -83,7 +83,7 @@ public partial class JpegEncoderTests
[WithFile(TestImages.Png.BikeGrayscale, nameof(LuminanceEncodingSetups), PixelTypes.L8)]
[WithFile(TestImages.Jpeg.Baseline.Cmyk, nameof(CmykEncodingSetups), PixelTypes.Rgb24)]
[WithFile(TestImages.Jpeg.Baseline.Ycck, nameof(YcckEncodingSetups), PixelTypes.Rgb24)]
public void EncodeBaseline_NonInterleavedMode<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality, float tolerance)
public void EncodeBaseline_NonInterleavedMode<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality, float tolerance)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();
@ -108,7 +108,7 @@ public partial class JpegEncoderTests
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 153, 21, PixelTypes.Rgb24)]
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 143, 81, PixelTypes.Rgb24)]
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 138, 24, PixelTypes.Rgb24)]
public void EncodeBaseline_WorksWithDifferentSizes<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality, float tolerance)
public void EncodeBaseline_WorksWithDifferentSizes<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality, float tolerance)
where TPixel : unmanaged, IPixel<TPixel> => TestJpegEncoderCore(provider, colorType, quality);
[Theory]
@ -121,7 +121,7 @@ public partial class JpegEncoderTests
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 48, 24, PixelTypes.Rgb24)]
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 46, 8, PixelTypes.Rgb24)]
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 51, 7, PixelTypes.Rgb24)]
public void EncodeBaseline_WithSmallImages_WorksWithDifferentSizes<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality, float tolerance)
public void EncodeBaseline_WithSmallImages_WorksWithDifferentSizes<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality, float tolerance)
where TPixel : unmanaged, IPixel<TPixel> => TestJpegEncoderCore(provider, colorType, quality, ImageComparer.Tolerant(0.12f));
[Theory]
@ -131,28 +131,28 @@ public partial class JpegEncoderTests
[WithSolidFilledImages(1, 1, 100, 100, 100, 255, PixelTypes.La16, 100)]
[WithSolidFilledImages(1, 1, 100, 100, 100, 255, PixelTypes.La32, 100)]
public void EncodeBaseline_Grayscale<TPixel>(TestImageProvider<TPixel> provider, int quality)
where TPixel : unmanaged, IPixel<TPixel> => TestJpegEncoderCore(provider, JpegEncodingColor.Luminance, quality);
where TPixel : unmanaged, IPixel<TPixel> => TestJpegEncoderCore(provider, JpegColorType.Luminance, quality);
[Theory]
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 96, 96, PixelTypes.Rgb24 | PixelTypes.Bgr24)]
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 48, 48, PixelTypes.Rgb24 | PixelTypes.Bgr24)]
public void EncodeBaseline_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality, float tolerance)
public void EncodeBaseline_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality, float tolerance)
where TPixel : unmanaged, IPixel<TPixel> => TestJpegEncoderCore(provider, colorType, quality);
[Theory]
[WithTestPatternImages(nameof(NonSubsampledEncodingSetups), 48, 48, PixelTypes.Rgb24 | PixelTypes.Bgr24)]
public void EncodeBaseline_WithSmallImages_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality, float tolerance)
public void EncodeBaseline_WithSmallImages_IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality, float tolerance)
where TPixel : unmanaged, IPixel<TPixel> => TestJpegEncoderCore(provider, colorType, quality, comparer: ImageComparer.Tolerant(0.06f));
[Theory]
[WithFile(TestImages.Png.CalliphoraPartial, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio444)]
[WithTestPatternImages(587, 821, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio444)]
[WithTestPatternImages(677, 683, PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio420)]
[WithSolidFilledImages(400, 400, nameof(Color.Red), PixelTypes.Rgb24, JpegEncodingColor.YCbCrRatio420)]
public void EncodeBaseline_WorksWithDiscontiguousBuffers<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType)
[WithFile(TestImages.Png.CalliphoraPartial, PixelTypes.Rgb24, JpegColorType.YCbCrRatio444)]
[WithTestPatternImages(587, 821, PixelTypes.Rgb24, JpegColorType.YCbCrRatio444)]
[WithTestPatternImages(677, 683, PixelTypes.Rgb24, JpegColorType.YCbCrRatio420)]
[WithSolidFilledImages(400, 400, nameof(Color.Red), PixelTypes.Rgb24, JpegColorType.YCbCrRatio420)]
public void EncodeBaseline_WorksWithDiscontiguousBuffers<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType)
where TPixel : unmanaged, IPixel<TPixel>
{
ImageComparer comparer = colorType == JpegEncodingColor.YCbCrRatio444
ImageComparer comparer = colorType == JpegColorType.YCbCrRatio444
? ImageComparer.TolerantPercentage(0.1f)
: ImageComparer.TolerantPercentage(5f);
@ -161,9 +161,9 @@ public partial class JpegEncoderTests
}
[Theory]
[InlineData(JpegEncodingColor.YCbCrRatio420)]
[InlineData(JpegEncodingColor.YCbCrRatio444)]
public async Task Encode_IsCancellable(JpegEncodingColor colorType)
[InlineData(JpegColorType.YCbCrRatio420)]
[InlineData(JpegColorType.YCbCrRatio444)]
public async Task Encode_IsCancellable(JpegColorType colorType)
{
CancellationTokenSource cts = new();
using PausedStream pausedStream = new(new MemoryStream());
@ -201,13 +201,13 @@ public partial class JpegEncoderTests
image.Mutate(x => x.Crop(132, 1606));
int[] quality = new int[] { 100, 50 };
JpegEncodingColor[] colors = new[] { JpegEncodingColor.YCbCrRatio444, JpegEncodingColor.YCbCrRatio420 };
JpegColorType[] colors = new[] { JpegColorType.YCbCrRatio444, JpegColorType.YCbCrRatio420 };
for (int i = 0; i < quality.Length; i++)
{
int q = quality[i];
for (int j = 0; j < colors.Length; j++)
{
JpegEncodingColor c = colors[j];
JpegColorType c = colors[j];
image.VerifyEncoder(provider, "jpeg", $"{q}-{c}", new JpegEncoder() { Quality = q, ColorType = c }, GetComparer(q, c));
}
}
@ -216,7 +216,7 @@ public partial class JpegEncoderTests
/// <summary>
/// Anton's SUPER-SCIENTIFIC tolerance threshold calculation
/// </summary>
private static ImageComparer GetComparer(int quality, JpegEncodingColor? colorType)
private static ImageComparer GetComparer(int quality, JpegColorType? colorType)
{
float tolerance = 0.015f; // ~1.5%
@ -224,10 +224,10 @@ public partial class JpegEncoderTests
{
tolerance *= 4.5f;
}
else if (quality < 75 || colorType == JpegEncodingColor.YCbCrRatio420)
else if (quality < 75 || colorType == JpegColorType.YCbCrRatio420)
{
tolerance *= 2.0f;
if (colorType == JpegEncodingColor.YCbCrRatio420)
if (colorType == JpegColorType.YCbCrRatio420)
{
tolerance *= 2.0f;
}
@ -236,15 +236,15 @@ public partial class JpegEncoderTests
return ImageComparer.Tolerant(tolerance);
}
private static void TestJpegEncoderCore<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality)
private static void TestJpegEncoderCore<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality)
where TPixel : unmanaged, IPixel<TPixel>
=> TestJpegEncoderCore(provider, colorType, quality, GetComparer(quality, colorType));
private static void TestJpegEncoderCore<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality, float tolerance)
private static void TestJpegEncoderCore<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality, float tolerance)
where TPixel : unmanaged, IPixel<TPixel>
=> TestJpegEncoderCore(provider, colorType, quality, new TolerantImageComparer(tolerance));
private static void TestJpegEncoderCore<TPixel>(TestImageProvider<TPixel> provider, JpegEncodingColor colorType, int quality, ImageComparer comparer)
private static void TestJpegEncoderCore<TPixel>(TestImageProvider<TPixel> provider, JpegColorType colorType, int quality, ImageComparer comparer)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> image = provider.GetImage();

4
tests/ImageSharp.Tests/Formats/Jpg/JpegMetadataTests.cs

@ -12,10 +12,10 @@ public class JpegMetadataTests
[Fact]
public void CloneIsDeep()
{
var meta = new JpegMetadata { ColorType = JpegEncodingColor.Luminance };
var meta = new JpegMetadata { ColorType = JpegColorType.Luminance };
var clone = (JpegMetadata)meta.DeepClone();
clone.ColorType = JpegEncodingColor.YCbCrRatio420;
clone.ColorType = JpegColorType.YCbCrRatio420;
Assert.False(meta.ColorType.Equals(clone.ColorType));
}

Loading…
Cancel
Save