Copying and negating Bitmap

Hi there. I'm very new to C and I'm trying to load in a bitmap, make it a negative and make the negative a new file without affecting the original.

The way I thought to do this was to read the original file bit-by-bit using fstream and either subtracting each binary digit from 1 (making it the negative i.e. 1-0 = 1, 1-1 = 0) or if I can't do that, byte-by-byte (subtracting from 1111 1111, or FF if it has to be in Hex). From browsing around I've found that I can copy a file using this;

1
2
3
4
5
ifstream f1("FullColour.bmp", fstream::binary);

  ofstream f2("output.bmp", fstream::trunc|fstream::binary);

  f2 << f1.rdbuf();


I'm just wondering if I can interrupt this process (or even if someone could explain exactly what this code is doing, very clearly) and manipulate the data in fstream before it is output? Am I barking completely up the wrong tree with this whole idea?

By the way, this is for a University project so I would ask if possible to please not post up working code segments because if I use them, I run the risk of plagiarism and frankly, I don't really deserve the marks for work that isn't my own.

EDIT: Yeah, I just realized that this won't work because I'll also be negating all the header information in the bitmap file. I have this header file;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#  ifndef WIN32
typedef struct                       /**** BMP file header structure ****/
    {
    unsigned short bfType;           /* Magic number for file */
    unsigned int   bfSize;           /* Size of file */
    unsigned short bfReserved1;      /* Reserved */
    unsigned short bfReserved2;      /* ... */
    unsigned int   bfOffBits;        /* Offset to bitmap data */
    } BITMAPFILEHEADER;

#  define BF_TYPE 0x4D42             /* "MB" */

typedef struct                       /**** BMP file info structure ****/
    {
    unsigned int   biSize;           /* Size of info header */
    int            biWidth;          /* Width of image */
    int            biHeight;         /* Height of image */
    unsigned short biPlanes;         /* Number of color planes */
    unsigned short biBitCount;       /* Number of bits per pixel */
    unsigned int   biCompression;    /* Type of compression to use */
    unsigned int   biSizeImage;      /* Size of image data */
    int            biXPelsPerMeter;  /* X pixels per meter */
    int            biYPelsPerMeter;  /* Y pixels per meter */
    unsigned int   biClrUsed;        /* Number of colors used */
    unsigned int   biClrImportant;   /* Number of important colors */
    } BITMAPINFOHEADER;

/*
 * Constants for the biCompression field...
 */

#  define BI_RGB       0             /* No compression - straight BGR data */
#  define BI_RLE8      1             /* 8-bit run-length compression */
#  define BI_RLE4      2             /* 4-bit run-length compression */
#  define BI_BITFIELDS 3             /* RGB bitmap with RGB masks */

typedef struct                       /**** Colormap entry structure ****/
    {
    unsigned char  rgbBlue;          /* Blue value */
    unsigned char  rgbGreen;         /* Green value */
    unsigned char  rgbRed;           /* Red value */
    unsigned char  rgbReserved;      /* Reserved */
    } RGBQUAD;

typedef struct                       /**** Bitmap information structure ****/
    {
    BITMAPINFOHEADER bmiHeader;      /* Image header */
    RGBQUAD          bmiColors[256]; /* Image colormap */
    } BITMAPINFO;
#  endif /* !WIN32 */

/*
 * Prototypes...
 */


But I have no idea whatsoever how to use it. I'm guessing I can use this to isolate the data part of the .bmp file and use the same negating method described above somehow?
Last edited on
Alas, you are going about it the wrong way. Colors are not necessarily stored in an image directly, nor are they inverted by simply flipping bits.

I suggest you first get and install a library called EasyBMP
http://easybmp.sourceforge.net/

This will allow you to read and write images using the Microsoft BMP file format.

What you need to do is read the image into memory, modify the colors of every pixel, and save the image. There is an example program called "ColorToGrey" which demonstrates all the important features of doing this. The only difference between the example and your program is how you wish to modify the color of the image.

To invert the color, simply subtract the original red, green, and blue components each from 1.0.

Hope this helps.
using a custom library is probably not the way I'm supposed to go about this but it certainly helps a great deal, especially the example program. Thank you very much, I'll se how I get on with this.
Topic archived. No new replies allowed.