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
, pure
, size
, name
= ''):
60 self
.sign
= type in (SIGNED
, FIXED
, FLOAT
)
72 def __eq__(self
, other
):
73 return self
.type == other
.type and self
.norm
== other
.norm
and self
.pure
== other
.pure
and self
.size
== other
.size
76 '''Maximum representable number.'''
77 if self
.type == FLOAT
:
79 if self
.type == FIXED
:
80 return (1 << (self
.size
/2)) - 1
83 if self
.type == UNSIGNED
:
84 return (1 << self
.size
) - 1
85 if self
.type == SIGNED
:
86 return (1 << (self
.size
- 1)) - 1
90 '''Minimum representable number.'''
91 if self
.type == FLOAT
:
93 if self
.type == FIXED
:
94 return -(1 << (self
.size
/2))
95 if self
.type == UNSIGNED
:
99 if self
.type == SIGNED
:
100 return -(1 << (self
.size
- 1))
105 '''Describe a pixel format.'''
107 def __init__(self
, name
, layout
, block_width
, block_height
, channels
, swizzles
, colorspace
):
110 self
.block_width
= block_width
111 self
.block_height
= block_height
112 self
.channels
= channels
113 self
.swizzles
= swizzles
115 self
.colorspace
= colorspace
120 def short_name(self
):
121 '''Make up a short norm for a format, suitable to be used as suffix in
125 if name
.startswith('PIPE_FORMAT_'):
126 name
= name
[len('PIPE_FORMAT_'):]
130 def block_size(self
):
132 for channel
in self
.channels
:
136 def nr_channels(self
):
138 for channel
in self
.channels
:
144 if self
.layout
!= PLAIN
:
146 ref_channel
= self
.channels
[0]
147 if ref_channel
.type == VOID
:
148 ref_channel
= self
.channels
[1]
149 for channel
in self
.channels
:
150 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
152 if channel
.type != VOID
:
153 if channel
.type != ref_channel
.type:
155 if channel
.norm
!= ref_channel
.norm
:
157 if channel
.pure
!= ref_channel
.pure
:
162 if self
.layout
!= PLAIN
:
164 ref_channel
= self
.channels
[0]
165 if ref_channel
.type == VOID
:
166 ref_channel
= self
.channels
[1]
167 for channel
in self
.channels
[1:]:
168 if channel
.type != VOID
:
169 if channel
.type != ref_channel
.type:
171 if channel
.norm
!= ref_channel
.norm
:
173 if channel
.pure
!= ref_channel
.pure
:
178 return is_pot(self
.block_size())
181 if self
.layout
!= PLAIN
:
183 for channel
in self
.channels
:
184 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
189 if self
.layout
!= PLAIN
:
191 for channel
in self
.channels
:
192 if channel
.type not in (VOID
, FLOAT
):
196 def is_bitmask(self
):
197 if self
.layout
!= PLAIN
:
199 if self
.block_size() not in (8, 16, 32):
201 for channel
in self
.channels
:
202 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
206 def inv_swizzles(self
):
207 '''Return an array[4] of inverse swizzle terms'''
208 '''Only pick the first matching value to avoid l8 getting blue and i8 getting alpha'''
209 inv_swizzle
= [None]*4
211 swizzle
= self
.swizzles
[i
]
212 if swizzle
< 4 and inv_swizzle
[swizzle
] == None:
213 inv_swizzle
[swizzle
] = i
217 return self
.block_size()/8
229 _swizzle_parse_map
= {
240 '''Parse the format descrition in CSV format in terms of the
241 Channel and Format classes above.'''
243 stream
= open(filename
)
247 comment
= line
.index('#')
251 line
= line
[:comment
]
256 fields
= [field
.strip() for field
in line
.split(',')]
260 block_width
, block_height
= map(int, fields
[2:4])
262 swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
263 colorspace
= fields
[9]
267 if colorspace
in (RGB
, SRGB
):
269 swizzle
= swizzles
[i
]
271 names
[swizzle
] += 'rgba'[i
]
272 elif colorspace
== ZS
:
274 swizzle
= swizzles
[i
]
276 names
[swizzle
] += 'zs'[i
]
283 names
= ['x', 'y', 'z', 'w']
286 for i
in range(0, 4):
287 field
= fields
[4 + i
]
289 type = _type_parse_map
[field
[0]]
293 size
= int(field
[2:])
294 elif field
[1] == 'p':
297 size
= int(field
[2:])
301 size
= int(field
[1:])
307 channel
= Channel(type, norm
, pure
, size
, names
[i
])
308 channels
.append(channel
)
310 format
= Format(name
, layout
, block_width
, block_height
, channels
, swizzles
, colorspace
)
311 formats
.append(format
)