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 running encodeBmp #455

Closed
larssn opened this issue Jan 17, 2023 · 26 comments
Closed

RangeError running encodeBmp #455

larssn opened this issue Jan 17, 2023 · 26 comments

Comments

@larssn
Copy link

larssn commented Jan 17, 2023

Hello again 🙂

Ran into a RangeError when running:

final img = await rootBundle.load('assets/hungry.png');
encodeBmp(decodeImage(img.buffer.asUint8List()));

On the following image:
hungry

This is using Flutter.

@brendan-duncan
Copy link
Owner

I tested this locally, and did not get a RangeError.

   final img = await decodePngFile('test/_data/png/hungry.png');
   await encodeBmpFile('$testOutputPath/bmp/hungry.bmp', img!);

Do you get the range error on the decodeImage, or the encodeBmp?

@larssn
Copy link
Author

larssn commented Jan 17, 2023

encodeBmp specifically.

Hm, I wonder if github does any processing on the uploaded image file. Let me test that idea tomorrow.

@brendan-duncan
Copy link
Owner

Also make sure you do a pub update to get any recent changes, since I've been pushing at a pretty fast pace recently.

@larssn

This comment was marked as outdated.

@larssn larssn closed this as completed Jan 18, 2023
@larssn
Copy link
Author

larssn commented Jan 18, 2023

After further testing, I'm still having problems with encodeBmp (and flip), so I'm reopening.

I've uploaded 3 pictures.

  • A girl_384.png, which always seems to get decoded/encoded/flipped successfully.
  • A hungry_180.png, which results in an invalid image when using encodeBmp. Also cannot be flipped and results in an entirely transparent image.
  • A hungry.png (179px width), which results in an invalid image when using encodeBmp. Crashes with a RangeError when using flip (no matter the direction): Index out of range: index should be less than 12: 1021.

encodePng always works, no matter what input (except If I start flipping the image).

Test setup

hungry.png (179px width)

hungry

Test: encodeBmp

final image = await rootBundle.load('hungry.png');
var data = decodeImage(image.buffer.asUint8List())!;
final img = encodeBmp(data); 

Result: Invalid image data when trying to display the image (using Flutter, but shouldn't matter).

Exception: Codec failed to produce an image, possibly due to invalid image data.

Test: flip and encodePng

final image = await rootBundle.load('hungry.png');
var data = decodeImage(image.buffer.asUint8List())!;
data = flip(data, direction: FlipDirection.horizontal);
final img = encodePng(data);

Result: flip exception

Unhandled Exception: RangeError (index): Index out of range: index should be less than 12: 1021
E/flutter ( 3195): #0      Uint8List.[] (dart:typed_data-patch/typed_data_patch.dart:2244:7)
E/flutter ( 3195): #1      PaletteUint8.getGreen
package:image/…/image/palette_uint8.dart:80
E/flutter ( 3195): #2      PixelUint8.g
package:image/…/image/pixel_uint8.dart:164
E/flutter ( 3195): #3      flipHorizontal
package:image/…/transform/flip.dart:82
E/flutter ( 3195): #4      flip
package:image/…/transform/flip.dart:20

Test: encodePng

final image = await rootBundle.load('hungry.png');
var data = decodeImage(image.buffer.asUint8List())!;
final img = encodePng(data); 

Result: Works ✅

hungry_180.png (180px width)

hungry_180

Test: encodeBmp

final image = await rootBundle.load('hungry_180.png');
var data = decodeImage(image.buffer.asUint8List())!;
final img = encodeBmp(data); 

Result: Invalid image data when trying to display the image (using Flutter, but shouldn't matter).

Exception: Codec failed to produce an image, possibly due to invalid image data.

Test: flip and encodePng

final image = await rootBundle.load('hungry_180.png');
var data = decodeImage(image.buffer.asUint8List())!;
data = flip(data, direction: FlipDirection.horizontal);
final img = encodePng(data);

Result: Image becomes 100% transparent

Test: encodePng

final image = await rootBundle.load('hungry_180.png');
var data = decodeImage(image.buffer.asUint8List())!;
final img = encodePng(data); 

Result: Works ✅

girl_384.png (384px width)

girl_384

All the above tests pass ✅

Also tried resizing girl_384.png to 383px width, to see if there was some kind of edge case with odd widths. But that worked just fine.

@larssn larssn reopened this Jan 18, 2023
@larssn
Copy link
Author

larssn commented Jan 23, 2023

@brendan-duncan Hope you had a great weekend! Have you had a chance to take a look at this? I hope my above tests make sense 🙂

@brendan-duncan
Copy link
Owner

Sorry, I missed seeing this was re-opened. I'll make sure to look into it again.

@brendan-duncan
Copy link
Owner

I pushed some fixes to git. I'll publish soon.

  1. flip (and other filter functions) weren't bounds checking palette access causing the error.
  2. bmp does not support palette alpha channels, so the image will now get auto converted to 32-bit rgba if its a palette image with an alpha channel.

@brendan-duncan
Copy link
Owner

The updates have been published.

@larssn
Copy link
Author

larssn commented Jan 24, 2023

@brendan-duncan Awesome, that seems to have fixed most of it. Much appreciated!

There's only one test that still fails, which is the flip one for the hungry_180.png:

Test: flip and encodePng

final image = await rootBundle.load('hungry_180.png');
var data = decodeImage(image.buffer.asUint8List())!;
data = flip(data, direction: FlipDirection.horizontal);
final img = encodePng(data);

It still outputs a completely transparent image.

@brendan-duncan
Copy link
Owner

Sorry about that, missed that one. It's fixed now in git. I'll push the fixes out later today (my time).

@larssn
Copy link
Author

larssn commented Jan 24, 2023

I'll give it a test tomorrow, really appreciate the snappy responses! 👍🏻🙂

@felipecastrosales
Copy link

@brendan-duncan, do you have any expectations for release with this fix?

I investigated quite a few errors in my tests and the reason apparently is the issue.

@felipecastrosales
Copy link

@larssn, Can you share more information about your tests?

I am using blurhash_dart and i have problems like this.

@brendan-duncan
Copy link
Owner

Did I not release this? How has it been a week? Sorry about that, I'll get it released as soon as I can (after my busy work morning).

@felipecastrosales
Copy link

I have this error using latest version from image and blurhash_dart packages:

image

In the versions below, I don't:

  • image: ^3.2.0
  • blurhash_dart: ^1.1.0

@larssn, Do you have any idea what it could be?

@larssn
Copy link
Author

larssn commented Jan 30, 2023

@felipecastrosales Im pretty sure its fixed in 4.0.13, which it sounds like is close to release.

@brendan-duncan
Copy link
Owner

@felipecastrosales image ^3.2.0 is the old version of the Image library, which wouldn't have the issues described here because it didn't support palettes. It probably has a different (or related) bug, but I wouldn't be able to fix it because pub doesn't support branching versions, so I wouldn't be able to release a 3.2.1 patch.

I'll get 4.0.13 released asap, but it means blurhash_dart (whatever that is) would have to update to the 4.x version of the Image library.

@felipecastrosales
Copy link

@brendan-duncan, interesting, thank you very much for sharing your idea and for the time.
We will be waiting for the version. All the best to you!

@brendan-duncan
Copy link
Owner

I pushed 4.0.13, which I think addresses the issues in this thread. It looks like blurhash_dart was updated to use image 4.* (https://github.com/justacid/blurhash-dart/blob/master/pubspec.yaml), so hopefully if you can get your dependencies updated, it should pull in the latest version with the fixes in it.

@felipecastrosales
Copy link

@larssn your problem is resolved? here persists.

@brendan-duncan
Copy link
Owner

@felipecastrosales You haven't verified if you updated the Image library to 4.0.13 or if you're still on 3.2.0.

@felipecastrosales
Copy link

felipecastrosales commented Feb 2, 2023

@brendan-duncan I updated yes, I even tested it in dependency_overrides, e.g.

@brendan-duncan
Copy link
Owner

If you can send me a small repo example I can test and fix whatever's wrong. But without that there's not much I can do.

@larssn
Copy link
Author

larssn commented Feb 3, 2023

Just a heads up: Finally had a chance to test out 4.0.13, which does indeed fix my before mentioned issue.

@felipecastrosales
Copy link

Situation resolved in blurhash_dart. 🎉

Thanks for opening up the discussion here, @larssn.

And success to you, @brendan-duncan. 🙏

@larssn larssn closed this as completed Feb 4, 2023
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