11#pragma version(1)
2+ #pragma rs java_package_name(com.android.rs.image)
3+ #pragma rs_fp_relaxed
24
3- #include "ip.rsh"
45
56int height;
67int width;
7- int radius;
8+ static int radius;
89
9- uchar4 * InPixel;
10- uchar4 * OutPixel;
11- float4 * ScratchPixel1;
12- float4 * ScratchPixel2;
10+ rs_allocation InPixel;
11+ rs_allocation ScratchPixel1;
12+ rs_allocation ScratchPixel2;
1313
14- rs_script vBlurScript;
15- rs_script hBlurScript;
16-
17- const int CMD_FINISHED = 1;
14+ const int MAX_RADIUS = 25;
1815
1916// Store our coefficients here
2017static float gaussian[MAX_RADIUS * 2 + 1];
2118
22-
23- static void computeGaussianWeights() {
19+ void setRadius(int rad) {
20+ radius = rad;
2421 // Compute gaussian weights for the blur
2522 // e is the euler's number
2623 float e = 2.718281828459045f;
@@ -45,49 +42,65 @@ static void computeGaussianWeights() {
4542
4643 float normalizeFactor = 0.0f;
4744 float floatR = 0.0f;
48- int r;
49- for (r = -radius; r <= radius; r ++) {
45+ for (int r = -radius; r <= radius; r ++) {
5046 floatR = (float)r;
5147 gaussian[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2);
5248 normalizeFactor += gaussian[r + radius];
5349 }
5450
5551 //Now we need to normalize the weights because all our coefficients need to add up to one
5652 normalizeFactor = 1.0f / normalizeFactor;
57- for (r = -radius; r <= radius; r ++) {
53+ for (int r = -radius; r <= radius; r ++) {
5854 floatR = (float)r;
5955 gaussian[r + radius] *= normalizeFactor;
6056 }
6157}
6258
59+ void copyIn(const uchar4 *in, float4 *out) {
60+ *out = convert_float4(*in);
61+ }
6362
64- static void copyInput() {
65- rs_allocation ain;
66- ain = rsGetAllocation(InPixel);
67- uint32_t dimx = rsAllocationGetDimX(ain);
68- uint32_t dimy = rsAllocationGetDimY(ain);
69- for (uint32_t y = 0; y < dimy; y++) {
70- for (uint32_t x = 0; x < dimx; x++) {
71- ScratchPixel1[x + y * dimx] = convert_float4(InPixel[x + y * dimx]);
63+ void vert(uchar4 *out, uint32_t x, uint32_t y) {
64+ float3 blurredPixel = 0;
65+ const float *gPtr = gaussian;
66+ if ((y > radius) && (y < (height - radius))) {
67+ for (int r = -radius; r <= radius; r ++) {
68+ const float4 *i = (const float4 *)rsGetElementAt(ScratchPixel2, x, y + r);
69+ blurredPixel += i->xyz * gPtr[0];
70+ gPtr++;
71+ }
72+ } else {
73+ for (int r = -radius; r <= radius; r ++) {
74+ int validH = rsClamp((int)y + r, (int)0, (int)(height - 1));
75+ const float4 *i = (const float4 *)rsGetElementAt(ScratchPixel2, x, validH);
76+ blurredPixel += i->xyz * gPtr[0];
77+ gPtr++;
7278 }
7379 }
74- }
7580
76- void filter() {
77- copyInput();
78- computeGaussianWeights();
79-
80- FilterStruct fs;
81- fs.gaussian = gaussian;
82- fs.width = width;
83- fs.height = height;
84- fs.radius = radius;
81+ out->xyz = convert_uchar3(clamp(blurredPixel, 0.f, 255.f));
82+ out->w = 0xff;
83+ }
8584
86- fs.ain = rsGetAllocation(ScratchPixel1);
87- rsForEach(hBlurScript, fs.ain, rsGetAllocation(ScratchPixel2), &fs, sizeof(fs));
85+ void horz(float4 *out, uint32_t x, uint32_t y) {
86+ float3 blurredPixel = 0;
87+ const float *gPtr = gaussian;
88+ if ((x > radius) && (x < (width - radius))) {
89+ for (int r = -radius; r <= radius; r ++) {
90+ const float4 *i = (const float4 *)rsGetElementAt(ScratchPixel1, x + r, y);
91+ blurredPixel += i->xyz * gPtr[0];
92+ gPtr++;
93+ }
94+ } else {
95+ for (int r = -radius; r <= radius; r ++) {
96+ // Stepping left and right away from the pixel
97+ int validX = rsClamp((int)x + r, (int)0, (int)(width - 1));
98+ const float4 *i = (const float4 *)rsGetElementAt(ScratchPixel1, validX, y);
99+ blurredPixel += i->xyz * gPtr[0];
100+ gPtr++;
101+ }
102+ }
88103
89- fs.ain = rsGetAllocation(ScratchPixel2);
90- rsForEach(vBlurScript, fs.ain, rsGetAllocation(OutPixel), &fs, sizeof(fs));
91- //rsSendToClientBlocking(CMD_FINISHED);
104+ out->xyz = blurredPixel;
92105}
93106
0 commit comments