const
TestPointsX = 56;
TestPointsY = 42;
MaxInt64 = Int64($7FFFFFFFFFFFFFFF);
BELOW_NORMAL_PRIORITY_CLASS = $00004000;
type
TRGB =
record
Blue: Byte;
Green: Byte;
Red: Byte;
end;
PRGB = ^TRGB;
TRefPoints =
array[0..TestPointsX-1, 0..TestPointsY-1]
of TRGB;
TPicInfoEntry =
record
FileLocation:
string;
Used: Boolean;
RefPoints: TRefPoints;
end;
TCatalogue =
record
PicInfoList:
array of TPicInfoEntry;
Count: Integer;
end;
TImageTableEntry =
record
FileName:
string;
Match: Int64;
CatThreadAddID: Byte;
PicID: Integer;
Avg: TRGB;
end;
TRndFillGrid =
record
ImgDone:
array of Boolean;
PicsLeft: Integer;
end;
TImageCompareThread =
class(TThread)
private
procedure DrawStatus;
protected
procedure Execute;
override;
public
CatID: Byte;
SourcePic:
array of TRefPoints;
RndFillGrid: TRndFillGrid;
B: TBitmap;
P: PRGB;
Terminating: Bool;
constructor Create(CatalogID: Byte);
virtual;
end;
var
ImageTable:
array of TImageTableEntry;
cs: TRTLCriticalSection;
ImageCompareThread:
array[1..4]
of TImageCompareThread;
Catalogs:
array[1..4]
of TCatalogue;
ThreadReady:
array[1..4]
of Boolean;
procedure TImageCompareThread.Execute;
var
i, j, k, m: Integer;
Match: Int64;
begin
SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_BELOW_NORMAL);
{$IFDEF NO_RANDOM_FILL}
for m:=0
to Length(SourcePic)-1
do
begin
{$ELSE}
while RndFillGrid.PicsLeft>0
do
begin
m:=-1;
while m=-1
do
begin
m:=Random(2350);
if RndFillGrid.ImgDone[m]
then m:=-1
else
begin
Dec(RndFillGrid.PicsLeft);
RndFillGrid.ImgDone[m]:=True;
Synchronize(DrawStatus);
end;
end;
{$ENDIF}
if Terminating
then
begin
self.Terminate;
exit;
end;
EnterCriticalSection(cs);
if ImageTable[m].FileName='
'
then ImageTable[m].Match:=MaxInt64;
LeaveCriticalSection(cs);
for i:=0
to Catalogs[CatID].Count-1
do
begin
Match:=0;
for j:=0
to TestPointsX-1
do
for k:=0
to TestPointsY-1
do
begin //die nächsten drei zeilen werden verdammt oft durchgegangen...
Inc(Match, Abs(Catalogs[CatID].PicInfoList[i].RefPoints[j, k].Blue - SourcePic[m, j, k].Blue));
Inc(Match, Abs(Catalogs[CatID].PicInfoList[i].RefPoints[j, k].Green - SourcePic[m, j, k].Green));
Inc(Match, Abs(Catalogs[CatID].PicInfoList[i].RefPoints[j, k].Red - SourcePic[m, j, k].Red));
end;
EnterCriticalSection(cs);
if (Match < ImageTable[m].Match)
and (Catalogs[CatID].PicInfoList[i].Used <> True)
then
begin
if ImageTable[m].FileName<>'
'
then Catalogs[ImageTable[m].CatThreadAddID].PicInfoList[ImageTable[m].PicID].Used:=False;
ImageTable[m].CatThreadAddID:=CatID;
ImageTable[m].PicID:=i;
ImageTable[m].Match:=Match;
ImageTable[m].FileName:=Catalogs[CatID].PicInfoList[i].FileLocation;
Catalogs[CatID].PicInfoList[i].Used:=True;
end;
LeaveCriticalSection(cs);
end;
end;
EnterCriticalSection(cs);
ThreadReady[CatID]:=True;
LeaveCriticalSection(cs);
end;