c - 用C从BMP文件中读取数据
问题描述
我从 bmp 文件中读取像素时遇到问题。它可能是在行尾有填充或base64填充的东西。我没有线索。我已经为此苦苦挣扎了几天,无法继续前进,因为下一个任务需要解决这个问题。我只分享代码的重要部分,因为读取 bmp 标头工作正常(测试有 0 次失败)。
bmp文件
struct pixel* read_data(FILE* stream, const struct bmp_header* header){
if(stream == NULL || header == NULL){
return 0;
}
// w == 1 && p == 1; w == 2 && p == 2; w == 3 && p == 3; w == 4 && p == 0
int padding = header->width % 4;
int num_of_pixels = header->width * header->height;
struct pixel* Pixel[num_of_pixels];
fseek(stream, 54, SEEK_SET); //move 54B (header size)
int index_p = 0;
for(int i = 0; i < header->height; i++){
for(int j = 0; j < header->width; j++){
Pixel[index_p] = malloc(sizeof(struct pixel));
fread(&(Pixel[index_p]->blue), 1, 1, stream);
fread(&(Pixel[index_p]->green), 1, 1, stream);
fread(&(Pixel[index_p]->red), 1, 1, stream);
index_p++;
}
fseek(stream, padding, SEEK_CUR); //padding at the end of row
}
return *Pixel;
}
bmp.h
struct pixel {
uint8_t blue;
uint8_t green;
uint8_t red;
//uint8_t alpha;
} __attribute__((__packed__));
/**
* Read the pixels
*
* Reads the data (pixels) from stream representing the image. If the stream
* is not open or header is not provided, returns `NULL`.
*
* @param stream opened stream, where the image data are located
* @param header the BMP header structure
* @return the pixels of the image or `NULL` if stream or header are broken
*/
struct pixel* read_data(FILE* stream, const struct bmp_header* header);
如果需要,标题(基本上我们只使用 24 位颜色)
struct bmp_header{
uint16_t type; // "BM" (0x42, 0x4D)
uint32_t size; // file size
uint16_t reserved1; // not used (0)
uint16_t reserved2; // not used (0)
uint32_t offset; // offset to image data (54B)
uint32_t dib_size; // DIB header size (40B)
uint32_t width; // width in pixels
uint32_t height; // height in pixels
uint16_t planes; // 1
uint16_t bpp; // bits per pixel (24)
uint32_t compression; // compression type (0/1/2) 0
uint32_t image_size; // size of picture in bytes, 0
uint32_t x_ppm; // X Pixels per meter (0)
uint32_t y_ppm; // X Pixels per meter (0)
uint32_t num_colors; // number of colors (0)
uint32_t important_colors; // important colors (0)
} __attribute__((__packed__));
main.c我不需要为调用的函数分配任何变量,因为我们有一个测试程序,我只需要在 main 中调用它们
int main(){
struct bmp_header* header;
FILE *stream = fopen("./assets/square.2x3.bmp", "rb");
header = read_bmp_header(stream);
read_data(stream, header);
read_bmp(stream);
struct bmp_image* image;
image = malloc(sizeof(struct bmp_image));
free_bmp_image(image);
fclose(stream);
return 0;
}
测试(有更多测试,但这应该足够了)
1:
FILE* stream = "Qk0+AAAAAAAAADYAAAAoAAAAAgAAAAEAAAABABgAAAAAAAgAAAAjLgAAIy4AAAAAAAAAAAAA/wAAAP8AAAA="; // base64 encoded stream
struct bmp_header* header = read_bmp_header(stream);
fseek(stream, offset, SEEK_SET);
Assertion 'read_data(stream, header) == "/wAAAP8A"' failed. [got "/wAAFctV"]
2:
FILE* stream = "Qk1GAAAAAAAAADYAAAAoAAAAAgAAAAIAAAABABgAAAAAABAAAAAjLgAAIy4AAAAAAAAAAAAA/wAAAAAAAAAAAP8A/wAAAA=="; // base64 encoded stream
struct bmp_header* header = read_bmp_header(stream);
fseek(stream, offset, SEEK_SET);
Assertion 'read_data(stream, header) == "/wAAAAAAAAD/AP8A"' failed. [got "/wAAAAAAAAAAAAAA"]
所以在“==”之后是预期的结果,括号中是我的代码的结果。正如我所提到的,它可能是带有填充的东西,因为它开始很好但没有很好结束。感谢帮助。
解决方案
简短答案:将填充设置为(4-((3*width)%4))%4
长答案:
您的代码包括:
int padding = header->width % 4;
//Some lines of code
fseek(stream, padding, SEEK_CUR);
在位图中,添加填充直到每行是 4 字节的倍数。您将填充作为width % 4
.
首先,每个像素占用 3 个字节(对于 rgb)。所以应该是(3*width)%4
。接下来,我们需要从 4 个字节中减去它(因为填充是 4- pixels occupied
)。所以填充将是4-((3*width)%4)
. 另一个小的修改,如果(3*width)%4==0
填充将变为 4(而我们希望它为 0)。所以我们采取另一个mod4只是为了确定
所以填充会出来 (4-((3*width)%4))%4
编辑:
正如用户 Craig Estey 在评论中指出的那样,最好使用 sizeof(struct pixel) 而不是 3
推荐阅读
- java - 在一个函数中使用两组常量
- java - Gson 不映射 Java 中的值
- python - 使用 yfinance python 从 yahoo Finance 获取财务数据
- java - 我可以从毫秒值中获取当前时间的值吗
- vb.net - EMGU CV 与 vb.net
- ms-access - 如何设计两个不直接相关的表的查询
- apache-spark - 如果 mapPartitions 与 toDF() 一起使用,为什么很少有分区被处理两次
- javascript - 如何从 http 请求运行节点 localhost 子进程?
- r - 如何将子图应用于具有辅助 y 轴的图列表
- excel - vba excel datediff计算不同的结果