From 4e17b69473fe2056e328d40c4deb1b72b9dd6a44 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Sat, 26 Mar 2022 15:06:48 +0300 Subject: [PATCH] Added second stage to the resizing decoding --- src/ImageSharp/Formats/Jpeg/JpegDecoder.cs | 25 +++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs index bab75d15c7..60ada0e0e0 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoder.cs @@ -3,10 +3,10 @@ using System.IO; using System.Threading; -using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; namespace SixLabors.ImageSharp.Formats.Jpeg { @@ -33,7 +33,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg => this.Decode(configuration, stream, cancellationToken); /// - /// TODO: this implementation is experimental + /// Placeholder summary. + /// + /// Placeholder2 + /// Placeholder3 + /// Placeholder4 + /// Placeholder5 + /// Placeholder6 + public Image Experimental__DecodeInto(Configuration configuration, Stream stream, Size targetSize, CancellationToken cancellationToken) + => this.Experimental__DecodeInto(configuration, stream, targetSize, cancellationToken); + + /// + /// Placeholder summary. /// /// Placeholder1 /// Placeholder2 @@ -48,12 +59,16 @@ namespace SixLabors.ImageSharp.Formats.Jpeg using var decoder = new JpegDecoderCore(configuration, this); - // Copied from ImageDecoderUtilities.cs - // TODO: interface cast druing exception handling and code duplication is not okay using var bufferedReadStream = new BufferedReadStream(configuration, stream); try { - return decoder.Experimental__DecodeInto(bufferedReadStream, targetSize, cancellationToken); + Image img = decoder.Experimental__DecodeInto(bufferedReadStream, targetSize, cancellationToken); + if (img.Size() != targetSize) + { + img.Mutate(ctx => ctx.Resize(targetSize, KnownResamplers.Box, compand: false)); + } + + return img; } catch (InvalidMemoryOperationException ex) {