pack.c revision af69d88d
13464ebd5Sriastradh/*
23464ebd5Sriastradh * Mesa 3-D graphics library
33464ebd5Sriastradh *
43464ebd5Sriastradh * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
53464ebd5Sriastradh * Copyright (C) 2009-2010  VMware, Inc.  All Rights Reserved.
63464ebd5Sriastradh *
73464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a
83464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"),
93464ebd5Sriastradh * to deal in the Software without restriction, including without limitation
103464ebd5Sriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
113464ebd5Sriastradh * and/or sell copies of the Software, and to permit persons to whom the
123464ebd5Sriastradh * Software is furnished to do so, subject to the following conditions:
133464ebd5Sriastradh *
143464ebd5Sriastradh * The above copyright notice and this permission notice shall be included
153464ebd5Sriastradh * in all copies or substantial portions of the Software.
163464ebd5Sriastradh *
173464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
183464ebd5Sriastradh * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193464ebd5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
203464ebd5Sriastradh * THEA AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
213464ebd5Sriastradh * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
223464ebd5Sriastradh * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
233464ebd5Sriastradh */
243464ebd5Sriastradh
253464ebd5Sriastradh
263464ebd5Sriastradh/**
273464ebd5Sriastradh * \file pack.c
283464ebd5Sriastradh * Image and pixel span packing and unpacking.
293464ebd5Sriastradh */
303464ebd5Sriastradh
313464ebd5Sriastradh
32af69d88dSmrg/*
33af69d88dSmrg * XXX: MSVC takes forever to compile this module for x86_64 unless we disable
34af69d88dSmrg * this global optimization.
35af69d88dSmrg *
36af69d88dSmrg * See also:
37af69d88dSmrg * - http://msdn.microsoft.com/en-us/library/1yk3ydd7.aspx
38af69d88dSmrg * - http://msdn.microsoft.com/en-us/library/chh3fb0k.aspx
39af69d88dSmrg */
40af69d88dSmrg#if defined(_MSC_VER) && defined(_M_X64)
41af69d88dSmrg#  pragma optimize( "g", off )
42af69d88dSmrg#endif
43af69d88dSmrg
44af69d88dSmrg
453464ebd5Sriastradh#include "glheader.h"
463464ebd5Sriastradh#include "colormac.h"
473464ebd5Sriastradh#include "enums.h"
483464ebd5Sriastradh#include "image.h"
493464ebd5Sriastradh#include "imports.h"
50af69d88dSmrg#include "macros.h"
513464ebd5Sriastradh#include "mtypes.h"
523464ebd5Sriastradh#include "pack.h"
533464ebd5Sriastradh#include "pixeltransfer.h"
543464ebd5Sriastradh#include "imports.h"
55af69d88dSmrg#include "glformats.h"
563464ebd5Sriastradh#include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
573464ebd5Sriastradh#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
583464ebd5Sriastradh
593464ebd5Sriastradh
603464ebd5Sriastradh/**
613464ebd5Sriastradh * Flip the 8 bits in each byte of the given array.
623464ebd5Sriastradh *
633464ebd5Sriastradh * \param p array.
643464ebd5Sriastradh * \param n number of bytes.
653464ebd5Sriastradh *
663464ebd5Sriastradh * \todo try this trick to flip bytes someday:
673464ebd5Sriastradh * \code
683464ebd5Sriastradh *  v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
693464ebd5Sriastradh *  v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
703464ebd5Sriastradh *  v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
713464ebd5Sriastradh * \endcode
723464ebd5Sriastradh */
733464ebd5Sriastradhstatic void
743464ebd5Sriastradhflip_bytes( GLubyte *p, GLuint n )
753464ebd5Sriastradh{
763464ebd5Sriastradh   GLuint i, a, b;
773464ebd5Sriastradh   for (i = 0; i < n; i++) {
783464ebd5Sriastradh      b = (GLuint) p[i];        /* words are often faster than bytes */
793464ebd5Sriastradh      a = ((b & 0x01) << 7) |
803464ebd5Sriastradh	  ((b & 0x02) << 5) |
813464ebd5Sriastradh	  ((b & 0x04) << 3) |
823464ebd5Sriastradh	  ((b & 0x08) << 1) |
833464ebd5Sriastradh	  ((b & 0x10) >> 1) |
843464ebd5Sriastradh	  ((b & 0x20) >> 3) |
853464ebd5Sriastradh	  ((b & 0x40) >> 5) |
863464ebd5Sriastradh	  ((b & 0x80) >> 7);
873464ebd5Sriastradh      p[i] = (GLubyte) a;
883464ebd5Sriastradh   }
893464ebd5Sriastradh}
903464ebd5Sriastradh
913464ebd5Sriastradh
923464ebd5Sriastradh
933464ebd5Sriastradh/*
943464ebd5Sriastradh * Unpack a 32x32 pixel polygon stipple from user memory using the
953464ebd5Sriastradh * current pixel unpack settings.
963464ebd5Sriastradh */
973464ebd5Sriastradhvoid
983464ebd5Sriastradh_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
993464ebd5Sriastradh                              const struct gl_pixelstore_attrib *unpacking )
1003464ebd5Sriastradh{
1013464ebd5Sriastradh   GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap(32, 32, pattern, unpacking);
1023464ebd5Sriastradh   if (ptrn) {
1033464ebd5Sriastradh      /* Convert pattern from GLubytes to GLuints and handle big/little
1043464ebd5Sriastradh       * endian differences
1053464ebd5Sriastradh       */
1063464ebd5Sriastradh      GLubyte *p = ptrn;
1073464ebd5Sriastradh      GLint i;
1083464ebd5Sriastradh      for (i = 0; i < 32; i++) {
1093464ebd5Sriastradh         dest[i] = (p[0] << 24)
1103464ebd5Sriastradh                 | (p[1] << 16)
1113464ebd5Sriastradh                 | (p[2] <<  8)
1123464ebd5Sriastradh                 | (p[3]      );
1133464ebd5Sriastradh         p += 4;
1143464ebd5Sriastradh      }
1153464ebd5Sriastradh      free(ptrn);
1163464ebd5Sriastradh   }
1173464ebd5Sriastradh}
1183464ebd5Sriastradh
1193464ebd5Sriastradh
1203464ebd5Sriastradh/*
1213464ebd5Sriastradh * Pack polygon stipple into user memory given current pixel packing
1223464ebd5Sriastradh * settings.
1233464ebd5Sriastradh */
1243464ebd5Sriastradhvoid
1253464ebd5Sriastradh_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
1263464ebd5Sriastradh                            const struct gl_pixelstore_attrib *packing )
1273464ebd5Sriastradh{
1283464ebd5Sriastradh   /* Convert pattern from GLuints to GLubytes to handle big/little
1293464ebd5Sriastradh    * endian differences.
1303464ebd5Sriastradh    */
1313464ebd5Sriastradh   GLubyte ptrn[32*4];
1323464ebd5Sriastradh   GLint i;
1333464ebd5Sriastradh   for (i = 0; i < 32; i++) {
1343464ebd5Sriastradh      ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
1353464ebd5Sriastradh      ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
1363464ebd5Sriastradh      ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
1373464ebd5Sriastradh      ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
1383464ebd5Sriastradh   }
1393464ebd5Sriastradh
1403464ebd5Sriastradh   _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
1413464ebd5Sriastradh}
1423464ebd5Sriastradh
1433464ebd5Sriastradh
1443464ebd5Sriastradh/*
1453464ebd5Sriastradh * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
1463464ebd5Sriastradh * order with row alignment = 1 byte.
1473464ebd5Sriastradh */
1483464ebd5SriastradhGLvoid *
1493464ebd5Sriastradh_mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
1503464ebd5Sriastradh                     const struct gl_pixelstore_attrib *packing )
1513464ebd5Sriastradh{
1523464ebd5Sriastradh   GLint bytes, row, width_in_bytes;
1533464ebd5Sriastradh   GLubyte *buffer, *dst;
1543464ebd5Sriastradh
1553464ebd5Sriastradh   if (!pixels)
1563464ebd5Sriastradh      return NULL;
1573464ebd5Sriastradh
1583464ebd5Sriastradh   /* Alloc dest storage */
1593464ebd5Sriastradh   bytes = ((width + 7) / 8 * height);
160af69d88dSmrg   buffer = malloc( bytes );
1613464ebd5Sriastradh   if (!buffer)
1623464ebd5Sriastradh      return NULL;
1633464ebd5Sriastradh
1643464ebd5Sriastradh   width_in_bytes = CEILING( width, 8 );
1653464ebd5Sriastradh   dst = buffer;
1663464ebd5Sriastradh   for (row = 0; row < height; row++) {
1673464ebd5Sriastradh      const GLubyte *src = (const GLubyte *)
1683464ebd5Sriastradh         _mesa_image_address2d(packing, pixels, width, height,
1693464ebd5Sriastradh                               GL_COLOR_INDEX, GL_BITMAP, row, 0);
1703464ebd5Sriastradh      if (!src) {
1713464ebd5Sriastradh         free(buffer);
1723464ebd5Sriastradh         return NULL;
1733464ebd5Sriastradh      }
1743464ebd5Sriastradh
1753464ebd5Sriastradh      if ((packing->SkipPixels & 7) == 0) {
1763464ebd5Sriastradh         memcpy( dst, src, width_in_bytes );
1773464ebd5Sriastradh         if (packing->LsbFirst) {
1783464ebd5Sriastradh            flip_bytes( dst, width_in_bytes );
1793464ebd5Sriastradh         }
1803464ebd5Sriastradh      }
1813464ebd5Sriastradh      else {
1823464ebd5Sriastradh         /* handling SkipPixels is a bit tricky (no pun intended!) */
1833464ebd5Sriastradh         GLint i;
1843464ebd5Sriastradh         if (packing->LsbFirst) {
1853464ebd5Sriastradh            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
1863464ebd5Sriastradh            GLubyte dstMask = 128;
1873464ebd5Sriastradh            const GLubyte *s = src;
1883464ebd5Sriastradh            GLubyte *d = dst;
1893464ebd5Sriastradh            *d = 0;
1903464ebd5Sriastradh            for (i = 0; i < width; i++) {
1913464ebd5Sriastradh               if (*s & srcMask) {
1923464ebd5Sriastradh                  *d |= dstMask;
1933464ebd5Sriastradh               }
1943464ebd5Sriastradh               if (srcMask == 128) {
1953464ebd5Sriastradh                  srcMask = 1;
1963464ebd5Sriastradh                  s++;
1973464ebd5Sriastradh               }
1983464ebd5Sriastradh               else {
1993464ebd5Sriastradh                  srcMask = srcMask << 1;
2003464ebd5Sriastradh               }
2013464ebd5Sriastradh               if (dstMask == 1) {
2023464ebd5Sriastradh                  dstMask = 128;
2033464ebd5Sriastradh                  d++;
2043464ebd5Sriastradh                  *d = 0;
2053464ebd5Sriastradh               }
2063464ebd5Sriastradh               else {
2073464ebd5Sriastradh                  dstMask = dstMask >> 1;
2083464ebd5Sriastradh               }
2093464ebd5Sriastradh            }
2103464ebd5Sriastradh         }
2113464ebd5Sriastradh         else {
2123464ebd5Sriastradh            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2133464ebd5Sriastradh            GLubyte dstMask = 128;
2143464ebd5Sriastradh            const GLubyte *s = src;
2153464ebd5Sriastradh            GLubyte *d = dst;
2163464ebd5Sriastradh            *d = 0;
2173464ebd5Sriastradh            for (i = 0; i < width; i++) {
2183464ebd5Sriastradh               if (*s & srcMask) {
2193464ebd5Sriastradh                  *d |= dstMask;
2203464ebd5Sriastradh               }
2213464ebd5Sriastradh               if (srcMask == 1) {
2223464ebd5Sriastradh                  srcMask = 128;
2233464ebd5Sriastradh                  s++;
2243464ebd5Sriastradh               }
2253464ebd5Sriastradh               else {
2263464ebd5Sriastradh                  srcMask = srcMask >> 1;
2273464ebd5Sriastradh               }
2283464ebd5Sriastradh               if (dstMask == 1) {
2293464ebd5Sriastradh                  dstMask = 128;
2303464ebd5Sriastradh                  d++;
2313464ebd5Sriastradh                  *d = 0;
2323464ebd5Sriastradh               }
2333464ebd5Sriastradh               else {
2343464ebd5Sriastradh                  dstMask = dstMask >> 1;
2353464ebd5Sriastradh               }
2363464ebd5Sriastradh            }
2373464ebd5Sriastradh         }
2383464ebd5Sriastradh      }
2393464ebd5Sriastradh      dst += width_in_bytes;
2403464ebd5Sriastradh   }
2413464ebd5Sriastradh
2423464ebd5Sriastradh   return buffer;
2433464ebd5Sriastradh}
2443464ebd5Sriastradh
2453464ebd5Sriastradh
2463464ebd5Sriastradh/*
2473464ebd5Sriastradh * Pack bitmap data.
2483464ebd5Sriastradh */
2493464ebd5Sriastradhvoid
2503464ebd5Sriastradh_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
2513464ebd5Sriastradh                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
2523464ebd5Sriastradh{
2533464ebd5Sriastradh   GLint row, width_in_bytes;
2543464ebd5Sriastradh   const GLubyte *src;
2553464ebd5Sriastradh
2563464ebd5Sriastradh   if (!source)
2573464ebd5Sriastradh      return;
2583464ebd5Sriastradh
2593464ebd5Sriastradh   width_in_bytes = CEILING( width, 8 );
2603464ebd5Sriastradh   src = source;
2613464ebd5Sriastradh   for (row = 0; row < height; row++) {
2623464ebd5Sriastradh      GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
2633464ebd5Sriastradh                       width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
2643464ebd5Sriastradh      if (!dst)
2653464ebd5Sriastradh         return;
2663464ebd5Sriastradh
2673464ebd5Sriastradh      if ((packing->SkipPixels & 7) == 0) {
2683464ebd5Sriastradh         memcpy( dst, src, width_in_bytes );
2693464ebd5Sriastradh         if (packing->LsbFirst) {
2703464ebd5Sriastradh            flip_bytes( dst, width_in_bytes );
2713464ebd5Sriastradh         }
2723464ebd5Sriastradh      }
2733464ebd5Sriastradh      else {
2743464ebd5Sriastradh         /* handling SkipPixels is a bit tricky (no pun intended!) */
2753464ebd5Sriastradh         GLint i;
2763464ebd5Sriastradh         if (packing->LsbFirst) {
2773464ebd5Sriastradh            GLubyte srcMask = 128;
2783464ebd5Sriastradh            GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
2793464ebd5Sriastradh            const GLubyte *s = src;
2803464ebd5Sriastradh            GLubyte *d = dst;
2813464ebd5Sriastradh            *d = 0;
2823464ebd5Sriastradh            for (i = 0; i < width; i++) {
2833464ebd5Sriastradh               if (*s & srcMask) {
2843464ebd5Sriastradh                  *d |= dstMask;
2853464ebd5Sriastradh               }
2863464ebd5Sriastradh               if (srcMask == 1) {
2873464ebd5Sriastradh                  srcMask = 128;
2883464ebd5Sriastradh                  s++;
2893464ebd5Sriastradh               }
2903464ebd5Sriastradh               else {
2913464ebd5Sriastradh                  srcMask = srcMask >> 1;
2923464ebd5Sriastradh               }
2933464ebd5Sriastradh               if (dstMask == 128) {
2943464ebd5Sriastradh                  dstMask = 1;
2953464ebd5Sriastradh                  d++;
2963464ebd5Sriastradh                  *d = 0;
2973464ebd5Sriastradh               }
2983464ebd5Sriastradh               else {
2993464ebd5Sriastradh                  dstMask = dstMask << 1;
3003464ebd5Sriastradh               }
3013464ebd5Sriastradh            }
3023464ebd5Sriastradh         }
3033464ebd5Sriastradh         else {
3043464ebd5Sriastradh            GLubyte srcMask = 128;
3053464ebd5Sriastradh            GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
3063464ebd5Sriastradh            const GLubyte *s = src;
3073464ebd5Sriastradh            GLubyte *d = dst;
3083464ebd5Sriastradh            *d = 0;
3093464ebd5Sriastradh            for (i = 0; i < width; i++) {
3103464ebd5Sriastradh               if (*s & srcMask) {
3113464ebd5Sriastradh                  *d |= dstMask;
3123464ebd5Sriastradh               }
3133464ebd5Sriastradh               if (srcMask == 1) {
3143464ebd5Sriastradh                  srcMask = 128;
3153464ebd5Sriastradh                  s++;
3163464ebd5Sriastradh               }
3173464ebd5Sriastradh               else {
3183464ebd5Sriastradh                  srcMask = srcMask >> 1;
3193464ebd5Sriastradh               }
3203464ebd5Sriastradh               if (dstMask == 1) {
3213464ebd5Sriastradh                  dstMask = 128;
3223464ebd5Sriastradh                  d++;
3233464ebd5Sriastradh                  *d = 0;
3243464ebd5Sriastradh               }
3253464ebd5Sriastradh               else {
3263464ebd5Sriastradh                  dstMask = dstMask >> 1;
3273464ebd5Sriastradh               }
3283464ebd5Sriastradh            }
3293464ebd5Sriastradh         }
3303464ebd5Sriastradh      }
3313464ebd5Sriastradh      src += width_in_bytes;
3323464ebd5Sriastradh   }
3333464ebd5Sriastradh}
3343464ebd5Sriastradh
3353464ebd5Sriastradh
3363464ebd5Sriastradh/**
3373464ebd5Sriastradh * Get indexes of color components for a basic color format, such as
3383464ebd5Sriastradh * GL_RGBA, GL_RED, GL_LUMINANCE_ALPHA, etc.  Return -1 for indexes
3393464ebd5Sriastradh * that do not apply.
3403464ebd5Sriastradh */
3413464ebd5Sriastradhstatic void
3423464ebd5Sriastradhget_component_indexes(GLenum format,
3433464ebd5Sriastradh                      GLint *redIndex,
3443464ebd5Sriastradh                      GLint *greenIndex,
3453464ebd5Sriastradh                      GLint *blueIndex,
3463464ebd5Sriastradh                      GLint *alphaIndex,
3473464ebd5Sriastradh                      GLint *luminanceIndex,
3483464ebd5Sriastradh                      GLint *intensityIndex)
3493464ebd5Sriastradh{
3503464ebd5Sriastradh   *redIndex = -1;
3513464ebd5Sriastradh   *greenIndex = -1;
3523464ebd5Sriastradh   *blueIndex = -1;
3533464ebd5Sriastradh   *alphaIndex = -1;
3543464ebd5Sriastradh   *luminanceIndex = -1;
3553464ebd5Sriastradh   *intensityIndex = -1;
3563464ebd5Sriastradh
3573464ebd5Sriastradh   switch (format) {
3583464ebd5Sriastradh   case GL_LUMINANCE:
3593464ebd5Sriastradh   case GL_LUMINANCE_INTEGER_EXT:
3603464ebd5Sriastradh      *luminanceIndex = 0;
3613464ebd5Sriastradh      break;
3623464ebd5Sriastradh   case GL_LUMINANCE_ALPHA:
3633464ebd5Sriastradh   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
3643464ebd5Sriastradh      *luminanceIndex = 0;
3653464ebd5Sriastradh      *alphaIndex = 1;
3663464ebd5Sriastradh      break;
3673464ebd5Sriastradh   case GL_INTENSITY:
3683464ebd5Sriastradh      *intensityIndex = 0;
3693464ebd5Sriastradh      break;
3703464ebd5Sriastradh   case GL_RED:
3713464ebd5Sriastradh   case GL_RED_INTEGER_EXT:
3723464ebd5Sriastradh      *redIndex = 0;
3733464ebd5Sriastradh      break;
3743464ebd5Sriastradh   case GL_GREEN:
3753464ebd5Sriastradh   case GL_GREEN_INTEGER_EXT:
3763464ebd5Sriastradh      *greenIndex = 0;
3773464ebd5Sriastradh      break;
3783464ebd5Sriastradh   case GL_BLUE:
3793464ebd5Sriastradh   case GL_BLUE_INTEGER_EXT:
3803464ebd5Sriastradh      *blueIndex = 0;
3813464ebd5Sriastradh      break;
3823464ebd5Sriastradh   case GL_ALPHA:
3833464ebd5Sriastradh   case GL_ALPHA_INTEGER_EXT:
3843464ebd5Sriastradh      *alphaIndex = 0;
3853464ebd5Sriastradh      break;
3863464ebd5Sriastradh   case GL_RG:
3873464ebd5Sriastradh   case GL_RG_INTEGER:
3883464ebd5Sriastradh      *redIndex = 0;
3893464ebd5Sriastradh      *greenIndex = 1;
3903464ebd5Sriastradh      break;
3913464ebd5Sriastradh   case GL_RGB:
3923464ebd5Sriastradh   case GL_RGB_INTEGER_EXT:
3933464ebd5Sriastradh      *redIndex = 0;
3943464ebd5Sriastradh      *greenIndex = 1;
3953464ebd5Sriastradh      *blueIndex = 2;
3963464ebd5Sriastradh      break;
3973464ebd5Sriastradh   case GL_BGR:
3983464ebd5Sriastradh   case GL_BGR_INTEGER_EXT:
3993464ebd5Sriastradh      *blueIndex = 0;
4003464ebd5Sriastradh      *greenIndex = 1;
4013464ebd5Sriastradh      *redIndex = 2;
4023464ebd5Sriastradh      break;
4033464ebd5Sriastradh   case GL_RGBA:
4043464ebd5Sriastradh   case GL_RGBA_INTEGER_EXT:
4053464ebd5Sriastradh      *redIndex = 0;
4063464ebd5Sriastradh      *greenIndex = 1;
4073464ebd5Sriastradh      *blueIndex = 2;
4083464ebd5Sriastradh      *alphaIndex = 3;
4093464ebd5Sriastradh      break;
4103464ebd5Sriastradh   case GL_BGRA:
4113464ebd5Sriastradh   case GL_BGRA_INTEGER:
4123464ebd5Sriastradh      *redIndex = 2;
4133464ebd5Sriastradh      *greenIndex = 1;
4143464ebd5Sriastradh      *blueIndex = 0;
4153464ebd5Sriastradh      *alphaIndex = 3;
4163464ebd5Sriastradh      break;
4173464ebd5Sriastradh   case GL_ABGR_EXT:
4183464ebd5Sriastradh      *redIndex = 3;
4193464ebd5Sriastradh      *greenIndex = 2;
4203464ebd5Sriastradh      *blueIndex = 1;
4213464ebd5Sriastradh      *alphaIndex = 0;
4223464ebd5Sriastradh      break;
4233464ebd5Sriastradh   default:
4243464ebd5Sriastradh      assert(0 && "bad format in get_component_indexes()");
4253464ebd5Sriastradh   }
4263464ebd5Sriastradh}
4273464ebd5Sriastradh
4283464ebd5Sriastradh
4293464ebd5Sriastradh
4303464ebd5Sriastradh/**
4313464ebd5Sriastradh * For small integer types, return the min and max possible values.
4323464ebd5Sriastradh * Used for clamping floats to unscaled integer types.
4333464ebd5Sriastradh * \return GL_TRUE if type is handled, GL_FALSE otherwise.
4343464ebd5Sriastradh */
4353464ebd5Sriastradhstatic GLboolean
4363464ebd5Sriastradhget_type_min_max(GLenum type, GLfloat *min, GLfloat *max)
4373464ebd5Sriastradh{
4383464ebd5Sriastradh   switch (type) {
4393464ebd5Sriastradh   case GL_BYTE:
4403464ebd5Sriastradh      *min = -128.0;
4413464ebd5Sriastradh      *max = 127.0;
4423464ebd5Sriastradh      return GL_TRUE;
4433464ebd5Sriastradh   case GL_UNSIGNED_BYTE:
4443464ebd5Sriastradh      *min = 0.0;
4453464ebd5Sriastradh      *max = 255.0;
4463464ebd5Sriastradh      return GL_TRUE;
4473464ebd5Sriastradh   case GL_SHORT:
4483464ebd5Sriastradh      *min = -32768.0;
4493464ebd5Sriastradh      *max = 32767.0;
4503464ebd5Sriastradh      return GL_TRUE;
4513464ebd5Sriastradh   case GL_UNSIGNED_SHORT:
4523464ebd5Sriastradh      *min = 0.0;
4533464ebd5Sriastradh      *max = 65535.0;
4543464ebd5Sriastradh      return GL_TRUE;
4553464ebd5Sriastradh   default:
4563464ebd5Sriastradh      return GL_FALSE;
4573464ebd5Sriastradh   }
4583464ebd5Sriastradh}
4593464ebd5Sriastradh
460af69d88dSmrg/* Customization of unsigned integer packing.
461af69d88dSmrg */
462af69d88dSmrg#define SRC_TYPE GLuint
463af69d88dSmrg
464af69d88dSmrg#define DST_TYPE GLuint
465af69d88dSmrg#define SRC_CONVERT(x) (x)
466af69d88dSmrg#define FN_NAME pack_uint_from_uint_rgba
467af69d88dSmrg#include "pack_tmp.h"
468af69d88dSmrg#undef DST_TYPE
469af69d88dSmrg#undef SRC_CONVERT
470af69d88dSmrg#undef FN_NAME
471af69d88dSmrg
472af69d88dSmrg#define DST_TYPE GLint
473af69d88dSmrg#define SRC_CONVERT(x) MIN2(x, 0x7fffffff)
474af69d88dSmrg#define FN_NAME pack_int_from_uint_rgba
475af69d88dSmrg#include "pack_tmp.h"
476af69d88dSmrg#undef DST_TYPE
477af69d88dSmrg#undef SRC_CONVERT
478af69d88dSmrg#undef FN_NAME
479af69d88dSmrg
480af69d88dSmrg#define DST_TYPE GLushort
481af69d88dSmrg#define SRC_CONVERT(x) MIN2(x, 0xffff)
482af69d88dSmrg#define FN_NAME pack_ushort_from_uint_rgba
483af69d88dSmrg#include "pack_tmp.h"
484af69d88dSmrg#undef DST_TYPE
485af69d88dSmrg#undef SRC_CONVERT
486af69d88dSmrg#undef FN_NAME
487af69d88dSmrg
488af69d88dSmrg#define DST_TYPE GLshort
489af69d88dSmrg#define SRC_CONVERT(x) CLAMP((int)x, -32768, 32767)
490af69d88dSmrg#define FN_NAME pack_short_from_uint_rgba
491af69d88dSmrg#include "pack_tmp.h"
492af69d88dSmrg#undef DST_TYPE
493af69d88dSmrg#undef SRC_CONVERT
494af69d88dSmrg#undef FN_NAME
495af69d88dSmrg
496af69d88dSmrg#define DST_TYPE GLubyte
497af69d88dSmrg#define SRC_CONVERT(x) MIN2(x, 0xff)
498af69d88dSmrg#define FN_NAME pack_ubyte_from_uint_rgba
499af69d88dSmrg#include "pack_tmp.h"
500af69d88dSmrg#undef DST_TYPE
501af69d88dSmrg#undef SRC_CONVERT
502af69d88dSmrg#undef FN_NAME
503af69d88dSmrg
504af69d88dSmrg#define DST_TYPE GLbyte
505af69d88dSmrg#define SRC_CONVERT(x) CLAMP((int)x, -128, 127)
506af69d88dSmrg#define FN_NAME pack_byte_from_uint_rgba
507af69d88dSmrg#include "pack_tmp.h"
508af69d88dSmrg#undef DST_TYPE
509af69d88dSmrg#undef SRC_CONVERT
510af69d88dSmrg#undef FN_NAME
511af69d88dSmrg
512af69d88dSmrg#undef SRC_TYPE
513af69d88dSmrg
514af69d88dSmrgstatic void
515af69d88dSmrg_pack_rgba_span_from_uints_problem(struct gl_context *ctx,
516af69d88dSmrg                                   GLenum dstFormat, GLenum dstType)
517af69d88dSmrg{
518af69d88dSmrg   _mesa_problem(ctx,
519af69d88dSmrg                 "Unsupported type (%s) / format (%s) "
520af69d88dSmrg                 "in _mesa_pack_rgba_span_from_uints",
521af69d88dSmrg                 _mesa_lookup_enum_by_nr(dstType),
522af69d88dSmrg                 _mesa_lookup_enum_by_nr(dstFormat));
523af69d88dSmrg}
524af69d88dSmrg
525af69d88dSmrgvoid
526af69d88dSmrg_mesa_pack_rgba_span_from_uints(struct gl_context *ctx, GLuint n, GLuint rgba[][4],
527af69d88dSmrg                                GLenum dstFormat, GLenum dstType,
528af69d88dSmrg                                GLvoid *dstAddr)
529af69d88dSmrg{
530af69d88dSmrg   GLuint i;
531af69d88dSmrg
532af69d88dSmrg   switch(dstType) {
533af69d88dSmrg   case GL_UNSIGNED_INT:
534af69d88dSmrg      pack_uint_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
535af69d88dSmrg      break;
536af69d88dSmrg   case GL_INT:
537af69d88dSmrg      pack_int_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
538af69d88dSmrg      break;
539af69d88dSmrg   case GL_UNSIGNED_SHORT:
540af69d88dSmrg      pack_ushort_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
541af69d88dSmrg      break;
542af69d88dSmrg   case GL_SHORT:
543af69d88dSmrg      pack_short_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
544af69d88dSmrg      break;
545af69d88dSmrg   case GL_UNSIGNED_BYTE:
546af69d88dSmrg      pack_ubyte_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
547af69d88dSmrg      break;
548af69d88dSmrg   case GL_BYTE:
549af69d88dSmrg      pack_byte_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
550af69d88dSmrg      break;
551af69d88dSmrg   case GL_UNSIGNED_BYTE_3_3_2:
552af69d88dSmrg      if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
553af69d88dSmrg         GLubyte *dst = (GLubyte *) dstAddr;
554af69d88dSmrg         for (i=0;i<n;i++) {
555af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 7) << 5)
556af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 7) << 2)
557af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 3)     );
558af69d88dSmrg         }
559af69d88dSmrg      } else {
560af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
561af69d88dSmrg      }
562af69d88dSmrg      break;
563af69d88dSmrg   case GL_UNSIGNED_BYTE_2_3_3_REV:
564af69d88dSmrg      if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
565af69d88dSmrg         GLubyte *dst = (GLubyte *) dstAddr;
566af69d88dSmrg         for (i=0;i<n;i++) {
567af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 7)     )
568af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 7) << 3)
569af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 3) << 6);
570af69d88dSmrg         }
571af69d88dSmrg      } else {
572af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
573af69d88dSmrg      }
574af69d88dSmrg      break;
575af69d88dSmrg   case GL_UNSIGNED_SHORT_5_6_5:
576af69d88dSmrg      if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
577af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
578af69d88dSmrg         for (i=0;i<n;i++) {
579af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 31) << 11)
580af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 63) <<  5)
581af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 31)      );
582af69d88dSmrg         }
583af69d88dSmrg      } else {
584af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
585af69d88dSmrg      }
586af69d88dSmrg      break;
587af69d88dSmrg   case GL_UNSIGNED_SHORT_5_6_5_REV:
588af69d88dSmrg      if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
589af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
590af69d88dSmrg         for (i=0;i<n;i++) {
591af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 31)      )
592af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 63) <<  5)
593af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 31) << 11);
594af69d88dSmrg         }
595af69d88dSmrg      } else {
596af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
597af69d88dSmrg      }
598af69d88dSmrg      break;
599af69d88dSmrg   case GL_UNSIGNED_SHORT_4_4_4_4:
600af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
601af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
602af69d88dSmrg         for (i=0;i<n;i++) {
603af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 15) << 12)
604af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 15) <<  8)
605af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 15) <<  4)
606af69d88dSmrg                   | (MIN2(rgba[i][ACOMP], 15)      );
607af69d88dSmrg         }
608af69d88dSmrg      }
609af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
610af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
611af69d88dSmrg         for (i=0;i<n;i++) {
612af69d88dSmrg            dst[i] = (MIN2(rgba[i][BCOMP], 15) << 12)
613af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 15) <<  8)
614af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 15) <<  4)
615af69d88dSmrg                   | (MIN2(rgba[i][ACOMP], 15)      );
616af69d88dSmrg         }
617af69d88dSmrg      }
618af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
619af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
620af69d88dSmrg         for (i=0;i<n;i++) {
621af69d88dSmrg            dst[i] = (MIN2(rgba[i][ACOMP], 15) << 12)
622af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 15) <<  8)
623af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 15) <<  4)
624af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 15)      );
625af69d88dSmrg         }
626af69d88dSmrg      } else {
627af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
628af69d88dSmrg      }
629af69d88dSmrg      break;
630af69d88dSmrg   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
631af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
632af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
633af69d88dSmrg         for (i=0;i<n;i++) {
634af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 15)      )
635af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 15) <<  4)
636af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 15) <<  8)
637af69d88dSmrg                   | (MIN2(rgba[i][ACOMP], 15) << 12);
638af69d88dSmrg         }
639af69d88dSmrg      }
640af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
641af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
642af69d88dSmrg         for (i=0;i<n;i++) {
643af69d88dSmrg            dst[i] = (MIN2(rgba[i][BCOMP], 15)      )
644af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 15) <<  4)
645af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 15) <<  8)
646af69d88dSmrg                   | (MIN2(rgba[i][ACOMP], 15) << 12);
647af69d88dSmrg         }
648af69d88dSmrg      }
649af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
650af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
651af69d88dSmrg         for (i=0;i<n;i++) {
652af69d88dSmrg            dst[i] = (MIN2(rgba[i][ACOMP], 15)      )
653af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 15) <<  4)
654af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 15) <<  8)
655af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 15) << 12);
656af69d88dSmrg         }
657af69d88dSmrg      } else {
658af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
659af69d88dSmrg      }
660af69d88dSmrg      break;
661af69d88dSmrg   case GL_UNSIGNED_SHORT_5_5_5_1:
662af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
663af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
664af69d88dSmrg         for (i=0;i<n;i++) {
665af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 31) << 11)
666af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 31) <<  6)
667af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 31) <<  1)
668af69d88dSmrg                   | (MIN2(rgba[i][ACOMP],  1)      );
669af69d88dSmrg         }
670af69d88dSmrg      }
671af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
672af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
673af69d88dSmrg         for (i=0;i<n;i++) {
674af69d88dSmrg            dst[i] = (MIN2(rgba[i][BCOMP], 31) << 11)
675af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 31) <<  6)
676af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 31) <<  1)
677af69d88dSmrg                   | (MIN2(rgba[i][ACOMP],  1)      );
678af69d88dSmrg         }
679af69d88dSmrg      }
680af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
681af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
682af69d88dSmrg         for (i=0;i<n;i++) {
683af69d88dSmrg            dst[i] = (MIN2(rgba[i][ACOMP], 31) << 11)
684af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 31) <<  6)
685af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 31) <<  1)
686af69d88dSmrg                   | (MIN2(rgba[i][RCOMP],  1)      );
687af69d88dSmrg         }
688af69d88dSmrg      } else {
689af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
690af69d88dSmrg      }
691af69d88dSmrg      break;
692af69d88dSmrg   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
693af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
694af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
695af69d88dSmrg         for (i=0;i<n;i++) {
696af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 31)      )
697af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 31) <<  5)
698af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 31) << 10)
699af69d88dSmrg                   | (MIN2(rgba[i][ACOMP],  1) << 15);
700af69d88dSmrg         }
701af69d88dSmrg      }
702af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
703af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
704af69d88dSmrg         for (i=0;i<n;i++) {
705af69d88dSmrg            dst[i] = (MIN2(rgba[i][BCOMP], 31)      )
706af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 31) <<  5)
707af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 31) << 10)
708af69d88dSmrg                   | (MIN2(rgba[i][ACOMP],  1) << 15);
709af69d88dSmrg         }
710af69d88dSmrg      }
711af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
712af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
713af69d88dSmrg         for (i=0;i<n;i++) {
714af69d88dSmrg            dst[i] = (MIN2(rgba[i][ACOMP], 31)      )
715af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 31) <<  5)
716af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 31) << 10)
717af69d88dSmrg                   | (MIN2(rgba[i][RCOMP],  1) << 15);
718af69d88dSmrg         }
719af69d88dSmrg      } else {
720af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
721af69d88dSmrg      }
722af69d88dSmrg      break;
723af69d88dSmrg   case GL_UNSIGNED_INT_8_8_8_8:
724af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
725af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
726af69d88dSmrg         for (i=0;i<n;i++) {
727af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 255) << 24)
728af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 255) << 16)
729af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 255) <<  8)
730af69d88dSmrg                   | (MIN2(rgba[i][ACOMP], 255)      );
731af69d88dSmrg         }
732af69d88dSmrg      }
733af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
734af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
735af69d88dSmrg         for (i=0;i<n;i++) {
736af69d88dSmrg            dst[i] = (MIN2(rgba[i][BCOMP], 255) << 24)
737af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 255) << 16)
738af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 255) <<  8)
739af69d88dSmrg                   | (MIN2(rgba[i][ACOMP], 255)      );
740af69d88dSmrg         }
741af69d88dSmrg      }
742af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
743af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
744af69d88dSmrg         for (i=0;i<n;i++) {
745af69d88dSmrg            dst[i] = (MIN2(rgba[i][ACOMP], 255) << 24)
746af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 255) << 16)
747af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 255) <<  8)
748af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 255)      );
749af69d88dSmrg         }
750af69d88dSmrg      } else {
751af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
752af69d88dSmrg      }
753af69d88dSmrg      break;
754af69d88dSmrg   case GL_UNSIGNED_INT_8_8_8_8_REV:
755af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
756af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
757af69d88dSmrg         for (i=0;i<n;i++) {
758af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 255)      )
759af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 255) <<  8)
760af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 255) << 16)
761af69d88dSmrg                   | (MIN2(rgba[i][ACOMP], 255) << 24);
762af69d88dSmrg         }
763af69d88dSmrg      }
764af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
765af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
766af69d88dSmrg         for (i=0;i<n;i++) {
767af69d88dSmrg            dst[i] = (MIN2(rgba[i][BCOMP], 255)      )
768af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 255) <<  8)
769af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 255) << 16)
770af69d88dSmrg                   | (MIN2(rgba[i][ACOMP], 255) << 24);
771af69d88dSmrg         }
772af69d88dSmrg      }
773af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
774af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
775af69d88dSmrg         for (i=0;i<n;i++) {
776af69d88dSmrg            dst[i] = (MIN2(rgba[i][ACOMP], 255)      )
777af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 255) <<  8)
778af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 255) << 16)
779af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 255) << 24);
780af69d88dSmrg         }
781af69d88dSmrg      } else {
782af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
783af69d88dSmrg      }
784af69d88dSmrg      break;
785af69d88dSmrg   case GL_UNSIGNED_INT_10_10_10_2:
786af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
787af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
788af69d88dSmrg         for (i=0;i<n;i++) {
789af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 1023) << 22)
790af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 1023) << 12)
791af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 1023) <<  2)
792af69d88dSmrg                   | (MIN2(rgba[i][ACOMP],    3)      );
793af69d88dSmrg         }
794af69d88dSmrg      }
795af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
796af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
797af69d88dSmrg         for (i=0;i<n;i++) {
798af69d88dSmrg            dst[i] = (MIN2(rgba[i][BCOMP], 1023) << 22)
799af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 1023) << 12)
800af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 1023) <<  2)
801af69d88dSmrg                   | (MIN2(rgba[i][ACOMP],    3)      );
802af69d88dSmrg         }
803af69d88dSmrg      }
804af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
805af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
806af69d88dSmrg         for (i=0;i<n;i++) {
807af69d88dSmrg            dst[i] = (MIN2(rgba[i][ACOMP], 1023) << 22)
808af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 1023) << 12)
809af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 1023) <<  2)
810af69d88dSmrg                   | (MIN2(rgba[i][RCOMP],    3)      );
811af69d88dSmrg         }
812af69d88dSmrg      } else {
813af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
814af69d88dSmrg      }
815af69d88dSmrg      break;
816af69d88dSmrg   case GL_UNSIGNED_INT_2_10_10_10_REV:
817af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
818af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
819af69d88dSmrg         for (i=0;i<n;i++) {
820af69d88dSmrg            dst[i] = (MIN2(rgba[i][RCOMP], 1023)      )
821af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 1023) << 10)
822af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 1023) << 20)
823af69d88dSmrg                   | (MIN2(rgba[i][ACOMP],    3) << 30);
824af69d88dSmrg         }
825af69d88dSmrg      }
826af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
827af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
828af69d88dSmrg         for (i=0;i<n;i++) {
829af69d88dSmrg            dst[i] = (MIN2(rgba[i][BCOMP], 1023)      )
830af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 1023) << 10)
831af69d88dSmrg                   | (MIN2(rgba[i][RCOMP], 1023) << 20)
832af69d88dSmrg                   | (MIN2(rgba[i][ACOMP],    3) << 30);
833af69d88dSmrg         }
834af69d88dSmrg      }
835af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
836af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
837af69d88dSmrg         for (i=0;i<n;i++) {
838af69d88dSmrg            dst[i] = (MIN2(rgba[i][ACOMP], 1023)      )
839af69d88dSmrg                   | (MIN2(rgba[i][BCOMP], 1023) << 10)
840af69d88dSmrg                   | (MIN2(rgba[i][GCOMP], 1023) << 20)
841af69d88dSmrg                   | (MIN2(rgba[i][RCOMP],    3) << 30);
842af69d88dSmrg         }
843af69d88dSmrg      } else {
844af69d88dSmrg         _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
845af69d88dSmrg      }
846af69d88dSmrg      break;
847af69d88dSmrg   default:
848af69d88dSmrg      _pack_rgba_span_from_uints_problem(ctx, dstFormat, dstType);
849af69d88dSmrg      return;
850af69d88dSmrg   }
851af69d88dSmrg}
852af69d88dSmrg
853af69d88dSmrg
854af69d88dSmrg/* Customization of signed integer packing.
855af69d88dSmrg */
856af69d88dSmrg#define SRC_TYPE GLint
857af69d88dSmrg
858af69d88dSmrg#define DST_TYPE GLuint
859af69d88dSmrg#define SRC_CONVERT(x) MAX2(x, 0)
860af69d88dSmrg#define FN_NAME pack_uint_from_int_rgba
861af69d88dSmrg#include "pack_tmp.h"
862af69d88dSmrg#undef DST_TYPE
863af69d88dSmrg#undef SRC_CONVERT
864af69d88dSmrg#undef FN_NAME
865af69d88dSmrg
866af69d88dSmrg#define DST_TYPE GLushort
867af69d88dSmrg#define SRC_CONVERT(x) MAX2(x, 0)
868af69d88dSmrg#define FN_NAME pack_ushort_from_int_rgba
869af69d88dSmrg#include "pack_tmp.h"
870af69d88dSmrg#undef DST_TYPE
871af69d88dSmrg#undef SRC_CONVERT
872af69d88dSmrg#undef FN_NAME
873af69d88dSmrg
874af69d88dSmrg#define DST_TYPE GLshort
875af69d88dSmrg#define SRC_CONVERT(x) CLAMP(x, -0x8000, 0x7fff)
876af69d88dSmrg#define FN_NAME pack_short_from_int_rgba
877af69d88dSmrg#include "pack_tmp.h"
878af69d88dSmrg#undef DST_TYPE
879af69d88dSmrg#undef SRC_CONVERT
880af69d88dSmrg#undef FN_NAME
881af69d88dSmrg
882af69d88dSmrg#define DST_TYPE GLubyte
883af69d88dSmrg#define SRC_CONVERT(x) MAX2(x, 0)
884af69d88dSmrg#define FN_NAME pack_ubyte_from_int_rgba
885af69d88dSmrg#include "pack_tmp.h"
886af69d88dSmrg#undef DST_TYPE
887af69d88dSmrg#undef SRC_CONVERT
888af69d88dSmrg#undef FN_NAME
889af69d88dSmrg
890af69d88dSmrg#define DST_TYPE GLbyte
891af69d88dSmrg#define SRC_CONVERT(x) CLAMP(x, -0x80, 0x7f)
892af69d88dSmrg#define FN_NAME pack_byte_from_int_rgba
893af69d88dSmrg#include "pack_tmp.h"
894af69d88dSmrg#undef DST_TYPE
895af69d88dSmrg#undef SRC_CONVERT
896af69d88dSmrg#undef FN_NAME
897af69d88dSmrg
898af69d88dSmrg#undef SRC_TYPE
899af69d88dSmrg
900af69d88dSmrgstatic void
901af69d88dSmrg_pack_rgba_span_from_ints_problem(struct gl_context *ctx,
902af69d88dSmrg                                   GLenum dstFormat, GLenum dstType)
903af69d88dSmrg{
904af69d88dSmrg   _mesa_problem(ctx,
905af69d88dSmrg                 "Unsupported type (%s) / format (%s) "
906af69d88dSmrg                 "in _mesa_pack_rgba_span_from_ints",
907af69d88dSmrg                 _mesa_lookup_enum_by_nr(dstType),
908af69d88dSmrg                 _mesa_lookup_enum_by_nr(dstFormat));
909af69d88dSmrg}
910af69d88dSmrg
911af69d88dSmrgvoid
912af69d88dSmrg_mesa_pack_rgba_span_from_ints(struct gl_context *ctx, GLuint n, GLint rgba[][4],
913af69d88dSmrg                               GLenum dstFormat, GLenum dstType,
914af69d88dSmrg                               GLvoid *dstAddr)
915af69d88dSmrg{
916af69d88dSmrg   GLuint i;
917af69d88dSmrg
918af69d88dSmrg   switch(dstType) {
919af69d88dSmrg   case GL_UNSIGNED_INT:
920af69d88dSmrg      pack_uint_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
921af69d88dSmrg      break;
922af69d88dSmrg   case GL_INT:
923af69d88dSmrg      /* No conversion necessary. */
924af69d88dSmrg      pack_uint_from_uint_rgba(ctx, dstAddr, dstFormat, (GLuint (*)[4]) rgba, n);
925af69d88dSmrg      break;
926af69d88dSmrg   case GL_UNSIGNED_SHORT:
927af69d88dSmrg      pack_ushort_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
928af69d88dSmrg      break;
929af69d88dSmrg   case GL_SHORT:
930af69d88dSmrg      pack_short_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
931af69d88dSmrg      break;
932af69d88dSmrg   case GL_UNSIGNED_BYTE:
933af69d88dSmrg      pack_ubyte_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
934af69d88dSmrg      break;
935af69d88dSmrg   case GL_BYTE:
936af69d88dSmrg      pack_byte_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
937af69d88dSmrg      break;
938af69d88dSmrg   case GL_UNSIGNED_BYTE_3_3_2:
939af69d88dSmrg      if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
940af69d88dSmrg         GLubyte *dst = (GLubyte *) dstAddr;
941af69d88dSmrg         for (i=0;i<n;i++) {
942af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 7) << 5)
943af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 7) << 2)
944af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 3)     );
945af69d88dSmrg         }
946af69d88dSmrg      } else {
947af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
948af69d88dSmrg      }
949af69d88dSmrg      break;
950af69d88dSmrg   case GL_UNSIGNED_BYTE_2_3_3_REV:
951af69d88dSmrg      if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
952af69d88dSmrg         GLubyte *dst = (GLubyte *) dstAddr;
953af69d88dSmrg         for (i=0;i<n;i++) {
954af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 7)     )
955af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 7) << 3)
956af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 3) << 6);
957af69d88dSmrg         }
958af69d88dSmrg      } else {
959af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
960af69d88dSmrg      }
961af69d88dSmrg      break;
962af69d88dSmrg   case GL_UNSIGNED_SHORT_5_6_5:
963af69d88dSmrg      if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
964af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
965af69d88dSmrg         for (i=0;i<n;i++) {
966af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31) << 11)
967af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 63) <<  5)
968af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 31)      );
969af69d88dSmrg         }
970af69d88dSmrg      } else {
971af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
972af69d88dSmrg      }
973af69d88dSmrg      break;
974af69d88dSmrg   case GL_UNSIGNED_SHORT_5_6_5_REV:
975af69d88dSmrg      if ((dstFormat == GL_RGB) || (dstFormat == GL_RGB_INTEGER)) {
976af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
977af69d88dSmrg         for (i=0;i<n;i++) {
978af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31)      )
979af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 63) <<  5)
980af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 31) << 11);
981af69d88dSmrg         }
982af69d88dSmrg      } else {
983af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
984af69d88dSmrg      }
985af69d88dSmrg      break;
986af69d88dSmrg   case GL_UNSIGNED_SHORT_4_4_4_4:
987af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
988af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
989af69d88dSmrg         for (i=0;i<n;i++) {
990af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 15) << 12)
991af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
992af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 15) <<  4)
993af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0, 15)      );
994af69d88dSmrg         }
995af69d88dSmrg      }
996af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
997af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
998af69d88dSmrg         for (i=0;i<n;i++) {
999af69d88dSmrg            dst[i] = (CLAMP(rgba[i][BCOMP], 0, 15) << 12)
1000af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
1001af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 15) <<  4)
1002af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0, 15)      );
1003af69d88dSmrg         }
1004af69d88dSmrg      }
1005af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
1006af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1007af69d88dSmrg         for (i=0;i<n;i++) {
1008af69d88dSmrg            dst[i] = (CLAMP(rgba[i][ACOMP], 0, 15) << 12)
1009af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 15) <<  8)
1010af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
1011af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 15)      );
1012af69d88dSmrg         }
1013af69d88dSmrg      } else {
1014af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1015af69d88dSmrg      }
1016af69d88dSmrg      break;
1017af69d88dSmrg   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1018af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1019af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1020af69d88dSmrg         for (i=0;i<n;i++) {
1021af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 15)      )
1022af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
1023af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 15) <<  8)
1024af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0, 15) << 12);
1025af69d88dSmrg         }
1026af69d88dSmrg      }
1027af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1028af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1029af69d88dSmrg         for (i=0;i<n;i++) {
1030af69d88dSmrg            dst[i] = (CLAMP(rgba[i][BCOMP], 0, 15)      )
1031af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 15) <<  4)
1032af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 15) <<  8)
1033af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0, 15) << 12);
1034af69d88dSmrg         }
1035af69d88dSmrg      }
1036af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
1037af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1038af69d88dSmrg         for (i=0;i<n;i++) {
1039af69d88dSmrg            dst[i] = (CLAMP(rgba[i][ACOMP], 0, 15)      )
1040af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 15) <<  4)
1041af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 15) <<  8)
1042af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 15) << 12);
1043af69d88dSmrg         }
1044af69d88dSmrg      } else {
1045af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1046af69d88dSmrg      }
1047af69d88dSmrg      break;
1048af69d88dSmrg   case GL_UNSIGNED_SHORT_5_5_5_1:
1049af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1050af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1051af69d88dSmrg         for (i=0;i<n;i++) {
1052af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31) << 11)
1053af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 31) <<  6)
1054af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 31) <<  1)
1055af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0,  1)      );
1056af69d88dSmrg         }
1057af69d88dSmrg      }
1058af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1059af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1060af69d88dSmrg         for (i=0;i<n;i++) {
1061af69d88dSmrg            dst[i] = (CLAMP(rgba[i][BCOMP], 0, 31) << 11)
1062af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 31) <<  6)
1063af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 31) <<  1)
1064af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0,  1)      );
1065af69d88dSmrg         }
1066af69d88dSmrg      }
1067af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
1068af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1069af69d88dSmrg         for (i=0;i<n;i++) {
1070af69d88dSmrg            dst[i] = (CLAMP(rgba[i][ACOMP], 0, 31) << 11)
1071af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 31) <<  6)
1072af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 31) <<  1)
1073af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0,  1)      );
1074af69d88dSmrg         }
1075af69d88dSmrg      } else {
1076af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1077af69d88dSmrg      }
1078af69d88dSmrg      break;
1079af69d88dSmrg   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1080af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1081af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1082af69d88dSmrg         for (i=0;i<n;i++) {
1083af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 31)      )
1084af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 31) <<  5)
1085af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 31) << 10)
1086af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0,  1) << 15);
1087af69d88dSmrg         }
1088af69d88dSmrg      }
1089af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1090af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1091af69d88dSmrg         for (i=0;i<n;i++) {
1092af69d88dSmrg            dst[i] = (CLAMP(rgba[i][BCOMP], 0, 31)      )
1093af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 31) <<  5)
1094af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 31) << 10)
1095af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0,  1) << 15);
1096af69d88dSmrg         }
1097af69d88dSmrg      }
1098af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
1099af69d88dSmrg         GLushort *dst = (GLushort *) dstAddr;
1100af69d88dSmrg         for (i=0;i<n;i++) {
1101af69d88dSmrg            dst[i] = (CLAMP(rgba[i][ACOMP], 0, 31)      )
1102af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 31) <<  5)
1103af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 31) << 10)
1104af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0,  1) << 15);
1105af69d88dSmrg         }
1106af69d88dSmrg      } else {
1107af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1108af69d88dSmrg      }
1109af69d88dSmrg      break;
1110af69d88dSmrg   case GL_UNSIGNED_INT_8_8_8_8:
1111af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1112af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1113af69d88dSmrg         for (i=0;i<n;i++) {
1114af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 255) << 24)
1115af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
1116af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 255) <<  8)
1117af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0, 255)      );
1118af69d88dSmrg         }
1119af69d88dSmrg      }
1120af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1121af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1122af69d88dSmrg         for (i=0;i<n;i++) {
1123af69d88dSmrg            dst[i] = (CLAMP(rgba[i][BCOMP], 0, 255) << 24)
1124af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
1125af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 255) <<  8)
1126af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0, 255)      );
1127af69d88dSmrg         }
1128af69d88dSmrg      }
1129af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
1130af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1131af69d88dSmrg         for (i=0;i<n;i++) {
1132af69d88dSmrg            dst[i] = (CLAMP(rgba[i][ACOMP], 0, 255) << 24)
1133af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 255) << 16)
1134af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
1135af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 255)      );
1136af69d88dSmrg         }
1137af69d88dSmrg      } else {
1138af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1139af69d88dSmrg      }
1140af69d88dSmrg      break;
1141af69d88dSmrg   case GL_UNSIGNED_INT_8_8_8_8_REV:
1142af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1143af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1144af69d88dSmrg         for (i=0;i<n;i++) {
1145af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 255)      )
1146af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
1147af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 255) << 16)
1148af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0, 255) << 24);
1149af69d88dSmrg         }
1150af69d88dSmrg      }
1151af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1152af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1153af69d88dSmrg         for (i=0;i<n;i++) {
1154af69d88dSmrg            dst[i] = (CLAMP(rgba[i][BCOMP], 0, 255)      )
1155af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 255) <<  8)
1156af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 255) << 16)
1157af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0, 255) << 24);
1158af69d88dSmrg         }
1159af69d88dSmrg      }
1160af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
1161af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1162af69d88dSmrg         for (i=0;i<n;i++) {
1163af69d88dSmrg            dst[i] = (CLAMP(rgba[i][ACOMP], 0, 255)      )
1164af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 255) <<  8)
1165af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 255) << 16)
1166af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 255) << 24);
1167af69d88dSmrg         }
1168af69d88dSmrg      } else {
1169af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1170af69d88dSmrg      }
1171af69d88dSmrg      break;
1172af69d88dSmrg   case GL_UNSIGNED_INT_10_10_10_2:
1173af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1174af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1175af69d88dSmrg         for (i=0;i<n;i++) {
1176af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 1023) << 22)
1177af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 1023) << 12)
1178af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 1023) <<  2)
1179af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0,    3)      );
1180af69d88dSmrg         }
1181af69d88dSmrg      }
1182af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1183af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1184af69d88dSmrg         for (i=0;i<n;i++) {
1185af69d88dSmrg            dst[i] = (CLAMP(rgba[i][BCOMP], 0, 1023) << 22)
1186af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 1023) << 12)
1187af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 1023) <<  2)
1188af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0,    3)      );
1189af69d88dSmrg         }
1190af69d88dSmrg      }
1191af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
1192af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1193af69d88dSmrg         for (i=0;i<n;i++) {
1194af69d88dSmrg            dst[i] = (CLAMP(rgba[i][ACOMP], 0, 1023) << 22)
1195af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 1023) << 12)
1196af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 1023) <<  2)
1197af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0,    3)      );
1198af69d88dSmrg         }
1199af69d88dSmrg      } else {
1200af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1201af69d88dSmrg      }
1202af69d88dSmrg      break;
1203af69d88dSmrg   case GL_UNSIGNED_INT_2_10_10_10_REV:
1204af69d88dSmrg      if ((dstFormat == GL_RGBA) || (dstFormat == GL_RGBA_INTEGER_EXT)) {
1205af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1206af69d88dSmrg         for (i=0;i<n;i++) {
1207af69d88dSmrg            dst[i] = (CLAMP(rgba[i][RCOMP], 0, 1023)      )
1208af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 1023) << 10)
1209af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 1023) << 20)
1210af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0,    3) << 30);
1211af69d88dSmrg         }
1212af69d88dSmrg      }
1213af69d88dSmrg      else if ((dstFormat == GL_BGRA) || (dstFormat == GL_BGRA_INTEGER)) {
1214af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1215af69d88dSmrg         for (i=0;i<n;i++) {
1216af69d88dSmrg            dst[i] = (CLAMP(rgba[i][BCOMP], 0, 1023)      )
1217af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 1023) << 10)
1218af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0, 1023) << 20)
1219af69d88dSmrg                   | (CLAMP(rgba[i][ACOMP], 0,    3) << 30);
1220af69d88dSmrg         }
1221af69d88dSmrg      }
1222af69d88dSmrg      else if (dstFormat == GL_ABGR_EXT) {
1223af69d88dSmrg         GLuint *dst = (GLuint *) dstAddr;
1224af69d88dSmrg         for (i=0;i<n;i++) {
1225af69d88dSmrg            dst[i] = (CLAMP(rgba[i][ACOMP], 0, 1023)      )
1226af69d88dSmrg                   | (CLAMP(rgba[i][BCOMP], 0, 1023) << 10)
1227af69d88dSmrg                   | (CLAMP(rgba[i][GCOMP], 0, 1023) << 20)
1228af69d88dSmrg                   | (CLAMP(rgba[i][RCOMP], 0,    3) << 30);
1229af69d88dSmrg         }
1230af69d88dSmrg      } else {
1231af69d88dSmrg         _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1232af69d88dSmrg      }
1233af69d88dSmrg      break;
1234af69d88dSmrg   default:
1235af69d88dSmrg      _pack_rgba_span_from_ints_problem(ctx, dstFormat, dstType);
1236af69d88dSmrg      return;
1237af69d88dSmrg   }
1238af69d88dSmrg}
12393464ebd5Sriastradh
12403464ebd5Sriastradh
12413464ebd5Sriastradh/**
12423464ebd5Sriastradh * Used to pack an array [][4] of RGBA float colors as specified
12433464ebd5Sriastradh * by the dstFormat, dstType and dstPacking.  Used by glReadPixels.
12443464ebd5Sriastradh * Historically, the RGBA values were in [0,1] and rescaled to fit
12453464ebd5Sriastradh * into GLubytes, etc.  But with new integer formats, the RGBA values
12463464ebd5Sriastradh * may have any value and we don't always rescale when converting to
12473464ebd5Sriastradh * integers.
12483464ebd5Sriastradh *
12493464ebd5Sriastradh * Note: the rgba values will be modified by this function when any pixel
12503464ebd5Sriastradh * transfer ops are enabled.
12513464ebd5Sriastradh */
12523464ebd5Sriastradhvoid
12533464ebd5Sriastradh_mesa_pack_rgba_span_float(struct gl_context *ctx, GLuint n, GLfloat rgba[][4],
12543464ebd5Sriastradh                           GLenum dstFormat, GLenum dstType,
12553464ebd5Sriastradh                           GLvoid *dstAddr,
12563464ebd5Sriastradh                           const struct gl_pixelstore_attrib *dstPacking,
12573464ebd5Sriastradh                           GLbitfield transferOps)
12583464ebd5Sriastradh{
12593464ebd5Sriastradh   GLfloat *luminance;
12603464ebd5Sriastradh   const GLint comps = _mesa_components_in_format(dstFormat);
1261af69d88dSmrg   const GLboolean intDstFormat = _mesa_is_enum_format_integer(dstFormat);
12623464ebd5Sriastradh   GLuint i;
12633464ebd5Sriastradh
12643464ebd5Sriastradh   if (dstFormat == GL_LUMINANCE ||
12653464ebd5Sriastradh       dstFormat == GL_LUMINANCE_ALPHA ||
12663464ebd5Sriastradh       dstFormat == GL_LUMINANCE_INTEGER_EXT ||
12673464ebd5Sriastradh       dstFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT) {
1268af69d88dSmrg      luminance = malloc(n * sizeof(GLfloat));
12693464ebd5Sriastradh      if (!luminance) {
12703464ebd5Sriastradh         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
12713464ebd5Sriastradh         return;
12723464ebd5Sriastradh      }
12733464ebd5Sriastradh   }
12743464ebd5Sriastradh   else {
12753464ebd5Sriastradh      luminance = NULL;
12763464ebd5Sriastradh   }
12773464ebd5Sriastradh
1278af69d88dSmrg   /* EXT_texture_integer specifies no transfer ops on integer
1279af69d88dSmrg    * types in the resolved issues section. Just set them to 0
1280af69d88dSmrg    * for integer surfaces.
1281af69d88dSmrg    */
1282af69d88dSmrg   if (intDstFormat)
1283af69d88dSmrg      transferOps = 0;
1284af69d88dSmrg
12853464ebd5Sriastradh   if (transferOps) {
12863464ebd5Sriastradh      _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
12873464ebd5Sriastradh   }
12883464ebd5Sriastradh
12893464ebd5Sriastradh   /*
12903464ebd5Sriastradh    * Component clamping (besides clamping to [0,1] in
12913464ebd5Sriastradh    * _mesa_apply_rgba_transfer_ops()).
12923464ebd5Sriastradh    */
12933464ebd5Sriastradh   if (intDstFormat) {
12943464ebd5Sriastradh      /* clamping to dest type's min/max values */
12953464ebd5Sriastradh      GLfloat min, max;
12963464ebd5Sriastradh      if (get_type_min_max(dstType, &min, &max)) {
12973464ebd5Sriastradh         for (i = 0; i < n; i++) {
12983464ebd5Sriastradh            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], min, max);
12993464ebd5Sriastradh            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], min, max);
13003464ebd5Sriastradh            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], min, max);
13013464ebd5Sriastradh            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], min, max);
13023464ebd5Sriastradh         }
13033464ebd5Sriastradh      }
13043464ebd5Sriastradh   }
13053464ebd5Sriastradh   else if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
13063464ebd5Sriastradh      /* compute luminance values */
13073464ebd5Sriastradh      if (transferOps & IMAGE_CLAMP_BIT) {
13083464ebd5Sriastradh         for (i = 0; i < n; i++) {
13093464ebd5Sriastradh            GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
13103464ebd5Sriastradh            luminance[i] = CLAMP(sum, 0.0F, 1.0F);
13113464ebd5Sriastradh         }
13123464ebd5Sriastradh      }
13133464ebd5Sriastradh      else {
13143464ebd5Sriastradh         for (i = 0; i < n; i++) {
13153464ebd5Sriastradh            luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
13163464ebd5Sriastradh         }
13173464ebd5Sriastradh      }
13183464ebd5Sriastradh   }
13193464ebd5Sriastradh
13203464ebd5Sriastradh   /*
13213464ebd5Sriastradh    * Pack/store the pixels.  Ugh!  Lots of cases!!!
13223464ebd5Sriastradh    */
13233464ebd5Sriastradh   switch (dstType) {
13243464ebd5Sriastradh      case GL_UNSIGNED_BYTE:
13253464ebd5Sriastradh         {
13263464ebd5Sriastradh            GLubyte *dst = (GLubyte *) dstAddr;
13273464ebd5Sriastradh            switch (dstFormat) {
13283464ebd5Sriastradh               case GL_RED:
13293464ebd5Sriastradh                  for (i=0;i<n;i++)
13303464ebd5Sriastradh                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
13313464ebd5Sriastradh                  break;
13323464ebd5Sriastradh               case GL_GREEN:
13333464ebd5Sriastradh                  for (i=0;i<n;i++)
13343464ebd5Sriastradh                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
13353464ebd5Sriastradh                  break;
13363464ebd5Sriastradh               case GL_BLUE:
13373464ebd5Sriastradh                  for (i=0;i<n;i++)
13383464ebd5Sriastradh                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
13393464ebd5Sriastradh                  break;
13403464ebd5Sriastradh               case GL_ALPHA:
13413464ebd5Sriastradh                  for (i=0;i<n;i++)
13423464ebd5Sriastradh                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
13433464ebd5Sriastradh                  break;
13443464ebd5Sriastradh               case GL_LUMINANCE:
13453464ebd5Sriastradh                  for (i=0;i<n;i++)
13463464ebd5Sriastradh                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
13473464ebd5Sriastradh                  break;
13483464ebd5Sriastradh               case GL_LUMINANCE_ALPHA:
13493464ebd5Sriastradh                  for (i=0;i<n;i++) {
13503464ebd5Sriastradh                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
13513464ebd5Sriastradh                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
13523464ebd5Sriastradh                  }
13533464ebd5Sriastradh                  break;
13543464ebd5Sriastradh               case GL_RG:
13553464ebd5Sriastradh                  for (i=0;i<n;i++) {
13563464ebd5Sriastradh                     dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
13573464ebd5Sriastradh                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
13583464ebd5Sriastradh                  }
13593464ebd5Sriastradh                  break;
13603464ebd5Sriastradh               case GL_RGB:
13613464ebd5Sriastradh                  for (i=0;i<n;i++) {
13623464ebd5Sriastradh                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
13633464ebd5Sriastradh                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
13643464ebd5Sriastradh                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
13653464ebd5Sriastradh                  }
13663464ebd5Sriastradh                  break;
13673464ebd5Sriastradh               case GL_RGBA:
13683464ebd5Sriastradh                  for (i=0;i<n;i++) {
13693464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
13703464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
13713464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
13723464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
13733464ebd5Sriastradh                  }
13743464ebd5Sriastradh                  break;
13753464ebd5Sriastradh               case GL_BGR:
13763464ebd5Sriastradh                  for (i=0;i<n;i++) {
13773464ebd5Sriastradh                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
13783464ebd5Sriastradh                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
13793464ebd5Sriastradh                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
13803464ebd5Sriastradh                  }
13813464ebd5Sriastradh                  break;
13823464ebd5Sriastradh               case GL_BGRA:
13833464ebd5Sriastradh                  for (i=0;i<n;i++) {
13843464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
13853464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
13863464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
13873464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
13883464ebd5Sriastradh                  }
13893464ebd5Sriastradh                  break;
13903464ebd5Sriastradh               case GL_ABGR_EXT:
13913464ebd5Sriastradh                  for (i=0;i<n;i++) {
13923464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
13933464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
13943464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
13953464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
13963464ebd5Sriastradh                  }
13973464ebd5Sriastradh                  break;
13983464ebd5Sriastradh               case GL_RED_INTEGER_EXT:
13993464ebd5Sriastradh                  for (i=0;i<n;i++) {
14003464ebd5Sriastradh                     dst[i] = (GLubyte) rgba[i][RCOMP];
14013464ebd5Sriastradh                  }
14023464ebd5Sriastradh                  break;
14033464ebd5Sriastradh               case GL_GREEN_INTEGER_EXT:
14043464ebd5Sriastradh                  for (i=0;i<n;i++) {
14053464ebd5Sriastradh                     dst[i] = (GLubyte) rgba[i][GCOMP];
14063464ebd5Sriastradh                  }
14073464ebd5Sriastradh                  break;
14083464ebd5Sriastradh               case GL_BLUE_INTEGER_EXT:
14093464ebd5Sriastradh                  for (i=0;i<n;i++) {
14103464ebd5Sriastradh                     dst[i] = (GLubyte) rgba[i][BCOMP];
14113464ebd5Sriastradh                  }
14123464ebd5Sriastradh                  break;
14133464ebd5Sriastradh               case GL_ALPHA_INTEGER_EXT:
14143464ebd5Sriastradh                  for (i=0;i<n;i++) {
14153464ebd5Sriastradh                     dst[i] = (GLubyte) rgba[i][ACOMP];
14163464ebd5Sriastradh                  }
14173464ebd5Sriastradh                  break;
1418af69d88dSmrg               case GL_RG_INTEGER:
1419af69d88dSmrg                  for (i=0;i<n;i++) {
1420af69d88dSmrg                     dst[i*2+0] = (GLubyte) rgba[i][RCOMP];
1421af69d88dSmrg                     dst[i*2+1] = (GLubyte) rgba[i][GCOMP];
1422af69d88dSmrg                  }
1423af69d88dSmrg                  break;
14243464ebd5Sriastradh               case GL_RGB_INTEGER_EXT:
14253464ebd5Sriastradh                  for (i=0;i<n;i++) {
14263464ebd5Sriastradh                     dst[i*3+0] = (GLubyte) rgba[i][RCOMP];
14273464ebd5Sriastradh                     dst[i*3+1] = (GLubyte) rgba[i][GCOMP];
14283464ebd5Sriastradh                     dst[i*3+2] = (GLubyte) rgba[i][BCOMP];
14293464ebd5Sriastradh                  }
14303464ebd5Sriastradh                  break;
14313464ebd5Sriastradh               case GL_RGBA_INTEGER_EXT:
14323464ebd5Sriastradh                  for (i=0;i<n;i++) {
14333464ebd5Sriastradh                     dst[i*4+0] = (GLubyte) rgba[i][RCOMP];
14343464ebd5Sriastradh                     dst[i*4+1] = (GLubyte) rgba[i][GCOMP];
14353464ebd5Sriastradh                     dst[i*4+2] = (GLubyte) rgba[i][BCOMP];
14363464ebd5Sriastradh                     dst[i*4+3] = (GLubyte) rgba[i][ACOMP];
14373464ebd5Sriastradh                  }
14383464ebd5Sriastradh                  break;
14393464ebd5Sriastradh               case GL_BGR_INTEGER_EXT:
14403464ebd5Sriastradh                  for (i=0;i<n;i++) {
14413464ebd5Sriastradh                     dst[i*3+0] = (GLubyte) rgba[i][BCOMP];
14423464ebd5Sriastradh                     dst[i*3+1] = (GLubyte) rgba[i][GCOMP];
14433464ebd5Sriastradh                     dst[i*3+2] = (GLubyte) rgba[i][RCOMP];
14443464ebd5Sriastradh                  }
14453464ebd5Sriastradh                  break;
14463464ebd5Sriastradh               case GL_BGRA_INTEGER_EXT:
14473464ebd5Sriastradh                  for (i=0;i<n;i++) {
14483464ebd5Sriastradh                     dst[i*4+0] = (GLubyte) rgba[i][BCOMP];
14493464ebd5Sriastradh                     dst[i*4+1] = (GLubyte) rgba[i][GCOMP];
14503464ebd5Sriastradh                     dst[i*4+2] = (GLubyte) rgba[i][RCOMP];
14513464ebd5Sriastradh                     dst[i*4+3] = (GLubyte) rgba[i][ACOMP];
14523464ebd5Sriastradh                  }
14533464ebd5Sriastradh                  break;
14543464ebd5Sriastradh               case GL_LUMINANCE_INTEGER_EXT:
14553464ebd5Sriastradh                  for (i=0;i<n;i++) {
14563464ebd5Sriastradh                     dst[i*2+0] = (GLubyte) (rgba[i][RCOMP] +
14573464ebd5Sriastradh                                             rgba[i][GCOMP] +
14583464ebd5Sriastradh                                             rgba[i][BCOMP]);
14593464ebd5Sriastradh                     dst[i*2+1] = (GLubyte) rgba[i][ACOMP];
14603464ebd5Sriastradh                  }
14613464ebd5Sriastradh                  break;
14623464ebd5Sriastradh               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
14633464ebd5Sriastradh                  for (i=0;i<n;i++) {
14643464ebd5Sriastradh                     dst[i] = (GLubyte) (rgba[i][RCOMP] +
14653464ebd5Sriastradh                                         rgba[i][GCOMP] +
14663464ebd5Sriastradh                                         rgba[i][BCOMP]);
14673464ebd5Sriastradh                  }
14683464ebd5Sriastradh                  break;
14693464ebd5Sriastradh               default:
14703464ebd5Sriastradh                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
14713464ebd5Sriastradh            }
14723464ebd5Sriastradh         }
14733464ebd5Sriastradh         break;
14743464ebd5Sriastradh      case GL_BYTE:
14753464ebd5Sriastradh         {
14763464ebd5Sriastradh            GLbyte *dst = (GLbyte *) dstAddr;
14773464ebd5Sriastradh            switch (dstFormat) {
14783464ebd5Sriastradh               case GL_RED:
14793464ebd5Sriastradh                  for (i=0;i<n;i++)
1480af69d88dSmrg                     dst[i] = FLOAT_TO_BYTE_TEX(rgba[i][RCOMP]);
14813464ebd5Sriastradh                  break;
14823464ebd5Sriastradh               case GL_GREEN:
14833464ebd5Sriastradh                  for (i=0;i<n;i++)
1484af69d88dSmrg                     dst[i] = FLOAT_TO_BYTE_TEX(rgba[i][GCOMP]);
14853464ebd5Sriastradh                  break;
14863464ebd5Sriastradh               case GL_BLUE:
14873464ebd5Sriastradh                  for (i=0;i<n;i++)
1488af69d88dSmrg                     dst[i] = FLOAT_TO_BYTE_TEX(rgba[i][BCOMP]);
14893464ebd5Sriastradh                  break;
14903464ebd5Sriastradh               case GL_ALPHA:
14913464ebd5Sriastradh                  for (i=0;i<n;i++)
1492af69d88dSmrg                     dst[i] = FLOAT_TO_BYTE_TEX(rgba[i][ACOMP]);
14933464ebd5Sriastradh                  break;
14943464ebd5Sriastradh               case GL_LUMINANCE:
14953464ebd5Sriastradh                  for (i=0;i<n;i++)
1496af69d88dSmrg                     dst[i] = FLOAT_TO_BYTE_TEX(luminance[i]);
14973464ebd5Sriastradh                  break;
14983464ebd5Sriastradh               case GL_LUMINANCE_ALPHA:
14993464ebd5Sriastradh                  for (i=0;i<n;i++) {
1500af69d88dSmrg                     dst[i*2+0] = FLOAT_TO_BYTE_TEX(luminance[i]);
1501af69d88dSmrg                     dst[i*2+1] = FLOAT_TO_BYTE_TEX(rgba[i][ACOMP]);
15023464ebd5Sriastradh                  }
15033464ebd5Sriastradh                  break;
15043464ebd5Sriastradh               case GL_RG:
15053464ebd5Sriastradh                  for (i=0;i<n;i++) {
1506af69d88dSmrg                     dst[i*2+0] = FLOAT_TO_BYTE_TEX(rgba[i][RCOMP]);
1507af69d88dSmrg                     dst[i*2+1] = FLOAT_TO_BYTE_TEX(rgba[i][GCOMP]);
15083464ebd5Sriastradh                  }
15093464ebd5Sriastradh                  break;
15103464ebd5Sriastradh               case GL_RGB:
15113464ebd5Sriastradh                  for (i=0;i<n;i++) {
1512af69d88dSmrg                     dst[i*3+0] = FLOAT_TO_BYTE_TEX(rgba[i][RCOMP]);
1513af69d88dSmrg                     dst[i*3+1] = FLOAT_TO_BYTE_TEX(rgba[i][GCOMP]);
1514af69d88dSmrg                     dst[i*3+2] = FLOAT_TO_BYTE_TEX(rgba[i][BCOMP]);
15153464ebd5Sriastradh                  }
15163464ebd5Sriastradh                  break;
15173464ebd5Sriastradh               case GL_RGBA:
15183464ebd5Sriastradh                  for (i=0;i<n;i++) {
1519af69d88dSmrg                     dst[i*4+0] = FLOAT_TO_BYTE_TEX(rgba[i][RCOMP]);
1520af69d88dSmrg                     dst[i*4+1] = FLOAT_TO_BYTE_TEX(rgba[i][GCOMP]);
1521af69d88dSmrg                     dst[i*4+2] = FLOAT_TO_BYTE_TEX(rgba[i][BCOMP]);
1522af69d88dSmrg                     dst[i*4+3] = FLOAT_TO_BYTE_TEX(rgba[i][ACOMP]);
15233464ebd5Sriastradh                  }
15243464ebd5Sriastradh                  break;
15253464ebd5Sriastradh               case GL_BGR:
15263464ebd5Sriastradh                  for (i=0;i<n;i++) {
1527af69d88dSmrg                     dst[i*3+0] = FLOAT_TO_BYTE_TEX(rgba[i][BCOMP]);
1528af69d88dSmrg                     dst[i*3+1] = FLOAT_TO_BYTE_TEX(rgba[i][GCOMP]);
1529af69d88dSmrg                     dst[i*3+2] = FLOAT_TO_BYTE_TEX(rgba[i][RCOMP]);
15303464ebd5Sriastradh                  }
15313464ebd5Sriastradh                  break;
15323464ebd5Sriastradh               case GL_BGRA:
15333464ebd5Sriastradh                  for (i=0;i<n;i++) {
1534af69d88dSmrg                     dst[i*4+0] = FLOAT_TO_BYTE_TEX(rgba[i][BCOMP]);
1535af69d88dSmrg                     dst[i*4+1] = FLOAT_TO_BYTE_TEX(rgba[i][GCOMP]);
1536af69d88dSmrg                     dst[i*4+2] = FLOAT_TO_BYTE_TEX(rgba[i][RCOMP]);
1537af69d88dSmrg                     dst[i*4+3] = FLOAT_TO_BYTE_TEX(rgba[i][ACOMP]);
15383464ebd5Sriastradh                  }
15393464ebd5Sriastradh		  break;
15403464ebd5Sriastradh               case GL_ABGR_EXT:
15413464ebd5Sriastradh                  for (i=0;i<n;i++) {
1542af69d88dSmrg                     dst[i*4+0] = FLOAT_TO_BYTE_TEX(rgba[i][ACOMP]);
1543af69d88dSmrg                     dst[i*4+1] = FLOAT_TO_BYTE_TEX(rgba[i][BCOMP]);
1544af69d88dSmrg                     dst[i*4+2] = FLOAT_TO_BYTE_TEX(rgba[i][GCOMP]);
1545af69d88dSmrg                     dst[i*4+3] = FLOAT_TO_BYTE_TEX(rgba[i][RCOMP]);
15463464ebd5Sriastradh                  }
15473464ebd5Sriastradh                  break;
15483464ebd5Sriastradh               case GL_RED_INTEGER_EXT:
15493464ebd5Sriastradh                  for (i=0;i<n;i++) {
15503464ebd5Sriastradh                     dst[i] = (GLbyte) rgba[i][RCOMP];
15513464ebd5Sriastradh                  }
15523464ebd5Sriastradh                  break;
15533464ebd5Sriastradh               case GL_GREEN_INTEGER_EXT:
15543464ebd5Sriastradh                  for (i=0;i<n;i++) {
15553464ebd5Sriastradh                     dst[i] = (GLbyte) rgba[i][GCOMP];
15563464ebd5Sriastradh                  }
15573464ebd5Sriastradh                  break;
15583464ebd5Sriastradh               case GL_BLUE_INTEGER_EXT:
15593464ebd5Sriastradh                  for (i=0;i<n;i++) {
15603464ebd5Sriastradh                     dst[i] = (GLbyte) rgba[i][BCOMP];
15613464ebd5Sriastradh                  }
15623464ebd5Sriastradh                  break;
15633464ebd5Sriastradh               case GL_ALPHA_INTEGER_EXT:
15643464ebd5Sriastradh                  for (i=0;i<n;i++) {
15653464ebd5Sriastradh                     dst[i] = (GLbyte) rgba[i][ACOMP];
15663464ebd5Sriastradh                  }
15673464ebd5Sriastradh                  break;
1568af69d88dSmrg               case GL_RG_INTEGER:
1569af69d88dSmrg                  for (i=0;i<n;i++) {
1570af69d88dSmrg                     dst[i*2+0] = (GLbyte) rgba[i][RCOMP];
1571af69d88dSmrg                     dst[i*2+1] = (GLbyte) rgba[i][GCOMP];
1572af69d88dSmrg                  }
1573af69d88dSmrg                  break;
15743464ebd5Sriastradh               case GL_RGB_INTEGER_EXT:
15753464ebd5Sriastradh                  for (i=0;i<n;i++) {
15763464ebd5Sriastradh                     dst[i*3+0] = (GLbyte) rgba[i][RCOMP];
15773464ebd5Sriastradh                     dst[i*3+1] = (GLbyte) rgba[i][GCOMP];
15783464ebd5Sriastradh                     dst[i*3+2] = (GLbyte) rgba[i][BCOMP];
15793464ebd5Sriastradh                  }
15803464ebd5Sriastradh                  break;
15813464ebd5Sriastradh               case GL_RGBA_INTEGER_EXT:
15823464ebd5Sriastradh                  for (i=0;i<n;i++) {
15833464ebd5Sriastradh                     dst[i*4+0] = (GLbyte) rgba[i][RCOMP];
15843464ebd5Sriastradh                     dst[i*4+1] = (GLbyte) rgba[i][GCOMP];
15853464ebd5Sriastradh                     dst[i*4+2] = (GLbyte) rgba[i][BCOMP];
15863464ebd5Sriastradh                     dst[i*4+3] = (GLbyte) rgba[i][ACOMP];
15873464ebd5Sriastradh                  }
15883464ebd5Sriastradh                  break;
15893464ebd5Sriastradh               case GL_BGR_INTEGER_EXT:
15903464ebd5Sriastradh                  for (i=0;i<n;i++) {
15913464ebd5Sriastradh                     dst[i*3+0] = (GLbyte) rgba[i][BCOMP];
15923464ebd5Sriastradh                     dst[i*3+1] = (GLbyte) rgba[i][GCOMP];
15933464ebd5Sriastradh                     dst[i*3+2] = (GLbyte) rgba[i][RCOMP];
15943464ebd5Sriastradh                  }
15953464ebd5Sriastradh                  break;
15963464ebd5Sriastradh               case GL_BGRA_INTEGER_EXT:
15973464ebd5Sriastradh                  for (i=0;i<n;i++) {
15983464ebd5Sriastradh                     dst[i*4+0] = (GLbyte) rgba[i][BCOMP];
15993464ebd5Sriastradh                     dst[i*4+1] = (GLbyte) rgba[i][GCOMP];
16003464ebd5Sriastradh                     dst[i*4+2] = (GLbyte) rgba[i][RCOMP];
16013464ebd5Sriastradh                     dst[i*4+3] = (GLbyte) rgba[i][ACOMP];
16023464ebd5Sriastradh                  }
16033464ebd5Sriastradh                  break;
16043464ebd5Sriastradh               case GL_LUMINANCE_INTEGER_EXT:
16053464ebd5Sriastradh                  for (i=0;i<n;i++) {
16063464ebd5Sriastradh                     dst[i*2+0] = (GLbyte) (rgba[i][RCOMP] +
16073464ebd5Sriastradh                                            rgba[i][GCOMP] +
16083464ebd5Sriastradh                                            rgba[i][BCOMP]);
16093464ebd5Sriastradh                     dst[i*2+1] = (GLbyte) rgba[i][ACOMP];
16103464ebd5Sriastradh                  }
16113464ebd5Sriastradh                  break;
16123464ebd5Sriastradh               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
16133464ebd5Sriastradh                  for (i=0;i<n;i++) {
16143464ebd5Sriastradh                     dst[i] = (GLbyte) (rgba[i][RCOMP] +
16153464ebd5Sriastradh                                        rgba[i][GCOMP] +
16163464ebd5Sriastradh                                        rgba[i][BCOMP]);
16173464ebd5Sriastradh                  }
16183464ebd5Sriastradh                  break;
16193464ebd5Sriastradh               default:
16203464ebd5Sriastradh                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
16213464ebd5Sriastradh            }
16223464ebd5Sriastradh         }
16233464ebd5Sriastradh         break;
16243464ebd5Sriastradh      case GL_UNSIGNED_SHORT:
16253464ebd5Sriastradh         {
16263464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
16273464ebd5Sriastradh            switch (dstFormat) {
16283464ebd5Sriastradh               case GL_RED:
16293464ebd5Sriastradh                  for (i=0;i<n;i++)
16303464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
16313464ebd5Sriastradh                  break;
16323464ebd5Sriastradh               case GL_GREEN:
16333464ebd5Sriastradh                  for (i=0;i<n;i++)
16343464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
16353464ebd5Sriastradh                  break;
16363464ebd5Sriastradh               case GL_BLUE:
16373464ebd5Sriastradh                  for (i=0;i<n;i++)
16383464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
16393464ebd5Sriastradh                  break;
16403464ebd5Sriastradh               case GL_ALPHA:
16413464ebd5Sriastradh                  for (i=0;i<n;i++)
16423464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
16433464ebd5Sriastradh                  break;
16443464ebd5Sriastradh               case GL_LUMINANCE:
16453464ebd5Sriastradh                  for (i=0;i<n;i++)
16463464ebd5Sriastradh                     UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
16473464ebd5Sriastradh                  break;
16483464ebd5Sriastradh               case GL_LUMINANCE_ALPHA:
16493464ebd5Sriastradh                  for (i=0;i<n;i++) {
16503464ebd5Sriastradh                     UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
16513464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
16523464ebd5Sriastradh                  }
16533464ebd5Sriastradh                  break;
16543464ebd5Sriastradh               case GL_RG:
16553464ebd5Sriastradh                  for (i=0;i<n;i++) {
16563464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+0], rgba[i][RCOMP]);
16573464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][GCOMP]);
16583464ebd5Sriastradh                  }
16593464ebd5Sriastradh                  break;
16603464ebd5Sriastradh               case GL_RGB:
16613464ebd5Sriastradh                  for (i=0;i<n;i++) {
16623464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
16633464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
16643464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
16653464ebd5Sriastradh                  }
16663464ebd5Sriastradh                  break;
16673464ebd5Sriastradh               case GL_RGBA:
16683464ebd5Sriastradh                  for (i=0;i<n;i++) {
16693464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
16703464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
16713464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
16723464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
16733464ebd5Sriastradh                  }
16743464ebd5Sriastradh                  break;
16753464ebd5Sriastradh               case GL_BGR:
16763464ebd5Sriastradh                  for (i=0;i<n;i++) {
16773464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
16783464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
16793464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
16803464ebd5Sriastradh                  }
16813464ebd5Sriastradh                  break;
16823464ebd5Sriastradh               case GL_BGRA:
16833464ebd5Sriastradh                  for (i=0;i<n;i++) {
16843464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
16853464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
16863464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
16873464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
16883464ebd5Sriastradh                  }
16893464ebd5Sriastradh                  break;
16903464ebd5Sriastradh               case GL_ABGR_EXT:
16913464ebd5Sriastradh                  for (i=0;i<n;i++) {
16923464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
16933464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
16943464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
16953464ebd5Sriastradh                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
16963464ebd5Sriastradh                  }
16973464ebd5Sriastradh                  break;
16983464ebd5Sriastradh               case GL_RED_INTEGER_EXT:
16993464ebd5Sriastradh                  for (i=0;i<n;i++) {
17003464ebd5Sriastradh                     dst[i] = (GLushort) rgba[i][RCOMP];
17013464ebd5Sriastradh                  }
17023464ebd5Sriastradh                  break;
17033464ebd5Sriastradh               case GL_GREEN_INTEGER_EXT:
17043464ebd5Sriastradh                  for (i=0;i<n;i++) {
17053464ebd5Sriastradh                     dst[i] = (GLushort) rgba[i][GCOMP];
17063464ebd5Sriastradh                  }
17073464ebd5Sriastradh                  break;
17083464ebd5Sriastradh               case GL_BLUE_INTEGER_EXT:
17093464ebd5Sriastradh                  for (i=0;i<n;i++) {
17103464ebd5Sriastradh                     dst[i] = (GLushort) rgba[i][BCOMP];
17113464ebd5Sriastradh                  }
17123464ebd5Sriastradh                  break;
17133464ebd5Sriastradh               case GL_ALPHA_INTEGER_EXT:
17143464ebd5Sriastradh                  for (i=0;i<n;i++) {
17153464ebd5Sriastradh                     dst[i] = (GLushort) rgba[i][ACOMP];
17163464ebd5Sriastradh                  }
17173464ebd5Sriastradh                  break;
1718af69d88dSmrg               case GL_RG_INTEGER:
1719af69d88dSmrg                  for (i=0;i<n;i++) {
1720af69d88dSmrg                     dst[i*2+0] = (GLushort) rgba[i][RCOMP];
1721af69d88dSmrg                     dst[i*2+1] = (GLushort) rgba[i][GCOMP];
1722af69d88dSmrg                  }
1723af69d88dSmrg                  break;
17243464ebd5Sriastradh               case GL_RGB_INTEGER_EXT:
17253464ebd5Sriastradh                  for (i=0;i<n;i++) {
17263464ebd5Sriastradh                     dst[i*3+0] = (GLushort) rgba[i][RCOMP];
17273464ebd5Sriastradh                     dst[i*3+1] = (GLushort) rgba[i][GCOMP];
17283464ebd5Sriastradh                     dst[i*3+2] = (GLushort) rgba[i][BCOMP];
17293464ebd5Sriastradh                  }
17303464ebd5Sriastradh                  break;
17313464ebd5Sriastradh               case GL_RGBA_INTEGER_EXT:
17323464ebd5Sriastradh                  for (i=0;i<n;i++) {
17333464ebd5Sriastradh                     dst[i*4+0] = (GLushort) rgba[i][RCOMP];
17343464ebd5Sriastradh                     dst[i*4+1] = (GLushort) rgba[i][GCOMP];
17353464ebd5Sriastradh                     dst[i*4+2] = (GLushort) rgba[i][BCOMP];
17363464ebd5Sriastradh                     dst[i*4+3] = (GLushort) rgba[i][ACOMP];
17373464ebd5Sriastradh                  }
17383464ebd5Sriastradh                  break;
17393464ebd5Sriastradh               case GL_BGR_INTEGER_EXT:
17403464ebd5Sriastradh                  for (i=0;i<n;i++) {
17413464ebd5Sriastradh                     dst[i*3+0] = (GLushort) rgba[i][BCOMP];
17423464ebd5Sriastradh                     dst[i*3+1] = (GLushort) rgba[i][GCOMP];
17433464ebd5Sriastradh                     dst[i*3+2] = (GLushort) rgba[i][RCOMP];
17443464ebd5Sriastradh                  }
17453464ebd5Sriastradh                  break;
17463464ebd5Sriastradh               case GL_BGRA_INTEGER_EXT:
17473464ebd5Sriastradh                  for (i=0;i<n;i++) {
17483464ebd5Sriastradh                     dst[i*4+0] = (GLushort) rgba[i][BCOMP];
17493464ebd5Sriastradh                     dst[i*4+1] = (GLushort) rgba[i][GCOMP];
17503464ebd5Sriastradh                     dst[i*4+2] = (GLushort) rgba[i][RCOMP];
17513464ebd5Sriastradh                     dst[i*4+3] = (GLushort) rgba[i][ACOMP];
17523464ebd5Sriastradh                  }
17533464ebd5Sriastradh                  break;
17543464ebd5Sriastradh               case GL_LUMINANCE_INTEGER_EXT:
17553464ebd5Sriastradh                  for (i=0;i<n;i++) {
17563464ebd5Sriastradh                     dst[i*2+0] = (GLushort) (rgba[i][RCOMP] +
17573464ebd5Sriastradh                                              rgba[i][GCOMP] +
17583464ebd5Sriastradh                                              rgba[i][BCOMP]);
17593464ebd5Sriastradh                     dst[i*2+1] = (GLushort) rgba[i][ACOMP];
17603464ebd5Sriastradh                  }
17613464ebd5Sriastradh                  break;
17623464ebd5Sriastradh               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
17633464ebd5Sriastradh                  for (i=0;i<n;i++) {
17643464ebd5Sriastradh                     dst[i] = (GLushort) (rgba[i][RCOMP] +
17653464ebd5Sriastradh                                          rgba[i][GCOMP] +
17663464ebd5Sriastradh                                          rgba[i][BCOMP]);
17673464ebd5Sriastradh                  }
17683464ebd5Sriastradh                  break;
17693464ebd5Sriastradh               default:
17703464ebd5Sriastradh                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
17713464ebd5Sriastradh            }
17723464ebd5Sriastradh         }
17733464ebd5Sriastradh         break;
17743464ebd5Sriastradh      case GL_SHORT:
17753464ebd5Sriastradh         {
17763464ebd5Sriastradh            GLshort *dst = (GLshort *) dstAddr;
17773464ebd5Sriastradh            switch (dstFormat) {
17783464ebd5Sriastradh               case GL_RED:
17793464ebd5Sriastradh                  for (i=0;i<n;i++)
1780af69d88dSmrg                     dst[i] = FLOAT_TO_SHORT_TEX(rgba[i][RCOMP]);
17813464ebd5Sriastradh                  break;
17823464ebd5Sriastradh               case GL_GREEN:
17833464ebd5Sriastradh                  for (i=0;i<n;i++)
1784af69d88dSmrg                     dst[i] = FLOAT_TO_SHORT_TEX(rgba[i][GCOMP]);
17853464ebd5Sriastradh                  break;
17863464ebd5Sriastradh               case GL_BLUE:
17873464ebd5Sriastradh                  for (i=0;i<n;i++)
1788af69d88dSmrg                     dst[i] = FLOAT_TO_SHORT_TEX(rgba[i][BCOMP]);
17893464ebd5Sriastradh                  break;
17903464ebd5Sriastradh               case GL_ALPHA:
17913464ebd5Sriastradh                  for (i=0;i<n;i++)
1792af69d88dSmrg                     dst[i] = FLOAT_TO_SHORT_TEX(rgba[i][ACOMP]);
17933464ebd5Sriastradh                  break;
17943464ebd5Sriastradh               case GL_LUMINANCE:
17953464ebd5Sriastradh                  for (i=0;i<n;i++)
1796af69d88dSmrg                     dst[i] = FLOAT_TO_SHORT_TEX(luminance[i]);
17973464ebd5Sriastradh                  break;
17983464ebd5Sriastradh               case GL_LUMINANCE_ALPHA:
17993464ebd5Sriastradh                  for (i=0;i<n;i++) {
1800af69d88dSmrg                     dst[i*2+0] = FLOAT_TO_SHORT_TEX(luminance[i]);
1801af69d88dSmrg                     dst[i*2+1] = FLOAT_TO_SHORT_TEX(rgba[i][ACOMP]);
18023464ebd5Sriastradh                  }
18033464ebd5Sriastradh                  break;
18043464ebd5Sriastradh               case GL_RG:
18053464ebd5Sriastradh                  for (i=0;i<n;i++) {
1806af69d88dSmrg                     dst[i*2+0] = FLOAT_TO_SHORT_TEX(rgba[i][RCOMP]);
1807af69d88dSmrg                     dst[i*2+1] = FLOAT_TO_SHORT_TEX(rgba[i][GCOMP]);
18083464ebd5Sriastradh                  }
18093464ebd5Sriastradh                  break;
18103464ebd5Sriastradh               case GL_RGB:
18113464ebd5Sriastradh                  for (i=0;i<n;i++) {
1812af69d88dSmrg                     dst[i*3+0] = FLOAT_TO_SHORT_TEX(rgba[i][RCOMP]);
1813af69d88dSmrg                     dst[i*3+1] = FLOAT_TO_SHORT_TEX(rgba[i][GCOMP]);
1814af69d88dSmrg                     dst[i*3+2] = FLOAT_TO_SHORT_TEX(rgba[i][BCOMP]);
18153464ebd5Sriastradh                  }
18163464ebd5Sriastradh                  break;
18173464ebd5Sriastradh               case GL_RGBA:
18183464ebd5Sriastradh                  for (i=0;i<n;i++) {
1819af69d88dSmrg                     dst[i*4+0] = FLOAT_TO_SHORT_TEX(rgba[i][RCOMP]);
1820af69d88dSmrg                     dst[i*4+1] = FLOAT_TO_SHORT_TEX(rgba[i][GCOMP]);
1821af69d88dSmrg                     dst[i*4+2] = FLOAT_TO_SHORT_TEX(rgba[i][BCOMP]);
1822af69d88dSmrg                     dst[i*4+3] = FLOAT_TO_SHORT_TEX(rgba[i][ACOMP]);
18233464ebd5Sriastradh                  }
18243464ebd5Sriastradh                  break;
18253464ebd5Sriastradh               case GL_BGR:
18263464ebd5Sriastradh                  for (i=0;i<n;i++) {
1827af69d88dSmrg                     dst[i*3+0] = FLOAT_TO_SHORT_TEX(rgba[i][BCOMP]);
1828af69d88dSmrg                     dst[i*3+1] = FLOAT_TO_SHORT_TEX(rgba[i][GCOMP]);
1829af69d88dSmrg                     dst[i*3+2] = FLOAT_TO_SHORT_TEX(rgba[i][RCOMP]);
18303464ebd5Sriastradh                  }
18313464ebd5Sriastradh                  break;
18323464ebd5Sriastradh               case GL_BGRA:
18333464ebd5Sriastradh                  for (i=0;i<n;i++) {
1834af69d88dSmrg                     dst[i*4+0] = FLOAT_TO_SHORT_TEX(rgba[i][BCOMP]);
1835af69d88dSmrg                     dst[i*4+1] = FLOAT_TO_SHORT_TEX(rgba[i][GCOMP]);
1836af69d88dSmrg                     dst[i*4+2] = FLOAT_TO_SHORT_TEX(rgba[i][RCOMP]);
1837af69d88dSmrg                     dst[i*4+3] = FLOAT_TO_SHORT_TEX(rgba[i][ACOMP]);
18383464ebd5Sriastradh                  }
18393464ebd5Sriastradh		  break;
18403464ebd5Sriastradh               case GL_ABGR_EXT:
18413464ebd5Sriastradh                  for (i=0;i<n;i++) {
1842af69d88dSmrg                     dst[i*4+0] = FLOAT_TO_SHORT_TEX(rgba[i][ACOMP]);
1843af69d88dSmrg                     dst[i*4+1] = FLOAT_TO_SHORT_TEX(rgba[i][BCOMP]);
1844af69d88dSmrg                     dst[i*4+2] = FLOAT_TO_SHORT_TEX(rgba[i][GCOMP]);
1845af69d88dSmrg                     dst[i*4+3] = FLOAT_TO_SHORT_TEX(rgba[i][RCOMP]);
18463464ebd5Sriastradh                  }
18473464ebd5Sriastradh                  break;
18483464ebd5Sriastradh               case GL_RED_INTEGER_EXT:
18493464ebd5Sriastradh                  for (i=0;i<n;i++) {
18503464ebd5Sriastradh                     dst[i] = (GLshort) rgba[i][RCOMP];
18513464ebd5Sriastradh                  }
18523464ebd5Sriastradh                  break;
18533464ebd5Sriastradh               case GL_GREEN_INTEGER_EXT:
18543464ebd5Sriastradh                  for (i=0;i<n;i++) {
18553464ebd5Sriastradh                     dst[i] = (GLshort) rgba[i][GCOMP];
18563464ebd5Sriastradh                  }
18573464ebd5Sriastradh                  break;
18583464ebd5Sriastradh               case GL_BLUE_INTEGER_EXT:
18593464ebd5Sriastradh                  for (i=0;i<n;i++) {
18603464ebd5Sriastradh                     dst[i] = (GLshort) rgba[i][BCOMP];
18613464ebd5Sriastradh                  }
18623464ebd5Sriastradh                  break;
18633464ebd5Sriastradh               case GL_ALPHA_INTEGER_EXT:
18643464ebd5Sriastradh                  for (i=0;i<n;i++) {
18653464ebd5Sriastradh                     dst[i] = (GLshort) rgba[i][ACOMP];
18663464ebd5Sriastradh                  }
18673464ebd5Sriastradh                  break;
1868af69d88dSmrg               case GL_RG_INTEGER:
1869af69d88dSmrg                  for (i=0;i<n;i++) {
1870af69d88dSmrg                     dst[i*2+0] = (GLshort) rgba[i][RCOMP];
1871af69d88dSmrg                     dst[i*2+1] = (GLshort) rgba[i][GCOMP];
1872af69d88dSmrg                  }
1873af69d88dSmrg                  break;
18743464ebd5Sriastradh               case GL_RGB_INTEGER_EXT:
18753464ebd5Sriastradh                  for (i=0;i<n;i++) {
18763464ebd5Sriastradh                     dst[i*3+0] = (GLshort) rgba[i][RCOMP];
18773464ebd5Sriastradh                     dst[i*3+1] = (GLshort) rgba[i][GCOMP];
18783464ebd5Sriastradh                     dst[i*3+2] = (GLshort) rgba[i][BCOMP];
18793464ebd5Sriastradh                  }
18803464ebd5Sriastradh                  break;
18813464ebd5Sriastradh               case GL_RGBA_INTEGER_EXT:
18823464ebd5Sriastradh                  for (i=0;i<n;i++) {
18833464ebd5Sriastradh                     dst[i*4+0] = (GLshort) rgba[i][RCOMP];
18843464ebd5Sriastradh                     dst[i*4+1] = (GLshort) rgba[i][GCOMP];
18853464ebd5Sriastradh                     dst[i*4+2] = (GLshort) rgba[i][BCOMP];
18863464ebd5Sriastradh                     dst[i*4+3] = (GLshort) rgba[i][ACOMP];
18873464ebd5Sriastradh                  }
18883464ebd5Sriastradh                  break;
18893464ebd5Sriastradh               case GL_BGR_INTEGER_EXT:
18903464ebd5Sriastradh                  for (i=0;i<n;i++) {
18913464ebd5Sriastradh                     dst[i*3+0] = (GLshort) rgba[i][BCOMP];
18923464ebd5Sriastradh                     dst[i*3+1] = (GLshort) rgba[i][GCOMP];
18933464ebd5Sriastradh                     dst[i*3+2] = (GLshort) rgba[i][RCOMP];
18943464ebd5Sriastradh                  }
18953464ebd5Sriastradh                  break;
18963464ebd5Sriastradh               case GL_BGRA_INTEGER_EXT:
18973464ebd5Sriastradh                  for (i=0;i<n;i++) {
18983464ebd5Sriastradh                     dst[i*4+0] = (GLshort) rgba[i][BCOMP];
18993464ebd5Sriastradh                     dst[i*4+1] = (GLshort) rgba[i][GCOMP];
19003464ebd5Sriastradh                     dst[i*4+2] = (GLshort) rgba[i][RCOMP];
19013464ebd5Sriastradh                     dst[i*4+3] = (GLshort) rgba[i][ACOMP];
19023464ebd5Sriastradh                  }
19033464ebd5Sriastradh                  break;
19043464ebd5Sriastradh               case GL_LUMINANCE_INTEGER_EXT:
19053464ebd5Sriastradh                  for (i=0;i<n;i++) {
19063464ebd5Sriastradh                     dst[i*2+0] = (GLshort) (rgba[i][RCOMP] +
19073464ebd5Sriastradh                                             rgba[i][GCOMP] +
19083464ebd5Sriastradh                                             rgba[i][BCOMP]);
19093464ebd5Sriastradh                     dst[i*2+1] = (GLshort) rgba[i][ACOMP];
19103464ebd5Sriastradh                  }
19113464ebd5Sriastradh                  break;
19123464ebd5Sriastradh               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
19133464ebd5Sriastradh                  for (i=0;i<n;i++) {
19143464ebd5Sriastradh                     dst[i] = (GLshort) (rgba[i][RCOMP] +
19153464ebd5Sriastradh                                         rgba[i][GCOMP] +
19163464ebd5Sriastradh                                         rgba[i][BCOMP]);
19173464ebd5Sriastradh                  }
19183464ebd5Sriastradh                  break;
19193464ebd5Sriastradh               default:
19203464ebd5Sriastradh                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
19213464ebd5Sriastradh            }
19223464ebd5Sriastradh         }
19233464ebd5Sriastradh         break;
19243464ebd5Sriastradh      case GL_UNSIGNED_INT:
19253464ebd5Sriastradh         {
19263464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
19273464ebd5Sriastradh            switch (dstFormat) {
19283464ebd5Sriastradh               case GL_RED:
19293464ebd5Sriastradh                  for (i=0;i<n;i++)
19303464ebd5Sriastradh                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
19313464ebd5Sriastradh                  break;
19323464ebd5Sriastradh               case GL_GREEN:
19333464ebd5Sriastradh                  for (i=0;i<n;i++)
19343464ebd5Sriastradh                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
19353464ebd5Sriastradh                  break;
19363464ebd5Sriastradh               case GL_BLUE:
19373464ebd5Sriastradh                  for (i=0;i<n;i++)
19383464ebd5Sriastradh                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
19393464ebd5Sriastradh                  break;
19403464ebd5Sriastradh               case GL_ALPHA:
19413464ebd5Sriastradh                  for (i=0;i<n;i++)
19423464ebd5Sriastradh                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
19433464ebd5Sriastradh                  break;
19443464ebd5Sriastradh               case GL_LUMINANCE:
19453464ebd5Sriastradh                  for (i=0;i<n;i++)
19463464ebd5Sriastradh                     dst[i] = FLOAT_TO_UINT(luminance[i]);
19473464ebd5Sriastradh                  break;
19483464ebd5Sriastradh               case GL_LUMINANCE_ALPHA:
19493464ebd5Sriastradh                  for (i=0;i<n;i++) {
19503464ebd5Sriastradh                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
19513464ebd5Sriastradh                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
19523464ebd5Sriastradh                  }
19533464ebd5Sriastradh                  break;
19543464ebd5Sriastradh               case GL_RG:
19553464ebd5Sriastradh                  for (i=0;i<n;i++) {
19563464ebd5Sriastradh                     dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
19573464ebd5Sriastradh                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
19583464ebd5Sriastradh                  }
19593464ebd5Sriastradh                  break;
19603464ebd5Sriastradh               case GL_RGB:
19613464ebd5Sriastradh                  for (i=0;i<n;i++) {
19623464ebd5Sriastradh                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
19633464ebd5Sriastradh                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
19643464ebd5Sriastradh                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
19653464ebd5Sriastradh                  }
19663464ebd5Sriastradh                  break;
19673464ebd5Sriastradh               case GL_RGBA:
19683464ebd5Sriastradh                  for (i=0;i<n;i++) {
19693464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
19703464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
19713464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
19723464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
19733464ebd5Sriastradh                  }
19743464ebd5Sriastradh                  break;
19753464ebd5Sriastradh               case GL_BGR:
19763464ebd5Sriastradh                  for (i=0;i<n;i++) {
19773464ebd5Sriastradh                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
19783464ebd5Sriastradh                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
19793464ebd5Sriastradh                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
19803464ebd5Sriastradh                  }
19813464ebd5Sriastradh                  break;
19823464ebd5Sriastradh               case GL_BGRA:
19833464ebd5Sriastradh                  for (i=0;i<n;i++) {
19843464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
19853464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
19863464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
19873464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
19883464ebd5Sriastradh                  }
19893464ebd5Sriastradh                  break;
19903464ebd5Sriastradh               case GL_ABGR_EXT:
19913464ebd5Sriastradh                  for (i=0;i<n;i++) {
19923464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
19933464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
19943464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
19953464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
19963464ebd5Sriastradh                  }
19973464ebd5Sriastradh                  break;
19983464ebd5Sriastradh               case GL_RED_INTEGER_EXT:
19993464ebd5Sriastradh                  for (i=0;i<n;i++) {
20003464ebd5Sriastradh                     dst[i] = (GLuint) rgba[i][RCOMP];
20013464ebd5Sriastradh                  }
20023464ebd5Sriastradh                  break;
20033464ebd5Sriastradh               case GL_GREEN_INTEGER_EXT:
20043464ebd5Sriastradh                  for (i=0;i<n;i++) {
20053464ebd5Sriastradh                     dst[i] = (GLuint) rgba[i][GCOMP];
20063464ebd5Sriastradh                  }
20073464ebd5Sriastradh                  break;
20083464ebd5Sriastradh               case GL_BLUE_INTEGER_EXT:
20093464ebd5Sriastradh                  for (i=0;i<n;i++) {
20103464ebd5Sriastradh                     dst[i] = (GLuint) rgba[i][BCOMP];
20113464ebd5Sriastradh                  }
20123464ebd5Sriastradh                  break;
20133464ebd5Sriastradh               case GL_ALPHA_INTEGER_EXT:
20143464ebd5Sriastradh                  for (i=0;i<n;i++) {
20153464ebd5Sriastradh                     dst[i] = (GLuint) rgba[i][ACOMP];
20163464ebd5Sriastradh                  }
20173464ebd5Sriastradh                  break;
2018af69d88dSmrg               case GL_RG_INTEGER:
2019af69d88dSmrg                  for (i=0;i<n;i++) {
2020af69d88dSmrg                     dst[i*2+0] = (GLuint) rgba[i][RCOMP];
2021af69d88dSmrg                     dst[i*2+1] = (GLuint) rgba[i][GCOMP];
2022af69d88dSmrg                  }
2023af69d88dSmrg                  break;
20243464ebd5Sriastradh               case GL_RGB_INTEGER_EXT:
20253464ebd5Sriastradh                  for (i=0;i<n;i++) {
20263464ebd5Sriastradh                     dst[i*3+0] = (GLuint) rgba[i][RCOMP];
20273464ebd5Sriastradh                     dst[i*3+1] = (GLuint) rgba[i][GCOMP];
20283464ebd5Sriastradh                     dst[i*3+2] = (GLuint) rgba[i][BCOMP];
20293464ebd5Sriastradh                  }
20303464ebd5Sriastradh                  break;
20313464ebd5Sriastradh               case GL_RGBA_INTEGER_EXT:
20323464ebd5Sriastradh                  for (i=0;i<n;i++) {
20333464ebd5Sriastradh                     dst[i*4+0] = (GLuint) rgba[i][RCOMP];
20343464ebd5Sriastradh                     dst[i*4+1] = (GLuint) rgba[i][GCOMP];
20353464ebd5Sriastradh                     dst[i*4+2] = (GLuint) rgba[i][BCOMP];
20363464ebd5Sriastradh                     dst[i*4+3] = (GLuint) rgba[i][ACOMP];
20373464ebd5Sriastradh                  }
20383464ebd5Sriastradh                  break;
20393464ebd5Sriastradh               case GL_BGR_INTEGER_EXT:
20403464ebd5Sriastradh                  for (i=0;i<n;i++) {
20413464ebd5Sriastradh                     dst[i*3+0] = (GLuint) rgba[i][BCOMP];
20423464ebd5Sriastradh                     dst[i*3+1] = (GLuint) rgba[i][GCOMP];
20433464ebd5Sriastradh                     dst[i*3+2] = (GLuint) rgba[i][RCOMP];
20443464ebd5Sriastradh                  }
20453464ebd5Sriastradh                  break;
20463464ebd5Sriastradh               case GL_BGRA_INTEGER_EXT:
20473464ebd5Sriastradh                  for (i=0;i<n;i++) {
20483464ebd5Sriastradh                     dst[i*4+0] = (GLuint) rgba[i][BCOMP];
20493464ebd5Sriastradh                     dst[i*4+1] = (GLuint) rgba[i][GCOMP];
20503464ebd5Sriastradh                     dst[i*4+2] = (GLuint) rgba[i][RCOMP];
20513464ebd5Sriastradh                     dst[i*4+3] = (GLuint) rgba[i][ACOMP];
20523464ebd5Sriastradh                  }
20533464ebd5Sriastradh                  break;
20543464ebd5Sriastradh               case GL_LUMINANCE_INTEGER_EXT:
20553464ebd5Sriastradh                  for (i=0;i<n;i++) {
20563464ebd5Sriastradh                     dst[i*2+0] = (GLuint) (rgba[i][RCOMP] +
20573464ebd5Sriastradh                                            rgba[i][GCOMP] +
20583464ebd5Sriastradh                                            rgba[i][BCOMP]);
20593464ebd5Sriastradh                     dst[i*2+1] = (GLuint) rgba[i][ACOMP];
20603464ebd5Sriastradh                  }
20613464ebd5Sriastradh                  break;
20623464ebd5Sriastradh               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
20633464ebd5Sriastradh                  for (i=0;i<n;i++) {
20643464ebd5Sriastradh                     dst[i] = (GLuint) (rgba[i][RCOMP] +
20653464ebd5Sriastradh                                        rgba[i][GCOMP] +
20663464ebd5Sriastradh                                        rgba[i][BCOMP]);
20673464ebd5Sriastradh                  }
20683464ebd5Sriastradh                  break;
20693464ebd5Sriastradh               default:
20703464ebd5Sriastradh                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
20713464ebd5Sriastradh            }
20723464ebd5Sriastradh         }
20733464ebd5Sriastradh         break;
20743464ebd5Sriastradh      case GL_INT:
20753464ebd5Sriastradh         {
20763464ebd5Sriastradh            GLint *dst = (GLint *) dstAddr;
20773464ebd5Sriastradh            switch (dstFormat) {
20783464ebd5Sriastradh               case GL_RED:
20793464ebd5Sriastradh                  for (i=0;i<n;i++)
20803464ebd5Sriastradh                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
20813464ebd5Sriastradh                  break;
20823464ebd5Sriastradh               case GL_GREEN:
20833464ebd5Sriastradh                  for (i=0;i<n;i++)
20843464ebd5Sriastradh                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
20853464ebd5Sriastradh                  break;
20863464ebd5Sriastradh               case GL_BLUE:
20873464ebd5Sriastradh                  for (i=0;i<n;i++)
20883464ebd5Sriastradh                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
20893464ebd5Sriastradh                  break;
20903464ebd5Sriastradh               case GL_ALPHA:
20913464ebd5Sriastradh                  for (i=0;i<n;i++)
20923464ebd5Sriastradh                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
20933464ebd5Sriastradh                  break;
20943464ebd5Sriastradh               case GL_LUMINANCE:
20953464ebd5Sriastradh                  for (i=0;i<n;i++)
20963464ebd5Sriastradh                     dst[i] = FLOAT_TO_INT(luminance[i]);
20973464ebd5Sriastradh                  break;
20983464ebd5Sriastradh               case GL_LUMINANCE_ALPHA:
20993464ebd5Sriastradh                  for (i=0;i<n;i++) {
21003464ebd5Sriastradh                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
21013464ebd5Sriastradh                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
21023464ebd5Sriastradh                  }
21033464ebd5Sriastradh                  break;
21043464ebd5Sriastradh               case GL_RG:
21053464ebd5Sriastradh                  for (i=0;i<n;i++) {
21063464ebd5Sriastradh                     dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
21073464ebd5Sriastradh                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
21083464ebd5Sriastradh                  }
21093464ebd5Sriastradh                  break;
21103464ebd5Sriastradh               case GL_RGB:
21113464ebd5Sriastradh                  for (i=0;i<n;i++) {
21123464ebd5Sriastradh                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
21133464ebd5Sriastradh                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
21143464ebd5Sriastradh                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
21153464ebd5Sriastradh                  }
21163464ebd5Sriastradh                  break;
21173464ebd5Sriastradh               case GL_RGBA:
21183464ebd5Sriastradh                  for (i=0;i<n;i++) {
21193464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
21203464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
21213464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
21223464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
21233464ebd5Sriastradh                  }
21243464ebd5Sriastradh                  break;
21253464ebd5Sriastradh               case GL_BGR:
21263464ebd5Sriastradh                  for (i=0;i<n;i++) {
21273464ebd5Sriastradh                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
21283464ebd5Sriastradh                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
21293464ebd5Sriastradh                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
21303464ebd5Sriastradh                  }
21313464ebd5Sriastradh                  break;
21323464ebd5Sriastradh               case GL_BGRA:
21333464ebd5Sriastradh                  for (i=0;i<n;i++) {
21343464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
21353464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
21363464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
21373464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
21383464ebd5Sriastradh                  }
21393464ebd5Sriastradh                  break;
21403464ebd5Sriastradh               case GL_ABGR_EXT:
21413464ebd5Sriastradh                  for (i=0;i<n;i++) {
21423464ebd5Sriastradh                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
21433464ebd5Sriastradh                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
21443464ebd5Sriastradh                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
21453464ebd5Sriastradh                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
21463464ebd5Sriastradh                  }
21473464ebd5Sriastradh                  break;
21483464ebd5Sriastradh               case GL_RED_INTEGER_EXT:
21493464ebd5Sriastradh                  for (i=0;i<n;i++) {
21503464ebd5Sriastradh                     dst[i] = (GLint) rgba[i][RCOMP];
21513464ebd5Sriastradh                  }
21523464ebd5Sriastradh                  break;
21533464ebd5Sriastradh               case GL_GREEN_INTEGER_EXT:
21543464ebd5Sriastradh                  for (i=0;i<n;i++) {
21553464ebd5Sriastradh                     dst[i] = (GLint) rgba[i][GCOMP];
21563464ebd5Sriastradh                  }
21573464ebd5Sriastradh                  break;
21583464ebd5Sriastradh               case GL_BLUE_INTEGER_EXT:
21593464ebd5Sriastradh                  for (i=0;i<n;i++) {
21603464ebd5Sriastradh                     dst[i] = (GLint) rgba[i][BCOMP];
21613464ebd5Sriastradh                  }
21623464ebd5Sriastradh                  break;
21633464ebd5Sriastradh               case GL_ALPHA_INTEGER_EXT:
21643464ebd5Sriastradh                  for (i=0;i<n;i++) {
21653464ebd5Sriastradh                     dst[i] = (GLint) rgba[i][ACOMP];
21663464ebd5Sriastradh                  }
21673464ebd5Sriastradh                  break;
2168af69d88dSmrg               case GL_RG_INTEGER:
2169af69d88dSmrg                  for (i=0;i<n;i++) {
2170af69d88dSmrg                     dst[i*2+0] = (GLint) rgba[i][RCOMP];
2171af69d88dSmrg                     dst[i*2+1] = (GLint) rgba[i][GCOMP];
2172af69d88dSmrg                  }
2173af69d88dSmrg                  break;
21743464ebd5Sriastradh               case GL_RGB_INTEGER_EXT:
21753464ebd5Sriastradh                  for (i=0;i<n;i++) {
21763464ebd5Sriastradh                     dst[i*3+0] = (GLint) rgba[i][RCOMP];
21773464ebd5Sriastradh                     dst[i*3+1] = (GLint) rgba[i][GCOMP];
21783464ebd5Sriastradh                     dst[i*3+2] = (GLint) rgba[i][BCOMP];
21793464ebd5Sriastradh                  }
21803464ebd5Sriastradh                  break;
21813464ebd5Sriastradh               case GL_RGBA_INTEGER_EXT:
21823464ebd5Sriastradh                  for (i=0;i<n;i++) {
21833464ebd5Sriastradh                     dst[i*4+0] = (GLint) rgba[i][RCOMP];
21843464ebd5Sriastradh                     dst[i*4+1] = (GLint) rgba[i][GCOMP];
21853464ebd5Sriastradh                     dst[i*4+2] = (GLint) rgba[i][BCOMP];
21863464ebd5Sriastradh                     dst[i*4+3] = (GLint) rgba[i][ACOMP];
21873464ebd5Sriastradh                  }
21883464ebd5Sriastradh                  break;
21893464ebd5Sriastradh               case GL_BGR_INTEGER_EXT:
21903464ebd5Sriastradh                  for (i=0;i<n;i++) {
21913464ebd5Sriastradh                     dst[i*3+0] = (GLint) rgba[i][BCOMP];
21923464ebd5Sriastradh                     dst[i*3+1] = (GLint) rgba[i][GCOMP];
21933464ebd5Sriastradh                     dst[i*3+2] = (GLint) rgba[i][RCOMP];
21943464ebd5Sriastradh                  }
21953464ebd5Sriastradh                  break;
21963464ebd5Sriastradh               case GL_BGRA_INTEGER_EXT:
21973464ebd5Sriastradh                  for (i=0;i<n;i++) {
21983464ebd5Sriastradh                     dst[i*4+0] = (GLint) rgba[i][BCOMP];
21993464ebd5Sriastradh                     dst[i*4+1] = (GLint) rgba[i][GCOMP];
22003464ebd5Sriastradh                     dst[i*4+2] = (GLint) rgba[i][RCOMP];
22013464ebd5Sriastradh                     dst[i*4+3] = (GLint) rgba[i][ACOMP];
22023464ebd5Sriastradh                  }
22033464ebd5Sriastradh                  break;
22043464ebd5Sriastradh               case GL_LUMINANCE_INTEGER_EXT:
22053464ebd5Sriastradh                  for (i=0;i<n;i++) {
22063464ebd5Sriastradh                     dst[i*2+0] = (GLint) (rgba[i][RCOMP] +
22073464ebd5Sriastradh                                           rgba[i][GCOMP] +
22083464ebd5Sriastradh                                           rgba[i][BCOMP]);
22093464ebd5Sriastradh                     dst[i*2+1] = (GLint) rgba[i][ACOMP];
22103464ebd5Sriastradh                  }
22113464ebd5Sriastradh                  break;
22123464ebd5Sriastradh               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
22133464ebd5Sriastradh                  for (i=0;i<n;i++) {
22143464ebd5Sriastradh                     dst[i] = (GLint) (rgba[i][RCOMP] +
22153464ebd5Sriastradh                                       rgba[i][GCOMP] +
22163464ebd5Sriastradh                                       rgba[i][BCOMP]);
22173464ebd5Sriastradh                  }
22183464ebd5Sriastradh                  break;
22193464ebd5Sriastradh               default:
22203464ebd5Sriastradh                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
22213464ebd5Sriastradh            }
22223464ebd5Sriastradh         }
22233464ebd5Sriastradh         break;
22243464ebd5Sriastradh      case GL_FLOAT:
22253464ebd5Sriastradh         {
22263464ebd5Sriastradh            GLfloat *dst = (GLfloat *) dstAddr;
22273464ebd5Sriastradh            switch (dstFormat) {
22283464ebd5Sriastradh               case GL_RED:
22293464ebd5Sriastradh                  for (i=0;i<n;i++)
22303464ebd5Sriastradh                     dst[i] = rgba[i][RCOMP];
22313464ebd5Sriastradh                  break;
22323464ebd5Sriastradh               case GL_GREEN:
22333464ebd5Sriastradh                  for (i=0;i<n;i++)
22343464ebd5Sriastradh                     dst[i] = rgba[i][GCOMP];
22353464ebd5Sriastradh                  break;
22363464ebd5Sriastradh               case GL_BLUE:
22373464ebd5Sriastradh                  for (i=0;i<n;i++)
22383464ebd5Sriastradh                     dst[i] = rgba[i][BCOMP];
22393464ebd5Sriastradh                  break;
22403464ebd5Sriastradh               case GL_ALPHA:
22413464ebd5Sriastradh                  for (i=0;i<n;i++)
22423464ebd5Sriastradh                     dst[i] = rgba[i][ACOMP];
22433464ebd5Sriastradh                  break;
22443464ebd5Sriastradh               case GL_LUMINANCE:
22453464ebd5Sriastradh                  for (i=0;i<n;i++)
22463464ebd5Sriastradh                     dst[i] = luminance[i];
22473464ebd5Sriastradh                  break;
22483464ebd5Sriastradh               case GL_LUMINANCE_ALPHA:
22493464ebd5Sriastradh                  for (i=0;i<n;i++) {
22503464ebd5Sriastradh                     dst[i*2+0] = luminance[i];
22513464ebd5Sriastradh                     dst[i*2+1] = rgba[i][ACOMP];
22523464ebd5Sriastradh                  }
22533464ebd5Sriastradh                  break;
22543464ebd5Sriastradh               case GL_RG:
22553464ebd5Sriastradh                  for (i=0;i<n;i++) {
22563464ebd5Sriastradh                     dst[i*2+0] = rgba[i][RCOMP];
22573464ebd5Sriastradh                     dst[i*2+1] = rgba[i][GCOMP];
22583464ebd5Sriastradh                  }
22593464ebd5Sriastradh                  break;
22603464ebd5Sriastradh               case GL_RGB:
22613464ebd5Sriastradh                  for (i=0;i<n;i++) {
22623464ebd5Sriastradh                     dst[i*3+0] = rgba[i][RCOMP];
22633464ebd5Sriastradh                     dst[i*3+1] = rgba[i][GCOMP];
22643464ebd5Sriastradh                     dst[i*3+2] = rgba[i][BCOMP];
22653464ebd5Sriastradh                  }
22663464ebd5Sriastradh                  break;
22673464ebd5Sriastradh               case GL_RGBA:
22683464ebd5Sriastradh                  for (i=0;i<n;i++) {
22693464ebd5Sriastradh                     dst[i*4+0] = rgba[i][RCOMP];
22703464ebd5Sriastradh                     dst[i*4+1] = rgba[i][GCOMP];
22713464ebd5Sriastradh                     dst[i*4+2] = rgba[i][BCOMP];
22723464ebd5Sriastradh                     dst[i*4+3] = rgba[i][ACOMP];
22733464ebd5Sriastradh                  }
22743464ebd5Sriastradh                  break;
22753464ebd5Sriastradh               case GL_BGR:
22763464ebd5Sriastradh                  for (i=0;i<n;i++) {
22773464ebd5Sriastradh                     dst[i*3+0] = rgba[i][BCOMP];
22783464ebd5Sriastradh                     dst[i*3+1] = rgba[i][GCOMP];
22793464ebd5Sriastradh                     dst[i*3+2] = rgba[i][RCOMP];
22803464ebd5Sriastradh                  }
22813464ebd5Sriastradh                  break;
22823464ebd5Sriastradh               case GL_BGRA:
22833464ebd5Sriastradh                  for (i=0;i<n;i++) {
22843464ebd5Sriastradh                     dst[i*4+0] = rgba[i][BCOMP];
22853464ebd5Sriastradh                     dst[i*4+1] = rgba[i][GCOMP];
22863464ebd5Sriastradh                     dst[i*4+2] = rgba[i][RCOMP];
22873464ebd5Sriastradh                     dst[i*4+3] = rgba[i][ACOMP];
22883464ebd5Sriastradh                  }
22893464ebd5Sriastradh                  break;
22903464ebd5Sriastradh               case GL_ABGR_EXT:
22913464ebd5Sriastradh                  for (i=0;i<n;i++) {
22923464ebd5Sriastradh                     dst[i*4+0] = rgba[i][ACOMP];
22933464ebd5Sriastradh                     dst[i*4+1] = rgba[i][BCOMP];
22943464ebd5Sriastradh                     dst[i*4+2] = rgba[i][GCOMP];
22953464ebd5Sriastradh                     dst[i*4+3] = rgba[i][RCOMP];
22963464ebd5Sriastradh                  }
22973464ebd5Sriastradh                  break;
22983464ebd5Sriastradh               default:
22993464ebd5Sriastradh                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
23003464ebd5Sriastradh            }
23013464ebd5Sriastradh         }
23023464ebd5Sriastradh         break;
23033464ebd5Sriastradh      case GL_HALF_FLOAT_ARB:
23043464ebd5Sriastradh         {
23053464ebd5Sriastradh            GLhalfARB *dst = (GLhalfARB *) dstAddr;
23063464ebd5Sriastradh            switch (dstFormat) {
23073464ebd5Sriastradh               case GL_RED:
23083464ebd5Sriastradh                  for (i=0;i<n;i++)
23093464ebd5Sriastradh                     dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
23103464ebd5Sriastradh                  break;
23113464ebd5Sriastradh               case GL_GREEN:
23123464ebd5Sriastradh                  for (i=0;i<n;i++)
23133464ebd5Sriastradh                     dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
23143464ebd5Sriastradh                  break;
23153464ebd5Sriastradh               case GL_BLUE:
23163464ebd5Sriastradh                  for (i=0;i<n;i++)
23173464ebd5Sriastradh                     dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
23183464ebd5Sriastradh                  break;
23193464ebd5Sriastradh               case GL_ALPHA:
23203464ebd5Sriastradh                  for (i=0;i<n;i++)
23213464ebd5Sriastradh                     dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
23223464ebd5Sriastradh                  break;
23233464ebd5Sriastradh               case GL_LUMINANCE:
23243464ebd5Sriastradh                  for (i=0;i<n;i++)
23253464ebd5Sriastradh                     dst[i] = _mesa_float_to_half(luminance[i]);
23263464ebd5Sriastradh                  break;
23273464ebd5Sriastradh               case GL_LUMINANCE_ALPHA:
23283464ebd5Sriastradh                  for (i=0;i<n;i++) {
23293464ebd5Sriastradh                     dst[i*2+0] = _mesa_float_to_half(luminance[i]);
23303464ebd5Sriastradh                     dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
23313464ebd5Sriastradh                  }
23323464ebd5Sriastradh                  break;
23333464ebd5Sriastradh               case GL_RG:
23343464ebd5Sriastradh                  for (i=0;i<n;i++) {
23353464ebd5Sriastradh                     dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
23363464ebd5Sriastradh                     dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
23373464ebd5Sriastradh                  }
23383464ebd5Sriastradh                  break;
23393464ebd5Sriastradh               case GL_RGB:
23403464ebd5Sriastradh                  for (i=0;i<n;i++) {
23413464ebd5Sriastradh                     dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
23423464ebd5Sriastradh                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
23433464ebd5Sriastradh                     dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
23443464ebd5Sriastradh                  }
23453464ebd5Sriastradh                  break;
23463464ebd5Sriastradh               case GL_RGBA:
23473464ebd5Sriastradh                  for (i=0;i<n;i++) {
23483464ebd5Sriastradh                     dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
23493464ebd5Sriastradh                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
23503464ebd5Sriastradh                     dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
23513464ebd5Sriastradh                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
23523464ebd5Sriastradh                  }
23533464ebd5Sriastradh                  break;
23543464ebd5Sriastradh               case GL_BGR:
23553464ebd5Sriastradh                  for (i=0;i<n;i++) {
23563464ebd5Sriastradh                     dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
23573464ebd5Sriastradh                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
23583464ebd5Sriastradh                     dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
23593464ebd5Sriastradh                  }
23603464ebd5Sriastradh                  break;
23613464ebd5Sriastradh               case GL_BGRA:
23623464ebd5Sriastradh                  for (i=0;i<n;i++) {
23633464ebd5Sriastradh                     dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
23643464ebd5Sriastradh                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
23653464ebd5Sriastradh                     dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
23663464ebd5Sriastradh                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
23673464ebd5Sriastradh                  }
23683464ebd5Sriastradh                  break;
23693464ebd5Sriastradh               case GL_ABGR_EXT:
23703464ebd5Sriastradh                  for (i=0;i<n;i++) {
23713464ebd5Sriastradh                     dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
23723464ebd5Sriastradh                     dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
23733464ebd5Sriastradh                     dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
23743464ebd5Sriastradh                     dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
23753464ebd5Sriastradh                  }
23763464ebd5Sriastradh                  break;
23773464ebd5Sriastradh               default:
23783464ebd5Sriastradh                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
23793464ebd5Sriastradh            }
23803464ebd5Sriastradh         }
23813464ebd5Sriastradh         break;
23823464ebd5Sriastradh      case GL_UNSIGNED_BYTE_3_3_2:
23833464ebd5Sriastradh         if (dstFormat == GL_RGB) {
23843464ebd5Sriastradh            GLubyte *dst = (GLubyte *) dstAddr;
23853464ebd5Sriastradh            for (i=0;i<n;i++) {
2386af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 7.0F) << 5)
2387af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 7.0F) << 2)
2388af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 3.0F)     );
23893464ebd5Sriastradh            }
23903464ebd5Sriastradh         }
23913464ebd5Sriastradh         break;
23923464ebd5Sriastradh      case GL_UNSIGNED_BYTE_2_3_3_REV:
23933464ebd5Sriastradh         if (dstFormat == GL_RGB) {
23943464ebd5Sriastradh            GLubyte *dst = (GLubyte *) dstAddr;
23953464ebd5Sriastradh            for (i=0;i<n;i++) {
2396af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 7.0F)     )
2397af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 7.0F) << 3)
2398af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 3.0F) << 6);
23993464ebd5Sriastradh            }
24003464ebd5Sriastradh         }
24013464ebd5Sriastradh         break;
24023464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_6_5:
24033464ebd5Sriastradh         if (dstFormat == GL_RGB) {
24043464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24053464ebd5Sriastradh            for (i=0;i<n;i++) {
2406af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F) << 11)
2407af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 63.0F) <<  5)
2408af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 31.0F)      );
24093464ebd5Sriastradh            }
24103464ebd5Sriastradh         }
24113464ebd5Sriastradh         break;
24123464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_6_5_REV:
24133464ebd5Sriastradh         if (dstFormat == GL_RGB) {
24143464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24153464ebd5Sriastradh            for (i=0;i<n;i++) {
2416af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F)      )
2417af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 63.0F) <<  5)
2418af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) << 11);
24193464ebd5Sriastradh            }
24203464ebd5Sriastradh         }
24213464ebd5Sriastradh         break;
24223464ebd5Sriastradh      case GL_UNSIGNED_SHORT_4_4_4_4:
24233464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
24243464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24253464ebd5Sriastradh            for (i=0;i<n;i++) {
2426af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 15.0F) << 12)
2427af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
2428af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  4)
2429af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] * 15.0F)      );
24303464ebd5Sriastradh            }
24313464ebd5Sriastradh         }
24323464ebd5Sriastradh         else if (dstFormat == GL_BGRA) {
24333464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24343464ebd5Sriastradh            for (i=0;i<n;i++) {
2435af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][BCOMP] * 15.0F) << 12)
2436af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
2437af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 15.0F) <<  4)
2438af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] * 15.0F)      );
24393464ebd5Sriastradh            }
24403464ebd5Sriastradh         }
24413464ebd5Sriastradh         else if (dstFormat == GL_ABGR_EXT) {
24423464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24433464ebd5Sriastradh            for (i=0;i<n;i++) {
2444af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12)
2445af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  8)
2446af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
2447af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 15.0F)      );
24483464ebd5Sriastradh            }
24493464ebd5Sriastradh         }
24503464ebd5Sriastradh         break;
24513464ebd5Sriastradh      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
24523464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
24533464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24543464ebd5Sriastradh            for (i=0;i<n;i++) {
2455af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 15.0F)      )
2456af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
2457af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  8)
2458af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12);
24593464ebd5Sriastradh            }
24603464ebd5Sriastradh         }
24613464ebd5Sriastradh         else if (dstFormat == GL_BGRA) {
24623464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24633464ebd5Sriastradh            for (i=0;i<n;i++) {
2464af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][BCOMP] * 15.0F)      )
2465af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
2466af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 15.0F) <<  8)
2467af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12);
24683464ebd5Sriastradh            }
24693464ebd5Sriastradh         }
24703464ebd5Sriastradh         else if (dstFormat == GL_ABGR_EXT) {
24713464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24723464ebd5Sriastradh            for (i=0;i<n;i++) {
2473af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][ACOMP] * 15.0F)      )
2474af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  4)
2475af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
2476af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 15.0F) << 12);
24773464ebd5Sriastradh            }
24783464ebd5Sriastradh         }
24793464ebd5Sriastradh         break;
24803464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_5_5_1:
24813464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
24823464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24833464ebd5Sriastradh            for (i=0;i<n;i++) {
2484af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F) << 11)
2485af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  6)
2486af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  1)
2487af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] *  1.0F)      );
24883464ebd5Sriastradh            }
24893464ebd5Sriastradh         }
24903464ebd5Sriastradh         else if (dstFormat == GL_BGRA) {
24913464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
24923464ebd5Sriastradh            for (i=0;i<n;i++) {
2493af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][BCOMP] * 31.0F) << 11)
2494af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  6)
2495af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 31.0F) <<  1)
2496af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] *  1.0F)      );
24973464ebd5Sriastradh            }
24983464ebd5Sriastradh         }
24993464ebd5Sriastradh         else if (dstFormat == GL_ABGR_EXT) {
25003464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
25013464ebd5Sriastradh            for (i=0;i<n;i++) {
2502af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][ACOMP] * 31.0F) << 11)
2503af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  6)
2504af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  1)
2505af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] *  1.0F)      );
25063464ebd5Sriastradh            }
25073464ebd5Sriastradh         }
25083464ebd5Sriastradh         break;
25093464ebd5Sriastradh      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
25103464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
25113464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
25123464ebd5Sriastradh            for (i=0;i<n;i++) {
2513af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F)      )
2514af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  5)
2515af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) << 10)
2516af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] *  1.0F) << 15);
25173464ebd5Sriastradh            }
25183464ebd5Sriastradh         }
25193464ebd5Sriastradh         else if (dstFormat == GL_BGRA) {
25203464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
25213464ebd5Sriastradh            for (i=0;i<n;i++) {
2522af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][BCOMP] * 31.0F)      )
2523af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  5)
2524af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 31.0F) << 10)
2525af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] *  1.0F) << 15);
25263464ebd5Sriastradh            }
25273464ebd5Sriastradh         }
25283464ebd5Sriastradh         else if (dstFormat == GL_ABGR_EXT) {
25293464ebd5Sriastradh            GLushort *dst = (GLushort *) dstAddr;
25303464ebd5Sriastradh            for (i=0;i<n;i++) {
2531af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][ACOMP] * 31.0F)      )
2532af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  5)
2533af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) << 10)
2534af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] *  1.0F) << 15);
25353464ebd5Sriastradh            }
25363464ebd5Sriastradh         }
25373464ebd5Sriastradh         break;
25383464ebd5Sriastradh      case GL_UNSIGNED_INT_8_8_8_8:
25393464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
25403464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
25413464ebd5Sriastradh            for (i=0;i<n;i++) {
2542af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 255.F) << 24)
2543af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 255.F) << 16)
2544af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 255.F) <<  8)
2545af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] * 255.F)      );
25463464ebd5Sriastradh            }
25473464ebd5Sriastradh         }
25483464ebd5Sriastradh         else if (dstFormat == GL_BGRA) {
25493464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
25503464ebd5Sriastradh            for (i=0;i<n;i++) {
2551af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][BCOMP] * 255.F) << 24)
2552af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 255.F) << 16)
2553af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 255.F) <<  8)
2554af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] * 255.F)      );
25553464ebd5Sriastradh            }
25563464ebd5Sriastradh         }
25573464ebd5Sriastradh         else if (dstFormat == GL_ABGR_EXT) {
25583464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
25593464ebd5Sriastradh            for (i=0;i<n;i++) {
2560af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][ACOMP] * 255.F) << 24)
2561af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 255.F) << 16)
2562af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 255.F) <<  8)
2563af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 255.F)      );
25643464ebd5Sriastradh            }
25653464ebd5Sriastradh         }
25663464ebd5Sriastradh         break;
25673464ebd5Sriastradh      case GL_UNSIGNED_INT_8_8_8_8_REV:
25683464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
25693464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
25703464ebd5Sriastradh            for (i=0;i<n;i++) {
2571af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 255.0F)      )
2572af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 255.0F) <<  8)
2573af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 255.0F) << 16)
2574af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] * 255.0F) << 24);
25753464ebd5Sriastradh            }
25763464ebd5Sriastradh         }
25773464ebd5Sriastradh         else if (dstFormat == GL_BGRA) {
25783464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
25793464ebd5Sriastradh            for (i=0;i<n;i++) {
2580af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][BCOMP] * 255.0F)      )
2581af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 255.0F) <<  8)
2582af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 255.0F) << 16)
2583af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] * 255.0F) << 24);
25843464ebd5Sriastradh            }
25853464ebd5Sriastradh         }
25863464ebd5Sriastradh         else if (dstFormat == GL_ABGR_EXT) {
25873464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
25883464ebd5Sriastradh            for (i=0;i<n;i++) {
2589af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][ACOMP] * 255.0F)      )
2590af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 255.0F) <<  8)
2591af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 255.0F) << 16)
2592af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 255.0F) << 24);
25933464ebd5Sriastradh            }
25943464ebd5Sriastradh         }
25953464ebd5Sriastradh         break;
25963464ebd5Sriastradh      case GL_UNSIGNED_INT_10_10_10_2:
25973464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
25983464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
25993464ebd5Sriastradh            for (i=0;i<n;i++) {
2600af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 1023.0F) << 22)
2601af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 12)
2602af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 1023.0F) <<  2)
2603af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] *    3.0F)      );
26043464ebd5Sriastradh            }
26053464ebd5Sriastradh         }
26063464ebd5Sriastradh         else if (dstFormat == GL_BGRA) {
26073464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
26083464ebd5Sriastradh            for (i=0;i<n;i++) {
2609af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 22)
2610af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 12)
2611af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 1023.0F) <<  2)
2612af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] *    3.0F)      );
26133464ebd5Sriastradh            }
26143464ebd5Sriastradh         }
26153464ebd5Sriastradh         else if (dstFormat == GL_ABGR_EXT) {
26163464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
26173464ebd5Sriastradh            for (i=0;i<n;i++) {
2618af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][ACOMP] * 1023.0F) << 22)
2619af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 12)
2620af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) <<  2)
2621af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] *    3.0F)      );
26223464ebd5Sriastradh            }
26233464ebd5Sriastradh         }
26243464ebd5Sriastradh         break;
26253464ebd5Sriastradh      case GL_UNSIGNED_INT_2_10_10_10_REV:
26263464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
26273464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
26283464ebd5Sriastradh            for (i=0;i<n;i++) {
2629af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][RCOMP] * 1023.0F)      )
2630af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 10)
2631af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 20)
2632af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] *    3.0F) << 30);
26333464ebd5Sriastradh            }
26343464ebd5Sriastradh         }
26353464ebd5Sriastradh         else if (dstFormat == GL_BGRA) {
26363464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
26373464ebd5Sriastradh            for (i=0;i<n;i++) {
2638af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][BCOMP] * 1023.0F)      )
2639af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 10)
2640af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] * 1023.0F) << 20)
2641af69d88dSmrg                      | (F_TO_I(rgba[i][ACOMP] *    3.0F) << 30);
26423464ebd5Sriastradh            }
26433464ebd5Sriastradh         }
26443464ebd5Sriastradh         else if (dstFormat == GL_ABGR_EXT) {
26453464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
26463464ebd5Sriastradh            for (i=0;i<n;i++) {
2647af69d88dSmrg               dst[i] = (F_TO_I(rgba[i][ACOMP] * 1023.0F)      )
2648af69d88dSmrg                      | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 10)
2649af69d88dSmrg                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 20)
2650af69d88dSmrg                      | (F_TO_I(rgba[i][RCOMP] *    3.0F) << 30);
26513464ebd5Sriastradh            }
26523464ebd5Sriastradh         }
26533464ebd5Sriastradh         break;
26543464ebd5Sriastradh      case GL_UNSIGNED_INT_5_9_9_9_REV:
26553464ebd5Sriastradh         {
26563464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
26573464ebd5Sriastradh            for (i = 0; i < n; i++) {
26583464ebd5Sriastradh               dst[i] = float3_to_rgb9e5(rgba[i]);
26593464ebd5Sriastradh            }
26603464ebd5Sriastradh         }
26613464ebd5Sriastradh         break;
26623464ebd5Sriastradh      case GL_UNSIGNED_INT_10F_11F_11F_REV:
26633464ebd5Sriastradh         {
26643464ebd5Sriastradh            GLuint *dst = (GLuint *) dstAddr;
26653464ebd5Sriastradh            for (i = 0; i < n; i++) {
26663464ebd5Sriastradh               dst[i] = float3_to_r11g11b10f(rgba[i]);
26673464ebd5Sriastradh            }
26683464ebd5Sriastradh         }
26693464ebd5Sriastradh         break;
26703464ebd5Sriastradh      default:
26713464ebd5Sriastradh         _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2672af69d88dSmrg         free(luminance);
26733464ebd5Sriastradh         return;
26743464ebd5Sriastradh   }
26753464ebd5Sriastradh
26763464ebd5Sriastradh   if (dstPacking->SwapBytes) {
26773464ebd5Sriastradh      GLint swapSize = _mesa_sizeof_packed_type(dstType);
26783464ebd5Sriastradh      if (swapSize == 2) {
2679af69d88dSmrg         _mesa_swap2((GLushort *) dstAddr, n * comps);
26803464ebd5Sriastradh      }
26813464ebd5Sriastradh      else if (swapSize == 4) {
2682af69d88dSmrg         _mesa_swap4((GLuint *) dstAddr, n * comps);
26833464ebd5Sriastradh      }
26843464ebd5Sriastradh   }
26853464ebd5Sriastradh
26863464ebd5Sriastradh   free(luminance);
26873464ebd5Sriastradh}
26883464ebd5Sriastradh
26893464ebd5Sriastradh
26903464ebd5Sriastradh
26913464ebd5Sriastradh#define SWAP2BYTE(VALUE)			\
26923464ebd5Sriastradh   {						\
26933464ebd5Sriastradh      GLubyte *bytes = (GLubyte *) &(VALUE);	\
26943464ebd5Sriastradh      GLubyte tmp = bytes[0];			\
26953464ebd5Sriastradh      bytes[0] = bytes[1];			\
26963464ebd5Sriastradh      bytes[1] = tmp;				\
26973464ebd5Sriastradh   }
26983464ebd5Sriastradh
26993464ebd5Sriastradh#define SWAP4BYTE(VALUE)			\
27003464ebd5Sriastradh   {						\
27013464ebd5Sriastradh      GLubyte *bytes = (GLubyte *) &(VALUE);	\
27023464ebd5Sriastradh      GLubyte tmp = bytes[0];			\
27033464ebd5Sriastradh      bytes[0] = bytes[3];			\
27043464ebd5Sriastradh      bytes[3] = tmp;				\
27053464ebd5Sriastradh      tmp = bytes[1];				\
27063464ebd5Sriastradh      bytes[1] = bytes[2];			\
27073464ebd5Sriastradh      bytes[2] = tmp;				\
27083464ebd5Sriastradh   }
27093464ebd5Sriastradh
27103464ebd5Sriastradh
27113464ebd5Sriastradhstatic void
27123464ebd5Sriastradhextract_uint_indexes(GLuint n, GLuint indexes[],
27133464ebd5Sriastradh                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
27143464ebd5Sriastradh                     const struct gl_pixelstore_attrib *unpack )
27153464ebd5Sriastradh{
27163464ebd5Sriastradh   ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
27173464ebd5Sriastradh
27183464ebd5Sriastradh   ASSERT(srcType == GL_BITMAP ||
27193464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE ||
27203464ebd5Sriastradh          srcType == GL_BYTE ||
27213464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT ||
27223464ebd5Sriastradh          srcType == GL_SHORT ||
27233464ebd5Sriastradh          srcType == GL_UNSIGNED_INT ||
27243464ebd5Sriastradh          srcType == GL_INT ||
27253464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_24_8_EXT ||
27263464ebd5Sriastradh          srcType == GL_HALF_FLOAT_ARB ||
2727af69d88dSmrg          srcType == GL_FLOAT ||
2728af69d88dSmrg          srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
27293464ebd5Sriastradh
27303464ebd5Sriastradh   switch (srcType) {
27313464ebd5Sriastradh      case GL_BITMAP:
27323464ebd5Sriastradh         {
27333464ebd5Sriastradh            GLubyte *ubsrc = (GLubyte *) src;
27343464ebd5Sriastradh            if (unpack->LsbFirst) {
27353464ebd5Sriastradh               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
27363464ebd5Sriastradh               GLuint i;
27373464ebd5Sriastradh               for (i = 0; i < n; i++) {
27383464ebd5Sriastradh                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
27393464ebd5Sriastradh                  if (mask == 128) {
27403464ebd5Sriastradh                     mask = 1;
27413464ebd5Sriastradh                     ubsrc++;
27423464ebd5Sriastradh                  }
27433464ebd5Sriastradh                  else {
27443464ebd5Sriastradh                     mask = mask << 1;
27453464ebd5Sriastradh                  }
27463464ebd5Sriastradh               }
27473464ebd5Sriastradh            }
27483464ebd5Sriastradh            else {
27493464ebd5Sriastradh               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
27503464ebd5Sriastradh               GLuint i;
27513464ebd5Sriastradh               for (i = 0; i < n; i++) {
27523464ebd5Sriastradh                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
27533464ebd5Sriastradh                  if (mask == 1) {
27543464ebd5Sriastradh                     mask = 128;
27553464ebd5Sriastradh                     ubsrc++;
27563464ebd5Sriastradh                  }
27573464ebd5Sriastradh                  else {
27583464ebd5Sriastradh                     mask = mask >> 1;
27593464ebd5Sriastradh                  }
27603464ebd5Sriastradh               }
27613464ebd5Sriastradh            }
27623464ebd5Sriastradh         }
27633464ebd5Sriastradh         break;
27643464ebd5Sriastradh      case GL_UNSIGNED_BYTE:
27653464ebd5Sriastradh         {
27663464ebd5Sriastradh            GLuint i;
27673464ebd5Sriastradh            const GLubyte *s = (const GLubyte *) src;
27683464ebd5Sriastradh            for (i = 0; i < n; i++)
27693464ebd5Sriastradh               indexes[i] = s[i];
27703464ebd5Sriastradh         }
27713464ebd5Sriastradh         break;
27723464ebd5Sriastradh      case GL_BYTE:
27733464ebd5Sriastradh         {
27743464ebd5Sriastradh            GLuint i;
27753464ebd5Sriastradh            const GLbyte *s = (const GLbyte *) src;
27763464ebd5Sriastradh            for (i = 0; i < n; i++)
27773464ebd5Sriastradh               indexes[i] = s[i];
27783464ebd5Sriastradh         }
27793464ebd5Sriastradh         break;
27803464ebd5Sriastradh      case GL_UNSIGNED_SHORT:
27813464ebd5Sriastradh         {
27823464ebd5Sriastradh            GLuint i;
27833464ebd5Sriastradh            const GLushort *s = (const GLushort *) src;
27843464ebd5Sriastradh            if (unpack->SwapBytes) {
27853464ebd5Sriastradh               for (i = 0; i < n; i++) {
27863464ebd5Sriastradh                  GLushort value = s[i];
27873464ebd5Sriastradh                  SWAP2BYTE(value);
27883464ebd5Sriastradh                  indexes[i] = value;
27893464ebd5Sriastradh               }
27903464ebd5Sriastradh            }
27913464ebd5Sriastradh            else {
27923464ebd5Sriastradh               for (i = 0; i < n; i++)
27933464ebd5Sriastradh                  indexes[i] = s[i];
27943464ebd5Sriastradh            }
27953464ebd5Sriastradh         }
27963464ebd5Sriastradh         break;
27973464ebd5Sriastradh      case GL_SHORT:
27983464ebd5Sriastradh         {
27993464ebd5Sriastradh            GLuint i;
28003464ebd5Sriastradh            const GLshort *s = (const GLshort *) src;
28013464ebd5Sriastradh            if (unpack->SwapBytes) {
28023464ebd5Sriastradh               for (i = 0; i < n; i++) {
28033464ebd5Sriastradh                  GLshort value = s[i];
28043464ebd5Sriastradh                  SWAP2BYTE(value);
28053464ebd5Sriastradh                  indexes[i] = value;
28063464ebd5Sriastradh               }
28073464ebd5Sriastradh            }
28083464ebd5Sriastradh            else {
28093464ebd5Sriastradh               for (i = 0; i < n; i++)
28103464ebd5Sriastradh                  indexes[i] = s[i];
28113464ebd5Sriastradh            }
28123464ebd5Sriastradh         }
28133464ebd5Sriastradh         break;
28143464ebd5Sriastradh      case GL_UNSIGNED_INT:
28153464ebd5Sriastradh         {
28163464ebd5Sriastradh            GLuint i;
28173464ebd5Sriastradh            const GLuint *s = (const GLuint *) src;
28183464ebd5Sriastradh            if (unpack->SwapBytes) {
28193464ebd5Sriastradh               for (i = 0; i < n; i++) {
28203464ebd5Sriastradh                  GLuint value = s[i];
28213464ebd5Sriastradh                  SWAP4BYTE(value);
28223464ebd5Sriastradh                  indexes[i] = value;
28233464ebd5Sriastradh               }
28243464ebd5Sriastradh            }
28253464ebd5Sriastradh            else {
28263464ebd5Sriastradh               for (i = 0; i < n; i++)
28273464ebd5Sriastradh                  indexes[i] = s[i];
28283464ebd5Sriastradh            }
28293464ebd5Sriastradh         }
28303464ebd5Sriastradh         break;
28313464ebd5Sriastradh      case GL_INT:
28323464ebd5Sriastradh         {
28333464ebd5Sriastradh            GLuint i;
28343464ebd5Sriastradh            const GLint *s = (const GLint *) src;
28353464ebd5Sriastradh            if (unpack->SwapBytes) {
28363464ebd5Sriastradh               for (i = 0; i < n; i++) {
28373464ebd5Sriastradh                  GLint value = s[i];
28383464ebd5Sriastradh                  SWAP4BYTE(value);
28393464ebd5Sriastradh                  indexes[i] = value;
28403464ebd5Sriastradh               }
28413464ebd5Sriastradh            }
28423464ebd5Sriastradh            else {
28433464ebd5Sriastradh               for (i = 0; i < n; i++)
28443464ebd5Sriastradh                  indexes[i] = s[i];
28453464ebd5Sriastradh            }
28463464ebd5Sriastradh         }
28473464ebd5Sriastradh         break;
28483464ebd5Sriastradh      case GL_FLOAT:
28493464ebd5Sriastradh         {
28503464ebd5Sriastradh            GLuint i;
28513464ebd5Sriastradh            const GLfloat *s = (const GLfloat *) src;
28523464ebd5Sriastradh            if (unpack->SwapBytes) {
28533464ebd5Sriastradh               for (i = 0; i < n; i++) {
28543464ebd5Sriastradh                  GLfloat value = s[i];
28553464ebd5Sriastradh                  SWAP4BYTE(value);
28563464ebd5Sriastradh                  indexes[i] = (GLuint) value;
28573464ebd5Sriastradh               }
28583464ebd5Sriastradh            }
28593464ebd5Sriastradh            else {
28603464ebd5Sriastradh               for (i = 0; i < n; i++)
28613464ebd5Sriastradh                  indexes[i] = (GLuint) s[i];
28623464ebd5Sriastradh            }
28633464ebd5Sriastradh         }
28643464ebd5Sriastradh         break;
28653464ebd5Sriastradh      case GL_HALF_FLOAT_ARB:
28663464ebd5Sriastradh         {
28673464ebd5Sriastradh            GLuint i;
28683464ebd5Sriastradh            const GLhalfARB *s = (const GLhalfARB *) src;
28693464ebd5Sriastradh            if (unpack->SwapBytes) {
28703464ebd5Sriastradh               for (i = 0; i < n; i++) {
28713464ebd5Sriastradh                  GLhalfARB value = s[i];
28723464ebd5Sriastradh                  SWAP2BYTE(value);
28733464ebd5Sriastradh                  indexes[i] = (GLuint) _mesa_half_to_float(value);
28743464ebd5Sriastradh               }
28753464ebd5Sriastradh            }
28763464ebd5Sriastradh            else {
28773464ebd5Sriastradh               for (i = 0; i < n; i++)
2878af69d88dSmrg                  indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
2879af69d88dSmrg            }
2880af69d88dSmrg         }
2881af69d88dSmrg         break;
2882af69d88dSmrg      case GL_UNSIGNED_INT_24_8_EXT:
2883af69d88dSmrg         {
2884af69d88dSmrg            GLuint i;
2885af69d88dSmrg            const GLuint *s = (const GLuint *) src;
2886af69d88dSmrg            if (unpack->SwapBytes) {
2887af69d88dSmrg               for (i = 0; i < n; i++) {
2888af69d88dSmrg                  GLuint value = s[i];
2889af69d88dSmrg                  SWAP4BYTE(value);
2890af69d88dSmrg                  indexes[i] = value & 0xff;  /* lower 8 bits */
2891af69d88dSmrg               }
2892af69d88dSmrg            }
2893af69d88dSmrg            else {
2894af69d88dSmrg               for (i = 0; i < n; i++)
2895af69d88dSmrg                  indexes[i] = s[i] & 0xff;  /* lower 8 bits */
28963464ebd5Sriastradh            }
28973464ebd5Sriastradh         }
28983464ebd5Sriastradh         break;
2899af69d88dSmrg      case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
29003464ebd5Sriastradh         {
29013464ebd5Sriastradh            GLuint i;
29023464ebd5Sriastradh            const GLuint *s = (const GLuint *) src;
29033464ebd5Sriastradh            if (unpack->SwapBytes) {
29043464ebd5Sriastradh               for (i = 0; i < n; i++) {
2905af69d88dSmrg                  GLuint value = s[i*2+1];
29063464ebd5Sriastradh                  SWAP4BYTE(value);
29073464ebd5Sriastradh                  indexes[i] = value & 0xff;  /* lower 8 bits */
29083464ebd5Sriastradh               }
29093464ebd5Sriastradh            }
29103464ebd5Sriastradh            else {
29113464ebd5Sriastradh               for (i = 0; i < n; i++)
2912af69d88dSmrg                  indexes[i] = s[i*2+1] & 0xff;  /* lower 8 bits */
29133464ebd5Sriastradh            }
29143464ebd5Sriastradh         }
29153464ebd5Sriastradh         break;
29163464ebd5Sriastradh
29173464ebd5Sriastradh      default:
29183464ebd5Sriastradh         _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
29193464ebd5Sriastradh         return;
29203464ebd5Sriastradh   }
29213464ebd5Sriastradh}
29223464ebd5Sriastradh
29233464ebd5Sriastradh
29243464ebd5Sriastradh/**
29253464ebd5Sriastradh * Return source/dest RGBA indexes for unpacking pixels.
29263464ebd5Sriastradh */
29273464ebd5Sriastradhstatic void
29283464ebd5Sriastradhget_component_mapping(GLenum format,
29293464ebd5Sriastradh                      GLint *rSrc,
29303464ebd5Sriastradh                      GLint *gSrc,
29313464ebd5Sriastradh                      GLint *bSrc,
29323464ebd5Sriastradh                      GLint *aSrc,
29333464ebd5Sriastradh                      GLint *rDst,
29343464ebd5Sriastradh                      GLint *gDst,
29353464ebd5Sriastradh                      GLint *bDst,
29363464ebd5Sriastradh                      GLint *aDst)
29373464ebd5Sriastradh{
29383464ebd5Sriastradh   switch (format) {
29393464ebd5Sriastradh   case GL_RED:
29403464ebd5Sriastradh   case GL_RED_INTEGER_EXT:
29413464ebd5Sriastradh      *rSrc = 0;
29423464ebd5Sriastradh      *gSrc = *bSrc = *aSrc = -1;
29433464ebd5Sriastradh      break;
29443464ebd5Sriastradh   case GL_GREEN:
29453464ebd5Sriastradh   case GL_GREEN_INTEGER_EXT:
29463464ebd5Sriastradh      *gSrc = 0;
29473464ebd5Sriastradh      *rSrc = *bSrc = *aSrc = -1;
29483464ebd5Sriastradh      break;
2949af69d88dSmrg   case GL_BLUE:
29503464ebd5Sriastradh   case GL_BLUE_INTEGER_EXT:
29513464ebd5Sriastradh      *bSrc = 0;
29523464ebd5Sriastradh      *rSrc = *gSrc = *aSrc = -1;
29533464ebd5Sriastradh      break;
29543464ebd5Sriastradh   case GL_ALPHA:
29553464ebd5Sriastradh   case GL_ALPHA_INTEGER_EXT:
29563464ebd5Sriastradh      *rSrc = *gSrc = *bSrc = -1;
29573464ebd5Sriastradh      *aSrc = 0;
29583464ebd5Sriastradh      break;
29593464ebd5Sriastradh   case GL_LUMINANCE:
29603464ebd5Sriastradh   case GL_LUMINANCE_INTEGER_EXT:
29613464ebd5Sriastradh      *rSrc = *gSrc = *bSrc = 0;
29623464ebd5Sriastradh      *aSrc = -1;
29633464ebd5Sriastradh      break;
29643464ebd5Sriastradh   case GL_LUMINANCE_ALPHA:
29653464ebd5Sriastradh   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
29663464ebd5Sriastradh      *rSrc = *gSrc = *bSrc = 0;
29673464ebd5Sriastradh      *aSrc = 1;
29683464ebd5Sriastradh      break;
29693464ebd5Sriastradh   case GL_INTENSITY:
29703464ebd5Sriastradh      *rSrc = *gSrc = *bSrc = *aSrc = 0;
29713464ebd5Sriastradh      break;
29723464ebd5Sriastradh   case GL_RG:
29733464ebd5Sriastradh   case GL_RG_INTEGER:
29743464ebd5Sriastradh      *rSrc = 0;
29753464ebd5Sriastradh      *gSrc = 1;
29763464ebd5Sriastradh      *bSrc = -1;
29773464ebd5Sriastradh      *aSrc = -1;
29783464ebd5Sriastradh      *rDst = 0;
29793464ebd5Sriastradh      *gDst = 1;
29803464ebd5Sriastradh      *bDst = 2;
29813464ebd5Sriastradh      *aDst = 3;
29823464ebd5Sriastradh      break;
29833464ebd5Sriastradh   case GL_RGB:
29843464ebd5Sriastradh   case GL_RGB_INTEGER:
29853464ebd5Sriastradh      *rSrc = 0;
29863464ebd5Sriastradh      *gSrc = 1;
29873464ebd5Sriastradh      *bSrc = 2;
29883464ebd5Sriastradh      *aSrc = -1;
29893464ebd5Sriastradh      *rDst = 0;
29903464ebd5Sriastradh      *gDst = 1;
29913464ebd5Sriastradh      *bDst = 2;
29923464ebd5Sriastradh      *aDst = 3;
29933464ebd5Sriastradh      break;
29943464ebd5Sriastradh   case GL_BGR:
2995af69d88dSmrg   case GL_BGR_INTEGER:
29963464ebd5Sriastradh      *rSrc = 2;
29973464ebd5Sriastradh      *gSrc = 1;
29983464ebd5Sriastradh      *bSrc = 0;
29993464ebd5Sriastradh      *aSrc = -1;
30003464ebd5Sriastradh      *rDst = 2;
30013464ebd5Sriastradh      *gDst = 1;
30023464ebd5Sriastradh      *bDst = 0;
30033464ebd5Sriastradh      *aDst = 3;
30043464ebd5Sriastradh      break;
30053464ebd5Sriastradh   case GL_RGBA:
30063464ebd5Sriastradh   case GL_RGBA_INTEGER:
30073464ebd5Sriastradh      *rSrc = 0;
30083464ebd5Sriastradh      *gSrc = 1;
30093464ebd5Sriastradh      *bSrc = 2;
30103464ebd5Sriastradh      *aSrc = 3;
30113464ebd5Sriastradh      *rDst = 0;
30123464ebd5Sriastradh      *gDst = 1;
30133464ebd5Sriastradh      *bDst = 2;
30143464ebd5Sriastradh      *aDst = 3;
30153464ebd5Sriastradh      break;
30163464ebd5Sriastradh   case GL_BGRA:
3017af69d88dSmrg   case GL_BGRA_INTEGER:
30183464ebd5Sriastradh      *rSrc = 2;
30193464ebd5Sriastradh      *gSrc = 1;
30203464ebd5Sriastradh      *bSrc = 0;
30213464ebd5Sriastradh      *aSrc = 3;
30223464ebd5Sriastradh      *rDst = 2;
30233464ebd5Sriastradh      *gDst = 1;
30243464ebd5Sriastradh      *bDst = 0;
30253464ebd5Sriastradh      *aDst = 3;
30263464ebd5Sriastradh      break;
30273464ebd5Sriastradh   case GL_ABGR_EXT:
30283464ebd5Sriastradh      *rSrc = 3;
30293464ebd5Sriastradh      *gSrc = 2;
30303464ebd5Sriastradh      *bSrc = 1;
30313464ebd5Sriastradh      *aSrc = 0;
30323464ebd5Sriastradh      *rDst = 3;
30333464ebd5Sriastradh      *gDst = 2;
30343464ebd5Sriastradh      *bDst = 1;
30353464ebd5Sriastradh      *aDst = 0;
30363464ebd5Sriastradh      break;
30373464ebd5Sriastradh   default:
30383464ebd5Sriastradh      _mesa_problem(NULL, "bad srcFormat %s in get_component_mapping",
30393464ebd5Sriastradh                    _mesa_lookup_enum_by_nr(format));
30403464ebd5Sriastradh      return;
30413464ebd5Sriastradh   }
30423464ebd5Sriastradh}
30433464ebd5Sriastradh
30443464ebd5Sriastradh
30453464ebd5Sriastradh
30463464ebd5Sriastradh/*
30473464ebd5Sriastradh * This function extracts floating point RGBA values from arbitrary
30483464ebd5Sriastradh * image data.  srcFormat and srcType are the format and type parameters
30493464ebd5Sriastradh * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
30503464ebd5Sriastradh *
30513464ebd5Sriastradh * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
30523464ebd5Sriastradh * implements the "Conversion to floating point", "Conversion to RGB",
30533464ebd5Sriastradh * and "Final Expansion to RGBA" operations.
30543464ebd5Sriastradh *
30553464ebd5Sriastradh * Args:  n - number of pixels
30563464ebd5Sriastradh *        rgba - output colors
30573464ebd5Sriastradh *        srcFormat - format of incoming data
30583464ebd5Sriastradh *        srcType - data type of incoming data
30593464ebd5Sriastradh *        src - source data pointer
30603464ebd5Sriastradh *        swapBytes - perform byteswapping of incoming data?
30613464ebd5Sriastradh */
30623464ebd5Sriastradhstatic void
30633464ebd5Sriastradhextract_float_rgba(GLuint n, GLfloat rgba[][4],
30643464ebd5Sriastradh                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
30653464ebd5Sriastradh                   GLboolean swapBytes)
30663464ebd5Sriastradh{
30673464ebd5Sriastradh   GLint rSrc, gSrc, bSrc, aSrc;
30683464ebd5Sriastradh   GLint stride;
30693464ebd5Sriastradh   GLint rDst, bDst, gDst, aDst;
30703464ebd5Sriastradh   GLboolean intFormat;
30713464ebd5Sriastradh   GLfloat rs = 1.0f, gs = 1.0f, bs = 1.0f, as = 1.0f; /* scale factors */
30723464ebd5Sriastradh
30733464ebd5Sriastradh   ASSERT(srcFormat == GL_RED ||
30743464ebd5Sriastradh          srcFormat == GL_GREEN ||
30753464ebd5Sriastradh          srcFormat == GL_BLUE ||
30763464ebd5Sriastradh          srcFormat == GL_ALPHA ||
30773464ebd5Sriastradh          srcFormat == GL_LUMINANCE ||
30783464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA ||
30793464ebd5Sriastradh          srcFormat == GL_INTENSITY ||
30803464ebd5Sriastradh          srcFormat == GL_RG ||
30813464ebd5Sriastradh          srcFormat == GL_RGB ||
30823464ebd5Sriastradh          srcFormat == GL_BGR ||
30833464ebd5Sriastradh          srcFormat == GL_RGBA ||
30843464ebd5Sriastradh          srcFormat == GL_BGRA ||
30853464ebd5Sriastradh          srcFormat == GL_ABGR_EXT ||
30863464ebd5Sriastradh          srcFormat == GL_RED_INTEGER_EXT ||
30873464ebd5Sriastradh          srcFormat == GL_GREEN_INTEGER_EXT ||
30883464ebd5Sriastradh          srcFormat == GL_BLUE_INTEGER_EXT ||
30893464ebd5Sriastradh          srcFormat == GL_ALPHA_INTEGER_EXT ||
3090af69d88dSmrg          srcFormat == GL_RG_INTEGER ||
30913464ebd5Sriastradh          srcFormat == GL_RGB_INTEGER_EXT ||
30923464ebd5Sriastradh          srcFormat == GL_RGBA_INTEGER_EXT ||
30933464ebd5Sriastradh          srcFormat == GL_BGR_INTEGER_EXT ||
30943464ebd5Sriastradh          srcFormat == GL_BGRA_INTEGER_EXT ||
30953464ebd5Sriastradh          srcFormat == GL_LUMINANCE_INTEGER_EXT ||
30963464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
30973464ebd5Sriastradh
30983464ebd5Sriastradh   ASSERT(srcType == GL_UNSIGNED_BYTE ||
30993464ebd5Sriastradh          srcType == GL_BYTE ||
31003464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT ||
31013464ebd5Sriastradh          srcType == GL_SHORT ||
31023464ebd5Sriastradh          srcType == GL_UNSIGNED_INT ||
31033464ebd5Sriastradh          srcType == GL_INT ||
31043464ebd5Sriastradh          srcType == GL_HALF_FLOAT_ARB ||
31053464ebd5Sriastradh          srcType == GL_FLOAT ||
31063464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
31073464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
31083464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
31093464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
31103464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
31113464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
31123464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
31133464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
31143464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
31153464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
31163464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
31173464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
31183464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
31193464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
31203464ebd5Sriastradh
31213464ebd5Sriastradh   get_component_mapping(srcFormat,
31223464ebd5Sriastradh                         &rSrc, &gSrc, &bSrc, &aSrc,
31233464ebd5Sriastradh                         &rDst, &gDst, &bDst, &aDst);
31243464ebd5Sriastradh
31253464ebd5Sriastradh   stride = _mesa_components_in_format(srcFormat);
31263464ebd5Sriastradh
3127af69d88dSmrg   intFormat = _mesa_is_enum_format_integer(srcFormat);
31283464ebd5Sriastradh
31293464ebd5Sriastradh#define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT_FLT, DEFAULT_INT, TYPE, CONVERSION) \
31303464ebd5Sriastradh   if ((SRC_INDEX) < 0) {						\
31313464ebd5Sriastradh      GLuint i;								\
31323464ebd5Sriastradh      if (intFormat) {							\
31333464ebd5Sriastradh         for (i = 0; i < n; i++) {					\
31343464ebd5Sriastradh            rgba[i][DST_INDEX] = DEFAULT_INT;				\
31353464ebd5Sriastradh         }								\
31363464ebd5Sriastradh      }									\
31373464ebd5Sriastradh      else {								\
31383464ebd5Sriastradh         for (i = 0; i < n; i++) {					\
31393464ebd5Sriastradh            rgba[i][DST_INDEX] = DEFAULT_FLT;				\
31403464ebd5Sriastradh         }								\
31413464ebd5Sriastradh      }									\
31423464ebd5Sriastradh   }									\
31433464ebd5Sriastradh   else if (swapBytes) {						\
31443464ebd5Sriastradh      const TYPE *s = (const TYPE *) src;				\
31453464ebd5Sriastradh      GLuint i;								\
31463464ebd5Sriastradh      for (i = 0; i < n; i++) {						\
31473464ebd5Sriastradh         TYPE value = s[SRC_INDEX];					\
31483464ebd5Sriastradh         if (sizeof(TYPE) == 2) {					\
31493464ebd5Sriastradh            SWAP2BYTE(value);						\
31503464ebd5Sriastradh         }								\
31513464ebd5Sriastradh         else if (sizeof(TYPE) == 4) {					\
31523464ebd5Sriastradh            SWAP4BYTE(value);						\
31533464ebd5Sriastradh         }								\
31543464ebd5Sriastradh         if (intFormat)							\
31553464ebd5Sriastradh            rgba[i][DST_INDEX] = (GLfloat) value;			\
31563464ebd5Sriastradh         else								\
31573464ebd5Sriastradh            rgba[i][DST_INDEX] = (GLfloat) CONVERSION(value);		\
31583464ebd5Sriastradh         s += stride;							\
31593464ebd5Sriastradh      }									\
31603464ebd5Sriastradh   }									\
31613464ebd5Sriastradh   else {								\
31623464ebd5Sriastradh      const TYPE *s = (const TYPE *) src;				\
31633464ebd5Sriastradh      GLuint i;								\
31643464ebd5Sriastradh      if (intFormat) {							\
31653464ebd5Sriastradh         for (i = 0; i < n; i++) {					\
31663464ebd5Sriastradh            rgba[i][DST_INDEX] = (GLfloat) s[SRC_INDEX];		\
31673464ebd5Sriastradh            s += stride;						\
31683464ebd5Sriastradh         }								\
31693464ebd5Sriastradh      }									\
31703464ebd5Sriastradh      else {								\
31713464ebd5Sriastradh         for (i = 0; i < n; i++) {					\
31723464ebd5Sriastradh            rgba[i][DST_INDEX] = (GLfloat) CONVERSION(s[SRC_INDEX]);	\
31733464ebd5Sriastradh            s += stride;						\
31743464ebd5Sriastradh         }								\
31753464ebd5Sriastradh      }									\
31763464ebd5Sriastradh   }
31773464ebd5Sriastradh
31783464ebd5Sriastradh   switch (srcType) {
31793464ebd5Sriastradh      case GL_UNSIGNED_BYTE:
31803464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
31813464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
31823464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
31833464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1.0F, 255, GLubyte, UBYTE_TO_FLOAT);
31843464ebd5Sriastradh         break;
31853464ebd5Sriastradh      case GL_BYTE:
3186af69d88dSmrg         PROCESS(rSrc, RCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOAT_TEX);
3187af69d88dSmrg         PROCESS(gSrc, GCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOAT_TEX);
3188af69d88dSmrg         PROCESS(bSrc, BCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOAT_TEX);
3189af69d88dSmrg         PROCESS(aSrc, ACOMP, 1.0F, 127, GLbyte, BYTE_TO_FLOAT_TEX);
31903464ebd5Sriastradh         break;
31913464ebd5Sriastradh      case GL_UNSIGNED_SHORT:
31923464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
31933464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
31943464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
31953464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1.0F, 0xffff, GLushort, USHORT_TO_FLOAT);
31963464ebd5Sriastradh         break;
31973464ebd5Sriastradh      case GL_SHORT:
3198af69d88dSmrg         PROCESS(rSrc, RCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOAT_TEX);
3199af69d88dSmrg         PROCESS(gSrc, GCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOAT_TEX);
3200af69d88dSmrg         PROCESS(bSrc, BCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOAT_TEX);
3201af69d88dSmrg         PROCESS(aSrc, ACOMP, 1.0F, 32767, GLshort, SHORT_TO_FLOAT_TEX);
32023464ebd5Sriastradh         break;
32033464ebd5Sriastradh      case GL_UNSIGNED_INT:
32043464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
32053464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
32063464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
32073464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1.0F, 0xffffffff, GLuint, UINT_TO_FLOAT);
32083464ebd5Sriastradh         break;
32093464ebd5Sriastradh      case GL_INT:
32103464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
32113464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
32123464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
32133464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1.0F, 2147483647, GLint, INT_TO_FLOAT);
32143464ebd5Sriastradh         break;
32153464ebd5Sriastradh      case GL_FLOAT:
32163464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
32173464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
32183464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
32193464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1.0F, 1.0F, GLfloat, (GLfloat));
32203464ebd5Sriastradh         break;
32213464ebd5Sriastradh      case GL_HALF_FLOAT_ARB:
32223464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
32233464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
32243464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
32253464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1.0F, 1.0F, GLhalfARB, _mesa_half_to_float);
32263464ebd5Sriastradh         break;
32273464ebd5Sriastradh      case GL_UNSIGNED_BYTE_3_3_2:
32283464ebd5Sriastradh         {
32293464ebd5Sriastradh            const GLubyte *ubsrc = (const GLubyte *) src;
32303464ebd5Sriastradh            GLuint i;
32313464ebd5Sriastradh            if (!intFormat) {
32323464ebd5Sriastradh               rs = 1.0F / 7.0F;
32333464ebd5Sriastradh               gs = 1.0F / 7.0F;
32343464ebd5Sriastradh               bs = 1.0F / 3.0F;
32353464ebd5Sriastradh            }
32363464ebd5Sriastradh            for (i = 0; i < n; i ++) {
32373464ebd5Sriastradh               GLubyte p = ubsrc[i];
32383464ebd5Sriastradh               rgba[i][rDst] = ((p >> 5)      ) * rs;
32393464ebd5Sriastradh               rgba[i][gDst] = ((p >> 2) & 0x7) * gs;
32403464ebd5Sriastradh               rgba[i][bDst] = ((p     ) & 0x3) * bs;
32413464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
32423464ebd5Sriastradh            }
32433464ebd5Sriastradh         }
32443464ebd5Sriastradh         break;
32453464ebd5Sriastradh      case GL_UNSIGNED_BYTE_2_3_3_REV:
32463464ebd5Sriastradh         {
32473464ebd5Sriastradh            const GLubyte *ubsrc = (const GLubyte *) src;
32483464ebd5Sriastradh            GLuint i;
32493464ebd5Sriastradh            if (!intFormat) {
32503464ebd5Sriastradh               rs = 1.0F / 7.0F;
32513464ebd5Sriastradh               gs = 1.0F / 7.0F;
32523464ebd5Sriastradh               bs = 1.0F / 3.0F;
32533464ebd5Sriastradh            }
32543464ebd5Sriastradh            for (i = 0; i < n; i ++) {
32553464ebd5Sriastradh               GLubyte p = ubsrc[i];
32563464ebd5Sriastradh               rgba[i][rDst] = ((p     ) & 0x7) * rs;
32573464ebd5Sriastradh               rgba[i][gDst] = ((p >> 3) & 0x7) * gs;
32583464ebd5Sriastradh               rgba[i][bDst] = ((p >> 6)      ) * bs;
32593464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
32603464ebd5Sriastradh            }
32613464ebd5Sriastradh         }
32623464ebd5Sriastradh         break;
32633464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_6_5:
32643464ebd5Sriastradh         if (!intFormat) {
32653464ebd5Sriastradh            rs = 1.0F / 31.0F;
32663464ebd5Sriastradh            gs = 1.0F / 63.0F;
32673464ebd5Sriastradh            bs = 1.0F / 31.0F;
32683464ebd5Sriastradh         }
32693464ebd5Sriastradh         if (swapBytes) {
32703464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
32713464ebd5Sriastradh            GLuint i;
32723464ebd5Sriastradh            for (i = 0; i < n; i ++) {
32733464ebd5Sriastradh               GLushort p = ussrc[i];
32743464ebd5Sriastradh               SWAP2BYTE(p);
32753464ebd5Sriastradh               rgba[i][rDst] = ((p >> 11)       ) * rs;
32763464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
32773464ebd5Sriastradh               rgba[i][bDst] = ((p      ) & 0x1f) * bs;
32783464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
32793464ebd5Sriastradh            }
32803464ebd5Sriastradh         }
32813464ebd5Sriastradh         else {
32823464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
32833464ebd5Sriastradh            GLuint i;
32843464ebd5Sriastradh            for (i = 0; i < n; i ++) {
32853464ebd5Sriastradh               GLushort p = ussrc[i];
32863464ebd5Sriastradh               rgba[i][rDst] = ((p >> 11)       ) * rs;
32873464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
32883464ebd5Sriastradh               rgba[i][bDst] = ((p      ) & 0x1f) * bs;
32893464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
32903464ebd5Sriastradh            }
32913464ebd5Sriastradh         }
32923464ebd5Sriastradh         break;
32933464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_6_5_REV:
32943464ebd5Sriastradh         if (!intFormat) {
32953464ebd5Sriastradh            rs = 1.0F / 31.0F;
32963464ebd5Sriastradh            gs = 1.0F / 63.0F;
32973464ebd5Sriastradh            bs = 1.0F / 31.0F;
32983464ebd5Sriastradh         }
32993464ebd5Sriastradh         if (swapBytes) {
33003464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
33013464ebd5Sriastradh            GLuint i;
33023464ebd5Sriastradh            for (i = 0; i < n; i ++) {
33033464ebd5Sriastradh               GLushort p = ussrc[i];
33043464ebd5Sriastradh               SWAP2BYTE(p);
33053464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x1f) * rs;
33063464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
33073464ebd5Sriastradh               rgba[i][bDst] = ((p >> 11)       ) * bs;
33083464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
33093464ebd5Sriastradh            }
33103464ebd5Sriastradh         }
33113464ebd5Sriastradh         else {
33123464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
33133464ebd5Sriastradh            GLuint i;
33143464ebd5Sriastradh            for (i = 0; i < n; i ++) {
33153464ebd5Sriastradh               GLushort p = ussrc[i];
33163464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x1f) * rs;
33173464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
33183464ebd5Sriastradh               rgba[i][bDst] = ((p >> 11)       ) * bs;
33193464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
33203464ebd5Sriastradh            }
33213464ebd5Sriastradh         }
33223464ebd5Sriastradh         break;
33233464ebd5Sriastradh      case GL_UNSIGNED_SHORT_4_4_4_4:
33243464ebd5Sriastradh         if (!intFormat) {
33253464ebd5Sriastradh            rs = gs = bs = as = 1.0F / 15.0F;
33263464ebd5Sriastradh         }
33273464ebd5Sriastradh         if (swapBytes) {
33283464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
33293464ebd5Sriastradh            GLuint i;
33303464ebd5Sriastradh            for (i = 0; i < n; i ++) {
33313464ebd5Sriastradh               GLushort p = ussrc[i];
33323464ebd5Sriastradh               SWAP2BYTE(p);
33333464ebd5Sriastradh               rgba[i][rDst] = ((p >> 12)      ) * rs;
33343464ebd5Sriastradh               rgba[i][gDst] = ((p >>  8) & 0xf) * gs;
33353464ebd5Sriastradh               rgba[i][bDst] = ((p >>  4) & 0xf) * bs;
33363464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0xf) * as;
33373464ebd5Sriastradh            }
33383464ebd5Sriastradh         }
33393464ebd5Sriastradh         else {
33403464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
33413464ebd5Sriastradh            GLuint i;
33423464ebd5Sriastradh            for (i = 0; i < n; i ++) {
33433464ebd5Sriastradh               GLushort p = ussrc[i];
33443464ebd5Sriastradh               rgba[i][rDst] = ((p >> 12)      ) * rs;
33453464ebd5Sriastradh               rgba[i][gDst] = ((p >>  8) & 0xf) * gs;
33463464ebd5Sriastradh               rgba[i][bDst] = ((p >>  4) & 0xf) * bs;
33473464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0xf) * as;
33483464ebd5Sriastradh            }
33493464ebd5Sriastradh         }
33503464ebd5Sriastradh         break;
33513464ebd5Sriastradh      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
33523464ebd5Sriastradh         if (!intFormat) {
33533464ebd5Sriastradh            rs = gs = bs = as = 1.0F / 15.0F;
33543464ebd5Sriastradh         }
33553464ebd5Sriastradh         if (swapBytes) {
33563464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
33573464ebd5Sriastradh            GLuint i;
33583464ebd5Sriastradh            for (i = 0; i < n; i ++) {
33593464ebd5Sriastradh               GLushort p = ussrc[i];
33603464ebd5Sriastradh               SWAP2BYTE(p);
33613464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0xf) * rs;
33623464ebd5Sriastradh               rgba[i][gDst] = ((p >>  4) & 0xf) * gs;
33633464ebd5Sriastradh               rgba[i][bDst] = ((p >>  8) & 0xf) * bs;
33643464ebd5Sriastradh               rgba[i][aDst] = ((p >> 12)      ) * as;
33653464ebd5Sriastradh            }
33663464ebd5Sriastradh         }
33673464ebd5Sriastradh         else {
33683464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
33693464ebd5Sriastradh            GLuint i;
33703464ebd5Sriastradh            for (i = 0; i < n; i ++) {
33713464ebd5Sriastradh               GLushort p = ussrc[i];
33723464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0xf) * rs;
33733464ebd5Sriastradh               rgba[i][gDst] = ((p >>  4) & 0xf) * gs;
33743464ebd5Sriastradh               rgba[i][bDst] = ((p >>  8) & 0xf) * bs;
33753464ebd5Sriastradh               rgba[i][aDst] = ((p >> 12)      ) * as;
33763464ebd5Sriastradh            }
33773464ebd5Sriastradh         }
33783464ebd5Sriastradh         break;
33793464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_5_5_1:
33803464ebd5Sriastradh         if (!intFormat) {
33813464ebd5Sriastradh            rs = gs = bs = 1.0F / 31.0F;
33823464ebd5Sriastradh         }
33833464ebd5Sriastradh         if (swapBytes) {
33843464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
33853464ebd5Sriastradh            GLuint i;
33863464ebd5Sriastradh            for (i = 0; i < n; i ++) {
33873464ebd5Sriastradh               GLushort p = ussrc[i];
33883464ebd5Sriastradh               SWAP2BYTE(p);
33893464ebd5Sriastradh               rgba[i][rDst] = ((p >> 11)       ) * rs;
33903464ebd5Sriastradh               rgba[i][gDst] = ((p >>  6) & 0x1f) * gs;
33913464ebd5Sriastradh               rgba[i][bDst] = ((p >>  1) & 0x1f) * bs;
33923464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0x1)  * as;
33933464ebd5Sriastradh            }
33943464ebd5Sriastradh         }
33953464ebd5Sriastradh         else {
33963464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
33973464ebd5Sriastradh            GLuint i;
33983464ebd5Sriastradh            for (i = 0; i < n; i ++) {
33993464ebd5Sriastradh               GLushort p = ussrc[i];
34003464ebd5Sriastradh               rgba[i][rDst] = ((p >> 11)       ) * rs;
34013464ebd5Sriastradh               rgba[i][gDst] = ((p >>  6) & 0x1f) * gs;
34023464ebd5Sriastradh               rgba[i][bDst] = ((p >>  1) & 0x1f) * bs;
34033464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0x1)  * as;
34043464ebd5Sriastradh            }
34053464ebd5Sriastradh         }
34063464ebd5Sriastradh         break;
34073464ebd5Sriastradh      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
34083464ebd5Sriastradh         if (!intFormat) {
34093464ebd5Sriastradh            rs = gs = bs = 1.0F / 31.0F;
34103464ebd5Sriastradh         }
34113464ebd5Sriastradh         if (swapBytes) {
34123464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
34133464ebd5Sriastradh            GLuint i;
34143464ebd5Sriastradh            for (i = 0; i < n; i ++) {
34153464ebd5Sriastradh               GLushort p = ussrc[i];
34163464ebd5Sriastradh               SWAP2BYTE(p);
34173464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x1f) * rs;
34183464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x1f) * gs;
34193464ebd5Sriastradh               rgba[i][bDst] = ((p >> 10) & 0x1f) * bs;
34203464ebd5Sriastradh               rgba[i][aDst] = ((p >> 15)       ) * as;
34213464ebd5Sriastradh            }
34223464ebd5Sriastradh         }
34233464ebd5Sriastradh         else {
34243464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
34253464ebd5Sriastradh            GLuint i;
34263464ebd5Sriastradh            for (i = 0; i < n; i ++) {
34273464ebd5Sriastradh               GLushort p = ussrc[i];
34283464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x1f) * rs;
34293464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x1f) * gs;
34303464ebd5Sriastradh               rgba[i][bDst] = ((p >> 10) & 0x1f) * bs;
34313464ebd5Sriastradh               rgba[i][aDst] = ((p >> 15)       ) * as;
34323464ebd5Sriastradh            }
34333464ebd5Sriastradh         }
34343464ebd5Sriastradh         break;
34353464ebd5Sriastradh      case GL_UNSIGNED_INT_8_8_8_8:
34363464ebd5Sriastradh         if (swapBytes) {
34373464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
34383464ebd5Sriastradh            GLuint i;
34393464ebd5Sriastradh            if (intFormat) {
34403464ebd5Sriastradh               for (i = 0; i < n; i ++) {
34413464ebd5Sriastradh                  GLuint p = uisrc[i];
34423464ebd5Sriastradh                  rgba[i][rDst] = (GLfloat) ((p      ) & 0xff);
34433464ebd5Sriastradh                  rgba[i][gDst] = (GLfloat) ((p >>  8) & 0xff);
34443464ebd5Sriastradh                  rgba[i][bDst] = (GLfloat) ((p >> 16) & 0xff);
34453464ebd5Sriastradh                  rgba[i][aDst] = (GLfloat) ((p >> 24)       );
34463464ebd5Sriastradh               }
34473464ebd5Sriastradh            }
34483464ebd5Sriastradh            else {
34493464ebd5Sriastradh               for (i = 0; i < n; i ++) {
34503464ebd5Sriastradh                  GLuint p = uisrc[i];
34513464ebd5Sriastradh                  rgba[i][rDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
34523464ebd5Sriastradh                  rgba[i][gDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
34533464ebd5Sriastradh                  rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
34543464ebd5Sriastradh                  rgba[i][aDst] = UBYTE_TO_FLOAT((p >> 24)       );
34553464ebd5Sriastradh               }
34563464ebd5Sriastradh            }
34573464ebd5Sriastradh         }
34583464ebd5Sriastradh         else {
34593464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
34603464ebd5Sriastradh            GLuint i;
34613464ebd5Sriastradh            if (intFormat) {
34623464ebd5Sriastradh               for (i = 0; i < n; i ++) {
34633464ebd5Sriastradh                  GLuint p = uisrc[i];
34643464ebd5Sriastradh                  rgba[i][rDst] = (GLfloat) ((p >> 24)       );
34653464ebd5Sriastradh                  rgba[i][gDst] = (GLfloat) ((p >> 16) & 0xff);
34663464ebd5Sriastradh                  rgba[i][bDst] = (GLfloat) ((p >>  8) & 0xff);
34673464ebd5Sriastradh                  rgba[i][aDst] = (GLfloat) ((p      ) & 0xff);
34683464ebd5Sriastradh               }
34693464ebd5Sriastradh            }
34703464ebd5Sriastradh            else {
34713464ebd5Sriastradh               for (i = 0; i < n; i ++) {
34723464ebd5Sriastradh                  GLuint p = uisrc[i];
34733464ebd5Sriastradh                  rgba[i][rDst] = UBYTE_TO_FLOAT((p >> 24)       );
34743464ebd5Sriastradh                  rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
34753464ebd5Sriastradh                  rgba[i][bDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
34763464ebd5Sriastradh                  rgba[i][aDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
34773464ebd5Sriastradh               }
34783464ebd5Sriastradh            }
34793464ebd5Sriastradh         }
34803464ebd5Sriastradh         break;
34813464ebd5Sriastradh      case GL_UNSIGNED_INT_8_8_8_8_REV:
34823464ebd5Sriastradh         if (swapBytes) {
34833464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
34843464ebd5Sriastradh            GLuint i;
34853464ebd5Sriastradh            if (intFormat) {
34863464ebd5Sriastradh               for (i = 0; i < n; i ++) {
34873464ebd5Sriastradh                  GLuint p = uisrc[i];
34883464ebd5Sriastradh                  rgba[i][rDst] = (GLfloat) ((p >> 24)       );
34893464ebd5Sriastradh                  rgba[i][gDst] = (GLfloat) ((p >> 16) & 0xff);
34903464ebd5Sriastradh                  rgba[i][bDst] = (GLfloat) ((p >>  8) & 0xff);
34913464ebd5Sriastradh                  rgba[i][aDst] = (GLfloat) ((p      ) & 0xff);
34923464ebd5Sriastradh               }
34933464ebd5Sriastradh            }
34943464ebd5Sriastradh            else {
34953464ebd5Sriastradh               for (i = 0; i < n; i ++) {
34963464ebd5Sriastradh                  GLuint p = uisrc[i];
34973464ebd5Sriastradh                  rgba[i][rDst] = UBYTE_TO_FLOAT((p >> 24)       );
34983464ebd5Sriastradh                  rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
34993464ebd5Sriastradh                  rgba[i][bDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
35003464ebd5Sriastradh                  rgba[i][aDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
35013464ebd5Sriastradh               }
35023464ebd5Sriastradh            }
35033464ebd5Sriastradh         }
35043464ebd5Sriastradh         else {
35053464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
35063464ebd5Sriastradh            GLuint i;
35073464ebd5Sriastradh            if (intFormat) {
35083464ebd5Sriastradh               for (i = 0; i < n; i ++) {
35093464ebd5Sriastradh                  GLuint p = uisrc[i];
35103464ebd5Sriastradh                  rgba[i][rDst] = (GLfloat) ((p      ) & 0xff);
35113464ebd5Sriastradh                  rgba[i][gDst] = (GLfloat) ((p >>  8) & 0xff);
35123464ebd5Sriastradh                  rgba[i][bDst] = (GLfloat) ((p >> 16) & 0xff);
35133464ebd5Sriastradh                  rgba[i][aDst] = (GLfloat) ((p >> 24)       );
35143464ebd5Sriastradh               }
35153464ebd5Sriastradh            }
35163464ebd5Sriastradh            else {
35173464ebd5Sriastradh               for (i = 0; i < n; i ++) {
35183464ebd5Sriastradh                  GLuint p = uisrc[i];
35193464ebd5Sriastradh                  rgba[i][rDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
35203464ebd5Sriastradh                  rgba[i][gDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
35213464ebd5Sriastradh                  rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
35223464ebd5Sriastradh                  rgba[i][aDst] = UBYTE_TO_FLOAT((p >> 24)       );
35233464ebd5Sriastradh               }
35243464ebd5Sriastradh            }
35253464ebd5Sriastradh         }
35263464ebd5Sriastradh         break;
35273464ebd5Sriastradh      case GL_UNSIGNED_INT_10_10_10_2:
35283464ebd5Sriastradh         if (!intFormat) {
35293464ebd5Sriastradh            rs = 1.0F / 1023.0F;
35303464ebd5Sriastradh            gs = 1.0F / 1023.0F;
35313464ebd5Sriastradh            bs = 1.0F / 1023.0F;
35323464ebd5Sriastradh            as = 1.0F / 3.0F;
35333464ebd5Sriastradh         }
35343464ebd5Sriastradh         if (swapBytes) {
35353464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
35363464ebd5Sriastradh            GLuint i;
35373464ebd5Sriastradh            for (i = 0; i < n; i ++) {
35383464ebd5Sriastradh               GLuint p = uisrc[i];
35393464ebd5Sriastradh               SWAP4BYTE(p);
35403464ebd5Sriastradh               rgba[i][rDst] = ((p >> 22)        ) * rs;
35413464ebd5Sriastradh               rgba[i][gDst] = ((p >> 12) & 0x3ff) * gs;
35423464ebd5Sriastradh               rgba[i][bDst] = ((p >>  2) & 0x3ff) * bs;
35433464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0x3  ) * as;
35443464ebd5Sriastradh            }
35453464ebd5Sriastradh         }
35463464ebd5Sriastradh         else {
35473464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
35483464ebd5Sriastradh            GLuint i;
35493464ebd5Sriastradh            for (i = 0; i < n; i ++) {
35503464ebd5Sriastradh               GLuint p = uisrc[i];
35513464ebd5Sriastradh               rgba[i][rDst] = ((p >> 22)        ) * rs;
35523464ebd5Sriastradh               rgba[i][gDst] = ((p >> 12) & 0x3ff) * gs;
35533464ebd5Sriastradh               rgba[i][bDst] = ((p >>  2) & 0x3ff) * bs;
35543464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0x3  ) * as;
35553464ebd5Sriastradh            }
35563464ebd5Sriastradh         }
35573464ebd5Sriastradh         break;
35583464ebd5Sriastradh      case GL_UNSIGNED_INT_2_10_10_10_REV:
35593464ebd5Sriastradh         if (!intFormat) {
35603464ebd5Sriastradh            rs = 1.0F / 1023.0F;
35613464ebd5Sriastradh            gs = 1.0F / 1023.0F;
35623464ebd5Sriastradh            bs = 1.0F / 1023.0F;
35633464ebd5Sriastradh            as = 1.0F / 3.0F;
35643464ebd5Sriastradh         }
35653464ebd5Sriastradh         if (swapBytes) {
35663464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
35673464ebd5Sriastradh            GLuint i;
35683464ebd5Sriastradh            for (i = 0; i < n; i ++) {
35693464ebd5Sriastradh               GLuint p = uisrc[i];
35703464ebd5Sriastradh               SWAP4BYTE(p);
35713464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x3ff) * rs;
35723464ebd5Sriastradh               rgba[i][gDst] = ((p >> 10) & 0x3ff) * gs;
35733464ebd5Sriastradh               rgba[i][bDst] = ((p >> 20) & 0x3ff) * bs;
3574af69d88dSmrg               if (aSrc < 0) {
3575af69d88dSmrg                  rgba[i][aDst] = 1.0F;
3576af69d88dSmrg               } else {
3577af69d88dSmrg                  rgba[i][aDst] = (p >> 30) * as;
3578af69d88dSmrg               }
35793464ebd5Sriastradh            }
35803464ebd5Sriastradh         }
35813464ebd5Sriastradh         else {
35823464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
35833464ebd5Sriastradh            GLuint i;
35843464ebd5Sriastradh            for (i = 0; i < n; i ++) {
35853464ebd5Sriastradh               GLuint p = uisrc[i];
35863464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x3ff) * rs;
35873464ebd5Sriastradh               rgba[i][gDst] = ((p >> 10) & 0x3ff) * gs;
35883464ebd5Sriastradh               rgba[i][bDst] = ((p >> 20) & 0x3ff) * bs;
3589af69d88dSmrg               if (aSrc < 0) {
3590af69d88dSmrg                  rgba[i][aDst] = 1.0F;
3591af69d88dSmrg               } else {
3592af69d88dSmrg                  rgba[i][aDst] = (p >> 30) * as;
3593af69d88dSmrg               }
35943464ebd5Sriastradh            }
35953464ebd5Sriastradh         }
35963464ebd5Sriastradh         break;
35973464ebd5Sriastradh      case GL_UNSIGNED_INT_5_9_9_9_REV:
35983464ebd5Sriastradh         if (swapBytes) {
35993464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
36003464ebd5Sriastradh            GLuint i;
36013464ebd5Sriastradh            GLfloat f[3];
36023464ebd5Sriastradh            for (i = 0; i < n; i ++) {
36033464ebd5Sriastradh               GLuint p = uisrc[i];
36043464ebd5Sriastradh               SWAP4BYTE(p);
36053464ebd5Sriastradh               rgb9e5_to_float3(p, f);
36063464ebd5Sriastradh               rgba[i][rDst] = f[0];
36073464ebd5Sriastradh               rgba[i][gDst] = f[1];
36083464ebd5Sriastradh               rgba[i][bDst] = f[2];
36093464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
36103464ebd5Sriastradh            }
36113464ebd5Sriastradh         }
36123464ebd5Sriastradh         else {
36133464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
36143464ebd5Sriastradh            GLuint i;
36153464ebd5Sriastradh            GLfloat f[3];
36163464ebd5Sriastradh            for (i = 0; i < n; i ++) {
36173464ebd5Sriastradh               rgb9e5_to_float3(uisrc[i], f);
36183464ebd5Sriastradh               rgba[i][rDst] = f[0];
36193464ebd5Sriastradh               rgba[i][gDst] = f[1];
36203464ebd5Sriastradh               rgba[i][bDst] = f[2];
36213464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
36223464ebd5Sriastradh            }
36233464ebd5Sriastradh         }
36243464ebd5Sriastradh         break;
36253464ebd5Sriastradh      case GL_UNSIGNED_INT_10F_11F_11F_REV:
36263464ebd5Sriastradh         if (swapBytes) {
36273464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
36283464ebd5Sriastradh            GLuint i;
36293464ebd5Sriastradh            GLfloat f[3];
36303464ebd5Sriastradh            for (i = 0; i < n; i ++) {
36313464ebd5Sriastradh               GLuint p = uisrc[i];
36323464ebd5Sriastradh               SWAP4BYTE(p);
36333464ebd5Sriastradh               r11g11b10f_to_float3(p, f);
36343464ebd5Sriastradh               rgba[i][rDst] = f[0];
36353464ebd5Sriastradh               rgba[i][gDst] = f[1];
36363464ebd5Sriastradh               rgba[i][bDst] = f[2];
36373464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
36383464ebd5Sriastradh            }
36393464ebd5Sriastradh         }
36403464ebd5Sriastradh         else {
36413464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
36423464ebd5Sriastradh            GLuint i;
36433464ebd5Sriastradh            GLfloat f[3];
36443464ebd5Sriastradh            for (i = 0; i < n; i ++) {
36453464ebd5Sriastradh               r11g11b10f_to_float3(uisrc[i], f);
36463464ebd5Sriastradh               rgba[i][rDst] = f[0];
36473464ebd5Sriastradh               rgba[i][gDst] = f[1];
36483464ebd5Sriastradh               rgba[i][bDst] = f[2];
36493464ebd5Sriastradh               rgba[i][aDst] = 1.0F;
36503464ebd5Sriastradh            }
36513464ebd5Sriastradh         }
36523464ebd5Sriastradh         break;
36533464ebd5Sriastradh      default:
36543464ebd5Sriastradh         _mesa_problem(NULL, "bad srcType in extract float data");
36553464ebd5Sriastradh         break;
36563464ebd5Sriastradh   }
36573464ebd5Sriastradh#undef PROCESS
36583464ebd5Sriastradh}
36593464ebd5Sriastradh
36603464ebd5Sriastradh
3661af69d88dSmrgstatic inline GLuint
36623464ebd5Sriastradhclamp_float_to_uint(GLfloat f)
36633464ebd5Sriastradh{
3664af69d88dSmrg   return f < 0.0F ? 0 : F_TO_I(f);
36653464ebd5Sriastradh}
36663464ebd5Sriastradh
36673464ebd5Sriastradh
3668af69d88dSmrgstatic inline GLuint
36693464ebd5Sriastradhclamp_half_to_uint(GLhalfARB h)
36703464ebd5Sriastradh{
36713464ebd5Sriastradh   GLfloat f = _mesa_half_to_float(h);
3672af69d88dSmrg   return f < 0.0F ? 0 : F_TO_I(f);
36733464ebd5Sriastradh}
36743464ebd5Sriastradh
36753464ebd5Sriastradh
36763464ebd5Sriastradh/**
36773464ebd5Sriastradh * \sa extract_float_rgba()
36783464ebd5Sriastradh */
36793464ebd5Sriastradhstatic void
36803464ebd5Sriastradhextract_uint_rgba(GLuint n, GLuint rgba[][4],
36813464ebd5Sriastradh                  GLenum srcFormat, GLenum srcType, const GLvoid *src,
36823464ebd5Sriastradh                  GLboolean swapBytes)
36833464ebd5Sriastradh{
36843464ebd5Sriastradh   GLint rSrc, gSrc, bSrc, aSrc;
36853464ebd5Sriastradh   GLint stride;
36863464ebd5Sriastradh   GLint rDst, bDst, gDst, aDst;
36873464ebd5Sriastradh
36883464ebd5Sriastradh   ASSERT(srcFormat == GL_RED ||
36893464ebd5Sriastradh          srcFormat == GL_GREEN ||
36903464ebd5Sriastradh          srcFormat == GL_BLUE ||
36913464ebd5Sriastradh          srcFormat == GL_ALPHA ||
36923464ebd5Sriastradh          srcFormat == GL_LUMINANCE ||
36933464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA ||
36943464ebd5Sriastradh          srcFormat == GL_INTENSITY ||
36953464ebd5Sriastradh          srcFormat == GL_RG ||
36963464ebd5Sriastradh          srcFormat == GL_RGB ||
36973464ebd5Sriastradh          srcFormat == GL_BGR ||
36983464ebd5Sriastradh          srcFormat == GL_RGBA ||
36993464ebd5Sriastradh          srcFormat == GL_BGRA ||
37003464ebd5Sriastradh          srcFormat == GL_ABGR_EXT ||
37013464ebd5Sriastradh          srcFormat == GL_RED_INTEGER_EXT ||
3702af69d88dSmrg          srcFormat == GL_RG_INTEGER ||
37033464ebd5Sriastradh          srcFormat == GL_GREEN_INTEGER_EXT ||
37043464ebd5Sriastradh          srcFormat == GL_BLUE_INTEGER_EXT ||
37053464ebd5Sriastradh          srcFormat == GL_ALPHA_INTEGER_EXT ||
37063464ebd5Sriastradh          srcFormat == GL_RGB_INTEGER_EXT ||
37073464ebd5Sriastradh          srcFormat == GL_RGBA_INTEGER_EXT ||
37083464ebd5Sriastradh          srcFormat == GL_BGR_INTEGER_EXT ||
37093464ebd5Sriastradh          srcFormat == GL_BGRA_INTEGER_EXT ||
37103464ebd5Sriastradh          srcFormat == GL_LUMINANCE_INTEGER_EXT ||
37113464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
37123464ebd5Sriastradh
37133464ebd5Sriastradh   ASSERT(srcType == GL_UNSIGNED_BYTE ||
37143464ebd5Sriastradh          srcType == GL_BYTE ||
37153464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT ||
37163464ebd5Sriastradh          srcType == GL_SHORT ||
37173464ebd5Sriastradh          srcType == GL_UNSIGNED_INT ||
37183464ebd5Sriastradh          srcType == GL_INT ||
37193464ebd5Sriastradh          srcType == GL_HALF_FLOAT_ARB ||
37203464ebd5Sriastradh          srcType == GL_FLOAT ||
37213464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
37223464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
37233464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
37243464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
37253464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
37263464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
37273464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
37283464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
37293464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
37303464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
37313464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
37323464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
37333464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
37343464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
37353464ebd5Sriastradh
37363464ebd5Sriastradh   get_component_mapping(srcFormat,
37373464ebd5Sriastradh                         &rSrc, &gSrc, &bSrc, &aSrc,
37383464ebd5Sriastradh                         &rDst, &gDst, &bDst, &aDst);
37393464ebd5Sriastradh
37403464ebd5Sriastradh   stride = _mesa_components_in_format(srcFormat);
37413464ebd5Sriastradh
37423464ebd5Sriastradh#define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT, TYPE, CONVERSION)	\
37433464ebd5Sriastradh   if ((SRC_INDEX) < 0) {						\
37443464ebd5Sriastradh      GLuint i;								\
37453464ebd5Sriastradh      for (i = 0; i < n; i++) {						\
37463464ebd5Sriastradh         rgba[i][DST_INDEX] = DEFAULT;					\
37473464ebd5Sriastradh      }									\
37483464ebd5Sriastradh   }									\
37493464ebd5Sriastradh   else if (swapBytes) {						\
37503464ebd5Sriastradh      const TYPE *s = (const TYPE *) src;				\
37513464ebd5Sriastradh      GLuint i;								\
37523464ebd5Sriastradh      for (i = 0; i < n; i++) {						\
37533464ebd5Sriastradh         TYPE value = s[SRC_INDEX];					\
37543464ebd5Sriastradh         if (sizeof(TYPE) == 2) {					\
37553464ebd5Sriastradh            SWAP2BYTE(value);						\
37563464ebd5Sriastradh         }								\
37573464ebd5Sriastradh         else if (sizeof(TYPE) == 4) {					\
37583464ebd5Sriastradh            SWAP4BYTE(value);						\
37593464ebd5Sriastradh         }								\
37603464ebd5Sriastradh         rgba[i][DST_INDEX] = CONVERSION(value);                        \
37613464ebd5Sriastradh         s += stride;							\
37623464ebd5Sriastradh      }									\
37633464ebd5Sriastradh   }									\
37643464ebd5Sriastradh   else {								\
37653464ebd5Sriastradh      const TYPE *s = (const TYPE *) src;				\
37663464ebd5Sriastradh      GLuint i;								\
37673464ebd5Sriastradh      for (i = 0; i < n; i++) {						\
37683464ebd5Sriastradh         rgba[i][DST_INDEX] = CONVERSION(s[SRC_INDEX]);			\
37693464ebd5Sriastradh         s += stride;							\
37703464ebd5Sriastradh      }									\
37713464ebd5Sriastradh   }
37723464ebd5Sriastradh
37733464ebd5Sriastradh   switch (srcType) {
37743464ebd5Sriastradh      case GL_UNSIGNED_BYTE:
37753464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0, GLubyte, (GLuint));
37763464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0, GLubyte, (GLuint));
37773464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0, GLubyte, (GLuint));
37783464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1, GLubyte, (GLuint));
37793464ebd5Sriastradh         break;
37803464ebd5Sriastradh      case GL_BYTE:
3781af69d88dSmrg         PROCESS(rSrc, RCOMP, 0, GLbyte, (GLuint));
3782af69d88dSmrg         PROCESS(gSrc, GCOMP, 0, GLbyte, (GLuint));
3783af69d88dSmrg         PROCESS(bSrc, BCOMP, 0, GLbyte, (GLuint));
3784af69d88dSmrg         PROCESS(aSrc, ACOMP, 1, GLbyte, (GLuint));
37853464ebd5Sriastradh         break;
37863464ebd5Sriastradh      case GL_UNSIGNED_SHORT:
37873464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0, GLushort, (GLuint));
37883464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0, GLushort, (GLuint));
37893464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0, GLushort, (GLuint));
37903464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1, GLushort, (GLuint));
37913464ebd5Sriastradh         break;
37923464ebd5Sriastradh      case GL_SHORT:
3793af69d88dSmrg         PROCESS(rSrc, RCOMP, 0, GLshort, (GLuint));
3794af69d88dSmrg         PROCESS(gSrc, GCOMP, 0, GLshort, (GLuint));
3795af69d88dSmrg         PROCESS(bSrc, BCOMP, 0, GLshort, (GLuint));
3796af69d88dSmrg         PROCESS(aSrc, ACOMP, 1, GLshort, (GLuint));
37973464ebd5Sriastradh         break;
37983464ebd5Sriastradh      case GL_UNSIGNED_INT:
37993464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0, GLuint, (GLuint));
38003464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0, GLuint, (GLuint));
38013464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0, GLuint, (GLuint));
38023464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1, GLuint, (GLuint));
38033464ebd5Sriastradh         break;
38043464ebd5Sriastradh      case GL_INT:
3805af69d88dSmrg         PROCESS(rSrc, RCOMP, 0, GLint, (GLuint));
3806af69d88dSmrg         PROCESS(gSrc, GCOMP, 0, GLint, (GLuint));
3807af69d88dSmrg         PROCESS(bSrc, BCOMP, 0, GLint, (GLuint));
3808af69d88dSmrg         PROCESS(aSrc, ACOMP, 1, GLint, (GLuint));
38093464ebd5Sriastradh         break;
38103464ebd5Sriastradh      case GL_FLOAT:
38113464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0, GLfloat, clamp_float_to_uint);
38123464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0, GLfloat, clamp_float_to_uint);
38133464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0, GLfloat, clamp_float_to_uint);
38143464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1, GLfloat, clamp_float_to_uint);
38153464ebd5Sriastradh         break;
38163464ebd5Sriastradh      case GL_HALF_FLOAT_ARB:
38173464ebd5Sriastradh         PROCESS(rSrc, RCOMP, 0, GLhalfARB, clamp_half_to_uint);
38183464ebd5Sriastradh         PROCESS(gSrc, GCOMP, 0, GLhalfARB, clamp_half_to_uint);
38193464ebd5Sriastradh         PROCESS(bSrc, BCOMP, 0, GLhalfARB, clamp_half_to_uint);
38203464ebd5Sriastradh         PROCESS(aSrc, ACOMP, 1, GLhalfARB, clamp_half_to_uint);
38213464ebd5Sriastradh         break;
38223464ebd5Sriastradh      case GL_UNSIGNED_BYTE_3_3_2:
38233464ebd5Sriastradh         {
38243464ebd5Sriastradh            const GLubyte *ubsrc = (const GLubyte *) src;
38253464ebd5Sriastradh            GLuint i;
38263464ebd5Sriastradh            for (i = 0; i < n; i ++) {
38273464ebd5Sriastradh               GLubyte p = ubsrc[i];
38283464ebd5Sriastradh               rgba[i][rDst] = ((p >> 5)      );
38293464ebd5Sriastradh               rgba[i][gDst] = ((p >> 2) & 0x7);
38303464ebd5Sriastradh               rgba[i][bDst] = ((p     ) & 0x3);
38313464ebd5Sriastradh               rgba[i][aDst] = 1;
38323464ebd5Sriastradh            }
38333464ebd5Sriastradh         }
38343464ebd5Sriastradh         break;
38353464ebd5Sriastradh      case GL_UNSIGNED_BYTE_2_3_3_REV:
38363464ebd5Sriastradh         {
38373464ebd5Sriastradh            const GLubyte *ubsrc = (const GLubyte *) src;
38383464ebd5Sriastradh            GLuint i;
38393464ebd5Sriastradh            for (i = 0; i < n; i ++) {
38403464ebd5Sriastradh               GLubyte p = ubsrc[i];
38413464ebd5Sriastradh               rgba[i][rDst] = ((p     ) & 0x7);
38423464ebd5Sriastradh               rgba[i][gDst] = ((p >> 3) & 0x7);
38433464ebd5Sriastradh               rgba[i][bDst] = ((p >> 6)      );
38443464ebd5Sriastradh               rgba[i][aDst] = 1;
38453464ebd5Sriastradh            }
38463464ebd5Sriastradh         }
38473464ebd5Sriastradh         break;
38483464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_6_5:
38493464ebd5Sriastradh         if (swapBytes) {
38503464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
38513464ebd5Sriastradh            GLuint i;
38523464ebd5Sriastradh            for (i = 0; i < n; i ++) {
38533464ebd5Sriastradh               GLushort p = ussrc[i];
38543464ebd5Sriastradh               SWAP2BYTE(p);
38553464ebd5Sriastradh               rgba[i][rDst] = ((p >> 11)       );
38563464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x3f);
38573464ebd5Sriastradh               rgba[i][bDst] = ((p      ) & 0x1f);
38583464ebd5Sriastradh               rgba[i][aDst] = 1;
38593464ebd5Sriastradh            }
38603464ebd5Sriastradh         }
38613464ebd5Sriastradh         else {
38623464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
38633464ebd5Sriastradh            GLuint i;
38643464ebd5Sriastradh            for (i = 0; i < n; i ++) {
38653464ebd5Sriastradh               GLushort p = ussrc[i];
38663464ebd5Sriastradh               rgba[i][rDst] = ((p >> 11)       );
38673464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x3f);
38683464ebd5Sriastradh               rgba[i][bDst] = ((p      ) & 0x1f);
38693464ebd5Sriastradh               rgba[i][aDst] = 1;
38703464ebd5Sriastradh            }
38713464ebd5Sriastradh         }
38723464ebd5Sriastradh         break;
38733464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_6_5_REV:
38743464ebd5Sriastradh         if (swapBytes) {
38753464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
38763464ebd5Sriastradh            GLuint i;
38773464ebd5Sriastradh            for (i = 0; i < n; i ++) {
38783464ebd5Sriastradh               GLushort p = ussrc[i];
38793464ebd5Sriastradh               SWAP2BYTE(p);
38803464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x1f);
38813464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x3f);
38823464ebd5Sriastradh               rgba[i][bDst] = ((p >> 11)       );
38833464ebd5Sriastradh               rgba[i][aDst] = 1;
38843464ebd5Sriastradh            }
38853464ebd5Sriastradh         }
38863464ebd5Sriastradh         else {
38873464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
38883464ebd5Sriastradh            GLuint i;
38893464ebd5Sriastradh            for (i = 0; i < n; i ++) {
38903464ebd5Sriastradh               GLushort p = ussrc[i];
38913464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x1f);
38923464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x3f);
38933464ebd5Sriastradh               rgba[i][bDst] = ((p >> 11)       );
38943464ebd5Sriastradh               rgba[i][aDst] = 1;
38953464ebd5Sriastradh            }
38963464ebd5Sriastradh         }
38973464ebd5Sriastradh         break;
38983464ebd5Sriastradh      case GL_UNSIGNED_SHORT_4_4_4_4:
38993464ebd5Sriastradh         if (swapBytes) {
39003464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
39013464ebd5Sriastradh            GLuint i;
39023464ebd5Sriastradh            for (i = 0; i < n; i ++) {
39033464ebd5Sriastradh               GLushort p = ussrc[i];
39043464ebd5Sriastradh               SWAP2BYTE(p);
39053464ebd5Sriastradh               rgba[i][rDst] = ((p >> 12)      );
39063464ebd5Sriastradh               rgba[i][gDst] = ((p >>  8) & 0xf);
39073464ebd5Sriastradh               rgba[i][bDst] = ((p >>  4) & 0xf);
39083464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0xf);
39093464ebd5Sriastradh            }
39103464ebd5Sriastradh         }
39113464ebd5Sriastradh         else {
39123464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
39133464ebd5Sriastradh            GLuint i;
39143464ebd5Sriastradh            for (i = 0; i < n; i ++) {
39153464ebd5Sriastradh               GLushort p = ussrc[i];
39163464ebd5Sriastradh               rgba[i][rDst] = ((p >> 12)      );
39173464ebd5Sriastradh               rgba[i][gDst] = ((p >>  8) & 0xf);
39183464ebd5Sriastradh               rgba[i][bDst] = ((p >>  4) & 0xf);
39193464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0xf);
39203464ebd5Sriastradh            }
39213464ebd5Sriastradh         }
39223464ebd5Sriastradh         break;
39233464ebd5Sriastradh      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
39243464ebd5Sriastradh         if (swapBytes) {
39253464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
39263464ebd5Sriastradh            GLuint i;
39273464ebd5Sriastradh            for (i = 0; i < n; i ++) {
39283464ebd5Sriastradh               GLushort p = ussrc[i];
39293464ebd5Sriastradh               SWAP2BYTE(p);
39303464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0xf);
39313464ebd5Sriastradh               rgba[i][gDst] = ((p >>  4) & 0xf);
39323464ebd5Sriastradh               rgba[i][bDst] = ((p >>  8) & 0xf);
39333464ebd5Sriastradh               rgba[i][aDst] = ((p >> 12)      );
39343464ebd5Sriastradh            }
39353464ebd5Sriastradh         }
39363464ebd5Sriastradh         else {
39373464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
39383464ebd5Sriastradh            GLuint i;
39393464ebd5Sriastradh            for (i = 0; i < n; i ++) {
39403464ebd5Sriastradh               GLushort p = ussrc[i];
39413464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0xf);
39423464ebd5Sriastradh               rgba[i][gDst] = ((p >>  4) & 0xf);
39433464ebd5Sriastradh               rgba[i][bDst] = ((p >>  8) & 0xf);
39443464ebd5Sriastradh               rgba[i][aDst] = ((p >> 12)      );
39453464ebd5Sriastradh            }
39463464ebd5Sriastradh         }
39473464ebd5Sriastradh         break;
39483464ebd5Sriastradh      case GL_UNSIGNED_SHORT_5_5_5_1:
39493464ebd5Sriastradh         if (swapBytes) {
39503464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
39513464ebd5Sriastradh            GLuint i;
39523464ebd5Sriastradh            for (i = 0; i < n; i ++) {
39533464ebd5Sriastradh               GLushort p = ussrc[i];
39543464ebd5Sriastradh               SWAP2BYTE(p);
39553464ebd5Sriastradh               rgba[i][rDst] = ((p >> 11)       );
39563464ebd5Sriastradh               rgba[i][gDst] = ((p >>  6) & 0x1f);
39573464ebd5Sriastradh               rgba[i][bDst] = ((p >>  1) & 0x1f);
39583464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0x1 );
39593464ebd5Sriastradh            }
39603464ebd5Sriastradh         }
39613464ebd5Sriastradh         else {
39623464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
39633464ebd5Sriastradh            GLuint i;
39643464ebd5Sriastradh            for (i = 0; i < n; i ++) {
39653464ebd5Sriastradh               GLushort p = ussrc[i];
39663464ebd5Sriastradh               rgba[i][rDst] = ((p >> 11)       );
39673464ebd5Sriastradh               rgba[i][gDst] = ((p >>  6) & 0x1f);
39683464ebd5Sriastradh               rgba[i][bDst] = ((p >>  1) & 0x1f);
39693464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0x1 );
39703464ebd5Sriastradh            }
39713464ebd5Sriastradh         }
39723464ebd5Sriastradh         break;
39733464ebd5Sriastradh      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
39743464ebd5Sriastradh         if (swapBytes) {
39753464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
39763464ebd5Sriastradh            GLuint i;
39773464ebd5Sriastradh            for (i = 0; i < n; i ++) {
39783464ebd5Sriastradh               GLushort p = ussrc[i];
39793464ebd5Sriastradh               SWAP2BYTE(p);
39803464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x1f);
39813464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x1f);
39823464ebd5Sriastradh               rgba[i][bDst] = ((p >> 10) & 0x1f);
39833464ebd5Sriastradh               rgba[i][aDst] = ((p >> 15)       );
39843464ebd5Sriastradh            }
39853464ebd5Sriastradh         }
39863464ebd5Sriastradh         else {
39873464ebd5Sriastradh            const GLushort *ussrc = (const GLushort *) src;
39883464ebd5Sriastradh            GLuint i;
39893464ebd5Sriastradh            for (i = 0; i < n; i ++) {
39903464ebd5Sriastradh               GLushort p = ussrc[i];
39913464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x1f);
39923464ebd5Sriastradh               rgba[i][gDst] = ((p >>  5) & 0x1f);
39933464ebd5Sriastradh               rgba[i][bDst] = ((p >> 10) & 0x1f);
39943464ebd5Sriastradh               rgba[i][aDst] = ((p >> 15)       );
39953464ebd5Sriastradh            }
39963464ebd5Sriastradh         }
39973464ebd5Sriastradh         break;
39983464ebd5Sriastradh      case GL_UNSIGNED_INT_8_8_8_8:
39993464ebd5Sriastradh         if (swapBytes) {
40003464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40013464ebd5Sriastradh            GLuint i;
40023464ebd5Sriastradh            for (i = 0; i < n; i ++) {
40033464ebd5Sriastradh               GLuint p = uisrc[i];
40043464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0xff);
40053464ebd5Sriastradh               rgba[i][gDst] = ((p >>  8) & 0xff);
40063464ebd5Sriastradh               rgba[i][bDst] = ((p >> 16) & 0xff);
40073464ebd5Sriastradh               rgba[i][aDst] = ((p >> 24)       );
40083464ebd5Sriastradh            }
40093464ebd5Sriastradh         }
40103464ebd5Sriastradh         else {
40113464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40123464ebd5Sriastradh            GLuint i;
40133464ebd5Sriastradh            for (i = 0; i < n; i ++) {
40143464ebd5Sriastradh               GLuint p = uisrc[i];
40153464ebd5Sriastradh               rgba[i][rDst] = ((p >> 24)       );
40163464ebd5Sriastradh               rgba[i][gDst] = ((p >> 16) & 0xff);
40173464ebd5Sriastradh               rgba[i][bDst] = ((p >>  8) & 0xff);
40183464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0xff);
40193464ebd5Sriastradh            }
40203464ebd5Sriastradh         }
40213464ebd5Sriastradh         break;
40223464ebd5Sriastradh      case GL_UNSIGNED_INT_8_8_8_8_REV:
40233464ebd5Sriastradh         if (swapBytes) {
40243464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40253464ebd5Sriastradh            GLuint i;
40263464ebd5Sriastradh            for (i = 0; i < n; i ++) {
40273464ebd5Sriastradh               GLuint p = uisrc[i];
40283464ebd5Sriastradh               rgba[i][rDst] = ((p >> 24)       );
40293464ebd5Sriastradh               rgba[i][gDst] = ((p >> 16) & 0xff);
40303464ebd5Sriastradh               rgba[i][bDst] = ((p >>  8) & 0xff);
40313464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0xff);
40323464ebd5Sriastradh            }
40333464ebd5Sriastradh         }
40343464ebd5Sriastradh         else {
40353464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40363464ebd5Sriastradh            GLuint i;
40373464ebd5Sriastradh            for (i = 0; i < n; i ++) {
40383464ebd5Sriastradh               GLuint p = uisrc[i];
40393464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0xff);
40403464ebd5Sriastradh               rgba[i][gDst] = ((p >>  8) & 0xff);
40413464ebd5Sriastradh               rgba[i][bDst] = ((p >> 16) & 0xff);
40423464ebd5Sriastradh               rgba[i][aDst] = ((p >> 24)       );
40433464ebd5Sriastradh            }
40443464ebd5Sriastradh         }
40453464ebd5Sriastradh         break;
40463464ebd5Sriastradh      case GL_UNSIGNED_INT_10_10_10_2:
40473464ebd5Sriastradh         if (swapBytes) {
40483464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40493464ebd5Sriastradh            GLuint i;
40503464ebd5Sriastradh            for (i = 0; i < n; i ++) {
40513464ebd5Sriastradh               GLuint p = uisrc[i];
40523464ebd5Sriastradh               SWAP4BYTE(p);
40533464ebd5Sriastradh               rgba[i][rDst] = ((p >> 22)        );
40543464ebd5Sriastradh               rgba[i][gDst] = ((p >> 12) & 0x3ff);
40553464ebd5Sriastradh               rgba[i][bDst] = ((p >>  2) & 0x3ff);
40563464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0x3  );
40573464ebd5Sriastradh            }
40583464ebd5Sriastradh         }
40593464ebd5Sriastradh         else {
40603464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40613464ebd5Sriastradh            GLuint i;
40623464ebd5Sriastradh            for (i = 0; i < n; i ++) {
40633464ebd5Sriastradh               GLuint p = uisrc[i];
40643464ebd5Sriastradh               rgba[i][rDst] = ((p >> 22)        );
40653464ebd5Sriastradh               rgba[i][gDst] = ((p >> 12) & 0x3ff);
40663464ebd5Sriastradh               rgba[i][bDst] = ((p >>  2) & 0x3ff);
40673464ebd5Sriastradh               rgba[i][aDst] = ((p      ) & 0x3  );
40683464ebd5Sriastradh            }
40693464ebd5Sriastradh         }
40703464ebd5Sriastradh         break;
40713464ebd5Sriastradh      case GL_UNSIGNED_INT_2_10_10_10_REV:
40723464ebd5Sriastradh         if (swapBytes) {
40733464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40743464ebd5Sriastradh            GLuint i;
40753464ebd5Sriastradh            for (i = 0; i < n; i ++) {
40763464ebd5Sriastradh               GLuint p = uisrc[i];
40773464ebd5Sriastradh               SWAP4BYTE(p);
40783464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x3ff);
40793464ebd5Sriastradh               rgba[i][gDst] = ((p >> 10) & 0x3ff);
40803464ebd5Sriastradh               rgba[i][bDst] = ((p >> 20) & 0x3ff);
40813464ebd5Sriastradh               rgba[i][aDst] = ((p >> 30)        );
40823464ebd5Sriastradh            }
40833464ebd5Sriastradh         }
40843464ebd5Sriastradh         else {
40853464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40863464ebd5Sriastradh            GLuint i;
40873464ebd5Sriastradh            for (i = 0; i < n; i ++) {
40883464ebd5Sriastradh               GLuint p = uisrc[i];
40893464ebd5Sriastradh               rgba[i][rDst] = ((p      ) & 0x3ff);
40903464ebd5Sriastradh               rgba[i][gDst] = ((p >> 10) & 0x3ff);
40913464ebd5Sriastradh               rgba[i][bDst] = ((p >> 20) & 0x3ff);
40923464ebd5Sriastradh               rgba[i][aDst] = ((p >> 30)        );
40933464ebd5Sriastradh            }
40943464ebd5Sriastradh         }
40953464ebd5Sriastradh         break;
40963464ebd5Sriastradh      case GL_UNSIGNED_INT_5_9_9_9_REV:
40973464ebd5Sriastradh         if (swapBytes) {
40983464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
40993464ebd5Sriastradh            GLuint i;
41003464ebd5Sriastradh            float f[3];
41013464ebd5Sriastradh            for (i = 0; i < n; i ++) {
41023464ebd5Sriastradh               GLuint p = uisrc[i];
41033464ebd5Sriastradh               SWAP4BYTE(p);
41043464ebd5Sriastradh               rgb9e5_to_float3(p, f);
41053464ebd5Sriastradh               rgba[i][rDst] = clamp_float_to_uint(f[0]);
41063464ebd5Sriastradh               rgba[i][gDst] = clamp_float_to_uint(f[1]);
41073464ebd5Sriastradh               rgba[i][bDst] = clamp_float_to_uint(f[2]);
41083464ebd5Sriastradh               rgba[i][aDst] = 1;
41093464ebd5Sriastradh            }
41103464ebd5Sriastradh         }
41113464ebd5Sriastradh         else {
41123464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
41133464ebd5Sriastradh            GLuint i;
41143464ebd5Sriastradh            float f[3];
41153464ebd5Sriastradh            for (i = 0; i < n; i ++) {
41163464ebd5Sriastradh               GLuint p = uisrc[i];
41173464ebd5Sriastradh               rgb9e5_to_float3(p, f);
41183464ebd5Sriastradh               rgba[i][rDst] = clamp_float_to_uint(f[0]);
41193464ebd5Sriastradh               rgba[i][gDst] = clamp_float_to_uint(f[1]);
41203464ebd5Sriastradh               rgba[i][bDst] = clamp_float_to_uint(f[2]);
41213464ebd5Sriastradh               rgba[i][aDst] = 1;
41223464ebd5Sriastradh            }
41233464ebd5Sriastradh         }
41243464ebd5Sriastradh         break;
41253464ebd5Sriastradh      case GL_UNSIGNED_INT_10F_11F_11F_REV:
41263464ebd5Sriastradh         if (swapBytes) {
41273464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
41283464ebd5Sriastradh            GLuint i;
41293464ebd5Sriastradh            float f[3];
41303464ebd5Sriastradh            for (i = 0; i < n; i ++) {
41313464ebd5Sriastradh               GLuint p = uisrc[i];
41323464ebd5Sriastradh               SWAP4BYTE(p);
41333464ebd5Sriastradh               r11g11b10f_to_float3(p, f);
41343464ebd5Sriastradh               rgba[i][rDst] = clamp_float_to_uint(f[0]);
41353464ebd5Sriastradh               rgba[i][gDst] = clamp_float_to_uint(f[1]);
41363464ebd5Sriastradh               rgba[i][bDst] = clamp_float_to_uint(f[2]);
41373464ebd5Sriastradh               rgba[i][aDst] = 1;
41383464ebd5Sriastradh            }
41393464ebd5Sriastradh         }
41403464ebd5Sriastradh         else {
41413464ebd5Sriastradh            const GLuint *uisrc = (const GLuint *) src;
41423464ebd5Sriastradh            GLuint i;
41433464ebd5Sriastradh            float f[3];
41443464ebd5Sriastradh            for (i = 0; i < n; i ++) {
41453464ebd5Sriastradh               GLuint p = uisrc[i];
41463464ebd5Sriastradh               r11g11b10f_to_float3(p, f);
41473464ebd5Sriastradh               rgba[i][rDst] = clamp_float_to_uint(f[0]);
41483464ebd5Sriastradh               rgba[i][gDst] = clamp_float_to_uint(f[1]);
41493464ebd5Sriastradh               rgba[i][bDst] = clamp_float_to_uint(f[2]);
41503464ebd5Sriastradh               rgba[i][aDst] = 1;
41513464ebd5Sriastradh            }
41523464ebd5Sriastradh         }
41533464ebd5Sriastradh         break;
41543464ebd5Sriastradh      default:
41553464ebd5Sriastradh         _mesa_problem(NULL, "bad srcType in extract uint data");
41563464ebd5Sriastradh         break;
41573464ebd5Sriastradh   }
41583464ebd5Sriastradh#undef PROCESS
41593464ebd5Sriastradh}
41603464ebd5Sriastradh
41613464ebd5Sriastradh
41623464ebd5Sriastradh
41633464ebd5Sriastradh/*
41643464ebd5Sriastradh * Unpack a row of color image data from a client buffer according to
41653464ebd5Sriastradh * the pixel unpacking parameters.
4166af69d88dSmrg * Return GLubyte values in the specified dest image format.
41673464ebd5Sriastradh * This is used by glDrawPixels and glTexImage?D().
41683464ebd5Sriastradh * \param ctx - the context
41693464ebd5Sriastradh *         n - number of pixels in the span
41703464ebd5Sriastradh *         dstFormat - format of destination color array
41713464ebd5Sriastradh *         dest - the destination color array
41723464ebd5Sriastradh *         srcFormat - source image format
41733464ebd5Sriastradh *         srcType - source image  data type
41743464ebd5Sriastradh *         source - source image pointer
41753464ebd5Sriastradh *         srcPacking - pixel unpacking parameters
41763464ebd5Sriastradh *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
41773464ebd5Sriastradh *
41783464ebd5Sriastradh * XXX perhaps expand this to process whole images someday.
41793464ebd5Sriastradh */
41803464ebd5Sriastradhvoid
4181af69d88dSmrg_mesa_unpack_color_span_ubyte(struct gl_context *ctx,
4182af69d88dSmrg                              GLuint n, GLenum dstFormat, GLubyte dest[],
41833464ebd5Sriastradh                              GLenum srcFormat, GLenum srcType,
41843464ebd5Sriastradh                              const GLvoid *source,
41853464ebd5Sriastradh                              const struct gl_pixelstore_attrib *srcPacking,
41863464ebd5Sriastradh                              GLbitfield transferOps )
41873464ebd5Sriastradh{
4188af69d88dSmrg   GLboolean intFormat = _mesa_is_enum_format_integer(srcFormat);
41893464ebd5Sriastradh   ASSERT(dstFormat == GL_ALPHA ||
41903464ebd5Sriastradh          dstFormat == GL_LUMINANCE ||
41913464ebd5Sriastradh          dstFormat == GL_LUMINANCE_ALPHA ||
41923464ebd5Sriastradh          dstFormat == GL_INTENSITY ||
41933464ebd5Sriastradh          dstFormat == GL_RED ||
41943464ebd5Sriastradh          dstFormat == GL_RG ||
41953464ebd5Sriastradh          dstFormat == GL_RGB ||
4196af69d88dSmrg          dstFormat == GL_RGBA);
41973464ebd5Sriastradh
41983464ebd5Sriastradh   ASSERT(srcFormat == GL_RED ||
41993464ebd5Sriastradh          srcFormat == GL_GREEN ||
42003464ebd5Sriastradh          srcFormat == GL_BLUE ||
42013464ebd5Sriastradh          srcFormat == GL_ALPHA ||
42023464ebd5Sriastradh          srcFormat == GL_LUMINANCE ||
42033464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA ||
42043464ebd5Sriastradh          srcFormat == GL_INTENSITY ||
42053464ebd5Sriastradh          srcFormat == GL_RG ||
42063464ebd5Sriastradh          srcFormat == GL_RGB ||
42073464ebd5Sriastradh          srcFormat == GL_BGR ||
42083464ebd5Sriastradh          srcFormat == GL_RGBA ||
42093464ebd5Sriastradh          srcFormat == GL_BGRA ||
42103464ebd5Sriastradh          srcFormat == GL_ABGR_EXT ||
42113464ebd5Sriastradh          srcFormat == GL_COLOR_INDEX);
42123464ebd5Sriastradh
42133464ebd5Sriastradh   ASSERT(srcType == GL_BITMAP ||
42143464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE ||
42153464ebd5Sriastradh          srcType == GL_BYTE ||
42163464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT ||
42173464ebd5Sriastradh          srcType == GL_SHORT ||
42183464ebd5Sriastradh          srcType == GL_UNSIGNED_INT ||
42193464ebd5Sriastradh          srcType == GL_INT ||
42203464ebd5Sriastradh          srcType == GL_HALF_FLOAT_ARB ||
42213464ebd5Sriastradh          srcType == GL_FLOAT ||
42223464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
42233464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
42243464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
42253464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
42263464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
42273464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
42283464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
42293464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
42303464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
42313464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
42323464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
42333464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
42343464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
42353464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
42363464ebd5Sriastradh
4237af69d88dSmrg   /* EXT_texture_integer specifies no transfer ops on integer
4238af69d88dSmrg    * types in the resolved issues section. Just set them to 0
4239af69d88dSmrg    * for integer surfaces.
4240af69d88dSmrg    */
4241af69d88dSmrg   if (intFormat)
4242af69d88dSmrg      transferOps = 0;
4243af69d88dSmrg
42443464ebd5Sriastradh   /* Try simple cases first */
42453464ebd5Sriastradh   if (transferOps == 0) {
4246af69d88dSmrg      if (srcType == GL_UNSIGNED_BYTE) {
42473464ebd5Sriastradh         if (dstFormat == GL_RGBA) {
42483464ebd5Sriastradh            if (srcFormat == GL_RGBA) {
4249af69d88dSmrg               memcpy( dest, source, n * 4 * sizeof(GLubyte) );
42503464ebd5Sriastradh               return;
42513464ebd5Sriastradh            }
42523464ebd5Sriastradh            else if (srcFormat == GL_RGB) {
42533464ebd5Sriastradh               GLuint i;
4254af69d88dSmrg               const GLubyte *src = (const GLubyte *) source;
4255af69d88dSmrg               GLubyte *dst = dest;
42563464ebd5Sriastradh               for (i = 0; i < n; i++) {
42573464ebd5Sriastradh                  dst[0] = src[0];
42583464ebd5Sriastradh                  dst[1] = src[1];
42593464ebd5Sriastradh                  dst[2] = src[2];
4260af69d88dSmrg                  dst[3] = 255;
42613464ebd5Sriastradh                  src += 3;
42623464ebd5Sriastradh                  dst += 4;
42633464ebd5Sriastradh               }
42643464ebd5Sriastradh               return;
42653464ebd5Sriastradh            }
42663464ebd5Sriastradh         }
42673464ebd5Sriastradh         else if (dstFormat == GL_RGB) {
42683464ebd5Sriastradh            if (srcFormat == GL_RGB) {
4269af69d88dSmrg               memcpy( dest, source, n * 3 * sizeof(GLubyte) );
42703464ebd5Sriastradh               return;
42713464ebd5Sriastradh            }
42723464ebd5Sriastradh            else if (srcFormat == GL_RGBA) {
42733464ebd5Sriastradh               GLuint i;
4274af69d88dSmrg               const GLubyte *src = (const GLubyte *) source;
4275af69d88dSmrg               GLubyte *dst = dest;
42763464ebd5Sriastradh               for (i = 0; i < n; i++) {
42773464ebd5Sriastradh                  dst[0] = src[0];
42783464ebd5Sriastradh                  dst[1] = src[1];
42793464ebd5Sriastradh                  dst[2] = src[2];
42803464ebd5Sriastradh                  src += 4;
42813464ebd5Sriastradh                  dst += 3;
42823464ebd5Sriastradh               }
42833464ebd5Sriastradh               return;
42843464ebd5Sriastradh            }
42853464ebd5Sriastradh         }
42863464ebd5Sriastradh         else if (dstFormat == srcFormat) {
42873464ebd5Sriastradh            GLint comps = _mesa_components_in_format(srcFormat);
42883464ebd5Sriastradh            assert(comps > 0);
4289af69d88dSmrg            memcpy( dest, source, n * comps * sizeof(GLubyte) );
42903464ebd5Sriastradh            return;
42913464ebd5Sriastradh         }
42923464ebd5Sriastradh      }
42933464ebd5Sriastradh   }
42943464ebd5Sriastradh
42953464ebd5Sriastradh
42963464ebd5Sriastradh   /* general solution begins here */
42973464ebd5Sriastradh   {
42983464ebd5Sriastradh      GLint dstComponents;
42993464ebd5Sriastradh      GLint rDst, gDst, bDst, aDst, lDst, iDst;
4300af69d88dSmrg      GLfloat (*rgba)[4] = malloc(4 * n * sizeof(GLfloat));
43013464ebd5Sriastradh
43023464ebd5Sriastradh      if (!rgba) {
43033464ebd5Sriastradh         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
43043464ebd5Sriastradh         return;
43053464ebd5Sriastradh      }
43063464ebd5Sriastradh
43073464ebd5Sriastradh      dstComponents = _mesa_components_in_format( dstFormat );
43083464ebd5Sriastradh      /* source & dest image formats should have been error checked by now */
43093464ebd5Sriastradh      assert(dstComponents > 0);
43103464ebd5Sriastradh
43113464ebd5Sriastradh      /*
43123464ebd5Sriastradh       * Extract image data and convert to RGBA floats
43133464ebd5Sriastradh       */
43143464ebd5Sriastradh      if (srcFormat == GL_COLOR_INDEX) {
4315af69d88dSmrg         GLuint *indexes = malloc(n * sizeof(GLuint));
43163464ebd5Sriastradh
43173464ebd5Sriastradh         if (!indexes) {
43183464ebd5Sriastradh            _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4319af69d88dSmrg            free(rgba);
43203464ebd5Sriastradh            return;
43213464ebd5Sriastradh         }
43223464ebd5Sriastradh
43233464ebd5Sriastradh         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
43243464ebd5Sriastradh                              srcPacking);
43253464ebd5Sriastradh
4326af69d88dSmrg	 /* Convert indexes to RGBA */
4327af69d88dSmrg	 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4328af69d88dSmrg	    _mesa_shift_and_offset_ci(ctx, n, indexes);
4329af69d88dSmrg	 }
4330af69d88dSmrg	 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
43313464ebd5Sriastradh
43323464ebd5Sriastradh         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
43333464ebd5Sriastradh          * with color indexes.
43343464ebd5Sriastradh          */
43353464ebd5Sriastradh         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
43363464ebd5Sriastradh
43373464ebd5Sriastradh         free(indexes);
43383464ebd5Sriastradh      }
43393464ebd5Sriastradh      else {
43403464ebd5Sriastradh         /* non-color index data */
43413464ebd5Sriastradh         extract_float_rgba(n, rgba, srcFormat, srcType, source,
43423464ebd5Sriastradh                            srcPacking->SwapBytes);
43433464ebd5Sriastradh      }
43443464ebd5Sriastradh
4345af69d88dSmrg      /* Need to clamp if returning GLubytes */
43463464ebd5Sriastradh      transferOps |= IMAGE_CLAMP_BIT;
43473464ebd5Sriastradh
43483464ebd5Sriastradh      if (transferOps) {
43493464ebd5Sriastradh         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
43503464ebd5Sriastradh      }
43513464ebd5Sriastradh
43523464ebd5Sriastradh      get_component_indexes(dstFormat,
43533464ebd5Sriastradh                            &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
43543464ebd5Sriastradh
4355af69d88dSmrg      /* Now return the GLubyte data in the requested dstFormat */
43563464ebd5Sriastradh      if (rDst >= 0) {
4357af69d88dSmrg         GLubyte *dst = dest;
43583464ebd5Sriastradh         GLuint i;
43593464ebd5Sriastradh         for (i = 0; i < n; i++) {
4360af69d88dSmrg            CLAMPED_FLOAT_TO_UBYTE(dst[rDst], rgba[i][RCOMP]);
43613464ebd5Sriastradh            dst += dstComponents;
43623464ebd5Sriastradh         }
43633464ebd5Sriastradh      }
43643464ebd5Sriastradh
43653464ebd5Sriastradh      if (gDst >= 0) {
4366af69d88dSmrg         GLubyte *dst = dest;
43673464ebd5Sriastradh         GLuint i;
43683464ebd5Sriastradh         for (i = 0; i < n; i++) {
4369af69d88dSmrg            CLAMPED_FLOAT_TO_UBYTE(dst[gDst], rgba[i][GCOMP]);
43703464ebd5Sriastradh            dst += dstComponents;
43713464ebd5Sriastradh         }
43723464ebd5Sriastradh      }
43733464ebd5Sriastradh
43743464ebd5Sriastradh      if (bDst >= 0) {
4375af69d88dSmrg         GLubyte *dst = dest;
43763464ebd5Sriastradh         GLuint i;
43773464ebd5Sriastradh         for (i = 0; i < n; i++) {
4378af69d88dSmrg            CLAMPED_FLOAT_TO_UBYTE(dst[bDst], rgba[i][BCOMP]);
43793464ebd5Sriastradh            dst += dstComponents;
43803464ebd5Sriastradh         }
43813464ebd5Sriastradh      }
43823464ebd5Sriastradh
43833464ebd5Sriastradh      if (aDst >= 0) {
4384af69d88dSmrg         GLubyte *dst = dest;
43853464ebd5Sriastradh         GLuint i;
43863464ebd5Sriastradh         for (i = 0; i < n; i++) {
4387af69d88dSmrg            CLAMPED_FLOAT_TO_UBYTE(dst[aDst], rgba[i][ACOMP]);
43883464ebd5Sriastradh            dst += dstComponents;
43893464ebd5Sriastradh         }
43903464ebd5Sriastradh      }
43913464ebd5Sriastradh
43923464ebd5Sriastradh      if (iDst >= 0) {
4393af69d88dSmrg         GLubyte *dst = dest;
43943464ebd5Sriastradh         GLuint i;
43953464ebd5Sriastradh         assert(iDst == 0);
43963464ebd5Sriastradh         assert(dstComponents == 1);
43973464ebd5Sriastradh         for (i = 0; i < n; i++) {
43983464ebd5Sriastradh            /* Intensity comes from red channel */
4399af69d88dSmrg            CLAMPED_FLOAT_TO_UBYTE(dst[i], rgba[i][RCOMP]);
44003464ebd5Sriastradh         }
44013464ebd5Sriastradh      }
44023464ebd5Sriastradh
44033464ebd5Sriastradh      if (lDst >= 0) {
4404af69d88dSmrg         GLubyte *dst = dest;
44053464ebd5Sriastradh         GLuint i;
44063464ebd5Sriastradh         assert(lDst == 0);
44073464ebd5Sriastradh         for (i = 0; i < n; i++) {
44083464ebd5Sriastradh            /* Luminance comes from red channel */
4409af69d88dSmrg            CLAMPED_FLOAT_TO_UBYTE(dst[0], rgba[i][RCOMP]);
44103464ebd5Sriastradh            dst += dstComponents;
44113464ebd5Sriastradh         }
44123464ebd5Sriastradh      }
44133464ebd5Sriastradh
44143464ebd5Sriastradh      free(rgba);
44153464ebd5Sriastradh   }
44163464ebd5Sriastradh}
44173464ebd5Sriastradh
44183464ebd5Sriastradh
44193464ebd5Sriastradh/**
4420af69d88dSmrg * Same as _mesa_unpack_color_span_ubyte(), but return GLfloat data
4421af69d88dSmrg * instead of GLubyte.
44223464ebd5Sriastradh */
44233464ebd5Sriastradhvoid
44243464ebd5Sriastradh_mesa_unpack_color_span_float( struct gl_context *ctx,
44253464ebd5Sriastradh                               GLuint n, GLenum dstFormat, GLfloat dest[],
44263464ebd5Sriastradh                               GLenum srcFormat, GLenum srcType,
44273464ebd5Sriastradh                               const GLvoid *source,
44283464ebd5Sriastradh                               const struct gl_pixelstore_attrib *srcPacking,
44293464ebd5Sriastradh                               GLbitfield transferOps )
44303464ebd5Sriastradh{
44313464ebd5Sriastradh   ASSERT(dstFormat == GL_ALPHA ||
44323464ebd5Sriastradh          dstFormat == GL_LUMINANCE ||
44333464ebd5Sriastradh          dstFormat == GL_LUMINANCE_ALPHA ||
44343464ebd5Sriastradh          dstFormat == GL_INTENSITY ||
44353464ebd5Sriastradh          dstFormat == GL_RED ||
44363464ebd5Sriastradh          dstFormat == GL_RG ||
44373464ebd5Sriastradh          dstFormat == GL_RGB ||
4438af69d88dSmrg          dstFormat == GL_RGBA);
44393464ebd5Sriastradh
44403464ebd5Sriastradh   ASSERT(srcFormat == GL_RED ||
44413464ebd5Sriastradh          srcFormat == GL_GREEN ||
44423464ebd5Sriastradh          srcFormat == GL_BLUE ||
44433464ebd5Sriastradh          srcFormat == GL_ALPHA ||
44443464ebd5Sriastradh          srcFormat == GL_LUMINANCE ||
44453464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA ||
44463464ebd5Sriastradh          srcFormat == GL_INTENSITY ||
44473464ebd5Sriastradh          srcFormat == GL_RG ||
44483464ebd5Sriastradh          srcFormat == GL_RGB ||
44493464ebd5Sriastradh          srcFormat == GL_BGR ||
44503464ebd5Sriastradh          srcFormat == GL_RGBA ||
44513464ebd5Sriastradh          srcFormat == GL_BGRA ||
44523464ebd5Sriastradh          srcFormat == GL_ABGR_EXT ||
44533464ebd5Sriastradh          srcFormat == GL_RED_INTEGER_EXT ||
44543464ebd5Sriastradh          srcFormat == GL_GREEN_INTEGER_EXT ||
44553464ebd5Sriastradh          srcFormat == GL_BLUE_INTEGER_EXT ||
44563464ebd5Sriastradh          srcFormat == GL_ALPHA_INTEGER_EXT ||
4457af69d88dSmrg          srcFormat == GL_RG_INTEGER ||
44583464ebd5Sriastradh          srcFormat == GL_RGB_INTEGER_EXT ||
44593464ebd5Sriastradh          srcFormat == GL_RGBA_INTEGER_EXT ||
44603464ebd5Sriastradh          srcFormat == GL_BGR_INTEGER_EXT ||
44613464ebd5Sriastradh          srcFormat == GL_BGRA_INTEGER_EXT ||
44623464ebd5Sriastradh          srcFormat == GL_LUMINANCE_INTEGER_EXT ||
44633464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT ||
44643464ebd5Sriastradh          srcFormat == GL_COLOR_INDEX);
44653464ebd5Sriastradh
44663464ebd5Sriastradh   ASSERT(srcType == GL_BITMAP ||
44673464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE ||
44683464ebd5Sriastradh          srcType == GL_BYTE ||
44693464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT ||
44703464ebd5Sriastradh          srcType == GL_SHORT ||
44713464ebd5Sriastradh          srcType == GL_UNSIGNED_INT ||
44723464ebd5Sriastradh          srcType == GL_INT ||
44733464ebd5Sriastradh          srcType == GL_HALF_FLOAT_ARB ||
44743464ebd5Sriastradh          srcType == GL_FLOAT ||
44753464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
44763464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
44773464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
44783464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
44793464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
44803464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
44813464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
44823464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
44833464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
44843464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
44853464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
44863464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
44873464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
44883464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
44893464ebd5Sriastradh
44903464ebd5Sriastradh   /* general solution, no special cases, yet */
44913464ebd5Sriastradh   {
44923464ebd5Sriastradh      GLint dstComponents;
44933464ebd5Sriastradh      GLint rDst, gDst, bDst, aDst, lDst, iDst;
4494af69d88dSmrg      GLfloat (*rgba)[4] = malloc(4 * n * sizeof(GLfloat));
4495af69d88dSmrg      GLboolean intFormat = _mesa_is_enum_format_integer(srcFormat);
44963464ebd5Sriastradh
44973464ebd5Sriastradh      if (!rgba) {
44983464ebd5Sriastradh         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
44993464ebd5Sriastradh         return;
45003464ebd5Sriastradh      }
45013464ebd5Sriastradh
45023464ebd5Sriastradh      dstComponents = _mesa_components_in_format( dstFormat );
45033464ebd5Sriastradh      /* source & dest image formats should have been error checked by now */
45043464ebd5Sriastradh      assert(dstComponents > 0);
45053464ebd5Sriastradh
4506af69d88dSmrg      /* EXT_texture_integer specifies no transfer ops on integer
4507af69d88dSmrg       * types in the resolved issues section. Just set them to 0
4508af69d88dSmrg       * for integer surfaces.
4509af69d88dSmrg       */
4510af69d88dSmrg      if (intFormat)
4511af69d88dSmrg         transferOps = 0;
4512af69d88dSmrg
45133464ebd5Sriastradh      /*
45143464ebd5Sriastradh       * Extract image data and convert to RGBA floats
45153464ebd5Sriastradh       */
45163464ebd5Sriastradh      if (srcFormat == GL_COLOR_INDEX) {
4517af69d88dSmrg         GLuint *indexes = malloc(n * sizeof(GLuint));
45183464ebd5Sriastradh
45193464ebd5Sriastradh         if (!indexes) {
45203464ebd5Sriastradh            _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
45213464ebd5Sriastradh            free(rgba);
45223464ebd5Sriastradh            return;
45233464ebd5Sriastradh         }
45243464ebd5Sriastradh
45253464ebd5Sriastradh         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
45263464ebd5Sriastradh                              srcPacking);
45273464ebd5Sriastradh
4528af69d88dSmrg	 /* Convert indexes to RGBA */
4529af69d88dSmrg	 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4530af69d88dSmrg	    _mesa_shift_and_offset_ci(ctx, n, indexes);
4531af69d88dSmrg	 }
4532af69d88dSmrg	 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
45333464ebd5Sriastradh
45343464ebd5Sriastradh         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
45353464ebd5Sriastradh          * with color indexes.
45363464ebd5Sriastradh          */
45373464ebd5Sriastradh         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
45383464ebd5Sriastradh
45393464ebd5Sriastradh         free(indexes);
45403464ebd5Sriastradh      }
45413464ebd5Sriastradh      else {
45423464ebd5Sriastradh         /* non-color index data */
45433464ebd5Sriastradh         extract_float_rgba(n, rgba, srcFormat, srcType, source,
45443464ebd5Sriastradh                            srcPacking->SwapBytes);
45453464ebd5Sriastradh      }
45463464ebd5Sriastradh
45473464ebd5Sriastradh      if (transferOps) {
45483464ebd5Sriastradh         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
45493464ebd5Sriastradh      }
45503464ebd5Sriastradh
45513464ebd5Sriastradh      get_component_indexes(dstFormat,
45523464ebd5Sriastradh                            &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
45533464ebd5Sriastradh
45543464ebd5Sriastradh      /* Now pack results in the requested dstFormat */
45553464ebd5Sriastradh      if (rDst >= 0) {
45563464ebd5Sriastradh         GLfloat *dst = dest;
45573464ebd5Sriastradh         GLuint i;
45583464ebd5Sriastradh         for (i = 0; i < n; i++) {
45593464ebd5Sriastradh            dst[rDst] = rgba[i][RCOMP];
45603464ebd5Sriastradh            dst += dstComponents;
45613464ebd5Sriastradh         }
45623464ebd5Sriastradh      }
45633464ebd5Sriastradh
45643464ebd5Sriastradh      if (gDst >= 0) {
45653464ebd5Sriastradh         GLfloat *dst = dest;
45663464ebd5Sriastradh         GLuint i;
45673464ebd5Sriastradh         for (i = 0; i < n; i++) {
45683464ebd5Sriastradh            dst[gDst] = rgba[i][GCOMP];
45693464ebd5Sriastradh            dst += dstComponents;
45703464ebd5Sriastradh         }
45713464ebd5Sriastradh      }
45723464ebd5Sriastradh
45733464ebd5Sriastradh      if (bDst >= 0) {
45743464ebd5Sriastradh         GLfloat *dst = dest;
45753464ebd5Sriastradh         GLuint i;
45763464ebd5Sriastradh         for (i = 0; i < n; i++) {
45773464ebd5Sriastradh            dst[bDst] = rgba[i][BCOMP];
45783464ebd5Sriastradh            dst += dstComponents;
45793464ebd5Sriastradh         }
45803464ebd5Sriastradh      }
45813464ebd5Sriastradh
45823464ebd5Sriastradh      if (aDst >= 0) {
45833464ebd5Sriastradh         GLfloat *dst = dest;
45843464ebd5Sriastradh         GLuint i;
45853464ebd5Sriastradh         for (i = 0; i < n; i++) {
45863464ebd5Sriastradh            dst[aDst] = rgba[i][ACOMP];
45873464ebd5Sriastradh            dst += dstComponents;
45883464ebd5Sriastradh         }
45893464ebd5Sriastradh      }
45903464ebd5Sriastradh
45913464ebd5Sriastradh      if (iDst >= 0) {
45923464ebd5Sriastradh         GLfloat *dst = dest;
45933464ebd5Sriastradh         GLuint i;
45943464ebd5Sriastradh         assert(iDst == 0);
45953464ebd5Sriastradh         assert(dstComponents == 1);
45963464ebd5Sriastradh         for (i = 0; i < n; i++) {
45973464ebd5Sriastradh            /* Intensity comes from red channel */
45983464ebd5Sriastradh            dst[i] = rgba[i][RCOMP];
45993464ebd5Sriastradh         }
46003464ebd5Sriastradh      }
46013464ebd5Sriastradh
46023464ebd5Sriastradh      if (lDst >= 0) {
46033464ebd5Sriastradh         GLfloat *dst = dest;
46043464ebd5Sriastradh         GLuint i;
46053464ebd5Sriastradh         assert(lDst == 0);
46063464ebd5Sriastradh         for (i = 0; i < n; i++) {
46073464ebd5Sriastradh            /* Luminance comes from red channel */
46083464ebd5Sriastradh            dst[0] = rgba[i][RCOMP];
46093464ebd5Sriastradh            dst += dstComponents;
46103464ebd5Sriastradh         }
46113464ebd5Sriastradh      }
46123464ebd5Sriastradh
46133464ebd5Sriastradh      free(rgba);
46143464ebd5Sriastradh   }
46153464ebd5Sriastradh}
46163464ebd5Sriastradh
46173464ebd5Sriastradh
46183464ebd5Sriastradh/**
4619af69d88dSmrg * Same as _mesa_unpack_color_span_ubyte(), but return GLuint data
4620af69d88dSmrg * instead of GLubyte.
46213464ebd5Sriastradh * No pixel transfer ops are applied.
46223464ebd5Sriastradh */
46233464ebd5Sriastradhvoid
46243464ebd5Sriastradh_mesa_unpack_color_span_uint(struct gl_context *ctx,
46253464ebd5Sriastradh                             GLuint n, GLenum dstFormat, GLuint *dest,
46263464ebd5Sriastradh                             GLenum srcFormat, GLenum srcType,
46273464ebd5Sriastradh                             const GLvoid *source,
46283464ebd5Sriastradh                             const struct gl_pixelstore_attrib *srcPacking)
46293464ebd5Sriastradh{
4630af69d88dSmrg   GLuint (*rgba)[4] = malloc(n * 4 * sizeof(GLfloat));
46313464ebd5Sriastradh
46323464ebd5Sriastradh   if (!rgba) {
46333464ebd5Sriastradh      _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
46343464ebd5Sriastradh      return;
46353464ebd5Sriastradh   }
46363464ebd5Sriastradh
46373464ebd5Sriastradh   ASSERT(dstFormat == GL_ALPHA ||
46383464ebd5Sriastradh          dstFormat == GL_LUMINANCE ||
46393464ebd5Sriastradh          dstFormat == GL_LUMINANCE_ALPHA ||
46403464ebd5Sriastradh          dstFormat == GL_INTENSITY ||
46413464ebd5Sriastradh          dstFormat == GL_RED ||
46423464ebd5Sriastradh          dstFormat == GL_RG ||
46433464ebd5Sriastradh          dstFormat == GL_RGB ||
46443464ebd5Sriastradh          dstFormat == GL_RGBA);
46453464ebd5Sriastradh
46463464ebd5Sriastradh   ASSERT(srcFormat == GL_RED ||
46473464ebd5Sriastradh          srcFormat == GL_GREEN ||
46483464ebd5Sriastradh          srcFormat == GL_BLUE ||
46493464ebd5Sriastradh          srcFormat == GL_ALPHA ||
46503464ebd5Sriastradh          srcFormat == GL_LUMINANCE ||
46513464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA ||
46523464ebd5Sriastradh          srcFormat == GL_INTENSITY ||
46533464ebd5Sriastradh          srcFormat == GL_RG ||
46543464ebd5Sriastradh          srcFormat == GL_RGB ||
46553464ebd5Sriastradh          srcFormat == GL_BGR ||
46563464ebd5Sriastradh          srcFormat == GL_RGBA ||
46573464ebd5Sriastradh          srcFormat == GL_BGRA ||
46583464ebd5Sriastradh          srcFormat == GL_ABGR_EXT ||
46593464ebd5Sriastradh          srcFormat == GL_RED_INTEGER_EXT ||
46603464ebd5Sriastradh          srcFormat == GL_GREEN_INTEGER_EXT ||
46613464ebd5Sriastradh          srcFormat == GL_BLUE_INTEGER_EXT ||
46623464ebd5Sriastradh          srcFormat == GL_ALPHA_INTEGER_EXT ||
4663af69d88dSmrg          srcFormat == GL_RG_INTEGER ||
46643464ebd5Sriastradh          srcFormat == GL_RGB_INTEGER_EXT ||
46653464ebd5Sriastradh          srcFormat == GL_RGBA_INTEGER_EXT ||
46663464ebd5Sriastradh          srcFormat == GL_BGR_INTEGER_EXT ||
46673464ebd5Sriastradh          srcFormat == GL_BGRA_INTEGER_EXT ||
46683464ebd5Sriastradh          srcFormat == GL_LUMINANCE_INTEGER_EXT ||
46693464ebd5Sriastradh          srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
46703464ebd5Sriastradh
46713464ebd5Sriastradh   ASSERT(srcType == GL_UNSIGNED_BYTE ||
46723464ebd5Sriastradh          srcType == GL_BYTE ||
46733464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT ||
46743464ebd5Sriastradh          srcType == GL_SHORT ||
46753464ebd5Sriastradh          srcType == GL_UNSIGNED_INT ||
46763464ebd5Sriastradh          srcType == GL_INT ||
46773464ebd5Sriastradh          srcType == GL_HALF_FLOAT_ARB ||
46783464ebd5Sriastradh          srcType == GL_FLOAT ||
46793464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
46803464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
46813464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
46823464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
46833464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
46843464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
46853464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
46863464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
46873464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
46883464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
46893464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
46903464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
46913464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
46923464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
46933464ebd5Sriastradh
46943464ebd5Sriastradh
46953464ebd5Sriastradh   /* Extract image data as uint[4] pixels */
46963464ebd5Sriastradh   extract_uint_rgba(n, rgba, srcFormat, srcType, source,
46973464ebd5Sriastradh                     srcPacking->SwapBytes);
46983464ebd5Sriastradh
46993464ebd5Sriastradh   if (dstFormat == GL_RGBA) {
47003464ebd5Sriastradh      /* simple case */
47013464ebd5Sriastradh      memcpy(dest, rgba, 4 * sizeof(GLuint) * n);
47023464ebd5Sriastradh   }
47033464ebd5Sriastradh   else {
47043464ebd5Sriastradh      /* general case */
47053464ebd5Sriastradh      GLint rDst, gDst, bDst, aDst, lDst, iDst;
47063464ebd5Sriastradh      GLint dstComponents = _mesa_components_in_format( dstFormat );
47073464ebd5Sriastradh
47083464ebd5Sriastradh      assert(dstComponents > 0);
47093464ebd5Sriastradh
47103464ebd5Sriastradh      get_component_indexes(dstFormat,
47113464ebd5Sriastradh                            &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
47123464ebd5Sriastradh
47133464ebd5Sriastradh      /* Now pack values in the requested dest format */
47143464ebd5Sriastradh      if (rDst >= 0) {
47153464ebd5Sriastradh         GLuint *dst = dest;
47163464ebd5Sriastradh         GLuint i;
47173464ebd5Sriastradh         for (i = 0; i < n; i++) {
47183464ebd5Sriastradh            dst[rDst] = rgba[i][RCOMP];
47193464ebd5Sriastradh            dst += dstComponents;
47203464ebd5Sriastradh         }
47213464ebd5Sriastradh      }
47223464ebd5Sriastradh
47233464ebd5Sriastradh      if (gDst >= 0) {
47243464ebd5Sriastradh         GLuint *dst = dest;
47253464ebd5Sriastradh         GLuint i;
47263464ebd5Sriastradh         for (i = 0; i < n; i++) {
47273464ebd5Sriastradh            dst[gDst] = rgba[i][GCOMP];
47283464ebd5Sriastradh            dst += dstComponents;
47293464ebd5Sriastradh         }
47303464ebd5Sriastradh      }
47313464ebd5Sriastradh
47323464ebd5Sriastradh      if (bDst >= 0) {
47333464ebd5Sriastradh         GLuint *dst = dest;
47343464ebd5Sriastradh         GLuint i;
47353464ebd5Sriastradh         for (i = 0; i < n; i++) {
47363464ebd5Sriastradh            dst[bDst] = rgba[i][BCOMP];
47373464ebd5Sriastradh            dst += dstComponents;
47383464ebd5Sriastradh         }
47393464ebd5Sriastradh      }
47403464ebd5Sriastradh
47413464ebd5Sriastradh      if (aDst >= 0) {
47423464ebd5Sriastradh         GLuint *dst = dest;
47433464ebd5Sriastradh         GLuint i;
47443464ebd5Sriastradh         for (i = 0; i < n; i++) {
47453464ebd5Sriastradh            dst[aDst] = rgba[i][ACOMP];
47463464ebd5Sriastradh            dst += dstComponents;
47473464ebd5Sriastradh         }
47483464ebd5Sriastradh      }
47493464ebd5Sriastradh
47503464ebd5Sriastradh      if (iDst >= 0) {
47513464ebd5Sriastradh         GLuint *dst = dest;
47523464ebd5Sriastradh         GLuint i;
47533464ebd5Sriastradh         assert(iDst == 0);
47543464ebd5Sriastradh         assert(dstComponents == 1);
47553464ebd5Sriastradh         for (i = 0; i < n; i++) {
47563464ebd5Sriastradh            /* Intensity comes from red channel */
47573464ebd5Sriastradh            dst[i] = rgba[i][RCOMP];
47583464ebd5Sriastradh         }
47593464ebd5Sriastradh      }
47603464ebd5Sriastradh
47613464ebd5Sriastradh      if (lDst >= 0) {
47623464ebd5Sriastradh         GLuint *dst = dest;
47633464ebd5Sriastradh         GLuint i;
47643464ebd5Sriastradh         assert(lDst == 0);
47653464ebd5Sriastradh         for (i = 0; i < n; i++) {
47663464ebd5Sriastradh            /* Luminance comes from red channel */
47673464ebd5Sriastradh            dst[0] = rgba[i][RCOMP];
47683464ebd5Sriastradh            dst += dstComponents;
47693464ebd5Sriastradh         }
47703464ebd5Sriastradh      }
47713464ebd5Sriastradh   }
47723464ebd5Sriastradh
47733464ebd5Sriastradh   free(rgba);
47743464ebd5Sriastradh}
47753464ebd5Sriastradh
47763464ebd5Sriastradh
47773464ebd5Sriastradh/*
47783464ebd5Sriastradh * Unpack a row of color index data from a client buffer according to
47793464ebd5Sriastradh * the pixel unpacking parameters.
47803464ebd5Sriastradh * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
47813464ebd5Sriastradh *
47823464ebd5Sriastradh * Args:  ctx - the context
47833464ebd5Sriastradh *        n - number of pixels
47843464ebd5Sriastradh *        dstType - destination data type
47853464ebd5Sriastradh *        dest - destination array
47863464ebd5Sriastradh *        srcType - source pixel type
47873464ebd5Sriastradh *        source - source data pointer
47883464ebd5Sriastradh *        srcPacking - pixel unpacking parameters
47893464ebd5Sriastradh *        transferOps - the pixel transfer operations to apply
47903464ebd5Sriastradh */
47913464ebd5Sriastradhvoid
47923464ebd5Sriastradh_mesa_unpack_index_span( struct gl_context *ctx, GLuint n,
47933464ebd5Sriastradh                         GLenum dstType, GLvoid *dest,
47943464ebd5Sriastradh                         GLenum srcType, const GLvoid *source,
47953464ebd5Sriastradh                         const struct gl_pixelstore_attrib *srcPacking,
47963464ebd5Sriastradh                         GLbitfield transferOps )
47973464ebd5Sriastradh{
47983464ebd5Sriastradh   ASSERT(srcType == GL_BITMAP ||
47993464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE ||
48003464ebd5Sriastradh          srcType == GL_BYTE ||
48013464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT ||
48023464ebd5Sriastradh          srcType == GL_SHORT ||
48033464ebd5Sriastradh          srcType == GL_UNSIGNED_INT ||
48043464ebd5Sriastradh          srcType == GL_INT ||
48053464ebd5Sriastradh          srcType == GL_HALF_FLOAT_ARB ||
48063464ebd5Sriastradh          srcType == GL_FLOAT);
48073464ebd5Sriastradh
48083464ebd5Sriastradh   ASSERT(dstType == GL_UNSIGNED_BYTE ||
48093464ebd5Sriastradh          dstType == GL_UNSIGNED_SHORT ||
48103464ebd5Sriastradh          dstType == GL_UNSIGNED_INT);
48113464ebd5Sriastradh
48123464ebd5Sriastradh
48133464ebd5Sriastradh   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
48143464ebd5Sriastradh
48153464ebd5Sriastradh   /*
48163464ebd5Sriastradh    * Try simple cases first
48173464ebd5Sriastradh    */
48183464ebd5Sriastradh   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
48193464ebd5Sriastradh       && dstType == GL_UNSIGNED_BYTE) {
48203464ebd5Sriastradh      memcpy(dest, source, n * sizeof(GLubyte));
48213464ebd5Sriastradh   }
48223464ebd5Sriastradh   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
48233464ebd5Sriastradh            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
48243464ebd5Sriastradh      memcpy(dest, source, n * sizeof(GLuint));
48253464ebd5Sriastradh   }
48263464ebd5Sriastradh   else {
48273464ebd5Sriastradh      /*
48283464ebd5Sriastradh       * general solution
48293464ebd5Sriastradh       */
4830af69d88dSmrg      GLuint *indexes = malloc(n * sizeof(GLuint));
48313464ebd5Sriastradh
48323464ebd5Sriastradh      if (!indexes) {
48333464ebd5Sriastradh         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
48343464ebd5Sriastradh         return;
48353464ebd5Sriastradh      }
48363464ebd5Sriastradh
48373464ebd5Sriastradh      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
48383464ebd5Sriastradh                           srcPacking);
48393464ebd5Sriastradh
48403464ebd5Sriastradh      if (transferOps)
48413464ebd5Sriastradh         _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
48423464ebd5Sriastradh
48433464ebd5Sriastradh      /* convert to dest type */
48443464ebd5Sriastradh      switch (dstType) {
48453464ebd5Sriastradh         case GL_UNSIGNED_BYTE:
48463464ebd5Sriastradh            {
48473464ebd5Sriastradh               GLubyte *dst = (GLubyte *) dest;
48483464ebd5Sriastradh               GLuint i;
48493464ebd5Sriastradh               for (i = 0; i < n; i++) {
48503464ebd5Sriastradh                  dst[i] = (GLubyte) (indexes[i] & 0xff);
48513464ebd5Sriastradh               }
48523464ebd5Sriastradh            }
48533464ebd5Sriastradh            break;
48543464ebd5Sriastradh         case GL_UNSIGNED_SHORT:
48553464ebd5Sriastradh            {
48563464ebd5Sriastradh               GLuint *dst = (GLuint *) dest;
48573464ebd5Sriastradh               GLuint i;
48583464ebd5Sriastradh               for (i = 0; i < n; i++) {
48593464ebd5Sriastradh                  dst[i] = (GLushort) (indexes[i] & 0xffff);
48603464ebd5Sriastradh               }
48613464ebd5Sriastradh            }
48623464ebd5Sriastradh            break;
48633464ebd5Sriastradh         case GL_UNSIGNED_INT:
48643464ebd5Sriastradh            memcpy(dest, indexes, n * sizeof(GLuint));
48653464ebd5Sriastradh            break;
48663464ebd5Sriastradh         default:
48673464ebd5Sriastradh            _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
48683464ebd5Sriastradh      }
48693464ebd5Sriastradh
48703464ebd5Sriastradh      free(indexes);
48713464ebd5Sriastradh   }
48723464ebd5Sriastradh}
48733464ebd5Sriastradh
48743464ebd5Sriastradh
48753464ebd5Sriastradhvoid
48763464ebd5Sriastradh_mesa_pack_index_span( struct gl_context *ctx, GLuint n,
48773464ebd5Sriastradh                       GLenum dstType, GLvoid *dest, const GLuint *source,
48783464ebd5Sriastradh                       const struct gl_pixelstore_attrib *dstPacking,
48793464ebd5Sriastradh                       GLbitfield transferOps )
48803464ebd5Sriastradh{
4881af69d88dSmrg   GLuint *indexes = malloc(n * sizeof(GLuint));
48823464ebd5Sriastradh
48833464ebd5Sriastradh   if (!indexes) {
48843464ebd5Sriastradh      _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
48853464ebd5Sriastradh      return;
48863464ebd5Sriastradh   }
48873464ebd5Sriastradh
48883464ebd5Sriastradh   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
48893464ebd5Sriastradh
48903464ebd5Sriastradh   if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
48913464ebd5Sriastradh      /* make a copy of input */
48923464ebd5Sriastradh      memcpy(indexes, source, n * sizeof(GLuint));
48933464ebd5Sriastradh      _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
48943464ebd5Sriastradh      source = indexes;
48953464ebd5Sriastradh   }
48963464ebd5Sriastradh
48973464ebd5Sriastradh   switch (dstType) {
48983464ebd5Sriastradh   case GL_UNSIGNED_BYTE:
48993464ebd5Sriastradh      {
49003464ebd5Sriastradh         GLubyte *dst = (GLubyte *) dest;
49013464ebd5Sriastradh         GLuint i;
49023464ebd5Sriastradh         for (i = 0; i < n; i++) {
49033464ebd5Sriastradh            *dst++ = (GLubyte) source[i];
49043464ebd5Sriastradh         }
49053464ebd5Sriastradh      }
49063464ebd5Sriastradh      break;
49073464ebd5Sriastradh   case GL_BYTE:
49083464ebd5Sriastradh      {
49093464ebd5Sriastradh         GLbyte *dst = (GLbyte *) dest;
49103464ebd5Sriastradh         GLuint i;
49113464ebd5Sriastradh         for (i = 0; i < n; i++) {
49123464ebd5Sriastradh            dst[i] = (GLbyte) source[i];
49133464ebd5Sriastradh         }
49143464ebd5Sriastradh      }
49153464ebd5Sriastradh      break;
49163464ebd5Sriastradh   case GL_UNSIGNED_SHORT:
49173464ebd5Sriastradh      {
49183464ebd5Sriastradh         GLushort *dst = (GLushort *) dest;
49193464ebd5Sriastradh         GLuint i;
49203464ebd5Sriastradh         for (i = 0; i < n; i++) {
49213464ebd5Sriastradh            dst[i] = (GLushort) source[i];
49223464ebd5Sriastradh         }
49233464ebd5Sriastradh         if (dstPacking->SwapBytes) {
49243464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
49253464ebd5Sriastradh         }
49263464ebd5Sriastradh      }
49273464ebd5Sriastradh      break;
49283464ebd5Sriastradh   case GL_SHORT:
49293464ebd5Sriastradh      {
49303464ebd5Sriastradh         GLshort *dst = (GLshort *) dest;
49313464ebd5Sriastradh         GLuint i;
49323464ebd5Sriastradh         for (i = 0; i < n; i++) {
49333464ebd5Sriastradh            dst[i] = (GLshort) source[i];
49343464ebd5Sriastradh         }
49353464ebd5Sriastradh         if (dstPacking->SwapBytes) {
49363464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
49373464ebd5Sriastradh         }
49383464ebd5Sriastradh      }
49393464ebd5Sriastradh      break;
49403464ebd5Sriastradh   case GL_UNSIGNED_INT:
49413464ebd5Sriastradh      {
49423464ebd5Sriastradh         GLuint *dst = (GLuint *) dest;
49433464ebd5Sriastradh         GLuint i;
49443464ebd5Sriastradh         for (i = 0; i < n; i++) {
49453464ebd5Sriastradh            dst[i] = (GLuint) source[i];
49463464ebd5Sriastradh         }
49473464ebd5Sriastradh         if (dstPacking->SwapBytes) {
49483464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
49493464ebd5Sriastradh         }
49503464ebd5Sriastradh      }
49513464ebd5Sriastradh      break;
49523464ebd5Sriastradh   case GL_INT:
49533464ebd5Sriastradh      {
49543464ebd5Sriastradh         GLint *dst = (GLint *) dest;
49553464ebd5Sriastradh         GLuint i;
49563464ebd5Sriastradh         for (i = 0; i < n; i++) {
49573464ebd5Sriastradh            dst[i] = (GLint) source[i];
49583464ebd5Sriastradh         }
49593464ebd5Sriastradh         if (dstPacking->SwapBytes) {
49603464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
49613464ebd5Sriastradh         }
49623464ebd5Sriastradh      }
49633464ebd5Sriastradh      break;
49643464ebd5Sriastradh   case GL_FLOAT:
49653464ebd5Sriastradh      {
49663464ebd5Sriastradh         GLfloat *dst = (GLfloat *) dest;
49673464ebd5Sriastradh         GLuint i;
49683464ebd5Sriastradh         for (i = 0; i < n; i++) {
49693464ebd5Sriastradh            dst[i] = (GLfloat) source[i];
49703464ebd5Sriastradh         }
49713464ebd5Sriastradh         if (dstPacking->SwapBytes) {
49723464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
49733464ebd5Sriastradh         }
49743464ebd5Sriastradh      }
49753464ebd5Sriastradh      break;
49763464ebd5Sriastradh   case GL_HALF_FLOAT_ARB:
49773464ebd5Sriastradh      {
49783464ebd5Sriastradh         GLhalfARB *dst = (GLhalfARB *) dest;
49793464ebd5Sriastradh         GLuint i;
49803464ebd5Sriastradh         for (i = 0; i < n; i++) {
49813464ebd5Sriastradh            dst[i] = _mesa_float_to_half((GLfloat) source[i]);
49823464ebd5Sriastradh         }
49833464ebd5Sriastradh         if (dstPacking->SwapBytes) {
49843464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
49853464ebd5Sriastradh         }
49863464ebd5Sriastradh      }
49873464ebd5Sriastradh      break;
49883464ebd5Sriastradh   default:
49893464ebd5Sriastradh      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
49903464ebd5Sriastradh   }
49913464ebd5Sriastradh
49923464ebd5Sriastradh   free(indexes);
49933464ebd5Sriastradh}
49943464ebd5Sriastradh
49953464ebd5Sriastradh
49963464ebd5Sriastradh/*
49973464ebd5Sriastradh * Unpack a row of stencil data from a client buffer according to
49983464ebd5Sriastradh * the pixel unpacking parameters.
49993464ebd5Sriastradh * This is (or will be) used by glDrawPixels
50003464ebd5Sriastradh *
50013464ebd5Sriastradh * Args:  ctx - the context
50023464ebd5Sriastradh *        n - number of pixels
50033464ebd5Sriastradh *        dstType - destination data type
50043464ebd5Sriastradh *        dest - destination array
50053464ebd5Sriastradh *        srcType - source pixel type
50063464ebd5Sriastradh *        source - source data pointer
50073464ebd5Sriastradh *        srcPacking - pixel unpacking parameters
50083464ebd5Sriastradh *        transferOps - apply offset/bias/lookup ops?
50093464ebd5Sriastradh */
50103464ebd5Sriastradhvoid
50113464ebd5Sriastradh_mesa_unpack_stencil_span( struct gl_context *ctx, GLuint n,
50123464ebd5Sriastradh                           GLenum dstType, GLvoid *dest,
50133464ebd5Sriastradh                           GLenum srcType, const GLvoid *source,
50143464ebd5Sriastradh                           const struct gl_pixelstore_attrib *srcPacking,
50153464ebd5Sriastradh                           GLbitfield transferOps )
50163464ebd5Sriastradh{
50173464ebd5Sriastradh   ASSERT(srcType == GL_BITMAP ||
50183464ebd5Sriastradh          srcType == GL_UNSIGNED_BYTE ||
50193464ebd5Sriastradh          srcType == GL_BYTE ||
50203464ebd5Sriastradh          srcType == GL_UNSIGNED_SHORT ||
50213464ebd5Sriastradh          srcType == GL_SHORT ||
50223464ebd5Sriastradh          srcType == GL_UNSIGNED_INT ||
50233464ebd5Sriastradh          srcType == GL_INT ||
50243464ebd5Sriastradh          srcType == GL_UNSIGNED_INT_24_8_EXT ||
50253464ebd5Sriastradh          srcType == GL_HALF_FLOAT_ARB ||
5026af69d88dSmrg          srcType == GL_FLOAT ||
5027af69d88dSmrg          srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
50283464ebd5Sriastradh
50293464ebd5Sriastradh   ASSERT(dstType == GL_UNSIGNED_BYTE ||
50303464ebd5Sriastradh          dstType == GL_UNSIGNED_SHORT ||
5031af69d88dSmrg          dstType == GL_UNSIGNED_INT ||
5032af69d88dSmrg          dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
50333464ebd5Sriastradh
50343464ebd5Sriastradh   /* only shift and offset apply to stencil */
50353464ebd5Sriastradh   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
50363464ebd5Sriastradh
50373464ebd5Sriastradh   /*
50383464ebd5Sriastradh    * Try simple cases first
50393464ebd5Sriastradh    */
50403464ebd5Sriastradh   if (transferOps == 0 &&
50413464ebd5Sriastradh       !ctx->Pixel.MapStencilFlag &&
50423464ebd5Sriastradh       srcType == GL_UNSIGNED_BYTE &&
50433464ebd5Sriastradh       dstType == GL_UNSIGNED_BYTE) {
50443464ebd5Sriastradh      memcpy(dest, source, n * sizeof(GLubyte));
50453464ebd5Sriastradh   }
50463464ebd5Sriastradh   else if (transferOps == 0 &&
50473464ebd5Sriastradh            !ctx->Pixel.MapStencilFlag &&
50483464ebd5Sriastradh            srcType == GL_UNSIGNED_INT &&
50493464ebd5Sriastradh            dstType == GL_UNSIGNED_INT &&
50503464ebd5Sriastradh            !srcPacking->SwapBytes) {
50513464ebd5Sriastradh      memcpy(dest, source, n * sizeof(GLuint));
50523464ebd5Sriastradh   }
50533464ebd5Sriastradh   else {
50543464ebd5Sriastradh      /*
50553464ebd5Sriastradh       * general solution
50563464ebd5Sriastradh       */
5057af69d88dSmrg      GLuint *indexes = malloc(n * sizeof(GLuint));
50583464ebd5Sriastradh
50593464ebd5Sriastradh      if (!indexes) {
50603464ebd5Sriastradh         _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil unpacking");
50613464ebd5Sriastradh         return;
50623464ebd5Sriastradh      }
50633464ebd5Sriastradh
50643464ebd5Sriastradh      extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
50653464ebd5Sriastradh                           srcPacking);
50663464ebd5Sriastradh
50673464ebd5Sriastradh      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
50683464ebd5Sriastradh         /* shift and offset indexes */
50693464ebd5Sriastradh         _mesa_shift_and_offset_ci(ctx, n, indexes);
50703464ebd5Sriastradh      }
50713464ebd5Sriastradh
50723464ebd5Sriastradh      if (ctx->Pixel.MapStencilFlag) {
50733464ebd5Sriastradh         /* Apply stencil lookup table */
50743464ebd5Sriastradh         const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
50753464ebd5Sriastradh         GLuint i;
50763464ebd5Sriastradh         for (i = 0; i < n; i++) {
50773464ebd5Sriastradh            indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
50783464ebd5Sriastradh         }
50793464ebd5Sriastradh      }
50803464ebd5Sriastradh
50813464ebd5Sriastradh      /* convert to dest type */
50823464ebd5Sriastradh      switch (dstType) {
50833464ebd5Sriastradh         case GL_UNSIGNED_BYTE:
50843464ebd5Sriastradh            {
50853464ebd5Sriastradh               GLubyte *dst = (GLubyte *) dest;
50863464ebd5Sriastradh               GLuint i;
50873464ebd5Sriastradh               for (i = 0; i < n; i++) {
50883464ebd5Sriastradh                  dst[i] = (GLubyte) (indexes[i] & 0xff);
50893464ebd5Sriastradh               }
50903464ebd5Sriastradh            }
50913464ebd5Sriastradh            break;
50923464ebd5Sriastradh         case GL_UNSIGNED_SHORT:
50933464ebd5Sriastradh            {
50943464ebd5Sriastradh               GLuint *dst = (GLuint *) dest;
50953464ebd5Sriastradh               GLuint i;
50963464ebd5Sriastradh               for (i = 0; i < n; i++) {
50973464ebd5Sriastradh                  dst[i] = (GLushort) (indexes[i] & 0xffff);
50983464ebd5Sriastradh               }
50993464ebd5Sriastradh            }
51003464ebd5Sriastradh            break;
51013464ebd5Sriastradh         case GL_UNSIGNED_INT:
51023464ebd5Sriastradh            memcpy(dest, indexes, n * sizeof(GLuint));
51033464ebd5Sriastradh            break;
5104af69d88dSmrg         case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
5105af69d88dSmrg            {
5106af69d88dSmrg               GLuint *dst = (GLuint *) dest;
5107af69d88dSmrg               GLuint i;
5108af69d88dSmrg               for (i = 0; i < n; i++) {
5109af69d88dSmrg                  dst[i*2+1] = indexes[i] & 0xff; /* lower 8 bits */
5110af69d88dSmrg               }
5111af69d88dSmrg            }
5112af69d88dSmrg            break;
51133464ebd5Sriastradh         default:
51143464ebd5Sriastradh            _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
51153464ebd5Sriastradh      }
51163464ebd5Sriastradh
51173464ebd5Sriastradh      free(indexes);
51183464ebd5Sriastradh   }
51193464ebd5Sriastradh}
51203464ebd5Sriastradh
51213464ebd5Sriastradh
51223464ebd5Sriastradhvoid
51233464ebd5Sriastradh_mesa_pack_stencil_span( struct gl_context *ctx, GLuint n,
5124af69d88dSmrg                         GLenum dstType, GLvoid *dest, const GLubyte *source,
51253464ebd5Sriastradh                         const struct gl_pixelstore_attrib *dstPacking )
51263464ebd5Sriastradh{
5127af69d88dSmrg   GLubyte *stencil = malloc(n * sizeof(GLubyte));
51283464ebd5Sriastradh
51293464ebd5Sriastradh   if (!stencil) {
51303464ebd5Sriastradh      _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil packing");
51313464ebd5Sriastradh      return;
51323464ebd5Sriastradh   }
51333464ebd5Sriastradh
51343464ebd5Sriastradh   if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
51353464ebd5Sriastradh       ctx->Pixel.MapStencilFlag) {
51363464ebd5Sriastradh      /* make a copy of input */
5137af69d88dSmrg      memcpy(stencil, source, n * sizeof(GLubyte));
51383464ebd5Sriastradh      _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
51393464ebd5Sriastradh      source = stencil;
51403464ebd5Sriastradh   }
51413464ebd5Sriastradh
51423464ebd5Sriastradh   switch (dstType) {
51433464ebd5Sriastradh   case GL_UNSIGNED_BYTE:
5144af69d88dSmrg      memcpy(dest, source, n);
51453464ebd5Sriastradh      break;
51463464ebd5Sriastradh   case GL_BYTE:
51473464ebd5Sriastradh      {
51483464ebd5Sriastradh         GLbyte *dst = (GLbyte *) dest;
51493464ebd5Sriastradh         GLuint i;
51503464ebd5Sriastradh         for (i=0;i<n;i++) {
51513464ebd5Sriastradh            dst[i] = (GLbyte) (source[i] & 0x7f);
51523464ebd5Sriastradh         }
51533464ebd5Sriastradh      }
51543464ebd5Sriastradh      break;
51553464ebd5Sriastradh   case GL_UNSIGNED_SHORT:
51563464ebd5Sriastradh      {
51573464ebd5Sriastradh         GLushort *dst = (GLushort *) dest;
51583464ebd5Sriastradh         GLuint i;
51593464ebd5Sriastradh         for (i=0;i<n;i++) {
51603464ebd5Sriastradh            dst[i] = (GLushort) source[i];
51613464ebd5Sriastradh         }
51623464ebd5Sriastradh         if (dstPacking->SwapBytes) {
51633464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
51643464ebd5Sriastradh         }
51653464ebd5Sriastradh      }
51663464ebd5Sriastradh      break;
51673464ebd5Sriastradh   case GL_SHORT:
51683464ebd5Sriastradh      {
51693464ebd5Sriastradh         GLshort *dst = (GLshort *) dest;
51703464ebd5Sriastradh         GLuint i;
51713464ebd5Sriastradh         for (i=0;i<n;i++) {
51723464ebd5Sriastradh            dst[i] = (GLshort) source[i];
51733464ebd5Sriastradh         }
51743464ebd5Sriastradh         if (dstPacking->SwapBytes) {
51753464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
51763464ebd5Sriastradh         }
51773464ebd5Sriastradh      }
51783464ebd5Sriastradh      break;
51793464ebd5Sriastradh   case GL_UNSIGNED_INT:
51803464ebd5Sriastradh      {
51813464ebd5Sriastradh         GLuint *dst = (GLuint *) dest;
51823464ebd5Sriastradh         GLuint i;
51833464ebd5Sriastradh         for (i=0;i<n;i++) {
51843464ebd5Sriastradh            dst[i] = (GLuint) source[i];
51853464ebd5Sriastradh         }
51863464ebd5Sriastradh         if (dstPacking->SwapBytes) {
51873464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
51883464ebd5Sriastradh         }
51893464ebd5Sriastradh      }
51903464ebd5Sriastradh      break;
51913464ebd5Sriastradh   case GL_INT:
51923464ebd5Sriastradh      {
51933464ebd5Sriastradh         GLint *dst = (GLint *) dest;
51943464ebd5Sriastradh         GLuint i;
51953464ebd5Sriastradh         for (i=0;i<n;i++) {
51963464ebd5Sriastradh            dst[i] = (GLint) source[i];
51973464ebd5Sriastradh         }
51983464ebd5Sriastradh         if (dstPacking->SwapBytes) {
51993464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
52003464ebd5Sriastradh         }
52013464ebd5Sriastradh      }
52023464ebd5Sriastradh      break;
52033464ebd5Sriastradh   case GL_FLOAT:
52043464ebd5Sriastradh      {
52053464ebd5Sriastradh         GLfloat *dst = (GLfloat *) dest;
52063464ebd5Sriastradh         GLuint i;
52073464ebd5Sriastradh         for (i=0;i<n;i++) {
52083464ebd5Sriastradh            dst[i] = (GLfloat) source[i];
52093464ebd5Sriastradh         }
52103464ebd5Sriastradh         if (dstPacking->SwapBytes) {
52113464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
52123464ebd5Sriastradh         }
52133464ebd5Sriastradh      }
52143464ebd5Sriastradh      break;
52153464ebd5Sriastradh   case GL_HALF_FLOAT_ARB:
52163464ebd5Sriastradh      {
52173464ebd5Sriastradh         GLhalfARB *dst = (GLhalfARB *) dest;
52183464ebd5Sriastradh         GLuint i;
52193464ebd5Sriastradh         for (i=0;i<n;i++) {
52203464ebd5Sriastradh            dst[i] = _mesa_float_to_half( (float) source[i] );
52213464ebd5Sriastradh         }
52223464ebd5Sriastradh         if (dstPacking->SwapBytes) {
52233464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
52243464ebd5Sriastradh         }
52253464ebd5Sriastradh      }
52263464ebd5Sriastradh      break;
52273464ebd5Sriastradh   case GL_BITMAP:
52283464ebd5Sriastradh      if (dstPacking->LsbFirst) {
52293464ebd5Sriastradh         GLubyte *dst = (GLubyte *) dest;
52303464ebd5Sriastradh         GLint shift = 0;
52313464ebd5Sriastradh         GLuint i;
52323464ebd5Sriastradh         for (i = 0; i < n; i++) {
52333464ebd5Sriastradh            if (shift == 0)
52343464ebd5Sriastradh               *dst = 0;
52353464ebd5Sriastradh            *dst |= ((source[i] != 0) << shift);
52363464ebd5Sriastradh            shift++;
52373464ebd5Sriastradh            if (shift == 8) {
52383464ebd5Sriastradh               shift = 0;
52393464ebd5Sriastradh               dst++;
52403464ebd5Sriastradh            }
52413464ebd5Sriastradh         }
52423464ebd5Sriastradh      }
52433464ebd5Sriastradh      else {
52443464ebd5Sriastradh         GLubyte *dst = (GLubyte *) dest;
52453464ebd5Sriastradh         GLint shift = 7;
52463464ebd5Sriastradh         GLuint i;
52473464ebd5Sriastradh         for (i = 0; i < n; i++) {
52483464ebd5Sriastradh            if (shift == 7)
52493464ebd5Sriastradh               *dst = 0;
52503464ebd5Sriastradh            *dst |= ((source[i] != 0) << shift);
52513464ebd5Sriastradh            shift--;
52523464ebd5Sriastradh            if (shift < 0) {
52533464ebd5Sriastradh               shift = 7;
52543464ebd5Sriastradh               dst++;
52553464ebd5Sriastradh            }
52563464ebd5Sriastradh         }
52573464ebd5Sriastradh      }
52583464ebd5Sriastradh      break;
52593464ebd5Sriastradh   default:
52603464ebd5Sriastradh      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
52613464ebd5Sriastradh   }
52623464ebd5Sriastradh
52633464ebd5Sriastradh   free(stencil);
52643464ebd5Sriastradh}
52653464ebd5Sriastradh
52663464ebd5Sriastradh#define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT)                              \
52673464ebd5Sriastradh    do {                                                                \
52683464ebd5Sriastradh        GLuint i;                                                       \
52693464ebd5Sriastradh        const GLTYPE *src = (const GLTYPE *)source;                     \
52703464ebd5Sriastradh        for (i = 0; i < n; i++) {                                       \
52713464ebd5Sriastradh            GLTYPE value = src[i];                                      \
52723464ebd5Sriastradh            if (srcPacking->SwapBytes) {                                \
52733464ebd5Sriastradh                if (sizeof(GLTYPE) == 2) {                              \
52743464ebd5Sriastradh                    SWAP2BYTE(value);                                   \
52753464ebd5Sriastradh                } else if (sizeof(GLTYPE) == 4) {                       \
52763464ebd5Sriastradh                    SWAP4BYTE(value);                                   \
52773464ebd5Sriastradh                }                                                       \
52783464ebd5Sriastradh            }                                                           \
52793464ebd5Sriastradh            depthValues[i] = GLTYPE2FLOAT(value);                       \
52803464ebd5Sriastradh        }                                                               \
52813464ebd5Sriastradh    } while (0)
52823464ebd5Sriastradh
52833464ebd5Sriastradh
52843464ebd5Sriastradh/**
52853464ebd5Sriastradh * Unpack a row of depth/z values from memory, returning GLushort, GLuint
52863464ebd5Sriastradh * or GLfloat values.
52873464ebd5Sriastradh * The glPixelTransfer (scale/bias) params will be applied.
52883464ebd5Sriastradh *
52893464ebd5Sriastradh * \param dstType  one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
52903464ebd5Sriastradh * \param depthMax  max value for returned GLushort or GLuint values
52913464ebd5Sriastradh *                  (ignored for GLfloat).
52923464ebd5Sriastradh */
52933464ebd5Sriastradhvoid
52943464ebd5Sriastradh_mesa_unpack_depth_span( struct gl_context *ctx, GLuint n,
52953464ebd5Sriastradh                         GLenum dstType, GLvoid *dest, GLuint depthMax,
52963464ebd5Sriastradh                         GLenum srcType, const GLvoid *source,
52973464ebd5Sriastradh                         const struct gl_pixelstore_attrib *srcPacking )
52983464ebd5Sriastradh{
52993464ebd5Sriastradh   GLfloat *depthTemp = NULL, *depthValues;
53003464ebd5Sriastradh   GLboolean needClamp = GL_FALSE;
53013464ebd5Sriastradh
53023464ebd5Sriastradh   /* Look for special cases first.
53033464ebd5Sriastradh    * Not only are these faster, they're less prone to numeric conversion
53043464ebd5Sriastradh    * problems.  Otherwise, converting from an int type to a float then
53053464ebd5Sriastradh    * back to an int type can introduce errors that will show up as
53063464ebd5Sriastradh    * artifacts in things like depth peeling which uses glCopyTexImage.
53073464ebd5Sriastradh    */
53083464ebd5Sriastradh   if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
53093464ebd5Sriastradh      if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
53103464ebd5Sriastradh         const GLuint *src = (const GLuint *) source;
53113464ebd5Sriastradh         GLushort *dst = (GLushort *) dest;
53123464ebd5Sriastradh         GLuint i;
53133464ebd5Sriastradh         for (i = 0; i < n; i++) {
53143464ebd5Sriastradh            dst[i] = src[i] >> 16;
53153464ebd5Sriastradh         }
53163464ebd5Sriastradh         return;
53173464ebd5Sriastradh      }
53183464ebd5Sriastradh      if (srcType == GL_UNSIGNED_SHORT
53193464ebd5Sriastradh          && dstType == GL_UNSIGNED_INT
53203464ebd5Sriastradh          && depthMax == 0xffffffff) {
53213464ebd5Sriastradh         const GLushort *src = (const GLushort *) source;
53223464ebd5Sriastradh         GLuint *dst = (GLuint *) dest;
53233464ebd5Sriastradh         GLuint i;
53243464ebd5Sriastradh         for (i = 0; i < n; i++) {
53253464ebd5Sriastradh            dst[i] = src[i] | (src[i] << 16);
53263464ebd5Sriastradh         }
53273464ebd5Sriastradh         return;
53283464ebd5Sriastradh      }
53293464ebd5Sriastradh      if (srcType == GL_UNSIGNED_INT_24_8
53303464ebd5Sriastradh          && dstType == GL_UNSIGNED_INT
53313464ebd5Sriastradh          && depthMax == 0xffffff) {
53323464ebd5Sriastradh         const GLuint *src = (const GLuint *) source;
53333464ebd5Sriastradh         GLuint *dst = (GLuint *) dest;
53343464ebd5Sriastradh         GLuint i;
53353464ebd5Sriastradh         for (i = 0; i < n; i++) {
53363464ebd5Sriastradh            dst[i] = src[i] >> 8;
53373464ebd5Sriastradh         }
53383464ebd5Sriastradh         return;
53393464ebd5Sriastradh      }
53403464ebd5Sriastradh      /* XXX may want to add additional cases here someday */
53413464ebd5Sriastradh   }
53423464ebd5Sriastradh
53433464ebd5Sriastradh   /* general case path follows */
53443464ebd5Sriastradh
53453464ebd5Sriastradh   if (dstType == GL_FLOAT) {
53463464ebd5Sriastradh      depthValues = (GLfloat *) dest;
53473464ebd5Sriastradh   }
53483464ebd5Sriastradh   else {
5349af69d88dSmrg      depthTemp = malloc(n * sizeof(GLfloat));
53503464ebd5Sriastradh      if (!depthTemp) {
53513464ebd5Sriastradh         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
53523464ebd5Sriastradh         return;
53533464ebd5Sriastradh      }
53543464ebd5Sriastradh
53553464ebd5Sriastradh      depthValues = depthTemp;
53563464ebd5Sriastradh   }
53573464ebd5Sriastradh
53583464ebd5Sriastradh   /* Convert incoming values to GLfloat.  Some conversions will require
53593464ebd5Sriastradh    * clamping, below.
53603464ebd5Sriastradh    */
53613464ebd5Sriastradh   switch (srcType) {
53623464ebd5Sriastradh      case GL_BYTE:
5363af69d88dSmrg         DEPTH_VALUES(GLbyte, BYTE_TO_FLOATZ);
53643464ebd5Sriastradh         needClamp = GL_TRUE;
53653464ebd5Sriastradh         break;
53663464ebd5Sriastradh      case GL_UNSIGNED_BYTE:
53673464ebd5Sriastradh         DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
53683464ebd5Sriastradh         break;
53693464ebd5Sriastradh      case GL_SHORT:
5370af69d88dSmrg         DEPTH_VALUES(GLshort, SHORT_TO_FLOATZ);
53713464ebd5Sriastradh         needClamp = GL_TRUE;
53723464ebd5Sriastradh         break;
53733464ebd5Sriastradh      case GL_UNSIGNED_SHORT:
53743464ebd5Sriastradh         DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
53753464ebd5Sriastradh         break;
53763464ebd5Sriastradh      case GL_INT:
53773464ebd5Sriastradh         DEPTH_VALUES(GLint, INT_TO_FLOAT);
53783464ebd5Sriastradh         needClamp = GL_TRUE;
53793464ebd5Sriastradh         break;
53803464ebd5Sriastradh      case GL_UNSIGNED_INT:
53813464ebd5Sriastradh         DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
53823464ebd5Sriastradh         break;
53833464ebd5Sriastradh      case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
53843464ebd5Sriastradh         if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
53853464ebd5Sriastradh             depthMax == 0xffffff &&
53863464ebd5Sriastradh             ctx->Pixel.DepthScale == 1.0 &&
53873464ebd5Sriastradh             ctx->Pixel.DepthBias == 0.0) {
53883464ebd5Sriastradh            const GLuint *src = (const GLuint *) source;
53893464ebd5Sriastradh            GLuint *zValues = (GLuint *) dest;
53903464ebd5Sriastradh            GLuint i;
53913464ebd5Sriastradh            for (i = 0; i < n; i++) {
53923464ebd5Sriastradh                GLuint value = src[i];
53933464ebd5Sriastradh                if (srcPacking->SwapBytes) {
53943464ebd5Sriastradh                    SWAP4BYTE(value);
53953464ebd5Sriastradh                }
53963464ebd5Sriastradh                zValues[i] = value & 0xffffff00;
53973464ebd5Sriastradh            }
53983464ebd5Sriastradh            free(depthTemp);
53993464ebd5Sriastradh            return;
54003464ebd5Sriastradh         }
54013464ebd5Sriastradh         else {
54023464ebd5Sriastradh            const GLuint *src = (const GLuint *) source;
54033464ebd5Sriastradh            const GLfloat scale = 1.0f / 0xffffff;
54043464ebd5Sriastradh            GLuint i;
54053464ebd5Sriastradh            for (i = 0; i < n; i++) {
54063464ebd5Sriastradh                GLuint value = src[i];
54073464ebd5Sriastradh                if (srcPacking->SwapBytes) {
54083464ebd5Sriastradh                    SWAP4BYTE(value);
54093464ebd5Sriastradh                }
54103464ebd5Sriastradh                depthValues[i] = (value >> 8) * scale;
54113464ebd5Sriastradh            }
54123464ebd5Sriastradh         }
54133464ebd5Sriastradh         break;
5414af69d88dSmrg      case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
5415af69d88dSmrg         {
5416af69d88dSmrg            GLuint i;
5417af69d88dSmrg            const GLfloat *src = (const GLfloat *)source;
5418af69d88dSmrg            for (i = 0; i < n; i++) {
5419af69d88dSmrg               GLfloat value = src[i * 2];
5420af69d88dSmrg               if (srcPacking->SwapBytes) {
5421af69d88dSmrg                  SWAP4BYTE(value);
5422af69d88dSmrg               }
5423af69d88dSmrg               depthValues[i] = value;
5424af69d88dSmrg            }
5425af69d88dSmrg            needClamp = GL_TRUE;
5426af69d88dSmrg         }
5427af69d88dSmrg         break;
54283464ebd5Sriastradh      case GL_FLOAT:
54293464ebd5Sriastradh         DEPTH_VALUES(GLfloat, 1*);
54303464ebd5Sriastradh         needClamp = GL_TRUE;
54313464ebd5Sriastradh         break;
54323464ebd5Sriastradh      case GL_HALF_FLOAT_ARB:
54333464ebd5Sriastradh         {
54343464ebd5Sriastradh            GLuint i;
54353464ebd5Sriastradh            const GLhalfARB *src = (const GLhalfARB *) source;
54363464ebd5Sriastradh            for (i = 0; i < n; i++) {
54373464ebd5Sriastradh               GLhalfARB value = src[i];
54383464ebd5Sriastradh               if (srcPacking->SwapBytes) {
54393464ebd5Sriastradh                  SWAP2BYTE(value);
54403464ebd5Sriastradh               }
54413464ebd5Sriastradh               depthValues[i] = _mesa_half_to_float(value);
54423464ebd5Sriastradh            }
54433464ebd5Sriastradh            needClamp = GL_TRUE;
54443464ebd5Sriastradh         }
54453464ebd5Sriastradh         break;
54463464ebd5Sriastradh      default:
54473464ebd5Sriastradh         _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
54483464ebd5Sriastradh         free(depthTemp);
54493464ebd5Sriastradh         return;
54503464ebd5Sriastradh   }
54513464ebd5Sriastradh
54523464ebd5Sriastradh   /* apply depth scale and bias */
54533464ebd5Sriastradh   {
54543464ebd5Sriastradh      const GLfloat scale = ctx->Pixel.DepthScale;
54553464ebd5Sriastradh      const GLfloat bias = ctx->Pixel.DepthBias;
54563464ebd5Sriastradh      if (scale != 1.0 || bias != 0.0) {
54573464ebd5Sriastradh         GLuint i;
54583464ebd5Sriastradh         for (i = 0; i < n; i++) {
54593464ebd5Sriastradh            depthValues[i] = depthValues[i] * scale + bias;
54603464ebd5Sriastradh         }
54613464ebd5Sriastradh         needClamp = GL_TRUE;
54623464ebd5Sriastradh      }
54633464ebd5Sriastradh   }
54643464ebd5Sriastradh
54653464ebd5Sriastradh   /* clamp to [0, 1] */
54663464ebd5Sriastradh   if (needClamp) {
54673464ebd5Sriastradh      GLuint i;
54683464ebd5Sriastradh      for (i = 0; i < n; i++) {
54693464ebd5Sriastradh         depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
54703464ebd5Sriastradh      }
54713464ebd5Sriastradh   }
54723464ebd5Sriastradh
54733464ebd5Sriastradh   /*
54743464ebd5Sriastradh    * Convert values to dstType
54753464ebd5Sriastradh    */
54763464ebd5Sriastradh   if (dstType == GL_UNSIGNED_INT) {
54773464ebd5Sriastradh      GLuint *zValues = (GLuint *) dest;
54783464ebd5Sriastradh      GLuint i;
54793464ebd5Sriastradh      if (depthMax <= 0xffffff) {
54803464ebd5Sriastradh         /* no overflow worries */
54813464ebd5Sriastradh         for (i = 0; i < n; i++) {
54823464ebd5Sriastradh            zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
54833464ebd5Sriastradh         }
54843464ebd5Sriastradh      }
54853464ebd5Sriastradh      else {
54863464ebd5Sriastradh         /* need to use double precision to prevent overflow problems */
54873464ebd5Sriastradh         for (i = 0; i < n; i++) {
5488af69d88dSmrg            GLdouble z = depthValues[i] * (GLdouble) depthMax;
54893464ebd5Sriastradh            if (z >= (GLdouble) 0xffffffff)
54903464ebd5Sriastradh               zValues[i] = 0xffffffff;
54913464ebd5Sriastradh            else
54923464ebd5Sriastradh               zValues[i] = (GLuint) z;
54933464ebd5Sriastradh         }
54943464ebd5Sriastradh      }
54953464ebd5Sriastradh   }
54963464ebd5Sriastradh   else if (dstType == GL_UNSIGNED_SHORT) {
54973464ebd5Sriastradh      GLushort *zValues = (GLushort *) dest;
54983464ebd5Sriastradh      GLuint i;
54993464ebd5Sriastradh      ASSERT(depthMax <= 0xffff);
55003464ebd5Sriastradh      for (i = 0; i < n; i++) {
55013464ebd5Sriastradh         zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
55023464ebd5Sriastradh      }
55033464ebd5Sriastradh   }
5504af69d88dSmrg   else if (dstType == GL_FLOAT) {
5505af69d88dSmrg      /* Nothing to do. depthValues is pointing to dest. */
5506af69d88dSmrg   }
5507af69d88dSmrg   else if (dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) {
5508af69d88dSmrg      GLfloat *zValues = (GLfloat*) dest;
5509af69d88dSmrg      GLuint i;
5510af69d88dSmrg      for (i = 0; i < n; i++) {
5511af69d88dSmrg         zValues[i*2] = depthValues[i];
5512af69d88dSmrg      }
5513af69d88dSmrg   }
55143464ebd5Sriastradh   else {
5515af69d88dSmrg      ASSERT(0);
55163464ebd5Sriastradh   }
55173464ebd5Sriastradh
55183464ebd5Sriastradh   free(depthTemp);
55193464ebd5Sriastradh}
55203464ebd5Sriastradh
55213464ebd5Sriastradh
55223464ebd5Sriastradh/*
55233464ebd5Sriastradh * Pack an array of depth values.  The values are floats in [0,1].
55243464ebd5Sriastradh */
55253464ebd5Sriastradhvoid
55263464ebd5Sriastradh_mesa_pack_depth_span( struct gl_context *ctx, GLuint n, GLvoid *dest,
55273464ebd5Sriastradh                       GLenum dstType, const GLfloat *depthSpan,
55283464ebd5Sriastradh                       const struct gl_pixelstore_attrib *dstPacking )
55293464ebd5Sriastradh{
5530af69d88dSmrg   GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
55313464ebd5Sriastradh   if (!depthCopy) {
55323464ebd5Sriastradh      _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
55333464ebd5Sriastradh      return;
55343464ebd5Sriastradh   }
55353464ebd5Sriastradh
55363464ebd5Sriastradh   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
55373464ebd5Sriastradh      memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
55383464ebd5Sriastradh      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
55393464ebd5Sriastradh      depthSpan = depthCopy;
55403464ebd5Sriastradh   }
55413464ebd5Sriastradh
55423464ebd5Sriastradh   switch (dstType) {
55433464ebd5Sriastradh   case GL_UNSIGNED_BYTE:
55443464ebd5Sriastradh      {
55453464ebd5Sriastradh         GLubyte *dst = (GLubyte *) dest;
55463464ebd5Sriastradh         GLuint i;
55473464ebd5Sriastradh         for (i = 0; i < n; i++) {
55483464ebd5Sriastradh            dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
55493464ebd5Sriastradh         }
55503464ebd5Sriastradh      }
55513464ebd5Sriastradh      break;
55523464ebd5Sriastradh   case GL_BYTE:
55533464ebd5Sriastradh      {
55543464ebd5Sriastradh         GLbyte *dst = (GLbyte *) dest;
55553464ebd5Sriastradh         GLuint i;
55563464ebd5Sriastradh         for (i = 0; i < n; i++) {
55573464ebd5Sriastradh            dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
55583464ebd5Sriastradh         }
55593464ebd5Sriastradh      }
55603464ebd5Sriastradh      break;
55613464ebd5Sriastradh   case GL_UNSIGNED_SHORT:
55623464ebd5Sriastradh      {
55633464ebd5Sriastradh         GLushort *dst = (GLushort *) dest;
55643464ebd5Sriastradh         GLuint i;
55653464ebd5Sriastradh         for (i = 0; i < n; i++) {
55663464ebd5Sriastradh            CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
55673464ebd5Sriastradh         }
55683464ebd5Sriastradh         if (dstPacking->SwapBytes) {
55693464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
55703464ebd5Sriastradh         }
55713464ebd5Sriastradh      }
55723464ebd5Sriastradh      break;
55733464ebd5Sriastradh   case GL_SHORT:
55743464ebd5Sriastradh      {
55753464ebd5Sriastradh         GLshort *dst = (GLshort *) dest;
55763464ebd5Sriastradh         GLuint i;
55773464ebd5Sriastradh         for (i = 0; i < n; i++) {
55783464ebd5Sriastradh            dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
55793464ebd5Sriastradh         }
55803464ebd5Sriastradh         if (dstPacking->SwapBytes) {
55813464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
55823464ebd5Sriastradh         }
55833464ebd5Sriastradh      }
55843464ebd5Sriastradh      break;
55853464ebd5Sriastradh   case GL_UNSIGNED_INT:
55863464ebd5Sriastradh      {
55873464ebd5Sriastradh         GLuint *dst = (GLuint *) dest;
55883464ebd5Sriastradh         GLuint i;
55893464ebd5Sriastradh         for (i = 0; i < n; i++) {
55903464ebd5Sriastradh            dst[i] = FLOAT_TO_UINT( depthSpan[i] );
55913464ebd5Sriastradh         }
55923464ebd5Sriastradh         if (dstPacking->SwapBytes) {
55933464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
55943464ebd5Sriastradh         }
55953464ebd5Sriastradh      }
55963464ebd5Sriastradh      break;
55973464ebd5Sriastradh   case GL_INT:
55983464ebd5Sriastradh      {
55993464ebd5Sriastradh         GLint *dst = (GLint *) dest;
56003464ebd5Sriastradh         GLuint i;
56013464ebd5Sriastradh         for (i = 0; i < n; i++) {
56023464ebd5Sriastradh            dst[i] = FLOAT_TO_INT( depthSpan[i] );
56033464ebd5Sriastradh         }
56043464ebd5Sriastradh         if (dstPacking->SwapBytes) {
56053464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
56063464ebd5Sriastradh         }
56073464ebd5Sriastradh      }
56083464ebd5Sriastradh      break;
56093464ebd5Sriastradh   case GL_FLOAT:
56103464ebd5Sriastradh      {
56113464ebd5Sriastradh         GLfloat *dst = (GLfloat *) dest;
56123464ebd5Sriastradh         GLuint i;
56133464ebd5Sriastradh         for (i = 0; i < n; i++) {
56143464ebd5Sriastradh            dst[i] = depthSpan[i];
56153464ebd5Sriastradh         }
56163464ebd5Sriastradh         if (dstPacking->SwapBytes) {
56173464ebd5Sriastradh            _mesa_swap4( (GLuint *) dst, n );
56183464ebd5Sriastradh         }
56193464ebd5Sriastradh      }
56203464ebd5Sriastradh      break;
56213464ebd5Sriastradh   case GL_HALF_FLOAT_ARB:
56223464ebd5Sriastradh      {
56233464ebd5Sriastradh         GLhalfARB *dst = (GLhalfARB *) dest;
56243464ebd5Sriastradh         GLuint i;
56253464ebd5Sriastradh         for (i = 0; i < n; i++) {
56263464ebd5Sriastradh            dst[i] = _mesa_float_to_half(depthSpan[i]);
56273464ebd5Sriastradh         }
56283464ebd5Sriastradh         if (dstPacking->SwapBytes) {
56293464ebd5Sriastradh            _mesa_swap2( (GLushort *) dst, n );
56303464ebd5Sriastradh         }
56313464ebd5Sriastradh      }
56323464ebd5Sriastradh      break;
56333464ebd5Sriastradh   default:
56343464ebd5Sriastradh      _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
56353464ebd5Sriastradh   }
56363464ebd5Sriastradh
56373464ebd5Sriastradh   free(depthCopy);
56383464ebd5Sriastradh}
56393464ebd5Sriastradh
56403464ebd5Sriastradh
56413464ebd5Sriastradh
56423464ebd5Sriastradh/**
5643af69d88dSmrg * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
56443464ebd5Sriastradh */
56453464ebd5Sriastradhvoid
5646af69d88dSmrg_mesa_pack_depth_stencil_span(struct gl_context *ctx,GLuint n,
5647af69d88dSmrg                              GLenum dstType, GLuint *dest,
56483464ebd5Sriastradh                              const GLfloat *depthVals,
5649af69d88dSmrg                              const GLubyte *stencilVals,
56503464ebd5Sriastradh                              const struct gl_pixelstore_attrib *dstPacking)
56513464ebd5Sriastradh{
5652af69d88dSmrg   GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
5653af69d88dSmrg   GLubyte *stencilCopy = malloc(n * sizeof(GLubyte));
56543464ebd5Sriastradh   GLuint i;
56553464ebd5Sriastradh
56563464ebd5Sriastradh   if (!depthCopy || !stencilCopy) {
56573464ebd5Sriastradh      _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
56583464ebd5Sriastradh      free(depthCopy);
56593464ebd5Sriastradh      free(stencilCopy);
56603464ebd5Sriastradh      return;
56613464ebd5Sriastradh   }
56623464ebd5Sriastradh
56633464ebd5Sriastradh   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
56643464ebd5Sriastradh      memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
56653464ebd5Sriastradh      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
56663464ebd5Sriastradh      depthVals = depthCopy;
56673464ebd5Sriastradh   }
56683464ebd5Sriastradh
56693464ebd5Sriastradh   if (ctx->Pixel.IndexShift ||
56703464ebd5Sriastradh       ctx->Pixel.IndexOffset ||
56713464ebd5Sriastradh       ctx->Pixel.MapStencilFlag) {
5672af69d88dSmrg      memcpy(stencilCopy, stencilVals, n * sizeof(GLubyte));
56733464ebd5Sriastradh      _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
56743464ebd5Sriastradh      stencilVals = stencilCopy;
56753464ebd5Sriastradh   }
56763464ebd5Sriastradh
5677af69d88dSmrg   switch (dstType) {
5678af69d88dSmrg   case GL_UNSIGNED_INT_24_8:
5679af69d88dSmrg      for (i = 0; i < n; i++) {
5680af69d88dSmrg         GLuint z = (GLuint) (depthVals[i] * 0xffffff);
5681af69d88dSmrg         dest[i] = (z << 8) | (stencilVals[i] & 0xff);
5682af69d88dSmrg      }
5683af69d88dSmrg      break;
5684af69d88dSmrg   case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
5685af69d88dSmrg      for (i = 0; i < n; i++) {
5686af69d88dSmrg         ((GLfloat*)dest)[i*2] = depthVals[i];
5687af69d88dSmrg         dest[i*2+1] = stencilVals[i] & 0xff;
5688af69d88dSmrg      }
5689af69d88dSmrg      break;
56903464ebd5Sriastradh   }
56913464ebd5Sriastradh
56923464ebd5Sriastradh   if (dstPacking->SwapBytes) {
56933464ebd5Sriastradh      _mesa_swap4(dest, n);
56943464ebd5Sriastradh   }
56953464ebd5Sriastradh
56963464ebd5Sriastradh   free(depthCopy);
56973464ebd5Sriastradh   free(stencilCopy);
56983464ebd5Sriastradh}
56993464ebd5Sriastradh
57003464ebd5Sriastradh
57013464ebd5Sriastradh
57023464ebd5Sriastradh
57033464ebd5Sriastradh/**
57043464ebd5Sriastradh * Unpack image data.  Apply byte swapping, byte flipping (bitmap).
57053464ebd5Sriastradh * Return all image data in a contiguous block.  This is used when we
57063464ebd5Sriastradh * compile glDrawPixels, glTexImage, etc into a display list.  We
57073464ebd5Sriastradh * need a copy of the data in a standard format.
57083464ebd5Sriastradh */
57093464ebd5Sriastradhvoid *
57103464ebd5Sriastradh_mesa_unpack_image( GLuint dimensions,
57113464ebd5Sriastradh                    GLsizei width, GLsizei height, GLsizei depth,
57123464ebd5Sriastradh                    GLenum format, GLenum type, const GLvoid *pixels,
57133464ebd5Sriastradh                    const struct gl_pixelstore_attrib *unpack )
57143464ebd5Sriastradh{
57153464ebd5Sriastradh   GLint bytesPerRow, compsPerRow;
57163464ebd5Sriastradh   GLboolean flipBytes, swap2, swap4;
57173464ebd5Sriastradh
57183464ebd5Sriastradh   if (!pixels)
57193464ebd5Sriastradh      return NULL;  /* not necessarily an error */
57203464ebd5Sriastradh
57213464ebd5Sriastradh   if (width <= 0 || height <= 0 || depth <= 0)
57223464ebd5Sriastradh      return NULL;  /* generate error later */
57233464ebd5Sriastradh
57243464ebd5Sriastradh   if (type == GL_BITMAP) {
57253464ebd5Sriastradh      bytesPerRow = (width + 7) >> 3;
57263464ebd5Sriastradh      flipBytes = unpack->LsbFirst;
57273464ebd5Sriastradh      swap2 = swap4 = GL_FALSE;
57283464ebd5Sriastradh      compsPerRow = 0;
57293464ebd5Sriastradh   }
57303464ebd5Sriastradh   else {
57313464ebd5Sriastradh      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
57323464ebd5Sriastradh      GLint components = _mesa_components_in_format(format);
57333464ebd5Sriastradh      GLint bytesPerComp;
57343464ebd5Sriastradh
57353464ebd5Sriastradh      if (_mesa_type_is_packed(type))
57363464ebd5Sriastradh          components = 1;
57373464ebd5Sriastradh
57383464ebd5Sriastradh      if (bytesPerPixel <= 0 || components <= 0)
57393464ebd5Sriastradh         return NULL;   /* bad format or type.  generate error later */
57403464ebd5Sriastradh      bytesPerRow = bytesPerPixel * width;
57413464ebd5Sriastradh      bytesPerComp = bytesPerPixel / components;
57423464ebd5Sriastradh      flipBytes = GL_FALSE;
57433464ebd5Sriastradh      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
57443464ebd5Sriastradh      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
57453464ebd5Sriastradh      compsPerRow = components * width;
57463464ebd5Sriastradh      assert(compsPerRow >= width);
57473464ebd5Sriastradh   }
57483464ebd5Sriastradh
57493464ebd5Sriastradh   {
57503464ebd5Sriastradh      GLubyte *destBuffer
5751af69d88dSmrg         = malloc(bytesPerRow * height * depth);
57523464ebd5Sriastradh      GLubyte *dst;
57533464ebd5Sriastradh      GLint img, row;
57543464ebd5Sriastradh      if (!destBuffer)
57553464ebd5Sriastradh         return NULL;   /* generate GL_OUT_OF_MEMORY later */
57563464ebd5Sriastradh
57573464ebd5Sriastradh      dst = destBuffer;
57583464ebd5Sriastradh      for (img = 0; img < depth; img++) {
57593464ebd5Sriastradh         for (row = 0; row < height; row++) {
57603464ebd5Sriastradh            const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
57613464ebd5Sriastradh                               width, height, format, type, img, row, 0);
57623464ebd5Sriastradh
57633464ebd5Sriastradh            if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
57643464ebd5Sriastradh               GLint i;
57653464ebd5Sriastradh               flipBytes = GL_FALSE;
57663464ebd5Sriastradh               if (unpack->LsbFirst) {
57673464ebd5Sriastradh                  GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
57683464ebd5Sriastradh                  GLubyte dstMask = 128;
57693464ebd5Sriastradh                  const GLubyte *s = src;
57703464ebd5Sriastradh                  GLubyte *d = dst;
57713464ebd5Sriastradh                  *d = 0;
57723464ebd5Sriastradh                  for (i = 0; i < width; i++) {
57733464ebd5Sriastradh                     if (*s & srcMask) {
57743464ebd5Sriastradh                        *d |= dstMask;
57753464ebd5Sriastradh                     }
57763464ebd5Sriastradh                     if (srcMask == 128) {
57773464ebd5Sriastradh                        srcMask = 1;
57783464ebd5Sriastradh                        s++;
57793464ebd5Sriastradh                     }
57803464ebd5Sriastradh                     else {
57813464ebd5Sriastradh                        srcMask = srcMask << 1;
57823464ebd5Sriastradh                     }
57833464ebd5Sriastradh                     if (dstMask == 1) {
57843464ebd5Sriastradh                        dstMask = 128;
57853464ebd5Sriastradh                        d++;
57863464ebd5Sriastradh                        *d = 0;
57873464ebd5Sriastradh                     }
57883464ebd5Sriastradh                     else {
57893464ebd5Sriastradh                        dstMask = dstMask >> 1;
57903464ebd5Sriastradh                     }
57913464ebd5Sriastradh                  }
57923464ebd5Sriastradh               }
57933464ebd5Sriastradh               else {
57943464ebd5Sriastradh                  GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
57953464ebd5Sriastradh                  GLubyte dstMask = 128;
57963464ebd5Sriastradh                  const GLubyte *s = src;
57973464ebd5Sriastradh                  GLubyte *d = dst;
57983464ebd5Sriastradh                  *d = 0;
57993464ebd5Sriastradh                  for (i = 0; i < width; i++) {
58003464ebd5Sriastradh                     if (*s & srcMask) {
58013464ebd5Sriastradh                        *d |= dstMask;
58023464ebd5Sriastradh                     }
58033464ebd5Sriastradh                     if (srcMask == 1) {
58043464ebd5Sriastradh                        srcMask = 128;
58053464ebd5Sriastradh                        s++;
58063464ebd5Sriastradh                     }
58073464ebd5Sriastradh                     else {
58083464ebd5Sriastradh                        srcMask = srcMask >> 1;
58093464ebd5Sriastradh                     }
58103464ebd5Sriastradh                     if (dstMask == 1) {
58113464ebd5Sriastradh                        dstMask = 128;
58123464ebd5Sriastradh                        d++;
58133464ebd5Sriastradh                        *d = 0;
58143464ebd5Sriastradh                     }
58153464ebd5Sriastradh                     else {
58163464ebd5Sriastradh                        dstMask = dstMask >> 1;
58173464ebd5Sriastradh                     }
58183464ebd5Sriastradh                  }
58193464ebd5Sriastradh               }
58203464ebd5Sriastradh            }
58213464ebd5Sriastradh            else {
58223464ebd5Sriastradh               memcpy(dst, src, bytesPerRow);
58233464ebd5Sriastradh            }
58243464ebd5Sriastradh
58253464ebd5Sriastradh            /* byte flipping/swapping */
58263464ebd5Sriastradh            if (flipBytes) {
58273464ebd5Sriastradh               flip_bytes((GLubyte *) dst, bytesPerRow);
58283464ebd5Sriastradh            }
58293464ebd5Sriastradh            else if (swap2) {
58303464ebd5Sriastradh               _mesa_swap2((GLushort*) dst, compsPerRow);
58313464ebd5Sriastradh            }
58323464ebd5Sriastradh            else if (swap4) {
58333464ebd5Sriastradh               _mesa_swap4((GLuint*) dst, compsPerRow);
58343464ebd5Sriastradh            }
58353464ebd5Sriastradh            dst += bytesPerRow;
58363464ebd5Sriastradh         }
58373464ebd5Sriastradh      }
58383464ebd5Sriastradh      return destBuffer;
58393464ebd5Sriastradh   }
58403464ebd5Sriastradh}
58413464ebd5Sriastradh
5842af69d88dSmrg
5843af69d88dSmrg
5844af69d88dSmrg/**
5845af69d88dSmrg * If we unpack colors from a luminance surface, we'll get pixel colors
5846af69d88dSmrg * such as (l, l, l, a).
5847af69d88dSmrg * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
5848af69d88dSmrg * function will compute L=R+G+B before packing.  The net effect is we'll
5849af69d88dSmrg * accidentally store luminance values = 3*l.
5850af69d88dSmrg * This function compensates for that by converting (aka rebasing) (l,l,l,a)
5851af69d88dSmrg * to be (l,0,0,a).
5852af69d88dSmrg * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
5853af69d88dSmrg * and INTENSITY.
5854af69d88dSmrg *
5855af69d88dSmrg * Finally, we also need to do this when the actual surface format does
5856af69d88dSmrg * not match the logical surface format.  For example, suppose the user
5857af69d88dSmrg * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
5858af69d88dSmrg * Again, we'll get pixel values like (l,l,l,a).
5859af69d88dSmrg */
5860af69d88dSmrgvoid
5861af69d88dSmrg_mesa_rebase_rgba_float(GLuint n, GLfloat rgba[][4], GLenum baseFormat)
5862af69d88dSmrg{
5863af69d88dSmrg   GLuint i;
5864af69d88dSmrg
5865af69d88dSmrg   switch (baseFormat) {
5866af69d88dSmrg   case GL_ALPHA:
5867af69d88dSmrg      for (i = 0; i < n; i++) {
5868af69d88dSmrg         rgba[i][RCOMP] = 0.0F;
5869af69d88dSmrg         rgba[i][GCOMP] = 0.0F;
5870af69d88dSmrg         rgba[i][BCOMP] = 0.0F;
5871af69d88dSmrg      }
5872af69d88dSmrg      break;
5873af69d88dSmrg   case GL_INTENSITY:
5874af69d88dSmrg      /* fall-through */
5875af69d88dSmrg   case GL_LUMINANCE:
5876af69d88dSmrg      for (i = 0; i < n; i++) {
5877af69d88dSmrg         rgba[i][GCOMP] = 0.0F;
5878af69d88dSmrg         rgba[i][BCOMP] = 0.0F;
5879af69d88dSmrg         rgba[i][ACOMP] = 1.0F;
5880af69d88dSmrg      }
5881af69d88dSmrg      break;
5882af69d88dSmrg   case GL_LUMINANCE_ALPHA:
5883af69d88dSmrg      for (i = 0; i < n; i++) {
5884af69d88dSmrg         rgba[i][GCOMP] = 0.0F;
5885af69d88dSmrg         rgba[i][BCOMP] = 0.0F;
5886af69d88dSmrg      }
5887af69d88dSmrg      break;
5888af69d88dSmrg   case GL_RGB:
5889af69d88dSmrg      for (i = 0; i < n; i++) {
5890af69d88dSmrg         rgba[i][ACOMP] = 1.0F;
5891af69d88dSmrg      }
5892af69d88dSmrg      break;
5893af69d88dSmrg   case GL_RG:
5894af69d88dSmrg      for (i = 0; i < n; i++) {
5895af69d88dSmrg         rgba[i][BCOMP] = 0.0F;
5896af69d88dSmrg         rgba[i][ACOMP] = 1.0F;
5897af69d88dSmrg      }
5898af69d88dSmrg      break;
5899af69d88dSmrg   case GL_RED:
5900af69d88dSmrg      for (i = 0; i < n; i++) {
5901af69d88dSmrg         rgba[i][GCOMP] = 0.0F;
5902af69d88dSmrg         rgba[i][BCOMP] = 0.0F;
5903af69d88dSmrg         rgba[i][ACOMP] = 1.0F;
5904af69d88dSmrg      }
5905af69d88dSmrg      break;
5906af69d88dSmrg
5907af69d88dSmrg   default:
5908af69d88dSmrg      /* no-op */
5909af69d88dSmrg      ;
5910af69d88dSmrg   }
5911af69d88dSmrg}
5912af69d88dSmrg
5913af69d88dSmrg
5914af69d88dSmrg/**
5915af69d88dSmrg * As above, but GLuint components.
5916af69d88dSmrg */
5917af69d88dSmrgvoid
5918af69d88dSmrg_mesa_rebase_rgba_uint(GLuint n, GLuint rgba[][4], GLenum baseFormat)
5919af69d88dSmrg{
5920af69d88dSmrg   GLuint i;
5921af69d88dSmrg
5922af69d88dSmrg   switch (baseFormat) {
5923af69d88dSmrg   case GL_ALPHA:
5924af69d88dSmrg      for (i = 0; i < n; i++) {
5925af69d88dSmrg         rgba[i][RCOMP] = 0;
5926af69d88dSmrg         rgba[i][GCOMP] = 0;
5927af69d88dSmrg         rgba[i][BCOMP] = 0;
5928af69d88dSmrg      }
5929af69d88dSmrg      break;
5930af69d88dSmrg   case GL_INTENSITY:
5931af69d88dSmrg      /* fall-through */
5932af69d88dSmrg   case GL_LUMINANCE:
5933af69d88dSmrg      for (i = 0; i < n; i++) {
5934af69d88dSmrg         rgba[i][GCOMP] = 0;
5935af69d88dSmrg         rgba[i][BCOMP] = 0;
5936af69d88dSmrg         rgba[i][ACOMP] = 1;
5937af69d88dSmrg      }
5938af69d88dSmrg      break;
5939af69d88dSmrg   case GL_LUMINANCE_ALPHA:
5940af69d88dSmrg      for (i = 0; i < n; i++) {
5941af69d88dSmrg         rgba[i][GCOMP] = 0;
5942af69d88dSmrg         rgba[i][BCOMP] = 0;
5943af69d88dSmrg      }
5944af69d88dSmrg      break;
5945af69d88dSmrg   case GL_RGB:
5946af69d88dSmrg      for (i = 0; i < n; i++) {
5947af69d88dSmrg         rgba[i][ACOMP] = 1;
5948af69d88dSmrg      }
5949af69d88dSmrg      break;
5950af69d88dSmrg   case GL_RG:
5951af69d88dSmrg      for (i = 0; i < n; i++) {
5952af69d88dSmrg         rgba[i][BCOMP] = 0;
5953af69d88dSmrg         rgba[i][ACOMP] = 1;
5954af69d88dSmrg      }
5955af69d88dSmrg      break;
5956af69d88dSmrg   case GL_RED:
5957af69d88dSmrg      for (i = 0; i < n; i++) {
5958af69d88dSmrg         rgba[i][GCOMP] = 0;
5959af69d88dSmrg         rgba[i][BCOMP] = 0;
5960af69d88dSmrg         rgba[i][ACOMP] = 1;
5961af69d88dSmrg      }
5962af69d88dSmrg   default:
5963af69d88dSmrg      /* no-op */
5964af69d88dSmrg      ;
5965af69d88dSmrg   }
5966af69d88dSmrg}
5967af69d88dSmrg
5968af69d88dSmrg
5969