저는 Renderscript를 사용하여 이미지에 가우시안 블러를 적용합니다. 하지만 내가 한 일과 상관 없습니다. ScriptIntrinsicBlur가 더 빠릅니다. 왜 이런 일이 일어 났습니까? ScriptIntrinsicBlur가 다른 방법을 사용하고 있습니까? 이 ID 내 RS 코드 :왜 ScriptIntrinsicBlur가 내 방법보다 빠릅니까?
#pragma version(1)
#pragma rs java_package_name(top.deepcolor.rsimage.utils)
//aussian blur algorithm.
//the max radius of gaussian blur
static const int MAX_BLUR_RADIUS = 1024;
//the ratio of pixels when blur
float blurRatio[(MAX_BLUR_RADIUS << 2) + 1];
//the acquiescent blur radius
int blurRadius = 0;
//the width and height of bitmap
uint32_t width;
uint32_t height;
//bind to the input bitmap
rs_allocation input;
//the temp alloction
rs_allocation temp;
//set the radius
void setBlurRadius(int radius)
{
if(1 > radius)
radius = 1;
else if(MAX_BLUR_RADIUS < radius)
radius = MAX_BLUR_RADIUS;
blurRadius = radius;
/**
calculate the blurRadius by Gaussian function
when the pixel is far way from the center, the pixel will not contribute to the center
so take the sigma is blurRadius/2.57
*/
float sigma = 1.0f * blurRadius/2.57f;
float deno = 1.0f/(sigma * sqrt(2.0f * M_PI));
float nume = -1.0/(2.0f * sigma * sigma);
//calculate the gaussian function
float sum = 0.0f;
for(int i = 0, r = -blurRadius; r <= blurRadius; ++i, ++r)
{
blurRatio[i] = deno * exp(nume * r * r);
sum += blurRatio[i];
}
//normalization to 1
int len = radius + radius + 1;
for(int i = 0; i < len; ++i)
{
blurRatio[i] /= sum;
}
}
/**
the gaussian blur is decomposed two steps:1
1.blur in the horizontal
2.blur in the vertical
*/
uchar4 RS_KERNEL horizontal(uint32_t x, uint32_t y)
{
float a, r, g, b;
for(int k = -blurRadius; k <= blurRadius; ++k)
{
int horizontalIndex = x + k;
if(0 > horizontalIndex) horizontalIndex = 0;
if(width <= horizontalIndex) horizontalIndex = width - 1;
uchar4 inputPixel = rsGetElementAt_uchar4(input, horizontalIndex, y);
int blurRatioIndex = k + blurRadius;
a += inputPixel.a * blurRatio[blurRatioIndex];
r += inputPixel.r * blurRatio[blurRatioIndex];
g += inputPixel.g * blurRatio[blurRatioIndex];
b += inputPixel.b * blurRatio[blurRatioIndex];
}
uchar4 out;
out.a = (uchar) a;
out.r = (uchar) r;
out.g = (uchar) g;
out.b = (uchar) b;
return out;
}
uchar4 RS_KERNEL vertical(uint32_t x, uint32_t y)
{
float a, r, g, b;
for(int k = -blurRadius; k <= blurRadius; ++k)
{
int verticalIndex = y + k;
if(0 > verticalIndex) verticalIndex = 0;
if(height <= verticalIndex) verticalIndex = height - 1;
uchar4 inputPixel = rsGetElementAt_uchar4(temp, x, verticalIndex);
int blurRatioIndex = k + blurRadius;
a += inputPixel.a * blurRatio[blurRatioIndex];
r += inputPixel.r * blurRatio[blurRatioIndex];
g += inputPixel.g * blurRatio[blurRatioIndex];
b += inputPixel.b * blurRatio[blurRatioIndex];
}
uchar4 out;
out.a = (uchar) a;
out.r = (uchar) r;
out.g = (uchar) g;
out.b = (uchar) b;
return out;
}
1. 시험을 어떻게하고 있었습니까? 2. 어떤 하드웨어/에뮬레이터를 테스트하고 있습니까? 3. 장치에있는 경우 - ODM이 응용 프로그램 개발자가 사용할 수없는 추가 하드웨어 리소스로 ScriptIntrinsics를 구현할 수 있다고 생각하십시오. –
나는 진짜 전화로 이미지 (293x220)로 테스트한다. 내 방법 비용은 약 120ms –
ODM의 평균은 무엇입니까? 나는 진짜 전화로 이미지 (293x220)로 테스트하고, 블러 반경은 20이다. 나의 방법 비용은 약 120ms이다. ScriptIntrinsicBlur 약 25ms .i는 copyTo() 메서드가 너무 많은 시간을 소비한다는 것을 알았습니다 (ScriptIntrinsicBlur도이 메서드를 사용하지만 시간이 거의 들지 않았습니다). 그런데 ScriptIntrinsicBlur에 대한 RS 소스 코드를 어디에서 찾을 수 있습니까? –