【问题标题】:Trouble reading .bmp header file properly无法正确读取 .bmp 头文件
【发布时间】:2014-05-21 06:05:24
【问题描述】:

我正在尝试获取 .bmp 文件并最终逐个编辑像素,但我遇到了在 INFOHEADER 结构中返回给我的宽度和高度的问题。返回的宽度是 13107200,高度是 65536。但是,每当我运行程序时,总共只计算了 60003 个像素。我不知道为什么会这样。任何帮助将不胜感激。

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

int main( int argc, char *argv[] ){
    //define structures
        typedef struct 
        {   unsigned short int Type; /* Magic identifier */
            unsigned int Size; /* File size in bytes */
            unsigned short int Reserved1, Reserved2;
            unsigned int Offset; /* Offset to data (in B)*/
        }HEADER; /* -- 14 Bytes -- */

        typedef struct 
        {   unsigned int Size; /* Header size in bytes */
            int Width, Height; /* Width / Height of image */
            unsigned short int Planes; /* Number of colour planes */
            unsigned short int Bits; /* Bits per pixel */
            unsigned int Compression; /* Compression type */
            unsigned int ImageSize; /* Image size in bytes */
            int xResolution, yResolution;/* Pixels per meter */
            unsigned int Colors; /* Number of colors */
            unsigned int ImportantColors;/* Important colors */
        }INFOHEADER; /* -- 40 Bytes -- */

        typedef struct
        { unsigned char Red, Green, Blue;
        }PIXEL;

    //make instance of all three structures
    HEADER data;
    INFOHEADER data2;
    PIXEL pixel;

    //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 apon error
    if( !(file = fopen( "CU.bmp","rb")))return  0;  
    //read HEADER data into data
    fread(&data,sizeof(HEADER),1,file);
    //read IB+NFOHEADER data into data2
    fread(&data2,sizeof(INFOHEADER),1,file);
    //Print PIXEL data      

    //Allocate space for pixelarray
    PIXEL **pixelarray;
    int r=0,c=0,rows=data2.Height,collumns=data2.Width;
    pixelarray= malloc(rows*sizeof(PIXEL *));
    for(r=0; r<rows; r++){
        pixelarray[r]=malloc(collumns*sizeof(PIXEL));
    }

    //fill pixel array with pixel structs
    r=0;c=0;
    int pixelnum=1;
    while( fread(&pixel,sizeof(PIXEL),1,file) ){
        if(c == collumns){
            c=0;
            r++;
        }
        pixelarray[r][c] = pixel;
        printf("\nPixel %10d: %02X%02X%02X",pixelnum,pixelarray[r][c].Red,pixelarray[r][c].Blue,pixelarray[r][c].Green);
        fflush(stdout);
        c++;pixelnum++;

    }

    free(pixelarray);   

    fclose(file);  //close the files prior to exiting

【问题讨论】:

  • 您的类型(intshort)的大小不固定。如果您想要 32 位和 16 位类型,请分别使用 int32_tint16_t(或 uint32_tuint16_t)。

标签: c dynamic width pixel bmp


【解决方案1】:

我猜你的问题是structure alignment。你可以参考herehere。要消除它,请使用 #pragma 指令。所以你的结构声明应该是这样的:

#pragma pack(push)  // push current alignment to stack
#pragma pack(1)     // set alignment to 1 byte boundary
typedef struct
{  
    unsigned short int Type; /* Magic identifier */
    unsigned int Size; /* File size in bytes */
    unsigned short int Reserved1;
    unsigned short int Reserved2;
    unsigned int Offset; /* Offset to data (in B)*/
}HEADER; /* -- 14 Bytes -- */

typedef struct
{
    unsigned int Size; /* Header size in bytes */
    int Width;
    int Height; /* Width / Height of image */
    unsigned short int Planes; /* Number of colour planes */
    unsigned short int Bits; /* Bits per pixel */
    unsigned int Compression; /* Compression type */
    unsigned int ImageSize; /* Image size in bytes */
    int xResolution;
    int yResolution;/* Pixels per meter */
    unsigned int Colors; /* Number of colors */
    unsigned int ImportantColors;/* Important colors */
}INFOHEADER; /* -- 40 Bytes -- */

typedef struct
{
    unsigned char Red;
    unsigned char Green;
    unsigned char Blue;
}PIXEL;
#pragma pack(pop)   // restore original alignment from stack

这可以正确读取 BMP 图像的宽度和高度。进一步在读取图像数据时,直接执行:

for( r=0; r<rows; r++ )
{
    for( c=0; c<collumns; c++ )     // read pixel data from image
    {
        fread(&pixelarray[r][c] , 1, sizeof(PIXEL), file);
        pixelnum++;
    }
}

【讨论】:

  • 谢谢,到目前为止一切正常。也感谢您的参考
【解决方案2】:

您必须使用 16 位变量(无符号短)而不是 int 来表示宽度和高度。根据Wikipedia,宽/高为16位。

【讨论】:

    【解决方案3】:

    在十六进制编辑器中打开 bmp 文件,查看 info 标头中的值。 然后调试您的代码并检查您在 infoheader 中读取的值。

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多