征集错误

zxj911 2004-08-10 09:12:56
#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;
}
...全文
111 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanstar200 2004-08-10
  • 打赏
  • 举报
回复
_EWBMPAPI BOOL WINAPI WINAPI EWBMP_Dat2Bmp(UINT nWidth0, UINT nHeight0, char* lpBkDat, char* lpRdDat, char* lpBmp )
爲什麽要用2個WINAPI ??
我認爲一個都不必用
zxj911 2004-08-10
  • 打赏
  • 举报
回复
这是一段位图处理的程序。
我现在定义了以下的错误
//struct define area
#define EWBMPAPIERR_SIZE -1
#define EWBMPAPIERR_FILEOPENB -2
#define EWBMPAPIERR_FILELENB -3
#define EWBMPAPIERR_FILEOPENR -4
#define EWBMPAPIERR_FILELENR -5
#define EWBMPAPIERR_WRITEOPENERR -20
#define EWBMPAPIERR_MALLOC -21
请大家帮忙看看程序在那还有可能发生错误或异常。请指教

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧