0

enter image description here

 int initWidth = bi.biWidth;
    int initHeight = bi.biHeight;
    int initpadding = (4 - (initWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    bi.biWidth*=f;
    bi.biHeight*=f;
    // determine padding for scanlines
    int finpadding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    bi.biSizeImage=((sizeof(RGBTRIPLE)*bi.biWidth)+finpadding)*abs(bi.biHeight);
    bf.bfSize=bi.biSizeImage+sizeof(BITMAPFILEHEADER)+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;i < abs(initHeight); i++)
    {   RGBTRIPLE triple;
        for(int a=0;a<f-1;a++)
        {       
                // iterate over pixels in scanline
                for (int j = 0; j < initWidth; j++)
                {

                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE),f,outptr);
                }
                // then add it back (to demonstrate how)
                for (int k = 0; k < finpadding; k++)
                {
                    fputc(0x00, outptr);
                }
                  // skip over padding, if any
                fseek(inptr, -(initWidth*3), SEEK_CUR);
        }
         for (int l= 0; l < initWidth; l++)
                {

                    // read RGB triple from infile
                    fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE),f,outptr);
                }
        for (int k = 0; k < finpadding; k++)
                {
                    fputc(0x00, outptr);
                }
                  // skip over padding, if any
        fseek(inptr,initpadding, SEEK_CUR);
    }
1

The problem is with the fwrites. This

fwrite(&triple, sizeof(RGBTRIPLE),f,outptr);

does not write the same triple f times.

It writes sizeof(RGBTRIPLE) * f bytes starting at &triple. For a factor of 3, it will write 9 bytes starting at the address of triple. The fread read only 3 bytes (sizeof(RBGTRIPLE)) to the address of triple, so the last 6 bytes are "junk".

Under the hood, fwrite calculates the total number of bytes (in this case sizeof(RGBTRIPLE) * f) and then gets that many bytes from the input pointer (&triple) and writes them to the ouptut pointer (outptr).

Since program needs to write each &triple f times, a loop is called for.

3
  • triple is an object of the structure RGBtriple. For each pixel that I iterate in the bmp, I write pixel f times through "fwrite(&triple, sizeof(RGBTRIPLE),f,outptr);" . I can't understand the difference between writing sizeof(RGBTRIPLE) * f bytes and &triple f times. Isn't both identical? May 6 '17 at 13:00
  • They are not identical. Answer edited. May 6 '17 at 13:49
  • Thanks a lot. @DinoCoderSaurus The correction suggested by you worked. If possible, help me with this segmentation fault too: cs50.stackexchange.com/questions/24784/… May 6 '17 at 13:52

You must log in to answer this question.

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