1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
#!/usr/bin/env python
############################################################################
#
# Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
#
# This file is part of the example classes of the Qt Toolkit.
#
# This file may be used under the terms of the GNU General Public
# License version 2.0 as published by the Free Software Foundation
# and appearing in the file LICENSE.GPL included in the packaging of
# self file. Please review the following information to ensure GNU
# General Public Licensing requirements will be met:
# http://www.trolltech.com/products/qt/opensource.html
#
# If you are unsure which license is appropriate for your use, please
# review the following information:
# http://www.trolltech.com/products/qt/licensing.html or contact the
# sales department at sales@trolltech.com.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
############################################################################
from PyQt4 import QtCore, QtGui
DefaultCenterX = -0.647011
DefaultCenterY = -0.0395159
DefaultScale = 0.00403897
ZoomInFactor = 0.8
ZoomOutFactor = 1 / ZoomInFactor
ScrollStep = 20
class RenderThread(QtCore.QThread):
ColormapSize = 512
renderedImage = QtCore.pyqtSignal(QtGui.QImage, float)
def __init__(self, parent=None):
super(RenderThread, self).__init__(parent)
self.mutex = QtCore.QMutex()
self.condition = QtCore.QWaitCondition()
self.centerX = 0.0
self.centerY = 0.0
self.scaleFactor = 0.0
self.resultSize = QtCore.QSize()
self.colormap = []
self.restart = False
self.abort = False
for i in range(RenderThread.ColormapSize):
self.colormap.append(self.rgbFromWaveLength(380.0 + (i * 400.0 / RenderThread.ColormapSize)))
def __del__(self):
self.mutex.lock()
self.abort = True
self.condition.wakeOne()
self.mutex.unlock()
self.wait()
def render(self, centerX, centerY, scaleFactor, resultSize):
locker = QtCore.QMutexLocker(self.mutex)
self.centerX = centerX
self.centerY = centerY
self.scaleFactor = scaleFactor
self.resultSize = resultSize
if not self.isRunning():
self.start(QtCore.QThread.LowPriority)
else:
self.restart = True
self.condition.wakeOne()
def run(self):
while True:
self.mutex.lock()
resultSize = self.resultSize
scaleFactor = self.scaleFactor
centerX = self.centerX
centerY = self.centerY
self.mutex.unlock()
halfWidth = resultSize.width() // 2
halfHeight = resultSize.height() // 2
image = QtGui.QImage(resultSize, QtGui.QImage.Format_RGB32)
NumPasses = 8
curpass = 0
while curpass < NumPasses:
MaxIterations = (1 << (2 * curpass + 6)) + 32
Limit = 4
allBlack = True
for y in range(-halfHeight, halfHeight):
if self.restart:
break
if self.abort:
return
ay = 1j * (centerY + (y * scaleFactor))
for x in range(-halfWidth, halfWidth):
c0 = centerX + (x * scaleFactor) + ay
c = c0
numIterations = 0
while numIterations < MaxIterations:
numIterations += 1
c = c*c + c0
if abs(c) >= Limit:
break
numIterations += 1
c = c*c + c0
if abs(c) >= Limit:
break
numIterations += 1
c = c*c + c0
if abs(c) >= Limit:
break
numIterations += 1
c = c*c + c0
if abs(c) >= Limit:
break
if numIterations < MaxIterations:
image.setPixel(x + halfWidth, y + halfHeight,
self.colormap[numIterations % RenderThread.ColormapSize])
allBlack = False
else:
image.setPixel(x + halfWidth, y + halfHeight, QtGui.qRgb(0, 0, 0))
if allBlack and curpass == 0:
curpass = 4
else:
if not self.restart:
self.renderedImage.emit(image, scaleFactor)
curpass += 1
self.mutex.lock()
if not self.restart:
self.condition.wait(self.mutex)
self.restart = False
self.mutex.unlock()
def rgbFromWaveLength(self, wave):
r = 0.0
g = 0.0
b = 0.0
if wave >= 380.0 and wave <= 440.0:
r = -1.0 * (wave - 440.0) / (440.0 - 380.0)
b = 1.0
elif wave >= 440.0 and wave <= 490.0:
g = (wave - 440.0) / (490.0 - 440.0)
b = 1.0
elif wave >= 490.0 and wave <= 510.0:
g = 1.0
b = -1.0 * (wave - 510.0) / (510.0 - 490.0)
elif wave >= 510.0 and wave <= 580.0:
r = (wave - 510.0) / (580.0 - 510.0)
g = 1.0
elif wave >= 580.0 and wave <= 645.0:
r = 1.0
g = -1.0 * (wave - 645.0) / (645.0 - 580.0)
elif wave >= 645.0 and wave <= 780.0:
r = 1.0
s = 1.0
if wave > 700.0:
s = 0.3 + 0.7 * (780.0 - wave) / (780.0 - 700.0)
elif wave < 420.0:
s = 0.3 + 0.7 * (wave - 380.0) / (420.0 - 380.0)
r = pow(r * s, 0.8)
g = pow(g * s, 0.8)
b = pow(b * s, 0.8)
return QtGui.qRgb(r*255, g*255, b*255)
class MandelbrotWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MandelbrotWidget, self).__init__(parent)
self.thread = RenderThread()
self.pixmap = QtGui.QPixmap()
self.pixmapOffset = QtCore.QPoint()
self.lastDragPos = QtCore.QPoint()
self.centerX = DefaultCenterX
self.centerY = DefaultCenterY
self.pixmapScale = DefaultScale
self.curScale = DefaultScale
self.thread.renderedImage.connect(self.updatePixmap)
self.setWindowTitle("Mandelbrot")
self.setCursor(QtCore.Qt.CrossCursor)
self.resize(550, 400)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.fillRect(self.rect(), QtCore.Qt.black)
if self.pixmap.isNull():
painter.setPen(QtCore.Qt.white)
painter.drawText(self.rect(), QtCore.Qt.AlignCenter,
"Rendering initial image, please wait...")
return
if self.curScale == self.pixmapScale:
painter.drawPixmap(self.pixmapOffset, self.pixmap)
else:
scaleFactor = self.pixmapScale / self.curScale
newWidth = int(self.pixmap.width() * scaleFactor)
newHeight = int(self.pixmap.height() * scaleFactor)
newX = self.pixmapOffset.x() + (self.pixmap.width() - newWidth) / 2
newY = self.pixmapOffset.y() + (self.pixmap.height() - newHeight) / 2
painter.save()
painter.translate(newX, newY)
painter.scale(scaleFactor, scaleFactor)
exposed, _ = painter.matrix().inverted()
exposed = exposed.mapRect(self.rect()).adjusted(-1, -1, 1, 1)
painter.drawPixmap(exposed, self.pixmap, exposed)
painter.restore()
text = "Use mouse wheel or the '+' and '-' keys to zoom. Press and " \
"hold left mouse button to scroll."
metrics = painter.fontMetrics()
textWidth = metrics.width(text)
painter.setPen(QtCore.Qt.NoPen)
painter.setBrush(QtGui.QColor(0, 0, 0, 127))
painter.drawRect((self.width() - textWidth) / 2 - 5, 0, textWidth + 10,
metrics.lineSpacing() + 5)
painter.setPen(QtCore.Qt.white)
painter.drawText((self.width() - textWidth) / 2,
metrics.leading() + metrics.ascent(), text)
def resizeEvent(self, event):
self.thread.render(self.centerX, self.centerY, self.curScale,
self.size())
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Plus:
self.zoom(ZoomInFactor)
elif event.key() == QtCore.Qt.Key_Minus:
self.zoom(ZoomOutFactor)
elif event.key() == QtCore.Qt.Key_Left:
self.scroll(-ScrollStep, 0)
elif event.key() == QtCore.Qt.Key_Right:
self.scroll(+ScrollStep, 0)
elif event.key() == QtCore.Qt.Key_Down:
self.scroll(0, -ScrollStep)
elif event.key() == QtCore.Qt.Key_Up:
self.scroll(0, +ScrollStep)
else:
super(MandelbrotWidget, self).keyPressEvent(event)
def wheelEvent(self, event):
numDegrees = event.delta() / 8
numSteps = numDegrees / 15.0
self.zoom(pow(ZoomInFactor, numSteps))
def mousePressEvent(self, event):
if event.buttons() == QtCore.Qt.LeftButton:
self.lastDragPos = QtCore.QPoint(event.pos())
def mouseMoveEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
self.pixmapOffset += event.pos() - self.lastDragPos
self.lastDragPos = QtCore.QPoint(event.pos())
self.update()
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.pixmapOffset += event.pos() - self.lastDragPos
self.lastDragPos = QtCore.QPoint()
deltaX = (self.width() - self.pixmap.width()) / 2 - self.pixmapOffset.x()
deltaY = (self.height() - self.pixmap.height()) / 2 - self.pixmapOffset.y()
self.scroll(deltaX, deltaY)
def updatePixmap(self, image, scaleFactor):
if not self.lastDragPos.isNull():
return
self.pixmap = QtGui.QPixmap.fromImage(image)
self.pixmapOffset = QtCore.QPoint()
self.lastDragPosition = QtCore.QPoint()
self.pixmapScale = scaleFactor
self.update()
def zoom(self, zoomFactor):
self.curScale *= zoomFactor
self.update()
self.thread.render(self.centerX, self.centerY, self.curScale,
self.size())
def scroll(self, deltaX, deltaY):
self.centerX += deltaX * self.curScale
self.centerY += deltaY * self.curScale
self.update()
self.thread.render(self.centerX, self.centerY, self.curScale,
self.size())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
widget = MandelbrotWidget()
widget.show()
sys.exit(app.exec_())
|