gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:59:10 25 4
gpt4 key购买 nike

我正在尝试读取给定的 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));
}

认为您基本上已经正确分配了内存。您的问题是尝试使用 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% 正确,但它至少会消除最严重的困惑。

关于c - 如何将 BMP 文件读入 C 中的像素网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43416933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com