Project

General

Profile

« Previous | Next » 

Revision a225e5aa

Added by Leszek Koltunski almost 8 years ago

Separable Gaussian Blur with linear sampling done.

Linear sampling implemented - i.e. blurring by a (2N+1)x(2N+1) gaussian kernel (centeral pixel + N pixels in each direction) requires exactly 2N + (N%2==1 ? 4:2) texture fetches.

View differences:

src/main/java/org/distorted/library/EffectQueuePostprocess.java
43 43

  
44 44
class EffectQueuePostprocess extends EffectQueue
45 45
  {
46
  private static final float GAUSSIAN[] =   // G(0.00), G(0.03), G(0.06), ..., G(3.00) where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2)
47
    {
46
  private static final float GAUSSIAN[] =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
47
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
48 48
    0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
49 49
    0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
50 50
    0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
......
55 55
    0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
56 56
    0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
57 57
    0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
58
    0.004432f
58
    0.004432f, 0.000000f
59 59
    };
60
  private static final float NUM_GAUSSIAN = GAUSSIAN.length-1;
60
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
61 61

  
62 62
  private static final int MAX_BLUR = 50;
63 63

  
......
175 175
    {
176 176
    if( radius>=MAX_BLUR ) radius = MAX_BLUR-1;
177 177

  
178
    // Box Blur size 'radius'
179
/*
178 180
    for(int i=0; i<=radius; i++)
179 181
      {
180
      // Box Blur size 'radius'
181 182
      mWeights[i] = 1.0f / (2.0f*radius+1.0f);
182 183
      mOffsets[i] = i*height;
184
      }
185
*/
186
    // Gaussian Blur size 'radius'
187

  
188
    float P = (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
189
    float x = 0.0f;
190
    mWeights[0] = GAUSSIAN[0];
191
    mOffsets[0] = 0.0f;
192
    float sum = GAUSSIAN[0];
193
    int j;
194
    float z;
195

  
196
    for(int i=1; i<=radius; i++)
197
      {
198
      x += P;
199
      j = (int)x;
200
      z = x-j;
183 201

  
184
      // Gaussian Blur size 'radius'
202
      mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
203
      sum += 2*mWeights[i];
204
      mOffsets[i] = i*height;
205
      }
185 206

  
207
    for(int i=0; i<=radius; i++)
208
      {
209
      mWeights[i] /= sum;
210
      }
186 211

  
212
    // squash the weights and offsets for linear sampling
213
    int numloops = radius/2;
214

  
215
    for(int i=0; i<numloops; i++)
216
      {
217
      mOffsets[i+1] = mWeights[2*i+1]*mOffsets[2*i+1] + mWeights[2*i+2]*mOffsets[2*i+2];
218
      mWeights[i+1] = mWeights[2*i+1] + mWeights[2*i+2];
219
      mOffsets[i+1] /= mWeights[i+1];
220
      }
221

  
222
    if( radius%2 == 1 )
223
      {
224
      int index = radius/2 +1;
225
      mOffsets[index]=mOffsets[radius];
226
      mWeights[index]=mWeights[radius];
227
      radius = numloops+1;
228
      }
229
    else
230
      {
231
      radius = numloops;
187 232
      }
188 233

  
189 234
    return radius;

Also available in: Unified diff