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

« back to all changes in this revision

Viewing changes to examples/threads/mandelbrot/renderthread.cpp

  • 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
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include <math.h>
 
32
 
 
33
#include "renderthread.h"
 
34
 
 
35
RenderThread::RenderThread(QObject *parent)
 
36
    : QThread(parent)
 
37
{
 
38
    restart = false;
 
39
    abort = false;
 
40
 
 
41
    for (int i = 0; i < ColormapSize; ++i)
 
42
        colormap[i] = rgbFromWaveLength(380.0 + (i * 400.0 / ColormapSize));
 
43
}
 
44
 
 
45
RenderThread::~RenderThread()
 
46
{
 
47
    mutex.lock();
 
48
    abort = true;
 
49
    condition.wakeOne();
 
50
    mutex.unlock();
 
51
 
 
52
    wait();
 
53
}
 
54
 
 
55
void RenderThread::render(double centerX, double centerY, double scaleFactor,
 
56
                          QSize resultSize)
 
57
{
 
58
    QMutexLocker locker(&mutex);
 
59
 
 
60
    this->centerX = centerX;
 
61
    this->centerY = centerY;
 
62
    this->scaleFactor = scaleFactor;
 
63
    this->resultSize = resultSize;
 
64
 
 
65
    if (!isRunning()) {
 
66
        start(LowPriority);
 
67
    } else {
 
68
        restart = true;
 
69
        condition.wakeOne();
 
70
    }
 
71
}
 
72
 
 
73
void RenderThread::run()
 
74
{
 
75
    forever {
 
76
        mutex.lock();
 
77
        QSize resultSize = this->resultSize;
 
78
        double scaleFactor = this->scaleFactor;
 
79
        double centerX = this->centerX;
 
80
        double centerY = this->centerY;
 
81
        mutex.unlock();
 
82
 
 
83
        int halfWidth = resultSize.width() / 2;
 
84
        int halfHeight = resultSize.height() / 2;
 
85
        QImage image(resultSize, QImage::Format_RGB32);
 
86
 
 
87
        const int NumPasses = 8;
 
88
        int pass = 0;
 
89
        while (pass < NumPasses) {
 
90
            const int MaxIterations = (1 << (2 * pass + 6)) + 32;
 
91
            const int Limit = 4;
 
92
            bool allBlack = true;
 
93
 
 
94
            for (int y = -halfHeight; y < halfHeight; ++y) {
 
95
                if (restart)
 
96
                    break;
 
97
                if (abort)
 
98
                    return;
 
99
 
 
100
                uint *scanLine =
 
101
                        reinterpret_cast<uint *>(image.scanLine(y + halfHeight));
 
102
                double ay = centerY + (y * scaleFactor);
 
103
 
 
104
                for (int x = -halfWidth; x < halfWidth; ++x) {
 
105
                    double ax = centerX + (x * scaleFactor);
 
106
                    double a1 = ax;
 
107
                    double b1 = ay;
 
108
                    int numIterations = 0;
 
109
 
 
110
                    do {
 
111
                        ++numIterations;
 
112
                        double a2 = (a1 * a1) - (b1 * b1) + ax;
 
113
                        double b2 = (2 * a1 * b1) + ay;
 
114
                        if ((a2 * a2) + (b2 * b2) > Limit)
 
115
                            break;
 
116
 
 
117
                        ++numIterations;
 
118
                        a1 = (a2 * a2) - (b2 * b2) + ax;
 
119
                        b1 = (2 * a2 * b2) + ay;
 
120
                        if ((a1 * a1) + (b1 * b1) > Limit)
 
121
                            break;
 
122
                    } while (numIterations < MaxIterations);
 
123
 
 
124
                    if (numIterations < MaxIterations) {
 
125
                        *scanLine++ = colormap[numIterations % ColormapSize];
 
126
                        allBlack = false;
 
127
                    } else {
 
128
                        *scanLine++ = qRgb(0, 0, 0);
 
129
                    }
 
130
                }
 
131
            }
 
132
 
 
133
            if (allBlack && pass == 0) {
 
134
                pass = 4;
 
135
            } else {
 
136
                if (!restart)
 
137
                    emit renderedImage(image, scaleFactor);
 
138
                ++pass;
 
139
            }
 
140
        }
 
141
 
 
142
        mutex.lock();
 
143
        if (!restart)
 
144
            condition.wait(&mutex);
 
145
        restart = false;
 
146
        mutex.unlock();
 
147
    }
 
148
}
 
149
 
 
150
uint RenderThread::rgbFromWaveLength(double wave)
 
151
{
 
152
    double r = 0.0;
 
153
    double g = 0.0;
 
154
    double b = 0.0;
 
155
 
 
156
    if (wave >= 380.0 && wave <= 440.0) {
 
157
        r = -1.0 * (wave - 440.0) / (440.0 - 380.0);
 
158
        b = 1.0;
 
159
    } else if (wave >= 440.0 && wave <= 490.0) {
 
160
        g = (wave - 440.0) / (490.0 - 440.0);
 
161
        b = 1.0;
 
162
    } else if (wave >= 490.0 && wave <= 510.0) {
 
163
        g = 1.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);
 
167
        g = 1.0;
 
168
    } else if (wave >= 580.0 && wave <= 645.0) {
 
169
        r = 1.0;
 
170
        g = -1.0 * (wave - 645.0) / (645.0 - 580.0);
 
171
    } else if (wave >= 645.0 && wave <= 780.0) {
 
172
        r = 1.0;
 
173
    }
 
174
 
 
175
    double s = 1.0;
 
176
    if (wave > 700.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);
 
180
 
 
181
    r = pow(r * s, 0.8);
 
182
    g = pow(g * s, 0.8);
 
183
    b = pow(b * s, 0.8);
 
184
    return qRgb(int(r * 255), int(g * 255), int(b * 255));
 
185
}