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

Add required parameters in _flipPixels function #6726

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/image/loading_displaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ p5.prototype.saveGif = async function(
pixels
);

data = _flipPixels(pixels);
data = _flipPixels(pixels, this.width, this.height);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! Does this still work correctly when you've set pixelDensity(2) in setup? I wonder if we need to be multiplying by the density here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using pixelDensity() in setup but nothing was being rendered. Am I missing something in this code?

let sketch = function(p) {
  p.setup = function() {
    pixelDensity(2);
    p.createCanvas(500, 500, p.WEBGL);
  };

  p.draw = function() {
  };

  p.keyPressed = function() {
    if (p.key === 's') {
      p.saveGif('test', 5);
    }
  }
};

let myp5 = new p5(sketch);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In instance mode I think you need p.pixelDensity(2).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ya. Thanks!

The current fix works as expected when setting different densities of pixels.

I also checked the behaviour by multiplying the dimensions with density while passing in function call but I was getting another console error saying : offset is out of bounds. So, looks like some calculations go wrong inside flipPixels function in the case when we multiply dimensions with density.

I saw a similar implementation in readPixelsWebGL() function and stackoverflow answers (https://stackoverflow.com/questions/41969562/how-can-i-flip-the-result-of-webglrenderingcontext-readpixels) so looks like we should not consider multiplying by density here? Or maybe we need to recalculate the flip pixels logic considering density (I tried that but wasn't able to figure out).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, wait, logically it should have it, but we just set it to 1 here while exporting:

this.pixelDensity(1);

So I think width/height on its own is fine!

} else {
data = this.drawingContext.getImageData(0, 0, this.width, this.height)
.data;
Expand Down Expand Up @@ -507,7 +507,7 @@ p5.prototype.saveGif = async function(
p5.prototype.downloadFile(blob, fileName, extension);
};

function _flipPixels(pixels) {
function _flipPixels(pixels, width, height) {
// extracting the pixels using readPixels returns
// an upside down image. we have to flip it back
// first. this solution is proposed by gman on
Expand Down
Loading