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