Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError #50

Closed
sanmadjack opened this issue Jun 15, 2017 · 33 comments
Closed

RangeError #50

sanmadjack opened this issue Jun 15, 2017 · 33 comments

Comments

@sanmadjack
Copy link

sanmadjack commented Jun 15, 2017

I get this error with a few images (very few out of thousands, but it keeps coming up), none of which appear to have any issues when opened in other applications: It occurs when I try to read the file.

RangeError (index): Invalid value: Only valid value is 0: 1
#0      List.[] (dart:core-patch/growable_array.dart:151)
#1      JpegScan._decodeHuffman (package:image/src/formats/jpeg/jpeg_scan.dart:134:18)
#2      JpegScan._decodeACSuccessive (package:image/src/formats/jpeg/jpeg_scan.dart:238:20)
#3      JpegScan._decodeBlock (package:image/src/formats/jpeg/jpeg_scan.dart:308:13)
#4      JpegScan.decode (package:image/src/formats/jpeg/jpeg_scan.dart:78:11)
#5      JpegData._readSOS (package:image/src/formats/jpeg/jpeg_data.dart:568:55)
#6      JpegData._read (package:image/src/formats/jpeg/jpeg_data.dart:338:11)
#7      JpegData.read (package:image/src/formats/jpeg/jpeg_data.dart:94:5)
#8      JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:46:10)
#9      decodeImage (package:image/src/formats/formats.dart:60:18)

Here is one such image:

test

@brendan-duncan
Copy link
Owner

brendan-duncan commented Jun 16, 2017 via email

@brendan-duncan
Copy link
Owner

I started taking a look at the problem. Might take a bit to work out. I pushed a fix that keeps it from crashing, but doesn't solve the underlying problem and the decoded image has a lot of block artifacts. I checked the image against several jpeg libraries. libjpeg, the standard library most programs use, does decode the image correctly. It's also one of the more complex decoders. Other decoders, such as the one I ported the dart version from, fail for this image. I'll have to figure out what libjpeg's secret ingredient is which will take a little time.

@sanmadjack
Copy link
Author

I tried opening the image in GIMP and when I did it said that the image has embedded color profile "c2", and asks if I want to convert it to the RGB working space. I don't know if that might have something to do with the odd decoding.

@brendan-duncan
Copy link
Owner

brendan-duncan commented Jun 28, 2017 via email

@brendan-duncan
Copy link
Owner

I rewrote the JPEG decoder and it's mostly finished, and it decodes this image correctly now. It should just be another day or so before I finalize all of the testing and push the new version.

@sanmadjack
Copy link
Author

Wow, I'm impressed. I really appreciate the work you put into this. Thanks a lot for tackling this.

@brendan-duncan
Copy link
Owner

I pushed the new jpeg decoder. I did a lot of testing and it seems to be ok, but if you wouldn't mind testing the git version with your usage, I'd appreciate it.

@sanmadjack
Copy link
Author

I've actually got about 200,000 images that I've previously successfully used with this library, I'm going to go ahead and run them all through the decoder to help make sure everything's okay.

@sanmadjack
Copy link
Author

sanmadjack commented Jul 7, 2017

Hit an error:

RangeError (index): Index out of range: index should be less than 15368: 15488
#0      Uint8List.[] (dart:typed_data-patch/typed_data_patch.dart:791)
#1      JpegDecoder._H2V1Convert (package:image/src/formats/jpeg_decoder.dart:371:21)
#2      JpegDecoder.decode (package:image/src/formats/jpeg_decoder.dart:219:11)
#3      JpegDecoder._decodeImage (package:image/src/formats/jpeg_decoder.dart:89:11)
#4      JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:67:12)
#5      decodeImage (package:image/src/formats/formats.dart:60:18)
#6      main (file:///D:/Source/dartlery/server/tool/test_images.dart:44:11)

2b15b164589aae44c5d4a61996736e3da1f6d982ee984be4ff51b28598569199

This image previously decoded fine. A few other images also output a similar error with this same stack trace, haven't encountered a different error yet.

@sanmadjack
Copy link
Author

sanmadjack commented Jul 7, 2017

Found one more error:

ImageException: JPEG ERROR: -232
#0      JpegDecoder._terminate (package:image/src/formats/jpeg_decoder.dart:2527:5)
#1      JpegDecoder._process_markers (package:image/src/formats/jpeg_decoder.dart:1259:5)
#2      JpegDecoder._locate_sos_marker (package:image/src/formats/jpeg_decoder.dart:1384:13)
#3      JpegDecoder._init_scan (package:image/src/formats/jpeg_decoder.dart:1704:9)
#4      JpegDecoder._init_progressive (package:image/src/formats/jpeg_decoder.dart:647:11)
#5      JpegDecoder._decode_start (package:image/src/formats/jpeg_decoder.dart:626:7)
#6      JpegDecoder.begin (package:image/src/formats/jpeg_decoder.dart:173:5)
#7      JpegDecoder._decodeImage (package:image/src/formats/jpeg_decoder.dart:85:5)
#8      JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:67:12)
#9      decodeImage (package:image/src/formats/formats.dart:60:18)

It's happened on several images which were not decoding before. I would make a new ticket, but since we're testing the re-written jpeg decoder I figured I'd just keep it all here. Here's one of the images:

266385 - artist-zeronis media-artwork tagme

@brendan-duncan
Copy link
Owner

Thanks a lot for the tests! The challenge in porting a C library to Dart is that Dart doesn't have pointers and many C libraries do a lot of pointer manipulation, and Dart and C have different integer representations, and C libraries like to do integer bit manipulation. There are a few code paths in the new version that I haven't fully tested yet due to lack of images that reach those parts of the code, so I appreciate the examples.

@sanmadjack
Copy link
Author

I completely understand, I was converting some compression-related C-code for work to a scripted language for work a little while back, and it was a quite the ordeal to convert all the fancy memory tricks it would do. I've only tested a small fraction of my images so far, should I go ahead and test everything and post all the results here, or wait for these results to be addressed first?

@brendan-duncan
Copy link
Owner

I'm almost done with these tests, the first one was because Dart and C seem to have a different order of operations for + and >>, which caused the index range error. Looking at why the second one fails now.

@sanmadjack
Copy link
Author

Okay, I'll just wait for your next push then.

@brendan-duncan
Copy link
Owner

I pushed the fixes for those images.

@brendan-duncan
Copy link
Owner

I did find another test case that fails, CMYK encoded jpegs. I don't think those are very common, the library I ported doesn't support them. Hopefully your images don't have any.

@sanmadjack
Copy link
Author

Got another:

ImageException: JPEG ERROR: -226
#0      JpegDecoder._terminate (package:image/src/formats/jpeg_decoder.dart:2533:5)
#1      JpegDecoder._init_frame (package:image/src/formats/jpeg_decoder.dart:1965:9)
#2      JpegDecoder._decode_start (package:image/src/formats/jpeg_decoder.dart:624:5)
#3      JpegDecoder.begin (package:image/src/formats/jpeg_decoder.dart:173:5)
#4      JpegDecoder._decodeImage (package:image/src/formats/jpeg_decoder.dart:85:5)
#5      JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:67:12)
#6      decodeImage (package:image/src/formats/formats.dart:60:18)

This one previously worked.

170508160932148 - tagme media artwork artist ednardo everything tagme character

@sanmadjack
Copy link
Author

I know I have at least one CMYK image, which you may remember from #25

CMYK encoding was working before, maybe it would be beneficial to keep the old decoder around and branch to it when CMYK is detected?

@brendan-duncan
Copy link
Owner

brendan-duncan commented Jul 7, 2017 via email

@brendan-duncan
Copy link
Owner

Picking away at it. I got 1:1 CMYK working, which handles the #25 image, and pushed that fix. But I still need to fix it for other scaling factors of CMYK, as well as non 1:1 grayscale images, which is the problem with the last image you attached. Fortunately once I get the non 1:1 grayscale images working, it'll be easy enough to apply that to the CMYK case (or vise versa), so I should be able to get these working soon. The only thing that may slow down getting a fix out is I will be on vacation starting tomorrow, so hopefully I can get through the issues today (neglecting my day job so I get through these issues sooner than later :-)), otherwise it'll be another week before I can get back to it.

@sanmadjack
Copy link
Author

Well geeze, thanks for putting in all that effort. I hope your vacation goes well!

@brendan-duncan
Copy link
Owner

The last grayscale image you posted seems to be working, just 2:2 cmyk format left, afaik. I don't know how common that format is vs 1:1 cmyk. Almost all open source decoders don't implement cmyk at all. Hope to get it figured out tonight, if not then it'll have to wait.

@sanmadjack
Copy link
Author

Sounds like you get bragging rights for being one of the only open source decoders to implement CMYK. Congratulations!

@sanmadjack
Copy link
Author

I know you're on vacation, but my test of all my images finished and I got a few to report. We can look into them when you get back.

@sanmadjack
Copy link
Author

ImageException: JPEG ERROR: -227
#0 JpegDecoder._terminate (package:image/src/formats/jpeg_decoder.dart:2584:5)
#1 JpegDecoder._decode_next_row (package:image/src/formats/jpeg_decoder.dart:589:17)
#2 JpegDecoder.decode (package:image/src/formats/jpeg_decoder.dart:194:9)
#3 JpegDecoder._decodeImage (package:image/src/formats/jpeg_decoder.dart:89:11)
#4 JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:67:12)
#5 decodeImage (package:image/src/formats/formats.dart:60:18)

ccff6f4dcdc83b9eabe9fd6380cfd03db8e91050e219c349051c165079f2bce0

@sanmadjack
Copy link
Author

ImageException: JPEG ERROR: -206
#0 JpegDecoder._terminate (package:image/src/formats/jpeg_decoder.dart:2584:5)
#1 JpegDecoder._read_sof_marker (package:image/src/formats/jpeg_decoder.dart:1115:7)
#2 JpegDecoder._locate_sof_marker (package:image/src/formats/jpeg_decoder.dart:1423:9)
#3 JpegDecoder._decode_init (package:image/src/formats/jpeg_decoder.dart:253:5)
#4 JpegDecoder.startDecode (package:image/src/formats/jpeg_decoder.dart:52:5)
#5 JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:66:5)
#6 decodeImage (package:image/src/formats/formats.dart:60:18)

The image for this error is rather large, let me know if you need it.

@sanmadjack
Copy link
Author

ImageException: JPEG ERROR: -226
#0 JpegDecoder._terminate (package:image/src/formats/jpeg_decoder.dart:2584:5)
#1 JpegDecoder._init_frame (package:image/src/formats/jpeg_decoder.dart:2017:9)
#2 JpegDecoder._decode_start (package:image/src/formats/jpeg_decoder.dart:669:5)
#3 JpegDecoder.begin (package:image/src/formats/jpeg_decoder.dart:173:5)
#4 JpegDecoder._decodeImage (package:image/src/formats/jpeg_decoder.dart:85:5)
#5 JpegDecoder.decodeImage (package:image/src/formats/jpeg_decoder.dart:67:12)
#6 decodeImage (package:image/src/formats/formats.dart:60:18)

8bdbe15af66cb3ee670e56ab9a6abef64e5ecc674d2d25ae45e19e926fa7c186

@brendan-duncan
Copy link
Owner

I am back working on this, here and there as time permits. I decided to merge that new jpeg decoder, which correctly handled the huffman decoding but not the cmyk/grayscale color spaces, with the original one, which worked in all cases but had the original huffman decoding bug that caused the range error. Hopefully I'll finish this hybrid decoder in the next day or two.

@sanmadjack
Copy link
Author

Welcome back. Hope your vacation went well.

@brendan-duncan
Copy link
Owner

Thanks, it was nice being away from the computer and with the family for a bit :-)

@brendan-duncan
Copy link
Owner

I pushed another change. I probably wasted some time porting that other decoder, it was incomplete and I just don't have the time/ability to fix up the missing pieces. So I reverted back to the original decoder, which was actually doing a pretty good job, and found/fixed the original problem which turned out to be simpler than I anticipated. All of the images you have sent me so far are decoding now.

One caveat is that the first image in this bug doesn't seem to be as smooth as other decoders, so there are some subtle blocking artifacts in the dart version. I'm not sure yet what's missing. I verified the original decoder that I had ported, which happens to be the same one Firefox uses for decoding PDF files, has the same results as this dart version, so any further improvements will require more brain power.

@david-alejandro-reyes-milian

Hi. Thanks for the library @brendan-duncan. Im also having a similar issue:
Index out of range: index should be less than 6294: 6294
Im trying to print a generated QR code into a pdf. Ideas?
Kind regards!

@brendan-duncan
Copy link
Owner

Do you have an example I can test against?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants