Debugger skips whole if statement
/**
* 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 BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// read infile 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 BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// determine padding for scanlines
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
// iterate over infile 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 problem is more specific with this one segment of 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 a BMP file to another file, but get rid of any red and replace it with white (most of this code is not my job, its pre-written code that I have to edit for the job). The copy function works, but for what For this reason, detecting red and replacing it with white does not work. Oddly enough, when I use the debugger, it completely skips this part of the code:
if(&red.rgbtRed == &triple.rgbtRed && &red.rgbtGreen != &triple.rgbtRed && &red.rgbtBlue != &triple.rgbtRed)
{
same = 1;
}
where i find the pixel is red, the debugger acts as if the line doesn't exist.
+3
source to share
3 answers