4 /**************************************************************************
6 * Copyright 2009 VMware, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sub license, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice (including the
18 * next paragraph) shall be included in all copies or substantial portions
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
25 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 **************************************************************************/
33 VOID
, UNSIGNED
, SIGNED
, FIXED
, FLOAT
= range(5)
35 SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_0
, SWIZZLE_1
, SWIZZLE_NONE
, = range(7)
46 return (x
& (x
- 1)) == 0;
49 VERY_LARGE
= 99999999999999999999999
53 '''Describe the channel of a color channel.'''
55 def __init__(self
, type, norm
, size
, name
= ''):
59 self
.sign
= type in (SIGNED
, FIXED
, FLOAT
)
69 def __eq__(self
, other
):
70 return self
.type == other
.type and self
.norm
== other
.norm
and self
.size
== other
.size
73 '''Maximum representable number.'''
74 if self
.type == FLOAT
:
78 if self
.type == UNSIGNED
:
79 return (1 << self
.size
) - 1
80 if self
.type == SIGNED
:
85 '''Minimum representable number.'''
86 if self
.type == FLOAT
:
88 if self
.type == UNSIGNED
:
92 if self
.type == SIGNED
:
93 return -(1 << (self
.size
- 1))
98 '''Describe a pixel format.'''
100 def __init__(self
, name
, layout
, block_width
, block_height
, channels
, swizzles
, colorspace
):
103 self
.block_width
= block_width
104 self
.block_height
= block_height
105 self
.channels
= channels
106 self
.swizzles
= swizzles
108 self
.colorspace
= colorspace
113 def short_name(self
):
114 '''Make up a short norm for a format, suitable to be used as suffix in
118 if name
.startswith('PIPE_FORMAT_'):
119 name
= name
[len('PIPE_FORMAT_'):]
123 def block_size(self
):
125 for channel
in self
.channels
:
129 def nr_channels(self
):
131 for channel
in self
.channels
:
137 ref_channel
= self
.channels
[0]
138 for channel
in self
.channels
[1:]:
139 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
144 ref_channel
= self
.channels
[0]
145 for channel
in self
.channels
[1:]:
146 if channel
.type != VOID
:
147 if channel
.type != ref_channel
.type:
149 if channel
.norm
!= ref_channel
.norm
:
154 return is_pot(self
.block_size())
157 for channel
in self
.channels
:
158 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
163 for channel
in self
.channels
:
164 if channel
.type not in (VOID
, FLOAT
):
168 def is_bitmask(self
):
169 if self
.block_size() > 32:
171 if not self
.is_pot():
173 for channel
in self
.channels
:
174 if not is_pot(channel
.size
):
176 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
178 if channel
.size
>= 32:
182 def inv_swizzles(self
):
183 '''Return an array[4] of inverse swizzle terms'''
184 inv_swizzle
= [None]*4
186 swizzle
= self
.swizzles
[i
]
188 inv_swizzle
[swizzle
] = i
192 return self
.block_size()/8
204 _swizzle_parse_map
= {
215 '''Parse the format descrition in CSV format in terms of the
216 Channel and Format classes above.'''
218 stream
= open(filename
)
222 comment
= line
.index('#')
226 line
= line
[:comment
]
231 fields
= [field
.strip() for field
in line
.split(',')]
235 block_width
, block_height
= map(int, fields
[2:4])
237 swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
238 colorspace
= fields
[9]
242 if colorspace
in (RGB
, SRGB
):
244 swizzle
= swizzles
[i
]
246 names
[swizzle
] += 'rgba'[i
]
247 elif colorspace
== ZS
:
249 swizzle
= swizzles
[i
]
251 names
[swizzle
] += 'zs'[i
]
258 names
= ['x', 'y', 'z', 'w']
261 for i
in range(0, 4):
262 field
= fields
[4 + i
]
264 type = _type_parse_map
[field
[0]]
267 size
= int(field
[2:])
270 size
= int(field
[1:])
275 channel
= Channel(type, norm
, size
, names
[i
])
276 channels
.append(channel
)
278 format
= Format(name
, layout
, block_width
, block_height
, channels
, swizzles
, colorspace
)
279 formats
.append(format
)