如何将BMP文件读入C中的像素网格?

时间:2017-04-14 18:11:55

标签: c multidimensional-array fread

我试图读取给定的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;

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

2 个答案:

答案 0 :(得分:0)

鉴于你是如何在结构,列表和基本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));
}

认为你基本上得到了正确的内存分配。如果您应该使用Pixel,那么您的问题是尝试使用BMPImage结构。虽然高度和宽度来自BMPHeader

the size of the pixels are variable according to the bits_per_pixel header以来,你不应该如何阅读BMP。您的结构仅支持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%正确,但它至少可以清除最糟糕的混乱。

答案 1 :(得分:-1)

这是未经测试的代码。

 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.

}

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

其次,需要释放内存,因为它看起来需要大量内存。 在一个区域中分配/释放内存是一种很好的做法。

相关问题