15c887af5e697491d493cc69803a597007dbfe75
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 '''Describe the type of a color channel.'''
43 def __init__(self
, kind
, norm
, size
):
47 self
.sign
= kind
in (SIGNED
, FIXED
, FLOAT
)
56 def __eq__(self
, other
):
57 return self
.kind
== other
.kind
and self
.norm
== other
.norm
and self
.size
== other
.size
61 '''Describe a pixel format.'''
63 def __init__(self
, name
, layout
, block_width
, block_height
, in_types
, out_swizzle
, colorspace
):
66 self
.block_width
= block_width
67 self
.block_height
= block_height
68 self
.in_types
= in_types
69 self
.out_swizzle
= out_swizzle
71 self
.colorspace
= colorspace
77 '''Make up a short norm for a format, suitable to be used as suffix in
81 if name
.startswith('PIPE_FORMAT_'):
82 name
= name
[len('PIPE_FORMAT_'):]
88 for type in self
.in_types
:
92 def nr_channels(self
):
94 for type in self
.in_types
:
100 ref_type
= self
.in_types
[0]
101 for type in self
.in_types
[1:]:
102 if type.size
and (type.size
!= ref_type
.size
or type.size
% 8):
107 ref_type
= self
.in_types
[0]
108 for type in self
.in_types
[1:]:
109 if type.kind
!= VOID
:
110 if type.kind
!= ref_type
.kind
:
112 if type.norm
!= ref_type
.norm
:
117 return self
.block_size()/8
129 _swizzle_parse_map
= {
140 '''Parse the format descrition in CSV format in terms of the
141 Type and Format classes above.'''
143 stream
= open(filename
)
147 comment
= line
.index('#')
151 line
= line
[:comment
]
155 fields
= [field
.strip() for field
in line
.split(',')]
158 block_width
, block_height
= map(int, fields
[2:4])
160 for field
in fields
[4:8]:
162 kind
= _kind_parse_map
[field
[0]]
165 size
= int(field
[2:])
168 size
= int(field
[1:])
173 in_type
= Type(kind
, norm
, size
)
174 in_types
.append(in_type
)
175 out_swizzle
= [_swizzle_parse_map
[swizzle
] for swizzle
in fields
[8]]
176 colorspace
= fields
[9]
177 formats
.append(Format(name
, layout
, block_width
, block_height
, in_types
, out_swizzle
, colorspace
))