unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, gr32;
type
TForm1 =
class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
// xor two memory blocks
procedure MemBlockXOR(
const Source1, Source2, Destination: Pointer;
Blocksize: Integer);
assembler;
asm
push edi
push esi
push ebx
mov esi,eax
mov edi,ecx
mov ecx,Blocksize;
mov eax,[esi]
and eax,[edx]
mov [edi],eax
mov eax,[esi+4]
and eax,[edx+4]
mov [edi+4],eax
add esi,ecx
add edx,ecx
add edi,ecx
shr ecx,3
test ecx,ecx
jz @@ending
neg ecx
@@doit:
mov eax,[esi+ecx*8]
mov ebx,[esi+ecx*8+4]
xor eax,[edx+ecx*8]
xor ebx,[edx+ecx*8+4]
mov [edi+ecx*8],eax
mov [edi+ecx*8+4],ebx
inc ecx
jnz @@doit
@@ending:
pop ebx
pop esi
pop edi
end;
// helper function for testing only
function IntToBin ( value: LongInt; digits: integer ):
string;
begin
result := StringOfChar ( '
0', digits ) ;
while value > 0
do begin
if ( value
and 1 ) = 1
then
result [ digits ] := '
1';
dec ( digits ) ;
value := value
shr 1;
end;
end;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var bmp1, bmp2, xbmp : TBitmap32;
begin
// create bitmaps
bmp1 := TBitmap32.Create;
bmp2 := TBitmap32.create;
xbmp := TBitmap32.create;
// FIRST test, everything works
bmp1.SetSize(2,2);
bmp1.Clear(tcolor32($124423213));
bmp2.SetSize(2,2);
bmp2.Clear(tcolor32(clblue));
xbmp.setSize(2,2);
// check what we get
memo1.Lines.Add('
TEST 1: ');
// xbmp := bmp1 xor bmp2;
MemBlockXOR(@bmp1.Bits[0], @bmp2.Bits[0], @xbmp.bits[0], sizeof(tcolor32) * (bmp1.width * bmp1.height));
memo1.lines.add(inttobin(bmp1.Pixels[0,0],32) + '
xor');
memo1.lines.add(inttobin(bmp2.Pixels[0,0],32) + '
=');
memo1.lines.add(inttobin(xbmp.Pixels[0,0],32));
// SECOND test, fail
bmp1.SetSize(1,1);
bmp1.Clear(tcolor32($124423213));
bmp2.SetSize(1,1);
bmp2.Clear(tcolor32(clblue));
xbmp.setSize(1,1);
// check what we get
memo1.Lines.Add('
TEST 2: ');
// xbmp := bmp1 xor bmp2;
MemBlockXOR(bmp1.ScanLine[0], bmp2.ScanLine[0], xbmp.ScanLine[0], sizeof(tcolor32) * (bmp1.width * bmp1.height));
memo1.lines.add(inttobin(bmp1.Pixels[0,0],32) + '
xor');
memo1.lines.add(inttobin(bmp2.Pixels[0,0],32) + '
=');
memo1.lines.add(inttobin(xbmp.Pixels[0,0],32));
// free bitmaps
bmp1.free;
bmp2.free;
xbmp.free;
end;
end.