麻烦阅读.bmp头文件正确 [英] Trouble reading .bmp header file properly

查看:163
本文介绍了麻烦阅读.bmp头文件正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to take in a .bmp file and eventually edit the pixels one by one but I am coming up with a problem with the width and height returned to me in the INFOHEADER struct. The width returned is 13107200 and the height is 65536. However, whenever I run through the program a total of only 60003 total pixels are counted. I have no idea why this is. Any help would be greatly appreciated.

#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


推荐答案

我猜你的问题是结构对齐。您可以参考此处此处。要消除它,请使用 #pragma 指令。所以你的结构声明将是这样的:

I guess your problem is structure alignment. You can refer to it here and here. To eliminate it use the #pragma directive. So your structure declaration would be something like this:

#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图像的宽度和高度。进一步阅读图像数据时,请直接执行:

This correctly reads the width and height of BMP image. Further while reading image data, do it directly:

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++;
    }
}

这篇关于麻烦阅读.bmp头文件正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆