This pset has been super hard for me. I am trying to resize horizontally before I tackle vertical and I am able to compile and run the program but when I go to open the output file I get the error message "Invalid or Unsupported Image Format."

I've backed out of my changes from copy.c and the problem seems to be when I change the fwrite from &bf to &bfresize and &bi to &biresize. I see several others have had the same problem but my code already includes one of the recommended fixes in one of them and doesn't include what I think was problematic in the others. I've read through them all and I still don't see it.

I was able to get the image to open yesterday and I can't remember what I changed since then that might be causing this problem. I've moved some things around, did I accidentally delete a line of code? I'm sure it's something stupid but I've looked at it over and over and I just don't see it. Help would be much appreciated.

// Copies a BMP file

#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 factor small large\n");
    return 1;

// remember filenames
char *small = argv[2];
char *larger = argv[3];
int n = atoi(argv[1]);

// open input file
FILE *inptr = fopen(small, "r");
if (inptr == NULL)
    fprintf(stderr, "Could not open %s.\n", small);
    return 2;

// open output file
FILE *outptr = fopen(larger, "w");
if (outptr == NULL)
    fprintf(stderr, "Could not create %s.\n", larger);
    return 3;

//Ensure argv[1] is a number between 1 and 100
if (n < 0 || n > 100)
    fprintf(stderr, "Choose a resize factor between 1 and 100.\n");
    return 4;

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

// read infile's BITMAPINFOHEADER
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)
    fprintf(stderr, "Unsupported file format.\n");
    return 4;


biresize.biWidth = bi.biWidth * n;
biresize.biHeight = bi.biHeight * n;

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

biresize.biSizeImage = ((sizeof(RGBTRIPLE) * biresize.biWidth) + paddingresize) * abs(biresize.biHeight);
bfresize.bfSize = biresize.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

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

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

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

        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, padding, SEEK_CUR);

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

// close infile

// close outfile

// success
return 0;
  • What are you asking? Except for vertical scaling, the code appears to be working.
    – Cliff B
    Mar 21, 2018 at 21:39
  • When I try to open the output file it won't open. I get a message that says "Invalid or Unsupported Image Format."
    – CM23
    Mar 21, 2018 at 21:51
  • Did you save the changed file (above) and remake it before running your code? Are you sure that you're editing, compiling and running the same/correct file? I have no trouble opening the output files.
    – Cliff B
    Mar 21, 2018 at 22:07
  • Thanks Cliff. Yes, I recompiled and executed it multiple times. I tried it again this morning and I'm still getting the same error message. I'll probably delete it and start over since I didn't get very far. Thanks again for replying. :)
    – CM23
    Mar 22, 2018 at 16:07
  • Hi Cliff. Is there anyone I can talk to? I tried running update50, completely deleting the resize folder and re-downloading the zip file, rebuilding everything from scratch. I'm not sure what the problem is and I'm completely blocked until I can get it fixed. There is a note in resize under the heading "Testing" that if you want to test you can run ./resize 4 small.bmp student.bmp and I get the same "Invalid image format" error using that command on the "student.bmp" file.
    – CM23
    Mar 23, 2018 at 20:51

1 Answer 1


Went back to the walkthrough and went through my code line by line with Zamyla. I think I see it (I was right, it is something really really obvious and called out specifically in the walkthrough). It's working on smiley (stretches smiley out). Doesn't seem to stretch small so much as force the pixels to run into the next set of pixels (I now have red and blue pixels instead of just green and white) but I am back to being hopeful.

You must log in to answer this question.

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