yaroslav
yaroslav

Reputation: 115

QImage transform returns null

I have an image, and I'm trying to transform selected part of the image using QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode) const method. But this method return null image. Also Qt wrote to std output:

"Qimage: out of memory, returning null image."

    QImage img;
    img.load("D:\\3.png");

QPolygonF polygonIn;
polygonIn  << QPointF(73, 63)
    << QPointF(362, 22)
    << QPointF(517, 325)
    << QPointF(1, 210);

QPolygonF polygonOut;
polygonOut  << QPointF(45, 39)
    << QPointF(228, 13)
    << QPointF(228, 198)
    << QPointF(180, 198);

    QTransform transform;
auto isOk = QTransform::quadToQuad(polygonIn, polygonOut, transform);
if(!isOk)
    throw std::runtime_error("Transformation impossible with such parameters.");

auto result = img.transformed(transform, Qt::TransformationMode::SmoothTransformation);

I've tried to debug QImage sources. On line 6633 of qimage.cpp

QImage dImage(wd, hd, target_format);
QIMAGE_SANITYCHECK_MEMORY(dImage);

wd and hd values are huge.

Maybe someone know how I can solve this problem. BTW I use qt 4.8.4.

Upvotes: 0

Views: 1314

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40502

QImage works fine. The problem is in your transform. Try the following code to check:

qDebug() << transform.map(QRectF(0, 0, img.width(), img.height())).boundingRect();

It returns QRectF(-2.21401e+08,-1.9792e+08 2.21401e+08x1.97921e+08) if image is 100x100. Obviously the image that large cannot fit in memory. You need to correct your transform so it scales your image to reasonable size. I don't know where did you get those magic numbers from, but try to get new ones.

Also img = img.scaled(img.width(), img.height()); apparently has no effect (what's the point in scaling an image to the size that it already has?). Code related to painter seems to be incomplete but this is probably a copy-paste issue.

Upvotes: 1

Related Questions