Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Resample RGB images in C++#29453
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Agg already has RGB resampling with output to RGBA builtin, so we justneed to correctly wire up the corresponding templates. With this RGBresampling mode, we save the extra copy from RGB to RGBA in NumPy landthat was required for the previous always-RGBA resampling.
In the case of RGBA, the RGB and A channels are resampled separately,but they are created as a view on the original to pass to the C++ code.The C++ code then copies it to a contiguous buffer, but Agg's RGBresampler supports manually stepping the RGB input by a custom stride.As this step is a template parameter, we can't handle any arbitrarayarray, but can special case steps of 3 or 4 units, which should coverthe common cases of RGB or RGBA-viewed-as-RGB input.
Using
Using The first commit here removes entry 3 by applying Agg templates for RGB, but we still end up allocating a similar size one in The second commit removes the need for that latter copy as well by applyingmore Agg templates for step size of 3 or 4. Strangely though, memray now attributesmore allocations to I think There are a few other copies that I can find by inspection:
Copies 1, 2, and 4 are 8 bytes, so 800M each, and copy 3 should be 3*byte = 300M. Many of these are replacing the previous version, or otherwise discarded, so I guess they don't count for I believe we can remove copies 2-4 with a port of I haven't committed that last thing, as I didn't finish alpha processing and wasn't sure if we wanted to move it to C++, but I think the numbers indicate this is a good idea. |
On a side note, |
anntzer commentedMar 23, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I think(?)#29776 would essentially remove the need for this PR (as it removes the use of _rgb_to_rgba). |
Uh oh!
There was an error while loading.Please reload this page.
PR summary
Agg already has RGB resampling with output to RGBA builtin, so we just need to correctly wire up the corresponding templates. With this RGB resampling mode, we save the extra copy from RGB to RGBA in NumPy land that was required for the previous always-RGBA resampling.
With the example from#29434, this saves 800MB of peak memory usage,
which actually works out totwo copies of 10000x10000x4 bytes; I'm not sure where the second copy comes from.I was thinking it was 4-byte RGBA, but it's actually 8-byte float64 input, so 1 copy removed as expected.While this does pass tests, I think there may be some room for further improvement, as the input to the C++ code isI have implemented this to save another copy.A[..., :3]
, which should be a discontiguous view, but then the pybind11 code forces it to be contiguous, which would make another copy. There are some offset and step settings in Agg that I think we can use to avoid this copy as well, but it might require extra template expansions.PR checklist