现在想用c语言做图象处理,想把一幅800*600的灰度图读到二维数组中:
定义数组
char gray[800][600];
编译的时候报错:数组太大!!
这如何处理?
现在想用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
这是主函数:编译通过,只是运行的时候说不能读bitmapfileheader数据结构!
版主帮我看看怎么回事
#include "stdio.h"
#include "D:\Image\c_alg\myheader.h"
#include <malloc.h>
main()
{
FILE *rfp,*wfp;
int linebytes,i,j;
int width,height,count;
char *p,*p1;
char gray;
char red,green,blue;
if((rfp=fopen("D:\Image\c_alg\car.bmp","rb"))==NULL)
{
printf("can not open file car.bmp!");
return 1;
}
if((wfp=fopen("D:\Image\c_alg\car_gray.bmp","wb"))==NULL)
{
printf("can not open file car_gray.bmp!");
return 1;
}
readbmpheader(rfp);
width=infoheader.biWidth;
height=infoheader.biHeight;
count=infoheader.biBitCount;
if((width*count)%32==0)
linebytes=(width*count)/8;
else
linebytes=(width*count)/8+4;
fseek(wfp,1078,SEEK_SET);
if(((p=malloc(linebytes))!=NULL)&&((p1=malloc(linebytes/3))!=NULL))
{
memset(p,0,sizeof(linebytes));
memset(p1,0,sizeof(linebytes/3));
for(i=0;i<height;i++)
{
readbmpline(p,rfp,linebytes); /*read one line from file*/
for(j=0;j<width;j++)
{
blue=*p;
green=*(p+1);
red=*(p+2);
gray=(unsigned char)(red*30+green*59+blue*11)/100; /*get the gray value*/
p1=&gray;
p1++;
}
fwrite(p1,linebytes/3,1,wfp);
}
free(p);
free(p1);
}
fclose(rfp);
fclose(wfp);
}
我不懂XMS,EMS是什么意思,能告诉我怎么用吗?
最好能举个例子~~
谢谢