imageanalyzer.cpp Example File
webkit/imageanalyzer/imageanalyzer.cpp
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkDiskCache>
#include "imageanalyzer.h"
ImageAnalyzer::ImageAnalyzer(QNetworkDiskCache* netcache, QObject* parent)
: QObject(parent), m_cache(netcache), m_outstandingFetches(0)
{
m_network = new QNetworkAccessManager(this);
m_watcher = new QFutureWatcher<QRgb>(this);
m_network->setCache(m_cache);
QObject::connect(m_network, SIGNAL(finished(QNetworkReply*)),
this, SLOT(handleReply(QNetworkReply*)));
QObject::connect(m_watcher, SIGNAL(finished()),
this, SLOT(doneProcessing()));
QObject::connect(m_watcher, SIGNAL(progressValueChanged(int)),
this, SLOT(progressStatus(int)));
}
ImageAnalyzer::~ImageAnalyzer()
{
delete(m_watcher);
}
QRgb ImageAnalyzer::lastResults()
{
int rTot = 0;
int bTot = 0;
int gTot = 0;
int count = m_watcher->future().results().size();
foreach(const QRgb & triplet, m_watcher->future().results())
{
rTot += qRed(triplet);
bTot += qBlue(triplet);
gTot += qGreen(triplet);
}
return qRgb(rTot/count, bTot/count, gTot/count);
}
float ImageAnalyzer::lastRed() { return qRed(lastResults())/2.55; }
float ImageAnalyzer::lastGreen() { return qGreen(lastResults())/2.55; }
float ImageAnalyzer::lastBlue() { return qBlue(lastResults())/2.55; }
void ImageAnalyzer::progressStatus(int newstat)
{
emit updateProgress(newstat, m_watcher->progressMaximum());
}
bool ImageAnalyzer::isBusy()
{
return m_watcher->isRunning();
}
void ImageAnalyzer::startAnalysis(const QStringList & urls)
{
m_URLQueue = urls;
fetchURLs();
}
void ImageAnalyzer::fetchURLs()
{
while (!m_URLQueue.isEmpty())
{
QString url = m_URLQueue.takeFirst();
QUrl URL = QUrl(url);
QIODevice * pData = m_cache->data(URL);
if (pData == 0) {
m_outstandingFetches++;
QNetworkRequest request = QNetworkRequest(URL);
request.setRawHeader("User-Agent", "Nokia - Custom QT app");
m_network->get(request);
} else {
QImage image;
image.load(pData, 0);
if (!image.isNull())
queueImage(image);
delete(pData);
}
}
}
void ImageAnalyzer::handleReply(QNetworkReply * pReply)
{
m_outstandingFetches--;
if (pReply->error()) {
qDebug() << "Error code" << pReply->error();
qDebug() << "Http code" << pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
return;
}
QImage image;
image.load(pReply, 0);
pReply->deleteLater();
if (image.isNull()) {
qDebug() << "bad image";
qDebug() << pReply->rawHeaderList();
foreach(QByteArray element, pReply->rawHeaderList()) {
qDebug() << element << " = " << pReply->rawHeader(element);
}
return;
}
queueImage(image);
}
void ImageAnalyzer::doneProcessing()
{
m_imageQueue = QList<QImage>();
emit finishedAnalysis();
}
void ImageAnalyzer::queueImage(QImage img)
{
if (!img.isNull())
m_imageQueue << img;
if (m_outstandingFetches == 0 && m_URLQueue.isEmpty()) {
m_watcher->setFuture(QtConcurrent::mapped(m_imageQueue, averageRGB));
}
}
QRgb averageRGB(const QImage &img)
{
int pixelCount = img.width() * img.height();
int rAvg, gAvg, bAvg;
for (int timeWaster=0; timeWaster < 100; timeWaster++) {
quint64 rTot = 0;
quint64 gTot = 0;
quint64 bTot = 0;
for (int i=0; i < img.width(); i++) {
for (int j=0; j < img.height(); j++) {
QRgb pixel = img.pixel(i,j);
rTot += qRed(pixel);
gTot += qGreen(pixel);
bTot += qBlue(pixel);
}
}
rAvg = (rTot)/(pixelCount);
gAvg = (gTot)/(pixelCount);
bAvg = (bTot)/(pixelCount);
}
return qRgb(rAvg, gAvg, bAvg);
}