unit filterContrastExplorer;
(* ***** 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
* Site :
* http://filters.sourceforge.net/
*
* ***** END LICENSE BLOCK ***** *)
{
edurand (filters@edurand.com)
}
interface
uses
filter, fparameters, image, SysUtils;
type
TFilterContrastExplorer = class(TFilter)
public
constructor Create; override;
destructor Destroy; override;
procedure Run(); override;
private
parameterPrecision : TParameterInteger;
parameterImageIn, parameterImageOut : TParameterImage;
parameterMode : TParameterString;
mode : Integer;
procedure _run();
end;
implementation
uses
imageIO, Math;
constructor TFilterContrastExplorer.Create;
begin
inherited;
parameterImageIn := addParameterImage('inImage','image to process');
parameterImageOut := addParameterImage('outImage','output image');
parameterPrecision := addParameterInteger('precision','set neighbor. Example : 1 for 3*3',0,10,1);
parameterMode := addParameterString('mode', 'NORMAL/NEGATIVE_CONTRAST/POSITIVE_CONTRAST', 'NORMAL');
end;
destructor TFilterContrastExplorer.Destroy;
begin
end;
procedure TFilterContrastExplorer.Run();
begin
if (parameterImageIn.Image<>nil) and (parameterImageOut.Image<>nil) then begin
if parameterMode.Value='NEGATIVE_CONTRAST' then mode := 1
else if parameterMode.Value='POSITIVE_CONTRAST' then mode := 2
else mode := 0;
_run();
end;
end;
procedure TFilterContrastExplorer._run();
var
pSrc, PDest : PColor32Array;
w,h :Integer;
value : Integer;
tIntensity, tIntensityCurrent, tIntensityMin, tIntensityMax : Integer;
tContraste : Integer;
neighbor :Integer;
imageSrcRow, imageSrcCol : Integer;
imageSrcRowmax, imageSrcColmax : Integer;
imageSrcNeighborRow, imageSrcNeighborCol : Integer;
imageSrcNeighborRowmin, imageSrcNeighborColmin : Integer;
imageSrcNeighborRowmax, imageSrcNeighborColmax : Integer;
bKeep : Boolean;
begin
h:=parameterImageIn.Image.Height;
w:=parameterImageIn.Image.Width;
neighbor:=parameterPrecision.Value;
pSrc:=parameterImageIn.Image.Bits;
imageSrcRowmax:=h-1;
imageSrcColmax:=w-1;
for imageSrcRow:=0 to imageSrcRowmax do begin
pDest:=parameterImageOut.Image.Bits;
Inc(pDest,imageSrcRow*w);
for imageSrcCol:=0 to imageSrcColmax do begin
value := pSrc[imageSrcRow*w+imageSrcCol];
tIntensityCurrent := image.Intensity(value);
tIntensityMin := 255+1;
tIntensityMax := -1;
imageSrcNeighborRowmin:=imageSrcRow-neighbor; if imageSrcNeighborRowmin<0 then imageSrcNeighborRowmin:=0;
imageSrcNeighborColmin:=imageSrcCol-neighbor; if imageSrcNeighborColmin<0 then imageSrcNeighborColmin:=0;
imageSrcNeighborRowmax:=imageSrcRow+neighbor; if imageSrcNeighborRowmax>imageSrcRowmax then imageSrcNeighborRowmax:=imageSrcRowmax;
imageSrcNeighborColmax:=imageSrcCol+neighbor; if imageSrcNeighborColmax>imageSrcColmax then imageSrcNeighborColmax:=imageSrcColmax;
for imageSrcNeighborRow:=imageSrcNeighborRowmin to imageSrcNeighborRowmax do begin
for imageSrcNeighborCol:=imageSrcNeighborColmin to imageSrcNeighborColmax do begin
value := pSrc[imageSrcNeighborRow*w+imageSrcNeighborCol];
tIntensity := image.Intensity(value);
if mode = 0 then begin
bKeep := True;
end else
if mode = 1 then begin
bKeep := (tIntensity<=tIntensityCurrent);
end else
if mode = 2 then begin
bKeep := (tIntensity>=tIntensityCurrent);
end;
if bKeep = True then begin
if tIntensity < tIntensityMin then begin
tIntensityMin := tIntensity;
end;
if tIntensity > tIntensityMax then begin
tIntensityMax := tIntensity;
end;
end;
end;
end;
tContraste := tIntensityMax - tIntensityMin;
// set gray value with Color32 for optimization
pDest^[0] := Color32(tContraste,tContraste,tContraste);
Inc(pDest);
end;
end;
end;
end.