- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试读取给定的 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;
}
}
同样,您有 BMPImage
和 BMPHeader
与 Pixel
混淆.此外,fopen
不返回要读取的结构。相反,您负责分配必要的内存(C 中的一个主题)。 fopen
返回读取的项目数。对所有文件操作进行错误检查是非常重要的,否则您将留下难以理解的垃圾。
我怀疑您可以通过读取一大块中的所有像素来更简单地完成此操作,但我不知道 BMP 格式的详细信息。
这绝不是 100% 正确,我什至不确定它是否 80% 正确,但它至少会消除最严重的困惑。
关于c - 如何将 BMP 文件读入 C 中的像素网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43416933/
假设您有 2 个文件,如下所示。 file_1_october.csv file_2_november.csv 文件具有相同的列。所以我想在 R 中读取这两个文件,我可以使用 map 轻松完成。我还想
我有一个制表符分隔的文本文件: 0730000 John 1 01 225 000 000 当我将它读入 R 时 stud_stats data.table::f
似乎最直观的是 .rdata 文件可能是 R 加载的快速文件格式,但是在扫描一些堆栈帖子时,似乎更多的注意力集中在提高 .csv 或其他格式的加载时间上。有确定的答案吗? 最佳答案 不是一个明确的答案
我是 R 的新手,目前在读取 .csv 文件并将其转换为 data.frame 时遇到了很多麻烦7 列。这是我正在做的: gene_symbols_table head(gene_symbols_t
基本上我有一个格式如下所示的 csv: csv 有 11 列,前五列和后五列完全相同。我希望能够读取 csv 并将第一列和第五列(期间和支出)的所有实例存储在一个列表中,它们具有值,并对另一个列表中的
我对 Julia 比较陌生,正在寻找一种有效的方法来从文本文件中读取并将每个“列”存储在数组中(我有 2 列,但通用解决方案也很棒)。例如,我想要输入 1 2 3 4 5 6
基本上我有一个格式如下所示的 csv: csv 有 11 列,前五列和后五列完全相同。我希望能够读取 csv 并将第一列和第五列(期间和支出)的所有实例存储在一个列表中,它们具有值,并对另一个列表中的
我的程序分配了一个 32 位 int,随后尝试使用 read(2) 从套接字将 4 个字节读入 int 有时读取不完整并返回读取 2 个字节。有什么方法可以从中恢复吗?我想我必须在 int 的中途生成
我有大量的 CSV 文件。有些标题从第一行开始,其他标题从第 3 行开始,其他的从第 7 行开始,依此类推。 标题看起来都一样,它们只是从不同文件的不同行开始。有没有办法有条件地 read.csv 文
我写了一个小程序来从 csv 文件中读取数据: using System; using System.Collections.Generic; using System.Linq; using Sys
我需要读入一个包含 10,000 个整数的列表,并将它们按升序放置在一个 vector 中。请注意,我不是在然后阅读排序,而是在同时阅读时排序。 我这样做是为了学习。我意识到阅读时排序是 O(n^2)
我有一个问题。不幸的是,我没有找到任何答案。如何将参数传递给脚本,这是另一个命令的结果。例如: ls | ./myscript.sh 我想将 ls 的结果传递给 myscript。如果我执行上面的命
我在读取扩展 ASCII 字符并将其转换为十进制值时遇到问题。我试过这样做: unsigned char temp; while(temp = cin.get != EOF) { cout << (i
我已经通过以下命令加载了文本文件。我想从 contents 中删除由 \n 分隔的第一行标题行。怎么做? txtfile = open(filepath, "rt") contents = txtfi
希望一切顺利...我正在将数据集输入到 sklearn 算法中进行分类,但找不到任何简单的数据集来开始,所以我自己制作了数据集。但有一个问题... import numpy as np import
我有一个 .csv 文件,它有 3 行和 5 列,值为 0、1、2、3、50 或 100。我将它从 Excel 工作表保存到 .csv 文件。我正在尝试使用 C++ 读取 .csv 文件,并根据最后三
我有一个 HTML 文件,它将作为我要发送的电子邮件的模板。 html 中有一些字段是可变的。我想知道是否有一种可靠的方法可以用变量替换 HTML 文件中的占位符。我知道我可以 string.Repl
我从未使用过 JSON 文件,但我有实现 JSON 文件的任务,我需要将其转换为 IEnumerable。当我尝试对 JSON 对象进行反序列化时,我得到一个异常,上面写着: An unhandled
我正在尝试阅读 IFormFile从这样的 HTTP POST 请求中收到: public async Task UploadDocument([FromForm]DataWrapper data)
我有一个包含大量多行文本 block 的文件。我想将该文件读入一个字符向量列表——每个 block 一个。我对 scan()、read.table() 等函数的文档的阅读似乎表明一行的结尾将结束向量。
我是一名优秀的程序员,十分优秀!