Skip to main content
3 of 5
added 40 characters in body; edited tags; edited title
kzidane
  • 17.7k
  • 3
  • 28
  • 100

How to Resize an Image Vertically?

I'm able to resize a given .bmp horizontally, but the code for vertical resizing is not working. Here is my code for vertical expansion.

// resize vertically

//move pointer to start of scanline of outptr
fseek(outptr,  -3 * (bi2.biWidth + padding), SEEK_CUR);
    
fgetpos (outptr,&pos);
    
//an array of RGBTRIPLE to store one scanline
RGBTRIPLE triple[bi2.biWidth + padding]; 
    
//reading the already written first line    
for (int p = 0 ; p < bi2.biWidth + padding; p++)
{ 
    fread(&triple[p], sizeof(RGBTRIPLE), 1, outptr); 
}
fgetpos (outptr,&pos);           
    
//writing the copied array to outptr
for (int k = 0 ; k < n; k++)
{             
    for (int p = 0 ; p < bi2.biWidth + padding; p++)
         
        fwrite(&triple[p], sizeof(RGBTRIPLE), 1, outptr);   
                    
}
  

This is executed after reading the first scanline of the original bmp. I used gdb to see the values of triple's elements, but I found that they're all garbage values.

Also when I check the position of the file position indicator in outptr using fgetpos(), I find it not changing!


Update: here's my code after modifications

// iterate over infile's scanlines
fpos_t pos;
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{   
    // iterate over pixels in scanline
    fgetpos (outptr,&pos);
    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 n times horizontally
        for (int k = 0 ; k < n; k++)
        {       
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);  
                       
        }
    }

    // skip over padding in infile, if any
    fseek(inptr, padding_in, SEEK_CUR);

    // then add padding to outfile
    for (int l = 0; l < padding; l++)
    {
        fputc(0x00, outptr);
    }        
   
    // resize vertically  
    //move pointer to start of scanline of outptr
    fsetpos( outptr, &pos );
    
    //an array of RGBTRIPLE to store one scanline
    RGBTRIPLE triplex[bi2.biWidth]; 
    
        
    //reading the already written first line    
    for (int m = 0 ; m < bi2.biWidth ; m++)
    {             
        fread(&triplex[m], sizeof(RGBTRIPLE), 1 , outptr); 
    }
    
    //skip padding
    fseek(outptr, padding, SEEK_CUR);
    for (int mn = 0 ; mn < n ; mn++)
    {
        //writing the copied array to outptr
        for (int o = 0 ; o < bi2.biWidth ; o++)
        {
            fwrite(&triplex[o], sizeof(RGBTRIPLE), 1 , outptr);   
        }
        for (int p = 0; p < padding; p++)
        {
            fputc(0x00, outptr);
        }   
    }               
}
sid
  • 15
  • 5