Results 1 to 5 of 5

Thread: Re: How to effiecently copy individual pixels (with QImage)

  1. #1
    Join Date
    Jul 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to effiecently copy individual pixels (with QImage)

    I have lately been experiencing some performance issues when using the pixel() and setPixel() functions of the QImage class.
    Basically this is the code that caused all the issues.
    Qt Code:
    1. frame->setPixel(x,y, tex.pixel(pixelCoord));
    To copy to clipboard, switch view to plain text mode 
    frame as well as tex are of type QImage and QImage* respectively. They both are in the Format RGB32.
    This function will be called up to a few million times a second.
    All I want to do is to copy the colour of one pixel from one image into the other.
    Does anybody know why copying pixels like that is extremely slow (shouldn't it be just as quick as setting any other variable) and how I can speed up my code? (Preferrably but not neccessarily using QImage)
    Last edited by KillerBerry; 17th July 2015 at 23:58.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How to effiecently copy individual pixels (with QImage)

    Haven't done any direct image manipulation myself with Qt, but if you're processing every bit of the image one bit at a time, have you looked at processing by scan line using QImage::scanLine() and QImage::bits()?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to effiecently copy individual pixels (with QImage)

    This function will be called up to a few million times a second.
    Really? Are you iterating over entire frames of video in real-time?
    All I want to do is to copy the colour of one pixel from one image into the other.
    If you are iterating over the entire QImage pixel-by-pixel then you are far better off copying the QImage.
    Qt Code:
    1. *frame = tex;
    To copy to clipboard, switch view to plain text mode 
    If you are, for example, inserting a smaller tex image into a position in the frame then you could do that with QPainter (which can resize and subset the images for you also):
    Qt Code:
    1. #include <QApplication>
    2. #include <QImage>
    3. #include <QPainter>
    4. #include <QTime>
    5. #include <QDebug>
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QImage frame(1024, 768, QImage::Format_RGB32);
    12. frame.fill(Qt::white);
    13.  
    14. QImage tex(256, 256, QImage::Format_RGB32);
    15. tex.fill(Qt::green);
    16.  
    17. QTime timer;
    18. timer.start();
    19. for (int i = 0; i < 1000000; ++i) {
    20. p.begin(&frame);
    21. p.drawImage(QRectF(100, 100, 128, 128), tex, QRectF(128, 128, 128, 128), Qt::AutoColor);
    22. p.end();
    23. }
    24. // Copied 128x128x1000000 == 16,384,000,000 pixels, about 6500 milliseconds on my machine
    25. qDebug() << "Done in" << timer.elapsed() << "msec";
    26.  
    27. frame.save("frame.png");
    28.  
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to effiecently copy individual pixels (with QImage)

    The line of code is part of an algorithm I wrote which performs a Z-Buffer operation and afterwards perspective correct texture mapping. (Without OpenGL, that's the entire point of the project)
    What I basically do is to iterate over parts of the frame and calculate texture coordinates. I then want to grab the pixel from the texture at said coordinate and copy it into my frame buffer.
    I have found a solution using some funky pointer arithmetic, however this only works well if I keep the format at RGB32. Is there any standard way to efficiently copy pixels and why are the methods in QImage so slow? In the Qt documentation it says something about setPixel() but not pixel().

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to effiecently copy individual pixels (with QImage)

    pixel() and setPixel() need at least to calculate the offset based on the two coordinates.
    Direct access via bits() or scanline() doesn't.

    It could additionally be the case that the compiler can optimize the array access (which using bits() or scanline() effectively is) better than it can access a nested for loop (which you likely have when using setPixel/pixel).

    But you could easily benchmark these guesses:
    - create an image of a specific size
    - iterate over it using pixel/setPixel to add a constant number to each pixel value. measure the time
    - do the same thing using a single for loop and bits(). also measure time.

    Maybe write it using QTestLib and its Q_BENCHMARK feature to get even better statistics.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 10th June 2011, 15:16
  2. QImage Restricted to 256 Pixels?
    By strateng in forum Newbie
    Replies: 3
    Last Post: 6th April 2010, 01:51
  3. Replies: 16
    Last Post: 29th March 2010, 08:05
  4. How to change the values of the pixels in an Qimage?
    By kid17 in forum Qt Programming
    Replies: 8
    Last Post: 23rd November 2008, 21:52
  5. Convert RAW 8 bit pixels into a QImage
    By danielperaza in forum Qt Programming
    Replies: 3
    Last Post: 10th March 2008, 15:53

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.