征集错误
#include "EWBMPAPI.h"
//declare iner function
//declare global var
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFOHEADER bmpInfoHeader;
BYTE* pBMP;
BYTE* pDot;
LONG ByteNumPerLine = 0;
BYTE g_rgbQuad[64]={0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,
0x00,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
0x80,0x00,0x00,0x00,0x80,0x00,0x80,0x00,
0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,
0xC0,0xC0,0xC0,0x00,0x00,0x00,0xFF,0x00,
0x00,0xFF,0x00,0x00,0x00,0xFF,0xFF,0x00,
0xFF,0x00,0x00,0x00,0xFF,0x00,0xFF,0x00,
0xFF,0xFF,0x00,0x00,0xFF,0xFF,0xFF,0x00};
#define DEFBMPAPI_REDCOLORLOW 0x09
#define DEFBMPAPI_REDCOLORHIGH 0x90
#define DEFBMPAPI_BLKCOLORLOW 0x00
#define DEFBMPAPI_BLKCOLORHIGH 0x00
//===================================================================
int EWBMP_ChkDatFile(int nWidth, int nHeight, char* fPathBlack, char* fPathRed);
int EWBMP_CreaHead(void);
int EWBMP_DoBkDat(char* fullFilePath,int nWidth, int nHeight, int nByte);
int EWBMP_DoRkDat(char* fullFilePath,int nWidth, int nHeight, int nByte);
int EWBMP_WriteToFile(char* fullFilePath,long size);
inline BOOL TestBit(BYTE num,int bit)
{
if(num>>bit&0x01==1)
return TRUE;
else
return FALSE;
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
//init
pBMP = NULL;
pDot = NULL;
break;
}
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
{
break;
}
}
return TRUE;
}
//////////////////////////////////////////////////////////
int EWBMP_ChkDatFile(int nWidth, int nHeight, char* fPathBlack, char* fPathRed)
{
int nByte = 0;
int hFile;
if ( (nWidth < 0) || (nHeight < 0) )
{
//write log
return EWBMPAPIERR_SIZE;
}
//calculate total bmp data byte num
if ((nWidth * nHeight)%8 == 0)
nByte = nWidth * nHeight / 8;
else
nByte = 1 + (int)(nWidth * nHeight / 8);
hFile = _open(fPathBlack, _O_BINARY | _O_RDONLY);
if ( hFile == -1 )
{
//write log
return EWBMPAPIERR_FILEOPENB;
}
else
{
if (_filelength(hFile) < nByte)
{
//write log
_close( hFile);
return EWBMPAPIERR_FILELENB ;
}
}
if (fPathRed)
{
hFile = _open(fPathRed, _O_BINARY | _O_RDONLY);
if ( hFile == -1 )
{
//write log
return EWBMPAPIERR_FILEOPENR;
}
else
{
if (_filelength(hFile) < nByte)
{
//write log
_close( hFile);
return EWBMPAPIERR_FILELENR;
}
}
}
return 0;
}
int EWBMP_CreaHead(UINT nWidth,UINT nHeight,LONG size)
{
memset(&bmpFileHeader, 0 ,sizeof(bmpFileHeader));
memset(&bmpInfoHeader,0, sizeof(bmpInfoHeader));
bmpFileHeader.bfType = 0x4D42;
bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(g_rgbQuad) + size;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = 0x0076;
bmpInfoHeader.biSize = 0x28;
bmpInfoHeader.biWidth = nWidth;
bmpInfoHeader.biHeight = nHeight;
bmpInfoHeader.biSizeImage = bmpFileHeader.bfOffBits + size;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biXPelsPerMeter = 0x0ec4;
bmpInfoHeader.biYPelsPerMeter = 0x0ec4;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biBitCount = 0;
bmpInfoHeader.biPlanes = 0x01 ;
bmpInfoHeader.biBitCount = 0x04 ;
return 0;
}
int EWBMP_DoBkDat(char* fullFilePath,int nWidth, int nHeight, int nByte)
{
BYTE DotByte;
BYTE* pCurrentByte;
long nDotCount = 0;
int x,y;
FILE* fDot;
fDot = fopen(fullFilePath,"r+b");
fread( pDot, sizeof(BYTE),nByte, fDot );
fclose(fDot);
for (int i=0;i<nByte;i++)
{
DotByte = (*(pDot + i));
for (int m=0;m<8;m++)
{
x = nDotCount/nHeight;
y = nHeight - nDotCount%nHeight -1;
pCurrentByte = pBMP + y * ByteNumPerLine + (x/2);
if (TestBit(DotByte,m))
{
if (x % 2 == 1)
(*pCurrentByte) = (*pCurrentByte) & 0xF0 | DEFBMPAPI_BLKCOLORLOW;
else
(*pCurrentByte) = (*pCurrentByte) & 0x0F | DEFBMPAPI_BLKCOLORHIGH;
}
nDotCount ++ ;
}
}
return 0;
}
int EWBMP_DoRkDat(char* fullFilePath,int nWidth, int nHeight, int nByte)
{
BYTE DotByte;
BYTE* pCurrentByte;
long nDotCount = 0;
int x,y;
FILE* fDot;
fDot = fopen(fullFilePath,"r+b");
fread( pDot, sizeof(BYTE),nByte, fDot );
fclose(fDot);
for (int i=0;i<nByte;i++)
{
DotByte = (*(pDot + i));
for (int m=0;m<8;m++)
{
x = nDotCount/nHeight;
y = nHeight - nDotCount%nHeight -1;
pCurrentByte = pBMP + y * ByteNumPerLine + (x/2);
if (TestBit(DotByte,m))
{
if (x % 2 == 1)
(*pCurrentByte) = (*pCurrentByte) & 0xF0 | DEFBMPAPI_REDCOLORLOW;
else
(*pCurrentByte) = (*pCurrentByte) & 0x0F | DEFBMPAPI_REDCOLORHIGH;
}
nDotCount ++ ;
}
}
return 0;
}
int EWBMP_WriteToFile(char* fullFilePath,long size)
{
FILE* file = NULL;
file = fopen(fullFilePath,"w+b");
//write log
if (!file)
return EWBMPAPIERR_WRITEOPENERR;
fwrite( &bmpFileHeader, sizeof( bmpFileHeader ), 1, file );
fwrite( &bmpInfoHeader, sizeof( bmpInfoHeader ), 1, file );
fwrite( &g_rgbQuad, sizeof( g_rgbQuad ), 1, file );
fseek(file, 0x76, SEEK_SET);
fwrite( pBMP, size, 1, file );
fclose(file);
return 0;
}
_EWBMPAPI BOOL WINAPI WINAPI EWBMP_Dat2Bmp(UINT nWidth0, UINT nHeight0, char* lpBkDat, char* lpRdDat, char* lpBmp )
{
long size = 0;
long nWidth = 0;
long nHeight = 0;
long nByte = 0;
int iRet = -1;
nWidth = nHeight0;
nHeight = nWidth0;
iRet = EWBMP_ChkDatFile(nWidth, nHeight, lpBkDat , lpRdDat);
if (iRet != 0) return FALSE;
//calculate byte num per line
ByteNumPerLine = ((nWidth*4+31)&~31)/8;
size = ByteNumPerLine * nHeight ;
//create bmp head
EWBMP_CreaHead(nWidth,nHeight,size);
pBMP = (BYTE*)malloc(size);
if (!pBMP)
{
//writelog
iRet = EWBMPAPIERR_MALLOC ;
return FALSE;
}
//calculate total bmp data byte num
if ((nWidth * nHeight)%8 == 0)
nByte = nWidth * nHeight / 8;
else
nByte = 1 + (int)(nWidth * nHeight / 8);
pDot = (BYTE*)malloc(nByte);
if (!pDot)
{
//writelog
iRet = EWBMPAPIERR_MALLOC ;
return FALSE;
}
memset(pBMP,0xFF,size);
memset(pDot,0x00,nByte);
EWBMP_DoBkDat(lpBkDat,nWidth, nHeight,nByte);
if (lpRdDat)
EWBMP_DoRkDat(lpRdDat,nWidth, nHeight,nByte);
iRet = EWBMP_WriteToFile(lpBmp,size);
if (iRet != 0)
{
free(pBMP);
free(pDot);
return FALSE;
}
free(pBMP);
free(pDot);
return TRUE;
}