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
| // For bitmaps of the same size, count the number of pixels that match
// and the number that are different.
PROCEDURE CompareBitmaps(CONST BitmapA, BitmapB: TBitmap;
VAR Total, Match, Differ: INTEGER);
TYPE
TRGBTripleArray = ARRAY[WORD] OF TRGBTriple;
pRGBTripleArray = ^TRGBTripleArray;
VAR
i : INTEGER;
j : INTEGER;
rowA: pRGBTripleArray;
rowB: pRGBTripleArray;
BEGIN
ASSERT( (BitmapA.width = BitmapB.width) AND
(BitmapA.height = BitmapB.height), 'Different sizes');
// Force bitmaps to have the same pf24bit Pixelformat
// (this may be undesirable, for example, for large pf1bit bitmaps)
BitmapA.PixelFormat := pf24bit;
BitmapB.PixelFormat := pf24bit;
Total := BitmapA.width * BitmapA.height;
Match := 0;
Differ := 0;
FOR j := 0 TO BitmapA.height-1 DO
BEGIN
rowA := BitmapA.Scanline[j];
rowB := BitmapB.Scanline[j];
FOR i := 0 TO BitmapA.width-1 DO
BEGIN
{$IFDEF DELPHI5}
{$DEFINE SIMPLEWAY}
{$ENDIF}
{$IFDEF DELPHI6} // should work in D6
{$DEFINE SIMPLEWAY}
{$ENDIF}
{$IFDEF SIMPLEWAY} // update this for D7 or later
// new easy way for D5 or later (actually D4.02 and later)
IF rowA[i] = rowB[i]
THEN INC(Match)
ELSE INC(Differ)
{$ELSE} // D3 and D4 cannot use "simple" way
// "bug" in earlier versions forces more code for test
// chapeau sinon compilo pas content ! -- modif jpt
IF (rowA^[i].rgbtRed = rowB^[i].rgbtRed) AND
(rowA^[i].rgbtGreen = rowB^[i].rgbtGreen) AND
(rowA^[i].rgbtBlue = rowB^[i].rgbtBlue)
THEN INC(Match)
ELSE begin INC(Differ);
form1.memo1.Lines.Add(inttostr(j)+' '+inttostr(i)); // ajout jpt
end;
{$ENDIF}
END
END
END {CompareBitmaps};
procedure TForm1.Button3Click(Sender: TObject);
// http://nono40.developpez.com/tutoriel/delphi/efg/scanline/fichiers/CompareTBitmaps.TXT
VAR
Bitmap1: TBitmap;
Bitmap2: TBitmap;
PixelsDiffer: INTEGER;
PixelsMatch : INTEGER;
PixelsTotal : INTEGER;
begin
Bitmap1 := TBitmap.Create;
Bitmap2 := TBitmap.Create;
TRY
Bitmap1.LoadFromFile('/_share_4_VM/Programmation/bart/img_4_tests/400x300x32_v3_96.bmp');
Bitmap2.LoadFromFile('/_share_4_VM/Programmation/bart/img_4_tests/400x300x32_v3M_96.bmp');
CompareBitmaps(Bitmap1, Bitmap2, PixelsTotal, PixelsMatch, PixelsDiffer);
// Sample output: Total=307200, Match=265, Differ=306935
ShowMessage(Format('Total=%d, Match=%d, Differ=%d',
[PixelsTotal, PixelsMatch, PixelsDiffer]));
FINALLY
Bitmap1.Free;
Bitmap2.Free
END
end; |
Partager