QML Canvas bug with getImageData with 5.0 and 5.1, or am I doing something wrong? [Fixed] Not really.
I'm not certain, but I think I've stumbled onto a bug with Canvas in Qt 5.0 and 5.1
var ctx = canvas.getContext('2d');
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
Now, as far as I understand imageData.data should now contain the pixel data. Using a C++ method I attempt to handle it (as a QVariantList) with:
setFromData(imageData.data, imageData.width, imageData.height);
This method creates a uchar* array of the pixels, creates a QImage from it, then returns.
Except this code doesn't work. The array received is 0 bytes long.
If I make a deep copy of the pixel array (in JS) into a new array and pass the copy it works perfectly:
var copied = new Array();
for( var x=0; x < imageData.data.length; x++ )
copied[x] = imageData.data[x];
printer.setFromData(copied, imageData.width, imageData.height);
I have made a fairly minimal example which reproduces the effect:
I have a suspicion that the offending code is here: http://qt.gitorious.org/qt/qtdeclarative/blobs/stable/src/qml/qml/v8/qv8engine.cpp#line241
It looks like if it's a pixel array it returns an empty QVariant. Am I right?
Any thoughts at all on this would be appreciated, thanks.
This does look like a bug. I do wonder which conversion codepath is taken for the second (manually deep-copied) array, though.
In any event, please file a bug report with the minimal example attached. Very soon I think v8 is going away, to be replaced by v4vm, so it's possible that this is already fixed in what will become 5.2.
My bug report became a feature request since it "never has worked".
It will most likely stay a stub which does nothing (not even a warning) and will instead return an empty QVariant to the native method.
In short the correct way is to quite literally copy the contents into a new array object (in JS) and return that.
Weak? I thought so too.