【问题标题】:Can not Read Bitmap data of a (1280 x 960) .bmp image In C无法在 C 中读取 (1280 x 960) .bmp 图像的位图数据
【发布时间】:2021-07-29 15:30:02
【问题描述】:

我正在尝试打开 .bmp 图像文件并将位图数据读取到缓冲区。但我没有得到例外的输出。我是 C 语言的新手,请协助我处理这个案例。

这是我的“main.c”文件。

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

typedef uint32_t DWORD;   // DWORD = unsigned 32 bit value
typedef uint16_t WORD;    // WORD = unsigned 16 bit value

#pragma pack(push, 1)

typedef struct {
    WORD bfType;  //specifies the file type
    DWORD bfSize;  //specifies the size in bytes of the bitmap file
    WORD bfReserved1;  //reserved; must be 0
    WORD bfReserved2;  //reserved; must be 0
    DWORD offset;  //specifies the offset in bytes from the bitmapfileheader to the bitmap bits
} BMP_FILE_HEADER;

#pragma pack(pop)

 unsigned char *LoadBitmapFile(char *filename)
{
    FILE *filePtr;  //our file pointer
    BMP_FILE_HEADER bitmapFileHeader;  //our bitmap file header
    unsigned char *bitmapImage;  //store image data
    size_t bytes_read;

    //open file in read binary mode
    filePtr = fopen(filename,"rb");
    if (filePtr == NULL)
        return NULL;

    //read the bitmap file header
    fread(&bitmapFileHeader,1,sizeof(BMP_FILE_HEADER),filePtr);
    printf("Type : %d \n",bitmapFileHeader.bfType );

    //verify that this is a .BMP file by checking bitmap id
    if (bitmapFileHeader.bfType !=0x4D42)
    {
        printf("This is not a bitmap" );

        fclose(filePtr);
        return NULL;
    }

    //move file pointer to the beginning of bitmap data
    fseek(filePtr,bitmapFileHeader.offset, SEEK_SET);
    printf("Where is the file pointer = %ld \n", ftell(filePtr) );

    //allocate enough memory for the bitmap image data
    bitmapImage = (unsigned char*)malloc(1280*960);

    //verify memory allocation
    if (!bitmapImage)
    {   
      printf("Memory Allocation failed" );
        free(bitmapImage);
        fclose(filePtr);
        return NULL;
    }


    //read in the bitmap image data
    bytes_read = fread(bitmapImage,1,1280*960 ,filePtr);
    printf("Read Bytes : %zu\n",bytes_read );

    //make sure bitmap image data was read
    if (bitmapImage == NULL)
    {
      printf("data was not read" );
        fclose(filePtr);
        return NULL;
    }
    printf("bitmapImage: %s\n", bitmapImage );

    printf("Header- size -> %i\n",bitmapFileHeader.bfSize );
    printf("Header- OffSet -> %i\n",bitmapFileHeader.offset );

    //close file and return bitmap image data
    fclose(filePtr);
    return bitmapImage;
}

int  main(){
  
  unsigned char *bitmapData;
  bitmapData = LoadBitmapFile("img_06.bmp");
  
  printf("bitmapData: %s", bitmapData );

return 0;
}

这是输出:

我无法打印“bitmapData”缓冲区的任何数据。这是我在程序中使用的.bmp文件。

图像尺寸 (1,229,878 字节)
宽度 1280 像素 / 高度 960 像素

【问题讨论】:

  • %s 在看到 0x00 时停止打印。尝试printf("bitmapData: %d", bitmapData[0] ); 打印第一个字节。
  • printf("bitmapData: "); fwrite(bitmapData, 1280*960, 1, stdout);?
  • 一般来说,这是二进制图像数据,而不是字符串。尝试将数据打印为字符串是一种荒谬的操作......就像尝试使用"%c" 打印每个字符一样。有几个不可打印的字符。查看每个字节的整数值(在您选择的基础中)实际上是“查看”原始数据的唯一方法。
  • 你在fread之后的检查if (bitmapImage == NULL)也是废话/没用,你需要检查返回值bytes_read来确定读取发生了什么。唯一需要检查bitmapImage 有效性的地方是在调用malloc 之后,就像你一样。
  • 另外,为什么不malloc(bfSize)(或者是bfSize - sizeof(BMP_FILE_HEADER)?)。应该有一种方法来确定/导出malloc 所需的数据量,而不是使用硬编码的常量。在一种天真的/简单的方法中,您至少可以malloc 文件大小,使您的代码适用于任何位图文件,而不仅仅是那些匹配 1280x960 的文件。

标签: c bitmap bitmapdata


【解决方案1】:
I replaced the line,
printf("bitmapData: %s", bitmapData );

with 
printf("bitmapData: "); fwrite(bitmapData, 1280*960, 1, stdout);

【讨论】:

    猜你喜欢
    • 2013-09-02
    • 2014-12-26
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    相关资源
    最近更新 更多