Search code examples
delphidelphi-7freepascaldelphi-5

FPC pointer operation to Delphi adjustent


I'm trying to adjust some code to Delphi:

https://github.com/cooljeanius/doublecmd/blob/master/src/platform/win/ugdiplus.pas

The original function (I have removed the irrelevant code):

{$mode objfpc}
// ...
function GetBitmapFromARGBPixels(graphics: GPGRAPHICS; pixels: LPBYTE; Width, Height: Integer): GPBITMAP;
var
  pSrc, pDst: LPDWORD;
begin
  // ...
  pSrc := LPDWORD(pixels);
  pDst := LPDWORD(bmData.Scan0);
  // Pixels retrieved by GetDIBits are bottom-up, left-right.
  for x := 0 to Width - 1 do
    for y := 0 to Height - 1 do            
      pDst[(Height - 1 - y) * Width + x] := pSrc[y * Width + x];
  GdipBitmapUnlockBits(Result, @bmData);
end;

How do I translate this line correctly? (pSrc, pDst are LPDWORD):

pDst[(Height - 1 - y) * Width + x] := pSrc[y * Width + x];

Delphi compiler shows error: [Error] Unit1.pas(802): Array type required

I have tried:

type
  _LPDWORD = ^_DWORD;
  _DWORD = array[0..0] of DWORD;
...
_LPDWORD(pDst)^[(Height - 1 - y) * Width + x] := _LPDWORD(pSrc)^[y * Width + x];

I'm not sure if that is correct?

Or maybe this?:

PByte(Cardinal(pDst) + (Height - 1 - y) * Width + x)^ := PByte(Cardinal(pSrc) + y * Width + x)^;

Solution

  • You can do something along the lines of this:

    function GetBitmapFromARGBPixels(graphics: GPGRAPHICS; pixels: LPBYTE; Width, Height: Integer): GPBITMAP;
    const
      MaxArraySize = MaxInt div sizeof(DWord);
    type
      TLongDWordArray = array[0..pred(MaxArraySize)] of DWord;
      PLongDWordArray = ^TLongDWordArray;
    var
      pSrc, pDst: PLongDWordArray;
    begin
      // ...