misc_setGamma.py
3.39 KB
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
title = "SetGamma"
tip = "applies gamma correction to colorbar"
onein = True
import numpy as np
from guidata.qt.QtGui import QColor
from guidata.qt.QtGui import QMessageBox
from guidata.dataset.datatypes import DataSet
from guidata.dataset.dataitems import (IntItem, StringItem, ChoiceItem, FloatItem, BoolItem)
from guiqwt.colormap import get_colormap_list, get_cmap, register_extra_colormap
from guiqwt.config import _
from guiqwt.transitional import QwtDoubleInterval
FULLRANGE = QwtDoubleInterval(0.0, 1.0)
LUT_SIZE = 1024
LUT_MAX = float(LUT_SIZE-1)
def nint(x):
if x > 0: return int(x+0.5)
else: return int(x-0.5)
class NOD3_App:
def __init__(self, parent):
self.parent = parent
self.parent.activateWindow()
def Error(self, msg):
QMessageBox.critical(self.parent.parent(), title,
_(u"Error:")+"\n%s" % str(msg))
def compute_app(self, **args):
class FuncParam(DataSet):
gamma = FloatItem('Gamma', default=1.0, min=0.01, max=10.0)
name = title.replace(" ", "")
if args == {}:
param = FuncParam(_(title), "Set gamma correction to colormap:")
else:
param = self.parent.ScriptParameter(name, args)
# if no parameter needed set param to None. activate next line
#param = None
self.parent.compute_11(name, lambda m, p: self.function(m, p), param, onein)
def gamma(self, a, gam):
y = hex(a)[2:-1]
x = [ord(c) for c in y.decode('hex')]
f = 255.0
l = x[0]
r = nint(((float(x[1])/f)**gam)*f)
g = nint(((float(x[2])/f)**gam)*f)
b = nint(((float(x[3])/f)**gam)*f)*0
return int(str("%02x%02x%02x%02x" % (l, r, g, b)), 16), r/f, g/f, b/f
def set_color_map(self, parent, table, gamma):
parent.cmap_table = table
cmap_a = parent.lut[3]
alpha = parent.imageparam.alpha
alpha_mask = parent.imageparam.alpha_mask
for i in range(LUT_SIZE):
if alpha_mask:
pix_alpha = alpha*(i/float(LUT_SIZE-1))
else:
pix_alpha = alpha
alpha_channel = np.uint32(255*pix_alpha+0.5).clip(0, 255) << 24
cmap_a[i] = np.uint32((table.rgb(FULLRANGE, i/LUT_MAX))
& 0xffffff) | alpha_channel
#cmap_a[i] = self.gamma(cmap_a[i], gamma)
cmap_a[i], r, g, b = self.gamma(cmap_a[i], gamma)
col = QColor()
col.setRgbF(r, g, b)
table.addColorStop(i, col)
cmap = table.colorTable(FULLRANGE)
for i in range(len(cmap)):
cmap[i] = self.gamma(cmap[i], gamma)
#register_extra_colormap(self.cname, table)
plot = parent.plot()
#print dir(table)
if plot:
#plot.update_colormap_axis(parent)
zaxis = plot.colormap_axis
axiswidget = plot.axisWidget(zaxis)
# changes the colorbar scaleing if necessary (not here)
#plot.setAxisScale(zaxis, parent.min, parent.max)
axiswidget.setColorMap(QwtDoubleInterval(parent.min, parent.max), table)
parent.plot().replot()
def function(self, m, p):
cmap_name = self.parent.cmap
self.cname = cmap_name+"_gamma"
cmap = get_cmap(cmap_name)
item = self.parent.items[-1]
self.set_color_map(item, cmap, 1.0/p.gamma)
return [], p