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

  • SEARCH
  • Home
  • 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 7973185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:02:37+00:00 2026-06-04T08:02:37+00:00

I found there’s lacking good documentation in RenderScript, for what I know, forEach in

  • 0

I found there’s lacking good documentation in RenderScript, for what I know, forEach in RS is to execute the root() for each individual item in the allocation.

I am trying to make a library for Renderscript that does Image processing, as a starting point, I reached this great answer. But the problem, is that the blur operation is on Each pixel and each pixel requires another loop (n with blur width) of calculation. Although running on multi-core, it is still a bit too slow.

I am trying to modify it to allow (two-pass) box filter, but that requires working on a single row or column instead of cell. So, is there any way to ask foreach to send an array to root()?

  • 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-04T08:02:39+00:00Added an answer on June 4, 2026 at 8:02 am

    rsForEach can only operate upon Allocations.

    If you want to have the rsForEach function call root() for each of the image rows you have to pass in an Allocation that is sized to be the same length as the number of rows and then work out which row you should be operating on inside root() (similarly for operating on each column). RenderScript should then divide up the work to run on the resources available (more than one row being processed at the same time on multi core devices).

    One way you could do that is by passing in an Allocation that give the offsets (within the image data array) of the image rows. The v_in argument inside the root() will then be the row offset. Since the Allocations the rsForEach call is operating upon is not the image data you cannot write the image out using the v_out argument and you must bind the output image separately.

    Here is some RenderScript that show this:

    #pragma version(1)
    #pragma rs java_package_name(com.android.example.hellocompute)
    
    rs_allocation gIn;
    rs_allocation gOut;
    rs_script gScript;
    
    int mImageWidth;
    const uchar4 *gInPixels;
    uchar4 *gOutPixels;
    
    void init() {
    }
    
    static const int kBlurWidth = 20;
    
    //
    // This is called per row.
    // The row indices are passed in as v_in or you could also use the x argument and multiply it by image width.
    //
    void root(const int32_t *v_in, int32_t *v_out, const void *usrData, uint32_t x, uint32_t y) {
        float3 blur[kBlurWidth];
        float3 cur_colour = {0.0f, 0.0f, 0.0f};
    
        for ( int i = 0; i < kBlurWidth; i++) {
            float3 init_colour = {0.0f, 0.0f, 0.0f};
            blur[i] = init_colour;
        }
    
        int32_t row_index = *v_in;
        int blur_index = 0;
    
        for ( int i = 0; i < mImageWidth; i++) {
            float4 pixel_colour = rsUnpackColor8888(gInPixels[i + row_index]);
    
            cur_colour -= blur[blur_index];
            blur[blur_index] = pixel_colour.rgb;
            cur_colour += blur[blur_index];
    
            blur_index += 1;
            if ( blur_index >= kBlurWidth) {
                blur_index = 0;
            }
    
            gOutPixels[i + row_index] = rsPackColorTo8888(cur_colour/(float)kBlurWidth);
            //gOutPixels[i + row_index] = rsPackColorTo8888(pixel_colour);
        }
    }
    
    
    void filter() {
        rsDebug("Number of rows:", rsAllocationGetDimX(gIn));
        rsForEach(gScript, gIn, gOut, NULL);
    }
    

    This would be setup using the following Java:

        mBlurRowScript = new ScriptC_blur_row(mRS, getResources(), R.raw.blur_row);
    
        int row_width = mBitmapIn.getWidth();
    
        //
        // Create an allocation that indexes each row.
        //
        int num_rows = mBitmapIn.getHeight();
        int[] row_indices = new int[num_rows];
        for ( int i = 0; i < num_rows; i++) {
            row_indices[i] = i * row_width;
        }
        Allocation row_indices_alloc = Allocation.createSized( mRS, Element.I32(mRS), num_rows, Allocation.USAGE_SCRIPT);
        row_indices_alloc.copyFrom(row_indices);
    
        //
        // The image data has to be bound to the pointers within the RenderScript so it can be accessed
        // from the root() function.
        //
        mBlurRowScript.bind_gInPixels(mInAllocation);
        mBlurRowScript.bind_gOutPixels(mOutAllocation);
    
        // Pass in the image width
        mBlurRowScript.set_mImageWidth(row_width);
    
        //
        // Pass in the row indices Allocation as the input. It is also passed in as the output though the output is not used.
        //
        mBlurRowScript.set_gIn(row_indices_alloc);
        mBlurRowScript.set_gOut(row_indices_alloc);
        mBlurRowScript.set_gScript(mBlurRowScript);
        mBlurRowScript.invoke_filter();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found there are two ways (submit and execute) to add a Runnable into
I read the apple documentation and some other links and found there are examples
I found there are also a pthread in Boost library, is it the same
Every LINQ blog I found there seemed around 2 years old, I understand the
After running exec sp_lock on one of our databases we found there's close to
I am a C# beginner. I found there are 2 way to write codes
When sniffing Gmail and Facebook traffic, I found there are leading deadlock code before
Hi I have created a java application. I found there is an application menu
I'm wondering what are some common misconceptions of jQuery? I've found there is a
I am reading a open source project, and I found there is a function

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.