中秋月圆时一个关于位图处理的问题(特邀季老大加入)
灯火斑斓 2003-09-12 02:00:54 我的项目遇到一个位图处理的问题,平时主要开发数据库,图像处理第一次遇,请各位高手,特别是想请季老大帮忙指导指导.
问题描述,先把位图处理成单色位图(1bit);然后从图片的第一列开始扫描,把第一至第八个像素读取出来放到一个Byte中,即Pix[0][0]放到B0,Pix[0][1]放到B1,Pix[0][2]放到B3,以此类推;读完第一列前八个像素后,再读第二列前八个像素,同样的处理,读完一个Width循环后,再读第一列9至16个像素(若未足8个像素则补零)以此类推直至读完本幅图的像素.因为处理完的文件是给单片机的液晶显示屏用的,所以要求在读原始位图的像素时,要按白-1,黑-0的方式来处理,也就是说放到B0-B7的值就只能是0,1;我现在是通过提取每个像素的颜色RGB数值来判断的,直觉告诉我这有问题.下面是我的源码,请高手赐教.非常感谢!
int iLen,iCount,iWidth,iHeight,iData;
int iFrom,iTo;
long lData;
int R,G,B;
TColor Clr;
FILE *fp;
fp=fopen("123.bmp","wb");
if(fp==NULL)
{
ShowMessage("Logo图片保存文件创建失败!");
return false;
}
AnsiString sFileName="abc.bmp";
if(FileExists(sFileName))
{
Graphics::TBitmap *tmpBitmap = new Graphics::TBitmap();
tmpBitmap->LoadFromFile(sFileName);
tmpBitmap->PixelFormat=pf1bit;
tmpBitmap->SaveToFile("tmp.bmp");
tmpBitmap->LoadFromFile("tmp.bmp");
iWidth=tmpBitmap->Width;
iHeight=tmpBitmap->Height;
lData=iWidth; //本幅图片的宽度
if(!DecToHex(lData))
{
Char[0]=1;Char[1]=0;Char[2]=0;Char[3]=0;
}
sStr = AnsiString((char)(int)Char[2])+AnsiString((char)(int)Char[1])+AnsiString((char)(int)Char[0]);
fwrite(sStr.c_str(),sStr.Length(),1,fp);
lData=iHeight; //本幅图片的高度
if(!DecToHex(lData))
{
Char[0]=1;Char[1]=0;Char[2]=0;Char[3]=0;
}
sStr = AnsiString((char)(int)Char[2])+AnsiString((char)(int)Char[1])+AnsiString((char)(int)Char[0]);
fwrite(sStr.c_str(),sStr.Length(),1,fp);
iTo=0;
iCount=0;
while(true)
{
if(iTo>=iHeight) {break;}
for(int i=0;i<iWidth;i++)
{
iFrom=8*iCount;
iTo=8*(iCount+1);
if(iTo>iHeight)
{
sStrTmp="";
for(int j=iFrom;j<iHeight;j++)
{
Clr=tmpBitmap->Canvas->Pixels[i][j];
R=(Clr>>16)&0xff;
G=(Clr>>8)&0xff;
B=Clr&0xff;
if(R<5&&G<5&&B<5)
sStrTmp ="0"+sStrTmp;
else
sStrTmp ="1"+sStrTmp;
}
for(int i=iHeight+1;i<=iTo;i++)
sStrTmp ="0"+sStrTmp;
}
else
{
sStrTmp="";
for(int j=iFrom;j<iTo;j++)
{
Clr = tmpBitmap->Canvas->Pixels[i][j];
R=(Clr>>16)&0xff;
G=(Clr>>8)&0xff;
B=Clr&0xff;
if(R<5&&G<5&&B<5)
sStrTmp ="0"+sStrTmp;
else
sStrTmp ="1"+sStrTmp;
}
}
iData=BINStrToChar(sStrTmp); //把二进制字符串转换成十进制数
sStr=AnsiString((char)iData);
fwrite(sStr.c_str(),sStr.Length(),1,fp);
}
iCount++;
sStr="";
}
delete tmpBitmap;
}
}
fclose(fp);