【问题标题】:Bitmap point processing位图点处理
【发布时间】:2011-08-15 19:44:28
【问题描述】:

希望能为我的一项任务提供头脑风暴帮助。我要编写一个对 .bmp 图像进行基本点处理的程序。程序会打开一个.bmp文件进行读写,不会改变文件头的任何部分,而是根据命令行参数改变文件中的像素值:

-fromrow x, where x specifies the bottommost row to process
-torowx, where x specifies the topmost row to process
-fromcol x, where x specifies the leftmost column to process
-tocol x, where x specifies the rightmost column to process
-op x, where x is one of the following:
    - 1 = threshold the image (any pixel value in the specifies range over 127 is changed to 255, and pixel values 127 or less is changed to 0)
    - 2 = negative (any pixel value p in the specified range is changed to 255-p)

To process image data, you will need to make use of the following:
- each pixel value is an unsigned char
- the number of rows in the image is stored as an int at position (byte address) 22 in the file
- the number of columns in the image is stored as an int at position (byte address) 18 in the file
- the position at which the pixel data starts is an int stored at position (byte address) 10 in the file
- pixel information is stored row by row, starting from the bottommost row in the image (row 0) and progressing upwards. within a row; pixel information is stored left to right. padding is added to the end of each row to make row length a multiple of 4 bytes (if the row has 479 columns, there is one extra padding at the end of the row before the next row starts)

我对如何开始有点迷茫,但我想我应该先像这样制作一个结构位图?

struct bitmap {
    unsigned int startrow;
    unsigned int endrow;
    unsigned int startcol;
    unsigned int endcol;
}

谁能帮助我了解我需要为分配引用的字节地址做些什么?任何其他头脑风暴的建议也将不胜感激。谢谢!

【问题讨论】:

  • 你知道如何打开文件吗?你知道如何读取文件吗?
  • 那种。我的最后一项任务是从文本文件中读取和解析数据,所以我认为它会有点相似? int main(int argc, char *argv[]) { int i; FILE *fp; for (i = 1; i < argc; i++) { fp = fopen(argv[i], "r+b"); if (fp == NULL) { fprintf(stderr, "cat: can't open %s\n", argv[i]); continue; } while (fgets(line, sizeof(line), fp) != NULL) { } fclose(fp); } 不过,对于位图文件该做什么,我有点迷茫。
  • 看到这个问题。您需要知道如何读取原始字节而不是文本。 stackoverflow.com/questions/5751749/…
  • 不知道如何读取原始字节。你介意解释一下吗?

标签: c image-processing bitmap


【解决方案1】:

您可以通过二进制模式打开文件来读取原始字节

FILE *fid = fopen("blah.bmp", "rb");

然后您可以读取一些数据:

int num_actually_read = fread(p, sizeof(*p), num_to_read, fid);

其中p 是指向某个缓冲区的指针。在这种情况下,您可能希望 p 的类型为 uint8_t *,因为您主要处理的是原始字节。

或者,您可以这样在文件中跳转:

fseek(fid, pos, SEEK_SET);

我希望这足以让你继续前进。

【讨论】:

    【解决方案2】:

    您将需要一个指向文件的字节地址 22 和 18 的指针。指向这些地址后,您将需要取消引用指针以获取行和列值。然后你必须将你的指针指向地址 10,然后逐个遍历像素。

    【讨论】:

    • 所以我创建了一个int *rowint *column。我如何将它指向一个字节地址?我想我对读取文件时位图数据的样子感到困惑。
    • -1:什么?指向文件中字节的指针?在这里将文件映射到内存不是一个值得的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2012-01-20
    相关资源
    最近更新 更多