-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessimagesthread.cpp
More file actions
101 lines (93 loc) · 2.95 KB
/
Copy pathprocessimagesthread.cpp
File metadata and controls
101 lines (93 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/***************************************************************************************************
*
* FILE: processimagesthread.cpp
*
* CREATED: 10-05-2010
*
* AUTHOR: Benjamin Caspari (becaspari@googlemail.com)
*
* PURPOSE: analyses the images in a database in a separate thread
*
* This program is licensed under the terms of the GPL Version 2
*
* Copyright 2010 by Benjamin Caspari
*
***************************************************************************************************/
#include "processimagesthread.h"
#include <QImage>
#include <QFileInfo>
#include <QDebug>
#define log(text) Debug::log(text)
ProcessImagesThread::ProcessImagesThread()
{
m_cancelNow = false;
this->pictures = 0;
}
void ProcessImagesThread::cancel()
{
m_cancelNow = true;
}
void ProcessImagesThread::processImages(QVector<PictureInfo *> *pictures)
{
Q_ASSERT (pictures != 0);
this->pictures = pictures;
m_cancelNow = false;
start();
}
void ProcessImagesThread::run()
{
Q_ASSERT(this->pictures != 0);
qDebug() << ("ProcessImagesThread::run called");
for (int i = 0; i < this->pictures->size(); i++) {
processImage(pictures->value(i));
if (m_cancelNow) {
qDebug() << ("ProcessImagesThread::run was canceled");
return;
}
emit complete(100.f * (float)i / (float)(pictures->size() - 1));
}
qDebug() << ("ProcessImagesThread::run done completly");
}
bool ProcessImagesThread::processImage(PictureInfo *picture)
{
Q_ASSERT(picture != 0);
qDebug() << ("ProcessImagesThread::processImage called for file " +
picture->getFile());
QFileInfo fileInfo(picture->getFile());
if (!fileInfo.exists()) {
picture->setValidFile(false);
picture->setProcessed(false);
return false;
}
if ((picture->processed()) && (picture->lastChanged() == fileInfo.lastModified())) {
//picture info is up to date
return true;
}
picture->setLastChanged(fileInfo.lastModified());
QImage *image = new QImage(picture->getFile());
if (image->isNull()) {
picture->setValidFile(false);
delete image;
return false;
}
picture->setValidFile(true);
picture->setDimensions(image->width(), image->height());
unsigned long long red = 0, green = 0, blue = 0;
for (int i = 0; i < image->width(); i++) {
for (int j = 0; j < image->height(); j++) {
QRgb color = image->pixel(QPoint(i, j));
red += qRed(color);
green += qGreen(color);
blue += qBlue(color);
}
}
unsigned int pixels = image->width() * image->height();
red /= pixels;
green /= pixels;
blue /= pixels;
picture->setColor(red, green, blue);
delete image;
picture->setProcessed(true);
qDebug() << ("ProcessImagesThread::processImage done with success");
return true;
}