go: image/jpeg: add options to partially decode or tolerantly decode invalid images?
go version devel +ce43e1f Mon Apr 13 23:27:35 2015 +0000 linux/amd64
Attempted to use jpeg.Decode
on the below image:
https://streamcoimg-a.akamaihd.net/000/340/810/9ae536dd97d2d92fc17a6590509a51c0.jpg
Expected the image to decode successfully, as it displays in a browser.
Actual result:
invalid JPEG format: short Huffman data
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 13
- Comments: 37 (16 by maintainers)
In case, anybody does care, I have forked now the jpeg package and fixed some of the issues with the help of the code of @whorfin
https://gitlab.com/golang-utils/image2
So to recap:
Great job!
go version go1.13.4 windows/amd64 this problem still exists.
All of the failing testcases here and others that I’ve found are truncated. They don’t have complete SOS segments, don’t contain an EOI, and raise warnings with other decoders. In @elsonwu’s case, it’s fixed by appending
\x00\xff\xd9
. The others are missing more data.Still, there is a bug in that there’s no way to decode truncated images, which seem relatively common and are readable with most other decoders.
My first thought is to return a partially decoded image (if there is one) along with the error; see https://github.com/special/go/commit/c7a05f392ca604f6070009364827b04535293974. I don’t mind adding docs/tests and submitting that if the approach is ok.
Otherwise, it seems slightly inappropriate to silence legitimate decoding errors, and there’s no API for decoding options, so I’m not sure what else to do.
I’d rather have a different package instead of adding TryToDecode to the standard library for the rest of Go 1.x’s lifetime. As a bonus, such a package wouldn’t be bound by the standard library feature freeze that we’re currently in.
I don’t know if any such package already exists, and I won’t have time to make one any time soon. Sorry.
Yeah, I’d still say what I said a couple of years ago: https://github.com/golang/go/issues/10447#issuecomment-310585843
We could possibly change jpeg.Decode (and the other image codecs) to return (non-nil Image, non-nil error) with partial results if it encounters an error, although that’s unusual in general for functions returning (T, err), and certainly not going to happen for Go 1.8.
As for matching whatever browsers do, what browsers do influences how far down the Postel’s law slope we slip, but I’m wary of the slippery slope, and according the JPEG spec, these are invalid images.