Ignore:
Timestamp:
Dec 8, 2023, 11:39:45 PM (5 months ago)
Author:
chronos
Message:
  • Added: Range checking for TPixelPointer record type.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/DpiControls/NativePixelPointer.pas

    r468 r487  
    3232    BytesPerPixel: Integer;
    3333    BytesPerLine: Integer;
     34    Data: PPixel32;
     35    Width: Integer;
     36    Height: Integer;
    3437    procedure NextLine; inline; // Move pointer to start of next line
    3538    procedure PreviousLine; inline; // Move pointer to start of previous line
     
    3841    procedure SetXY(X, Y: Integer); inline; // Set pixel position relative to base
    3942    procedure SetX(X: Integer); inline; // Set horizontal pixel position relative to base
     43    procedure CheckRange; inline; // Check if current pixel position is not out of range
     44    class function Create(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0): TPixelPointer; static;
    4045  end;
    4146  PPixelPointer = ^TPixelPointer;
    4247
    43   function PixelPointer(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0): TPixelPointer; inline;
    4448  function Color32ToColor(Color: TColor32): TColor;
    4549  function ColorToColor32(Color: TColor): TColor32;
     
    4852implementation
    4953
    50 { TPixel32 }
     54resourcestring
     55  SOutOfRange = 'Pixel pointer out of range';
     56  SWrongBitmapSize = 'Wrong bitmap size [width: %d, height: %d]';
     57
     58  { TPixel32 }
    5159
    5260procedure TPixel32.SetRGB(Color: TColor32);
     
    97105end;
    98106
    99 function PixelPointer(Bitmap: TRasterImage; BaseX: Integer;
     107procedure TPixelPointer.CheckRange;
     108begin
     109  {$IFOPT R+}
     110  if (PByte(Pixel) < PByte(Data)) or
     111    (PByte(Pixel) >= PByte(Data) + (Width + Height * BytesPerLine) * BytesPerPixel) then
     112    raise Exception.Create(SOutOfRange);
     113  {$ENDIF}
     114end;
     115
     116class function TPixelPointer.Create(Bitmap: TRasterImage; BaseX: Integer;
    100117  BaseY: Integer): TPixelPointer;
    101118begin
     119  Result.Width := Bitmap.Width;
     120  Result.Height := Bitmap.Height;
     121  if (Result.Width < 0) or (Result.Height < 0) then
     122    raise Exception.Create(Format(SWrongBitmapSize, [Result.Width, Result.Height]));
    102123  Result.BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
    103124  Result.BytesPerPixel := Bitmap.RawImage.Description.BitsPerPixel shr 3;
     125  Result.Data := PPixel32(Bitmap.RawImage.Data);
    104126  Result.Base := PPixel32(Bitmap.RawImage.Data + BaseX * Result.BytesPerPixel +
    105127    BaseY * Result.BytesPerLine);
Note: See TracChangeset for help on using the changeset viewer.