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

PNG: Fix check for too few axes, memory, orientations #2713

Merged
merged 8 commits into from
Nov 21, 2024
Merged

Conversation

Lestropie
Copy link
Member

Fixes erroneous check put in place in #2535 (3acf6cc).

@jdtournier #2535 looks wrong just from the code. Look at the check vs. the error message. This I expect to still catch the erroneous use case before segfaulting, but no longer block normal operation as seems to be happening with 3.0.4. Would ideally have some unit tests...

Fixes erroneous check put in place in #2535 (3acf6cc).
@Lestropie
Copy link
Member Author

Lestropie commented Sep 25, 2023

While I believe the fix in de6d0c3 to be correct, couple of other things came up when running the relevant code in debugging mode.

  • PNG::load() seems to be allocating way more memory than is actually necessary (which lead to memory allocation fault in one instance). It's taking a product of voxel_count (header) and files.size(). It should be either enough memory for the image, or enough memory for one slice times the number of slices.

  • Better would be for PNG::load() to use a separate address per input file.
    Edit: Had issues with memory access when trying to do this; chose to revert to prior behaviour.

- Fix failure to return dynamically allocated memory (two separate instances).
- Fix orientation of exported PNGs in some use cases.
- Fix excessive memory allocation for images spread across multiple PNG files.
- When reading from multiple input PNG images, allocate memory for read buffers on a per-slice basis.
- Do not attempt to support writing bitwise images, even if the number of pixels in the width direction is a factor of 8.
- Fix intensity scaling of bitwise images such that values of 0 and 255 are used in the output uint8 image.
 Add test data and tests for verifying operation of PNG export.
@Lestropie Lestropie force-pushed the png_axes_fix branch 2 times, most recently from 0a275cd to 386db1f Compare October 4, 2023 03:10
@Lestropie Lestropie changed the title PNG: Fix check for too few axes PNG: Fix check for too few axes, memory Oct 5, 2023
@Lestropie
Copy link
Member Author

I ended up expanding the scope of this PR, as I discovered multiple other issues in the process of diagnosing and rectifying the original observation.

This PR includes adding new test data, which demonstrates the capability to generate PNG images in different slice directions with the correct orientation (proving rectification of #2179), and new mrconvert tests to make sure that these are generated and loaded correctly. These tests have also been confirmed to pass with using address sanitiser.

@Lestropie Lestropie requested a review from a team October 5, 2023 03:33
jdtournier
jdtournier previously approved these changes Oct 9, 2023
Copy link
Member

@jdtournier jdtournier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems all good. Hit a slight snag when writing to a non-existent folder (segfault when I was expecting a more informative error message), but that last commit seems to sort that out. Otherwise, things seem to work with the very limited testing I've done on my system. But your battery of tests seems way more comprehensive, so I expect that's plenty enough validation to merge. 👍

@Lestropie Lestropie requested a review from a team October 9, 2023 23:11
@Lestropie Lestropie assigned Lestropie and unassigned Lestropie Oct 9, 2023
@Lestropie
Copy link
Member Author

I am unable to merge this PR:

New changes require approval from someone other than jdtournier because they were the last pusher.

I cannot re-review it myself, even after removing myself as assignee. So presumably it is checking for first commit / most commits.

While I have the ability to use admin rights to bypass branch protections, we should maybe think about conforming to what's being semi-enforced as best practise.

@Lestropie Lestropie changed the title PNG: Fix check for too few axes, memory PNG: Fix check for too few axes, memory, orientations Feb 15, 2024
- Fix erroneous modification of strides upon header concatenation; this replicates the fix included in 41c02e9 as part of #2806, but is necessary for the suite of fixes here to work. Note that this affects mrcat in addition to multi-image format handling.
- Fix setting of strides upon PNG read.
- Expand testing of PNG handling. Evaluation of read and write functionality is performed separately, with the reference data fully visible and verifiable within the test data repository. Some previous tests bundled read and write testing together, which obscured the erroneous intermediate interpretation.
@Lestropie
Copy link
Member Author

I really hope this finally addresses all issues now.

You can look at files testing/binaries/data/mrconvert/png* and see that they are all sensible and of the correct orientation. The updated tests then make sure that:

  1. The generated PNGs match those pre-calculated;
  2. Reading the pre-calculated PNGs and comparing to .mif-based data (where the voxel size and transform have been set to default, but the orientations are otherwise correct) yields no differences.

@Lestropie
Copy link
Member Author

Seeking review on this as I want to subsequently merge back to dev and continue on #2806.
Notably, in #2792, where I added a new test for single input image support:
mrcat dwi.mif - | testing_diff_image - dwi.mif
, I immediately noticed that the piped image has different strides, which should not need to be the case.

@Lestropie Lestropie added this to the 3.0.5 updates milestone Sep 16, 2024
- Omit explicit deletion of ImageIO::Base data where unnecessary; this can be handled by ImageIO::Base::close().
- FIx additional memory leaks in ImageIO::PNG.
@Lestropie
Copy link
Member Author

No leaks across all mrconvert tests using valgrind:

testing_mrconvert.log

Copy link
Member

@jdtournier jdtournier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a look through the code and all the tests, and everything looks good. 👍

@Lestropie Lestropie added this pull request to the merge queue Nov 21, 2024
Merged via the queue into master with commit c852831 Nov 21, 2024
5 checks passed
@Lestropie Lestropie deleted the png_axes_fix branch November 21, 2024 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants