0

my resize code doesn't work if n is greater than 1 and i don't understand why, if i printf the values they are right but peek values are wrong. My code is:

//resize the image

// Get n
int n = atoi(argv[1]);
if (n < 1 || n > 100)
{
    printf("Factor must be in [1,100]!\n");
    return 1;
}

// Get bipmap info
BITMAPFILEHEADER bf_resize = bf;
BITMAPINFOHEADER bi_resize = bi;

// Get new size
bi_resize.biWidth  *= n;
bi_resize.biHeight *= n;


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

// determine new padding
int padding_resize = (4 - (bi_resize.biWidth * sizeof(RGBTRIPLE)) %4) % 4;

// determine new sizes

// -the info
bi_resize.biSizeImage = (bi_resize.biWidth * sizeof(RGBTRIPLE) + padding_resize) * abs(bi_resize.biHeight);

// -total
bf_resize.bfSize = 54 + bi_resize.biSizeImage;

// write file
fwrite(&bf_resize, sizeof(BITMAPFILEHEADER), 1, outptr);

// write info
fwrite(&bi_resize, sizeof(BITMAPINFOHEADER), 1, outptr);

// 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++)
    {

        // temporary storage
        RGBTRIPLE triple;

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

        // write RGB triple to outfile
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

        // Add a new padding
        for (int l = 0; l < padding_resize; l++)
            fputc(0x00, outptr);

        // Return to the beginning of a scanline
        if (j < n - 1)
            fseek(inptr, -bi.biWidth * sizeof(RGBTRIPLE), SEEK_CUR);
    }

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

// close infile
fclose(inptr);

// close outfile
fclose(outptr);

// that's all folks
return 0;

}

0

Assuming that you're reading the headers correctly (which i guess you are, since it's already done for us in copy.c), the 2 errors I see are:

1) Why are you writing the padding there? What have you written so far to the file when you write the padding?

2) You're not resizing the file horizontally at all. You read a triple, you write a triple. And then you're fseeking the whole scanline? Also, you're not resizing vertically.

You must log in to answer this question.

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