0

I have spent lots of time reading to find someone with alike problem, but simply couldn't. Would be so much thankful for any hint! My resize is acting weird, despite the varriables store correct values, the bmp opens in some cases with 1 pixel less than needed.(it works for num = 1,4,5,7,8 for instance though doesnt resize correctly....I cannot find the problem at least with the size of bmp). Edited: The staff's program implementation is acting the same on my IDE...Here is the link: https://ide.cs50.io/weatherwax_esme/ide50#openfile-x Should I report a bug?

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

#include "bmp.h"

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

// remember filenames
int num = atoi(argv[1]);
char* infile = argv[2];
char* outfile = argv[3];


// open input file 
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("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;
}
BITMAPFILEHEADER bf_upd = bf;
BITMAPINFOHEADER bi_upd = bi;

bi_upd.biWidth = bi.biWidth * num;
bi_upd.biHeight = bi.biHeight * num;

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


bi_upd.biSizeImage = (bi_upd.biWidth*sizeof(RGBTRIPLE) + padding_upd) * abs(bi_upd.biHeight);
bf_upd.bfSize = bi_upd.biSizeImage + sizeof(bi_upd) + sizeof(bf_upd);

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

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


// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // print num times the line
    for (int s = 0; s < num; s++)    
    {
        // 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);

            // write RGB triple to outfile
            for (int k = 0; k < num; k++)
            {
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }

        // add padding
        for (int z = 0; z < padding_upd; z++)
        {
            fputc(0x00, outptr);
        }

        // Return to beginning of line in inptr
        fseek(inptr, -bi.biWidth*sizeof(RGBTRIPLE), SEEK_CUR);
    }
    //Go forward to end of line
    fseek(inptr, bi.biWidth*sizeof(RGBTRIPLE), SEEK_CUR);

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

} ` Thanks so much if you could help me to find this mistake!

5
  • Interesting thing. I plugged your code into mine and it worked fine. Is it possible that your error appears earlier in your code, or that you are running a different version of the code than you are editing?
    – Cliff B
    Apr 10 '16 at 5:25
  • Thanks a lot for your answer! I cannot understand what the problem is, it doesnt work still at the num = 2, 3, 6, 9! The code before is just simple copy wih slight changes of copy.c program. I will add it now to question.
    – Valeriya
    Apr 10 '16 at 9:51
  • And no, I am running the same version of code, I double checked! I think that maybe the problem is with padding, but it seems OK for me. Cannot see what the problem is at all!
    – Valeriya
    Apr 10 '16 at 9:57
  • Also the problem is the program PASSES all tests! And neverthe less I see this for 6x6 bmp (W: 5, H: 5): ide.cs50.io/weatherwax_esme/ide50#openfile-x
    – Valeriya
    Apr 10 '16 at 10:12
  • The weird thing is that the staff's program is acting the same on my IDE)))
    – Valeriya
    Apr 10 '16 at 10:18
0

The answer found! Thanks so much for your help! The problem is that under Google Chrome 49.0.2623.112 m version on Windows7 IDE does not behave well. The staff version also behave crazy(screens attached). IDE saw 5x5 pixels where it ought to be 6x6. It was solved with using IE (OMG!). I have spent HOURS to draw this lesson - sometimes you should consider it's not your bug and start searching somewhere else. :))enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .