简体   繁体   English

读取bmp文件并将其在C中反转

[英]Reading a bmp file and inverting it in C

I have an assignment that deals with reading a bmp file into memory, inverts the pixels, and then saves the inverted image to a new file. 我有一个作业,涉及将bmp文件读取到内存中,反转像素,然后将反转的图像保存到新文件中。 From this description it seems fairly easy, however I don't think my professor did a great job in explaining the necessary steps to go about doing so. 从这个描述看来,这似乎很容易,但是我认为我的教授在解释这样做的必要步骤方面做得并不出色。 He taught us about fread and fwrite but there is so much more. 他教会了我们有关fread和fwrite的知识,但还有很多。 Can anyone explain the process in going about this problem (I'm no looking for a direct answer just an explanation). 任何人都可以解释解决此问题的过程(我不是在寻找直接的答案,而只是一个解释)。

Here is the link to the problem's description: https://engineering.purdue.edu/OOSD/F2012/Exercises/ex5.html 这是问题描述的链接: https : //engineering.purdue.edu/OOSD/F2012/Exercises/ex5.html

Thanks in advance for any sort of help. 在此先感谢您提供的任何帮助。 NOTE: I actually have looked into this problem but since I don't have a good standing on this info it's not quite "clicking". 注意:我实际上已经研究了这个问题,但是由于我对这个信息没有很好的了解,因此并不是很“点击”。

I guess the part that I am stuck on right now is: 我想我现在停留的部分是:

    /* The input argument is the source file pointer. The function will first construct a BMP_Image image by allocating memory to it.
 * Then the function read the header from source image to the image's header.
 * Compute data size, width, height, and bytes_per_pixel of the image and stores them as image's attributes.
 * Finally, allocate menory for image's data according to the image size.
 * Return image;
*/
BMP_Image* CreateBMPImage(FILE* fptr)
{

  //Allocate memory for BMP_Image*;

  //Read the first 54 bytes of the source into the header

  //Compute data size, width, height, and bytes per pixel;

  //Allocate memory for image data
}

The BMP_Image structure looks like this: BMP_Image结构如下所示:

typedef struct {
    BMP_Header header;
    int data_size;
    int width;
    int height;
    int bytes_per_pixel; // This amount should be equals to number of bits/8
    char *data;
} BMP_Image;

Read the BMP file format (http://en.wikipedia.org/wiki/BMP_file_format), specially pixal storage details. 阅读BMP文件格式(http://en.wikipedia.org/wiki/BMP_file_format),尤其是像素存储详细信息。 They are stored in rows. 它们存储在行中。 Read them in bytes and use bit operators and may be uint8_t :) Because it is an assignment, you should write your code yourself. 以字节为单位读取它们并使用位运算符,并且可能是uint8_t :)因为这是一个赋值,所以您应该自己编写代码。 At least try. 至少尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM