From: <ef...@us...> - 2007-11-18 18:59:57
|
Revision: 4374 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4374&view=rev Author: efiring Date: 2007-11-18 10:59:56 -0800 (Sun, 18 Nov 2007) Log Message: ----------- Let to_rgba return uint8; track changes to cmap Images require rgba as 4 uint8s, so it is more efficient to generate these directly in to_rgba than to generate 4 doubles and convert them later. The tracking of changes in ScalarMappable was handling communication between objects, but was not keeping track of when to_rgba needs to be rerun. A dictionary was added to do this. Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/cm.py Modified: trunk/matplotlib/lib/matplotlib/cm.py =================================================================== --- trunk/matplotlib/lib/matplotlib/cm.py 2007-11-18 16:45:28 UTC (rev 4373) +++ trunk/matplotlib/lib/matplotlib/cm.py 2007-11-18 18:59:56 UTC (rev 4374) @@ -40,6 +40,7 @@ self.cmap = cmap self.observers = [] self.colorbar = None + self.update_dict = {'array':False} def set_colorbar(self, im, ax): 'set the colorbar image and axes associated with mappable' @@ -47,11 +48,26 @@ def to_rgba(self, x, alpha=1.0, bytes=False): '''Return a normalized rgba array corresponding to x. - If x is already an rgb or rgba array, return it unchanged. + If x is already an rgb array, insert alpha; if it is + already rgba, return it unchanged. + If bytes is True, return rgba as 4 uint8s instead of 4 floats. ''' try: - if x.ndim == 3 and (x.shape[2] == 3 or x.shape[2] == 4): - return x + if x.ndim == 3: + if x.shape[2] == 3: + if x.dtype == npy.uint8: + alpha = npy.array(alpha*255, npy.uint8) + m, n = npy.shape[:2] + xx = npy.empty(shape=(m,n,4), dtype = x.dtype) + xx[:,:,:3] = x + xx[:,:,3] = alpha + elif x.shape[2] == 4: + xx = x + else: + raise ValueError("third dimension must be 3 or 4") + if bytes and xx.dtype != npy.uint8: + xx = (xx * 255).astype(npy.uint8) + return xx except AttributeError: pass x = ma.asarray(x) @@ -62,6 +78,7 @@ def set_array(self, A): 'Set the image array from numpy array A' self._A = A + self.update_dict['array'] = True def get_array(self): 'Return the array' @@ -124,7 +141,23 @@ self.changed() + def add_checker(self, checker): + """ + Add an entry to a dictionary of boolean flags + that are set to True when the mappable is changed. + """ + self.update_dict[checker] = False + def check_update(self, checker): + """ + If mappable has changed since the last check, + return True; else return False + """ + if self.update_dict[checker]: + self.update_dict[checker] = False + return True + return False + def add_observer(self, mappable): """ whenever the norm, clim or cmap is set, call the notify @@ -158,3 +191,6 @@ """ for observer in self.observers: observer.notify(self) + for key in self.update_dict: + self.update_dict[key] = True + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |