123e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams/*
223e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * Copyright (C) 2012 The Android Open Source Project
323e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams *
423e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
523e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * you may not use this file except in compliance with the License.
623e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * You may obtain a copy of the License at
723e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams *
823e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
923e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams *
1023e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * Unless required by applicable law or agreed to in writing, software
1123e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
1223e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1323e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * See the License for the specific language governing permissions and
1423e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams * limitations under the License.
1523e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams */
1623e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
1723e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams#include "ip.rsh"
1823e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams#pragma rs_fp_relaxed
1923e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
2023e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Samsrs_allocation gBlur;
2123e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
2223e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Samsstatic float gOverWm1;
2323e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Samsstatic float gOverHm1;
2423e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Samsstatic uchar gLutR[256];
2523e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Samsstatic uchar gLutG[256];
2623e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Samsstatic uchar gLutB[256];
2723e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
2823e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Samsvoid setup() {
2923e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    int w = rsAllocationGetDimX(gBlur);
3023e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    int h = rsAllocationGetDimY(gBlur);
3123e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    gOverWm1 = 1.f / w;
3223e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    gOverHm1 = 1.f / h;
3323e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
3423e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    for (int x=0; x < 256; x++) {
3523e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams        gLutR[x] = x;//255-x;
3623e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams        gLutG[x] = x;//255-x;
3723e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams        gLutB[x] = x;//255-x;
3823e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    }
3923e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams}
4023e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
41025b5f82971c431eb22df3c9d0f00b3cbe426bdbChris Wailesuchar4 RS_KERNEL process(uchar4 in, uint32_t x, uint32_t y) {
4223e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    float2 xyDist;
4323e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    xyDist.x = (x * gOverWm1 - 0.5f);
4423e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    xyDist.y = (y * gOverHm1 - 0.5f);
4523e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
4623e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    // color
4723e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    float4 v1 = rsUnpackColor8888(in);
4823e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    float4 v2 = rsUnpackColor8888(rsGetElementAt_uchar4(gBlur, x, y));
4923e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
5023e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    float dist = dot(xyDist, xyDist) * 1.4f;
5123e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    float pdist = native_powr(dist, 2.7f * 0.5f);
5223e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    //float pdist = powr(dist, 2.7f * 0.5f);
5323e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
5423e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    pdist = clamp(pdist, 0.f, 1.f);
5523e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    v1 = mix(v1, v2, dist * 2.f);
5623e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    v1 *= 1.f - pdist;
5723e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
5823e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    // apply curve
5923e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    uchar4 out = rsPackColorTo8888(v1);
6023e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
6123e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    out.r = gLutR[out.r];
6223e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    out.g = gLutG[out.g];
6323e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    out.b = gLutB[out.b];
6423e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams    return out;
6523e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams}
6623e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
6723e1074f29f431f68c6b3230c1315ea0f7c7bc86Jason Sams
68