zig
zig

Reputation: 4624

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)^;

Upvotes: 2

Views: 267

Answers (2)

David Heffernan
David Heffernan

Reputation: 613013

I think that the simplest way to tackle this in Delphi is to configure the compile to support indexing of pointers:

{$POINTERMATH ON}

Once you include this directive the original code will compile.

Sadly for you this option isn't available in Delphi 7. You'll need to declare a pointer to array of DWORD, and cast to that.

Upvotes: 2

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
  // ...

Upvotes: 4

Related Questions