debian: Add graphviz dependency
[gfxprim/pasky.git] / pylib / gp_codegen / pixeltype.py
blob9887136fdd2dd08bc1f6be1f490da12b023ce7d9
2 # gfxprim.pixeltype - Module with PixelType descrition class
4 # 2011 - Tomas Gavenciak <gavento@ucw.cz>
5 # 2013 Cyril Hrubis <metan@ucw.cz>
8 import re
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
15 self.append(name)
16 self.append(offset)
17 self.append(size)
18 # Add index (position in pixel from left)
19 self.idx = idx
20 # Add some convinience variables
21 self.name = name
22 self.off = offset
23 self.size = size
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)
45 """
46 assert re.match('\A[A-Za-z][A-Za-z0-9_]*\Z', name)
47 self.name = name
48 # Create channel list with convinience variables
49 new_chanslist = []
50 idx = 0
51 for i in chanslist:
52 new_chanslist.append(PixelChannel(i, idx))
53 idx = idx + 1
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()
65 self.chans[c[0]] = c
66 for i in range(c[1], c[1] + c[2]):
67 assert(i < self.pixelsize.size)
68 assert(self.bits[i] == 'x')
69 self.bits[i] = c[0]
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)
78 def __str__(self):
79 return "<PixelType " + self.name + ">"
81 def is_palette(self):
82 return ('P' in self.chans)
84 def is_unknown(self):
85 return (self.name == "UNKNOWN")
87 def is_rgb(self):
88 for i in 'RGB':
89 if i not in self.chans: return False
90 return True
92 def is_gray(self):
93 return ('V' in self.chans)
95 def is_cmyk(self):
96 for i in 'CMYK':
97 if i not in self.chans: return False
98 return True
100 def is_alpha(self):
101 return ('A' in self.chans)