Skip to content

Commit 59a8449

Browse files
committed
Throw NotSupportedException for arithmetic coding and lossless jpeg's
1 parent 9b09775 commit 59a8449

File tree

8 files changed

+96
-1
lines changed

8 files changed

+96
-1
lines changed

src/ImageSharp/Formats/Jpeg/JpegConstants.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ internal static class Markers
133133
/// </summary>
134134
public const byte APP15 = 0xEF;
135135

136+
/// <summary>
137+
/// Define arithmetic coding conditioning marker.
138+
/// </summary>
139+
public const byte DAC = 0xCC;
140+
136141
/// <summary>
137142
/// The text comment marker
138143
/// </summary>
@@ -173,6 +178,46 @@ internal static class Markers
173178
/// </summary>
174179
public const byte SOF2 = 0xC2;
175180

181+
/// <summary>
182+
/// Start of Frame marker, non differential lossless, Huffman coding.
183+
/// </summary>
184+
public const byte SOF3 = 0xC3;
185+
186+
/// <summary>
187+
/// Start of Frame marker, differential lossless, Huffman coding.
188+
/// </summary>
189+
public const byte SOF7 = 0xC7;
190+
191+
/// <summary>
192+
/// Start of Frame marker, non-differential, arithmetic coding, Extended sequential DCT.
193+
/// </summary>
194+
public const byte SOF9 = 0xC9;
195+
196+
/// <summary>
197+
/// Start of Frame marker, non-differential, arithmetic coding, Progressive DCT.
198+
/// </summary>
199+
public const byte SOF10 = 0xCA;
200+
201+
/// <summary>
202+
/// Start of Frame marker, non-differential, arithmetic coding, Lossless (sequential).
203+
/// </summary>
204+
public const byte SOF11 = 0xCB;
205+
206+
/// <summary>
207+
/// Start of Frame marker, differential, arithmetic coding, Differential sequential DCT.
208+
/// </summary>
209+
public const byte SOF13 = 0xCD;
210+
211+
/// <summary>
212+
/// Start of Frame marker, differential, arithmetic coding, Differential progressive DCT.
213+
/// </summary>
214+
public const byte SOF14 = 0xCE;
215+
216+
/// <summary>
217+
/// Start of Frame marker, differential, arithmetic coding, Differential lossless (sequential).
218+
/// </summary>
219+
public const byte SOF15 = 0xCF;
220+
176221
/// <summary>
177222
/// Define Huffman Table(s)
178223
/// <remarks>

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,20 @@ internal void ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDeco
247247
this.ProcessStartOfFrameMarker(stream, remaining, fileMarker, metadataOnly);
248248
break;
249249

250+
case JpegConstants.Markers.SOF3:
251+
case JpegConstants.Markers.SOF7:
252+
JpegThrowHelper.ThrowNotSupportedException("Decoding lossless jpeg files is not supported.");
253+
break;
254+
255+
case JpegConstants.Markers.SOF9:
256+
case JpegConstants.Markers.SOF10:
257+
case JpegConstants.Markers.SOF11:
258+
case JpegConstants.Markers.SOF13:
259+
case JpegConstants.Markers.SOF14:
260+
case JpegConstants.Markers.SOF15:
261+
JpegThrowHelper.ThrowNotSupportedException("Decoding jpeg files with arithmetic coding is not supported.");
262+
break;
263+
250264
case JpegConstants.Markers.SOS:
251265
if (!metadataOnly)
252266
{
@@ -326,6 +340,10 @@ internal void ParseStream(BufferedReadStream stream, HuffmanScanDecoder scanDeco
326340
case JpegConstants.Markers.COM:
327341
stream.Skip(remaining);
328342
break;
343+
344+
case JpegConstants.Markers.DAC:
345+
JpegThrowHelper.ThrowNotSupportedException("Decoding jpeg files with arithmetic coding is not supported.");
346+
break;
329347
}
330348
}
331349

src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
88
{
99
internal static class JpegThrowHelper
1010
{
11+
/// <summary>
12+
/// Cold path optimization for throwing <see cref="NotSupportedException"/>'s.
13+
/// </summary>
14+
/// <param name="errorMessage">The error message for the exception.</param>
15+
[MethodImpl(InliningOptions.ColdPath)]
16+
public static void ThrowNotSupportedException(string errorMessage) => throw new NotSupportedException(errorMessage);
17+
1118
/// <summary>
1219
/// Cold path optimization for throwing <see cref="InvalidImageContentException"/>'s.
1320
/// </summary>

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void ParseStream_BasicPropertiesAreCorrect()
7474
byte[] bytes = TestFile.Create(TestImages.Jpeg.Progressive.Progress).Bytes;
7575
using var ms = new MemoryStream(bytes);
7676
using var bufferedStream = new BufferedReadStream(Configuration.Default, ms);
77-
var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder());
77+
using var decoder = new JpegDecoderCore(Configuration.Default, new JpegDecoder());
7878
using Image<Rgba32> image = decoder.Decode<Rgba32>(bufferedStream, cancellationToken: default);
7979

8080
// I don't know why these numbers are different. All I know is that the decoder works
@@ -174,6 +174,19 @@ public async Task Identify_IsCancellable()
174174
await Assert.ThrowsAsync<TaskCanceledException>(async () => await Image.IdentifyAsync(config, "someFakeFile", cts.Token));
175175
}
176176

177+
[Theory]
178+
[WithFile(TestImages.Jpeg.Baseline.ArithmeticCoding, PixelTypes.Rgba32)]
179+
[WithFile(TestImages.Jpeg.Baseline.ArithmeticCodingProgressive, PixelTypes.Rgba32)]
180+
[WithFile(TestImages.Jpeg.Baseline.Lossless, PixelTypes.Rgba32)]
181+
public void ThrowsNotSupported_WithUnsupportedJpegs<TPixel>(TestImageProvider<TPixel> provider)
182+
where TPixel : unmanaged, IPixel<TPixel>
183+
{
184+
Assert.Throws<NotSupportedException>(() =>
185+
{
186+
using Image<TPixel> image = provider.GetImage(JpegDecoder);
187+
});
188+
}
189+
177190
// https://github.com/SixLabors/ImageSharp/pull/1732
178191
[Theory]
179192
[WithFile(TestImages.Jpeg.Issues.WrongColorSpace, PixelTypes.Rgba32)]

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ public static class Bad
200200
public const string App13WithEmptyIptc = "Jpg/baseline/iptc-psAPP13-wIPTCempty.jpg";
201201
public const string HistogramEqImage = "Jpg/baseline/640px-Unequalized_Hawkes_Bay_NZ.jpg";
202202
public const string ForestBridgeDifferentComponentsQuality = "Jpg/baseline/forest_bridge.jpg";
203+
public const string ArithmeticCoding = "Jpg/baseline/arithmetic_coding.jpg";
204+
public const string ArithmeticCodingProgressive = "Jpg/baseline/arithmetic_progressive.jpg";
205+
public const string Lossless = "Jpg/baseline/lossless.jpg";
203206

204207
public static readonly string[] All =
205208
{
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)