Merge commit 'origin/7.8'
[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
125 void
126 util_format_r8g8bx_snorm_unpack_rgba_float(float *dst_row, unsigned dst_stride,
127 const uint8_t *src_row, unsigned src_stride,
128 unsigned width, unsigned height)
129 {
130 unsigned x, y;
131
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++;
137 int16_t r, g;
138
139 #ifdef PIPE_ARCH_BIG_ENDIAN
140 value = util_bswap32(value);
141 #endif
142
143 r = ((int16_t)(value << 8)) >> 8;
144 g = ((int16_t)(value << 0)) >> 8;
145
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 */
150 dst += 4;
151 }
152 src_row += src_stride;
153 dst_row += dst_stride/sizeof(*dst_row);
154 }
155 }
156
157
158 void
159 util_format_r8g8bx_snorm_unpack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
160 const uint8_t *src_row, unsigned src_stride,
161 unsigned width, unsigned height)
162 {
163 unsigned x, y;
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++;
169 int16_t r, g;
170
171 #ifdef PIPE_ARCH_BIG_ENDIAN
172 value = util_bswap32(value);
173 #endif
174
175 r = ((int16_t)(value << 8)) >> 8;
176 g = ((int16_t)(value << 0)) >> 8;
177
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 */
182 dst += 4;
183 }
184 src_row += src_stride;
185 dst_row += dst_stride/sizeof(*dst_row);
186 }
187 }
188
189
190 void
191 util_format_r8g8bx_snorm_pack_rgba_float(uint8_t *dst_row, unsigned dst_stride,
192 const float *src_row, unsigned src_stride,
193 unsigned width, unsigned height)
194 {
195 unsigned x, y;
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) {
200 uint16_t value = 0;
201
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) ;
204
205 #ifdef PIPE_ARCH_BIG_ENDIAN
206 value = util_bswap32(value);
207 #endif
208
209 *dst++ = value;
210
211 src += 4;
212 }
213 dst_row += dst_stride;
214 src_row += src_stride/sizeof(*src_row);
215 }
216 }
217
218
219 void
220 util_format_r8g8bx_snorm_pack_rgba_8unorm(uint8_t *dst_row, unsigned dst_stride,
221 const uint8_t *src_row, unsigned src_stride,
222 unsigned width, unsigned height)
223 {
224 unsigned x, y;
225
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) {
230 uint16_t value = 0;
231
232 value |= src[0] >> 1;
233 value |= (src[1] >> 1) << 8;
234
235 #ifdef PIPE_ARCH_BIG_ENDIAN
236 value = util_bswap32(value);
237 #endif
238
239 *dst++ = value;
240
241 src += 4;
242 }
243 dst_row += dst_stride;
244 src_row += src_stride/sizeof(*src_row);
245 }
246 }
247
248
249 void
250 util_format_r8g8bx_snorm_fetch_rgba_float(float *dst, const uint8_t *src,
251 unsigned i, unsigned j)
252 {
253 uint16_t value = *(const uint16_t *)src;
254 int16_t r, g;
255
256 #ifdef PIPE_ARCH_BIG_ENDIAN
257 value = util_bswap32(value);
258 #endif
259
260 r = ((int16_t)(value << 8)) >> 8;
261 g = ((int16_t)(value << 0)) >> 8;
262
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 */
267 }