/usr/lib/python2.7/dist-packages/ginga/LayerImage.py is in python-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | #
# LayerImage.py -- Abstraction of an generic layered image.
#
# Eric Jeschke (eric@naoj.org)
#
# Copyright (c) Eric R. Jeschke. All rights reserved.
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import numpy
import time
from ginga import BaseImage
from ginga.misc import Bunch
class LayerImage(object):
"""Mixin class for BaseImage subclasses. Adds layers and alpha/rgb
compositing.
"""
def __init__(self):
self._layer = []
self.cnt = 0
self.compose_types = ('alpha', 'rgb')
self.compose = 'alpha'
def _insert_layer(self, idx, image, alpha=None, name=None):
if alpha is None:
alpha = 1.0
if name is None:
name = "layer%d" % (self.cnt)
self.cnt += 1
bnch = Bunch.Bunch(image=image, alpha=alpha, name=name)
self._layer.insert(idx, bnch)
def insert_layer(self, idx, image, alpha=None, name=None,
compose=True):
self._insert_layer(idx, image, alpha=alpha, name=name)
if compose:
self.compose_layers()
def set_layer(self, idx, image, alpha=None, name=None,
compose=True):
self.delete_layer(idx, compose=False)
self._insert_layer(idx, image, alpha=alpha, name=name)
if compose:
self.compose_layers()
def delete_layer(self, idx, compose=True):
self._layer.pop(idx)
if compose:
self.compose_layers()
def get_layer(self, idx):
return self._layer[idx]
def num_layers(self):
return len(self._layer)
def get_max_shape(self, entity='image'):
maxdim = -1
maxshape = ()
for layer in self._layer:
if entity == 'image':
shape = layer[entity].get_shape()
elif entity == 'alpha':
item = layer.alpha
# If alpha is an image, get the array
if isinstance(item, BaseImage.BaseImage):
item = layer.alpha.get_data()
shape = numpy.shape(item)
else:
raise BaseImage.ImageError("entity '%s' not in (image, alpha)" % (
entity))
if len(shape) > maxdim:
maxdim = len(shape)
maxshape = shape
return maxshape
## def alpha_combine(self, src, alpha, dst):
## return (src * alpha) + (dst * (1.0 - alpha))
def mono2color(self, data):
return numpy.dstack((data, data, data))
def alpha_multiply(self, alpha, data, shape=None):
"""(alpha) can be a scalar or an array.
"""
# alpha can be a scalar or an array
if shape is None:
shape = data.shape
if len(data.shape) == 2:
res = alpha * data
# If desired shape is monochrome then return a mono image
# otherwise broadcast to a grey color image.
if len(shape) == 2:
return res
# note: in timing tests, dstack was not as efficient here...
#data = numpy.dstack((res, res, res))
data = numpy.empty(shape)
data[:, :, 0] = res[:, :]
data[:, :, 1] = res[:, :]
data[:, :, 2] = res[:, :]
return data
else:
# note: in timing tests, dstack was not as efficient here...
#res = numpy.dstack((data[:, :, 0] * alpha,
# data[:, :, 1] * alpha,
# data[:, :, 2] * alpha))
res = numpy.empty(shape)
res[:, :, 0] = data[:, :, 0] * alpha
res[:, :, 1] = data[:, :, 1] * alpha
res[:, :, 2] = data[:, :, 2] * alpha
return res
def alpha_compose(self):
start_time = time.time()
shape = self.get_max_shape()
## ht, wd = shape[:2]
## # alpha can be a scalar or an array, prepare for the appropriate kind
## ashape = self.get_max_shape(entity='alpha')
## if len(ashape) == 0:
## alpha_used = 0.0
## else:
## alpha_used = numpy.zeros((ht, wd))
# result holds the result of the composition
result = numpy.zeros(shape)
cnt = 0
for layer in self._layer:
alpha = layer.alpha
if isinstance(alpha, BaseImage.BaseImage):
alpha = alpha.get_data()
#alpha = numpy.clip((1.0 - alpha_used) * alpha, 0.0, 1.0)
#mina = numpy.min(alpha)
#print "cnt=%d mina=%f" % (cnt, mina)
data = layer.image.get_data()
result += self.alpha_multiply(alpha, data, shape=shape)
## alpha_used += layer.alpha
#numpy.clip(alpha_used, 0.0, 1.0)
cnt += 1
self.set_data(result)
end_time = time.time()
self.logger.debug("alpha compose=%.4f sec" % (end_time - start_time))
# def rgb_compose(self):
# slices = []
# start_time = time.time()
# for i in range(len(self._layer)):
# layer = self.get_layer(i)
# data = self.alpha_multiply(layer.alpha, layer.image.get_data())
# slices.append(data)
# split_time = time.time()
# result = numpy.dstack(slices)
# end_time = time.time()
# self.set_data(result)
# print "rgb_compose alpha multiply=%.4f sec dstack=%.4f sec sec total=%.4f sec" % (
# split_time - start_time, end_time - split_time,
# end_time - start_time)
def rgb_compose(self):
#num = self.num_layers()
num = 3
layer = self.get_layer(0)
wd, ht = layer.image.get_size()
result = numpy.empty((ht, wd, num))
start_time = time.time()
for i in range(len(self._layer)):
layer = self.get_layer(i)
alpha = layer.alpha
if isinstance(alpha, BaseImage.BaseImage):
alpha = alpha.get_data()
data = self.alpha_multiply(alpha, layer.image.get_data())
result[:, :, i] = data[:, :]
end_time = time.time()
self.set_data(result)
self.logger.debug("rgb_compose total=%.4f sec" % (
end_time - start_time))
def rgb_decompose(self, image):
data = image.get_data()
shape = data.shape
if len(shape) == 2:
self._insert_layer(0, image)
else:
names = ("Red", "Green", "Blue")
alphas = (0.292, 0.594, 0.114)
for i in range(shape[2]):
imgslice = data[:, :, i]
#img = BaseImage.BaseImage(data_np=imgslice, logger=self.logger)
# Create the same type of image as we are decomposing
img = image.__class__(data_np=imgslice, logger=self.logger)
if i < 3:
name = names[i]
alpha = alphas[i]
else:
name = "layer%d" % i
alpha = 0.0
self._insert_layer(i, img, name=name, alpha=alpha)
self.compose_layers()
def set_compose_type(self, ctype):
assert ctype in self.compose_types, \
BaseImage.ImageError("Bad compose type '%s': must be one of %s" % (
ctype, str(self.compose_types)))
self.compose = ctype
self.compose_layers()
def set_alpha(self, lidx, val):
layer = self._layer[lidx]
layer.alpha = val
self.compose_layers()
def set_alphas(self, vals):
for lidx in range(len(vals)):
layer = self._layer[lidx]
layer.alpha = vals[lidx]
self.compose_layers()
def compose_layers(self):
if self.compose == 'rgb':
self.rgb_compose()
else:
self.alpha_compose()
#END
|