Results 1 to 6 of 6

Thread: How can I use concurrency to generate an QImage

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How can I use concurrency to generate an QImage

    Hi,

    I need to generate an QImage repeatedly from data using following code. How can I use QtConcurrent to take advantage of multi-cores computer?

    Thanks!

    Qt Code:
    1. QImage toImage( QVector<const float*> data, int width, int height )
    2. {
    3. QImage image( width, height, QImage::Format_ARGB32 );
    4.  
    5. for ( int iy = 0; iy < image.height(); iy++ ) {
    6. for ( int ix = 0; ix < image.width(); ix++ ) {
    7. QRgb rgb = calcRbgFromSomewhere( data[ ix ][ iy ] );
    8. image.setPixel( ix, iy, pixel );
    9. }
    10. }
    11. return image;
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I use concurrency to generate an QImage

    Hi!

    Yeah.. the docs on the QtConcurrent stuff are really poor!

    What you could do is store your float data together with the width and height value together in a small class, like so:

    Qt Code:
    1. class FloatImage
    2. {
    3. public:
    4. FloatImage(QVector<const float*> data, int width, int height) : data(data),width(width),height(height) {}
    5. QVector<const float*> data;
    6. int width;
    7. int height;
    8. QImage toImage()
    9. {
    10. // Your code..
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 
    You can call this with QtConcurrent::run

    Qt Code:
    1. FloatImage fi(yourData,200,100);
    2. QFutureSynchronizer<QImage> synchronizer;
    3. synchronizer.addFuture( QtConcurrent::run(fi,&FloatImage::toImage));
    4. ...
    5. synchronizer.waitForFinished();
    To copy to clipboard, switch view to plain text mode 

    This shows how to wait for multiple operations with QFutureSynchronizer!

    HIH

    Johannes

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I use concurrency to generate an QImage

    But this way you will only use one core. It's better to use the "map" semantics and let every pixel or scanline be processed by a different thread.
    For instance:
    Qt Code:
    1. QImage img(640,480);
    2. struct Elem {
    3. uchar *scanLine;
    4. int lineIndex;
    5. int lineSize;
    6. };
    7.  
    8. void func(Elem &e) {
    9. for(int i=0;i<e.lineSize;++i){
    10. QRgb rgb = calc(i, e.lineIndex);
    11. e.scanLine[i] = rgb; // or something like this
    12. }
    13. }
    14.  
    15. QList<Elem> image;
    16. for(int i=0;i<img.height();++i){
    17. Elem e;
    18. e.scanLine = img.scanLine(i);
    19. e.lineIndex = i;
    20. e.lineSize = img.width();
    21. }
    22. QtConcurrent::blockingMap(image, func);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I use concurrency to generate an QImage

    Hello wysota!

    I understood lni as if he had multiple images to process. For that he only would need to call QtConcurrent:run multiple times with his different images. I tried to indicate that with "..."

    But now he has examples for both granularities!

    I think "image.append(e);" is missing after line 20 in your code.

    Cheers,

    Joh
    Last edited by JohannesMunk; 30th August 2011 at 15:05.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How can I use concurrency to generate an QImage

    Yes, of course, there is a missing line in my code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I use concurrency to generate an QImage

    Thank you all!! I will try that.

Similar Threads

  1. Replies: 5
    Last Post: 16th May 2011, 21:15
  2. Generate an exe from an exe
    By swethapradeep in forum Qt Programming
    Replies: 4
    Last Post: 25th March 2011, 07:51
  3. concurrency issues with QwtData
    By sun in forum Qwt
    Replies: 1
    Last Post: 28th January 2010, 09:08
  4. Generate PDF
    By lexfridman in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2008, 12:44
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

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
  •  
Qt is a trademark of The Qt Company.