请翻译C函数,写成d的 ( 积分: 200 )

D

deepfar

Unregistered / Unconfirmed
GUEST, unregistred user!
1.第一个
BOOL MirrorDIB(LPSTR lpDIBBits, LONG lWidth, LONG lHeight, BOOL bDirection,int nImageBits)
{
// 指向源图像的指针
LPSTR lpSrc;
// 指向要复制区域的指针
LPSTR lpDst;
// 指向复制图像的指针
LPSTR lpBits;
HLOCAL hBits;
// 循环变量
LONG i;
LONG j;
int nBits;//每像素占的位数
// 图像每行的字节数
LONG lLineBytes;
// 计算图像每行的字节数
lLineBytes = WIDTHBYTES(lWidth *nImageBits);
// 暂时分配内存,以保存一行图像
hBits = LocalAlloc(LHND, lLineBytes);
if (hBits == NULL)
{
// 分配内存失败
return FALSE;
}
// 锁定内存
lpBits = (char * )LocalLock(hBits);
int nStep=nImageBits/8;
long lCenter=lWidth/2*nStep;
// 判断镜像方式
if (bDirection)
{
// 水平镜像
// 针对图像每行进行操作
for(i = 0; i < lHeight; i++)
{
// 针对每行图像左半部分进行操作
for(j = 0; j < lCenter; j+=nStep)
{
for(nBits=0;nBits<nStep;nBits++)
{
lpSrc = (char *)lpDIBBits + lLineBytes * i +lCenter- j+nBits;
lpDst = (char *)lpDIBBits + lLineBytes * i +lCenter+ j+nBits;
*lpBits = *lpDst;
*lpDst = *lpSrc;
*lpSrc = *lpBits;
}

}

}
}
else
{
// 垂直镜像
// 针对上半图像进行操作
for(i = 0; i < lHeight / 2; i++)
{
// 指向倒数第i行象素起点的指针
lpSrc = (char *)lpDIBBits + lLineBytes * i;
// 指向第i行象素起点的指针
lpDst = (char *)lpDIBBits + lLineBytes * (lHeight - i - 1);
// 备份一行,宽度为lWidth
memcpy(lpBits, lpDst, lLineBytes);
// 将倒数第i行象素复制到第i行
memcpy(lpDst, lpSrc, lLineBytes);
// 将第i行象素复制到倒数第i行
memcpy(lpSrc, lpBits, lLineBytes);

}
}
// 释放内存
LocalUnlock(hBits);
LocalFree(hBits);
// 返回
return TRUE;
}

2.第二个
void SaveBmpFile24(LPCSTR szFileName, BYTE* srcImage, LONG lWidth, LONG lHeight)
{
BITMAPFILEHEADER bfh = { NULL };
BITMAPINFOHEADER bih = { NULL };

LONG lImageSize = lHeight * WIDTHBYTES(lWidth * 24);
bfh.bfType = 0x4d42;
bfh.bfSize = lImageSize + sizeof(bfh) + sizeof(bih);
bfh.bfOffBits = sizeof(bfh) + sizeof(bih);

ContructBih(lWidth, lHeight, bih);

FILE* mFile = fopen(szFileName, &quot;wb&quot;);
fwrite(&amp;bfh, sizeof(bfh), 1, mFile);
fwrite(&amp;bih, sizeof(bih), 1, mFile);
fwrite(srcImage, lImageSize, 1, mFile);
fclose(mFile);
}

void ContructBih(int nWidth, int nHeight, BITMAPINFOHEADER&amp; bih)
{
bih.biSize = sizeof(bih);
bih.biWidth = nWidth;
bih.biHeight = nHeight;
bih.biPlanes = 1;
bih.biBitCount = 24;
bih.biCompression = BI_RGB;
bih.biSizeImage = nHeight * WIDTHBYTES(nWidth * 24);
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
}
请各位高手忙!谢谢!
 
靠!这也太长了吧?而且都是指针,翻译成d也不见得管用啊!
我们只能学习思想和他的解决方法
 
to: lovezyp
要先翻译好了才能学习呀!不然看不懂哦!

还请高手们帮忙啊!~~~~~~~~~~~
 
//这个函数不知道你没给出源代码,我为了编译.先给一个空的
function WIDTHBYTES(V: Integer): Integer;
begin
Result := 0;
end;

function MirrorDIB(lpDIBBits: LPSTR;
lWidth, lHeight: Longint; bDirection: BOOL;
nImageBits: Integer): BOOL;
var
// 指向源图像的指针
lpSrc : LPSTR;
// 指向要复制区域的指针
lpDst : LPSTR;
// 指向复制图像的指针
lpBits : LPSTR;
hBits : HLOCAL;
// 循环变量
i : Integer;
j : Integer;
nBits : Integer; //每像素占的位数
// 图像每行的字节数
lLineBytes : Integer;
nStep, lCenter : Integer;
begin
Result := False;
// 计算图像每行的字节数
lLineBytes := WIDTHBYTES(lWidth * nImageBits);
// 暂时分配内存,以保存一行图像
hBits := LocalAlloc(LHND, lLineBytes);
if (hBits = 0) then
// 分配内存失败
Exit;
// 锁定内存
lpBits := PChar(LocalLock(hBits));
nStep := nImageBits div 8;
lCenter := lWidth div 2 * nStep;
if (bDirection) then
begin
// 水平镜像
// 针对图像每行进行操作
for i := 0 to lHeight - 1 do
begin
// 针对每行图像左半部分进行操作
J := 0;
while J < lCenter do
begin
for nBits := 0 to nStep - 1 do
begin
lpSrc := PChar(lpDIBBits) + lLineBytes * i + lCenter - j + nBits;
lpDst := PChar(lpDIBBits) + lLineBytes * i + lCenter + j + nBits;
lpBits^ := lpDst^;
lpDst^ := lpSrc^;
lpSrc^ := lpBits^;
end;
Inc(J, nStep);
end;
end;
end
else
begin
// 垂直镜像
// 针对上半图像进行操作
for i := 0 to lHeight div 2 - 1 do
begin
// 指向倒数第i行象素起点的指针
lpSrc := PChar(lpDIBBits) + lLineBytes * i;
// 指向第i行象素起点的指针
lpDst := PChar(lpDIBBits) + lLineBytes * (lHeight - i - 1);
// 备份一行,宽度为lWidth
CopyMemory(lpBits, lpDst, lLineBytes);
// 将倒数第i行象素复制到第i行
CopyMemory(lpDst, lpSrc, lLineBytes);
// 将第i行象素复制到倒数第i行
CopyMemory(lpSrc, lpBits, lLineBytes);

end;
end;
// 释放内存
LocalUnlock(hBits);
LocalFree(hBits);
// 返回
Result := TRUE;
end;

procedure SaveBmpFile24(szFileName: LPCSTR; srcImage: PChar; lWidth, lHeight: Longint);
var
bfh : TBitmapFileHeader;
bih : TBitmapInfoHeader;
lImageSize : Integer;
hFile : THandle;
begin
ZeroMemory(@bfh, SizeOf(TBitmapFileHeader));
ZeroMemory(@bih, SizeOf(TBitmapInfoHeader));
lImageSize := lHeight * WIDTHBYTES(lWidth * 24);

bfh.bfType := $4D42;
bfh.bfSize := lImageSize + sizeof(bfh) + sizeof(bih);
bfh.bfOffBits := sizeof(bfh) + sizeof(bih);

ContructBih(lWidth, lHeight, bih);

hFile := FileOpen(szFileName, fmOpenReadWrite or fmShareDenyNone);
FileWrite(hFile, bfh, sizeof(bfh));
FileWrite(hFile, bih, sizeof(bih));
FileWrite(hFile, srcImage, lImageSize);
FileClose(hFile);

end;

procedure ContructBih(nWidth, nHeight: Integer; var bih: TBitmapInfoHeader);
begin
with bih do
begin
biSize := sizeof(bih);
biWidth := nWidth;
biHeight := nHeight;
biPlanes := 1;
biBitCount := 24;
biCompression := BI_RGB;
biSizeImage := nHeight * WIDTHBYTES(nWidth * 24);
biXPelsPerMeter := 0;
biYPelsPerMeter := 0;
biClrUsed := 0;
biClrImportant := 0;
end;
end;
 
顶部