I've started out this problem as bad as it gets :( trying very hard to understand bitmab files and how they work (especially padding) for a few days(as I'm not a native English speaker). now that I can kind of understand how they work' I started taking the walkthrough step by step, so I updated the file headers and kept a copy of the old one, now I am trying to resize horizontally only first, when I try it on smiley.bmb this is what I get:


this i guess is right but I don't know about the padding under the image

how ever when I try it on small.bmb this is what I get:

enter image description here

it seems very mixed up.

here is my code for doing this

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

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi , old_bi;
    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 6;

    // update width and height
    bi.biWidth *= n;
    bi.biHeight *= n;

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

    bi.biSizeImage = ((sizeof(RGBTRIPLE) * bi.biWidth) + padding) *abs(bi.biHeight);
    bf.bfSize = bi.biSizeImage + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPINFOHEADER);

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

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi, 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 f = 0; f < n; f++)
                // write RGB triple to outfile
                fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
            // write outfile padding
            for (int k = 0; k < padding; k++)
            fputc(0x00, outptr);

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


    // close infile

    // close outfile

    // success
    return 0;

any tips or help is appreciated, thanks


So, where do you set the contents of old_bi?

Tip: get everything working for n=1 - first the header and then the image. Then do n=2, get the header right and then the image. ;-)

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

| improve this answer | |
  • Oh thanks that opens my eyes to the header files again and I seem to be not memorizing the old width and height, thank you very much – Yasser Altamimi Mar 6 '17 at 7:33
  • Just in case you missed it, you can copy entire structures with a simple a=b; statement. It's only required that both structures are of the exact same type. – Cliff B Mar 6 '17 at 8:24
  • Ya this is exactly what I did but now I got into a run time error regarding fseek (unsigned integer overflow), this is really what will get me stuck :(( – Yasser Altamimi Mar 6 '17 at 8:27

You must log in to answer this question.

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