edc6bba79591489d442603d85037272250aec2a6
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)
41 return (x
& (x
- 1)) == 0;
45 '''Describe the channel of a color channel.'''
47 def __init__(self
, type, norm
, size
):
51 self
.sign
= type in (SIGNED
, FIXED
, FLOAT
)
60 def __eq__(self
, other
):
61 return self
.type == other
.type and self
.norm
== other
.norm
and self
.size
== other
.size
65 '''Describe a pixel format.'''
67 def __init__(self
, name
, layout
, block_width
, block_height
, channels
, swizzles
, colorspace
):
70 self
.block_width
= block_width
71 self
.block_height
= block_height
72 self
.channels
= channels
73 self
.swizzles
= swizzles
75 self
.colorspace
= colorspace
81 '''Make up a short norm for a format, suitable to be used as suffix in
85 if name
.startswith('PIPE_FORMAT_'):
86 name
= name
[len('PIPE_FORMAT_'):]
92 for channel
in self
.channels
:
96 def nr_channels(self
):
98 for channel
in self
.channels
:
104 ref_channel
= self
.channels
[0]
105 for channel
in self
.channels
[1:]:
106 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
111 ref_channel
= self
.channels
[0]
112 for channel
in self
.channels
[1:]:
113 if channel
.type != VOID
:
114 if channel
.type != ref_channel
.type:
116 if channel
.norm
!= ref_channel
.norm
:
121 return is_pot(self
.block_size())
124 for channel
in self
.channels
:
125 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
130 for channel
in self
.channels
:
131 if channel
.type not in (VOID
, FLOAT
):
136 return self
.block_size()/8
148 _swizzle_parse_map
= {
159 '''Parse the format descrition in CSV format in terms of the
160 Channel and Format classes above.'''
162 stream
= open(filename
)
166 comment
= line
.index('#')
170 line
= line
[:comment
]
174 fields
= [field
.strip() for field
in line
.split(',')]
177 block_width
, block_height
= map(int, fields
[2:4])
179 for field
in fields
[4:8]:
181 type = _type_parse_map
[field
[0]]
184 size
= int(field
[2:])
187 size
= int(field
[1:])
192 channel
= Channel(type, norm
, size
)
193 channels
.append(channel
)
194 swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
195 colorspace
= fields
[9]
196 formats
.append(Format(name
, layout
, block_width
, block_height
, channels
, swizzles
, colorspace
))