Android Question Bitmap Brightness

Panagitois Lazos

Member
Licensed User
After a few research i found that the fastest way to do that is Renderscript.
Well i did find the proper RS and java to do it but i was unable ro make it work in b4a.
So here is the java
B4X:
Public Bitmap brightnessContrastRs(Bitmap bmIn, Int brightness, Int contrast)
{
    Bitmap bmOut = Bitmap.createBitmap(bmIn.getWidth(), bmIn.getHeight(),
            bmIn.getConfig());
    Allocation allocIn;
    allocIn = Allocation.createFromBitmap(rs, bmIn,
            Allocation.MipmapControl.MIPMAP_NONE,
            Allocation.USAGE_SCRIPT);
    Allocation allocOut = Allocation.createTyped(rs, allocIn.GetType());

    scriptCBrightnessContrast.set_in(allocIn);
    allocIn.destroy();
    scriptCBrightnessContrast.set_out(allocOut);
    scriptCBrightnessContrast.set_script(scriptCBrightnessContrast);
    Float rowContrast = ((100.0f + contrast) * (100.0f + contrast) / 10000.0f);
    Float rowBrightness = brightness / 255.f;
    scriptCBrightnessContrast.set_rowBrightness(rowBrightness);
    scriptCBrightnessContrast.set_rowContrast(rowContrast);
    scriptCBrightnessContrast.invoke_filter();
    allocOut.copyTo(bmOut);
    allocOut.destroy();
    Return bmOut;
}
and there is the Renderscript
B4X:
rs_allocation out;
rs_allocation In;
rs_script script;

Float rowBrightness;
Float rowContrast;

void root(Const uchar4* v_in, uchar4* v_out, Const void* usrData, uint32_t x,
          uint32_t y)
{
   float4 current = rsUnpackColor8888(*v_in);

   current.r = clamp(((clamp(current.r + rowBrightness, 0.0f, 1.0f) - 0.5f) * rowContrast + 0.5f), 0.0f, 1.0f);
   current.g = clamp(((clamp(current.g + rowBrightness, 0.0f, 1.0f)- 0.5f) * rowContrast + 0.5f), 0.0f, 1.0f);
   current.b = clamp(((clamp(current.b + rowBrightness, 0.0f, 1.0f) - 0.5f) * rowContrast + 0.5f), 0.0f, 1.0f);

   *v_out = rsPackColorTo8888(current.r, current.g, current.b, current.a);
}
void filter()
{
    #If !defined(RS_VERSION) || (RS_VERSION < 14)
       rsForEach(script, In, out, 0);
    #else
       rsForEach(script, In, out);
    #endif
}
does anyone done anything similar cause im completely confused on how to implement it in b4a.
Thanx guys!
 

Erel

Administrator
Staff member
Licensed User
Can you tell more about this?
The idea is to allow you to add Java (or Objective C for B4i) code inside the project. You will then be able to call this code with CallSub or JavaObject.

B4X:
#If JAVA
public void SomeMethod(...) {
 ...
}
#End If
When we will get this Update?
It will take some time.
 

Informatix

Expert
Licensed User
After a few research i found that the fastest way to do that is Renderscript.
In fact, the best way is to use libGDX with shaders (or directly OpenGL with shaders). There's a demo changing brightness and contrast provided with the library. It's so fast that it can be used in real-time.
 

Informatix

Expert
Licensed User
Shaders are working Realy Fast my problem is that i want to change a bitmaps brightness witch i m displaying on a TouchImageView ,
so is there a way to change brightness of a bitmap in libgdx and then load it on a TouchImageView?
You don't need a TouchImageView if you use libGDX. Zooming in/out is very easy with libGDX.
 

Panagitois Lazos

Member
Licensed User
So im Trying to use Simply Library Compiler to create a small lib with renderscript , the rs (kernel) file need to be in the root of the Java file
so it will be reflected with scriptC so how can i do that in library compiler ?.
any ideas ?
Please Forgive me if any of dont make sence itm my first try in library making , and i dont know java :p
 

Informatix

Expert
Licensed User
So im Trying to use Simply Library Compiler to create a small lib with renderscript , the rs (kernel) file need to be in the root of the Java file
so it will be reflected with scriptC so how can i do that in library compiler ?.
any ideas ?
Please Forgive me if any of dont make sence itm my first try in library making , and i dont know java :p
Did you see this thread?
 
Top