Creating QImage from raw uchar pointer



  • 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()
    	);
    }
    

  • Moderators



  • 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?



  • 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


  • Lifetime Qt Champion

    Hi,

    Do you have a sample application that does what you are trying to achieve ?



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.