So, I have completed less comfortable version of resize and moved to more comfortable version.
I came up with an idea of averaging when we downsize. i.e, if initially pixels were 16x16 and n = 0.5, now after resizing it should be 8x8 right, what I am doing is I am taking an average of all of the first 2x2 box and storing them as a single pixel in the out-file.
But I am getting the wrong answer when I tried to check with check50.
MY CODE:enter code here
BITMAPFILEHEADER bf_new;
BITMAPINFOHEADER bi_new;
// 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 1;
}
bi_new = bi;
bf_new = bf;
bi_new.biWidth = floor(bi.biWidth * n);
bi_new.biHeight = floor(bi.biHeight* n);
int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
int pad_new = (4 - (bi_new.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
bi_new.biSizeImage = ( (sizeof(RGBTRIPLE) * bi_new.biWidth) + pad_new) * abs(bi_new.biHeight);
bf_new.bfSize = bi_new.biSizeImage + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
// write outfile's BITMAPFILEHEADER
fwrite(&bf_new, sizeof(BITMAPFILEHEADER), 1, outptr);
// write outfile's BITMAPINFOHEADER
fwrite(&bi_new, sizeof(BITMAPINFOHEADER), 1, outptr);
int scale = round(pow(n,-1));
// determine padding for scanlines
RGBTRIPLE tmatrix[abs(bi.biHeight)][bi.biWidth];
for(int i=0, biHeight = abs(bi.biHeight) ; i < biHeight ; i++)
{
for(int j=0; j < bi.biWidth; j++)
{
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
tmatrix[i][j] = triple;
}
fseek(inptr, padding, SEEK_CUR);
}
for(int i=0, biHeight = abs(bi.biHeight);i<biHeight; i += scale)
{
for(int j=0 ; j < bi.biWidth; j += scale)
{
RGBTRIPLE sample;
sample.rgbtBlue = 0;
sample.rgbtGreen = 0;
sample.rgbtRed = 0;
for(int k1=i;k1<(i+scale)&& k1 <(biHeight);k1++)
{
for(int j1=j ; j1 < (j+scale) && j1 < (bi.biWidth); j1 += scale)
{
sample.rgbtBlue += tmatrix[k1][j1].rgbtBlue;
sample.rgbtGreen += tmatrix[k1][j1].rgbtGreen;
sample.rgbtRed += tmatrix[k1][j1].rgbtRed;
}
}
sample.rgbtBlue /= scale;
sample.rgbtGreen /= scale;
sample.rgbtRed /= scale;
fwrite(&sample, sizeof(BITMAPINFOHEADER), 1, outptr);
}
for (int p1 =0;p1 < pad_new; p1++)
{
fputc(0x00, outptr);
}
}
fclose(inptr);
fclose(outptr);
Can some-one explain to me where I am going wrong, and can someone suggest me how to proceed (or) where to read for further information.
TIA.