Skip to content

Conversation

@brianpopow
Copy link
Collaborator

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Currently the jpeg decoder will fail with an object null exception with jpegs which are not supported yet.
This PR changes the jpeg decoder to throw a NotSupportedException instead.

/// Start of Frame marker, non differential lossless, Huffman coding.
/// </summary>
public const byte SOF3 = 0xC3;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: there are also 0xC5 and 0xC6 StartOfFrame markers for Differential progressive DCT and Differential sequential DCT. I could not find any real life examples of those, so I was unsure if I should add those here too.

Copy link
Contributor

@br3aker br3aker Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afaik arithmetic coding is not very common (at least for general internet use case) as it's harder to compute and storage gain is not really better than DCT one. Adding yet another rare format just for 'why-not-ness' won't break anything as it's internal or kill the performance. So, why not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone gets bored they could copy implementations of the missing decoder functionality from here.

https://github.com/yigolden/JpegLibrary

They copied a bunch of stuff from us so all fair.

/// </summary>
/// <param name="errorMessage">The error message for the exception.</param>
[MethodImpl(InliningOptions.ColdPath)]
public static void ThrowNotSupportedException(string errorMessage) => throw new NotSupportedException(errorMessage);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we throw ImageFormatException or NotSupportedException in other cases we don't support decoding a valid input?

Ideally, we need to make sure to document all exceptions we throw on Image.Load overloads, although I'm less strict on this since I know that the BCL is also full of undocumented exceptions :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we throw ImageFormatException in cases when the image data is not as expected, e.g. not as the specification suggest.
Those cases here the jpeg files are valid, so i think NotSupportedException is the correct Exception to throw.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for NotSupportedException because corrupted image and not supported image are very different scenarios :)

Copy link
Member

@antonfirsov antonfirsov Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see the docs fixed in Image.FromFile.cs and Image.FromBytes.cs ! 👍

I would also change the docs in Image.FromStream.cs:

/// <exception cref="NotSupportedException">The stream is not readable. -or- The image format is not supported.</exception>


[Theory]
[WithFile(TestImages.Jpeg.Baseline.ArithmeticCoding, PixelTypes.Rgba32)]
[WithFile(TestImages.Jpeg.Baseline.ArithmeticCodingProgressive, PixelTypes.Rgba32)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Progressive image should be placed to Jpeg/Progressive folder.

@codecov
Copy link

codecov bot commented Aug 17, 2021

Codecov Report

Merging #1742 (d1fe136) into master (9b09775) will decrease coverage by 0.01%.
The diff coverage is 41.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1742      +/-   ##
==========================================
- Coverage   84.46%   84.44%   -0.02%     
==========================================
  Files         836      836              
  Lines       36737    36748      +11     
  Branches     4306     4306              
==========================================
+ Hits        31029    31032       +3     
- Misses       4876     4883       +7     
- Partials      832      833       +1     
Flag Coverage Δ
unittests 84.44% <41.66%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/ImageSharp/Formats/Jpeg/JpegConstants.cs 100.00% <ø> (ø)
src/ImageSharp/Image.FromFile.cs 98.71% <ø> (ø)
src/ImageSharp/Image.FromStream.cs 83.94% <ø> (ø)
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs 89.50% <22.22%> (-1.88%) ⬇️
src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs 54.54% <100.00%> (+4.54%) ⬆️
src/ImageSharp/Image.FromBytes.cs 93.90% <100.00%> (ø)
...ogramEqualizationSlidingWindowProcessor{TPixel}.cs 98.91% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 36f4ad4...d1fe136. Read the comment docs.

@brianpopow brianpopow requested a review from antonfirsov August 23, 2021 07:56
Copy link
Member

@JimBobSquarePants JimBobSquarePants left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff!

@brianpopow brianpopow merged commit 5d82124 into master Aug 23, 2021
@brianpopow brianpopow deleted the bp/unsupportedjpegs branch August 23, 2021 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants