数字图像处理的框架结构(读文件、处理、保存)

#include "stdio.h"
#include "Windows.h"
//给定一个图像文件及其路径,读入图像数据。 
unsigned char *readBmp(char *bmpName, int *width, int *height, int *byteCount)
{
	//打开文件,
  FILE *fp=fopen(bmpName,"rb");
  if(fp==0) return 0;
  //跳过文件头
  fseek(fp, sizeof(BITMAPFILEHEADER),0);

  //读入信息头
  int w, h, b;
  BITMAPINFOHEADER head;
  fread(&head, sizeof(BITMAPINFOHEADER), 1,fp); 
  w = head.biWidth;
  h = head.biHeight;
  b = head.biBitCount/8;
  int lineByte=(w * b+3)/4*4; //每行的字节数为4的倍数

  //跳过颜色表 (颜色表的大小为1024)(彩色图像并没有颜色表,不需要这一步)
  if(b==1)
    fseek(fp, 1024,1);

  //图像数据
  unsigned char *imgBuf=new unsigned char[w * h * b];
  for(int i=0;i<h;i++)
  {
    fread(imgBuf+i*w*b,w*b, 1,fp);
    fseek(fp, lineByte-w*b, 1);
  }
  fclose(fp);

  *width=w,  *height=h, *byteCount=b;

  return imgBuf;
}


bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int byteCount)
{
  if(!imgBuf)
    return 0;

  //灰度图像颜色表空间1024,彩色图像没有颜色表
  int palettesize=0;
  if(byteCount==1) palettesize=1024;

  //一行象素字节数为4的倍数
  int lineByte=(width * byteCount+3)/4*4;

  FILE *fp=fopen(bmpName,"wb");
  if(fp==0) return 0;

  //填写文件头
  BITMAPFILEHEADER fileHead;
  fileHead.bfType = 0x4D42;
  fileHead.bfSize= 
    sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ palettesize + lineByte*height;
  fileHead.bfReserved1 = 0;
  fileHead.bfReserved2 = 0;
  fileHead.bfOffBits=54+palettesize;
  fwrite(&fileHead, sizeof(BITMAPFILEHEADER),1, fp);
 
  // 填写信息头
  BITMAPINFOHEADER head; 
  head.biBitCount=byteCount*8;
  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(palettesize==1024)
  {
    unsigned char palette[1024];
    for(int i=0;i<256;i++)
    {
      *(palette+i*4+0)=i;
      *(palette+i*4+1)=i;
      *(palette+i*4+2)=i;
      *(palette+i*4+3)=0;     
    }
    fwrite(palette, 1024,1, fp);
  }
  
  //准备数据并写文件
  unsigned char *buf=new unsigned char[height*lineByte];
  for(int i=0;i<height;i++)
  {
    for(int j=0;j<width*byteCount; j++)
      *(buf+i*lineByte+j)=*(imgBuf+i*width*byteCount+j);
  }
  fwrite(buf, height*lineByte, 1, fp);
    
  delete []buf;

  fclose(fp);

 return 1;
}

void main()
{
	unsigned char *imgbuf;
	int width, height, byteCount;
	char readPath[]="C:\\Users\\a404\\Desktop\\狗.bmp";
	imgbuf=readBmp(readPath, &width, &height, &byteCount);
	printf("宽=%d,高=%d,字节=%d\n",width, height, byteCount);

	for(int i=0;i<height/2;i++)
	{
    	for(int j=0;j<width/2;j++)
		{
			for(int k=0;k<byteCount;k++)
				// *(imgbuf+i*width*byteCount+j*byteCount+k)=0;
			*(imgbuf+i*width+j)=0;
	     //	*(imgbuf+i*width*3+j*3+0)=0;
		//	*(imgbuf+i*width*3+j*3+1)=0;
	    //	*(imgbuf+i*width*3+j*3+2)=0;
		}
	}

	char writePath[]="C:\\Users\\a404\\Desktop\\狗1.bmp";
	saveBmp(writePath, imgbuf, width, height, byteCount);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值