Changeset 487 for trunk/Packages/Common


Ignore:
Timestamp:
Dec 8, 2023, 11:39:45 PM (11 months ago)
Author:
chronos
Message:
  • Added: Range checking for TPixelPointer record type.
Location:
trunk/Packages/Common
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/PixelPointer.pas

    r456 r487  
    3535    BytesPerPixel: Integer;
    3636    BytesPerLine: Integer;
     37    Data: PPixel32;
     38    Width: Integer;
     39    Height: Integer;
    3740    procedure NextLine; inline; // Move pointer to start of next line
    3841    procedure PreviousLine; inline; // Move pointer to start of previous line
     
    4144    procedure SetXY(X, Y: Integer); inline; // Set pixel position relative to base
    4245    procedure SetX(X: Integer); inline; // Set horizontal pixel position relative to base
    43     class function Create(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0): TPixelPointer; inline; static;
     46    procedure CheckRange; inline; // Check if current pixel position is not out of range
     47    class function Create(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0): TPixelPointer; static;
    4448  end;
    4549  PPixelPointer = ^TPixelPointer;
     
    6367implementation
    6468
     69resourcestring
     70  SOutOfRange = 'Pixel pointer out of range';
     71  SWrongBitmapSize = 'Wrong bitmap size [width: %d, height: %d]';
     72
    6573{ TPixel32 }
    6674
     
    110118begin
    111119  Pixel := Pointer(Line) + X * BytesPerPixel;
     120end;
     121
     122procedure TPixelPointer.CheckRange;
     123begin
     124  {$IFOPT R+}
     125  if (PByte(Pixel) < PByte(Data)) or
     126    (PByte(Pixel) >= PByte(Data) + (Width + Height * BytesPerLine) * BytesPerPixel) then
     127    raise Exception.Create(SOutOfRange);
     128  {$ENDIF}
    112129end;
    113130
     
    298315  BaseY: Integer): TPixelPointer;
    299316begin
     317  Result.Width := Bitmap.Width;
     318  Result.Height := Bitmap.Height;
     319  if (Result.Width < 0) or (Result.Height < 0) then
     320    raise Exception.Create(Format(SWrongBitmapSize, [Result.Width, Result.Height]));
    300321  Result.BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
    301322  Result.BytesPerPixel := Bitmap.RawImage.Description.BitsPerPixel shr 3;
     323  Result.Data := PPixel32(Bitmap.RawImage.Data);
    302324  Result.Base := PPixel32(Bitmap.RawImage.Data + BaseX * Result.BytesPerPixel +
    303325    BaseY * Result.BytesPerLine);
Note: See TracChangeset for help on using the changeset viewer.