bitmap? Need help!

I have a complete bitmap code. But I don't understand what code is doing what. I need to figure out what each part of the program is actually doing. Can anyone help me? Here is the code:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

// In-class exercises arrays //24 bit only
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include "stdint.h"

using namespace std;

// If on Windows, use this to pack structures
#ifdef _WIN32
#pragma pack(2)
#endif

// structures related to bitmaps
typedef struct tagBITMAPFILEHEADER
{
    uint16_t bfType;  //specifies the file type
    uint32_t bfSize;  //specifies the size in bytes of the bitmap file
    uint16_t bfReserved1;  //reserved; must be 0
    uint16_t bfReserved2;  //reserved; must be 0
    uint32_t bOffBits;  //species the offset in bytes from the bitmapfileheader to the bitmap bits
}  BITMAPFILEHEADER;


typedef struct tagBITMAPINFOHEADER {
    uint32_t biSize;
    int32_t  biWidth;
    int32_t  biHeight;
    int16_t biPlanes;
    int16_t  biBitCount;
    uint32_t biCompression;
    uint32_t biSizeImage;
    int32_t  biXPelsPerMeter;
    int32_t  biYPelsPerMeter;
    uint32_t biClrUsed;
    uint32_t biClrImportant;
}  BITMAPINFOHEADER, *PBITMAPINFOHEADER;

typedef struct tagRGBPixel {
    uint8_t red;
    uint8_t green;  // Can be flipped. BGR istead of RGB. 
    uint8_t blue;
} RGBPixel;


static char const *const OUTPUT_FILE = "/H:/Profile/February/april22/modified.bmp";

static char const *const INPUT_FILE = "/H:/Profile/February/april22/interlocking_uh_flat.bmp";

uint8_t *bmpToRgbBuffer(uint8_t  *bitmapBuffer, int32_t width, int32_t height);
void rgbToBmpBuffer(uint8_t *bitmapBuffer, uint8_t *destArray, int32_t width, int32_t height);


/**
* Main entry point
*/
int main ()
{
    BITMAPFILEHEADER fileHeader = {0};
    BITMAPINFOHEADER infoHeader = {0};
    ifstream file;
    file.open(INPUT_FILE, ios::binary);
    if (file.fail()){
        cout << "Failed to open file" << endl;
        exit(0);
    }


    file.read((char *)&fileHeader,sizeof(BITMAPFILEHEADER));
    file.read((char *)&infoHeader,sizeof(BITMAPINFOHEADER));

    if(infoHeader.biBitCount != 24){
        cout << "This file has " << infoHeader.biBitCount << " bits per pixel." << endl;
        cout << "Can't handle this file. Sorry." << endl;
        exit(1);
    }

    int rows = abs(infoHeader.biHeight);
    int columns = infoHeader.biWidth;

    long size = fileHeader.bfSize - fileHeader.bOffBits; //Why?


    unsigned char* bitmapArray = new unsigned char[size];


    file.seekg(fileHeader.bOffBits);
    file.read((char *)bitmapArray, size);
    file.close();

    RGBPixel* rgbBuffer = (RGBPixel*) bmpToRgbBuffer(bitmapArray,infoHeader.biWidth, infoHeader.biHeight);
    


    // Manipulate image data here //The 3 stands for the number of bytes in one RGBPixel
    for(int n = 0; n < size/3; n++){   //change colors here! In this loop. Dont change loop.

    }

    // Convert back from RGB to BGR format to save the file
    rgbToBmpBuffer((uint8_t *) rgbBuffer, bitmapArray, infoHeader.biWidth, infoHeader.biHeight);

    // now write out the results
    ofstream outfile;
    // write file header
    outfile.open(OUTPUT_FILE, ios::binary);

    if (outfile.fail()){
        cout << "Cannot open output file" << endl;
        exit(1);
    }
    outfile.write((char *)&fileHeader, sizeof(BITMAPFILEHEADER));

    outfile.write((char *) &infoHeader, sizeof(BITMAPINFOHEADER));

    outfile.write((char *)bitmapArray, infoHeader.biSizeImage);
    
    outfile.close();

    delete[] bitmapArray;

}

uint8_t *bmpToRgbBuffer(uint8_t *bitmapBuffer, int32_t width, int32_t height) {

    int padding_bytes = 0;
    int scanline_bytes = width * sizeof(RGBPixel);

    uint8_t * new_buffer = new uint8_t[scanline_bytes * height];

    while((scanline_bytes + padding_bytes) % 4 != 0)
        padding_bytes++;
    cout << "Each scanline has " << padding_bytes << " padding bytes at the end" << endl;

    int bytes_per_line = scanline_bytes + padding_bytes;

    long buffer_position = 0;
    long new_position = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < scanline_bytes; x+=3) {
            new_position = y * scanline_bytes + x;
            buffer_position = (height - y - 1) * bytes_per_line + x;
            new_buffer[new_position] = bitmapBuffer[buffer_position + 2];
            new_buffer[new_position + 1] = bitmapBuffer[buffer_position + 1];
            new_buffer[new_position + 2] = bitmapBuffer[buffer_position];

        }
    }
    return new_buffer;
}
void rgbToBmpBuffer(uint8_t *bitmapBuffer, uint8_t *destArray, int32_t width, int32_t height) {

    int padding_bytes = 0;
    int scanline_bytes = width * sizeof(RGBPixel);

    uint8_t * new_buffer = new uint8_t[scanline_bytes * height];

    while((scanline_bytes + padding_bytes) % 4 != 0)
        padding_bytes++; //Why divisible by 4?
    cout << "Each scanline has " << padding_bytes << " padding bytes at the end" << endl;

    int bytes_per_line = scanline_bytes + padding_bytes;


    memset(destArray,0,height * bytes_per_line); //lookup what memset is.

    long buffer_position = 0;
    long new_position = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < scanline_bytes; x+=3) {
            new_position = y * scanline_bytes + x;
            buffer_position = (height - y - 1) * bytes_per_line + x;
            destArray[new_position] = bitmapBuffer[buffer_position + 2];
            destArray[new_position + 1] = bitmapBuffer[buffer_position + 1];
            destArray[new_position + 2] = bitmapBuffer[buffer_position];

        }
    }

}

Last edited on
Help! I am so lost...
Topic archived. No new replies allowed.