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 
59 
60 /**
61  * Flip the 8 bits in each byte of the given array.
62  *
63  * \param p array.
64  * \param n number of bytes.
65  *
66  * \todo try this trick to flip bytes someday:
67  * \code
68  *  v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
69  *  v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
70  *  v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
71  * \endcode
72  */
73 static void
flip_bytes(GLubyte * p,GLuint n)74 flip_bytes( GLubyte *p, GLuint n )
75 {
76    GLuint i, a, b;
77    for (i = 0; i < n; i++) {
78       b = (GLuint) p[i];        /* words are often faster than bytes */
79       a = ((b & 0x01) << 7) |
80 	  ((b & 0x02) << 5) |
81 	  ((b & 0x04) << 3) |
82 	  ((b & 0x08) << 1) |
83 	  ((b & 0x10) >> 1) |
84 	  ((b & 0x20) >> 3) |
85 	  ((b & 0x40) >> 5) |
86 	  ((b & 0x80) >> 7);
87       p[i] = (GLubyte) a;
88    }
89 }
90 
91 
92 
93 /*
94  * Unpack a 32x32 pixel polygon stipple from user memory using the
95  * current pixel unpack settings.
96  */
97 void
_mesa_unpack_polygon_stipple(const GLubyte * pattern,GLuint dest[32],const struct gl_pixelstore_attrib * unpacking)98 _mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
99                               const struct gl_pixelstore_attrib *unpacking )
100 {
101    GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap(32, 32, pattern, unpacking);
102    if (ptrn) {
103       /* Convert pattern from GLubytes to GLuints and handle big/little
104        * endian differences
105        */
106       GLubyte *p = ptrn;
107       GLint i;
108       for (i = 0; i < 32; i++) {
109          dest[i] = (p[0] << 24)
110                  | (p[1] << 16)
111                  | (p[2] <<  8)
112                  | (p[3]      );
113          p += 4;
114       }
115       free(ptrn);
116    }
117 }
118 
119 
120 /*
121  * Pack polygon stipple into user memory given current pixel packing
122  * settings.
123  */
124 void
_mesa_pack_polygon_stipple(const GLuint pattern[32],GLubyte * dest,const struct gl_pixelstore_attrib * packing)125 _mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
126                             const struct gl_pixelstore_attrib *packing )
127 {
128    /* Convert pattern from GLuints to GLubytes to handle big/little
129     * endian differences.
130     */
131    GLubyte ptrn[32*4];
132    GLint i;
133    for (i = 0; i < 32; i++) {
134       ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
135       ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
136       ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
137       ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
138    }
139 
140    _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
141 }
142 
143 
144 /*
145  * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
146  * order with row alignment = 1 byte.
147  */
148 GLvoid *
_mesa_unpack_bitmap(GLint width,GLint height,const GLubyte * pixels,const struct gl_pixelstore_attrib * packing)149 _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
150                      const struct gl_pixelstore_attrib *packing )
151 {
152    GLint bytes, row, width_in_bytes;
153    GLubyte *buffer, *dst;
154 
155    if (!pixels)
156       return NULL;
157 
158    /* Alloc dest storage */
159    bytes = ((width + 7) / 8 * height);
160    buffer = (GLubyte *) malloc( bytes );
161    if (!buffer)
162       return NULL;
163 
164    width_in_bytes = CEILING( width, 8 );
165    dst = buffer;
166    for (row = 0; row < height; row++) {
167       const GLubyte *src = (const GLubyte *)
168          _mesa_image_address2d(packing, pixels, width, height,
169                                GL_COLOR_INDEX, GL_BITMAP, row, 0);
170       if (!src) {
171          free(buffer);
172          return NULL;
173       }
174 
175       if ((packing->SkipPixels & 7) == 0) {
176          memcpy( dst, src, width_in_bytes );
177          if (packing->LsbFirst) {
178             flip_bytes( dst, width_in_bytes );
179          }
180       }
181       else {
182          /* handling SkipPixels is a bit tricky (no pun intended!) */
183          GLint i;
184          if (packing->LsbFirst) {
185             GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
186             GLubyte dstMask = 128;
187             const GLubyte *s = src;
188             GLubyte *d = dst;
189             *d = 0;
190             for (i = 0; i < width; i++) {
191                if (*s & srcMask) {
192                   *d |= dstMask;
193                }
194                if (srcMask == 128) {
195                   srcMask = 1;
196                   s++;
197                }
198                else {
199                   srcMask = srcMask << 1;
200                }
201                if (dstMask == 1) {
202                   dstMask = 128;
203                   d++;
204                   *d = 0;
205                }
206                else {
207                   dstMask = dstMask >> 1;
208                }
209             }
210          }
211          else {
212             GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
213             GLubyte dstMask = 128;
214             const GLubyte *s = src;
215             GLubyte *d = dst;
216             *d = 0;
217             for (i = 0; i < width; i++) {
218                if (*s & srcMask) {
219                   *d |= dstMask;
220                }
221                if (srcMask == 1) {
222                   srcMask = 128;
223                   s++;
224                }
225                else {
226                   srcMask = srcMask >> 1;
227                }
228                if (dstMask == 1) {
229                   dstMask = 128;
230                   d++;
231                   *d = 0;
232                }
233                else {
234                   dstMask = dstMask >> 1;
235                }
236             }
237          }
238       }
239       dst += width_in_bytes;
240    }
241 
242    return buffer;
243 }
244 
245 
246 /*
247  * Pack bitmap data.
248  */
249 void
_mesa_pack_bitmap(GLint width,GLint height,const GLubyte * source,GLubyte * dest,const struct gl_pixelstore_attrib * packing)250 _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
251                    GLubyte *dest, const struct gl_pixelstore_attrib *packing )
252 {
253    GLint row, width_in_bytes;
254    const GLubyte *src;
255 
256    if (!source)
257       return;
258 
259    width_in_bytes = CEILING( width, 8 );
260    src = source;
261    for (row = 0; row < height; row++) {
262       GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
263                        width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
264       if (!dst)
265          return;
266 
267       if ((packing->SkipPixels & 7) == 0) {
268          memcpy( dst, src, width_in_bytes );
269          if (packing->LsbFirst) {
270             flip_bytes( dst, width_in_bytes );
271          }
272       }
273       else {
274          /* handling SkipPixels is a bit tricky (no pun intended!) */
275          GLint i;
276          if (packing->LsbFirst) {
277             GLubyte srcMask = 128;
278             GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
279             const GLubyte *s = src;
280             GLubyte *d = dst;
281             *d = 0;
282             for (i = 0; i < width; i++) {
283                if (*s & srcMask) {
284                   *d |= dstMask;
285                }
286                if (srcMask == 1) {
287                   srcMask = 128;
288                   s++;
289                }
290                else {
291                   srcMask = srcMask >> 1;
292                }
293                if (dstMask == 128) {
294                   dstMask = 1;
295                   d++;
296                   *d = 0;
297                }
298                else {
299                   dstMask = dstMask << 1;
300                }
301             }
302          }
303          else {
304             GLubyte srcMask = 128;
305             GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
306             const GLubyte *s = src;
307             GLubyte *d = dst;
308             *d = 0;
309             for (i = 0; i < width; i++) {
310                if (*s & srcMask) {
311                   *d |= dstMask;
312                }
313                if (srcMask == 1) {
314                   srcMask = 128;
315                   s++;
316                }
317                else {
318                   srcMask = srcMask >> 1;
319                }
320                if (dstMask == 1) {
321                   dstMask = 128;
322                   d++;
323                   *d = 0;
324                }
325                else {
326                   dstMask = dstMask >> 1;
327                }
328             }
329          }
330       }
331       src += width_in_bytes;
332    }
333 }
334 
335 
336 /**
337  * Get indexes of color components for a basic color format, such as
338  * GL_RGBA, GL_RED, GL_LUMINANCE_ALPHA, etc.  Return -1 for indexes
339  * that do not apply.
340  */
341 static void
get_component_indexes(GLenum format,GLint * redIndex,GLint * greenIndex,GLint * blueIndex,GLint * alphaIndex,GLint * luminanceIndex,GLint * intensityIndex)342 get_component_indexes(GLenum format,
343                       GLint *redIndex,
344                       GLint *greenIndex,
345                       GLint *blueIndex,
346                       GLint *alphaIndex,
347                       GLint *luminanceIndex,
348                       GLint *intensityIndex)
349 {
350    *redIndex = -1;
351    *greenIndex = -1;
352    *blueIndex = -1;
353    *alphaIndex = -1;
354    *luminanceIndex = -1;
355    *intensityIndex = -1;
356 
357    switch (format) {
358    case GL_LUMINANCE:
359    case GL_LUMINANCE_INTEGER_EXT:
360       *luminanceIndex = 0;
361       break;
362    case GL_LUMINANCE_ALPHA:
363    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
364       *luminanceIndex = 0;
365       *alphaIndex = 1;
366       break;
367    case GL_INTENSITY:
368       *intensityIndex = 0;
369       break;
370    case GL_RED:
371    case GL_RED_INTEGER_EXT:
372       *redIndex = 0;
373       break;
374    case GL_GREEN:
375    case GL_GREEN_INTEGER_EXT:
376       *greenIndex = 0;
377       break;
378    case GL_BLUE:
379    case GL_BLUE_INTEGER_EXT:
380       *blueIndex = 0;
381       break;
382    case GL_ALPHA:
383    case GL_ALPHA_INTEGER_EXT:
384       *alphaIndex = 0;
385       break;
386    case GL_RG:
387    case GL_RG_INTEGER:
388       *redIndex = 0;
389       *greenIndex = 1;
390       break;
391    case GL_RGB:
392    case GL_RGB_INTEGER_EXT:
393       *redIndex = 0;
394       *greenIndex = 1;
395       *blueIndex = 2;
396       break;
397    case GL_BGR:
398    case GL_BGR_INTEGER_EXT:
399       *blueIndex = 0;
400       *greenIndex = 1;
401       *redIndex = 2;
402       break;
403    case GL_RGBA:
404    case GL_RGBA_INTEGER_EXT:
405       *redIndex = 0;
406       *greenIndex = 1;
407       *blueIndex = 2;
408       *alphaIndex = 3;
409       break;
410    case GL_BGRA:
411    case GL_BGRA_INTEGER:
412       *redIndex = 2;
413       *greenIndex = 1;
414       *blueIndex = 0;
415       *alphaIndex = 3;
416       break;
417    case GL_ABGR_EXT:
418       *redIndex = 3;
419       *greenIndex = 2;
420       *blueIndex = 1;
421       *alphaIndex = 0;
422       break;
423    case GL_DU8DV8_ATI:
424    case GL_DUDV_ATI:
425       *redIndex = 0;
426       *greenIndex = 1;
427       break;
428    default:
429       assert(0 && "bad format in get_component_indexes()");
430    }
431 }
432 
433 
434 
435 /**
436  * For small integer types, return the min and max possible values.
437  * Used for clamping floats to unscaled integer types.
438  * \return GL_TRUE if type is handled, GL_FALSE otherwise.
439  */
440 static GLboolean
get_type_min_max(GLenum type,GLfloat * min,GLfloat * max)441 get_type_min_max(GLenum type, GLfloat *min, GLfloat *max)
442 {
443    switch (type) {
444    case GL_BYTE:
445       *min = -128.0;
446       *max = 127.0;
447       return GL_TRUE;
448    case GL_UNSIGNED_BYTE:
449       *min = 0.0;
450       *max = 255.0;
451       return GL_TRUE;
452    case GL_SHORT:
453       *min = -32768.0;
454       *max = 32767.0;
455       return GL_TRUE;
456    case GL_UNSIGNED_SHORT:
457       *min = 0.0;
458       *max = 65535.0;
459       return GL_TRUE;
460    default:
461       return GL_FALSE;
462    }
463 }
464 
465 /* Customization of unsigned integer packing.
466  */
467 #define SRC_TYPE GLuint
468 
469 #define DST_TYPE GLuint
470 #define SRC_CONVERT(x) (x)
471 #define FN_NAME pack_uint_from_uint_rgba
472 #include "pack_tmp.h"
473 #undef DST_TYPE
474 #undef SRC_CONVERT
475 #undef FN_NAME
476 
477 #define DST_TYPE GLint
478 #define SRC_CONVERT(x) MIN2(x, 0x7fffffff)
479 #define FN_NAME pack_int_from_uint_rgba
480 #include "pack_tmp.h"
481 #undef DST_TYPE
482 #undef SRC_CONVERT
483 #undef FN_NAME
484 
485 #define DST_TYPE GLushort
486 #define SRC_CONVERT(x) MIN2(x, 0xffff)
487 #define FN_NAME pack_ushort_from_uint_rgba
488 #include "pack_tmp.h"
489 #undef DST_TYPE
490 #undef SRC_CONVERT
491 #undef FN_NAME
492 
493 #define DST_TYPE GLshort
494 #define SRC_CONVERT(x) CLAMP((int)x, -32768, 32767)
495 #define FN_NAME pack_short_from_uint_rgba
496 #include "pack_tmp.h"
497 #undef DST_TYPE
498 #undef SRC_CONVERT
499 #undef FN_NAME
500 
501 #define DST_TYPE GLubyte
502 #define SRC_CONVERT(x) MIN2(x, 0xff)
503 #define FN_NAME pack_ubyte_from_uint_rgba
504 #include "pack_tmp.h"
505 #undef DST_TYPE
506 #undef SRC_CONVERT
507 #undef FN_NAME
508 
509 #define DST_TYPE GLbyte
510 #define SRC_CONVERT(x) CLAMP((int)x, -128, 127)
511 #define FN_NAME pack_byte_from_uint_rgba
512 #include "pack_tmp.h"
513 #undef DST_TYPE
514 #undef SRC_CONVERT
515 #undef FN_NAME
516 
517 #undef SRC_TYPE
518 
519 static void
_pack_rgba_span_from_uints_problem(struct gl_context * ctx,GLenum dstFormat,GLenum dstType)520 _pack_rgba_span_from_uints_problem(struct gl_context *ctx,
521                                    GLenum dstFormat, GLenum dstType)
522 {
523    _mesa_problem(ctx,
524                  "Unsupported type (%s) / format (%s) "
525                  "in _mesa_pack_rgba_span_from_uints",
526                  _mesa_lookup_enum_by_nr(dstType),
527                  _mesa_lookup_enum_by_nr(dstFormat));
528 }
529 
530 void
_mesa_pack_rgba_span_from_uints(struct gl_context * ctx,GLuint n,GLuint rgba[][4],GLenum dstFormat,GLenum dstType,GLvoid * dstAddr)531 _mesa_pack_rgba_span_from_uints(struct gl_context *ctx, GLuint n, GLuint rgba[][4],
532                                 GLenum dstFormat, GLenum dstType,
533                                 GLvoid *dstAddr)
534 {
535    GLuint i;
536 
537    switch(dstType) {
538    case GL_UNSIGNED_INT:
539       pack_uint_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
540       break;
541    case GL_INT:
542       pack_int_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
543       break;
544    case GL_UNSIGNED_SHORT:
545       pack_ushort_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
546       break;
547    case GL_SHORT:
548       pack_short_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
549       break;
550    case GL_UNSIGNED_BYTE:
551       pack_ubyte_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
552       break;
553    case GL_BYTE:
554       pack_byte_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
555       break;
556    case GL_UNSIGNED_BYTE_3_3_2:
557       if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
558          GLubyte *dst = (GLubyte *) dstAddr;
559          for (i=0;i<n;i++) {
560             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 7) << 5)
561                    | (CLAMP(rgba[i][GCOMP], 0, 7) << 2)
562                    | (CLAMP(rgba[i][BCOMP], 0, 3)     );
563          }
564       } else {
565          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
566       }
567       break;
568    case GL_UNSIGNED_BYTE_2_3_3_REV:
569       if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
570          GLubyte *dst = (GLubyte *) dstAddr;
571          for (i=0;i<n;i++) {
572             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 7)     )
573                    | (CLAMP(rgba[i][GCOMP], 0, 7) << 3)
574                    | (CLAMP(rgba[i][BCOMP], 0, 3) << 6);
575          }
576       } else {
577          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
578       }
579       break;
580    case GL_UNSIGNED_SHORT_5_6_5:
581       if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
582          GLushort *dst = (GLushort *) dstAddr;
583          for (i=0;i<n;i++) {
584             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31) << 11)
585                    | (CLAMP(rgba[i][GCOMP], 0, 63) <<  5)
586                    | (CLAMP(rgba[i][BCOMP], 0, 31)      );
587          }
588       } else {
589          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
590       }
591       break;
592    case GL_UNSIGNED_SHORT_5_6_5_REV:
593       if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
594          GLushort *dst = (GLushort *) dstAddr;
595          for (i=0;i<n;i++) {
596             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31)      )
597                    | (CLAMP(rgba[i][GCOMP], 0, 63) <<  5)
598                    | (CLAMP(rgba[i][BCOMP], 0, 31) << 11);
599          }
600       } else {
601          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
602       }
603       break;
604    case GL_UNSIGNED_SHORT_4_4_4_4:
605       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
606          GLushort *dst = (GLushort *) dstAddr;
607          for (i=0;i<n;i++) {
608             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 15) << 12)
609                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
610                    | (CLAMP(rgba[i][BCOMP], 0, 15) <<  4)
611                    | (CLAMP(rgba[i][ACOMP], 0, 15)      );
612          }
613       }
614       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
615          GLushort *dst = (GLushort *) dstAddr;
616          for (i=0;i<n;i++) {
617             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 15) << 12)
618                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
619                    | (CLAMP(rgba[i][RCOMP], 0, 15) <<  4)
620                    | (CLAMP(rgba[i][ACOMP], 0, 15)      );
621          }
622       }
623       else if (dstFormat == GL_ABGR_EXT) {
624          GLushort *dst = (GLushort *) dstAddr;
625          for (i=0;i<n;i++) {
626             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 15) << 12)
627                    | (CLAMP(rgba[i][BCOMP], 0, 15) <<  8)
628                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
629                    | (CLAMP(rgba[i][RCOMP], 0, 15)      );
630          }
631       } else {
632          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
633       }
634       break;
635    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
636       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
637          GLushort *dst = (GLushort *) dstAddr;
638          for (i=0;i<n;i++) {
639             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 15)      )
640                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
641                    | (CLAMP(rgba[i][BCOMP], 0, 15) <<  8)
642                    | (CLAMP(rgba[i][ACOMP], 0, 15) << 12);
643          }
644       }
645       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
646          GLushort *dst = (GLushort *) dstAddr;
647          for (i=0;i<n;i++) {
648             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 15)      )
649                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
650                    | (CLAMP(rgba[i][RCOMP], 0, 15) <<  8)
651                    | (CLAMP(rgba[i][ACOMP], 0, 15) << 12);
652          }
653       }
654       else if (dstFormat == GL_ABGR_EXT) {
655          GLushort *dst = (GLushort *) dstAddr;
656          for (i=0;i<n;i++) {
657             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 15)      )
658                    | (CLAMP(rgba[i][BCOMP], 0, 15) <<  4)
659                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
660                    | (CLAMP(rgba[i][RCOMP], 0, 15) << 12);
661          }
662       } else {
663          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
664       }
665       break;
666    case GL_UNSIGNED_SHORT_5_5_5_1:
667       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
668          GLushort *dst = (GLushort *) dstAddr;
669          for (i=0;i<n;i++) {
670             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31) << 11)
671                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  6)
672                    | (CLAMP(rgba[i][BCOMP], 0, 31) <<  1)
673                    | (CLAMP(rgba[i][ACOMP], 0,  1)      );
674          }
675       }
676       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
677          GLushort *dst = (GLushort *) dstAddr;
678          for (i=0;i<n;i++) {
679             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 31) << 11)
680                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  6)
681                    | (CLAMP(rgba[i][RCOMP], 0, 31) <<  1)
682                    | (CLAMP(rgba[i][ACOMP], 0,  1)      );
683          }
684       }
685       else if (dstFormat == GL_ABGR_EXT) {
686          GLushort *dst = (GLushort *) dstAddr;
687          for (i=0;i<n;i++) {
688             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 31) << 11)
689                    | (CLAMP(rgba[i][BCOMP], 0, 31) <<  6)
690                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  1)
691                    | (CLAMP(rgba[i][RCOMP], 0,  1)      );
692          }
693       } else {
694          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
695       }
696       break;
697    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
698       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
699          GLushort *dst = (GLushort *) dstAddr;
700          for (i=0;i<n;i++) {
701             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31)      )
702                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  5)
703                    | (CLAMP(rgba[i][BCOMP], 0, 31) << 10)
704                    | (CLAMP(rgba[i][ACOMP], 0,  1) << 15);
705          }
706       }
707       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
708          GLushort *dst = (GLushort *) dstAddr;
709          for (i=0;i<n;i++) {
710             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 31)      )
711                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  5)
712                    | (CLAMP(rgba[i][RCOMP], 0, 31) << 10)
713                    | (CLAMP(rgba[i][ACOMP], 0,  1) << 15);
714          }
715       }
716       else if (dstFormat == GL_ABGR_EXT) {
717          GLushort *dst = (GLushort *) dstAddr;
718          for (i=0;i<n;i++) {
719             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 31)      )
720                    | (CLAMP(rgba[i][BCOMP], 0, 31) <<  5)
721                    | (CLAMP(rgba[i][GCOMP], 0, 31) << 10)
722                    | (CLAMP(rgba[i][RCOMP], 0,  1) << 15);
723          }
724       } else {
725          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
726       }
727       break;
728    case GL_UNSIGNED_INT_8_8_8_8:
729       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
730          GLuint *dst = (GLuint *) dstAddr;
731          for (i=0;i<n;i++) {
732             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 255) << 24)
733                    | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
734                    | (CLAMP(rgba[i][BCOMP], 0, 255) <<  8)
735                    | (CLAMP(rgba[i][ACOMP], 0, 255)      );
736          }
737       }
738       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
739          GLuint *dst = (GLuint *) dstAddr;
740          for (i=0;i<n;i++) {
741             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 255) << 24)
742                    | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
743                    | (CLAMP(rgba[i][RCOMP], 0, 255) <<  8)
744                    | (CLAMP(rgba[i][ACOMP], 0, 255)      );
745          }
746       }
747       else if (dstFormat == GL_ABGR_EXT) {
748          GLuint *dst = (GLuint *) dstAddr;
749          for (i=0;i<n;i++) {
750             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 255) << 24)
751                    | (CLAMP(rgba[i][BCOMP], 0, 255) << 16)
752                    | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
753                    | (CLAMP(rgba[i][RCOMP], 0, 255)      );
754          }
755       } else {
756          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
757       }
758       break;
759    case GL_UNSIGNED_INT_8_8_8_8_REV:
760       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
761          GLuint *dst = (GLuint *) dstAddr;
762          for (i=0;i<n;i++) {
763             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 255)      )
764                    | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
765                    | (CLAMP(rgba[i][BCOMP], 0, 255) << 16)
766                    | (CLAMP(rgba[i][ACOMP], 0, 255) << 24);
767          }
768       }
769       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
770          GLuint *dst = (GLuint *) dstAddr;
771          for (i=0;i<n;i++) {
772             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 255)      )
773                    | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
774                    | (CLAMP(rgba[i][RCOMP], 0, 255) << 16)
775                    | (CLAMP(rgba[i][ACOMP], 0, 255) << 24);
776          }
777       }
778       else if (dstFormat == GL_ABGR_EXT) {
779          GLuint *dst = (GLuint *) dstAddr;
780          for (i=0;i<n;i++) {
781             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 255)      )
782                    | (CLAMP(rgba[i][BCOMP], 0, 255) <<  8)
783                    | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
784                    | (CLAMP(rgba[i][RCOMP], 0, 255) << 24);
785          }
786       } else {
787          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
788       }
789       break;
790    case GL_UNSIGNED_INT_10_10_10_2:
791       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
792          GLuint *dst = (GLuint *) dstAddr;
793          for (i=0;i<n;i++) {
794             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 1023) << 22)
795                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 12)
796                    | (CLAMP(rgba[i][BCOMP], 0, 1023) <<  2)
797                    | (CLAMP(rgba[i][ACOMP], 0,    3)      );
798          }
799       }
800       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
801          GLuint *dst = (GLuint *) dstAddr;
802          for (i=0;i<n;i++) {
803             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 1023) << 22)
804                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 12)
805                    | (CLAMP(rgba[i][RCOMP], 0, 1023) <<  2)
806                    | (CLAMP(rgba[i][ACOMP], 0,    3)      );
807          }
808       }
809       else if (dstFormat == GL_ABGR_EXT) {
810          GLuint *dst = (GLuint *) dstAddr;
811          for (i=0;i<n;i++) {
812             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 1023) << 22)
813                    | (CLAMP(rgba[i][BCOMP], 0, 1023) << 12)
814                    | (CLAMP(rgba[i][GCOMP], 0, 1023) <<  2)
815                    | (CLAMP(rgba[i][RCOMP], 0,    3)      );
816          }
817       } else {
818          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
819       }
820       break;
821    case GL_UNSIGNED_INT_2_10_10_10_REV:
822       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
823          GLuint *dst = (GLuint *) dstAddr;
824          for (i=0;i<n;i++) {
825             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 1023)      )
826                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 10)
827                    | (CLAMP(rgba[i][BCOMP], 0, 1023) << 20)
828                    | (CLAMP(rgba[i][ACOMP], 0,    3) << 30);
829          }
830       }
831       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
832          GLuint *dst = (GLuint *) dstAddr;
833          for (i=0;i<n;i++) {
834             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 1023)      )
835                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 10)
836                    | (CLAMP(rgba[i][RCOMP], 0, 1023) << 20)
837                    | (CLAMP(rgba[i][ACOMP], 0,    3) << 30);
838          }
839       }
840       else if (dstFormat == GL_ABGR_EXT) {
841          GLuint *dst = (GLuint *) dstAddr;
842          for (i=0;i<n;i++) {
843             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 1023)      )
844                    | (CLAMP(rgba[i][BCOMP], 0, 1023) << 10)
845                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 20)
846                    | (CLAMP(rgba[i][RCOMP], 0,    3) << 30);
847          }
848       } else {
849          _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
850       }
851       break;
852    default:
853       _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
854       return;
855    }
856 }
857 
858 
859 /* Customization of signed integer packing.
860  */
861 #define SRC_TYPE GLint
862 
863 #define DST_TYPE GLuint
864 #define SRC_CONVERT(x) MAX2(x, 0)
865 #define FN_NAME pack_uint_from_int_rgba
866 #include "pack_tmp.h"
867 #undef DST_TYPE
868 #undef SRC_CONVERT
869 #undef FN_NAME
870 
871 #define DST_TYPE GLushort
872 #define SRC_CONVERT(x) MAX2(x, 0)
873 #define FN_NAME pack_ushort_from_int_rgba
874 #include "pack_tmp.h"
875 #undef DST_TYPE
876 #undef SRC_CONVERT
877 #undef FN_NAME
878 
879 #define DST_TYPE GLshort
880 #define SRC_CONVERT(x) CLAMP(x, -0x8000, 0x7fff)
881 #define FN_NAME pack_short_from_int_rgba
882 #include "pack_tmp.h"
883 #undef DST_TYPE
884 #undef SRC_CONVERT
885 #undef FN_NAME
886 
887 #define DST_TYPE GLubyte
888 #define SRC_CONVERT(x) MAX2(x, 0)
889 #define FN_NAME pack_ubyte_from_int_rgba
890 #include "pack_tmp.h"
891 #undef DST_TYPE
892 #undef SRC_CONVERT
893 #undef FN_NAME
894 
895 #define DST_TYPE GLbyte
896 #define SRC_CONVERT(x) CLAMP(x, -0x80, 0x7f)
897 #define FN_NAME pack_byte_from_int_rgba
898 #include "pack_tmp.h"
899 #undef DST_TYPE
900 #undef SRC_CONVERT
901 #undef FN_NAME
902 
903 #undef SRC_TYPE
904 
905 static void
_pack_rgba_span_from_ints_problem(struct gl_context * ctx,GLenum dstFormat,GLenum dstType)906 _pack_rgba_span_from_ints_problem(struct gl_context *ctx,
907                                    GLenum dstFormat, GLenum dstType)
908 {
909    _mesa_problem(ctx,
910                  "Unsupported type (%s) / format (%s) "
911                  "in _mesa_pack_rgba_span_from_ints",
912                  _mesa_lookup_enum_by_nr(dstType),
913                  _mesa_lookup_enum_by_nr(dstFormat));
914 }
915 
916 void
_mesa_pack_rgba_span_from_ints(struct gl_context * ctx,GLuint n,GLint rgba[][4],GLenum dstFormat,GLenum dstType,GLvoid * dstAddr)917 _mesa_pack_rgba_span_from_ints(struct gl_context *ctx, GLuint n, GLint rgba[][4],
918                                GLenum dstFormat, GLenum dstType,
919                                GLvoid *dstAddr)
920 {
921    GLuint i;
922 
923    switch(dstType) {
924    case GL_UNSIGNED_INT:
925       pack_uint_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
926       break;
927    case GL_INT:
928       /* No conversion necessary. */
929       pack_uint_from_uint_rgba(ctx, dstAddr, dstFormat, (GLuint (*)[4]) rgba, n);
930       break;
931    case GL_UNSIGNED_SHORT:
932       pack_ushort_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
933       break;
934    case GL_SHORT:
935       pack_short_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
936       break;
937    case GL_UNSIGNED_BYTE:
938       pack_ubyte_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
939       break;
940    case GL_BYTE:
941       pack_byte_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
942       break;
943    case GL_UNSIGNED_BYTE_3_3_2:
944       if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
945          GLubyte *dst = (GLubyte *) dstAddr;
946          for (i=0;i<n;i++) {
947             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 7) << 5)
948                    | (CLAMP(rgba[i][GCOMP], 0, 7) << 2)
949                    | (CLAMP(rgba[i][BCOMP], 0, 3)     );
950          }
951       } else {
952          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
953       }
954       break;
955    case GL_UNSIGNED_BYTE_2_3_3_REV:
956       if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
957          GLubyte *dst = (GLubyte *) dstAddr;
958          for (i=0;i<n;i++) {
959             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 7)     )
960                    | (CLAMP(rgba[i][GCOMP], 0, 7) << 3)
961                    | (CLAMP(rgba[i][BCOMP], 0, 3) << 6);
962          }
963       } else {
964          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
965       }
966       break;
967    case GL_UNSIGNED_SHORT_5_6_5:
968       if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
969          GLushort *dst = (GLushort *) dstAddr;
970          for (i=0;i<n;i++) {
971             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31) << 11)
972                    | (CLAMP(rgba[i][GCOMP], 0, 63) <<  5)
973                    | (CLAMP(rgba[i][BCOMP], 0, 31)      );
974          }
975       } else {
976          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
977       }
978       break;
979    case GL_UNSIGNED_SHORT_5_6_5_REV:
980       if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
981          GLushort *dst = (GLushort *) dstAddr;
982          for (i=0;i<n;i++) {
983             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31)      )
984                    | (CLAMP(rgba[i][GCOMP], 0, 63) <<  5)
985                    | (CLAMP(rgba[i][BCOMP], 0, 31) << 11);
986          }
987       } else {
988          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
989       }
990       break;
991    case GL_UNSIGNED_SHORT_4_4_4_4:
992       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
993          GLushort *dst = (GLushort *) dstAddr;
994          for (i=0;i<n;i++) {
995             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 15) << 12)
996                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
997                    | (CLAMP(rgba[i][BCOMP], 0, 15) <<  4)
998                    | (CLAMP(rgba[i][ACOMP], 0, 15)      );
999          }
1000       }
1001       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1002          GLushort *dst = (GLushort *) dstAddr;
1003          for (i=0;i<n;i++) {
1004             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 15) << 12)
1005                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
1006                    | (CLAMP(rgba[i][RCOMP], 0, 15) <<  4)
1007                    | (CLAMP(rgba[i][ACOMP], 0, 15)      );
1008          }
1009       }
1010       else if (dstFormat == GL_ABGR_EXT) {
1011          GLushort *dst = (GLushort *) dstAddr;
1012          for (i=0;i<n;i++) {
1013             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 15) << 12)
1014                    | (CLAMP(rgba[i][BCOMP], 0, 15) <<  8)
1015                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
1016                    | (CLAMP(rgba[i][RCOMP], 0, 15)      );
1017          }
1018       } else {
1019          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1020       }
1021       break;
1022    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1023       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1024          GLushort *dst = (GLushort *) dstAddr;
1025          for (i=0;i<n;i++) {
1026             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 15)      )
1027                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
1028                    | (CLAMP(rgba[i][BCOMP], 0, 15) <<  8)
1029                    | (CLAMP(rgba[i][ACOMP], 0, 15) << 12);
1030          }
1031       }
1032       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1033          GLushort *dst = (GLushort *) dstAddr;
1034          for (i=0;i<n;i++) {
1035             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 15)      )
1036                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
1037                    | (CLAMP(rgba[i][RCOMP], 0, 15) <<  8)
1038                    | (CLAMP(rgba[i][ACOMP], 0, 15) << 12);
1039          }
1040       }
1041       else if (dstFormat == GL_ABGR_EXT) {
1042          GLushort *dst = (GLushort *) dstAddr;
1043          for (i=0;i<n;i++) {
1044             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 15)      )
1045                    | (CLAMP(rgba[i][BCOMP], 0, 15) <<  4)
1046                    | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
1047                    | (CLAMP(rgba[i][RCOMP], 0, 15) << 12);
1048          }
1049       } else {
1050          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1051       }
1052       break;
1053    case GL_UNSIGNED_SHORT_5_5_5_1:
1054       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1055          GLushort *dst = (GLushort *) dstAddr;
1056          for (i=0;i<n;i++) {
1057             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31) << 11)
1058                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  6)
1059                    | (CLAMP(rgba[i][BCOMP], 0, 31) <<  1)
1060                    | (CLAMP(rgba[i][ACOMP], 0,  1)      );
1061          }
1062       }
1063       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1064          GLushort *dst = (GLushort *) dstAddr;
1065          for (i=0;i<n;i++) {
1066             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 31) << 11)
1067                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  6)
1068                    | (CLAMP(rgba[i][RCOMP], 0, 31) <<  1)
1069                    | (CLAMP(rgba[i][ACOMP], 0,  1)      );
1070          }
1071       }
1072       else if (dstFormat == GL_ABGR_EXT) {
1073          GLushort *dst = (GLushort *) dstAddr;
1074          for (i=0;i<n;i++) {
1075             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 31) << 11)
1076                    | (CLAMP(rgba[i][BCOMP], 0, 31) <<  6)
1077                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  1)
1078                    | (CLAMP(rgba[i][RCOMP], 0,  1)      );
1079          }
1080       } else {
1081          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1082       }
1083       break;
1084    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1085       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1086          GLushort *dst = (GLushort *) dstAddr;
1087          for (i=0;i<n;i++) {
1088             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31)      )
1089                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  5)
1090                    | (CLAMP(rgba[i][BCOMP], 0, 31) << 10)
1091                    | (CLAMP(rgba[i][ACOMP], 0,  1) << 15);
1092          }
1093       }
1094       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1095          GLushort *dst = (GLushort *) dstAddr;
1096          for (i=0;i<n;i++) {
1097             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 31)      )
1098                    | (CLAMP(rgba[i][GCOMP], 0, 31) <<  5)
1099                    | (CLAMP(rgba[i][RCOMP], 0, 31) << 10)
1100                    | (CLAMP(rgba[i][ACOMP], 0,  1) << 15);
1101          }
1102       }
1103       else if (dstFormat == GL_ABGR_EXT) {
1104          GLushort *dst = (GLushort *) dstAddr;
1105          for (i=0;i<n;i++) {
1106             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 31)      )
1107                    | (CLAMP(rgba[i][BCOMP], 0, 31) <<  5)
1108                    | (CLAMP(rgba[i][GCOMP], 0, 31) << 10)
1109                    | (CLAMP(rgba[i][RCOMP], 0,  1) << 15);
1110          }
1111       } else {
1112          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1113       }
1114       break;
1115    case GL_UNSIGNED_INT_8_8_8_8:
1116       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1117          GLuint *dst = (GLuint *) dstAddr;
1118          for (i=0;i<n;i++) {
1119             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 255) << 24)
1120                    | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
1121                    | (CLAMP(rgba[i][BCOMP], 0, 255) <<  8)
1122                    | (CLAMP(rgba[i][ACOMP], 0, 255)      );
1123          }
1124       }
1125       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1126          GLuint *dst = (GLuint *) dstAddr;
1127          for (i=0;i<n;i++) {
1128             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 255) << 24)
1129                    | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
1130                    | (CLAMP(rgba[i][RCOMP], 0, 255) <<  8)
1131                    | (CLAMP(rgba[i][ACOMP], 0, 255)      );
1132          }
1133       }
1134       else if (dstFormat == GL_ABGR_EXT) {
1135          GLuint *dst = (GLuint *) dstAddr;
1136          for (i=0;i<n;i++) {
1137             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 255) << 24)
1138                    | (CLAMP(rgba[i][BCOMP], 0, 255) << 16)
1139                    | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
1140                    | (CLAMP(rgba[i][RCOMP], 0, 255)      );
1141          }
1142       } else {
1143          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1144       }
1145       break;
1146    case GL_UNSIGNED_INT_8_8_8_8_REV:
1147       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1148          GLuint *dst = (GLuint *) dstAddr;
1149          for (i=0;i<n;i++) {
1150             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 255)      )
1151                    | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
1152                    | (CLAMP(rgba[i][BCOMP], 0, 255) << 16)
1153                    | (CLAMP(rgba[i][ACOMP], 0, 255) << 24);
1154          }
1155       }
1156       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1157          GLuint *dst = (GLuint *) dstAddr;
1158          for (i=0;i<n;i++) {
1159             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 255)      )
1160                    | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
1161                    | (CLAMP(rgba[i][RCOMP], 0, 255) << 16)
1162                    | (CLAMP(rgba[i][ACOMP], 0, 255) << 24);
1163          }
1164       }
1165       else if (dstFormat == GL_ABGR_EXT) {
1166          GLuint *dst = (GLuint *) dstAddr;
1167          for (i=0;i<n;i++) {
1168             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 255)      )
1169                    | (CLAMP(rgba[i][BCOMP], 0, 255) <<  8)
1170                    | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
1171                    | (CLAMP(rgba[i][RCOMP], 0, 255) << 24);
1172          }
1173       } else {
1174          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1175       }
1176       break;
1177    case GL_UNSIGNED_INT_10_10_10_2:
1178       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1179          GLuint *dst = (GLuint *) dstAddr;
1180          for (i=0;i<n;i++) {
1181             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 1023) << 22)
1182                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 12)
1183                    | (CLAMP(rgba[i][BCOMP], 0, 1023) <<  2)
1184                    | (CLAMP(rgba[i][ACOMP], 0,    3)      );
1185          }
1186       }
1187       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1188          GLuint *dst = (GLuint *) dstAddr;
1189          for (i=0;i<n;i++) {
1190             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 1023) << 22)
1191                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 12)
1192                    | (CLAMP(rgba[i][RCOMP], 0, 1023) <<  2)
1193                    | (CLAMP(rgba[i][ACOMP], 0,    3)      );
1194          }
1195       }
1196       else if (dstFormat == GL_ABGR_EXT) {
1197          GLuint *dst = (GLuint *) dstAddr;
1198          for (i=0;i<n;i++) {
1199             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 1023) << 22)
1200                    | (CLAMP(rgba[i][BCOMP], 0, 1023) << 12)
1201                    | (CLAMP(rgba[i][GCOMP], 0, 1023) <<  2)
1202                    | (CLAMP(rgba[i][RCOMP], 0,    3)      );
1203          }
1204       } else {
1205          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1206       }
1207       break;
1208    case GL_UNSIGNED_INT_2_10_10_10_REV:
1209       if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1210          GLuint *dst = (GLuint *) dstAddr;
1211          for (i=0;i<n;i++) {
1212             dst[i] = (CLAMP(rgba[i][RCOMP], 0, 1023)      )
1213                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 10)
1214                    | (CLAMP(rgba[i][BCOMP], 0, 1023) << 20)
1215                    | (CLAMP(rgba[i][ACOMP], 0,    3) << 30);
1216          }
1217       }
1218       else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1219          GLuint *dst = (GLuint *) dstAddr;
1220          for (i=0;i<n;i++) {
1221             dst[i] = (CLAMP(rgba[i][BCOMP], 0, 1023)      )
1222                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 10)
1223                    | (CLAMP(rgba[i][RCOMP], 0, 1023) << 20)
1224                    | (CLAMP(rgba[i][ACOMP], 0,    3) << 30);
1225          }
1226       }
1227       else if (dstFormat == GL_ABGR_EXT) {
1228          GLuint *dst = (GLuint *) dstAddr;
1229          for (i=0;i<n;i++) {
1230             dst[i] = (CLAMP(rgba[i][ACOMP], 0, 1023)      )
1231                    | (CLAMP(rgba[i][BCOMP], 0, 1023) << 10)
1232                    | (CLAMP(rgba[i][GCOMP], 0, 1023) << 20)
1233                    | (CLAMP(rgba[i][RCOMP], 0,    3) << 30);
1234          }
1235       } else {
1236          _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1237       }
1238       break;
1239    default:
1240       _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1241       return;
1242    }
1243 }
1244 
1245 
1246 /**
1247  * Used to pack an array [][4] of RGBA float colors as specified
1248  * by the dstFormat, dstType and dstPacking.  Used by glReadPixels.
1249  * Historically, the RGBA values were in [0,1] and rescaled to fit
1250  * into GLubytes, etc.  But with new integer formats, the RGBA values
1251  * may have any value and we don't always rescale when converting to
1252  * integers.
1253  *
1254  * Note: the rgba values will be modified by this function when any pixel
1255  * transfer ops are enabled.
1256  */
1257 void
_mesa_pack_rgba_span_float(struct gl_context * ctx,GLuint n,GLfloat rgba[][4],GLenum dstFormat,GLenum dstType,GLvoid * dstAddr,const struct gl_pixelstore_attrib * dstPacking,GLbitfield transferOps)1258 _mesa_pack_rgba_span_float(struct gl_context *ctx, GLuint n, GLfloat rgba[][4],
1259                            GLenum dstFormat, GLenum dstType,
1260                            GLvoid *dstAddr,
1261                            const struct gl_pixelstore_attrib *dstPacking,
1262                            GLbitfield transferOps)
1263 {
1264    GLfloat *luminance;
1265    const GLint comps = _mesa_components_in_format(dstFormat);
1266    const GLboolean intDstFormat = _mesa_is_enum_format_integer(dstFormat);
1267    GLuint i;
1268 
1269    if (dstFormat == GL_LUMINANCE ||
1270        dstFormat == GL_LUMINANCE_ALPHA ||
1271        dstFormat == GL_LUMINANCE_INTEGER_EXT ||
1272        dstFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT) {
1273       luminance = (GLfloat *) malloc(n * sizeof(GLfloat));
1274       if (!luminance) {
1275          _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
1276          return;
1277       }
1278    }
1279    else {
1280       luminance = NULL;
1281    }
1282 
1283    /* EXT_texture_integer specifies no transfer ops on integer
1284     * types in the resolved issues section. Just set them to 0
1285     * for integer surfaces.
1286     */
1287    if (intDstFormat)
1288       transferOps = 0;
1289 
1290    if (transferOps) {
1291       _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
1292    }
1293 
1294    /*
1295     * Component clamping (besides clamping to [0,1] in
1296     * _mesa_apply_rgba_transfer_ops()).
1297     */
1298    if (intDstFormat) {
1299       /* clamping to dest type's min/max values */
1300       GLfloat min, max;
1301       if (get_type_min_max(dstType, &min, &max)) {
1302          for (i = 0; i < n; i++) {
1303             rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], min, max);
1304             rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], min, max);
1305             rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], min, max);
1306             rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], min, max);
1307          }
1308       }
1309    }
1310    else if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
1311       /* compute luminance values */
1312       if (transferOps & IMAGE_CLAMP_BIT) {
1313          for (i = 0; i < n; i++) {
1314             GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1315             luminance[i] = CLAMP(sum, 0.0F, 1.0F);
1316          }
1317       }
1318       else {
1319          for (i = 0; i < n; i++) {
1320             luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1321          }
1322       }
1323    }
1324 
1325    /*
1326     * Pack/store the pixels.  Ugh!  Lots of cases!!!
1327     */
1328    switch (dstType) {
1329       case GL_UNSIGNED_BYTE:
1330          {
1331             GLubyte *dst = (GLubyte *) dstAddr;
1332             switch (dstFormat) {
1333                case GL_RED:
1334                   for (i=0;i<n;i++)
1335                      dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1336                   break;
1337                case GL_GREEN:
1338                   for (i=0;i<n;i++)
1339                      dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1340                   break;
1341                case GL_BLUE:
1342                   for (i=0;i<n;i++)
1343                      dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1344                   break;
1345                case GL_ALPHA:
1346                   for (i=0;i<n;i++)
1347                      dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1348                   break;
1349                case GL_LUMINANCE:
1350                   for (i=0;i<n;i++)
1351                      dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1352                   break;
1353                case GL_LUMINANCE_ALPHA:
1354                   for (i=0;i<n;i++) {
1355                      dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1356                      dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1357                   }
1358                   break;
1359                case GL_RG:
1360                   for (i=0;i<n;i++) {
1361                      dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1362                      dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1363                   }
1364                   break;
1365                case GL_RGB:
1366                   for (i=0;i<n;i++) {
1367                      dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1368                      dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1369                      dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1370                   }
1371                   break;
1372                case GL_RGBA:
1373                   for (i=0;i<n;i++) {
1374                      dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1375                      dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1376                      dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1377                      dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1378                   }
1379                   break;
1380                case GL_BGR:
1381                   for (i=0;i<n;i++) {
1382                      dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1383                      dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1384                      dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1385                   }
1386                   break;
1387                case GL_BGRA:
1388                   for (i=0;i<n;i++) {
1389                      dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1390                      dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1391                      dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1392                      dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1393                   }
1394                   break;
1395                case GL_ABGR_EXT:
1396                   for (i=0;i<n;i++) {
1397                      dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1398                      dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1399                      dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1400                      dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1401                   }
1402                   break;
1403                case GL_RED_INTEGER_EXT:
1404                   for (i=0;i<n;i++) {
1405                      dst[i] = (GLubyte) rgba[i][RCOMP];
1406                   }
1407                   break;
1408                case GL_GREEN_INTEGER_EXT:
1409                   for (i=0;i<n;i++) {
1410                      dst[i] = (GLubyte) rgba[i][GCOMP];
1411                   }
1412                   break;
1413                case GL_BLUE_INTEGER_EXT:
1414                   for (i=0;i<n;i++) {
1415                      dst[i] = (GLubyte) rgba[i][BCOMP];
1416                   }
1417                   break;
1418                case GL_ALPHA_INTEGER_EXT:
1419                   for (i=0;i<n;i++) {
1420                      dst[i] = (GLubyte) rgba[i][ACOMP];
1421                   }
1422                   break;
1423                case GL_RG_INTEGER:
1424                   for (i=0;i<n;i++) {
1425                      dst[i*2+0] = (GLubyte) rgba[i][RCOMP];
1426                      dst[i*2+1] = (GLubyte) rgba[i][GCOMP];
1427                   }
1428                   break;
1429                case GL_RGB_INTEGER_EXT:
1430                   for (i=0;i<n;i++) {
1431                      dst[i*3+0] = (GLubyte) rgba[i][RCOMP];
1432                      dst[i*3+1] = (GLubyte) rgba[i][GCOMP];
1433                      dst[i*3+2] = (GLubyte) rgba[i][BCOMP];
1434                   }
1435                   break;
1436                case GL_RGBA_INTEGER_EXT:
1437                   for (i=0;i<n;i++) {
1438                      dst[i*4+0] = (GLubyte) rgba[i][RCOMP];
1439                      dst[i*4+1] = (GLubyte) rgba[i][GCOMP];
1440                      dst[i*4+2] = (GLubyte) rgba[i][BCOMP];
1441                      dst[i*4+3] = (GLubyte) rgba[i][ACOMP];
1442                   }
1443                   break;
1444                case GL_BGR_INTEGER_EXT:
1445                   for (i=0;i<n;i++) {
1446                      dst[i*3+0] = (GLubyte) rgba[i][BCOMP];
1447                      dst[i*3+1] = (GLubyte) rgba[i][GCOMP];
1448                      dst[i*3+2] = (GLubyte) rgba[i][RCOMP];
1449                   }
1450                   break;
1451                case GL_BGRA_INTEGER_EXT:
1452                   for (i=0;i<n;i++) {
1453                      dst[i*4+0] = (GLubyte) rgba[i][BCOMP];
1454                      dst[i*4+1] = (GLubyte) rgba[i][GCOMP];
1455                      dst[i*4+2] = (GLubyte) rgba[i][RCOMP];
1456                      dst[i*4+3] = (GLubyte) rgba[i][ACOMP];
1457                   }
1458                   break;
1459                case GL_LUMINANCE_INTEGER_EXT:
1460                   for (i=0;i<n;i++) {
1461                      dst[i*2+0] = (GLubyte) (rgba[i][RCOMP] +
1462                                              rgba[i][GCOMP] +
1463                                              rgba[i][BCOMP]);
1464                      dst[i*2+1] = (GLubyte) rgba[i][ACOMP];
1465                   }
1466                   break;
1467                case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1468                   for (i=0;i<n;i++) {
1469                      dst[i] = (GLubyte) (rgba[i][RCOMP] +
1470                                          rgba[i][GCOMP] +
1471                                          rgba[i][BCOMP]);
1472                   }
1473                   break;
1474                case GL_DUDV_ATI:
1475                case GL_DU8DV8_ATI:
1476                   for (i=0;i<n;i++) {
1477                      dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1478                      dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1479                   }
1480                   break;
1481                default:
1482                   _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1483             }
1484          }
1485          break;
1486       case GL_BYTE:
1487          {
1488             GLbyte *dst = (GLbyte *) dstAddr;
1489             switch (dstFormat) {
1490                case GL_RED:
1491                   for (i=0;i<n;i++)
1492                      dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1493                   break;
1494                case GL_GREEN:
1495                   for (i=0;i<n;i++)
1496                      dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1497                   break;
1498                case GL_BLUE:
1499                   for (i=0;i<n;i++)
1500                      dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1501                   break;
1502                case GL_ALPHA:
1503                   for (i=0;i<n;i++)
1504                      dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1505                   break;
1506                case GL_LUMINANCE:
1507                   for (i=0;i<n;i++)
1508                      dst[i] = FLOAT_TO_BYTE(luminance[i]);
1509                   break;
1510                case GL_LUMINANCE_ALPHA:
1511                   for (i=0;i<n;i++) {
1512                      dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1513                      dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1514                   }
1515                   break;
1516                case GL_RG:
1517                   for (i=0;i<n;i++) {
1518                      dst[i*2+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1519                      dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1520                   }
1521                   break;
1522                case GL_RGB:
1523                   for (i=0;i<n;i++) {
1524                      dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1525                      dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1526                      dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1527                   }
1528                   break;
1529                case GL_RGBA:
1530                   for (i=0;i<n;i++) {
1531                      dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1532                      dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1533                      dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1534                      dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1535                   }
1536                   break;
1537                case GL_BGR:
1538                   for (i=0;i<n;i++) {
1539                      dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1540                      dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1541                      dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1542                   }
1543                   break;
1544                case GL_BGRA:
1545                   for (i=0;i<n;i++) {
1546                      dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1547                      dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1548                      dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1549                      dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1550                   }
1551 		  break;
1552                case GL_ABGR_EXT:
1553                   for (i=0;i<n;i++) {
1554                      dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1555                      dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1556                      dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1557                      dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1558                   }
1559                   break;
1560                case GL_RED_INTEGER_EXT:
1561                   for (i=0;i<n;i++) {
1562                      dst[i] = (GLbyte) rgba[i][RCOMP];
1563                   }
1564                   break;
1565                case GL_GREEN_INTEGER_EXT:
1566                   for (i=0;i<n;i++) {
1567                      dst[i] = (GLbyte) rgba[i][GCOMP];
1568                   }
1569                   break;
1570                case GL_BLUE_INTEGER_EXT:
1571                   for (i=0;i<n;i++) {
1572                      dst[i] = (GLbyte) rgba[i][BCOMP];
1573                   }
1574                   break;
1575                case GL_ALPHA_INTEGER_EXT:
1576                   for (i=0;i<n;i++) {
1577                      dst[i] = (GLbyte) rgba[i][ACOMP];
1578                   }
1579                   break;
1580                case GL_RG_INTEGER:
1581                   for (i=0;i<n;i++) {
1582                      dst[i*2+0] = (GLbyte) rgba[i][RCOMP];
1583                      dst[i*2+1] = (GLbyte) rgba[i][GCOMP];
1584                   }
1585                   break;
1586                case GL_RGB_INTEGER_EXT:
1587                   for (i=0;i<n;i++) {
1588                      dst[i*3+0] = (GLbyte) rgba[i][RCOMP];
1589                      dst[i*3+1] = (GLbyte) rgba[i][GCOMP];
1590                      dst[i*3+2] = (GLbyte) rgba[i][BCOMP];
1591                   }
1592                   break;
1593                case GL_RGBA_INTEGER_EXT:
1594                   for (i=0;i<n;i++) {
1595                      dst[i*4+0] = (GLbyte) rgba[i][RCOMP];
1596                      dst[i*4+1] = (GLbyte) rgba[i][GCOMP];
1597                      dst[i*4+2] = (GLbyte) rgba[i][BCOMP];
1598                      dst[i*4+3] = (GLbyte) rgba[i][ACOMP];
1599                   }
1600                   break;
1601                case GL_BGR_INTEGER_EXT:
1602                   for (i=0;i<n;i++) {
1603                      dst[i*3+0] = (GLbyte) rgba[i][BCOMP];
1604                      dst[i*3+1] = (GLbyte) rgba[i][GCOMP];
1605                      dst[i*3+2] = (GLbyte) rgba[i][RCOMP];
1606                   }
1607                   break;
1608                case GL_BGRA_INTEGER_EXT:
1609                   for (i=0;i<n;i++) {
1610                      dst[i*4+0] = (GLbyte) rgba[i][BCOMP];
1611                      dst[i*4+1] = (GLbyte) rgba[i][GCOMP];
1612                      dst[i*4+2] = (GLbyte) rgba[i][RCOMP];
1613                      dst[i*4+3] = (GLbyte) rgba[i][ACOMP];
1614                   }
1615                   break;
1616                case GL_LUMINANCE_INTEGER_EXT:
1617                   for (i=0;i<n;i++) {
1618                      dst[i*2+0] = (GLbyte) (rgba[i][RCOMP] +
1619                                             rgba[i][GCOMP] +
1620                                             rgba[i][BCOMP]);
1621                      dst[i*2+1] = (GLbyte) rgba[i][ACOMP];
1622                   }
1623                   break;
1624                case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1625                   for (i=0;i<n;i++) {
1626                      dst[i] = (GLbyte) (rgba[i][RCOMP] +
1627                                         rgba[i][GCOMP] +
1628                                         rgba[i][BCOMP]);
1629                   }
1630                   break;
1631                case GL_DUDV_ATI:
1632                case GL_DU8DV8_ATI:
1633                   for (i=0;i<n;i++) {
1634                      dst[i*2+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1635                      dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1636                   }
1637                   break;
1638                default:
1639                   _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1640             }
1641          }
1642          break;
1643       case GL_UNSIGNED_SHORT:
1644          {
1645             GLushort *dst = (GLushort *) dstAddr;
1646             switch (dstFormat) {
1647                case GL_RED:
1648                   for (i=0;i<n;i++)
1649                      CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
1650                   break;
1651                case GL_GREEN:
1652                   for (i=0;i<n;i++)
1653                      CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
1654                   break;
1655                case GL_BLUE:
1656                   for (i=0;i<n;i++)
1657                      CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
1658                   break;
1659                case GL_ALPHA:
1660                   for (i=0;i<n;i++)
1661                      CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
1662                   break;
1663                case GL_LUMINANCE:
1664                   for (i=0;i<n;i++)
1665                      UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
1666                   break;
1667                case GL_LUMINANCE_ALPHA:
1668                   for (i=0;i<n;i++) {
1669                      UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
1670                      CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
1671                   }
1672                   break;
1673                case GL_RG:
1674                   for (i=0;i<n;i++) {
1675                      CLAMPED_FLOAT_TO_USHORT(dst[i*2+0], rgba[i][RCOMP]);
1676                      CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][GCOMP]);
1677                   }
1678                   break;
1679                case GL_RGB:
1680                   for (i=0;i<n;i++) {
1681                      CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
1682                      CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1683                      CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
1684                   }
1685                   break;
1686                case GL_RGBA:
1687                   for (i=0;i<n;i++) {
1688                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
1689                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1690                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
1691                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1692                   }
1693                   break;
1694                case GL_BGR:
1695                   for (i=0;i<n;i++) {
1696                      CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
1697                      CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1698                      CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
1699                   }
1700                   break;
1701                case GL_BGRA:
1702                   for (i=0;i<n;i++) {
1703                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
1704                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1705                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
1706                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1707                   }
1708                   break;
1709                case GL_ABGR_EXT:
1710                   for (i=0;i<n;i++) {
1711                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
1712                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
1713                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
1714                      CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
1715                   }
1716                   break;
1717                case GL_RED_INTEGER_EXT:
1718                   for (i=0;i<n;i++) {
1719                      dst[i] = (GLushort) rgba[i][RCOMP];
1720                   }
1721                   break;
1722                case GL_GREEN_INTEGER_EXT:
1723                   for (i=0;i<n;i++) {
1724                      dst[i] = (GLushort) rgba[i][GCOMP];
1725                   }
1726                   break;
1727                case GL_BLUE_INTEGER_EXT:
1728                   for (i=0;i<n;i++) {
1729                      dst[i] = (GLushort) rgba[i][BCOMP];
1730                   }
1731                   break;
1732                case GL_ALPHA_INTEGER_EXT:
1733                   for (i=0;i<n;i++) {
1734                      dst[i] = (GLushort) rgba[i][ACOMP];
1735                   }
1736                   break;
1737                case GL_RG_INTEGER:
1738                   for (i=0;i<n;i++) {
1739                      dst[i*2+0] = (GLushort) rgba[i][RCOMP];
1740                      dst[i*2+1] = (GLushort) rgba[i][GCOMP];
1741                   }
1742                   break;
1743                case GL_RGB_INTEGER_EXT:
1744                   for (i=0;i<n;i++) {
1745                      dst[i*3+0] = (GLushort) rgba[i][RCOMP];
1746                      dst[i*3+1] = (GLushort) rgba[i][GCOMP];
1747                      dst[i*3+2] = (GLushort) rgba[i][BCOMP];
1748                   }
1749                   break;
1750                case GL_RGBA_INTEGER_EXT:
1751                   for (i=0;i<n;i++) {
1752                      dst[i*4+0] = (GLushort) rgba[i][RCOMP];
1753                      dst[i*4+1] = (GLushort) rgba[i][GCOMP];
1754                      dst[i*4+2] = (GLushort) rgba[i][BCOMP];
1755                      dst[i*4+3] = (GLushort) rgba[i][ACOMP];
1756                   }
1757                   break;
1758                case GL_BGR_INTEGER_EXT:
1759                   for (i=0;i<n;i++) {
1760                      dst[i*3+0] = (GLushort) rgba[i][BCOMP];
1761                      dst[i*3+1] = (GLushort) rgba[i][GCOMP];
1762                      dst[i*3+2] = (GLushort) rgba[i][RCOMP];
1763                   }
1764                   break;
1765                case GL_BGRA_INTEGER_EXT:
1766                   for (i=0;i<n;i++) {
1767                      dst[i*4+0] = (GLushort) rgba[i][BCOMP];
1768                      dst[i*4+1] = (GLushort) rgba[i][GCOMP];
1769                      dst[i*4+2] = (GLushort) rgba[i][RCOMP];
1770                      dst[i*4+3] = (GLushort) rgba[i][ACOMP];
1771                   }
1772                   break;
1773                case GL_LUMINANCE_INTEGER_EXT:
1774                   for (i=0;i<n;i++) {
1775                      dst[i*2+0] = (GLushort) (rgba[i][RCOMP] +
1776                                               rgba[i][GCOMP] +
1777                                               rgba[i][BCOMP]);
1778                      dst[i*2+1] = (GLushort) rgba[i][ACOMP];
1779                   }
1780                   break;
1781                case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1782                   for (i=0;i<n;i++) {
1783                      dst[i] = (GLushort) (rgba[i][RCOMP] +
1784                                           rgba[i][GCOMP] +
1785                                           rgba[i][BCOMP]);
1786                   }
1787                   break;
1788                case GL_DUDV_ATI:
1789                case GL_DU8DV8_ATI:
1790                   for (i=0;i<n;i++) {
1791                      dst[i*2+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1792                      dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1793                   }
1794                   break;
1795                default:
1796                   _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1797             }
1798          }
1799          break;
1800       case GL_SHORT:
1801          {
1802             GLshort *dst = (GLshort *) dstAddr;
1803             switch (dstFormat) {
1804                case GL_RED:
1805                   for (i=0;i<n;i++)
1806                      dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1807                   break;
1808                case GL_GREEN:
1809                   for (i=0;i<n;i++)
1810                      dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1811                   break;
1812                case GL_BLUE:
1813                   for (i=0;i<n;i++)
1814                      dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1815                   break;
1816                case GL_ALPHA:
1817                   for (i=0;i<n;i++)
1818                      dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1819                   break;
1820                case GL_LUMINANCE:
1821                   for (i=0;i<n;i++)
1822                      dst[i] = FLOAT_TO_SHORT(luminance[i]);
1823                   break;
1824                case GL_LUMINANCE_ALPHA:
1825                   for (i=0;i<n;i++) {
1826                      dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1827                      dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1828                   }
1829                   break;
1830                case GL_RG:
1831                   for (i=0;i<n;i++) {
1832                      dst[i*2+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1833                      dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1834                   }
1835                   break;
1836                case GL_RGB:
1837                   for (i=0;i<n;i++) {
1838                      dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1839                      dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1840                      dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1841                   }
1842                   break;
1843                case GL_RGBA:
1844                   for (i=0;i<n;i++) {
1845                      dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1846                      dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1847                      dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1848                      dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1849                   }
1850                   break;
1851                case GL_BGR:
1852                   for (i=0;i<n;i++) {
1853                      dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1854                      dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1855                      dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1856                   }
1857                   break;
1858                case GL_BGRA:
1859                   for (i=0;i<n;i++) {
1860                      dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1861                      dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1862                      dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1863                      dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1864                   }
1865 		  break;
1866                case GL_ABGR_EXT:
1867                   for (i=0;i<n;i++) {
1868                      dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1869                      dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1870                      dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1871                      dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1872                   }
1873                   break;
1874                case GL_RED_INTEGER_EXT:
1875                   for (i=0;i<n;i++) {
1876                      dst[i] = (GLshort) rgba[i][RCOMP];
1877                   }
1878                   break;
1879                case GL_GREEN_INTEGER_EXT:
1880                   for (i=0;i<n;i++) {
1881                      dst[i] = (GLshort) rgba[i][GCOMP];
1882                   }
1883                   break;
1884                case GL_BLUE_INTEGER_EXT:
1885                   for (i=0;i<n;i++) {
1886                      dst[i] = (GLshort) rgba[i][BCOMP];
1887                   }
1888                   break;
1889                case GL_ALPHA_INTEGER_EXT:
1890                   for (i=0;i<n;i++) {
1891                      dst[i] = (GLshort) rgba[i][ACOMP];
1892                   }
1893                   break;
1894                case GL_RG_INTEGER:
1895                   for (i=0;i<n;i++) {
1896                      dst[i*2+0] = (GLshort) rgba[i][RCOMP];
1897                      dst[i*2+1] = (GLshort) rgba[i][GCOMP];
1898                   }
1899                   break;
1900                case GL_RGB_INTEGER_EXT:
1901                   for (i=0;i<n;i++) {
1902                      dst[i*3+0] = (GLshort) rgba[i][RCOMP];
1903                      dst[i*3+1] = (GLshort) rgba[i][GCOMP];
1904                      dst[i*3+2] = (GLshort) rgba[i][BCOMP];
1905                   }
1906                   break;
1907                case GL_RGBA_INTEGER_EXT:
1908                   for (i=0;i<n;i++) {
1909                      dst[i*4+0] = (GLshort) rgba[i][RCOMP];
1910                      dst[i*4+1] = (GLshort) rgba[i][GCOMP];
1911                      dst[i*4+2] = (GLshort) rgba[i][BCOMP];
1912                      dst[i*4+3] = (GLshort) rgba[i][ACOMP];
1913                   }
1914                   break;
1915                case GL_BGR_INTEGER_EXT:
1916                   for (i=0;i<n;i++) {
1917                      dst[i*3+0] = (GLshort) rgba[i][BCOMP];
1918                      dst[i*3+1] = (GLshort) rgba[i][GCOMP];
1919                      dst[i*3+2] = (GLshort) rgba[i][RCOMP];
1920                   }
1921                   break;
1922                case GL_BGRA_INTEGER_EXT:
1923                   for (i=0;i<n;i++) {
1924                      dst[i*4+0] = (GLshort) rgba[i][BCOMP];
1925                      dst[i*4+1] = (GLshort) rgba[i][GCOMP];
1926                      dst[i*4+2] = (GLshort) rgba[i][RCOMP];
1927                      dst[i*4+3] = (GLshort) rgba[i][ACOMP];
1928                   }
1929                   break;
1930                case GL_LUMINANCE_INTEGER_EXT:
1931                   for (i=0;i<n;i++) {
1932                      dst[i*2+0] = (GLshort) (rgba[i][RCOMP] +
1933                                              rgba[i][GCOMP] +
1934                                              rgba[i][BCOMP]);
1935                      dst[i*2+1] = (GLshort) rgba[i][ACOMP];
1936                   }
1937                   break;
1938                case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1939                   for (i=0;i<n;i++) {
1940                      dst[i] = (GLshort) (rgba[i][RCOMP] +
1941                                          rgba[i][GCOMP] +
1942                                          rgba[i][BCOMP]);
1943                   }
1944                   break;
1945                case GL_DUDV_ATI:
1946                case GL_DU8DV8_ATI:
1947                   for (i=0;i<n;i++) {
1948                      dst[i*2+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1949                      dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1950                   }
1951                   break;
1952                default:
1953                   _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1954             }
1955          }
1956          break;
1957       case GL_UNSIGNED_INT:
1958          {
1959             GLuint *dst = (GLuint *) dstAddr;
1960             switch (dstFormat) {
1961                case GL_RED:
1962                   for (i=0;i<n;i++)
1963                      dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1964                   break;
1965                case GL_GREEN:
1966                   for (i=0;i<n;i++)
1967                      dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1968                   break;
1969                case GL_BLUE:
1970                   for (i=0;i<n;i++)
1971                      dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1972                   break;
1973                case GL_ALPHA:
1974                   for (i=0;i<n;i++)
1975                      dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1976                   break;
1977                case GL_LUMINANCE:
1978                   for (i=0;i<n;i++)
1979                      dst[i] = FLOAT_TO_UINT(luminance[i]);
1980                   break;
1981                case GL_LUMINANCE_ALPHA:
1982                   for (i=0;i<n;i++) {
1983                      dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
1984                      dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1985                   }
1986                   break;
1987                case GL_RG:
1988                   for (i=0;i<n;i++) {
1989                      dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1990                      dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1991                   }
1992                   break;
1993                case GL_RGB:
1994                   for (i=0;i<n;i++) {
1995                      dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1996                      dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1997                      dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1998                   }
1999                   break;
2000                case GL_RGBA:
2001                   for (i=0;i<n;i++) {
2002                      dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2003                      dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2004                      dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2005                      dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2006                   }
2007                   break;
2008                case GL_BGR:
2009                   for (i=0;i<n;i++) {
2010                      dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2011                      dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2012                      dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2013                   }
2014                   break;
2015                case GL_BGRA:
2016                   for (i=0;i<n;i++) {
2017                      dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2018                      dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2019                      dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2020                      dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2021                   }
2022                   break;
2023                case GL_ABGR_EXT:
2024                   for (i=0;i<n;i++) {
2025                      dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2026                      dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2027                      dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2028                      dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2029                   }
2030                   break;
2031                case GL_RED_INTEGER_EXT:
2032                   for (i=0;i<n;i++) {
2033                      dst[i] = (GLuint) rgba[i][RCOMP];
2034                   }
2035                   break;
2036                case GL_GREEN_INTEGER_EXT:
2037                   for (i=0;i<n;i++) {
2038                      dst[i] = (GLuint) rgba[i][GCOMP];
2039                   }
2040                   break;
2041                case GL_BLUE_INTEGER_EXT:
2042                   for (i=0;i<n;i++) {
2043                      dst[i] = (GLuint) rgba[i][BCOMP];
2044                   }
2045                   break;
2046                case GL_ALPHA_INTEGER_EXT:
2047                   for (i=0;i<n;i++) {
2048                      dst[i] = (GLuint) rgba[i][ACOMP];
2049                   }
2050                   break;
2051                case GL_RG_INTEGER:
2052                   for (i=0;i<n;i++) {
2053                      dst[i*2+0] = (GLuint) rgba[i][RCOMP];
2054                      dst[i*2+1] = (GLuint) rgba[i][GCOMP];
2055                   }
2056                   break;
2057                case GL_RGB_INTEGER_EXT:
2058                   for (i=0;i<n;i++) {
2059                      dst[i*3+0] = (GLuint) rgba[i][RCOMP];
2060                      dst[i*3+1] = (GLuint) rgba[i][GCOMP];
2061                      dst[i*3+2] = (GLuint) rgba[i][BCOMP];
2062                   }
2063                   break;
2064                case GL_RGBA_INTEGER_EXT:
2065                   for (i=0;i<n;i++) {
2066                      dst[i*4+0] = (GLuint) rgba[i][RCOMP];
2067                      dst[i*4+1] = (GLuint) rgba[i][GCOMP];
2068                      dst[i*4+2] = (GLuint) rgba[i][BCOMP];
2069                      dst[i*4+3] = (GLuint) rgba[i][ACOMP];
2070                   }
2071                   break;
2072                case GL_BGR_INTEGER_EXT:
2073                   for (i=0;i<n;i++) {
2074                      dst[i*3+0] = (GLuint) rgba[i][BCOMP];
2075                      dst[i*3+1] = (GLuint) rgba[i][GCOMP];
2076                      dst[i*3+2] = (GLuint) rgba[i][RCOMP];
2077                   }
2078                   break;
2079                case GL_BGRA_INTEGER_EXT:
2080                   for (i=0;i<n;i++) {
2081                      dst[i*4+0] = (GLuint) rgba[i][BCOMP];
2082                      dst[i*4+1] = (GLuint) rgba[i][GCOMP];
2083                      dst[i*4+2] = (GLuint) rgba[i][RCOMP];
2084                      dst[i*4+3] = (GLuint) rgba[i][ACOMP];
2085                   }
2086                   break;
2087                case GL_LUMINANCE_INTEGER_EXT:
2088                   for (i=0;i<n;i++) {
2089                      dst[i*2+0] = (GLuint) (rgba[i][RCOMP] +
2090                                             rgba[i][GCOMP] +
2091                                             rgba[i][BCOMP]);
2092                      dst[i*2+1] = (GLuint) rgba[i][ACOMP];
2093                   }
2094                   break;
2095                case GL_LUMINANCE_ALPHA_INTEGER_EXT:
2096                   for (i=0;i<n;i++) {
2097                      dst[i] = (GLuint) (rgba[i][RCOMP] +
2098                                         rgba[i][GCOMP] +
2099                                         rgba[i][BCOMP]);
2100                   }
2101                   break;
2102                case GL_DUDV_ATI:
2103                case GL_DU8DV8_ATI:
2104                   for (i=0;i<n;i++) {
2105                      dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2106                      dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2107                   }
2108                   break;
2109                default:
2110                   _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2111             }
2112          }
2113          break;
2114       case GL_INT:
2115          {
2116             GLint *dst = (GLint *) dstAddr;
2117             switch (dstFormat) {
2118                case GL_RED:
2119                   for (i=0;i<n;i++)
2120                      dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
2121                   break;
2122                case GL_GREEN:
2123                   for (i=0;i<n;i++)
2124                      dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
2125                   break;
2126                case GL_BLUE:
2127                   for (i=0;i<n;i++)
2128                      dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
2129                   break;
2130                case GL_ALPHA:
2131                   for (i=0;i<n;i++)
2132                      dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
2133                   break;
2134                case GL_LUMINANCE:
2135                   for (i=0;i<n;i++)
2136                      dst[i] = FLOAT_TO_INT(luminance[i]);
2137                   break;
2138                case GL_LUMINANCE_ALPHA:
2139                   for (i=0;i<n;i++) {
2140                      dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2141                      dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
2142                   }
2143                   break;
2144                case GL_RG:
2145                   for (i=0;i<n;i++) {
2146                      dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2147                      dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2148                   }
2149                   break;
2150                case GL_RGB:
2151                   for (i=0;i<n;i++) {
2152                      dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2153                      dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2154                      dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2155                   }
2156                   break;
2157                case GL_RGBA:
2158                   for (i=0;i<n;i++) {
2159                      dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2160                      dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2161                      dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2162                      dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2163                   }
2164                   break;
2165                case GL_BGR:
2166                   for (i=0;i<n;i++) {
2167                      dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2168                      dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2169                      dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2170                   }
2171                   break;
2172                case GL_BGRA:
2173                   for (i=0;i<n;i++) {
2174                      dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2175                      dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2176                      dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2177                      dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2178                   }
2179                   break;
2180                case GL_ABGR_EXT:
2181                   for (i=0;i<n;i++) {
2182                      dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
2183                      dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
2184                      dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
2185                      dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
2186                   }
2187                   break;
2188                case GL_DUDV_ATI:
2189                case GL_DU8DV8_ATI:
2190                   for (i=0;i<n;i++) {
2191                      dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2192                      dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2193                   }
2194                   break;
2195                case GL_RED_INTEGER_EXT:
2196                   for (i=0;i<n;i++) {
2197                      dst[i] = (GLint) rgba[i][RCOMP];
2198                   }
2199                   break;
2200                case GL_GREEN_INTEGER_EXT:
2201                   for (i=0;i<n;i++) {
2202                      dst[i] = (GLint) rgba[i][GCOMP];
2203                   }
2204                   break;
2205                case GL_BLUE_INTEGER_EXT:
2206                   for (i=0;i<n;i++) {
2207                      dst[i] = (GLint) rgba[i][BCOMP];
2208                   }
2209                   break;
2210                case GL_ALPHA_INTEGER_EXT:
2211                   for (i=0;i<n;i++) {
2212                      dst[i] = (GLint) rgba[i][ACOMP];
2213                   }
2214                   break;
2215                case GL_RG_INTEGER:
2216                   for (i=0;i<n;i++) {
2217                      dst[i*2+0] = (GLint) rgba[i][RCOMP];
2218                      dst[i*2+1] = (GLint) rgba[i][GCOMP];
2219                   }
2220                   break;
2221                case GL_RGB_INTEGER_EXT:
2222                   for (i=0;i<n;i++) {
2223                      dst[i*3+0] = (GLint) rgba[i][RCOMP];
2224                      dst[i*3+1] = (GLint) rgba[i][GCOMP];
2225                      dst[i*3+2] = (GLint) rgba[i][BCOMP];
2226                   }
2227                   break;
2228                case GL_RGBA_INTEGER_EXT:
2229                   for (i=0;i<n;i++) {
2230                      dst[i*4+0] = (GLint) rgba[i][RCOMP];
2231                      dst[i*4+1] = (GLint) rgba[i][GCOMP];
2232                      dst[i*4+2] = (GLint) rgba[i][BCOMP];
2233                      dst[i*4+3] = (GLint) rgba[i][ACOMP];
2234                   }
2235                   break;
2236                case GL_BGR_INTEGER_EXT:
2237                   for (i=0;i<n;i++) {
2238                      dst[i*3+0] = (GLint) rgba[i][BCOMP];
2239                      dst[i*3+1] = (GLint) rgba[i][GCOMP];
2240                      dst[i*3+2] = (GLint) rgba[i][RCOMP];
2241                   }
2242                   break;
2243                case GL_BGRA_INTEGER_EXT:
2244                   for (i=0;i<n;i++) {
2245                      dst[i*4+0] = (GLint) rgba[i][BCOMP];
2246                      dst[i*4+1] = (GLint) rgba[i][GCOMP];
2247                      dst[i*4+2] = (GLint) rgba[i][RCOMP];
2248                      dst[i*4+3] = (GLint) rgba[i][ACOMP];
2249                   }
2250                   break;
2251                case GL_LUMINANCE_INTEGER_EXT:
2252                   for (i=0;i<n;i++) {
2253                      dst[i*2+0] = (GLint) (rgba[i][RCOMP] +
2254                                            rgba[i][GCOMP] +
2255                                            rgba[i][BCOMP]);
2256                      dst[i*2+1] = (GLint) rgba[i][ACOMP];
2257                   }
2258                   break;
2259                case GL_LUMINANCE_ALPHA_INTEGER_EXT:
2260                   for (i=0;i<n;i++) {
2261                      dst[i] = (GLint) (rgba[i][RCOMP] +
2262                                        rgba[i][GCOMP] +
2263                                        rgba[i][BCOMP]);
2264                   }
2265                   break;
2266                default:
2267                   _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2268             }
2269          }
2270          break;
2271       case GL_FLOAT:
2272          {
2273             GLfloat *dst = (GLfloat *) dstAddr;
2274             switch (dstFormat) {
2275                case GL_RED:
2276                   for (i=0;i<n;i++)
2277                      dst[i] = rgba[i][RCOMP];
2278                   break;
2279                case GL_GREEN:
2280                   for (i=0;i<n;i++)
2281                      dst[i] = rgba[i][GCOMP];
2282                   break;
2283                case GL_BLUE:
2284                   for (i=0;i<n;i++)
2285                      dst[i] = rgba[i][BCOMP];
2286                   break;
2287                case GL_ALPHA:
2288                   for (i=0;i<n;i++)
2289                      dst[i] = rgba[i][ACOMP];
2290                   break;
2291                case GL_LUMINANCE:
2292                   for (i=0;i<n;i++)
2293                      dst[i] = luminance[i];
2294                   break;
2295                case GL_LUMINANCE_ALPHA:
2296                   for (i=0;i<n;i++) {
2297                      dst[i*2+0] = luminance[i];
2298                      dst[i*2+1] = rgba[i][ACOMP];
2299                   }
2300                   break;
2301                case GL_RG:
2302                   for (i=0;i<n;i++) {
2303                      dst[i*2+0] = rgba[i][RCOMP];
2304                      dst[i*2+1] = rgba[i][GCOMP];
2305                   }
2306                   break;
2307                case GL_RGB:
2308                   for (i=0;i<n;i++) {
2309                      dst[i*3+0] = rgba[i][RCOMP];
2310                      dst[i*3+1] = rgba[i][GCOMP];
2311                      dst[i*3+2] = rgba[i][BCOMP];
2312                   }
2313                   break;
2314                case GL_RGBA:
2315                   for (i=0;i<n;i++) {
2316                      dst[i*4+0] = rgba[i][RCOMP];
2317                      dst[i*4+1] = rgba[i][GCOMP];
2318                      dst[i*4+2] = rgba[i][BCOMP];
2319                      dst[i*4+3] = rgba[i][ACOMP];
2320                   }
2321                   break;
2322                case GL_BGR:
2323                   for (i=0;i<n;i++) {
2324                      dst[i*3+0] = rgba[i][BCOMP];
2325                      dst[i*3+1] = rgba[i][GCOMP];
2326                      dst[i*3+2] = rgba[i][RCOMP];
2327                   }
2328                   break;
2329                case GL_BGRA:
2330                   for (i=0;i<n;i++) {
2331                      dst[i*4+0] = rgba[i][BCOMP];
2332                      dst[i*4+1] = rgba[i][GCOMP];
2333                      dst[i*4+2] = rgba[i][RCOMP];
2334                      dst[i*4+3] = rgba[i][ACOMP];
2335                   }
2336                   break;
2337                case GL_ABGR_EXT:
2338                   for (i=0;i<n;i++) {
2339                      dst[i*4+0] = rgba[i][ACOMP];
2340                      dst[i*4+1] = rgba[i][BCOMP];
2341                      dst[i*4+2] = rgba[i][GCOMP];
2342                      dst[i*4+3] = rgba[i][RCOMP];
2343                   }
2344                   break;
2345                case GL_DUDV_ATI:
2346                case GL_DU8DV8_ATI:
2347                   for (i=0;i<n;i++) {
2348                      dst[i*2+0] = rgba[i][RCOMP];
2349                      dst[i*2+1] = rgba[i][GCOMP];
2350                   }
2351                   break;
2352                default:
2353                   _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2354             }
2355          }
2356          break;
2357       case GL_HALF_FLOAT_ARB:
2358          {
2359             GLhalfARB *dst = (GLhalfARB *) dstAddr;
2360             switch (dstFormat) {
2361                case GL_RED:
2362                   for (i=0;i<n;i++)
2363                      dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
2364                   break;
2365                case GL_GREEN:
2366                   for (i=0;i<n;i++)
2367                      dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
2368                   break;
2369                case GL_BLUE:
2370                   for (i=0;i<n;i++)
2371                      dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
2372                   break;
2373                case GL_ALPHA:
2374                   for (i=0;i<n;i++)
2375                      dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
2376                   break;
2377                case GL_LUMINANCE:
2378                   for (i=0;i<n;i++)
2379                      dst[i] = _mesa_float_to_half(luminance[i]);
2380                   break;
2381                case GL_LUMINANCE_ALPHA:
2382                   for (i=0;i<n;i++) {
2383                      dst[i*2+0] = _mesa_float_to_half(luminance[i]);
2384                      dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
2385                   }
2386                   break;
2387                case GL_RG:
2388                   for (i=0;i<n;i++) {
2389                      dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2390                      dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2391                   }
2392                   break;
2393                case GL_RGB:
2394                   for (i=0;i<n;i++) {
2395                      dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2396                      dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2397                      dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2398                   }
2399                   break;
2400                case GL_RGBA:
2401                   for (i=0;i<n;i++) {
2402                      dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2403                      dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2404                      dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2405                      dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2406                   }
2407                   break;
2408                case GL_BGR:
2409                   for (i=0;i<n;i++) {
2410                      dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2411                      dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2412                      dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2413                   }
2414                   break;
2415                case GL_BGRA:
2416                   for (i=0;i<n;i++) {
2417                      dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2418                      dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2419                      dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2420                      dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2421                   }
2422                   break;
2423                case GL_ABGR_EXT:
2424                   for (i=0;i<n;i++) {
2425                      dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
2426                      dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
2427                      dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
2428                      dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
2429                   }
2430                   break;
2431                case GL_DUDV_ATI:
2432                case GL_DU8DV8_ATI:
2433                   for (i=0;i<n;i++) {
2434                      dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2435                      dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2436                   }
2437                   break;
2438                default:
2439                   _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2440             }
2441          }
2442          break;
2443       case GL_UNSIGNED_BYTE_3_3_2:
2444          if (dstFormat == GL_RGB) {
2445             GLubyte *dst = (GLubyte *) dstAddr;
2446             for (i=0;i<n;i++) {
2447                dst[i] = (F_TO_I(rgba[i][RCOMP] * 7.0F) << 5)
2448                       | (F_TO_I(rgba[i][GCOMP] * 7.0F) << 2)
2449                       | (F_TO_I(rgba[i][BCOMP] * 3.0F)     );
2450             }
2451          }
2452          break;
2453       case GL_UNSIGNED_BYTE_2_3_3_REV:
2454          if (dstFormat == GL_RGB) {
2455             GLubyte *dst = (GLubyte *) dstAddr;
2456             for (i=0;i<n;i++) {
2457                dst[i] = (F_TO_I(rgba[i][RCOMP] * 7.0F)     )
2458                       | (F_TO_I(rgba[i][GCOMP] * 7.0F) << 3)
2459                       | (F_TO_I(rgba[i][BCOMP] * 3.0F) << 6);
2460             }
2461          }
2462          break;
2463       case GL_UNSIGNED_SHORT_5_6_5:
2464          if (dstFormat == GL_RGB) {
2465             GLushort *dst = (GLushort *) dstAddr;
2466             for (i=0;i<n;i++) {
2467                dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F) << 11)
2468                       | (F_TO_I(rgba[i][GCOMP] * 63.0F) <<  5)
2469                       | (F_TO_I(rgba[i][BCOMP] * 31.0F)      );
2470             }
2471          }
2472          break;
2473       case GL_UNSIGNED_SHORT_5_6_5_REV:
2474          if (dstFormat == GL_RGB) {
2475             GLushort *dst = (GLushort *) dstAddr;
2476             for (i=0;i<n;i++) {
2477                dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F)      )
2478                       | (F_TO_I(rgba[i][GCOMP] * 63.0F) <<  5)
2479                       | (F_TO_I(rgba[i][BCOMP] * 31.0F) << 11);
2480             }
2481          }
2482          break;
2483       case GL_UNSIGNED_SHORT_4_4_4_4:
2484          if (dstFormat == GL_RGBA) {
2485             GLushort *dst = (GLushort *) dstAddr;
2486             for (i=0;i<n;i++) {
2487                dst[i] = (F_TO_I(rgba[i][RCOMP] * 15.0F) << 12)
2488                       | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
2489                       | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  4)
2490                       | (F_TO_I(rgba[i][ACOMP] * 15.0F)      );
2491             }
2492          }
2493          else if (dstFormat == GL_BGRA) {
2494             GLushort *dst = (GLushort *) dstAddr;
2495             for (i=0;i<n;i++) {
2496                dst[i] = (F_TO_I(rgba[i][BCOMP] * 15.0F) << 12)
2497                       | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
2498                       | (F_TO_I(rgba[i][RCOMP] * 15.0F) <<  4)
2499                       | (F_TO_I(rgba[i][ACOMP] * 15.0F)      );
2500             }
2501          }
2502          else if (dstFormat == GL_ABGR_EXT) {
2503             GLushort *dst = (GLushort *) dstAddr;
2504             for (i=0;i<n;i++) {
2505                dst[i] = (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12)
2506                       | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  8)
2507                       | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
2508                       | (F_TO_I(rgba[i][RCOMP] * 15.0F)      );
2509             }
2510          }
2511          break;
2512       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2513          if (dstFormat == GL_RGBA) {
2514             GLushort *dst = (GLushort *) dstAddr;
2515             for (i=0;i<n;i++) {
2516                dst[i] = (F_TO_I(rgba[i][RCOMP] * 15.0F)      )
2517                       | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
2518                       | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  8)
2519                       | (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12);
2520             }
2521          }
2522          else if (dstFormat == GL_BGRA) {
2523             GLushort *dst = (GLushort *) dstAddr;
2524             for (i=0;i<n;i++) {
2525                dst[i] = (F_TO_I(rgba[i][BCOMP] * 15.0F)      )
2526                       | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
2527                       | (F_TO_I(rgba[i][RCOMP] * 15.0F) <<  8)
2528                       | (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12);
2529             }
2530          }
2531          else if (dstFormat == GL_ABGR_EXT) {
2532             GLushort *dst = (GLushort *) dstAddr;
2533             for (i=0;i<n;i++) {
2534                dst[i] = (F_TO_I(rgba[i][ACOMP] * 15.0F)      )
2535                       | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  4)
2536                       | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
2537                       | (F_TO_I(rgba[i][RCOMP] * 15.0F) << 12);
2538             }
2539          }
2540          break;
2541       case GL_UNSIGNED_SHORT_5_5_5_1:
2542          if (dstFormat == GL_RGBA) {
2543             GLushort *dst = (GLushort *) dstAddr;
2544             for (i=0;i<n;i++) {
2545                dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F) << 11)
2546                       | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  6)
2547                       | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  1)
2548                       | (F_TO_I(rgba[i][ACOMP] *  1.0F)      );
2549             }
2550          }
2551          else if (dstFormat == GL_BGRA) {
2552             GLushort *dst = (GLushort *) dstAddr;
2553             for (i=0;i<n;i++) {
2554                dst[i] = (F_TO_I(rgba[i][BCOMP] * 31.0F) << 11)
2555                       | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  6)
2556                       | (F_TO_I(rgba[i][RCOMP] * 31.0F) <<  1)
2557                       | (F_TO_I(rgba[i][ACOMP] *  1.0F)      );
2558             }
2559          }
2560          else if (dstFormat == GL_ABGR_EXT) {
2561             GLushort *dst = (GLushort *) dstAddr;
2562             for (i=0;i<n;i++) {
2563                dst[i] = (F_TO_I(rgba[i][ACOMP] * 31.0F) << 11)
2564                       | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  6)
2565                       | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  1)
2566                       | (F_TO_I(rgba[i][RCOMP] *  1.0F)      );
2567             }
2568          }
2569          break;
2570       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2571          if (dstFormat == GL_RGBA) {
2572             GLushort *dst = (GLushort *) dstAddr;
2573             for (i=0;i<n;i++) {
2574                dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F)      )
2575                       | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  5)
2576                       | (F_TO_I(rgba[i][BCOMP] * 31.0F) << 10)
2577                       | (F_TO_I(rgba[i][ACOMP] *  1.0F) << 15);
2578             }
2579          }
2580          else if (dstFormat == GL_BGRA) {
2581             GLushort *dst = (GLushort *) dstAddr;
2582             for (i=0;i<n;i++) {
2583                dst[i] = (F_TO_I(rgba[i][BCOMP] * 31.0F)      )
2584                       | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  5)
2585                       | (F_TO_I(rgba[i][RCOMP] * 31.0F) << 10)
2586                       | (F_TO_I(rgba[i][ACOMP] *  1.0F) << 15);
2587             }
2588          }
2589          else if (dstFormat == GL_ABGR_EXT) {
2590             GLushort *dst = (GLushort *) dstAddr;
2591             for (i=0;i<n;i++) {
2592                dst[i] = (F_TO_I(rgba[i][ACOMP] * 31.0F)      )
2593                       | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  5)
2594                       | (F_TO_I(rgba[i][GCOMP] * 31.0F) << 10)
2595                       | (F_TO_I(rgba[i][RCOMP] *  1.0F) << 15);
2596             }
2597          }
2598          break;
2599       case GL_UNSIGNED_INT_8_8_8_8:
2600          if (dstFormat == GL_RGBA) {
2601             GLuint *dst = (GLuint *) dstAddr;
2602             for (i=0;i<n;i++) {
2603                dst[i] = (F_TO_I(rgba[i][RCOMP] * 255.F) << 24)
2604                       | (F_TO_I(rgba[i][GCOMP] * 255.F) << 16)
2605                       | (F_TO_I(rgba[i][BCOMP] * 255.F) <<  8)
2606                       | (F_TO_I(rgba[i][ACOMP] * 255.F)      );
2607             }
2608          }
2609          else if (dstFormat == GL_BGRA) {
2610             GLuint *dst = (GLuint *) dstAddr;
2611             for (i=0;i<n;i++) {
2612                dst[i] = (F_TO_I(rgba[i][BCOMP] * 255.F) << 24)
2613                       | (F_TO_I(rgba[i][GCOMP] * 255.F) << 16)
2614                       | (F_TO_I(rgba[i][RCOMP] * 255.F) <<  8)
2615                       | (F_TO_I(rgba[i][ACOMP] * 255.F)      );
2616             }
2617          }
2618          else if (dstFormat == GL_ABGR_EXT) {
2619             GLuint *dst = (GLuint *) dstAddr;
2620             for (i=0;i<n;i++) {
2621                dst[i] = (F_TO_I(rgba[i][ACOMP] * 255.F) << 24)
2622                       | (F_TO_I(rgba[i][BCOMP] * 255.F) << 16)
2623                       | (F_TO_I(rgba[i][GCOMP] * 255.F) <<  8)
2624                       | (F_TO_I(rgba[i][RCOMP] * 255.F)      );
2625             }
2626          }
2627          break;
2628       case GL_UNSIGNED_INT_8_8_8_8_REV:
2629          if (dstFormat == GL_RGBA) {
2630             GLuint *dst = (GLuint *) dstAddr;
2631             for (i=0;i<n;i++) {
2632                dst[i] = (F_TO_I(rgba[i][RCOMP] * 255.0F)      )
2633                       | (F_TO_I(rgba[i][GCOMP] * 255.0F) <<  8)
2634                       | (F_TO_I(rgba[i][BCOMP] * 255.0F) << 16)
2635                       | (F_TO_I(rgba[i][ACOMP] * 255.0F) << 24);
2636             }
2637          }
2638          else if (dstFormat == GL_BGRA) {
2639             GLuint *dst = (GLuint *) dstAddr;
2640             for (i=0;i<n;i++) {
2641                dst[i] = (F_TO_I(rgba[i][BCOMP] * 255.0F)      )
2642                       | (F_TO_I(rgba[i][GCOMP] * 255.0F) <<  8)
2643                       | (F_TO_I(rgba[i][RCOMP] * 255.0F) << 16)
2644                       | (F_TO_I(rgba[i][ACOMP] * 255.0F) << 24);
2645             }
2646          }
2647          else if (dstFormat == GL_ABGR_EXT) {
2648             GLuint *dst = (GLuint *) dstAddr;
2649             for (i=0;i<n;i++) {
2650                dst[i] = (F_TO_I(rgba[i][ACOMP] * 255.0F)      )
2651                       | (F_TO_I(rgba[i][BCOMP] * 255.0F) <<  8)
2652                       | (F_TO_I(rgba[i][GCOMP] * 255.0F) << 16)
2653                       | (F_TO_I(rgba[i][RCOMP] * 255.0F) << 24);
2654             }
2655          }
2656          break;
2657       case GL_UNSIGNED_INT_10_10_10_2:
2658          if (dstFormat == GL_RGBA) {
2659             GLuint *dst = (GLuint *) dstAddr;
2660             for (i=0;i<n;i++) {
2661                dst[i] = (F_TO_I(rgba[i][RCOMP] * 1023.0F) << 22)
2662                       | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 12)
2663                       | (F_TO_I(rgba[i][BCOMP] * 1023.0F) <<  2)
2664                       | (F_TO_I(rgba[i][ACOMP] *    3.0F)      );
2665             }
2666          }
2667          else if (dstFormat == GL_BGRA) {
2668             GLuint *dst = (GLuint *) dstAddr;
2669             for (i=0;i<n;i++) {
2670                dst[i] = (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 22)
2671                       | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 12)
2672                       | (F_TO_I(rgba[i][RCOMP] * 1023.0F) <<  2)
2673                       | (F_TO_I(rgba[i][ACOMP] *    3.0F)      );
2674             }
2675          }
2676          else if (dstFormat == GL_ABGR_EXT) {
2677             GLuint *dst = (GLuint *) dstAddr;
2678             for (i=0;i<n;i++) {
2679                dst[i] = (F_TO_I(rgba[i][ACOMP] * 1023.0F) << 22)
2680                       | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 12)
2681                       | (F_TO_I(rgba[i][GCOMP] * 1023.0F) <<  2)
2682                       | (F_TO_I(rgba[i][RCOMP] *    3.0F)      );
2683             }
2684          }
2685          break;
2686       case GL_UNSIGNED_INT_2_10_10_10_REV:
2687          if (dstFormat == GL_RGBA) {
2688             GLuint *dst = (GLuint *) dstAddr;
2689             for (i=0;i<n;i++) {
2690                dst[i] = (F_TO_I(rgba[i][RCOMP] * 1023.0F)      )
2691                       | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 10)
2692                       | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 20)
2693                       | (F_TO_I(rgba[i][ACOMP] *    3.0F) << 30);
2694             }
2695          }
2696          else if (dstFormat == GL_BGRA) {
2697             GLuint *dst = (GLuint *) dstAddr;
2698             for (i=0;i<n;i++) {
2699                dst[i] = (F_TO_I(rgba[i][BCOMP] * 1023.0F)      )
2700                       | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 10)
2701                       | (F_TO_I(rgba[i][RCOMP] * 1023.0F) << 20)
2702                       | (F_TO_I(rgba[i][ACOMP] *    3.0F) << 30);
2703             }
2704          }
2705          else if (dstFormat == GL_ABGR_EXT) {
2706             GLuint *dst = (GLuint *) dstAddr;
2707             for (i=0;i<n;i++) {
2708                dst[i] = (F_TO_I(rgba[i][ACOMP] * 1023.0F)      )
2709                       | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 10)
2710                       | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 20)
2711                       | (F_TO_I(rgba[i][RCOMP] *    3.0F) << 30);
2712             }
2713          }
2714          break;
2715       case GL_UNSIGNED_INT_5_9_9_9_REV:
2716          {
2717             GLuint *dst = (GLuint *) dstAddr;
2718             for (i = 0; i < n; i++) {
2719                dst[i] = float3_to_rgb9e5(rgba[i]);
2720             }
2721          }
2722          break;
2723       case GL_UNSIGNED_INT_10F_11F_11F_REV:
2724          {
2725             GLuint *dst = (GLuint *) dstAddr;
2726             for (i = 0; i < n; i++) {
2727                dst[i] = float3_to_r11g11b10f(rgba[i]);
2728             }
2729          }
2730          break;
2731       default:
2732          _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2733          free(luminance);
2734          return;
2735    }
2736 
2737    if (dstPacking->SwapBytes) {
2738       GLint swapSize = _mesa_sizeof_packed_type(dstType);
2739       if (swapSize == 2) {
2740          _mesa_swap2((GLushort *) dstAddr, n * comps);
2741       }
2742       else if (swapSize == 4) {
2743          _mesa_swap4((GLuint *) dstAddr, n * comps);
2744       }
2745    }
2746 
2747    free(luminance);
2748 }
2749 
2750 
2751 
2752 #define SWAP2BYTE(VALUE)			\
2753    {						\
2754       GLubyte *bytes = (GLubyte *) &(VALUE);	\
2755       GLubyte tmp = bytes[0];			\
2756       bytes[0] = bytes[1];			\
2757       bytes[1] = tmp;				\
2758    }
2759 
2760 #define SWAP4BYTE(VALUE)			\
2761    {						\
2762       GLubyte *bytes = (GLubyte *) &(VALUE);	\
2763       GLubyte tmp = bytes[0];			\
2764       bytes[0] = bytes[3];			\
2765       bytes[3] = tmp;				\
2766       tmp = bytes[1];				\
2767       bytes[1] = bytes[2];			\
2768       bytes[2] = tmp;				\
2769    }
2770 
2771 
2772 static void
extract_uint_indexes(GLuint n,GLuint indexes[],GLenum srcFormat,GLenum srcType,const GLvoid * src,const struct gl_pixelstore_attrib * unpack)2773 extract_uint_indexes(GLuint n, GLuint indexes[],
2774                      GLenum srcFormat, GLenum srcType, const GLvoid *src,
2775                      const struct gl_pixelstore_attrib *unpack )
2776 {
2777    ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2778 
2779    ASSERT(srcType == GL_BITMAP ||
2780           srcType == GL_UNSIGNED_BYTE ||
2781           srcType == GL_BYTE ||
2782           srcType == GL_UNSIGNED_SHORT ||
2783           srcType == GL_SHORT ||
2784           srcType == GL_UNSIGNED_INT ||
2785           srcType == GL_INT ||
2786           srcType == GL_UNSIGNED_INT_24_8_EXT ||
2787           srcType == GL_HALF_FLOAT_ARB ||
2788           srcType == GL_FLOAT ||
2789           srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
2790 
2791    switch (srcType) {
2792       case GL_BITMAP:
2793          {
2794             GLubyte *ubsrc = (GLubyte *) src;
2795             if (unpack->LsbFirst) {
2796                GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2797                GLuint i;
2798                for (i = 0; i < n; i++) {
2799                   indexes[i] = (*ubsrc & mask) ? 1 : 0;
2800                   if (mask == 128) {
2801                      mask = 1;
2802                      ubsrc++;
2803                   }
2804                   else {
2805                      mask = mask << 1;
2806                   }
2807                }
2808             }
2809             else {
2810                GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2811                GLuint i;
2812                for (i = 0; i < n; i++) {
2813                   indexes[i] = (*ubsrc & mask) ? 1 : 0;
2814                   if (mask == 1) {
2815                      mask = 128;
2816                      ubsrc++;
2817                   }
2818                   else {
2819                      mask = mask >> 1;
2820                   }
2821                }
2822             }
2823          }
2824          break;
2825       case GL_UNSIGNED_BYTE:
2826          {
2827             GLuint i;
2828             const GLubyte *s = (const GLubyte *) src;
2829             for (i = 0; i < n; i++)
2830                indexes[i] = s[i];
2831          }
2832          break;
2833       case GL_BYTE:
2834          {
2835             GLuint i;
2836             const GLbyte *s = (const GLbyte *) src;
2837             for (i = 0; i < n; i++)
2838                indexes[i] = s[i];
2839          }
2840          break;
2841       case GL_UNSIGNED_SHORT:
2842          {
2843             GLuint i;
2844             const GLushort *s = (const GLushort *) src;
2845             if (unpack->SwapBytes) {
2846                for (i = 0; i < n; i++) {
2847                   GLushort value = s[i];
2848                   SWAP2BYTE(value);
2849                   indexes[i] = value;
2850                }
2851             }
2852             else {
2853                for (i = 0; i < n; i++)
2854                   indexes[i] = s[i];
2855             }
2856          }
2857          break;
2858       case GL_SHORT:
2859          {
2860             GLuint i;
2861             const GLshort *s = (const GLshort *) src;
2862             if (unpack->SwapBytes) {
2863                for (i = 0; i < n; i++) {
2864                   GLshort value = s[i];
2865                   SWAP2BYTE(value);
2866                   indexes[i] = value;
2867                }
2868             }
2869             else {
2870                for (i = 0; i < n; i++)
2871                   indexes[i] = s[i];
2872             }
2873          }
2874          break;
2875       case GL_UNSIGNED_INT:
2876          {
2877             GLuint i;
2878             const GLuint *s = (const GLuint *) src;
2879             if (unpack->SwapBytes) {
2880                for (i = 0; i < n; i++) {
2881                   GLuint value = s[i];
2882                   SWAP4BYTE(value);
2883                   indexes[i] = value;
2884                }
2885             }
2886             else {
2887                for (i = 0; i < n; i++)
2888                   indexes[i] = s[i];
2889             }
2890          }
2891          break;
2892       case GL_INT:
2893          {
2894             GLuint i;
2895             const GLint *s = (const GLint *) src;
2896             if (unpack->SwapBytes) {
2897                for (i = 0; i < n; i++) {
2898                   GLint value = s[i];
2899                   SWAP4BYTE(value);
2900                   indexes[i] = value;
2901                }
2902             }
2903             else {
2904                for (i = 0; i < n; i++)
2905                   indexes[i] = s[i];
2906             }
2907          }
2908          break;
2909       case GL_FLOAT:
2910          {
2911             GLuint i;
2912             const GLfloat *s = (const GLfloat *) src;
2913             if (unpack->SwapBytes) {
2914                for (i = 0; i < n; i++) {
2915                   GLfloat value = s[i];
2916                   SWAP4BYTE(value);
2917                   indexes[i] = (GLuint) value;
2918                }
2919             }
2920             else {
2921                for (i = 0; i < n; i++)
2922                   indexes[i] = (GLuint) s[i];
2923             }
2924          }
2925          break;
2926       case GL_HALF_FLOAT_ARB:
2927          {
2928             GLuint i;
2929             const GLhalfARB *s = (const GLhalfARB *) src;
2930             if (unpack->SwapBytes) {
2931                for (i = 0; i < n; i++) {
2932                   GLhalfARB value = s[i];
2933                   SWAP2BYTE(value);
2934                   indexes[i] = (GLuint) _mesa_half_to_float(value);
2935                }
2936             }
2937             else {
2938                for (i = 0; i < n; i++)
2939                   indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
2940             }
2941          }
2942          break;
2943       case GL_UNSIGNED_INT_24_8_EXT:
2944          {
2945             GLuint i;
2946             const GLuint *s = (const GLuint *) src;
2947             if (unpack->SwapBytes) {
2948                for (i = 0; i < n; i++) {
2949                   GLuint value = s[i];
2950                   SWAP4BYTE(value);
2951                   indexes[i] = value & 0xff;  /* lower 8 bits */
2952                }
2953             }
2954             else {
2955                for (i = 0; i < n; i++)
2956                   indexes[i] = s[i] & 0xff;  /* lower 8 bits */
2957             }
2958          }
2959          break;
2960       case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
2961          {
2962             GLuint i;
2963             const GLuint *s = (const GLuint *) src;
2964             if (unpack->SwapBytes) {
2965                for (i = 0; i < n; i++) {
2966                   GLuint value = s[i*2+1];
2967                   SWAP4BYTE(value);
2968                   indexes[i] = value & 0xff;  /* lower 8 bits */
2969                }
2970             }
2971             else {
2972                for (i = 0; i < n; i++)
2973                   indexes[i] = s[i*2+1] & 0xff;  /* lower 8 bits */
2974             }
2975          }
2976          break;
2977 
2978       default:
2979          _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
2980          return;
2981    }
2982 }
2983 
2984 
2985 /**
2986  * Return source/dest RGBA indexes for unpacking pixels.
2987  */
2988 static void
get_component_mapping(GLenum format,GLint * rSrc,GLint * gSrc,GLint * bSrc,GLint * aSrc,GLint * rDst,GLint * gDst,GLint * bDst,GLint * aDst)2989 get_component_mapping(GLenum format,
2990                       GLint *rSrc,
2991                       GLint *gSrc,
2992                       GLint *bSrc,
2993                       GLint *aSrc,
2994                       GLint *rDst,
2995                       GLint *gDst,
2996                       GLint *bDst,
2997                       GLint *aDst)
2998 {
2999    switch (format) {
3000    case GL_RED:
3001    case GL_RED_INTEGER_EXT:
3002       *rSrc = 0;
3003       *gSrc = *bSrc = *aSrc = -1;
3004       break;
3005    case GL_GREEN:
3006    case GL_GREEN_INTEGER_EXT:
3007       *gSrc = 0;
3008       *rSrc = *bSrc = *aSrc = -1;
3009       break;
3010    case GL_BLUE:
3011    case GL_BLUE_INTEGER_EXT:
3012       *bSrc = 0;
3013       *rSrc = *gSrc = *aSrc = -1;
3014       break;
3015    case GL_ALPHA:
3016    case GL_ALPHA_INTEGER_EXT:
3017       *rSrc = *gSrc = *bSrc = -1;
3018       *aSrc = 0;
3019       break;
3020    case GL_LUMINANCE:
3021    case GL_LUMINANCE_INTEGER_EXT:
3022       *rSrc = *gSrc = *bSrc = 0;
3023       *aSrc = -1;
3024       break;
3025    case GL_LUMINANCE_ALPHA:
3026    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
3027       *rSrc = *gSrc = *bSrc = 0;
3028       *aSrc = 1;
3029       break;
3030    case GL_INTENSITY:
3031       *rSrc = *gSrc = *bSrc = *aSrc = 0;
3032       break;
3033    case GL_RG:
3034    case GL_RG_INTEGER:
3035       *rSrc = 0;
3036       *gSrc = 1;
3037       *bSrc = -1;
3038       *aSrc = -1;
3039       *rDst = 0;
3040       *gDst = 1;
3041       *bDst = 2;
3042       *aDst = 3;
3043       break;
3044    case GL_RGB:
3045    case GL_RGB_INTEGER:
3046       *rSrc = 0;
3047       *gSrc = 1;
3048       *bSrc = 2;
3049       *aSrc = -1;
3050       *rDst = 0;
3051       *gDst = 1;
3052       *bDst = 2;
3053       *aDst = 3;
3054       break;
3055    case GL_BGR:
3056    case GL_BGR_INTEGER:
3057       *rSrc = 2;
3058       *gSrc = 1;
3059       *bSrc = 0;
3060       *aSrc = -1;
3061       *rDst = 2;
3062       *gDst = 1;
3063       *bDst = 0;
3064       *aDst = 3;
3065       break;
3066    case GL_RGBA:
3067    case GL_RGBA_INTEGER:
3068       *rSrc = 0;
3069       *gSrc = 1;
3070       *bSrc = 2;
3071       *aSrc = 3;
3072       *rDst = 0;
3073       *gDst = 1;
3074       *bDst = 2;
3075       *aDst = 3;
3076       break;
3077    case GL_BGRA:
3078    case GL_BGRA_INTEGER:
3079       *rSrc = 2;
3080       *gSrc = 1;
3081       *bSrc = 0;
3082       *aSrc = 3;
3083       *rDst = 2;
3084       *gDst = 1;
3085       *bDst = 0;
3086       *aDst = 3;
3087       break;
3088    case GL_ABGR_EXT:
3089       *rSrc = 3;
3090       *gSrc = 2;
3091       *bSrc = 1;
3092       *aSrc = 0;
3093       *rDst = 3;
3094       *gDst = 2;
3095       *bDst = 1;
3096       *aDst = 0;
3097       break;
3098    case GL_DU8DV8_ATI:
3099    case GL_DUDV_ATI:
3100       *rSrc = 0;
3101       *gSrc = 1;
3102       *bSrc = -1;
3103       *aSrc = -1;
3104       break;
3105    default:
3106       _mesa_problem(NULL, "bad srcFormat %s in get_component_mapping",
3107                     _mesa_lookup_enum_by_nr(format));
3108       return;
3109    }
3110 }
3111 
3112 
3113 
3114 /*
3115  * This function extracts floating point RGBA values from arbitrary
3116  * image data.  srcFormat and srcType are the format and type parameters
3117  * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
3118  *
3119  * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
3120  * implements the "Conversion to floating point", "Conversion to RGB",
3121  * and "Final Expansion to RGBA" operations.
3122  *
3123  * Args:  n - number of pixels
3124  *        rgba - output colors
3125  *        srcFormat - format of incoming data
3126  *        srcType - data type of incoming data
3127  *        src - source data pointer
3128  *        swapBytes - perform byteswapping of incoming data?
3129  */
3130 static void
extract_float_rgba(GLuint n,GLfloat rgba[][4],GLenum srcFormat,GLenum srcType,const GLvoid * src,GLboolean swapBytes)3131 extract_float_rgba(GLuint n, GLfloat rgba[][4],
3132                    GLenum srcFormat, GLenum srcType, const GLvoid *src,
3133                    GLboolean swapBytes)
3134 {
3135    GLint rSrc, gSrc, bSrc, aSrc;
3136    GLint stride;
3137    GLint rDst, bDst, gDst, aDst;
3138    GLboolean intFormat;
3139    GLfloat rs = 1.0f, gs = 1.0f, bs = 1.0f, as = 1.0f; /* scale factors */
3140 
3141    ASSERT(srcFormat == GL_RED ||
3142           srcFormat == GL_GREEN ||
3143           srcFormat == GL_BLUE ||
3144           srcFormat == GL_ALPHA ||
3145           srcFormat == GL_LUMINANCE ||
3146           srcFormat == GL_LUMINANCE_ALPHA ||
3147           srcFormat == GL_INTENSITY ||
3148           srcFormat == GL_RG ||
3149           srcFormat == GL_RGB ||
3150           srcFormat == GL_BGR ||
3151           srcFormat == GL_RGBA ||
3152           srcFormat == GL_BGRA ||
3153           srcFormat == GL_ABGR_EXT ||
3154           srcFormat == GL_DU8DV8_ATI ||
3155           srcFormat == GL_DUDV_ATI ||
3156           srcFormat == GL_RED_INTEGER_EXT ||
3157           srcFormat == GL_GREEN_INTEGER_EXT ||
3158           srcFormat == GL_BLUE_INTEGER_EXT ||
3159           srcFormat == GL_ALPHA_INTEGER_EXT ||
3160           srcFormat == GL_RG_INTEGER ||
3161           srcFormat == GL_RGB_INTEGER_EXT ||
3162           srcFormat == GL_RGBA_INTEGER_EXT ||
3163           srcFormat == GL_BGR_INTEGER_EXT ||
3164           srcFormat == GL_BGRA_INTEGER_EXT ||
3165           srcFormat == GL_LUMINANCE_INTEGER_EXT ||
3166           srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
3167 
3168    ASSERT(srcType == GL_UNSIGNED_BYTE ||
3169           srcType == GL_BYTE ||
3170           srcType == GL_UNSIGNED_SHORT ||
3171           srcType == GL_SHORT ||
3172           srcType == GL_UNSIGNED_INT ||
3173           srcType == GL_INT ||
3174           srcType == GL_HALF_FLOAT_ARB ||
3175           srcType == GL_FLOAT ||
3176           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3177           srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3178           srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3179           srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3180           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3181           srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3182           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3183           srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3184           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3185           srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3186           srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3187           srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
3188           srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
3189           srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
3190 
3191    get_component_mapping(srcFormat,
3192                          &rSrc, &gSrc, &bSrc, &aSrc,
3193                          &rDst, &gDst, &bDst, &aDst);
3194 
3195    stride = _mesa_components_in_format(srcFormat);
3196 
3197    intFormat = _mesa_is_enum_format_integer(srcFormat);
3198 
3199 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT_FLT, DEFAULT_INT, TYPE, CONVERSION) \
3200    if ((SRC_INDEX) < 0) {						\
3201       GLuint i;								\
3202       if (intFormat) {							\
3203          for (i = 0; i < n; i++) {					\
3204             rgba[i][DST_INDEX] = DEFAULT_INT;				\
3205          }								\
3206       }									\
3207       else {								\
3208          for (i = 0; i < n; i++) {					\
3209             rgba[i][DST_INDEX] = DEFAULT_FLT;				\
3210          }								\
3211       }									\
3212    }									\
3213    else if (swapBytes) {						\
3214       const TYPE *s = (const TYPE *) src;				\
3215       GLuint i;								\
3216       for (i = 0; i < n; i++) {						\
3217          TYPE value = s[SRC_INDEX];					\
3218          if (sizeof(TYPE) == 2) {					\
3219             SWAP2BYTE(value);						\
3220          }								\
3221          else if (sizeof(TYPE) == 4) {					\
3222             SWAP4BYTE(value);						\
3223          }								\
3224          if (intFormat)							\
3225             rgba[i][DST_INDEX] = (GLfloat) value;			\
3226          else								\
3227             rgba[i][DST_INDEX] = (GLfloat) CONVERSION(value);		\
3228          s += stride;							\
3229       }									\
3230    }									\
3231    else {								\
3232       const TYPE *s = (const TYPE *) src;				\
3233       GLuint i;								\
3234       if (intFormat) {							\
3235          for (i = 0; i < n; i++) {					\
3236             rgba[i][DST_INDEX] = (GLfloat) s[SRC_INDEX];		\
3237             s += stride;						\
3238          }								\
3239       }									\
3240       else {								\
3241          for (i = 0; i < n; i++) {					\
3242             rgba[i][DST_INDEX] = (GLfloat) CONVERSION(s[SRC_INDEX]);	\
3243             s += stride;						\
3244          }								\
3245       }									\
3246    }
3247 
3248    switch (srcType) {
3249       case GL_UNSIGNED_BYTE:
3250          PROCESS(rSrc, RCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
3251          PROCESS(gSrc, GCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
3252          PROCESS(bSrc, BCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
3253          PROCESS(aSrc, ACOMP, 1.0F, 255, GLubyte, UBYTE_TO_FLOAT);
3254          break;
3255       case GL_BYTE:
3256          PROCESS(rSrc, RCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOATZ);
3257          PROCESS(gSrc, GCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOATZ);
3258          PROCESS(bSrc, BCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOATZ);
3259          PROCESS(aSrc, ACOMP, 1.0F, 127, GLbyte, BYTE_TO_FLOATZ);
3260          break;
3261       case GL_UNSIGNED_SHORT:
3262          PROCESS(rSrc, RCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
3263          PROCESS(gSrc, GCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
3264          PROCESS(bSrc, BCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
3265          PROCESS(aSrc, ACOMP, 1.0F, 0xffff, GLushort, USHORT_TO_FLOAT);
3266          break;
3267       case GL_SHORT:
3268          PROCESS(rSrc, RCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOATZ);
3269          PROCESS(gSrc, GCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOATZ);
3270          PROCESS(bSrc, BCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOATZ);
3271          PROCESS(aSrc, ACOMP, 1.0F, 32767, GLshort, SHORT_TO_FLOATZ);
3272          break;
3273       case GL_UNSIGNED_INT:
3274          PROCESS(rSrc, RCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
3275          PROCESS(gSrc, GCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
3276          PROCESS(bSrc, BCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
3277          PROCESS(aSrc, ACOMP, 1.0F, 0xffffffff, GLuint, UINT_TO_FLOAT);
3278          break;
3279       case GL_INT:
3280          PROCESS(rSrc, RCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
3281          PROCESS(gSrc, GCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
3282          PROCESS(bSrc, BCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
3283          PROCESS(aSrc, ACOMP, 1.0F, 2147483647, GLint, INT_TO_FLOAT);
3284          break;
3285       case GL_FLOAT:
3286          PROCESS(rSrc, RCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
3287          PROCESS(gSrc, GCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
3288          PROCESS(bSrc, BCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
3289          PROCESS(aSrc, ACOMP, 1.0F, 1.0F, GLfloat, (GLfloat));
3290          break;
3291       case GL_HALF_FLOAT_ARB:
3292          PROCESS(rSrc, RCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
3293          PROCESS(gSrc, GCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
3294          PROCESS(bSrc, BCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
3295          PROCESS(aSrc, ACOMP, 1.0F, 1.0F, GLhalfARB, _mesa_half_to_float);
3296          break;
3297       case GL_UNSIGNED_BYTE_3_3_2:
3298          {
3299             const GLubyte *ubsrc = (const GLubyte *) src;
3300             GLuint i;
3301             if (!intFormat) {
3302                rs = 1.0F / 7.0F;
3303                gs = 1.0F / 7.0F;
3304                bs = 1.0F / 3.0F;
3305             }
3306             for (i = 0; i < n; i ++) {
3307                GLubyte p = ubsrc[i];
3308                rgba[i][rDst] = ((p >> 5)      ) * rs;
3309                rgba[i][gDst] = ((p >> 2) & 0x7) * gs;
3310                rgba[i][bDst] = ((p     ) & 0x3) * bs;
3311                rgba[i][aDst] = 1.0F;
3312             }
3313          }
3314          break;
3315       case GL_UNSIGNED_BYTE_2_3_3_REV:
3316          {
3317             const GLubyte *ubsrc = (const GLubyte *) src;
3318             GLuint i;
3319             if (!intFormat) {
3320                rs = 1.0F / 7.0F;
3321                gs = 1.0F / 7.0F;
3322                bs = 1.0F / 3.0F;
3323             }
3324             for (i = 0; i < n; i ++) {
3325                GLubyte p = ubsrc[i];
3326                rgba[i][rDst] = ((p     ) & 0x7) * rs;
3327                rgba[i][gDst] = ((p >> 3) & 0x7) * gs;
3328                rgba[i][bDst] = ((p >> 6)      ) * bs;
3329                rgba[i][aDst] = 1.0F;
3330             }
3331          }
3332          break;
3333       case GL_UNSIGNED_SHORT_5_6_5:
3334          if (!intFormat) {
3335             rs = 1.0F / 31.0F;
3336             gs = 1.0F / 63.0F;
3337             bs = 1.0F / 31.0F;
3338          }
3339          if (swapBytes) {
3340             const GLushort *ussrc = (const GLushort *) src;
3341             GLuint i;
3342             for (i = 0; i < n; i ++) {
3343                GLushort p = ussrc[i];
3344                SWAP2BYTE(p);
3345                rgba[i][rDst] = ((p >> 11)       ) * rs;
3346                rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
3347                rgba[i][bDst] = ((p      ) & 0x1f) * bs;
3348                rgba[i][aDst] = 1.0F;
3349             }
3350          }
3351          else {
3352             const GLushort *ussrc = (const GLushort *) src;
3353             GLuint i;
3354             for (i = 0; i < n; i ++) {
3355                GLushort p = ussrc[i];
3356                rgba[i][rDst] = ((p >> 11)       ) * rs;
3357                rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
3358                rgba[i][bDst] = ((p      ) & 0x1f) * bs;
3359                rgba[i][aDst] = 1.0F;
3360             }
3361          }
3362          break;
3363       case GL_UNSIGNED_SHORT_5_6_5_REV:
3364          if (!intFormat) {
3365             rs = 1.0F / 31.0F;
3366             gs = 1.0F / 63.0F;
3367             bs = 1.0F / 31.0F;
3368          }
3369          if (swapBytes) {
3370             const GLushort *ussrc = (const GLushort *) src;
3371             GLuint i;
3372             for (i = 0; i < n; i ++) {
3373                GLushort p = ussrc[i];
3374                SWAP2BYTE(p);
3375                rgba[i][rDst] = ((p      ) & 0x1f) * rs;
3376                rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
3377                rgba[i][bDst] = ((p >> 11)       ) * bs;
3378                rgba[i][aDst] = 1.0F;
3379             }
3380          }
3381          else {
3382             const GLushort *ussrc = (const GLushort *) src;
3383             GLuint i;
3384             for (i = 0; i < n; i ++) {
3385                GLushort p = ussrc[i];
3386                rgba[i][rDst] = ((p      ) & 0x1f) * rs;
3387                rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
3388                rgba[i][bDst] = ((p >> 11)       ) * bs;
3389                rgba[i][aDst] = 1.0F;
3390             }
3391          }
3392          break;
3393       case GL_UNSIGNED_SHORT_4_4_4_4:
3394          if (!intFormat) {
3395             rs = gs = bs = as = 1.0F / 15.0F;
3396          }
3397          if (swapBytes) {
3398             const GLushort *ussrc = (const GLushort *) src;
3399             GLuint i;
3400             for (i = 0; i < n; i ++) {
3401                GLushort p = ussrc[i];
3402                SWAP2BYTE(p);
3403                rgba[i][rDst] = ((p >> 12)      ) * rs;
3404                rgba[i][gDst] = ((p >>  8) & 0xf) * gs;
3405                rgba[i][bDst] = ((p >>  4) & 0xf) * bs;
3406                rgba[i][aDst] = ((p      ) & 0xf) * as;
3407             }
3408          }
3409          else {
3410             const GLushort *ussrc = (const GLushort *) src;
3411             GLuint i;
3412             for (i = 0; i < n; i ++) {
3413                GLushort p = ussrc[i];
3414                rgba[i][rDst] = ((p >> 12)      ) * rs;
3415                rgba[i][gDst] = ((p >>  8) & 0xf) * gs;
3416                rgba[i][bDst] = ((p >>  4) & 0xf) * bs;
3417                rgba[i][aDst] = ((p      ) & 0xf) * as;
3418             }
3419          }
3420          break;
3421       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3422          if (!intFormat) {
3423             rs = gs = bs = as = 1.0F / 15.0F;
3424          }
3425          if (swapBytes) {
3426             const GLushort *ussrc = (const GLushort *) src;
3427             GLuint i;
3428             for (i = 0; i < n; i ++) {
3429                GLushort p = ussrc[i];
3430                SWAP2BYTE(p);
3431                rgba[i][rDst] = ((p      ) & 0xf) * rs;
3432                rgba[i][gDst] = ((p >>  4) & 0xf) * gs;
3433                rgba[i][bDst] = ((p >>  8) & 0xf) * bs;
3434                rgba[i][aDst] = ((p >> 12)      ) * as;
3435             }
3436          }
3437          else {
3438             const GLushort *ussrc = (const GLushort *) src;
3439             GLuint i;
3440             for (i = 0; i < n; i ++) {
3441                GLushort p = ussrc[i];
3442                rgba[i][rDst] = ((p      ) & 0xf) * rs;
3443                rgba[i][gDst] = ((p >>  4) & 0xf) * gs;
3444                rgba[i][bDst] = ((p >>  8) & 0xf) * bs;
3445                rgba[i][aDst] = ((p >> 12)      ) * as;
3446             }
3447          }
3448          break;
3449       case GL_UNSIGNED_SHORT_5_5_5_1:
3450          if (!intFormat) {
3451             rs = gs = bs = 1.0F / 31.0F;
3452          }
3453          if (swapBytes) {
3454             const GLushort *ussrc = (const GLushort *) src;
3455             GLuint i;
3456             for (i = 0; i < n; i ++) {
3457                GLushort p = ussrc[i];
3458                SWAP2BYTE(p);
3459                rgba[i][rDst] = ((p >> 11)       ) * rs;
3460                rgba[i][gDst] = ((p >>  6) & 0x1f) * gs;
3461                rgba[i][bDst] = ((p >>  1) & 0x1f) * bs;
3462                rgba[i][aDst] = ((p      ) & 0x1)  * as;
3463             }
3464          }
3465          else {
3466             const GLushort *ussrc = (const GLushort *) src;
3467             GLuint i;
3468             for (i = 0; i < n; i ++) {
3469                GLushort p = ussrc[i];
3470                rgba[i][rDst] = ((p >> 11)       ) * rs;
3471                rgba[i][gDst] = ((p >>  6) & 0x1f) * gs;
3472                rgba[i][bDst] = ((p >>  1) & 0x1f) * bs;
3473                rgba[i][aDst] = ((p      ) & 0x1)  * as;
3474             }
3475          }
3476          break;
3477       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3478          if (!intFormat) {
3479             rs = gs = bs = 1.0F / 31.0F;
3480          }
3481          if (swapBytes) {
3482             const GLushort *ussrc = (const GLushort *) src;
3483             GLuint i;
3484             for (i = 0; i < n; i ++) {
3485                GLushort p = ussrc[i];
3486                SWAP2BYTE(p);
3487                rgba[i][rDst] = ((p      ) & 0x1f) * rs;
3488                rgba[i][gDst] = ((p >>  5) & 0x1f) * gs;
3489                rgba[i][bDst] = ((p >> 10) & 0x1f) * bs;
3490                rgba[i][aDst] = ((p >> 15)       ) * as;
3491             }
3492          }
3493          else {
3494             const GLushort *ussrc = (const GLushort *) src;
3495             GLuint i;
3496             for (i = 0; i < n; i ++) {
3497                GLushort p = ussrc[i];
3498                rgba[i][rDst] = ((p      ) & 0x1f) * rs;
3499                rgba[i][gDst] = ((p >>  5) & 0x1f) * gs;
3500                rgba[i][bDst] = ((p >> 10) & 0x1f) * bs;
3501                rgba[i][aDst] = ((p >> 15)       ) * as;
3502             }
3503          }
3504          break;
3505       case GL_UNSIGNED_INT_8_8_8_8:
3506          if (swapBytes) {
3507             const GLuint *uisrc = (const GLuint *) src;
3508             GLuint i;
3509             if (intFormat) {
3510                for (i = 0; i < n; i ++) {
3511                   GLuint p = uisrc[i];
3512                   rgba[i][rDst] = (GLfloat) ((p      ) & 0xff);
3513                   rgba[i][gDst] = (GLfloat) ((p >>  8) & 0xff);
3514                   rgba[i][bDst] = (GLfloat) ((p >> 16) & 0xff);
3515                   rgba[i][aDst] = (GLfloat) ((p >> 24)       );
3516                }
3517             }
3518             else {
3519                for (i = 0; i < n; i ++) {
3520                   GLuint p = uisrc[i];
3521                   rgba[i][rDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
3522                   rgba[i][gDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3523                   rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3524                   rgba[i][aDst] = UBYTE_TO_FLOAT((p >> 24)       );
3525                }
3526             }
3527          }
3528          else {
3529             const GLuint *uisrc = (const GLuint *) src;
3530             GLuint i;
3531             if (intFormat) {
3532                for (i = 0; i < n; i ++) {
3533                   GLuint p = uisrc[i];
3534                   rgba[i][rDst] = (GLfloat) ((p >> 24)       );
3535                   rgba[i][gDst] = (GLfloat) ((p >> 16) & 0xff);
3536                   rgba[i][bDst] = (GLfloat) ((p >>  8) & 0xff);
3537                   rgba[i][aDst] = (GLfloat) ((p      ) & 0xff);
3538                }
3539             }
3540             else {
3541                for (i = 0; i < n; i ++) {
3542                   GLuint p = uisrc[i];
3543                   rgba[i][rDst] = UBYTE_TO_FLOAT((p >> 24)       );
3544                   rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3545                   rgba[i][bDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3546                   rgba[i][aDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
3547                }
3548             }
3549          }
3550          break;
3551       case GL_UNSIGNED_INT_8_8_8_8_REV:
3552          if (swapBytes) {
3553             const GLuint *uisrc = (const GLuint *) src;
3554             GLuint i;
3555             if (intFormat) {
3556                for (i = 0; i < n; i ++) {
3557                   GLuint p = uisrc[i];
3558                   rgba[i][rDst] = (GLfloat) ((p >> 24)       );
3559                   rgba[i][gDst] = (GLfloat) ((p >> 16) & 0xff);
3560                   rgba[i][bDst] = (GLfloat) ((p >>  8) & 0xff);
3561                   rgba[i][aDst] = (GLfloat) ((p      ) & 0xff);
3562                }
3563             }
3564             else {
3565                for (i = 0; i < n; i ++) {
3566                   GLuint p = uisrc[i];
3567                   rgba[i][rDst] = UBYTE_TO_FLOAT((p >> 24)       );
3568                   rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3569                   rgba[i][bDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3570                   rgba[i][aDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
3571                }
3572             }
3573          }
3574          else {
3575             const GLuint *uisrc = (const GLuint *) src;
3576             GLuint i;
3577             if (intFormat) {
3578                for (i = 0; i < n; i ++) {
3579                   GLuint p = uisrc[i];
3580                   rgba[i][rDst] = (GLfloat) ((p      ) & 0xff);
3581                   rgba[i][gDst] = (GLfloat) ((p >>  8) & 0xff);
3582                   rgba[i][bDst] = (GLfloat) ((p >> 16) & 0xff);
3583                   rgba[i][aDst] = (GLfloat) ((p >> 24)       );
3584                }
3585             }
3586             else {
3587                for (i = 0; i < n; i ++) {
3588                   GLuint p = uisrc[i];
3589                   rgba[i][rDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
3590                   rgba[i][gDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3591                   rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3592                   rgba[i][aDst] = UBYTE_TO_FLOAT((p >> 24)       );
3593                }
3594             }
3595          }
3596          break;
3597       case GL_UNSIGNED_INT_10_10_10_2:
3598          if (!intFormat) {
3599             rs = 1.0F / 1023.0F;
3600             gs = 1.0F / 1023.0F;
3601             bs = 1.0F / 1023.0F;
3602             as = 1.0F / 3.0F;
3603          }
3604          if (swapBytes) {
3605             const GLuint *uisrc = (const GLuint *) src;
3606             GLuint i;
3607             for (i = 0; i < n; i ++) {
3608                GLuint p = uisrc[i];
3609                SWAP4BYTE(p);
3610                rgba[i][rDst] = ((p >> 22)        ) * rs;
3611                rgba[i][gDst] = ((p >> 12) & 0x3ff) * gs;
3612                rgba[i][bDst] = ((p >>  2) & 0x3ff) * bs;
3613                rgba[i][aDst] = ((p      ) & 0x3  ) * as;
3614             }
3615          }
3616          else {
3617             const GLuint *uisrc = (const GLuint *) src;
3618             GLuint i;
3619             for (i = 0; i < n; i ++) {
3620                GLuint p = uisrc[i];
3621                rgba[i][rDst] = ((p >> 22)        ) * rs;
3622                rgba[i][gDst] = ((p >> 12) & 0x3ff) * gs;
3623                rgba[i][bDst] = ((p >>  2) & 0x3ff) * bs;
3624                rgba[i][aDst] = ((p      ) & 0x3  ) * as;
3625             }
3626          }
3627          break;
3628       case GL_UNSIGNED_INT_2_10_10_10_REV:
3629          if (!intFormat) {
3630             rs = 1.0F / 1023.0F;
3631             gs = 1.0F / 1023.0F;
3632             bs = 1.0F / 1023.0F;
3633             as = 1.0F / 3.0F;
3634          }
3635          if (swapBytes) {
3636             const GLuint *uisrc = (const GLuint *) src;
3637             GLuint i;
3638             for (i = 0; i < n; i ++) {
3639                GLuint p = uisrc[i];
3640                SWAP4BYTE(p);
3641                rgba[i][rDst] = ((p      ) & 0x3ff) * rs;
3642                rgba[i][gDst] = ((p >> 10) & 0x3ff) * gs;
3643                rgba[i][bDst] = ((p >> 20) & 0x3ff) * bs;
3644                rgba[i][aDst] = ((p >> 30)        ) * as;
3645             }
3646          }
3647          else {
3648             const GLuint *uisrc = (const GLuint *) src;
3649             GLuint i;
3650             for (i = 0; i < n; i ++) {
3651                GLuint p = uisrc[i];
3652                rgba[i][rDst] = ((p      ) & 0x3ff) * rs;
3653                rgba[i][gDst] = ((p >> 10) & 0x3ff) * gs;
3654                rgba[i][bDst] = ((p >> 20) & 0x3ff) * bs;
3655                rgba[i][aDst] = ((p >> 30)        ) * as;
3656             }
3657          }
3658          break;
3659       case GL_UNSIGNED_INT_5_9_9_9_REV:
3660          if (swapBytes) {
3661             const GLuint *uisrc = (const GLuint *) src;
3662             GLuint i;
3663             GLfloat f[3];
3664             for (i = 0; i < n; i ++) {
3665                GLuint p = uisrc[i];
3666                SWAP4BYTE(p);
3667                rgb9e5_to_float3(p, f);
3668                rgba[i][rDst] = f[0];
3669                rgba[i][gDst] = f[1];
3670                rgba[i][bDst] = f[2];
3671                rgba[i][aDst] = 1.0F;
3672             }
3673          }
3674          else {
3675             const GLuint *uisrc = (const GLuint *) src;
3676             GLuint i;
3677             GLfloat f[3];
3678             for (i = 0; i < n; i ++) {
3679                rgb9e5_to_float3(uisrc[i], f);
3680                rgba[i][rDst] = f[0];
3681                rgba[i][gDst] = f[1];
3682                rgba[i][bDst] = f[2];
3683                rgba[i][aDst] = 1.0F;
3684             }
3685          }
3686          break;
3687       case GL_UNSIGNED_INT_10F_11F_11F_REV:
3688          if (swapBytes) {
3689             const GLuint *uisrc = (const GLuint *) src;
3690             GLuint i;
3691             GLfloat f[3];
3692             for (i = 0; i < n; i ++) {
3693                GLuint p = uisrc[i];
3694                SWAP4BYTE(p);
3695                r11g11b10f_to_float3(p, f);
3696                rgba[i][rDst] = f[0];
3697                rgba[i][gDst] = f[1];
3698                rgba[i][bDst] = f[2];
3699                rgba[i][aDst] = 1.0F;
3700             }
3701          }
3702          else {
3703             const GLuint *uisrc = (const GLuint *) src;
3704             GLuint i;
3705             GLfloat f[3];
3706             for (i = 0; i < n; i ++) {
3707                r11g11b10f_to_float3(uisrc[i], f);
3708                rgba[i][rDst] = f[0];
3709                rgba[i][gDst] = f[1];
3710                rgba[i][bDst] = f[2];
3711                rgba[i][aDst] = 1.0F;
3712             }
3713          }
3714          break;
3715       default:
3716          _mesa_problem(NULL, "bad srcType in extract float data");
3717          break;
3718    }
3719 #undef PROCESS
3720 }
3721 
3722 
3723 static inline GLuint
clamp_float_to_uint(GLfloat f)3724 clamp_float_to_uint(GLfloat f)
3725 {
3726    return f < 0.0F ? 0 : F_TO_I(f);
3727 }
3728 
3729 
3730 static inline GLuint
clamp_half_to_uint(GLhalfARB h)3731 clamp_half_to_uint(GLhalfARB h)
3732 {
3733    GLfloat f = _mesa_half_to_float(h);
3734    return f < 0.0F ? 0 : F_TO_I(f);
3735 }
3736 
3737 
3738 /**
3739  * \sa extract_float_rgba()
3740  */
3741 static void
extract_uint_rgba(GLuint n,GLuint rgba[][4],GLenum srcFormat,GLenum srcType,const GLvoid * src,GLboolean swapBytes)3742 extract_uint_rgba(GLuint n, GLuint rgba[][4],
3743                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
3744                   GLboolean swapBytes)
3745 {
3746    GLint rSrc, gSrc, bSrc, aSrc;
3747    GLint stride;
3748    GLint rDst, bDst, gDst, aDst;
3749 
3750    ASSERT(srcFormat == GL_RED ||
3751           srcFormat == GL_GREEN ||
3752           srcFormat == GL_BLUE ||
3753           srcFormat == GL_ALPHA ||
3754           srcFormat == GL_LUMINANCE ||
3755           srcFormat == GL_LUMINANCE_ALPHA ||
3756           srcFormat == GL_INTENSITY ||
3757           srcFormat == GL_RG ||
3758           srcFormat == GL_RGB ||
3759           srcFormat == GL_BGR ||
3760           srcFormat == GL_RGBA ||
3761           srcFormat == GL_BGRA ||
3762           srcFormat == GL_ABGR_EXT ||
3763           srcFormat == GL_DU8DV8_ATI ||
3764           srcFormat == GL_DUDV_ATI ||
3765           srcFormat == GL_RED_INTEGER_EXT ||
3766           srcFormat == GL_RG_INTEGER ||
3767           srcFormat == GL_GREEN_INTEGER_EXT ||
3768           srcFormat == GL_BLUE_INTEGER_EXT ||
3769           srcFormat == GL_ALPHA_INTEGER_EXT ||
3770           srcFormat == GL_RGB_INTEGER_EXT ||
3771           srcFormat == GL_RGBA_INTEGER_EXT ||
3772           srcFormat == GL_BGR_INTEGER_EXT ||
3773           srcFormat == GL_BGRA_INTEGER_EXT ||
3774           srcFormat == GL_LUMINANCE_INTEGER_EXT ||
3775           srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
3776 
3777    ASSERT(srcType == GL_UNSIGNED_BYTE ||
3778           srcType == GL_BYTE ||
3779           srcType == GL_UNSIGNED_SHORT ||
3780           srcType == GL_SHORT ||
3781           srcType == GL_UNSIGNED_INT ||
3782           srcType == GL_INT ||
3783           srcType == GL_HALF_FLOAT_ARB ||
3784           srcType == GL_FLOAT ||
3785           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3786           srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3787           srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3788           srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3789           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3790           srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3791           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3792           srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3793           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3794           srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3795           srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3796           srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
3797           srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
3798           srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
3799 
3800    get_component_mapping(srcFormat,
3801                          &rSrc, &gSrc, &bSrc, &aSrc,
3802                          &rDst, &gDst, &bDst, &aDst);
3803 
3804    stride = _mesa_components_in_format(srcFormat);
3805 
3806 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT, TYPE, CONVERSION)	\
3807    if ((SRC_INDEX) < 0) {						\
3808       GLuint i;								\
3809       for (i = 0; i < n; i++) {						\
3810          rgba[i][DST_INDEX] = DEFAULT;					\
3811       }									\
3812    }									\
3813    else if (swapBytes) {						\
3814       const TYPE *s = (const TYPE *) src;				\
3815       GLuint i;								\
3816       for (i = 0; i < n; i++) {						\
3817          TYPE value = s[SRC_INDEX];					\
3818          if (sizeof(TYPE) == 2) {					\
3819             SWAP2BYTE(value);						\
3820          }								\
3821          else if (sizeof(TYPE) == 4) {					\
3822             SWAP4BYTE(value);						\
3823          }								\
3824          rgba[i][DST_INDEX] = CONVERSION(value);                        \
3825          s += stride;							\
3826       }									\
3827    }									\
3828    else {								\
3829       const TYPE *s = (const TYPE *) src;				\
3830       GLuint i;								\
3831       for (i = 0; i < n; i++) {						\
3832          rgba[i][DST_INDEX] = CONVERSION(s[SRC_INDEX]);			\
3833          s += stride;							\
3834       }									\
3835    }
3836 
3837    switch (srcType) {
3838       case GL_UNSIGNED_BYTE:
3839          PROCESS(rSrc, RCOMP, 0, GLubyte, (GLuint));
3840          PROCESS(gSrc, GCOMP, 0, GLubyte, (GLuint));
3841          PROCESS(bSrc, BCOMP, 0, GLubyte, (GLuint));
3842          PROCESS(aSrc, ACOMP, 1, GLubyte, (GLuint));
3843          break;
3844       case GL_BYTE:
3845          PROCESS(rSrc, RCOMP, 0, GLbyte, (GLuint));
3846          PROCESS(gSrc, GCOMP, 0, GLbyte, (GLuint));
3847          PROCESS(bSrc, BCOMP, 0, GLbyte, (GLuint));
3848          PROCESS(aSrc, ACOMP, 1, GLbyte, (GLuint));
3849          break;
3850       case GL_UNSIGNED_SHORT:
3851          PROCESS(rSrc, RCOMP, 0, GLushort, (GLuint));
3852          PROCESS(gSrc, GCOMP, 0, GLushort, (GLuint));
3853          PROCESS(bSrc, BCOMP, 0, GLushort, (GLuint));
3854          PROCESS(aSrc, ACOMP, 1, GLushort, (GLuint));
3855          break;
3856       case GL_SHORT:
3857          PROCESS(rSrc, RCOMP, 0, GLshort, (GLuint));
3858          PROCESS(gSrc, GCOMP, 0, GLshort, (GLuint));
3859          PROCESS(bSrc, BCOMP, 0, GLshort, (GLuint));
3860          PROCESS(aSrc, ACOMP, 1, GLshort, (GLuint));
3861          break;
3862       case GL_UNSIGNED_INT:
3863          PROCESS(rSrc, RCOMP, 0, GLuint, (GLuint));
3864          PROCESS(gSrc, GCOMP, 0, GLuint, (GLuint));
3865          PROCESS(bSrc, BCOMP, 0, GLuint, (GLuint));
3866          PROCESS(aSrc, ACOMP, 1, GLuint, (GLuint));
3867          break;
3868       case GL_INT:
3869          PROCESS(rSrc, RCOMP, 0, GLint, (GLuint));
3870          PROCESS(gSrc, GCOMP, 0, GLint, (GLuint));
3871          PROCESS(bSrc, BCOMP, 0, GLint, (GLuint));
3872          PROCESS(aSrc, ACOMP, 1, GLint, (GLuint));
3873          break;
3874       case GL_FLOAT:
3875          PROCESS(rSrc, RCOMP, 0, GLfloat, clamp_float_to_uint);
3876          PROCESS(gSrc, GCOMP, 0, GLfloat, clamp_float_to_uint);
3877          PROCESS(bSrc, BCOMP, 0, GLfloat, clamp_float_to_uint);
3878          PROCESS(aSrc, ACOMP, 1, GLfloat, clamp_float_to_uint);
3879          break;
3880       case GL_HALF_FLOAT_ARB:
3881          PROCESS(rSrc, RCOMP, 0, GLhalfARB, clamp_half_to_uint);
3882          PROCESS(gSrc, GCOMP, 0, GLhalfARB, clamp_half_to_uint);
3883          PROCESS(bSrc, BCOMP, 0, GLhalfARB, clamp_half_to_uint);
3884          PROCESS(aSrc, ACOMP, 1, GLhalfARB, clamp_half_to_uint);
3885          break;
3886       case GL_UNSIGNED_BYTE_3_3_2:
3887          {
3888             const GLubyte *ubsrc = (const GLubyte *) src;
3889             GLuint i;
3890             for (i = 0; i < n; i ++) {
3891                GLubyte p = ubsrc[i];
3892                rgba[i][rDst] = ((p >> 5)      );
3893                rgba[i][gDst] = ((p >> 2) & 0x7);
3894                rgba[i][bDst] = ((p     ) & 0x3);
3895                rgba[i][aDst] = 1;
3896             }
3897          }
3898          break;
3899       case GL_UNSIGNED_BYTE_2_3_3_REV:
3900          {
3901             const GLubyte *ubsrc = (const GLubyte *) src;
3902             GLuint i;
3903             for (i = 0; i < n; i ++) {
3904                GLubyte p = ubsrc[i];
3905                rgba[i][rDst] = ((p     ) & 0x7);
3906                rgba[i][gDst] = ((p >> 3) & 0x7);
3907                rgba[i][bDst] = ((p >> 6)      );
3908                rgba[i][aDst] = 1;
3909             }
3910          }
3911          break;
3912       case GL_UNSIGNED_SHORT_5_6_5:
3913          if (swapBytes) {
3914             const GLushort *ussrc = (const GLushort *) src;
3915             GLuint i;
3916             for (i = 0; i < n; i ++) {
3917                GLushort p = ussrc[i];
3918                SWAP2BYTE(p);
3919                rgba[i][rDst] = ((p >> 11)       );
3920                rgba[i][gDst] = ((p >>  5) & 0x3f);
3921                rgba[i][bDst] = ((p      ) & 0x1f);
3922                rgba[i][aDst] = 1;
3923             }
3924          }
3925          else {
3926             const GLushort *ussrc = (const GLushort *) src;
3927             GLuint i;
3928             for (i = 0; i < n; i ++) {
3929                GLushort p = ussrc[i];
3930                rgba[i][rDst] = ((p >> 11)       );
3931                rgba[i][gDst] = ((p >>  5) & 0x3f);
3932                rgba[i][bDst] = ((p      ) & 0x1f);
3933                rgba[i][aDst] = 1;
3934             }
3935          }
3936          break;
3937       case GL_UNSIGNED_SHORT_5_6_5_REV:
3938          if (swapBytes) {
3939             const GLushort *ussrc = (const GLushort *) src;
3940             GLuint i;
3941             for (i = 0; i < n; i ++) {
3942                GLushort p = ussrc[i];
3943                SWAP2BYTE(p);
3944                rgba[i][rDst] = ((p      ) & 0x1f);
3945                rgba[i][gDst] = ((p >>  5) & 0x3f);
3946                rgba[i][bDst] = ((p >> 11)       );
3947                rgba[i][aDst] = 1;
3948             }
3949          }
3950          else {
3951             const GLushort *ussrc = (const GLushort *) src;
3952             GLuint i;
3953             for (i = 0; i < n; i ++) {
3954                GLushort p = ussrc[i];
3955                rgba[i][rDst] = ((p      ) & 0x1f);
3956                rgba[i][gDst] = ((p >>  5) & 0x3f);
3957                rgba[i][bDst] = ((p >> 11)       );
3958                rgba[i][aDst] = 1;
3959             }
3960          }
3961          break;
3962       case GL_UNSIGNED_SHORT_4_4_4_4:
3963          if (swapBytes) {
3964             const GLushort *ussrc = (const GLushort *) src;
3965             GLuint i;
3966             for (i = 0; i < n; i ++) {
3967                GLushort p = ussrc[i];
3968                SWAP2BYTE(p);
3969                rgba[i][rDst] = ((p >> 12)      );
3970                rgba[i][gDst] = ((p >>  8) & 0xf);
3971                rgba[i][bDst] = ((p >>  4) & 0xf);
3972                rgba[i][aDst] = ((p      ) & 0xf);
3973             }
3974          }
3975          else {
3976             const GLushort *ussrc = (const GLushort *) src;
3977             GLuint i;
3978             for (i = 0; i < n; i ++) {
3979                GLushort p = ussrc[i];
3980                rgba[i][rDst] = ((p >> 12)      );
3981                rgba[i][gDst] = ((p >>  8) & 0xf);
3982                rgba[i][bDst] = ((p >>  4) & 0xf);
3983                rgba[i][aDst] = ((p      ) & 0xf);
3984             }
3985          }
3986          break;
3987       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3988          if (swapBytes) {
3989             const GLushort *ussrc = (const GLushort *) src;
3990             GLuint i;
3991             for (i = 0; i < n; i ++) {
3992                GLushort p = ussrc[i];
3993                SWAP2BYTE(p);
3994                rgba[i][rDst] = ((p      ) & 0xf);
3995                rgba[i][gDst] = ((p >>  4) & 0xf);
3996                rgba[i][bDst] = ((p >>  8) & 0xf);
3997                rgba[i][aDst] = ((p >> 12)      );
3998             }
3999          }
4000          else {
4001             const GLushort *ussrc = (const GLushort *) src;
4002             GLuint i;
4003             for (i = 0; i < n; i ++) {
4004                GLushort p = ussrc[i];
4005                rgba[i][rDst] = ((p      ) & 0xf);
4006                rgba[i][gDst] = ((p >>  4) & 0xf);
4007                rgba[i][bDst] = ((p >>  8) & 0xf);
4008                rgba[i][aDst] = ((p >> 12)      );
4009             }
4010          }
4011          break;
4012       case GL_UNSIGNED_SHORT_5_5_5_1:
4013          if (swapBytes) {
4014             const GLushort *ussrc = (const GLushort *) src;
4015             GLuint i;
4016             for (i = 0; i < n; i ++) {
4017                GLushort p = ussrc[i];
4018                SWAP2BYTE(p);
4019                rgba[i][rDst] = ((p >> 11)       );
4020                rgba[i][gDst] = ((p >>  6) & 0x1f);
4021                rgba[i][bDst] = ((p >>  1) & 0x1f);
4022                rgba[i][aDst] = ((p      ) & 0x1 );
4023             }
4024          }
4025          else {
4026             const GLushort *ussrc = (const GLushort *) src;
4027             GLuint i;
4028             for (i = 0; i < n; i ++) {
4029                GLushort p = ussrc[i];
4030                rgba[i][rDst] = ((p >> 11)       );
4031                rgba[i][gDst] = ((p >>  6) & 0x1f);
4032                rgba[i][bDst] = ((p >>  1) & 0x1f);
4033                rgba[i][aDst] = ((p      ) & 0x1 );
4034             }
4035          }
4036          break;
4037       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
4038          if (swapBytes) {
4039             const GLushort *ussrc = (const GLushort *) src;
4040             GLuint i;
4041             for (i = 0; i < n; i ++) {
4042                GLushort p = ussrc[i];
4043                SWAP2BYTE(p);
4044                rgba[i][rDst] = ((p      ) & 0x1f);
4045                rgba[i][gDst] = ((p >>  5) & 0x1f);
4046                rgba[i][bDst] = ((p >> 10) & 0x1f);
4047                rgba[i][aDst] = ((p >> 15)       );
4048             }
4049          }
4050          else {
4051             const GLushort *ussrc = (const GLushort *) src;
4052             GLuint i;
4053             for (i = 0; i < n; i ++) {
4054                GLushort p = ussrc[i];
4055                rgba[i][rDst] = ((p      ) & 0x1f);
4056                rgba[i][gDst] = ((p >>  5) & 0x1f);
4057                rgba[i][bDst] = ((p >> 10) & 0x1f);
4058                rgba[i][aDst] = ((p >> 15)       );
4059             }
4060          }
4061          break;
4062       case GL_UNSIGNED_INT_8_8_8_8:
4063          if (swapBytes) {
4064             const GLuint *uisrc = (const GLuint *) src;
4065             GLuint i;
4066             for (i = 0; i < n; i ++) {
4067                GLuint p = uisrc[i];
4068                rgba[i][rDst] = ((p      ) & 0xff);
4069                rgba[i][gDst] = ((p >>  8) & 0xff);
4070                rgba[i][bDst] = ((p >> 16) & 0xff);
4071                rgba[i][aDst] = ((p >> 24)       );
4072             }
4073          }
4074          else {
4075             const GLuint *uisrc = (const GLuint *) src;
4076             GLuint i;
4077             for (i = 0; i < n; i ++) {
4078                GLuint p = uisrc[i];
4079                rgba[i][rDst] = ((p >> 24)       );
4080                rgba[i][gDst] = ((p >> 16) & 0xff);
4081                rgba[i][bDst] = ((p >>  8) & 0xff);
4082                rgba[i][aDst] = ((p      ) & 0xff);
4083             }
4084          }
4085          break;
4086       case GL_UNSIGNED_INT_8_8_8_8_REV:
4087          if (swapBytes) {
4088             const GLuint *uisrc = (const GLuint *) src;
4089             GLuint i;
4090             for (i = 0; i < n; i ++) {
4091                GLuint p = uisrc[i];
4092                rgba[i][rDst] = ((p >> 24)       );
4093                rgba[i][gDst] = ((p >> 16) & 0xff);
4094                rgba[i][bDst] = ((p >>  8) & 0xff);
4095                rgba[i][aDst] = ((p      ) & 0xff);
4096             }
4097          }
4098          else {
4099             const GLuint *uisrc = (const GLuint *) src;
4100             GLuint i;
4101             for (i = 0; i < n; i ++) {
4102                GLuint p = uisrc[i];
4103                rgba[i][rDst] = ((p      ) & 0xff);
4104                rgba[i][gDst] = ((p >>  8) & 0xff);
4105                rgba[i][bDst] = ((p >> 16) & 0xff);
4106                rgba[i][aDst] = ((p >> 24)       );
4107             }
4108          }
4109          break;
4110       case GL_UNSIGNED_INT_10_10_10_2:
4111          if (swapBytes) {
4112             const GLuint *uisrc = (const GLuint *) src;
4113             GLuint i;
4114             for (i = 0; i < n; i ++) {
4115                GLuint p = uisrc[i];
4116                SWAP4BYTE(p);
4117                rgba[i][rDst] = ((p >> 22)        );
4118                rgba[i][gDst] = ((p >> 12) & 0x3ff);
4119                rgba[i][bDst] = ((p >>  2) & 0x3ff);
4120                rgba[i][aDst] = ((p      ) & 0x3  );
4121             }
4122          }
4123          else {
4124             const GLuint *uisrc = (const GLuint *) src;
4125             GLuint i;
4126             for (i = 0; i < n; i ++) {
4127                GLuint p = uisrc[i];
4128                rgba[i][rDst] = ((p >> 22)        );
4129                rgba[i][gDst] = ((p >> 12) & 0x3ff);
4130                rgba[i][bDst] = ((p >>  2) & 0x3ff);
4131                rgba[i][aDst] = ((p      ) & 0x3  );
4132             }
4133          }
4134          break;
4135       case GL_UNSIGNED_INT_2_10_10_10_REV:
4136          if (swapBytes) {
4137             const GLuint *uisrc = (const GLuint *) src;
4138             GLuint i;
4139             for (i = 0; i < n; i ++) {
4140                GLuint p = uisrc[i];
4141                SWAP4BYTE(p);
4142                rgba[i][rDst] = ((p      ) & 0x3ff);
4143                rgba[i][gDst] = ((p >> 10) & 0x3ff);
4144                rgba[i][bDst] = ((p >> 20) & 0x3ff);
4145                rgba[i][aDst] = ((p >> 30)        );
4146             }
4147          }
4148          else {
4149             const GLuint *uisrc = (const GLuint *) src;
4150             GLuint i;
4151             for (i = 0; i < n; i ++) {
4152                GLuint p = uisrc[i];
4153                rgba[i][rDst] = ((p      ) & 0x3ff);
4154                rgba[i][gDst] = ((p >> 10) & 0x3ff);
4155                rgba[i][bDst] = ((p >> 20) & 0x3ff);
4156                rgba[i][aDst] = ((p >> 30)        );
4157             }
4158          }
4159          break;
4160       case GL_UNSIGNED_INT_5_9_9_9_REV:
4161          if (swapBytes) {
4162             const GLuint *uisrc = (const GLuint *) src;
4163             GLuint i;
4164             float f[3];
4165             for (i = 0; i < n; i ++) {
4166                GLuint p = uisrc[i];
4167                SWAP4BYTE(p);
4168                rgb9e5_to_float3(p, f);
4169                rgba[i][rDst] = clamp_float_to_uint(f[0]);
4170                rgba[i][gDst] = clamp_float_to_uint(f[1]);
4171                rgba[i][bDst] = clamp_float_to_uint(f[2]);
4172                rgba[i][aDst] = 1;
4173             }
4174          }
4175          else {
4176             const GLuint *uisrc = (const GLuint *) src;
4177             GLuint i;
4178             float f[3];
4179             for (i = 0; i < n; i ++) {
4180                GLuint p = uisrc[i];
4181                rgb9e5_to_float3(p, f);
4182                rgba[i][rDst] = clamp_float_to_uint(f[0]);
4183                rgba[i][gDst] = clamp_float_to_uint(f[1]);
4184                rgba[i][bDst] = clamp_float_to_uint(f[2]);
4185                rgba[i][aDst] = 1;
4186             }
4187          }
4188          break;
4189       case GL_UNSIGNED_INT_10F_11F_11F_REV:
4190          if (swapBytes) {
4191             const GLuint *uisrc = (const GLuint *) src;
4192             GLuint i;
4193             float f[3];
4194             for (i = 0; i < n; i ++) {
4195                GLuint p = uisrc[i];
4196                SWAP4BYTE(p);
4197                r11g11b10f_to_float3(p, f);
4198                rgba[i][rDst] = clamp_float_to_uint(f[0]);
4199                rgba[i][gDst] = clamp_float_to_uint(f[1]);
4200                rgba[i][bDst] = clamp_float_to_uint(f[2]);
4201                rgba[i][aDst] = 1;
4202             }
4203          }
4204          else {
4205             const GLuint *uisrc = (const GLuint *) src;
4206             GLuint i;
4207             float f[3];
4208             for (i = 0; i < n; i ++) {
4209                GLuint p = uisrc[i];
4210                r11g11b10f_to_float3(p, f);
4211                rgba[i][rDst] = clamp_float_to_uint(f[0]);
4212                rgba[i][gDst] = clamp_float_to_uint(f[1]);
4213                rgba[i][bDst] = clamp_float_to_uint(f[2]);
4214                rgba[i][aDst] = 1;
4215             }
4216          }
4217          break;
4218       default:
4219          _mesa_problem(NULL, "bad srcType in extract uint data");
4220          break;
4221    }
4222 #undef PROCESS
4223 }
4224 
4225 
4226 
4227 /*
4228  * Unpack a row of color image data from a client buffer according to
4229  * the pixel unpacking parameters.
4230  * Return GLubyte values in the specified dest image format.
4231  * This is used by glDrawPixels and glTexImage?D().
4232  * \param ctx - the context
4233  *         n - number of pixels in the span
4234  *         dstFormat - format of destination color array
4235  *         dest - the destination color array
4236  *         srcFormat - source image format
4237  *         srcType - source image  data type
4238  *         source - source image pointer
4239  *         srcPacking - pixel unpacking parameters
4240  *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
4241  *
4242  * XXX perhaps expand this to process whole images someday.
4243  */
4244 void
_mesa_unpack_color_span_ubyte(struct gl_context * ctx,GLuint n,GLenum dstFormat,GLubyte dest[],GLenum srcFormat,GLenum srcType,const GLvoid * source,const struct gl_pixelstore_attrib * srcPacking,GLbitfield transferOps)4245 _mesa_unpack_color_span_ubyte(struct gl_context *ctx,
4246                               GLuint n, GLenum dstFormat, GLubyte dest[],
4247                               GLenum srcFormat, GLenum srcType,
4248                               const GLvoid *source,
4249                               const struct gl_pixelstore_attrib *srcPacking,
4250                               GLbitfield transferOps )
4251 {
4252    GLboolean intFormat = _mesa_is_enum_format_integer(srcFormat);
4253    ASSERT(dstFormat == GL_ALPHA ||
4254           dstFormat == GL_LUMINANCE ||
4255           dstFormat == GL_LUMINANCE_ALPHA ||
4256           dstFormat == GL_INTENSITY ||
4257           dstFormat == GL_RED ||
4258           dstFormat == GL_RG ||
4259           dstFormat == GL_RGB ||
4260           dstFormat == GL_RGBA);
4261 
4262    ASSERT(srcFormat == GL_RED ||
4263           srcFormat == GL_GREEN ||
4264           srcFormat == GL_BLUE ||
4265           srcFormat == GL_ALPHA ||
4266           srcFormat == GL_LUMINANCE ||
4267           srcFormat == GL_LUMINANCE_ALPHA ||
4268           srcFormat == GL_INTENSITY ||
4269           srcFormat == GL_RG ||
4270           srcFormat == GL_RGB ||
4271           srcFormat == GL_BGR ||
4272           srcFormat == GL_RGBA ||
4273           srcFormat == GL_BGRA ||
4274           srcFormat == GL_ABGR_EXT ||
4275           srcFormat == GL_COLOR_INDEX);
4276 
4277    ASSERT(srcType == GL_BITMAP ||
4278           srcType == GL_UNSIGNED_BYTE ||
4279           srcType == GL_BYTE ||
4280           srcType == GL_UNSIGNED_SHORT ||
4281           srcType == GL_SHORT ||
4282           srcType == GL_UNSIGNED_INT ||
4283           srcType == GL_INT ||
4284           srcType == GL_HALF_FLOAT_ARB ||
4285           srcType == GL_FLOAT ||
4286           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
4287           srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
4288           srcType == GL_UNSIGNED_SHORT_5_6_5 ||
4289           srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
4290           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
4291           srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
4292           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
4293           srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
4294           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
4295           srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
4296           srcType == GL_UNSIGNED_INT_10_10_10_2 ||
4297           srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
4298           srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
4299           srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
4300 
4301    /* EXT_texture_integer specifies no transfer ops on integer
4302     * types in the resolved issues section. Just set them to 0
4303     * for integer surfaces.
4304     */
4305    if (intFormat)
4306       transferOps = 0;
4307 
4308    /* Try simple cases first */
4309    if (transferOps == 0) {
4310       if (srcType == GL_UNSIGNED_BYTE) {
4311          if (dstFormat == GL_RGBA) {
4312             if (srcFormat == GL_RGBA) {
4313                memcpy( dest, source, n * 4 * sizeof(GLubyte) );
4314                return;
4315             }
4316             else if (srcFormat == GL_RGB) {
4317                GLuint i;
4318                const GLubyte *src = (const GLubyte *) source;
4319                GLubyte *dst = dest;
4320                for (i = 0; i < n; i++) {
4321                   dst[0] = src[0];
4322                   dst[1] = src[1];
4323                   dst[2] = src[2];
4324                   dst[3] = 255;
4325                   src += 3;
4326                   dst += 4;
4327                }
4328                return;
4329             }
4330          }
4331          else if (dstFormat == GL_RGB) {
4332             if (srcFormat == GL_RGB) {
4333                memcpy( dest, source, n * 3 * sizeof(GLubyte) );
4334                return;
4335             }
4336             else if (srcFormat == GL_RGBA) {
4337                GLuint i;
4338                const GLubyte *src = (const GLubyte *) source;
4339                GLubyte *dst = dest;
4340                for (i = 0; i < n; i++) {
4341                   dst[0] = src[0];
4342                   dst[1] = src[1];
4343                   dst[2] = src[2];
4344                   src += 4;
4345                   dst += 3;
4346                }
4347                return;
4348             }
4349          }
4350          else if (dstFormat == srcFormat) {
4351             GLint comps = _mesa_components_in_format(srcFormat);
4352             assert(comps > 0);
4353             memcpy( dest, source, n * comps * sizeof(GLubyte) );
4354             return;
4355          }
4356       }
4357    }
4358 
4359 
4360    /* general solution begins here */
4361    {
4362       GLint dstComponents;
4363       GLint rDst, gDst, bDst, aDst, lDst, iDst;
4364       GLfloat (*rgba)[4] = (GLfloat (*)[4]) malloc(4 * n * sizeof(GLfloat));
4365 
4366       if (!rgba) {
4367          _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4368          return;
4369       }
4370 
4371       dstComponents = _mesa_components_in_format( dstFormat );
4372       /* source & dest image formats should have been error checked by now */
4373       assert(dstComponents > 0);
4374 
4375       /*
4376        * Extract image data and convert to RGBA floats
4377        */
4378       if (srcFormat == GL_COLOR_INDEX) {
4379          GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
4380 
4381          if (!indexes) {
4382             _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4383             free(rgba);
4384             return;
4385          }
4386 
4387          extract_uint_indexes(n, indexes, srcFormat, srcType, source,
4388                               srcPacking);
4389 
4390 	 /* Convert indexes to RGBA */
4391 	 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4392 	    _mesa_shift_and_offset_ci(ctx, n, indexes);
4393 	 }
4394 	 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
4395 
4396          /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
4397           * with color indexes.
4398           */
4399          transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
4400 
4401          free(indexes);
4402       }
4403       else {
4404          /* non-color index data */
4405          extract_float_rgba(n, rgba, srcFormat, srcType, source,
4406                             srcPacking->SwapBytes);
4407       }
4408 
4409       /* Need to clamp if returning GLubytes */
4410       transferOps |= IMAGE_CLAMP_BIT;
4411 
4412       if (transferOps) {
4413          _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
4414       }
4415 
4416       get_component_indexes(dstFormat,
4417                             &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
4418 
4419       /* Now return the GLubyte data in the requested dstFormat */
4420       if (rDst >= 0) {
4421          GLubyte *dst = dest;
4422          GLuint i;
4423          for (i = 0; i < n; i++) {
4424             CLAMPED_FLOAT_TO_UBYTE(dst[rDst], rgba[i][RCOMP]);
4425             dst += dstComponents;
4426          }
4427       }
4428 
4429       if (gDst >= 0) {
4430          GLubyte *dst = dest;
4431          GLuint i;
4432          for (i = 0; i < n; i++) {
4433             CLAMPED_FLOAT_TO_UBYTE(dst[gDst], rgba[i][GCOMP]);
4434             dst += dstComponents;
4435          }
4436       }
4437 
4438       if (bDst >= 0) {
4439          GLubyte *dst = dest;
4440          GLuint i;
4441          for (i = 0; i < n; i++) {
4442             CLAMPED_FLOAT_TO_UBYTE(dst[bDst], rgba[i][BCOMP]);
4443             dst += dstComponents;
4444          }
4445       }
4446 
4447       if (aDst >= 0) {
4448          GLubyte *dst = dest;
4449          GLuint i;
4450          for (i = 0; i < n; i++) {
4451             CLAMPED_FLOAT_TO_UBYTE(dst[aDst], rgba[i][ACOMP]);
4452             dst += dstComponents;
4453          }
4454       }
4455 
4456       if (iDst >= 0) {
4457          GLubyte *dst = dest;
4458          GLuint i;
4459          assert(iDst == 0);
4460          assert(dstComponents == 1);
4461          for (i = 0; i < n; i++) {
4462             /* Intensity comes from red channel */
4463             CLAMPED_FLOAT_TO_UBYTE(dst[i], rgba[i][RCOMP]);
4464          }
4465       }
4466 
4467       if (lDst >= 0) {
4468          GLubyte *dst = dest;
4469          GLuint i;
4470          assert(lDst == 0);
4471          for (i = 0; i < n; i++) {
4472             /* Luminance comes from red channel */
4473             CLAMPED_FLOAT_TO_UBYTE(dst[0], rgba[i][RCOMP]);
4474             dst += dstComponents;
4475          }
4476       }
4477 
4478       free(rgba);
4479    }
4480 }
4481 
4482 
4483 /**
4484  * Same as _mesa_unpack_color_span_ubyte(), but return GLfloat data
4485  * instead of GLubyte.
4486  */
4487 void
_mesa_unpack_color_span_float(struct gl_context * ctx,GLuint n,GLenum dstFormat,GLfloat dest[],GLenum srcFormat,GLenum srcType,const GLvoid * source,const struct gl_pixelstore_attrib * srcPacking,GLbitfield transferOps)4488 _mesa_unpack_color_span_float( struct gl_context *ctx,
4489                                GLuint n, GLenum dstFormat, GLfloat dest[],
4490                                GLenum srcFormat, GLenum srcType,
4491                                const GLvoid *source,
4492                                const struct gl_pixelstore_attrib *srcPacking,
4493                                GLbitfield transferOps )
4494 {
4495    ASSERT(dstFormat == GL_ALPHA ||
4496           dstFormat == GL_LUMINANCE ||
4497           dstFormat == GL_LUMINANCE_ALPHA ||
4498           dstFormat == GL_INTENSITY ||
4499           dstFormat == GL_RED ||
4500           dstFormat == GL_RG ||
4501           dstFormat == GL_RGB ||
4502           dstFormat == GL_RGBA);
4503 
4504    ASSERT(srcFormat == GL_RED ||
4505           srcFormat == GL_GREEN ||
4506           srcFormat == GL_BLUE ||
4507           srcFormat == GL_ALPHA ||
4508           srcFormat == GL_LUMINANCE ||
4509           srcFormat == GL_LUMINANCE_ALPHA ||
4510           srcFormat == GL_INTENSITY ||
4511           srcFormat == GL_RG ||
4512           srcFormat == GL_RGB ||
4513           srcFormat == GL_BGR ||
4514           srcFormat == GL_RGBA ||
4515           srcFormat == GL_BGRA ||
4516           srcFormat == GL_ABGR_EXT ||
4517           srcFormat == GL_RED_INTEGER_EXT ||
4518           srcFormat == GL_GREEN_INTEGER_EXT ||
4519           srcFormat == GL_BLUE_INTEGER_EXT ||
4520           srcFormat == GL_ALPHA_INTEGER_EXT ||
4521           srcFormat == GL_RG_INTEGER ||
4522           srcFormat == GL_RGB_INTEGER_EXT ||
4523           srcFormat == GL_RGBA_INTEGER_EXT ||
4524           srcFormat == GL_BGR_INTEGER_EXT ||
4525           srcFormat == GL_BGRA_INTEGER_EXT ||
4526           srcFormat == GL_LUMINANCE_INTEGER_EXT ||
4527           srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT ||
4528           srcFormat == GL_COLOR_INDEX);
4529 
4530    ASSERT(srcType == GL_BITMAP ||
4531           srcType == GL_UNSIGNED_BYTE ||
4532           srcType == GL_BYTE ||
4533           srcType == GL_UNSIGNED_SHORT ||
4534           srcType == GL_SHORT ||
4535           srcType == GL_UNSIGNED_INT ||
4536           srcType == GL_INT ||
4537           srcType == GL_HALF_FLOAT_ARB ||
4538           srcType == GL_FLOAT ||
4539           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
4540           srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
4541           srcType == GL_UNSIGNED_SHORT_5_6_5 ||
4542           srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
4543           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
4544           srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
4545           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
4546           srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
4547           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
4548           srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
4549           srcType == GL_UNSIGNED_INT_10_10_10_2 ||
4550           srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
4551           srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
4552           srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
4553 
4554    /* general solution, no special cases, yet */
4555    {
4556       GLint dstComponents;
4557       GLint rDst, gDst, bDst, aDst, lDst, iDst;
4558       GLfloat (*rgba)[4] = (GLfloat (*)[4]) malloc(4 * n * sizeof(GLfloat));
4559       GLboolean intFormat = _mesa_is_enum_format_integer(srcFormat);
4560 
4561       if (!rgba) {
4562          _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4563          return;
4564       }
4565 
4566       dstComponents = _mesa_components_in_format( dstFormat );
4567       /* source & dest image formats should have been error checked by now */
4568       assert(dstComponents > 0);
4569 
4570       /* EXT_texture_integer specifies no transfer ops on integer
4571        * types in the resolved issues section. Just set them to 0
4572        * for integer surfaces.
4573        */
4574       if (intFormat)
4575          transferOps = 0;
4576 
4577       /*
4578        * Extract image data and convert to RGBA floats
4579        */
4580       if (srcFormat == GL_COLOR_INDEX) {
4581          GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
4582 
4583          if (!indexes) {
4584             _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4585             free(rgba);
4586             return;
4587          }
4588 
4589          extract_uint_indexes(n, indexes, srcFormat, srcType, source,
4590                               srcPacking);
4591 
4592 	 /* Convert indexes to RGBA */
4593 	 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4594 	    _mesa_shift_and_offset_ci(ctx, n, indexes);
4595 	 }
4596 	 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
4597 
4598          /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
4599           * with color indexes.
4600           */
4601          transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
4602 
4603          free(indexes);
4604       }
4605       else {
4606          /* non-color index data */
4607          extract_float_rgba(n, rgba, srcFormat, srcType, source,
4608                             srcPacking->SwapBytes);
4609       }
4610 
4611       if (transferOps) {
4612          _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
4613       }
4614 
4615       get_component_indexes(dstFormat,
4616                             &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
4617 
4618       /* Now pack results in the requested dstFormat */
4619       if (rDst >= 0) {
4620          GLfloat *dst = dest;
4621          GLuint i;
4622          for (i = 0; i < n; i++) {
4623             dst[rDst] = rgba[i][RCOMP];
4624             dst += dstComponents;
4625          }
4626       }
4627 
4628       if (gDst >= 0) {
4629          GLfloat *dst = dest;
4630          GLuint i;
4631          for (i = 0; i < n; i++) {
4632             dst[gDst] = rgba[i][GCOMP];
4633             dst += dstComponents;
4634          }
4635       }
4636 
4637       if (bDst >= 0) {
4638          GLfloat *dst = dest;
4639          GLuint i;
4640          for (i = 0; i < n; i++) {
4641             dst[bDst] = rgba[i][BCOMP];
4642             dst += dstComponents;
4643          }
4644       }
4645 
4646       if (aDst >= 0) {
4647          GLfloat *dst = dest;
4648          GLuint i;
4649          for (i = 0; i < n; i++) {
4650             dst[aDst] = rgba[i][ACOMP];
4651             dst += dstComponents;
4652          }
4653       }
4654 
4655       if (iDst >= 0) {
4656          GLfloat *dst = dest;
4657          GLuint i;
4658          assert(iDst == 0);
4659          assert(dstComponents == 1);
4660          for (i = 0; i < n; i++) {
4661             /* Intensity comes from red channel */
4662             dst[i] = rgba[i][RCOMP];
4663          }
4664       }
4665 
4666       if (lDst >= 0) {
4667          GLfloat *dst = dest;
4668          GLuint i;
4669          assert(lDst == 0);
4670          for (i = 0; i < n; i++) {
4671             /* Luminance comes from red channel */
4672             dst[0] = rgba[i][RCOMP];
4673             dst += dstComponents;
4674          }
4675       }
4676 
4677       free(rgba);
4678    }
4679 }
4680 
4681 
4682 /**
4683  * Same as _mesa_unpack_color_span_ubyte(), but return GLuint data
4684  * instead of GLubyte.
4685  * No pixel transfer ops are applied.
4686  */
4687 void
_mesa_unpack_color_span_uint(struct gl_context * ctx,GLuint n,GLenum dstFormat,GLuint * dest,GLenum srcFormat,GLenum srcType,const GLvoid * source,const struct gl_pixelstore_attrib * srcPacking)4688 _mesa_unpack_color_span_uint(struct gl_context *ctx,
4689                              GLuint n, GLenum dstFormat, GLuint *dest,
4690                              GLenum srcFormat, GLenum srcType,
4691                              const GLvoid *source,
4692                              const struct gl_pixelstore_attrib *srcPacking)
4693 {
4694    GLuint (*rgba)[4] = (GLuint (*)[4]) malloc(n * 4 * sizeof(GLfloat));
4695 
4696    if (!rgba) {
4697       _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4698       return;
4699    }
4700 
4701    ASSERT(dstFormat == GL_ALPHA ||
4702           dstFormat == GL_LUMINANCE ||
4703           dstFormat == GL_LUMINANCE_ALPHA ||
4704           dstFormat == GL_INTENSITY ||
4705           dstFormat == GL_RED ||
4706           dstFormat == GL_RG ||
4707           dstFormat == GL_RGB ||
4708           dstFormat == GL_RGBA);
4709 
4710    ASSERT(srcFormat == GL_RED ||
4711           srcFormat == GL_GREEN ||
4712           srcFormat == GL_BLUE ||
4713           srcFormat == GL_ALPHA ||
4714           srcFormat == GL_LUMINANCE ||
4715           srcFormat == GL_LUMINANCE_ALPHA ||
4716           srcFormat == GL_INTENSITY ||
4717           srcFormat == GL_RG ||
4718           srcFormat == GL_RGB ||
4719           srcFormat == GL_BGR ||
4720           srcFormat == GL_RGBA ||
4721           srcFormat == GL_BGRA ||
4722           srcFormat == GL_ABGR_EXT ||
4723           srcFormat == GL_RED_INTEGER_EXT ||
4724           srcFormat == GL_GREEN_INTEGER_EXT ||
4725           srcFormat == GL_BLUE_INTEGER_EXT ||
4726           srcFormat == GL_ALPHA_INTEGER_EXT ||
4727           srcFormat == GL_RG_INTEGER ||
4728           srcFormat == GL_RGB_INTEGER_EXT ||
4729           srcFormat == GL_RGBA_INTEGER_EXT ||
4730           srcFormat == GL_BGR_INTEGER_EXT ||
4731           srcFormat == GL_BGRA_INTEGER_EXT ||
4732           srcFormat == GL_LUMINANCE_INTEGER_EXT ||
4733           srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
4734 
4735    ASSERT(srcType == GL_UNSIGNED_BYTE ||
4736           srcType == GL_BYTE ||
4737           srcType == GL_UNSIGNED_SHORT ||
4738           srcType == GL_SHORT ||
4739           srcType == GL_UNSIGNED_INT ||
4740           srcType == GL_INT ||
4741           srcType == GL_HALF_FLOAT_ARB ||
4742           srcType == GL_FLOAT ||
4743           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
4744           srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
4745           srcType == GL_UNSIGNED_SHORT_5_6_5 ||
4746           srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
4747           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
4748           srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
4749           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
4750           srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
4751           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
4752           srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
4753           srcType == GL_UNSIGNED_INT_10_10_10_2 ||
4754           srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
4755           srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
4756           srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
4757 
4758 
4759    /* Extract image data as uint[4] pixels */
4760    extract_uint_rgba(n, rgba, srcFormat, srcType, source,
4761                      srcPacking->SwapBytes);
4762 
4763    if (dstFormat == GL_RGBA) {
4764       /* simple case */
4765       memcpy(dest, rgba, 4 * sizeof(GLuint) * n);
4766    }
4767    else {
4768       /* general case */
4769       GLint rDst, gDst, bDst, aDst, lDst, iDst;
4770       GLint dstComponents = _mesa_components_in_format( dstFormat );
4771 
4772       assert(dstComponents > 0);
4773 
4774       get_component_indexes(dstFormat,
4775                             &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
4776 
4777       /* Now pack values in the requested dest format */
4778       if (rDst >= 0) {
4779          GLuint *dst = dest;
4780          GLuint i;
4781          for (i = 0; i < n; i++) {
4782             dst[rDst] = rgba[i][RCOMP];
4783             dst += dstComponents;
4784          }
4785       }
4786 
4787       if (gDst >= 0) {
4788          GLuint *dst = dest;
4789          GLuint i;
4790          for (i = 0; i < n; i++) {
4791             dst[gDst] = rgba[i][GCOMP];
4792             dst += dstComponents;
4793          }
4794       }
4795 
4796       if (bDst >= 0) {
4797          GLuint *dst = dest;
4798          GLuint i;
4799          for (i = 0; i < n; i++) {
4800             dst[bDst] = rgba[i][BCOMP];
4801             dst += dstComponents;
4802          }
4803       }
4804 
4805       if (aDst >= 0) {
4806          GLuint *dst = dest;
4807          GLuint i;
4808          for (i = 0; i < n; i++) {
4809             dst[aDst] = rgba[i][ACOMP];
4810             dst += dstComponents;
4811          }
4812       }
4813 
4814       if (iDst >= 0) {
4815          GLuint *dst = dest;
4816          GLuint i;
4817          assert(iDst == 0);
4818          assert(dstComponents == 1);
4819          for (i = 0; i < n; i++) {
4820             /* Intensity comes from red channel */
4821             dst[i] = rgba[i][RCOMP];
4822          }
4823       }
4824 
4825       if (lDst >= 0) {
4826          GLuint *dst = dest;
4827          GLuint i;
4828          assert(lDst == 0);
4829          for (i = 0; i < n; i++) {
4830             /* Luminance comes from red channel */
4831             dst[0] = rgba[i][RCOMP];
4832             dst += dstComponents;
4833          }
4834       }
4835    }
4836 
4837    free(rgba);
4838 }
4839 
4840 
4841 
4842 /**
4843  * Similar to _mesa_unpack_color_span_float(), but for dudv data instead of rgba,
4844  * directly return GLbyte data, no transfer ops apply.
4845  */
4846 void
_mesa_unpack_dudv_span_byte(struct gl_context * ctx,GLuint n,GLenum dstFormat,GLbyte dest[],GLenum srcFormat,GLenum srcType,const GLvoid * source,const struct gl_pixelstore_attrib * srcPacking,GLbitfield transferOps)4847 _mesa_unpack_dudv_span_byte( struct gl_context *ctx,
4848                              GLuint n, GLenum dstFormat, GLbyte dest[],
4849                              GLenum srcFormat, GLenum srcType,
4850                              const GLvoid *source,
4851                              const struct gl_pixelstore_attrib *srcPacking,
4852                              GLbitfield transferOps )
4853 {
4854    ASSERT(dstFormat == GL_DUDV_ATI);
4855    ASSERT(srcFormat == GL_DUDV_ATI ||
4856 	  srcFormat == GL_DU8DV8_ATI);
4857 
4858    ASSERT(srcType == GL_UNSIGNED_BYTE ||
4859           srcType == GL_BYTE ||
4860           srcType == GL_UNSIGNED_SHORT ||
4861           srcType == GL_SHORT ||
4862           srcType == GL_UNSIGNED_INT ||
4863           srcType == GL_INT ||
4864           srcType == GL_HALF_FLOAT_ARB ||
4865           srcType == GL_FLOAT);
4866 
4867    /* general solution */
4868    {
4869       GLint dstComponents;
4870       GLbyte *dst = dest;
4871       GLuint i;
4872       GLfloat (*rgba)[4] = (GLfloat (*)[4]) malloc(4 * n * sizeof(GLfloat));
4873 
4874       if (!rgba) {
4875          _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4876          return;
4877       }
4878 
4879       dstComponents = _mesa_components_in_format( dstFormat );
4880       /* source & dest image formats should have been error checked by now */
4881       assert(dstComponents > 0);
4882 
4883       /*
4884        * Extract image data and convert to RGBA floats
4885        */
4886       extract_float_rgba(n, rgba, srcFormat, srcType, source,
4887                          srcPacking->SwapBytes);
4888 
4889 
4890       /* Now determine which color channels we need to produce.
4891        * And determine the dest index (offset) within each color tuple.
4892        */
4893 
4894       /* Now pack results in the requested dstFormat */
4895       for (i = 0; i < n; i++) {
4896          /* not sure - need clamp[-1,1] here? */
4897          dst[0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
4898          dst[1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
4899          dst += dstComponents;
4900       }
4901 
4902       free(rgba);
4903    }
4904 }
4905 
4906 /*
4907  * Unpack a row of color index data from a client buffer according to
4908  * the pixel unpacking parameters.
4909  * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
4910  *
4911  * Args:  ctx - the context
4912  *        n - number of pixels
4913  *        dstType - destination data type
4914  *        dest - destination array
4915  *        srcType - source pixel type
4916  *        source - source data pointer
4917  *        srcPacking - pixel unpacking parameters
4918  *        transferOps - the pixel transfer operations to apply
4919  */
4920 void
_mesa_unpack_index_span(struct gl_context * ctx,GLuint n,GLenum dstType,GLvoid * dest,GLenum srcType,const GLvoid * source,const struct gl_pixelstore_attrib * srcPacking,GLbitfield transferOps)4921 _mesa_unpack_index_span( struct gl_context *ctx, GLuint n,
4922                          GLenum dstType, GLvoid *dest,
4923                          GLenum srcType, const GLvoid *source,
4924                          const struct gl_pixelstore_attrib *srcPacking,
4925                          GLbitfield transferOps )
4926 {
4927    ASSERT(srcType == GL_BITMAP ||
4928           srcType == GL_UNSIGNED_BYTE ||
4929           srcType == GL_BYTE ||
4930           srcType == GL_UNSIGNED_SHORT ||
4931           srcType == GL_SHORT ||
4932           srcType == GL_UNSIGNED_INT ||
4933           srcType == GL_INT ||
4934           srcType == GL_HALF_FLOAT_ARB ||
4935           srcType == GL_FLOAT);
4936 
4937    ASSERT(dstType == GL_UNSIGNED_BYTE ||
4938           dstType == GL_UNSIGNED_SHORT ||
4939           dstType == GL_UNSIGNED_INT);
4940 
4941 
4942    transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4943 
4944    /*
4945     * Try simple cases first
4946     */
4947    if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
4948        && dstType == GL_UNSIGNED_BYTE) {
4949       memcpy(dest, source, n * sizeof(GLubyte));
4950    }
4951    else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
4952             && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
4953       memcpy(dest, source, n * sizeof(GLuint));
4954    }
4955    else {
4956       /*
4957        * general solution
4958        */
4959       GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
4960 
4961       if (!indexes) {
4962          _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4963          return;
4964       }
4965 
4966       extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
4967                            srcPacking);
4968 
4969       if (transferOps)
4970          _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4971 
4972       /* convert to dest type */
4973       switch (dstType) {
4974          case GL_UNSIGNED_BYTE:
4975             {
4976                GLubyte *dst = (GLubyte *) dest;
4977                GLuint i;
4978                for (i = 0; i < n; i++) {
4979                   dst[i] = (GLubyte) (indexes[i] & 0xff);
4980                }
4981             }
4982             break;
4983          case GL_UNSIGNED_SHORT:
4984             {
4985                GLuint *dst = (GLuint *) dest;
4986                GLuint i;
4987                for (i = 0; i < n; i++) {
4988                   dst[i] = (GLushort) (indexes[i] & 0xffff);
4989                }
4990             }
4991             break;
4992          case GL_UNSIGNED_INT:
4993             memcpy(dest, indexes, n * sizeof(GLuint));
4994             break;
4995          default:
4996             _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
4997       }
4998 
4999       free(indexes);
5000    }
5001 }
5002 
5003 
5004 void
_mesa_pack_index_span(struct gl_context * ctx,GLuint n,GLenum dstType,GLvoid * dest,const GLuint * source,const struct gl_pixelstore_attrib * dstPacking,GLbitfield transferOps)5005 _mesa_pack_index_span( struct gl_context *ctx, GLuint n,
5006                        GLenum dstType, GLvoid *dest, const GLuint *source,
5007                        const struct gl_pixelstore_attrib *dstPacking,
5008                        GLbitfield transferOps )
5009 {
5010    GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
5011 
5012    if (!indexes) {
5013       _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
5014       return;
5015    }
5016 
5017    transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
5018 
5019    if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
5020       /* make a copy of input */
5021       memcpy(indexes, source, n * sizeof(GLuint));
5022       _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
5023       source = indexes;
5024    }
5025 
5026    switch (dstType) {
5027    case GL_UNSIGNED_BYTE:
5028       {
5029          GLubyte *dst = (GLubyte *) dest;
5030          GLuint i;
5031          for (i = 0; i < n; i++) {
5032             *dst++ = (GLubyte) source[i];
5033          }
5034       }
5035       break;
5036    case GL_BYTE:
5037       {
5038          GLbyte *dst = (GLbyte *) dest;
5039          GLuint i;
5040          for (i = 0; i < n; i++) {
5041             dst[i] = (GLbyte) source[i];
5042          }
5043       }
5044       break;
5045    case GL_UNSIGNED_SHORT:
5046       {
5047          GLushort *dst = (GLushort *) dest;
5048          GLuint i;
5049          for (i = 0; i < n; i++) {
5050             dst[i] = (GLushort) source[i];
5051          }
5052          if (dstPacking->SwapBytes) {
5053             _mesa_swap2( (GLushort *) dst, n );
5054          }
5055       }
5056       break;
5057    case GL_SHORT:
5058       {
5059          GLshort *dst = (GLshort *) dest;
5060          GLuint i;
5061          for (i = 0; i < n; i++) {
5062             dst[i] = (GLshort) source[i];
5063          }
5064          if (dstPacking->SwapBytes) {
5065             _mesa_swap2( (GLushort *) dst, n );
5066          }
5067       }
5068       break;
5069    case GL_UNSIGNED_INT:
5070       {
5071          GLuint *dst = (GLuint *) dest;
5072          GLuint i;
5073          for (i = 0; i < n; i++) {
5074             dst[i] = (GLuint) source[i];
5075          }
5076          if (dstPacking->SwapBytes) {
5077             _mesa_swap4( (GLuint *) dst, n );
5078          }
5079       }
5080       break;
5081    case GL_INT:
5082       {
5083          GLint *dst = (GLint *) dest;
5084          GLuint i;
5085          for (i = 0; i < n; i++) {
5086             dst[i] = (GLint) source[i];
5087          }
5088          if (dstPacking->SwapBytes) {
5089             _mesa_swap4( (GLuint *) dst, n );
5090          }
5091       }
5092       break;
5093    case GL_FLOAT:
5094       {
5095          GLfloat *dst = (GLfloat *) dest;
5096          GLuint i;
5097          for (i = 0; i < n; i++) {
5098             dst[i] = (GLfloat) source[i];
5099          }
5100          if (dstPacking->SwapBytes) {
5101             _mesa_swap4( (GLuint *) dst, n );
5102          }
5103       }
5104       break;
5105    case GL_HALF_FLOAT_ARB:
5106       {
5107          GLhalfARB *dst = (GLhalfARB *) dest;
5108          GLuint i;
5109          for (i = 0; i < n; i++) {
5110             dst[i] = _mesa_float_to_half((GLfloat) source[i]);
5111          }
5112          if (dstPacking->SwapBytes) {
5113             _mesa_swap2( (GLushort *) dst, n );
5114          }
5115       }
5116       break;
5117    default:
5118       _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
5119    }
5120 
5121    free(indexes);
5122 }
5123 
5124 
5125 /*
5126  * Unpack a row of stencil data from a client buffer according to
5127  * the pixel unpacking parameters.
5128  * This is (or will be) used by glDrawPixels
5129  *
5130  * Args:  ctx - the context
5131  *        n - number of pixels
5132  *        dstType - destination data type
5133  *        dest - destination array
5134  *        srcType - source pixel type
5135  *        source - source data pointer
5136  *        srcPacking - pixel unpacking parameters
5137  *        transferOps - apply offset/bias/lookup ops?
5138  */
5139 void
_mesa_unpack_stencil_span(struct gl_context * ctx,GLuint n,GLenum dstType,GLvoid * dest,GLenum srcType,const GLvoid * source,const struct gl_pixelstore_attrib * srcPacking,GLbitfield transferOps)5140 _mesa_unpack_stencil_span( struct gl_context *ctx, GLuint n,
5141                            GLenum dstType, GLvoid *dest,
5142                            GLenum srcType, const GLvoid *source,
5143                            const struct gl_pixelstore_attrib *srcPacking,
5144                            GLbitfield transferOps )
5145 {
5146    ASSERT(srcType == GL_BITMAP ||
5147           srcType == GL_UNSIGNED_BYTE ||
5148           srcType == GL_BYTE ||
5149           srcType == GL_UNSIGNED_SHORT ||
5150           srcType == GL_SHORT ||
5151           srcType == GL_UNSIGNED_INT ||
5152           srcType == GL_INT ||
5153           srcType == GL_UNSIGNED_INT_24_8_EXT ||
5154           srcType == GL_HALF_FLOAT_ARB ||
5155           srcType == GL_FLOAT ||
5156           srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
5157 
5158    ASSERT(dstType == GL_UNSIGNED_BYTE ||
5159           dstType == GL_UNSIGNED_SHORT ||
5160           dstType == GL_UNSIGNED_INT ||
5161           dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
5162 
5163    /* only shift and offset apply to stencil */
5164    transferOps &= IMAGE_SHIFT_OFFSET_BIT;
5165 
5166    /*
5167     * Try simple cases first
5168     */
5169    if (transferOps == 0 &&
5170        !ctx->Pixel.MapStencilFlag &&
5171        srcType == GL_UNSIGNED_BYTE &&
5172        dstType == GL_UNSIGNED_BYTE) {
5173       memcpy(dest, source, n * sizeof(GLubyte));
5174    }
5175    else if (transferOps == 0 &&
5176             !ctx->Pixel.MapStencilFlag &&
5177             srcType == GL_UNSIGNED_INT &&
5178             dstType == GL_UNSIGNED_INT &&
5179             !srcPacking->SwapBytes) {
5180       memcpy(dest, source, n * sizeof(GLuint));
5181    }
5182    else {
5183       /*
5184        * general solution
5185        */
5186       GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
5187 
5188       if (!indexes) {
5189          _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil unpacking");
5190          return;
5191       }
5192 
5193       extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
5194                            srcPacking);
5195 
5196       if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
5197          /* shift and offset indexes */
5198          _mesa_shift_and_offset_ci(ctx, n, indexes);
5199       }
5200 
5201       if (ctx->Pixel.MapStencilFlag) {
5202          /* Apply stencil lookup table */
5203          const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
5204          GLuint i;
5205          for (i = 0; i < n; i++) {
5206             indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
5207          }
5208       }
5209 
5210       /* convert to dest type */
5211       switch (dstType) {
5212          case GL_UNSIGNED_BYTE:
5213             {
5214                GLubyte *dst = (GLubyte *) dest;
5215                GLuint i;
5216                for (i = 0; i < n; i++) {
5217                   dst[i] = (GLubyte) (indexes[i] & 0xff);
5218                }
5219             }
5220             break;
5221          case GL_UNSIGNED_SHORT:
5222             {
5223                GLuint *dst = (GLuint *) dest;
5224                GLuint i;
5225                for (i = 0; i < n; i++) {
5226                   dst[i] = (GLushort) (indexes[i] & 0xffff);
5227                }
5228             }
5229             break;
5230          case GL_UNSIGNED_INT:
5231             memcpy(dest, indexes, n * sizeof(GLuint));
5232             break;
5233          case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
5234             {
5235                GLuint *dst = (GLuint *) dest;
5236                GLuint i;
5237                for (i = 0; i < n; i++) {
5238                   dst[i*2+1] = indexes[i] & 0xff; /* lower 8 bits */
5239                }
5240             }
5241             break;
5242          default:
5243             _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
5244       }
5245 
5246       free(indexes);
5247    }
5248 }
5249 
5250 
5251 void
_mesa_pack_stencil_span(struct gl_context * ctx,GLuint n,GLenum dstType,GLvoid * dest,const GLubyte * source,const struct gl_pixelstore_attrib * dstPacking)5252 _mesa_pack_stencil_span( struct gl_context *ctx, GLuint n,
5253                          GLenum dstType, GLvoid *dest, const GLubyte *source,
5254                          const struct gl_pixelstore_attrib *dstPacking )
5255 {
5256    GLubyte *stencil = (GLubyte *) malloc(n * sizeof(GLubyte));
5257 
5258    if (!stencil) {
5259       _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil packing");
5260       return;
5261    }
5262 
5263    if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
5264        ctx->Pixel.MapStencilFlag) {
5265       /* make a copy of input */
5266       memcpy(stencil, source, n * sizeof(GLubyte));
5267       _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
5268       source = stencil;
5269    }
5270 
5271    switch (dstType) {
5272    case GL_UNSIGNED_BYTE:
5273       memcpy(dest, source, n);
5274       break;
5275    case GL_BYTE:
5276       {
5277          GLbyte *dst = (GLbyte *) dest;
5278          GLuint i;
5279          for (i=0;i<n;i++) {
5280             dst[i] = (GLbyte) (source[i] & 0x7f);
5281          }
5282       }
5283       break;
5284    case GL_UNSIGNED_SHORT:
5285       {
5286          GLushort *dst = (GLushort *) dest;
5287          GLuint i;
5288          for (i=0;i<n;i++) {
5289             dst[i] = (GLushort) source[i];
5290          }
5291          if (dstPacking->SwapBytes) {
5292             _mesa_swap2( (GLushort *) dst, n );
5293          }
5294       }
5295       break;
5296    case GL_SHORT:
5297       {
5298          GLshort *dst = (GLshort *) dest;
5299          GLuint i;
5300          for (i=0;i<n;i++) {
5301             dst[i] = (GLshort) source[i];
5302          }
5303          if (dstPacking->SwapBytes) {
5304             _mesa_swap2( (GLushort *) dst, n );
5305          }
5306       }
5307       break;
5308    case GL_UNSIGNED_INT:
5309       {
5310          GLuint *dst = (GLuint *) dest;
5311          GLuint i;
5312          for (i=0;i<n;i++) {
5313             dst[i] = (GLuint) source[i];
5314          }
5315          if (dstPacking->SwapBytes) {
5316             _mesa_swap4( (GLuint *) dst, n );
5317          }
5318       }
5319       break;
5320    case GL_INT:
5321       {
5322          GLint *dst = (GLint *) dest;
5323          GLuint i;
5324          for (i=0;i<n;i++) {
5325             dst[i] = (GLint) source[i];
5326          }
5327          if (dstPacking->SwapBytes) {
5328             _mesa_swap4( (GLuint *) dst, n );
5329          }
5330       }
5331       break;
5332    case GL_FLOAT:
5333       {
5334          GLfloat *dst = (GLfloat *) dest;
5335          GLuint i;
5336          for (i=0;i<n;i++) {
5337             dst[i] = (GLfloat) source[i];
5338          }
5339          if (dstPacking->SwapBytes) {
5340             _mesa_swap4( (GLuint *) dst, n );
5341          }
5342       }
5343       break;
5344    case GL_HALF_FLOAT_ARB:
5345       {
5346          GLhalfARB *dst = (GLhalfARB *) dest;
5347          GLuint i;
5348          for (i=0;i<n;i++) {
5349             dst[i] = _mesa_float_to_half( (float) source[i] );
5350          }
5351          if (dstPacking->SwapBytes) {
5352             _mesa_swap2( (GLushort *) dst, n );
5353          }
5354       }
5355       break;
5356    case GL_BITMAP:
5357       if (dstPacking->LsbFirst) {
5358          GLubyte *dst = (GLubyte *) dest;
5359          GLint shift = 0;
5360          GLuint i;
5361          for (i = 0; i < n; i++) {
5362             if (shift == 0)
5363                *dst = 0;
5364             *dst |= ((source[i] != 0) << shift);
5365             shift++;
5366             if (shift == 8) {
5367                shift = 0;
5368                dst++;
5369             }
5370          }
5371       }
5372       else {
5373          GLubyte *dst = (GLubyte *) dest;
5374          GLint shift = 7;
5375          GLuint i;
5376          for (i = 0; i < n; i++) {
5377             if (shift == 7)
5378                *dst = 0;
5379             *dst |= ((source[i] != 0) << shift);
5380             shift--;
5381             if (shift < 0) {
5382                shift = 7;
5383                dst++;
5384             }
5385          }
5386       }
5387       break;
5388    default:
5389       _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
5390    }
5391 
5392    free(stencil);
5393 }
5394 
5395 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT)                              \
5396     do {                                                                \
5397         GLuint i;                                                       \
5398         const GLTYPE *src = (const GLTYPE *)source;                     \
5399         for (i = 0; i < n; i++) {                                       \
5400             GLTYPE value = src[i];                                      \
5401             if (srcPacking->SwapBytes) {                                \
5402                 if (sizeof(GLTYPE) == 2) {                              \
5403                     SWAP2BYTE(value);                                   \
5404                 } else if (sizeof(GLTYPE) == 4) {                       \
5405                     SWAP4BYTE(value);                                   \
5406                 }                                                       \
5407             }                                                           \
5408             depthValues[i] = GLTYPE2FLOAT(value);                       \
5409         }                                                               \
5410     } while (0)
5411 
5412 
5413 /**
5414  * Unpack a row of depth/z values from memory, returning GLushort, GLuint
5415  * or GLfloat values.
5416  * The glPixelTransfer (scale/bias) params will be applied.
5417  *
5418  * \param dstType  one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
5419  * \param depthMax  max value for returned GLushort or GLuint values
5420  *                  (ignored for GLfloat).
5421  */
5422 void
_mesa_unpack_depth_span(struct gl_context * ctx,GLuint n,GLenum dstType,GLvoid * dest,GLuint depthMax,GLenum srcType,const GLvoid * source,const struct gl_pixelstore_attrib * srcPacking)5423 _mesa_unpack_depth_span( struct gl_context *ctx, GLuint n,
5424                          GLenum dstType, GLvoid *dest, GLuint depthMax,
5425                          GLenum srcType, const GLvoid *source,
5426                          const struct gl_pixelstore_attrib *srcPacking )
5427 {
5428    GLfloat *depthTemp = NULL, *depthValues;
5429    GLboolean needClamp = GL_FALSE;
5430 
5431    /* Look for special cases first.
5432     * Not only are these faster, they're less prone to numeric conversion
5433     * problems.  Otherwise, converting from an int type to a float then
5434     * back to an int type can introduce errors that will show up as
5435     * artifacts in things like depth peeling which uses glCopyTexImage.
5436     */
5437    if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
5438       if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
5439          const GLuint *src = (const GLuint *) source;
5440          GLushort *dst = (GLushort *) dest;
5441          GLuint i;
5442          for (i = 0; i < n; i++) {
5443             dst[i] = src[i] >> 16;
5444          }
5445          return;
5446       }
5447       if (srcType == GL_UNSIGNED_SHORT
5448           && dstType == GL_UNSIGNED_INT
5449           && depthMax == 0xffffffff) {
5450          const GLushort *src = (const GLushort *) source;
5451          GLuint *dst = (GLuint *) dest;
5452          GLuint i;
5453          for (i = 0; i < n; i++) {
5454             dst[i] = src[i] | (src[i] << 16);
5455          }
5456          return;
5457       }
5458       if (srcType == GL_UNSIGNED_INT_24_8
5459           && dstType == GL_UNSIGNED_INT
5460           && depthMax == 0xffffff) {
5461          const GLuint *src = (const GLuint *) source;
5462          GLuint *dst = (GLuint *) dest;
5463          GLuint i;
5464          for (i = 0; i < n; i++) {
5465             dst[i] = src[i] >> 8;
5466          }
5467          return;
5468       }
5469       /* XXX may want to add additional cases here someday */
5470    }
5471 
5472    /* general case path follows */
5473 
5474    if (dstType == GL_FLOAT) {
5475       depthValues = (GLfloat *) dest;
5476    }
5477    else {
5478       depthTemp = (GLfloat *) malloc(n * sizeof(GLfloat));
5479       if (!depthTemp) {
5480          _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
5481          return;
5482       }
5483 
5484       depthValues = depthTemp;
5485    }
5486 
5487    /* Convert incoming values to GLfloat.  Some conversions will require
5488     * clamping, below.
5489     */
5490    switch (srcType) {
5491       case GL_BYTE:
5492          DEPTH_VALUES(GLbyte, BYTE_TO_FLOATZ);
5493          needClamp = GL_TRUE;
5494          break;
5495       case GL_UNSIGNED_BYTE:
5496          DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
5497          break;
5498       case GL_SHORT:
5499          DEPTH_VALUES(GLshort, SHORT_TO_FLOATZ);
5500          needClamp = GL_TRUE;
5501          break;
5502       case GL_UNSIGNED_SHORT:
5503          DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
5504          break;
5505       case GL_INT:
5506          DEPTH_VALUES(GLint, INT_TO_FLOAT);
5507          needClamp = GL_TRUE;
5508          break;
5509       case GL_UNSIGNED_INT:
5510          DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
5511          break;
5512       case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
5513          if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
5514              depthMax == 0xffffff &&
5515              ctx->Pixel.DepthScale == 1.0 &&
5516              ctx->Pixel.DepthBias == 0.0) {
5517             const GLuint *src = (const GLuint *) source;
5518             GLuint *zValues = (GLuint *) dest;
5519             GLuint i;
5520             for (i = 0; i < n; i++) {
5521                 GLuint value = src[i];
5522                 if (srcPacking->SwapBytes) {
5523                     SWAP4BYTE(value);
5524                 }
5525                 zValues[i] = value & 0xffffff00;
5526             }
5527             free(depthTemp);
5528             return;
5529          }
5530          else {
5531             const GLuint *src = (const GLuint *) source;
5532             const GLfloat scale = 1.0f / 0xffffff;
5533             GLuint i;
5534             for (i = 0; i < n; i++) {
5535                 GLuint value = src[i];
5536                 if (srcPacking->SwapBytes) {
5537                     SWAP4BYTE(value);
5538                 }
5539                 depthValues[i] = (value >> 8) * scale;
5540             }
5541          }
5542          break;
5543       case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
5544          {
5545             GLuint i;
5546             const GLfloat *src = (const GLfloat *)source;
5547             for (i = 0; i < n; i++) {
5548                GLfloat value = src[i * 2];
5549                if (srcPacking->SwapBytes) {
5550                   SWAP4BYTE(value);
5551                }
5552                depthValues[i] = value;
5553             }
5554             needClamp = GL_TRUE;
5555          }
5556          break;
5557       case GL_FLOAT:
5558          DEPTH_VALUES(GLfloat, 1*);
5559          needClamp = GL_TRUE;
5560          break;
5561       case GL_HALF_FLOAT_ARB:
5562          {
5563             GLuint i;
5564             const GLhalfARB *src = (const GLhalfARB *) source;
5565             for (i = 0; i < n; i++) {
5566                GLhalfARB value = src[i];
5567                if (srcPacking->SwapBytes) {
5568                   SWAP2BYTE(value);
5569                }
5570                depthValues[i] = _mesa_half_to_float(value);
5571             }
5572             needClamp = GL_TRUE;
5573          }
5574          break;
5575       default:
5576          _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
5577          free(depthTemp);
5578          return;
5579    }
5580 
5581    /* apply depth scale and bias */
5582    {
5583       const GLfloat scale = ctx->Pixel.DepthScale;
5584       const GLfloat bias = ctx->Pixel.DepthBias;
5585       if (scale != 1.0 || bias != 0.0) {
5586          GLuint i;
5587          for (i = 0; i < n; i++) {
5588             depthValues[i] = depthValues[i] * scale + bias;
5589          }
5590          needClamp = GL_TRUE;
5591       }
5592    }
5593 
5594    /* clamp to [0, 1] */
5595    if (needClamp) {
5596       GLuint i;
5597       for (i = 0; i < n; i++) {
5598          depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
5599       }
5600    }
5601 
5602    /*
5603     * Convert values to dstType
5604     */
5605    if (dstType == GL_UNSIGNED_INT) {
5606       GLuint *zValues = (GLuint *) dest;
5607       GLuint i;
5608       if (depthMax <= 0xffffff) {
5609          /* no overflow worries */
5610          for (i = 0; i < n; i++) {
5611             zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
5612          }
5613       }
5614       else {
5615          /* need to use double precision to prevent overflow problems */
5616          for (i = 0; i < n; i++) {
5617             GLdouble z = depthValues[i] * (GLdouble) depthMax;
5618             if (z >= (GLdouble) 0xffffffff)
5619                zValues[i] = 0xffffffff;
5620             else
5621                zValues[i] = (GLuint) z;
5622          }
5623       }
5624    }
5625    else if (dstType == GL_UNSIGNED_SHORT) {
5626       GLushort *zValues = (GLushort *) dest;
5627       GLuint i;
5628       ASSERT(depthMax <= 0xffff);
5629       for (i = 0; i < n; i++) {
5630          zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
5631       }
5632    }
5633    else if (dstType == GL_FLOAT) {
5634       /* Nothing to do. depthValues is pointing to dest. */
5635    }
5636    else if (dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) {
5637       GLfloat *zValues = (GLfloat*) dest;
5638       GLuint i;
5639       for (i = 0; i < n; i++) {
5640          zValues[i*2] = depthValues[i];
5641       }
5642    }
5643    else {
5644       ASSERT(0);
5645    }
5646 
5647    free(depthTemp);
5648 }
5649 
5650 
5651 /*
5652  * Pack an array of depth values.  The values are floats in [0,1].
5653  */
5654 void
_mesa_pack_depth_span(struct gl_context * ctx,GLuint n,GLvoid * dest,GLenum dstType,const GLfloat * depthSpan,const struct gl_pixelstore_attrib * dstPacking)5655 _mesa_pack_depth_span( struct gl_context *ctx, GLuint n, GLvoid *dest,
5656                        GLenum dstType, const GLfloat *depthSpan,
5657                        const struct gl_pixelstore_attrib *dstPacking )
5658 {
5659    GLfloat *depthCopy = (GLfloat *) malloc(n * sizeof(GLfloat));
5660    if (!depthCopy) {
5661       _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
5662       return;
5663    }
5664 
5665    if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
5666       memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
5667       _mesa_scale_and_bias_depth(ctx, n, depthCopy);
5668       depthSpan = depthCopy;
5669    }
5670 
5671    switch (dstType) {
5672    case GL_UNSIGNED_BYTE:
5673       {
5674          GLubyte *dst = (GLubyte *) dest;
5675          GLuint i;
5676          for (i = 0; i < n; i++) {
5677             dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
5678          }
5679       }
5680       break;
5681    case GL_BYTE:
5682       {
5683          GLbyte *dst = (GLbyte *) dest;
5684          GLuint i;
5685          for (i = 0; i < n; i++) {
5686             dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
5687          }
5688       }
5689       break;
5690    case GL_UNSIGNED_SHORT:
5691       {
5692          GLushort *dst = (GLushort *) dest;
5693          GLuint i;
5694          for (i = 0; i < n; i++) {
5695             CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
5696          }
5697          if (dstPacking->SwapBytes) {
5698             _mesa_swap2( (GLushort *) dst, n );
5699          }
5700       }
5701       break;
5702    case GL_SHORT:
5703       {
5704          GLshort *dst = (GLshort *) dest;
5705          GLuint i;
5706          for (i = 0; i < n; i++) {
5707             dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
5708          }
5709          if (dstPacking->SwapBytes) {
5710             _mesa_swap2( (GLushort *) dst, n );
5711          }
5712       }
5713       break;
5714    case GL_UNSIGNED_INT:
5715       {
5716          GLuint *dst = (GLuint *) dest;
5717          GLuint i;
5718          for (i = 0; i < n; i++) {
5719             dst[i] = FLOAT_TO_UINT( depthSpan[i] );
5720          }
5721          if (dstPacking->SwapBytes) {
5722             _mesa_swap4( (GLuint *) dst, n );
5723          }
5724       }
5725       break;
5726    case GL_INT:
5727       {
5728          GLint *dst = (GLint *) dest;
5729          GLuint i;
5730          for (i = 0; i < n; i++) {
5731             dst[i] = FLOAT_TO_INT( depthSpan[i] );
5732          }
5733          if (dstPacking->SwapBytes) {
5734             _mesa_swap4( (GLuint *) dst, n );
5735          }
5736       }
5737       break;
5738    case GL_FLOAT:
5739       {
5740          GLfloat *dst = (GLfloat *) dest;
5741          GLuint i;
5742          for (i = 0; i < n; i++) {
5743             dst[i] = depthSpan[i];
5744          }
5745          if (dstPacking->SwapBytes) {
5746             _mesa_swap4( (GLuint *) dst, n );
5747          }
5748       }
5749       break;
5750    case GL_HALF_FLOAT_ARB:
5751       {
5752          GLhalfARB *dst = (GLhalfARB *) dest;
5753          GLuint i;
5754          for (i = 0; i < n; i++) {
5755             dst[i] = _mesa_float_to_half(depthSpan[i]);
5756          }
5757          if (dstPacking->SwapBytes) {
5758             _mesa_swap2( (GLushort *) dst, n );
5759          }
5760       }
5761       break;
5762    default:
5763       _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
5764    }
5765 
5766    free(depthCopy);
5767 }
5768 
5769 
5770 
5771 /**
5772  * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
5773  */
5774 void
_mesa_pack_depth_stencil_span(struct gl_context * ctx,GLuint n,GLenum dstType,GLuint * dest,const GLfloat * depthVals,const GLubyte * stencilVals,const struct gl_pixelstore_attrib * dstPacking)5775 _mesa_pack_depth_stencil_span(struct gl_context *ctx,GLuint n,
5776                               GLenum dstType, GLuint *dest,
5777                               const GLfloat *depthVals,
5778                               const GLubyte *stencilVals,
5779                               const struct gl_pixelstore_attrib *dstPacking)
5780 {
5781    GLfloat *depthCopy = (GLfloat *) malloc(n * sizeof(GLfloat));
5782    GLubyte *stencilCopy = (GLubyte *) malloc(n * sizeof(GLubyte));
5783    GLuint i;
5784 
5785    if (!depthCopy || !stencilCopy) {
5786       _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
5787       free(depthCopy);
5788       free(stencilCopy);
5789       return;
5790    }
5791 
5792    if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
5793       memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
5794       _mesa_scale_and_bias_depth(ctx, n, depthCopy);
5795       depthVals = depthCopy;
5796    }
5797 
5798    if (ctx->Pixel.IndexShift ||
5799        ctx->Pixel.IndexOffset ||
5800        ctx->Pixel.MapStencilFlag) {
5801       memcpy(stencilCopy, stencilVals, n * sizeof(GLubyte));
5802       _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
5803       stencilVals = stencilCopy;
5804    }
5805 
5806    switch (dstType) {
5807    case GL_UNSIGNED_INT_24_8:
5808       for (i = 0; i < n; i++) {
5809          GLuint z = (GLuint) (depthVals[i] * 0xffffff);
5810          dest[i] = (z << 8) | (stencilVals[i] & 0xff);
5811       }
5812       break;
5813    case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
5814       for (i = 0; i < n; i++) {
5815          ((GLfloat*)dest)[i*2] = depthVals[i];
5816          dest[i*2+1] = stencilVals[i] & 0xff;
5817       }
5818       break;
5819    }
5820 
5821    if (dstPacking->SwapBytes) {
5822       _mesa_swap4(dest, n);
5823    }
5824 
5825    free(depthCopy);
5826    free(stencilCopy);
5827 }
5828 
5829 
5830 
5831 
5832 /**
5833  * Unpack image data.  Apply byte swapping, byte flipping (bitmap).
5834  * Return all image data in a contiguous block.  This is used when we
5835  * compile glDrawPixels, glTexImage, etc into a display list.  We
5836  * need a copy of the data in a standard format.
5837  */
5838 void *
_mesa_unpack_image(GLuint dimensions,GLsizei width,GLsizei height,GLsizei depth,GLenum format,GLenum type,const GLvoid * pixels,const struct gl_pixelstore_attrib * unpack)5839 _mesa_unpack_image( GLuint dimensions,
5840                     GLsizei width, GLsizei height, GLsizei depth,
5841                     GLenum format, GLenum type, const GLvoid *pixels,
5842                     const struct gl_pixelstore_attrib *unpack )
5843 {
5844    GLint bytesPerRow, compsPerRow;
5845    GLboolean flipBytes, swap2, swap4;
5846 
5847    if (!pixels)
5848       return NULL;  /* not necessarily an error */
5849 
5850    if (width <= 0 || height <= 0 || depth <= 0)
5851       return NULL;  /* generate error later */
5852 
5853    if (type == GL_BITMAP) {
5854       bytesPerRow = (width + 7) >> 3;
5855       flipBytes = unpack->LsbFirst;
5856       swap2 = swap4 = GL_FALSE;
5857       compsPerRow = 0;
5858    }
5859    else {
5860       const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
5861       GLint components = _mesa_components_in_format(format);
5862       GLint bytesPerComp;
5863 
5864       if (_mesa_type_is_packed(type))
5865           components = 1;
5866 
5867       if (bytesPerPixel <= 0 || components <= 0)
5868          return NULL;   /* bad format or type.  generate error later */
5869       bytesPerRow = bytesPerPixel * width;
5870       bytesPerComp = bytesPerPixel / components;
5871       flipBytes = GL_FALSE;
5872       swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
5873       swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
5874       compsPerRow = components * width;
5875       assert(compsPerRow >= width);
5876    }
5877 
5878    {
5879       GLubyte *destBuffer
5880          = (GLubyte *) malloc(bytesPerRow * height * depth);
5881       GLubyte *dst;
5882       GLint img, row;
5883       if (!destBuffer)
5884          return NULL;   /* generate GL_OUT_OF_MEMORY later */
5885 
5886       dst = destBuffer;
5887       for (img = 0; img < depth; img++) {
5888          for (row = 0; row < height; row++) {
5889             const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
5890                                width, height, format, type, img, row, 0);
5891 
5892             if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
5893                GLint i;
5894                flipBytes = GL_FALSE;
5895                if (unpack->LsbFirst) {
5896                   GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
5897                   GLubyte dstMask = 128;
5898                   const GLubyte *s = src;
5899                   GLubyte *d = dst;
5900                   *d = 0;
5901                   for (i = 0; i < width; i++) {
5902                      if (*s & srcMask) {
5903                         *d |= dstMask;
5904                      }
5905                      if (srcMask == 128) {
5906                         srcMask = 1;
5907                         s++;
5908                      }
5909                      else {
5910                         srcMask = srcMask << 1;
5911                      }
5912                      if (dstMask == 1) {
5913                         dstMask = 128;
5914                         d++;
5915                         *d = 0;
5916                      }
5917                      else {
5918                         dstMask = dstMask >> 1;
5919                      }
5920                   }
5921                }
5922                else {
5923                   GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
5924                   GLubyte dstMask = 128;
5925                   const GLubyte *s = src;
5926                   GLubyte *d = dst;
5927                   *d = 0;
5928                   for (i = 0; i < width; i++) {
5929                      if (*s & srcMask) {
5930                         *d |= dstMask;
5931                      }
5932                      if (srcMask == 1) {
5933                         srcMask = 128;
5934                         s++;
5935                      }
5936                      else {
5937                         srcMask = srcMask >> 1;
5938                      }
5939                      if (dstMask == 1) {
5940                         dstMask = 128;
5941                         d++;
5942                         *d = 0;
5943                      }
5944                      else {
5945                         dstMask = dstMask >> 1;
5946                      }
5947                   }
5948                }
5949             }
5950             else {
5951                memcpy(dst, src, bytesPerRow);
5952             }
5953 
5954             /* byte flipping/swapping */
5955             if (flipBytes) {
5956                flip_bytes((GLubyte *) dst, bytesPerRow);
5957             }
5958             else if (swap2) {
5959                _mesa_swap2((GLushort*) dst, compsPerRow);
5960             }
5961             else if (swap4) {
5962                _mesa_swap4((GLuint*) dst, compsPerRow);
5963             }
5964             dst += bytesPerRow;
5965          }
5966       }
5967       return destBuffer;
5968    }
5969 }
5970 
5971 
5972 
5973 /**
5974  * If we unpack colors from a luminance surface, we'll get pixel colors
5975  * such as (l, l, l, a).
5976  * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
5977  * function will compute L=R+G+B before packing.  The net effect is we'll
5978  * accidentally store luminance values = 3*l.
5979  * This function compensates for that by converting (aka rebasing) (l,l,l,a)
5980  * to be (l,0,0,a).
5981  * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
5982  * and INTENSITY.
5983  *
5984  * Finally, we also need to do this when the actual surface format does
5985  * not match the logical surface format.  For example, suppose the user
5986  * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
5987  * Again, we'll get pixel values like (l,l,l,a).
5988  */
5989 void
_mesa_rebase_rgba_float(GLuint n,GLfloat rgba[][4],GLenum baseFormat)5990 _mesa_rebase_rgba_float(GLuint n, GLfloat rgba[][4], GLenum baseFormat)
5991 {
5992    GLuint i;
5993 
5994    switch (baseFormat) {
5995    case GL_ALPHA:
5996       for (i = 0; i < n; i++) {
5997          rgba[i][RCOMP] = 0.0F;
5998          rgba[i][GCOMP] = 0.0F;
5999          rgba[i][BCOMP] = 0.0F;
6000       }
6001       break;
6002    case GL_INTENSITY:
6003       /* fall-through */
6004    case GL_LUMINANCE:
6005       for (i = 0; i < n; i++) {
6006          rgba[i][GCOMP] = 0.0F;
6007          rgba[i][BCOMP] = 0.0F;
6008          rgba[i][ACOMP] = 1.0F;
6009       }
6010       break;
6011    case GL_LUMINANCE_ALPHA:
6012       for (i = 0; i < n; i++) {
6013          rgba[i][GCOMP] = 0.0F;
6014          rgba[i][BCOMP] = 0.0F;
6015       }
6016       break;
6017    default:
6018       /* no-op */
6019       ;
6020    }
6021 }
6022 
6023 
6024 /**
6025  * As above, but GLuint components.
6026  */
6027 void
_mesa_rebase_rgba_uint(GLuint n,GLuint rgba[][4],GLenum baseFormat)6028 _mesa_rebase_rgba_uint(GLuint n, GLuint rgba[][4], GLenum baseFormat)
6029 {
6030    GLuint i;
6031 
6032    switch (baseFormat) {
6033    case GL_ALPHA:
6034       for (i = 0; i < n; i++) {
6035          rgba[i][RCOMP] = 0;
6036          rgba[i][GCOMP] = 0;
6037          rgba[i][BCOMP] = 0;
6038       }
6039       break;
6040    case GL_INTENSITY:
6041       /* fall-through */
6042    case GL_LUMINANCE:
6043       for (i = 0; i < n; i++) {
6044          rgba[i][GCOMP] = 0;
6045          rgba[i][BCOMP] = 0;
6046          rgba[i][ACOMP] = 1;
6047       }
6048       break;
6049    case GL_LUMINANCE_ALPHA:
6050       for (i = 0; i < n; i++) {
6051          rgba[i][GCOMP] = 0;
6052          rgba[i][BCOMP] = 0;
6053       }
6054       break;
6055    default:
6056       /* no-op */
6057       ;
6058    }
6059 }
6060 
6061 
6062