Browse Source

decoder jpeg to matching pixel format

af/merge-core
James Jackson-South 6 years ago
parent
commit
12a95ae094
  1. 18
      src/ImageSharp/Formats/Jpeg/JpegDecoder.cs
  2. 75
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

18
src/ImageSharp/Formats/Jpeg/JpegDecoder.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors and contributors.
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.IO;
@ -28,6 +28,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
}
}
/// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream)
{
Guard.NotNull(stream, nameof(stream));
using (var decoder = new JpegDecoderCore(configuration, this))
{
return decoder.Decode(stream);
}
}
/// <inheritdoc/>
public IImageInfo Identify(Configuration configuration, Stream stream)
{
@ -38,8 +49,5 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
return decoder.Identify(stream);
}
}
/// <inheritdoc />
public Image Decode(Configuration configuration, Stream stream) => this.Decode<Rgba32>(configuration, stream);
}
}
}

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

@ -204,6 +204,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
return new JpegFileMarker(marker[1], stream.Position - 2, true);
}
/// <summary>
/// Decodes the image from the specified <see cref="Stream"/> and sets the data to image.
/// </summary>
/// <param name="stream">The stream, where the image should be.</param>
/// <returns>The decoded image.</returns>
public Image Decode(Stream stream)
{
this.ParseStream(stream);
this.InitExifProfile();
this.InitIccProfile();
this.InitDerivedMetadataProperties();
return this.ColorSpace == JpegColorSpace.Grayscale
? (Image)this.PostProcessIntoImage<Gray8>()
: this.PostProcessIntoImage<Rgb24>();
}
/// <summary>
/// Decodes the image from the specified <see cref="Stream"/> and sets the data to image.
/// </summary>
@ -649,48 +665,51 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
switch (quantizationTableSpec >> 4)
{
case 0:
{
// 8 bit values
if (remaining < 64)
{
// 8 bit values
if (remaining < 64)
{
done = true;
break;
}
done = true;
break;
}
this.InputStream.Read(this.temp, 0, 64);
remaining -= 64;
this.InputStream.Read(this.temp, 0, 64);
remaining -= 64;
ref Block8x8F table = ref this.QuantizationTables[tableIndex];
for (int j = 0; j < 64; j++)
{
table[j] = this.temp[j];
}
ref Block8x8F table = ref this.QuantizationTables[tableIndex];
for (int j = 0; j < 64; j++)
{
table[j] = this.temp[j];
}
}
break;
break;
case 1:
{
// 16 bit values
if (remaining < 128)
{
// 16 bit values
if (remaining < 128)
{
done = true;
break;
}
done = true;
break;
}
this.InputStream.Read(this.temp, 0, 128);
remaining -= 128;
this.InputStream.Read(this.temp, 0, 128);
remaining -= 128;
ref Block8x8F table = ref this.QuantizationTables[tableIndex];
for (int j = 0; j < 64; j++)
{
table[j] = (this.temp[2 * j] << 8) | this.temp[(2 * j) + 1];
}
ref Block8x8F table = ref this.QuantizationTables[tableIndex];
for (int j = 0; j < 64; j++)
{
table[j] = (this.temp[2 * j] << 8) | this.temp[(2 * j) + 1];
}
}
break;
break;
default:
{
JpegThrowHelper.ThrowBadQuantizationTable();
break;
}
}
if (done)

Loading…
Cancel
Save