colorcube.rs revision 572a5031a5d8602db0bec0b253428a034bd4dd59
1/*
2 * Copyright (C) 2012 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
20
21static rs_allocation gCube;
22static int4 gDims;
23static int4 gCoordMul;
24
25
26void setCube(rs_allocation c) {
27    gCube = c;
28    gDims.x = rsAllocationGetDimX(gCube);
29    gDims.y = rsAllocationGetDimY(gCube);
30    gDims.z = rsAllocationGetDimZ(gCube);
31    gDims.w = 0;
32
33    float4 m = (float4)(1.f / 255.f) * convert_float4(gDims - 1);
34    gCoordMul = convert_int4(m * (float4)0x10000);
35
36    rsDebug("dims", gDims);
37    rsDebug("gCoordMul", gCoordMul);
38}
39
40void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
41    //rsDebug("root", in);
42
43    int4 baseCoord = convert_int4(*in) * gCoordMul;
44    int4 coord1 = baseCoord >> (int4)16;
45    int4 coord2 = min(coord1 + 1, gDims - 1);
46
47    int4 weight2 = baseCoord & 0xffff;
48    int4 weight1 = (int4)0x10000 - weight2;
49
50    uint4 v000 = convert_uint4(rsGetElementAt_uchar4(gCube, coord1.x, coord1.y, coord1.z));
51    uint4 v100 = convert_uint4(rsGetElementAt_uchar4(gCube, coord2.x, coord1.y, coord1.z));
52    uint4 v010 = convert_uint4(rsGetElementAt_uchar4(gCube, coord1.x, coord2.y, coord1.z));
53    uint4 v110 = convert_uint4(rsGetElementAt_uchar4(gCube, coord2.x, coord2.y, coord1.z));
54    uint4 v001 = convert_uint4(rsGetElementAt_uchar4(gCube, coord1.x, coord1.y, coord2.z));
55    uint4 v101 = convert_uint4(rsGetElementAt_uchar4(gCube, coord2.x, coord1.y, coord2.z));
56    uint4 v011 = convert_uint4(rsGetElementAt_uchar4(gCube, coord1.x, coord2.y, coord2.z));
57    uint4 v111 = convert_uint4(rsGetElementAt_uchar4(gCube, coord2.x, coord2.y, coord2.z));
58
59    uint4 yz00 = ((v000 * weight1.x) + (v100 * weight2.x)) >> (int4)8;
60    uint4 yz10 = ((v010 * weight1.x) + (v110 * weight2.x)) >> (int4)8;
61    uint4 yz01 = ((v001 * weight1.x) + (v101 * weight2.x)) >> (int4)8;
62    uint4 yz11 = ((v011 * weight1.x) + (v111 * weight2.x)) >> (int4)8;
63
64    uint4 z0 = ((yz00 * weight1.y) + (yz10 * weight2.y)) >> (int4)16;
65    uint4 z1 = ((yz01 * weight1.y) + (yz11 * weight2.y)) >> (int4)16;
66
67    uint4 v = ((z0 * weight1.z) + (z1 * weight2.z)) >> (int4)16;
68    uint4 v2 = (v + 0x7f) >> (int4)8;
69
70    *out = convert_uchar4(v2);
71    out->a = 0xff;
72
73    #if 0
74    if (in->r != out->r) {
75        rsDebug("dr", in->r - out->r);
76        //rsDebug("in", convert_int4(*in));
77        //rsDebug("coord1", coord1);
78        //rsDebug("coord2", coord2);
79        //rsDebug("weight1", weight1);
80        //rsDebug("weight2", weight2);
81        //rsDebug("yz00", yz00);
82        //rsDebug("z0", z0);
83        //rsDebug("v", v);
84        //rsDebug("v2", v2);
85        //rsDebug("out", convert_int4(*out));
86    }
87    #endif
88}
89
90