Repository navigation
AccessViolationException when loading TIFF image #2123
Description
Activity
- changed the title
[-]System.ExecutionEngineException when loading TIFF image[/-][+]AccessViolationExceptionwhen loading TIFF image[/+]on May 20, 2022 - changed the title
[-]AccessViolationExceptionwhen loading TIFF image[/-][+]AccessViolationException when loading TIFF image[/+]on May 20, 2022 @botinko is it ok, if we use the provided image in unit tests?
@botinko is it ok, if we use the provided image in unit tests?
Sorry, but this is an image from our client and it didn't give permission for that.
ok no problem, I will try to create a similar one with the same properties.
I think I found the reason for the Exception: See branch FixIssue2123
While the image decodes now without errors, the colors seem to be wrong. The image is all pink. This indicates the wrong colorspace is determined. The image uses Jpeg compression and the
Photometric Interpretationis YCbCr. It seems this should be decoded as RGB instead of YCbCr. Not sure howto deal with that as thePhotometric Interpretationclearly states it should be YCbCr.tiffinfo output:
TIFFFetchNormalTag: Warning, ASCII value for tag "Software" does not end in null byte. TIFF Directory at offset 0x8 (8) Subfile Type: multi-page document (2 = 0x2) Image Width: 2550 Image Length: 3300 Resolution: 300, 300 pixels/inch Bits/Sample: 8 Compression Scheme: JPEG Photometric Interpretation: YCbCr FillOrder: msb-to-lsb YCbCr Subsampling: 2, 2 YCbCr Positioning: centered Orientation: row 0 top, col 0 lhs Samples/Pixel: 3 Rows/Strip: 3300 Planar Configuration: single image plane Page Number: 0-0 Software: KM_C308 DateTime: 2022:05:19 15:56:57@brianpopow it's rather strange that RGB would use chroma subsampling. Looks like the image itself is corrupted.
Are we absolutely, positively sure we're detecting the colorspace properly?
Given that I can open the file just fine using Paint.NET it suggests we're detecting something incorrectly.
EDIT
tiffinfo is part of the libtiff toolset isn't it? So we're probably detecting the colorspace correctly.For debugging ease I would suggest opening the image using LibTiff.NET and if it's successful we can step through and see what is going on.
tiffinfo is from libtiff, yes. The component id's of the jpeg data are 1, 2 and 3, which according to jpeg_metadata, also indicates it should be YCbCr instead of RGB. So I also think there is something wrong with the image, but imagemagick can convert this image also without problems. Im not sure how we can determine that this should be RGB rather then YCBCr.
The component id's of the jpeg data are 1, 2 and 3, which according to jpeg_metadata, also indicates it should be YCbCr instead of RGB.
It's not that simple, component id is a 'hint' that this may be a YCbCr encoded image. Adobe APP14 marker decides what exact ebcoding was used, i.e.
if(componentCount == 3) { if (app14Marker == null) { // fallback to default 3 component encoding - YCbCr } else if(app14Marker.IsYCbCr) { /* Explicit YCbCr */ } else { /* RGB */ } }Reacted by James Jackson-SouthThank you, guys! For us, it was enough that there was no
AccessViolationException, which causes the application to crash. I would like to get the Exception, which I can catch in case if image have wrong header.Ok, I think I do understand now what is going wrong here with the colors:
The image has PhotometricInterpretation set to YCbCr and is JpegCompressed. What is happening now is that decompressor (jpeg decoder) will decompress the data and converts it from YCbCr to RGB, then the Tiff Decoder again converts to YCbCr, because the PhotometricInterpretation is YCbCr (which is one time too much). If the image is compressed with JPEG, the jpeg decoder should take care of the conversion and not the TiffDecoder.
- added a commit that references this issue
on May 22, 2022 With #2124 merged, this issue is fixed. There is a new nuget version 2.1.2 of ImageSharp with this fix.
Reacted by James Jackson-South
Prerequisites
DEBUGandRELEASEmodeImageSharp version
2.1.1
Other ImageSharp packages and versions
2.1.1
Environment (Operating system, version and so on)
Windows 10 21H2, Linux
.NET Framework version
.NET 5
Description
Steps to Reproduce
Just load a problematic error.tiff from attachment.
Images
error.zip