希望能对我的一项作业提供一些集思广益的帮助。我要编写一个程序,对 .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;
}
任何人都可以帮助我了解我需要对分配引用的字节地址执行哪些操作吗?任何其他集思广益的建议也将不胜感激。谢谢!
最佳答案
您可以通过以二进制模式打开文件来读取原始字节:
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);
我希望这足以让您继续前进。
关于c - 位图点处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837855/