编程论坛's Archiver

ba_wang_mao 发表于 2007-1-19 14:23

800X600X256色下24K图片为什么显示不出来?(16色图片显示正常)

<P><BR>我依照NEO的子程序,可是24K的BMP图片显示不出来,请指点?<BR><BR>#define SRGB332(r, g, b) ((((r) / 9) &lt;&lt; 5) + (((g) / 9)&lt;&lt;2) + (b) / 21)<BR><BR>void _Cdecl Show_BMP(char *File_Name, int x, int y)<BR>{<BR>    int i, j, width, color,len;<BR>    FILE *fp;<BR>    unsigned char *buffer;<BR>    RGB24 *RGB_buffer;<BR>    RGBQUAD *RGB;<BR>    BITMAPFILEHEADER *FileHead;<BR>    BITMAPINFOHEADER *InfoHead;</P>
<P>    if ((fp=fopen(File_Name,"rb")) == NULL)<BR>    {<BR>        printf("BMP File not exist ...");<BR>        return;<BR>    }<BR>    RGB = (RGBQUAD *)malloc(sizeof(RGBQUAD));<BR>    FileHead = (BITMAPFILEHEADER *)malloc(sizeof(BITMAPFILEHEADER));<BR>    InfoHead = (BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER));<BR>    fread(FileHead,sizeof(BITMAPFILEHEADER),1,fp);<BR>    if (FileHead-&gt;bfType!='BM')<BR>    {<BR>        printf("BMP File type Error ...");<BR>        return;<BR>    }<BR>    fread(InfoHead,sizeof(BITMAPINFOHEADER),1,fp);<BR>    if (InfoHead-&gt;biCompression !=0 || (InfoHead-&gt;biBitCount!=8 &amp;&amp; InfoHead-&gt;biBitCount!=24))<BR>    {<BR>        printf("BMP File not Support Compression type ...");<BR>        fclose(fp);<BR>        return;<BR>    }<BR>    width =((int)InfoHead-&gt;biWidth+3)/4*4;    // 每行字节数--4的整数倍<BR>    if ((buffer = (unsigned char *)malloc(width)) == NULL) // 申请缓冲区<BR>    {<BR>        fclose(fp);<BR>        return;<BR>    }<BR>    if ((int)InfoHead-&gt;biBitCount == 8)  // 色深=8<BR>    {<BR>        for (i = 0 ; i &lt; 256 ; i++)<BR>        {<BR>            fread(RGB,sizeof(RGBQUAD),1,fp);<BR>            outportb(0x3C8,i);<BR>            outportb(0x3C9,RGB-&gt;rgbRed&gt;&gt;2);<BR>            outportb(0x3C9,RGB-&gt;rgbGreen&gt;&gt;2);<BR>            outportb(0x3C9,RGB-&gt;rgbBlue&gt;&gt;2);<BR>        }<BR>        for (j = (int)InfoHead-&gt;biHeight-1 ; j &gt;= 0 ; j--)<BR>        {<BR>            fread(buffer,width,sizeof(unsigned char),fp);<BR>            for (i = 0; i &lt; width; i++)<BR>                PutPixel(i+x,j+y,buffer[i]);<BR>        }<BR>    }<BR>    else if ((int)InfoHead-&gt;biBitCount == 24)  // 色深=24<BR>    {<BR>        len=(int)InfoHead-&gt;biWidth*sizeof(RGB24)+(int)InfoHead-&gt;biWidth&amp;3;<BR>        if ((RGB_buffer = (RGB24 *)malloc(len)) == NULL)<BR>        {<BR>            fclose(fp);<BR>            return;<BR>        }<BR>        for (j = (int)InfoHead-&gt;biHeight-1 ; j &gt;= 0 ; j--)<BR>        {<BR>            fread(RGB_buffer,len,sizeof(char),fp); /*读取该行像素的所有点的颜色*/<BR>            for (i=0;i&lt;len;i++)<BR>            {<BR>                color = SRGB332(RGB_buffer[i].r,RGB_buffer[i].g,RGB_buffer[i].b);<BR>                PutPixel(i+x,j+y,color);<BR>            }<BR>        }</P>
<P>    }<BR>    free(FileHead);<BR>    free(InfoHead);<BR>    free(RGB);<BR>}<BR></P>

ba_wang_mao 发表于 2007-1-19 14:25

<P>/*第一部分为位图文件头BITMAPFILEHEADER,其定义如下:*/<BR>typedef struct tagBITMAPFILEHEADER<BR>{<BR>    unsigned int bfType;            /*指定文件类型,*.bmp文件的头两个字节都是"BM"*/<BR>    unsigned long bfSize;            /*指定文件大小,包括这14个字节*/<BR>    unsigned int Reserved1;            /*为保留字,不用考虑*/<BR>    unsigned int reserved2;            /*为保留字,不用考虑*/<BR>    unsigned long bfOffset;            /*为从文件头到实际的位图数据的偏移字节数,前三个部分的长度之和。*/<BR>}BITMAPFILEHEADER;</P>
<P>/*第二部分为位图信息头BITMAPINFOHEADER,这个结构的长度是固定的,为40个字节其定义如下:*/<BR>typedef struct tagBITMAPINFOHEADER<BR>{<BR>    unsigned long biSize;            /*指定这个结构的长度,为40*/<BR>    unsigned long biWidth;            /*指定图象的宽度,单位是象素*/<BR>    unsigned long biHeight;            /*指定图象的高度,单位是象素*/<BR>    unsigned int biPlanes;            /*必须是1,不用考虑*/<BR>    unsigned int biBitCount;        /*指定表示颜色时要用到的位数,常用的值为1(黑白二色图),4(16色图),8(256色),24(真彩色图)*/<BR>    unsigned long biCompression;    /*指定位图是否压缩,有效的值为BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS*/<BR>    unsigned long biSizeImage;        /*指定实际的位图数据占用的字节数*/<BR>    unsigned long biXpolsPerMeter;    /*指定目标设备的水平分辨率,单位是每米的象素个数。*/<BR>    unsigned long biYpelsPerMeter;    /*指定目标设备的垂直分辨率,单位同上。*/<BR>    unsigned long biClrUsed;        /*指定本图象实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次方。*/<BR>    unsigned long biClrImportant;    /*指定本图象中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。*/<BR>}BITMAPINFOHEADER;</P>
<P><BR>typedef struct tagRGBQUAD            /*用于读取调色板信息*/<BR>{<BR>    unsigned char rgbBlue;<BR>    unsigned char rgbGreen;<BR>    unsigned char rgbRed;<BR>    unsigned char Reserved;<BR>}RGBQUAD;</P>
<P>typedef struct tagRGB<BR>{<BR>    unsigned char r;<BR>    unsigned char g;<BR>    unsigned char b;<BR>}RGB24;</P>

ba_wang_mao 发表于 2007-1-22 10:14

难到无人知到24K BMP图象显示?

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.