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/Dpi.PixelPointer.pas

    r468 r487  
    44
    55uses
    6   Classes, SysUtils, Dpi.Graphics;
     6  Classes, SysUtils, Dpi.Graphics, Dpi.Common;
    77
    88type
     
    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
     
    8391  Line := Pointer(Line) + BytesPerLine;
    8492  Pixel := Line;
     93  CheckRange;
    8594end;
    8695
     
    8998  Line := Pointer(Line) - BytesPerLine;
    9099  Pixel := Line;
     100  CheckRange;
    91101end;
    92102
     
    94104begin
    95105  Pixel := Pointer(Pixel) + BytesPerPixel;
     106  CheckRange;
    96107end;
    97108
     
    99110begin
    100111  Pixel := Pointer(Pixel) - BytesPerPixel;
     112  CheckRange;
    101113end;
    102114
     
    110122begin
    111123  Pixel := Pointer(Line) + X * BytesPerPixel;
     124  CheckRange;
     125end;
     126
     127procedure TPixelPointer.CheckRange;
     128begin
     129  {$IFOPT R+}
     130  if (PByte(Pixel) < PByte(Data)) or
     131    (PByte(Pixel) >= PByte(Data) + (Width + Height * BytesPerLine) * BytesPerPixel) then
     132    raise Exception.Create(SOutOfRange);
     133  {$ENDIF}
    112134end;
    113135
     
    298320  BaseY: Integer): TPixelPointer;
    299321begin
     322  Result.Width := ScaleToNative(Bitmap.Width);
     323  Result.Height := ScaleToNative(Bitmap.Height);
     324  if (Result.Width < 0) or (Result.Height < 0) then
     325    raise Exception.Create(Format(SWrongBitmapSize, [Result.Width, Result.Height]));
    300326  Result.BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
    301327  Result.BytesPerPixel := Bitmap.RawImage.Description.BitsPerPixel shr 3;
     328  Result.Data := PPixel32(Bitmap.RawImage.Data);
    302329  Result.Base := PPixel32(Bitmap.RawImage.Data + BaseX * Result.BytesPerPixel +
    303330    BaseY * Result.BytesPerLine);
Note: See TracChangeset for help on using the changeset viewer.