Clean up the generating code
[gfxprim.git] / core / gen / pixeltype.py
blob5370814541a2cae1d597e0e85bc2e5d21ad67f8e
1 # Module with PixelType descrition class
2 # 2011 - Tomas Gavenciak <gavento@ucw.cz>
4 # pixel format:
5 # type
6 # - PAL
7 # - HSVRGBA
8 # per chan
9 # name, pos, size
10 # bitwidth
11 # name
13 import re
16 ## *Global* dictionary of all pixeltypes { name : PixelType }
17 pixeltypes = {}
19 ## *Global* set of all encountered channel names
20 channels = set()
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)
30 """
31 assert re.match('\A[A-Za-z][A-Za-z0-9_]*\Z', name)
32 self.name = name
33 self.chanslist = chanslist
34 self.chans = dict() # { chan_name: (offset, size) }
35 assert(size in [1,2,4,8,16,24,32])
36 self.size = size
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
43 for c in chanslist:
44 assert c[0] not in self.chans.keys()
45 self.chans[c[0]] = c
46 for i in range(c[1],c[1]+c[2]):
47 assert(i<self.size)
48 assert(self.bits[i]=='x')
49 self.bits[i] = c[0]
51 assert self.name not in pixeltypes.keys()
52 pixeltypes[self.name] = self
53 channels.update(set(self.chans.keys()))
55 def __str__(self):
56 return "<PixelType " + self.name + ">"