~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/threads-mandelbrot-renderthread-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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">
 
5
<head>
 
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>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
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.
 
29
**
 
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.
 
34
**
 
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.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
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.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
 
 
50
    #include &lt;math.h&gt;
 
51
 
 
52
    #include &quot;renderthread.h&quot;
 
53
 
 
54
    RenderThread::RenderThread(QObject *parent)
 
55
        : QThread(parent)
 
56
    {
 
57
        restart = false;
 
58
        abort = false;
 
59
 
 
60
        for (int i = 0; i &lt; ColormapSize; ++i)
 
61
            colormap[i] = rgbFromWaveLength(380.0 + (i * 400.0 / ColormapSize));
 
62
    }
 
63
 
 
64
    RenderThread::~RenderThread()
 
65
    {
 
66
        mutex.lock();
 
67
        abort = true;
 
68
        condition.wakeOne();
 
69
        mutex.unlock();
 
70
 
 
71
        wait();
 
72
    }
 
73
 
 
74
    void RenderThread::render(double centerX, double centerY, double scaleFactor,
 
75
                              QSize resultSize)
 
76
    {
 
77
        QMutexLocker locker(&amp;mutex);
 
78
 
 
79
        this-&gt;centerX = centerX;
 
80
        this-&gt;centerY = centerY;
 
81
        this-&gt;scaleFactor = scaleFactor;
 
82
        this-&gt;resultSize = resultSize;
 
83
 
 
84
        if (!isRunning()) {
 
85
            start(LowPriority);
 
86
        } else {
 
87
            restart = true;
 
88
            condition.wakeOne();
 
89
        }
 
90
    }
 
91
 
 
92
    void RenderThread::run()
 
93
    {
 
94
        forever {
 
95
            mutex.lock();
 
96
            QSize resultSize = this-&gt;resultSize;
 
97
            double scaleFactor = this-&gt;scaleFactor;
 
98
            double centerX = this-&gt;centerX;
 
99
            double centerY = this-&gt;centerY;
 
100
            mutex.unlock();
 
101
 
 
102
            int halfWidth = resultSize.width() / 2;
 
103
            int halfHeight = resultSize.height() / 2;
 
104
            QImage image(resultSize, QImage::Format_RGB32);
 
105
 
 
106
            const int NumPasses = 8;
 
107
            int pass = 0;
 
108
            while (pass &lt; NumPasses) {
 
109
                const int MaxIterations = (1 &lt;&lt; (2 * pass + 6)) + 32;
 
110
                const int Limit = 4;
 
111
                bool allBlack = true;
 
112
 
 
113
                for (int y = -halfHeight; y &lt; halfHeight; ++y) {
 
114
                    if (restart)
 
115
                        break;
 
116
                    if (abort)
 
117
                        return;
 
118
 
 
119
                    uint *scanLine =
 
120
                            reinterpret_cast&lt;uint *&gt;(image.scanLine(y + halfHeight));
 
121
                    double ay = centerY + (y * scaleFactor);
 
122
 
 
123
                    for (int x = -halfWidth; x &lt; halfWidth; ++x) {
 
124
                        double ax = centerX + (x * scaleFactor);
 
125
                        double a1 = ax;
 
126
                        double b1 = ay;
 
127
                        int numIterations = 0;
 
128
 
 
129
                        do {
 
130
                            ++numIterations;
 
131
                            double a2 = (a1 * a1) - (b1 * b1) + ax;
 
132
                            double b2 = (2 * a1 * b1) + ay;
 
133
                            if ((a2 * a2) + (b2 * b2) &gt; Limit)
 
134
                                break;
 
135
 
 
136
                            ++numIterations;
 
137
                            a1 = (a2 * a2) - (b2 * b2) + ax;
 
138
                            b1 = (2 * a2 * b2) + ay;
 
139
                            if ((a1 * a1) + (b1 * b1) &gt; Limit)
 
140
                                break;
 
141
                        } while (numIterations &lt; MaxIterations);
 
142
 
 
143
                        if (numIterations &lt; MaxIterations) {
 
144
                            *scanLine++ = colormap[numIterations % ColormapSize];
 
145
                            allBlack = false;
 
146
                        } else {
 
147
                            *scanLine++ = qRgb(0, 0, 0);
 
148
                        }
 
149
                    }
 
150
                }
 
151
 
 
152
                if (allBlack &amp;&amp; pass == 0) {
 
153
                    pass = 4;
 
154
                } else {
 
155
                    if (!restart)
 
156
                        emit renderedImage(image, scaleFactor);
 
157
                    ++pass;
 
158
                }
 
159
            }
 
160
 
 
161
            mutex.lock();
 
162
            if (!restart)
 
163
                condition.wait(&amp;mutex);
 
164
            restart = false;
 
165
            mutex.unlock();
 
166
        }
 
167
    }
 
168
 
 
169
    uint RenderThread::rgbFromWaveLength(double wave)
 
170
    {
 
171
        double r = 0.0;
 
172
        double g = 0.0;
 
173
        double b = 0.0;
 
174
 
 
175
        if (wave &gt;= 380.0 &amp;&amp; wave &lt;= 440.0) {
 
176
            r = -1.0 * (wave - 440.0) / (440.0 - 380.0);
 
177
            b = 1.0;
 
178
        } else if (wave &gt;= 440.0 &amp;&amp; wave &lt;= 490.0) {
 
179
            g = (wave - 440.0) / (490.0 - 440.0);
 
180
            b = 1.0;
 
181
        } else if (wave &gt;= 490.0 &amp;&amp; wave &lt;= 510.0) {
 
182
            g = 1.0;
 
183
            b = -1.0 * (wave - 510.0) / (510.0 - 490.0);
 
184
        } else if (wave &gt;= 510.0 &amp;&amp; wave &lt;= 580.0) {
 
185
            r = (wave - 510.0) / (580.0 - 510.0);
 
186
            g = 1.0;
 
187
        } else if (wave &gt;= 580.0 &amp;&amp; wave &lt;= 645.0) {
 
188
            r = 1.0;
 
189
            g = -1.0 * (wave - 645.0) / (645.0 - 580.0);
 
190
        } else if (wave &gt;= 645.0 &amp;&amp; wave &lt;= 780.0) {
 
191
            r = 1.0;
 
192
        }
 
193
 
 
194
        double s = 1.0;
 
195
        if (wave &gt; 700.0)
 
196
            s = 0.3 + 0.7 * (780.0 - wave) / (780.0 - 700.0);
 
197
        else if (wave &lt;  420.0)
 
198
            s = 0.3 + 0.7 * (wave - 380.0) / (420.0 - 380.0);
 
199
 
 
200
        r = pow(r * s, 0.8);
 
201
        g = pow(g * s, 0.8);
 
202
        b = pow(b * s, 0.8);
 
203
        return qRgb(int(r * 255), int(g * 255), int(b * 255));
 
204
    }</pre>
 
205
<p /><address><hr /><div align="center">
 
206
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
207
<td width="30%">Copyright &copy; 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>
 
211
</html>