0

Can anybody tell me what's wrong the link for check50 mistake is https://sandbox.cs50.net/checks/28278d1be858417299f79aab9879e519

and infile is smiley.bmp

and outfile is enter image description here

when n was 2.

/**
* Copies a BMP piece by piece, just because.
*/

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

#include "bmp.h"

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

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

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

int fctr = atoi(argv[1]);

if (fctr <= 0 || fctr > 100)
{
    fprintf(stderr, "positive integer must be between 0 and 100");
    return 4;
}
// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf, bf_out;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
bf_out = bf;

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

// Updating out file width and height
bi_out.biWidth = bi.biWidth * fctr;
bi_out.biHeight = bi.biHeight * fctr;

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

// Updating out file size
bi_out.biSizeImage = ((sizeof(RGBTRIPLE) * bi_out.biWidth) + padding_out);
bf_out.bfSize = bi_out.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

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

            for(int m = 0; m < fctr; m++) 
            {
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            }
        }
        // skip over padding, if any
        fseek(inptr, padding, SEEK_CUR);

        // then add it back
        for (int k = 0; k < padding_out; k++)
        {
            fputc(0x00, outptr);
        }
        fseek(inptr, -(bi.biWidth * 3 + padding ), SEEK_CUR);
    }
    fseek(inptr, bi.biWidth * 3 + padding, SEEK_CUR);
}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// success
return 0;
}
3
  • So check50 says you're all resizing wrong (but does not provide much information). With only that information, probably nobody can tell you. Maybe show your source code, could be for example in a github gist or something similar, or here on the site (in this case, use the {} button in the editor). – Blauelf May 5 '17 at 11:55
  • Did you verify that the headers are being processed correctly with the peek tool and comparing to the output of the staff solution? – Cliff B May 5 '17 at 16:14
  • Link for my code is: github.com/submit50/rahulatrkm/blob/… – Rahul May 5 '17 at 17:47
1

There are several issues going on here. I'll cover some to steer you in the right direction and let you have a chance to sort out the rest on your own.

First, there's an infinite loop that causes the output file to exceed available space.

    for (int l = 0; l <out_padding; k++)

Since l is not being incremented, it's an infinite loop. I assume this is just a cut and paste typo.

Next, look at this code:

bi.biHeight *= fctr;
...
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)

This for loop controls traversal through the input file, but is being controlled by bi.biHeight, which has already been scaled up. The size of the output file can't be used to control the input file.

Finally, when scaling horizontally, the code needs to read each RGBTRIPLE once and then output it fctr times. The code above reads and writes one for one RGBTRIPLE, so it's running through the pixels without replicating them. It would eventually run past the end of the input file.

Because of the last issue, the code rewrite may or may not fix any remaining issues. But this will get you going again.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Sorry can you see my new edited code as I had changed something – Rahul May 13 '17 at 10:53

You must log in to answer this question.

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