CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

有谁可以提供bmp图片格式的数据存放结构吗?

楼主hinberta(我)2002-11-29 21:03:49 在 C/C++ / C语言 提问

我自己已经知道它的头文件的储存了,但其真正的数据区的结构却并不知道;  
  我用一个二进制工具打开过几个bmp文件,发现每一个文件的表示行结束的bit数都不一样,  
  我是准备编一个象window自带的画图程序一样的东西  
  问题点数:100、回复次数:1Top

1 楼gggxin(于吉)回复于 2002-11-29 21:57:49 得分 100

我有一个.  
  getbmp.h  
   
  /*2002-11-28  
    *  
    */  
   
  #define   true   1  
  #define   false   0  
  /*位图文件头*/  
  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;  
  }  
  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);  
  }Top

相关问题

  • DIB 和 RGB 图像的区别 结构格式详解 两者在内存的存放区别
  • bmp格式help
  • 谁能介绍一下BMP文件的具体格式,是什么结构,每个结构的长度等
  • 谁能介绍一下BMP文件的具体格式,是什么结构,每个结构的长度等
  • 寻找:关于一般的图像格式(BMP,jpeg, gif... ) 的结构方面资料!!!
  • 寻找:关于一般图像格式(BMP, jpeg, gif ...) 的结构方面的资料
  • bmp 文件格式 问题???????
  • 关于bmp文件格式
  • 关于BMP图形格式
  • Parabox7不支持jpg格式的图片存放

关键词

  • 文件
  • 指针
  • 图像
  • 结构
  • 数据
  • structbmp
  • imgbmpinfhead
  • pallette
  • imgwidth
  • bmpbuffer

得分解答快速导航

  • 帖主:hinberta
  • gggxin

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo