0

I've been trying 'resize' for a while now, and I don't understand what's going wrong. On input of small.bmp, the first output line is correct, but the following lines are all different colours (the pixels of small.bmp are green if not white). I think the padding calculations are correct, it's most likely something to do with my vertical scaling.

I created new header files for the output by copying the values, and changing the: biWidth, biHeight and bfSize dependent on the scaling factor n. The code should horizontally scale, add the new padding, then vertically scale for each line of the original input.

Is it something to do with pointers? Are both my sets of header files pointing to the same place?

Does fputc move the pointer in outptr?

Also, if I try to resize smiley.bmp, I get an unreadable output file. Why should I get different results from two different images?

//New header files for resized image

//Change the bitmap width and height
BITMAPINFOHEADER nbi = bi;
nbi.biWidth = n*(bi.biWidth);
nbi.biHeight = n*(bi.biHeight);

// determine padding for scanlines, required for new 
int newpadding =  (4 - (nbi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

BITMAPFILEHEADER nbf = bf;
nbf.bfSize = ((nbi.biWidth * sizeof(RGBTRIPLE) + newpadding) * abs(nbi.biHeight)) + 54;

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

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

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

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{
    for (int l = 0; l < n; l++) //vertical scaling
    {


    // 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 m = 0; m < n; m++) //horizontal scaling
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
    }

    // skip over padding, if any, skips to the next line.
    fseek(inptr, padding, SEEK_CUR);

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

    fseek(outptr, newpadding, SEEK_CUR); //set output pointer to the next line.

    }

}

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;
}
1

Resize works by "expanding" each pixel horizontally and vertically by a value, it seems you're trying to read input multiple times and expand it horizontally to match output width, which would explain the weird colours you described, because fread may read more memory than what is allocated for the input image, making your code write to ouput whatever it can find.
Also, be sure to initialize padding "pixels", since it can't be known the values they contain before they are set. You can't see the padding when opening the image but check50 or the grader may complain.

| improve this answer | |

You must log in to answer this question.

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