misc_setGamma.py 3.39 KB
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