0

I am having a problem with fseek() to move the cursor back for copying line

My code is below:

int old_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    bi.biWidth *=  n;
    bi.biHeight *= n;
    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(BITMAPFILEHEADER);
    printf("Old padding: %i, new padding %i\n", old_padding, padding);
    // write outfile's BITMAPINFOHEADER
    fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
    fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
    // write outfile's BITMAPFILEHEADER



    // iterate over infile's scanlines
    // avoid padding by using biWidth
    for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        for (int b = 0; b <= n ; b++)
        {
            //when b is 0, it means the file is writing the line for 1st time.
            if (b>0)
            {
                fseek(inptr, -(long int)(sizeof(RGBTRIPLE)*bi.biWidth), SEEK_CUR);
            }
            // 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 a = 0; a <= n; a++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }

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

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

I'm guessing there is something wrong with my offset for fseek. Can anyone point me to the right direction?

1

Not only with fseek. In the beginning you redefine bi.biWidth and bi.biHeight - now its new width and new height. But when you iterate over scanlines and pixels, you want to do it over infile's ones, that is old bi.biWidth and old bi.biHeight.

You must log in to answer this question.

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