Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating QImage from raw uchar pointer

Creating QImage from raw uchar pointer

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimagebytesperlinelinestride
6 Posts 4 Posters 1.8k Views
  • 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 Offline
    W Offline
    wasawi
    wrote on last edited by wasawi
    #1

    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()
    	);
    }
    
    jsulmJ 1 Reply Last reply
    0
    • W 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()
      	);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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
      1
      • W Offline
        W Offline
        wasawi
        wrote on last edited by
        #3

        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
        0
        • W Offline
          W Offline
          wasawi
          wrote on last edited by
          #4

          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
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            0
            • H Offline
              H Offline
              HalfTough
              wrote on last edited by
              #6

              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
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved