将一幅1280*1024的图像进行读入和居中剪裁
读入 0.bmp 文件,生成 1.bmp 文件
#define X_Source 1280
#define Y_Source 1024
#define X_SIZE 640
#define Y_SIZE 512
#define Win_X 320
#define Win_Y 256
#include
int image_read(unsigned char bitHead[54],unsigned char color[1024],unsigned char *data,char *filename)
{
FILE *fp;
fp = fopen(filename,"rb");
if(fp == NULL) return -1;
else{
fread(bitHead,1,54,fp);
fread(color,1,1024,fp);
fread(data, (size_t)X_SIZE,(size_t)Y_SIZE,fp);
fclose(fp);
return 0;
}
}
void SaveBmp(char* filename,unsigned char *data,int x_size,int y_size)
{
int i;
unsigned char color[1024];
unsigned int size ;
unsigned short head[27];
FILE* fp;
for(i=0;i<1024;i++)
{
if(i%4==3)
color[i]=0xff;
else
color[i]=i/4;
}
color[396]=145,color[397]=44;color[398]=238;
size = x_size*y_size+54+1024;
head[0]=0x4D42;
head[1]=size%0x10000;
head[2]=size/0x10000;
head[3]=0;
head[4]=0;
head[5]=0x436;
head[6]=0;
head[7]=0x28;
head[8]=0;
head[9]=x_size%0x10000;
head[10]=x_size/0x10000;
head[11]=y_size%0x10000;
head[12]=y_size/0x10000;
head[13]=0x1;
head[14]=0x08;
head[15]=0;
head[16]=0;
head[17]=0;
head[18]=0;
head[19]=0;
head[20]=0;
head[21]=0;
head[22]=0;
head[23]=0x100;
head[24]=0;
head[25]=0x100;
head[26]=0;
fp=fopen(filename,"wb");
if(!fp)return;
fwrite(head,1,sizeof(head),fp);
fwrite(color,1,1024,fp);
fwrite(data,x_size,y_size,fp);
fclose(fp);
}
void imwindow(unsigned char pic[Y_Source][X_Source],unsigned char picinv[Y_SIZE][X_SIZE])
{
int i,j,a,b;
for(i=0;i
{
picinv[0][i]=0;
}
for(i=1;i
{
picinv[i][0]=0;
picinv[i][X_SIZE-1]=0;
}
for(i=1;i
for(j=1;j
{
a=i+Win_Y;
b=j+Win_X;
picinv[i][j]=pic[a][b];
}
/*for(i=1;i
for(j=1;j
{
picinv[i][j]=pic[X_SIZE-j-1][i];
}*/
}
void main()
{
unsigned char imagein[Y_Source][X_Source];
unsigned char imageout[Y_SIZE][X_SIZE];
unsigned char bitHead[54];
unsigned char color[1024];
image_read(bitHead,color,imagein,"0.bmp");
imwindow(imagein,imageout);
SaveBmp("1.bmp",imageout,X_SIZE,Y_SIZE);
}