如何将BMP文件读取到C中的像素网格中



我正在尝试在给定的BMP文件中读取并将其存储在图像中。我对语法感到困惑,因为我正在努力理解给我的.h文件。这是我要在图像中阅读的方式:

BMPImage * readImage(FILE * fp) {
// FILL IN
BMPHeader * hp = malloc(sizeof(BMPHeader);
Pixel * p = malloc(sizeof(Pixel));
p -> pixels = malloc(p -> height_px * sizeof(Pixel *));
for(int i = 0; i < p -> height_px; i++){
  p -> pixels[i] = malloc(p -> width_px * sizeof(Pixel));
}
for (i = 0; i < p -> height_px; i++){
  for(int j = 0; j < p -> width_px; j++){
    Pixel px = fread(hp, sizeof(Pixel), 1, fp);
    p -> pixels[i][j] = px;
  }
}
return p;

}

这是.h文件:

typedef struct __attribute__((packed)) BMPHeader  {             // Total: 54 bytes
  uint16_t  type;             // Magic identifier: 0x4d42
  uint32_t  size;             // File size in bytes
  uint16_t  reserved1;        // Not used
  uint16_t  reserved2;        // Not used
  uint32_t  offset;           // Offset to image data in bytes from beginning of file (54 bytes)
  uint32_t  dib_header_size;  // DIB Header size in bytes (40 bytes)
  int32_t   width_px;         // Width of the image
  int32_t   height_px;        // Height of image
  uint16_t  num_planes;       // Number of color planes
  uint16_t  bits_per_pixel;   // Bits per pixel
  uint32_t  compression;      // Compression type
  uint32_t  image_size_bytes; // Image size in bytes
  int32_t   x_resolution_ppm; // Pixels per meter
  int32_t   y_resolution_ppm; // Pixels per meter
  uint32_t  num_colors;       // Number of colors  
  uint32_t  important_colors; // Important colors 
} BMPHeader;
typedef struct __attribute__((packed)) Pixel {
    uint8_t blue;
    uint8_t green;
    uint8_t red;
    uint8_t alpha;
} Pixel;
typedef struct BMPImage {
    BMPHeader header;
    int norm_height; //normalized height
    Pixel * * pixels;
} BMPImage;

我应该如何纠正阅读方法?

鉴于您如何在结构,列表和基本I/O中挣扎,在BMP中阅读可能已经超过您的头。我建议尝试填充更简单的结构。如果是用于生产代码的,请使用现有库。

我将进行一些基本修复程序,以便您的代码至少编译,希望您可以从那里拿走。

BMPImage *readImage(FILE * fp) {
    // Allocate space for the image.
    // This also covers BMPHeader, since it's not a pointer.
    BMPImage *bmp = malloc(sizeof(BMPImage));
    BMPHeader *bmph = &(bmp->header);

基本单元是BMPImage结构。其中包含bmpheader以及指向像素列表的指针。BMPHeader不是指针,实际内存包含在BMPImage结构中,因此malloc(sizeof(BMPImage))还为BMPHeader分配内存。我已经将指针指向BMPHeader,以使其更容易使用,否则代码撒上bmp.header->height_x

我将填充Bmpheader留给您。完成此操作后...

// Allocate space for the pixels.
bmp->pixels = malloc( bmph->height_px * sizeof(Pixel *) );
for(int i = 0; i < bmph->height_px; i++){
    bmp->pixels[i] = malloc(bmph->width_px * sizeof(Pixel));
}

i think 您基本上可以正确地获得内存分配。当您应该使用BMPImage时,您的问题是尝试使用Pixel结构。而高度和宽度来自BMPHeader

这并不是您应该如何在BMP中读取的方式,因为根据bits_per_pixel标头,像素的大小是可变的。您的结构仅支持8BPP格式。它也可能被压缩。我将假设这是一种练习,而不是生产代码,所以我将避免进一步了解BMP的细节。

// Read in each pixel
for (int i = 0; i < bmph->height_px; i++){
    for(int j = 0; j < bmph->width_px; j++){
        Pixel px;
        if( fread(&px, sizeof(Pixel), 1, fp) < 1 ) {
            fprintf(stderr, "Error while reading bmp: %s", strerror(errno));
            return NULL;
        }
        bmp->pixels[i][j] = px;
    }
}

再次,您将BMPImageBMPHeaderPixel混合。此外,fopen不会返回要读取的结构。相反,您负责分配必要的内存(C中的主题(。fopen返回读取的项目数。非常重要的是要在所有文件操作上检查错误,否则您将难以理解垃圾。

我怀疑您可以通过在一个大块的所有像素中阅读所有像素,但是我不知道BMP格式的细节。

这绝不是100%正确的,我什至不确定它是80%正确的,但至少会清理最坏的混乱。

这是未经测试的代码。

 void readImage(FILE * fp, BMPImage *bpp) {
    Pixel pixel;
    for (i = 0; i < p -> height_px; i++){
       for(int j = 0; j < p -> width_px; j++){
           fread(&pixel, sizeof(Pixel), 1, fp);
           bpp->pixels[i][j] = pixel;
       }
   }
  int
  main()
  {
     BMPHeader *hp = malloc(sizeof(* hp));
     FILE *fp;
     int i;
     ... read header info and stored into hp->header
     ... open fp file
     hp->pixels = malloc(bpp->header.width_px * sizeof(Pixel *));
     for( i=0; i<hp->header.width_px; i++ )
        hp->pixels[i] = malloc(bpp->header.height_px * sizeof(Pixel));
     //Send in pointers.
     readImage(fp, hp);
     ... need to free up memory.

}

。首先,需要遵循复合数据结构。

第二,需要释放内存,因为它看起来需要很多内存。在一个区域中分配/释放内存是一个很好的做法。

最新更新