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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 **************************************************************************/
30 #include "u_format_other.h"
34 util_format_r9g9b9e5_float_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
35 const uint8_t *src_row
, unsigned src_stride
,
36 unsigned width
, unsigned height
)
42 util_format_r9g9b9e5_float_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
43 const float *src_row
, unsigned src_stride
,
44 unsigned width
, unsigned height
)
50 util_format_r9g9b9e5_float_fetch_rgba_float(float *dst
, const uint8_t *src
,
51 unsigned i
, unsigned j
)
58 util_format_r9g9b9e5_float_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
59 const uint8_t *src_row
, unsigned src_stride
,
60 unsigned width
, unsigned height
)
67 util_format_r9g9b9e5_float_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
68 const uint8_t *src_row
, unsigned src_stride
,
69 unsigned width
, unsigned height
)
76 util_format_r1_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
77 const uint8_t *src_row
, unsigned src_stride
,
78 unsigned width
, unsigned height
)
85 util_format_r1_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
86 const float *src_row
, unsigned src_stride
,
87 unsigned width
, unsigned height
)
94 util_format_r1_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
,
95 unsigned i
, unsigned j
)
102 util_format_r1_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
103 const uint8_t *src_row
, unsigned src_stride
,
104 unsigned width
, unsigned height
)
111 util_format_r1_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
112 const uint8_t *src_row
, unsigned src_stride
,
113 unsigned width
, unsigned height
)
119 * PIPE_FORMAT_R8G8Bx_SNORM
121 * A.k.a. D3DFMT_CxV8U8
125 r8g8bx_derive(int16_t r
, int16_t g
)
127 /* Derive blue from red and green components.
128 * Apparently, we must always use integers to perform calculations,
129 * otherwise the results won't match D3D's CxV8U8 definition.
131 return (uint8_t)sqrtf(0x7f * 0x7f - r
* r
- g
* g
) * 0xff / 0x7f;
135 util_format_r8g8bx_snorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
136 const uint8_t *src_row
, unsigned src_stride
,
137 unsigned width
, unsigned height
)
141 for(y
= 0; y
< height
; y
+= 1) {
142 float *dst
= dst_row
;
143 const uint16_t *src
= (const uint16_t *)src_row
;
144 for(x
= 0; x
< width
; x
+= 1) {
145 uint16_t value
= *src
++;
148 #ifdef PIPE_ARCH_BIG_ENDIAN
149 value
= util_bswap32(value
);
152 r
= ((int16_t)(value
<< 8)) >> 8;
153 g
= ((int16_t)(value
<< 0)) >> 8;
155 dst
[0] = (float)(r
* (1.0f
/0x7f)); /* r */
156 dst
[1] = (float)(g
* (1.0f
/0x7f)); /* g */
157 dst
[2] = r8g8bx_derive(r
, g
) * (1.0f
/0xff); /* b */
158 dst
[3] = 1.0f
; /* a */
161 src_row
+= src_stride
;
162 dst_row
+= dst_stride
/sizeof(*dst_row
);
168 util_format_r8g8bx_snorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
169 const uint8_t *src_row
, unsigned src_stride
,
170 unsigned width
, unsigned height
)
173 for(y
= 0; y
< height
; y
+= 1) {
174 uint8_t *dst
= dst_row
;
175 const uint16_t *src
= (const uint16_t *)src_row
;
176 for(x
= 0; x
< width
; x
+= 1) {
177 uint16_t value
= *src
++;
180 #ifdef PIPE_ARCH_BIG_ENDIAN
181 value
= util_bswap32(value
);
184 r
= ((int16_t)(value
<< 8)) >> 8;
185 g
= ((int16_t)(value
<< 0)) >> 8;
187 dst
[0] = (uint8_t)(((uint16_t)MAX2(r
, 0)) * 0xff / 0x7f); /* r */
188 dst
[1] = (uint8_t)(((uint16_t)MAX2(g
, 0)) * 0xff / 0x7f); /* g */
189 dst
[2] = r8g8bx_derive(r
, g
); /* b */
190 dst
[3] = 255; /* a */
193 src_row
+= src_stride
;
194 dst_row
+= dst_stride
/sizeof(*dst_row
);
200 util_format_r8g8bx_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
201 const float *src_row
, unsigned src_stride
,
202 unsigned width
, unsigned height
)
205 for(y
= 0; y
< height
; y
+= 1) {
206 const float *src
= src_row
;
207 uint16_t *dst
= (uint16_t *)dst_row
;
208 for(x
= 0; x
< width
; x
+= 1) {
211 value
|= (uint16_t)(((int8_t)(CLAMP(src
[0], -1, 1) * 0x7f)) & 0xff) ;
212 value
|= (uint16_t)((((int8_t)(CLAMP(src
[1], -1, 1) * 0x7f)) & 0xff) << 8) ;
214 #ifdef PIPE_ARCH_BIG_ENDIAN
215 value
= util_bswap32(value
);
222 dst_row
+= dst_stride
;
223 src_row
+= src_stride
/sizeof(*src_row
);
229 util_format_r8g8bx_snorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
230 const uint8_t *src_row
, unsigned src_stride
,
231 unsigned width
, unsigned height
)
235 for(y
= 0; y
< height
; y
+= 1) {
236 const uint8_t *src
= src_row
;
237 uint16_t *dst
= (uint16_t *)dst_row
;
238 for(x
= 0; x
< width
; x
+= 1) {
241 value
|= src
[0] >> 1;
242 value
|= (src
[1] >> 1) << 8;
244 #ifdef PIPE_ARCH_BIG_ENDIAN
245 value
= util_bswap32(value
);
252 dst_row
+= dst_stride
;
253 src_row
+= src_stride
/sizeof(*src_row
);
259 util_format_r8g8bx_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
,
260 unsigned i
, unsigned j
)
262 uint16_t value
= *(const uint16_t *)src
;
265 #ifdef PIPE_ARCH_BIG_ENDIAN
266 value
= util_bswap32(value
);
269 r
= ((int16_t)(value
<< 8)) >> 8;
270 g
= ((int16_t)(value
<< 0)) >> 8;
272 dst
[0] = r
* (1.0f
/0x7f); /* r */
273 dst
[1] = g
* (1.0f
/0x7f); /* g */
274 dst
[2] = r8g8bx_derive(r
, g
) * (1.0f
/0xff); /* b */
275 dst
[3] = 1.0f
; /* a */