64,632
社区成员
发帖
与我相关
我的任务
分享
#include <stdafx.h>
#include <Windows.h>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//---------------------------------------------------------------------------------------
//以下该模块是完成BMP图像(彩色图像是24bit RGB各8bit)的像素获取,并存在文件名为xiang_su_zhi.txt中
unsigned char *pBmpBuf;//读入图像数据的指针
unsigned char *pResult;
int bmpWidth;//图像的宽
int bmpHeight;//图像的高
int main()
{
FILE *fp=fopen("D:\\Raw.BMP","r+b");//二进制读方式打开指定的图像文件
if(fp==0)
return 0;
//跳过位图文件头结构BITMAPFILEHEADER
fseek(fp, sizeof(BITMAPFILEHEADER),0);
//定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
BITMAPINFOHEADER head;
fread(&head, sizeof(BITMAPINFOHEADER),1,fp); //获取图像宽、高、每像素所占位数等信息
bmpWidth = head.biWidth;
bmpHeight = head.biHeight;
//申请位图数据所需要的空间,读位图数据进内存
pBmpBuf=new unsigned char[(bmpWidth + 2) * (bmpHeight+2)];
pResult = new unsigned char[(bmpWidth+2)*(bmpHeight+2)];
fseek(fp,0 - (bmpWidth+2) *(bmpHeight+2),SEEK_END);
fread(pBmpBuf,1,(bmpWidth+2) *(bmpHeight+2),fp);
for(int i = 2; i < bmpWidth - 2; ++i)
{
for(int j = 2; j < bmpHeight - 2; ++j)
{
pBmpBuf[i * bmpWidth + j] += pBmpBuf[(i - 2) * bmpWidth + j] + pBmpBuf[(i + 2) * bmpWidth + j]
+ pBmpBuf[i * bmpWidth + j - 2] + pBmpBuf[i * bmpWidth + j + 2];
pResult[i*bmpWidth+j] = pBmpBuf[i * bmpWidth + j];
}
}
/* //另一种方式处理
int nonce[5] = {0};
for(int i=4;i<bmpHeight-4;i++)
{
for(int j=4;j<bmpWidth-4;j++)
{
nonce[0] = pBmpBuf[i*bmpWidth+j];
nonce[1] = pBmpBuf[i*bmpWidth+j + 2]+pBmpBuf[i*bmpWidth+j + 4];
nonce[2] = pBmpBuf[i*bmpWidth+j - 2]+pBmpBuf[i*bmpWidth+j + 4];
nonce[3] = pBmpBuf[(i + 2)*bmpWidth+j]+pBmpBuf[(i + 4)*bmpWidth+j];
nonce[4] = pBmpBuf[(i - 2)*bmpWidth+j]+pBmpBuf[(i + 4)*bmpWidth+j];
int nValue;
nValue = (int)nonce[0] + (int)nonce[1] + (int)nonce[2] + (int)nonce[3] + (int)nonce[4];
if (nValue >=255)
{
nValue = 255;
}
pResult[i*bmpWidth+j] = (unsigned char)nValue;
}
}*/
fseek(fp,0 - bmpWidth *bmpHeight,SEEK_END);
fwrite(pResult,1,bmpWidth *bmpHeight,fp);
fclose(fp);
system("pause");
return 0;
}