Advertisement
Guest User

Untitled

a guest
Oct 6th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. float sourceWidth=192.0f;
  2. float sourceHeight=128.0f;
  3.  
  4. float scaledWidth=sourceWidth*1.5f;
  5. float scaledHeight=sourceheight*1.5f;
  6.  
  7. // First we need to find a ratio between the source image and the scaled image
  8. // We already know it's 1.5
  9.  
  10. float ratioX=scaledWidth/sourceWidth;
  11. float ratioY=scaledHeight/sourceHeight;
  12.  
  13. // Then we need to loop through each scaled image pixel
  14.  
  15. for(int i=0;i<scaledWidth;i++)
  16. {
  17.     for(int j=0;j<scaledHeight;j++)
  18.     {
  19.         // Time to get the real number position on the source image
  20.  
  21.         float sourceX=(float)i/ratioX;
  22.         float sourceY=(float)j/ratioY;
  23.  
  24.         // Then we find the four pixels around our position
  25.  
  26.         int minX=floor(sourceX);
  27.         int minY=floor(sourceY);
  28.         int maxX=minX+1;
  29.         int maxY=minY+1;
  30.  
  31.         // We need to check if the bottom-right pixel is out of the source image
  32.  
  33.         if(maxX>=sourceWidth)
  34.             maxX--;
  35.  
  36.         if(maxY>=sourceHeight)
  37.             maxY--;
  38.  
  39.         // Now we get the distance between the top-left source pixel and
  40.         // The real number position
  41.  
  42.         float dX=sourceX-(float)minX;
  43.         float dY=sourceY-(float)minY;
  44.  
  45.         // And for each color component (R,G,B) we apply the formula
  46.         // You can find it in the second link I posted
  47.  
  48.         // I assume I have both GetPixelComp and SetPixelComp functions
  49.         // And both images represented as source and scaled
  50.  
  51.         for(char k=0;k<3;k++)
  52.         {
  53.             unsigned char component=(unsigned char)
  54.             (GetPixelComp(source,minX,minY,k)*(1-dX)*(1-dY)+ // Top-Left
  55.             GetPixelComp(source,maxX,minY,k)*dX*(1-dY)+ // Top-Right
  56.             GetPixelComp(source,minX,maxY,k)*(1-dX)*dY+ // Bottom-Left
  57.             GetPixelComp(source,maxX,maxY,k)*dX*dY); // Bottom-Right
  58.  
  59.             SetPixelComp(scaled,i,j,k,component);
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement