summaryrefslogtreecommitdiffstats
path: root/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/vertical_blur.rs
blob: 60fd71bf6fd47dde7d791a162251eb2399c94804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma version(1)
#pragma rs_fp_relaxed

#include "ip.rsh"

static float saturation;
static rs_matrix3x3 colorMat;

void setSaturation(float sat) {
    saturation = sat;

    // Saturation
    // Linear weights
    //float rWeight = 0.3086f;
    //float gWeight = 0.6094f;
    //float bWeight = 0.0820f;

    // Gamma 2.2 weights (we haven't converted our image to linear space yet for perf reasons)
    float rWeight = 0.299f;
    float gWeight = 0.587f;
    float bWeight = 0.114f;

    float oneMinusS = 1.0f - saturation;
    rsMatrixSet(&colorMat, 0, 0, oneMinusS * rWeight + saturation);
    rsMatrixSet(&colorMat, 0, 1, oneMinusS * rWeight);
    rsMatrixSet(&colorMat, 0, 2, oneMinusS * rWeight);
    rsMatrixSet(&colorMat, 1, 0, oneMinusS * gWeight);
    rsMatrixSet(&colorMat, 1, 1, oneMinusS * gWeight + saturation);
    rsMatrixSet(&colorMat, 1, 2, oneMinusS * gWeight);
    rsMatrixSet(&colorMat, 2, 0, oneMinusS * bWeight);
    rsMatrixSet(&colorMat, 2, 1, oneMinusS * bWeight);
    rsMatrixSet(&colorMat, 2, 2, oneMinusS * bWeight + saturation);
}

void root(uchar4 *out, const void *usrData, uint32_t x, uint32_t y) {
    const FilterStruct *fs = (const FilterStruct *)usrData;
    float3 blurredPixel = 0;
    const float *gPtr = fs->gaussian;
    if ((y > fs->radius) && (y < (fs->height - fs->radius))) {
        for (int r = -fs->radius; r <= fs->radius; r ++) {
            const float4 *i = (const float4 *)rsGetElementAt(fs->ain, x, y + r);
            blurredPixel += i->xyz * gPtr[0];
            gPtr++;
        }
    } else {
        for (int r = -fs->radius; r <= fs->radius; r ++) {
            int validH = rsClamp((int)y + r, (int)0, (int)(fs->height - 1));
            const float4 *i = (const float4 *)rsGetElementAt(fs->ain, x, validH);
            blurredPixel += i->xyz * gPtr[0];
            gPtr++;
        }
    }

    float3 temp = rsMatrixMultiply(&colorMat, blurredPixel);
    temp = clamp(temp, 0.f, 255.f);
    out->xyz = convert_uchar3(temp);
    out->w = 0xff;
}