in RenderScriptIntrinsic/Application/src/main/java/com/example/android/renderscriptintrinsic/MainActivity.java [170:224]
private void performFilter(Allocation inAllocation,
Allocation outAllocation, Bitmap bitmapOut, float value) {
switch (mFilterMode) {
case MODE_BLUR:
// Set blur kernel size
mScriptBlur.setRadius(value);
// Invoke filter kernel
mScriptBlur.setInput(inAllocation);
mScriptBlur.forEach(outAllocation);
break;
case MODE_CONVOLVE: {
@SuppressWarnings("UnnecessaryLocalVariable")
float f1 = value;
float f2 = 1.0f - f1;
// Emboss filter kernel
float coefficients[] = {-f1 * 2, 0, -f1, 0, 0, 0, -f2 * 2, -f2, 0,
0, -f1, -f2, 1, f2, f1, 0, 0, f2, f2 * 2, 0, 0, 0, f1, 0,
f1 * 2,};
// Set kernel parameter
mScriptConvolve.setCoefficients(coefficients);
// Invoke filter kernel
mScriptConvolve.setInput(inAllocation);
mScriptConvolve.forEach(outAllocation);
break;
}
case MODE_COLORMATRIX: {
// Set HUE rotation matrix
// The matrix below performs a combined operation of,
// RGB->HSV transform * HUE rotation * HSV->RGB transform
float cos = (float) Math.cos((double) value);
float sin = (float) Math.sin((double) value);
Matrix3f mat = new Matrix3f();
mat.set(0, 0, (float) (.299 + .701 * cos + .168 * sin));
mat.set(1, 0, (float) (.587 - .587 * cos + .330 * sin));
mat.set(2, 0, (float) (.114 - .114 * cos - .497 * sin));
mat.set(0, 1, (float) (.299 - .299 * cos - .328 * sin));
mat.set(1, 1, (float) (.587 + .413 * cos + .035 * sin));
mat.set(2, 1, (float) (.114 - .114 * cos + .292 * sin));
mat.set(0, 2, (float) (.299 - .3 * cos + 1.25 * sin));
mat.set(1, 2, (float) (.587 - .588 * cos - 1.05 * sin));
mat.set(2, 2, (float) (.114 + .886 * cos - .203 * sin));
mScriptMatrix.setColorMatrix(mat);
// Invoke filter kernel
mScriptMatrix.forEach(inAllocation, outAllocation);
}
break;
}
// Copy to bitmap and invalidate image view
outAllocation.copyTo(bitmapOut);
}