1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Pixel format accessor functions.
32 * @author Jose Fonseca <jfonseca@vmware.com>
39 #include "u_format_s3tc.h"
41 #include "pipe/p_defines.h"
45 util_format_is_float(enum pipe_format format
)
47 const struct util_format_description
*desc
= util_format_description(format
);
55 i
= util_format_get_first_non_void_channel(format
);
60 return desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
? TRUE
: FALSE
;
64 /** Test if the format contains RGB, but not alpha */
66 util_format_is_rgb_no_alpha(enum pipe_format format
)
68 const struct util_format_description
*desc
=
69 util_format_description(format
);
71 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
72 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
73 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
) {
81 util_format_is_luminance(enum pipe_format format
)
83 const struct util_format_description
*desc
=
84 util_format_description(format
);
86 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
87 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
88 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
89 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
90 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
91 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_1
) {
98 util_format_is_pure_integer(enum pipe_format format
)
100 const struct util_format_description
*desc
= util_format_description(format
);
103 /* Find the first non-void channel. */
104 i
= util_format_get_first_non_void_channel(format
);
108 return desc
->channel
[i
].pure_integer
? TRUE
: FALSE
;
112 util_format_is_pure_sint(enum pipe_format format
)
114 const struct util_format_description
*desc
= util_format_description(format
);
117 i
= util_format_get_first_non_void_channel(format
);
121 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
125 util_format_is_pure_uint(enum pipe_format format
)
127 const struct util_format_description
*desc
= util_format_description(format
);
130 i
= util_format_get_first_non_void_channel(format
);
134 return (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
&& desc
->channel
[i
].pure_integer
) ? TRUE
: FALSE
;
138 util_format_is_array(const struct util_format_description
*desc
)
142 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
143 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
||
144 desc
->block
.width
!= 1 ||
145 desc
->block
.height
!= 1) {
149 for (chan
= 0; chan
< desc
->nr_channels
; ++chan
) {
150 if (desc
->channel
[chan
].size
!= desc
->channel
[0].size
)
153 if (desc
->channel
[chan
].type
== UTIL_FORMAT_TYPE_VOID
&& (chan
+ 1) == desc
->nr_channels
)
156 if (desc
->channel
[chan
].type
!= desc
->channel
[0].type
)
159 if (desc
->channel
[chan
].normalized
!= desc
->channel
[0].normalized
)
162 if (desc
->channel
[chan
].pure_integer
!= desc
->channel
[0].pure_integer
)
166 if (desc
->nr_channels
== 4) {
167 if (desc
->swizzle
[3] < 3)
170 for (chan
= 0; chan
< desc
->nr_channels
; ++chan
) {
171 if (desc
->swizzle
[chan
] != chan
)
180 util_format_is_luminance_alpha(enum pipe_format format
)
182 const struct util_format_description
*desc
=
183 util_format_description(format
);
185 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
186 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
187 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
188 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
189 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
190 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_Y
) {
198 util_format_is_intensity(enum pipe_format format
)
200 const struct util_format_description
*desc
=
201 util_format_description(format
);
203 if ((desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
204 desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) &&
205 desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_X
&&
206 desc
->swizzle
[1] == UTIL_FORMAT_SWIZZLE_X
&&
207 desc
->swizzle
[2] == UTIL_FORMAT_SWIZZLE_X
&&
208 desc
->swizzle
[3] == UTIL_FORMAT_SWIZZLE_X
) {
216 util_format_is_supported(enum pipe_format format
, unsigned bind
)
218 if (util_format_is_s3tc(format
) && !util_format_s3tc_enabled
) {
222 #ifndef TEXTURE_FLOAT_ENABLED
223 if ((bind
& PIPE_BIND_RENDER_TARGET
) &&
224 format
!= PIPE_FORMAT_R9G9B9E5_FLOAT
&&
225 format
!= PIPE_FORMAT_R11G11B10_FLOAT
&&
226 util_format_is_float(format
)) {
236 util_format_read_4f(enum pipe_format format
,
237 float *dst
, unsigned dst_stride
,
238 const void *src
, unsigned src_stride
,
239 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
241 const struct util_format_description
*format_desc
;
242 const uint8_t *src_row
;
245 format_desc
= util_format_description(format
);
247 assert(x
% format_desc
->block
.width
== 0);
248 assert(y
% format_desc
->block
.height
== 0);
250 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
253 format_desc
->unpack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
258 util_format_write_4f(enum pipe_format format
,
259 const float *src
, unsigned src_stride
,
260 void *dst
, unsigned dst_stride
,
261 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
263 const struct util_format_description
*format_desc
;
265 const float *src_row
;
267 format_desc
= util_format_description(format
);
269 assert(x
% format_desc
->block
.width
== 0);
270 assert(y
% format_desc
->block
.height
== 0);
272 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
275 format_desc
->pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
280 util_format_read_4ub(enum pipe_format format
, uint8_t *dst
, unsigned dst_stride
, const void *src
, unsigned src_stride
, unsigned x
, unsigned y
, unsigned w
, unsigned h
)
282 const struct util_format_description
*format_desc
;
283 const uint8_t *src_row
;
286 format_desc
= util_format_description(format
);
288 assert(x
% format_desc
->block
.width
== 0);
289 assert(y
% format_desc
->block
.height
== 0);
291 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
294 format_desc
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
299 util_format_write_4ub(enum pipe_format format
, const uint8_t *src
, unsigned src_stride
, void *dst
, unsigned dst_stride
, unsigned x
, unsigned y
, unsigned w
, unsigned h
)
301 const struct util_format_description
*format_desc
;
303 const uint8_t *src_row
;
305 format_desc
= util_format_description(format
);
307 assert(x
% format_desc
->block
.width
== 0);
308 assert(y
% format_desc
->block
.height
== 0);
310 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
313 format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
317 util_format_read_4ui(enum pipe_format format
,
318 unsigned *dst
, unsigned dst_stride
,
319 const void *src
, unsigned src_stride
,
320 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
322 const struct util_format_description
*format_desc
;
323 const uint8_t *src_row
;
326 format_desc
= util_format_description(format
);
328 assert(x
% format_desc
->block
.width
== 0);
329 assert(y
% format_desc
->block
.height
== 0);
331 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
334 format_desc
->unpack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
338 util_format_write_4ui(enum pipe_format format
,
339 const unsigned int *src
, unsigned src_stride
,
340 void *dst
, unsigned dst_stride
,
341 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
343 const struct util_format_description
*format_desc
;
345 const unsigned *src_row
;
347 format_desc
= util_format_description(format
);
349 assert(x
% format_desc
->block
.width
== 0);
350 assert(y
% format_desc
->block
.height
== 0);
352 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
355 format_desc
->pack_rgba_uint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
359 util_format_read_4i(enum pipe_format format
,
360 int *dst
, unsigned dst_stride
,
361 const void *src
, unsigned src_stride
,
362 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
364 const struct util_format_description
*format_desc
;
365 const uint8_t *src_row
;
368 format_desc
= util_format_description(format
);
370 assert(x
% format_desc
->block
.width
== 0);
371 assert(y
% format_desc
->block
.height
== 0);
373 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
376 format_desc
->unpack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
380 util_format_write_4i(enum pipe_format format
,
381 const int *src
, unsigned src_stride
,
382 void *dst
, unsigned dst_stride
,
383 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
385 const struct util_format_description
*format_desc
;
389 format_desc
= util_format_description(format
);
391 assert(x
% format_desc
->block
.width
== 0);
392 assert(y
% format_desc
->block
.height
== 0);
394 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
397 format_desc
->pack_rgba_sint(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
401 util_is_format_compatible(const struct util_format_description
*src_desc
,
402 const struct util_format_description
*dst_desc
)
406 if (src_desc
->format
== dst_desc
->format
) {
410 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
411 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
415 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
416 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
417 src_desc
->colorspace
!= dst_desc
->colorspace
) {
421 for (chan
= 0; chan
< 4; ++chan
) {
422 if (src_desc
->channel
[chan
].size
!=
423 dst_desc
->channel
[chan
].size
) {
428 for (chan
= 0; chan
< 4; ++chan
) {
429 enum util_format_swizzle swizzle
= dst_desc
->swizzle
[chan
];
432 if (src_desc
->swizzle
[chan
] != swizzle
) {
435 if ((src_desc
->channel
[swizzle
].type
!=
436 dst_desc
->channel
[swizzle
].type
) ||
437 (src_desc
->channel
[swizzle
].normalized
!=
438 dst_desc
->channel
[swizzle
].normalized
)) {
449 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
454 * After linearized sRGB values require more than 8bits.
457 if (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
) {
461 switch (format_desc
->layout
) {
463 case UTIL_FORMAT_LAYOUT_S3TC
:
465 * These are straight forward.
468 case UTIL_FORMAT_LAYOUT_RGTC
:
469 if (format_desc
->format
== PIPE_FORMAT_RGTC1_SNORM
||
470 format_desc
->format
== PIPE_FORMAT_RGTC2_SNORM
||
471 format_desc
->format
== PIPE_FORMAT_LATC1_SNORM
||
472 format_desc
->format
== PIPE_FORMAT_LATC2_SNORM
)
476 case UTIL_FORMAT_LAYOUT_PLAIN
:
478 * For these we can find a generic rule.
481 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
482 switch (format_desc
->channel
[chan
].type
) {
483 case UTIL_FORMAT_TYPE_VOID
:
485 case UTIL_FORMAT_TYPE_UNSIGNED
:
486 if (!format_desc
->channel
[chan
].normalized
||
487 format_desc
->channel
[chan
].size
> 8) {
499 * Handle all others on a case by case basis.
502 switch (format_desc
->format
) {
503 case PIPE_FORMAT_R1_UNORM
:
504 case PIPE_FORMAT_UYVY
:
505 case PIPE_FORMAT_YUYV
:
506 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
507 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
518 util_format_translate(enum pipe_format dst_format
,
519 void *dst
, unsigned dst_stride
,
520 unsigned dst_x
, unsigned dst_y
,
521 enum pipe_format src_format
,
522 const void *src
, unsigned src_stride
,
523 unsigned src_x
, unsigned src_y
,
524 unsigned width
, unsigned height
)
526 const struct util_format_description
*dst_format_desc
;
527 const struct util_format_description
*src_format_desc
;
529 const uint8_t *src_row
;
530 unsigned x_step
, y_step
;
534 dst_format_desc
= util_format_description(dst_format
);
535 src_format_desc
= util_format_description(src_format
);
537 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
542 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
543 width
, height
, src
, (int)src_stride
,
548 assert(dst_x
% dst_format_desc
->block
.width
== 0);
549 assert(dst_y
% dst_format_desc
->block
.height
== 0);
550 assert(src_x
% src_format_desc
->block
.width
== 0);
551 assert(src_y
% src_format_desc
->block
.height
== 0);
553 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
554 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
557 * This works because all pixel formats have pixel blocks with power of two
561 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
562 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
563 assert(y_step
% dst_format_desc
->block
.height
== 0);
564 assert(y_step
% src_format_desc
->block
.height
== 0);
566 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
567 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
570 * TODO: double formats will loose precision
571 * TODO: Add a special case for formats that are mere swizzles of each other
574 if (src_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
||
575 dst_format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
577 uint8_t *tmp_s
= NULL
;
582 if (src_format_desc
->unpack_z_float
&&
583 dst_format_desc
->pack_z_float
) {
584 tmp_z
= MALLOC(width
* sizeof *tmp_z
);
587 if (src_format_desc
->unpack_s_8uint
&&
588 dst_format_desc
->pack_s_8uint
) {
589 tmp_s
= MALLOC(width
* sizeof *tmp_s
);
594 src_format_desc
->unpack_z_float(tmp_z
, 0, src_row
, src_stride
, width
, 1);
595 dst_format_desc
->pack_z_float(dst_row
, dst_stride
, tmp_z
, 0, width
, 1);
599 src_format_desc
->unpack_s_8uint(tmp_s
, 0, src_row
, src_stride
, width
, 1);
600 dst_format_desc
->pack_s_8uint(dst_row
, dst_stride
, tmp_s
, 0, width
, 1);
614 if (util_format_fits_8unorm(src_format_desc
) ||
615 util_format_fits_8unorm(dst_format_desc
)) {
619 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
620 tmp_row
= MALLOC(y_step
* tmp_stride
);
624 while (height
>= y_step
) {
625 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
626 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
634 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
635 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
644 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
645 tmp_row
= MALLOC(y_step
* tmp_stride
);
649 while (height
>= y_step
) {
650 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
651 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
659 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
660 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
667 void util_format_compose_swizzles(const unsigned char swz1
[4],
668 const unsigned char swz2
[4],
669 unsigned char dst
[4])
673 for (i
= 0; i
< 4; i
++) {
674 dst
[i
] = swz2
[i
] <= UTIL_FORMAT_SWIZZLE_W
?
675 swz1
[swz2
[i
]] : swz2
[i
];
679 void util_format_swizzle_4f(float *dst
, const float *src
,
680 const unsigned char swz
[4])
684 for (i
= 0; i
< 4; i
++) {
685 if (swz
[i
] <= UTIL_FORMAT_SWIZZLE_W
)
686 dst
[i
] = src
[swz
[i
]];
687 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_0
)
689 else if (swz
[i
] == UTIL_FORMAT_SWIZZLE_1
)
694 void util_format_unswizzle_4f(float *dst
, const float *src
,
695 const unsigned char swz
[4])
699 for (i
= 0; i
< 4; i
++) {
701 case UTIL_FORMAT_SWIZZLE_X
:
704 case UTIL_FORMAT_SWIZZLE_Y
:
707 case UTIL_FORMAT_SWIZZLE_Z
:
710 case UTIL_FORMAT_SWIZZLE_W
: