1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: renderthread.cpp Example File (threads/mandelbrot/renderthread.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">renderthread.cpp Example File<br /><small><small>threads/mandelbrot/renderthread.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
50
#include <math.h>
52
#include "renderthread.h"
54
RenderThread::RenderThread(QObject *parent)
60
for (int i = 0; i < ColormapSize; ++i)
61
colormap[i] = rgbFromWaveLength(380.0 + (i * 400.0 / ColormapSize));
64
RenderThread::~RenderThread()
74
void RenderThread::render(double centerX, double centerY, double scaleFactor,
77
QMutexLocker locker(&mutex);
79
this->centerX = centerX;
80
this->centerY = centerY;
81
this->scaleFactor = scaleFactor;
82
this->resultSize = resultSize;
92
void RenderThread::run()
96
QSize resultSize = this->resultSize;
97
double scaleFactor = this->scaleFactor;
98
double centerX = this->centerX;
99
double centerY = this->centerY;
102
int halfWidth = resultSize.width() / 2;
103
int halfHeight = resultSize.height() / 2;
104
QImage image(resultSize, QImage::Format_RGB32);
106
const int NumPasses = 8;
108
while (pass < NumPasses) {
109
const int MaxIterations = (1 << (2 * pass + 6)) + 32;
111
bool allBlack = true;
113
for (int y = -halfHeight; y < halfHeight; ++y) {
120
reinterpret_cast<uint *>(image.scanLine(y + halfHeight));
121
double ay = centerY + (y * scaleFactor);
123
for (int x = -halfWidth; x < halfWidth; ++x) {
124
double ax = centerX + (x * scaleFactor);
127
int numIterations = 0;
131
double a2 = (a1 * a1) - (b1 * b1) + ax;
132
double b2 = (2 * a1 * b1) + ay;
133
if ((a2 * a2) + (b2 * b2) > Limit)
137
a1 = (a2 * a2) - (b2 * b2) + ax;
138
b1 = (2 * a2 * b2) + ay;
139
if ((a1 * a1) + (b1 * b1) > Limit)
141
} while (numIterations < MaxIterations);
143
if (numIterations < MaxIterations) {
144
*scanLine++ = colormap[numIterations % ColormapSize];
147
*scanLine++ = qRgb(0, 0, 0);
152
if (allBlack && pass == 0) {
156
emit renderedImage(image, scaleFactor);
163
condition.wait(&mutex);
169
uint RenderThread::rgbFromWaveLength(double wave)
175
if (wave >= 380.0 && wave <= 440.0) {
176
r = -1.0 * (wave - 440.0) / (440.0 - 380.0);
178
} else if (wave >= 440.0 && wave <= 490.0) {
179
g = (wave - 440.0) / (490.0 - 440.0);
181
} else if (wave >= 490.0 && wave <= 510.0) {
183
b = -1.0 * (wave - 510.0) / (510.0 - 490.0);
184
} else if (wave >= 510.0 && wave <= 580.0) {
185
r = (wave - 510.0) / (580.0 - 510.0);
187
} else if (wave >= 580.0 && wave <= 645.0) {
189
g = -1.0 * (wave - 645.0) / (645.0 - 580.0);
190
} else if (wave >= 645.0 && wave <= 780.0) {
196
s = 0.3 + 0.7 * (780.0 - wave) / (780.0 - 700.0);
197
else if (wave < 420.0)
198
s = 0.3 + 0.7 * (wave - 380.0) / (420.0 - 380.0);
203
return qRgb(int(r * 255), int(g * 255), int(b * 255));
205
<p /><address><hr /><div align="center">
206
<table width="100%" cellspacing="0" border="0"><tr class="address">
207
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
208
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
209
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
210
</tr></table></div></address></body>