/*2002-11-28
*
*/
/*位图文件头*/
typedef struct
{
int imgType;/*位图的标志,即ASCII码为BM。两字节*/
long imgHeadSize;/*位图的文件的总字节数 4字节*/
long imgReserved;/*保留字4字节00 00 00 00*/
long imgBeginBit;/*位图阵列的起始位置,一般是54字节开始为位图阵列*/
}IMGHEAD;
/*位图信息头
bfSize:位图文件的大小。等于位图文件头+信息头+颜色表+位数据。以字节为单位即:sizeof(bmfh)+sizeof(bmih)+sizeof(RGBQUAD)*256+bmih.biSizeImage
*/
typedef struct
{
long imgHeadInfSize;/*位图信息头的长度,一般位图信息头占用40个字节。*/
long imgWidth;/*位图宽*/
long imgHeight;
int imgPlance;/*位图设备级别 01 00*/
int imgBitcount;/*位图级别00 18H=24即24位真彩色。*/
long imgCompression;/*压缩类型 为0代理不压缩。*/
long imgSizeimage;/*位图大小*/
long imgXpels;/*水平分辨率。*/
long imgYpels;/*垂真分辨率*/
long imgClrused;/*位图实际使用的颜色表中的颜色变址数*/
long imgClrimportant;/*位图显示过程中被认为重要颜色变址数*/
}IMGINFHEAD;
typedef struct RGBCOLOR
{
unsigned char R;
unsigned char G;
unsigned char B;
unsigned char Z;
}RGBColor;
typedef struct
{
IMGHEAD IMGBmpHead;
IMGINFHEAD IMGBMPINFHEAD;
RGBColor *pallette;
unsigned char *BmpBuffer;
}IMGBmp;
int GetBmp(char *,IMGBmp *);
void ShowBmp(IMGBmp * ,int ,int ,int ,int ,int ,int );
void SetAllPal();
/* 提取图片里的某一块
* >参数说明<
* pic_x,pi_y
* 所要提取图块所在的位置,
* pic_x表示往右第几块,pic_y表示往下第几块
* width,height
* 所要提取图块的宽高
*
*/
void ShowBmp(IMGBmp * structBMP,int x,int y,int pic_x,int pic_y,int width,int height)
{
unsigned char *p;
int i,ii,old_x;/*每行多出的00字符*/
p=structBMP->BmpBuffer+(structBMP->IMGBMPINFHEAD).imgHeight*(structBMP->IMGBMPINFHEAD).imgWidth;/*让p指向图像数据区尾部*/
p-=(structBMP->IMGBMPINFHEAD).imgWidth;/*指针退一行*/
if(pic_y>0)
{
p=p-pic_y*(structBMP->IMGBMPINFHEAD).imgWidth*height; //移到y点
}
old_x=x;
/* 设置调色板 */
for(i=1;i<(1<<(structBMP->IMGBMPINFHEAD).imgBitcount);i++)
{
setpal(i,(structBMP->pallette+i)->B>>2,(structBMP->pallette+i)->G>>2,(structBMP->pallette+i)->R>>2);
}
for(i=0;i<height;i++)
{
for(ii=pic_x*width;ii<pic_x*width+width;ii++,x++)
{
if(*(p+ii)!=0)
PutPixel(x,y,*(p+ii));
}
y++;
x=old_x;
p-=(structBMP->IMGBMPINFHEAD).imgWidth;/*指针退一行*/
}
}
/*
*
* 传递文件名和指向图像数据区的指针
* structBMP 传递过来的是结构的地址
*/
int GetBmp(char *fileName,IMGBmp * structBMP)
{
int i;
long imgWidthPixel;/*图像每行的字节数*/
FILE *img_fp;
if((img_fp=fopen(fileName,"rb"))==NULL)/*test256*/
{
printf("/nCan't open The file for %s ! /n",fileName);
printf("Press any key to halt!");
return(FALSE);
}
fread(&(structBMP->IMGBmpHead),sizeof(structBMP->IMGBmpHead),1, img_fp); /*读位图文件头*/
fread(&(structBMP->IMGBMPINFHEAD),sizeof(structBMP->IMGBMPINFHEAD),1, img_fp); /*读位图信息头*/
if((structBMP->IMGBMPINFHEAD).imgBitcount!=8)
{
printf("/nSorry this soft only support 256 color! /n");
printf("Press any key to halt!");
fclose(img_fp);
return(FALSE);
}
if((structBMP->IMGBMPINFHEAD).imgCompression)
{
printf("/nSorry this soft only support noCompression BMP! /n");
printf("Press any key to halt!");
fclose(img_fp);
return(FALSE);
}
/*读取调色板信息 分配 3*256 2的8次等于256*/
if((structBMP->pallette=(RGBColor *)malloc(sizeof(RGBColor)*(1<<(structBMP->IMGBMPINFHEAD).imgBitcount)))==NULL)/*分配调色板信息的大小*/
{
printf("/nmalloc pat failt! /n");
printf("Press any key to halt!");
fclose(img_fp);
free(structBMP->pallette);
return FALSE;
}
fread(structBMP->pallette,sizeof(RGBColor),1<<(structBMP->IMGBMPINFHEAD).imgBitcount,img_fp);/*读256次,每次3个字节,这样就读出了所用的颜色表了*/
/* 设置调色板 */
/*
for(i=1;i<(1<<(structBMP->IMGBMPINFHEAD).imgBitcount);i++)
{
setpal(i,(structBMP->pallette+i)->B>>2,(structBMP->pallette+i)->G>>2,(structBMP->pallette+i)->R>>2);
}
*/
fseek(img_fp,(structBMP->IMGBmpHead).imgBeginBit,SEEK_SET);/*移到数据区*/
i=0;
imgWidthPixel=(structBMP->IMGBMPINFHEAD).imgSizeimage/(structBMP->IMGBMPINFHEAD).imgHeight;/*得到图像每行的字节数*/
/*structBMP->BmpBuffer指向新分配的内存*/
if((structBMP->BmpBuffer=(char *)malloc((structBMP->IMGBMPINFHEAD).imgHeight*(structBMP->IMGBMPINFHEAD).imgWidth))==NULL)/*分配图像的大小*/
{
printf("/nmalloc images failt! %ld /n",((structBMP->IMGBMPINFHEAD).imgHeight*(structBMP->IMGBMPINFHEAD).imgWidth));
printf("Press any key to halt!");
fclose(img_fp);
free(structBMP->BmpBuffer);
return FALSE;
}
while(!feof(img_fp))
{
fread(structBMP->BmpBuffer+i,(structBMP->IMGBMPINFHEAD).imgWidth,1, img_fp); /*读取每一行,把数据塞到新分配的内存*/
if(!feof(img_fp))
{
fseek(img_fp,imgWidthPixel-(structBMP->IMGBMPINFHEAD).imgWidth,SEEK_CUR);/*跳过每行结束的00字符*/
i+=(structBMP->IMGBMPINFHEAD).imgWidth;/*i 指向了下一行*/
}
}
/* fseek(fil_ptr,bmfh.bfOffBits,SEEK_SET);*/
fclose(img_fp);
/* p=structBMP->BmpBuffer+(structBMP->IMGBMPINFHEAD).imgHeight*(structBMP->IMGBMPINFHEAD).imgWidth;/*让p指向图像数据区尾部*/
/* p-=(structBMP->IMGBMPINFHEAD).imgWidth;/*指针退一行*/
/* for(i=0;i<(structBMP->IMGBMPINFHEAD).imgHeight;i++)
{
for(ii=0;ii<(structBMP->IMGBMPINFHEAD).imgWidth;ii++)
{
PutPixel(ii,i,*(p+ii));
}
p-=(structBMP->IMGBMPINFHEAD).imgWidth;/*指针退一行*/
/* }
getch();*/
return(TRUE);
}
/* 设置系统所有调色板 */
void SetAllPal()
{
FILE *act;
int i,r,g,b;
act=fopen("comm.act","rb");
for (i=0;i<256;i++)
{
r=fgetc(act)>>2;
g=fgetc(act)>>2;
b=fgetc(act)>>2;
if(i==0)continue;/*去除背景色*/
setpal(i,r,g,b);
}
fclose(act);
}