[Development] What is the correct way to use QTransform to rotate a QImage by the Y axis?

JiDe Zhang zccrs at live.com
Wed Jul 20 07:48:47 CEST 2022


I want to rotate the image to 45 degrees by Y axis, what should I do?

bool func1()
{
    QImage image(3840, 2160, QImage::Format_ARGB32_Premultiplied);
    image.fill(Qt::red);

    QTransform t;
    t.translate(image.width() / 2.0, image.height() / 2.0);
    t.rotate(-45, Qt::YAxis);
    t.translate(-image.width() / 2.0, -image.height() / 2.0);
    image = image.transformed(t);

    return image.save("/tmp/func1.png");
}

The func1 is output the "QImage: out of memory, returning null image" error message and return false. This behavior is related to the size of the image. If it is a 1000x1000 image, it can be successful. But the rotated image is different as use GIMP, is this a Qt bug?

bool func2()
{
    QImage image(3840, 2160, QImage::Format_ARGB32_Premultiplied);
    image.fill(Qt::red);

    QTransform t;
    t.translate(image.width() / 2.0, image.height() / 2.0);

    qreal b = qDegreesToRadians(-45);
    qreal sina = qSin(b);
    qreal cosa = qCos(b);
    QTransform tmp(cosa, 0, -sina / image.width(), // There is "-sina / 1024" in QTransform::rotate
                   0, 1, 0,
                   0, 0, 1);

    t = tmp * t;
    t.translate(-image.width() / 2.0, -image.height() / 2.0);
    image = image.transformed(t);

    return image.save("/tmp/func2.png");
}

The func2 is return true. I can't understand the QTransform::rotate, What is the "inv_dist_to_plane"? Why is its value 1/1024?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20220720/e68176ee/attachment.htm>


More information about the Development mailing list