title: Redraw image from 3d perspective to 2d
body: <p>I need an inverse perspective transform written in Pascal/Delphi/Lazarus. See the following image:</p>
  
  <p><img src="https://i.stack.imgur.com/n6d0w.jpg" alt="image process"></p>
  
  <p>I think I need to walk through destination pixels and then calculate the corresponding position in the source image (To avoid problems with rounding errors etc.).</p>
  
  <pre><code>function redraw_3d_to_2d(sourcebitmap:tbitmap, sourceaspect:extended, point_a, point_b, point_c, point_d:tpoint, megapixelcount:integer):tbitmap;
  var
     destinationbitmap:tbitmap;
     x,y,sx,sy:integer;
  begin
    destinationbitmap:=tbitmap.create;
    destinationbitmap.width=megapixelcount*sourceaspect*???; // I dont how to calculate this
    destinationbitmap.height=megapixelcount*sourceaspect*???; // I dont how to calculate this
    for x:=0 to destinationbitmap.width-1 do
      for y:=0 to destinationbitmap.height-1 do
      begin
          sx:=??;
          sy:=??;
          destinationbitmap.canvas.pixels[x,y]=sourcebitmap.canvas.pixels[sx,sy];
      end;
    result:=destinationbitmap;
  end;
  </code></pre>
  
  <p>I need the real formula... So an OpenGL solution would not be ideal...</p>