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>
42 util_format_read_4f(enum pipe_format format
,
43 float *dst
, unsigned dst_stride
,
44 const void *src
, unsigned src_stride
,
45 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
47 const struct util_format_description
*format_desc
;
48 const uint8_t *src_row
;
51 format_desc
= util_format_description(format
);
53 assert(x
% format_desc
->block
.width
== 0);
54 assert(y
% format_desc
->block
.height
== 0);
56 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
59 format_desc
->unpack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
64 util_format_write_4f(enum pipe_format format
,
65 const float *src
, unsigned src_stride
,
66 void *dst
, unsigned dst_stride
,
67 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
69 const struct util_format_description
*format_desc
;
73 format_desc
= util_format_description(format
);
75 assert(x
% format_desc
->block
.width
== 0);
76 assert(y
% format_desc
->block
.height
== 0);
78 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
81 format_desc
->pack_rgba_float(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
86 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
)
88 const struct util_format_description
*format_desc
;
89 const uint8_t *src_row
;
92 format_desc
= util_format_description(format
);
94 assert(x
% format_desc
->block
.width
== 0);
95 assert(y
% format_desc
->block
.height
== 0);
97 src_row
= (const uint8_t *)src
+ y
*src_stride
+ x
*(format_desc
->block
.bits
/8);
100 format_desc
->unpack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
105 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
)
107 const struct util_format_description
*format_desc
;
109 const uint8_t *src_row
;
111 format_desc
= util_format_description(format
);
113 assert(x
% format_desc
->block
.width
== 0);
114 assert(y
% format_desc
->block
.height
== 0);
116 dst_row
= (uint8_t *)dst
+ y
*dst_stride
+ x
*(format_desc
->block
.bits
/8);
119 format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, src_row
, src_stride
, w
, h
);
124 util_is_format_compatible(const struct util_format_description
*src_desc
,
125 const struct util_format_description
*dst_desc
)
129 if (src_desc
->format
== dst_desc
->format
) {
133 if (src_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
134 dst_desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
138 if (src_desc
->block
.bits
!= dst_desc
->block
.bits
||
139 src_desc
->nr_channels
!= dst_desc
->nr_channels
||
140 src_desc
->colorspace
!= dst_desc
->colorspace
) {
144 for (chan
= 0; chan
< 4; ++chan
) {
145 if (src_desc
->channel
[chan
].size
!=
146 dst_desc
->channel
[chan
].size
) {
151 for (chan
= 0; chan
< 4; ++chan
) {
152 enum util_format_swizzle swizzle
= dst_desc
->swizzle
[chan
];
155 if (src_desc
->swizzle
[chan
] != swizzle
) {
158 if ((src_desc
->channel
[swizzle
].type
!=
159 dst_desc
->channel
[swizzle
].type
) ||
160 (src_desc
->channel
[swizzle
].normalized
!=
161 dst_desc
->channel
[swizzle
].normalized
)) {
172 util_format_fits_8unorm(const struct util_format_description
*format_desc
)
176 switch (format_desc
->layout
) {
178 case UTIL_FORMAT_LAYOUT_S3TC
:
179 case UTIL_FORMAT_LAYOUT_RGTC
:
181 * These are straight forward.
186 case UTIL_FORMAT_LAYOUT_PLAIN
:
188 * For these we can find a generic rule.
191 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
192 switch (format_desc
->channel
[chan
].type
) {
193 case UTIL_FORMAT_TYPE_VOID
:
195 case UTIL_FORMAT_TYPE_UNSIGNED
:
196 if (!format_desc
->channel
[chan
].normalized
||
197 format_desc
->channel
[chan
].size
> 8) {
209 * Handle all others on a case by case basis.
212 switch (format_desc
->format
) {
213 case PIPE_FORMAT_R1_UNORM
:
214 case PIPE_FORMAT_UYVY
:
215 case PIPE_FORMAT_YUYV
:
216 case PIPE_FORMAT_R8G8_B8G8_UNORM
:
217 case PIPE_FORMAT_G8R8_G8B8_UNORM
:
228 util_format_translate(enum pipe_format dst_format
,
229 void *dst
, unsigned dst_stride
,
230 unsigned dst_x
, unsigned dst_y
,
231 enum pipe_format src_format
,
232 const void *src
, unsigned src_stride
,
233 unsigned src_x
, unsigned src_y
,
234 unsigned width
, unsigned height
)
236 const struct util_format_description
*dst_format_desc
;
237 const struct util_format_description
*src_format_desc
;
239 const uint8_t *src_row
;
240 unsigned x_step
, y_step
;
244 dst_format_desc
= util_format_description(dst_format
);
245 src_format_desc
= util_format_description(src_format
);
247 if (util_is_format_compatible(src_format_desc
, dst_format_desc
)) {
252 util_copy_rect(dst
, dst_format
, dst_stride
, dst_x
, dst_y
,
253 width
, height
, src
, (int)src_stride
,
258 assert(dst_x
% dst_format_desc
->block
.width
== 0);
259 assert(dst_y
% dst_format_desc
->block
.height
== 0);
260 assert(src_x
% src_format_desc
->block
.width
== 0);
261 assert(src_y
% src_format_desc
->block
.height
== 0);
263 dst_row
= (uint8_t *)dst
+ dst_y
*dst_stride
+ dst_x
*(dst_format_desc
->block
.bits
/8);
264 src_row
= (const uint8_t *)src
+ src_y
*src_stride
+ src_x
*(src_format_desc
->block
.bits
/8);
267 * This works because all pixel formats have pixel blocks with power of two
271 y_step
= MAX2(dst_format_desc
->block
.height
, src_format_desc
->block
.height
);
272 x_step
= MAX2(dst_format_desc
->block
.width
, src_format_desc
->block
.width
);
273 assert(y_step
% dst_format_desc
->block
.height
== 0);
274 assert(y_step
% src_format_desc
->block
.height
== 0);
276 dst_step
= y_step
/ dst_format_desc
->block
.height
* dst_stride
;
277 src_step
= y_step
/ src_format_desc
->block
.height
* src_stride
;
280 * TODO: double formats will loose precision
281 * TODO: Add a special case for formats that are mere swizzles of each other
284 if (util_format_fits_8unorm(src_format_desc
) ||
285 util_format_fits_8unorm(dst_format_desc
)) {
289 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
290 tmp_row
= MALLOC(y_step
* tmp_stride
);
294 while (height
>= y_step
) {
295 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
296 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
304 src_format_desc
->unpack_rgba_8unorm(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
305 dst_format_desc
->pack_rgba_8unorm(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);
314 tmp_stride
= MAX2(width
, x_step
) * 4 * sizeof *tmp_row
;
315 tmp_row
= MALLOC(y_step
* tmp_stride
);
319 while (height
>= y_step
) {
320 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, y_step
);
321 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, y_step
);
329 src_format_desc
->unpack_rgba_float(tmp_row
, tmp_stride
, src_row
, src_stride
, width
, height
);
330 dst_format_desc
->pack_rgba_float(dst_row
, dst_stride
, tmp_row
, tmp_stride
, width
, height
);