I think I have the headers correct except for the new bf.Size not to sure on that one. I am having real problems with resizing the bitmap and I can't figure out what it is, any hints or pointers in the right direction would be very appreciated.

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

bfc = bf;
bic = bi;

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

bic.biSizeImage = ((bi.biWidth * n) * sizeof(RGBTRIPLE) + newpadding  ) * (abs(bi.biHeight * n));
 bfc.bfSize = bic.biSizeImage + 54;
bic.biHeight = (bi.biHeight * n);
bic.biWidth = (bi.biWidth * n);

// write outfile's BITMAPINFOHEADER

fwrite(&bic, sizeof(BITMAPINFOHEADER), n, outptr);

// write outfile's BITMAPFILEHEADER

fwrite(&bfc, sizeof(BITMAPFILEHEADER), n, 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);

        // write RGB triple to outfile
        for (int l = 0; l < n; l++)
        if (l > 0)
        fseek(outptr, sizeof(RGBTRIPLE)* l, SEEK_CUR);
        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 < newpadding; k++)
        fputc(0x00, outptr);


1 Answer 1


There are a number of issues that need to be fixed. Since the philosophy of this forum is to guide people to solutions and not just to fix code, I'll point you at two subtle but major issues that will help you with the rest.

First, your generated bitmap files are not being recognized as bitmaps. Very simply, when you write out the BITMAPFILEHEADER and the BITMAPINFOHEADER, you are writing the wrong one out first. This is corrupting the header and preventing you from using the peek tool. You could still look at the header with xxd using just the filename as an input parameter.

Second, when you write the headers out, how many copies should you write out? How many copies are you actually writing out with fwrite(&bic, sizeof(BITMAPINFOHEADER), n, outptr);? When I did this with your code, it caused a segmentation fault.

Once you fix these two issues, you should be able to use the xxd and peek tools to troubleshoot the rest of the issues, starting by comparing the results of your program with the results of the staff program, using the same inputs and scaling factor. ( xxd -? will list the usage and all of the control parameters.)

One more hint: When you run your program and generate a file 3 or more times larger than the original, look at the size of the input file and the output file. The size of these files should be roughly different by the scaling factor. If they're roughly the same size for a scaling factor of 3 or more, or if the output file size is significantly different than the input file size times the scaling factor, then something is really off.

If you get to a point where you aren't making any more progress, and have looked at other questions on the subject (most problems have been dealt with in this forum), and have reviewed the pset4 assignment (it's a great roadmap) and the week's videos, feel free to post a new question with a description of the problem, what you've done, and what you think is the problem, along with the relevant part of the code, and we'll help you move forward.

If this answers your question, please accept the answer to remove the question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

  • thanks, first two issues fixed!! Sorry about the horrible post on the forum, was super fried and needed another set of eyes as you can see by the "n" in fwrite(&bic, sizeof(BITMAPINFOHEADER), n, outptr);
    – JV305
    Jun 7, 2015 at 4:05
  • That's OK! When you get to where you can't see the forest for the trees, that's when it's time to get another set of eyes on it! Or to walk away from it for a day or two. ;-) It helps everyone, though, if you include more details on what you think is broken, what it isn't doing, what it is doing wrong, and what you've done to try to find and fix the problems. Not so horrible, just trying to give you some guidance on how to do it better next time.
    – Cliff B
    Jun 7, 2015 at 5:39

You must log in to answer this question.

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