Creating QImage from raw uchar pointer
-
wrote on 19 Jan 2018, 02:33 last edited by wasawi
I wanted to copy images from openFrameworks ofImage to qImage and I faced a problem.
It seems that qImage adds some bytes per line if image width is not multiple of four.
Therefore, seems that bytesPerLine() is not pixelsperline*bytesperpixel, something else is added at the end of the line.Is there any better workaround than this?
thank you!// copy from oF to Qt qImage = QImage(w, h, QImage::Format_RGB888); for (int y = 0; y < qImage.height(); y++) { memcpy( &qImage.bits()[y*qImage.bytesPerLine()], &ofImage.getPixels().getData()[y*ofImage.getPixels().getBytesStride()], qImage.bytesPerLine() ); }
-
I wanted to copy images from openFrameworks ofImage to qImage and I faced a problem.
It seems that qImage adds some bytes per line if image width is not multiple of four.
Therefore, seems that bytesPerLine() is not pixelsperline*bytesperpixel, something else is added at the end of the line.Is there any better workaround than this?
thank you!// copy from oF to Qt qImage = QImage(w, h, QImage::Format_RGB888); for (int y = 0; y < qImage.height(); y++) { memcpy( &qImage.bits()[y*qImage.bytesPerLine()], &ofImage.getPixels().getData()[y*ofImage.getPixels().getBytesStride()], qImage.bytesPerLine() ); }
@wasawi Why don't you use http://doc.qt.io/qt-5/qimage.html#fromData ?
-
wrote on 19 Jan 2018, 06:59 last edited by
I did try it. It had the same result.
Does anybody know about how pixel information is stored in qImage? why only images with a width multiple of 4 are displaying correctly? -
wrote on 19 Jan 2018, 07:19 last edited by
Just for clarity:
None of the APIs provided by qImage would copy the data correctly if the image is not multiple of 4.I'm using qt5.8 on visual studio 2015.
In my current setup the the qImage is created in the main application from an openframeworks ofImage and displayed in another widget using paintEvent callback, that widget happens to be loaded dynamically using qt plugins tools (i guess it is not a relevant point.. but just in case.).Isolating code to provide an example would take some time. So if anybody knows about this issue please let me know. Otherwise i will keep using my hacky code to copy the image correctly.
Thank you!
j -
Hi,
Do you have a sample application that does what you are trying to achieve ?
-
wrote on 20 Jan 2018, 15:11 last edited by
Back when I was using char table to modify QImage on QT 4, each pixel was stored using 4 unsigned chars (Red, Green, Blue, transparency). I imagine it's still stored that way, so 10th pixel was represented by ptr[40], ptr[41], ptr[42] and ptr[43].
Perhaps that's where your problem lies.
6/6