Jason Sams | d85e283 | 2012-09-11 16:04:27 -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 | |
| 18 | #include "rsdCore.h" |
| 19 | #include "rsdIntrinsics.h" |
| 20 | #include "rsdAllocation.h" |
| 21 | |
| 22 | #include "rsdIntrinsicInlines.h" |
| 23 | |
| 24 | using namespace android; |
| 25 | using namespace android::renderscript; |
| 26 | |
| 27 | struct ConvolveParams { |
| 28 | float fp[104]; |
| 29 | short ip[104]; |
| 30 | float radius; |
| 31 | int iradius; |
| 32 | ObjectBaseRef<Allocation> alloc; |
| 33 | }; |
| 34 | |
| 35 | static void ComputeGaussianWeights(ConvolveParams *cp) { |
| 36 | // Compute gaussian weights for the blur |
| 37 | // e is the euler's number |
| 38 | float e = 2.718281828459045f; |
| 39 | float pi = 3.1415926535897932f; |
| 40 | // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 ) |
| 41 | // x is of the form [-radius .. 0 .. radius] |
| 42 | // and sigma varies with radius. |
| 43 | // Based on some experimental radius values and sigma's |
| 44 | // we approximately fit sigma = f(radius) as |
| 45 | // sigma = radius * 0.4 + 0.6 |
| 46 | // The larger the radius gets, the more our gaussian blur |
| 47 | // will resemble a box blur since with large sigma |
| 48 | // the gaussian curve begins to lose its shape |
| 49 | float sigma = 0.4f * cp->radius + 0.6f; |
| 50 | |
| 51 | // Now compute the coefficients. We will store some redundant values to save |
| 52 | // some math during the blur calculations precompute some values |
| 53 | float coeff1 = 1.0f / (sqrtf(2.0f * pi) * sigma); |
| 54 | float coeff2 = - 1.0f / (2.0f * sigma * sigma); |
| 55 | |
| 56 | float normalizeFactor = 0.0f; |
| 57 | float floatR = 0.0f; |
| 58 | int r; |
| 59 | cp->iradius = (float)ceil(cp->radius) + 0.5f; |
| 60 | for (r = -cp->iradius; r <= cp->iradius; r ++) { |
| 61 | floatR = (float)r; |
| 62 | cp->fp[r + cp->iradius] = coeff1 * powf(e, floatR * floatR * coeff2); |
| 63 | normalizeFactor += cp->fp[r + cp->iradius]; |
| 64 | } |
| 65 | |
| 66 | //Now we need to normalize the weights because all our coefficients need to add up to one |
| 67 | normalizeFactor = 1.0f / normalizeFactor; |
| 68 | for (r = -cp->iradius; r <= cp->iradius; r ++) { |
| 69 | cp->fp[r + cp->iradius] *= normalizeFactor; |
| 70 | cp->ip[r + cp->iradius] = (short)(cp->ip[r + cp->iradius] * 32768); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static void Blur_Bind(const Context *dc, const Script *script, |
| 75 | void * intrinsicData, uint32_t slot, Allocation *data) { |
| 76 | ConvolveParams *cp = (ConvolveParams *)intrinsicData; |
| 77 | rsAssert(slot == 1); |
| 78 | cp->alloc.set(data); |
| 79 | } |
| 80 | |
| 81 | static void Blur_SetVar(const Context *dc, const Script *script, void * intrinsicData, |
| 82 | uint32_t slot, void *data, size_t dataLength) { |
| 83 | ConvolveParams *cp = (ConvolveParams *)intrinsicData; |
| 84 | rsAssert(slot == 0); |
| 85 | |
| 86 | cp->radius = ((const float *)data)[0]; |
| 87 | ComputeGaussianWeights(cp); |
| 88 | } |
| 89 | |
| 90 | extern "C" void rsdIntrinsicConvolve3x3_K(void *dst, const void *y0, const void *y1, const void *y2, const short *coef, uint32_t count); |
| 91 | |
| 92 | |
| 93 | static void OneV(const RsForEachStubParamStruct *p, float4 *out, int32_t x, int32_t y, |
| 94 | const uchar *ptrIn, int iStride, const float* gPtr, int iradius) { |
| 95 | |
| 96 | const uchar *pi = ptrIn + x*4; |
| 97 | |
| 98 | float4 blurredPixel = 0; |
| 99 | for (int r = -iradius; r <= iradius; r ++) { |
| 100 | int validY = rsMax((y + r), 0); |
| 101 | validY = rsMin(validY, (int)(p->dimY - 1)); |
| 102 | const uchar4 *pvy = (const uchar4 *)&pi[validY * iStride]; |
| 103 | float4 pf = convert_float4(pvy[0]); |
| 104 | blurredPixel += pf * gPtr[0]; |
| 105 | gPtr++; |
| 106 | } |
| 107 | |
| 108 | out->xyzw = blurredPixel; |
| 109 | } |
| 110 | |
| 111 | static void OneH(const RsForEachStubParamStruct *p, uchar4 *out, int32_t x, |
| 112 | const float4 *ptrIn, const float* gPtr, int iradius) { |
| 113 | |
| 114 | float4 blurredPixel = 0; |
| 115 | for (int r = -iradius; r <= iradius; r ++) { |
| 116 | int validX = rsMax((x + r), 0); |
| 117 | validX = rsMin(validX, (int)(p->dimX - 1)); |
| 118 | float4 pf = ptrIn[validX]; |
| 119 | blurredPixel += pf * gPtr[0]; |
| 120 | gPtr++; |
| 121 | } |
| 122 | |
| 123 | out->xyzw = convert_uchar4(blurredPixel); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static void Blur_uchar4(const RsForEachStubParamStruct *p, |
| 128 | uint32_t xstart, uint32_t xend, |
| 129 | uint32_t instep, uint32_t outstep) { |
| 130 | float buf[4 * 2048]; |
| 131 | ConvolveParams *cp = (ConvolveParams *)p->usr; |
| 132 | DrvAllocation *din = (DrvAllocation *)cp->alloc->mHal.drv; |
| 133 | const uchar *pin = (const uchar *)din->lod[0].mallocPtr; |
| 134 | |
| 135 | uchar4 *out = (uchar4 *)p->out; |
| 136 | uint32_t x1 = xstart; |
| 137 | uint32_t x2 = xend; |
| 138 | |
| 139 | float4 *fout = (float4 *)buf; |
| 140 | while(x2 > x1) { |
| 141 | OneV(p, fout, x1, p->y, pin, din->lod[0].stride, cp->fp, cp->iradius); |
| 142 | fout++; |
| 143 | x1++; |
| 144 | } |
| 145 | |
| 146 | x1 = xstart; |
| 147 | while(x2 > x1) { |
| 148 | OneH(p, out, x1, (float4 *)buf, cp->fp, cp->iradius); |
| 149 | out++; |
| 150 | x1++; |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | void * rsdIntrinsic_InitBlur(const android::renderscript::Context *dc, |
| 156 | android::renderscript::Script *script, |
| 157 | RsdIntriniscFuncs_t *funcs) { |
| 158 | |
| 159 | script->mHal.info.exportedVariableCount = 2; |
| 160 | funcs->bind = Blur_Bind; |
| 161 | funcs->setVar = Blur_SetVar; |
| 162 | funcs->root = Blur_uchar4; |
| 163 | |
| 164 | ConvolveParams *cp = (ConvolveParams *)calloc(1, sizeof(ConvolveParams)); |
| 165 | cp->radius = 5; |
| 166 | ComputeGaussianWeights(cp); |
| 167 | return cp; |
| 168 | } |
| 169 | |
| 170 | |