Interpolation (Resizing) of a Bitmap image

Posted: October 20, 2009 in C, C++, CUDA, Multimedia, Programming Tips, VC++
Tags: , , , , , , , , , , ,

#define PADBYTES 4
int Interpolation(BYTE* pbySrc, BYTE* pbyDest, int iSrcWidth, int iSrcHeight, int iSrcBPP, int iDestWidth, int iDestHeight)
{  
 //process each line
 for (int iRow = 0; iRow < iDestHeight; iRow++)
 {
 double ay = (double) iRow * iSrcHeight / iDestHeight;    //scaled source iRow
 int     y = (int)ay;                                    //truncates source iRow
 ay = ay - y;                                        //distance as fractional part of the scaled pixel, used for weighting

 //process each pixel within the line
 for (int iCol = 0; iCol < iDestWidth; iCol++)
 {
 double R, G, B;

 double ax = (double)iCol * iSrcWidth / iDestWidth;    //scaled source column
 int     x = (int)ax;                                //truncates source column
 ax = ax - x;                                    //distance as fractional part of the scaled pixel, used for weighting

 if ((x < iSrcWidth - 1) && (y < iSrcHeight - 1))
 {
 //interpolate each channel
 R = (1.0 - ax) * (1.0 - ay) * GetValueR(x+0, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 ax  * (1.0 - ay) * GetValueR(x+1, y+0, iSrcWidth, iSrcHeight, pbySrc) +
 (1.0 - ax) *        ay  * GetValueR(x+0, y+1, iSrcWidth, iSrcHeight, pbySrc) +
 ax  *        ay  * GetValueR(x+1, y+1, iSrcWidth, iSrcHeight, pbySrc);
 
 G = (1.0 - ax) * (1.0 - ay) * GetValueG(x+0, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 ax  * (1.0 - ay) * GetValueG(x+1, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 (1.0 - ax) *        ay  * GetValueG(x+0, y+1, iSrcWidth, iSrcHeight, pbySrc) +
 ax  *        ay  * GetValueG(x+1, y+1, iSrcWidth, iSrcHeight, pbySrc);

 B = (1.0 - ax) * (1.0 - ay) * GetValueB(x+0, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 ax  * (1.0 - ay) * GetValueB(x+1, y+0, iSrcWidth, iSrcHeight, pbySrc) + 
 (1.0 - ax) *        ay  * GetValueB(x+0, y+1, iSrcWidth, iSrcHeight, pbySrc) +
 ax  *        ay  * GetValueB(x+1, y+1, iSrcWidth, iSrcHeight, pbySrc);
 }
 else
 {
 //set to zero to blank these lines instead of copying source
 R = pbySrc[y * iSrcWidth * iSrcBPP + x * iSrcBPP + 0];
 G = pbySrc[y * iSrcWidth * iSrcBPP + x * iSrcBPP + 1];
 B = pbySrc[y * iSrcWidth * iSrcBPP + x * iSrcBPP + 2];
 }

 //write to destination buffer, change order for BGR
 *pbyDest++ = (BYTE)R;
 *pbyDest++ = (BYTE)G;
 *pbyDest++ = (BYTE)B;
 }

 int iPad = (iDestWidth * 3) % PADBYTES ? PADBYTES - (iDestWidth * 3) % PADBYTES : 0;
 for (int idx = 0; idx < iPad; idx++)
 {
 *pbyDest++ = 0;
 }
 }
 return 0;
}

pbySrc      -> Source Buffer
pbyDest     -> Destination Buffer
iSrcWidth   -> Source Image Width
iSrcHeight  -> Source Image Height
iSrcBPP     -> Bytes per pixel od the source Image
iDestWidth  -> New width of the image
iDestHeight -> New Height of the Image

NB: The memory allocation for the new image should be allocated. the Bytes per pixel of the new image is 3. For the other function used here please refer the previous posts.

About these ads
Comments
  1. Ivan says:

    Thank you! Good example! It helped!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s