public void Decoder_PixelBufferComparison <TPixel>(TestImageProvider <TPixel> provider) where TPixel : unmanaged, IPixel <TPixel> { // Stream byte[] sourceBytes = TestFile.Create(provider.SourceFileOrDescription).Bytes; using var ms = new MemoryStream(sourceBytes); using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); // Decoding using var converter = new SpectralConverter <TPixel>(Configuration.Default); using var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder()); var scanDecoder = new HuffmanScanDecoder(bufferedStream, converter, cancellationToken: default); decoder.ParseStream(bufferedStream, scanDecoder, cancellationToken: default); // Test metadata provider.Utility.TestGroupName = nameof(JpegDecoderTests); provider.Utility.TestName = JpegDecoderTests.DecodeBaselineJpegOutputName; // Comparison using (Image <TPixel> image = new Image <TPixel>(Configuration.Default, converter.GetPixelBuffer(CancellationToken.None), new ImageMetadata())) using (Image <TPixel> referenceImage = provider.GetReferenceOutputImage <TPixel>(appendPixelTypeToFileName: false)) { ImageSimilarityReport report = ImageComparer.Exact.CompareImagesOrFrames(referenceImage, image); this.Output.WriteLine($"*** {provider.SourceFileOrDescription} ***"); this.Output.WriteLine($"Difference: {report.DifferencePercentageString}"); // ReSharper disable once PossibleInvalidOperationException Assert.True(report.TotalNormalizedDifference.Value < 0.005f); } }
/// <inheritdoc/> public Image <TPixel> Decode <TPixel>(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel <TPixel> { using var spectralConverter = new SpectralConverter <TPixel>(this.Configuration, cancellationToken); var scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); this.ParseStream(stream, scanDecoder, cancellationToken); this.InitExifProfile(); this.InitIccProfile(); this.InitIptcProfile(); this.InitDerivedMetadataProperties(); return(new Image <TPixel>(this.Configuration, spectralConverter.GetPixelBuffer(), this.Metadata)); }
/// <inheritdoc/> protected override void Decompress(BufferedReadStream stream, int byteCount, int stripHeight, Span <byte> buffer) { if (this.jpegTables != null) { using var jpegDecoder = new JpegDecoderCore(this.configuration, new JpegDecoder()); // If the PhotometricInterpretation is YCbCr we explicitly assume the JPEG data is in RGB color space. // There seems no other way to determine that the JPEG data is RGB colorspace (no APP14 marker, componentId's are not RGB). using SpectralConverter <Rgb24> spectralConverter = this.photometricInterpretation == TiffPhotometricInterpretation.YCbCr ? new RgbJpegSpectralConverter <Rgb24>(this.configuration) : new SpectralConverter <Rgb24>(this.configuration); var scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, CancellationToken.None); jpegDecoder.LoadTables(this.jpegTables, scanDecoder); scanDecoder.ResetInterval = 0; jpegDecoder.ParseStream(stream, scanDecoder, CancellationToken.None); // TODO: Should we pass through the CancellationToken from the tiff decoder? CopyImageBytesToBuffer(buffer, spectralConverter.GetPixelBuffer(CancellationToken.None)); } else { using var image = Image.Load <Rgb24>(stream); CopyImageBytesToBuffer(buffer, image.Frames.RootFrame.PixelBuffer); } }