unit filterResize;
(* ***** 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)
eburgel (filters@burgel.com)
}
interface
uses
filter, fparameters, image, filterBlur;
type
TFilterResize = class(TFilter)
public
constructor Create; override;
destructor Destroy; override;
procedure Run(); override;
private
parameterImageIn, parameterImageOut : TParameterImage;
parameterMode : TParameterInteger ;
imgIn, imgOut : PBitmap32 ;
filterBlur : TFilterBlur ;
procedure _run();
end;
implementation
uses
imageIO;
constructor TFilterResize.Create;
begin
inherited;
parameterImageIn:=addParameterImage('inImage', 'input image to Resize');
parameterImageOut:=addParameterImage('outImage', 'result image of Resize');
parameterMode := addParameterInteger('mode', 'Aproximation method', 0, 1, 0) ;
filterBlur := TFilterBlur.Create ;
end;
destructor TFilterResize.Destroy;
begin
inherited;
filterBlur.Free ;
End ;
procedure TFilterResize.Run();
begin
imgIn := parameterImageIn.Image ;
imgOut := parameterImageOut.Image ;
if (imgIn<>nil) and (imgOut<>nil) then begin
_run();
end;
end;
procedure TFilterResize._run();
var
pSrc, PDest : PColor32Array;
x, y, maxX, maxY : Integer ;
newX, newY : Integer ;
preImg, postImg : PBitmap32 ;
widthOut, widthIn : Cardinal ;
heightOut, heightIn : Cardinal ;
r, rV, rH : Single ;
mode : Integer ;
radius : Integer ;
preBlur, postBlur : boolean ;
begin
widthOut := scanWidth(imgOut) ;
widthIn := scanWidth(imgIn) ;
heightOut := scanHeight(imgOut) ;
heightIn := scanHeight(imgIn) ;
mode := parameterMode.Value ;
rH := widthIn / widthOut ;
rV := heightIn / heightOut ;
r := (rH+rV) / 2 ;
preBlur := ((mode = 1) and (r>1)) ;
postBlur := ((mode = 1) and (r<1)) ;
if preBlur then Begin
preImg := image.createImageLike(imgIn) ;
filterBlur.setParameterImage('inImage', imgIn);
filterBlur.setParameterImage('outImage', preImg);
radius := round((r-1)*20) ;
filterBlur.setParameterInteger('radius', radius);
filterBlur.Run ;
end else
preImg := imgIn ;
if postBlur then
postImg := image.createImageLike(imgOut)
else
postImg := imgOut ;
maxX := widthOut-1 ;
maxY := heightOut-1 ;
for y:=0 to maxY do begin
newY := trunc(y*rV+0.5) ;
pSrc := scanLine(preImg, newY) ;
pDest := scanLine(postImg, y) ;
for x:=0 to maxX do begin
newX := trunc(x*rH+0.5) ;
pDest^[x] := pSrc^[newX];
end ;
end ;
if postBlur then Begin
filterBlur.setParameterImage('inImage', postImg);
filterBlur.setParameterImage('outImage', imgOut);
radius := round((1/r-1)*60) ;
filterBlur.setParameterInteger('radius', radius);
filterBlur.Run ;
End ;
if preBlur Then image.freeImage(preImg);
if postBlur Then image.freeImage(postImg);
end;
end.