现在想用c语言做图象处理,想把一幅800*600的灰度图读到二维数组中:
定义数组
char gray[800][600];
编译的时候报错:数组太大!!
这如何处理?
这是我写的变换的一个头文件myheader.h
#ifndef _MYHEADER_H
#define _MYHEADER_H
/*typedef unsigned char BYTE //8位无符号
//typedef unsigned int WORD //16位无符号
//typedef unsigned long int DWORD //32位无符号
//typedef long int LONG //32位有符号*/
typedef struct tagBITMAPFILEHEADER
{
unsigned int bfType; /* 位图文件的类型,必须为BM*/
unsigned long int bfSize; /*位图文件的大小,以字节为单位 */
unsigned int bfReserved1; /*位图文件保留字,必须为0*/
unsigned int bfReserved2; /*位图文件保留字,必须为0*/
unsigned long int bfOffBits; /* 位图数据的起始位置,以相对于位图文件头的偏移量表示,以字节为单位*/
}bitmapfileheader;
typedef struct tagBITMAPINFOHEADER{ /*BMP位图信息头数据用于说明位图的尺寸等信息*/
unsigned long int biSize; /*本结构所占用字节数*/
long int biWidth; /*位图的宽度,以像素为单位*/
long int biHeight; /*位图的高度,以像素为单位*/
unsigned int biPlanes; /*目标设备的级别,必须为1*/
unsigned int biBitCount;/* 每个像素所需的位数,必须是1(双色),*/
/*4(16色),8(256色)或24(真彩色)之一*/
unsigned long int biCompression; /* 位图压缩类型,必须是 0(不压缩),*/
/*1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一*/
unsigned long int biSizeImage; /* 位图的大小,以字节为单位*/
long int biXPelsPerMeter; /*位图水平分辨率,每米像素数*/
long int biYPelsPerMeter; /* 位图垂直分辨率,每米像素数*/
unsigned long int biClrUsed;/* 位图实际使用的颜色表中的颜色数*/
unsigned long int biClrImportant;/*位图显示过程中重要的颜色数*/
} bitmapinfoheader;
typedef struct tagRGBQUAD { /*颜色表*/
unsigned char rgbBlue;/* 蓝色的亮度(值范围为0-255)*/
unsigned char rgbGreen; /* 绿色的亮度(值范围为0-255)*/
unsigned char rgbRed; /* 红色的亮度(值范围为0-255)*/
unsigned char rgbReserved;/* 保留,必须为0*/
} rgbquad;
typedef struct byte_bit{/*define bit field for write one bit to a byte*/
unsigned a:1;
unsigned b:1;
unsigned c:1;
unsigned d:1;
unsigned e:1;
unsigned f:1;
unsigned g:1;
unsigned h:1;
}bytebit;
bitmapfileheader fileheader;
bitmapinfoheader infoheader;
bitmapfileheader wbpfileheader;
bitmapinfoheader wbpinfoheader;
static rgbquad palette[256];
static char palette2[6]={0x00,0x00,0x00,0xff,0xff,0xff};
int readbmpheader1(FILE *fp);
int readbmpheader8(FILE *fp);
int readbmpline(char *p,FILE *fp,int linebytes);
/*int readbmpfile(FILE *fp);*/
int writebmpheader(FILE *fp);
int writebmpline8(char *p,FILE *fp,int linebytes);
int writebmpline1(char *p,FILE *fp,int linebytes);
/*int writebmpfile(FILE *fp);*/
int readbmpheader(FILE *fp)
{
memset(fileheader,0,sizeof(bitmapfileheader));
memset(infoheader,0,sizeof(bitmapinfoheader));
if(fread((char *)&fileheader,1,sizeof(bitmapfileheader),fp)!=sizeof(bitmapfileheader))
{
printf("read fileheader error!");
return 1;
}
if(fread((char *)&infoheader,1,sizeof(bitmapinfoheader),fp)!=sizeof(bitmapinfoheader))
{
printf("read infoheader error!");
return 1;
}
if(infoheader.biCompression!=0)
{
printf("only supports non_compressed bmp file.\n");
return 1;
}
memset(palette,0,sizeof(rgbquad)*256);
if(infoheader.biBitCount==8)
{
fseek(fp,54,SEEK_SET);
if(fread((char *)&palette,256,sizeof(rgbquad),fp)!=sizeof(rgbquad)*256)
{
printf("read palette error!");
return 1;
}
}
}
int readbmpline(char *p,FILE *fp,int linebytes)
{
if(infoheader.biBitCount==8)
{
fseek(fp,1078,SEEK_SET); /*54+256*4*/
if(fread(p,1,linebytes,fp)!=linebytes)
{
printf("read linebytes error!");
return 1;
}
}
if(infoheader.biBitCount==24)
{
fseek(fp,54,SEEK_SET);
if(fread(p,1,linebytes,fp)!=linebytes)
{
printf("read linebytes error!");
return 1;
}
}
}
int writebmpheader8(FILE *fp)
{
int i;
rgbquad palette256[256];
memset(wbpfileheader,0,sizeof(bitmapfileheader));
memset(wbpinfoheader,0,sizeof(bitmapinfoheader));
wbpfileheader.bfType=0x4d42;
wbpfileheader.bfOffBits=0x0436;
wbpinfoheader.biSize=sizeof(bitmapinfoheader);
wbpinfoheader.biWidth=800;
wbpinfoheader.biHeight=600;
wbpinfoheader.biPlanes=1;
wbpinfoheader.biBitCount=8;
if(fwrite((char *)&wbpfileheader,sizeof(bitmapfileheader),1,fp)!=sizeof(bitmapfileheader))
{
printf("write fileheader error!");
return 1;
}
if(fwrite((char *)&wbpinfoheader,sizeof(bitmapinfoheader),1,fp)!=sizeof(bitmapinfoheader))
{
printf("write infoheader error!");
return 1;
}
for(i=0;i<256;i++)
{
palette256[i].rgbRed=(char)i;
palette256[i].rgbGreen=(char)i;
palette256[i].rgbBlue=(char)i;
palette256[i].rgbReserved=(char)0;
}
fwrite(palette256,sizeof(rgbquad),256,fp);
}
int writebmpheader1(FILE *fp)
{
int i;
memset(wbpfileheader,0,sizeof(bitmapfileheader));
memset(wbpinfoheader,0,sizeof(bitmapinfoheader));
wbpfileheader.bfType=0x4d42;
wbpfileheader.bfOffBits=0x3e;
wbpinfoheader.biSize=sizeof(bitmapinfoheader);
wbpinfoheader.biWidth=800;
wbpinfoheader.biHeight=600;
wbpinfoheader.biPlanes=1;
wbpinfoheader.biBitCount=1;
if(fwrite((char *)&wbpfileheader,sizeof(bitmapfileheader),1,fp)!=sizeof(bitmapfileheader))
{
printf("write fileheader error!");
return 1;
}
if(fwrite((char *)&wbpinfoheader,sizeof(bitmapinfoheader),1,fp)!=sizeof(bitmapinfoheader))
{
printf("write infoheader error!");
return 1;
}
if(fwrite(palette2,sizeof(rgbquad),2,fp)!=sizeof(rgbquad)*2)
{
printf("write palette error!");
return 1;
}
}
int writebmpline8(char *p,FILE *fp,int linebytes)
{
if(fwrite(p,linebytes,1,fp)!=linebytes)
{
printf("write line error!");
return 1;
}
}
int writebmpline1(char *p,FILE *fp,int linebytes)
{
if(fwrite(p,linebytes,1,fp)!=linebytes)
{
printf("write line error!");
return 1;
}
}
#endif