pset4 resize issues I am now struggling with risize.c. Most time it gives me "invalid or unsupported image format" error. It seems that it only works with ./resize 1 small.bmp large.bmp, but even not ./resize 1 smiley.bmp large.bmp (isn't this weird?) This is what I have tried to do: open input file; read its width and height; calculate output file's width. height, padding, file size as well as image size; write these into the output file's head; then I have four layers of for-loop: layer 1, for each pixel, write n times; layer 2, do this for every pixel in a line and take care of padding at the end of the line; layer 3, repeat this for n times to expand in y axis; layer 4, repeat layer 1-3 for every line in the input file.

I am kind of suspecting that the problem is with the head file part: maybe what the updated information on width, height etc is not read into the output file?

Here is the major part of my code (head part and loop part):

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

// read infile's BITMAPINFOHEADER
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

int n=atoi(argv[1]);

int old_width=bi.biWidth;
int old_height=abs(bi.biHeight);
int new_width=old_width*n;
int new_height=abs(old_height)*n;

// determine padding for scanlines before resize
int old_padding =  (4 - (old_width * sizeof(RGBTRIPLE)) % 4) % 4;

//determine padding after resize
int new_padding= (4 - (new_width * sizeof(RGBTRIPLE)) % 4) % 4;

// write outfile's BITMAPFILEHEADER
bf.bfSize =new_width*new_height*3+new_padding*new_height+54;
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
bi.biWidth = new_width;
bi.biHeight = new_height;
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(old_height); i < biHeight; i++)

    //iterate n time of each scanline
    for (int j=0; j<n; j++)
        // iterate over pixels in scanline
        for (int k = 0; k < old_width; k++)
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            //write each pixel n times in a row
            for(int l=0; l<n; l++)
               // write RGB triple to outfile

               fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);


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

        // then add it back (to demonstrate how)
        for (int m = 0; m < new_padding; m++)
            fputc(0x00, outptr);


  • You posted a link to your cloud 9 workspace, which requires permission. Can you please edit your question and post it there, or post it somewhere that it can be accessed and update the link here? Thanks.
    – Cliff B
    Aug 6, 2016 at 6:29
  • Thanks. I also realized this issue. Now I copied the code here.
    – zliu
    Aug 6, 2016 at 6:35

1 Answer 1


You definitely have a header problem. I could tell you, but what fun would that be? ;-) You need to very carefully review which fields the spec says to update and which fields you actually updated.

After that, an after looking at your code, I'd say that if there are any problems, you should be able to track them down.

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

  • Thank you so much. Indeed, after carefully reading the description on the bmp header, I fixed two errors and now the image could be opened. However, there is still one issue left: the enlarged image is not exactly enlarged. Once I solve it, I will click the check mark. Thanks again for spending time reading my code. I really appreciate it!!!
    – zliu
    Aug 6, 2016 at 13:19
  • After spending another two or three hours and doing some research online, I finally solved the problem in the loop: I didn't know that I need to reset the cursor to the beginning of the line in order to expand the lines vertically. Now the code works perfectly. Thank you so much!
    – zliu
    Aug 6, 2016 at 16:16

You must log in to answer this question.

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