c 語言實現24位bmp圖片載入,讀寫,放大縮小 .
發現好多人網上查詢c 語言版本的bmp影象讀取,儲存,放大,縮小程式,很難找到完整的。 現在將自己寫的貼出來 供大家學習參考交流。轉載請標明出處,尊重作者勞動成果。
/**********************************************************
作者:fankaipeng
時間:2010-04-27
檔名稱:zoombmp.cpp
描述:c 語言實現24位bmp圖片讀寫,放大縮小。
開發工具 microsoft visual c++
開發平臺 windows
***********************************************************/
#include <windows.h>
#include <stdio.h>
#define FXZOOMRATIO 1.5 //x軸放大倍數
#define FYZOOMRATIO 1.5 //y軸放大倍數
unsigned char *pBmpBuf; //讀入影象資料的指標
unsigned char *pNewBmpBuf;
int bmpWidth;//影象的寬
int bmpHeight;//影象的高
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 lineByte = (bmpWidth *biBitCount/8+3)/4*4;//計算影象每行畫素所佔的位元組數
if(biBitCount == 8)
{
pColorTable = new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
pBmpBuf = new unsigned char [lineByte *bmpHeight];
fread(pBmpBuf,1,lineByte *bmpHeight,fp);
fclose(fp);
return 1;
}
/****************************************************************************
*函式名稱: saveBmp()
*函式引數: const char *bmpName 寫入bmp格式檔案的路徑及名稱
unsigned char *imgBuf 待存檔的點陣圖資料
int width, 以畫素為單位待存檔的點陣圖寬
int height, 以畫素為單位待存檔的點陣圖高
int biBitCount, 每個畫素佔的位數
RGBQUAD *pColorTable 顏色表指標
*函式返回值:0為失敗 1為成功
*函式描述:給定寫入bmp檔案的名稱和路徑 要寫入影象的點陣圖資料,寬,高,寫進檔案中
*
***************************************************************************/
bool saveBmp(const char* bmpName,unsigned char *imgBuf,int width,int height,int biBitCount,RGBQUAD *pColorTable)
{
if(!imgBuf)//imgBuf 待存檔的點陣圖資料
return 0;
int colorTablesize = 0;
if(biBitCount == 8)
colorTablesize =1024;
int lineByte = (width * biBitCount/8+3)/4*4;
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 == 8)
fwrite(pColorTable,sizeof(RGBQUAD),256,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) +0.5);
newBmpHeight = (long) (bmpHeight * FYZOOMRATIO +0.5);
newLineByte = (newBmpWidth * biBitCount/8+3)/4*4;
pNewBmpBuf = new unsigned char [newLineByte * newBmpHeight];
//printf("width = %d, height = %d,biBitCount = %d/n",bmpWidth,bmpHeight,biBitCount);
//printf("newwidth = %d, newheight = %d,biBitCount = %d/n",newBmpWidth,newBmpHeight,biBitCount);
long i,j,k;
long i0,j0;
int lineByte =(bmpWidth*biBitCount/8+3)/4*4;
if(biBitCount==8)
{
for(i = 0;i < bmpHeight/2;i++)
{
for(j = 0;j<bmpWidth/2;j++)
*(pBmpBuf+i*lineByte+j) = 0;
}
}
if(biBitCount == 24)
{
for(i = 0;i < newBmpHeight;i++)
{
for(j = 0; j<newBmpWidth;j++)
for(k=0;k<3;k++)
{
i0 = (long)(i/FYZOOMRATIO+0.5);
j0 = (long)(j/FXZOOMRATIO+0.5);
if((j0 >=0) && (j0 < bmpWidth) && (i0 >=0)&& (i0 <bmpHeight))
{
*(pNewBmpBuf+i*newLineByte+j*3+k) = *(pBmpBuf+i0*lineByte+j0*3+k);
}
else
{
*(pNewBmpBuf+i*newLineByte+j*3+k)=255;
}
}
}
}
saveBmp(szDstBmp,pNewBmpBuf,newBmpWidth,newBmpHeight,biBitCount,pColorTable);
delete []pNewBmpBuf;
if(biBitCount == 8)
delete []pColorTable;
return 1;
}
/***********************************************************************
*函式名稱:main()
*引數 空
*返回值 空
*描述:影象變化的訪問
***************************************************************************/
void main()
{
char str1[80];
char str2[80];
printf("輸入bmp檔案路徑及名稱");
gets(str1);
printf("輸入bmp檔案儲存的路徑及名稱");
gets(str2);
bmpzoom(str1,str2);}