5

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 は負の値 (ビットマップの幅) ですが、これは予想されることです。私は何を間違っていますか?

編集:上記のコードは、これまでに処理されたコメントを反映するように変更されています。

4

2 に答える 2

5

負のインデックスにアクセスしないようにするには、

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;
于 2012-05-01T20:08:15.093 に答える
1

多くのビットマップでは、ラインを格納するためにボトムアップ方式を頻繁に使用するため、LineLength は負になります。MSDN: BITMAPINFOHEADER . そのため、このソリューションはそのような場合に合わせて変更する必要があります。

于 2012-05-01T17:04:49.880 に答える