processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.2k stars 3.23k forks source link

Account for pixel density when masking images #6788

Closed Papershine closed 2 months ago

Papershine commented 5 months ago

Resolves #6770

Changes:

Previously, image pixel densities were hard coded to be 1, but when image pixel densities were changed to be variable, the mask function was not updated, resulting in incorrectly sized images being masked. By taking into account the pixel density when calculating the actual pixel width and height for masking, the issue should be fixed.

Screenshots of the change:

On a high pixel density display, the previous behaviour of the code snippet in #6770 has been restored:

Screenshot 2024-02-03 at 6 29 10 PM

PR Checklist

welcome[bot] commented 5 months ago

🎉 Thanks for opening this pull request! Please check out our contributing guidelines if you haven't already. And be sure to add yourself to the list of contributors on the readme page!

Papershine commented 5 months ago

Potential changes needed:

Papershine commented 5 months ago

I'm trying to set up and test masking when manually changing the pixel density, but things keep bugging out. Looking into the code for pixelDensity, I think there are some problems here

https://github.com/processing/p5.js/blob/304ee9008d64d842ddfb4a62999a5cd62c944316/src/image/p5.Image.js#L256-L260

If the original pixel density is 2, and we want to make it 4, it seems setting pixelDensity(4) would break the correct width and height of the image. Is it something that should be fixed?

davepagurek commented 5 months ago

I think the idea is that when you import e.g. an 800x800 image from a file, we have no idea what the density is, so we assume 1x density. But if we tell p5 that actually it should be treated like a 400x400 image at 2x density, then it will change its width and height to accommodate. So in this code path, we don't want to change any of the underlying pixels, just reinterpret it.

I'll be different if you create it from a p5.Graphics though: for that, since it's a canvas that you draw to, we give you a new blank canvas at the size you ask for. So if you start with an 800x800 canvas and then tell it to use a pixel density of 2, under the hood, you get 1600x1600 pixels under the hood. Then if you call .get() on that canvas, you should also end up with an image that also has width and height of 800 at density 2, for a total of 1600x1600 pixels.

So for tests, I think you'd either (1) make a graphic at the size and density you want and then turn it into an image with .get(), or (2) start with an image with the full number of pixels you want, and then tell p5 about its density to scale down the width/height.

taliacotton commented 3 months ago

Hi team! Wondering if there's a plan to integrate this update into the newest release of p5. Would be grateful! Thank you!!

davepagurek commented 3 months ago

@Papershine are you interested in continuing to finish up the tests? No worries if not, we can always continue development from what you've started!

Papershine commented 3 months ago

Hey @davepagurek, sorry about the delay! I've been extremely busy with school and still doing exams right now, it would be great if anybody interested could take over and finish this PR, thank you so much! If nobody is interested, I could work on the tests in a week when my exams are done.

Papershine commented 2 months ago

A test for pixel density 2, as well as a test for the case where pixel densities aren't the same, have been added.

Do we also want to have a test with createGraphics instead of createImage?

davepagurek commented 2 months ago

Thanks @Papershine, the tests so far look great! A createGraphics test would be good too just to make sure we've handled everything, then I think we're good to go!

Papershine commented 2 months ago

Just added that!

davepagurek commented 2 months ago

@all-contributors please add @Papershine for code, test

allcontributors[bot] commented 2 months ago

@davepagurek

I've put up a pull request to add @Papershine! :tada: