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

Fixed blur filter throwing exception for too large values relative to image #972

Merged
merged 1 commit into from
Mar 21, 2025
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
28 changes: 17 additions & 11 deletions core/src/processing/core/PImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,9 @@ else if (format == ARGB)
* [toxi 050728]
*/
protected void buildBlurKernel(float r) {
float maxRadius = Math.min(width, height) / 2.0f;
float maxR = maxRadius / 3.5f;
r = Math.min(r, maxR);
int radius = (int) (r * 3.5f);
if (radius < 1) radius = 1;
if (radius > 248) radius = 248;
Expand All @@ -1083,6 +1086,9 @@ protected void buildBlurKernel(float r) {
}
}

private int safeDivide(int numerator, int denominator) {
return denominator == 0 ? numerator : numerator / denominator;
}

protected void blurAlpha(float r) {
int sum, cb;
Expand Down Expand Up @@ -1115,7 +1121,7 @@ protected void blurAlpha(float r) {
read++;
}
ri = yi + x;
b2[ri] = cb / sum;
b2[ri] = safeDivide(cb, sum);
}
yi += pixelWidth;
}
Expand Down Expand Up @@ -1146,7 +1152,7 @@ protected void blurAlpha(float r) {
ri++;
read += pixelWidth;
}
pixels[x+yi] = (cb/sum);
pixels[x+yi] = safeDivide(cb, sum);
}
yi += pixelWidth;
ymi += pixelWidth;
Expand Down Expand Up @@ -1191,9 +1197,9 @@ protected void blurRGB(float r) {
read++;
}
ri = yi + x;
r2[ri] = cr / sum;
g2[ri] = cg / sum;
b2[ri] = cb / sum;
r2[ri] = safeDivide(cr, sum);
g2[ri] = safeDivide(cg, sum);
b2[ri] = safeDivide(cb, sum);
}
yi += pixelWidth;
}
Expand Down Expand Up @@ -1228,7 +1234,7 @@ protected void blurRGB(float r) {
ri++;
read += pixelWidth;
}
pixels[x+yi] = 0xff000000 | (cr/sum)<<16 | (cg/sum)<<8 | (cb/sum);
pixels[x+yi] = 0xff000000 | (safeDivide(cr, sum))<<16 | (safeDivide(cg, sum))<<8 | (safeDivide(cb, sum));
}
yi += pixelWidth;
ymi += pixelWidth;
Expand Down Expand Up @@ -1276,10 +1282,10 @@ protected void blurARGB(float r) {
read++;
}
ri = yi + x;
a2[ri] = ca / sum;
r2[ri] = cr / sum;
g2[ri] = cg / sum;
b2[ri] = cb / sum;
a2[ri] = safeDivide(ca, sum);
r2[ri] = safeDivide(cr, sum);
g2[ri] = safeDivide(cg, sum);
b2[ri] = safeDivide(cb, sum);
}
yi += pixelWidth;
}
Expand Down Expand Up @@ -1315,7 +1321,7 @@ protected void blurARGB(float r) {
ri++;
read += pixelWidth;
}
pixels[x+yi] = (ca/sum)<<24 | (cr/sum)<<16 | (cg/sum)<<8 | (cb/sum);
pixels[x+yi] = (safeDivide(ca, sum))<<24 | (safeDivide(cr, sum))<<16 | (safeDivide(cg, sum))<<8 | (safeDivide(cb, sum));
}
yi += pixelWidth;
ymi += pixelWidth;
Expand Down
Loading