merge gl_texture_image RowStride from DRI 4.0.4
[mesa.git] / src / mesa / main / texformat_tmp.h
1 /* $Id: texformat_tmp.h,v 1.8 2002/09/23 16:37:14 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Author:
27 * Gareth Hughes <gareth@valinux.com>
28 */
29
30 #if DIM == 1
31
32 #define CHAN_SRC( t, i, j, k, sz ) \
33 ((GLchan *)(t)->Data + (i) * (sz))
34 #define UBYTE_SRC( t, i, j, k, sz ) \
35 ((GLubyte *)(t)->Data + (i) * (sz))
36 #define USHORT_SRC( t, i, j, k ) \
37 ((GLushort *)(t)->Data + (i))
38 #define FLOAT_SRC( t, i, j, k ) \
39 ((GLfloat *)(t)->Data + (i))
40
41 #define FETCH(x) fetch_1d_texel_##x
42
43 #elif DIM == 2
44
45 #define CHAN_SRC( t, i, j, k, sz ) \
46 ((GLchan *)(t)->Data + ((t)->RowStride * (j) + (i)) * (sz))
47 #define UBYTE_SRC( t, i, j, k, sz ) \
48 ((GLubyte *)(t)->Data + ((t)->RowStride * (j) + (i)) * (sz))
49 #define USHORT_SRC( t, i, j, k ) \
50 ((GLushort *)(t)->Data + ((t)->RowStride * (j) + (i)))
51 #define FLOAT_SRC( t, i, j, k ) \
52 ((GLfloat *)(t)->Data + ((t)->RowStride * (j) + (i)))
53
54 #define FETCH(x) fetch_2d_texel_##x
55
56 #elif DIM == 3
57
58 #define CHAN_SRC( t, i, j, k, sz ) \
59 (GLchan *)(t)->Data + (((t)->Height * (k) + (j)) * \
60 (t)->RowStride + (i)) * (sz)
61 #define UBYTE_SRC( t, i, j, k, sz ) \
62 ((GLubyte *)(t)->Data + (((t)->Height * (k) + (j)) * \
63 (t)->RowStride + (i)) * (sz))
64 #define USHORT_SRC( t, i, j, k ) \
65 ((GLushort *)(t)->Data + (((t)->Height * (k) + (j)) * \
66 (t)->RowStride + (i)))
67 #define FLOAT_SRC( t, i, j, k ) \
68 ((GLfloat *)(t)->Data + (((t)->Height * (k) + (j)) * \
69 (t)->RowStride + (i)))
70
71 #define FETCH(x) fetch_3d_texel_##x
72
73 #else
74 #error illegal number of texture dimensions
75 #endif
76
77
78 static void FETCH(rgba)( const struct gl_texture_image *texImage,
79 GLint i, GLint j, GLint k, GLvoid *texel )
80 {
81 const GLchan *src = CHAN_SRC( texImage, i, j, k, 4 );
82 GLchan *rgba = (GLchan *) texel;
83 COPY_CHAN4( rgba, src );
84 }
85
86 static void FETCH(rgb)( const struct gl_texture_image *texImage,
87 GLint i, GLint j, GLint k, GLvoid *texel )
88 {
89 const GLchan *src = CHAN_SRC( texImage, i, j, k, 3 );
90 GLchan *rgba = (GLchan *) texel;
91 rgba[RCOMP] = src[0];
92 rgba[GCOMP] = src[1];
93 rgba[BCOMP] = src[2];
94 rgba[ACOMP] = CHAN_MAX;
95 }
96
97 static void FETCH(alpha)( const struct gl_texture_image *texImage,
98 GLint i, GLint j, GLint k, GLvoid *texel )
99 {
100 const GLchan *src = CHAN_SRC( texImage, i, j, k, 1 );
101 GLchan *rgba = (GLchan *) texel;
102 rgba[RCOMP] = 0;
103 rgba[GCOMP] = 0;
104 rgba[BCOMP] = 0;
105 rgba[ACOMP] = src[0];
106 }
107
108 static void FETCH(luminance)( const struct gl_texture_image *texImage,
109 GLint i, GLint j, GLint k, GLvoid *texel )
110 {
111 const GLchan *src = CHAN_SRC( texImage, i, j, k, 1 );
112 GLchan *rgba = (GLchan *) texel;
113 rgba[RCOMP] = src[0];
114 rgba[GCOMP] = src[0];
115 rgba[BCOMP] = src[0];
116 rgba[ACOMP] = CHAN_MAX;
117 }
118
119 static void FETCH(luminance_alpha)( const struct gl_texture_image *texImage,
120 GLint i, GLint j, GLint k, GLvoid *texel )
121 {
122 const GLchan *src = CHAN_SRC( texImage, i, j, k, 2 );
123 GLchan *rgba = (GLchan *) texel;
124 rgba[RCOMP] = src[0];
125 rgba[GCOMP] = src[0];
126 rgba[BCOMP] = src[0];
127 rgba[ACOMP] = src[1];
128 }
129
130 static void FETCH(intensity)( const struct gl_texture_image *texImage,
131 GLint i, GLint j, GLint k, GLvoid *texel )
132 {
133 const GLchan *src = CHAN_SRC( texImage, i, j, k, 1 );
134 GLchan *rgba = (GLchan *) texel;
135 rgba[RCOMP] = src[0];
136 rgba[GCOMP] = src[0];
137 rgba[BCOMP] = src[0];
138 rgba[ACOMP] = src[0];
139 }
140
141 static void FETCH(color_index)( const struct gl_texture_image *texImage,
142 GLint i, GLint j, GLint k, GLvoid *texel )
143 {
144 const GLchan *src = CHAN_SRC( texImage, i, j, k, 1 );
145 GLchan *index = (GLchan *) texel;
146 *index = *src;
147 }
148
149 static void FETCH(depth_component)( const struct gl_texture_image *texImage,
150 GLint i, GLint j, GLint k, GLvoid *texel )
151 {
152 const GLfloat *src = FLOAT_SRC( texImage, i, j, k );
153 GLfloat *depth = (GLfloat *) texel;
154 *depth = *src;
155 }
156
157 static void FETCH(rgba8888)( const struct gl_texture_image *texImage,
158 GLint i, GLint j, GLint k, GLvoid *texel )
159 {
160 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 4 );
161 GLchan *rgba = (GLchan *) texel;
162 rgba[RCOMP] = UBYTE_TO_CHAN( src[3] );
163 rgba[GCOMP] = UBYTE_TO_CHAN( src[2] );
164 rgba[BCOMP] = UBYTE_TO_CHAN( src[1] );
165 rgba[ACOMP] = UBYTE_TO_CHAN( src[0] );
166 }
167
168 static void FETCH(argb8888)( const struct gl_texture_image *texImage,
169 GLint i, GLint j, GLint k, GLvoid *texel )
170 {
171 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 4 );
172 GLchan *rgba = (GLchan *) texel;
173 rgba[RCOMP] = UBYTE_TO_CHAN( src[2] );
174 rgba[GCOMP] = UBYTE_TO_CHAN( src[1] );
175 rgba[BCOMP] = UBYTE_TO_CHAN( src[0] );
176 rgba[ACOMP] = UBYTE_TO_CHAN( src[3] );
177 }
178
179 static void FETCH(rgb888)( const struct gl_texture_image *texImage,
180 GLint i, GLint j, GLint k, GLvoid *texel )
181 {
182 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 3 );
183 GLchan *rgba = (GLchan *) texel;
184 rgba[RCOMP] = UBYTE_TO_CHAN( src[2] );
185 rgba[GCOMP] = UBYTE_TO_CHAN( src[1] );
186 rgba[BCOMP] = UBYTE_TO_CHAN( src[0] );
187 rgba[ACOMP] = CHAN_MAX;
188 }
189
190 static void FETCH(rgb565)( const struct gl_texture_image *texImage,
191 GLint i, GLint j, GLint k, GLvoid *texel )
192 {
193 const GLushort *src = USHORT_SRC( texImage, i, j, k );
194 const GLushort s = *src;
195 GLchan *rgba = (GLchan *) texel;
196 rgba[RCOMP] = UBYTE_TO_CHAN( ((s >> 8) & 0xf8) * 255 / 0xf8 );
197 rgba[GCOMP] = UBYTE_TO_CHAN( ((s >> 3) & 0xfc) * 255 / 0xfc );
198 rgba[BCOMP] = UBYTE_TO_CHAN( ((s << 3) & 0xf8) * 255 / 0xf8 );
199 rgba[ACOMP] = CHAN_MAX;
200 }
201
202 static void FETCH(argb4444)( const struct gl_texture_image *texImage,
203 GLint i, GLint j, GLint k, GLvoid *texel )
204 {
205 const GLushort *src = USHORT_SRC( texImage, i, j, k );
206 const GLushort s = *src;
207 GLchan *rgba = (GLchan *) texel;
208 rgba[RCOMP] = UBYTE_TO_CHAN( ((s >> 8) & 0xf) * 255 / 0xf );
209 rgba[GCOMP] = UBYTE_TO_CHAN( ((s >> 4) & 0xf) * 255 / 0xf );
210 rgba[BCOMP] = UBYTE_TO_CHAN( ((s ) & 0xf) * 255 / 0xf );
211 rgba[ACOMP] = UBYTE_TO_CHAN( ((s >> 12) & 0xf) * 255 / 0xf );
212 }
213
214 static void FETCH(argb1555)( const struct gl_texture_image *texImage,
215 GLint i, GLint j, GLint k, GLvoid *texel )
216 {
217 const GLushort *src = USHORT_SRC( texImage, i, j, k );
218 const GLushort s = *src;
219 GLchan *rgba = (GLchan *) texel;
220 rgba[RCOMP] = UBYTE_TO_CHAN( ((s >> 10) & 0x1f) * 255 / 0x1f );
221 rgba[GCOMP] = UBYTE_TO_CHAN( ((s >> 5) & 0x1f) * 255 / 0x1f );
222 rgba[BCOMP] = UBYTE_TO_CHAN( ((s ) & 0x1f) * 255 / 0x1f );
223 rgba[ACOMP] = UBYTE_TO_CHAN( ((s >> 15) & 0x01) * 255 );
224 }
225
226 static void FETCH(al88)( const struct gl_texture_image *texImage,
227 GLint i, GLint j, GLint k, GLvoid *texel )
228 {
229 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 2 );
230 GLchan *rgba = (GLchan *) texel;
231 rgba[RCOMP] = UBYTE_TO_CHAN( src[0] );
232 rgba[GCOMP] = UBYTE_TO_CHAN( src[0] );
233 rgba[BCOMP] = UBYTE_TO_CHAN( src[0] );
234 rgba[ACOMP] = UBYTE_TO_CHAN( src[1] );
235 }
236
237 static void FETCH(rgb332)( const struct gl_texture_image *texImage,
238 GLint i, GLint j, GLint k, GLvoid *texel )
239 {
240 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 1 );
241 const GLubyte s = *src;
242 GLchan *rgba = (GLchan *) texel;
243 rgba[RCOMP] = UBYTE_TO_CHAN( ((s ) & 0xe0) * 255 / 0xe0 );
244 rgba[GCOMP] = UBYTE_TO_CHAN( ((s << 3) & 0xe0) * 255 / 0xe0 );
245 rgba[BCOMP] = UBYTE_TO_CHAN( ((s << 5) & 0xc0) * 255 / 0xc0 );
246 rgba[ACOMP] = CHAN_MAX;
247 }
248
249 static void FETCH(a8)( const struct gl_texture_image *texImage,
250 GLint i, GLint j, GLint k, GLvoid *texel )
251 {
252 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 1 );
253 GLchan *rgba = (GLchan *) texel;
254 rgba[RCOMP] = 0;
255 rgba[GCOMP] = 0;
256 rgba[BCOMP] = 0;
257 rgba[ACOMP] = UBYTE_TO_CHAN( src[0] );
258 }
259
260 static void FETCH(l8)( const struct gl_texture_image *texImage,
261 GLint i, GLint j, GLint k, GLvoid *texel )
262 {
263 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 1 );
264 GLchan *rgba = (GLchan *) texel;
265 rgba[RCOMP] = UBYTE_TO_CHAN( src[0] );
266 rgba[GCOMP] = UBYTE_TO_CHAN( src[0] );
267 rgba[BCOMP] = UBYTE_TO_CHAN( src[0] );
268 rgba[ACOMP] = CHAN_MAX;
269 }
270
271 static void FETCH(i8)( const struct gl_texture_image *texImage,
272 GLint i, GLint j, GLint k, GLvoid *texel )
273 {
274 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 1 );
275 GLchan *rgba = (GLchan *) texel;
276 rgba[RCOMP] = UBYTE_TO_CHAN( src[0] );
277 rgba[GCOMP] = UBYTE_TO_CHAN( src[0] );
278 rgba[BCOMP] = UBYTE_TO_CHAN( src[0] );
279 rgba[ACOMP] = UBYTE_TO_CHAN( src[0] );
280 }
281
282 static void FETCH(ci8)( const struct gl_texture_image *texImage,
283 GLint i, GLint j, GLint k, GLvoid *texel )
284 {
285 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 1 );
286 GLchan *index = (GLchan *) texel;
287 *index = UBYTE_TO_CHAN( *src );
288 }
289
290 /* XXX this may break if GLchan != GLubyte */
291 static void FETCH(ycbcr)( const struct gl_texture_image *texImage,
292 GLint i, GLint j, GLint k, GLvoid *texel )
293 {
294 const GLushort *src0 = USHORT_SRC( texImage, (i & ~1), j, k ); /* even */
295 const GLushort *src1 = src0 + 1; /* odd */
296 const GLubyte y0 = (*src0 >> 8) & 0xff; /* luminance */
297 const GLubyte cb = *src0 & 0xff; /* chroma U */
298 const GLubyte y1 = (*src1 >> 8) & 0xff; /* luminance */
299 const GLubyte cr = *src1 & 0xff; /* chroma V */
300 GLchan *rgba = (GLchan *) texel;
301 GLint r, g, b;
302 if (i & 1) {
303 /* odd pixel: use y1,cr,cb */
304 r = (GLint) (1.164 * (y1-16) + 1.596 * (cr-128));
305 g = (GLint) (1.164 * (y1-16) - 0.813 * (cr-128) - 0.391 * (cb-128));
306 b = (GLint) (1.164 * (y1-16) + 2.018 * (cb-128));
307 }
308 else {
309 /* even pixel: use y0,cr,cb */
310 r = (GLint) (1.164 * (y0-16) + 1.596 * (cr-128));
311 g = (GLint) (1.164 * (y0-16) - 0.813 * (cr-128) - 0.391 * (cb-128));
312 b = (GLint) (1.164 * (y0-16) + 2.018 * (cb-128));
313 }
314 rgba[RCOMP] = CLAMP(r, 0, CHAN_MAX);
315 rgba[GCOMP] = CLAMP(g, 0, CHAN_MAX);
316 rgba[BCOMP] = CLAMP(b, 0, CHAN_MAX);
317 rgba[ACOMP] = CHAN_MAX;
318 }
319
320 /* XXX this may break if GLchan != GLubyte */
321 static void FETCH(ycbcr_rev)( const struct gl_texture_image *texImage,
322 GLint i, GLint j, GLint k, GLvoid *texel )
323 {
324 const GLushort *src0 = USHORT_SRC( texImage, (i & ~1), j, k ); /* even */
325 const GLushort *src1 = src0 + 1; /* odd */
326 const GLubyte y0 = *src0 & 0xff; /* luminance */
327 const GLubyte cr = (*src0 >> 8) & 0xff; /* chroma U */
328 const GLubyte y1 = *src1 & 0xff; /* luminance */
329 const GLubyte cb = (*src1 >> 8) & 0xff; /* chroma V */
330 GLchan *rgba = (GLchan *) texel;
331 GLint r, g, b;
332 if (i & 1) {
333 /* odd pixel: use y1,cr,cb */
334 r = (GLint) (1.164 * (y1-16) + 1.596 * (cr-128));
335 g = (GLint) (1.164 * (y1-16) - 0.813 * (cr-128) - 0.391 * (cb-128));
336 b = (GLint) (1.164 * (y1-16) + 2.018 * (cb-128));
337 }
338 else {
339 /* even pixel: use y0,cr,cb */
340 r = (GLint) (1.164 * (y0-16) + 1.596 * (cr-128));
341 g = (GLint) (1.164 * (y0-16) - 0.813 * (cr-128) - 0.391 * (cb-128));
342 b = (GLint) (1.164 * (y0-16) + 2.018 * (cb-128));
343 }
344 rgba[RCOMP] = CLAMP(r, 0, CHAN_MAX);
345 rgba[GCOMP] = CLAMP(g, 0, CHAN_MAX);
346 rgba[BCOMP] = CLAMP(b, 0, CHAN_MAX);
347 rgba[ACOMP] = CHAN_MAX;
348 }
349
350
351
352 /* big-endian */
353
354 #if 0
355 static void FETCH(abgr8888)( const struct gl_texture_image *texImage,
356 GLint i, GLint j, GLint k, GLvoid *texel )
357 {
358 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 4 );
359 GLchan *rgba = (GLchan *) texel;
360 rgba[RCOMP] = UBYTE_TO_CHAN( src[3] );
361 rgba[GCOMP] = UBYTE_TO_CHAN( src[2] );
362 rgba[BCOMP] = UBYTE_TO_CHAN( src[1] );
363 rgba[ACOMP] = UBYTE_TO_CHAN( src[0] );
364 }
365
366 static void FETCH(bgra8888)( const struct gl_texture_image *texImage,
367 GLint i, GLint j, GLint k, GLvoid *texel )
368 {
369 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 4 );
370 GLchan *rgba = (GLchan *) texel;
371 rgba[RCOMP] = UBYTE_TO_CHAN( src[2] );
372 rgba[GCOMP] = UBYTE_TO_CHAN( src[1] );
373 rgba[BCOMP] = UBYTE_TO_CHAN( src[0] );
374 rgba[ACOMP] = UBYTE_TO_CHAN( src[3] );
375 }
376
377 static void FETCH(bgr888)( const struct gl_texture_image *texImage,
378 GLint i, GLint j, GLint k, GLvoid *texel )
379 {
380 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 3 );
381 GLchan *rgba = (GLchan *) texel;
382 rgba[RCOMP] = UBYTE_TO_CHAN( src[2] );
383 rgba[GCOMP] = UBYTE_TO_CHAN( src[1] );
384 rgba[BCOMP] = UBYTE_TO_CHAN( src[0] );
385 rgba[ACOMP] = CHAN_MAX;
386 }
387
388 static void FETCH(bgr565)( const struct gl_texture_image *texImage,
389 GLint i, GLint j, GLint k, GLvoid *texel )
390 {
391 const GLushort *src = USHORT_SRC( texImage, i, j, k );
392 const GLushort s = *src;
393 GLchan *rgba = (GLchan *) texel;
394 rgba[RCOMP] = UBYTE_TO_CHAN( ((s >> 8) & 0xf8) * 255 / 0xf8 );
395 rgba[GCOMP] = UBYTE_TO_CHAN( ((s >> 3) & 0xfc) * 255 / 0xfc );
396 rgba[BCOMP] = UBYTE_TO_CHAN( ((s << 3) & 0xf8) * 255 / 0xf8 );
397 rgba[ACOMP] = CHAN_MAX;
398 }
399
400 static void FETCH(bgra4444)( const struct gl_texture_image *texImage,
401 GLint i, GLint j, GLint k, GLvoid *texel )
402 {
403 const GLushort *src = USHORT_SRC( texImage, i, j, k );
404 const GLushort s = *src;
405 GLchan *rgba = (GLchan *) texel;
406 rgba[RCOMP] = UBYTE_TO_CHAN( ((s >> 8) & 0xf) * 255 / 0xf );
407 rgba[GCOMP] = UBYTE_TO_CHAN( ((s >> 4) & 0xf) * 255 / 0xf );
408 rgba[BCOMP] = UBYTE_TO_CHAN( ((s ) & 0xf) * 255 / 0xf );
409 rgba[ACOMP] = UBYTE_TO_CHAN( ((s >> 12) & 0xf) * 255 / 0xf );
410 }
411
412 static void FETCH(bgra5551)( const struct gl_texture_image *texImage,
413 GLint i, GLint j, GLint k, GLvoid *texel )
414 {
415 const GLushort *src = USHORT_SRC( texImage, i, j, k );
416 const GLushort s = *src;
417 GLchan *rgba = (GLchan *) texel;
418 rgba[RCOMP] = UBYTE_TO_CHAN( ((s >> 10) & 0x1f) * 255 / 0x1f );
419 rgba[GCOMP] = UBYTE_TO_CHAN( ((s >> 5) & 0x1f) * 255 / 0x1f );
420 rgba[BCOMP] = UBYTE_TO_CHAN( ((s ) & 0x1f) * 255 / 0x1f );
421 rgba[ACOMP] = UBYTE_TO_CHAN( ((s >> 15) & 0x01) * 255 );
422 }
423
424 static void FETCH(la88)( const struct gl_texture_image *texImage,
425 GLint i, GLint j, GLint k, GLvoid *texel )
426 {
427 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 2 );
428 GLchan *rgba = (GLchan *) texel;
429 rgba[RCOMP] = UBYTE_TO_CHAN( src[0] );
430 rgba[GCOMP] = UBYTE_TO_CHAN( src[0] );
431 rgba[BCOMP] = UBYTE_TO_CHAN( src[0] );
432 rgba[ACOMP] = UBYTE_TO_CHAN( src[1] );
433 }
434
435 static void FETCH(bgr233)( const struct gl_texture_image *texImage,
436 GLint i, GLint j, GLint k, GLvoid *texel )
437 {
438 const GLubyte *src = UBYTE_SRC( texImage, i, j, k, 1 );
439 const GLubyte s = *src;
440 GLchan *rgba = (GLchan *) texel;
441 rgba[RCOMP] = UBYTE_TO_CHAN( ((s ) & 0xe0) * 255 / 0xe0 );
442 rgba[GCOMP] = UBYTE_TO_CHAN( ((s << 3) & 0xe0) * 255 / 0xe0 );
443 rgba[BCOMP] = UBYTE_TO_CHAN( ((s << 5) & 0xc0) * 255 / 0xc0 );
444 rgba[ACOMP] = CHAN_MAX;
445 }
446 #endif
447
448
449 #undef CHAN_SRC
450 #undef UBYTE_SRC
451 #undef USHORT_SRC
452 #undef FLOAT_SRC
453 #undef FETCH
454 #undef DIM