title: Looking for fast image distortion algorithms
body: <p>I am trying to implement an application which uses shpere distortion filter. I am using an the algorithm from <a href="http://www.codeproject.com/KB/GDI-plus/displacementfilters.aspx" rel="nofollow">here</a> which changes pixels location by getPixel() and setpixel() methods. My problem is it is too slow for Android devices and there are applications which implements same sphere(and others) filter way faster than my approach. ( for example Picsay Pro app) Could anyone share or give direction to find or implement fast distortion algorithms. </p>
  
  <p>Actual filter that implements the algorithm:</p>
  
  <pre><code>public boolean sphereFilter(Bitmap b, boolean bSmoothing)
  {   
      int nWidth = b.getWidth();
      int nHeight = b.getHeight();
  
      Point  [][] pt = new Point[nWidth][nHeight];
      Point mid = new Point();
      mid.x = nWidth/2;
      mid.y = nHeight/2;
  
      double theta, radius;
      double newX, newY;
  
      for (int x = 0; x &lt; nWidth; ++x)
          for (int y = 0; y &lt; nHeight; ++y)
          {
              pt[x][y]= new Point();
          }
  
      for (int x = 0; x &lt; nWidth; ++x)
          for (int y = 0; y &lt; nHeight; ++y)
          {
              int trueX = x - mid.x;
              int trueY = y - mid.y;
              theta = Math.atan2((trueY),(trueX));
  
              radius = Math.sqrt(trueX*trueX + trueY*trueY);
  
              double newRadius = radius * radius/(Math.max(mid.x, mid.y));
  
              newX = mid.x + (newRadius * Math.cos(theta));
  
              if (newX &gt; 0 &amp;&amp; newX &lt; nWidth)
              {
                  pt[x][y].x = (int) newX;
              }
              else
              {
                  pt[x][y].x = 0;
                  pt[x][y].y = 0;
              }
  
              newY = mid.y + (newRadius * Math.sin(theta));
  
              if (newY &gt; 0 &amp;&amp; newY &lt; nHeight &amp;&amp; newX &gt; 0 &amp;&amp; newX &lt; nWidth)
              {                   
                  pt[x][ y].y = (int) newY;
              }
              else
              {
                  pt[x][y].x = pt[x][y].y = 0;
              }
          }
      offsetFilterAbs(b, pt);
      return true;
  }
  </code></pre>
  
  <p>The code that replaces the calculated pixels' positions. </p>
  
  <pre><code>public boolean offsetFilterAbs(Bitmap b, Point[][] offset )
  {
          int nWidth = b.getWidth();
          int nHeight = b.getHeight();
  
          int xOffset, yOffset;
  
          for(int y=0;y &lt; nHeight;++y)
          {
              for(int x=0; x &lt; nWidth; ++x )
              {   
                  xOffset = offset[x][y].x;
                  yOffset = offset[x][y].y;
  
                  if (yOffset &gt;= 0 &amp;&amp; yOffset &lt; nHeight &amp;&amp; xOffset &gt;= 0 &amp;&amp; xOffset &lt; nWidth)
                  {
                      b.setPixel(x, y, b.getPixel(xOffset, yOffset));
                  }                   
              }               
          }
  
      return true;
  }
  </code></pre>