С++, rotate bmp file

88 Views Asked by At

I need to rotate a bmp file exactly with 180 degrees. (If possible, do not change anything except the RotateImage function.)

At the moment, from this picture:

Input image

I get this:

Output image

What is the problem and how to fix it?

Code:

void RotateImage(tagRGBQUAD* RGB, int biSize, FILE* f) {
    int width = 404; // Image width
    int height = 404; // Image height
    fseek(f, 1078, SEEK_SET); // Offset of bitmap data from the header in bytes

    // Create a temporary array to store the pixels
    tagRGBQUAD* temp = new tagRGBQUAD[width * height];

    // Read the image pixels into the temporary array
    fread(temp, sizeof(tagRGBQUAD), width * height, f);

    // Rotate the image 180 degrees without distortion
    for (int y = 0; y < height / 2; y++) {
        for (int x = 0; x < width; x++) {
            int index1 = (y * width + x);
            int index2 = ((height - 1) - y) * width + x;

            // Swap pixels
            tagRGBQUAD tempPixel = temp[index1];
            temp[index1] = temp[index2];
            temp[index2] = tempPixel;
        }
    }

    // Write the modified pixels back to the file
    fseek(f, 1078, SEEK_SET);
    fwrite(temp, sizeof(tagRGBQUAD), width * height, f);

    delete[] temp;
}


int main(int argc, char* argv[])
{
BitMapHeader header;
tagRGBQUAD RGBQuad[256];
BYTE c;
FILE* fb;
char filename[] = "sqw.bmp";
int i;
if (!(fb = fopen(filename, "r+b"))) { printf("file not open"); return 1; }

    header = ReadHeader(fb);
    
    RotateImage(RGBQuad, header.biSize + 14, fb);
    
    fclose(fb);
    return 0;

}

(most of the code had to be trimmed so that stackoverflow would not give an error)

0

There are 0 best solutions below