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