mesa/pack: refactor _mesa_pack_rgba_span_float()
[mesa.git] / src / mesa / main / pack.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THEA AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file pack.c
28 * Image and pixel span packing and unpacking.
29 */
30
31
32 /*
33 * XXX: MSVC takes forever to compile this module for x86_64 unless we disable
34 * this global optimization.
35 *
36 * See also:
37 * - http://msdn.microsoft.com/en-us/library/1yk3ydd7.aspx
38 * - http://msdn.microsoft.com/en-us/library/chh3fb0k.aspx
39 */
40 #if defined(_MSC_VER) && defined(_M_X64)
41 # pragma optimize( "g", off )
42 #endif
43
44
45 #include "glheader.h"
46 #include "colormac.h"
47 #include "enums.h"
48 #include "image.h"
49 #include "imports.h"
50 #include "macros.h"
51 #include "mtypes.h"
52 #include "pack.h"
53 #include "pixeltransfer.h"
54 #include "imports.h"
55 #include "glformats.h"
56 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
57 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
58 #include "format_utils.h"
59 #include "format_pack.h"
60
61
62 /**
63 * Flip the 8 bits in each byte of the given array.
64 *
65 * \param p array.
66 * \param n number of bytes.
67 *
68 * \todo try this trick to flip bytes someday:
69 * \code
70 * v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
71 * v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
72 * v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
73 * \endcode
74 */
75 static void
76 flip_bytes( GLubyte *p, GLuint n )
77 {
78 GLuint i, a, b;
79 for (i = 0; i < n; i++) {
80 b = (GLuint) p[i]; /* words are often faster than bytes */
81 a = ((b & 0x01) << 7) |
82 ((b & 0x02) << 5) |
83 ((b & 0x04) << 3) |
84 ((b & 0x08) << 1) |
85 ((b & 0x10) >> 1) |
86 ((b & 0x20) >> 3) |
87 ((b & 0x40) >> 5) |
88 ((b & 0x80) >> 7);
89 p[i] = (GLubyte) a;
90 }
91 }
92
93
94
95 /*
96 * Unpack a 32x32 pixel polygon stipple from user memory using the
97 * current pixel unpack settings.
98 */
99 void
100 _mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
101 const struct gl_pixelstore_attrib *unpacking )
102 {
103 GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap(32, 32, pattern, unpacking);
104 if (ptrn) {
105 /* Convert pattern from GLubytes to GLuints and handle big/little
106 * endian differences
107 */
108 GLubyte *p = ptrn;
109 GLint i;
110 for (i = 0; i < 32; i++) {
111 dest[i] = (p[0] << 24)
112 | (p[1] << 16)
113 | (p[2] << 8)
114 | (p[3] );
115 p += 4;
116 }
117 free(ptrn);
118 }
119 }
120
121
122 /*
123 * Pack polygon stipple into user memory given current pixel packing
124 * settings.
125 */
126 void
127 _mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
128 const struct gl_pixelstore_attrib *packing )
129 {
130 /* Convert pattern from GLuints to GLubytes to handle big/little
131 * endian differences.
132 */
133 GLubyte ptrn[32*4];
134 GLint i;
135 for (i = 0; i < 32; i++) {
136 ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
137 ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
138 ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
139 ptrn[i * 4 + 3] = (GLubyte) ((pattern[i] ) & 0xff);
140 }
141
142 _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
143 }
144
145
146 /*
147 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
148 * order with row alignment = 1 byte.
149 */
150 GLvoid *
151 _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
152 const struct gl_pixelstore_attrib *packing )
153 {
154 GLint bytes, row, width_in_bytes;
155 GLubyte *buffer, *dst;
156
157 if (!pixels)
158 return NULL;
159
160 /* Alloc dest storage */
161 bytes = ((width + 7) / 8 * height);
162 buffer = malloc( bytes );
163 if (!buffer)
164 return NULL;
165
166 width_in_bytes = CEILING( width, 8 );
167 dst = buffer;
168 for (row = 0; row < height; row++) {
169 const GLubyte *src = (const GLubyte *)
170 _mesa_image_address2d(packing, pixels, width, height,
171 GL_COLOR_INDEX, GL_BITMAP, row, 0);
172 if (!src) {
173 free(buffer);
174 return NULL;
175 }
176
177 if ((packing->SkipPixels & 7) == 0) {
178 memcpy( dst, src, width_in_bytes );
179 if (packing->LsbFirst) {
180 flip_bytes( dst, width_in_bytes );
181 }
182 }
183 else {
184 /* handling SkipPixels is a bit tricky (no pun intended!) */
185 GLint i;
186 if (packing->LsbFirst) {
187 GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
188 GLubyte dstMask = 128;
189 const GLubyte *s = src;
190 GLubyte *d = dst;
191 *d = 0;
192 for (i = 0; i < width; i++) {
193 if (*s & srcMask) {
194 *d |= dstMask;
195 }
196 if (srcMask == 128) {
197 srcMask = 1;
198 s++;
199 }
200 else {
201 srcMask = srcMask << 1;
202 }
203 if (dstMask == 1) {
204 dstMask = 128;
205 d++;
206 *d = 0;
207 }
208 else {
209 dstMask = dstMask >> 1;
210 }
211 }
212 }
213 else {
214 GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
215 GLubyte dstMask = 128;
216 const GLubyte *s = src;
217 GLubyte *d = dst;
218 *d = 0;
219 for (i = 0; i < width; i++) {
220 if (*s & srcMask) {
221 *d |= dstMask;
222 }
223 if (srcMask == 1) {
224 srcMask = 128;
225 s++;
226 }
227 else {
228 srcMask = srcMask >> 1;
229 }
230 if (dstMask == 1) {
231 dstMask = 128;
232 d++;
233 *d = 0;
234 }
235 else {
236 dstMask = dstMask >> 1;
237 }
238 }
239 }
240 }
241 dst += width_in_bytes;
242 }
243
244 return buffer;
245 }
246
247
248 /*
249 * Pack bitmap data.
250 */
251 void
252 _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
253 GLubyte *dest, const struct gl_pixelstore_attrib *packing )
254 {
255 GLint row, width_in_bytes;
256 const GLubyte *src;
257
258 if (!source)
259 return;
260
261 width_in_bytes = CEILING( width, 8 );
262 src = source;
263 for (row = 0; row < height; row++) {
264 GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
265 width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
266 if (!dst)
267 return;
268
269 if ((packing->SkipPixels & 7) == 0) {
270 memcpy( dst, src, width_in_bytes );
271 if (packing->LsbFirst) {
272 flip_bytes( dst, width_in_bytes );
273 }
274 }
275 else {
276 /* handling SkipPixels is a bit tricky (no pun intended!) */
277 GLint i;
278 if (packing->LsbFirst) {
279 GLubyte srcMask = 128;
280 GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
281 const GLubyte *s = src;
282 GLubyte *d = dst;
283 *d = 0;
284 for (i = 0; i < width; i++) {
285 if (*s & srcMask) {
286 *d |= dstMask;
287 }
288 if (srcMask == 1) {
289 srcMask = 128;
290 s++;
291 }
292 else {
293 srcMask = srcMask >> 1;
294 }
295 if (dstMask == 128) {
296 dstMask = 1;
297 d++;
298 *d = 0;
299 }
300 else {
301 dstMask = dstMask << 1;
302 }
303 }
304 }
305 else {
306 GLubyte srcMask = 128;
307 GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
308 const GLubyte *s = src;
309 GLubyte *d = dst;
310 *d = 0;
311 for (i = 0; i < width; i++) {
312 if (*s & srcMask) {
313 *d |= dstMask;
314 }
315 if (srcMask == 1) {
316 srcMask = 128;
317 s++;
318 }
319 else {
320 srcMask = srcMask >> 1;
321 }
322 if (dstMask == 1) {
323 dstMask = 128;
324 d++;
325 *d = 0;
326 }
327 else {
328 dstMask = dstMask >> 1;
329 }
330 }
331 }
332 }
333 src += width_in_bytes;
334 }
335 }
336
337
338 /**
339 * Get indexes of color components for a basic color format, such as
340 * GL_RGBA, GL_RED, GL_LUMINANCE_ALPHA, etc. Return -1 for indexes
341 * that do not apply.
342 */
343 static void
344 get_component_indexes(GLenum format,
345 GLint *redIndex,
346 GLint *greenIndex,
347 GLint *blueIndex,
348 GLint *alphaIndex,
349 GLint *luminanceIndex,
350 GLint *intensityIndex)
351 {
352 *redIndex = -1;
353 *greenIndex = -1;
354 *blueIndex = -1;
355 *alphaIndex = -1;
356 *luminanceIndex = -1;
357 *intensityIndex = -1;
358
359 switch (format) {
360 case GL_LUMINANCE:
361 case GL_LUMINANCE_INTEGER_EXT:
362 *luminanceIndex = 0;
363 break;
364 case GL_LUMINANCE_ALPHA:
365 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
366 *luminanceIndex = 0;
367 *alphaIndex = 1;
368 break;
369 case GL_INTENSITY:
370 *intensityIndex = 0;
371 break;
372 case GL_RED:
373 case GL_RED_INTEGER_EXT:
374 *redIndex = 0;
375 break;
376 case GL_GREEN:
377 case GL_GREEN_INTEGER_EXT:
378 *greenIndex = 0;
379 break;
380 case GL_BLUE:
381 case GL_BLUE_INTEGER_EXT:
382 *blueIndex = 0;
383 break;
384 case GL_ALPHA:
385 case GL_ALPHA_INTEGER_EXT:
386 *alphaIndex = 0;
387 break;
388 case GL_RG:
389 case GL_RG_INTEGER:
390 *redIndex = 0;
391 *greenIndex = 1;
392 break;
393 case GL_RGB:
394 case GL_RGB_INTEGER_EXT:
395 *redIndex = 0;
396 *greenIndex = 1;
397 *blueIndex = 2;
398 break;
399 case GL_BGR:
400 case GL_BGR_INTEGER_EXT:
401 *blueIndex = 0;
402 *greenIndex = 1;
403 *redIndex = 2;
404 break;
405 case GL_RGBA:
406 case GL_RGBA_INTEGER_EXT:
407 *redIndex = 0;
408 *greenIndex = 1;
409 *blueIndex = 2;
410 *alphaIndex = 3;
411 break;
412 case GL_BGRA:
413 case GL_BGRA_INTEGER:
414 *redIndex = 2;
415 *greenIndex = 1;
416 *blueIndex = 0;
417 *alphaIndex = 3;
418 break;
419 case GL_ABGR_EXT:
420 *redIndex = 3;
421 *greenIndex = 2;
422 *blueIndex = 1;
423 *alphaIndex = 0;
424 break;
425 default:
426 assert(0 && "bad format in get_component_indexes()");
427 }
428 }
429
430
431
432 /**
433 * For small integer types, return the min and max possible values.
434 * Used for clamping floats to unscaled integer types.
435 * \return GL_TRUE if type is handled, GL_FALSE otherwise.
436 */
437 static GLboolean
438 get_type_min_max(GLenum type, GLfloat *min, GLfloat *max)
439 {
440 switch (type) {
441 case GL_BYTE:
442 *min = -128.0;
443 *max = 127.0;
444 return GL_TRUE;
445 case GL_UNSIGNED_BYTE:
446 *min = 0.0;
447 *max = 255.0;
448 return GL_TRUE;
449 case GL_SHORT:
450 *min = -32768.0;
451 *max = 32767.0;
452 return GL_TRUE;
453 case GL_UNSIGNED_SHORT:
454 *min = 0.0;
455 *max = 65535.0;
456 return GL_TRUE;
457 default:
458 return GL_FALSE;
459 }
460 }
461
462 /* Customization of unsigned integer packing.
463 */
464 #define SRC_TYPE GLuint
465
466 #define DST_TYPE GLuint
467 #define SRC_CONVERT(x) (x)
468 #define FN_NAME pack_uint_from_uint_rgba
469 #include "pack_tmp.h"
470 #undef DST_TYPE
471 #undef SRC_CONVERT
472 #undef FN_NAME
473
474 #define DST_TYPE GLint
475 #define SRC_CONVERT(x) MIN2(x, 0x7fffffff)
476 #define FN_NAME pack_int_from_uint_rgba
477 #include "pack_tmp.h"
478 #undef DST_TYPE
479 #undef SRC_CONVERT
480 #undef FN_NAME
481
482 #define DST_TYPE GLushort
483 #define SRC_CONVERT(x) MIN2(x, 0xffff)
484 #define FN_NAME pack_ushort_from_uint_rgba
485 #include "pack_tmp.h"
486 #undef DST_TYPE
487 #undef SRC_CONVERT
488 #undef FN_NAME
489
490 #define DST_TYPE GLshort
491 #define SRC_CONVERT(x) CLAMP((int)x, -32768, 32767)
492 #define FN_NAME pack_short_from_uint_rgba
493 #include "pack_tmp.h"
494 #undef DST_TYPE
495 #undef SRC_CONVERT
496 #undef FN_NAME
497
498 #define DST_TYPE GLubyte
499 #define SRC_CONVERT(x) MIN2(x, 0xff)
500 #define FN_NAME pack_ubyte_from_uint_rgba
501 #include "pack_tmp.h"
502 #undef DST_TYPE
503 #undef SRC_CONVERT
504 #undef FN_NAME
505
506 #define DST_TYPE GLbyte
507 #define SRC_CONVERT(x) CLAMP((int)x, -128, 127)
508 #define FN_NAME pack_byte_from_uint_rgba
509 #include "pack_tmp.h"
510 #undef DST_TYPE
511 #undef SRC_CONVERT
512 #undef FN_NAME
513
514 #undef SRC_TYPE
515
516 static void
517 _pack_rgba_span_from_uints_problem(struct gl_context *ctx,
518 GLenum dstFormat, GLenum dstType)
519 {
520 _mesa_problem(ctx,
521 "Unsupported type (%s) / format (%s) "
522 "in _mesa_pack_rgba_span_from_uints",
523 _mesa_lookup_enum_by_nr(dstType),
524 _mesa_lookup_enum_by_nr(dstFormat));
525 }
526
527 void
528 _mesa_pack_rgba_span_from_uints(struct gl_context *ctx, GLuint n, GLuint rgba[][4],
529 GLenum dstFormat, GLenum dstType,
530 GLvoid *dstAddr)
531 {
532 switch(dstType) {
533 case GL_UNSIGNED_INT:
534 pack_uint_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
535 break;
536 case GL_INT:
537 pack_int_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
538 break;
539 case GL_UNSIGNED_SHORT:
540 pack_ushort_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
541 break;
542 case GL_SHORT:
543 pack_short_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
544 break;
545 case GL_UNSIGNED_BYTE:
546 pack_ubyte_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
547 break;
548 case GL_BYTE:
549 pack_byte_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
550 break;
551 case GL_UNSIGNED_BYTE_3_3_2:
552 if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER))
553 _mesa_pack_uint_rgba_row(MESA_FORMAT_B2G3R3_UNORM, n, (void *)rgba[0], (void *)dstAddr);
554 else
555 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
556 break;
557 case GL_UNSIGNED_BYTE_2_3_3_REV:
558 if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER))
559 _mesa_pack_uint_rgba_row(MESA_FORMAT_R3G3B2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
560 else
561 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
562 break;
563 case GL_UNSIGNED_SHORT_5_6_5:
564 if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER))
565 _mesa_pack_uint_rgba_row(MESA_FORMAT_B5G6R5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
566 else
567 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
568 break;
569 case GL_UNSIGNED_SHORT_5_6_5_REV:
570 if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER))
571 _mesa_pack_uint_rgba_row(MESA_FORMAT_R5G6B5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
572 else
573 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
574 break;
575 case GL_UNSIGNED_SHORT_4_4_4_4:
576 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
577 _mesa_pack_uint_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
578 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
579 _mesa_pack_uint_rgba_row(MESA_FORMAT_A4R4G4B4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
580 else if (dstFormat == GL_ABGR_EXT)
581 _mesa_pack_uint_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
582 else
583 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
584 break;
585 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
586 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
587 _mesa_pack_uint_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
588 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
589 _mesa_pack_uint_rgba_row(MESA_FORMAT_B4G4R4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
590 else if (dstFormat == GL_ABGR_EXT)
591 _mesa_pack_uint_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
592 else
593 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
594 break;
595 case GL_UNSIGNED_SHORT_5_5_5_1:
596 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
597 _mesa_pack_uint_rgba_row(MESA_FORMAT_A1B5G5R5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
598 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
599 _mesa_pack_uint_rgba_row(MESA_FORMAT_A1R5G5B5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
600 else if (dstFormat == GL_ABGR_EXT)
601 _mesa_pack_uint_rgba_row(MESA_FORMAT_R1G5B5A5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
602 else
603 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
604 break;
605 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
606 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
607 _mesa_pack_uint_rgba_row(MESA_FORMAT_R5G5B5A1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
608 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
609 _mesa_pack_uint_rgba_row(MESA_FORMAT_B5G5R5A1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
610 else if (dstFormat == GL_ABGR_EXT)
611 _mesa_pack_uint_rgba_row(MESA_FORMAT_A5B5G5R1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
612 else
613 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
614 break;
615 case GL_UNSIGNED_INT_8_8_8_8:
616 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
617 _mesa_pack_uint_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
618 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
619 _mesa_pack_uint_rgba_row(MESA_FORMAT_A8R8G8B8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
620 else if (dstFormat == GL_ABGR_EXT)
621 _mesa_pack_uint_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
622 else
623 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
624 break;
625 case GL_UNSIGNED_INT_8_8_8_8_REV:
626 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
627 _mesa_pack_uint_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
628 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
629 _mesa_pack_uint_rgba_row(MESA_FORMAT_B8G8R8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
630 else if (dstFormat == GL_ABGR_EXT)
631 _mesa_pack_uint_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
632 else
633 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
634 break;
635 case GL_UNSIGNED_INT_10_10_10_2:
636 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
637 _mesa_pack_uint_rgba_row(MESA_FORMAT_A2B10G10R10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
638 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
639 _mesa_pack_uint_rgba_row(MESA_FORMAT_A2R10G10B10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
640 else if (dstFormat == GL_ABGR_EXT)
641 _mesa_pack_uint_rgba_row(MESA_FORMAT_R2G10B10A10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
642 else
643 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
644 break;
645 case GL_UNSIGNED_INT_2_10_10_10_REV:
646 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
647 _mesa_pack_uint_rgba_row(MESA_FORMAT_R10G10B10A2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
648 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
649 _mesa_pack_uint_rgba_row(MESA_FORMAT_B10G10R10A2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
650 else if (dstFormat == GL_ABGR_EXT)
651 _mesa_pack_uint_rgba_row(MESA_FORMAT_A10B10G10R2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
652 else
653 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
654 break;
655 default:
656 _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
657 return;
658 }
659 }
660
661 /* Customization of signed integer packing.
662 */
663 #define SRC_TYPE GLint
664
665 #define DST_TYPE GLuint
666 #define SRC_CONVERT(x) MAX2(x, 0)
667 #define FN_NAME pack_uint_from_int_rgba
668 #include "pack_tmp.h"
669 #undef DST_TYPE
670 #undef SRC_CONVERT
671 #undef FN_NAME
672
673 #define DST_TYPE GLushort
674 #define SRC_CONVERT(x) MAX2(x, 0)
675 #define FN_NAME pack_ushort_from_int_rgba
676 #include "pack_tmp.h"
677 #undef DST_TYPE
678 #undef SRC_CONVERT
679 #undef FN_NAME
680
681 #define DST_TYPE GLshort
682 #define SRC_CONVERT(x) CLAMP(x, -0x8000, 0x7fff)
683 #define FN_NAME pack_short_from_int_rgba
684 #include "pack_tmp.h"
685 #undef DST_TYPE
686 #undef SRC_CONVERT
687 #undef FN_NAME
688
689 #define DST_TYPE GLubyte
690 #define SRC_CONVERT(x) MAX2(x, 0)
691 #define FN_NAME pack_ubyte_from_int_rgba
692 #include "pack_tmp.h"
693 #undef DST_TYPE
694 #undef SRC_CONVERT
695 #undef FN_NAME
696
697 #define DST_TYPE GLbyte
698 #define SRC_CONVERT(x) CLAMP(x, -0x80, 0x7f)
699 #define FN_NAME pack_byte_from_int_rgba
700 #include "pack_tmp.h"
701 #undef DST_TYPE
702 #undef SRC_CONVERT
703 #undef FN_NAME
704
705 #undef SRC_TYPE
706
707 static void
708 _pack_rgba_span_from_ints_problem(struct gl_context *ctx,
709 GLenum dstFormat, GLenum dstType)
710 {
711 _mesa_problem(ctx,
712 "Unsupported type (%s) / format (%s) "
713 "in _mesa_pack_rgba_span_from_ints",
714 _mesa_lookup_enum_by_nr(dstType),
715 _mesa_lookup_enum_by_nr(dstFormat));
716 }
717
718 void
719 _mesa_pack_rgba_span_from_ints(struct gl_context *ctx, GLuint n, GLint rgba[][4],
720 GLenum dstFormat, GLenum dstType,
721 GLvoid *dstAddr)
722 {
723 switch(dstType) {
724 case GL_UNSIGNED_INT:
725 pack_uint_from_int_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
726 break;
727 case GL_INT:
728 /* No conversion necessary. */
729 pack_uint_from_uint_rgba(ctx, dstAddr, dstFormat, (GLuint (*)[4]) rgba, NULL, n);
730 break;
731 case GL_UNSIGNED_SHORT:
732 pack_ushort_from_int_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
733 break;
734 case GL_SHORT:
735 pack_short_from_int_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
736 break;
737 case GL_UNSIGNED_BYTE:
738 pack_ubyte_from_int_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
739 break;
740 case GL_BYTE:
741 pack_byte_from_int_rgba(ctx, dstAddr, dstFormat, rgba, NULL, n);
742 break;
743 case GL_UNSIGNED_BYTE_3_3_2:
744 if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER))
745 _mesa_pack_int_rgba_row(MESA_FORMAT_B2G3R3_UNORM, n, (void *)rgba[0], (void *)dstAddr);
746 else
747 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
748 break;
749 case GL_UNSIGNED_BYTE_2_3_3_REV:
750 if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER))
751 _mesa_pack_int_rgba_row(MESA_FORMAT_R3G3B2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
752 else
753 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
754 break;
755 case GL_UNSIGNED_SHORT_5_6_5:
756 if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER))
757 _mesa_pack_int_rgba_row(MESA_FORMAT_B5G6R5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
758 else
759 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
760 break;
761 case GL_UNSIGNED_SHORT_5_6_5_REV:
762 if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER))
763 _mesa_pack_int_rgba_row(MESA_FORMAT_R5G6B5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
764 else
765 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
766 break;
767 case GL_UNSIGNED_SHORT_4_4_4_4:
768 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
769 _mesa_pack_int_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
770 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
771 _mesa_pack_int_rgba_row(MESA_FORMAT_A4R4G4B4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
772 else if (dstFormat == GL_ABGR_EXT)
773 _mesa_pack_int_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
774 else
775 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
776 break;
777 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
778 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
779 _mesa_pack_int_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
780 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
781 _mesa_pack_int_rgba_row(MESA_FORMAT_B4G4R4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
782 else if (dstFormat == GL_ABGR_EXT)
783 _mesa_pack_int_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
784 else
785 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
786 break;
787 case GL_UNSIGNED_SHORT_5_5_5_1:
788 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
789 _mesa_pack_int_rgba_row(MESA_FORMAT_A1B5G5R5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
790 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
791 _mesa_pack_int_rgba_row(MESA_FORMAT_A1R5G5B5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
792 else if (dstFormat == GL_ABGR_EXT)
793 _mesa_pack_int_rgba_row(MESA_FORMAT_R1G5B5A5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
794 else
795 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
796 break;
797 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
798 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
799 _mesa_pack_int_rgba_row(MESA_FORMAT_R5G5B5A1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
800 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
801 _mesa_pack_int_rgba_row(MESA_FORMAT_B5G5R5A1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
802 else if (dstFormat == GL_ABGR_EXT)
803 _mesa_pack_int_rgba_row(MESA_FORMAT_A5B5G5R1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
804 else
805 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
806 break;
807 case GL_UNSIGNED_INT_8_8_8_8:
808 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
809 _mesa_pack_int_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
810 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
811 _mesa_pack_int_rgba_row(MESA_FORMAT_A8R8G8B8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
812 else if (dstFormat == GL_ABGR_EXT)
813 _mesa_pack_int_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
814 else
815 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
816 break;
817 case GL_UNSIGNED_INT_8_8_8_8_REV:
818 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
819 _mesa_pack_int_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
820 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
821 _mesa_pack_int_rgba_row(MESA_FORMAT_B8G8R8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
822 else if (dstFormat == GL_ABGR_EXT)
823 _mesa_pack_int_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
824 else
825 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
826 break;
827 case GL_UNSIGNED_INT_10_10_10_2:
828 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
829 _mesa_pack_int_rgba_row(MESA_FORMAT_A2B10G10R10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
830 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
831 _mesa_pack_int_rgba_row(MESA_FORMAT_A2R10G10B10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
832 else if (dstFormat == GL_ABGR_EXT)
833 _mesa_pack_int_rgba_row(MESA_FORMAT_R2G10B10A10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
834 else
835 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
836 break;
837 case GL_UNSIGNED_INT_2_10_10_10_REV:
838 if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT))
839 _mesa_pack_int_rgba_row(MESA_FORMAT_R10G10B10A2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
840 else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER))
841 _mesa_pack_int_rgba_row(MESA_FORMAT_B10G10R10A2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
842 else if (dstFormat == GL_ABGR_EXT)
843 _mesa_pack_int_rgba_row(MESA_FORMAT_A10B10G10R2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
844 else
845 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
846 break;
847 default:
848 _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
849 return;
850 }
851 }
852
853 /* Customization of float packing.
854 */
855 #define SRC_TYPE GLfloat
856
857 #define DST_TYPE GLuint
858 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UINT(x)
859 #define SRC_CONVERT(x) (GLuint) x
860 #define FN_NAME pack_uint_from_float_rgba
861 #include "pack_tmp.h"
862 #undef DST_TYPE
863 #undef SRC_CONVERT
864 #undef FLOAT_SRC_CONVERT
865 #undef FN_NAME
866
867 #define DST_TYPE GLint
868 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_INT(x)
869 #define SRC_CONVERT(x) (GLint) x
870 #define FN_NAME pack_int_from_float_rgba
871 #include "pack_tmp.h"
872 #undef DST_TYPE
873 #undef SRC_CONVERT
874 #undef FLOAT_SRC_CONVERT
875 #undef FN_NAME
876
877 #define DST_TYPE GLshort
878 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_SHORT_TEX(x)
879 #define SRC_CONVERT(x) (GLshort) x
880 #define FN_NAME pack_short_from_float_rgba
881 #include "pack_tmp.h"
882 #undef DST_TYPE
883 #undef SRC_CONVERT
884 #undef FLOAT_SRC_CONVERT
885 #undef FN_NAME
886
887 #define DST_TYPE GLubyte
888 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UBYTE(x)
889 #define SRC_CONVERT(x) (GLubyte) x
890 #define FN_NAME pack_ubyte_from_float_rgba
891 #include "pack_tmp.h"
892 #undef DST_TYPE
893 #undef SRC_CONVERT
894 #undef FLOAT_SRC_CONVERT
895 #undef FN_NAME
896
897 #define DST_TYPE GLbyte
898 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_BYTE_TEX(x)
899 #define SRC_CONVERT(x) (GLbyte) x
900 #define FN_NAME pack_byte_from_float_rgba
901 #include "pack_tmp.h"
902 #undef DST_TYPE
903 #undef SRC_CONVERT
904 #undef FLOAT_SRC_CONVERT
905 #undef FN_NAME
906
907 #define DST_TYPE GLfloat
908 #define FLOAT_SRC_CONVERT(x) x
909 #define SRC_CONVERT(x) x
910 #define FN_NAME pack_float_from_float_rgba
911 #include "pack_tmp.h"
912 #undef DST_TYPE
913 #undef SRC_CONVERT
914 #undef FLOAT_SRC_CONVERT
915 #undef FN_NAME
916
917 #define DST_TYPE GLhalfARB
918 #define FLOAT_SRC_CONVERT(x) _mesa_float_to_half(x)
919 #define FN_NAME pack_half_float_from_float_rgba
920 #include "pack_tmp.h"
921 #undef DST_TYPE
922 #undef SRC_CONVERT
923 #undef FLOAT_SRC_CONVERT
924 #undef FN_NAME
925
926 #undef SRC_TYPE
927
928 /**
929 * Used to pack an array [][4] of RGBA float colors as specified
930 * by the dstFormat, dstType and dstPacking. Used by glReadPixels.
931 * Historically, the RGBA values were in [0,1] and rescaled to fit
932 * into GLubytes, etc. But with new integer formats, the RGBA values
933 * may have any value and we don't always rescale when converting to
934 * integers.
935 *
936 * Note: the rgba values will be modified by this function when any pixel
937 * transfer ops are enabled.
938 */
939 void
940 _mesa_pack_rgba_span_float(struct gl_context *ctx, GLuint n, GLfloat rgba[][4],
941 GLenum dstFormat, GLenum dstType,
942 GLvoid *dstAddr,
943 const struct gl_pixelstore_attrib *dstPacking,
944 GLbitfield transferOps)
945 {
946 GLfloat *luminance;
947 const GLint comps = _mesa_components_in_format(dstFormat);
948 const GLboolean intDstFormat = _mesa_is_enum_format_integer(dstFormat);
949 GLuint i;
950
951 if (dstFormat == GL_LUMINANCE ||
952 dstFormat == GL_LUMINANCE_ALPHA ||
953 dstFormat == GL_LUMINANCE_INTEGER_EXT ||
954 dstFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT) {
955 luminance = malloc(n * sizeof(GLfloat));
956 if (!luminance) {
957 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
958 return;
959 }
960 }
961 else {
962 luminance = NULL;
963 }
964
965 /* EXT_texture_integer specifies no transfer ops on integer
966 * types in the resolved issues section. Just set them to 0
967 * for integer surfaces.
968 */
969 if (intDstFormat)
970 transferOps = 0;
971
972 if (transferOps) {
973 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
974 }
975
976 /*
977 * Component clamping (besides clamping to [0,1] in
978 * _mesa_apply_rgba_transfer_ops()).
979 */
980 if (intDstFormat) {
981 /* clamping to dest type's min/max values */
982 GLfloat min, max;
983 if (get_type_min_max(dstType, &min, &max)) {
984 for (i = 0; i < n; i++) {
985 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], min, max);
986 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], min, max);
987 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], min, max);
988 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], min, max);
989 }
990 }
991 }
992 else if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
993 /* compute luminance values */
994 if (transferOps & IMAGE_CLAMP_BIT) {
995 for (i = 0; i < n; i++) {
996 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
997 luminance[i] = CLAMP(sum, 0.0F, 1.0F);
998 }
999 }
1000 else {
1001 for (i = 0; i < n; i++) {
1002 luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1003 }
1004 }
1005 }
1006
1007 /*
1008 * Pack/store the pixels. Ugh! Lots of cases!!!
1009 */
1010 switch (dstType) {
1011 case GL_UNSIGNED_BYTE:
1012 pack_ubyte_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
1013 break;
1014 case GL_BYTE:
1015 pack_byte_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
1016 break;
1017 case GL_UNSIGNED_SHORT:
1018 {
1019 GLushort *dst = (GLushort *) dstAddr;
1020 switch (dstFormat) {
1021 case GL_RED:
1022 for (i=0;i<n;i++)
1023 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
1024 break;
1025 case GL_GREEN:
1026 for (i=0;i<n;i++)
1027 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
1028 break;
1029 case GL_BLUE:
1030 for (i=0;i<n;i++)
1031 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
1032 break;
1033 case GL_ALPHA:
1034 for (i=0;i<n;i++)
1035 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
1036 break;
1037 case GL_LUMINANCE:
1038 for (i=0;i<n;i++)
1039 UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
1040 break;
1041 case GL_LUMINANCE_ALPHA:
1042 for (i=0;i<n;i++) {
1043 UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
1044 CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
1045 }
1046 break;
1047 case GL_RG:
1048 for (i=0;i<n;i++) {
1049 CLAMPED_FLOAT_TO_USHORT(dst[i*2+0], rgba[i][RCOMP]);
1050 CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][GCOMP]);
1051 }
1052 break;
1053 case GL_RGB:
1054 for (i=0;i<n;i++) {
1055 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
1056 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1057 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
1058 }
1059 break;
1060 case GL_RGBA:
1061 for (i=0;i<n;i++) {
1062 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
1063 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1064 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
1065 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1066 }
1067 break;
1068 case GL_BGR:
1069 for (i=0;i<n;i++) {
1070 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
1071 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1072 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
1073 }
1074 break;
1075 case GL_BGRA:
1076 for (i=0;i<n;i++) {
1077 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
1078 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1079 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
1080 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1081 }
1082 break;
1083 case GL_ABGR_EXT:
1084 for (i=0;i<n;i++) {
1085 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
1086 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
1087 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
1088 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
1089 }
1090 break;
1091 case GL_RED_INTEGER_EXT:
1092 for (i=0;i<n;i++) {
1093 dst[i] = (GLushort) rgba[i][RCOMP];
1094 }
1095 break;
1096 case GL_GREEN_INTEGER_EXT:
1097 for (i=0;i<n;i++) {
1098 dst[i] = (GLushort) rgba[i][GCOMP];
1099 }
1100 break;
1101 case GL_BLUE_INTEGER_EXT:
1102 for (i=0;i<n;i++) {
1103 dst[i] = (GLushort) rgba[i][BCOMP];
1104 }
1105 break;
1106 case GL_ALPHA_INTEGER_EXT:
1107 for (i=0;i<n;i++) {
1108 dst[i] = (GLushort) rgba[i][ACOMP];
1109 }
1110 break;
1111 case GL_RG_INTEGER:
1112 for (i=0;i<n;i++) {
1113 dst[i*2+0] = (GLushort) rgba[i][RCOMP];
1114 dst[i*2+1] = (GLushort) rgba[i][GCOMP];
1115 }
1116 break;
1117 case GL_RGB_INTEGER_EXT:
1118 for (i=0;i<n;i++) {
1119 dst[i*3+0] = (GLushort) rgba[i][RCOMP];
1120 dst[i*3+1] = (GLushort) rgba[i][GCOMP];
1121 dst[i*3+2] = (GLushort) rgba[i][BCOMP];
1122 }
1123 break;
1124 case GL_RGBA_INTEGER_EXT:
1125 for (i=0;i<n;i++) {
1126 dst[i*4+0] = (GLushort) rgba[i][RCOMP];
1127 dst[i*4+1] = (GLushort) rgba[i][GCOMP];
1128 dst[i*4+2] = (GLushort) rgba[i][BCOMP];
1129 dst[i*4+3] = (GLushort) rgba[i][ACOMP];
1130 }
1131 break;
1132 case GL_BGR_INTEGER_EXT:
1133 for (i=0;i<n;i++) {
1134 dst[i*3+0] = (GLushort) rgba[i][BCOMP];
1135 dst[i*3+1] = (GLushort) rgba[i][GCOMP];
1136 dst[i*3+2] = (GLushort) rgba[i][RCOMP];
1137 }
1138 break;
1139 case GL_BGRA_INTEGER_EXT:
1140 for (i=0;i<n;i++) {
1141 dst[i*4+0] = (GLushort) rgba[i][BCOMP];
1142 dst[i*4+1] = (GLushort) rgba[i][GCOMP];
1143 dst[i*4+2] = (GLushort) rgba[i][RCOMP];
1144 dst[i*4+3] = (GLushort) rgba[i][ACOMP];
1145 }
1146 break;
1147 case GL_LUMINANCE_INTEGER_EXT:
1148 for (i=0;i<n;i++) {
1149 dst[i*2+0] = (GLushort) (rgba[i][RCOMP] +
1150 rgba[i][GCOMP] +
1151 rgba[i][BCOMP]);
1152 dst[i*2+1] = (GLushort) rgba[i][ACOMP];
1153 }
1154 break;
1155 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1156 for (i=0;i<n;i++) {
1157 dst[i] = (GLushort) (rgba[i][RCOMP] +
1158 rgba[i][GCOMP] +
1159 rgba[i][BCOMP]);
1160 }
1161 break;
1162 default:
1163 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1164 }
1165 }
1166 break;
1167 case GL_SHORT:
1168 pack_short_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
1169 break;
1170 case GL_UNSIGNED_INT:
1171 pack_uint_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
1172 break;
1173 case GL_INT:
1174 pack_int_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
1175 break;
1176 case GL_FLOAT:
1177 /* No conversion necessary. */
1178 pack_float_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
1179 break;
1180 case GL_HALF_FLOAT_ARB:
1181 pack_half_float_from_float_rgba(ctx, dstAddr, dstFormat, rgba, luminance, n);
1182 break;
1183 case GL_UNSIGNED_BYTE_3_3_2:
1184 if (dstFormat == GL_RGB)
1185 _mesa_pack_float_rgba_row(MESA_FORMAT_B2G3R3_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1186 break;
1187 case GL_UNSIGNED_BYTE_2_3_3_REV:
1188 if (dstFormat == GL_RGB)
1189 _mesa_pack_float_rgba_row(MESA_FORMAT_R3G3B2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1190 break;
1191 case GL_UNSIGNED_SHORT_5_6_5:
1192 if (dstFormat == GL_RGB)
1193 _mesa_pack_float_rgba_row(MESA_FORMAT_B5G6R5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1194 break;
1195 case GL_UNSIGNED_SHORT_5_6_5_REV:
1196 if (dstFormat == GL_RGB)
1197 _mesa_pack_float_rgba_row(MESA_FORMAT_R5G6B5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1198 break;
1199 case GL_UNSIGNED_SHORT_4_4_4_4:
1200 if (dstFormat == GL_RGBA)
1201 _mesa_pack_float_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1202 else if (dstFormat == GL_BGRA)
1203 _mesa_pack_float_rgba_row(MESA_FORMAT_A4R4G4B4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1204 else if (dstFormat == GL_ABGR_EXT)
1205 _mesa_pack_float_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1206 break;
1207 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1208 if (dstFormat == GL_RGBA)
1209 _mesa_pack_float_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1210 else if (dstFormat == GL_BGRA)
1211 _mesa_pack_float_rgba_row(MESA_FORMAT_B4G4R4A4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1212 else if (dstFormat == GL_ABGR_EXT)
1213 _mesa_pack_float_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1214 break;
1215 case GL_UNSIGNED_SHORT_5_5_5_1:
1216 if (dstFormat == GL_RGBA)
1217 _mesa_pack_float_rgba_row(MESA_FORMAT_A1B5G5R5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1218 else if (dstFormat == GL_BGRA)
1219 _mesa_pack_float_rgba_row(MESA_FORMAT_A1R5G5B5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1220 else if (dstFormat == GL_ABGR_EXT)
1221 _mesa_pack_float_rgba_row(MESA_FORMAT_R1G5B5A5_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1222 break;
1223 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1224 if (dstFormat == GL_RGBA)
1225 _mesa_pack_float_rgba_row(MESA_FORMAT_R5G5B5A1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1226 else if (dstFormat == GL_BGRA)
1227 _mesa_pack_float_rgba_row(MESA_FORMAT_B5G5R5A1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1228 else if (dstFormat == GL_ABGR_EXT)
1229 _mesa_pack_float_rgba_row(MESA_FORMAT_A5B5G5R1_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1230 break;
1231 case GL_UNSIGNED_INT_8_8_8_8:
1232 if (dstFormat == GL_RGBA)
1233 _mesa_pack_float_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1234 else if (dstFormat == GL_BGRA)
1235 _mesa_pack_float_rgba_row(MESA_FORMAT_A8R8G8B8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1236 else if (dstFormat == GL_ABGR_EXT)
1237 _mesa_pack_float_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1238 break;
1239 case GL_UNSIGNED_INT_8_8_8_8_REV:
1240 if (dstFormat == GL_RGBA)
1241 _mesa_pack_float_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1242 else if (dstFormat == GL_BGRA)
1243 _mesa_pack_float_rgba_row(MESA_FORMAT_B8G8R8A8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1244 else if (dstFormat == GL_ABGR_EXT)
1245 _mesa_pack_float_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1246 break;
1247 case GL_UNSIGNED_INT_10_10_10_2:
1248 if (dstFormat == GL_RGBA)
1249 _mesa_pack_float_rgba_row(MESA_FORMAT_A2B10G10R10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1250 else if (dstFormat == GL_BGRA)
1251 _mesa_pack_float_rgba_row(MESA_FORMAT_A2R10G10B10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1252 else if (dstFormat == GL_ABGR_EXT)
1253 _mesa_pack_float_rgba_row(MESA_FORMAT_R2G10B10A10_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1254 break;
1255 case GL_UNSIGNED_INT_2_10_10_10_REV:
1256 if (dstFormat == GL_RGBA)
1257 _mesa_pack_float_rgba_row(MESA_FORMAT_R10G10B10A2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1258 else if (dstFormat == GL_BGRA)
1259 _mesa_pack_float_rgba_row(MESA_FORMAT_B10G10R10A2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1260 else if (dstFormat == GL_ABGR_EXT)
1261 _mesa_pack_float_rgba_row(MESA_FORMAT_A10B10G10R2_UNORM, n, (void *)rgba[0], (void *)dstAddr);
1262 break;
1263 case GL_UNSIGNED_INT_5_9_9_9_REV:
1264 _mesa_pack_float_rgba_row(MESA_FORMAT_R9G9B9E5_FLOAT, n, (void *)rgba[0], (void *)dstAddr);
1265 break;
1266 case GL_UNSIGNED_INT_10F_11F_11F_REV:
1267 _mesa_pack_float_rgba_row(MESA_FORMAT_R11G11B10_FLOAT, n, (void *)rgba[0], (void *)dstAddr);
1268 break;
1269 default:
1270 _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
1271 free(luminance);
1272 return;
1273 }
1274
1275 if (dstPacking->SwapBytes) {
1276 GLint swapSize = _mesa_sizeof_packed_type(dstType);
1277 if (swapSize == 2) {
1278 _mesa_swap2((GLushort *) dstAddr, n * comps);
1279 }
1280 else if (swapSize == 4) {
1281 _mesa_swap4((GLuint *) dstAddr, n * comps);
1282 }
1283 }
1284
1285 free(luminance);
1286 }
1287
1288
1289
1290 #define SWAP2BYTE(VALUE) \
1291 { \
1292 GLubyte *bytes = (GLubyte *) &(VALUE); \
1293 GLubyte tmp = bytes[0]; \
1294 bytes[0] = bytes[1]; \
1295 bytes[1] = tmp; \
1296 }
1297
1298 #define SWAP4BYTE(VALUE) \
1299 { \
1300 GLubyte *bytes = (GLubyte *) &(VALUE); \
1301 GLubyte tmp = bytes[0]; \
1302 bytes[0] = bytes[3]; \
1303 bytes[3] = tmp; \
1304 tmp = bytes[1]; \
1305 bytes[1] = bytes[2]; \
1306 bytes[2] = tmp; \
1307 }
1308
1309
1310 static void
1311 extract_uint_indexes(GLuint n, GLuint indexes[],
1312 GLenum srcFormat, GLenum srcType, const GLvoid *src,
1313 const struct gl_pixelstore_attrib *unpack )
1314 {
1315 ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
1316
1317 ASSERT(srcType == GL_BITMAP ||
1318 srcType == GL_UNSIGNED_BYTE ||
1319 srcType == GL_BYTE ||
1320 srcType == GL_UNSIGNED_SHORT ||
1321 srcType == GL_SHORT ||
1322 srcType == GL_UNSIGNED_INT ||
1323 srcType == GL_INT ||
1324 srcType == GL_UNSIGNED_INT_24_8_EXT ||
1325 srcType == GL_HALF_FLOAT_ARB ||
1326 srcType == GL_FLOAT ||
1327 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
1328
1329 switch (srcType) {
1330 case GL_BITMAP:
1331 {
1332 GLubyte *ubsrc = (GLubyte *) src;
1333 if (unpack->LsbFirst) {
1334 GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
1335 GLuint i;
1336 for (i = 0; i < n; i++) {
1337 indexes[i] = (*ubsrc & mask) ? 1 : 0;
1338 if (mask == 128) {
1339 mask = 1;
1340 ubsrc++;
1341 }
1342 else {
1343 mask = mask << 1;
1344 }
1345 }
1346 }
1347 else {
1348 GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
1349 GLuint i;
1350 for (i = 0; i < n; i++) {
1351 indexes[i] = (*ubsrc & mask) ? 1 : 0;
1352 if (mask == 1) {
1353 mask = 128;
1354 ubsrc++;
1355 }
1356 else {
1357 mask = mask >> 1;
1358 }
1359 }
1360 }
1361 }
1362 break;
1363 case GL_UNSIGNED_BYTE:
1364 {
1365 GLuint i;
1366 const GLubyte *s = (const GLubyte *) src;
1367 for (i = 0; i < n; i++)
1368 indexes[i] = s[i];
1369 }
1370 break;
1371 case GL_BYTE:
1372 {
1373 GLuint i;
1374 const GLbyte *s = (const GLbyte *) src;
1375 for (i = 0; i < n; i++)
1376 indexes[i] = s[i];
1377 }
1378 break;
1379 case GL_UNSIGNED_SHORT:
1380 {
1381 GLuint i;
1382 const GLushort *s = (const GLushort *) src;
1383 if (unpack->SwapBytes) {
1384 for (i = 0; i < n; i++) {
1385 GLushort value = s[i];
1386 SWAP2BYTE(value);
1387 indexes[i] = value;
1388 }
1389 }
1390 else {
1391 for (i = 0; i < n; i++)
1392 indexes[i] = s[i];
1393 }
1394 }
1395 break;
1396 case GL_SHORT:
1397 {
1398 GLuint i;
1399 const GLshort *s = (const GLshort *) src;
1400 if (unpack->SwapBytes) {
1401 for (i = 0; i < n; i++) {
1402 GLshort value = s[i];
1403 SWAP2BYTE(value);
1404 indexes[i] = value;
1405 }
1406 }
1407 else {
1408 for (i = 0; i < n; i++)
1409 indexes[i] = s[i];
1410 }
1411 }
1412 break;
1413 case GL_UNSIGNED_INT:
1414 {
1415 GLuint i;
1416 const GLuint *s = (const GLuint *) src;
1417 if (unpack->SwapBytes) {
1418 for (i = 0; i < n; i++) {
1419 GLuint value = s[i];
1420 SWAP4BYTE(value);
1421 indexes[i] = value;
1422 }
1423 }
1424 else {
1425 for (i = 0; i < n; i++)
1426 indexes[i] = s[i];
1427 }
1428 }
1429 break;
1430 case GL_INT:
1431 {
1432 GLuint i;
1433 const GLint *s = (const GLint *) src;
1434 if (unpack->SwapBytes) {
1435 for (i = 0; i < n; i++) {
1436 GLint value = s[i];
1437 SWAP4BYTE(value);
1438 indexes[i] = value;
1439 }
1440 }
1441 else {
1442 for (i = 0; i < n; i++)
1443 indexes[i] = s[i];
1444 }
1445 }
1446 break;
1447 case GL_FLOAT:
1448 {
1449 GLuint i;
1450 const GLfloat *s = (const GLfloat *) src;
1451 if (unpack->SwapBytes) {
1452 for (i = 0; i < n; i++) {
1453 GLfloat value = s[i];
1454 SWAP4BYTE(value);
1455 indexes[i] = (GLuint) value;
1456 }
1457 }
1458 else {
1459 for (i = 0; i < n; i++)
1460 indexes[i] = (GLuint) s[i];
1461 }
1462 }
1463 break;
1464 case GL_HALF_FLOAT_ARB:
1465 {
1466 GLuint i;
1467 const GLhalfARB *s = (const GLhalfARB *) src;
1468 if (unpack->SwapBytes) {
1469 for (i = 0; i < n; i++) {
1470 GLhalfARB value = s[i];
1471 SWAP2BYTE(value);
1472 indexes[i] = (GLuint) _mesa_half_to_float(value);
1473 }
1474 }
1475 else {
1476 for (i = 0; i < n; i++)
1477 indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
1478 }
1479 }
1480 break;
1481 case GL_UNSIGNED_INT_24_8_EXT:
1482 {
1483 GLuint i;
1484 const GLuint *s = (const GLuint *) src;
1485 if (unpack->SwapBytes) {
1486 for (i = 0; i < n; i++) {
1487 GLuint value = s[i];
1488 SWAP4BYTE(value);
1489 indexes[i] = value & 0xff; /* lower 8 bits */
1490 }
1491 }
1492 else {
1493 for (i = 0; i < n; i++)
1494 indexes[i] = s[i] & 0xff; /* lower 8 bits */
1495 }
1496 }
1497 break;
1498 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1499 {
1500 GLuint i;
1501 const GLuint *s = (const GLuint *) src;
1502 if (unpack->SwapBytes) {
1503 for (i = 0; i < n; i++) {
1504 GLuint value = s[i*2+1];
1505 SWAP4BYTE(value);
1506 indexes[i] = value & 0xff; /* lower 8 bits */
1507 }
1508 }
1509 else {
1510 for (i = 0; i < n; i++)
1511 indexes[i] = s[i*2+1] & 0xff; /* lower 8 bits */
1512 }
1513 }
1514 break;
1515
1516 default:
1517 _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
1518 return;
1519 }
1520 }
1521
1522
1523 /**
1524 * Return source/dest RGBA indexes for unpacking pixels.
1525 */
1526 static void
1527 get_component_mapping(GLenum format,
1528 GLint *rSrc,
1529 GLint *gSrc,
1530 GLint *bSrc,
1531 GLint *aSrc,
1532 GLint *rDst,
1533 GLint *gDst,
1534 GLint *bDst,
1535 GLint *aDst)
1536 {
1537 switch (format) {
1538 case GL_RED:
1539 case GL_RED_INTEGER_EXT:
1540 *rSrc = 0;
1541 *gSrc = *bSrc = *aSrc = -1;
1542 break;
1543 case GL_GREEN:
1544 case GL_GREEN_INTEGER_EXT:
1545 *gSrc = 0;
1546 *rSrc = *bSrc = *aSrc = -1;
1547 break;
1548 case GL_BLUE:
1549 case GL_BLUE_INTEGER_EXT:
1550 *bSrc = 0;
1551 *rSrc = *gSrc = *aSrc = -1;
1552 break;
1553 case GL_ALPHA:
1554 case GL_ALPHA_INTEGER_EXT:
1555 *rSrc = *gSrc = *bSrc = -1;
1556 *aSrc = 0;
1557 break;
1558 case GL_LUMINANCE:
1559 case GL_LUMINANCE_INTEGER_EXT:
1560 *rSrc = *gSrc = *bSrc = 0;
1561 *aSrc = -1;
1562 break;
1563 case GL_LUMINANCE_ALPHA:
1564 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1565 *rSrc = *gSrc = *bSrc = 0;
1566 *aSrc = 1;
1567 break;
1568 case GL_INTENSITY:
1569 *rSrc = *gSrc = *bSrc = *aSrc = 0;
1570 break;
1571 case GL_RG:
1572 case GL_RG_INTEGER:
1573 *rSrc = 0;
1574 *gSrc = 1;
1575 *bSrc = -1;
1576 *aSrc = -1;
1577 *rDst = 0;
1578 *gDst = 1;
1579 *bDst = 2;
1580 *aDst = 3;
1581 break;
1582 case GL_RGB:
1583 case GL_RGB_INTEGER:
1584 *rSrc = 0;
1585 *gSrc = 1;
1586 *bSrc = 2;
1587 *aSrc = -1;
1588 *rDst = 0;
1589 *gDst = 1;
1590 *bDst = 2;
1591 *aDst = 3;
1592 break;
1593 case GL_BGR:
1594 case GL_BGR_INTEGER:
1595 *rSrc = 2;
1596 *gSrc = 1;
1597 *bSrc = 0;
1598 *aSrc = -1;
1599 *rDst = 2;
1600 *gDst = 1;
1601 *bDst = 0;
1602 *aDst = 3;
1603 break;
1604 case GL_RGBA:
1605 case GL_RGBA_INTEGER:
1606 *rSrc = 0;
1607 *gSrc = 1;
1608 *bSrc = 2;
1609 *aSrc = 3;
1610 *rDst = 0;
1611 *gDst = 1;
1612 *bDst = 2;
1613 *aDst = 3;
1614 break;
1615 case GL_BGRA:
1616 case GL_BGRA_INTEGER:
1617 *rSrc = 2;
1618 *gSrc = 1;
1619 *bSrc = 0;
1620 *aSrc = 3;
1621 *rDst = 2;
1622 *gDst = 1;
1623 *bDst = 0;
1624 *aDst = 3;
1625 break;
1626 case GL_ABGR_EXT:
1627 *rSrc = 3;
1628 *gSrc = 2;
1629 *bSrc = 1;
1630 *aSrc = 0;
1631 *rDst = 3;
1632 *gDst = 2;
1633 *bDst = 1;
1634 *aDst = 0;
1635 break;
1636 default:
1637 _mesa_problem(NULL, "bad srcFormat %s in get_component_mapping",
1638 _mesa_lookup_enum_by_nr(format));
1639 return;
1640 }
1641 }
1642
1643
1644
1645 /*
1646 * This function extracts floating point RGBA values from arbitrary
1647 * image data. srcFormat and srcType are the format and type parameters
1648 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1649 *
1650 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1651 * implements the "Conversion to floating point", "Conversion to RGB",
1652 * and "Final Expansion to RGBA" operations.
1653 *
1654 * Args: n - number of pixels
1655 * rgba - output colors
1656 * srcFormat - format of incoming data
1657 * srcType - data type of incoming data
1658 * src - source data pointer
1659 * swapBytes - perform byteswapping of incoming data?
1660 */
1661 static void
1662 extract_float_rgba(GLuint n, GLfloat rgba[][4],
1663 GLenum srcFormat, GLenum srcType, const GLvoid *src,
1664 GLboolean swapBytes)
1665 {
1666 GLint rSrc, gSrc, bSrc, aSrc;
1667 GLint stride;
1668 GLint rDst, bDst, gDst, aDst;
1669 GLboolean intFormat;
1670 GLfloat rs = 1.0f, gs = 1.0f, bs = 1.0f, as = 1.0f; /* scale factors */
1671
1672 ASSERT(srcFormat == GL_RED ||
1673 srcFormat == GL_GREEN ||
1674 srcFormat == GL_BLUE ||
1675 srcFormat == GL_ALPHA ||
1676 srcFormat == GL_LUMINANCE ||
1677 srcFormat == GL_LUMINANCE_ALPHA ||
1678 srcFormat == GL_INTENSITY ||
1679 srcFormat == GL_RG ||
1680 srcFormat == GL_RGB ||
1681 srcFormat == GL_BGR ||
1682 srcFormat == GL_RGBA ||
1683 srcFormat == GL_BGRA ||
1684 srcFormat == GL_ABGR_EXT ||
1685 srcFormat == GL_RED_INTEGER_EXT ||
1686 srcFormat == GL_GREEN_INTEGER_EXT ||
1687 srcFormat == GL_BLUE_INTEGER_EXT ||
1688 srcFormat == GL_ALPHA_INTEGER_EXT ||
1689 srcFormat == GL_RG_INTEGER ||
1690 srcFormat == GL_RGB_INTEGER_EXT ||
1691 srcFormat == GL_RGBA_INTEGER_EXT ||
1692 srcFormat == GL_BGR_INTEGER_EXT ||
1693 srcFormat == GL_BGRA_INTEGER_EXT ||
1694 srcFormat == GL_LUMINANCE_INTEGER_EXT ||
1695 srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
1696
1697 ASSERT(srcType == GL_UNSIGNED_BYTE ||
1698 srcType == GL_BYTE ||
1699 srcType == GL_UNSIGNED_SHORT ||
1700 srcType == GL_SHORT ||
1701 srcType == GL_UNSIGNED_INT ||
1702 srcType == GL_INT ||
1703 srcType == GL_HALF_FLOAT_ARB ||
1704 srcType == GL_FLOAT ||
1705 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
1706 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
1707 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
1708 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
1709 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
1710 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
1711 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
1712 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
1713 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1714 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
1715 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
1716 srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
1717 srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
1718 srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
1719
1720 get_component_mapping(srcFormat,
1721 &rSrc, &gSrc, &bSrc, &aSrc,
1722 &rDst, &gDst, &bDst, &aDst);
1723
1724 stride = _mesa_components_in_format(srcFormat);
1725
1726 intFormat = _mesa_is_enum_format_integer(srcFormat);
1727
1728 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT_FLT, DEFAULT_INT, TYPE, CONVERSION) \
1729 if ((SRC_INDEX) < 0) { \
1730 GLuint i; \
1731 if (intFormat) { \
1732 for (i = 0; i < n; i++) { \
1733 rgba[i][DST_INDEX] = DEFAULT_INT; \
1734 } \
1735 } \
1736 else { \
1737 for (i = 0; i < n; i++) { \
1738 rgba[i][DST_INDEX] = DEFAULT_FLT; \
1739 } \
1740 } \
1741 } \
1742 else if (swapBytes) { \
1743 const TYPE *s = (const TYPE *) src; \
1744 GLuint i; \
1745 for (i = 0; i < n; i++) { \
1746 TYPE value = s[SRC_INDEX]; \
1747 if (sizeof(TYPE) == 2) { \
1748 SWAP2BYTE(value); \
1749 } \
1750 else if (sizeof(TYPE) == 4) { \
1751 SWAP4BYTE(value); \
1752 } \
1753 if (intFormat) \
1754 rgba[i][DST_INDEX] = (GLfloat) value; \
1755 else \
1756 rgba[i][DST_INDEX] = (GLfloat) CONVERSION(value); \
1757 s += stride; \
1758 } \
1759 } \
1760 else { \
1761 const TYPE *s = (const TYPE *) src; \
1762 GLuint i; \
1763 if (intFormat) { \
1764 for (i = 0; i < n; i++) { \
1765 rgba[i][DST_INDEX] = (GLfloat) s[SRC_INDEX]; \
1766 s += stride; \
1767 } \
1768 } \
1769 else { \
1770 for (i = 0; i < n; i++) { \
1771 rgba[i][DST_INDEX] = (GLfloat) CONVERSION(s[SRC_INDEX]); \
1772 s += stride; \
1773 } \
1774 } \
1775 }
1776
1777 switch (srcType) {
1778 case GL_UNSIGNED_BYTE:
1779 PROCESS(rSrc, RCOMP, 0.0F, 0, GLubyte, UBYTE_TO_FLOAT);
1780 PROCESS(gSrc, GCOMP, 0.0F, 0, GLubyte, UBYTE_TO_FLOAT);
1781 PROCESS(bSrc, BCOMP, 0.0F, 0, GLubyte, UBYTE_TO_FLOAT);
1782 PROCESS(aSrc, ACOMP, 1.0F, 255, GLubyte, UBYTE_TO_FLOAT);
1783 break;
1784 case GL_BYTE:
1785 PROCESS(rSrc, RCOMP, 0.0F, 0, GLbyte, BYTE_TO_FLOAT_TEX);
1786 PROCESS(gSrc, GCOMP, 0.0F, 0, GLbyte, BYTE_TO_FLOAT_TEX);
1787 PROCESS(bSrc, BCOMP, 0.0F, 0, GLbyte, BYTE_TO_FLOAT_TEX);
1788 PROCESS(aSrc, ACOMP, 1.0F, 127, GLbyte, BYTE_TO_FLOAT_TEX);
1789 break;
1790 case GL_UNSIGNED_SHORT:
1791 PROCESS(rSrc, RCOMP, 0.0F, 0, GLushort, USHORT_TO_FLOAT);
1792 PROCESS(gSrc, GCOMP, 0.0F, 0, GLushort, USHORT_TO_FLOAT);
1793 PROCESS(bSrc, BCOMP, 0.0F, 0, GLushort, USHORT_TO_FLOAT);
1794 PROCESS(aSrc, ACOMP, 1.0F, 0xffff, GLushort, USHORT_TO_FLOAT);
1795 break;
1796 case GL_SHORT:
1797 PROCESS(rSrc, RCOMP, 0.0F, 0, GLshort, SHORT_TO_FLOAT_TEX);
1798 PROCESS(gSrc, GCOMP, 0.0F, 0, GLshort, SHORT_TO_FLOAT_TEX);
1799 PROCESS(bSrc, BCOMP, 0.0F, 0, GLshort, SHORT_TO_FLOAT_TEX);
1800 PROCESS(aSrc, ACOMP, 1.0F, 32767, GLshort, SHORT_TO_FLOAT_TEX);
1801 break;
1802 case GL_UNSIGNED_INT:
1803 PROCESS(rSrc, RCOMP, 0.0F, 0, GLuint, UINT_TO_FLOAT);
1804 PROCESS(gSrc, GCOMP, 0.0F, 0, GLuint, UINT_TO_FLOAT);
1805 PROCESS(bSrc, BCOMP, 0.0F, 0, GLuint, UINT_TO_FLOAT);
1806 PROCESS(aSrc, ACOMP, 1.0F, 0xffffffff, GLuint, UINT_TO_FLOAT);
1807 break;
1808 case GL_INT:
1809 PROCESS(rSrc, RCOMP, 0.0F, 0, GLint, INT_TO_FLOAT);
1810 PROCESS(gSrc, GCOMP, 0.0F, 0, GLint, INT_TO_FLOAT);
1811 PROCESS(bSrc, BCOMP, 0.0F, 0, GLint, INT_TO_FLOAT);
1812 PROCESS(aSrc, ACOMP, 1.0F, 2147483647, GLint, INT_TO_FLOAT);
1813 break;
1814 case GL_FLOAT:
1815 PROCESS(rSrc, RCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
1816 PROCESS(gSrc, GCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
1817 PROCESS(bSrc, BCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
1818 PROCESS(aSrc, ACOMP, 1.0F, 1.0F, GLfloat, (GLfloat));
1819 break;
1820 case GL_HALF_FLOAT_ARB:
1821 PROCESS(rSrc, RCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
1822 PROCESS(gSrc, GCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
1823 PROCESS(bSrc, BCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
1824 PROCESS(aSrc, ACOMP, 1.0F, 1.0F, GLhalfARB, _mesa_half_to_float);
1825 break;
1826 case GL_UNSIGNED_BYTE_3_3_2:
1827 {
1828 const GLubyte *ubsrc = (const GLubyte *) src;
1829 GLuint i;
1830 if (!intFormat) {
1831 rs = 1.0F / 7.0F;
1832 gs = 1.0F / 7.0F;
1833 bs = 1.0F / 3.0F;
1834 }
1835 for (i = 0; i < n; i ++) {
1836 GLubyte p = ubsrc[i];
1837 rgba[i][rDst] = ((p >> 5) ) * rs;
1838 rgba[i][gDst] = ((p >> 2) & 0x7) * gs;
1839 rgba[i][bDst] = ((p ) & 0x3) * bs;
1840 rgba[i][aDst] = 1.0F;
1841 }
1842 }
1843 break;
1844 case GL_UNSIGNED_BYTE_2_3_3_REV:
1845 {
1846 const GLubyte *ubsrc = (const GLubyte *) src;
1847 GLuint i;
1848 if (!intFormat) {
1849 rs = 1.0F / 7.0F;
1850 gs = 1.0F / 7.0F;
1851 bs = 1.0F / 3.0F;
1852 }
1853 for (i = 0; i < n; i ++) {
1854 GLubyte p = ubsrc[i];
1855 rgba[i][rDst] = ((p ) & 0x7) * rs;
1856 rgba[i][gDst] = ((p >> 3) & 0x7) * gs;
1857 rgba[i][bDst] = ((p >> 6) ) * bs;
1858 rgba[i][aDst] = 1.0F;
1859 }
1860 }
1861 break;
1862 case GL_UNSIGNED_SHORT_5_6_5:
1863 if (!intFormat) {
1864 rs = 1.0F / 31.0F;
1865 gs = 1.0F / 63.0F;
1866 bs = 1.0F / 31.0F;
1867 }
1868 if (swapBytes) {
1869 const GLushort *ussrc = (const GLushort *) src;
1870 GLuint i;
1871 for (i = 0; i < n; i ++) {
1872 GLushort p = ussrc[i];
1873 SWAP2BYTE(p);
1874 rgba[i][rDst] = ((p >> 11) ) * rs;
1875 rgba[i][gDst] = ((p >> 5) & 0x3f) * gs;
1876 rgba[i][bDst] = ((p ) & 0x1f) * bs;
1877 rgba[i][aDst] = 1.0F;
1878 }
1879 }
1880 else {
1881 const GLushort *ussrc = (const GLushort *) src;
1882 GLuint i;
1883 for (i = 0; i < n; i ++) {
1884 GLushort p = ussrc[i];
1885 rgba[i][rDst] = ((p >> 11) ) * rs;
1886 rgba[i][gDst] = ((p >> 5) & 0x3f) * gs;
1887 rgba[i][bDst] = ((p ) & 0x1f) * bs;
1888 rgba[i][aDst] = 1.0F;
1889 }
1890 }
1891 break;
1892 case GL_UNSIGNED_SHORT_5_6_5_REV:
1893 if (!intFormat) {
1894 rs = 1.0F / 31.0F;
1895 gs = 1.0F / 63.0F;
1896 bs = 1.0F / 31.0F;
1897 }
1898 if (swapBytes) {
1899 const GLushort *ussrc = (const GLushort *) src;
1900 GLuint i;
1901 for (i = 0; i < n; i ++) {
1902 GLushort p = ussrc[i];
1903 SWAP2BYTE(p);
1904 rgba[i][rDst] = ((p ) & 0x1f) * rs;
1905 rgba[i][gDst] = ((p >> 5) & 0x3f) * gs;
1906 rgba[i][bDst] = ((p >> 11) ) * bs;
1907 rgba[i][aDst] = 1.0F;
1908 }
1909 }
1910 else {
1911 const GLushort *ussrc = (const GLushort *) src;
1912 GLuint i;
1913 for (i = 0; i < n; i ++) {
1914 GLushort p = ussrc[i];
1915 rgba[i][rDst] = ((p ) & 0x1f) * rs;
1916 rgba[i][gDst] = ((p >> 5) & 0x3f) * gs;
1917 rgba[i][bDst] = ((p >> 11) ) * bs;
1918 rgba[i][aDst] = 1.0F;
1919 }
1920 }
1921 break;
1922 case GL_UNSIGNED_SHORT_4_4_4_4:
1923 if (!intFormat) {
1924 rs = gs = bs = as = 1.0F / 15.0F;
1925 }
1926 if (swapBytes) {
1927 const GLushort *ussrc = (const GLushort *) src;
1928 GLuint i;
1929 for (i = 0; i < n; i ++) {
1930 GLushort p = ussrc[i];
1931 SWAP2BYTE(p);
1932 rgba[i][rDst] = ((p >> 12) ) * rs;
1933 rgba[i][gDst] = ((p >> 8) & 0xf) * gs;
1934 rgba[i][bDst] = ((p >> 4) & 0xf) * bs;
1935 rgba[i][aDst] = ((p ) & 0xf) * as;
1936 }
1937 }
1938 else {
1939 const GLushort *ussrc = (const GLushort *) src;
1940 GLuint i;
1941 for (i = 0; i < n; i ++) {
1942 GLushort p = ussrc[i];
1943 rgba[i][rDst] = ((p >> 12) ) * rs;
1944 rgba[i][gDst] = ((p >> 8) & 0xf) * gs;
1945 rgba[i][bDst] = ((p >> 4) & 0xf) * bs;
1946 rgba[i][aDst] = ((p ) & 0xf) * as;
1947 }
1948 }
1949 break;
1950 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1951 if (!intFormat) {
1952 rs = gs = bs = as = 1.0F / 15.0F;
1953 }
1954 if (swapBytes) {
1955 const GLushort *ussrc = (const GLushort *) src;
1956 GLuint i;
1957 for (i = 0; i < n; i ++) {
1958 GLushort p = ussrc[i];
1959 SWAP2BYTE(p);
1960 rgba[i][rDst] = ((p ) & 0xf) * rs;
1961 rgba[i][gDst] = ((p >> 4) & 0xf) * gs;
1962 rgba[i][bDst] = ((p >> 8) & 0xf) * bs;
1963 rgba[i][aDst] = ((p >> 12) ) * as;
1964 }
1965 }
1966 else {
1967 const GLushort *ussrc = (const GLushort *) src;
1968 GLuint i;
1969 for (i = 0; i < n; i ++) {
1970 GLushort p = ussrc[i];
1971 rgba[i][rDst] = ((p ) & 0xf) * rs;
1972 rgba[i][gDst] = ((p >> 4) & 0xf) * gs;
1973 rgba[i][bDst] = ((p >> 8) & 0xf) * bs;
1974 rgba[i][aDst] = ((p >> 12) ) * as;
1975 }
1976 }
1977 break;
1978 case GL_UNSIGNED_SHORT_5_5_5_1:
1979 if (!intFormat) {
1980 rs = gs = bs = 1.0F / 31.0F;
1981 }
1982 if (swapBytes) {
1983 const GLushort *ussrc = (const GLushort *) src;
1984 GLuint i;
1985 for (i = 0; i < n; i ++) {
1986 GLushort p = ussrc[i];
1987 SWAP2BYTE(p);
1988 rgba[i][rDst] = ((p >> 11) ) * rs;
1989 rgba[i][gDst] = ((p >> 6) & 0x1f) * gs;
1990 rgba[i][bDst] = ((p >> 1) & 0x1f) * bs;
1991 rgba[i][aDst] = ((p ) & 0x1) * as;
1992 }
1993 }
1994 else {
1995 const GLushort *ussrc = (const GLushort *) src;
1996 GLuint i;
1997 for (i = 0; i < n; i ++) {
1998 GLushort p = ussrc[i];
1999 rgba[i][rDst] = ((p >> 11) ) * rs;
2000 rgba[i][gDst] = ((p >> 6) & 0x1f) * gs;
2001 rgba[i][bDst] = ((p >> 1) & 0x1f) * bs;
2002 rgba[i][aDst] = ((p ) & 0x1) * as;
2003 }
2004 }
2005 break;
2006 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2007 if (!intFormat) {
2008 rs = gs = bs = 1.0F / 31.0F;
2009 }
2010 if (swapBytes) {
2011 const GLushort *ussrc = (const GLushort *) src;
2012 GLuint i;
2013 for (i = 0; i < n; i ++) {
2014 GLushort p = ussrc[i];
2015 SWAP2BYTE(p);
2016 rgba[i][rDst] = ((p ) & 0x1f) * rs;
2017 rgba[i][gDst] = ((p >> 5) & 0x1f) * gs;
2018 rgba[i][bDst] = ((p >> 10) & 0x1f) * bs;
2019 rgba[i][aDst] = ((p >> 15) ) * as;
2020 }
2021 }
2022 else {
2023 const GLushort *ussrc = (const GLushort *) src;
2024 GLuint i;
2025 for (i = 0; i < n; i ++) {
2026 GLushort p = ussrc[i];
2027 rgba[i][rDst] = ((p ) & 0x1f) * rs;
2028 rgba[i][gDst] = ((p >> 5) & 0x1f) * gs;
2029 rgba[i][bDst] = ((p >> 10) & 0x1f) * bs;
2030 rgba[i][aDst] = ((p >> 15) ) * as;
2031 }
2032 }
2033 break;
2034 case GL_UNSIGNED_INT_8_8_8_8:
2035 if (swapBytes) {
2036 const GLuint *uisrc = (const GLuint *) src;
2037 GLuint i;
2038 if (intFormat) {
2039 for (i = 0; i < n; i ++) {
2040 GLuint p = uisrc[i];
2041 rgba[i][rDst] = (GLfloat) ((p ) & 0xff);
2042 rgba[i][gDst] = (GLfloat) ((p >> 8) & 0xff);
2043 rgba[i][bDst] = (GLfloat) ((p >> 16) & 0xff);
2044 rgba[i][aDst] = (GLfloat) ((p >> 24) );
2045 }
2046 }
2047 else {
2048 for (i = 0; i < n; i ++) {
2049 GLuint p = uisrc[i];
2050 rgba[i][rDst] = UBYTE_TO_FLOAT((p ) & 0xff);
2051 rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
2052 rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2053 rgba[i][aDst] = UBYTE_TO_FLOAT((p >> 24) );
2054 }
2055 }
2056 }
2057 else {
2058 const GLuint *uisrc = (const GLuint *) src;
2059 GLuint i;
2060 if (intFormat) {
2061 for (i = 0; i < n; i ++) {
2062 GLuint p = uisrc[i];
2063 rgba[i][rDst] = (GLfloat) ((p >> 24) );
2064 rgba[i][gDst] = (GLfloat) ((p >> 16) & 0xff);
2065 rgba[i][bDst] = (GLfloat) ((p >> 8) & 0xff);
2066 rgba[i][aDst] = (GLfloat) ((p ) & 0xff);
2067 }
2068 }
2069 else {
2070 for (i = 0; i < n; i ++) {
2071 GLuint p = uisrc[i];
2072 rgba[i][rDst] = UBYTE_TO_FLOAT((p >> 24) );
2073 rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2074 rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
2075 rgba[i][aDst] = UBYTE_TO_FLOAT((p ) & 0xff);
2076 }
2077 }
2078 }
2079 break;
2080 case GL_UNSIGNED_INT_8_8_8_8_REV:
2081 if (swapBytes) {
2082 const GLuint *uisrc = (const GLuint *) src;
2083 GLuint i;
2084 if (intFormat) {
2085 for (i = 0; i < n; i ++) {
2086 GLuint p = uisrc[i];
2087 rgba[i][rDst] = (GLfloat) ((p >> 24) );
2088 rgba[i][gDst] = (GLfloat) ((p >> 16) & 0xff);
2089 rgba[i][bDst] = (GLfloat) ((p >> 8) & 0xff);
2090 rgba[i][aDst] = (GLfloat) ((p ) & 0xff);
2091 }
2092 }
2093 else {
2094 for (i = 0; i < n; i ++) {
2095 GLuint p = uisrc[i];
2096 rgba[i][rDst] = UBYTE_TO_FLOAT((p >> 24) );
2097 rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2098 rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
2099 rgba[i][aDst] = UBYTE_TO_FLOAT((p ) & 0xff);
2100 }
2101 }
2102 }
2103 else {
2104 const GLuint *uisrc = (const GLuint *) src;
2105 GLuint i;
2106 if (intFormat) {
2107 for (i = 0; i < n; i ++) {
2108 GLuint p = uisrc[i];
2109 rgba[i][rDst] = (GLfloat) ((p ) & 0xff);
2110 rgba[i][gDst] = (GLfloat) ((p >> 8) & 0xff);
2111 rgba[i][bDst] = (GLfloat) ((p >> 16) & 0xff);
2112 rgba[i][aDst] = (GLfloat) ((p >> 24) );
2113 }
2114 }
2115 else {
2116 for (i = 0; i < n; i ++) {
2117 GLuint p = uisrc[i];
2118 rgba[i][rDst] = UBYTE_TO_FLOAT((p ) & 0xff);
2119 rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
2120 rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2121 rgba[i][aDst] = UBYTE_TO_FLOAT((p >> 24) );
2122 }
2123 }
2124 }
2125 break;
2126 case GL_UNSIGNED_INT_10_10_10_2:
2127 if (!intFormat) {
2128 rs = 1.0F / 1023.0F;
2129 gs = 1.0F / 1023.0F;
2130 bs = 1.0F / 1023.0F;
2131 as = 1.0F / 3.0F;
2132 }
2133 if (swapBytes) {
2134 const GLuint *uisrc = (const GLuint *) src;
2135 GLuint i;
2136 for (i = 0; i < n; i ++) {
2137 GLuint p = uisrc[i];
2138 SWAP4BYTE(p);
2139 rgba[i][rDst] = ((p >> 22) ) * rs;
2140 rgba[i][gDst] = ((p >> 12) & 0x3ff) * gs;
2141 rgba[i][bDst] = ((p >> 2) & 0x3ff) * bs;
2142 rgba[i][aDst] = ((p ) & 0x3 ) * as;
2143 }
2144 }
2145 else {
2146 const GLuint *uisrc = (const GLuint *) src;
2147 GLuint i;
2148 for (i = 0; i < n; i ++) {
2149 GLuint p = uisrc[i];
2150 rgba[i][rDst] = ((p >> 22) ) * rs;
2151 rgba[i][gDst] = ((p >> 12) & 0x3ff) * gs;
2152 rgba[i][bDst] = ((p >> 2) & 0x3ff) * bs;
2153 rgba[i][aDst] = ((p ) & 0x3 ) * as;
2154 }
2155 }
2156 break;
2157 case GL_UNSIGNED_INT_2_10_10_10_REV:
2158 if (!intFormat) {
2159 rs = 1.0F / 1023.0F;
2160 gs = 1.0F / 1023.0F;
2161 bs = 1.0F / 1023.0F;
2162 as = 1.0F / 3.0F;
2163 }
2164 if (swapBytes) {
2165 const GLuint *uisrc = (const GLuint *) src;
2166 GLuint i;
2167 for (i = 0; i < n; i ++) {
2168 GLuint p = uisrc[i];
2169 SWAP4BYTE(p);
2170 rgba[i][rDst] = ((p ) & 0x3ff) * rs;
2171 rgba[i][gDst] = ((p >> 10) & 0x3ff) * gs;
2172 rgba[i][bDst] = ((p >> 20) & 0x3ff) * bs;
2173 if (aSrc < 0) {
2174 rgba[i][aDst] = 1.0F;
2175 } else {
2176 rgba[i][aDst] = (p >> 30) * as;
2177 }
2178 }
2179 }
2180 else {
2181 const GLuint *uisrc = (const GLuint *) src;
2182 GLuint i;
2183 for (i = 0; i < n; i ++) {
2184 GLuint p = uisrc[i];
2185 rgba[i][rDst] = ((p ) & 0x3ff) * rs;
2186 rgba[i][gDst] = ((p >> 10) & 0x3ff) * gs;
2187 rgba[i][bDst] = ((p >> 20) & 0x3ff) * bs;
2188 if (aSrc < 0) {
2189 rgba[i][aDst] = 1.0F;
2190 } else {
2191 rgba[i][aDst] = (p >> 30) * as;
2192 }
2193 }
2194 }
2195 break;
2196 case GL_UNSIGNED_INT_5_9_9_9_REV:
2197 if (swapBytes) {
2198 const GLuint *uisrc = (const GLuint *) src;
2199 GLuint i;
2200 GLfloat f[3];
2201 for (i = 0; i < n; i ++) {
2202 GLuint p = uisrc[i];
2203 SWAP4BYTE(p);
2204 rgb9e5_to_float3(p, f);
2205 rgba[i][rDst] = f[0];
2206 rgba[i][gDst] = f[1];
2207 rgba[i][bDst] = f[2];
2208 rgba[i][aDst] = 1.0F;
2209 }
2210 }
2211 else {
2212 const GLuint *uisrc = (const GLuint *) src;
2213 GLuint i;
2214 GLfloat f[3];
2215 for (i = 0; i < n; i ++) {
2216 rgb9e5_to_float3(uisrc[i], f);
2217 rgba[i][rDst] = f[0];
2218 rgba[i][gDst] = f[1];
2219 rgba[i][bDst] = f[2];
2220 rgba[i][aDst] = 1.0F;
2221 }
2222 }
2223 break;
2224 case GL_UNSIGNED_INT_10F_11F_11F_REV:
2225 if (swapBytes) {
2226 const GLuint *uisrc = (const GLuint *) src;
2227 GLuint i;
2228 GLfloat f[3];
2229 for (i = 0; i < n; i ++) {
2230 GLuint p = uisrc[i];
2231 SWAP4BYTE(p);
2232 r11g11b10f_to_float3(p, f);
2233 rgba[i][rDst] = f[0];
2234 rgba[i][gDst] = f[1];
2235 rgba[i][bDst] = f[2];
2236 rgba[i][aDst] = 1.0F;
2237 }
2238 }
2239 else {
2240 const GLuint *uisrc = (const GLuint *) src;
2241 GLuint i;
2242 GLfloat f[3];
2243 for (i = 0; i < n; i ++) {
2244 r11g11b10f_to_float3(uisrc[i], f);
2245 rgba[i][rDst] = f[0];
2246 rgba[i][gDst] = f[1];
2247 rgba[i][bDst] = f[2];
2248 rgba[i][aDst] = 1.0F;
2249 }
2250 }
2251 break;
2252 default:
2253 _mesa_problem(NULL, "bad srcType in extract float data");
2254 break;
2255 }
2256 #undef PROCESS
2257 }
2258
2259
2260 static inline GLuint
2261 clamp_float_to_uint(GLfloat f)
2262 {
2263 return f < 0.0F ? 0 : F_TO_I(f);
2264 }
2265
2266
2267 static inline GLuint
2268 clamp_half_to_uint(GLhalfARB h)
2269 {
2270 GLfloat f = _mesa_half_to_float(h);
2271 return f < 0.0F ? 0 : F_TO_I(f);
2272 }
2273
2274
2275 /**
2276 * \sa extract_float_rgba()
2277 */
2278 static void
2279 extract_uint_rgba(GLuint n, GLuint rgba[][4],
2280 GLenum srcFormat, GLenum srcType, const GLvoid *src,
2281 GLboolean swapBytes)
2282 {
2283 GLint rSrc, gSrc, bSrc, aSrc;
2284 GLint stride;
2285 GLint rDst, bDst, gDst, aDst;
2286
2287 ASSERT(srcFormat == GL_RED ||
2288 srcFormat == GL_GREEN ||
2289 srcFormat == GL_BLUE ||
2290 srcFormat == GL_ALPHA ||
2291 srcFormat == GL_LUMINANCE ||
2292 srcFormat == GL_LUMINANCE_ALPHA ||
2293 srcFormat == GL_INTENSITY ||
2294 srcFormat == GL_RG ||
2295 srcFormat == GL_RGB ||
2296 srcFormat == GL_BGR ||
2297 srcFormat == GL_RGBA ||
2298 srcFormat == GL_BGRA ||
2299 srcFormat == GL_ABGR_EXT ||
2300 srcFormat == GL_RED_INTEGER_EXT ||
2301 srcFormat == GL_RG_INTEGER ||
2302 srcFormat == GL_GREEN_INTEGER_EXT ||
2303 srcFormat == GL_BLUE_INTEGER_EXT ||
2304 srcFormat == GL_ALPHA_INTEGER_EXT ||
2305 srcFormat == GL_RGB_INTEGER_EXT ||
2306 srcFormat == GL_RGBA_INTEGER_EXT ||
2307 srcFormat == GL_BGR_INTEGER_EXT ||
2308 srcFormat == GL_BGRA_INTEGER_EXT ||
2309 srcFormat == GL_LUMINANCE_INTEGER_EXT ||
2310 srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
2311
2312 ASSERT(srcType == GL_UNSIGNED_BYTE ||
2313 srcType == GL_BYTE ||
2314 srcType == GL_UNSIGNED_SHORT ||
2315 srcType == GL_SHORT ||
2316 srcType == GL_UNSIGNED_INT ||
2317 srcType == GL_INT ||
2318 srcType == GL_HALF_FLOAT_ARB ||
2319 srcType == GL_FLOAT ||
2320 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2321 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2322 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2323 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2324 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2325 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2326 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2327 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2328 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2329 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2330 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2331 srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
2332 srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
2333 srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
2334
2335 get_component_mapping(srcFormat,
2336 &rSrc, &gSrc, &bSrc, &aSrc,
2337 &rDst, &gDst, &bDst, &aDst);
2338
2339 stride = _mesa_components_in_format(srcFormat);
2340
2341 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT, TYPE, CONVERSION) \
2342 if ((SRC_INDEX) < 0) { \
2343 GLuint i; \
2344 for (i = 0; i < n; i++) { \
2345 rgba[i][DST_INDEX] = DEFAULT; \
2346 } \
2347 } \
2348 else if (swapBytes) { \
2349 const TYPE *s = (const TYPE *) src; \
2350 GLuint i; \
2351 for (i = 0; i < n; i++) { \
2352 TYPE value = s[SRC_INDEX]; \
2353 if (sizeof(TYPE) == 2) { \
2354 SWAP2BYTE(value); \
2355 } \
2356 else if (sizeof(TYPE) == 4) { \
2357 SWAP4BYTE(value); \
2358 } \
2359 rgba[i][DST_INDEX] = CONVERSION(value); \
2360 s += stride; \
2361 } \
2362 } \
2363 else { \
2364 const TYPE *s = (const TYPE *) src; \
2365 GLuint i; \
2366 for (i = 0; i < n; i++) { \
2367 rgba[i][DST_INDEX] = CONVERSION(s[SRC_INDEX]); \
2368 s += stride; \
2369 } \
2370 }
2371
2372 switch (srcType) {
2373 case GL_UNSIGNED_BYTE:
2374 PROCESS(rSrc, RCOMP, 0, GLubyte, (GLuint));
2375 PROCESS(gSrc, GCOMP, 0, GLubyte, (GLuint));
2376 PROCESS(bSrc, BCOMP, 0, GLubyte, (GLuint));
2377 PROCESS(aSrc, ACOMP, 1, GLubyte, (GLuint));
2378 break;
2379 case GL_BYTE:
2380 PROCESS(rSrc, RCOMP, 0, GLbyte, (GLuint));
2381 PROCESS(gSrc, GCOMP, 0, GLbyte, (GLuint));
2382 PROCESS(bSrc, BCOMP, 0, GLbyte, (GLuint));
2383 PROCESS(aSrc, ACOMP, 1, GLbyte, (GLuint));
2384 break;
2385 case GL_UNSIGNED_SHORT:
2386 PROCESS(rSrc, RCOMP, 0, GLushort, (GLuint));
2387 PROCESS(gSrc, GCOMP, 0, GLushort, (GLuint));
2388 PROCESS(bSrc, BCOMP, 0, GLushort, (GLuint));
2389 PROCESS(aSrc, ACOMP, 1, GLushort, (GLuint));
2390 break;
2391 case GL_SHORT:
2392 PROCESS(rSrc, RCOMP, 0, GLshort, (GLuint));
2393 PROCESS(gSrc, GCOMP, 0, GLshort, (GLuint));
2394 PROCESS(bSrc, BCOMP, 0, GLshort, (GLuint));
2395 PROCESS(aSrc, ACOMP, 1, GLshort, (GLuint));
2396 break;
2397 case GL_UNSIGNED_INT:
2398 PROCESS(rSrc, RCOMP, 0, GLuint, (GLuint));
2399 PROCESS(gSrc, GCOMP, 0, GLuint, (GLuint));
2400 PROCESS(bSrc, BCOMP, 0, GLuint, (GLuint));
2401 PROCESS(aSrc, ACOMP, 1, GLuint, (GLuint));
2402 break;
2403 case GL_INT:
2404 PROCESS(rSrc, RCOMP, 0, GLint, (GLuint));
2405 PROCESS(gSrc, GCOMP, 0, GLint, (GLuint));
2406 PROCESS(bSrc, BCOMP, 0, GLint, (GLuint));
2407 PROCESS(aSrc, ACOMP, 1, GLint, (GLuint));
2408 break;
2409 case GL_FLOAT:
2410 PROCESS(rSrc, RCOMP, 0, GLfloat, clamp_float_to_uint);
2411 PROCESS(gSrc, GCOMP, 0, GLfloat, clamp_float_to_uint);
2412 PROCESS(bSrc, BCOMP, 0, GLfloat, clamp_float_to_uint);
2413 PROCESS(aSrc, ACOMP, 1, GLfloat, clamp_float_to_uint);
2414 break;
2415 case GL_HALF_FLOAT_ARB:
2416 PROCESS(rSrc, RCOMP, 0, GLhalfARB, clamp_half_to_uint);
2417 PROCESS(gSrc, GCOMP, 0, GLhalfARB, clamp_half_to_uint);
2418 PROCESS(bSrc, BCOMP, 0, GLhalfARB, clamp_half_to_uint);
2419 PROCESS(aSrc, ACOMP, 1, GLhalfARB, clamp_half_to_uint);
2420 break;
2421 case GL_UNSIGNED_BYTE_3_3_2:
2422 {
2423 const GLubyte *ubsrc = (const GLubyte *) src;
2424 GLuint i;
2425 for (i = 0; i < n; i ++) {
2426 GLubyte p = ubsrc[i];
2427 rgba[i][rDst] = ((p >> 5) );
2428 rgba[i][gDst] = ((p >> 2) & 0x7);
2429 rgba[i][bDst] = ((p ) & 0x3);
2430 rgba[i][aDst] = 1;
2431 }
2432 }
2433 break;
2434 case GL_UNSIGNED_BYTE_2_3_3_REV:
2435 {
2436 const GLubyte *ubsrc = (const GLubyte *) src;
2437 GLuint i;
2438 for (i = 0; i < n; i ++) {
2439 GLubyte p = ubsrc[i];
2440 rgba[i][rDst] = ((p ) & 0x7);
2441 rgba[i][gDst] = ((p >> 3) & 0x7);
2442 rgba[i][bDst] = ((p >> 6) );
2443 rgba[i][aDst] = 1;
2444 }
2445 }
2446 break;
2447 case GL_UNSIGNED_SHORT_5_6_5:
2448 if (swapBytes) {
2449 const GLushort *ussrc = (const GLushort *) src;
2450 GLuint i;
2451 for (i = 0; i < n; i ++) {
2452 GLushort p = ussrc[i];
2453 SWAP2BYTE(p);
2454 rgba[i][rDst] = ((p >> 11) );
2455 rgba[i][gDst] = ((p >> 5) & 0x3f);
2456 rgba[i][bDst] = ((p ) & 0x1f);
2457 rgba[i][aDst] = 1;
2458 }
2459 }
2460 else {
2461 const GLushort *ussrc = (const GLushort *) src;
2462 GLuint i;
2463 for (i = 0; i < n; i ++) {
2464 GLushort p = ussrc[i];
2465 rgba[i][rDst] = ((p >> 11) );
2466 rgba[i][gDst] = ((p >> 5) & 0x3f);
2467 rgba[i][bDst] = ((p ) & 0x1f);
2468 rgba[i][aDst] = 1;
2469 }
2470 }
2471 break;
2472 case GL_UNSIGNED_SHORT_5_6_5_REV:
2473 if (swapBytes) {
2474 const GLushort *ussrc = (const GLushort *) src;
2475 GLuint i;
2476 for (i = 0; i < n; i ++) {
2477 GLushort p = ussrc[i];
2478 SWAP2BYTE(p);
2479 rgba[i][rDst] = ((p ) & 0x1f);
2480 rgba[i][gDst] = ((p >> 5) & 0x3f);
2481 rgba[i][bDst] = ((p >> 11) );
2482 rgba[i][aDst] = 1;
2483 }
2484 }
2485 else {
2486 const GLushort *ussrc = (const GLushort *) src;
2487 GLuint i;
2488 for (i = 0; i < n; i ++) {
2489 GLushort p = ussrc[i];
2490 rgba[i][rDst] = ((p ) & 0x1f);
2491 rgba[i][gDst] = ((p >> 5) & 0x3f);
2492 rgba[i][bDst] = ((p >> 11) );
2493 rgba[i][aDst] = 1;
2494 }
2495 }
2496 break;
2497 case GL_UNSIGNED_SHORT_4_4_4_4:
2498 if (swapBytes) {
2499 const GLushort *ussrc = (const GLushort *) src;
2500 GLuint i;
2501 for (i = 0; i < n; i ++) {
2502 GLushort p = ussrc[i];
2503 SWAP2BYTE(p);
2504 rgba[i][rDst] = ((p >> 12) );
2505 rgba[i][gDst] = ((p >> 8) & 0xf);
2506 rgba[i][bDst] = ((p >> 4) & 0xf);
2507 rgba[i][aDst] = ((p ) & 0xf);
2508 }
2509 }
2510 else {
2511 const GLushort *ussrc = (const GLushort *) src;
2512 GLuint i;
2513 for (i = 0; i < n; i ++) {
2514 GLushort p = ussrc[i];
2515 rgba[i][rDst] = ((p >> 12) );
2516 rgba[i][gDst] = ((p >> 8) & 0xf);
2517 rgba[i][bDst] = ((p >> 4) & 0xf);
2518 rgba[i][aDst] = ((p ) & 0xf);
2519 }
2520 }
2521 break;
2522 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2523 if (swapBytes) {
2524 const GLushort *ussrc = (const GLushort *) src;
2525 GLuint i;
2526 for (i = 0; i < n; i ++) {
2527 GLushort p = ussrc[i];
2528 SWAP2BYTE(p);
2529 rgba[i][rDst] = ((p ) & 0xf);
2530 rgba[i][gDst] = ((p >> 4) & 0xf);
2531 rgba[i][bDst] = ((p >> 8) & 0xf);
2532 rgba[i][aDst] = ((p >> 12) );
2533 }
2534 }
2535 else {
2536 const GLushort *ussrc = (const GLushort *) src;
2537 GLuint i;
2538 for (i = 0; i < n; i ++) {
2539 GLushort p = ussrc[i];
2540 rgba[i][rDst] = ((p ) & 0xf);
2541 rgba[i][gDst] = ((p >> 4) & 0xf);
2542 rgba[i][bDst] = ((p >> 8) & 0xf);
2543 rgba[i][aDst] = ((p >> 12) );
2544 }
2545 }
2546 break;
2547 case GL_UNSIGNED_SHORT_5_5_5_1:
2548 if (swapBytes) {
2549 const GLushort *ussrc = (const GLushort *) src;
2550 GLuint i;
2551 for (i = 0; i < n; i ++) {
2552 GLushort p = ussrc[i];
2553 SWAP2BYTE(p);
2554 rgba[i][rDst] = ((p >> 11) );
2555 rgba[i][gDst] = ((p >> 6) & 0x1f);
2556 rgba[i][bDst] = ((p >> 1) & 0x1f);
2557 rgba[i][aDst] = ((p ) & 0x1 );
2558 }
2559 }
2560 else {
2561 const GLushort *ussrc = (const GLushort *) src;
2562 GLuint i;
2563 for (i = 0; i < n; i ++) {
2564 GLushort p = ussrc[i];
2565 rgba[i][rDst] = ((p >> 11) );
2566 rgba[i][gDst] = ((p >> 6) & 0x1f);
2567 rgba[i][bDst] = ((p >> 1) & 0x1f);
2568 rgba[i][aDst] = ((p ) & 0x1 );
2569 }
2570 }
2571 break;
2572 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2573 if (swapBytes) {
2574 const GLushort *ussrc = (const GLushort *) src;
2575 GLuint i;
2576 for (i = 0; i < n; i ++) {
2577 GLushort p = ussrc[i];
2578 SWAP2BYTE(p);
2579 rgba[i][rDst] = ((p ) & 0x1f);
2580 rgba[i][gDst] = ((p >> 5) & 0x1f);
2581 rgba[i][bDst] = ((p >> 10) & 0x1f);
2582 rgba[i][aDst] = ((p >> 15) );
2583 }
2584 }
2585 else {
2586 const GLushort *ussrc = (const GLushort *) src;
2587 GLuint i;
2588 for (i = 0; i < n; i ++) {
2589 GLushort p = ussrc[i];
2590 rgba[i][rDst] = ((p ) & 0x1f);
2591 rgba[i][gDst] = ((p >> 5) & 0x1f);
2592 rgba[i][bDst] = ((p >> 10) & 0x1f);
2593 rgba[i][aDst] = ((p >> 15) );
2594 }
2595 }
2596 break;
2597 case GL_UNSIGNED_INT_8_8_8_8:
2598 if (swapBytes) {
2599 const GLuint *uisrc = (const GLuint *) src;
2600 GLuint i;
2601 for (i = 0; i < n; i ++) {
2602 GLuint p = uisrc[i];
2603 rgba[i][rDst] = ((p ) & 0xff);
2604 rgba[i][gDst] = ((p >> 8) & 0xff);
2605 rgba[i][bDst] = ((p >> 16) & 0xff);
2606 rgba[i][aDst] = ((p >> 24) );
2607 }
2608 }
2609 else {
2610 const GLuint *uisrc = (const GLuint *) src;
2611 GLuint i;
2612 for (i = 0; i < n; i ++) {
2613 GLuint p = uisrc[i];
2614 rgba[i][rDst] = ((p >> 24) );
2615 rgba[i][gDst] = ((p >> 16) & 0xff);
2616 rgba[i][bDst] = ((p >> 8) & 0xff);
2617 rgba[i][aDst] = ((p ) & 0xff);
2618 }
2619 }
2620 break;
2621 case GL_UNSIGNED_INT_8_8_8_8_REV:
2622 if (swapBytes) {
2623 const GLuint *uisrc = (const GLuint *) src;
2624 GLuint i;
2625 for (i = 0; i < n; i ++) {
2626 GLuint p = uisrc[i];
2627 rgba[i][rDst] = ((p >> 24) );
2628 rgba[i][gDst] = ((p >> 16) & 0xff);
2629 rgba[i][bDst] = ((p >> 8) & 0xff);
2630 rgba[i][aDst] = ((p ) & 0xff);
2631 }
2632 }
2633 else {
2634 const GLuint *uisrc = (const GLuint *) src;
2635 GLuint i;
2636 for (i = 0; i < n; i ++) {
2637 GLuint p = uisrc[i];
2638 rgba[i][rDst] = ((p ) & 0xff);
2639 rgba[i][gDst] = ((p >> 8) & 0xff);
2640 rgba[i][bDst] = ((p >> 16) & 0xff);
2641 rgba[i][aDst] = ((p >> 24) );
2642 }
2643 }
2644 break;
2645 case GL_UNSIGNED_INT_10_10_10_2:
2646 if (swapBytes) {
2647 const GLuint *uisrc = (const GLuint *) src;
2648 GLuint i;
2649 for (i = 0; i < n; i ++) {
2650 GLuint p = uisrc[i];
2651 SWAP4BYTE(p);
2652 rgba[i][rDst] = ((p >> 22) );
2653 rgba[i][gDst] = ((p >> 12) & 0x3ff);
2654 rgba[i][bDst] = ((p >> 2) & 0x3ff);
2655 rgba[i][aDst] = ((p ) & 0x3 );
2656 }
2657 }
2658 else {
2659 const GLuint *uisrc = (const GLuint *) src;
2660 GLuint i;
2661 for (i = 0; i < n; i ++) {
2662 GLuint p = uisrc[i];
2663 rgba[i][rDst] = ((p >> 22) );
2664 rgba[i][gDst] = ((p >> 12) & 0x3ff);
2665 rgba[i][bDst] = ((p >> 2) & 0x3ff);
2666 rgba[i][aDst] = ((p ) & 0x3 );
2667 }
2668 }
2669 break;
2670 case GL_UNSIGNED_INT_2_10_10_10_REV:
2671 if (swapBytes) {
2672 const GLuint *uisrc = (const GLuint *) src;
2673 GLuint i;
2674 for (i = 0; i < n; i ++) {
2675 GLuint p = uisrc[i];
2676 SWAP4BYTE(p);
2677 rgba[i][rDst] = ((p ) & 0x3ff);
2678 rgba[i][gDst] = ((p >> 10) & 0x3ff);
2679 rgba[i][bDst] = ((p >> 20) & 0x3ff);
2680 rgba[i][aDst] = ((p >> 30) );
2681 }
2682 }
2683 else {
2684 const GLuint *uisrc = (const GLuint *) src;
2685 GLuint i;
2686 for (i = 0; i < n; i ++) {
2687 GLuint p = uisrc[i];
2688 rgba[i][rDst] = ((p ) & 0x3ff);
2689 rgba[i][gDst] = ((p >> 10) & 0x3ff);
2690 rgba[i][bDst] = ((p >> 20) & 0x3ff);
2691 rgba[i][aDst] = ((p >> 30) );
2692 }
2693 }
2694 break;
2695 case GL_UNSIGNED_INT_5_9_9_9_REV:
2696 if (swapBytes) {
2697 const GLuint *uisrc = (const GLuint *) src;
2698 GLuint i;
2699 float f[3];
2700 for (i = 0; i < n; i ++) {
2701 GLuint p = uisrc[i];
2702 SWAP4BYTE(p);
2703 rgb9e5_to_float3(p, f);
2704 rgba[i][rDst] = clamp_float_to_uint(f[0]);
2705 rgba[i][gDst] = clamp_float_to_uint(f[1]);
2706 rgba[i][bDst] = clamp_float_to_uint(f[2]);
2707 rgba[i][aDst] = 1;
2708 }
2709 }
2710 else {
2711 const GLuint *uisrc = (const GLuint *) src;
2712 GLuint i;
2713 float f[3];
2714 for (i = 0; i < n; i ++) {
2715 GLuint p = uisrc[i];
2716 rgb9e5_to_float3(p, f);
2717 rgba[i][rDst] = clamp_float_to_uint(f[0]);
2718 rgba[i][gDst] = clamp_float_to_uint(f[1]);
2719 rgba[i][bDst] = clamp_float_to_uint(f[2]);
2720 rgba[i][aDst] = 1;
2721 }
2722 }
2723 break;
2724 case GL_UNSIGNED_INT_10F_11F_11F_REV:
2725 if (swapBytes) {
2726 const GLuint *uisrc = (const GLuint *) src;
2727 GLuint i;
2728 float f[3];
2729 for (i = 0; i < n; i ++) {
2730 GLuint p = uisrc[i];
2731 SWAP4BYTE(p);
2732 r11g11b10f_to_float3(p, f);
2733 rgba[i][rDst] = clamp_float_to_uint(f[0]);
2734 rgba[i][gDst] = clamp_float_to_uint(f[1]);
2735 rgba[i][bDst] = clamp_float_to_uint(f[2]);
2736 rgba[i][aDst] = 1;
2737 }
2738 }
2739 else {
2740 const GLuint *uisrc = (const GLuint *) src;
2741 GLuint i;
2742 float f[3];
2743 for (i = 0; i < n; i ++) {
2744 GLuint p = uisrc[i];
2745 r11g11b10f_to_float3(p, f);
2746 rgba[i][rDst] = clamp_float_to_uint(f[0]);
2747 rgba[i][gDst] = clamp_float_to_uint(f[1]);
2748 rgba[i][bDst] = clamp_float_to_uint(f[2]);
2749 rgba[i][aDst] = 1;
2750 }
2751 }
2752 break;
2753 default:
2754 _mesa_problem(NULL, "bad srcType in extract uint data");
2755 break;
2756 }
2757 #undef PROCESS
2758 }
2759
2760
2761
2762 /*
2763 * Unpack a row of color image data from a client buffer according to
2764 * the pixel unpacking parameters.
2765 * Return GLubyte values in the specified dest image format.
2766 * This is used by glDrawPixels and glTexImage?D().
2767 * \param ctx - the context
2768 * n - number of pixels in the span
2769 * dstFormat - format of destination color array
2770 * dest - the destination color array
2771 * srcFormat - source image format
2772 * srcType - source image data type
2773 * source - source image pointer
2774 * srcPacking - pixel unpacking parameters
2775 * transferOps - bitmask of IMAGE_*_BIT values of operations to apply
2776 *
2777 * XXX perhaps expand this to process whole images someday.
2778 */
2779 void
2780 _mesa_unpack_color_span_ubyte(struct gl_context *ctx,
2781 GLuint n, GLenum dstFormat, GLubyte dest[],
2782 GLenum srcFormat, GLenum srcType,
2783 const GLvoid *source,
2784 const struct gl_pixelstore_attrib *srcPacking,
2785 GLbitfield transferOps )
2786 {
2787 GLboolean intFormat = _mesa_is_enum_format_integer(srcFormat);
2788 ASSERT(dstFormat == GL_ALPHA ||
2789 dstFormat == GL_LUMINANCE ||
2790 dstFormat == GL_LUMINANCE_ALPHA ||
2791 dstFormat == GL_INTENSITY ||
2792 dstFormat == GL_RED ||
2793 dstFormat == GL_RG ||
2794 dstFormat == GL_RGB ||
2795 dstFormat == GL_RGBA);
2796
2797 ASSERT(srcFormat == GL_RED ||
2798 srcFormat == GL_GREEN ||
2799 srcFormat == GL_BLUE ||
2800 srcFormat == GL_ALPHA ||
2801 srcFormat == GL_LUMINANCE ||
2802 srcFormat == GL_LUMINANCE_ALPHA ||
2803 srcFormat == GL_INTENSITY ||
2804 srcFormat == GL_RG ||
2805 srcFormat == GL_RGB ||
2806 srcFormat == GL_BGR ||
2807 srcFormat == GL_RGBA ||
2808 srcFormat == GL_BGRA ||
2809 srcFormat == GL_ABGR_EXT ||
2810 srcFormat == GL_COLOR_INDEX);
2811
2812 ASSERT(srcType == GL_BITMAP ||
2813 srcType == GL_UNSIGNED_BYTE ||
2814 srcType == GL_BYTE ||
2815 srcType == GL_UNSIGNED_SHORT ||
2816 srcType == GL_SHORT ||
2817 srcType == GL_UNSIGNED_INT ||
2818 srcType == GL_INT ||
2819 srcType == GL_HALF_FLOAT_ARB ||
2820 srcType == GL_FLOAT ||
2821 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2822 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2823 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2824 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2825 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2826 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2827 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2828 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2829 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2830 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2831 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2832 srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
2833 srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
2834 srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
2835
2836 /* EXT_texture_integer specifies no transfer ops on integer
2837 * types in the resolved issues section. Just set them to 0
2838 * for integer surfaces.
2839 */
2840 if (intFormat)
2841 transferOps = 0;
2842
2843 /* Try simple cases first */
2844 if (transferOps == 0) {
2845 if (srcType == GL_UNSIGNED_BYTE) {
2846 if (dstFormat == GL_RGBA) {
2847 if (srcFormat == GL_RGBA) {
2848 memcpy( dest, source, n * 4 * sizeof(GLubyte) );
2849 return;
2850 }
2851 else if (srcFormat == GL_RGB) {
2852 GLuint i;
2853 const GLubyte *src = (const GLubyte *) source;
2854 GLubyte *dst = dest;
2855 for (i = 0; i < n; i++) {
2856 dst[0] = src[0];
2857 dst[1] = src[1];
2858 dst[2] = src[2];
2859 dst[3] = 255;
2860 src += 3;
2861 dst += 4;
2862 }
2863 return;
2864 }
2865 }
2866 else if (dstFormat == GL_RGB) {
2867 if (srcFormat == GL_RGB) {
2868 memcpy( dest, source, n * 3 * sizeof(GLubyte) );
2869 return;
2870 }
2871 else if (srcFormat == GL_RGBA) {
2872 GLuint i;
2873 const GLubyte *src = (const GLubyte *) source;
2874 GLubyte *dst = dest;
2875 for (i = 0; i < n; i++) {
2876 dst[0] = src[0];
2877 dst[1] = src[1];
2878 dst[2] = src[2];
2879 src += 4;
2880 dst += 3;
2881 }
2882 return;
2883 }
2884 }
2885 else if (dstFormat == srcFormat) {
2886 GLint comps = _mesa_components_in_format(srcFormat);
2887 assert(comps > 0);
2888 memcpy( dest, source, n * comps * sizeof(GLubyte) );
2889 return;
2890 }
2891 }
2892 }
2893
2894
2895 /* general solution begins here */
2896 {
2897 GLint dstComponents;
2898 GLint rDst, gDst, bDst, aDst, lDst, iDst;
2899 GLfloat (*rgba)[4] = malloc(4 * n * sizeof(GLfloat));
2900
2901 if (!rgba) {
2902 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
2903 return;
2904 }
2905
2906 dstComponents = _mesa_components_in_format( dstFormat );
2907 /* source & dest image formats should have been error checked by now */
2908 assert(dstComponents > 0);
2909
2910 /*
2911 * Extract image data and convert to RGBA floats
2912 */
2913 if (srcFormat == GL_COLOR_INDEX) {
2914 GLuint *indexes = malloc(n * sizeof(GLuint));
2915
2916 if (!indexes) {
2917 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
2918 free(rgba);
2919 return;
2920 }
2921
2922 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2923 srcPacking);
2924
2925 /* Convert indexes to RGBA */
2926 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2927 _mesa_shift_and_offset_ci(ctx, n, indexes);
2928 }
2929 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
2930
2931 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
2932 * with color indexes.
2933 */
2934 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
2935
2936 free(indexes);
2937 }
2938 else {
2939 /* non-color index data */
2940 extract_float_rgba(n, rgba, srcFormat, srcType, source,
2941 srcPacking->SwapBytes);
2942 }
2943
2944 /* Need to clamp if returning GLubytes */
2945 transferOps |= IMAGE_CLAMP_BIT;
2946
2947 if (transferOps) {
2948 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
2949 }
2950
2951 get_component_indexes(dstFormat,
2952 &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
2953
2954 /* Now return the GLubyte data in the requested dstFormat */
2955 if (rDst >= 0) {
2956 GLubyte *dst = dest;
2957 GLuint i;
2958 for (i = 0; i < n; i++) {
2959 CLAMPED_FLOAT_TO_UBYTE(dst[rDst], rgba[i][RCOMP]);
2960 dst += dstComponents;
2961 }
2962 }
2963
2964 if (gDst >= 0) {
2965 GLubyte *dst = dest;
2966 GLuint i;
2967 for (i = 0; i < n; i++) {
2968 CLAMPED_FLOAT_TO_UBYTE(dst[gDst], rgba[i][GCOMP]);
2969 dst += dstComponents;
2970 }
2971 }
2972
2973 if (bDst >= 0) {
2974 GLubyte *dst = dest;
2975 GLuint i;
2976 for (i = 0; i < n; i++) {
2977 CLAMPED_FLOAT_TO_UBYTE(dst[bDst], rgba[i][BCOMP]);
2978 dst += dstComponents;
2979 }
2980 }
2981
2982 if (aDst >= 0) {
2983 GLubyte *dst = dest;
2984 GLuint i;
2985 for (i = 0; i < n; i++) {
2986 CLAMPED_FLOAT_TO_UBYTE(dst[aDst], rgba[i][ACOMP]);
2987 dst += dstComponents;
2988 }
2989 }
2990
2991 if (iDst >= 0) {
2992 GLubyte *dst = dest;
2993 GLuint i;
2994 assert(iDst == 0);
2995 assert(dstComponents == 1);
2996 for (i = 0; i < n; i++) {
2997 /* Intensity comes from red channel */
2998 CLAMPED_FLOAT_TO_UBYTE(dst[i], rgba[i][RCOMP]);
2999 }
3000 }
3001
3002 if (lDst >= 0) {
3003 GLubyte *dst = dest;
3004 GLuint i;
3005 assert(lDst == 0);
3006 for (i = 0; i < n; i++) {
3007 /* Luminance comes from red channel */
3008 CLAMPED_FLOAT_TO_UBYTE(dst[0], rgba[i][RCOMP]);
3009 dst += dstComponents;
3010 }
3011 }
3012
3013 free(rgba);
3014 }
3015 }
3016
3017
3018 /**
3019 * Same as _mesa_unpack_color_span_ubyte(), but return GLfloat data
3020 * instead of GLubyte.
3021 */
3022 void
3023 _mesa_unpack_color_span_float( struct gl_context *ctx,
3024 GLuint n, GLenum dstFormat, GLfloat dest[],
3025 GLenum srcFormat, GLenum srcType,
3026 const GLvoid *source,
3027 const struct gl_pixelstore_attrib *srcPacking,
3028 GLbitfield transferOps )
3029 {
3030 ASSERT(dstFormat == GL_ALPHA ||
3031 dstFormat == GL_LUMINANCE ||
3032 dstFormat == GL_LUMINANCE_ALPHA ||
3033 dstFormat == GL_INTENSITY ||
3034 dstFormat == GL_RED ||
3035 dstFormat == GL_RG ||
3036 dstFormat == GL_RGB ||
3037 dstFormat == GL_RGBA);
3038
3039 ASSERT(srcFormat == GL_RED ||
3040 srcFormat == GL_GREEN ||
3041 srcFormat == GL_BLUE ||
3042 srcFormat == GL_ALPHA ||
3043 srcFormat == GL_LUMINANCE ||
3044 srcFormat == GL_LUMINANCE_ALPHA ||
3045 srcFormat == GL_INTENSITY ||
3046 srcFormat == GL_RG ||
3047 srcFormat == GL_RGB ||
3048 srcFormat == GL_BGR ||
3049 srcFormat == GL_RGBA ||
3050 srcFormat == GL_BGRA ||
3051 srcFormat == GL_ABGR_EXT ||
3052 srcFormat == GL_RED_INTEGER_EXT ||
3053 srcFormat == GL_GREEN_INTEGER_EXT ||
3054 srcFormat == GL_BLUE_INTEGER_EXT ||
3055 srcFormat == GL_ALPHA_INTEGER_EXT ||
3056 srcFormat == GL_RG_INTEGER ||
3057 srcFormat == GL_RGB_INTEGER_EXT ||
3058 srcFormat == GL_RGBA_INTEGER_EXT ||
3059 srcFormat == GL_BGR_INTEGER_EXT ||
3060 srcFormat == GL_BGRA_INTEGER_EXT ||
3061 srcFormat == GL_LUMINANCE_INTEGER_EXT ||
3062 srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT ||
3063 srcFormat == GL_COLOR_INDEX);
3064
3065 ASSERT(srcType == GL_BITMAP ||
3066 srcType == GL_UNSIGNED_BYTE ||
3067 srcType == GL_BYTE ||
3068 srcType == GL_UNSIGNED_SHORT ||
3069 srcType == GL_SHORT ||
3070 srcType == GL_UNSIGNED_INT ||
3071 srcType == GL_INT ||
3072 srcType == GL_HALF_FLOAT_ARB ||
3073 srcType == GL_FLOAT ||
3074 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3075 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3076 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3077 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3078 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3079 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3080 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3081 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3082 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3083 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3084 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3085 srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
3086 srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
3087 srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
3088
3089 /* general solution, no special cases, yet */
3090 {
3091 GLint dstComponents;
3092 GLint rDst, gDst, bDst, aDst, lDst, iDst;
3093 GLfloat (*rgba)[4] = malloc(4 * n * sizeof(GLfloat));
3094 GLboolean intFormat = _mesa_is_enum_format_integer(srcFormat);
3095
3096 if (!rgba) {
3097 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3098 return;
3099 }
3100
3101 dstComponents = _mesa_components_in_format( dstFormat );
3102 /* source & dest image formats should have been error checked by now */
3103 assert(dstComponents > 0);
3104
3105 /* EXT_texture_integer specifies no transfer ops on integer
3106 * types in the resolved issues section. Just set them to 0
3107 * for integer surfaces.
3108 */
3109 if (intFormat)
3110 transferOps = 0;
3111
3112 /*
3113 * Extract image data and convert to RGBA floats
3114 */
3115 if (srcFormat == GL_COLOR_INDEX) {
3116 GLuint *indexes = malloc(n * sizeof(GLuint));
3117
3118 if (!indexes) {
3119 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3120 free(rgba);
3121 return;
3122 }
3123
3124 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3125 srcPacking);
3126
3127 /* Convert indexes to RGBA */
3128 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3129 _mesa_shift_and_offset_ci(ctx, n, indexes);
3130 }
3131 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3132
3133 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3134 * with color indexes.
3135 */
3136 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3137
3138 free(indexes);
3139 }
3140 else {
3141 /* non-color index data */
3142 extract_float_rgba(n, rgba, srcFormat, srcType, source,
3143 srcPacking->SwapBytes);
3144 }
3145
3146 if (transferOps) {
3147 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3148 }
3149
3150 get_component_indexes(dstFormat,
3151 &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
3152
3153 /* Now pack results in the requested dstFormat */
3154 if (rDst >= 0) {
3155 GLfloat *dst = dest;
3156 GLuint i;
3157 for (i = 0; i < n; i++) {
3158 dst[rDst] = rgba[i][RCOMP];
3159 dst += dstComponents;
3160 }
3161 }
3162
3163 if (gDst >= 0) {
3164 GLfloat *dst = dest;
3165 GLuint i;
3166 for (i = 0; i < n; i++) {
3167 dst[gDst] = rgba[i][GCOMP];
3168 dst += dstComponents;
3169 }
3170 }
3171
3172 if (bDst >= 0) {
3173 GLfloat *dst = dest;
3174 GLuint i;
3175 for (i = 0; i < n; i++) {
3176 dst[bDst] = rgba[i][BCOMP];
3177 dst += dstComponents;
3178 }
3179 }
3180
3181 if (aDst >= 0) {
3182 GLfloat *dst = dest;
3183 GLuint i;
3184 for (i = 0; i < n; i++) {
3185 dst[aDst] = rgba[i][ACOMP];
3186 dst += dstComponents;
3187 }
3188 }
3189
3190 if (iDst >= 0) {
3191 GLfloat *dst = dest;
3192 GLuint i;
3193 assert(iDst == 0);
3194 assert(dstComponents == 1);
3195 for (i = 0; i < n; i++) {
3196 /* Intensity comes from red channel */
3197 dst[i] = rgba[i][RCOMP];
3198 }
3199 }
3200
3201 if (lDst >= 0) {
3202 GLfloat *dst = dest;
3203 GLuint i;
3204 assert(lDst == 0);
3205 for (i = 0; i < n; i++) {
3206 /* Luminance comes from red channel */
3207 dst[0] = rgba[i][RCOMP];
3208 dst += dstComponents;
3209 }
3210 }
3211
3212 free(rgba);
3213 }
3214 }
3215
3216
3217 /**
3218 * Same as _mesa_unpack_color_span_ubyte(), but return GLuint data
3219 * instead of GLubyte.
3220 * No pixel transfer ops are applied.
3221 */
3222 void
3223 _mesa_unpack_color_span_uint(struct gl_context *ctx,
3224 GLuint n, GLenum dstFormat, GLuint *dest,
3225 GLenum srcFormat, GLenum srcType,
3226 const GLvoid *source,
3227 const struct gl_pixelstore_attrib *srcPacking)
3228 {
3229 GLuint (*rgba)[4] = malloc(n * 4 * sizeof(GLfloat));
3230
3231 if (!rgba) {
3232 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3233 return;
3234 }
3235
3236 ASSERT(dstFormat == GL_ALPHA ||
3237 dstFormat == GL_LUMINANCE ||
3238 dstFormat == GL_LUMINANCE_ALPHA ||
3239 dstFormat == GL_INTENSITY ||
3240 dstFormat == GL_RED ||
3241 dstFormat == GL_RG ||
3242 dstFormat == GL_RGB ||
3243 dstFormat == GL_RGBA);
3244
3245 ASSERT(srcFormat == GL_RED ||
3246 srcFormat == GL_GREEN ||
3247 srcFormat == GL_BLUE ||
3248 srcFormat == GL_ALPHA ||
3249 srcFormat == GL_LUMINANCE ||
3250 srcFormat == GL_LUMINANCE_ALPHA ||
3251 srcFormat == GL_INTENSITY ||
3252 srcFormat == GL_RG ||
3253 srcFormat == GL_RGB ||
3254 srcFormat == GL_BGR ||
3255 srcFormat == GL_RGBA ||
3256 srcFormat == GL_BGRA ||
3257 srcFormat == GL_ABGR_EXT ||
3258 srcFormat == GL_RED_INTEGER_EXT ||
3259 srcFormat == GL_GREEN_INTEGER_EXT ||
3260 srcFormat == GL_BLUE_INTEGER_EXT ||
3261 srcFormat == GL_ALPHA_INTEGER_EXT ||
3262 srcFormat == GL_RG_INTEGER ||
3263 srcFormat == GL_RGB_INTEGER_EXT ||
3264 srcFormat == GL_RGBA_INTEGER_EXT ||
3265 srcFormat == GL_BGR_INTEGER_EXT ||
3266 srcFormat == GL_BGRA_INTEGER_EXT ||
3267 srcFormat == GL_LUMINANCE_INTEGER_EXT ||
3268 srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
3269
3270 ASSERT(srcType == GL_UNSIGNED_BYTE ||
3271 srcType == GL_BYTE ||
3272 srcType == GL_UNSIGNED_SHORT ||
3273 srcType == GL_SHORT ||
3274 srcType == GL_UNSIGNED_INT ||
3275 srcType == GL_INT ||
3276 srcType == GL_HALF_FLOAT_ARB ||
3277 srcType == GL_FLOAT ||
3278 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3279 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3280 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3281 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3282 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3283 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3284 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3285 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3286 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3287 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3288 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3289 srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
3290 srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
3291 srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
3292
3293
3294 /* Extract image data as uint[4] pixels */
3295 extract_uint_rgba(n, rgba, srcFormat, srcType, source,
3296 srcPacking->SwapBytes);
3297
3298 if (dstFormat == GL_RGBA) {
3299 /* simple case */
3300 memcpy(dest, rgba, 4 * sizeof(GLuint) * n);
3301 }
3302 else {
3303 /* general case */
3304 GLint rDst, gDst, bDst, aDst, lDst, iDst;
3305 GLint dstComponents = _mesa_components_in_format( dstFormat );
3306
3307 assert(dstComponents > 0);
3308
3309 get_component_indexes(dstFormat,
3310 &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
3311
3312 /* Now pack values in the requested dest format */
3313 if (rDst >= 0) {
3314 GLuint *dst = dest;
3315 GLuint i;
3316 for (i = 0; i < n; i++) {
3317 dst[rDst] = rgba[i][RCOMP];
3318 dst += dstComponents;
3319 }
3320 }
3321
3322 if (gDst >= 0) {
3323 GLuint *dst = dest;
3324 GLuint i;
3325 for (i = 0; i < n; i++) {
3326 dst[gDst] = rgba[i][GCOMP];
3327 dst += dstComponents;
3328 }
3329 }
3330
3331 if (bDst >= 0) {
3332 GLuint *dst = dest;
3333 GLuint i;
3334 for (i = 0; i < n; i++) {
3335 dst[bDst] = rgba[i][BCOMP];
3336 dst += dstComponents;
3337 }
3338 }
3339
3340 if (aDst >= 0) {
3341 GLuint *dst = dest;
3342 GLuint i;
3343 for (i = 0; i < n; i++) {
3344 dst[aDst] = rgba[i][ACOMP];
3345 dst += dstComponents;
3346 }
3347 }
3348
3349 if (iDst >= 0) {
3350 GLuint *dst = dest;
3351 GLuint i;
3352 assert(iDst == 0);
3353 assert(dstComponents == 1);
3354 for (i = 0; i < n; i++) {
3355 /* Intensity comes from red channel */
3356 dst[i] = rgba[i][RCOMP];
3357 }
3358 }
3359
3360 if (lDst >= 0) {
3361 GLuint *dst = dest;
3362 GLuint i;
3363 assert(lDst == 0);
3364 for (i = 0; i < n; i++) {
3365 /* Luminance comes from red channel */
3366 dst[0] = rgba[i][RCOMP];
3367 dst += dstComponents;
3368 }
3369 }
3370 }
3371
3372 free(rgba);
3373 }
3374
3375
3376 /*
3377 * Unpack a row of color index data from a client buffer according to
3378 * the pixel unpacking parameters.
3379 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3380 *
3381 * Args: ctx - the context
3382 * n - number of pixels
3383 * dstType - destination data type
3384 * dest - destination array
3385 * srcType - source pixel type
3386 * source - source data pointer
3387 * srcPacking - pixel unpacking parameters
3388 * transferOps - the pixel transfer operations to apply
3389 */
3390 void
3391 _mesa_unpack_index_span( struct gl_context *ctx, GLuint n,
3392 GLenum dstType, GLvoid *dest,
3393 GLenum srcType, const GLvoid *source,
3394 const struct gl_pixelstore_attrib *srcPacking,
3395 GLbitfield transferOps )
3396 {
3397 ASSERT(srcType == GL_BITMAP ||
3398 srcType == GL_UNSIGNED_BYTE ||
3399 srcType == GL_BYTE ||
3400 srcType == GL_UNSIGNED_SHORT ||
3401 srcType == GL_SHORT ||
3402 srcType == GL_UNSIGNED_INT ||
3403 srcType == GL_INT ||
3404 srcType == GL_HALF_FLOAT_ARB ||
3405 srcType == GL_FLOAT);
3406
3407 ASSERT(dstType == GL_UNSIGNED_BYTE ||
3408 dstType == GL_UNSIGNED_SHORT ||
3409 dstType == GL_UNSIGNED_INT);
3410
3411
3412 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3413
3414 /*
3415 * Try simple cases first
3416 */
3417 if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
3418 && dstType == GL_UNSIGNED_BYTE) {
3419 memcpy(dest, source, n * sizeof(GLubyte));
3420 }
3421 else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
3422 && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
3423 memcpy(dest, source, n * sizeof(GLuint));
3424 }
3425 else {
3426 /*
3427 * general solution
3428 */
3429 GLuint *indexes = malloc(n * sizeof(GLuint));
3430
3431 if (!indexes) {
3432 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3433 return;
3434 }
3435
3436 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
3437 srcPacking);
3438
3439 if (transferOps)
3440 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3441
3442 /* convert to dest type */
3443 switch (dstType) {
3444 case GL_UNSIGNED_BYTE:
3445 {
3446 GLubyte *dst = (GLubyte *) dest;
3447 GLuint i;
3448 for (i = 0; i < n; i++) {
3449 dst[i] = (GLubyte) (indexes[i] & 0xff);
3450 }
3451 }
3452 break;
3453 case GL_UNSIGNED_SHORT:
3454 {
3455 GLuint *dst = (GLuint *) dest;
3456 GLuint i;
3457 for (i = 0; i < n; i++) {
3458 dst[i] = (GLushort) (indexes[i] & 0xffff);
3459 }
3460 }
3461 break;
3462 case GL_UNSIGNED_INT:
3463 memcpy(dest, indexes, n * sizeof(GLuint));
3464 break;
3465 default:
3466 _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
3467 }
3468
3469 free(indexes);
3470 }
3471 }
3472
3473
3474 void
3475 _mesa_pack_index_span( struct gl_context *ctx, GLuint n,
3476 GLenum dstType, GLvoid *dest, const GLuint *source,
3477 const struct gl_pixelstore_attrib *dstPacking,
3478 GLbitfield transferOps )
3479 {
3480 GLuint *indexes = malloc(n * sizeof(GLuint));
3481
3482 if (!indexes) {
3483 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
3484 return;
3485 }
3486
3487 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3488
3489 if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
3490 /* make a copy of input */
3491 memcpy(indexes, source, n * sizeof(GLuint));
3492 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3493 source = indexes;
3494 }
3495
3496 switch (dstType) {
3497 case GL_UNSIGNED_BYTE:
3498 {
3499 GLubyte *dst = (GLubyte *) dest;
3500 GLuint i;
3501 for (i = 0; i < n; i++) {
3502 *dst++ = (GLubyte) source[i];
3503 }
3504 }
3505 break;
3506 case GL_BYTE:
3507 {
3508 GLbyte *dst = (GLbyte *) dest;
3509 GLuint i;
3510 for (i = 0; i < n; i++) {
3511 dst[i] = (GLbyte) source[i];
3512 }
3513 }
3514 break;
3515 case GL_UNSIGNED_SHORT:
3516 {
3517 GLushort *dst = (GLushort *) dest;
3518 GLuint i;
3519 for (i = 0; i < n; i++) {
3520 dst[i] = (GLushort) source[i];
3521 }
3522 if (dstPacking->SwapBytes) {
3523 _mesa_swap2( (GLushort *) dst, n );
3524 }
3525 }
3526 break;
3527 case GL_SHORT:
3528 {
3529 GLshort *dst = (GLshort *) dest;
3530 GLuint i;
3531 for (i = 0; i < n; i++) {
3532 dst[i] = (GLshort) source[i];
3533 }
3534 if (dstPacking->SwapBytes) {
3535 _mesa_swap2( (GLushort *) dst, n );
3536 }
3537 }
3538 break;
3539 case GL_UNSIGNED_INT:
3540 {
3541 GLuint *dst = (GLuint *) dest;
3542 GLuint i;
3543 for (i = 0; i < n; i++) {
3544 dst[i] = (GLuint) source[i];
3545 }
3546 if (dstPacking->SwapBytes) {
3547 _mesa_swap4( (GLuint *) dst, n );
3548 }
3549 }
3550 break;
3551 case GL_INT:
3552 {
3553 GLint *dst = (GLint *) dest;
3554 GLuint i;
3555 for (i = 0; i < n; i++) {
3556 dst[i] = (GLint) source[i];
3557 }
3558 if (dstPacking->SwapBytes) {
3559 _mesa_swap4( (GLuint *) dst, n );
3560 }
3561 }
3562 break;
3563 case GL_FLOAT:
3564 {
3565 GLfloat *dst = (GLfloat *) dest;
3566 GLuint i;
3567 for (i = 0; i < n; i++) {
3568 dst[i] = (GLfloat) source[i];
3569 }
3570 if (dstPacking->SwapBytes) {
3571 _mesa_swap4( (GLuint *) dst, n );
3572 }
3573 }
3574 break;
3575 case GL_HALF_FLOAT_ARB:
3576 {
3577 GLhalfARB *dst = (GLhalfARB *) dest;
3578 GLuint i;
3579 for (i = 0; i < n; i++) {
3580 dst[i] = _mesa_float_to_half((GLfloat) source[i]);
3581 }
3582 if (dstPacking->SwapBytes) {
3583 _mesa_swap2( (GLushort *) dst, n );
3584 }
3585 }
3586 break;
3587 default:
3588 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
3589 }
3590
3591 free(indexes);
3592 }
3593
3594
3595 /*
3596 * Unpack a row of stencil data from a client buffer according to
3597 * the pixel unpacking parameters.
3598 * This is (or will be) used by glDrawPixels
3599 *
3600 * Args: ctx - the context
3601 * n - number of pixels
3602 * dstType - destination data type
3603 * dest - destination array
3604 * srcType - source pixel type
3605 * source - source data pointer
3606 * srcPacking - pixel unpacking parameters
3607 * transferOps - apply offset/bias/lookup ops?
3608 */
3609 void
3610 _mesa_unpack_stencil_span( struct gl_context *ctx, GLuint n,
3611 GLenum dstType, GLvoid *dest,
3612 GLenum srcType, const GLvoid *source,
3613 const struct gl_pixelstore_attrib *srcPacking,
3614 GLbitfield transferOps )
3615 {
3616 ASSERT(srcType == GL_BITMAP ||
3617 srcType == GL_UNSIGNED_BYTE ||
3618 srcType == GL_BYTE ||
3619 srcType == GL_UNSIGNED_SHORT ||
3620 srcType == GL_SHORT ||
3621 srcType == GL_UNSIGNED_INT ||
3622 srcType == GL_INT ||
3623 srcType == GL_UNSIGNED_INT_24_8_EXT ||
3624 srcType == GL_HALF_FLOAT_ARB ||
3625 srcType == GL_FLOAT ||
3626 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
3627
3628 ASSERT(dstType == GL_UNSIGNED_BYTE ||
3629 dstType == GL_UNSIGNED_SHORT ||
3630 dstType == GL_UNSIGNED_INT ||
3631 dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
3632
3633 /* only shift and offset apply to stencil */
3634 transferOps &= IMAGE_SHIFT_OFFSET_BIT;
3635
3636 /*
3637 * Try simple cases first
3638 */
3639 if (transferOps == 0 &&
3640 !ctx->Pixel.MapStencilFlag &&
3641 srcType == GL_UNSIGNED_BYTE &&
3642 dstType == GL_UNSIGNED_BYTE) {
3643 memcpy(dest, source, n * sizeof(GLubyte));
3644 }
3645 else if (transferOps == 0 &&
3646 !ctx->Pixel.MapStencilFlag &&
3647 srcType == GL_UNSIGNED_INT &&
3648 dstType == GL_UNSIGNED_INT &&
3649 !srcPacking->SwapBytes) {
3650 memcpy(dest, source, n * sizeof(GLuint));
3651 }
3652 else {
3653 /*
3654 * general solution
3655 */
3656 GLuint *indexes = malloc(n * sizeof(GLuint));
3657
3658 if (!indexes) {
3659 _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil unpacking");
3660 return;
3661 }
3662
3663 extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
3664 srcPacking);
3665
3666 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3667 /* shift and offset indexes */
3668 _mesa_shift_and_offset_ci(ctx, n, indexes);
3669 }
3670
3671 if (ctx->Pixel.MapStencilFlag) {
3672 /* Apply stencil lookup table */
3673 const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
3674 GLuint i;
3675 for (i = 0; i < n; i++) {
3676 indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
3677 }
3678 }
3679
3680 /* convert to dest type */
3681 switch (dstType) {
3682 case GL_UNSIGNED_BYTE:
3683 {
3684 GLubyte *dst = (GLubyte *) dest;
3685 GLuint i;
3686 for (i = 0; i < n; i++) {
3687 dst[i] = (GLubyte) (indexes[i] & 0xff);
3688 }
3689 }
3690 break;
3691 case GL_UNSIGNED_SHORT:
3692 {
3693 GLuint *dst = (GLuint *) dest;
3694 GLuint i;
3695 for (i = 0; i < n; i++) {
3696 dst[i] = (GLushort) (indexes[i] & 0xffff);
3697 }
3698 }
3699 break;
3700 case GL_UNSIGNED_INT:
3701 memcpy(dest, indexes, n * sizeof(GLuint));
3702 break;
3703 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
3704 {
3705 GLuint *dst = (GLuint *) dest;
3706 GLuint i;
3707 for (i = 0; i < n; i++) {
3708 dst[i*2+1] = indexes[i] & 0xff; /* lower 8 bits */
3709 }
3710 }
3711 break;
3712 default:
3713 _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
3714 }
3715
3716 free(indexes);
3717 }
3718 }
3719
3720
3721 void
3722 _mesa_pack_stencil_span( struct gl_context *ctx, GLuint n,
3723 GLenum dstType, GLvoid *dest, const GLubyte *source,
3724 const struct gl_pixelstore_attrib *dstPacking )
3725 {
3726 GLubyte *stencil = malloc(n * sizeof(GLubyte));
3727
3728 if (!stencil) {
3729 _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil packing");
3730 return;
3731 }
3732
3733 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
3734 ctx->Pixel.MapStencilFlag) {
3735 /* make a copy of input */
3736 memcpy(stencil, source, n * sizeof(GLubyte));
3737 _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
3738 source = stencil;
3739 }
3740
3741 switch (dstType) {
3742 case GL_UNSIGNED_BYTE:
3743 memcpy(dest, source, n);
3744 break;
3745 case GL_BYTE:
3746 {
3747 GLbyte *dst = (GLbyte *) dest;
3748 GLuint i;
3749 for (i=0;i<n;i++) {
3750 dst[i] = (GLbyte) (source[i] & 0x7f);
3751 }
3752 }
3753 break;
3754 case GL_UNSIGNED_SHORT:
3755 {
3756 GLushort *dst = (GLushort *) dest;
3757 GLuint i;
3758 for (i=0;i<n;i++) {
3759 dst[i] = (GLushort) source[i];
3760 }
3761 if (dstPacking->SwapBytes) {
3762 _mesa_swap2( (GLushort *) dst, n );
3763 }
3764 }
3765 break;
3766 case GL_SHORT:
3767 {
3768 GLshort *dst = (GLshort *) dest;
3769 GLuint i;
3770 for (i=0;i<n;i++) {
3771 dst[i] = (GLshort) source[i];
3772 }
3773 if (dstPacking->SwapBytes) {
3774 _mesa_swap2( (GLushort *) dst, n );
3775 }
3776 }
3777 break;
3778 case GL_UNSIGNED_INT:
3779 {
3780 GLuint *dst = (GLuint *) dest;
3781 GLuint i;
3782 for (i=0;i<n;i++) {
3783 dst[i] = (GLuint) source[i];
3784 }
3785 if (dstPacking->SwapBytes) {
3786 _mesa_swap4( (GLuint *) dst, n );
3787 }
3788 }
3789 break;
3790 case GL_INT:
3791 {
3792 GLint *dst = (GLint *) dest;
3793 GLuint i;
3794 for (i=0;i<n;i++) {
3795 dst[i] = (GLint) source[i];
3796 }
3797 if (dstPacking->SwapBytes) {
3798 _mesa_swap4( (GLuint *) dst, n );
3799 }
3800 }
3801 break;
3802 case GL_FLOAT:
3803 {
3804 GLfloat *dst = (GLfloat *) dest;
3805 GLuint i;
3806 for (i=0;i<n;i++) {
3807 dst[i] = (GLfloat) source[i];
3808 }
3809 if (dstPacking->SwapBytes) {
3810 _mesa_swap4( (GLuint *) dst, n );
3811 }
3812 }
3813 break;
3814 case GL_HALF_FLOAT_ARB:
3815 {
3816 GLhalfARB *dst = (GLhalfARB *) dest;
3817 GLuint i;
3818 for (i=0;i<n;i++) {
3819 dst[i] = _mesa_float_to_half( (float) source[i] );
3820 }
3821 if (dstPacking->SwapBytes) {
3822 _mesa_swap2( (GLushort *) dst, n );
3823 }
3824 }
3825 break;
3826 case GL_BITMAP:
3827 if (dstPacking->LsbFirst) {
3828 GLubyte *dst = (GLubyte *) dest;
3829 GLint shift = 0;
3830 GLuint i;
3831 for (i = 0; i < n; i++) {
3832 if (shift == 0)
3833 *dst = 0;
3834 *dst |= ((source[i] != 0) << shift);
3835 shift++;
3836 if (shift == 8) {
3837 shift = 0;
3838 dst++;
3839 }
3840 }
3841 }
3842 else {
3843 GLubyte *dst = (GLubyte *) dest;
3844 GLint shift = 7;
3845 GLuint i;
3846 for (i = 0; i < n; i++) {
3847 if (shift == 7)
3848 *dst = 0;
3849 *dst |= ((source[i] != 0) << shift);
3850 shift--;
3851 if (shift < 0) {
3852 shift = 7;
3853 dst++;
3854 }
3855 }
3856 }
3857 break;
3858 default:
3859 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
3860 }
3861
3862 free(stencil);
3863 }
3864
3865 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
3866 do { \
3867 GLuint i; \
3868 const GLTYPE *src = (const GLTYPE *)source; \
3869 for (i = 0; i < n; i++) { \
3870 GLTYPE value = src[i]; \
3871 if (srcPacking->SwapBytes) { \
3872 if (sizeof(GLTYPE) == 2) { \
3873 SWAP2BYTE(value); \
3874 } else if (sizeof(GLTYPE) == 4) { \
3875 SWAP4BYTE(value); \
3876 } \
3877 } \
3878 depthValues[i] = GLTYPE2FLOAT(value); \
3879 } \
3880 } while (0)
3881
3882
3883 /**
3884 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
3885 * or GLfloat values.
3886 * The glPixelTransfer (scale/bias) params will be applied.
3887 *
3888 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
3889 * \param depthMax max value for returned GLushort or GLuint values
3890 * (ignored for GLfloat).
3891 */
3892 void
3893 _mesa_unpack_depth_span( struct gl_context *ctx, GLuint n,
3894 GLenum dstType, GLvoid *dest, GLuint depthMax,
3895 GLenum srcType, const GLvoid *source,
3896 const struct gl_pixelstore_attrib *srcPacking )
3897 {
3898 GLfloat *depthTemp = NULL, *depthValues;
3899 GLboolean needClamp = GL_FALSE;
3900
3901 /* Look for special cases first.
3902 * Not only are these faster, they're less prone to numeric conversion
3903 * problems. Otherwise, converting from an int type to a float then
3904 * back to an int type can introduce errors that will show up as
3905 * artifacts in things like depth peeling which uses glCopyTexImage.
3906 */
3907 if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
3908 if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
3909 const GLuint *src = (const GLuint *) source;
3910 GLushort *dst = (GLushort *) dest;
3911 GLuint i;
3912 for (i = 0; i < n; i++) {
3913 dst[i] = src[i] >> 16;
3914 }
3915 return;
3916 }
3917 if (srcType == GL_UNSIGNED_SHORT
3918 && dstType == GL_UNSIGNED_INT
3919 && depthMax == 0xffffffff) {
3920 const GLushort *src = (const GLushort *) source;
3921 GLuint *dst = (GLuint *) dest;
3922 GLuint i;
3923 for (i = 0; i < n; i++) {
3924 dst[i] = src[i] | (src[i] << 16);
3925 }
3926 return;
3927 }
3928 if (srcType == GL_UNSIGNED_INT_24_8
3929 && dstType == GL_UNSIGNED_INT
3930 && depthMax == 0xffffff) {
3931 const GLuint *src = (const GLuint *) source;
3932 GLuint *dst = (GLuint *) dest;
3933 GLuint i;
3934 for (i = 0; i < n; i++) {
3935 dst[i] = src[i] >> 8;
3936 }
3937 return;
3938 }
3939 /* XXX may want to add additional cases here someday */
3940 }
3941
3942 /* general case path follows */
3943
3944 if (dstType == GL_FLOAT) {
3945 depthValues = (GLfloat *) dest;
3946 }
3947 else {
3948 depthTemp = malloc(n * sizeof(GLfloat));
3949 if (!depthTemp) {
3950 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3951 return;
3952 }
3953
3954 depthValues = depthTemp;
3955 }
3956
3957 /* Convert incoming values to GLfloat. Some conversions will require
3958 * clamping, below.
3959 */
3960 switch (srcType) {
3961 case GL_BYTE:
3962 DEPTH_VALUES(GLbyte, BYTE_TO_FLOATZ);
3963 needClamp = GL_TRUE;
3964 break;
3965 case GL_UNSIGNED_BYTE:
3966 DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
3967 break;
3968 case GL_SHORT:
3969 DEPTH_VALUES(GLshort, SHORT_TO_FLOATZ);
3970 needClamp = GL_TRUE;
3971 break;
3972 case GL_UNSIGNED_SHORT:
3973 DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
3974 break;
3975 case GL_INT:
3976 DEPTH_VALUES(GLint, INT_TO_FLOAT);
3977 needClamp = GL_TRUE;
3978 break;
3979 case GL_UNSIGNED_INT:
3980 DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
3981 break;
3982 case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
3983 if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
3984 depthMax == 0xffffff &&
3985 ctx->Pixel.DepthScale == 1.0 &&
3986 ctx->Pixel.DepthBias == 0.0) {
3987 const GLuint *src = (const GLuint *) source;
3988 GLuint *zValues = (GLuint *) dest;
3989 GLuint i;
3990 for (i = 0; i < n; i++) {
3991 GLuint value = src[i];
3992 if (srcPacking->SwapBytes) {
3993 SWAP4BYTE(value);
3994 }
3995 zValues[i] = value & 0xffffff00;
3996 }
3997 free(depthTemp);
3998 return;
3999 }
4000 else {
4001 const GLuint *src = (const GLuint *) source;
4002 const GLfloat scale = 1.0f / 0xffffff;
4003 GLuint i;
4004 for (i = 0; i < n; i++) {
4005 GLuint value = src[i];
4006 if (srcPacking->SwapBytes) {
4007 SWAP4BYTE(value);
4008 }
4009 depthValues[i] = (value >> 8) * scale;
4010 }
4011 }
4012 break;
4013 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
4014 {
4015 GLuint i;
4016 const GLfloat *src = (const GLfloat *)source;
4017 for (i = 0; i < n; i++) {
4018 GLfloat value = src[i * 2];
4019 if (srcPacking->SwapBytes) {
4020 SWAP4BYTE(value);
4021 }
4022 depthValues[i] = value;
4023 }
4024 needClamp = GL_TRUE;
4025 }
4026 break;
4027 case GL_FLOAT:
4028 DEPTH_VALUES(GLfloat, 1*);
4029 needClamp = GL_TRUE;
4030 break;
4031 case GL_HALF_FLOAT_ARB:
4032 {
4033 GLuint i;
4034 const GLhalfARB *src = (const GLhalfARB *) source;
4035 for (i = 0; i < n; i++) {
4036 GLhalfARB value = src[i];
4037 if (srcPacking->SwapBytes) {
4038 SWAP2BYTE(value);
4039 }
4040 depthValues[i] = _mesa_half_to_float(value);
4041 }
4042 needClamp = GL_TRUE;
4043 }
4044 break;
4045 default:
4046 _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
4047 free(depthTemp);
4048 return;
4049 }
4050
4051 /* apply depth scale and bias */
4052 {
4053 const GLfloat scale = ctx->Pixel.DepthScale;
4054 const GLfloat bias = ctx->Pixel.DepthBias;
4055 if (scale != 1.0 || bias != 0.0) {
4056 GLuint i;
4057 for (i = 0; i < n; i++) {
4058 depthValues[i] = depthValues[i] * scale + bias;
4059 }
4060 needClamp = GL_TRUE;
4061 }
4062 }
4063
4064 /* clamp to [0, 1] */
4065 if (needClamp) {
4066 GLuint i;
4067 for (i = 0; i < n; i++) {
4068 depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
4069 }
4070 }
4071
4072 /*
4073 * Convert values to dstType
4074 */
4075 if (dstType == GL_UNSIGNED_INT) {
4076 GLuint *zValues = (GLuint *) dest;
4077 GLuint i;
4078 if (depthMax <= 0xffffff) {
4079 /* no overflow worries */
4080 for (i = 0; i < n; i++) {
4081 zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
4082 }
4083 }
4084 else {
4085 /* need to use double precision to prevent overflow problems */
4086 for (i = 0; i < n; i++) {
4087 GLdouble z = depthValues[i] * (GLdouble) depthMax;
4088 if (z >= (GLdouble) 0xffffffff)
4089 zValues[i] = 0xffffffff;
4090 else
4091 zValues[i] = (GLuint) z;
4092 }
4093 }
4094 }
4095 else if (dstType == GL_UNSIGNED_SHORT) {
4096 GLushort *zValues = (GLushort *) dest;
4097 GLuint i;
4098 ASSERT(depthMax <= 0xffff);
4099 for (i = 0; i < n; i++) {
4100 zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
4101 }
4102 }
4103 else if (dstType == GL_FLOAT) {
4104 /* Nothing to do. depthValues is pointing to dest. */
4105 }
4106 else if (dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) {
4107 GLfloat *zValues = (GLfloat*) dest;
4108 GLuint i;
4109 for (i = 0; i < n; i++) {
4110 zValues[i*2] = depthValues[i];
4111 }
4112 }
4113 else {
4114 ASSERT(0);
4115 }
4116
4117 free(depthTemp);
4118 }
4119
4120
4121 /*
4122 * Pack an array of depth values. The values are floats in [0,1].
4123 */
4124 void
4125 _mesa_pack_depth_span( struct gl_context *ctx, GLuint n, GLvoid *dest,
4126 GLenum dstType, const GLfloat *depthSpan,
4127 const struct gl_pixelstore_attrib *dstPacking )
4128 {
4129 GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
4130 if (!depthCopy) {
4131 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
4132 return;
4133 }
4134
4135 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4136 memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
4137 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4138 depthSpan = depthCopy;
4139 }
4140
4141 switch (dstType) {
4142 case GL_UNSIGNED_BYTE:
4143 {
4144 GLubyte *dst = (GLubyte *) dest;
4145 GLuint i;
4146 for (i = 0; i < n; i++) {
4147 dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
4148 }
4149 }
4150 break;
4151 case GL_BYTE:
4152 {
4153 GLbyte *dst = (GLbyte *) dest;
4154 GLuint i;
4155 for (i = 0; i < n; i++) {
4156 dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
4157 }
4158 }
4159 break;
4160 case GL_UNSIGNED_SHORT:
4161 {
4162 GLushort *dst = (GLushort *) dest;
4163 GLuint i;
4164 for (i = 0; i < n; i++) {
4165 CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
4166 }
4167 if (dstPacking->SwapBytes) {
4168 _mesa_swap2( (GLushort *) dst, n );
4169 }
4170 }
4171 break;
4172 case GL_SHORT:
4173 {
4174 GLshort *dst = (GLshort *) dest;
4175 GLuint i;
4176 for (i = 0; i < n; i++) {
4177 dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
4178 }
4179 if (dstPacking->SwapBytes) {
4180 _mesa_swap2( (GLushort *) dst, n );
4181 }
4182 }
4183 break;
4184 case GL_UNSIGNED_INT:
4185 {
4186 GLuint *dst = (GLuint *) dest;
4187 GLuint i;
4188 for (i = 0; i < n; i++) {
4189 dst[i] = FLOAT_TO_UINT( depthSpan[i] );
4190 }
4191 if (dstPacking->SwapBytes) {
4192 _mesa_swap4( (GLuint *) dst, n );
4193 }
4194 }
4195 break;
4196 case GL_INT:
4197 {
4198 GLint *dst = (GLint *) dest;
4199 GLuint i;
4200 for (i = 0; i < n; i++) {
4201 dst[i] = FLOAT_TO_INT( depthSpan[i] );
4202 }
4203 if (dstPacking->SwapBytes) {
4204 _mesa_swap4( (GLuint *) dst, n );
4205 }
4206 }
4207 break;
4208 case GL_FLOAT:
4209 {
4210 GLfloat *dst = (GLfloat *) dest;
4211 GLuint i;
4212 for (i = 0; i < n; i++) {
4213 dst[i] = depthSpan[i];
4214 }
4215 if (dstPacking->SwapBytes) {
4216 _mesa_swap4( (GLuint *) dst, n );
4217 }
4218 }
4219 break;
4220 case GL_HALF_FLOAT_ARB:
4221 {
4222 GLhalfARB *dst = (GLhalfARB *) dest;
4223 GLuint i;
4224 for (i = 0; i < n; i++) {
4225 dst[i] = _mesa_float_to_half(depthSpan[i]);
4226 }
4227 if (dstPacking->SwapBytes) {
4228 _mesa_swap2( (GLushort *) dst, n );
4229 }
4230 }
4231 break;
4232 default:
4233 _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
4234 }
4235
4236 free(depthCopy);
4237 }
4238
4239
4240
4241 /**
4242 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
4243 */
4244 void
4245 _mesa_pack_depth_stencil_span(struct gl_context *ctx,GLuint n,
4246 GLenum dstType, GLuint *dest,
4247 const GLfloat *depthVals,
4248 const GLubyte *stencilVals,
4249 const struct gl_pixelstore_attrib *dstPacking)
4250 {
4251 GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
4252 GLubyte *stencilCopy = malloc(n * sizeof(GLubyte));
4253 GLuint i;
4254
4255 if (!depthCopy || !stencilCopy) {
4256 _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
4257 free(depthCopy);
4258 free(stencilCopy);
4259 return;
4260 }
4261
4262 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4263 memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
4264 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4265 depthVals = depthCopy;
4266 }
4267
4268 if (ctx->Pixel.IndexShift ||
4269 ctx->Pixel.IndexOffset ||
4270 ctx->Pixel.MapStencilFlag) {
4271 memcpy(stencilCopy, stencilVals, n * sizeof(GLubyte));
4272 _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
4273 stencilVals = stencilCopy;
4274 }
4275
4276 switch (dstType) {
4277 case GL_UNSIGNED_INT_24_8:
4278 for (i = 0; i < n; i++) {
4279 GLuint z = (GLuint) (depthVals[i] * 0xffffff);
4280 dest[i] = (z << 8) | (stencilVals[i] & 0xff);
4281 }
4282 break;
4283 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
4284 for (i = 0; i < n; i++) {
4285 ((GLfloat*)dest)[i*2] = depthVals[i];
4286 dest[i*2+1] = stencilVals[i] & 0xff;
4287 }
4288 break;
4289 }
4290
4291 if (dstPacking->SwapBytes) {
4292 _mesa_swap4(dest, n);
4293 }
4294
4295 free(depthCopy);
4296 free(stencilCopy);
4297 }
4298
4299
4300
4301
4302 /**
4303 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
4304 * Return all image data in a contiguous block. This is used when we
4305 * compile glDrawPixels, glTexImage, etc into a display list. We
4306 * need a copy of the data in a standard format.
4307 */
4308 void *
4309 _mesa_unpack_image( GLuint dimensions,
4310 GLsizei width, GLsizei height, GLsizei depth,
4311 GLenum format, GLenum type, const GLvoid *pixels,
4312 const struct gl_pixelstore_attrib *unpack )
4313 {
4314 GLint bytesPerRow, compsPerRow;
4315 GLboolean flipBytes, swap2, swap4;
4316
4317 if (!pixels)
4318 return NULL; /* not necessarily an error */
4319
4320 if (width <= 0 || height <= 0 || depth <= 0)
4321 return NULL; /* generate error later */
4322
4323 if (type == GL_BITMAP) {
4324 bytesPerRow = (width + 7) >> 3;
4325 flipBytes = unpack->LsbFirst;
4326 swap2 = swap4 = GL_FALSE;
4327 compsPerRow = 0;
4328 }
4329 else {
4330 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
4331 GLint components = _mesa_components_in_format(format);
4332 GLint bytesPerComp;
4333
4334 if (_mesa_type_is_packed(type))
4335 components = 1;
4336
4337 if (bytesPerPixel <= 0 || components <= 0)
4338 return NULL; /* bad format or type. generate error later */
4339 bytesPerRow = bytesPerPixel * width;
4340 bytesPerComp = bytesPerPixel / components;
4341 flipBytes = GL_FALSE;
4342 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
4343 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
4344 compsPerRow = components * width;
4345 assert(compsPerRow >= width);
4346 }
4347
4348 {
4349 GLubyte *destBuffer
4350 = malloc(bytesPerRow * height * depth);
4351 GLubyte *dst;
4352 GLint img, row;
4353 if (!destBuffer)
4354 return NULL; /* generate GL_OUT_OF_MEMORY later */
4355
4356 dst = destBuffer;
4357 for (img = 0; img < depth; img++) {
4358 for (row = 0; row < height; row++) {
4359 const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
4360 width, height, format, type, img, row, 0);
4361
4362 if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
4363 GLint i;
4364 flipBytes = GL_FALSE;
4365 if (unpack->LsbFirst) {
4366 GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
4367 GLubyte dstMask = 128;
4368 const GLubyte *s = src;
4369 GLubyte *d = dst;
4370 *d = 0;
4371 for (i = 0; i < width; i++) {
4372 if (*s & srcMask) {
4373 *d |= dstMask;
4374 }
4375 if (srcMask == 128) {
4376 srcMask = 1;
4377 s++;
4378 }
4379 else {
4380 srcMask = srcMask << 1;
4381 }
4382 if (dstMask == 1) {
4383 dstMask = 128;
4384 d++;
4385 *d = 0;
4386 }
4387 else {
4388 dstMask = dstMask >> 1;
4389 }
4390 }
4391 }
4392 else {
4393 GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
4394 GLubyte dstMask = 128;
4395 const GLubyte *s = src;
4396 GLubyte *d = dst;
4397 *d = 0;
4398 for (i = 0; i < width; i++) {
4399 if (*s & srcMask) {
4400 *d |= dstMask;
4401 }
4402 if (srcMask == 1) {
4403 srcMask = 128;
4404 s++;
4405 }
4406 else {
4407 srcMask = srcMask >> 1;
4408 }
4409 if (dstMask == 1) {
4410 dstMask = 128;
4411 d++;
4412 *d = 0;
4413 }
4414 else {
4415 dstMask = dstMask >> 1;
4416 }
4417 }
4418 }
4419 }
4420 else {
4421 memcpy(dst, src, bytesPerRow);
4422 }
4423
4424 /* byte flipping/swapping */
4425 if (flipBytes) {
4426 flip_bytes((GLubyte *) dst, bytesPerRow);
4427 }
4428 else if (swap2) {
4429 _mesa_swap2((GLushort*) dst, compsPerRow);
4430 }
4431 else if (swap4) {
4432 _mesa_swap4((GLuint*) dst, compsPerRow);
4433 }
4434 dst += bytesPerRow;
4435 }
4436 }
4437 return destBuffer;
4438 }
4439 }
4440
4441
4442
4443 /**
4444 * If we unpack colors from a luminance surface, we'll get pixel colors
4445 * such as (l, l, l, a).
4446 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
4447 * function will compute L=R+G+B before packing. The net effect is we'll
4448 * accidentally store luminance values = 3*l.
4449 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
4450 * to be (l,0,0,a).
4451 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
4452 * and INTENSITY.
4453 *
4454 * Finally, we also need to do this when the actual surface format does
4455 * not match the logical surface format. For example, suppose the user
4456 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
4457 * Again, we'll get pixel values like (l,l,l,a).
4458 */
4459 void
4460 _mesa_rebase_rgba_float(GLuint n, GLfloat rgba[][4], GLenum baseFormat)
4461 {
4462 GLuint i;
4463
4464 switch (baseFormat) {
4465 case GL_ALPHA:
4466 for (i = 0; i < n; i++) {
4467 rgba[i][RCOMP] = 0.0F;
4468 rgba[i][GCOMP] = 0.0F;
4469 rgba[i][BCOMP] = 0.0F;
4470 }
4471 break;
4472 case GL_INTENSITY:
4473 /* fall-through */
4474 case GL_LUMINANCE:
4475 for (i = 0; i < n; i++) {
4476 rgba[i][GCOMP] = 0.0F;
4477 rgba[i][BCOMP] = 0.0F;
4478 rgba[i][ACOMP] = 1.0F;
4479 }
4480 break;
4481 case GL_LUMINANCE_ALPHA:
4482 for (i = 0; i < n; i++) {
4483 rgba[i][GCOMP] = 0.0F;
4484 rgba[i][BCOMP] = 0.0F;
4485 }
4486 break;
4487 case GL_RGB:
4488 for (i = 0; i < n; i++) {
4489 rgba[i][ACOMP] = 1.0F;
4490 }
4491 break;
4492 case GL_RG:
4493 for (i = 0; i < n; i++) {
4494 rgba[i][BCOMP] = 0.0F;
4495 rgba[i][ACOMP] = 1.0F;
4496 }
4497 break;
4498 case GL_RED:
4499 for (i = 0; i < n; i++) {
4500 rgba[i][GCOMP] = 0.0F;
4501 rgba[i][BCOMP] = 0.0F;
4502 rgba[i][ACOMP] = 1.0F;
4503 }
4504 break;
4505
4506 default:
4507 /* no-op */
4508 ;
4509 }
4510 }
4511
4512
4513 /**
4514 * As above, but GLuint components.
4515 */
4516 void
4517 _mesa_rebase_rgba_uint(GLuint n, GLuint rgba[][4], GLenum baseFormat)
4518 {
4519 GLuint i;
4520
4521 switch (baseFormat) {
4522 case GL_ALPHA:
4523 for (i = 0; i < n; i++) {
4524 rgba[i][RCOMP] = 0;
4525 rgba[i][GCOMP] = 0;
4526 rgba[i][BCOMP] = 0;
4527 }
4528 break;
4529 case GL_INTENSITY:
4530 /* fall-through */
4531 case GL_LUMINANCE:
4532 for (i = 0; i < n; i++) {
4533 rgba[i][GCOMP] = 0;
4534 rgba[i][BCOMP] = 0;
4535 rgba[i][ACOMP] = 1;
4536 }
4537 break;
4538 case GL_LUMINANCE_ALPHA:
4539 for (i = 0; i < n; i++) {
4540 rgba[i][GCOMP] = 0;
4541 rgba[i][BCOMP] = 0;
4542 }
4543 break;
4544 case GL_RGB:
4545 for (i = 0; i < n; i++) {
4546 rgba[i][ACOMP] = 1;
4547 }
4548 break;
4549 case GL_RG:
4550 for (i = 0; i < n; i++) {
4551 rgba[i][BCOMP] = 0;
4552 rgba[i][ACOMP] = 1;
4553 }
4554 break;
4555 case GL_RED:
4556 for (i = 0; i < n; i++) {
4557 rgba[i][GCOMP] = 0;
4558 rgba[i][BCOMP] = 0;
4559 rgba[i][ACOMP] = 1;
4560 }
4561 default:
4562 /* no-op */
4563 ;
4564 }
4565 }
4566
4567