-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphicsRGBLUT.py
323 lines (294 loc) · 12.4 KB
/
graphicsRGBLUT.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
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
"""
This File is part of bLUe software.
Copyright (C) 2017 Bernard Virot <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Lesser Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from PySide2 import QtCore
from PySide2.QtCore import QRect
from PySide2.QtCore import Qt, QRectF
from PySide2.QtWidgets import QPushButton, QGraphicsScene
from bLUeGui.graphicsSpline import activeCubicSpline, graphicsCurveForm, activePoint, channelValues
from utils import optionsWidget, QbLUePushButton
class graphicsForm(graphicsCurveForm) :
"""
Form for interactive RGB curves
"""
@classmethod
def getNewWindow(cls, targetImage=None, axeSize=500, layer=None, parent=None, mainForm=None):
newWindow = graphicsForm(targetImage=targetImage, axeSize=axeSize, layer=layer, parent=parent, mainForm=mainForm)
newWindow.setWindowTitle(layer.name)
return newWindow
def __init__(self, targetImage=None, axeSize=500, layer=None, parent=None, mainForm=None):
super().__init__(targetImage=targetImage, axeSize=axeSize, layer=layer, parent=parent, mainForm=mainForm)
# Brightness curve
cubic = activeCubicSpline(axeSize)
graphicsScene = self.scene()
graphicsScene.addItem(cubic)
graphicsScene.cubicRGB = cubic
cubic.channel = channelValues.RGB
cubic.histImg = self.scene().layer.inputImg().histogram(size=graphicsScene.axeSize, bgColor=graphicsScene.bgColor, chans=[], mode='Luminosity')
cubic.initFixedPoints()
# Red curve
cubic = activeCubicSpline(axeSize)
graphicsScene.addItem(cubic)
graphicsScene.cubicR = cubic
cubic.channel = channelValues.Red
cubic.histImg = self.scene().layer.inputImg().histogram(size=graphicsScene.axeSize, bgColor=graphicsScene.bgColor, chans=channelValues.Red)
cubic.initFixedPoints()
# Green curve
cubic = activeCubicSpline(axeSize)
graphicsScene.addItem(cubic)
graphicsScene.cubicG = cubic
cubic.channel = channelValues.Green
cubic.histImg = self.scene().layer.inputImg().histogram(size=graphicsScene.axeSize, bgColor=graphicsScene.bgColor, chans=channelValues.Green)
cubic.initFixedPoints()
# Blue curve
cubic = activeCubicSpline(axeSize)
graphicsScene.addItem(cubic)
graphicsScene.cubicB = cubic
cubic.channel = channelValues.Blue
cubic.histImg = self.scene().layer.inputImg().histogram(size=graphicsScene.axeSize, bgColor=graphicsScene.bgColor, chans=channelValues.Blue)
cubic.initFixedPoints()
# set current curve to brightness
graphicsScene.cubicItem = graphicsScene.cubicRGB
graphicsScene.cubicItem.setVisible(True)
# buttons
pushButton1 = QbLUePushButton("Reset Current")
pushButton1.move(100,20)
pushButton1.adjustSize()
pushButton1.clicked.connect(self.resetCurve)
graphicsScene.addWidget(pushButton1)
pushButton2 = QbLUePushButton("Reset R,G,B")
pushButton2.move(100, 50)
pushButton2.adjustSize()
pushButton2.clicked.connect(self.resetAllCurves)
graphicsScene.addWidget(pushButton2)
# options
options = ['RGB', 'Red', 'Green', 'Blue']
self.listWidget1 = optionsWidget(options=options, exclusive=True)
self.listWidget1.setGeometry(0, 10, self.listWidget1.sizeHintForColumn(0) + 5, self.listWidget1.sizeHintForRow(0)*len(options) + 5)
graphicsScene.addWidget(self.listWidget1)
# selection changed handler
curves = [graphicsScene.cubicRGB, graphicsScene.cubicR, graphicsScene.cubicG, graphicsScene.cubicB]
curveDict = dict(zip(options, curves))
def onSelect1(item):
self.scene().cubicItem.setVisible(False)
self.scene().cubicItem = curveDict[item.text()]
pushButton2.setEnabled(item.text() != 'RGB')
self.scene().cubicItem.setVisible(True)
l = self.scene().layer
l.applyToStack()
l.parentImage.onImageChanged()
# Force redraw histogram
self.scene().invalidate(QRectF(0.0, -self.scene().axeSize, self.scene().axeSize, self.scene().axeSize),
QGraphicsScene.BackgroundLayer)
self.listWidget1.onSelect = onSelect1
# set initial selection to RGB
item = self.listWidget1.items[options[0]]
item.setCheckState(Qt.Checked)
self.listWidget1.select(item)
self.setWhatsThis("""<b>RGB curves</b><br>""" + self.whatsThis())
def f():
l = self.scene().layer
l.applyToStack()
l.parentImage.onImageChanged()
self.scene().cubicRGB.curveChanged.sig.connect(f)
self.scene().cubicR.curveChanged.sig.connect(f)
self.scene().cubicG.curveChanged.sig.connect(f)
self.scene().cubicB.curveChanged.sig.connect(f)
def colorPickedSlot(self, x, y, modifiers):
"""
sets black/white points
(x,y) coordinates are relative to the full size image.
@param x:
@type x:
@param y:
@type y:
@param modifiers:
@type modifiers:
"""
r,g,b= self.scene().targetImage.getActivePixel(x, y)
if (modifiers & QtCore.Qt.ControlModifier) and (modifiers & QtCore.Qt.ShiftModifier):
self.setBlackPoint(r,g,b)
elif (modifiers & QtCore.Qt.ControlModifier):
self.setWhitePoint(r, g, b)
def setBlackPoint(self, r, g ,b):
"""
@param r:
@type r:
@param g:
@type g:
@param b:
@type b:
"""
sc = self.scene()
bPoint = min(r, g, b)
# don't set black point to white !
if bPoint >= 255:
bPoint -= 10.0
cubicRGB, cubicR, cubicG, cubicB = sc.cubicRGB, sc.cubicR, sc.cubicG, sc.cubicB
for cubic in [cubicRGB, cubicR, cubicG, cubicB]:
scale = cubic.size / 255.0
fp = cubic.fixedPoints
# find current white point
wPoint = cubic.size
tmp = [p.x() for p in fp if p.y() == -cubic.size]
if tmp:
wPoint = min(tmp)
# remove control points at the left of wPoint, but the first
for p in list(fp[1:-1]):
if p.x() < wPoint:
fp.remove(p)
sc.removeItem(p)
# add new black point if needed
if bPoint > 0.0:
a = activePoint(bPoint*scale, 0.0, parentItem=cubic)
cubic.fixedPoints.append(a)
cubic.fixedPoints.sort(key=lambda z: z.scenePos().x())
cubic.updatePath()
cubic.updateLUTXY()
l = self.scene().layer
l.applyToStack()
l.parentImage.onImageChanged()
def setWhitePoint(self, r, g, b):
"""
@param r:
@type r:
@param g:
@type g:
@param b:
@type b:
"""
sc = self.scene()
cubicRGB, cubicR, cubicG, cubicB = sc.cubicRGB, sc.cubicR, sc.cubicG, sc.cubicB
for i, cubic in enumerate([cubicRGB, cubicR, cubicG, cubicB]):
scale = cubic.size / 255.0
fp = cubic.fixedPoints
wPoint = max(r, g, b) if i==0 else r if i==1 else g if i==2 else b
# don't set white point to black!
if wPoint <= 10:
wPoint += 10.0
# find black point
bPoint = 0.0
tmp = [p.x() for p in fp if p.y() == 0.0]
if tmp:
bPoint = max(tmp)
# remove control points at the right of bPoint
for p in list(fp[1:-1]):
if p.x() > bPoint:
cubic.fixedPoints.remove(p)
sc.removeItem(p)
# add new white point if needed
if wPoint < cubic.size:
p = activePoint(wPoint * scale, -cubic.size, parentItem=cubic)
cubic.fixedPoints.append(p)
cubic.fixedPoints.sort(key=lambda z: z.scenePos().x())
cubic.updatePath()
cubic.updateLUTXY()
l = self.scene().layer
l.applyToStack()
l.parentImage.onImageChanged()
def drawBackground(self, qp, qrF):
"""
Overrides QGraphicsView.drawBackground
@param qp:
@type qp: QPainter
@param qrF:
@type qrF: QRectF
"""
super().drawBackground(qp, qrF)
graphicsScene = self.scene()
s = graphicsScene.axeSize
if graphicsScene.cubicItem.histImg is not None:
qp.drawImage(QRect(0, -s, s, s), graphicsScene.cubicItem.histImg)
def updateHist(self, curve, redraw=True):
"""
Updates the channel histogram displayed under the curve
@param curve:
@type curve:
"""
sc = self.scene()
if curve is sc.cubicRGB:
curve.histImg = sc.layer.inputImg().histogram(size=sc.axeSize, bgColor=sc.bgColor, chans=[], mode='Luminosity')
elif curve is sc.cubicR:
curve.histImg = sc.layer.inputImg().histogram(size=sc.axeSize, bgColor=sc.bgColor, chans=channelValues.Red)
elif curve is sc.cubicG:
curve.histImg = sc.layer.inputImg().histogram(size=sc.axeSize, bgColor=sc.bgColor, chans=channelValues.Green)
elif curve is sc.cubicB:
curve.histImg = sc.layer.inputImg().histogram(size=sc.axeSize, bgColor=sc.bgColor, chans=channelValues.Blue)
# Force to redraw histogram
if redraw:
sc.invalidate(QRectF(0.0, -sc.axeSize, sc.axeSize, sc.axeSize),
sc.BackgroundLayer)
def updateHists(self):
"""
Updates all histograms
@return:
@rtype:
"""
sc = self.scene()
for curve in [sc.cubicRGB, sc.cubicR, sc.cubicG, sc.cubicB]:
self.updateHist(curve, redraw=False)
# Force to redraw histogram
sc.invalidate(QRectF(0.0, -sc.axeSize, sc.axeSize, sc.axeSize),
sc.BackgroundLayer)
def resetCurve(self):
"""
Button event handler
Reset the current curve
"""
graphicsScene = self.scene()
graphicsScene.cubicItem.reset()
self.updateHist(graphicsScene.cubicItem)
# self.scene().onUpdateLUT()
l = graphicsScene.layer
l.applyToStack()
l.parentImage.onImageChanged()
def resetAllCurves(self):
"""
Button event handler
Reset R,G,B curves
"""
graphicsScene = self.scene()
for cubicItem in [graphicsScene.cubicR, graphicsScene.cubicG, graphicsScene.cubicB]:
cubicItem.reset()
self.updateHists()
l = graphicsScene.layer
l.applyToStack()
l.parentImage.onImageChanged()
def writeToStream(self, outStream):
"""
@param outStream:
@type outStream: QDataStream
@return:
@rtype: QDataStream
"""
graphicsScene = self.scene()
layer = graphicsScene.layer
outStream.writeQString(layer.actionName)
outStream.writeQString(layer.name)
if layer.actionName in ['actionBrightness_Contrast', 'actionCurves_HSpB', 'actionCurves_Lab']:
outStream.writeQString(self.listWidget1.selectedItems()[0].text())
graphicsScene.cubicRGB.writeToStream(outStream)
graphicsScene.cubicR.writeToStream(outStream)
graphicsScene.cubicG.writeToStream(outStream)
graphicsScene.cubicB.writeToStream(outStream)
return outStream
def readFromStream(self, inStream):
actionName = inStream.readQString()
name = inStream.readQString()
sel = inStream.readQString()
graphicsScene = self.scene()
graphicsScene.cubicRGB.readFromStream(inStream)
graphicsScene.cubicR.readFromStream(inStream)
graphicsScene.cubicG.readFromStream(inStream)
graphicsScene.cubicB.readFromStream(inStream)
return inStream