Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Unsolved Creating QImage from raw uchar pointer

    General and Desktop
    qimage bytesperline linestride
    4
    6
    1422
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      wasawi 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()
      	);
      }
      
      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @wasawi last edited by

        @wasawi Why don't you use http://doc.qt.io/qt-5/qimage.html#fromData ?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • W
          wasawi 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?

          1 Reply Last reply Reply Quote 0
          • W
            wasawi 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

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Hi,

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • H
                HalfTough 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.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post