PDA

View Full Version : How to return pixels from QPixmap to QML



cmessineo
15th December 2013, 17:31
Hello, How would one return Image bits to QML from C++?

I have this code in c++:
....


QImage image = m_pixmap.copy(sx, sy, sw, sh).toImage();

image = image.convertToFormat(QImage::Format_ARGB32);
QByteArray bytes((char *) image.bits(), image.byteCount());

return ?????

How would I package up the QByteArray properly and return the data to QML in rgba format, so it can be manipulated on the QML side?

Thanks

aamer4yu
16th December 2013, 09:55
Have a look at QQuickImageProvider :)


And if you want to edit image,, you better have the manipulation code in C++ and call that from QML... dont keep processing on QML side.

wysota
16th December 2013, 13:28
Hello, How would one return Image bits to QML from C++?

I have this code in c++:
....


QImage image = m_pixmap.copy(sx, sy, sw, sh).toImage();

image = image.convertToFormat(QImage::Format_ARGB32);
QByteArray bytes((char *) image.bits(), image.byteCount());

return ?????

How would I package up the QByteArray properly and return the data to QML in rgba format, so it can be manipulated on the QML side?

Thanks


class ImageManipulator : public QObject {
public:
ImageManipulator(QObject *parent = 0) : QObject(parent) {}
void setImage(const QImage &img) { m_image = img; }
QImage image() const { return m_image; }

Q_INVOKABLE QColor pixel(int x, int y) const { return QColor(m_image.pixel(x, y)); }
Q_INVOKABLE void setPixel(int x, int y, QColor c) { m_image.setPixel(x, y, c); }
private:
QImage m_image;
};

cmessineo
16th December 2013, 17:01
I don't want to provide an image I just want to provide an array of pixels that can be updated on the qml side, then I will send the pixels back to c++ and update the image. I just don't know how to send the initial image pixel array to qml.

I'm updating the Qt Labs Canvas control

https://qt.gitorious.org/qt-labs/qmlcanvas/source/a7afc8f332bdfdb02ba0858187e17e36883c8a03:

The getImageData and putImageData functions were never implemented for the context2d class.

Thanks

cmessineo
16th December 2013, 19:40
I was able to figure it out in case anyone needs to this. I had to use a QVariantList, and I'm not sure why but the image.bits() came out in BGRA format so I had to switch it around in the loop.



QImage image = m_pixmap.copy(sx, sy, sw, sh).toImage();
image = image.convertToFormat(QImage::Format_ARGB32);

uchar *bits = image.bits();

QVariantList pixels;

for (int i = 0; i < image.byteCount();)
{
pixels << bits[i+2] << bits[i+1] << bits[i] << bits[i+3];
i += 4;
}

return pixels;

wysota
17th December 2013, 07:29
I don't want to provide an image I just want to provide an array of pixels that can be updated on the qml side, then I will send the pixels back to c++ and update the image.
That's a totally inefficient approach for QML. Don't try to mimic APIs that are not meant for a particular environment.



I'm updating the Qt Labs Canvas control

https://qt.gitorious.org/qt-labs/qmlcanvas/source/a7afc8f332bdfdb02ba0858187e17e36883c8a03:

The getImageData and putImageData functions were never implemented for the context2d class.


The Canvas element is part of QtQuick 2. It has getImageData and putImageData implemented. Bear in mind using these functions in QtQuick 2 probably triggers a slow code path in Canvas though.