Skip to main content
2 of 2
added 29 characters in body
ChrisG
  • 7.4k
  • 2
  • 17
  • 26

Padding, pset4, resize. weird xxd output

I have a padding problem in the resize (pset4). I can resize any image that doesnt need padding in their new version, but when it comes to padding it just doesn't work. I checked the headers with peek and they're not the problem. The program also calculates the values of oldpadding and newpadding correctly. xxd output for small.bmp resized by 2. (xxd output for small.bmp resized by 2.)It also has a weird xxd output. As you see above, it shows 8 pixels as the wiidth of the image despite it says 6 when i peek in the bitmapinfoheader. I would appreciate every little tip. Because this problem is driving me crazy.

//change the size

LONG cbiwidth = bi.biWidth*n;

// determine padding for scanlines
int newpadding =  (4 - (cbiwidth * sizeof(RGBTRIPLE)) % 4) % 4;
int oldpadding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;    
LONG cbiheight = bi.biHeight*n;

DWORD cbisizeimage = abs(cbiheight)* (abs(cbiwidth)*sizeof(RGBTRIPLE) + newpadding) ;

DWORD cbfsize = cbisizeimage +sizeof(bf)+ sizeof(bi);    

// write outfile's BITMAPFILEHEADER
fwrite(&bf.bfType, sizeof(WORD), 1, outptr);
fwrite(&cbfsize, sizeof(DWORD),1,outptr);
fwrite(&bf.bfReserved1, sizeof(WORD)*2 + sizeof(DWORD),1,outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(DWORD),1,outptr);
fwrite(&cbiwidth, sizeof(LONG),1,outptr);
fwrite(&cbiheight, sizeof(LONG),1,outptr);
fwrite(&bi.biPlanes, sizeof(WORD)*2 + sizeof(DWORD)*1,1,outptr);
fwrite(&cbisizeimage, sizeof(DWORD),1, outptr);
fwrite(&bi.biXPelsPerMeter, sizeof(LONG)*2 +sizeof(DWORD)*2, 1, outptr);

fseek(inptr,sizeof(BITMAPINFOHEADER) +sizeof(BITMAPFILEHEADER),SEEK_SET);


// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
     for(int h=0; h < n; h++)
     {
            fseek(inptr,(-(sizeof(RGBTRIPLE)*(bi.biWidth)+oldpadding))*i, 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 l = 0; l < n ; l++)
                {
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }
            }
                        
        // skip over padding, if any
        fseek(inptr, oldpadding, SEEK_CUR);

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