Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:53:52+00:00 2026-06-01T16:53:52+00:00

I have been trying to implement a box blur algorithm in android. The code

  • 0

I have been trying to implement a box blur algorithm in android.
The code seems to be fine but when trying to apply it, some areas in the blurred image have big yellow and white smudges all over the blurred photo.
Can anyone help me find out what i’m doing wrong?
Thanks:

Here is what i have:

public static Bitmap boxBlur(Bitmap bmp, int range) {
    assert (range & 1) == 0 : "Range must be odd.";

    Bitmap blurred = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
            Config.ARGB_8888);
    Canvas c = new Canvas(blurred);

    int w = bmp.getWidth();
    int h = bmp.getHeight();

    int[] pixels = new int[bmp.getWidth() * bmp.getHeight()];
    bmp.getPixels(pixels, 0, w, 0, 0, w, h);

    boxBlurHorizontal(pixels, w, h, range / 2);
    boxBlurVertical(pixels, w, h, range / 2);

    c.drawBitmap(pixels, 0, w, 0.0F, 0.0F, w, h, true, null);

    return blurred;
}

private static void boxBlurHorizontal(int[] pixels, int w, int h,
        int halfRange) {
    int index = 0;
    int[] newColors = new int[w];

    for (int y = 0; y < h; y++) {
        int hits = 0;
        long r = 0;
        long g = 0;
        long b = 0;
        for (int x = -halfRange; x < w; x++) {
            int oldPixel = x - halfRange - 1;
            if (oldPixel >= 0) {
                int color = pixels[index + oldPixel];
                if (color != 0) {
                    r -= Color.red(color);
                    g -= Color.green(color);
                    b -= Color.blue(color);
                }
                hits--;
            }

            int newPixel = x + halfRange;
            if (newPixel < w) {
                int color = pixels[index + newPixel];
                if (color != 0) {
                    r += Color.red(color);
                    g += Color.green(color);
                    b += Color.blue(color);
                }
                hits++;
            }

            if (x >= 0) {
                newColors[x] = Color.argb(0xFF, (byte) (r / hits),
                        (byte) (g / hits), (byte) (b / hits));
            }
        }

        for (int x = 0; x < w; x++) {
            pixels[index + x] = newColors[x];
        }

        index += w;
    }
}

private static void boxBlurVertical(int[] pixels, int w, int h,
        int halfRange) {

    int[] newColors = new int[h];
    int oldPixelOffset = -(halfRange + 1) * w;
    int newPixelOffset = (halfRange) * w;

    for (int x = 0; x < w; x++) {
        int hits = 0;
        long r = 0;
        long g = 0;
        long b = 0;
        int index = -halfRange * w + x;
        for (int y = -halfRange; y < h; y++) {
            int oldPixel = y - halfRange - 1;
            if (oldPixel >= 0) {
                int color = pixels[index + oldPixelOffset];
                if (color != 0) {
                    r -= Color.red(color);
                    g -= Color.green(color);
                    b -= Color.blue(color);
                }
                hits--;
            }

            int newPixel = y + halfRange;
            if (newPixel < h) {
                int color = pixels[index + newPixelOffset];
                if (color != 0) {
                    r += Color.red(color);
                    g += Color.green(color);
                    b += Color.blue(color);
                }
                hits++;
            }

            if (y >= 0) {
                newColors[y] = Color.argb(0xFF, (byte) (r / hits),
                        (byte) (g / hits), (byte) (b / hits));
            }

            index += w;
        }

        for (int y = 0; y < h; y++) {
            pixels[y * w + x] = newColors[y];
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:53:53+00:00Added an answer on June 1, 2026 at 4:53 pm

    Found the problem!
    The line:
    newColors[x] = Color.argb(0xFF, (byte) (r / hits), (byte) (g / hits), (byte) (b / hits));

    I converted the averages to bytes, where they should have been ints.
    Changed it to:

    newColors[x] = Color.argb(0xFF, (int) (r / hits), (int) (g / hits), (int) (b / hits));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to implement unit testing and currently have some code that does
Hello guys, I have been trying to implement the DSUM function but failed to
I have been trying to implement a small SERVER - CLIENT app but ran
I have been trying to implement this code , where i capture a image
I have been trying to implement the delete BST function but I don't know
I have been trying to implement a comment engine in my app (UItableView) but
i'm new to c++ and have been trying to implement the bellman ford algorithm,
I have been trying to implement an efficient string comparing algorithm that will given
I have been trying to implement XOR linked list and its operations but I
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.