Results 1 to 9 of 9

Thread: Multithreaded per pixel operations on QImage

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Multithreaded per pixel operations on QImage

    Hi, I'm writing a small program which does per pixel operations on an QImage. Because of its use to handle large images I want several threads to work on the image. Therefore I handle n parts of the image with QtConcurrent, which sadly doesn't work properly.
    As a first test I just try to convert the image to grayscale, but instead of gray the Image gets black, just a few Pixels (ordered in a characteristic pattern ) really get gray. I add an result image under the post...
    Here is the code I wrote:

    Qt Code:
    1. // imgOrig is QImage* which holds the original image
    2. // imgDone is QImage* which should hold the result image
    3.  
    4. // function which is called
    5. void Scan::convertToGrayscale() {
    6. // get best number of threads
    7. int n = QThreadPool::globalInstance()->maxThreadCount();
    8. // calculate part width (each thread will handle a part of the image)
    9. int step = imgOrig->width() / n;
    10. for (int i=0; i<n; i++) {
    11. if (i+1 < n) {
    12. QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
    13. }
    14. else {
    15. // the last part should really reach the end (int a/b*b != a)
    16. QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, imgOrig->width());
    17. }
    18. }
    19. QThreadPool::globalInstance()->waitForDone();
    20. }
    21.  
    22. // function to handle a part of the image
    23. void Scan::convertToGrayscale(int from, int to) {
    24. int temp;
    25. for (int i=from; i<to; i++) {
    26. for (int j=0; j<imgOrig->height(); j++) {
    27. temp = qGray(imgOrig->pixel(i, j));
    28. imgDone->setPixel(i, j, qRgb(temp, temp, temp));
    29. }
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    The Screenshot was scaled down to 25% in width and heigth, but you can still see the strange paterns. The System I runned the program on has two cores, as you can see the image really is split into two halfes with similar patern. Is this misbehaviour because both threads write into the same QImage or something?

    PS: Qt 4.5 under Windows XP SP3, don't know if this matters.
    Attached Images Attached Images
    Last edited by N¤X; 11th September 2009 at 22:56.

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.