-
Notifications
You must be signed in to change notification settings - Fork 21
/
pipette.py
257 lines (210 loc) · 9.81 KB
/
pipette.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
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
import ctypes
import logging
from typing import cast, Dict, List, TypeVar, Union
from PyQt5 import Qt
import vapoursynth as vs
from vspreview.core import AbstractMainWindow, AbstractToolbar, Output
from vspreview.utils import set_qobject_names
from vspreview.widgets import ColorView
Number = TypeVar('Number', int, float)
class PipetteToolbar(AbstractToolbar):
__slots__ = (
'color_view', 'outputs', 'position', 'pos_fmt',
'rgb_dec', 'rgb_hex', 'rgb_label',
'src_dec', 'src_dec_fmt', 'src_hex', 'src_hex_fmt', 'src_label',
)
data_types = {
vs.INTEGER: {
1: ctypes.c_uint8,
2: ctypes.c_uint16,
# 4: ctypes.c_char * 4,
},
vs.FLOAT: {
# 2: ctypes.c_char * 2,
4: ctypes.c_float,
}
}
def __init__(self, main: AbstractMainWindow) -> None:
super().__init__(main, 'Pipette')
self.setup_ui()
self.main.graphics_view.mouseMoved.connect(self.mouse_moved)
self.pos_fmt = '{},{}'
self.src_hex_fmt = '{:2X}'
self.src_max_val: Union[int, float] = 2**8 - 1
self.src_dec_fmt = '{:3d}'
self.src_norm_fmt = '{:0.5f}'
self.outputs: Dict[Output, vs.VideoNode] = {}
set_qobject_names(self)
def setup_ui(self) -> None:
layout = Qt.QHBoxLayout(self)
layout.setObjectName('PipetteToolbar.setup_ui.layout')
layout.setContentsMargins(0, 0, 0, 0)
self.color_view = ColorView(self)
self.color_view.setFixedSize(self.height() // 2 , self.height() // 2)
layout.addWidget(self.color_view)
font = Qt.QFont('Consolas', 9)
font.setStyleHint(Qt.QFont.Monospace)
self.position = Qt.QLabel(self)
self.position.setFont(font)
self.position.setTextInteractionFlags(Qt.Qt.TextSelectableByMouse)
layout.addWidget(self.position)
self.rgb_label = Qt.QLabel(self)
self.rgb_label.setText('Rendered (RGB):')
layout.addWidget(self.rgb_label)
self.rgb_hex = Qt.QLabel(self)
self.rgb_hex.setFont(font)
self.rgb_hex.setTextInteractionFlags(Qt.Qt.TextSelectableByMouse)
layout.addWidget(self.rgb_hex)
self.rgb_dec = Qt.QLabel(self)
self.rgb_dec.setFont(font)
self.rgb_dec.setTextInteractionFlags(Qt.Qt.TextSelectableByMouse)
layout.addWidget(self.rgb_dec)
self.rgb_norm = Qt.QLabel(self)
self.rgb_norm.setFont(font)
self.rgb_norm.setTextInteractionFlags(Qt.Qt.TextSelectableByMouse)
layout.addWidget(self.rgb_norm)
self.src_label = Qt.QLabel(self)
layout.addWidget(self.src_label)
self.src_hex = Qt.QLabel(self)
self.src_hex.setFont(font)
self.src_hex.setTextInteractionFlags(Qt.Qt.TextSelectableByMouse)
layout.addWidget(self.src_hex)
self.src_dec = Qt.QLabel(self)
self.src_dec.setFont(font)
self.src_dec.setTextInteractionFlags(Qt.Qt.TextSelectableByMouse)
layout.addWidget(self.src_dec)
self.src_norm = Qt.QLabel(self)
self.src_norm.setFont(font)
self.src_norm.setTextInteractionFlags(Qt.Qt.TextSelectableByMouse)
layout.addWidget(self.src_norm)
layout.addStretch()
def mouse_moved(self, event: Qt.QMouseEvent) -> None:
if not event.buttons() & Qt.Qt.LeftButton:
self.update_labels(event.pos())
def update_labels(self, local_pos: Qt.QPoint) -> None:
from math import floor, trunc
from struct import unpack
point_f = self.main.graphics_view.mapToScene(local_pos)
point = Qt.QPoint(floor(point_f.x()), floor(point_f.y()))
if not self.main.current_output.graphics_scene_item.contains(point_f):
return
color = self.main.current_output.graphics_scene_item.image() \
.pixelColor(point)
self.color_view.color = color
self.position.setText(self.pos_fmt.format(point.x(), point.y()))
self.rgb_hex.setText('{:2X},{:2X},{:2X}'.format(
color.red(), color.green(), color.blue()))
self.rgb_dec.setText('{:3d},{:3d},{:3d}'.format(
color.red(), color.green(), color.blue()))
self.rgb_norm.setText('{:0.5f},{:0.5f},{:0.5f}'.format(
color.red() / 255, color.green() / 255, color.blue() / 255))
if not self.src_label.isVisible():
return
def extract_value(vs_frame: vs.VideoFrame, i: int, idx: int) \
-> Union[int, float]:
fmt = vs_frame.format
if fmt.sample_type == vs.FLOAT and fmt.bytes_per_sample == 2:
ptr = ctypes.cast(vs_frame.get_read_ptr(i), ctypes.POINTER(
ctypes.c_char * (2 * vs_frame.width * vs_frame.height)))
val = unpack('e', ptr.contents[(idx * 2):(idx * 2 + 2)])[0] # type: ignore
return cast(float, val)
else:
ptr = ctypes.cast(vs_frame.get_read_ptr(i), ctypes.POINTER(
self.data_types[fmt.sample_type][fmt.bytes_per_sample] * ( # type:ignore
vs_frame.width * vs_frame.height)))
return ptr.contents[idx] # type: ignore
vs_frame = self.outputs[self.main.current_output].get_frame(
int(self.main.current_frame))
fmt = vs_frame.format
idx = point.y() * vs_frame.width + point.x()
src_vals = [extract_value(vs_frame, i, idx)
for i in range(fmt.num_planes)]
if self.main.current_output.has_alpha:
vs_alpha = self.main.current_output.source_vs_alpha.get_frame(
int(self.main.current_frame))
src_vals.append(extract_value(vs_alpha, 0, idx))
self.src_dec.setText(self.src_dec_fmt.format(*src_vals))
if fmt.sample_type == vs.INTEGER:
self.src_hex.setText(self.src_hex_fmt.format(*src_vals))
self.src_norm.setText(self.src_norm_fmt.format(
*[src_val / self.src_max_val for src_val in src_vals]))
elif fmt.sample_type == vs.FLOAT:
self.src_norm.setText(self.src_norm_fmt.format(*[
self.clip(val, 0.0, 1.0) if i in (0, 3) else
self.clip(val, -0.5, 0.5) + 0.5
for i, val in enumerate(src_vals)
]))
def on_current_output_changed(self, index: int, prev_index: int) -> None:
from math import ceil, log
super().on_current_output_changed(index, prev_index)
fmt = self.main.current_output.format
src_label_text = ''
if fmt.color_family == vs.RGB:
src_label_text = 'Raw (RGB{}):'
elif fmt.color_family == vs.YUV:
src_label_text = 'Raw (YUV{}):'
elif fmt.color_family == vs.GRAY:
src_label_text = 'Raw (Gray{}):'
elif fmt.color_family == vs.YCOCG:
src_label_text = 'Raw (YCoCg{}):'
elif fmt.id == vs.COMPATBGR32.value:
src_label_text = 'Raw (RGB{}):'
elif fmt.id == vs.COMPATYUY2.value:
src_label_text = 'Raw (YUV{}):'
has_alpha = self.main.current_output.has_alpha
if not has_alpha:
self.src_label.setText(src_label_text.format(''))
else:
self.src_label.setText(src_label_text.format(' + Alpha'))
self.pos_fmt = '{{:{}d}},{{:{}d}}'.format(
ceil(log(self.main.current_output.width, 10)),
ceil(log(self.main.current_output.height, 10)))
if self.main.current_output not in self.outputs:
self.outputs[self.main.current_output] = self.prepare_vs_output(
self.main.current_output.source_vs_output)
src_fmt = self.outputs[self.main.current_output].format
if src_fmt.sample_type == vs.INTEGER:
self.src_max_val = 2**src_fmt.bits_per_sample - 1
elif src_fmt.sample_type == vs.FLOAT:
self.src_hex.setVisible(False)
self.src_max_val = 1.0
src_num_planes = src_fmt.num_planes + int(has_alpha)
self.src_hex_fmt = ','.join(('{{:{w}X}}',) * src_num_planes) \
.format(w=ceil(log(self.src_max_val, 16)))
if src_fmt.sample_type == vs.INTEGER:
self.src_dec_fmt = ','.join(('{{:{w}d}}',) * src_num_planes) \
.format(w=ceil(log(self.src_max_val, 10)))
elif src_fmt.sample_type == vs.FLOAT:
self.src_dec_fmt = ','.join(('{: 0.5f}',) * src_num_planes)
self.src_norm_fmt = ','.join(('{:0.5f}',) * src_num_planes)
self.update_labels(self.main.graphics_view.mapFromGlobal(
self.main.cursor().pos()))
def on_toggle(self, new_state: bool) -> None:
super().on_toggle(new_state)
self.main.graphics_view.setMouseTracking(new_state)
if new_state is True:
self.main.graphics_view.setDragMode(Qt.QGraphicsView.NoDrag)
else:
self.main.graphics_view.setDragMode(
Qt.QGraphicsView.ScrollHandDrag)
@staticmethod
def prepare_vs_output(vs_output: vs.VideoNode) -> vs.VideoNode:
def non_subsampled_format(fmt: vs.Format) -> vs.Format:
if fmt.id == vs.COMPATBGR32.value:
return vs.RGB24 # type: ignore
elif fmt.id == vs.COMPATYUY2.value:
return vs.YUV444P8 # type: ignore
else:
return vs.core.register_format(
color_family=fmt.color_family,
sample_type=fmt.sample_type,
bits_per_sample=fmt.bits_per_sample,
subsampling_w=0,
subsampling_h=0
)
return vs.core.resize.Bicubic(
vs_output,
format=non_subsampled_format(vs_output.format))
@staticmethod
def clip(value: Number, lower_bound: Number, upper_bound: Number) -> Number:
return max(lower_bound, min(value, upper_bound))