cfilecs50bmp

CS50 pset3 i want to resize this image, code is working for some and not for others? Can anyone tell me whats the problem?


i want to resize it by "n", it is working for some but not for others , i don't know why, it has no image specific code as per i know, it should work for every image

// Get multiplier
int n = atoi(argv[1]);

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

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi,biR;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
biR = bi;
// 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;
}

// modify bitmapinfoheader for height an width
biR.biWidth = bi.biWidth*n;
biR.biHeight = bi.biHeight*n;

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

// modify bisizeimage
biR.biSizeImage = (((biR.biWidth*(sizeof(RGBTRIPLE)))+outpadding)*abs(biR.biHeight));

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

// modify bfsize before writing it
bfR.bfSize = (sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(biR.biSizeImage));

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


// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for(int m = 0; m < n; m++)
    {
        // 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 < n; k++)
            {
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }

        }

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

        //go back to reprint the same line
       if(m != n-1)
        {
            fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE),SEEK_CUR);
        }   



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

Can anyone provide me with identifying the problem?I tried to look online but couldn't find any solution.website says "It looks like your post is mostly code; please add some more details", what i'm supposed to write more......


Solution

  • The problem was due to the way padding was implemented.