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

问题描述

我想参加一个.bmp文件,并最终编辑像素一个接一个,但我想出的宽度和高度的问题在INFOHEADER结构退还给我。返回的宽度是13107200,高度为65536然而,每当我通过程序总共只有60003总像素计数运行。我不知道这是为什么。任何帮助将大大AP preciated。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主(INT ARGC,CHAR *的argv []){
    //定义结构
        typedef结构
        {无符号短整型; / *魔法标识符* /
            unsigned int类型的大小; / *文件的字节大小* /
            无符号短整型保留1,RESERVED2;
            unsigned int类型胶印机;偏移到数据(B)/ * * /
        }报头; / * - 14字节 - * /        typedef结构
        {unsigned int类型的大小; / *头字节大小* /
            INT宽度,高度; / *宽/图像高度* /
            无符号短整型平面; / *颜色平面数* /
            无符号短整数位; / *每个像素位* /
            unsigned int类型的COM pression; / *的COM pression型* /
            无符号整型IMAGESIZE; / *图像大小以字节为单位* /
            INT xResolution,yResolution; / *每米*像素/
            unsigned int类型的颜色; / *色数* /
            unsigned int类型ImportantColors; / *重要的颜色* /
        } INFOHEADER; / * - 40字节 - * /        typedef结构
        {unsigned char型红,绿,蓝;
        }像素;    //使所有三个结构的一个实例
    标题数据;
    INFOHEADER DATA2;
    PIXEL像素;    //声明文件中读出指针
    FILE *文件;    //声明FILEOUT读指针
    // FILE * FILEOUT; //声明文件打印文件指针    参数计数器//打开文件1,并返回0 APON错误
    如果(!(文件= FOPEN(CU.bmp,RB)))返回0;
    //读取头数据到数据
    的fread(安培;数据的sizeof(HEADER),1,文件);
    //读IB + NFOHEADER数据到数据2
    的fread(安培;数据2,的sizeof(INFOHEADER),1,文件);
    //打印像素数据    //为分配空间pixelarray
    PIXEL ** pixelarray;
    INT R = 0,C = 0,行= data2.Height,collumns = data2.Width;
    pixelarray =的malloc(行*的sizeof(PIXEL *));
    为(R = 0;为r行; R ++){
        pixelarray [R] =的malloc(collumns *的sizeof(PIXEL));
    }    //填充像素结构像素阵列
    使r = 0 C = 0;
    INT pixelnum = 1;
    而(的fread(安培;像素的sizeof(PIXEL),1,文件)){
        如果(C == collumns){
            C = 0;
             - [R ++;
        }
        pixelarray [R] [C] =像素;
        的printf(\\ nPixel%10D:%02X%02X%02X,pixelnum,pixelarray [R] [C] .Red,pixelarray [R] [C]。蓝色,pixelarray [R] [C]。绿色);
        fflush(标准输出);
        C ++; pixelnum ++;    }    免费(pixelarray);    FCLOSE(文件); //之前关闭文件退出


解决方案

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

 的#pragma包(推)//推动当前对齐堆栈
的#pragma包(1)//设置对齐1字节边界
typedef结构
{
    无符号短整型; / *魔法标识符* /
    unsigned int类型的大小; / *文件的字节大小* /
    无符号短整型保留1;
    无符号短整型RESERVED2;
    unsigned int类型胶印机;偏移到数据(B)/ * * /
}报头; / * - 14字节 - * /typedef结构
{
    unsigned int类型的大小; / *头字节大小* /
    INT宽度;
    INT高度; / *宽/图像高度* /
    无符号短整型平面; / *颜色平面数* /
    无符号短整数位; / *每个像素位* /
    unsigned int类型的COM pression; / *的COM pression型* /
    无符号整型IMAGESIZE; / *图像大小以字节为单位* /
    INT xResolution;
    INT yResolution; / *每米像素* /
    unsigned int类型的颜色; / *色数* /
    unsigned int类型ImportantColors; / *重要的颜色* /
} INFOHEADER; / * - 40字节 - * /typedef结构
{
    unsigned char型红;
    unsigned char型绿色;
    unsigned char型蓝色;
}像素;
的#pragma包(POP)//恢复从堆栈原定线

这正确读取BMP图像的宽度和高度。而且在读取图像数据,直接做到这一点:

 为(R = 0;为r行; R ++)
{
    为(C = 0;℃下collumns; C ++)//读取图像的像素数据
    {
        的fread(安培; pixelarray [R] [C],1,sizeof的(PIXEL),文件);
        pixelnum ++;
    }
}

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
解决方案

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

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头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:54