A
download filterRotation.pas
Language: Delphi
Copyright: (C) 2004 Durand Emmanuel (C) 2004 Burgel Eric
LOC: 310
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
unit filterRotation;
(* ***** 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)
 edurand (filters@edurand.com)
}

interface
uses
  filter, fparameters, image;

type
  TFilterRotation = class(TFilter)
  public
    constructor Create; override;
    destructor Destroy; override;
    procedure Run(); override;
  private
    imageIn, imgOutMonitoring : PBitmap32 ;
    outImage : PBitmap32;
    CenterX, CenterY : Single ;
    AngleR, sinus, cosinus :  Extended ;
    deltaH, deltaW : Integer;
    missingPixelColorMode : Integer;
    missingPixelColor : TColor32;
    parameterImageIn : TParameterImage;
    outputparameterImageOut : TParameterImage;
    parameterImageOutMonitoring : TParameterImage;
    parameterMonitoring : TParameterBoolean;
    parameterAngle : TParameterSingle ;
    parameterCenterX, parameterCenterY : TParameterSingle ;
    parameterInterpolationMode : TParameterInteger ;
    parameterMissingPixelColorMode : TParameterString;
    parameterAutoAdjustSize : TParameterBoolean;
    procedure _run();
    procedure rotationInterpolationNearestNeighbor();
    procedure rotationInterpolationBasicFast();
    procedure deleteImages;
  end;

implementation
uses
  imageIO, Math, divers;

constructor TFilterRotation.Create;
begin
  inherited;
  parameterImageIn:=addParameterImage('inImage', 'input image to invert');
  outputparameterImageOut := addOutputParameterImage('outImage', 'outImage');
  parameterImageOutMonitoring:=addParameterImage('outImageMonitoring', 'image out monitoring');
  parameterMonitoring:=addParameterBoolean('monitoring','monitoring',True);
  parameterAngle:=addParameterSingle('angle', 'angle in degree', -10000, 10000, 90);
  parameterCenterX:=addParameterSingle('xCenter', 'X center of rotation', -10000, 10000, 0);
  parameterCenterY:=addParameterSingle('yCenter', 'Y center of rotation', -10000, 10000, 0);
  parameterInterpolationMode := addParameterInteger('interpolationMode', 'Interpolation mode', 0, 2, 0) ;
  parameterMissingPixelColorMode:=addParameterString('missingPixelColorMode','how the filter must set the missing pixel due to the rotation [MIRROR/WHITE/BLACK]','MIRROR');
  parameterAutoAdjustSize:=addParameterBoolean('autoAdjustSize','auto adjust imageOut size to show all imageIn pixel',true);
end;

destructor TFilterRotation.Destroy;
begin
  deleteImages;
  inherited;
end;

procedure TFilterRotation.deleteImages;
begin
  image.freeImage(outImage);
end;

procedure TFilterRotation.Run();
begin
  imageIn  := parameterImageIn.Image ;
  if (imageIn<>nil) then begin
    _run();
  end;
end;

procedure TFilterRotation._run();
var
  InterpolationMode : Integer ;
  imageOutWidth, imageOutHeight : Integer;
  minX, maxX, minY, maxY : Integer;

  procedure searchMinMaxXY(inPointX,inPointY:Integer);
  var
    outPoint : TFPoint;
  begin
    outPoint:=divers.getRotatedPoint(divers.fpoint(inPointX,inPointY),divers.fpoint(CenterX,CenterY),AngleR);
    minX:=Min(minX,Floor(outPoint.x));
    maxX:=Max(maxX,Floor(outPoint.x));
    minY:=Min(minY,Floor(outPoint.y));
    maxY:=Max(maxY,Floor(outPoint.y));
  end;

begin
  deleteImages;
  AngleR := -parameterAngle.Value *PI/180 ;
  CenterX := parameterCenterX.Value ;
  CenterY := parameterCenterY.value ;

  imageOutWidth:=imageIn.Width;
  imageOutHeight:=imageIn.Height;
  deltaW:=0;
  deltaH:=0;
  if parameterAutoAdjustSize.Value=true then begin
    CenterX:=imageIn.Width div 2;
    CenterY:=imageIn.Height div 2;
    minX:=MAXINT; maxX:=-MAXINT; minY:=MAXINT; maxY:=-MAXINT;
    searchMinMaxXY(0,0);
    searchMinMaxXY(imageIn.Width-1,0);
    searchMinMaxXY(imageIn.Width-1,imageIn.Height-1);
    searchMinMaxXY(0,imageIn.Height-1);
    deltaW:=((maxX-minX)-imageIn.Width);
    deltaH:=((maxY-minY)-imageIn.Height);
    Inc(imageOutWidth,deltaW);
    Inc(imageOutHeight,deltaH);
  end;
  outImage:=createImage(imageOutWidth,imageOutHeight);

  InterpolationMode := parameterInterpolationMode.Value ;
  if parameterMissingPixelColorMode.Value='MIRROR' then begin
    missingPixelColorMode:=0;
    missingPixelColor:=clBlack32;
  end
  else if parameterMissingPixelColorMode.Value='WHITE' then begin
    missingPixelColorMode:=1;
    missingPixelColor:=clWhite32;
  end
  else if parameterMissingPixelColorMode.Value='BLACK' then begin
    missingPixelColorMode:=2;
    missingPixelColor:=clBlack32;
  end;

  sinCos(AngleR, sinus, cosinus) ;

  // monitoring
  imgOutMonitoring:=parameterImageOutMonitoring.Image;
  if (parameterMonitoring.Value=True) and (imgOutMonitoring<>nil) then begin
    image.copyImageToImage(imageIn,imgOutMonitoring);
  end else
  if imgOutMonitoring<>nil then begin
    image.eraseImage(imgOutMonitoring);
  end;

  case InterpolationMode of
    0 : rotationInterpolationNearestNeighbor() ;
    1, 2 : rotationInterpolationBasicFast();
  end ;

  setOutputParameterImage('outImage',outImage);
end;

procedure TFilterRotation.rotationInterpolationNearestNeighbor();
var
  pSrc, PDest : PColor32Array;
  x, y, maxX, maxY : Integer ;
  newX, newY : Integer ;
  imgInWidth, imgInHeight : Integer ;
  Kx, Ky : Extended ;
  deltaWdiv2, deltaHdiv2 : Single ;
  begin
  imgInWidth := imageIn.Width ;
  imgInHeight := imageIn.Height ;

  deltaWdiv2:=deltaW / 2;
  deltaHdiv2:=deltaH / 2;

  maxX := outImage.Width -1 ;
  maxY := outImage.Height -1 ;
  Kx := CenterX - cosinus*(CenterX+deltaWdiv2) - sinus*(CenterY+deltaHdiv2) + 0.5 ;
  ky := CenterY - cosinus*(CenterY+deltaHdiv2) + sinus*(CenterX+deltaWdiv2) + 0.5 ;
  for y:=0 to maxY do begin
    pDest := scanLine(outImage, y) ;
    for x:=0 to maxX do begin
      // compute X
      //newX := trunc((x-centerX-deltaWdiv2)*cosinus + (y-centerY-deltaHdiv2)*sinus + centerX) ;
      newX := trunc(cosinus*x + sinus*y + kx) ;
      while newX >= imgInWidth do newX := newX - imgInWidth ;
      while newX < 0 do newX := newX + imgInWidth ;
      // compute Y
      //newY := trunc((y-centerY-deltaHdiv2)*cosinus - (x-centerX-deltaWdiv2)*sinus + CenterY) ;
      newY := trunc(cosinus*y - sinus*x + ky) ;
      while newY >= imgInHeight do newY := newY - imgInHeight ;
      while newY < 0 do newY := newY + imgInHeight ;
      pSrc := scanLine(imageIn, newY) ;
      pDest^[0] := pSrc^[NewX] ;
      inc(pDest) ;
    end ;
  end ;
end;

procedure TFilterRotation.rotationInterpolationBasicFast();
var
  pSrc1, pSrc2, PDest : PColor32Array;
  x, y, maxX, maxY : Integer ;
  newX, newY : Single ;
  floorX, floorY : Integer ;
  T, U : Single ;
  Color1, Color2 : TColor32 ;
  cosinusMulX : array of Single;
  sinusMulY : array of Single;
  cosinusMulY : array of Single;
  sinusMulX : array of Single;
  sinusMulYy, cosinusMulYy : Single;
  pcosinusMulXx, psinusMulXx : ^Single;
  bPixelProcessed : boolean;
  newXYmin, newXYmax : TFPoint;
  tmpColor : TColor32;
  deltaWdiv2, deltaHdiv2 : Integer;

  T1, T2, U1, U2 : Word ;
  r_b1, _v_1, r_b2, _v_2 : TColor32 ;
  basicFast : boolean ;
begin
  basicFast := (parameterInterpolationMode.Value=1) ;
  maxX := outImage.Width -1 ;
  maxY := outImage.Height -1 ;

  newXYmin.x:=maxX;
  newXYmin.y:=maxY;
  newXYmax.x:=0;
  newXYmax.y:=0;

  deltaWdiv2:=deltaW div 2;
  deltaHdiv2:=deltaH div 2;
  SetLength(cosinusMulX,maxX+1);
  SetLength(sinusMulX,maxX+1);
  for x:=0 to maxX do begin
    cosinusMulX[x]:=cosinus*(x-CenterX-deltaWdiv2);
    sinusMulX[x]:=-sinus*(x-CenterX-deltaWdiv2)+CenterY;
  end;
  SetLength(sinusMulY,maxY+1);
  SetLength(cosinusMulY,maxY+1);
  for y:=0 to maxY do begin
    sinusMulY[y]:=sinus*(y-CenterY-deltaHdiv2)+CenterX;
    cosinusMulY[y]:=cosinus*(y-CenterY-deltaHdiv2);
  end;

  pDest := outImage.Bits;
  for y:=0 to maxY do begin
    sinusMulYy:=sinusMulY[y];
    cosinusMulYy:=cosinusMulY[y];
    pcosinusMulXx:=@cosinusMulX[0];
    psinusMulXx:=@sinusMulX[0];
    for x:=0 to maxX do begin
      bPixelProcessed:=false;

      // without pre-calculation, we would use this formula
      //newX := cosinus*(x-CenterX) + sinus*(y-CenterY)+CenterX;
      //newY := cosinus*(y-CenterY) - sinus*(x-CenterX)+CenterY;
      newX := pcosinusMulXx^ + sinusMulYy;
      newY := cosinusMulYy + psinusMulXx^;
      // the newX/Y can correspond of an nonexisting pixel in imageIn
      if (newX<0) or (newY<0) or
         (newX>=imageIn.Width-1) or (newY>=imageIn.Height-1) then begin
        if missingPixelColorMode<>0 then begin
          pDest^[0] := missingPixelColor;
          bPixelProcessed:=true;
        end else begin
          newX:=Abs(newX);
          if newX>=imageIn.Width-1 then begin
            newX := newX - imageIn.Width+1;
            if newX>=imageIn.Width then newX := imageIn.Width-1;
          end;
          newY:=Abs(newY);
          if newY>=imageIn.Height-1 then begin
            newY := newY - imageIn.Height+1;
            if newY>=imageIn.Height then newY := imageIn.Height-1;
          end;
        end;
      end;

      if parameterMonitoring.Value=true then begin
        floorX := floor(newX);
        floorY := floor(newY);
        if (floorX>=0) and (floorY>=0) and
           (floorX<imageIn.Width) and (floorY<imageIn.Height) then begin
            if floorX<newXYmin.x then newXYmin.x:=floorX;
            if floorY<newXYmin.y then newXYmin.y:=floorY;
            if floorX>newXYmax.x then newXYmax.x:=floorX;
            if floorY>newXYmax.y then newXYmax.y:=floorY;
            tmpColor:=image.getPixel(imgOutMonitoring,floorX,floorY);
            tmpColor:=tmpColor or $0000AA00;
            image.setPixel(imgOutMonitoring,floorX,floorY,tmpColor);
        end;
      end;

      if bPixelProcessed=false then begin

          floorX := floor(newX);
          floorY := floor(newY);
          T := frac(newX) ;
          U := frac(newY) ;

          if(floorX>(imageIn.Width-2))then begin
            floorX:=imageIn.Width-2;
          end;

          pSrc1:=imageIn.Bits;
          Inc(pSrc1,floorY*imageIn.Width);
          pSrc2:=pSrc1;
          Inc(pSrc2,imageIn.Width);

IF basicFast then Begin

          if T < 0.34 Then
            color1 := pSrc1[floorX]
          else if T > 0.66 then
            color1 := pSrc1[floorX+1]
          else
            color1 := ((pSrc1[floorX] and $FEFEFE) shr 1) + ((pSrc1[floorX+1] and $FEFEFE) shr 1) ;

          if T < 0.34 Then
            color2 := pSrc2[floorX]
          else if T > 0.66 then
            color2 := pSrc2[floorX+1]
          else
            color2 := ((pSrc2[floorX] and $FEFEFE) shr 1) + ((pSrc2[floorX+1] and $FEFEFE) shr 1) ;

          if U < 0.34 Then
            pDest^[0] := color1
          else if U > 0.66 then
            pDest^[0] := color2
          else
            pDest^[0] := ((color1 and $FEFEFE) shr 1) + ((color2 and $FEFEFE) shr 1) ;
end else Begin
          // Horizontal
          T2 := floor(T*128) ;
          T1 := 128-T2 ;

          r_b1 := pSrc1[floorX]   and $00FF00FF ;
          _v_1 := pSrc1[floorX]   and $0000FF00 ;
          r_b2 := pSrc1[floorX+1] and $00FF00FF ;
          _v_2 := pSrc1[floorX+1] and $0000FF00 ;

          color1 := (((r_b1* t1 + r_b2* t2) div 128) and $00FF00FF) or (((_v_1*t1 + _v_2*t2)div 128) and $0000FF00) ;

          r_b1 := pSrc2[floorX]   and $00FF00FF ;
          _v_1 := pSrc2[floorX]   and $0000FF00 ;
          r_b2 := pSrc2[floorX+1] and $00FF00FF ;
          _v_2 := pSrc2[floorX+1] and $0000FF00 ;

          color2 := (((r_b1* t1 + r_b2* t2) div 128) and $00FF00FF) or (((_v_1*t1 + _v_2*t2)div 128) and $0000FF00) ;

          // Vertical
          U2 := floor(U*128) ;
          U1 := 128-U2 ;

          r_b1 := color1 and $00FF00FF ;
          _v_1 := color1 and $0000FF00 ;
          r_b2 := color2 and $00FF00FF ;
          _v_2 := color2 and $0000FF00 ;

          pDest^[0] := (((r_b1* U1 + r_b2* U2) div 128) and $00FF00FF) or (((_v_1*U1 + _v_2*U2)div 128) and $0000FF00) ;

end ;

        end;
        Inc(pDest) ;
        Inc(pcosinusMulXx);
        Inc(psinusMulXx);
    end;
  end ;

  if parameterMonitoring.Value=true then begin
    image.drawLine(imgOutMonitoring,newXYmin,newXYmax,clYellow32);
  end;

end;

end.

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