Delphi TBitmap.ScanLine[]执行时间太长
我在德尔福工作。我正在代码中使用Delphi TBitmap.ScanLine[]执行时间太长,delphi,tbitmap,Delphi,Tbitmap,我在德尔福工作。我正在代码中使用bmp.ScanLine[]。我的代码如下: bmp := TBitmap.Create; bmp.Height := imgMain.Height; bmp.Width := imgMain.Width; for i := 0 to imgMain.Height - 1 do begin prgb := bmp.ScanLine[i]; p1 := imgMain.ScanLine[i]; for j
bmp.ScanLine[]
。我的代码如下:
bmp := TBitmap.Create;
bmp.Height := imgMain.Height;
bmp.Width := imgMain.Width;
for i := 0 to imgMain.Height - 1 do begin
prgb := bmp.ScanLine[i];
p1 := imgMain.ScanLine[i];
for j := 0 to imgMain.Width - 1 do begin
//Some code
end;
end;
这里,imgMain是TBitmap类型。我的问题是,当我执行这段代码时,它会占用太多的时间
prgb := bmp.ScanLine[i];
p1 := imgMain.ScanLine[i];
请告诉我哪里错了?嗯,可以获得一些东西(介绍rowpitch,见下文),但这并不过分。可能将for循环更改为while循环,该循环会增加指针并与最后一个像素的指针值进行比较
// from memory, might need an additional typecast here or there.
// will typically be negative
scanline0:=imga.scanline[0];
rowpitchimga:=integer(imga.scanline[1])-integer(scanline0); // bytes to jump row.
prgb1 :=scanline0;
for i:=0 to imgmain.height-1;
begin
prgbend:=prgb1;
inc(prgbend,width); // if prgbend, it will be with sizeof(prgb1^)
while(prgb1<prbend) do // smaller then, since prgb1[] is 0 based.
begin
// do your thing
inc(prgb1);
end;
prgb1:=prgbend;
inc(pointer(prgb1),rowpitch-width*sizeof(prgb1^)); // skip alignmentbytes
inc(pointer(prgbend),rowpitch);
end;
//从内存中,可能需要在此处或此处进行额外的类型转换。
//通常为负值
扫描线0:=imga.scanline[0];
rowpitchimga:=整数(imga.scanline[1])-整数(scanline0);//要跳转行的字节数。
prgb1:=扫描线0;
对于i:=0至imgmain.height-1;
开始
prgbend:=prgb1;
inc(prgbend,宽度);//如果是prgbend,则其大小为sizeof(prgb1^)
while(prgb1)这些行需要多少时间?你期望什么?这就是方法。你是对的。如果你想让你的代码准备好使用64位编译器,不要忘记使用NativeInt而不是Integer!这里没有Troll:rowpitchimga:=NativeInt(imga.scanline[1])-NativeInt(scanline0);在Delphi 2007之前,您必须定义类型NativeInt=integer;它实际上被定义为PtrInt,并且已经在64位上运行。它最初是使用免费的Pascal开发的。为什么Embarcadero选择了不同的标识符,我不知道。嘿@Marco,很抱歉迟才回复。是的,这可能是一个解决方案。但我观察到的另一件事是,如果我只使用ritep1:=imgMain.Scanline[i];
这样就不会花费太多时间。因此,它只是在prgb:=bmp.Scanline[i];
中花费时间。这里,imgMain是一个位图,它是使用TBitmap.Assign();
从TImage位图复制而来的。Delphi位图是基于windows的(用windows分配的句柄),而不是基于windows的(与设备无关的iirc)。延迟的可能是两种类型之间的转换。