title: Applying QTransform on QPainter - how to find translations
body: <p>I would like to apply certain transformations on a <code>QImage</code> - and because of performance restrictions, I would like to apply these transformations at the time of rendering if possible.</p>
  <p>The transformations I need - rotating 90, 180, 270 degrees, and vertical mirror.</p>
  <p>I am rendering a <code>QGraphicsScene</code> to a <code>QImage</code>.<br />
  I would like the result to be rotated (0/90/180/270) or mirrored vertically.</p>
  <p>My original code was easy:</p>
  <pre><code>QImage image = QImage(wOutput, hOutput, QImage::Format_Mono);
  image.fill(QColor(Qt::white).rgb());
  QPainter painter;
  painter.begin(&amp;image);
  outputScene-&gt;render(&amp;painter);
  painter.end();
  </code></pre>
  <p>To rotate, I thought it would be effective to rotate the <code>QPainter</code> before painting - that way I do not have to perform additional transformations post process. (I am planning for a device with very restricted memory and speed, and  Qt4.8)</p>
  <p>It should work... but in addition to rotation I must also add a translation, and I can't figure out how much.</p>
  <p>Without translation I am only getting blank images.</p>
  <p>Also, rotating to 90/-90 I get smaller images. So I need to scale... by how much ?</p>
  <p>My code that tries to transform (rotate and mirror) an image:</p>
  <pre><code>#include &lt;QApplication&gt;
  #include &lt;QGraphicsScene&gt;
  #include &lt;QImage&gt;
  #include &lt;QPainter&gt;
  #include &lt;QTransform&gt;
  
  QImage badProcessScene(QGraphicsScene *s, int orientation, bool mirror);
  QImage wantProcessScene(QGraphicsScene *s, int orientation, bool mirror);
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      QGraphicsScene* s = new QGraphicsScene(-20, -100, 800, 600);
      s-&gt;addText(&quot;abcd&quot;, QFont(&quot;Arial&quot;, 20));
      s-&gt;setBackgroundBrush(Qt::red);
  
      // orientation = -90, 0, 90, 180;
      int orientation = -90;
      // vertical mirror true/false
      bool mirror = true;
  
      // what I am trying to do
      QImage im = wantProcessScene(s, orientation, mirror);
      im.save(&quot;test.bmp&quot;);
  
      // what I would like to come out like (Qt version)
      QImage im1 = badProcessScene(s, orientation, mirror);
      im1.save(&quot;test1.bmp&quot;);
  
      a.exit();
      return 0;
  }
  
  // Bad version, though functional - typical approach, of rotating images after being rendered
  QImage badProcessScene(QGraphicsScene *s, int orientation, bool mirror)
  {
      int wOutput = s-&gt;width(), hOutput = s-&gt;height();
  
      QImage image = QImage(wOutput, hOutput, QImage::Format_ARGB32_Premultiplied);
      image.fill(QColor(Qt::white).rgb());
  
      QPainter painter;
      painter.begin(&amp;image);
      s-&gt;render(&amp;painter);
      painter.end();
  
      if(mirror)
          image = image.mirrored(0, 1);
      image = image.transformed(QMatrix().rotate(orientation));
  
      return image;
  }
  
  // Desired but needs adjustments 
  QImage wantProcessScene(QGraphicsScene* s, int orientation, bool mirror)
  {
      int wOutput = s-&gt;width(), hOutput = s-&gt;height();
  
      // translation
      int wTr = wOutput, hTr = hOutput;
      // coefficients of transformation matrix
      qreal m11 = 1, m12 = 0, m21 = 0, m22 = 1, m31 = 0, m32 = 0;
      switch(orientation)
      {
      //case 0: break;
      case -90: wTr = hOutput; hTr = wOutput; m11 = 0; m12 = -1; m21 = 1; m22 = 0; 
                m31 = x?; m32 = x?; break;  
      case 180: wTr = wOutput; hTr = hOutput; m11 = -1, m22 = -1; 
               m31 = x?; m32 = x?; break;  
      case 90: wTr = hOutput; hTr = wOutput; m11 = 0; m12 = 1; m21 = -1; m22 = 0; 
                m31 = x?; m32 = x?; break;  
      }
  
      QImage image = QImage(wTr, hTr, QImage::Format_ARGB32_Premultiplied);
      image.fill(QColor(Qt::white).rgb());
  
      QPainter painter;
      painter.begin(&amp;image);
  
      QTransform painterTransform;
      if(mirror)
      {
          // I have seen that negative scaling actually flips
          m22 *= -1;
          // I am guessing on shifts...
          switch(orientation)
          {
          case 0: m31 = x?; m32 = x?; break;  
          case 180: m31 = x?; m32 = x?; break; 
          case 90: m31 = x?; m32 = x?; break;
          case -90: m31 = x?; m32 = x?; break; 
          }
      }
  
      painterTransform.setMatrix(m11, m12, 0, m21, m22, 0, m31, m32, 1);
  
      painter.setTransform(painterTransform);
      s-&gt;render(&amp;painter);
      painter.end();
  
      return image;
  }
  </code></pre>
  <p>Note in the code above - I have 2 functions:</p>
  <ul>
  <li>what I am trying to do, by rotating the <code>QPainter</code> before rendering (<code>wantProcessScene</code>)</li>
  <li>one that does Qt &quot;transforms&quot; - which iterate through image pixels and move them after the image was already rendered - and on very large images, on memory and CPU limited devices is extremely slow; It works as I said in code and it is not acceptable. (<code>badProcessScene</code>)</li>
  </ul>
  <p>I have not been able to figure out a formula for translation and scaling (because for 90/-90 it seems result is smaller) on rotating 180/90/-90.</p>
  <p>As for mirror, I hope my idea of resizing by -1 works - but I have the same issue of determining shifts.</p>
  <p>With incorrect shifts, I just see a white image. As I get this to work, 90/-90 rotations seem to result in a smaller image (that fits the larger size into the smaller, keeping aspect ratio).</p>
  <p>Please help me figure out how to perform these transformations. I would love a matrix that determines translations (and scaling to reset images back to same size) based on angle - not weird angles just these.</p>