首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

[HELP]诚心请热心人帮忙,先谢了,看看哪里不行!该如何处理

2012-03-09 
[HELP]诚心请热心人帮忙,先谢了,看看哪里不行!此程序是BMP文件头文件的读取,编译无错,但读取信息不对口!图

[HELP]诚心请热心人帮忙,先谢了,看看哪里不行!


此程序是BMP文件头文件的读取,编译无错,但读取信息不对口!
图片地址在此:http://p.blog.csdn.net/images/p_blog_csdn_net/jianxiong8814/291722/o_TEST24.BMP


#include   "stdio.h "


typedef   char   CHAR;
typedef   short   SHORT;
typedef   long   LONG;
typedef   unsigned   long               DWORD;//4个字节
typedef   int                     BOOL;   //4个字节
typedef   unsigned   char               BYTE;   //1个字节
typedef   unsigned   short             WORD;   //2个字节


WORD     Width,Height,BitCount,Planes,ImageType;
WORD     Compression,Orientation;
DWORD   OffBits,ColorMap,Size;
WORD     BytesPerLine,ColorNum;


//文件头结构体的定义,14字节
typedef   struct   tagBITMAPFILEHEADER   {
                WORD         bfType;//2字节
                DWORD       bfSize;//4
                WORD         bfReserved1;//2
                WORD         bfReserved2;//2
                DWORD       bfOffBits;//4
}   BITMAPFILEHEADER;


//位图信息头的定义,40字节
typedef   struct   tagBITMAPINFOHEADER{
                DWORD             biSize;//4
                LONG               biWidth;//4
                LONG               biHeight;//4
                WORD               biPlanes;//2
                WORD               biBitCount;//2
                DWORD             biCompression;                   //4
                DWORD             biSizeImage;//4
                LONG               biXPelsPerMeter;                   //4
                LONG               biYPelsPerMeter;//4
                DWORD             biClrUsed;//4
                DWORD             biClrImportant;//4
}   BITMAPINFOHEADER;



//读BMP文件头函数
int   loadBMPhead(char   *file)                                                 /*     读BMP文件头     */
{
    BITMAPFILEHEADER     bmphead;
    BITMAPINFOHEADER     bmi;


    FILE   *fp;
    int     flag=0;

   
    fp=fopen(file, "rb ");/*     打开图像文件     */
    if   (fp   !=   NULL)   {
        flag=1;
        fseek(fp,0L,SEEK_SET);

        fread(&bmphead,sizeof(BITMAPFILEHEADER),1,fp);/*     读位图文件头     */
        fread(&bmi,sizeof(BITMAPINFOHEADER),1,fp);         /*     读位图信息头     */

              /*     以下取出图像参数     */
        OffBits             =   bmphead.bfOffBits;
        Width                 =   (WORD)   bmi.biWidth;
        Height               =   (WORD)   bmi.biHeight;
        BitCount           =   bmi.biBitCount;
        Planes               =   bmi.biPlanes;
        Compression     =   (WORD)   bmi.biCompression;
        BytesPerLine   =   (Width*BitCount+31)/32*4;         /*     计算每行字节数     */
        ColorMap           =   sizeof(BITMAPFILEHEADER)   +   bmi.biSize;
        ColorNum           =   0;

        fclose(fp);                                                                       /*     关闭图像文件     */


    }
    return(flag);
}


main()
{
loadBMPhead( "C://TEST24.bmp ");
exit(1);

}

//图片地址在此:http://p.blog.csdn.net/images/p_blog_csdn_net/jianxiong8814/291722/o_TEST24.BMP
大家注意:下面是结构体里个各量的正确值,不知道为什么我老是得不到文件头和信息头的正确值

typedef   struct   tagBITMAPFILEHEADER   {
                WORD         bfType;// 'BM '或0X424D或16973
                DWORD       bfSize;//137144
                WORD         bfReserved1;//0
                WORD         bfReserved2;//0
                DWORD       bfOffBits;//54
}   BITMAPFILEHEADER;


typedef   struct   tagBITMAPINFOHEADER{
                DWORD             biSize;//40
                LONG               biWidth;//181
                LONG               biHeight;//252
                WORD               biPlanes;//1


                WORD               biBitCount;//24
                DWORD             biCompression;//0
                DWORD             biSizeImage;//0
                LONG               biXPelsPerMeter;//2834
                LONG               biYPelsPerMeter;//2834
                DWORD             biClrUsed;//0
                DWORD             biClrImportant;//0
}   BITMAPINFOHEADER;


//大家也可以用自己的图片调试,其实也不难知道对不对

请大家帮帮忙指出错误,谢谢了!

我确实不知道哪里错了,哎。。。。




[解决办法]
lz在编译时,结构体的对齐有没有设置成按1字节对齐?
[解决办法]
同意WizardLucien~~
在结构体前后分别加上
#pragma pack(push,1)

#pragma pack(pop)
或者只加
#pragma pack(1)
即可

[解决办法]
估计就这个问题吧
[解决办法]
http://www.5inet.net/Develop/Visual%20C/029645,ZaiVCZhongZiJianCaoZuoBMPLiTuWenJianDeLei.aspx
[解决办法]
恩, 应该是结构体对齐的问题 ~~

对齐单位不正确,
导致 sizeof 不正确,
读取的信息就出错了~
[解决办法]
结构体对齐
[解决办法]
一定要加吗??我也在写BMP图像的读取,没有加楼上说的那个对齐也可以正确显示哦!

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>

typedef unsigned short int WORD;
typedef unsigned long int DWORD;
typedef unsigned char BYTE ;

typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;

typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
DWORD biWidth;
DWORD biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
DWORD biXPelsPerMeter;
DWORD biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;

typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;

BITMAPFILEHEADER bmfHeader; /*BMP file header*/
BITMAPINFOHEADER bmiHeader; /*BMP info header*/

void main(void)
{

FILE *fp;
int Palette_Num;
int Width,Height, Width_4;
int i, j;

if ((fp=fopen( "d:\\1.bmp ", "rb "))==NULL){
printf( "can not open source file\n ");
exit(0);
}

fread(&bmfHeader,sizeof(bmfHeader),1,fp);
fread(&bmiHeader,sizeof(bmiHeader),1,fp);

printf( "Print File Header:\n ");


printf( "bfType=%x \n ",bmfHeader.bfType);
printf( "bfSize=%d \n ",bmfHeader.bfSize);
printf( "bfOffBits=%d \n ",bmfHeader.bfOffBits);

printf( "\n Printf Information Header:\n ");
printf( "biSize=%d \n ",bmiHeader.biSize);
printf( "biWidth=%d \n ",bmiHeader.biWidth);
printf( "biHeight=%d \n ",bmiHeader.biHeight);
printf( "biBitCount=%d \n ",bmiHeader.biBitCount);
printf( "biClrUsed=%d \n ",bmiHeader.biClrUsed);
getchar();
}


热点排行