2 # gfxprim.pixeltype - Module with PixelType descrition class
4 # 2011 - Tomas Gavenciak <gavento@ucw.cz>
11 from gfxprim
import die
14 def get_size_suffix(self
.bpp
, bit_endian
):
15 "Create pixel-size suffix (like 16BPP or 4BPP_LE)"
17 assert bpp
in self
.sizes
18 assert bit_endian
in self
.bit_endians
19 size_suffix
= '%dBPP' % (bpp
)
21 size_suffix
+= '_' + bit_endian
25 class PixelType(object):
26 """Representation of one GP_PixelType"""
28 def __init__(self
, name
, size
, chanslist
, bit_endian
=None):
29 """`name` must be a valid C identifier
30 `size` is type bit-width
31 `bit_endian` is required in 1,2,4 BPP types to determine the order of
32 pixels in a byte, either 'BE' or 'LE'
33 `chanslist` is a list of triplets describing individual channels as
34 [ (`chan_name`, `bit_offset`, `bit_size`) ]
35 where `chan_name` is usually one of: R, G, B,
36 V (value, used for grayscale), A (opacity)
38 assert re
.match('\A[A-Za-z][A-Za-z0-9_]*\Z', name
)
40 self
.chanslist
= chanslist
41 self
.chans
= dict() # { chan_name: (offset, size) }
43 self
.bit_endian
= bit_endian
46 self
.size_suffix
= "INVALID"
48 self
.size_suffix
= get_size_suffix(self
.size
, self
.bit_endian
)
50 # Verify channel bits for overlaps
51 # also builds a bit-map of the PixelType
52 self
.bits
= ['x']*size
54 assert c
[0] not in self
.chans
.keys()
56 for i
in range(c
[1],c
[1]+c
[2]):
58 assert(self
.bits
[i
]=='x')
61 def valid_for_config(self
, config
):
62 "Check PixelType compatibility with given GfxPrimConfig."
64 # all types except UNKNOWN=0 must have one of these sizes
65 if self
.name
!= "UNKNOWN":
66 assert(self
.size
in config
.sizes
)
68 # bit_endian matters only for non-multiple-of-8 bpp
69 if size
% 8 != 0 or bit_endian
is not None:
70 assert bit_endian
in config
.bit_endians
73 return "<PixelType " + self
.name
+ ">"
76 return ('P' in self
.chans
)