哪位好心人看下c处理bmp图像
大家好,刚加入该论坛,最近学bmp图像的处理,遇到了麻烦。下面这个程序结果不对。主要完成功能是把384*512的图像下采样成192*256的图像,但是最终显示不出来。。。 哪位好心人帮忙看下
#include"stdio.h"
/*#include"alloc.h"*/
#include"dos.h"
/*#include"graphics.h"*/
#include"math.h"
typedef struct
{
int bftype; /* 类型标志,总是BM */
long bfsize; /* 文件大小 */
int bfreserved1;
int bfreserved2;
long bfOffbits; /* 位图点阵偏移量*/
}HEAD;
typedef struct
{
long bisize; /* 结构体字节总数 */
long biwidth; /* 图像宽度 */
long biheight; /*图像高度 */
int biplanes; /* 必须为1 */
int bibitCount; /* 每个像素所占二进制位数,可能是1,4,8或 24 */
long bicompress; /*压缩方式*/
long bisizeimage; /*像素点阵大小 */
long bixpelspermeter; /* 水平像素数*/
long biypelspermeter; /* 垂直像素数 */
long biClrUsed; /*使用的颜色数 */
long biClrImportant; /*重要颜色数 */
}INFO;
typedef struct
{
unsigned char rgbblue; /*蓝色所占比重*/
unsigned char rgbgreen; /*绿色所占比重*/
unsigned char rgbred; /*红色所占比重*/
unsigned char rgbreserved; /*保留字节 */
}RGBQUAD;
/*int Match_f(int nRed,int nGreen,int nBlue); /*寻找与像素匹配的调色板的序号*/
downsamp(FILE *fpi, FILE *fpo); /* 下采样处理*/
Initial_f(int nchoice); /*初始化图像文件*/
RGBQUAD strapla[256]; /*256色调色板*/
HEAD strhead;
INFO strinfo;
unsigned int nwidth=384,ndepth=512; /*处理384×512位图*/
downsamp(FILE *fpi,FILE *fpo) /*下采样处理*/
{ RGBQUAD strapla[256]; /*256色调色板*/
HEAD strhead;
INFO strinfo;
int nwidth=384,ndepth=512; /*处理384×512位图*/
int ncounti,ncountj,k,l,m,k1,l1,m1,k2,l2,m2,countk;
unsigned char pixel[384][512];
unsigned char samppixel[192][256];
fread((char *)&strhead,1,sizeof(strhead),fpi);
fread((char *)&strinfo,1,sizeof(strinfo),fpi);
for(ncounti=0;ncounti<256;ncounti++)
fread((char *)&strapla[ncounti],1,sizeof(RGBQUAD),fpi);
strhead.bfsize=(ndepth/2)*(nwidth/2)+14+40+256;
fwrite((char *)&strhead,1,sizeof(strhead),fpo);
strinfo.biwidth=256;
strinfo.biheight=192;
fwrite((char*)&strinfo,1,sizeof(strinfo),fpo);
for(ncounti=0;ncounti<256;ncounti++)
fwrite((char *)&strapla[ncounti],1,sizeof(RGBQUAD),fpo);
/*for(ncounti=1;ncounti<=ndepth;ncounti++)
for(ncountj=1;ncountj<=nwidth;ncountj++)*/
fread(&pixel,1,sizeof(pixel),fpi);
for(ncounti=1;ncounti<=(2/ndepth);ncounti++)
for(ncountj=1;ncountj<=(2/nwidth);ncountj++)
{
samppixel[ncounti][ncountj]=0;
k=pixel[2*ncounti-1][2*ncountj-1];
l=pixel[2*ncounti-1][2*ncountj];
m=pixel[2*ncounti-1][2*ncountj+1];
k1=pixel[2*ncounti][2*ncountj-1];
l1=pixel[2*ncounti][2*ncounti];
m1=pixel[2*ncounti][2*ncountj+1];
k2=pixel[2*ncounti+1][2*ncountj-1];
l2=pixel[2*ncounti+1][2*ncountj];
m2=pixel[2*ncounti+1][2*ncountj+1];
countk=0;
if (k<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+k;
countk+=1;
}
if (l<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+l;
countk+=1;
}
if (m<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+m;
countk+=1;
}
if (k1<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+k1;
countk+=1;
}
if (l1<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+l1;
countk+=1;
}
if (m1<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+m1;
countk+=1;
}
if (k2<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+k2;
countk+=1;
}
if (l2<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+l2;
countk+=1;
}
if (m2<255)
{ samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]+m2;
countk+=1;
}
samppixel[ncounti][ncountj]=samppixel[ncounti][ncountj]/countk;
}
/*for(ncounti=1;ncounti<=(2/ndepth);ncounti++)
for(ncountj=1;ncountj<=(2/nwidth);ncountj++)*/
fwrite(&samppixel,1,sizeof(samppixel),fpo);
}
/*初始化*/
Initial_f(int nChoice)
{
char szFilena1[30],szFilena2[30];
FILE *fpi, *fpo;
printf("Input the bmp source file: ");
scanf("%s",szFilena1);
printf("Input the bmp termini file: ");
scanf("%s", szFilena2);
if((fpi=fopen(szFilena1,"rb"))==NULL)
{
printf("OPEN SOURCE FILE ERROR\n");
/*exit(0);*/
}
if((fpo=fopen(szFilena2,"wb"))==NULL)
{
printf("OPEN TERMINI FILE ERROR\n");
/*exit(0);*/
}
if(nChoice==1)
downsamp(fpi,fpo);
fclose(fpi);
fclose(fpo);
}
void main()
{
int nChoice;
do
{
printf("\n\t1. downsamp\n");
printf("\n\t2. Exit\n");
scanf("%d",&nChoice);
switch(nChoice)
{
case 1:
Initial_f(nChoice);
break;
case 2:
break;
default:
printf("Input again!\n");
break;
}
}while(nChoice!=2);
}