Skip to content

Instantly share code, notes, and snippets.

@CoffeeCode
Created May 16, 2014 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CoffeeCode/7f14c606eab8932a6a9e to your computer and use it in GitHub Desktop.
Save CoffeeCode/7f14c606eab8932a6a9e to your computer and use it in GitHub Desktop.
#pragma version(1)
#pragma rs java_package_name(com.ap.wificam)
#pragma rs_fp_imprecise
rs_allocation input;
rs_allocation mask;
rs_allocation histogram_r;
rs_allocation histogram_g;
rs_allocation histogram_b;
uint32_t width;
uint32_t height;
int32_t *laplaceMax;
int32_t *laplaceSum;
void root(uchar4* out, uint32_t x, uint32_t y) {
const int size_x = 5;
const int size_y = 5;
const int offset_x = size_x/2;
const int offset_y = size_y/2;
//Histogram Accumulation
int4 pixel = 0.0f;
pixel = convert_int4(rsGetElementAt_uchar4(input, x, y));
int bucket_r = rsGetElementAt_int(histogram_r, pixel.x, 0);
int bucket_g = rsGetElementAt_int(histogram_g, pixel.y, 0);
int bucket_b = rsGetElementAt_int(histogram_b, pixel.z, 0);
rsSetElementAt_int(histogram_r, bucket_r+1, pixel.x, 0);
rsSetElementAt_int(histogram_g, bucket_g+1, pixel.y, 0);
rsSetElementAt_int(histogram_b, bucket_b+1, pixel.z, 0);
//Laplace Convolution
int4 sum = 0.0f;
for (int yi = 0; yi < size_y; ++yi) {
int yc = y - offset_y + yi;
yc = min(max(yc, 0), (int)height-1);
for (int xi = 0; xi < size_x; ++xi) {
int xc = x - offset_x + xi;
xc = min(max(xc, 0), (int)width-1);
sum += convert_int4(rsGetElementAt_uchar4(input, xc, yc)) *
rsGetElementAt_int(mask, xi, yi);
}
}
int laplaceValue = (sum.x + sum.y + sum.z)/3;
//Calculate maximum Laplace and Laplace Sum
if(laplaceValue > *laplaceMax){
*laplaceMax = laplaceValue;
}
//normalize the laplace image values
sum = min(sum, 255);
sum = max(sum, 0);
sum.w = 255;
int laplaceValueNorm = (sum.x + sum.y + sum.z)/3;
if((laplaceValueNorm > 128))
*laplaceSum = *laplaceSum +1;
if((x == width-1) && (y == height-1)){
rsDebug("RenderScript.convolution.rs.sum", *laplaceSum);
rsDebug("RenderScript.convolution.rs.max", *laplaceMax);
}
if((x == 500) && (y == 400)){
rsDebug("RenderScript.convolution.rs.23 histogram.x at value 50", rsGetElementAt_int(histogram_g, 50, 0));
}
*out = convert_uchar4(sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment