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;
}
显示图片出错.