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

Image rework: part 1 (soundness) #878

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cyrgani
Copy link
Contributor

@cyrgani cyrgani commented Jan 10, 2025

Part of #748.

This PR mitigates the unsoundness caused by the Image fields being public and adds new safe methods for immutable access and unsafe methods for mutable access. The second commit changes all usages of these fields within macroquad itself to the new methods.

The reason for this unsoundness is that Image::get_image_data relies on the invariant that width * height == bytes.len() to efficiently convert the image bytes into a color slice.

Fixes #634.
Fixes #746.

New methods

  • Image::from_parts: method for creating an image from width, height and bytes while checking if the amount of bytes is correct
  • Image::bytes, Image::bytes_mut: returns an immutable / mutable slice of all bytes in the image
  • Image::width_mut, Image::height_mut, Image::bytes_vec_mut: unsafe methods that return a mutable reference to the respective field, allowing for unchecked manipulation (mostly exist to support migration of strange usecases)

Other non-breaking changes

  • Accessing any of the Image fields directly gives a deprecation warning and suggests using the methods instead.

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

Successfully merging this pull request may close these issues.

Image fields should be private for soundness Make Image Safe
1 participant