2 # gfxprim.pixeltype - Module with PixelType descrition class
4 # 2011 - Tomas Gavenciak <gavento@ucw.cz>
5 # 2013 Cyril Hrubis <metan@ucw.cz>
9 from .pixelsize
import PixelSize
11 class PixelChannel(list):
12 def __init__(self
, triplet
, idx
):
13 (name
, offset
, size
) = triplet
14 # Create the list -> backward compatibility with triplets
18 # Add index (position in pixel from left)
20 # Add some convinience variables
24 # Shift ready to used in C
25 self
.C_shift
= " << " + hex(offset
)
26 # Maximal channel value as an integer
27 self
.max = 2 ** size
- 1
28 # Maximal value as a C string
29 self
.C_max
= hex(self
.max)
30 # Chanel bitmask as int
31 self
.mask
= self
.max * (2 ** offset
)
32 # Channel bitmas as hex string
33 self
.C_mask
= hex(self
.mask
)
35 class PixelType(object):
36 """Representation of one GP_PixelType"""
38 def __init__(self
, name
, pixelsize
, chanslist
):
39 """`name` must be a valid C identifier
40 `pixelsize` is an instance of PixelSize
41 `chanslist` is a list of triplets describing individual channels as
42 [ (`chan_name`, `bit_offset`, `bit_size`) ]
43 where `chan_name` is usually one of: R, G, B,
44 V (value, used for grayscale), A (opacity)
46 assert re
.match('\A[A-Za-z][A-Za-z0-9_]*\Z', name
)
48 # Create channel list with convinience variables
52 new_chanslist
.append(PixelChannel(i
, idx
))
54 self
.chanslist
= new_chanslist
55 self
.chans
= dict() # { chan_name: (offset, size) }
56 self
.pixelsize
= pixelsize
57 # C enum as defined in GP_Pixel.gen.h
58 self
.C_type
= "GP_PIXEL_" + self
.name
60 # Verify channel bits for overlaps
61 # also builds a bit-map of the PixelType
62 self
.bits
= ['x'] * pixelsize
.size
63 for c
in new_chanslist
:
64 assert c
[0] not in self
.chans
.keys()
66 for i
in range(c
[1], c
[1] + c
[2]):
67 assert(i
< self
.pixelsize
.size
)
68 assert(self
.bits
[i
] == 'x')
71 def valid_for_config(self
, config
):
72 "Check PixelType compatibility with given GfxPrimConfig."
74 # all types except UNKNOWN must have one of these sizes
75 if not self
.is_unknown():
76 assert(self
.pixelsize
in config
.pixelsizes
)
79 return "<PixelType " + self
.name
+ ">"
82 return ('P' in self
.chans
)
85 return (self
.name
== "UNKNOWN")
89 if i
not in self
.chans
: return False
93 return ('V' in self
.chans
)
97 if i
not in self
.chans
: return False
101 return ('A' in self
.chans
)