unit filterArithmeticSubstract;
(* ***** 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 ***** *)
{
edurand (filters@edurand.com)
}
interface
uses
filter, fparameters, image;
type
TfilterArithmeticSubstract = class(TFilter)
public
constructor Create; override;
procedure Run(); override;
private
parameterImageIn1, parameterImageIn2, parameterImageOut : TParameterImage;
parameterMode : TParameterString;
ouputParameterDeltaTotal : TParameterInteger;
normalizedDeltaTotal : Integer;
procedure _run();
end;
implementation
uses
Math;
constructor TfilterArithmeticSubstract.Create;
begin
inherited;
parameterImageIn1:=addParameterImage('inImage1','input image 1');
parameterImageIn2:=addParameterImage('inImage2','input image 2');
parameterImageOut:=addParameterImage('outImage','see mode parameter');
parameterMode:=addParameterString('mode','SUBSTRACT(inImage1-inImage2) or DIFFERENCE(abs(inImage1-inImage2))','DIFFERENCE');
ouputParameterDeltaTotal := addOutputParameterInteger('normalizedDeltaTotal', 'somme of all substract or difference, divide by total pixels number') ;
end;
procedure TfilterArithmeticSubstract.Run();
begin
if (parameterImageIn1.Image<>nil) and (parameterImageIn2.Image<>nil) and (parameterImageOut.Image<>nil) then begin
_run();
end;
end;
procedure TfilterArithmeticSubstract._run();
var
pSrc1, pSrc2, PDest : PColor32Array;
w,h :Integer;
x, max : Cardinal ;
color1, color2 : TColor32;
intensity, intensity1, intensity2 : Integer;
bModeDifference : boolean;
fnormalizedDeltaTotal : Single;
begin
pSrc1:=parameterImageIn1.Image.Bits;
pSrc2:=parameterImageIn2.Image.Bits;
pDest:=parameterImageOut.Image.Bits;
h:=parameterImageIn1.Image.Height;
w:=parameterImageIn1.Image.Width;
normalizedDeltaTotal:=0;
fnormalizedDeltaTotal:=0;
bModeDifference:=true;
if(parameterMode.Value<>'DIFFERENCE') then begin
bModeDifference:=false;
end;
max := h*w-1 ;
// B/W
for x:= 0 to max do Begin
color1:=pSrc1^[0];
color2:=pSrc2^[0];
intensity1:=image.Intensity(color1);
intensity2:=image.Intensity(color2);
intensity:=intensity1-intensity2;
if bModeDifference=true then begin
intensity:=abs(intensity);
end else begin
if intensity<0 then intensity:=0;
end;
fnormalizedDeltaTotal:=fnormalizedDeltaTotal + intensity / max;
pDest[0]:=Color32(intensity,intensity,intensity);
inc(pSrc1);
inc(pSrc2);
inc(pDest);
end ;
{ // Color
for x:= 0 to max do Begin
color1:=pSrc1^[0];
color2:=pSrc2^[0];
r:=RedComponent(color1)-RedComponent(color2);
if r<0 then r:=0;
g:=GreenComponent(color1)-GreenComponent(color2);
if g<0 then g:=0;
b:=BlueComponent(color1)-BlueComponent(color2);
if b<0 then b:=0;
pDest[0]:=Color32(r,g,b);
inc(pSrc1);
inc(pSrc2);
inc(pDest);
end ;
}
normalizedDeltaTotal:=Floor(fnormalizedDeltaTotal);
setOutputParameterInteger('normalizedDeltaTotal', normalizedDeltaTotal);
end;
end.