-
Notifications
You must be signed in to change notification settings - Fork 9
Create BlurUtils.kt #9
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @BzCoder, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
This pull request introduces a new utility class, BlurUtils.kt
, which provides methods for blurring bitmaps. It includes two blurring algorithms: rsBlur
, which uses RenderScript for faster blurring (but requires API 17+), and blur
, which is a more basic implementation that should work on older devices. The class is defined as an object, making its methods accessible statically.
Highlights
- New utility class: Introduces
BlurUtils.kt
for blurring bitmaps. - RenderScript blurring: Implements
rsBlur
using RenderScript for faster blurring on API 17+. - Basic blurring: Implements
blur
for older devices without RenderScript support.
Changelog
- app/src/main/java/me/bzcoder/webview/utils/BlurUtils.kt
- Created
BlurUtils.kt
withrsBlur
andblur
methods. - Added
rsBlur
function to perform blurring using RenderScript. - Added
blur
function to perform basic blurring algorithm. - Defined
BlurUtils
as an object for static access.
- Created
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
A blurry haze,
RenderScript's gentle phase,
Or simple soft touch.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The code introduces a utility class BlurUtils
with two methods for blurring bitmaps: rsBlur
using RenderScript and blur
using a box blur algorithm. The RenderScript method is generally faster but requires API level 17 or higher. The box blur method is a fallback for older devices. Overall, the implementation seems reasonable, but there are some areas for improvement regarding efficiency, API level handling, and code clarity.
Summary of Findings
- API Level Check for RenderScript Blur: The
rsBlur
method should include an API level check to ensure it's only used on devices that support RenderScript. Without this, the app could crash on older devices. - Potential Optimization in Box Blur: The
blur
method uses a lot of integer arrays for intermediate calculations. Consider using a single array or explore alternative data structures to potentially reduce memory allocation and improve performance. - Package Naming: The package name
me.bzcoder.easyglide.util
seems misplaced for a general utility class. Consider moving it to a more appropriate package, such asme.bzcoder.webview.utils
.
Merge Readiness
The code introduces useful image blurring utilities. However, the rsBlur
method needs an API level check to prevent crashes on older devices. Addressing this and considering the other suggestions would improve the code's robustness and maintainability. I am unable to approve this pull request, and recommend that it not be merged until the critical issue is addressed. Others should review and approve this code before merging.
fun rsBlur(context: Context?, toTransform: Bitmap, radius: Int): Bitmap { | ||
val renderScript = RenderScript.create(context) | ||
val input = Allocation.createFromBitmap(renderScript, toTransform) | ||
val output = Allocation.createTyped(renderScript, input.type) | ||
val scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)) | ||
scriptIntrinsicBlur.setInput(input) | ||
scriptIntrinsicBlur.setRadius(radius.toFloat()) | ||
scriptIntrinsicBlur.forEach(output) | ||
output.copyTo(toTransform) | ||
renderScript.destroy() | ||
return toTransform |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rsBlur
method uses RenderScript, which requires API level 17 or higher. Add an API level check to prevent crashes on older devices. You can use @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
to indicate the minimum API level required and wrap the method body in an if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
check.
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
fun rsBlur(context: Context?, toTransform: Bitmap, radius: Int): Bitmap {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
val renderScript = RenderScript.create(context)
val input = Allocation.createFromBitmap(renderScript, toTransform)
val output = Allocation.createTyped(renderScript, input.type)
val scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript))
scriptIntrinsicBlur.setInput(input)
scriptIntrinsicBlur.setRadius(radius.toFloat())
scriptIntrinsicBlur.forEach(output)
output.copyTo(toTransform)
renderScript.destroy()
return toTransform
} else {
// Fallback to a different blur method or return the original bitmap
return toTransform
}
}
import android.renderscript.RenderScript | ||
import android.renderscript.ScriptIntrinsicBlur | ||
import androidx.annotation.RequiresApi | ||
import android.view.View2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1,231 @@ | |||
package me.bzcoder.easyglide.util |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The package name me.bzcoder.easyglide.util
seems more related to image loading than general utilities. Consider moving this class to a more appropriate package like me.bzcoder.webview.utils
to better reflect its purpose.
package me.bzcoder.easyglide.util | |
package me.bzcoder.webview.utils |
sir[2] = p and 0x0000ff | ||
rinsum += sir[0] | ||
ginsum += sir[1] | ||
binsum += sir[2] | ||
rsum += rinsum | ||
gsum += ginsum | ||
bsum += binsum | ||
stackpointer = (stackpointer + 1) % div | ||
sir = stack[stackpointer % div] | ||
routsum += sir[0] | ||
goutsum += sir[1] | ||
boutsum += sir[2] | ||
rinsum -= sir[0] | ||
ginsum -= sir[1] | ||
binsum -= sir[2] | ||
yi++ | ||
x++ | ||
} | ||
yw += w | ||
y++ | ||
} | ||
x = 0 | ||
while (x < w) { | ||
bsum = 0 | ||
gsum = bsum | ||
rsum = gsum | ||
boutsum = rsum | ||
goutsum = boutsum | ||
routsum = goutsum | ||
binsum = routsum | ||
ginsum = binsum | ||
rinsum = ginsum | ||
yp = -radius * w | ||
i = -radius | ||
while (i <= radius) { | ||
yi = Math.max(0, yp) + x | ||
sir = stack[i + radius] | ||
sir[0] = r[yi] | ||
sir[1] = g[yi] | ||
sir[2] = b[yi] | ||
rbs = r1 - Math.abs(i) | ||
rsum += r[yi] * rbs | ||
gsum += g[yi] * rbs | ||
bsum += b[yi] * rbs | ||
if (i > 0) { | ||
rinsum += sir[0] | ||
ginsum += sir[1] | ||
binsum += sir[2] | ||
} else { | ||
routsum += sir[0] | ||
goutsum += sir[1] | ||
boutsum += sir[2] | ||
} | ||
if (i < hm) { | ||
yp += w | ||
} | ||
i++ | ||
} | ||
yi = x | ||
stackpointer = radius | ||
y = 0 | ||
while (y < h) { | ||
pix[yi] = -0x1000000 and pix[yi] or (dv[rsum] shl 16) or (dv[gsum] shl 8) or dv[bsum] | ||
rsum -= routsum | ||
gsum -= goutsum | ||
bsum -= boutsum | ||
stackstart = stackpointer - radius + div | ||
sir = stack[stackstart % div] | ||
routsum -= sir[0] | ||
goutsum -= sir[1] | ||
boutsum -= sir[2] | ||
if (x == 0) { | ||
vmin[y] = Math.min(y + r1, hm) * w | ||
} | ||
p = x + vmin[y] | ||
sir[0] = r[p] | ||
sir[1] = g[p] | ||
sir[2] = b[p] | ||
rinsum += sir[0] | ||
ginsum += sir[1] | ||
binsum += sir[2] | ||
rsum += rinsum | ||
gsum += ginsum | ||
bsum += binsum | ||
stackpointer = (stackpointer + 1) % div | ||
sir = stack[stackpointer] | ||
routsum += sir[0] | ||
goutsum += sir[1] | ||
boutsum += sir[2] | ||
rinsum -= sir[0] | ||
ginsum -= sir[1] | ||
binsum -= sir[2] | ||
yi += w | ||
y++ | ||
} | ||
x++ | ||
} | ||
toTransform.setPixels(pix, 0, w, 0, 0, w, h) | ||
return toTransform | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The blur
method uses a box blur algorithm. While functional, it involves numerous integer array allocations (pix
, r
, g
, b
, vmin
, dv
, stack
, sir
). Consider exploring optimizations such as reusing arrays or using more efficient data structures to reduce memory overhead and improve performance. Also, consider using Kotlin's built-in array operations for better readability.
No description provided.