0

I am having trouble to make my file resize vertically. My code is below

for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
    {
        //for each resize factor
        for (int b = 0; b < n ; b++)
        {
            // when b is 0, it means the file is writing the line for 1st time.
            // we want to avoid rewriting the first line again
            if (b != 0)
            {
                fseek(inptr, -(long int)(sizeof(RGBTRIPLE)*bi.biWidth*b+ new_padding), SEEK_CUR);
            }
            // for each 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
                // write extra pixel n times
                for (int a = 0; a < n; a++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }
            //skip over padding, if any
            //notice that, we are skipping over infile's padding
            fseek(inptr, old_padding, SEEK_CUR);
            //then add to the padding new padding 
            for (int k = 0; k < new_padding; k++)
            {
                fputc(0x00, outptr);
            }
        }
    }

Please point out any flaws in my logic, in the comments in my code. Also, anything wrong with my fseek() offset?

1

Hope your bi.biHeight and bi.biWidth are old height and old width.

Seems you know where your problem is but for whatever reason can not identify it. You do not even need to run debug50, you can debug it in your head.

With fseek, you want to move cursor back to the beginning of infile's scanline. Let's assume that you resize small.bmp by n = 4 to get large.bmp. In small.bmp's scanline, there are 3*3 bytes in image and 3 bytes in padding. In large.bmp, there are 12*3 bytes in image and 0 bytes in padding.

For how many bytes you want to move the cursor back for each infile's scanline? Does it depend on b?

3
  • Hi obesman. You have been helping me a lot. Following your logic, I removed b and moved back -(long int) (sizeof(RGBTRIPLE)*bi.biWidth). It works on smiley, but not on small, so I assuming padding is involved, cause smiley have no padding. Still don't know how to fix though. Can you help? – Ha Tran Jul 8 '17 at 12:06
  • Hi. OK, so you've answered to my second question - it doesn't depends on b so you've removed b. But what about my first question? "For how many bytes you want to move the cursor back for each infile's scanline?" Give the answer to yourself and to me, if you want. In bytes. And then compare to how you actually move it. – obesman Jul 8 '17 at 12:38
  • Can't upvote you twice. It is clear now, I really have to think in bytes. we will want to move back 3*3+3 ( sizeof(RGB), bi.biWidth and old padding. – Ha Tran Jul 8 '17 at 13:55

You must log in to answer this question.

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