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