注册 登录
编程论坛 C图形专区

turbo c for windows环境下24位真彩色图象灰度变换问题!

lyood 发布于 2006-12-14 13:53, 2013 次点击

现在想用c语言做图象处理,想把一幅800*600的灰度图读到二维数组中:
定义数组
char gray[800][600];
编译的时候报错:数组太大!!
这如何处理?

12 回复
#2
lyood2006-12-14 13:55

这是我写的变换的一个头文件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

#3
lyood2006-12-14 14:00

这是主函数:编译通过,只是运行的时候说不能读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);
}

#4
ba_wang_mao2006-12-14 14:00
用XMS(申请XMS 512K 内存空间),或者EMS(申请8页),
将图像先读到一个临时缓冲区,然后再将它传送到XMS或者EMS中。
#5
lyood2006-12-14 14:03
还有就是:二值bmp图象1bit表示一个像素,用c语言如何存取二值bmp图象?
#6
一笔苍穹2006-12-14 14:14
800*600要差不多500KB的空间,TC的MALLOC最大只能申请64K空间
二值位图和其它的一样嘛,要么一个int存16个像素,显示的时候解压,要行一个char一个,满占空间就是
#7
lyood2006-12-17 13:51

谢谢各位啊~~

但是定义为 char gray[800*600];又可以编译通过,它和gray[800][600]用的空间不一样大吗?

#8
lyood2006-12-17 13:54
以下是引用ba_wang_mao在2006-12-14 14:00:20的发言:
用XMS(申请XMS 512K 内存空间),或者EMS(申请8页),
将图像先读到一个临时缓冲区,然后再将它传送到XMS或者EMS中。

我不懂XMS,EMS是什么意思,能告诉我怎么用吗?
最好能举个例子~~
谢谢

#9
lyood2006-12-17 13:56

有人能帮我看看上面的代码能实现灰度变换?
到底那里有问题?

#10
一笔苍穹2006-12-18 10:10
以下是引用lyood在2006-12-17 13:51:51的发言:

谢谢各位啊~~

但是定义为 char gray[800*600];又可以编译通过,它和gray[800][600]用的空间不一样大吗?


是一样大,定义为char gray[800*600];不报错是个假象,因为TC的[ ]里只能接收一个无符号整数,800*600=480000,只取低十六位就是21248,而21248是个比较小的数字,编译不会报错。所以实际上你申请到的数组只是char gray[21248];,与480000相比差的远了。你可以将char gray[800*600];改成char gray[800L*600L];试试,立马报错。
在TC、TC++下不要试图用开辟一个超过64KB(即65535)字节的数组,你只能这样char gray[65535];或者这样int gray[32767];又或者long gray[16383];,一个子儿也不能多。
所以要申请大于64KB的缓冲,可以试试用farmalloc()函数。

#11
ba_wang_mao2006-12-18 11:20

typedef unsigned int XMS_HANDLE;
//-------------------------------------------------------------------
//XMS规范使用扩充内存所用的数据结构
//-------------------------------------------------------------------
typedef struct XMS
{
unsigned long length; // 需传输的数据字节数(32位)
XMS_HANDLE source_handle; // 源数据块句柄
unsigned long source_offset; // 源偏移量(32位)
XMS_HANDLE dest_handle; // 目标数据块句柄
unsigned long dest_offset; // 目标偏移量(32位)
};

int _Cdecl XMS_Test(void);
int _Cdecl XMS_Size(void);
int _Cdecl XMS_Alloc(int size);
int _Cdecl XMS_Free(XMS_HANDLE handle);
int _Cdecl XMS_Move(struct XMS *xms);
void _Cdecl Write_To_XMS(char *buf,unsigned int size,XMS_HANDLE handle,unsigned long offset);
void _Cdecl Read_From_XMS(char *buf,unsigned int size,XMS_HANDLE handle,unsigned long offset);
void _Cdecl ReadHzkXMS(char *Hzkname);
void _Cdecl HzkFree(void);

/////////////////////////////////////////////////////////////////////////////////////////////////
//检测XMS是否安装
/////////////////////////////////////////////////////////////////////////////////////////////////
int _Cdecl XMS_Test(void)
{
_AX = 0x4300;
__int__(0x2F);
if (_AL == 0x80)
{
_AX = 0x4310;
__int__(0x2F);
XMS_Function=(void far *)MK_FP(_ES,_BX);
return (True);
}
return (False);
}


/////////////////////////////////////////////////////////////////////////////////////////////////
//查询空闲的扩充内存空间(不包括HMA)
/////////////////////////////////////////////////////////////////////////////////////////////////
int _Cdecl XMS_Size(void)
{
_AH = 0x08;
XMS_Function();
return (_DX);
}


/////////////////////////////////////////////////////////////////////////////////////////////////
//分配扩充内存
/////////////////////////////////////////////////////////////////////////////////////////////////
int _Cdecl XMS_Alloc(int size)
{
_DX = size;
_AH = 0x09;
XMS_Function();
if(_AX == 1)
return (_DX);
return (False);
}


/////////////////////////////////////////////////////////////////////////////////////////////////
// 重新分配扩充内存
/////////////////////////////////////////////////////////////////////////////////////////////////
char XMS_ReAlloc(XMS_HANDLE handle,int resize)
{
_AH = 0x0f;
_DX = handle;
_BX = resize;
XMS_Function();
return _AX ? 0:_BL;
}


/////////////////////////////////////////////////////////////////////////////////////////////////
//释放指定句柄所分配的扩充内存
/////////////////////////////////////////////////////////////////////////////////////////////////
int _Cdecl XMS_Free(XMS_HANDLE handle)
{
_DX = handle;
_AH = 0x0A;
XMS_Function();
if(_AX)
return (True);
return (False);
}


/////////////////////////////////////////////////////////////////////////////////////////////////
//扩充内存和常规内存中的数据交换(移动扩充内存块)
/////////////////////////////////////////////////////////////////////////////////////////////////
int _Cdecl XMS_Move(struct XMS *xms)
{
_DS = FP_SEG(xms);
_SI = FP_OFF(xms);
_AH = 0x0B;
XMS_Function();
if (_AX)
return (True);
return (False);
}


/////////////////////////////////////////////////////////////////////////////////////////////////
//将汉字库点阵字模从常规内存缓冲区buf装载到扩充内存
/////////////////////////////////////////////////////////////////////////////////////////////////
void _Cdecl Write_To_XMS(char *buf,unsigned int size,XMS_HANDLE handle,unsigned long offset)
{
xms.length = size;
xms.source_handle = 0;
xms.source_offset = FP_SEG((void far *)buf);
xms.source_offset <<= 16;
xms.source_offset += FP_OFF((void far *)buf);
xms.dest_handle = handle;
xms.dest_offset = offset;
XMS_Move(&xms);
}


/////////////////////////////////////////////////////////////////////////////////////////////////
//从扩充内存中读取汉字点阵字模到常规内存缓冲区buf
/////////////////////////////////////////////////////////////////////////////////////////////////
void _Cdecl Read_From_XMS(char *buf,unsigned int size,XMS_HANDLE handle,unsigned long offset)
{
xms.length = size;
xms.source_handle = handle;
xms.source_offset = offset;
xms.dest_handle = 0;
xms.dest_offset = FP_SEG((void far *)buf);
xms.dest_offset <<= 16;
xms.dest_offset += FP_OFF((void far *)buf);
XMS_Move(&xms);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
//汉字库HZK16点阵装载到扩充内存
/////////////////////////////////////////////////////////////////////////////////////////////////
void _Cdecl ReadHzkXMS(char *Hzkname)
{
unsigned long hzksize;
unsigned int size;
unsigned long offset = 0;
char buf[32];
FILE *fp;

if (!XMS_Test())
{
puts("not found XMS");
exit(1);
}
if ((fp = fopen(Hzkname,"rb")) == NULL)
{
puts("Cannot Open HZK16 ...\n");
exit(1);
}
fseek(fp,0L,SEEK_END);
hzksize = ftell(fp);
fseek(fp,0L,SEEK_SET);
size = (int)(hzksize>>10) +1;
if (XMS_Size() < size)
{
puts("not enough XMS");
fclose(fp);
exit(-1);
}
if ((xms_handle = XMS_Alloc(size)) == 0)
{
puts("XMS handle Error...");
fclose(fp);
exit(-1);
}
while (!feof(fp))
{
size = fread(buf,sizeof(char),32,fp);
Write_To_XMS(buf,size,xms_handle,offset);
offset += size;
}
fclose(fp);
ROM_ASCII = Current_ASCII_ROM(S16X8);
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// 释放XMS内存
/////////////////////////////////////////////////////////////////////////////////////////////////
void _Cdecl HzkFree(void)
{
XMS_Free(xms_handle);
}

#12
ba_wang_mao2006-12-18 11:22
#define False 0
#define True 1
void far (*XMS_Function)(void) = 0L;// XMS服务程序入口地址
struct XMS xms;
XMS_HANDLE xms_handle; // 伪指针

你也可以参考董凯的NEO源代码,就在本站。
#13
lyood2006-12-20 21:20
高手!!
谢谢啊~~
学到很多知识!!
1