首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

求大哥帮小弟我解释一个函数

2012-05-23 
求大哥帮我解释一个函数Delphi(Pascal) codefunction BytesPerScanline(PixelsPerScanline, BitsPerPixel,

求大哥帮我解释一个函数

Delphi(Pascal) code
function BytesPerScanline(PixelsPerScanline, BitsPerPixel, Alignment: Longint): Longint;begin  Dec(Alignment);  Result := ((PixelsPerScanline * BitsPerPixel) + Alignment) and not Alignment;  Result := Result div 8;end;

帮助文档有这个函数,但是只写了个声明。

[解决办法]
function BytesPerScanline(PixelsPerScanline, BitsPerPixel, Alignment: Longint): Longint;
begin
Dec(Alignment); //相当于Alignment := Alignment-1;
Result := ((PixelsPerScanline * BitsPerPixel) + Alignment) and not Alignment;
//这个是前两数相乘加上第三个数,背后就是Alignment取反,然后与前面括号内的结果进行“位与”
Result := Result div 8;//结果整除8
end;


[解决办法]
Dec(Alignment);
Result := ((PixelsPerScanline * BitsPerPixel) + Alignment) and not Alignment;

进行PixelsPerScanline * BitsPerPixel的结果,与Alignment进行字节对齐(请baidu啥叫字节对齐)

然后result div 8,8就是byte的bit数,也就是字节数

整个函数的意思是:
根据传入bitmap的width及格式,求出bitmap每行需要占用的字节数,一般用于直接操作bitmap的内存块操作。

每个赋值其实我都懂了,但连在一块就不懂了,我也经常这样,一般就是上下关联多看看

[解决办法]
字节对齐的基本操作就是:
result := (v + (a - 1)) and not (a - 1);

如:
v=1,a=4, r = 4
v=4,a=4, r = 4
v=5,a=4, r = 8
...
意思是:根据v,求出与a整倍的值

热点排行