brendan-duncan / image

Dart Image Library for opening, manipulating, and saving various different image file formats.
MIT License
1.14k stars 255 forks source link

fix: Fixing png image trim issue #662

Closed iquirino closed 1 week ago

iquirino commented 1 week ago

Hello guys, I've catch a new issue/bug with the new version 4.2.0.

This PR is intended to fix the issue.

https://github.com/brendan-duncan/image/issues/661

I'm having issues to crop images. Using the following code results in an empty image. Rollback version to 4.1.7 solved the issue.

  final cmd = img.Command()
    ..decodeImage(await httpResponse.stream.toBytes())
    ..trim(mode: img.TrimMode.transparent);
  await cmd.executeCommand();
  final croppedImage = await cmd.getImage();

  if (croppedImage == null) {
    return HttpGetResponse(httpResponse);
  }

  final croppedImageBytes = img.encodePng(croppedImage);

Image used to crop:

https://front.epossible.com.br/public/assets/6682c14170fa73490e0bf0e4/logo.png

Thank you!

brendan-duncan commented 1 week ago

Thanks! I'll get to this as soon as I can, work has been really busy.

brendan-duncan commented 1 week ago

Sorry it took a little while to get to this. The spirit of this PR is good, but the solution isn't quite right. What you're doing is when the image is converted due to having a palette for the composite function, it's just ignoring the results, So it does that work for nothing.

I pushed a better fix. Composite does need to do the palette conversion, but only for when it needs to do blending like alpha blending. It doesn't need to do palette conversion when blending isn't involved, like BlendMode.Direct. Trim uses BlendMode.Direct. So the fix is to not do palette conversion when the blendMode is BlendMode.Direct.

if (blend != BlendMode.direct && dst.hasPalette) {
  dst = dst.convert(numChannels: dst.numChannels);
}

That essentially does what your PR does, but doesn't break the composite function for its other uses and doesn't do the work unnecessarily.

The fix is in the git repo.

iquirino commented 1 week ago

Awesome! I've just run the tests and everything goes well. Should be good to have result images to compare if everything goes well. The test that I added will cover my use case, but we're missing your side case. How can I create tests to match your use case? I'm open to contribute with your awesome project 😉

brendan-duncan commented 1 week ago

Any tests you can think of will be helpful. I wish I had more time to dedicate to this library, but I have too many projects, work, and life spread me pretty thin these days.