Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / auxiliary / util / u_format_other.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
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.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29 #include "u_math.h"
30 #include "u_format_other.h"
31
32
33 void
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)
37 {
38
39 }
40
41 void
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)
45 {
46
47 }
48
49 void
50 util_format_r9g9b9e5_float_fetch_rgba_float(float *dst, const uint8_t *src,
51 unsigned i, unsigned j)
52 {
53
54 }
55
56
57 void
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)
61 {
62
63 }
64
65
66 void
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)
70 {
71
72 }
73
74
75 void
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)
79 {
80
81 }
82
83
84 void
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)
88 {
89
90 }
91
92
93 void
94 util_format_r1_unorm_fetch_rgba_float(float *dst, const uint8_t *src,
95 unsigned i, unsigned j)
96 {
97
98 }
99
100
101 void
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)
105 {
106
107 }
108
109
110 void
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)
114 {
115 }
116
117
118 /*
119 * PIPE_FORMAT_R8G8Bx_SNORM
120 *
121 * A.k.a. D3DFMT_CxV8U8
122 */
123
124 static uint8_t
125 r8g8bx_derive(int16_t r, int16_t g)
126 {
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.
130 */
131 return (uint8_t)sqrtf(0x7f * 0x7f - r * r - g * g) * 0xff / 0x7f;
132 }
133
134 void
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)
138 {
139 unsigned x, y;
140
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++;
146 int16_t r, g;
147
148 #ifdef PIPE_ARCH_BIG_ENDIAN
149 value = util_bswap32(value);
150 #endif
151
152 r = ((int16_t)(value << 8)) >> 8;
153 g = ((int16_t)(value << 0)) >> 8;
154
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 */
159 dst += 4;
160 }
161 src_row += src_stride;
162 dst_row += dst_stride/sizeof(*dst_row);
163 }
164 }
165
166
167 void
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)
171 {
172 unsigned x, y;
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++;
178 int16_t r, g;
179
180 #ifdef PIPE_ARCH_BIG_ENDIAN
181 value = util_bswap32(value);
182 #endif
183
184 r = ((int16_t)(value << 8)) >> 8;
185 g = ((int16_t)(value << 0)) >> 8;
186
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 */
191 dst += 4;
192 }
193 src_row += src_stride;
194 dst_row += dst_stride/sizeof(*dst_row);
195 }
196 }
197
198
199 void
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)
203 {
204 unsigned x, y;
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) {
209 uint16_t value = 0;
210
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) ;
213
214 #ifdef PIPE_ARCH_BIG_ENDIAN
215 value = util_bswap32(value);
216 #endif
217
218 *dst++ = value;
219
220 src += 4;
221 }
222 dst_row += dst_stride;
223 src_row += src_stride/sizeof(*src_row);
224 }
225 }
226
227
228 void
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)
232 {
233 unsigned x, y;
234
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) {
239 uint16_t value = 0;
240
241 value |= src[0] >> 1;
242 value |= (src[1] >> 1) << 8;
243
244 #ifdef PIPE_ARCH_BIG_ENDIAN
245 value = util_bswap32(value);
246 #endif
247
248 *dst++ = value;
249
250 src += 4;
251 }
252 dst_row += dst_stride;
253 src_row += src_stride/sizeof(*src_row);
254 }
255 }
256
257
258 void
259 util_format_r8g8bx_snorm_fetch_rgba_float(float *dst, const uint8_t *src,
260 unsigned i, unsigned j)
261 {
262 uint16_t value = *(const uint16_t *)src;
263 int16_t r, g;
264
265 #ifdef PIPE_ARCH_BIG_ENDIAN
266 value = util_bswap32(value);
267 #endif
268
269 r = ((int16_t)(value << 8)) >> 8;
270 g = ((int16_t)(value << 0)) >> 8;
271
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 */
276 }