当前位置: 技术问答>linux和unix
急,急,急。。在LINUX下无何处理BMP图片呢?如果把三副BMP合成一副呢?
来源: 互联网 发布时间:2016-02-08
本文导语: 急,急,急。。在LINUX下无何处理BMP图片呢?如果把三副BMP合成一副呢? 望高手提供思路或者提供源代码例子,或者提供一些线索。。。 公司要我做,现在遇到麻烦了。。。 | 贴个源码参考,加载部分 bool LoadBmp(...
急,急,急。。在LINUX下无何处理BMP图片呢?如果把三副BMP合成一副呢?
望高手提供思路或者提供源代码例子,或者提供一些线索。。。
公司要我做,现在遇到麻烦了。。。
|
贴个源码参考,加载部分
bool LoadBmp(ImgInf * pRetInf, const char * psz)
{
bool bRet;
FILE * fh=NULL;
int nSize=0, nReadSize;
uint8 * pBuf=NULL;
fh=OpenImageFile(psz);
if(!fh)
return false;
nSize=GetFileSize(fh);
if(!nSize) return false;
pBuf=(uint8 *)malloc(nSize);
if(!pBuf)
{
dprintf("n*** Error: malloc failed to read file!");
return false;
}
memset(pBuf, 0, nSize);
nReadSize=fread(pBuf, 1, nSize, fh);
if(nReadSize != nSize)
{
dprintf("n*** Error: Read size %d != Real size %d", nReadSize, nSize);
}
fclose(fh);
bRet=ConvertBmpToImgInf(pRetInf, pBuf);
if(!bRet)
{
dprintf("nConvertBmpToImgInf failed!");
}
free(pBuf);
return bRet;
}
FILE * OpenImageFile(const char * psz)
{
FILE *fh;
char szPathName[MAX_PATHNAME];
if(!psz || !(*psz)) return false;
AddDefPath_Postfix(szPathName, psz, NULL, NULL);
fh=fopen(szPathName, "rb");
if(!fh)
{
dprintf("nOpen %s failed!", szPathName);
return NULL;
}
return fh;
}
bool ConvertBmpToImgInf(ImgInf * pImgInf, uint8 * pBuf)
{
ColorTbl * pTbl, * pCurrTbl;
uint16 nBitsPerPixel, biCompression, biPlanes;
uint8 * pData, * pDataCurr, * pLineDataCurr;
uint32 nCurrX, nCurrY, nWidth, nHeight;
int nLineDataSize, nTmp;
uint32 nSrcColor, nSht, biSize;
color CurrColor, *pCurrCol;
if(!pImgInf || !pBuf) return false;
biSize =*((uint32*)(pBuf+nSizeBmpFileHeader+0));
nWidth =*((uint32*)(pBuf+nSizeBmpFileHeader+4));
nHeight =*((uint32*)(pBuf+nSizeBmpFileHeader+8));
biPlanes =*((uint16*)(pBuf+nSizeBmpFileHeader+12));
nBitsPerPixel =*((uint16*)(pBuf+nSizeBmpFileHeader+14));
biCompression =*((uint32*)(pBuf+nSizeBmpFileHeader+16));
if(biCompression)
{
dprintf("n*** Error: Can not open compressed file");
return false;
}
if(nBitsPerPixel != 1 && nBitsPerPixel != 2 && nBitsPerPixel != 4 && nBitsPerPixel != 8 &&
nBitsPerPixel != 16 && nBitsPerPixel != 24)
{
dprintf("n*** Error: The Specified file has an unknown bit format!");
return false;
}
pImgInf->size.x=nWidth;
pImgInf->size.y=nHeight;
pImgInf->pData=(color *)malloc(sizeof(color)*pImgInf->size.x*pImgInf->size.y);
if(!pImgInf->pData)
{
dprintf("n*** Error: Alloc memory for image info failed!");
return false;
}
pCurrCol=pImgInf->pData;
pData=pBuf+*(uint32*)(pBuf+10);
if(nBitsPerPixel2);
CurrColor>3);
//*/
/*
CurrColor |=(color)(pCurrTbl->blue>>3);
CurrColor>2);
CurrColor>3);
*/
}
else
{
pLineDataCurr=pDataCurr+(nCurrX*nBitsPerPixel)/8;
switch(nBitsPerPixel)
{
case 16:
CurrColor =*((color*)pLineDataCurr);
break;
case 24:
/*
CurrColor|=(color)(*(pLineDataCurr+2)>>3);
CurrColor2);
CurrColor3);
*/
/*
CurrColor|=(color)((*(pLineDataCurr+0)>>3)&0x1f);
CurrColor2)&0x3f);
CurrColor3)&0x1f);
*/
CurrColor|=((((color)(*(pLineDataCurr+2)))
bool LoadBmp(ImgInf * pRetInf, const char * psz)
{
bool bRet;
FILE * fh=NULL;
int nSize=0, nReadSize;
uint8 * pBuf=NULL;
fh=OpenImageFile(psz);
if(!fh)
return false;
nSize=GetFileSize(fh);
if(!nSize) return false;
pBuf=(uint8 *)malloc(nSize);
if(!pBuf)
{
dprintf("n*** Error: malloc failed to read file!");
return false;
}
memset(pBuf, 0, nSize);
nReadSize=fread(pBuf, 1, nSize, fh);
if(nReadSize != nSize)
{
dprintf("n*** Error: Read size %d != Real size %d", nReadSize, nSize);
}
fclose(fh);
bRet=ConvertBmpToImgInf(pRetInf, pBuf);
if(!bRet)
{
dprintf("nConvertBmpToImgInf failed!");
}
free(pBuf);
return bRet;
}
FILE * OpenImageFile(const char * psz)
{
FILE *fh;
char szPathName[MAX_PATHNAME];
if(!psz || !(*psz)) return false;
AddDefPath_Postfix(szPathName, psz, NULL, NULL);
fh=fopen(szPathName, "rb");
if(!fh)
{
dprintf("nOpen %s failed!", szPathName);
return NULL;
}
return fh;
}
bool ConvertBmpToImgInf(ImgInf * pImgInf, uint8 * pBuf)
{
ColorTbl * pTbl, * pCurrTbl;
uint16 nBitsPerPixel, biCompression, biPlanes;
uint8 * pData, * pDataCurr, * pLineDataCurr;
uint32 nCurrX, nCurrY, nWidth, nHeight;
int nLineDataSize, nTmp;
uint32 nSrcColor, nSht, biSize;
color CurrColor, *pCurrCol;
if(!pImgInf || !pBuf) return false;
biSize =*((uint32*)(pBuf+nSizeBmpFileHeader+0));
nWidth =*((uint32*)(pBuf+nSizeBmpFileHeader+4));
nHeight =*((uint32*)(pBuf+nSizeBmpFileHeader+8));
biPlanes =*((uint16*)(pBuf+nSizeBmpFileHeader+12));
nBitsPerPixel =*((uint16*)(pBuf+nSizeBmpFileHeader+14));
biCompression =*((uint32*)(pBuf+nSizeBmpFileHeader+16));
if(biCompression)
{
dprintf("n*** Error: Can not open compressed file");
return false;
}
if(nBitsPerPixel != 1 && nBitsPerPixel != 2 && nBitsPerPixel != 4 && nBitsPerPixel != 8 &&
nBitsPerPixel != 16 && nBitsPerPixel != 24)
{
dprintf("n*** Error: The Specified file has an unknown bit format!");
return false;
}
pImgInf->size.x=nWidth;
pImgInf->size.y=nHeight;
pImgInf->pData=(color *)malloc(sizeof(color)*pImgInf->size.x*pImgInf->size.y);
if(!pImgInf->pData)
{
dprintf("n*** Error: Alloc memory for image info failed!");
return false;
}
pCurrCol=pImgInf->pData;
pData=pBuf+*(uint32*)(pBuf+10);
if(nBitsPerPixel2);
CurrColor>3);
//*/
/*
CurrColor |=(color)(pCurrTbl->blue>>3);
CurrColor>2);
CurrColor>3);
*/
}
else
{
pLineDataCurr=pDataCurr+(nCurrX*nBitsPerPixel)/8;
switch(nBitsPerPixel)
{
case 16:
CurrColor =*((color*)pLineDataCurr);
break;
case 24:
/*
CurrColor|=(color)(*(pLineDataCurr+2)>>3);
CurrColor2);
CurrColor3);
*/
/*
CurrColor|=(color)((*(pLineDataCurr+0)>>3)&0x1f);
CurrColor2)&0x3f);
CurrColor3)&0x1f);
*/
CurrColor|=((((color)(*(pLineDataCurr+2)))