0

I compared my headers to the staff's, and they were the same, as well as use the "diff" command and they were the indeed the same. I have read other forums on this site, and I believe the problem lies with my padding calculation. Maybe I am adding too much or too little. Maybe a fresh of eyes can see the problem because I am clueless??

 // declare updated headers
 BITMAPFILEHEADER NewBf = bf;
 BITMAPINFOHEADER NewBi = bi;

 // update BITMAPINFOHEADER
 NewBi.biWidth = bi.biWidth * n;
 NewBi.biHeight = bi.biHeight * n;

 // determine padding for scanlines
 int NewPadding =  (4 - (NewBi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

 // update BITMAPINFOHEADER and BITMAPINFOHEADER
 NewBi.biSizeImage = (NewBi.biWidth * abs(NewBi.biHeight) * sizeof(RGBTRIPLE)) + (NewPadding * abs(NewBi.biHeight) * 1);
 NewBf.bfSize = sizeof(bf) + sizeof(bi) + NewBi.biSizeImage;

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

// write outfile's BITMAPINFOHEADER

fwrite(&NewBi, sizeof(BITMAPINFOHEADER), 1, outptr);

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // Array of pixels
    int scanline_size = NewBi.biWidth * sizeof(RGBTRIPLE);
    RGBTRIPLE scanline[scanline_size];
    int p = 0;

    // 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 triple to scanline array n times
        for (int k = 0 ; k < n; k++)
        {
            scanline[p] = triple;
            p++;
        }


        // write RGB triple to outfile
        //fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    }

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

    // write scanline array to outptr n times
    for (int k = 0; k < n; k++)
    {
        fwrite(&scanline, scanline_size, 1, outptr);
    }

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

} enter image description here enter image description here

0

only time it works is when the file requires no padding. check that your padding is added to the size calculations correctly and that you are adding it correctly to the scan lines imageSize = (width * sizeof(pixels) + padding) * height

NewBi.biSizeImage = (NewBi.biWidth * abs(NewBi.biHeight) * sizeof(RGBTRIPLE)) + (NewPadding * abs(NewBi.biHeight) * 1)

Edit this is correct your calculations are correct

you are writing the padding to the file after writing the scan lines

On a three by three from a one by one the file after headers should look like this

ffffff ffffff ffffff 000000
ffffff ffffff ffffff 000000
ffffff ffffff ffffff 000000

yours will look like this

ffffff ffffff ffffff
ffffff ffffff ffffff 
ffffff ffffff ffffff 000000

you could put the padding loop inside that scan line loop

6
  • What do you mean "not all that above"? Are you saying saying that in order to add the padding correctly to the scan lines I need to use the formula: imageSize = (width * sizeof(pixels) + padding) * height, instead of, NewBi.biSizeImage = (NewBi.biWidth * abs(NewBi.biHeight) * sizeof(RGBTRIPLE)) + (NewPadding * abs(NewBi.biHeight) * 1) ?? Aug 2 '15 at 2:09
  • yes, that is exactly what I am saying. as I said your current code only works with padding is zero
    – ebobtron
    Aug 2 '15 at 3:21
  • Sorry your calculation were correct just couldn't see it but the padding issue is still that it works when there is none. Modified the answer above.
    – ebobtron
    Aug 2 '15 at 3:46
  • Oh, alright, that's good to hear Aug 2 '15 at 4:10
  • Yess! It worked, it turns out that when when I added the padding loop inside the the scan line loop it added the padding to each scan line as it goes through. I assumed that based on the copy.c program that we were supposed to add the padding after all the scan lines were finished. Anyway, thanks for the help. Aug 2 '15 at 4:22

You must log in to answer this question.

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