1/*
2 * Copyright (C) 2015 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#pragma version(1)
18#pragma rs java_package_name(com.android.example.rscamera)
19#pragma rs_fp_relaxed
20
21rs_allocation gCurrentFrame;
22
23uchar4 __attribute__((kernel)) peak(uint32_t x, uint32_t y) {
24
25    uchar4 curPixel;
26    curPixel.r = rsGetElementAtYuv_uchar_Y(gCurrentFrame, x, y);
27    curPixel.g = rsGetElementAtYuv_uchar_U(gCurrentFrame, x, y);
28    curPixel.b = rsGetElementAtYuv_uchar_V(gCurrentFrame, x, y);
29
30    int dx = x + ((x == 0) ? 1 : -1);
31    int sum = 0;
32    int tmp;
33
34    tmp = rsGetElementAtYuv_uchar_Y(gCurrentFrame, dx, y) - curPixel.r;
35    sum += tmp * tmp;
36    tmp = rsGetElementAtYuv_uchar_U(gCurrentFrame, dx, y) - curPixel.g;
37    sum += tmp * tmp;
38    tmp = rsGetElementAtYuv_uchar_V(gCurrentFrame, dx, y) - curPixel.b;
39    sum += tmp * tmp;
40
41
42    int dy = y + ((y == 0) ? 1 : -1);
43    tmp = rsGetElementAtYuv_uchar_Y(gCurrentFrame, x, dy) - curPixel.r;
44    sum += tmp * tmp;
45    tmp = rsGetElementAtYuv_uchar_U(gCurrentFrame, x, dy) - curPixel.g;
46    sum += tmp * tmp;
47    tmp = rsGetElementAtYuv_uchar_V(gCurrentFrame, x, dy) - curPixel.b;
48    sum += tmp * tmp;
49
50    sum >>= 9;
51    sum *= sum * sum;
52
53    curPixel.a = 255;
54
55    uchar4 mergedPixel = curPixel;
56
57    int4 rgb;
58    rgb.r = mergedPixel.r +
59            mergedPixel.b * 1436 / 1024 - 179 + sum;
60    rgb.g = mergedPixel.r -
61            mergedPixel.g * 46549 / 131072 + 44 -
62            mergedPixel.b * 93604 / 131072 + 91 + sum;
63    rgb.b = mergedPixel.r +
64            mergedPixel.g * 1814 / 1024 - 227;
65    rgb.a = 255;
66
67    // Write out merged HDR result
68    uchar4 out = convert_uchar4(clamp(rgb, 0, 255));
69
70    return out;
71}