1 # Module with PixelType descrition class
2 # 2011 - Tomas Gavenciak <gavento@ucw.cz>
16 ## *Global* dictionary of all pixeltypes { name : PixelType }
19 ## *Global* set of all encountered channel names
22 class PixelType(object):
23 """Representation of one GP_PixelType"""
24 def __init__(self
, name
, size
, chanslist
):
25 """`name` must be a valid C identifier
26 `size` is in bits, allowed are 1, 2, 4, 8, 16, 24, 32
27 `chanslist` is a list of triplets describing individual channels as
28 [ (`chan_name`, `bit_offset`, `bit_size`) ]
29 `chan_name` is usually one of: R, G, B, V (value, used for grayscale), A (opacity)
31 assert re
.match('\A[A-Za-z][A-Za-z0-9_]*\Z', name
)
33 self
.chanslist
= chanslist
34 self
.chans
= dict() # { chan_name: (offset, size) }
35 assert(size
in [1,2,4,8,16,24,32])
37 # Numbering from 1 (0 for invalid type)
38 self
.number
= min([ptype
.number
for ptype
in pixeltypes
.values()] + [0]) + 1
40 # Verify channel bits for overlaps
41 # also builds a bit-map of the PixelType
42 self
.bits
= ['x']*size
44 assert c
[0] not in self
.chans
.keys()
46 for i
in range(c
[1],c
[1]+c
[2]):
48 assert(self
.bits
[i
]=='x')
51 assert self
.name
not in pixeltypes
.keys()
52 pixeltypes
[self
.name
] = self
53 channels
.update(set(self
.chans
.keys()))
56 return "<PixelType " + self
.name
+ ">"