2011-04-29 79 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