9f6ac36de1178352e59aa6f823b775eb1468b814
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 **************************************************************************/
35 VOID
, UNSIGNED
, SIGNED
, FIXED
, FLOAT
= range(5)
37 SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_W
, SWIZZLE_0
, SWIZZLE_1
, SWIZZLE_NONE
, = range(7)
47 # Not cross-compiler friendly
48 is_big_endian
= sys
.byteorder
== 'big'
51 return (x
& (x
- 1)) == 0
54 VERY_LARGE
= 99999999999999999999999
58 '''Describe the channel of a color channel.'''
60 def __init__(self
, type, norm
, pure
, size
, name
= ''):
65 self
.sign
= type in (SIGNED
, FIXED
, FLOAT
)
77 def __eq__(self
, other
):
78 return self
.type == other
.type and self
.norm
== other
.norm
and self
.pure
== other
.pure
and self
.size
== other
.size
81 '''Maximum representable number.'''
82 if self
.type == FLOAT
:
84 if self
.type == FIXED
:
85 return (1 << (self
.size
/2)) - 1
88 if self
.type == UNSIGNED
:
89 return (1 << self
.size
) - 1
90 if self
.type == SIGNED
:
91 return (1 << (self
.size
- 1)) - 1
95 '''Minimum representable number.'''
96 if self
.type == FLOAT
:
98 if self
.type == FIXED
:
99 return -(1 << (self
.size
/2))
100 if self
.type == UNSIGNED
:
104 if self
.type == SIGNED
:
105 return -(1 << (self
.size
- 1))
110 '''Describe a pixel format.'''
112 def __init__(self
, name
, layout
, block_width
, block_height
, channels
, swizzles
, colorspace
):
115 self
.block_width
= block_width
116 self
.block_height
= block_height
117 self
.channels
= channels
118 self
.swizzles
= swizzles
120 self
.colorspace
= colorspace
125 def short_name(self
):
126 '''Make up a short norm for a format, suitable to be used as suffix in
130 if name
.startswith('PIPE_FORMAT_'):
131 name
= name
[len('PIPE_FORMAT_'):]
135 def block_size(self
):
137 for channel
in self
.channels
:
141 def nr_channels(self
):
143 for channel
in self
.channels
:
148 def array_element(self
):
149 if self
.layout
!= PLAIN
:
151 ref_channel
= self
.channels
[0]
152 if ref_channel
.type == VOID
:
153 ref_channel
= self
.channels
[1]
154 for channel
in self
.channels
:
155 if channel
.size
and (channel
.size
!= ref_channel
.size
or channel
.size
% 8):
157 if channel
.type != VOID
:
158 if channel
.type != ref_channel
.type:
160 if channel
.norm
!= ref_channel
.norm
:
162 if channel
.pure
!= ref_channel
.pure
:
167 return self
.array_element() != None
170 if self
.layout
!= PLAIN
:
172 ref_channel
= self
.channels
[0]
173 if ref_channel
.type == VOID
:
174 ref_channel
= self
.channels
[1]
175 for channel
in self
.channels
[1:]:
176 if channel
.type != VOID
:
177 if channel
.type != ref_channel
.type:
179 if channel
.norm
!= ref_channel
.norm
:
181 if channel
.pure
!= ref_channel
.pure
:
186 return is_pot(self
.block_size())
189 if self
.layout
!= PLAIN
:
191 for channel
in self
.channels
:
192 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
197 if self
.layout
!= PLAIN
:
199 for channel
in self
.channels
:
200 if channel
.type not in (VOID
, FLOAT
):
204 def is_bitmask(self
):
205 if self
.layout
!= PLAIN
:
207 if self
.block_size() not in (8, 16, 32):
209 for channel
in self
.channels
:
210 if channel
.type not in (VOID
, UNSIGNED
, SIGNED
):
214 def is_pure_color(self
):
215 if self
.layout
!= PLAIN
or self
.colorspace
== ZS
:
217 pures
= [channel
.pure
218 for channel
in self
.channels
219 if channel
.type != VOID
]
224 def channel_type(self
):
225 types
= [channel
.type
226 for channel
in self
.channels
227 if channel
.type != VOID
]
232 def is_pure_signed(self
):
233 return self
.is_pure_color() and self
.channel_type() == SIGNED
235 def is_pure_unsigned(self
):
236 return self
.is_pure_color() and self
.channel_type() == UNSIGNED
238 def has_channel(self
, id):
239 return self
.swizzles
[id] != SWIZZLE_NONE
242 return self
.colorspace
== ZS
and self
.has_channel(0)
244 def has_stencil(self
):
245 return self
.colorspace
== ZS
and self
.has_channel(1)
248 return self
.block_size()/8
260 _swizzle_parse_map
= {
270 def _parse_channels(fields
, layout
, colorspace
, swizzles
):
273 if colorspace
in (RGB
, SRGB
):
275 swizzle
= swizzles
[i
]
277 names
[swizzle
] += 'rgba'[i
]
278 elif colorspace
== ZS
:
280 swizzle
= swizzles
[i
]
282 names
[swizzle
] += 'zs'[i
]
289 names
= ['x', 'y', 'z', 'w']
292 for i
in range(0, 4):
295 type = _type_parse_map
[field
[0]]
299 size
= int(field
[2:])
300 elif field
[1] == 'p':
303 size
= int(field
[2:])
307 size
= int(field
[1:])
313 channel
= Channel(type, norm
, pure
, size
, names
[i
])
314 channels
.append(channel
)
319 '''Parse the format descrition in CSV format in terms of the
320 Channel and Format classes above.'''
322 stream
= open(filename
)
326 comment
= line
.index('#')
330 line
= line
[:comment
]
335 fields
= [field
.strip() for field
in line
.split(',')]
339 block_width
, block_height
= map(int, fields
[2:4])
340 colorspace
= fields
[9]
342 swizzles
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
343 channels
= _parse_channels(fields
[4:8], layout
, colorspace
, swizzles
)
346 for channel
in channels
[3::-1] if is_big_endian
else channels
:
347 channel
.shift
= shift
348 shift
+= channel
.size
350 format
= Format(name
, layout
, block_width
, block_height
, channels
, swizzles
, colorspace
)
351 formats
.append(format
)