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 * Pixel format accessor functions.
35 * @author Jose Fonseca <jfonseca@vmware.com>
43 from u_format_pack
import *
46 def is_format_supported(format
):
47 '''Determines whether we actually have the plumbing necessary to generate the
48 to read/write to/from this format.'''
50 # FIXME: Ideally we would support any format combination here.
52 # XXX: It should be straightforward to support srgb
53 if format
.colorspace
not in ('rgb', 'zs'):
56 if format
.layout
!= PLAIN
:
60 channel
= format
.channels
[i
]
61 if channel
.type not in (VOID
, UNSIGNED
, FLOAT
):
64 # We can only read a color from a depth/stencil format if the depth channel is present
65 if format
.colorspace
== 'zs' and format
.swizzles
[0] == SWIZZLE_NONE
:
71 def native_type(format
):
72 '''Get the native appropriate for a format.'''
74 if format
.layout
== PLAIN
:
75 if not format
.is_array():
76 # For arithmetic pixel formats return the integer type that matches the whole pixel
77 return 'uint%u_t' % format
.block_size()
79 # For array pixel formats return the integer type that matches the color channel
80 channel
= format
.channels
[0]
81 if channel
.type == UNSIGNED
:
82 return 'uint%u_t' % channel
.size
83 elif channel
.type == SIGNED
:
84 return 'int%u_t' % channel
.size
85 elif channel
.type == FLOAT
:
86 if channel
.size
== 32:
88 elif channel
.size
== 64:
98 def generate_srgb_tables():
99 print 'static ubyte srgb_to_linear[256] = {'
101 print ' %s,' % (int(math
.pow((i
/ 255.0 + 0.055) / 1.055, 2.4) * 255))
104 print 'static ubyte linear_to_srgb[256] = {'
106 for i
in range(1, 256):
107 print ' %s,' % (int((1.055 * math
.pow(i
/ 255.0, 0.41666) - 0.055) * 255))
112 def generate_format_read(format
, dst_channel
, dst_native_type
, dst_suffix
):
113 '''Generate the function to read pixels from a particular format'''
115 name
= format
.short_name()
117 src_native_type
= native_type(format
)
120 print 'util_format_%s_read_%s(%s *dst, unsigned dst_stride, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0, unsigned w, unsigned h)' % (name
, dst_suffix
, dst_native_type
)
122 print ' unsigned x, y;'
123 print ' const uint8_t *src_row = src + y0*src_stride;'
124 print ' %s *dst_row = dst;' % dst_native_type
125 print ' for (y = 0; y < h; ++y) {'
126 print ' const %s *src_pixel = (const %s *)(src_row + x0*%u);' % (src_native_type
, src_native_type
, format
.stride())
127 print ' %s *dst_pixel = dst_row;' %dst
_native
_type
128 print ' for (x = 0; x < w; ++x) {'
131 if format
.colorspace
== 'rgb':
133 swizzle
= format
.swizzles
[i
]
135 names
[swizzle
] += 'rgba'[i
]
136 elif format
.colorspace
== 'zs':
137 swizzle
= format
.swizzles
[0]
145 if format
.layout
== PLAIN
:
146 if not format
.is_array():
147 print ' %s pixel = *src_pixel++;' % src_native_type
150 src_channel
= format
.channels
[i
]
151 width
= src_channel
.size
154 mask
= (1 << width
) - 1
156 value
= '(%s >> %u)' % (value
, shift
)
157 if shift
+ width
< format
.block_size():
158 value
= '(%s & 0x%x)' % (value
, mask
)
159 value
= conversion_expr(src_channel
, dst_channel
, dst_native_type
, value
)
160 print ' %s %s = %s;' % (dst_native_type
, names
[i
], value
)
164 src_channel
= format
.channels
[i
]
166 value
= 'src_pixel[%u]' % i
167 value
= conversion_expr(src_channel
, dst_channel
, dst_native_type
, value
)
168 print ' %s %s = %s;' % (dst_native_type
, names
[i
], value
)
169 print ' src_pixel += %u;' % (format
.nr_channels())
174 if format
.colorspace
== 'rgb':
175 swizzle
= format
.swizzles
[i
]
177 value
= names
[swizzle
]
178 elif swizzle
== SWIZZLE_0
:
180 elif swizzle
== SWIZZLE_1
:
181 value
= get_one(dst_channel
)
184 elif format
.colorspace
== 'zs':
188 value
= get_one(dst_channel
)
191 print ' *dst_pixel++ = %s; /* %s */' % (value
, 'rgba'[i
])
194 print ' src_row += src_stride;'
195 print ' dst_row += dst_stride/sizeof(*dst_row);'
201 def generate_format_write(format
, src_channel
, src_native_type
, src_suffix
):
202 '''Generate the function to write pixels to a particular format'''
204 name
= format
.short_name()
206 dst_native_type
= native_type(format
)
209 print 'util_format_%s_write_%s(const %s *src, unsigned src_stride, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0, unsigned w, unsigned h)' % (name
, src_suffix
, src_native_type
)
211 print ' unsigned x, y;'
212 print ' uint8_t *dst_row = dst + y0*dst_stride;'
213 print ' const %s *src_row = src;' % src_native_type
214 print ' for (y = 0; y < h; ++y) {'
215 print ' %s *dst_pixel = (%s *)(dst_row + x0*%u);' % (dst_native_type
, dst_native_type
, format
.stride())
216 print ' const %s *src_pixel = src_row;' %src_native
_type
217 print ' for (x = 0; x < w; ++x) {'
219 inv_swizzle
= format
.inv_swizzles()
221 if format
.layout
== PLAIN
:
222 if not format
.is_array():
223 print ' %s pixel = 0;' % dst_native_type
226 dst_channel
= format
.channels
[i
]
227 width
= dst_channel
.size
228 if inv_swizzle
[i
] is not None:
229 value
= 'src_pixel[%u]' % inv_swizzle
[i
]
230 value
= conversion_expr(src_channel
, dst_channel
, dst_native_type
, value
)
232 value
= '(%s << %u)' % (value
, shift
)
233 print ' pixel |= %s;' % value
235 print ' *dst_pixel++ = pixel;'
238 dst_channel
= format
.channels
[i
]
239 if inv_swizzle
[i
] is not None:
240 value
= 'src_pixel[%u]' % inv_swizzle
[i
]
241 value
= conversion_expr(src_channel
, dst_channel
, dst_native_type
, value
)
242 print ' *dst_pixel++ = %s;' % value
245 print ' src_pixel += 4;'
248 print ' dst_row += dst_stride;'
249 print ' src_row += src_stride/sizeof(*src_row);'
255 def generate_read(formats
, dst_channel
, dst_native_type
, dst_suffix
):
256 '''Generate the dispatch function to read pixels from any format'''
258 for format
in formats
:
259 if is_format_supported(format
):
260 generate_format_read(format
, dst_channel
, dst_native_type
, dst_suffix
)
263 print 'util_format_read_%s(enum pipe_format format, %s *dst, unsigned dst_stride, const void *src, unsigned src_stride, unsigned x, unsigned y, unsigned w, unsigned h)' % (dst_suffix
, dst_native_type
)
265 print ' void (*func)(%s *dst, unsigned dst_stride, const uint8_t *src, unsigned src_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % dst_native_type
266 print ' switch(format) {'
267 for format
in formats
:
268 if is_format_supported(format
):
269 print ' case %s:' % format
.name
270 print ' func = &util_format_%s_read_%s;' % (format
.short_name(), dst_suffix
)
273 print ' debug_printf("unsupported format\\n");'
276 print ' func(dst, dst_stride, (const uint8_t *)src, src_stride, x, y, w, h);'
281 def generate_write(formats
, src_channel
, src_native_type
, src_suffix
):
282 '''Generate the dispatch function to write pixels to any format'''
284 for format
in formats
:
285 if is_format_supported(format
):
286 generate_format_write(format
, src_channel
, src_native_type
, src_suffix
)
289 print 'util_format_write_%s(enum pipe_format format, const %s *src, unsigned src_stride, void *dst, unsigned dst_stride, unsigned x, unsigned y, unsigned w, unsigned h)' % (src_suffix
, src_native_type
)
292 print ' void (*func)(const %s *src, unsigned src_stride, uint8_t *dst, unsigned dst_stride, unsigned x0, unsigned y0, unsigned w, unsigned h);' % src_native_type
293 print ' switch(format) {'
294 for format
in formats
:
295 if is_format_supported(format
):
296 print ' case %s:' % format
.name
297 print ' func = &util_format_%s_write_%s;' % (format
.short_name(), src_suffix
)
300 print ' debug_printf("unsupported format\\n");'
303 print ' func(src, src_stride, (uint8_t *)dst, dst_stride, x, y, w, h);'
310 for arg
in sys
.argv
[1:]:
311 formats
.extend(parse(arg
))
313 print '/* This file is autogenerated by u_format_access.py from u_format.csv. Do not edit directly. */'
315 # This will print the copyright message on the top of this file
316 print __doc__
.strip()
318 print '#include "pipe/p_compiler.h"'
319 print '#include "u_math.h"'
320 print '#include "u_format_pack.h"'
323 generate_srgb_tables()
325 type = Channel(FLOAT
, False, 32)
326 native_type
= 'float'
329 generate_read(formats
, type, native_type
, suffix
)
330 generate_write(formats
, type, native_type
, suffix
)
332 type = Channel(UNSIGNED
, True, 8)
333 native_type
= 'uint8_t'
336 generate_read(formats
, type, native_type
, suffix
)
337 generate_write(formats
, type, native_type
, suffix
)
340 if __name__
== '__main__':