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 19uchar __attribute__((kernel)) genRand() { 20 return (uchar)rsRand(0xff); 21} 22 23/* 24 * Convolution matrix of distance 2 with fixed point of 'kShiftBits' bits 25 * shifted. Thus the sum of this matrix should be 'kShiftValue'. Entries of 26 * small values are not calculated to gain efficiency. 27 * The order ot pixels represented in this matrix is: 28 * 1 2 3 29 * 4 0 5 30 * 6 7 8 31 * and the matrix should be: {230, 56, 114, 56, 114, 114, 56, 114, 56}. 32 * However, since most of the valus are identical, we only use the first three 33 * entries and the entries corresponding to the pixels is: 34 * 1 2 1 35 * 2 0 2 36 * 1 2 1 37 */ 38 39int32_t gWMask; 40int32_t gHMask; 41 42rs_allocation gBlendSource; 43uchar __attribute__((kernel)) blend9(uint32_t x, uint32_t y) { 44 uint32_t x1 = (x-1) & gWMask; 45 uint32_t x2 = (x+1) & gWMask; 46 uint32_t y1 = (y-1) & gHMask; 47 uint32_t y2 = (y+1) & gHMask; 48 49 uint p00 = 56 * rsGetElementAt_uchar(gBlendSource, x1, y1); 50 uint p01 = 114 * rsGetElementAt_uchar(gBlendSource, x, y1); 51 uint p02 = 56 * rsGetElementAt_uchar(gBlendSource, x2, y1); 52 uint p10 = 114 * rsGetElementAt_uchar(gBlendSource, x1, y); 53 uint p11 = 230 * rsGetElementAt_uchar(gBlendSource, x, y); 54 uint p12 = 114 * rsGetElementAt_uchar(gBlendSource, x2, y); 55 uint p20 = 56 * rsGetElementAt_uchar(gBlendSource, x1, y2); 56 uint p21 = 114 * rsGetElementAt_uchar(gBlendSource, x, y2); 57 uint p22 = 56 * rsGetElementAt_uchar(gBlendSource, x2, y2); 58 59 p00 += p01; 60 p02 += p10; 61 p11 += p12; 62 p20 += p21; 63 64 p22 += p00; 65 p02 += p11; 66 67 p20 += p22; 68 p20 += p02; 69 70 p20 = min(p20 >> 10, (uint)255); 71 return (uchar)p20; 72} 73 74float gNoiseStrength; 75 76rs_allocation gNoise; 77uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t x, uint32_t y) { 78 float4 ip = convert_float4(in); 79 float pnoise = (float) rsGetElementAt_uchar(gNoise, x & gWMask, y & gHMask); 80 81 float energy_level = ip.r + ip.g + ip.b; 82 float energy_mask = (28.f - sqrt(energy_level)) * 0.03571f; 83 pnoise = (pnoise - 128.f) * energy_mask; 84 85 ip += pnoise * gNoiseStrength; 86 ip = clamp(ip, 0.f, 255.f); 87 88 uchar4 p = convert_uchar4(ip); 89 p.a = 0xff; 90 return p; 91} 92