// imgOrig is QImage* which holds the original image
// imgDone is QImage* which should hold the result image
// function which is called
void Scan::convertToGrayscale() {
// get best number of threads
int n = QThreadPool::globalInstance()->maxThreadCount();
// calculate part width (each thread will handle a part of the image)
int step = imgOrig->width() / n;
for (int i=0; i<n; i++) {
if (i+1 < n) {
QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
}
else {
// the last part should really reach the end (int a/b*b != a)
QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, imgOrig->width());
}
}
QThreadPool::globalInstance()->waitForDone();
}
// function to handle a part of the image
void Scan::convertToGrayscale(int from, int to) {
int temp;
for (int i=from; i<to; i++) {
for (int j=0; j<imgOrig->height(); j++) {
temp = qGray(imgOrig->pixel(i, j));
imgDone->setPixel(i, j, qRgb(temp, temp, temp));
}
}
}
// imgOrig is QImage* which holds the original image
// imgDone is QImage* which should hold the result image
// function which is called
void Scan::convertToGrayscale() {
// get best number of threads
int n = QThreadPool::globalInstance()->maxThreadCount();
// calculate part width (each thread will handle a part of the image)
int step = imgOrig->width() / n;
for (int i=0; i<n; i++) {
if (i+1 < n) {
QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, (i+1)*step);
}
else {
// the last part should really reach the end (int a/b*b != a)
QtConcurrent::run(this, &Scan::convertToGrayscale, i*step, imgOrig->width());
}
}
QThreadPool::globalInstance()->waitForDone();
}
// function to handle a part of the image
void Scan::convertToGrayscale(int from, int to) {
int temp;
for (int i=from; i<to; i++) {
for (int j=0; j<imgOrig->height(); j++) {
temp = qGray(imgOrig->pixel(i, j));
imgDone->setPixel(i, j, qRgb(temp, temp, temp));
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks