1

I am struggling with this program. I cannot even check the headers with peek (I am receiving the message "could not recognize sample.bmp"). I am sure my program has several issues, but I would like to know first if I am updating my headers correctly. Then, is the loop right? thank you for you time.

I changed sizeimage : new_bi.biSizeImage = (new_width * sizeofRGBTRiple + newpadding) * abs(new height), but I have the same problem. I double check the new variables (paper and pencil) with the following example: original figure: 3 x 3, factor: 4; so, new width = 12, sizeofRGB= 3, no padding (because the width is 12), new height (abs)= 12 , the final calculation for new Sizeimage: (12 x 3 + 0) * 12 = 432, then new bf = 54 + 432 = 486. I think these results are correct; therefore, the formulas are also correct?

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

#include "bmp.h"

int main(int argc, char* argv[])

{
    // ensure proper usage
    if (argc != 4)

    {
        printf("Usage: ./copy infile outfile\n");
        return 1;
    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    }

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

    int n = atoi(argv[1]);
    if (n > 100 || n < 1)
    {    
        printf("n must be between 1 and 100");
    }

    // open input file 
    FILE* inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        printf("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;
    }

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

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

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

    // update header
    BITMAPFILEHEADER new_bf = bf;   
    BITMAPINFOHEADER new_bi = bi;     

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

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

    new_bi.biSizeImage = (new_bi.biWidth * sizeof(RGBTRIPLE) + new_padding) * abs(new_bi.biHeight);
    new_bf.bfSize = sizeof(bf) + new_bi.biSizeImage;    

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

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

    // iterate over infile's scanlines

(This was only the first part)

3
  • maybe this question will help you cs50.stackexchange.com/questions/8978/resizes-padding-issue/…
    – wallek876
    Jul 17 '15 at 18:53
  • I changed sizeimage. new_bi.biSizeImage = (new_width * sizeofRGBTRiple + newpadding) * abs(new height), but I have the same problem. I double check the new variables (paper and pencil) with the following example: original figure: 3 x 3, factor: 4; so, new width = 12, sizeofRGB= 3, no padding (because the width is 12), new height (abs)= 12 , the final calculation for new Sizeimage: (12 x 3 + 0) * 12 = 432, then new bf = 54 + 432 = 486. I think these results are correct; therefore, the formulas are also correct?
    – abc2015
    Jul 18 '15 at 5:24
  • What you just posted in the comment is correct, but is not the same you have in your code for bfsize, it would be new_bf.bfSize = sizeof(bf) + sizeof(bi) + new_bi.biSizeImage
    – wallek876
    Jul 18 '15 at 11:14
2

Your problem is with the new_bf.bfSize. It holds the size of the whole bmp file, not just the headers and the image as you imply, but also the offset from the beginning of the BITMAPFILEHEADER structure to the bitmap bits (per the official microsoft's documentation here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd183374(v=vs.85).aspx).
Instead of

new_bf.bfSize = sizeof(bf) + new_bi.biSizeImage;

try

new_bf.bfSize = new_bf.bfOffBits + new_bi.biSizeImage;
1
  • Thank you guys. I finished the program. The problem was the declaration of new_bi and new_bf. I did not initialize the variables. Your feedback as helpful.
    – abc2015
    Jul 21 '15 at 3:52

You must log in to answer this question.

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