Debugger skipping a whole if statement

Viewed 75
/**
 * Copies a BMP piece by piece, just because.
 * All we have to do is change all the red pixels to 


 */

#include <stdio.h>
#include <stdlib.h>

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 3)
    {
        fprintf(stderr, "Usage: ./copy infile outfile\n");
        return 1;
    }

    // remember filenames
    char *infile = argv[1];
    char *outfile = argv[2];

    // open input file 
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    // write outfile's BITMAPFILEHEADER
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

    // determine padding for scanlines
    int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // iterate over infile's scanlines
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        // iterate over pixels in scanline
        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            RGBTRIPLE red = {0, 0, 255};
            int same = 0; 
            RGBTRIPLE white = {255, 255, 255};

            if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
            {
                same = 1; 
            }

            if(same == 0) //copy the pixel, if the pixel isn't red
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);    
            }
            if(same == 1)
            {
                fwrite(&white, sizeof(RGBTRIPLE), 1, outptr);
            }

        }

        // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);

        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
    }

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

My issue is more specifically with this one segment of the code:

// temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            RGBTRIPLE red = {0, 0, 255};
            int same = 0; 
            RGBTRIPLE white = {255, 255, 255};

            if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
            {
                same = 1; 
            }

            if(same == 0) //copy the pixel, if the pixel isn't red
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);    
            }
            if(same == 1)
            {
                fwrite(&white, sizeof(RGBTRIPLE), 1, outptr);
            }

I am trying to copy over a bmp file to another file, but get rid of any red and replace it with white (most of this code is not my work, its prewritten code that I am supposed to edit for the assignment), The copy function works, but for some reason detecting red and replacing it with white is not working. Oddly when I use the debugger, it completely skips over this part of the code:

 if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
        {
            same = 1; 
        }

which is where I am detecting if the pixel is red, the debugger acts as if the line doesn't exist.

3 Answers
Related