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_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_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_float(float *dst
, const uint8_t *src
,
51 unsigned i
, unsigned j
)
58 util_format_r9g9b9e5_float_unpack_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_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_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_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_float(float *dst
, const uint8_t *src
,
95 unsigned i
, unsigned j
)
102 util_format_r1_unorm_unpack_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_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
126 util_format_r8g8bx_snorm_unpack_float(float *dst_row
, unsigned dst_stride
,
127 const uint8_t *src_row
, unsigned src_stride
,
128 unsigned width
, unsigned height
)
132 for(y
= 0; y
< height
; y
+= 1) {
133 float *dst
= dst_row
;
134 const uint16_t *src
= (const uint16_t *)src_row
;
135 for(x
= 0; x
< width
; x
+= 1) {
136 uint16_t value
= *src
++;
139 #ifdef PIPE_ARCH_BIG_ENDIAN
140 value
= util_bswap32(value
);
143 r
= ((int16_t)(value
<< 8)) >> 8;
144 g
= ((int16_t)(value
<< 0)) >> 8;
146 dst
[0] = (float)(r
* (1.0f
/0x7f)); /* r */
147 dst
[1] = (float)(g
* (1.0f
/0x7f)); /* g */
148 dst
[2] = sqrtf(1.0f
- dst
[0] * dst
[0] - dst
[1] * dst
[1]); /* b */
149 dst
[3] = 1.0f
; /* a */
152 src_row
+= src_stride
;
153 dst_row
+= dst_stride
/sizeof(*dst_row
);
159 util_format_r8g8bx_snorm_unpack_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
160 const uint8_t *src_row
, unsigned src_stride
,
161 unsigned width
, unsigned height
)
164 for(y
= 0; y
< height
; y
+= 1) {
165 uint8_t *dst
= dst_row
;
166 const uint16_t *src
= (const uint16_t *)src_row
;
167 for(x
= 0; x
< width
; x
+= 1) {
168 uint16_t value
= *src
++;
171 #ifdef PIPE_ARCH_BIG_ENDIAN
172 value
= util_bswap32(value
);
175 r
= ((int16_t)(value
<< 8)) >> 8;
176 g
= ((int16_t)(value
<< 0)) >> 8;
178 dst
[0] = (uint8_t)(((uint16_t)MAX2(r
, 0)) * 0xff / 0x7f); /* r */
179 dst
[1] = (uint8_t)(((uint16_t)MAX2(g
, 0)) * 0xff / 0x7f); /* g */
180 dst
[2] = (uint8_t)sqrtf(0x7f*0x7f - r
* r
- g
* g
) * 0xff / 0x7f; /* b */
181 dst
[3] = 255; /* a */
184 src_row
+= src_stride
;
185 dst_row
+= dst_stride
/sizeof(*dst_row
);
191 util_format_r8g8bx_snorm_pack_float(uint8_t *dst_row
, unsigned dst_stride
,
192 const float *src_row
, unsigned src_stride
,
193 unsigned width
, unsigned height
)
196 for(y
= 0; y
< height
; y
+= 1) {
197 const float *src
= src_row
;
198 uint16_t *dst
= (uint16_t *)dst_row
;
199 for(x
= 0; x
< width
; x
+= 1) {
202 value
|= (uint16_t)(((int8_t)(CLAMP(src
[0], -1, 1) * 0x7f)) & 0xff) ;
203 value
|= (uint16_t)((((int8_t)(CLAMP(src
[1], -1, 1) * 0x7f)) & 0xff) << 8) ;
205 #ifdef PIPE_ARCH_BIG_ENDIAN
206 value
= util_bswap32(value
);
213 dst_row
+= dst_stride
;
214 src_row
+= src_stride
/sizeof(*src_row
);
220 util_format_r8g8bx_snorm_pack_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
221 const uint8_t *src_row
, unsigned src_stride
,
222 unsigned width
, unsigned height
)
226 for(y
= 0; y
< height
; y
+= 1) {
227 const uint8_t *src
= src_row
;
228 uint16_t *dst
= (uint16_t *)dst_row
;
229 for(x
= 0; x
< width
; x
+= 1) {
232 value
|= src
[0] >> 1;
233 value
|= (src
[1] >> 1) << 8;
235 #ifdef PIPE_ARCH_BIG_ENDIAN
236 value
= util_bswap32(value
);
243 dst_row
+= dst_stride
;
244 src_row
+= src_stride
/sizeof(*src_row
);
250 util_format_r8g8bx_snorm_fetch_float(float *dst
, const uint8_t *src
,
251 unsigned i
, unsigned j
)
253 uint16_t value
= *(const uint16_t *)src
;
256 #ifdef PIPE_ARCH_BIG_ENDIAN
257 value
= util_bswap32(value
);
260 r
= ((int16_t)(value
<< 8)) >> 8;
261 g
= ((int16_t)(value
<< 0)) >> 8;
263 dst
[0] = r
* (1.0f
/0x7f); /* r */
264 dst
[1] = g
* (1.0f
/0x7f); /* g */
265 dst
[2] = sqrtf(1.0f
- dst
[0] * dst
[0] - dst
[1] * dst
[1]); /* b */
266 dst
[3] = 1.0f
; /* a */