Какой самый быстрый способ проверить, совпадают ли два Tbitmaps? - PullRequest
8 голосов
/ 30 августа 2009

Есть ли лучший способ, чем рассматривать их попиксельно?

Ответы [ 3 ]

16 голосов
/ 30 августа 2009

Вы можете сохранить оба растровых изображения в TMemoryStream и сравнить их, используя CompareMem:

function IsSameBitmap(Bitmap1, Bitmap2: TBitmap): Boolean;
var
 Stream1, Stream2: TMemoryStream;
begin
  Assert((Bitmap1 <> nil) and (Bitmap2 <> nil), 'Params can''t be nil');
  Result:= False;
  if (Bitmap1.Height <> Bitmap2.Height) or (Bitmap1.Width <> Bitmap2.Width) then
     Exit;
  Stream1:= TMemoryStream.Create;
  try
    Bitmap1.SaveToStream(Stream1);
    Stream2:= TMemoryStream.Create;
    try
      Bitmap2.SaveToStream(Stream2);
      if Stream1.Size = Stream2.Size Then
        Result:= CompareMem(Stream1.Memory, Stream2.Memory, Stream1.Size);
    finally
      Stream2.Free;
    end;
  finally
    Stream1.Free;
  end;
end;

begin
  if IsSameBitmap(MyImage1.Picture.Bitmap, MyImage2.Picture.Bitmap) then
  begin
    // your code for same bitmap
  end;
end;

Я не тестировал этот код X scanline. Если да, сообщите нам, какой из них самый быстрый.

12 голосов
/ 30 августа 2009

Использование ScanLine, без TMemoryStream.

function IsSameBitmapUsingScanLine(Bitmap1, Bitmap2: TBitmap): Boolean;
var
 i           : Integer;
 ScanBytes   : Integer;
begin
  Result:= (Bitmap1<>nil) and (Bitmap2<>nil);
  if not Result then exit;
  Result:=(bitmap1.Width=bitmap2.Width) and (bitmap1.Height=bitmap2.Height) and (bitmap1.PixelFormat=bitmap2.PixelFormat) ;

  if not Result then exit;

  ScanBytes := Abs(Integer(Bitmap1.Scanline[1]) - Integer(Bitmap1.Scanline[0]));
  for i:=0 to Bitmap1.Height-1 do
  Begin
    Result:=CompareMem(Bitmap1.ScanLine[i],Bitmap2.ScanLine[i],ScanBytes);
    if not Result then exit;
  End;

end;

Bye.

0 голосов
/ 30 августа 2009

Если вам нужен точный ответ, нет. Если вам нужно приближение, вы можете проверить выбор пикселей. Но если вы хотите выяснить, являются ли две битовые карты абсолютно идентичными, вам нужно сравнить все данные о пикселях и о формате пикселей.

...