delphi - 如何正确实现TBitmap的扫描线访问?

标签 delphi graphics bitmap delphi-xe2 scanline

我正在尝试根据 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 为负(位图的宽度),但这可能是预期的。我做错了什么?

编辑:上面的代码已更改,以反射(reflect)迄今为止处理的备注。

最佳答案

为了不访问任何负索引,我会这样做

procedure TForm1.Button1Click(Sender: TObject);
var Bitmap: TBitmap;
    q: TRGBQuad;
    x, y: LongInt;
    line{, FirstLine}: PRGBQuadArray;
    idx: NativeInt;
    LastLine: PRGBQuadArray;
    LineLength: NativeInt;
begin
   q.r := 0; q.g := 0;
   Bitmap := TBitmap.Create;
   Bitmap.Height := Image.Height;
   Bitmap.Width  := Image.Width;
   Bitmap.PixelFormat := pf32Bit;

   LastLine := Bitmap.ScanLine[Bitmap.Height - 1];
   LineLength := (NativeInt(Bitmap.Scanline[Bitmap.Height - 2]) - NativeInt(Lastline)) div SizeOf(TRGBQuad);
   try
      for y := 0 to Bitmap.Height - 1 do
      begin
         for x := 0 to Bitmap.Width - 1 do
         begin
            q.b := (x xor y) mod 255;
            idx := y * LineLength + x;
            LastLine [idx] := q;
         end; // for
      end; // for
      Image.Picture.Assign (Bitmap);
   finally
       Bitmap.Free;
   end; // try..finally
end;

关于delphi - 如何正确实现TBitmap的扫描线访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10400819/

相关文章:

delphi - Firemonkey 样式设计器中另一个列表框中的列表框

sql-server-2005 - ADO连接超时问题

android - 检测使用 Rect 变换绘制的位图的触摸

android - 从 "Recent Images"文件夹中选择图像时,图片路径为空

mysql - 我们如何控制表中字段的动态排序?

delphi - Delphi标签值排序

c - OpenGL从角落移动物体

c# - 为什么我需要在一种情况下将位图转换为图像,而在另一种情况下不需要?

graphics - 对 Vulkan API 中的渲染 channel 感到困惑

android - 解决textview转位图时黑屏