| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2885 人关注过本帖
标题:8位读BMP图片问题
取消只看楼主 加入收藏
pegliu
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-8-9
收藏
 问题点数:0 回复次数:3 
8位读BMP图片问题
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <fcntl.h>
#include <bios.h>
#include <dos.h>
#include <io.h>
#include <graphics.h>
int COLORS[16][3]=
{
        /* R   G   B        Index    ColorName        */
        {  0,  0,  0},     /* 00 Black                */
        {  0,  0,255},     /* 01 Blue                */
        {  0,128,  0},        /* 02 Green                */
        {  0,255,255},     /* 03 Cyan                */
        {255,  0,  0},     /* 04 Red                */
        {255,  0,255},     /* 05 Magenta            */
        {165, 42, 42},     /* 06 Brown                */
        {211,211,211},     /* 07 LightGray        */
        {169,169,169},     /* 08 DarkGray            */
        {173,216,230},     /* 09 LightBlue        */
        {144,238,144},     /* 10 LightGreen        */
        {144,238,238},        /* 11 LightCyan        */
        {238,144,144},     /* 12 LightRed            */
        {238,144,238},     /* 13 LightMegenta    */
        {255,255,  0},     /* 14 Yellow            */
        {255,255,255},        /* 15 White                */
};
typedef struct _tagBITMAPFILEHEADER{
  unsigned short WType;
  unsigned long  DSize;
  int WReserved1;
  unsigned short WReserved2;
  unsigned long  DOffBits;
}BITMAPFILEHEADER;

typedef struct _tagBITMAPINFOHEADER{
  unsigned long  DSize; //表示本结构的大小
  long  LWidth; //位图的宽度
  long  LHeight; //位图的高度
  unsigned short WPlanes; //永远为1 ,由于没有用过所以 没做研究 附msdn解释
                   //Specifies the number of planes for the target device. This value must be set to 1.
  unsigned short WBitCount;//位图的位数  分为1 4 8 16 24 32 本文没对1 4 进行研究
  unsigned long  DCompression; //本以为压缩类型,但是却另外有作用,稍候解释
  unsigned long  DSizeImage; //表示位图数据区域的大小以字节为单位
  long  LXPelsPerMeter;
  long  LYPelsPerMeter;
  unsigned long  DClrUsed;
  unsigned long  DClrImportant;
}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 _tagBITMAPINFO{
  BITMAPINFOHEADER  bmiHeader;
  RGBQUAD bmiColors;
}BITMAPINFO;

BITMAPFILEHEADER bmfh;
BITMAPINFO info;

int GetColor(unsigned int red,unsigned int green,unsigned int blue)
{
    int i,index=0;
    unsigned long dist,temp;
    temp=195075;
    for(i=0;i<15;i++)
    {
        dist=0;
        dist+=(COLORS[i][0]-red)*(COLORS[i][0]-red);
        dist+=(COLORS[i][1]-green)*(COLORS[i][1]-green);
        dist+=(COLORS[i][2]-blue)*(COLORS[i][2]-blue);
         /* minimum dist^2=2492, 623=[minimum dist^2]/4 */
        if(dist<=623)
            return i;
        if(dist<temp)
        {
            index=i;
            temp=dist;
        }
    }
    return index;
}
/* draw X and Y axes */
int main()
{
  int gd = DETECT;
  int gm = 0;
  int i;
  int j;
  int index;
  FILE *fp;
  long width;
  long height;
  int  pitch;
  RGBQUAD quad[256];
  unsigned char *buffer;
  unsigned char r,g,b;
  registerbgidriver(EGAVGA_driver);
  initgraph(&gd,&gm,"");
  cleardevice();
  fp = fopen("C:\\WINDOWS\\pix.bmp","r");
  if(fp == NULL)
  {
    printf("no file find!\n");
    return -1;
  }
  fread(&bmfh,sizeof(bmfh),1,fp);
  if(bmfh.WType!=0x4d42)
  {
    printf("this picture is not BMPfile!\n");
    return -1;
  }
  fread(&info.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
  width  = info.bmiHeader.LWidth;
  height = info.bmiHeader.LHeight;
  buffer = malloc(info.bmiHeader.DSizeImage);
  fseek(fp,bmfh.DOffBits,0);
  fread(buffer,info.bmiHeader.DSizeImage,1,fp);
  if(width%4 == 0)
  {
    pitch = width;
  }
  else
  {
    pitch = width+4-width%4;
  }
  //width = (width * bmfh.DOffBits + 31) / 32 * 4;
  fseek(fp,bmfh.DOffBits-sizeof(RGBQUAD)*256,0);
  fread(quad,sizeof(RGBQUAD)*256,1,fp);
  if(height>0)
  {
    for(i=0;i<height;i++)
    {
      for(j=0;j<width;j++)
      {
        index = buffer[i*pitch+j];
        r     = quad[index].rgbRed;
        g     = quad[index].rgbGreen;
        b     = quad[index].rgbBlue;
        putpixel(220+i,140+j,GetColor(r,g,b));//RGB(r,g,b));

      }
    }
  }
  else
    for(i=0;i<0-height;i++)
    {
       for(j=0;j<width;j++)
       {
          index = buffer[i*pitch+j];
          r     = quad[index].rgbRed;
          g     = quad[index].rgbGreen;
          b     = quad[index].rgbBlue;
          putpixel(220+i,140+j,GetColor(r,g,b));//RGB(r,g,b));
       }
    }

  getch();
  closegraph();
  return 0;
}

显示图片出错.
搜索更多相关主题的帖子: include BMP Green Cyan math 
2008-08-09 13:42
pegliu
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-8-9
收藏
得分:0 
跪求大家给我一个可以显示256色的代码  用C写的.能在TC2.0下编译通过
2008-08-09 14:53
pegliu
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-8-9
收藏
得分:0 
我的邮箱是pegliu@
2008-08-09 19:41
pegliu
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-8-9
收藏
得分:0 
呵呵,正在学,有一部分是网上的,呵呵
2008-08-10 14:25
快速回复:8位读BMP图片问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013384 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved