读取BitMap标题信息并打印

时间:2017-02-15 02:48:59

标签: c bitmap bit fread

我有2个c文件和1个头文件。我正在尝试读取任何bmp文件并打印出其信息。但是我每个字段变量都会得到非常大的数字,我不知道为什么。任何帮助将不胜感激。

BMPread.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "BMPreadfns.h"

int main( int argc, char *argv[] )
{
    //declare file read pointer
    FILE *file;

    //declare fileout read pointer
    //FILE *fileout; //declare file printed file pointer

    // open file 1 of argument counter and return 0 upon error
    if (!(file = fopen(argv[1], "rb")))return 0;

    short BMPid_mark;
    long BMPfile_len;
    short BMPreserved;
    long OffBits;
    long biSize;
    long biWidth;
    long biHeight;
    short biPlanes;
    short biBitCount;
    long biCompression;
    long biSizeImage;
    long biXPelsPerMeter;
    long biYPelsPerMeter;
    long biClrUsed;
    long biClrImportant;
    get_headervalue(&BMPid_mark,2,file);
    get_headervalue(&BMPfile_len,4,file);
    get_headervalue(&BMPreserved,2,file);
    get_headervalue(&OffBits,4,file);
    get_headervalue(&biSize,4,file);
    get_headervalue(&biWidth,4,file);
    get_headervalue(&biHeight,4,file);
    get_headervalue(&biPlanes,2,file);
    get_headervalue(&biBitCount,2,file);
    get_headervalue(&biCompression,4,file);
    get_headervalue(&biSizeImage,4,file);
    get_headervalue(&biXPelsPerMeter,4,file);
    get_headervalue(&biYPelsPerMeter,4,file);
    get_headervalue(&biClrUsed,4,file);
    get_headervalue(&biClrImportant,4,file);


    printf("\nType:%hd\n", BMPid_mark);
    printf("Size:%ld\n", BMPfile_len);
    printf("OffBits:%ld\n",OffBits);
    printf("biSize:%ld\n",biSize);
    printf("Width:%ld\n", biWidth);
    printf("Height:%ld\n", biHeight);
    printf("biPlanes:%hd\n", biPlanes);
    printf("biBitCount:%hd\n", biBitCount);
    printf("biCompression:%ld\n", biCompression);
    printf("biSizeImage:%ld\n", biSizeImage);
    printf("biXPelsPerMeter:%ld\n", biXPelsPerMeter);
    printf("biYPelsPerMeter:%ld\n", biYPelsPerMeter);
    printf("biClrUsed:%ld\n", biClrUsed);
    printf("biClrImportant:%ld\n\n", biClrImportant);


    fclose(file);

    return 0;
}

BMPreadfns.c

#include <stdio.h>
#include <stdlib.h>
#include "BMPreadfns.h"

int get_headervalue(int *i,int bitSize, FILE *fp)
{
    fread(i,bitSize,1,fp);
}

BMPreadfns.h

#ifndef BMPREAD_BMPREADFNS_H
#define BMPREAD_BMPREADFNS_H
int get_headervalue(int *i,int bitSize, FILE *fp);
#endif //BMPREAD_BMPREADFNS_H

0 个答案:

没有答案