【发布时间】:2012-05-01 16:26:45
【问题描述】:
我正在尝试根据article on Embarcadero 访问位图的扫描线。使用像
这样的扫描线for y := 0 to n do
begin
line := bitmap.scanline [y];
for x := 0 to n do line [x] := value;
我以前实现过。我注意到访问扫描线需要相当多的时间,上面提到的文章提供了一个解决方案。我无法正确实现它。我的代码是:
unit SCTester;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
ExtCtrls;
type
TRGBQuad = packed record
b: uInt8;
g: uInt8;
r: uInt8;
alpha: uInt8;
end; // Record: TQuad //
// Override the definitions in Graphics.pas
TRGBQuadArray = packed array [0..MaxInt div SizeOf (TRGBQuad) - 1] of TRGBQuad;
PRGBQuadArray = ^TRGBQuadArray;
TForm1 = class(TForm)
Image: TImage;
procedure ImageDblClick(Sender: TObject);
end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ImageDblClick(Sender: TObject);
var Bitmap: TBitmap;
q: TRGBQuad;
x, y: NativeInt;
FirstLine: PRGBQuadArray;
idx: NativeInt;
LineLength: NativeInt;
begin
q.r := 0; q.g := 0;
Bitmap := TBitmap.Create;
Bitmap.Height := Image.Height;
Bitmap.Width := Image.Width;
Bitmap.PixelFormat := pf32Bit;
FirstLine := Bitmap.ScanLine [0];
LineLength := (NativeInt (Bitmap.Scanline [1]) - NativeInt (FirstLine)) div SizeOf (TRGBQuad);
try
for y := Bitmap.Height - 1 downto 0 do
begin
for x := 0 to Bitmap.Width - 1 do
begin
q.b := (x xor y) mod 255;
idx := y * LineLength + x;
FirstLine [idx] := q;
end; // for
end; // for
Image.Picture.Assign (Bitmap);
finally
Bitmap.Free;
end; // try..finally
end;
end.
当 y=1 和 x=0 时,我总是得到非法访问。 LineLength 是负数(位图的宽度),但这可能是预期的。我做错了什么?
编辑:上面的代码被改变以反映到目前为止处理的备注。
【问题讨论】:
-
idx应声明为NativeInt,因此您的代码也可以在 x64 中使用。LineLength不能为负数(因此非法访问)。我的结论是您正在以 64 位模式运行此代码。 -
@LURD,我的想法完全正确 - 每个 LongInt(...) 都应该替换为 NativeUInt(...)
-
@LURD 确定代码在 64 位模式下不正确,因为指针转换为 longints
-
@Arnold 是的,我在 windows7 64 位上有 Delphi XE,你的代码可以工作,除了在 Delphi XE2 64 位模式下将指针转换为 longints 之外,我在你的代码中没有发现其他问题。
-
将
LineLength := (Longint (Bitmap.Scanline [1]) - Longint (FirstLine))更改为LineLength := (NativeInt (Bitmap.Scanline [1]) - NativeInt (FirstLine))并将所有LongInt声明更改为NativeInt。
标签: delphi graphics bitmap delphi-xe2 scanline