#include<math.h>
#include <iomanip.h>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>ios
//---------------------------------------------------------------------------------------
//如下該模塊是完成BMP圖像(彩色圖像是24bit RGB各8bit)的像素獲取,並存在文件名爲xiang_su_zhi.txt中
unsigned char *pBmpBuf;//讀入圖像數據的指針windows
int bmpWidth;//圖像的寬
int bmpHeight;//圖像的高
RGBQUAD *pColorTable;//顏色表指針函數
int biBitCount;//圖像類型,每像素位數指針
//-------------------------------------------------------------------------------------------
//讀圖像的位圖數據、寬、高、顏色表及每像素位數等數據進內存,存放在相應的全局變量中
bool readBmp(char *bmpName)
{
FILE *fp=fopen(bmpName,"rb");//二進制讀方式打開指定的圖像文件圖片
if(fp==0)
return 0;ip
//跳過位圖文件頭結構BITMAPFILEHEADER內存
fseek(fp, sizeof(BITMAPFILEHEADER),0);圖片處理
//定義位圖信息頭結構變量,讀取位圖信息頭進內存,存放在變量head中it
BITMAPINFOHEADER head; io
fread(&head, sizeof(BITMAPINFOHEADER), 1,fp); //獲取圖像寬、高、每像素所佔位數等信息
bmpWidth = head.biWidth;
bmpHeight = head.biHeight;
biBitCount = head.biBitCount;//定義變量,計算圖像每行像素所佔的字節數(必須是4的倍數)
int lineByte=(bmpWidth * biBitCount/8+3)/4*4;//灰度圖像有顏色表,且顏色表表項爲256
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;//讀取文件成功
}
//-----------------------------------------------------------------------------------------
//給定一個圖像位圖數據、寬、高、顏色表指針及每像素所佔的位數等信息,將其寫到指定文件中
bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable)
{
//若是位圖數據指針爲0,則沒有數據傳入,函數返回
if(!imgBuf)
return 0;
//顏色表大小,以字節爲單位,灰度圖像顏色表爲1024字節,彩色圖像顏色表大小爲0
int colorTablesize=0;
if(biBitCount==8)
colorTablesize=1024;
//待存儲圖像數據每行字節數爲4的倍數
int lineByte=(width * biBitCount/8+3)/4*4;
//以二進制寫的方式打開文件
FILE *fp=fopen(bmpName,"wb");
if(fp==0)
return 0;
//申請位圖文件頭結構變量,填寫文件頭信息
BITMAPFILEHEADER fileHead;
fileHead.bfType = 0x4D42;//bmp類型
//bfSize是圖像文件4個組成部分之和
fileHead.bfSize= sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte*height;
fileHead.bfReserved1 = 0;
fileHead.bfReserved2 = 0;
//bfOffBits是圖像文件前3個部分所需空間之和
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;
}
//----------------------------------------------------------------------------------------
//如下爲像素的讀取函數
void doIt()
{
//讀入指定BMP文件進內存
char readPath[]="nv.BMP";
readBmp(readPath);
//輸出圖像的信息
cout<<"width="<<bmpWidth<<" height="<<bmpHeight<<" biBitCount="<<biBitCount<<endl;
//循環變量,圖像的座標
//每行字節數
int lineByte=(bmpWidth*biBitCount/8+3)/4*4;
//循環變量,針對彩色圖像,遍歷每像素的三個份量
int m=0,n=0,count_xiang_su=0;
//將圖像左下角1/4部分置成黑色
ofstream outfile("圖像像素.txt",ios::in|ios::trunc);
if(biBitCount==8) //對於灰度圖像
{
//------------------------------------------------------------------------------------
//如下完成圖像的分割成8*8小單元,並把像素值存儲到指定文本中。因爲BMP圖像的像素數據是從
//左下角:由左往右,由上往下逐行掃描的
int L1=0;
int hang=63;
int lie=0;
//int L2=0;
//int fen_ge=8;
for(int fen_ge_hang=0;fen_ge_hang<8;fen_ge_hang++)//64*64矩陣行循環
{
for(int fen_ge_lie=0;fen_ge_lie<8;fen_ge_lie++)//64*64列矩陣循環
{
//--------------------------------------------
for(L1=hang;L1>hang-8;L1--)//8*8矩陣行
{
for(int L2=lie;L2<lie+8;L2++)//8*8矩陣列
{
m=*(pBmpBuf+L1*lineByte+L2);
outfile<<m<<" ";
count_xiang_su++;
if(count_xiang_su%8==0)//每8*8矩陣讀入文本文件
{
outfile<<endl;
}
}
}
//---------------------------------------------
hang=63-fen_ge_hang*8;//64*64矩陣行變換
lie+=8;//64*64矩陣列變換
//該一行(64)由8個8*8矩陣的行組成
}
hang-=8;//64*64矩陣的列變換
lie=0;//64*64juzhen
}
}
//double xiang_su[2048];
//ofstream outfile("xiang_su_zhi.txt",ios::in|ios::trunc);
if(!outfile)
{
cout<<"open error!"<<endl;
exit(1);
}
else if(biBitCount==24)
{//彩色圖像
for(int i=0;i<bmpHeight;i++)
{
for(int j=0;j<bmpWidth;j++)
{
for(int k=0;k<3;k++)//每像素RGB三個份量分別置0才變成黑色
{
//*(pBmpBuf+i*lineByte+j*3+k)-=40;
m=*(pBmpBuf+i*lineByte+j*3+k);
outfile<<m<<" ";
count_xiang_su++;
if(count_xiang_su%8==0)
{
outfile<<endl;
}
//n++;
}
n++;
}
}
cout<<"總的像素個素爲:"<<n<<endl;
cout<<"----------------------------------------------------"<<endl;
}
//將圖像數據存盤
char writePath[]="nvcpy.BMP";//圖片處理後再存儲
saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable);
//清除緩衝區,pBmpBuf和pColorTable是全局變量,在文件讀入時申請的空間
delete []pBmpBuf;
if(biBitCount==8)
delete []pColorTable;
}
void main(){ doIt();}