0

Resize is compiling but the outfile bmp doesn't open. It gives me an error "Not enough memory to load bmp". Does anybody have any idea why that might be happening? Where am I going wrong? Here's my source code..

Thank you so much.

for (int i = 0; i < n; i++)
{
    // write outfile's BITMAPFILEHEADER
    fwrite(&bf, (n*n*sizeof(BITMAPFILEHEADER)), 1, outptr);

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

// determine padding for scanlines
int padding =  (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

RGBTRIPLE pic[bi.biHeight][bi.biWidth+padding], imp[n*bi.biHeight][n*(bi.biWidth+padding)];
 // temporary storage
RGBTRIPLE triple;
// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
    {


        // read RGB triple from infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
        pic[i][j]=triple;
    }
}
for(int i=0; i<(n*bi.biHeight); i++)
{
    for (int j=0; j<(n*(bi.biWidth+padding)); j++)
    {
        for(int a=0;a<n;a++)
        {
            for (int b=0;b<n;b++)
            {
                imp[n*i+a][n*j+b]=pic[i][j];
            }
        }
    }
}
for (int i=0; i<(n*bi.biHeight); i++)
{
    for (int j=0; j<(n*bi.biWidth); j++)
    {
        triple=imp[i][j];
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    }
}

for (int l = 0; l < padding; l++)
{
            fputc(0x00, outptr);
}
1

There is at least one problem, as shown in the following lines of code:

for (int i = 0; i < n; i++)
{
    // write outfile's BITMAPFILEHEADER
    fwrite(&bf, (n*n*sizeof(BITMAPFILEHEADER)), 1, outptr);

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

This would only be correct if n is 1, but since n is not shown, it could be assumed that this is greater than 1. It is not clear why n, or a for loop, is used here, but this is not necessary.

There are a few reasons why this is would not be correct.

  1. The program is writing the BITMAPFILEHEADER and BITMAPINFOHEADER multiple times. A bitmap file should only contain only one instance of each header.
  2. The program is writing the incorrect size for each header. In each case, the header size (sizeof(BITMAPINFOHEADER)) is being multiplied by the n squared. As an example, say n is 2, and the header size is 14 bytes, then the actual number of bytes being written will be 14*2*2 = 56.

The correct procedure would be to write the exact number of bytes in each header once:

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

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

You must log in to answer this question.

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