0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui>
|
|
43 |
|
|
44 |
#include <math.h>
|
|
45 |
|
|
46 |
#include "renderthread.h"
|
|
47 |
|
|
48 |
//! [0]
|
|
49 |
RenderThread::RenderThread(QObject *parent)
|
|
50 |
: QThread(parent)
|
|
51 |
{
|
|
52 |
restart = false;
|
|
53 |
abort = false;
|
|
54 |
|
|
55 |
for (int i = 0; i < ColormapSize; ++i)
|
|
56 |
colormap[i] = rgbFromWaveLength(380.0 + (i * 400.0 / ColormapSize));
|
|
57 |
}
|
|
58 |
//! [0]
|
|
59 |
|
|
60 |
//! [1]
|
|
61 |
RenderThread::~RenderThread()
|
|
62 |
{
|
|
63 |
mutex.lock();
|
|
64 |
abort = true;
|
|
65 |
condition.wakeOne();
|
|
66 |
mutex.unlock();
|
|
67 |
|
|
68 |
wait();
|
|
69 |
}
|
|
70 |
//! [1]
|
|
71 |
|
|
72 |
//! [2]
|
|
73 |
void RenderThread::render(double centerX, double centerY, double scaleFactor,
|
|
74 |
QSize resultSize)
|
|
75 |
{
|
|
76 |
QMutexLocker locker(&mutex);
|
|
77 |
|
|
78 |
this->centerX = centerX;
|
|
79 |
this->centerY = centerY;
|
|
80 |
this->scaleFactor = scaleFactor;
|
|
81 |
this->resultSize = resultSize;
|
|
82 |
|
|
83 |
if (!isRunning()) {
|
|
84 |
start(LowPriority);
|
|
85 |
} else {
|
|
86 |
restart = true;
|
|
87 |
condition.wakeOne();
|
|
88 |
}
|
|
89 |
}
|
|
90 |
//! [2]
|
|
91 |
|
|
92 |
//! [3]
|
|
93 |
void RenderThread::run()
|
|
94 |
{
|
|
95 |
forever {
|
|
96 |
mutex.lock();
|
|
97 |
QSize resultSize = this->resultSize;
|
|
98 |
double scaleFactor = this->scaleFactor;
|
|
99 |
double centerX = this->centerX;
|
|
100 |
double centerY = this->centerY;
|
|
101 |
mutex.unlock();
|
|
102 |
//! [3]
|
|
103 |
|
|
104 |
//! [4]
|
|
105 |
int halfWidth = resultSize.width() / 2;
|
|
106 |
//! [4] //! [5]
|
|
107 |
int halfHeight = resultSize.height() / 2;
|
|
108 |
QImage image(resultSize, QImage::Format_RGB32);
|
|
109 |
|
|
110 |
const int NumPasses = 8;
|
|
111 |
int pass = 0;
|
|
112 |
while (pass < NumPasses) {
|
|
113 |
const int MaxIterations = (1 << (2 * pass + 6)) + 32;
|
|
114 |
const int Limit = 4;
|
|
115 |
bool allBlack = true;
|
|
116 |
|
|
117 |
for (int y = -halfHeight; y < halfHeight; ++y) {
|
|
118 |
if (restart)
|
|
119 |
break;
|
|
120 |
if (abort)
|
|
121 |
return;
|
|
122 |
|
|
123 |
uint *scanLine =
|
|
124 |
reinterpret_cast<uint *>(image.scanLine(y + halfHeight));
|
|
125 |
double ay = centerY + (y * scaleFactor);
|
|
126 |
|
|
127 |
for (int x = -halfWidth; x < halfWidth; ++x) {
|
|
128 |
double ax = centerX + (x * scaleFactor);
|
|
129 |
double a1 = ax;
|
|
130 |
double b1 = ay;
|
|
131 |
int numIterations = 0;
|
|
132 |
|
|
133 |
do {
|
|
134 |
++numIterations;
|
|
135 |
double a2 = (a1 * a1) - (b1 * b1) + ax;
|
|
136 |
double b2 = (2 * a1 * b1) + ay;
|
|
137 |
if ((a2 * a2) + (b2 * b2) > Limit)
|
|
138 |
break;
|
|
139 |
|
|
140 |
++numIterations;
|
|
141 |
a1 = (a2 * a2) - (b2 * b2) + ax;
|
|
142 |
b1 = (2 * a2 * b2) + ay;
|
|
143 |
if ((a1 * a1) + (b1 * b1) > Limit)
|
|
144 |
break;
|
|
145 |
} while (numIterations < MaxIterations);
|
|
146 |
|
|
147 |
if (numIterations < MaxIterations) {
|
|
148 |
*scanLine++ = colormap[numIterations % ColormapSize];
|
|
149 |
allBlack = false;
|
|
150 |
} else {
|
|
151 |
*scanLine++ = qRgb(0, 0, 0);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
if (allBlack && pass == 0) {
|
|
157 |
pass = 4;
|
|
158 |
} else {
|
|
159 |
if (!restart)
|
|
160 |
emit renderedImage(image, scaleFactor);
|
|
161 |
//! [5] //! [6]
|
|
162 |
++pass;
|
|
163 |
}
|
|
164 |
//! [6] //! [7]
|
|
165 |
}
|
|
166 |
//! [7]
|
|
167 |
|
|
168 |
//! [8]
|
|
169 |
mutex.lock();
|
|
170 |
//! [8] //! [9]
|
|
171 |
if (!restart)
|
|
172 |
condition.wait(&mutex);
|
|
173 |
restart = false;
|
|
174 |
mutex.unlock();
|
|
175 |
}
|
|
176 |
}
|
|
177 |
//! [9]
|
|
178 |
|
|
179 |
//! [10]
|
|
180 |
uint RenderThread::rgbFromWaveLength(double wave)
|
|
181 |
{
|
|
182 |
double r = 0.0;
|
|
183 |
double g = 0.0;
|
|
184 |
double b = 0.0;
|
|
185 |
|
|
186 |
if (wave >= 380.0 && wave <= 440.0) {
|
|
187 |
r = -1.0 * (wave - 440.0) / (440.0 - 380.0);
|
|
188 |
b = 1.0;
|
|
189 |
} else if (wave >= 440.0 && wave <= 490.0) {
|
|
190 |
g = (wave - 440.0) / (490.0 - 440.0);
|
|
191 |
b = 1.0;
|
|
192 |
} else if (wave >= 490.0 && wave <= 510.0) {
|
|
193 |
g = 1.0;
|
|
194 |
b = -1.0 * (wave - 510.0) / (510.0 - 490.0);
|
|
195 |
} else if (wave >= 510.0 && wave <= 580.0) {
|
|
196 |
r = (wave - 510.0) / (580.0 - 510.0);
|
|
197 |
g = 1.0;
|
|
198 |
} else if (wave >= 580.0 && wave <= 645.0) {
|
|
199 |
r = 1.0;
|
|
200 |
g = -1.0 * (wave - 645.0) / (645.0 - 580.0);
|
|
201 |
} else if (wave >= 645.0 && wave <= 780.0) {
|
|
202 |
r = 1.0;
|
|
203 |
}
|
|
204 |
|
|
205 |
double s = 1.0;
|
|
206 |
if (wave > 700.0)
|
|
207 |
s = 0.3 + 0.7 * (780.0 - wave) / (780.0 - 700.0);
|
|
208 |
else if (wave < 420.0)
|
|
209 |
s = 0.3 + 0.7 * (wave - 380.0) / (420.0 - 380.0);
|
|
210 |
|
|
211 |
r = pow(r * s, 0.8);
|
|
212 |
g = pow(g * s, 0.8);
|
|
213 |
b = pow(b * s, 0.8);
|
|
214 |
return qRgb(int(r * 255), int(g * 255), int(b * 255));
|
|
215 |
}
|
|
216 |
//! [10]
|