ImageSharp icon indicating copy to clipboard operation
ImageSharp copied to clipboard

Some old jpg images are saved with glitches (regression in alpha)

Open botinko opened this issue 4 years ago • 4 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am running the latest version of ImageSharp
  • [ ] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [x] I have searched open and closed issues to ensure it has not already been reported

Description

Some pictures of our clients get corrupted when resizing and even when simply resaving by ImageSharp. Issue exists in 2.0.0-alpha.0.168 but doesn't exists in v1.0.4.

Steps to Reproduce

            var img = Image.Load(@"issue.jpg");
            img.Save("copy.jpg");

before issue after copy

Link to original image and image after resaving https://drive.google.com/file/d/1_k54RFuPciKeKBtFDLftIKetOPsWkOFO/view?usp=sharing

If you resave the image in any editor, the problem stops reproducing. So you need an original image from archive to reproduce.

System Configuration

  • Other ImageSharp packages and versions: 2.0.0-alpha.0.168
  • Environment (Operating system, version and so on): Windows 10, docker
  • .NET Framework version: dotnet 5, 6
  • Additional information: issue doesn't exists in v1.0.4.

botinko avatar Feb 05 '22 06:02 botinko

Can confirm on my machine. Will work on it asap.

br3aker avatar Feb 05 '22 11:02 br3aker

I've found error leading to this. It was broken in https://github.com/SixLabors/ImageSharp/pull/1694 which did spectral -> color conversion withing spectral strides instead of full image.

Everything does work in current tests because we don't have a single component jpeg with non 1x1 sampling factors in current test suite. ITU spec says that any single component jpeg must be decoded as 'non-interleaved' jpeg:

When Ns = 1 (where Ns is the number of components in a scan), the order of data units within a scan shall be left-to-right and top-to-bottom, as shown in Figure A.2. This ordering applies whenever Ns = 1, regardless of the values of H1 and V1. image

Current decoder implementation decodes single-component jpegs int interleaved mode and it works for 1x1/2x1/4x1 sampling jpegs but vertical sampling factors break it due to interleaved 'traversal': image

Fix should be fairly simple, will try to create a PR today.

P.S. @botinko can we include your example image in out test suite? It would help a lot as we don't have such exotic image right now :)

br3aker avatar Feb 05 '22 12:02 br3aker

@br3aker Thank you very much for such a quick response! Sure, you can use it for your tests.

botinko avatar Feb 06 '22 00:02 botinko

@botinko thanks! Fix is ready and should be available in the nearest nightly build after merge, thanks for pointing this one :)

br3aker avatar Feb 06 '22 00:02 br3aker