diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/IRawJpegData.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/IRawJpegData.cs
index b1ac1f78f5..b715eef98c 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/IRawJpegData.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/IRawJpegData.cs
@@ -26,11 +26,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
///
JpegColorSpace ColorSpace { get; }
- ///
- /// Gets the number of bits used for precision.
- ///
- int Precision { get; }
-
///
/// Gets the components.
///
@@ -41,4 +36,4 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
///
Block8x8F[] QuantizationTables { get; }
}
-}
\ No newline at end of file
+}
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
index e0311dafef..7cfbaddcc1 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
@@ -38,11 +38,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
///
private Size subSamplingDivisors;
- ///
- /// Defines the maximum value derived from the bitdepth.
- ///
- private readonly int maximumValue;
-
///
/// Initializes a new instance of the struct.
///
@@ -53,7 +48,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
int qtIndex = component.QuantizationTableIndex;
this.DequantiazationTable = ZigZag.CreateDequantizationTable(ref decoder.QuantizationTables[qtIndex]);
this.subSamplingDivisors = component.SubSamplingDivisors;
- this.maximumValue = (int)MathF.Pow(2, decoder.Precision) - 1;
this.SourceBlock = default;
this.WorkspaceBlock1 = default;
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
index 79965a3f0c..31214b4c1d 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
@@ -21,11 +21,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
///
private readonly Size blockAreaSize;
+ ///
+ /// Jpeg frame instance containing required decoding metadata.
+ ///
+ private readonly JpegFrame frame;
+
///
/// Initializes a new instance of the class.
///
- public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, IRawJpegData rawJpeg, Size postProcessorBufferSize, IJpegComponent component)
+ public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, JpegFrame frame, IRawJpegData rawJpeg, Size postProcessorBufferSize, IJpegComponent component)
{
+ this.frame = frame;
+
this.Component = component;
this.RawJpeg = rawJpeg;
this.blockAreaSize = this.Component.SubSamplingDivisors * 8;
@@ -70,7 +77,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
Buffer2D spectralBuffer = this.Component.SpectralBlocks;
var blockPp = new JpegBlockPostProcessor(this.RawJpeg, this.Component);
- float maximumValue = MathF.Pow(2, this.RawJpeg.Precision) - 1;
+
+ // TODO: this is a constant value for ALL components
+ float maximumValue = MathF.Pow(2, this.frame.Precision) - 1;
int destAreaStride = this.ColorBuffer.Width;
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs
index 9f3d4195cc..50cfa0188a 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs
@@ -78,7 +78,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
this.componentProcessors = new JpegComponentPostProcessor[frame.Components.Length];
for (int i = 0; i < this.componentProcessors.Length; i++)
{
- this.componentProcessors[i] = new JpegComponentPostProcessor(allocator, jpegData, postProcessorBufferSize, frame.Components[i]);
+ this.componentProcessors[i] = new JpegComponentPostProcessor(allocator, frame, jpegData, postProcessorBufferSize, frame.Components[i]);
}
// single 'stride' rgba32 buffer for conversion between spectral and TPixel
diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
index 97a6d39990..3c262be329 100644
--- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
+++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
@@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
///
/// The only supported precision
///
- private readonly int[] supportedPrecisions = { 8, 12 };
+ private readonly byte[] supportedPrecisions = { 8, 12 };
///
/// The buffer used to temporarily store bytes read from the stream.
@@ -148,9 +148,6 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
///
public JpegColorSpace ColorSpace { get; private set; }
- ///
- public int Precision { get; private set; }
-
///
/// Gets the components.
///
@@ -825,23 +822,24 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
JpegThrowHelper.ThrowInvalidImageContentException("Multiple SOF markers. Only single frame jpegs supported.");
}
- // Read initial marker definitions.
+ // Read initial marker definitions
const int length = 6;
stream.Read(this.temp, 0, length);
- // We only support 8-bit and 12-bit precision.
- if (Array.IndexOf(this.supportedPrecisions, this.temp[0]) == -1)
+ // 1 byte: Bits/sample precision
+ byte precision = this.temp[0];
+
+ // Validity check: only 8-bit and 12-bit precisions are supported
+ if (Array.IndexOf(this.supportedPrecisions, precision) == -1)
{
JpegThrowHelper.ThrowInvalidImageContentException("Only 8-Bit and 12-Bit precision supported.");
}
- this.Precision = this.temp[0];
-
this.Frame = new JpegFrame
{
Extended = frameMarker.Marker == JpegConstants.Markers.SOF1,
Progressive = frameMarker.Marker == JpegConstants.Markers.SOF2,
- Precision = this.temp[0],
+ Precision = precision,
PixelHeight = (this.temp[1] << 8) | this.temp[2],
PixelWidth = (this.temp[3] << 8) | this.temp[4],
ComponentCount = this.temp[5]