1
/****************************************************************************
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
33
#include "renderthread.h"
35
RenderThread::RenderThread(QObject *parent)
41
for (int i = 0; i < ColormapSize; ++i)
42
colormap[i] = rgbFromWaveLength(380.0 + (i * 400.0 / ColormapSize));
45
RenderThread::~RenderThread()
55
void RenderThread::render(double centerX, double centerY, double scaleFactor,
58
QMutexLocker locker(&mutex);
60
this->centerX = centerX;
61
this->centerY = centerY;
62
this->scaleFactor = scaleFactor;
63
this->resultSize = resultSize;
73
void RenderThread::run()
77
QSize resultSize = this->resultSize;
78
double scaleFactor = this->scaleFactor;
79
double centerX = this->centerX;
80
double centerY = this->centerY;
83
int halfWidth = resultSize.width() / 2;
84
int halfHeight = resultSize.height() / 2;
85
QImage image(resultSize, QImage::Format_RGB32);
87
const int NumPasses = 8;
89
while (pass < NumPasses) {
90
const int MaxIterations = (1 << (2 * pass + 6)) + 32;
94
for (int y = -halfHeight; y < halfHeight; ++y) {
101
reinterpret_cast<uint *>(image.scanLine(y + halfHeight));
102
double ay = centerY + (y * scaleFactor);
104
for (int x = -halfWidth; x < halfWidth; ++x) {
105
double ax = centerX + (x * scaleFactor);
108
int numIterations = 0;
112
double a2 = (a1 * a1) - (b1 * b1) + ax;
113
double b2 = (2 * a1 * b1) + ay;
114
if ((a2 * a2) + (b2 * b2) > Limit)
118
a1 = (a2 * a2) - (b2 * b2) + ax;
119
b1 = (2 * a2 * b2) + ay;
120
if ((a1 * a1) + (b1 * b1) > Limit)
122
} while (numIterations < MaxIterations);
124
if (numIterations < MaxIterations) {
125
*scanLine++ = colormap[numIterations % ColormapSize];
128
*scanLine++ = qRgb(0, 0, 0);
133
if (allBlack && pass == 0) {
137
emit renderedImage(image, scaleFactor);
144
condition.wait(&mutex);
150
uint RenderThread::rgbFromWaveLength(double wave)
156
if (wave >= 380.0 && wave <= 440.0) {
157
r = -1.0 * (wave - 440.0) / (440.0 - 380.0);
159
} else if (wave >= 440.0 && wave <= 490.0) {
160
g = (wave - 440.0) / (490.0 - 440.0);
162
} else if (wave >= 490.0 && wave <= 510.0) {
164
b = -1.0 * (wave - 510.0) / (510.0 - 490.0);
165
} else if (wave >= 510.0 && wave <= 580.0) {
166
r = (wave - 510.0) / (580.0 - 510.0);
168
} else if (wave >= 580.0 && wave <= 645.0) {
170
g = -1.0 * (wave - 645.0) / (645.0 - 580.0);
171
} else if (wave >= 645.0 && wave <= 780.0) {
177
s = 0.3 + 0.7 * (780.0 - wave) / (780.0 - 700.0);
178
else if (wave < 420.0)
179
s = 0.3 + 0.7 * (wave - 380.0) / (420.0 - 380.0);
184
return qRgb(int(r * 255), int(g * 255), int(b * 255));