在Delphi中,“type PDW = ^dword;”是什么意思?

3

在将Delphi代码转换为C时,我发现有些复杂。

以下是Delphi的完整代码:

unit resizeunit;

{ resize bm1 to fit in bm2
  bm1,bm2 bitmaps must be created and width,height set }

interface

uses windows,graphics;

procedure BMresize;

var bm1,bm2 : Tbitmap;

implementation

type PDW = ^dword;

procedure BMresize;
//copy bm1 to bm2
var ps0,pd0,psStep,pdStep : dword;       //scanline[0], row steps
    sx1,sy1,sx2,sy2 : single;             //source field positions
    x,y,i,j,destwidth,destheight : word;  //source,dest field pixels
    destR,destG,destB : single;           //destination colors
    sR,sG,sB : byte;                      //source colors
    fx,fy,fix,fiy,dyf : single;           //factors
    fxstep,fystep, dx,dy : single;
    color : dword;
    pdy,pdx,psi,psj : dword;
    AP : single;
    istart,iend,jstart,jend : word;
    devX1,devX2,devY1,devY2 : single;
begin
 ps0 := DWORD(bm1.scanline[0]);
 psstep := ps0 - DWORD(bm1.scanline[1]);
 pd0 := DWORD(bm2.scanline[0]);
 pdstep := pd0 - DWORD(bm2.scanline[1]);
 destwidth := bm2.Width-1;
 destheight := bm2.Height-1;
 fx := bm1.width/ bm2.width;
 fy := bm1.height/bm2.height;
 fix := 1/fx;
 fiy := 1/fy;
 fxstep := 0.9999 * fx;
 fystep := 0.9999 * fy;
 pdy := pd0;
 for y := 0 to destheight do         //vertical destination pixels
  begin
   sy1 := fy * y;
   sy2 := sy1 + fystep;
   jstart := trunc(sy1);
   jend := trunc(sy2);
   devY1 := 1-sy1+jstart;
   devY2 := jend+1-sy2;
   pdx := pdy;
   for x := 0 to destwidth do        //horizontal destination pixels
    begin
     sx1 := fx * x;                        //x related values are repeated
     sx2 := sx1 + fxstep;                  //for each y and may be placed in
     istart := trunc(sx1);                 //lookup table
     iend := trunc(sx2);                   //...
     devX1 := 1-sx1+istart;                  //...
     devX2 := iend+1-sx2;                  //...
     destR := 0; destG := 0; destB := 0;   //clear destination colors
     psj := ps0-jstart*psStep;
     dy := devY1;
     for j := jstart to jend do  //vertical source pixels
      begin
       if j = jend then dy := dy - devY2;
       dyf := dy*fiy;
       psi := psj + (istart shl 2);
       dx := devX1;
       for i := istart to iend do //horizontal source pixels
        begin
         if i = iend then dx := dx - devX2;
         AP := dx*dyf*fix;
         color := PDW(psi)^;
         sB := color;
         destB := destB + sB*AP;
         sG := color shr 8;
         destG := destG + sG*AP;
         sR := color shr 16;
         destR := destR + sR*AP;
         inc(psi,4);
         dx := 1;
        end;//for i
       dec(psj,psStep);
       dy := 1;
      end;//for j
      sB := round(destB);
      color := sB or (sG shl 8) or (sR shl 16);
     PDW(pdx)^ := color;
     inc(pdx,4);
    end;//for x
   dec(pdy,pdstep);
  end;//for y
end;

end.

我正试图理解

关于IT技术方面的相关内容。

type PDW = ^dword; ,inc(psi,4); and dec(psj,psStep); 

意思。

添加

我想知道color = PDW(psi) ^;的含义; 如果我想应用一些数组变量,那我该怎么做?

这来自delphi代码。我正在尝试转换成C语言。 我以前从未接触过delphi代码。

能否请您帮我理解它们的含义并将其转换为C语言?


1
PDW = ^dword; 是将类型 PDW 声明为指向 DWORD 的指针。inc(psi,4); 是将变量 psi 的值增加 4。dec(psj,psStep); 是将变量 psj 的值减去变量 psStep 的值。 - Victoria
@Victoria 我还是不太明白。你能举个例子让我理解你的评论吗? - start02
为什么不使用typedef DWORD *PDW;作为示例呢? - Victoria
1个回答

4

就像维多利亚所说的

type PDW = ^DWORD;

等同于

typedef DWORD* PDW;

此外
inc(psi,4);

等同于

psi+=4;

同样地。
dec(psj,psStep);

等同于

psj-=psStep;

最后
color = PDW(psi)^

等同于

color = *((PDW)psi); // I think. It reads the DWORD value at address psi.

(只有在代码中所示的变量定义的情况下,以上所有内容才有效)

PDW是C语言中的某种数组,例如“unsigned char tmp[]”吗? - start02
@RemyLebeau:不完全正确。类型为“指向数据的指针”的变量可以用作数组。而不是类型定义本身。PDW不是一个变量 - 它是一种类型,因此不是数组。但是,该类型的变量可以用作基础类型(在这种情况下为DWORD)的一维数组。 - HeartWare
@RudyVelthuis:是的,但PDW不是指向数据的指针 - 它是一种类型定义。因此,PDW _不是_“C语言中某些类型的数组”。然而,psi(它的类型是PDW)确实是一个“指向数据”的指针,并且可以用作DWORDs的零基数组。 - HeartWare
@HeartWare:刚刚重新阅读了Remy的话,然后又重新阅读了你的回复。我相信Remy知道类型和变量之间的区别。 - Rudy Velthuis
@RudyVelthuis:我已经阅读(并反复阅读)了Remy所说的话,只能将其解读为对我上面陈述的评论 - 在这种情况下,我的回复是相关的,因为他谈论的是_变量_,而我谈论的是_类型_,“除了在变量声明中,二者永远不会相遇”:-)。是的 - 我也确信Remy知道区别,但也许其他人对Delphi语言的复杂性不如他和我熟悉... - HeartWare
显示剩余13条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接