Package flumotion :: Package component :: Package effects :: Package colorbalance :: Module colorbalance
[hide private]

Source Code for Module flumotion.component.effects.colorbalance.colorbalance

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  import gst 
 23  import gst.interfaces 
 24   
 25  from flumotion.component import feedcomponent 
 26   
 27  __version__ = "$Rev$" 
 28   
 29   
30 -class Colorbalance(feedcomponent.Effect):
31 logCategory = "colorbalance" 32
33 - def __init__(self, name, element, hue, saturation, brightness, contrast, 34 pipeline=None):
35 """ 36 @param element: the GStreamer element supporting the colorbalance 37 interface 38 @param hue: the colorbalance hue, as a percentage 39 @type hue: float 40 @param saturation: the colorbalance saturation, as a percentage 41 @type saturation: float 42 @param brightness: the colorbalance brightness, as a percentage 43 @type brightness: float 44 @param contrast: the colorbalance contrast, as a percentage 45 @type contrast: float 46 @param pipeline: the pipeline 47 @type pipeline: L{gst.Pipeline} 48 """ 49 self.debug("colorbalance init") 50 feedcomponent.Effect.__init__(self, name) 51 self._element = element 52 if pipeline: 53 bus = pipeline.get_bus() 54 bus.connect('message::state-changed', 55 self._bus_message_received_cb, 56 hue, saturation, brightness, contrast) 57 58 self._channels = None
59
60 - def setUIState(self, state):
61 feedcomponent.Effect.setUIState(self, state) 62 if state: 63 for k in 'Hue', 'Saturation', 'Brightness', 'Contrast': 64 state.addKey('colorbalance-%s' % k, 0.0)
65 66 # State change handling for 0.10 67
68 - def _bus_message_received_cb(self, bus, message, hue, saturation, 69 brightness, contrast):
70 """ 71 @param bus: the message bus sending the message 72 @param message: the message received 73 """ 74 t = message.type 75 if t == gst.MESSAGE_STATE_CHANGED and message.src == self._element: 76 (old, new, pending) = message.parse_state_changed() 77 # we have a state change 78 if old == gst.STATE_READY and new == gst.STATE_PAUSED: 79 self._setInitialColorBalance(hue, 80 saturation, brightness, contrast)
81
82 - def effect_setColorBalanceProperty(self, which, value):
83 """ 84 Set a color balance property. 85 86 @param which: which property to change 87 @param value: what value to set it to (float between 0.0 and 100.0) 88 89 Returns: the actual percentage it was set to 90 """ 91 if not self._channels: 92 return value 93 94 for i in self._channels: 95 if i.label == which: 96 if value: 97 device_value = _percent_to_value(value, 98 i.min_value, i.max_value) 99 self.debug("setting percentage: %.1f, " 100 "value %d <= %d <= %d", 101 value, i.min_value, device_value, 102 i.max_value) 103 self._element.set_value(i, device_value) 104 device_value = self._element.get_value(i) 105 percent = _value_to_percent(device_value, 106 i.min_value, i.max_value) 107 self.debug('device says %s=%.1f', i.label, percent) 108 # notify all others too 109 if not self.uiState: 110 self.warning("effect %s doesn't have a uiState" % 111 self.name) 112 else: 113 self.uiState.set('colorbalance-%s' % which, percent) 114 return percent 115 116 # didn't find it 117 return value
118
119 - def _setInitialColorBalance(self, hue, saturation, brightness, contrast):
120 self._channels = self._element.list_colorbalance_channels() 121 self.debug('colorbalance channels: %d' % len(self._channels)) 122 self.effect_setColorBalanceProperty('Hue', hue) 123 self.effect_setColorBalanceProperty('Saturation', saturation) 124 self.effect_setColorBalanceProperty('Brightness', brightness) 125 self.effect_setColorBalanceProperty('Contrast', contrast)
126 127
128 -def _value_to_percent(value, min, max):
129 """ 130 Convert an integer value between min and max to a percentage. 131 """ 132 return float(value - min) / float(max - min) * 100.0
133 134
135 -def _percent_to_value(percentage, min, max):
136 """ 137 Convert an percentage value to an integer value between min and max. 138 """ 139 return int(min + percentage / 100.0 * (max - min))
140