-
Notifications
You must be signed in to change notification settings - Fork 150
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
Add rotate_about_center_no_crop
to prevent pixel loss during image rotations
#688
base: master
Are you sure you want to change the base?
Conversation
…on cropping rotations
…oosing pixels due to image dimensions
"into" usually used for owned/moved values. |
Tests would be good |
The decision to name the functions as "..._into" is based on the already existing |
I am having a hard time finding the equivalent function's ( |
I know, it's hard. Maybe you can start with property testing just to detect panics and limitations, you can introduce invariants. For example, center should be in bounds. |
Ok, thanks! I will get to it :) |
fdd617d
to
0923401
Compare
…ise` and `test_rotate_nearest_half_turn_anticlockwise`
I added the test I mentioned previously. It checks that the average color of the image does not change after a 45º rotation, which would happen if pixels were being cropped as is the case with the #[test]
fn test_rotate_about_center_no_crop() {
let pixel_val = 255;
let square_size = 512;
let image_area = square_size * square_size;
let image = GrayImage::from_vec(
square_size,
square_size,
vec![pixel_val; image_area as usize],
)
.unwrap();
let expected_proportion = image.iter().map(|&x| x as u32).sum::<u32>() as f32
/ (pixel_val as u32 * image_area) as f32;
let rotated_image =
rotate_about_center_no_crop(&image, PI * 0.25, Interpolation::Nearest, Luma([0]));
let rotated_proportion = rotated_image.iter().map(|&x| x as u32).sum::<u32>() as f32
/ (pixel_val as u32 * image_area) as f32;
assert_approx_eq!(rotated_proportion, expected_proportion, 0.01);
} |
You can try to increase epsilon, maybe it will help. Another idea I would try is to create a very simple image and attach a very visible watermark. After rotation, you can check that this watermark remains and that it is in a certain place, in a corner. |
The difference between "no crop" rotation and simple rotation is that the watermark should always be in the output, so such a test would be especially useful. |
I finally found the associated tests for the previous rotation functions. I feel so stupid for not finding them before. I have used the helper functions and followed the same procedure to test my new rotation function. |
|
1beaf7b
to
41785c7
Compare
You are right |
Maybe let's introduce only 1 new public function? |
Not because I'm against those functions. I just think that every public function deserves a separate review from several individuals. |
I feel like rotate_into and other into functions might be useful as public functions. |
Ok, there's only 1 thread left about the |
Yes, |
@theotherphil You can take a look |
Still 1 test would be useful :) |
This PR introduces a new method,
rotate_about_center_no_crop
, that rotates an image about its center without cropping, ensuring no pixel data is lost in the process. The function calculates the new dimensions to fully accommodate the rotated image and fills any empty space outside the original image boundaries with a user-defined default pixel value.To support this new method, two auxiliary functions are included:
rotate_about_center_into
: Same asrotate_about_center_into
except it writes the output to an image passed as a mutable reference. Dimensions of the input image and the output do not have to be the same.rotate_into
: Same asrotate_into
except it writes the output to an image passed as a mutable reference. Dimensions of the input image and the output do not have to be the same.