是否可以对一系列renderscript操作使用一个分配?

时间:2013-03-21 06:59:03

标签: android renderscript

例如我从位图进行分配,然后对它进行了一次亮度控制,然后在该分配上使用类似的实现,并通过brightnessContrastRs()动作进行更改?

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;
}

RS脚本:

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
}

2 个答案:

答案 0 :(得分:1)

如果你问是否可以通过这样的分配并让事情有效,是的,你当然可以这样做。但是,您不能尽早销毁allocIn,因为脚本在启动时仍需要使用它。

答案 1 :(得分:1)

您应该使用ScriptGroup。