调试器跳过整个if语句

时间:2017-07-19 18:01:52

标签: c bmp cs50

/**
 * 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;
}

我的问题更具体地说是代码的这一部分:

// 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);
            }

我正在尝试将bmp文件复制到另一个文件,但是除去任何红色并将其替换为白色(大部分代码不是我的工作,我应该编辑的预编写代码用于分配) ,复制功能有效,但由于某种原因检测到红色并用白色替换它不起作用。奇怪的是,当我使用调试器时,它完全跳过这部分代码:

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

这是我检测像素是否为红色的地方,调试器的行为就像线路不存在一样。

3 个答案:

答案 0 :(得分:3)

不要比较地址,您根本不需要使用int same变量。

if(red.rgbtRed == triple.rgbtRed && red.rgbtGreen != triple.rgbtRed && red.rgbtBlue != triple.rgbtRed) 
{
    fwrite(&white, sizeof(RGBTRIPLE), 1, outptr);
}
else
{
    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
}

答案 1 :(得分:2)

不要比较地址,它总会失败。比较这些值,因为这会导致简单的整数比较

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

答案 2 :(得分:2)

您的编译器可能会检测到这个&#34; if&#34; condition始终为false,并优化这些行。因此,这部分代码不存在于您的可执行文件中,而您的调试器也看不到它。