0

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.

0

Instead of averaging the relevant pixels (which will change the colours), try determining which of the original pixels to copy.

For the new image, for each pixel in a scanline (or row in the image), you should figure out which of the original pixels (or rows) to copy.

So, if your original image is 20 pixels wide and your new image is 50 pixels wide (ie a scale factor f of 2.5), then for any pixel in the new image, eg pixel 15, which original pixel to copy? old pixel = 15 / 50 * 20 or old pixel = 15 / f = old pixel 6

Or more generically, new pixel at location l should copy old pixel at location (l / scale). I found that doing the division as longs, then converting into an int worked: (int) ( (long) l / (long) scale )

You must log in to answer this question.

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