将基数机打包和解包为四个字节



我必须将一个Cardinal打包和解包为四个单字节字段(Delphi 2010)。

我在一个大图像的所有像素上做这个,所以我需要它快!

谁能告诉我怎么写这两个函数?(const和out关键字只是为了清晰。如果它们干扰内联汇编,那么我可以删除它们。)

procedure FromCardinalToBytes( const aInput: Cardinal;
                               out   aByte1: Byte;
                               out   aByte2: Byte;
                               out   aByte3: Byte;
                               out   aByte4: Byte); inline;
function FromBytesToCardinal( const aByte1: Byte;
                              const aByte2: Byte;
                              const aByte3: Byte;
                              const aByte4: Byte):Cardinal; inline;

我建议不要使用函数,只使用一个变量记录。

type
  TCardinalRec = packed record
    case Integer of
      0: (Value: Cardinal;);
      1: (Bytes: array[0..3] of Byte;);
    end;

那么您可以很容易地使用它来获取单个字节。

var
  LPixel: TCardinalRec;
...
  LPixel.Value := 123455;
  //Then read each of the bytes using
  B1 := LPixel.Bytes[0];
  B2 := LPixel.Bytes[1];
  //etc.

如果必须的话,可以将其放入函数中,但是不必为函数调用的开销而烦恼。


编辑
为了说明可变记录方法的效率,请考虑以下问题(假设您正在从流中读取图像)。

var
  LPixelBuffer: array[0..1023] of TCardinalRec;
...
  ImageStream.Read(LPixelBuffer, SizeOf(LPixelBuffer));
  for I := Low(LPixelBuffer) to High(LPixelBuffer) do
  begin
    //Here each byte is accessible by:
    LPixelBuffer[I].Bytes[0]
    LPixelBuffer[I].Bytes[1]
    LPixelBuffer[I].Bytes[2]
    LPixelBuffer[I].Bytes[3]
  end;

PS:您可以显式地将变体记录中的每个字节命名为Red、Green、Blue(以及第四个字节的任何含义),而不是任意通用的Bytes数组。

有很多方法。最简单的是

function FromBytesToCardinal(const AByte1, AByte2, AByte3,
  AByte4: byte): cardinal; inline;
begin
  result := AByte1 + (AByte2 shl 8) + (AByte3 shl 16) + (AByte4 shl 24);
end;
procedure FromCardinalToBytes(const AInput: cardinal; out AByte1,
  AByte2, AByte3, AByte4: byte); inline;
begin
  AByte1 := byte(AInput);
  AByte2 := byte(AInput shr 8);
  AByte3 := byte(AInput shr 16);
  AByte4 := byte(AInput shr 24);
end;

稍微复杂一点(但不一定更快)的是

function FromBytesToCardinal2(const AByte1, AByte2, AByte3,
  AByte4: byte): cardinal; inline;
begin
  PByte(@result)^ := AByte1;
  PByte(NativeUInt(@result) + 1)^ := AByte2;
  PByte(NativeUInt(@result) + 2)^ := AByte3;
  PByte(NativeUInt(@result) + 3)^ := AByte4;
end;
procedure FromCardinalToBytes2(const AInput: cardinal; out AByte1,
  AByte2, AByte3, AByte4: byte); inline;
begin
  AByte1 := PByte(@AInput)^;
  AByte2 := PByte(NativeUInt(@AInput) + 1)^;
  AByte3 := PByte(NativeUInt(@AInput) + 2)^;
  AByte4 := PByte(NativeUInt(@AInput) + 3)^;
end;

如果你不需要字节是字节变量,你可以做一些更棘手的事情,比如声明

type
  PCardinalRec = ^TCardinalRec;
  TCardinalRec = packed record
    Byte1,
    Byte2,
    Byte3,
    Byte4: byte;
  end;

然后转换为:

var
  c: cardinal;
begin
  c := $12345678;
  PCardinalRec(@c)^.Byte3 // get or set byte 3 in c

如果您希望快速,则需要考虑80x86架构。

速度很大程度上取决于你如何处理字节。x86可以使用AL和AH寄存器非常快地访问底部2字节
(32位EAX寄存器中的最低有效字节)

如果你想获得高阶的两个字节,你不想直接访问它们。因为你会得到一个未对齐的内存访问,浪费cpu周期和搞乱缓存。

让它更快
所有这些干扰单个字节的东西都是不必要的。如果你真的想要更快,一次使用4个字节。

NewPixel:= OldPixel or $0f0f0f0f;

如果你想快速处理像素,使用内联MMX汇编,每次处理8个字节。

链接:
维基百科:http://en.wikipedia.org/wiki/MMX_%28instruction_set%29
MMX指令集说明:http://webster.cs.ucr.edu/AoA/Windows/HTML/TheMMXInstructionSet.html

或者重新问你关于SO的问题:我如何做这个位图操作…在MMX .

非常非常快
如果你想要非常非常快,比如比MMX快100或1000倍,你的显卡可以做到这一点。Google for CUDA或GPGPU

最新更新