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