Skip to content

Commit

Permalink
introduced OldJpegDecoder : IImageDecoder for the GolangPort decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfirsov committed Aug 18, 2017
1 parent 493deda commit b3b4827
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
29 changes: 29 additions & 0 deletions src/ImageSharp/Formats/Jpeg/GolangPort/OldJpegDecoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace ImageSharp.Formats.Jpeg.GolangPort
{
using System.IO;

using ImageSharp.PixelFormats;

/// <summary>
/// Image decoder for generating an image out of a jpg stream.
/// </summary>
public sealed class OldJpegDecoder : IImageDecoder, IJpegDecoderOptions
{
/// <summary>
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
/// </summary>
public bool IgnoreMetadata { get; set; }

/// <inheritdoc/>
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream)
where TPixel : struct, IPixel<TPixel>
{
Guard.NotNull(stream, nameof(stream));

using (var decoder = new OldJpegDecoderCore(configuration, this))
{
return decoder.Decode<TPixel>(stream);
}
}
}
}
36 changes: 6 additions & 30 deletions tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public JpegDecoderTests(ITestOutputHelper output)

private ITestOutputHelper Output { get; }

private static IImageDecoder OriginalDecoder => new JpegDecoder();
private static IImageDecoder OriginalDecoder => new OldJpegDecoder();

private static IImageDecoder GetPdfJsDecoder => throw new NotImplementedException();
private static IImageDecoder PdfJsDecoder => new JpegDecoder();

private float GetDifferenceInPercents<TPixel>(Image<TPixel> image, TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
Expand Down Expand Up @@ -76,7 +76,7 @@ public void CompareJpegDecoders<TPixel>(TestImageProvider<TPixel> provider)
this.Output.WriteLine($"Difference using ORIGINAL decoder: {d:0.0000}%");
}

using (Image<TPixel> image = provider.GetImage(GetPdfJsDecoder))
using (Image<TPixel> image = provider.GetImage(PdfJsDecoder))
{
double d = this.GetDifferenceInPercents(image, provider);
this.Output.WriteLine($"Difference using PDFJS decoder: {d:0.0000}%");
Expand All @@ -100,7 +100,7 @@ public void DecodeBaselineJpeg<TPixel>(TestImageProvider<TPixel> provider)
public void DecodeBaselineJpeg_PdfJs<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(GetPdfJsDecoder))
using (Image<TPixel> image = provider.GetImage(PdfJsDecoder))
{
image.DebugSave(provider);

Expand All @@ -126,7 +126,7 @@ public void DecodeProgressiveJpeg<TPixel>(TestImageProvider<TPixel> provider)
public void DecodeProgressiveJpeg_PdfJs<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage(GetPdfJsDecoder))
using (Image<TPixel> image = provider.GetImage(PdfJsDecoder))
{
image.DebugSave(provider, VeryTolerantJpegComparer);
}
Expand Down Expand Up @@ -160,31 +160,7 @@ public void DecodeGenerated<TPixel>(
var mirror = Image.Load<TPixel>(data);
mirror.DebugSave(provider, $"_{subsample}_Q{quality}");
}

[Theory]
[WithSolidFilledImages(42, 88, 255, 0, 0, PixelTypes.Rgba32)]
public void DecodeGenerated_MetadataOnly<TPixel>(
TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
using (var ms = new MemoryStream())
{
image.Save(ms, new JpegEncoder());
ms.Seek(0, SeekOrigin.Begin);

using (var decoder = new OldJpegDecoderCore(null, new JpegDecoder()))
{
decoder.Decode<TPixel>(ms);

Assert.Equal(decoder.ImageWidth, image.Width);
Assert.Equal(decoder.ImageHeight, image.Height);
}
}
}
}


[Fact]
public void Decoder_Reads_Correct_Resolution_From_Jfif()
{
Expand Down

0 comments on commit b3b4827

Please sign in to comment.