Delphi程序转换成VC5程序?(100分)

L

ldg

Unregistered / Unconfirmed
GUEST, unregistred user!
本人由于Delphi刚入门,现想将下面这段程序转换成VC5程序,
敬请帮忙,或告之Delphi变量与VC5变量的对应关系?
function bbb(xyz:string):integer;stdcall
var
InFile: TMemoryStream;
BMPFile: TMemoryStream;
AWord: word;
AByte: Byte;
i: Longint;
Max: word;
Min: word;
ALong: Longint;
begin

InFile:= TMemoryStream.Create;
BMPFile:= TMemoryStream.Create;
InFile.LoadFromFile('C:/Temp1.BMP');
InFIle.Seek(0, 0);
for Max:= 1 to 539do
begin
InFIle.Read(AWord, 2);
BMPFile.Write(Aword, 2);
end;

BMPFile.Seek(54, 0);
for i:= 0 to 255do
begin
AByte:= i;
BMPFile.Write(AByte, 1);
BMPFile.Write(AByte, 1);
BMPFile.Write(AByte, 1);
Abyte:=0;
BMPFile.Write(AByte, 1);
end;

InFIle.Clear;
InFile.LoadFromFile(xyz);
InFile.Seek(1080, 0);
Max:= 0;
Min:= $FFFF;
for i:= 1 to 320*240do
begin
InFile.Read(AWord, 2);
AWord:= Swap(Aword);
if AWord>Max then
Max:= AWord;
if AWord<Min then
Min:= AWord;
end;

InFile.Seek(1080, 0);
for i:= 1 to 320*240do
begin
InFile.Read(AWord, 2);
AWord:= Swap(Aword);
AByte:= round((AWord-Min)/(Max-Min)*255);
BMPFile.Write(AByte, 1);
end;

ALong:= 320*240+1078;
BMPFile.Seek(2, 0);
BMPFile.Write(ALong, 4);
ALong:= 320;
BMPFile.Seek(18, 0);
BMPFile.Write(ALong, 4);
ALong:= 240;
BMPFile.Write(ALong, 4
BMPFile.SaveToFile('C:/Temp.BMP');
end;
 
自己改吧, 变换大致如下:
InFile, BMPFile实质上是一个文件缓冲区, 可以用char *InFile, *BMPFile
来代替, 要自己控制下标的移动. create用malloc分配内存, 最后要free掉.
loadFromFile可以用文件块读写函数, 用fopen打开一个文件句柄操作.也可以用
文件流来操作(>>). 写文件也是类似.
Word对应为unsigned short或WORD(Windows下, 注意大写)
Longint, integer对应为int, byte对应为unsigned char
delphi中赋值符号:=对应于VC中的=, 还有if和for的语法不太一样, 自己调整
一下啦, 要注意的是C中的逻辑等于是==而不是=! begin
end对应的是{ }
函数头定义自己改改吧.
 
还是请高手帮忙翻译一下,我再加100分.
function bbb(xyz:string):integer;stdcall
var
InFile: TMemoryStream;
BMPFile: TMemoryStream;
AWord: word;
AByte: Byte;
i: Longint;
Max: word;
Min: word;
ALong: Longint;
begin
InFile:= TMemoryStream.Create;
BMPFile:= TMemoryStream.Create;
InFile.LoadFromFile('C:/Temp1.BMP');
InFIle.Seek(0, 0);
for Max:= 1 to 539do
begin
InFIle.Read(AWord, 2);
BMPFile.Write(Aword, 2);
end;
BMPFile.Seek(54, 0);
for i:= 0 to 255do
begin
AByte:= i;
BMPFile.Write(AByte, 1);
BMPFile.Write(AByte, 1);
BMPFile.Write(AByte, 1);
Abyte:=0;
BMPFile.Write(AByte, 1);
end;
InFIle.Clear;
InFile.LoadFromFile(xyz);
InFile.Seek(1080, 0);
Max:= 0;
Min:= $FFFF;
for i:= 1 to 320*240do
begin
InFile.Read(AWord, 2);
AWord:= Swap(Aword);
if AWord>Max then
Max:= AWord;
if AWord<Min then
Min:= AWord;
end;
InFile.Seek(1080, 0);
for i:= 1 to 320*240do
begin
InFile.Read(AWord, 2);
AWord:= Swap(Aword);
AByte:= round((AWord-Min)/(Max-Min)*255);
BMPFile.Write(AByte, 1);
end;
ALong:= 320*240+1078;
BMPFile.Seek(2, 0);
BMPFile.Write(ALong, 4);
ALong:= 320;
BMPFile.Seek(18, 0);
BMPFile.Write(ALong, 4);
ALong:= 240;
BMPFile.Write(ALong, 4);
BMPFile.SaveToFile('C:/Temp.BMP');
end;
 
上面程序没完,在这里补上:
if AWord>Max then
Max:= AWord;
if AWord<Min then
Min:= AWord;
end;
InFile.Seek(1080, 0);
for i:= 1 to 320*240do
begin
InFile.Read(AWord, 2);
AWord:= Swap(Aword);
AByte:= round((AWord-Min)/(Max-Min)*255);
BMPFile.Write(AByte, 1);
end;
ALong:= 320*240+1078;
BMPFile.Seek(2, 0);
BMPFile.Write(ALong, 4);
ALong:= 320;
BMPFile.Seek(18, 0);
BMPFile.Write(ALong, 4);
ALong:= 240;
BMPFile.Write(ALong, 4);
BMPFile.SaveToFile('C:/Temp.BMP');
end;
 
真是莫名其妙,程序贴不完全,还得继续贴:
InFile.Seek(1080, 0);
for i:= 1 to 320*240do
begin
InFile.Read(AWord, 2);
AWord:= Swap(Aword);
AByte:= round((AWord-Min)/(Max-Min)*255);
BMPFile.Write(AByte, 1);
end;
ALong:= 320*240+1078;
BMPFile.Seek(2, 0);
BMPFile.Write(ALong, 4);
ALong:= 320;
BMPFile.Seek(18, 0);
BMPFile.Write(ALong, 4);
ALong:= 240;
BMPFile.Write(ALong, 4);
BMPFile.SaveToFile('C:/Temp.BMP');
end;
 
改编了一下, 大致的差不多了, 细节上你自己慢慢调整吧.
没调试过, 自己调试一下.

#include <stdio.h>
#include <mem.h>
#include <windows.h>
#ifndef WORD
#define WORD unsigned short
#endif
#ifndef BYTE
#define BYTE unsigned char
#endif
#ifndef WINAPI
#define WINAPI far pascal
#endif
#define BUFSIZE 16384
#ifndef swap
#define swap(WORD w) ( ((w &amp;
0x00FF)<<16) | ((w &amp;
0xFF00)>>16) )
#endif
int WINAPI bbb(char *xyz)
{
char *InFile;
char *BMPFile;
WORD AWord;
BYTE AByte;
long i;
long k;
WORD Max;
WORD Min;
long ALong;
FILE *InF;
FILE *OutF;
if ((InFile = (char *)malloc(BUFSIZE))==NULL)
{
return -3;
}
if ((BMPFile = (char *)malloc(BUFSIZE))==NULL)
{
return -4;
}
if (InF=fopen("C:/Temp1.BMP", "rb")==NULL)
{
/* error open source file*/
return -1;
}
if (OutF=fopen("C:/Temp.BMP", "wb")==NULL)
{
/* error write target file*/
return -2;
}
i=fread(InFile, sizeof(char), 539*2, InF);
memncpy(BMPFile, InFile, 539*2);
ALong = 320*240+1078;
(LONG)*(BMPFile+2) = ALong;
ALong:= 320;
(LONG)*(BMPFile+18) = ALong;
ALong:= 240;
(LONG)*(BMPFile+18+sizeof(long)) = ALong;

k = 54;
for (i=0;i<256;i++)
{
*(BMPFile+k) = (BYTE)(i &amp;
0xFF);
k++;
*(BMPFile+k) = (BYTE)(i &amp;
0xFF);
k++;
*(BMPFile+k) = (BYTE)(i &amp;
0xFF);
k++;
*(BMPFile+k) = 0;
k++;
}
fwrite(BMPFile, sizeof(char), k, OutF);
fclose(InF);
if (InF=fopen(xyz, "rb")==NULL)
{
/* error open xyz file*/
return -5;
}

fseek(InF, 1080, SEEK_SET);

Max = 0;
Min = 0xFFFF;

for (i=1;i<=(320*240);i++)
{
fread(&amp;AWord, sizeof(char), 2, InF);
AWord = swap(Aword);
if (AWord>Max)
{
Max = AWord;
}
if (AWord<Min)
{
Min = AWord;
}
}
fseek(InF, 1080, SEEK_SET);
for (i=1;i<=(320*240);i++)
{
fread(&amp;AWord, sizeof(char), 2, InF);
AWord = swap(Aword);
AByte:= (AWord-Min)*255/(Max-Min);
fwrite(&amp;AByte, sizeof(char), 1, OutF);
}

fclose(OutF);
free(BMPFile);
free(InFile);
}
 
我接受答案,并打算再给你加100分,但不知怎样加,望告之.非常感谢!!
 

Similar threads

顶部