C言語によるモノクロビットマップの拡大
5077 ワード
一般的にネット上でC言語が単色ビットマップの増幅プログラムを実現することはめったになく、以下は主にCを通じて単色ビットマップの増幅を実現する.
#define FXZOOMRATIO 5.8 //x
#define FYZOOMRATIO 5.8 //y
#pragma pack(push, 2)
typedef struct
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
BYTE rgbBlue;// ( 0-255)
BYTE rgbGreen;// ( 0-255)
BYTE rgbRed;// ( 0-255)
BYTE rgbReserved;// , 0
}RGBQUAD;
#pragma pack(pop)
unsigned char *pBmpBuf; //
unsigned char *pNewBmpBuf;
int bmpWidth; //
int bmpHeight; //
int g_lineByte; //
RGBQUAD *pColorTable; //
int biBitCount; // ,
long newBmpWidth; //
long newBmpHeight; //
long newLineByte; //
/****************************************************************************
* :readBmp()
* :const char *bmpName bmp
* :0 1
* : , , , ,
*
***************************************************************************/
bool readBmp(const char* bmpName)
{
FILE *fp=fopen(bmpName,"rb");
if(fp==0)
{
printf("cannot open file");
return 0;
}
fseek(fp,sizeof(BITMAPFILEHEADER),0);
BITMAPINFOHEADER head;
fread(&head,sizeof(BITMAPINFOHEADER),1,fp);
/* */
bmpWidth = head.biWidth;
bmpHeight = head.biHeight;
biBitCount = head.biBitCount;
int iexwidth = bmpWidth;
if(bmpWidth % 8){
iexwidth += 8;
}
iexwidth /= 8;
g_lineByte = (iexwidth + 3) & ~0x03; // , 4
if(biBitCount == 1)
{
pColorTable =(RGBQUAD *)malloc(sizeof(RGBQUAD)*2);
fread(pColorTable,sizeof(RGBQUAD),2,fp); //
}
pBmpBuf = (unsigned char *)malloc(g_lineByte *bmpHeight); //
fread(pBmpBuf,1,g_lineByte *bmpHeight,fp);
fclose(fp);
return 1;
}
bool saveBmp(const char* bmpName,unsigned char *imgBuf,int width,int height,int biBitCount,RGBQUAD *pColorTable)
{
if(!imgBuf) //
return 0;
int colorTablesize = 0;
if(biBitCount == 1)
colorTablesize =8; // 4*2
int lineByte = newLineByte;
FILE *fp = fopen(bmpName,"wb");
if(fp == 0) return 0;
BITMAPFILEHEADER fileHead;
fileHead.bfType= 0x4d42;
fileHead.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte *height;
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
fileHead.bfOffBits = 54 +colorTablesize;
fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp);
BITMAPINFOHEADER head;
head.biBitCount = biBitCount;
head.biClrImportant = 0;
head.biClrUsed = 0;
head.biCompression = 0;
head.biHeight = height;
head.biPlanes =1;
head.biSize = 40;
head.biSizeImage = lineByte *height;
head.biWidth = width;
head.biXPelsPerMeter = 0;
head.biYPelsPerMeter = 0;
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp);
if(biBitCount == 1){
fwrite(pColorTable,sizeof(RGBQUAD),2,fp);
}
fwrite(imgBuf,height * lineByte,1,fp);
fclose(fp);
return 1;
}
/****************************************************************************
* : bmpzoom()
* : const char* szSrcBmp bmp
const char* szDstBmp bmp
* :0 1
* : FXZOOMRATIO FYZOOMRATIO
*
***************************************************************************/
bool bmpzoom(const char* szSrcBmp, const char* szDstBmp)
{
readBmp(szSrcBmp);
newBmpWidth = (long) (bmpWidth * FXZOOMRATIO); //
newBmpHeight = (long) (bmpHeight * FYZOOMRATIO);
int iexwidth = newBmpWidth;
if(bmpWidth % 8){
iexwidth += 8;
}
iexwidth /= 8;
newLineByte = (iexwidth + 3) & ~0x03;
pNewBmpBuf = (unsigned char*)malloc(newLineByte * newBmpHeight); //
memset(pNewBmpBuf,0,newLineByte * newBmpHeight); // 0
long i,j,k;
long i0,j0;
if(biBitCount==1){
for(i = 0;i < newBmpHeight;i++){
for(j = 0; j=0) && (j0 < bmpWidth) && ((i0 >=0)&& (i0 > ibit)){ // 1
*(pNewBmpBuf+i*newLineByte+inewbyte) |= (unsigned char)(0x80 >> inewbit); // 1
}
}
}
}
}
saveBmp(szDstBmp,pNewBmpBuf,newBmpWidth,newBmpHeight,biBitCount,pColorTable); //
if(pNewBmpBuf != NULL){
free(pNewBmpBuf);
pNewBmpBuf = NULL;
}
if(pBmpBuf != NULL){
free(pBmpBuf);
pBmpBuf = NULL;
}
if(biBitCount == 1){
if(pColorTable != NULL){
free(pColorTable);
pColorTable = NULL;
}
}
return 1;
}