如何将 BMP 文件读入 C 中的像素网格?
How to read a BMP file into a grid of pixels in 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));
}
我认为你基本上已经正确分配了内存。您的问题是在您应该使用 BMPImage
时尝试使用 Pixel
结构。而高度和宽度来自 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;
}
}
同样,您将 BMPImage
和 BMPHeader
与 Pixel
混淆了。此外,fopen
不会 return 要读取的结构。相反,您负责分配必要的内存(C 中的一个主题)。 fopen
returns 阅读的项目数。对所有文件操作进行错误检查非常重要,否则您将留下难以理解的垃圾。
我怀疑您可以通过读取一大块中的所有像素来更简单地完成此操作,但我不知道 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.
}
。
首先,需要遵循复合数据结构。
其次,需要释放内存,因为它看起来需要大量内存。
allocate/free 在一个区域增加内存是一个很好的做法。
我正在尝试读取给定的 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));
}
我认为你基本上已经正确分配了内存。您的问题是在您应该使用 BMPImage
时尝试使用 Pixel
结构。而高度和宽度来自 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;
}
}
同样,您将 BMPImage
和 BMPHeader
与 Pixel
混淆了。此外,fopen
不会 return 要读取的结构。相反,您负责分配必要的内存(C 中的一个主题)。 fopen
returns 阅读的项目数。对所有文件操作进行错误检查非常重要,否则您将留下难以理解的垃圾。
我怀疑您可以通过读取一大块中的所有像素来更简单地完成此操作,但我不知道 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.
}
。 首先,需要遵循复合数据结构。
其次,需要释放内存,因为它看起来需要大量内存。 allocate/free 在一个区域增加内存是一个很好的做法。