1

I am having a problem with resize. I can resize smiley.bmp perfectly at X1, X2, and X10, but not for factors between 2 and 10--I just get an abstract blob of colors. I assume this has something to do with how my program is handling padding, but I am banging my head to figure out what I am doing wrong.

// read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
DWORD size = bf.bfSize - bf.bfOffBits;
bf.bfSize = (size * pow(n, 2)) + bf.bfOffBits;

// read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

// determines original padding
int padding = (4 - ((bi.biWidth / n) * sizeof(RGBTRIPLE)) % 4) % 4;

// determines padding of resized image
int new_padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

bi.biHeight = n * bi.biHeight;
bi.biWidth = n * bi.biWidth;
bi.biSizeImage =  3 * abs(bi.biHeight) * bi.biWidth + (padding * abs(bi.biHeight));

// ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 || 
bi.biBitCount != 24 || bi.biCompression != 0)
{
    fclose(outptr);
    fclose(inptr);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}

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

// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

// iterates over rows
for (int i = 0, biHeight = abs(bi.biHeight) / n; i < biHeight; i++)
{
    // prints each row n times
    for (int p = 0; p < n; p++)
    {  
        if (p != 0) 
        {
            // offsets to start of row each time 
            fseek(inptr, -(((bi.biWidth / n) * sizeof(RGBTRIPLE))  + padding), SEEK_CUR);
        }
            // iterates over each ppixel in scanline
            for (int j = 0; j < bi.biWidth / n; j++)
            {      
                // temporary stor
                RGBTRIPLE triple;

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

                for (int q = 0; q < n; q++)
                {    
                    // write RGB triple to outfile
                    fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                }

                // read RGB triple from infile
               fseek(inptr, padding, SEEK_CUR);
               for (int k = 0; k < new_padding; k++)
               {
                   fputc(0x00, outptr);
               }
           } 
    }
}
// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;
}
4
  • 1
    Hi, i really do not understand the way you are calculating the bfSize with the function pow(); can you explain a bit? – wallek876 Dec 16 '14 at 8:28
  • for (int q = 0; q < n; q++) { // write RGB triple to outfile fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr); } – Happytreat Dec 17 '14 at 15:00
  • The way I reasoned bfsize is that I wanted to increase the size of the bitmap image but not the size of the headers. So I subtracted the bf.bfOffBits (the bytes at the beginning that are not bitmap bits). I then multiplied what remained, the size of the bitmap bits, by n squared and added the non bitmap bits back in. Did I lose a thread in my somewhat convoluted reasoning? :) – PaulJ Dec 18 '14 at 21:47
  • It's wrong because of paddings. bfSize is a sum of biSizeImage and header's size. – Vladimir Klimenko Feb 20 '15 at 13:24

You must log in to answer this question.

Browse other questions tagged .