2011-04-29 82 views
0

会喜欢我的任务之一集思广益一些帮助。我将编写一个程序来处理.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; 
} 

任何人都可以帮助我通过什么,我需要做的字节地址,该任务引用?任何其他头脑风暴的建议也将不胜感激。谢谢!

+0

你知道如何打开一个文件?你知道如何从文件中读取吗? – 2011-04-29 22:05:42

+0

种。我最后的任务是从文本文件中读取和解析数据,所以我认为它会有点类似? 'INT主(INT的argc,字符* argv的[]){ \t INT I; \t FILE * fp; (i = 1; i raphnguyen 2011-04-29 22:12:09

+0

看到这个问题。你需要知道如何阅读原始字节,而不是文本。http://stackoverflow.com/questions/5751749/how-can-i-read-bmp-pixel-values-into-an-array – 2011-04-29 22:17:09

回答

1

可以通过在二进制模式打开文件读取的原始字节:

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

然后可以读出的数据的一定量的这样的:

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

其中p是一个指向一些缓冲。在这种情况下,你可能想puint8_t *类型的,因为你所面对的大多是原始字节。

或者,你可以在这样一个文件跳来跳去:

fseek(fid, pos, SEEK_SET); 

我希望这是足以让你去。

-1

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

+0

所以我创建了一个'int * row'和'int * column'。我如何将它指向一个字节地址?我想我在读入文件时对什么位图数据看起来很困惑。 – raphnguyen 2011-04-29 22:25:52

+0

-1:什么?指向文件中的字节的指针?将文件映射到内存中并不是一个有价值的解决方案。 – 2011-04-29 22:26:53