-
Notifications
You must be signed in to change notification settings - Fork 4
/
preview.py
132 lines (124 loc) · 4.08 KB
/
preview.py
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
from PySide.QtCore import *
from PySide.QtGui import *
from dutils import *
from movie import *
class Preview(QWidget):
def __init__(self, parent, movie):
QWidget.__init__(self, parent)
parent.setBackgroundRole(QPalette.Dark)
self.movie = movie
self.resize(self.movie.movieRect.width(), self.movie.movieRect.height())
self.parent().parent().parent().parent().selectionChanged.connect(self.selectionChanged)
def selectionChanged(self):
self.update()
def paintEvent(self, ev):
if self.movie.currFrame == -1:
return
painter = QPainter(self)
palette = getPaletteFor(self.movie.currFrame, self.movie)
bgcol = self.movie.stageColor & (2**self.movie.colorDepth - 1)
if bgcol < len(palette)/4: # TODO: ??
painter.setBrush(QBrush(QColor(ord(palette[4*bgcol+2]), ord(palette[4*bgcol+1]), ord(palette[4*bgcol]))))
painter.drawRect(0, 0, self.movie.movieRect.width(), self.movie.movieRect.height())
painter.setBrush(QBrush())
for i in range(channelCount):
info = self.movie.frames[self.movie.currFrame].sprites[i+1]
if not info.enabled:
continue
castinfo = None
if info.castId in self.movie.cast:
castinfo = self.movie.cast[info.castId]
myId = 1024 + info.castId
mystr = str(info.castId)
img = None
wasDib = False
if myId in self.movie.dibs:
dib = self.movie.dibs[myId]
dib.seek(0)
data = dib.read()
imgdata = imageFromDIB(data, palette)
img = QImage.fromData(imgdata, "bmp")
wasDib = True
elif myId in self.movie.bitmaps:
bitmap = self.movie.bitmaps[myId]
bitmap.seek(0)
data = bitmap.read()
data = unPackBits(data)
if castinfo:
rect = castinfo.initialRect
width = rect.width()
height = rect.height()
else:
# TODO
width = info.width
height = info.height
img = QImage(QSize(width, height), QImage.Format_Indexed8)
alignedwidth = width
if width & 1:
alignedwidth = width + 1
mypal = []
for i in range(256):
# flip the stupid palette to mac order..
#n = 255 - i
n = i
mypal.append(qRgb(ord(palette[n*4]),ord(palette[n*4+1]),ord(palette[n*4+2])))
img.setColorTable(mypal)
for y in range(height):
scanline = memoryview(img.scanLine(y))
ourline = data[alignedwidth*y:alignedwidth*(y+1)]
ourline = ourline + '\x00' * (len(scanline) - len(ourline))
scanline[0:width] = ourline[0:width]
if img:
pixmap = QPixmap.fromImage(img)
ink = info.flags & 0x3f
mask = None
if ink == 8: # matte
mask = img.createHeuristicMask()
if ink == 0x24: # background transparent
depth = 0
if wasDib:
depth = ord(data[14])
if depth == 1:
if qGray(img.color(0)) >= qGray(img.color(1)):
# qt's bmp decoder deliberately sabotages the image in this case!
# see the swapPixel01 call in qbmphandler.cpp
colorkey = 0
else:
colorkey = 1
elif depth == 4:
colorkey = 15
else:
colorkey = 255
if not wasDib:
depth = 8
colorkey = 0
mask = QImage(img.size(), QImage.Format_Mono)
for x in range(img.size().width()):
for y in range(img.size().height()):
if img.pixelIndex(x, y) == colorkey:
mask.setPixel(x, y, 1)
else:
mask.setPixel(x, y, 0)
if mask:
bitmask = QBitmap.fromImage(mask)
pixmap.setMask(bitmask)
offx = info.x - castinfo.regX
offy = info.y - castinfo.regY
if castinfo:
offx = offx + castinfo.initialRect.left
offy = offy + castinfo.initialRect.top
painter.drawPixmap(offx, offy, pixmap)
if self.movie.currChannel != None and self.movie.currChannel > 0:
info = self.movie.frames[self.movie.currFrame].sprites[self.movie.currChannel]
pen = QPen("black")
pen.setStyle(Qt.PenStyle.DashDotLine)
painter.setPen(pen)
painter.setBackgroundMode(Qt.OpaqueMode)
castinfo = self.movie.cast[info.castId]
offx = info.x
offy = info.y
if castinfo.castType == castBitmap:
offx = offx + castinfo.initialRect.left - castinfo.regX
offy = offy + castinfo.initialRect.top - castinfo.regY
painter.drawRect(offx, offy, info.width, info.height)
return True