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"
31 #include "util/format_rgb9e5.h"
32 #include "util/format_r11g11b10f.h"
36 util_format_r9g9b9e5_float_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
37 const uint8_t *src_row
, unsigned src_stride
,
38 unsigned width
, unsigned height
)
41 for(y
= 0; y
< height
; y
+= 1) {
43 const uint8_t *src
= src_row
;
44 for(x
= 0; x
< width
; x
+= 1) {
45 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
46 rgb9e5_to_float3(value
, dst
);
51 src_row
+= src_stride
;
52 dst_row
+= dst_stride
/sizeof(*dst_row
);
57 util_format_r9g9b9e5_float_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
58 const float *src_row
, unsigned src_stride
,
59 unsigned width
, unsigned height
)
62 for(y
= 0; y
< height
; y
+= 1) {
63 const float *src
= src_row
;
64 uint8_t *dst
= dst_row
;
65 for(x
= 0; x
< width
; x
+= 1) {
66 uint32_t value
= util_cpu_to_le32(float3_to_rgb9e5(src
));
67 *(uint32_t *)dst
= value
;
71 dst_row
+= dst_stride
;
72 src_row
+= src_stride
/sizeof(*src_row
);
77 util_format_r9g9b9e5_float_fetch_rgba_float(float *dst
, const uint8_t *src
,
78 unsigned i
, unsigned j
)
80 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
81 rgb9e5_to_float3(value
, dst
);
87 util_format_r9g9b9e5_float_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
88 const uint8_t *src_row
, unsigned src_stride
,
89 unsigned width
, unsigned height
)
93 for(y
= 0; y
< height
; y
+= 1) {
94 uint8_t *dst
= dst_row
;
95 const uint8_t *src
= src_row
;
96 for(x
= 0; x
< width
; x
+= 1) {
97 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
98 rgb9e5_to_float3(value
, p
);
99 dst
[0] = float_to_ubyte(p
[0]); /* r */
100 dst
[1] = float_to_ubyte(p
[1]); /* g */
101 dst
[2] = float_to_ubyte(p
[2]); /* b */
102 dst
[3] = 255; /* a */
106 src_row
+= src_stride
;
107 dst_row
+= dst_stride
/sizeof(*dst_row
);
113 util_format_r9g9b9e5_float_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
114 const uint8_t *src_row
, unsigned src_stride
,
115 unsigned width
, unsigned height
)
119 for(y
= 0; y
< height
; y
+= 1) {
120 const uint8_t *src
= src_row
;
121 uint8_t *dst
= dst_row
;
122 for(x
= 0; x
< width
; x
+= 1) {
124 p
[0] = ubyte_to_float(src
[0]);
125 p
[1] = ubyte_to_float(src
[1]);
126 p
[2] = ubyte_to_float(src
[2]);
127 value
= util_cpu_to_le32(float3_to_rgb9e5(p
));
128 *(uint32_t *)dst
= value
;
132 dst_row
+= dst_stride
;
133 src_row
+= src_stride
/sizeof(*src_row
);
139 util_format_r11g11b10_float_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
140 const uint8_t *src_row
, unsigned src_stride
,
141 unsigned width
, unsigned height
)
144 for(y
= 0; y
< height
; y
+= 1) {
145 float *dst
= dst_row
;
146 const uint8_t *src
= src_row
;
147 for(x
= 0; x
< width
; x
+= 1) {
148 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
149 r11g11b10f_to_float3(value
, dst
);
154 src_row
+= src_stride
;
155 dst_row
+= dst_stride
/sizeof(*dst_row
);
160 util_format_r11g11b10_float_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
161 const float *src_row
, unsigned src_stride
,
162 unsigned width
, unsigned height
)
165 for(y
= 0; y
< height
; y
+= 1) {
166 const float *src
= src_row
;
167 uint8_t *dst
= dst_row
;
168 for(x
= 0; x
< width
; x
+= 1) {
169 uint32_t value
= util_cpu_to_le32(float3_to_r11g11b10f(src
));
170 *(uint32_t *)dst
= value
;
174 dst_row
+= dst_stride
;
175 src_row
+= src_stride
/sizeof(*src_row
);
180 util_format_r11g11b10_float_fetch_rgba_float(float *dst
, const uint8_t *src
,
181 unsigned i
, unsigned j
)
183 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
184 r11g11b10f_to_float3(value
, dst
);
190 util_format_r11g11b10_float_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
191 const uint8_t *src_row
, unsigned src_stride
,
192 unsigned width
, unsigned height
)
196 for(y
= 0; y
< height
; y
+= 1) {
197 uint8_t *dst
= dst_row
;
198 const uint8_t *src
= src_row
;
199 for(x
= 0; x
< width
; x
+= 1) {
200 uint32_t value
= util_cpu_to_le32(*(const uint32_t *)src
);
201 r11g11b10f_to_float3(value
, p
);
202 dst
[0] = float_to_ubyte(p
[0]); /* r */
203 dst
[1] = float_to_ubyte(p
[1]); /* g */
204 dst
[2] = float_to_ubyte(p
[2]); /* b */
205 dst
[3] = 255; /* a */
209 src_row
+= src_stride
;
210 dst_row
+= dst_stride
/sizeof(*dst_row
);
216 util_format_r11g11b10_float_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
217 const uint8_t *src_row
, unsigned src_stride
,
218 unsigned width
, unsigned height
)
222 for(y
= 0; y
< height
; y
+= 1) {
223 const uint8_t *src
= src_row
;
224 uint8_t *dst
= dst_row
;
225 for(x
= 0; x
< width
; x
+= 1) {
227 p
[0] = ubyte_to_float(src
[0]);
228 p
[1] = ubyte_to_float(src
[1]);
229 p
[2] = ubyte_to_float(src
[2]);
230 value
= util_cpu_to_le32(float3_to_r11g11b10f(p
));
231 *(uint32_t *)dst
= value
;
235 dst_row
+= dst_stride
;
236 src_row
+= src_stride
/sizeof(*src_row
);
242 util_format_r1_unorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
243 const uint8_t *src_row
, unsigned src_stride
,
244 unsigned width
, unsigned height
)
251 util_format_r1_unorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
252 const float *src_row
, unsigned src_stride
,
253 unsigned width
, unsigned height
)
260 util_format_r1_unorm_fetch_rgba_float(float *dst
, const uint8_t *src
,
261 unsigned i
, unsigned j
)
268 util_format_r1_unorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
269 const uint8_t *src_row
, unsigned src_stride
,
270 unsigned width
, unsigned height
)
277 util_format_r1_unorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
278 const uint8_t *src_row
, unsigned src_stride
,
279 unsigned width
, unsigned height
)
285 * PIPE_FORMAT_R8G8Bx_SNORM
287 * A.k.a. D3DFMT_CxV8U8
291 r8g8bx_derive(int16_t r
, int16_t g
)
293 /* Derive blue from red and green components.
294 * Apparently, we must always use integers to perform calculations,
295 * otherwise the results won't match D3D's CxV8U8 definition.
297 return (uint8_t)sqrtf(0x7f * 0x7f - r
* r
- g
* g
) * 0xff / 0x7f;
301 util_format_r8g8bx_snorm_unpack_rgba_float(float *dst_row
, unsigned dst_stride
,
302 const uint8_t *src_row
, unsigned src_stride
,
303 unsigned width
, unsigned height
)
307 for(y
= 0; y
< height
; y
+= 1) {
308 float *dst
= dst_row
;
309 const uint16_t *src
= (const uint16_t *)src_row
;
310 for(x
= 0; x
< width
; x
+= 1) {
311 uint16_t value
= util_cpu_to_le16(*src
++);
314 r
= ((int16_t)(value
<< 8)) >> 8;
315 g
= ((int16_t)(value
<< 0)) >> 8;
317 dst
[0] = (float)(r
* (1.0f
/0x7f)); /* r */
318 dst
[1] = (float)(g
* (1.0f
/0x7f)); /* g */
319 dst
[2] = r8g8bx_derive(r
, g
) * (1.0f
/0xff); /* b */
320 dst
[3] = 1.0f
; /* a */
323 src_row
+= src_stride
;
324 dst_row
+= dst_stride
/sizeof(*dst_row
);
330 util_format_r8g8bx_snorm_unpack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
331 const uint8_t *src_row
, unsigned src_stride
,
332 unsigned width
, unsigned height
)
335 for(y
= 0; y
< height
; y
+= 1) {
336 uint8_t *dst
= dst_row
;
337 const uint16_t *src
= (const uint16_t *)src_row
;
338 for(x
= 0; x
< width
; x
+= 1) {
339 uint16_t value
= util_cpu_to_le16(*src
++);
342 r
= ((int16_t)(value
<< 8)) >> 8;
343 g
= ((int16_t)(value
<< 0)) >> 8;
345 dst
[0] = (uint8_t)(((uint16_t)MAX2(r
, 0)) * 0xff / 0x7f); /* r */
346 dst
[1] = (uint8_t)(((uint16_t)MAX2(g
, 0)) * 0xff / 0x7f); /* g */
347 dst
[2] = r8g8bx_derive(r
, g
); /* b */
348 dst
[3] = 255; /* a */
351 src_row
+= src_stride
;
352 dst_row
+= dst_stride
/sizeof(*dst_row
);
358 util_format_r8g8bx_snorm_pack_rgba_float(uint8_t *dst_row
, unsigned dst_stride
,
359 const float *src_row
, unsigned src_stride
,
360 unsigned width
, unsigned height
)
363 for(y
= 0; y
< height
; y
+= 1) {
364 const float *src
= src_row
;
365 uint16_t *dst
= (uint16_t *)dst_row
;
366 for(x
= 0; x
< width
; x
+= 1) {
369 value
|= (uint16_t)(((int8_t)(CLAMP(src
[0], -1, 1) * 0x7f)) & 0xff) ;
370 value
|= (uint16_t)((((int8_t)(CLAMP(src
[1], -1, 1) * 0x7f)) & 0xff) << 8) ;
372 *dst
++ = util_le16_to_cpu(value
);
376 dst_row
+= dst_stride
;
377 src_row
+= src_stride
/sizeof(*src_row
);
383 util_format_r8g8bx_snorm_pack_rgba_8unorm(uint8_t *dst_row
, unsigned dst_stride
,
384 const uint8_t *src_row
, unsigned src_stride
,
385 unsigned width
, unsigned height
)
389 for(y
= 0; y
< height
; y
+= 1) {
390 const uint8_t *src
= src_row
;
391 uint16_t *dst
= (uint16_t *)dst_row
;
392 for(x
= 0; x
< width
; x
+= 1) {
395 value
|= src
[0] >> 1;
396 value
|= (src
[1] >> 1) << 8;
398 *dst
++ = util_le16_to_cpu(value
);
402 dst_row
+= dst_stride
;
403 src_row
+= src_stride
/sizeof(*src_row
);
409 util_format_r8g8bx_snorm_fetch_rgba_float(float *dst
, const uint8_t *src
,
410 unsigned i
, unsigned j
)
412 uint16_t value
= util_cpu_to_le16(*(const uint16_t *)src
);
415 r
= ((int16_t)(value
<< 8)) >> 8;
416 g
= ((int16_t)(value
<< 0)) >> 8;
418 dst
[0] = r
* (1.0f
/0x7f); /* r */
419 dst
[1] = g
* (1.0f
/0x7f); /* g */
420 dst
[2] = r8g8bx_derive(r
, g
) * (1.0f
/0xff); /* b */
421 dst
[3] = 1.0f
; /* a */