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

DynamicImage::crop() is slow because to_image() is slow #2295

Open
Shnatsel opened this issue Jul 28, 2024 · 1 comment
Open

DynamicImage::crop() is slow because to_image() is slow #2295

Shnatsel opened this issue Jul 28, 2024 · 1 comment
Labels
kind: API missing or awkward public APIs next: breaking Information tag for PRs and ideas that require an interface break

Comments

@Shnatsel
Copy link
Contributor

In v0.25.2, the crop operation is implemented as:

dynamic_map!(*self, ref mut p => imageops::crop(p, x, y, width, height).to_image())

which obtains a view into an image, and then calls to_image() on it to turn it into a new image buffer. And to_image() shuffles pixels one by one in a purely scalar fashion and with bounds checks on every pixel:

image/src/image.rs

Lines 1131 to 1136 in e176cd4

for y in 0..self.inner.ystride {
for x in 0..self.inner.xstride {
let p = borrowed.get_pixel(x + self.inner.xoffset, y + self.inner.yoffset);
out.put_pixel(x, y, p);
}
}

A much better way would be iterating over the rows, and copying over entire rows with slice::copy_from_slice(). That would both leverage vector instructions and reduce the amount of bounds checks from n^2 to n.

@jamninetyfive
Copy link

same here. do a PR?

@Shnatsel Shnatsel added kind: API missing or awkward public APIs next: breaking Information tag for PRs and ideas that require an interface break labels Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: API missing or awkward public APIs next: breaking Information tag for PRs and ideas that require an interface break
Projects
None yet
Development

No branches or pull requests

2 participants