download filterBlur.pas
Language: Delphi
Copyright: (C) 2004 Durand Emmanuel (C) 2004 Burgel Eric
LOC: 368
Project Info
Filters
Server: SourceForge
Type: cvs
...ilters\Filters1\Src\Delphi\
   ...ractfilterNeighbor3.pas
   Chronometer.pas
   divers.pas
   filter.pas
   filterAdjust.pas
   filterArithmeticAdd.pas
   ...ithmeticConstantAdd.pas
   ...ArithmeticSubstract.pas
   filterBlobBalance.pas
   filterBlobExplorer.pas
   filterBlobGrouping.pas
   ...erBlobRepositioning.pas
   ...rBlobRepositioning2.pas
   filterBlur.pas
   filterCanny.pas
   filterContour.pas
   filterContrastExplorer.pas
   filterConvolution.pas
   filterCoocurenceMatrix.pas
   filterCopy.pas
   filterCorrelation.pas
   filterCutter.pas
   filterDistancesMap.pas
   filterExplorer.pas
   ...nisotropicDiffusion.pas
   ...GranularityExplorer.pas
   filterHistogram.pas
   ...erHistogramContrast.pas
   filterImageCreator.pas
   filterImageLoader.pas
   filterImageSaver.pas
   filterIntegration.pas
   filterInvert.pas
   filterLocalDeviation.pas
   filterLogPolar.pas
   filterMedian.pas
   filterMorphology.pas
   ...onMaximaSuppression.pas
   filterNormalize.pas
   filterOnOffCell.pas
   filterProjectionLine.pas
   filterPyramid.pas
   filterRescaleIntensity.pas
   filterResize.pas
   filterRotation.pas
   filterSigmoid.pas
   filterSmoothBilateral.pas
   filterSobel.pas
   filterSPV.pas
   filterStackProcessor.pas
   filterStackSmasher.pas
   ...erStandardDeviation.pas
   filterSUSAN.pas
   filterThresholdBinary.pas
   filterVectorHistogram.pas
   filterWavelets.pas
   filterWaves.pas
   fmask.pas
   fparameters.pas
   image.pas
   imageIO.pas
   imageIOVideo.pas
   lacModel.pas
   polygonalyzation.pas
   wrapper_itk.pas
   wrapper_opencv.pas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
unit filterBlur ;
(* ***** BEGIN LICENSE BLOCK *****
 * Copyright (C) 2004 Durand Emmanuel
 * Copyright (C) 2004 Burgel Eric
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact :
 *   filters@edurand.com
 *   filters@burgel.com
 *
 * ***** END LICENSE BLOCK ***** *)

{
 eburgel (filters@burgel.com)
}

interface
uses
  filter, fparameters, image;

type
  TFilterBlur = class(TFilter)
  public
    constructor Create; override;
    procedure Run(); override;
    procedure RunGaussian() ;
    procedure RunFastBlur() ;
    procedure RunFastAnisotropicBlur();
  private
    parameterImageIn, parameterImageOut : TParameterImage;
    parameterRadius : TParameterInteger ;
    parameterBlurType : TParameterInteger ;
    hMultA : array of Integer ;
    vMultA : array of Integer ;
    imgIn, imgOut : PBitmap32 ;
    procedure blurHLineAnisotropic(pDest, pSrc :PColor32Array ; width:Integer ; radius:Integer) ;
    procedure blurVLineAnisotropic(pDest, pSrc :PColor32Array ; width, height:Integer ; radius:Integer) ;
  end;

implementation

uses Windows, Math ;

constructor TFilterBlur.Create;
begin
  inherited;
  parameterImageIn:=addParameterImage('inImage', 'input image to blur');
  parameterImageOut:=addParameterImage('outImage', 'blur result image');
  parameterRadius := addParameterInteger('radius', 'radius', 0, 255, 128) ;
  parameterBlurType := addParameterInteger('mode', 'blur type', 0, 2, 0) ;
end;

procedure TFilterBlur.Run();
Begin
  imgIn  := parameterImageIn.Image ;
  imgOut := parameterImageOut.Image ;
  if (imgIn<>nil) and (imgOut<>nil) then begin
    case parameterBlurType.Value of
      0 : RunFastBlur();
      1 : RunFastAnisotropicBlur();
      2 : RunGaussian() ;
    end;
  end ;
End ;


// rather fast Blur
procedure blurLine(pDest, pSrc :PColor32Array ; width:Integer ; radius:Cardinal) ;
Var
  x : Cardinal ;
  red, green, blue : Cardinal ;
  cumulRed, cumulGreen, cumulBlue : Cardinal ;
  multA, multB : Cardinal ;
  color : Cardinal ;
Begin
  multA := radius ;
  multB := $100-radius ;

  //cumulRed := ((pSrc^[0] shr 16) and $FF)*$100 ;
  cumulRed := ((pSrc^[0] shr 16) and $FF) ;
  cumulGreen := (pSrc^[0] shr 8) and $FF ;
  cumulBlue := pSrc^[0] and $FF ;

  for x:=0 to Width-1 do Begin
    // Horizontal blur
    //red := (pSrc^[0] shr 8) and $FF00  ;
    red := (pSrc^[0] shr 16) and $FF ;
    green := (pSrc^[0] shr 8) and $FF ;
    blue := pSrc^[0] and $FF ;

    cumulRed := (cumulRed * multA + red * multB+$80) div $100 ;
    cumulGreen := (cumulGreen * multA + Green * multB+$80) div $100  ;
    cumulBlue := (cumulBlue * multA + Blue * multB+$80) div $100 ;

    color := ((cumulRed) shl 16) or (cumulGreen shl 8) or (cumulBlue) ;
    pDest^[0] := color ;
    inc(pSrc) ;
    inc(pDest) ;
  End ;

  dec(pDest) ;
  pSrc := pDest ;
  cumulRed := ((pSrc^[0] shr 16) and $FF) ;
  cumulGreen := (pSrc^[0] shr 8) and $FF ;
  cumulBlue := pSrc^[0] and $FF ;
  for x:=Width-1 Downto 0 do Begin
    // Horizontal blur
    red := (pSrc^[0] shr 16) and $FF ;
    green := (pSrc^[0] shr 8) and $FF ;
    blue := pSrc^[0] and $FF ;
    //DivMod((cumulRed * multA + red * multB), $1000, cumulRed,
    cumulRed := (cumulRed * multA + red * multB+$80) div $100 ;
    cumulGreen := (cumulGreen * multA + Green * multB+$80) div $100  ;
    cumulBlue := (cumulBlue * multA + Blue * multB+$80) div $100 ;

    color := (cumulRed shl 16) or (cumulGreen shl 8) or (cumulBlue) ;
    pDest^[0] := color ;
    dec(pSrc) ;
    dec(pDest) ;
  End ;

End ;

procedure TFilterBlur.RunFastBlur();
var
  pSrc, PDest, PLine : PColor32Array;
  x, y, maxX, maxY : Cardinal ;
  color : Cardinal ;
  radius : Cardinal ;
  multA, multB : Cardinal ;
  red, green, blue : Cardinal ;
  cumulRed, cumulGreen, cumulBlue : Cardinal ;
  imgIn_Hline : PColor32Array;
begin
  maxX := scanWidth(imgIn)-1 ;
  maxY := scanHeight(imgIn)-1 ;

  setlength(hMultA, maxX+1) ;
  setlength(vMultA, maxY+1) ;

  radius := parameterRadius.Value ;
  multA := radius ;
  multB := 256-radius ;

  //pLine := imgIn.Hline ;
  //pSrc := scanLine(imgIn, 0) ;
  //pDest := scanLine(imgOut, 0) ;

  GetMem(imgIn_Hline, imgIn.Width*SizeOf(TColor32)) ;
  FillChar(imgIn_Hline^, imgIn.Width*SizeOf(TColor32), 0);


  for y:=0 to maxY do Begin
    pSrc := scanLine(imgIn, y) ;
    pDest := scanLine(imgOut, y) ;
    pLine := imgIn_Hline ;

    blurLine(pDest, pSrc, maxX+1, radius) ;

    for x:=RegionOfInterest.Left to maxX do Begin

      cumulRed := (pLine^[0] shr 16) and $FF ;
      cumulGreen := (pLine^[0] shr 8) and $FF ;
      cumulBlue := pLine^[0] and $FF ;

      // Vertical blur
      red := (((pDest^[0] shr 16) and $FF) * multB + cumulRed * multA) div $100 ;
      green := (((pDest^[0] shr 8) and $FF) * multB + cumulGreen * multA) div $100 ;
      blue := ((pDest^[0] and $FF) * multB + cumulBlue * multA) div $100 ;

      color := (red shl 16) or (green shl 8) or (blue) ;
      pLine^[0] := color ;
      //pDest^[0] := pLine^[0] ;
      pDest^[0] := color ;

      inc(pDest) ;
      //inc(pSrc) ;
      inc(pLine) ;
    End ;
  end ;

  for y:=maxY downto 0 do Begin
//    pSrc := scanLine(imgIn, y) ;
    pDest := scanLine(imgOut, y) ;
    pLine := imgIn_Hline ;

    for x:=RegionOfInterest.Left to maxX do Begin

      cumulRed := (pLine^[0] shr 16) and $FF ;
      cumulGreen := (pLine^[0] shr 8) and $FF ;
      cumulBlue := pLine^[0] and $FF ;

      // Vertical blur
      red := (((pDest^[0] shr 16) and $FF) * multB + cumulRed * multA) div $100 ;
      green := (((pDest^[0] shr 8) and $FF) * multB + cumulGreen * multA) div $100 ;
      blue := ((pDest^[0] and $FF) * multB + cumulBlue * multA) div $100 ;

      color := (red shl 16) or (green shl 8) or (blue) ;
      pLine^[0] := color ;
      pDest^[0] := color ;

      inc(pDest) ;
//      inc(pSrc) ;
      inc(pLine) ;
    End ;
  End ;

  FreeMem( imgIn_Hline );

end;

{************************************************************************
 * Anisotropic version of fast blur                                     *
 ************************************************************************}

// Anisotropic version
procedure TFilterBlur.blurHLineAnisotropic(pDest, pSrc :PColor32Array ; width:Integer ; radius:Integer) ;
Var
  x : Cardinal ;
  red, green, blue : Integer ;
  cumulRed, cumulGreen, cumulBlue : Integer ;
  color : Cardinal ;
  multA, multB : Integer ;
  diffIntensity, curIntensity, prevIntensity : Integer ;
  pSrcVar : PColor32Array ;
Begin
  prevIntensity := 1000 ;
  pSrcVar := pSrc ;
  for x:=0 to Width-1 do Begin
    curIntensity := Intensity(pSrcVar^[0]) ;
    diffIntensity := Abs(curIntensity-prevIntensity) ;
    multA := radius - diffIntensity*4 ;
    if multA > 200 then multA := 200 else if multA < 0 then multA := 0 ;
    hMultA[x] := multA ;
    prevIntensity := curIntensity ;
    inc(pSrcVar) ;
  end ;

  cumulRed := ((pSrc^[0] shr 16) and $FF) ;
  cumulGreen := (pSrc^[0] shr 8) and $FF ;
  cumulBlue := pSrc^[0] and $FF ;

  for x:=0 to Width-1 do Begin
    // Horizontal blur
    // color extraction
    red := (pSrc^[0] shr 16) and $FF ;
    green := (pSrc^[0] shr 8) and $FF ;
    blue := pSrc^[0] and $FF ;

    multA := hMultA[x] ;
    multB := $100-multA ;

    cumulRed := (cumulRed * multA + red * multB+$80) div $100 ;
    cumulGreen := (cumulGreen * multA + Green * multB+$80) div $100  ;
    cumulBlue := (cumulBlue * multA + Blue * multB+$80) div $100 ;

    color := ((cumulRed) shl 16) or (cumulGreen shl 8) or (cumulBlue) ;
    pDest^[0] := color ;
    inc(pSrc) ;
    inc(pDest) ;
  End ;

  dec(pDest) ;
  pSrc := pDest ;
  cumulRed := ((pSrc^[0] shr 16) and $FF) ;
  cumulGreen := (pSrc^[0] shr 8) and $FF ;
  cumulBlue := pSrc^[0] and $FF ;
  for x:=Width-1 Downto 0 do Begin
    // Horizontal blur
    red := (pSrc^[0] shr 16) and $FF ;
    green := (pSrc^[0] shr 8) and $FF ;
    blue := pSrc^[0] and $FF ;

    multA := hMultA[x] ;
    multB := $100-multA ;

    //DivMod((cumulRed * multA + red * multB), $1000, cumulRed,
    cumulRed := (cumulRed * multA + red * multB+$80) div $100 ;
    cumulGreen := (cumulGreen * multA + Green * multB+$80) div $100  ;
    cumulBlue := (cumulBlue * multA + Blue * multB+$80) div $100 ;

    color := (cumulRed shl 16) or (cumulGreen shl 8) or (cumulBlue) ;
    pDest^[0] := color ;
    dec(pSrc) ;
    dec(pDest) ;
  End ;

End ;

procedure TFilterBlur.blurVLineAnisotropic(pDest, pSrc :PColor32Array ; width, height:Integer ; radius:Integer) ;
Var
  y : Cardinal ;
  red, green, blue : Integer ;
  cumulRed, cumulGreen, cumulBlue : Integer ;
  color : Cardinal ;
  multA, multB : Integer ;
  diffIntensity, curIntensity, prevIntensity : Integer ;
  pSrcVar : PColor32Array ;
Begin
  prevIntensity := 1000 ;
  pSrcVar := pSrc ;
  for y:=0 to Height-1 do Begin
    curIntensity := Intensity(pSrcVar^[0]) ;
    diffIntensity := Abs(curIntensity-prevIntensity) ;
    multA := radius - diffIntensity*4 ;
    if multA > 200 then multA := 200 else if multA < 0 then multA := 0 ;
    vMultA[y] := multA ;
    prevIntensity := curIntensity ;
    inc(pSrcVar, width) ;
  end ;

  cumulRed := ((pSrc^[0] shr 16) and $FF) ;
  cumulGreen := (pSrc^[0] shr 8) and $FF ;
  cumulBlue := pSrc^[0] and $FF ;

  for y:=0 to Height-1 do Begin
    // Vertical blur
    // color extraction
    red := (pSrc^[0] shr 16) and $FF ;
    green := (pSrc^[0] shr 8) and $FF ;
    blue := pSrc^[0] and $FF ;

    multA := vMultA[y] ;
    multB := $100-multA ;

    cumulRed := (cumulRed * multA + red * multB+$80) div $100 ;
    cumulGreen := (cumulGreen * multA + Green * multB+$80) div $100  ;
    cumulBlue := (cumulBlue * multA + Blue * multB+$80) div $100 ;

    color := ((cumulRed) shl 16) or (cumulGreen shl 8) or (cumulBlue) ;
    pDest^[0] := color ;
    inc(pSrc, width) ;
    inc(pDest, width) ;
  End ;

  dec(pDest, width) ;
  pSrc := pDest ;
  cumulRed := ((pSrc^[0] shr 16) and $FF) ;
  cumulGreen := (pSrc^[0] shr 8) and $FF ;
  cumulBlue := pSrc^[0] and $FF ;
  for y:=Height-1 Downto 0 do Begin
    // Vertical blur
    red := (pSrc^[0] shr 16) and $FF ;
    green := (pSrc^[0] shr 8) and $FF ;
    blue := pSrc^[0] and $FF ;

    multA := vMultA[y] ;
    multB := $100-multA ;

    //DivMod((cumulRed * multA + red * multB), $1000, cumulRed,
    cumulRed := (cumulRed * multA + red * multB+$80) div $100 ;
    cumulGreen := (cumulGreen * multA + Green * multB+$80) div $100  ;
    cumulBlue := (cumulBlue * multA + Blue * multB+$80) div $100 ;

    color := (cumulRed shl 16) or (cumulGreen shl 8) or (cumulBlue) ;
    pDest^[0] := color ;
    dec(pSrc, width) ;
    dec(pDest, width) ;
  End ;
End ;

procedure TFilterBlur.RunFastAnisotropicBlur();
var
  pSrc, PDest : PColor32Array;
  x, y, maxX, maxY : Cardinal ;
  radius : Cardinal ;
begin
  maxX := scanWidth(imgIn)-1 ;
  maxY := scanHeight(imgIn)-1 ;

  setlength(hMultA, maxX+1) ;
  setlength(vMultA, maxY+1) ;

  radius := parameterRadius.Value ;

  //pSrc := scanLine(imgIn, 0) ;
  //pDest := scanLine(imgOut, 0) ;

  // Horizontal blur
  for y:=0 to maxY do Begin
    pSrc := scanLine(imgIn, y) ;
    pDest := scanLine(imgOut, y) ;
    blurHLineAnisotropic(pDest, pSrc, maxX+1, radius) ;
  end ;

  // Vertical blur
  pSrc := scanLine(imgOut, 0) ;
  inc(pSrc, RegionOfInterest.Left) ;
  for x:=0 to maxX do Begin
    blurVLineAnisotropic(pSrc, pSrc, maxX+1, maxY+1, radius) ;
    inc(pSrc) ;
  end ;
end;


{************************************************************************
 * Implementation of                                                    *
 * An efficient algorithm for Gaussian blur using finite-state machines *
 * Frederick M. Waltza and John W. V. Millerb                           *
 ************************************************************************}
procedure TFilterBlur.RunGaussian();
var
  pSrc, PDest : PColor32Array;
  x, y, maxX, maxY : Integer ;
  bw, blurWidth, blurW_1, blurW_2, blurW_d2 : Integer ;
  SR : Array of Cardinal ;
  SC : Array of Array of Cardinal ;
  //color : TColorRec ;
  tmp1, tmp2 : Cardinal ;
  //divisor : Cardinal ;
begin
  maxX := scanWidth(imgIn)-1 ;
  maxY := scanHeight(imgIn)-1 ;

  blurWidth := (parameterRadius.Value * 13 div 256)*2+1 ;
  if blurWidth < 3 then blurWidth := 3 ;
  blurW_1 := blurWidth -1 ;
  blurW_2 := blurWidth -2 ;
  blurW_d2 := blurWidth div 2 ;

  setLength(SR, blurW_1) ;       // rows
  SetLength(SC, blurW_1, maxX+1) ; // columns
  for x:=0 to maxX do begin
    for bw:=0 to blurW_2 do Begin
      SC[bw, x] := 0 ;
    end ;
  end ;

  for y:=-blurW_d2 to maxY+blurW_d2 do begin
    if y<0 then
      pSrc := scanLine(imgIn, 0)
    else if y<=MaxY then
      pSrc := scanLine(imgIn, y)
    else
      pSrc := scanLine(imgIn, maxY) ;

    if y<0 then
      pDest := scanLine(imgOut, 0)
    else if y>=blurW_d2 then
      pDest := scanLine(imgOut, y-blurW_d2)
    else
      pDest := scanLine(imgOut, 0) ;

    //**** Horizontal blur
    for bw:=0 to blurW_2 do Begin
      SR[bw] := 0 ;
    end ;

    // initialization (left side)
    for x:=1 to blurW_d2 do begin
      tmp1 := Intensity(pSrc^[0]) ;
      for bw:=0 to blurW_2 do Begin
        tmp2 := SR[bw] + tmp1 ;
        SR[bw] := tmp1 ;
        tmp1 := tmp2 ;
      end ;
      inc(pSrc) ;
    end ;

    for x:=blurW_d2 to maxX do begin
      //horizontal
      tmp1 := Intensity(pSrc^[0]) ;

      for bw:=0 to blurW_2 do Begin
        tmp2 := SR[bw] + tmp1 ;
        SR[bw] := tmp1 ;
        tmp1 := tmp2 ;
      end ;
      // Vertical
      tmp1 := tmp1 shr blurW_1 ;
      for bw:=0 to blurW_2 do Begin
        tmp2 := SC[bw, x] + tmp1 ;
        SC[bw, x] := tmp1 ;
        tmp1 := tmp2 ;
      end ;
      pDest^[0] := gray32(tmp1 shr blurW_1) ;
      inc(pDest) ;
      inc(pSrc) ;
    end ;

    for x:=1 to blurW_d2 do begin
      tmp1 := Intensity(pSrc^[0]) ;
      for bw:=0 to blurW_2 do Begin
        tmp2 := SR[bw] + tmp1 ;
        SR[bw] := tmp1 ;
        tmp1 := tmp2 ;
      end ;
      // Vertical
      tmp1 := tmp1 shr blurW_1 ;
      for bw:=0 to blurW_2 do Begin
        tmp2 := SC[bw, x-1] + tmp1 ;
        SC[bw, x-1] := tmp1 ;
        tmp1 := tmp2 ;
      end ;
      pDest^[0] := gray32(tmp1 shr blurW_1) ;
      inc(pDest) ;
    end ;

  end ;
end;


end.

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us