0

Iam trying to warp an image strip onto another image with QPainter. This is my code. Iam using quadtoQuad function to determine the transform.

void test123()
{
    // Fill image strip
    int nStripLength = 500;
    int nStripHeight = 2;
    QImage strip(nStripLength, nStripHeight, QImage::Format::Format_ARGB32);
    
    for (int i = 0; i < nStripLength; i++)
    {
        bool bRed = (i < nStripLength / 2);
        auto color = QColor(bRed ? Qt::red : Qt::green).rgba();

        for (int j=0;j < nStripHeight;j++)
            strip.setPixel(i, j, color);
    }

    strip.save("C:/temp/testStrip.png");

    QPolygonF polyStrip, polyBeam;

    polyStrip << QPointF(strip.rect().topLeft());
    polyStrip << QPointF(strip.rect().topRight());
    polyStrip << QPointF(strip.rect().bottomRight());
    polyStrip << QPointF(strip.rect().bottomLeft());

    polyBeam << QPointF(200, 10);  // TL
    polyBeam << QPointF(800, 10);  // TR
    polyBeam << QPointF(800, 50); // BR
    polyBeam << QPointF(200, 30); // BL
    
    // Our canvas
    QImage img(1000, 1000, QImage::Format::Format_ARGB32);
    img.fill(Qt::white);

    QPainter painter(&img);
    painter.setRenderHints(QPainter::RenderHint::Antialiasing);
    painter.setRenderHints(QPainter::RenderHint::SmoothPixmapTransform);

    QTransform transform;
    bool bTransOk = QTransform::quadToQuad(polyStrip, polyBeam, transform);
    assert(bTransOk);

    if (bTransOk)
    {
        painter.setTransform(transform);
        painter.drawImage(QPoint(), strip);
        
        QPen p(Qt::blue, 1);
        p.setCosmetic(true);
        painter.setPen(p);
        painter.drawPolygon(polyStrip);

        img.save("C:/temp/testWarp.png");
    }

This is the strip: enter image description here

This is the result: enter image description here

So Iam a bit lost, why is the drawn poly and image not the same in size? Secondly, why the red-green is not divided more evenly.

If someone knows a solution?

Richy
  • 534
  • 4
  • 13

0 Answers0