-1

check50 shows that i cant resize 1x1px image by 2 and 3, others are fine and i don't know why....can anyone give me some hints. thanks. My headers are fine as i checked...

// read infile's BITMAPFILEHEADER BITMAPFILEHEADER bfOld; BITMAPFILEHEADER bfNew; fread(&bfOld, sizeof(BITMAPFILEHEADER), 1, inptr);

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

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

biNew = biOld;
bfNew = bfOld;
biNew.biWidth = biOld.biWidth * factor;
biNew.biHeight = biOld.biHeight * factor;
int newpadding = (4 - (biNew.biWidth * factor * sizeof(RGBTRIPLE)) % 4) % 4;
biNew.biSizeImage = ((biNew.biWidth * sizeof(RGBTRIPLE)) + newpadding) * abs(biNew.biHeight);
bfNew.bfSize = biNew.biSizeImage + 54;
// write outfile's BITMAPFILEHEADER
fwrite(&bfNew, sizeof(BITMAPFILEHEADER), 1, outptr);

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(biOld.biHeight); i < biHeight; i++)
{
    //Vertical resizing
    RGBTRIPLE line[biNew.biWidth];
    int b = 0;
    // iterate over pixels in scanline
    for (int j = 0; j < biOld.biWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

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

        // write RGB triple to outfile, Horizontal Resizing
        for(int k = 0; k < factor; k++)
        {

          line[b] = triple;
          b++;
        }

    }

    for(int c = 0; c < factor ; c++)
    {

    fwrite(&line, sizeof(RGBTRIPLE), biNew.biWidth, outptr);

    for (int k = 0; k < newpadding; k++)
    {
        fputc(0x00, outptr);
    }

    }

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

    // then add it back (to demonstrate how)




}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

2
  • Just for starters I wanted to ask whether your new padding is correct? You multiplied the width by factor, then to determine your padding you multiplied your new width by the factor again? – user3115 Nov 28 '14 at 19:25
  • Just for starters I wanted to ask whether your new padding is correct? You multiplied the width by factor, then to determine your padding you multiplied your new width by the factor again? – user3115 Nov 28 '14 at 19:25
0

The problem is caused by these two lines:

biNew.biWidth = biOld.biWidth * factor;
...
int newpadding = (4 - (biNew.biWidth * factor * sizeof(RGBTRIPLE)) % 4) % 4;

biNew.biWidth is already the width of the new image, so there is no need to multiply it by factor again when calculating the new padding. This problem can be fixed by changing the second line to either

int newpadding = (4 - (biNew.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

or

int newpadding = (4 - (biOld.biWidth * factor * sizeof(RGBTRIPLE)) % 4) % 4;

You must log in to answer this question.

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