1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ip.rsh"
18#pragma rs_fp_relaxed
19
20int32_t gWidthIn;
21int32_t gHeightIn;
22rs_allocation gIn;
23float scale;
24
25
26uchar4 __attribute__((kernel)) nearest(uint32_t x, uint32_t y) {
27    float xf = clamp(x * scale, 0.f, (float)gWidthIn - 1.f);
28    float yf = clamp(y * scale, 0.f, (float)gHeightIn - 1.f);
29    uint32_t ix = xf;
30    uint32_t iy = yf;
31
32    uchar4 tmp = rsGetElementAt_uchar4(gIn, ix, iy);
33    tmp.a = 0xff;
34    return tmp;
35}
36
37
38static float4 cubicInterpolate (float4 p0,float4 p1,float4 p2,float4 p3 , float x) {
39    return p1 + 0.5f * x * (p2 - p0 + x * (2.f * p0 - 5.f * p1 + 4.f * p2 - p3
40            + x * (3.f * (p1 - p2) + p3 - p0)));
41}
42
43uchar4 __attribute__((kernel)) bicubic(uint32_t x, uint32_t y) {
44    float xf = (x + 0.5f) * scale - 0.5f;
45    float yf = (y + 0.5f) * scale - 0.5f;
46
47    int startx = (int) floor(xf - 1);
48    int starty = (int) floor(yf - 1);
49    xf = xf - floor(xf);
50    yf = yf - floor(yf);
51    int maxx = gWidthIn - 1;
52    int maxy = gHeightIn - 1;
53
54    uint32_t xs0 = (uint32_t) max(0, startx + 0);
55    uint32_t xs1 = (uint32_t) max(0, startx + 1);
56    uint32_t xs2 = (uint32_t) min(maxx, startx + 2);
57    uint32_t xs3 = (uint32_t) min(maxx, startx + 3);
58
59    uint32_t ys0 = (uint32_t) max(0, starty + 0);
60    uint32_t ys1 = (uint32_t) max(0, starty + 1);
61    uint32_t ys2 = (uint32_t) min(maxy, starty + 2);
62    uint32_t ys3 = (uint32_t) min(maxy, starty + 3);
63
64    float4 p00 = convert_float4(rsGetElementAt_uchar4(gIn, xs0, ys0));
65    float4 p01 = convert_float4(rsGetElementAt_uchar4(gIn, xs1, ys0));
66    float4 p02 = convert_float4(rsGetElementAt_uchar4(gIn, xs2, ys0));
67    float4 p03 = convert_float4(rsGetElementAt_uchar4(gIn, xs3, ys0));
68    float4 p0  = cubicInterpolate(p00, p01, p02, p03, xf);
69
70    float4 p10 = convert_float4(rsGetElementAt_uchar4(gIn, xs0, ys1));
71    float4 p11 = convert_float4(rsGetElementAt_uchar4(gIn, xs1, ys1));
72    float4 p12 = convert_float4(rsGetElementAt_uchar4(gIn, xs2, ys1));
73    float4 p13 = convert_float4(rsGetElementAt_uchar4(gIn, xs3, ys1));
74    float4 p1  = cubicInterpolate(p10, p11, p12, p13, xf);
75
76    float4 p20 = convert_float4(rsGetElementAt_uchar4(gIn, xs0, ys2));
77    float4 p21 = convert_float4(rsGetElementAt_uchar4(gIn, xs1, ys2));
78    float4 p22 = convert_float4(rsGetElementAt_uchar4(gIn, xs2, ys2));
79    float4 p23 = convert_float4(rsGetElementAt_uchar4(gIn, xs3, ys2));
80    float4 p2  = cubicInterpolate(p20, p21, p22, p23, xf);
81
82    float4 p30 = convert_float4(rsGetElementAt_uchar4(gIn, xs0, ys3));
83    float4 p31 = convert_float4(rsGetElementAt_uchar4(gIn, xs1, ys3));
84    float4 p32 = convert_float4(rsGetElementAt_uchar4(gIn, xs2, ys3));
85    float4 p33 = convert_float4(rsGetElementAt_uchar4(gIn, xs3, ys3));
86    float4 p3  = cubicInterpolate(p30, p31, p32, p33, xf);
87
88    float4 p  = cubicInterpolate(p0, p1, p2, p3, yf);
89    p = clamp(p + 0.5f, 0.f, 255.f);
90    return convert_uchar4(p);
91}
92
93