pack.c revision 7ef270867cb1f3e19067c93449e48987a32730d3
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Mesa 3-D graphics library
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2009-2010  VMware, Inc.  All Rights Reserved.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copy of this software and associated documentation files (the "Software"),
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * to deal in the Software without restriction, including without limitation
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the rights to use, copy, modify, merge, publish, distribute, sublicense,
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci * and/or sell copies of the Software, and to permit persons to whom the
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Software is furnished to do so, subject to the following conditions:
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu * The above copyright notice and this permission notice shall be included
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * in all copies or substantial portions of the Software.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THEA AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \file pack.c
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Image and pixel span packing and unpacking.
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/*
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * XXX: MSVC takes forever to compile this module for x86_64 unless we disable
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * this global optimization.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * See also:
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - http://msdn.microsoft.com/en-us/library/1yk3ydd7.aspx
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * - http://msdn.microsoft.com/en-us/library/chh3fb0k.aspx
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(_MSC_VER) && defined(_M_X64)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#  pragma optimize( "g", off )
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "glheader.h"
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "colormac.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "enums.h"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "image.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "imports.h"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "macros.h"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "mtypes.h"
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "pack.h"
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pixeltransfer.h"
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "imports.h"
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "glformats.h"
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
6168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) * Flip the 8 bits in each byte of the given array.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param p array.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \param n number of bytes.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * \todo try this trick to flip bytes someday:
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * \code
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *  v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *  v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *  v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * \endcode
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)flip_bytes( GLubyte *p, GLuint n )
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   GLuint i, a, b;
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   for (i = 0; i < n; i++) {
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      b = (GLuint) p[i];        /* words are often faster than bytes */
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      a = ((b & 0x01) << 7) |
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	  ((b & 0x02) << 5) |
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	  ((b & 0x04) << 3) |
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	  ((b & 0x08) << 1) |
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)	  ((b & 0x10) >> 1) |
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	  ((b & 0x20) >> 3) |
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	  ((b & 0x40) >> 5) |
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)	  ((b & 0x80) >> 7);
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      p[i] = (GLubyte) a;
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   }
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/*
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Unpack a 32x32 pixel polygon stipple from user memory using the
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * current pixel unpack settings.
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              const struct gl_pixelstore_attrib *unpacking )
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap(32, 32, pattern, unpacking);
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   if (ptrn) {
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      /* Convert pattern from GLubytes to GLuints and handle big/little
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       * endian differences
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       */
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      GLubyte *p = ptrn;
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      GLint i;
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      for (i = 0; i < 32; i++) {
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         dest[i] = (p[0] << 24)
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 | (p[1] << 16)
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 | (p[2] <<  8)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 | (p[3]      );
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         p += 4;
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      free(ptrn);
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   }
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/*
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Pack polygon stipple into user memory given current pixel packing
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * settings.
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const struct gl_pixelstore_attrib *packing )
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   /* Convert pattern from GLuints to GLubytes to handle big/little
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    * endian differences.
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    */
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   GLubyte ptrn[32*4];
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   GLint i;
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   for (i = 0; i < 32; i++) {
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   }
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/*
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * order with row alignment = 1 byte.
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) */
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GLvoid *
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)_mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     const struct gl_pixelstore_attrib *packing )
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
152f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)   GLint bytes, row, width_in_bytes;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   GLubyte *buffer, *dst;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!pixels)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* Alloc dest storage */
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   bytes = ((width + 7) / 8 * height);
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   buffer = (GLubyte *) malloc( bytes );
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!buffer)
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return NULL;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   width_in_bytes = CEILING( width, 8 );
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   dst = buffer;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   for (row = 0; row < height; row++) {
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const GLubyte *src = (const GLubyte *)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         _mesa_image_address2d(packing, pixels, width, height,
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GL_COLOR_INDEX, GL_BITMAP, row, 0);
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!src) {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         free(buffer);
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return NULL;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if ((packing->SkipPixels & 7) == 0) {
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         memcpy( dst, src, width_in_bytes );
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         if (packing->LsbFirst) {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            flip_bytes( dst, width_in_bytes );
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else {
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         /* handling SkipPixels is a bit tricky (no pun intended!) */
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         GLint i;
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         if (packing->LsbFirst) {
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            GLubyte dstMask = 128;
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            const GLubyte *s = src;
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            GLubyte *d = dst;
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            *d = 0;
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            for (i = 0; i < width; i++) {
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (*s & srcMask) {
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  *d |= dstMask;
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (srcMask == 128) {
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  srcMask = 1;
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  s++;
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               else {
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  srcMask = srcMask << 1;
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (dstMask == 1) {
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dstMask = 128;
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  d++;
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  *d = 0;
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               else {
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dstMask = dstMask >> 1;
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            }
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         }
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         else {
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            GLubyte dstMask = 128;
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            const GLubyte *s = src;
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            GLubyte *d = dst;
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            *d = 0;
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            for (i = 0; i < width; i++) {
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (*s & srcMask) {
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  *d |= dstMask;
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (srcMask == 1) {
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  srcMask = 128;
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  s++;
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               else {
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  srcMask = srcMask >> 1;
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (dstMask == 1) {
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dstMask = 128;
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  d++;
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  *d = 0;
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               else {
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dstMask = dstMask >> 1;
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            }
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
239b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      dst += width_in_bytes;
240b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)   }
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   return buffer;
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Pack bitmap data.
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   GLint row, width_in_bytes;
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   const GLubyte *src;
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (!source)
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   width_in_bytes = CEILING( width, 8 );
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   src = source;
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   for (row = 0; row < height; row++) {
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!dst)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return;
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if ((packing->SkipPixels & 7) == 0) {
268a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)         memcpy( dst, src, width_in_bytes );
269a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)         if (packing->LsbFirst) {
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            flip_bytes( dst, width_in_bytes );
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else {
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         /* handling SkipPixels is a bit tricky (no pun intended!) */
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         GLint i;
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         if (packing->LsbFirst) {
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            GLubyte srcMask = 128;
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            const GLubyte *s = src;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            GLubyte *d = dst;
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            *d = 0;
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            for (i = 0; i < width; i++) {
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (*s & srcMask) {
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  *d |= dstMask;
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               }
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               if (srcMask == 1) {
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  srcMask = 128;
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  s++;
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               else {
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  srcMask = srcMask >> 1;
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               }
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (dstMask == 128) {
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  dstMask = 1;
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  d++;
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  *d = 0;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               }
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               else {
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  dstMask = dstMask << 1;
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)               }
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         else {
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            GLubyte srcMask = 128;
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            const GLubyte *s = src;
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            GLubyte *d = dst;
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            *d = 0;
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            for (i = 0; i < width; i++) {
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               if (*s & srcMask) {
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  *d |= dstMask;
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (srcMask == 1) {
314a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                  srcMask = 128;
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  s++;
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               else {
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  srcMask = srcMask >> 1;
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               }
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               if (dstMask == 1) {
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  dstMask = 128;
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  d++;
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  *d = 0;
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               }
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               else {
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  dstMask = dstMask >> 1;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               }
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      src += width_in_bytes;
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Get indexes of color components for a basic color format, such as
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * GL_RGBA, GL_RED, GL_LUMINANCE_ALPHA, etc.  Return -1 for indexes
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * that do not apply.
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)get_component_indexes(GLenum format,
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      GLint *redIndex,
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      GLint *greenIndex,
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      GLint *blueIndex,
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      GLint *alphaIndex,
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      GLint *luminanceIndex,
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      GLint *intensityIndex)
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   *redIndex = -1;
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   *greenIndex = -1;
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   *blueIndex = -1;
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   *alphaIndex = -1;
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   *luminanceIndex = -1;
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   *intensityIndex = -1;
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   switch (format) {
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_LUMINANCE:
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_LUMINANCE_INTEGER_EXT:
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *luminanceIndex = 0;
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_LUMINANCE_ALPHA:
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *luminanceIndex = 0;
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *alphaIndex = 1;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_INTENSITY:
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *intensityIndex = 0;
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_RED:
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_RED_INTEGER_EXT:
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *redIndex = 0;
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_GREEN:
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_GREEN_INTEGER_EXT:
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *greenIndex = 0;
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_BLUE:
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_BLUE_INTEGER_EXT:
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *blueIndex = 0;
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_ALPHA:
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_ALPHA_INTEGER_EXT:
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *alphaIndex = 0;
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_RG:
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_RG_INTEGER:
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *redIndex = 0;
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *greenIndex = 1;
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_RGB:
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_RGB_INTEGER_EXT:
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *redIndex = 0;
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *greenIndex = 1;
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *blueIndex = 2;
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_BGR:
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_BGR_INTEGER_EXT:
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *blueIndex = 0;
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *greenIndex = 1;
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *redIndex = 2;
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_RGBA:
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_RGBA_INTEGER_EXT:
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *redIndex = 0;
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *greenIndex = 1;
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *blueIndex = 2;
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *alphaIndex = 3;
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_BGRA:
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_BGRA_INTEGER:
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *redIndex = 2;
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *greenIndex = 1;
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *blueIndex = 0;
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *alphaIndex = 3;
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_ABGR_EXT:
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *redIndex = 3;
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *greenIndex = 2;
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *blueIndex = 1;
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *alphaIndex = 0;
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_DU8DV8_ATI:
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_DUDV_ATI:
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *redIndex = 0;
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      *greenIndex = 1;
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   default:
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      assert(0 && "bad format in get_component_indexes()");
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * For small integer types, return the min and max possible values.
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Used for clamping floats to unscaled integer types.
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * \return GL_TRUE if type is handled, GL_FALSE otherwise.
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static GLboolean
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)get_type_min_max(GLenum type, GLfloat *min, GLfloat *max)
4422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
4432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   switch (type) {
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_BYTE:
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *min = -128.0;
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *max = 127.0;
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return GL_TRUE;
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_UNSIGNED_BYTE:
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *min = 0.0;
4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *max = 255.0;
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return GL_TRUE;
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_SHORT:
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *min = -32768.0;
4542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *max = 32767.0;
4552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return GL_TRUE;
4562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_UNSIGNED_SHORT:
4572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *min = 0.0;
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      *max = 65535.0;
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return GL_TRUE;
4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   default:
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return GL_FALSE;
4622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   }
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/* Customization of unsigned integer packing.
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define SRC_TYPE GLuint
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define DST_TYPE GLuint
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define SRC_CONVERT(x) (x)
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define FN_NAME pack_uint_from_uint_rgba
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "pack_tmp.h"
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#undef DST_TYPE
4742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#undef SRC_CONVERT
4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#undef FN_NAME
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DST_TYPE GLint
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SRC_CONVERT(x) MIN2(x, 0x7fffffff)
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FN_NAME pack_int_from_uint_rgba
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pack_tmp.h"
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FN_NAME
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DST_TYPE GLushort
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SRC_CONVERT(x) MIN2(x, 0xffff)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FN_NAME pack_ushort_from_uint_rgba
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pack_tmp.h"
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FN_NAME
4921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
4931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define DST_TYPE GLshort
4941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define SRC_CONVERT(x) CLAMP((int)x, -32768, 32767)
495cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define FN_NAME pack_short_from_uint_rgba
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pack_tmp.h"
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FN_NAME
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DST_TYPE GLubyte
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SRC_CONVERT(x) MIN2(x, 0xff)
503b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#define FN_NAME pack_ubyte_from_uint_rgba
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pack_tmp.h"
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
507cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#undef FN_NAME
5082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DST_TYPE GLbyte
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SRC_CONVERT(x) CLAMP((int)x, -128, 127)
511cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define FN_NAME pack_byte_from_uint_rgba
5122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "pack_tmp.h"
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FN_NAME
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_TYPE
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)_mesa_pack_rgba_span_from_uints(struct gl_context *ctx, GLuint n, GLuint rgba[][4],
5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                GLenum dstFormat, GLenum dstType,
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                GLvoid *dstAddr)
5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
5242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   switch(dstType) {
5252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_UNSIGNED_INT:
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_uint_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_INT:
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_int_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
5312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_UNSIGNED_SHORT:
5322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      pack_ushort_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
5332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
5342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   case GL_SHORT:
5352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      pack_short_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_UNSIGNED_BYTE:
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_ubyte_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_BYTE:
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_byte_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      break;
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   default:
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      _mesa_problem(ctx,
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         "Unsupported type (%s) for format (%s)",
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         _mesa_lookup_enum_by_nr(dstType),
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         _mesa_lookup_enum_by_nr(dstFormat));
5495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Customization of signed integer packing.
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SRC_TYPE GLint
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define DST_TYPE GLuint
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SRC_CONVERT(x) MAX2(x, 0)
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FN_NAME pack_uint_from_int_rgba
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pack_tmp.h"
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
5645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#undef FN_NAME
5655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
5665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define DST_TYPE GLushort
5675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define SRC_CONVERT(x) MAX2(x, 0)
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FN_NAME pack_ushort_from_int_rgba
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pack_tmp.h"
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FN_NAME
5735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DST_TYPE GLshort
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SRC_CONVERT(x) CLAMP(x, -0x8000, 0x7fff)
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FN_NAME pack_short_from_int_rgba
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pack_tmp.h"
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
580effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#undef FN_NAME
581effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
582effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define DST_TYPE GLubyte
583effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define SRC_CONVERT(x) MAX2(x, 0)
584effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define FN_NAME pack_ubyte_from_int_rgba
585effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "pack_tmp.h"
586effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#undef DST_TYPE
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FN_NAME
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DST_TYPE GLbyte
591effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define SRC_CONVERT(x) CLAMP(x, -0x80, 0x7f)
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define FN_NAME pack_byte_from_int_rgba
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pack_tmp.h"
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DST_TYPE
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_CONVERT
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef FN_NAME
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef SRC_TYPE
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)_mesa_pack_rgba_span_from_ints(struct gl_context *ctx, GLuint n, GLint rgba[][4],
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLenum dstFormat, GLenum dstType,
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               GLvoid *dstAddr)
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   switch(dstType) {
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_UNSIGNED_INT:
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_uint_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_INT:
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* No conversion necessary. */
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_uint_from_uint_rgba(ctx, dstAddr, dstFormat, rgba, n);
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_UNSIGNED_SHORT:
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_ushort_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_SHORT:
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_short_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_UNSIGNED_BYTE:
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_ubyte_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   case GL_BYTE:
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      pack_byte_from_int_rgba(ctx, dstAddr, dstFormat, rgba, n);
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   default:
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      _mesa_problem(ctx,
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         "Unsupported type (%s) for format (%s)",
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         _mesa_lookup_enum_by_nr(dstType),
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         _mesa_lookup_enum_by_nr(dstFormat));
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Used to pack an array [][4] of RGBA float colors as specified
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * by the dstFormat, dstType and dstPacking.  Used by glReadPixels.
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Historically, the RGBA values were in [0,1] and rescaled to fit
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * into GLubytes, etc.  But with new integer formats, the RGBA values
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * may have any value and we don't always rescale when converting to
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * integers.
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Note: the rgba values will be modified by this function when any pixel
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * transfer ops are enabled.
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)_mesa_pack_rgba_span_float(struct gl_context *ctx, GLuint n, GLfloat rgba[][4],
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           GLenum dstFormat, GLenum dstType,
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           GLvoid *dstAddr,
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           const struct gl_pixelstore_attrib *dstPacking,
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           GLbitfield transferOps)
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   GLfloat *luminance;
6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   const GLint comps = _mesa_components_in_format(dstFormat);
6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   const GLboolean intDstFormat = _mesa_is_enum_format_integer(dstFormat);
6582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   GLuint i;
6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   if (dstFormat == GL_LUMINANCE ||
6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       dstFormat == GL_LUMINANCE_ALPHA ||
6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       dstFormat == GL_LUMINANCE_INTEGER_EXT ||
6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       dstFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT) {
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      luminance = (GLfloat *) malloc(n * sizeof(GLfloat));
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!luminance) {
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         return;
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   else {
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      luminance = NULL;
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /* EXT_texture_integer specifies no transfer ops on integer
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * types in the resolved issues section. Just set them to 0
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * for integer surfaces.
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    */
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (intDstFormat)
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      transferOps = 0;
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (transferOps) {
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   /*
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * Component clamping (besides clamping to [0,1] in
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * _mesa_apply_rgba_transfer_ops()).
6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    */
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   if (intDstFormat) {
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* clamping to dest type's min/max values */
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      GLfloat min, max;
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (get_type_min_max(dstType, &min, &max)) {
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         for (i = 0; i < n; i++) {
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], min, max);
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], min, max);
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], min, max);
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], min, max);
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         }
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   else if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      /* compute luminance values */
7032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (transferOps & IMAGE_CLAMP_BIT) {
7042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         for (i = 0; i < n; i++) {
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            luminance[i] = CLAMP(sum, 0.0F, 1.0F);
7072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         }
7082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      else {
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         for (i = 0; i < n; i++) {
7112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
7122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         }
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   }
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   /*
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    * Pack/store the pixels.  Ugh!  Lots of cases!!!
7182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    */
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   switch (dstType) {
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case GL_UNSIGNED_BYTE:
7212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         {
7222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            GLubyte *dst = (GLubyte *) dstAddr;
7232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            switch (dstFormat) {
7242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               case GL_RED:
7252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  for (i=0;i<n;i++)
7262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
7272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  break;
7282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               case GL_GREEN:
7292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  for (i=0;i<n;i++)
7302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
7312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  break;
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               case GL_BLUE:
7332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  for (i=0;i<n;i++)
7342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
7352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  break;
7362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               case GL_ALPHA:
7372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  for (i=0;i<n;i++)
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  break;
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               case GL_LUMINANCE:
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  for (i=0;i<n;i++)
7422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
7432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  break;
7442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               case GL_LUMINANCE_ALPHA:
7452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  for (i=0;i<n;i++) {
7462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
7472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  }
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  break;
7502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               case GL_RG:
7512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  for (i=0;i<n;i++) {
7522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
7532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
7542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  }
7552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  break;
7562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               case GL_RGB:
7572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  for (i=0;i<n;i++) {
7582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
7592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
7602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
7612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  }
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  break;
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               case GL_RGBA:
7642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  for (i=0;i<n;i++) {
7652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
7662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
7672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
7682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
7692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                  }
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  break;
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               case GL_BGR:
7725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  for (i=0;i<n;i++) {
7735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
7745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
7755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
7765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  }
7775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  break;
7785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               case GL_BGRA:
7795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  for (i=0;i<n;i++) {
780                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
781                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
782                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
783                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
784                  }
785                  break;
786               case GL_ABGR_EXT:
787                  for (i=0;i<n;i++) {
788                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
789                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
790                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
791                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
792                  }
793                  break;
794               case GL_RED_INTEGER_EXT:
795                  for (i=0;i<n;i++) {
796                     dst[i] = (GLubyte) rgba[i][RCOMP];
797                  }
798                  break;
799               case GL_GREEN_INTEGER_EXT:
800                  for (i=0;i<n;i++) {
801                     dst[i] = (GLubyte) rgba[i][GCOMP];
802                  }
803                  break;
804               case GL_BLUE_INTEGER_EXT:
805                  for (i=0;i<n;i++) {
806                     dst[i] = (GLubyte) rgba[i][BCOMP];
807                  }
808                  break;
809               case GL_ALPHA_INTEGER_EXT:
810                  for (i=0;i<n;i++) {
811                     dst[i] = (GLubyte) rgba[i][ACOMP];
812                  }
813                  break;
814               case GL_RG_INTEGER:
815                  for (i=0;i<n;i++) {
816                     dst[i*2+0] = (GLubyte) rgba[i][RCOMP];
817                     dst[i*2+1] = (GLubyte) rgba[i][GCOMP];
818                  }
819                  break;
820               case GL_RGB_INTEGER_EXT:
821                  for (i=0;i<n;i++) {
822                     dst[i*3+0] = (GLubyte) rgba[i][RCOMP];
823                     dst[i*3+1] = (GLubyte) rgba[i][GCOMP];
824                     dst[i*3+2] = (GLubyte) rgba[i][BCOMP];
825                  }
826                  break;
827               case GL_RGBA_INTEGER_EXT:
828                  for (i=0;i<n;i++) {
829                     dst[i*4+0] = (GLubyte) rgba[i][RCOMP];
830                     dst[i*4+1] = (GLubyte) rgba[i][GCOMP];
831                     dst[i*4+2] = (GLubyte) rgba[i][BCOMP];
832                     dst[i*4+3] = (GLubyte) rgba[i][ACOMP];
833                  }
834                  break;
835               case GL_BGR_INTEGER_EXT:
836                  for (i=0;i<n;i++) {
837                     dst[i*3+0] = (GLubyte) rgba[i][BCOMP];
838                     dst[i*3+1] = (GLubyte) rgba[i][GCOMP];
839                     dst[i*3+2] = (GLubyte) rgba[i][RCOMP];
840                  }
841                  break;
842               case GL_BGRA_INTEGER_EXT:
843                  for (i=0;i<n;i++) {
844                     dst[i*4+0] = (GLubyte) rgba[i][BCOMP];
845                     dst[i*4+1] = (GLubyte) rgba[i][GCOMP];
846                     dst[i*4+2] = (GLubyte) rgba[i][RCOMP];
847                     dst[i*4+3] = (GLubyte) rgba[i][ACOMP];
848                  }
849                  break;
850               case GL_LUMINANCE_INTEGER_EXT:
851                  for (i=0;i<n;i++) {
852                     dst[i*2+0] = (GLubyte) (rgba[i][RCOMP] +
853                                             rgba[i][GCOMP] +
854                                             rgba[i][BCOMP]);
855                     dst[i*2+1] = (GLubyte) rgba[i][ACOMP];
856                  }
857                  break;
858               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
859                  for (i=0;i<n;i++) {
860                     dst[i] = (GLubyte) (rgba[i][RCOMP] +
861                                         rgba[i][GCOMP] +
862                                         rgba[i][BCOMP]);
863                  }
864                  break;
865               case GL_DUDV_ATI:
866               case GL_DU8DV8_ATI:
867                  for (i=0;i<n;i++) {
868                     dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
869                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
870                  }
871                  break;
872               default:
873                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
874            }
875         }
876         break;
877      case GL_BYTE:
878         {
879            GLbyte *dst = (GLbyte *) dstAddr;
880            switch (dstFormat) {
881               case GL_RED:
882                  for (i=0;i<n;i++)
883                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
884                  break;
885               case GL_GREEN:
886                  for (i=0;i<n;i++)
887                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
888                  break;
889               case GL_BLUE:
890                  for (i=0;i<n;i++)
891                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
892                  break;
893               case GL_ALPHA:
894                  for (i=0;i<n;i++)
895                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
896                  break;
897               case GL_LUMINANCE:
898                  for (i=0;i<n;i++)
899                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
900                  break;
901               case GL_LUMINANCE_ALPHA:
902                  for (i=0;i<n;i++) {
903                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
904                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
905                  }
906                  break;
907               case GL_RG:
908                  for (i=0;i<n;i++) {
909                     dst[i*2+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
910                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
911                  }
912                  break;
913               case GL_RGB:
914                  for (i=0;i<n;i++) {
915                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
916                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
917                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
918                  }
919                  break;
920               case GL_RGBA:
921                  for (i=0;i<n;i++) {
922                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
923                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
924                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
925                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
926                  }
927                  break;
928               case GL_BGR:
929                  for (i=0;i<n;i++) {
930                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
931                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
932                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
933                  }
934                  break;
935               case GL_BGRA:
936                  for (i=0;i<n;i++) {
937                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
938                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
939                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
940                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
941                  }
942		  break;
943               case GL_ABGR_EXT:
944                  for (i=0;i<n;i++) {
945                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
946                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
947                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
948                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
949                  }
950                  break;
951               case GL_RED_INTEGER_EXT:
952                  for (i=0;i<n;i++) {
953                     dst[i] = (GLbyte) rgba[i][RCOMP];
954                  }
955                  break;
956               case GL_GREEN_INTEGER_EXT:
957                  for (i=0;i<n;i++) {
958                     dst[i] = (GLbyte) rgba[i][GCOMP];
959                  }
960                  break;
961               case GL_BLUE_INTEGER_EXT:
962                  for (i=0;i<n;i++) {
963                     dst[i] = (GLbyte) rgba[i][BCOMP];
964                  }
965                  break;
966               case GL_ALPHA_INTEGER_EXT:
967                  for (i=0;i<n;i++) {
968                     dst[i] = (GLbyte) rgba[i][ACOMP];
969                  }
970                  break;
971               case GL_RG_INTEGER:
972                  for (i=0;i<n;i++) {
973                     dst[i*2+0] = (GLbyte) rgba[i][RCOMP];
974                     dst[i*2+1] = (GLbyte) rgba[i][GCOMP];
975                  }
976                  break;
977               case GL_RGB_INTEGER_EXT:
978                  for (i=0;i<n;i++) {
979                     dst[i*3+0] = (GLbyte) rgba[i][RCOMP];
980                     dst[i*3+1] = (GLbyte) rgba[i][GCOMP];
981                     dst[i*3+2] = (GLbyte) rgba[i][BCOMP];
982                  }
983                  break;
984               case GL_RGBA_INTEGER_EXT:
985                  for (i=0;i<n;i++) {
986                     dst[i*4+0] = (GLbyte) rgba[i][RCOMP];
987                     dst[i*4+1] = (GLbyte) rgba[i][GCOMP];
988                     dst[i*4+2] = (GLbyte) rgba[i][BCOMP];
989                     dst[i*4+3] = (GLbyte) rgba[i][ACOMP];
990                  }
991                  break;
992               case GL_BGR_INTEGER_EXT:
993                  for (i=0;i<n;i++) {
994                     dst[i*3+0] = (GLbyte) rgba[i][BCOMP];
995                     dst[i*3+1] = (GLbyte) rgba[i][GCOMP];
996                     dst[i*3+2] = (GLbyte) rgba[i][RCOMP];
997                  }
998                  break;
999               case GL_BGRA_INTEGER_EXT:
1000                  for (i=0;i<n;i++) {
1001                     dst[i*4+0] = (GLbyte) rgba[i][BCOMP];
1002                     dst[i*4+1] = (GLbyte) rgba[i][GCOMP];
1003                     dst[i*4+2] = (GLbyte) rgba[i][RCOMP];
1004                     dst[i*4+3] = (GLbyte) rgba[i][ACOMP];
1005                  }
1006                  break;
1007               case GL_LUMINANCE_INTEGER_EXT:
1008                  for (i=0;i<n;i++) {
1009                     dst[i*2+0] = (GLbyte) (rgba[i][RCOMP] +
1010                                            rgba[i][GCOMP] +
1011                                            rgba[i][BCOMP]);
1012                     dst[i*2+1] = (GLbyte) rgba[i][ACOMP];
1013                  }
1014                  break;
1015               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1016                  for (i=0;i<n;i++) {
1017                     dst[i] = (GLbyte) (rgba[i][RCOMP] +
1018                                        rgba[i][GCOMP] +
1019                                        rgba[i][BCOMP]);
1020                  }
1021                  break;
1022               case GL_DUDV_ATI:
1023               case GL_DU8DV8_ATI:
1024                  for (i=0;i<n;i++) {
1025                     dst[i*2+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1026                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1027                  }
1028                  break;
1029               default:
1030                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1031            }
1032         }
1033         break;
1034      case GL_UNSIGNED_SHORT:
1035         {
1036            GLushort *dst = (GLushort *) dstAddr;
1037            switch (dstFormat) {
1038               case GL_RED:
1039                  for (i=0;i<n;i++)
1040                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
1041                  break;
1042               case GL_GREEN:
1043                  for (i=0;i<n;i++)
1044                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
1045                  break;
1046               case GL_BLUE:
1047                  for (i=0;i<n;i++)
1048                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
1049                  break;
1050               case GL_ALPHA:
1051                  for (i=0;i<n;i++)
1052                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
1053                  break;
1054               case GL_LUMINANCE:
1055                  for (i=0;i<n;i++)
1056                     UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
1057                  break;
1058               case GL_LUMINANCE_ALPHA:
1059                  for (i=0;i<n;i++) {
1060                     UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
1061                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
1062                  }
1063                  break;
1064               case GL_RG:
1065                  for (i=0;i<n;i++) {
1066                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+0], rgba[i][RCOMP]);
1067                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][GCOMP]);
1068                  }
1069                  break;
1070               case GL_RGB:
1071                  for (i=0;i<n;i++) {
1072                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
1073                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1074                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
1075                  }
1076                  break;
1077               case GL_RGBA:
1078                  for (i=0;i<n;i++) {
1079                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
1080                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1081                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
1082                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1083                  }
1084                  break;
1085               case GL_BGR:
1086                  for (i=0;i<n;i++) {
1087                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
1088                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1089                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
1090                  }
1091                  break;
1092               case GL_BGRA:
1093                  for (i=0;i<n;i++) {
1094                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
1095                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1096                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
1097                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1098                  }
1099                  break;
1100               case GL_ABGR_EXT:
1101                  for (i=0;i<n;i++) {
1102                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
1103                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
1104                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
1105                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
1106                  }
1107                  break;
1108               case GL_RED_INTEGER_EXT:
1109                  for (i=0;i<n;i++) {
1110                     dst[i] = (GLushort) rgba[i][RCOMP];
1111                  }
1112                  break;
1113               case GL_GREEN_INTEGER_EXT:
1114                  for (i=0;i<n;i++) {
1115                     dst[i] = (GLushort) rgba[i][GCOMP];
1116                  }
1117                  break;
1118               case GL_BLUE_INTEGER_EXT:
1119                  for (i=0;i<n;i++) {
1120                     dst[i] = (GLushort) rgba[i][BCOMP];
1121                  }
1122                  break;
1123               case GL_ALPHA_INTEGER_EXT:
1124                  for (i=0;i<n;i++) {
1125                     dst[i] = (GLushort) rgba[i][ACOMP];
1126                  }
1127                  break;
1128               case GL_RG_INTEGER:
1129                  for (i=0;i<n;i++) {
1130                     dst[i*2+0] = (GLushort) rgba[i][RCOMP];
1131                     dst[i*2+1] = (GLushort) rgba[i][GCOMP];
1132                  }
1133                  break;
1134               case GL_RGB_INTEGER_EXT:
1135                  for (i=0;i<n;i++) {
1136                     dst[i*3+0] = (GLushort) rgba[i][RCOMP];
1137                     dst[i*3+1] = (GLushort) rgba[i][GCOMP];
1138                     dst[i*3+2] = (GLushort) rgba[i][BCOMP];
1139                  }
1140                  break;
1141               case GL_RGBA_INTEGER_EXT:
1142                  for (i=0;i<n;i++) {
1143                     dst[i*4+0] = (GLushort) rgba[i][RCOMP];
1144                     dst[i*4+1] = (GLushort) rgba[i][GCOMP];
1145                     dst[i*4+2] = (GLushort) rgba[i][BCOMP];
1146                     dst[i*4+3] = (GLushort) rgba[i][ACOMP];
1147                  }
1148                  break;
1149               case GL_BGR_INTEGER_EXT:
1150                  for (i=0;i<n;i++) {
1151                     dst[i*3+0] = (GLushort) rgba[i][BCOMP];
1152                     dst[i*3+1] = (GLushort) rgba[i][GCOMP];
1153                     dst[i*3+2] = (GLushort) rgba[i][RCOMP];
1154                  }
1155                  break;
1156               case GL_BGRA_INTEGER_EXT:
1157                  for (i=0;i<n;i++) {
1158                     dst[i*4+0] = (GLushort) rgba[i][BCOMP];
1159                     dst[i*4+1] = (GLushort) rgba[i][GCOMP];
1160                     dst[i*4+2] = (GLushort) rgba[i][RCOMP];
1161                     dst[i*4+3] = (GLushort) rgba[i][ACOMP];
1162                  }
1163                  break;
1164               case GL_LUMINANCE_INTEGER_EXT:
1165                  for (i=0;i<n;i++) {
1166                     dst[i*2+0] = (GLushort) (rgba[i][RCOMP] +
1167                                              rgba[i][GCOMP] +
1168                                              rgba[i][BCOMP]);
1169                     dst[i*2+1] = (GLushort) rgba[i][ACOMP];
1170                  }
1171                  break;
1172               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1173                  for (i=0;i<n;i++) {
1174                     dst[i] = (GLushort) (rgba[i][RCOMP] +
1175                                          rgba[i][GCOMP] +
1176                                          rgba[i][BCOMP]);
1177                  }
1178                  break;
1179               case GL_DUDV_ATI:
1180               case GL_DU8DV8_ATI:
1181                  for (i=0;i<n;i++) {
1182                     dst[i*2+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1183                     dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1184                  }
1185                  break;
1186               default:
1187                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1188            }
1189         }
1190         break;
1191      case GL_SHORT:
1192         {
1193            GLshort *dst = (GLshort *) dstAddr;
1194            switch (dstFormat) {
1195               case GL_RED:
1196                  for (i=0;i<n;i++)
1197                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1198                  break;
1199               case GL_GREEN:
1200                  for (i=0;i<n;i++)
1201                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1202                  break;
1203               case GL_BLUE:
1204                  for (i=0;i<n;i++)
1205                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1206                  break;
1207               case GL_ALPHA:
1208                  for (i=0;i<n;i++)
1209                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1210                  break;
1211               case GL_LUMINANCE:
1212                  for (i=0;i<n;i++)
1213                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
1214                  break;
1215               case GL_LUMINANCE_ALPHA:
1216                  for (i=0;i<n;i++) {
1217                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1218                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1219                  }
1220                  break;
1221               case GL_RG:
1222                  for (i=0;i<n;i++) {
1223                     dst[i*2+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1224                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1225                  }
1226                  break;
1227               case GL_RGB:
1228                  for (i=0;i<n;i++) {
1229                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1230                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1231                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1232                  }
1233                  break;
1234               case GL_RGBA:
1235                  for (i=0;i<n;i++) {
1236                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1237                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1238                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1239                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1240                  }
1241                  break;
1242               case GL_BGR:
1243                  for (i=0;i<n;i++) {
1244                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1245                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1246                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1247                  }
1248                  break;
1249               case GL_BGRA:
1250                  for (i=0;i<n;i++) {
1251                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1252                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1253                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1254                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1255                  }
1256		  break;
1257               case GL_ABGR_EXT:
1258                  for (i=0;i<n;i++) {
1259                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1260                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1261                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1262                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1263                  }
1264                  break;
1265               case GL_RED_INTEGER_EXT:
1266                  for (i=0;i<n;i++) {
1267                     dst[i] = (GLshort) rgba[i][RCOMP];
1268                  }
1269                  break;
1270               case GL_GREEN_INTEGER_EXT:
1271                  for (i=0;i<n;i++) {
1272                     dst[i] = (GLshort) rgba[i][GCOMP];
1273                  }
1274                  break;
1275               case GL_BLUE_INTEGER_EXT:
1276                  for (i=0;i<n;i++) {
1277                     dst[i] = (GLshort) rgba[i][BCOMP];
1278                  }
1279                  break;
1280               case GL_ALPHA_INTEGER_EXT:
1281                  for (i=0;i<n;i++) {
1282                     dst[i] = (GLshort) rgba[i][ACOMP];
1283                  }
1284                  break;
1285               case GL_RG_INTEGER:
1286                  for (i=0;i<n;i++) {
1287                     dst[i*2+0] = (GLshort) rgba[i][RCOMP];
1288                     dst[i*2+1] = (GLshort) rgba[i][GCOMP];
1289                  }
1290                  break;
1291               case GL_RGB_INTEGER_EXT:
1292                  for (i=0;i<n;i++) {
1293                     dst[i*3+0] = (GLshort) rgba[i][RCOMP];
1294                     dst[i*3+1] = (GLshort) rgba[i][GCOMP];
1295                     dst[i*3+2] = (GLshort) rgba[i][BCOMP];
1296                  }
1297                  break;
1298               case GL_RGBA_INTEGER_EXT:
1299                  for (i=0;i<n;i++) {
1300                     dst[i*4+0] = (GLshort) rgba[i][RCOMP];
1301                     dst[i*4+1] = (GLshort) rgba[i][GCOMP];
1302                     dst[i*4+2] = (GLshort) rgba[i][BCOMP];
1303                     dst[i*4+3] = (GLshort) rgba[i][ACOMP];
1304                  }
1305                  break;
1306               case GL_BGR_INTEGER_EXT:
1307                  for (i=0;i<n;i++) {
1308                     dst[i*3+0] = (GLshort) rgba[i][BCOMP];
1309                     dst[i*3+1] = (GLshort) rgba[i][GCOMP];
1310                     dst[i*3+2] = (GLshort) rgba[i][RCOMP];
1311                  }
1312                  break;
1313               case GL_BGRA_INTEGER_EXT:
1314                  for (i=0;i<n;i++) {
1315                     dst[i*4+0] = (GLshort) rgba[i][BCOMP];
1316                     dst[i*4+1] = (GLshort) rgba[i][GCOMP];
1317                     dst[i*4+2] = (GLshort) rgba[i][RCOMP];
1318                     dst[i*4+3] = (GLshort) rgba[i][ACOMP];
1319                  }
1320                  break;
1321               case GL_LUMINANCE_INTEGER_EXT:
1322                  for (i=0;i<n;i++) {
1323                     dst[i*2+0] = (GLshort) (rgba[i][RCOMP] +
1324                                             rgba[i][GCOMP] +
1325                                             rgba[i][BCOMP]);
1326                     dst[i*2+1] = (GLshort) rgba[i][ACOMP];
1327                  }
1328                  break;
1329               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1330                  for (i=0;i<n;i++) {
1331                     dst[i] = (GLshort) (rgba[i][RCOMP] +
1332                                         rgba[i][GCOMP] +
1333                                         rgba[i][BCOMP]);
1334                  }
1335                  break;
1336               case GL_DUDV_ATI:
1337               case GL_DU8DV8_ATI:
1338                  for (i=0;i<n;i++) {
1339                     dst[i*2+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1340                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1341                  }
1342                  break;
1343               default:
1344                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1345            }
1346         }
1347         break;
1348      case GL_UNSIGNED_INT:
1349         {
1350            GLuint *dst = (GLuint *) dstAddr;
1351            switch (dstFormat) {
1352               case GL_RED:
1353                  for (i=0;i<n;i++)
1354                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1355                  break;
1356               case GL_GREEN:
1357                  for (i=0;i<n;i++)
1358                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1359                  break;
1360               case GL_BLUE:
1361                  for (i=0;i<n;i++)
1362                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1363                  break;
1364               case GL_ALPHA:
1365                  for (i=0;i<n;i++)
1366                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1367                  break;
1368               case GL_LUMINANCE:
1369                  for (i=0;i<n;i++)
1370                     dst[i] = FLOAT_TO_UINT(luminance[i]);
1371                  break;
1372               case GL_LUMINANCE_ALPHA:
1373                  for (i=0;i<n;i++) {
1374                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
1375                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1376                  }
1377                  break;
1378               case GL_RG:
1379                  for (i=0;i<n;i++) {
1380                     dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1381                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1382                  }
1383                  break;
1384               case GL_RGB:
1385                  for (i=0;i<n;i++) {
1386                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1387                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1388                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1389                  }
1390                  break;
1391               case GL_RGBA:
1392                  for (i=0;i<n;i++) {
1393                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1394                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1395                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1396                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1397                  }
1398                  break;
1399               case GL_BGR:
1400                  for (i=0;i<n;i++) {
1401                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1402                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1403                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1404                  }
1405                  break;
1406               case GL_BGRA:
1407                  for (i=0;i<n;i++) {
1408                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1409                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1410                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1411                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1412                  }
1413                  break;
1414               case GL_ABGR_EXT:
1415                  for (i=0;i<n;i++) {
1416                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1417                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1418                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1419                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1420                  }
1421                  break;
1422               case GL_RED_INTEGER_EXT:
1423                  for (i=0;i<n;i++) {
1424                     dst[i] = (GLuint) rgba[i][RCOMP];
1425                  }
1426                  break;
1427               case GL_GREEN_INTEGER_EXT:
1428                  for (i=0;i<n;i++) {
1429                     dst[i] = (GLuint) rgba[i][GCOMP];
1430                  }
1431                  break;
1432               case GL_BLUE_INTEGER_EXT:
1433                  for (i=0;i<n;i++) {
1434                     dst[i] = (GLuint) rgba[i][BCOMP];
1435                  }
1436                  break;
1437               case GL_ALPHA_INTEGER_EXT:
1438                  for (i=0;i<n;i++) {
1439                     dst[i] = (GLuint) rgba[i][ACOMP];
1440                  }
1441                  break;
1442               case GL_RG_INTEGER:
1443                  for (i=0;i<n;i++) {
1444                     dst[i*2+0] = (GLuint) rgba[i][RCOMP];
1445                     dst[i*2+1] = (GLuint) rgba[i][GCOMP];
1446                  }
1447                  break;
1448               case GL_RGB_INTEGER_EXT:
1449                  for (i=0;i<n;i++) {
1450                     dst[i*3+0] = (GLuint) rgba[i][RCOMP];
1451                     dst[i*3+1] = (GLuint) rgba[i][GCOMP];
1452                     dst[i*3+2] = (GLuint) rgba[i][BCOMP];
1453                  }
1454                  break;
1455               case GL_RGBA_INTEGER_EXT:
1456                  for (i=0;i<n;i++) {
1457                     dst[i*4+0] = (GLuint) rgba[i][RCOMP];
1458                     dst[i*4+1] = (GLuint) rgba[i][GCOMP];
1459                     dst[i*4+2] = (GLuint) rgba[i][BCOMP];
1460                     dst[i*4+3] = (GLuint) rgba[i][ACOMP];
1461                  }
1462                  break;
1463               case GL_BGR_INTEGER_EXT:
1464                  for (i=0;i<n;i++) {
1465                     dst[i*3+0] = (GLuint) rgba[i][BCOMP];
1466                     dst[i*3+1] = (GLuint) rgba[i][GCOMP];
1467                     dst[i*3+2] = (GLuint) rgba[i][RCOMP];
1468                  }
1469                  break;
1470               case GL_BGRA_INTEGER_EXT:
1471                  for (i=0;i<n;i++) {
1472                     dst[i*4+0] = (GLuint) rgba[i][BCOMP];
1473                     dst[i*4+1] = (GLuint) rgba[i][GCOMP];
1474                     dst[i*4+2] = (GLuint) rgba[i][RCOMP];
1475                     dst[i*4+3] = (GLuint) rgba[i][ACOMP];
1476                  }
1477                  break;
1478               case GL_LUMINANCE_INTEGER_EXT:
1479                  for (i=0;i<n;i++) {
1480                     dst[i*2+0] = (GLuint) (rgba[i][RCOMP] +
1481                                            rgba[i][GCOMP] +
1482                                            rgba[i][BCOMP]);
1483                     dst[i*2+1] = (GLuint) rgba[i][ACOMP];
1484                  }
1485                  break;
1486               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1487                  for (i=0;i<n;i++) {
1488                     dst[i] = (GLuint) (rgba[i][RCOMP] +
1489                                        rgba[i][GCOMP] +
1490                                        rgba[i][BCOMP]);
1491                  }
1492                  break;
1493               case GL_DUDV_ATI:
1494               case GL_DU8DV8_ATI:
1495                  for (i=0;i<n;i++) {
1496                     dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1497                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1498                  }
1499                  break;
1500               default:
1501                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1502            }
1503         }
1504         break;
1505      case GL_INT:
1506         {
1507            GLint *dst = (GLint *) dstAddr;
1508            switch (dstFormat) {
1509               case GL_RED:
1510                  for (i=0;i<n;i++)
1511                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
1512                  break;
1513               case GL_GREEN:
1514                  for (i=0;i<n;i++)
1515                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
1516                  break;
1517               case GL_BLUE:
1518                  for (i=0;i<n;i++)
1519                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
1520                  break;
1521               case GL_ALPHA:
1522                  for (i=0;i<n;i++)
1523                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
1524                  break;
1525               case GL_LUMINANCE:
1526                  for (i=0;i<n;i++)
1527                     dst[i] = FLOAT_TO_INT(luminance[i]);
1528                  break;
1529               case GL_LUMINANCE_ALPHA:
1530                  for (i=0;i<n;i++) {
1531                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
1532                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
1533                  }
1534                  break;
1535               case GL_RG:
1536                  for (i=0;i<n;i++) {
1537                     dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1538                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1539                  }
1540                  break;
1541               case GL_RGB:
1542                  for (i=0;i<n;i++) {
1543                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1544                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1545                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1546                  }
1547                  break;
1548               case GL_RGBA:
1549                  for (i=0;i<n;i++) {
1550                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1551                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1552                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1553                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1554                  }
1555                  break;
1556               case GL_BGR:
1557                  for (i=0;i<n;i++) {
1558                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1559                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1560                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1561                  }
1562                  break;
1563               case GL_BGRA:
1564                  for (i=0;i<n;i++) {
1565                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1566                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1567                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1568                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1569                  }
1570                  break;
1571               case GL_ABGR_EXT:
1572                  for (i=0;i<n;i++) {
1573                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
1574                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
1575                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
1576                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
1577                  }
1578                  break;
1579               case GL_DUDV_ATI:
1580               case GL_DU8DV8_ATI:
1581                  for (i=0;i<n;i++) {
1582                     dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1583                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1584                  }
1585                  break;
1586               case GL_RED_INTEGER_EXT:
1587                  for (i=0;i<n;i++) {
1588                     dst[i] = (GLint) rgba[i][RCOMP];
1589                  }
1590                  break;
1591               case GL_GREEN_INTEGER_EXT:
1592                  for (i=0;i<n;i++) {
1593                     dst[i] = (GLint) rgba[i][GCOMP];
1594                  }
1595                  break;
1596               case GL_BLUE_INTEGER_EXT:
1597                  for (i=0;i<n;i++) {
1598                     dst[i] = (GLint) rgba[i][BCOMP];
1599                  }
1600                  break;
1601               case GL_ALPHA_INTEGER_EXT:
1602                  for (i=0;i<n;i++) {
1603                     dst[i] = (GLint) rgba[i][ACOMP];
1604                  }
1605                  break;
1606               case GL_RG_INTEGER:
1607                  for (i=0;i<n;i++) {
1608                     dst[i*2+0] = (GLint) rgba[i][RCOMP];
1609                     dst[i*2+1] = (GLint) rgba[i][GCOMP];
1610                  }
1611                  break;
1612               case GL_RGB_INTEGER_EXT:
1613                  for (i=0;i<n;i++) {
1614                     dst[i*3+0] = (GLint) rgba[i][RCOMP];
1615                     dst[i*3+1] = (GLint) rgba[i][GCOMP];
1616                     dst[i*3+2] = (GLint) rgba[i][BCOMP];
1617                  }
1618                  break;
1619               case GL_RGBA_INTEGER_EXT:
1620                  for (i=0;i<n;i++) {
1621                     dst[i*4+0] = (GLint) rgba[i][RCOMP];
1622                     dst[i*4+1] = (GLint) rgba[i][GCOMP];
1623                     dst[i*4+2] = (GLint) rgba[i][BCOMP];
1624                     dst[i*4+3] = (GLint) rgba[i][ACOMP];
1625                  }
1626                  break;
1627               case GL_BGR_INTEGER_EXT:
1628                  for (i=0;i<n;i++) {
1629                     dst[i*3+0] = (GLint) rgba[i][BCOMP];
1630                     dst[i*3+1] = (GLint) rgba[i][GCOMP];
1631                     dst[i*3+2] = (GLint) rgba[i][RCOMP];
1632                  }
1633                  break;
1634               case GL_BGRA_INTEGER_EXT:
1635                  for (i=0;i<n;i++) {
1636                     dst[i*4+0] = (GLint) rgba[i][BCOMP];
1637                     dst[i*4+1] = (GLint) rgba[i][GCOMP];
1638                     dst[i*4+2] = (GLint) rgba[i][RCOMP];
1639                     dst[i*4+3] = (GLint) rgba[i][ACOMP];
1640                  }
1641                  break;
1642               case GL_LUMINANCE_INTEGER_EXT:
1643                  for (i=0;i<n;i++) {
1644                     dst[i*2+0] = (GLint) (rgba[i][RCOMP] +
1645                                           rgba[i][GCOMP] +
1646                                           rgba[i][BCOMP]);
1647                     dst[i*2+1] = (GLint) rgba[i][ACOMP];
1648                  }
1649                  break;
1650               case GL_LUMINANCE_ALPHA_INTEGER_EXT:
1651                  for (i=0;i<n;i++) {
1652                     dst[i] = (GLint) (rgba[i][RCOMP] +
1653                                       rgba[i][GCOMP] +
1654                                       rgba[i][BCOMP]);
1655                  }
1656                  break;
1657               default:
1658                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1659            }
1660         }
1661         break;
1662      case GL_FLOAT:
1663         {
1664            GLfloat *dst = (GLfloat *) dstAddr;
1665            switch (dstFormat) {
1666               case GL_RED:
1667                  for (i=0;i<n;i++)
1668                     dst[i] = rgba[i][RCOMP];
1669                  break;
1670               case GL_GREEN:
1671                  for (i=0;i<n;i++)
1672                     dst[i] = rgba[i][GCOMP];
1673                  break;
1674               case GL_BLUE:
1675                  for (i=0;i<n;i++)
1676                     dst[i] = rgba[i][BCOMP];
1677                  break;
1678               case GL_ALPHA:
1679                  for (i=0;i<n;i++)
1680                     dst[i] = rgba[i][ACOMP];
1681                  break;
1682               case GL_LUMINANCE:
1683                  for (i=0;i<n;i++)
1684                     dst[i] = luminance[i];
1685                  break;
1686               case GL_LUMINANCE_ALPHA:
1687                  for (i=0;i<n;i++) {
1688                     dst[i*2+0] = luminance[i];
1689                     dst[i*2+1] = rgba[i][ACOMP];
1690                  }
1691                  break;
1692               case GL_RG:
1693                  for (i=0;i<n;i++) {
1694                     dst[i*2+0] = rgba[i][RCOMP];
1695                     dst[i*2+1] = rgba[i][GCOMP];
1696                  }
1697                  break;
1698               case GL_RGB:
1699                  for (i=0;i<n;i++) {
1700                     dst[i*3+0] = rgba[i][RCOMP];
1701                     dst[i*3+1] = rgba[i][GCOMP];
1702                     dst[i*3+2] = rgba[i][BCOMP];
1703                  }
1704                  break;
1705               case GL_RGBA:
1706                  for (i=0;i<n;i++) {
1707                     dst[i*4+0] = rgba[i][RCOMP];
1708                     dst[i*4+1] = rgba[i][GCOMP];
1709                     dst[i*4+2] = rgba[i][BCOMP];
1710                     dst[i*4+3] = rgba[i][ACOMP];
1711                  }
1712                  break;
1713               case GL_BGR:
1714                  for (i=0;i<n;i++) {
1715                     dst[i*3+0] = rgba[i][BCOMP];
1716                     dst[i*3+1] = rgba[i][GCOMP];
1717                     dst[i*3+2] = rgba[i][RCOMP];
1718                  }
1719                  break;
1720               case GL_BGRA:
1721                  for (i=0;i<n;i++) {
1722                     dst[i*4+0] = rgba[i][BCOMP];
1723                     dst[i*4+1] = rgba[i][GCOMP];
1724                     dst[i*4+2] = rgba[i][RCOMP];
1725                     dst[i*4+3] = rgba[i][ACOMP];
1726                  }
1727                  break;
1728               case GL_ABGR_EXT:
1729                  for (i=0;i<n;i++) {
1730                     dst[i*4+0] = rgba[i][ACOMP];
1731                     dst[i*4+1] = rgba[i][BCOMP];
1732                     dst[i*4+2] = rgba[i][GCOMP];
1733                     dst[i*4+3] = rgba[i][RCOMP];
1734                  }
1735                  break;
1736               case GL_DUDV_ATI:
1737               case GL_DU8DV8_ATI:
1738                  for (i=0;i<n;i++) {
1739                     dst[i*2+0] = rgba[i][RCOMP];
1740                     dst[i*2+1] = rgba[i][GCOMP];
1741                  }
1742                  break;
1743               default:
1744                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1745            }
1746         }
1747         break;
1748      case GL_HALF_FLOAT_ARB:
1749         {
1750            GLhalfARB *dst = (GLhalfARB *) dstAddr;
1751            switch (dstFormat) {
1752               case GL_RED:
1753                  for (i=0;i<n;i++)
1754                     dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
1755                  break;
1756               case GL_GREEN:
1757                  for (i=0;i<n;i++)
1758                     dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
1759                  break;
1760               case GL_BLUE:
1761                  for (i=0;i<n;i++)
1762                     dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
1763                  break;
1764               case GL_ALPHA:
1765                  for (i=0;i<n;i++)
1766                     dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
1767                  break;
1768               case GL_LUMINANCE:
1769                  for (i=0;i<n;i++)
1770                     dst[i] = _mesa_float_to_half(luminance[i]);
1771                  break;
1772               case GL_LUMINANCE_ALPHA:
1773                  for (i=0;i<n;i++) {
1774                     dst[i*2+0] = _mesa_float_to_half(luminance[i]);
1775                     dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
1776                  }
1777                  break;
1778               case GL_RG:
1779                  for (i=0;i<n;i++) {
1780                     dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
1781                     dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1782                  }
1783                  break;
1784               case GL_RGB:
1785                  for (i=0;i<n;i++) {
1786                     dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
1787                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1788                     dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
1789                  }
1790                  break;
1791               case GL_RGBA:
1792                  for (i=0;i<n;i++) {
1793                     dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
1794                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1795                     dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
1796                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
1797                  }
1798                  break;
1799               case GL_BGR:
1800                  for (i=0;i<n;i++) {
1801                     dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
1802                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1803                     dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
1804                  }
1805                  break;
1806               case GL_BGRA:
1807                  for (i=0;i<n;i++) {
1808                     dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
1809                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1810                     dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
1811                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
1812                  }
1813                  break;
1814               case GL_ABGR_EXT:
1815                  for (i=0;i<n;i++) {
1816                     dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
1817                     dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
1818                     dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
1819                     dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
1820                  }
1821                  break;
1822               case GL_DUDV_ATI:
1823               case GL_DU8DV8_ATI:
1824                  for (i=0;i<n;i++) {
1825                     dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
1826                     dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1827                  }
1828                  break;
1829               default:
1830                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1831            }
1832         }
1833         break;
1834      case GL_UNSIGNED_BYTE_3_3_2:
1835         if (dstFormat == GL_RGB) {
1836            GLubyte *dst = (GLubyte *) dstAddr;
1837            for (i=0;i<n;i++) {
1838               dst[i] = (F_TO_I(rgba[i][RCOMP] * 7.0F) << 5)
1839                      | (F_TO_I(rgba[i][GCOMP] * 7.0F) << 2)
1840                      | (F_TO_I(rgba[i][BCOMP] * 3.0F)     );
1841            }
1842         }
1843         break;
1844      case GL_UNSIGNED_BYTE_2_3_3_REV:
1845         if (dstFormat == GL_RGB) {
1846            GLubyte *dst = (GLubyte *) dstAddr;
1847            for (i=0;i<n;i++) {
1848               dst[i] = (F_TO_I(rgba[i][RCOMP] * 7.0F)     )
1849                      | (F_TO_I(rgba[i][GCOMP] * 7.0F) << 3)
1850                      | (F_TO_I(rgba[i][BCOMP] * 3.0F) << 6);
1851            }
1852         }
1853         break;
1854      case GL_UNSIGNED_SHORT_5_6_5:
1855         if (dstFormat == GL_RGB) {
1856            GLushort *dst = (GLushort *) dstAddr;
1857            for (i=0;i<n;i++) {
1858               dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F) << 11)
1859                      | (F_TO_I(rgba[i][GCOMP] * 63.0F) <<  5)
1860                      | (F_TO_I(rgba[i][BCOMP] * 31.0F)      );
1861            }
1862         }
1863         break;
1864      case GL_UNSIGNED_SHORT_5_6_5_REV:
1865         if (dstFormat == GL_RGB) {
1866            GLushort *dst = (GLushort *) dstAddr;
1867            for (i=0;i<n;i++) {
1868               dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F)      )
1869                      | (F_TO_I(rgba[i][GCOMP] * 63.0F) <<  5)
1870                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) << 11);
1871            }
1872         }
1873         break;
1874      case GL_UNSIGNED_SHORT_4_4_4_4:
1875         if (dstFormat == GL_RGBA) {
1876            GLushort *dst = (GLushort *) dstAddr;
1877            for (i=0;i<n;i++) {
1878               dst[i] = (F_TO_I(rgba[i][RCOMP] * 15.0F) << 12)
1879                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
1880                      | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  4)
1881                      | (F_TO_I(rgba[i][ACOMP] * 15.0F)      );
1882            }
1883         }
1884         else if (dstFormat == GL_BGRA) {
1885            GLushort *dst = (GLushort *) dstAddr;
1886            for (i=0;i<n;i++) {
1887               dst[i] = (F_TO_I(rgba[i][BCOMP] * 15.0F) << 12)
1888                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
1889                      | (F_TO_I(rgba[i][RCOMP] * 15.0F) <<  4)
1890                      | (F_TO_I(rgba[i][ACOMP] * 15.0F)      );
1891            }
1892         }
1893         else if (dstFormat == GL_ABGR_EXT) {
1894            GLushort *dst = (GLushort *) dstAddr;
1895            for (i=0;i<n;i++) {
1896               dst[i] = (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12)
1897                      | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  8)
1898                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
1899                      | (F_TO_I(rgba[i][RCOMP] * 15.0F)      );
1900            }
1901         }
1902         break;
1903      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1904         if (dstFormat == GL_RGBA) {
1905            GLushort *dst = (GLushort *) dstAddr;
1906            for (i=0;i<n;i++) {
1907               dst[i] = (F_TO_I(rgba[i][RCOMP] * 15.0F)      )
1908                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
1909                      | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  8)
1910                      | (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12);
1911            }
1912         }
1913         else if (dstFormat == GL_BGRA) {
1914            GLushort *dst = (GLushort *) dstAddr;
1915            for (i=0;i<n;i++) {
1916               dst[i] = (F_TO_I(rgba[i][BCOMP] * 15.0F)      )
1917                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  4)
1918                      | (F_TO_I(rgba[i][RCOMP] * 15.0F) <<  8)
1919                      | (F_TO_I(rgba[i][ACOMP] * 15.0F) << 12);
1920            }
1921         }
1922         else if (dstFormat == GL_ABGR_EXT) {
1923            GLushort *dst = (GLushort *) dstAddr;
1924            for (i=0;i<n;i++) {
1925               dst[i] = (F_TO_I(rgba[i][ACOMP] * 15.0F)      )
1926                      | (F_TO_I(rgba[i][BCOMP] * 15.0F) <<  4)
1927                      | (F_TO_I(rgba[i][GCOMP] * 15.0F) <<  8)
1928                      | (F_TO_I(rgba[i][RCOMP] * 15.0F) << 12);
1929            }
1930         }
1931         break;
1932      case GL_UNSIGNED_SHORT_5_5_5_1:
1933         if (dstFormat == GL_RGBA) {
1934            GLushort *dst = (GLushort *) dstAddr;
1935            for (i=0;i<n;i++) {
1936               dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F) << 11)
1937                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  6)
1938                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  1)
1939                      | (F_TO_I(rgba[i][ACOMP] *  1.0F)      );
1940            }
1941         }
1942         else if (dstFormat == GL_BGRA) {
1943            GLushort *dst = (GLushort *) dstAddr;
1944            for (i=0;i<n;i++) {
1945               dst[i] = (F_TO_I(rgba[i][BCOMP] * 31.0F) << 11)
1946                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  6)
1947                      | (F_TO_I(rgba[i][RCOMP] * 31.0F) <<  1)
1948                      | (F_TO_I(rgba[i][ACOMP] *  1.0F)      );
1949            }
1950         }
1951         else if (dstFormat == GL_ABGR_EXT) {
1952            GLushort *dst = (GLushort *) dstAddr;
1953            for (i=0;i<n;i++) {
1954               dst[i] = (F_TO_I(rgba[i][ACOMP] * 31.0F) << 11)
1955                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  6)
1956                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  1)
1957                      | (F_TO_I(rgba[i][RCOMP] *  1.0F)      );
1958            }
1959         }
1960         break;
1961      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1962         if (dstFormat == GL_RGBA) {
1963            GLushort *dst = (GLushort *) dstAddr;
1964            for (i=0;i<n;i++) {
1965               dst[i] = (F_TO_I(rgba[i][RCOMP] * 31.0F)      )
1966                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  5)
1967                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) << 10)
1968                      | (F_TO_I(rgba[i][ACOMP] *  1.0F) << 15);
1969            }
1970         }
1971         else if (dstFormat == GL_BGRA) {
1972            GLushort *dst = (GLushort *) dstAddr;
1973            for (i=0;i<n;i++) {
1974               dst[i] = (F_TO_I(rgba[i][BCOMP] * 31.0F)      )
1975                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) <<  5)
1976                      | (F_TO_I(rgba[i][RCOMP] * 31.0F) << 10)
1977                      | (F_TO_I(rgba[i][ACOMP] *  1.0F) << 15);
1978            }
1979         }
1980         else if (dstFormat == GL_ABGR_EXT) {
1981            GLushort *dst = (GLushort *) dstAddr;
1982            for (i=0;i<n;i++) {
1983               dst[i] = (F_TO_I(rgba[i][ACOMP] * 31.0F)      )
1984                      | (F_TO_I(rgba[i][BCOMP] * 31.0F) <<  5)
1985                      | (F_TO_I(rgba[i][GCOMP] * 31.0F) << 10)
1986                      | (F_TO_I(rgba[i][RCOMP] *  1.0F) << 15);
1987            }
1988         }
1989         break;
1990      case GL_UNSIGNED_INT_8_8_8_8:
1991         if (dstFormat == GL_RGBA) {
1992            GLuint *dst = (GLuint *) dstAddr;
1993            for (i=0;i<n;i++) {
1994               dst[i] = (F_TO_I(rgba[i][RCOMP] * 255.F) << 24)
1995                      | (F_TO_I(rgba[i][GCOMP] * 255.F) << 16)
1996                      | (F_TO_I(rgba[i][BCOMP] * 255.F) <<  8)
1997                      | (F_TO_I(rgba[i][ACOMP] * 255.F)      );
1998            }
1999         }
2000         else if (dstFormat == GL_BGRA) {
2001            GLuint *dst = (GLuint *) dstAddr;
2002            for (i=0;i<n;i++) {
2003               dst[i] = (F_TO_I(rgba[i][BCOMP] * 255.F) << 24)
2004                      | (F_TO_I(rgba[i][GCOMP] * 255.F) << 16)
2005                      | (F_TO_I(rgba[i][RCOMP] * 255.F) <<  8)
2006                      | (F_TO_I(rgba[i][ACOMP] * 255.F)      );
2007            }
2008         }
2009         else if (dstFormat == GL_ABGR_EXT) {
2010            GLuint *dst = (GLuint *) dstAddr;
2011            for (i=0;i<n;i++) {
2012               dst[i] = (F_TO_I(rgba[i][ACOMP] * 255.F) << 24)
2013                      | (F_TO_I(rgba[i][BCOMP] * 255.F) << 16)
2014                      | (F_TO_I(rgba[i][GCOMP] * 255.F) <<  8)
2015                      | (F_TO_I(rgba[i][RCOMP] * 255.F)      );
2016            }
2017         }
2018         break;
2019      case GL_UNSIGNED_INT_8_8_8_8_REV:
2020         if (dstFormat == GL_RGBA) {
2021            GLuint *dst = (GLuint *) dstAddr;
2022            for (i=0;i<n;i++) {
2023               dst[i] = (F_TO_I(rgba[i][RCOMP] * 255.0F)      )
2024                      | (F_TO_I(rgba[i][GCOMP] * 255.0F) <<  8)
2025                      | (F_TO_I(rgba[i][BCOMP] * 255.0F) << 16)
2026                      | (F_TO_I(rgba[i][ACOMP] * 255.0F) << 24);
2027            }
2028         }
2029         else if (dstFormat == GL_BGRA) {
2030            GLuint *dst = (GLuint *) dstAddr;
2031            for (i=0;i<n;i++) {
2032               dst[i] = (F_TO_I(rgba[i][BCOMP] * 255.0F)      )
2033                      | (F_TO_I(rgba[i][GCOMP] * 255.0F) <<  8)
2034                      | (F_TO_I(rgba[i][RCOMP] * 255.0F) << 16)
2035                      | (F_TO_I(rgba[i][ACOMP] * 255.0F) << 24);
2036            }
2037         }
2038         else if (dstFormat == GL_ABGR_EXT) {
2039            GLuint *dst = (GLuint *) dstAddr;
2040            for (i=0;i<n;i++) {
2041               dst[i] = (F_TO_I(rgba[i][ACOMP] * 255.0F)      )
2042                      | (F_TO_I(rgba[i][BCOMP] * 255.0F) <<  8)
2043                      | (F_TO_I(rgba[i][GCOMP] * 255.0F) << 16)
2044                      | (F_TO_I(rgba[i][RCOMP] * 255.0F) << 24);
2045            }
2046         }
2047         break;
2048      case GL_UNSIGNED_INT_10_10_10_2:
2049         if (dstFormat == GL_RGBA) {
2050            GLuint *dst = (GLuint *) dstAddr;
2051            for (i=0;i<n;i++) {
2052               dst[i] = (F_TO_I(rgba[i][RCOMP] * 1023.0F) << 22)
2053                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 12)
2054                      | (F_TO_I(rgba[i][BCOMP] * 1023.0F) <<  2)
2055                      | (F_TO_I(rgba[i][ACOMP] *    3.0F)      );
2056            }
2057         }
2058         else if (dstFormat == GL_BGRA) {
2059            GLuint *dst = (GLuint *) dstAddr;
2060            for (i=0;i<n;i++) {
2061               dst[i] = (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 22)
2062                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 12)
2063                      | (F_TO_I(rgba[i][RCOMP] * 1023.0F) <<  2)
2064                      | (F_TO_I(rgba[i][ACOMP] *    3.0F)      );
2065            }
2066         }
2067         else if (dstFormat == GL_ABGR_EXT) {
2068            GLuint *dst = (GLuint *) dstAddr;
2069            for (i=0;i<n;i++) {
2070               dst[i] = (F_TO_I(rgba[i][ACOMP] * 1023.0F) << 22)
2071                      | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 12)
2072                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) <<  2)
2073                      | (F_TO_I(rgba[i][RCOMP] *    3.0F)      );
2074            }
2075         }
2076         break;
2077      case GL_UNSIGNED_INT_2_10_10_10_REV:
2078         if (dstFormat == GL_RGBA) {
2079            GLuint *dst = (GLuint *) dstAddr;
2080            for (i=0;i<n;i++) {
2081               dst[i] = (F_TO_I(rgba[i][RCOMP] * 1023.0F)      )
2082                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 10)
2083                      | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 20)
2084                      | (F_TO_I(rgba[i][ACOMP] *    3.0F) << 30);
2085            }
2086         }
2087         else if (dstFormat == GL_BGRA) {
2088            GLuint *dst = (GLuint *) dstAddr;
2089            for (i=0;i<n;i++) {
2090               dst[i] = (F_TO_I(rgba[i][BCOMP] * 1023.0F)      )
2091                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 10)
2092                      | (F_TO_I(rgba[i][RCOMP] * 1023.0F) << 20)
2093                      | (F_TO_I(rgba[i][ACOMP] *    3.0F) << 30);
2094            }
2095         }
2096         else if (dstFormat == GL_ABGR_EXT) {
2097            GLuint *dst = (GLuint *) dstAddr;
2098            for (i=0;i<n;i++) {
2099               dst[i] = (F_TO_I(rgba[i][ACOMP] * 1023.0F)      )
2100                      | (F_TO_I(rgba[i][BCOMP] * 1023.0F) << 10)
2101                      | (F_TO_I(rgba[i][GCOMP] * 1023.0F) << 20)
2102                      | (F_TO_I(rgba[i][RCOMP] *    3.0F) << 30);
2103            }
2104         }
2105         break;
2106      case GL_UNSIGNED_INT_5_9_9_9_REV:
2107         {
2108            GLuint *dst = (GLuint *) dstAddr;
2109            for (i = 0; i < n; i++) {
2110               dst[i] = float3_to_rgb9e5(rgba[i]);
2111            }
2112         }
2113         break;
2114      case GL_UNSIGNED_INT_10F_11F_11F_REV:
2115         {
2116            GLuint *dst = (GLuint *) dstAddr;
2117            for (i = 0; i < n; i++) {
2118               dst[i] = float3_to_r11g11b10f(rgba[i]);
2119            }
2120         }
2121         break;
2122      default:
2123         _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2124         free(luminance);
2125         return;
2126   }
2127
2128   if (dstPacking->SwapBytes) {
2129      GLint swapSize = _mesa_sizeof_packed_type(dstType);
2130      if (swapSize == 2) {
2131         _mesa_swap2((GLushort *) dstAddr, n * comps);
2132      }
2133      else if (swapSize == 4) {
2134         _mesa_swap4((GLuint *) dstAddr, n * comps);
2135      }
2136   }
2137
2138   free(luminance);
2139}
2140
2141
2142
2143#define SWAP2BYTE(VALUE)			\
2144   {						\
2145      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2146      GLubyte tmp = bytes[0];			\
2147      bytes[0] = bytes[1];			\
2148      bytes[1] = tmp;				\
2149   }
2150
2151#define SWAP4BYTE(VALUE)			\
2152   {						\
2153      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2154      GLubyte tmp = bytes[0];			\
2155      bytes[0] = bytes[3];			\
2156      bytes[3] = tmp;				\
2157      tmp = bytes[1];				\
2158      bytes[1] = bytes[2];			\
2159      bytes[2] = tmp;				\
2160   }
2161
2162
2163static void
2164extract_uint_indexes(GLuint n, GLuint indexes[],
2165                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
2166                     const struct gl_pixelstore_attrib *unpack )
2167{
2168   ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2169
2170   ASSERT(srcType == GL_BITMAP ||
2171          srcType == GL_UNSIGNED_BYTE ||
2172          srcType == GL_BYTE ||
2173          srcType == GL_UNSIGNED_SHORT ||
2174          srcType == GL_SHORT ||
2175          srcType == GL_UNSIGNED_INT ||
2176          srcType == GL_INT ||
2177          srcType == GL_UNSIGNED_INT_24_8_EXT ||
2178          srcType == GL_HALF_FLOAT_ARB ||
2179          srcType == GL_FLOAT ||
2180          srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
2181
2182   switch (srcType) {
2183      case GL_BITMAP:
2184         {
2185            GLubyte *ubsrc = (GLubyte *) src;
2186            if (unpack->LsbFirst) {
2187               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2188               GLuint i;
2189               for (i = 0; i < n; i++) {
2190                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2191                  if (mask == 128) {
2192                     mask = 1;
2193                     ubsrc++;
2194                  }
2195                  else {
2196                     mask = mask << 1;
2197                  }
2198               }
2199            }
2200            else {
2201               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2202               GLuint i;
2203               for (i = 0; i < n; i++) {
2204                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2205                  if (mask == 1) {
2206                     mask = 128;
2207                     ubsrc++;
2208                  }
2209                  else {
2210                     mask = mask >> 1;
2211                  }
2212               }
2213            }
2214         }
2215         break;
2216      case GL_UNSIGNED_BYTE:
2217         {
2218            GLuint i;
2219            const GLubyte *s = (const GLubyte *) src;
2220            for (i = 0; i < n; i++)
2221               indexes[i] = s[i];
2222         }
2223         break;
2224      case GL_BYTE:
2225         {
2226            GLuint i;
2227            const GLbyte *s = (const GLbyte *) src;
2228            for (i = 0; i < n; i++)
2229               indexes[i] = s[i];
2230         }
2231         break;
2232      case GL_UNSIGNED_SHORT:
2233         {
2234            GLuint i;
2235            const GLushort *s = (const GLushort *) src;
2236            if (unpack->SwapBytes) {
2237               for (i = 0; i < n; i++) {
2238                  GLushort value = s[i];
2239                  SWAP2BYTE(value);
2240                  indexes[i] = value;
2241               }
2242            }
2243            else {
2244               for (i = 0; i < n; i++)
2245                  indexes[i] = s[i];
2246            }
2247         }
2248         break;
2249      case GL_SHORT:
2250         {
2251            GLuint i;
2252            const GLshort *s = (const GLshort *) src;
2253            if (unpack->SwapBytes) {
2254               for (i = 0; i < n; i++) {
2255                  GLshort value = s[i];
2256                  SWAP2BYTE(value);
2257                  indexes[i] = value;
2258               }
2259            }
2260            else {
2261               for (i = 0; i < n; i++)
2262                  indexes[i] = s[i];
2263            }
2264         }
2265         break;
2266      case GL_UNSIGNED_INT:
2267         {
2268            GLuint i;
2269            const GLuint *s = (const GLuint *) src;
2270            if (unpack->SwapBytes) {
2271               for (i = 0; i < n; i++) {
2272                  GLuint value = s[i];
2273                  SWAP4BYTE(value);
2274                  indexes[i] = value;
2275               }
2276            }
2277            else {
2278               for (i = 0; i < n; i++)
2279                  indexes[i] = s[i];
2280            }
2281         }
2282         break;
2283      case GL_INT:
2284         {
2285            GLuint i;
2286            const GLint *s = (const GLint *) src;
2287            if (unpack->SwapBytes) {
2288               for (i = 0; i < n; i++) {
2289                  GLint value = s[i];
2290                  SWAP4BYTE(value);
2291                  indexes[i] = value;
2292               }
2293            }
2294            else {
2295               for (i = 0; i < n; i++)
2296                  indexes[i] = s[i];
2297            }
2298         }
2299         break;
2300      case GL_FLOAT:
2301         {
2302            GLuint i;
2303            const GLfloat *s = (const GLfloat *) src;
2304            if (unpack->SwapBytes) {
2305               for (i = 0; i < n; i++) {
2306                  GLfloat value = s[i];
2307                  SWAP4BYTE(value);
2308                  indexes[i] = (GLuint) value;
2309               }
2310            }
2311            else {
2312               for (i = 0; i < n; i++)
2313                  indexes[i] = (GLuint) s[i];
2314            }
2315         }
2316         break;
2317      case GL_HALF_FLOAT_ARB:
2318         {
2319            GLuint i;
2320            const GLhalfARB *s = (const GLhalfARB *) src;
2321            if (unpack->SwapBytes) {
2322               for (i = 0; i < n; i++) {
2323                  GLhalfARB value = s[i];
2324                  SWAP2BYTE(value);
2325                  indexes[i] = (GLuint) _mesa_half_to_float(value);
2326               }
2327            }
2328            else {
2329               for (i = 0; i < n; i++)
2330                  indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
2331            }
2332         }
2333         break;
2334      case GL_UNSIGNED_INT_24_8_EXT:
2335         {
2336            GLuint i;
2337            const GLuint *s = (const GLuint *) src;
2338            if (unpack->SwapBytes) {
2339               for (i = 0; i < n; i++) {
2340                  GLuint value = s[i];
2341                  SWAP4BYTE(value);
2342                  indexes[i] = value & 0xff;  /* lower 8 bits */
2343               }
2344            }
2345            else {
2346               for (i = 0; i < n; i++)
2347                  indexes[i] = s[i] & 0xff;  /* lower 8 bits */
2348            }
2349         }
2350         break;
2351      case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
2352         {
2353            GLuint i;
2354            const GLuint *s = (const GLuint *) src;
2355            if (unpack->SwapBytes) {
2356               for (i = 0; i < n; i++) {
2357                  GLuint value = s[i*2+1];
2358                  SWAP4BYTE(value);
2359                  indexes[i] = value & 0xff;  /* lower 8 bits */
2360               }
2361            }
2362            else {
2363               for (i = 0; i < n; i++)
2364                  indexes[i] = s[i*2+1] & 0xff;  /* lower 8 bits */
2365            }
2366         }
2367         break;
2368
2369      default:
2370         _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
2371         return;
2372   }
2373}
2374
2375
2376/**
2377 * Return source/dest RGBA indexes for unpacking pixels.
2378 */
2379static void
2380get_component_mapping(GLenum format,
2381                      GLint *rSrc,
2382                      GLint *gSrc,
2383                      GLint *bSrc,
2384                      GLint *aSrc,
2385                      GLint *rDst,
2386                      GLint *gDst,
2387                      GLint *bDst,
2388                      GLint *aDst)
2389{
2390   switch (format) {
2391   case GL_RED:
2392   case GL_RED_INTEGER_EXT:
2393      *rSrc = 0;
2394      *gSrc = *bSrc = *aSrc = -1;
2395      break;
2396   case GL_GREEN:
2397   case GL_GREEN_INTEGER_EXT:
2398      *gSrc = 0;
2399      *rSrc = *bSrc = *aSrc = -1;
2400      break;
2401   case GL_BLUE:
2402   case GL_BLUE_INTEGER_EXT:
2403      *bSrc = 0;
2404      *rSrc = *gSrc = *aSrc = -1;
2405      break;
2406   case GL_ALPHA:
2407   case GL_ALPHA_INTEGER_EXT:
2408      *rSrc = *gSrc = *bSrc = -1;
2409      *aSrc = 0;
2410      break;
2411   case GL_LUMINANCE:
2412   case GL_LUMINANCE_INTEGER_EXT:
2413      *rSrc = *gSrc = *bSrc = 0;
2414      *aSrc = -1;
2415      break;
2416   case GL_LUMINANCE_ALPHA:
2417   case GL_LUMINANCE_ALPHA_INTEGER_EXT:
2418      *rSrc = *gSrc = *bSrc = 0;
2419      *aSrc = 1;
2420      break;
2421   case GL_INTENSITY:
2422      *rSrc = *gSrc = *bSrc = *aSrc = 0;
2423      break;
2424   case GL_RG:
2425   case GL_RG_INTEGER:
2426      *rSrc = 0;
2427      *gSrc = 1;
2428      *bSrc = -1;
2429      *aSrc = -1;
2430      *rDst = 0;
2431      *gDst = 1;
2432      *bDst = 2;
2433      *aDst = 3;
2434      break;
2435   case GL_RGB:
2436   case GL_RGB_INTEGER:
2437      *rSrc = 0;
2438      *gSrc = 1;
2439      *bSrc = 2;
2440      *aSrc = -1;
2441      *rDst = 0;
2442      *gDst = 1;
2443      *bDst = 2;
2444      *aDst = 3;
2445      break;
2446   case GL_BGR:
2447   case GL_BGR_INTEGER:
2448      *rSrc = 2;
2449      *gSrc = 1;
2450      *bSrc = 0;
2451      *aSrc = -1;
2452      *rDst = 2;
2453      *gDst = 1;
2454      *bDst = 0;
2455      *aDst = 3;
2456      break;
2457   case GL_RGBA:
2458   case GL_RGBA_INTEGER:
2459      *rSrc = 0;
2460      *gSrc = 1;
2461      *bSrc = 2;
2462      *aSrc = 3;
2463      *rDst = 0;
2464      *gDst = 1;
2465      *bDst = 2;
2466      *aDst = 3;
2467      break;
2468   case GL_BGRA:
2469   case GL_BGRA_INTEGER:
2470      *rSrc = 2;
2471      *gSrc = 1;
2472      *bSrc = 0;
2473      *aSrc = 3;
2474      *rDst = 2;
2475      *gDst = 1;
2476      *bDst = 0;
2477      *aDst = 3;
2478      break;
2479   case GL_ABGR_EXT:
2480      *rSrc = 3;
2481      *gSrc = 2;
2482      *bSrc = 1;
2483      *aSrc = 0;
2484      *rDst = 3;
2485      *gDst = 2;
2486      *bDst = 1;
2487      *aDst = 0;
2488      break;
2489   case GL_DU8DV8_ATI:
2490   case GL_DUDV_ATI:
2491      *rSrc = 0;
2492      *gSrc = 1;
2493      *bSrc = -1;
2494      *aSrc = -1;
2495      break;
2496   default:
2497      _mesa_problem(NULL, "bad srcFormat %s in get_component_mapping",
2498                    _mesa_lookup_enum_by_nr(format));
2499      return;
2500   }
2501}
2502
2503
2504
2505/*
2506 * This function extracts floating point RGBA values from arbitrary
2507 * image data.  srcFormat and srcType are the format and type parameters
2508 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2509 *
2510 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2511 * implements the "Conversion to floating point", "Conversion to RGB",
2512 * and "Final Expansion to RGBA" operations.
2513 *
2514 * Args:  n - number of pixels
2515 *        rgba - output colors
2516 *        srcFormat - format of incoming data
2517 *        srcType - data type of incoming data
2518 *        src - source data pointer
2519 *        swapBytes - perform byteswapping of incoming data?
2520 */
2521static void
2522extract_float_rgba(GLuint n, GLfloat rgba[][4],
2523                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
2524                   GLboolean swapBytes)
2525{
2526   GLint rSrc, gSrc, bSrc, aSrc;
2527   GLint stride;
2528   GLint rDst, bDst, gDst, aDst;
2529   GLboolean intFormat;
2530   GLfloat rs = 1.0f, gs = 1.0f, bs = 1.0f, as = 1.0f; /* scale factors */
2531
2532   ASSERT(srcFormat == GL_RED ||
2533          srcFormat == GL_GREEN ||
2534          srcFormat == GL_BLUE ||
2535          srcFormat == GL_ALPHA ||
2536          srcFormat == GL_LUMINANCE ||
2537          srcFormat == GL_LUMINANCE_ALPHA ||
2538          srcFormat == GL_INTENSITY ||
2539          srcFormat == GL_RG ||
2540          srcFormat == GL_RGB ||
2541          srcFormat == GL_BGR ||
2542          srcFormat == GL_RGBA ||
2543          srcFormat == GL_BGRA ||
2544          srcFormat == GL_ABGR_EXT ||
2545          srcFormat == GL_DU8DV8_ATI ||
2546          srcFormat == GL_DUDV_ATI ||
2547          srcFormat == GL_RED_INTEGER_EXT ||
2548          srcFormat == GL_GREEN_INTEGER_EXT ||
2549          srcFormat == GL_BLUE_INTEGER_EXT ||
2550          srcFormat == GL_ALPHA_INTEGER_EXT ||
2551          srcFormat == GL_RG_INTEGER ||
2552          srcFormat == GL_RGB_INTEGER_EXT ||
2553          srcFormat == GL_RGBA_INTEGER_EXT ||
2554          srcFormat == GL_BGR_INTEGER_EXT ||
2555          srcFormat == GL_BGRA_INTEGER_EXT ||
2556          srcFormat == GL_LUMINANCE_INTEGER_EXT ||
2557          srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
2558
2559   ASSERT(srcType == GL_UNSIGNED_BYTE ||
2560          srcType == GL_BYTE ||
2561          srcType == GL_UNSIGNED_SHORT ||
2562          srcType == GL_SHORT ||
2563          srcType == GL_UNSIGNED_INT ||
2564          srcType == GL_INT ||
2565          srcType == GL_HALF_FLOAT_ARB ||
2566          srcType == GL_FLOAT ||
2567          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2568          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2569          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2570          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2571          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2572          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2573          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2574          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2575          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2576          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2577          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2578          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
2579          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
2580          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
2581
2582   get_component_mapping(srcFormat,
2583                         &rSrc, &gSrc, &bSrc, &aSrc,
2584                         &rDst, &gDst, &bDst, &aDst);
2585
2586   stride = _mesa_components_in_format(srcFormat);
2587
2588   intFormat = _mesa_is_enum_format_integer(srcFormat);
2589
2590#define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT_FLT, DEFAULT_INT, TYPE, CONVERSION) \
2591   if ((SRC_INDEX) < 0) {						\
2592      GLuint i;								\
2593      if (intFormat) {							\
2594         for (i = 0; i < n; i++) {					\
2595            rgba[i][DST_INDEX] = DEFAULT_INT;				\
2596         }								\
2597      }									\
2598      else {								\
2599         for (i = 0; i < n; i++) {					\
2600            rgba[i][DST_INDEX] = DEFAULT_FLT;				\
2601         }								\
2602      }									\
2603   }									\
2604   else if (swapBytes) {						\
2605      const TYPE *s = (const TYPE *) src;				\
2606      GLuint i;								\
2607      for (i = 0; i < n; i++) {						\
2608         TYPE value = s[SRC_INDEX];					\
2609         if (sizeof(TYPE) == 2) {					\
2610            SWAP2BYTE(value);						\
2611         }								\
2612         else if (sizeof(TYPE) == 4) {					\
2613            SWAP4BYTE(value);						\
2614         }								\
2615         if (intFormat)							\
2616            rgba[i][DST_INDEX] = (GLfloat) value;			\
2617         else								\
2618            rgba[i][DST_INDEX] = (GLfloat) CONVERSION(value);		\
2619         s += stride;							\
2620      }									\
2621   }									\
2622   else {								\
2623      const TYPE *s = (const TYPE *) src;				\
2624      GLuint i;								\
2625      if (intFormat) {							\
2626         for (i = 0; i < n; i++) {					\
2627            rgba[i][DST_INDEX] = (GLfloat) s[SRC_INDEX];		\
2628            s += stride;						\
2629         }								\
2630      }									\
2631      else {								\
2632         for (i = 0; i < n; i++) {					\
2633            rgba[i][DST_INDEX] = (GLfloat) CONVERSION(s[SRC_INDEX]);	\
2634            s += stride;						\
2635         }								\
2636      }									\
2637   }
2638
2639   switch (srcType) {
2640      case GL_UNSIGNED_BYTE:
2641         PROCESS(rSrc, RCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
2642         PROCESS(gSrc, GCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
2643         PROCESS(bSrc, BCOMP, 0.0F,   0, GLubyte, UBYTE_TO_FLOAT);
2644         PROCESS(aSrc, ACOMP, 1.0F, 255, GLubyte, UBYTE_TO_FLOAT);
2645         break;
2646      case GL_BYTE:
2647         PROCESS(rSrc, RCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOATZ);
2648         PROCESS(gSrc, GCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOATZ);
2649         PROCESS(bSrc, BCOMP, 0.0F,   0, GLbyte, BYTE_TO_FLOATZ);
2650         PROCESS(aSrc, ACOMP, 1.0F, 127, GLbyte, BYTE_TO_FLOATZ);
2651         break;
2652      case GL_UNSIGNED_SHORT:
2653         PROCESS(rSrc, RCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
2654         PROCESS(gSrc, GCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
2655         PROCESS(bSrc, BCOMP, 0.0F,      0, GLushort, USHORT_TO_FLOAT);
2656         PROCESS(aSrc, ACOMP, 1.0F, 0xffff, GLushort, USHORT_TO_FLOAT);
2657         break;
2658      case GL_SHORT:
2659         PROCESS(rSrc, RCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOATZ);
2660         PROCESS(gSrc, GCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOATZ);
2661         PROCESS(bSrc, BCOMP, 0.0F,     0, GLshort, SHORT_TO_FLOATZ);
2662         PROCESS(aSrc, ACOMP, 1.0F, 32767, GLshort, SHORT_TO_FLOATZ);
2663         break;
2664      case GL_UNSIGNED_INT:
2665         PROCESS(rSrc, RCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
2666         PROCESS(gSrc, GCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
2667         PROCESS(bSrc, BCOMP, 0.0F,          0, GLuint, UINT_TO_FLOAT);
2668         PROCESS(aSrc, ACOMP, 1.0F, 0xffffffff, GLuint, UINT_TO_FLOAT);
2669         break;
2670      case GL_INT:
2671         PROCESS(rSrc, RCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
2672         PROCESS(gSrc, GCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
2673         PROCESS(bSrc, BCOMP, 0.0F,          0, GLint, INT_TO_FLOAT);
2674         PROCESS(aSrc, ACOMP, 1.0F, 2147483647, GLint, INT_TO_FLOAT);
2675         break;
2676      case GL_FLOAT:
2677         PROCESS(rSrc, RCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
2678         PROCESS(gSrc, GCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
2679         PROCESS(bSrc, BCOMP, 0.0F, 0.0F, GLfloat, (GLfloat));
2680         PROCESS(aSrc, ACOMP, 1.0F, 1.0F, GLfloat, (GLfloat));
2681         break;
2682      case GL_HALF_FLOAT_ARB:
2683         PROCESS(rSrc, RCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
2684         PROCESS(gSrc, GCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
2685         PROCESS(bSrc, BCOMP, 0.0F, 0.0F, GLhalfARB, _mesa_half_to_float);
2686         PROCESS(aSrc, ACOMP, 1.0F, 1.0F, GLhalfARB, _mesa_half_to_float);
2687         break;
2688      case GL_UNSIGNED_BYTE_3_3_2:
2689         {
2690            const GLubyte *ubsrc = (const GLubyte *) src;
2691            GLuint i;
2692            if (!intFormat) {
2693               rs = 1.0F / 7.0F;
2694               gs = 1.0F / 7.0F;
2695               bs = 1.0F / 3.0F;
2696            }
2697            for (i = 0; i < n; i ++) {
2698               GLubyte p = ubsrc[i];
2699               rgba[i][rDst] = ((p >> 5)      ) * rs;
2700               rgba[i][gDst] = ((p >> 2) & 0x7) * gs;
2701               rgba[i][bDst] = ((p     ) & 0x3) * bs;
2702               rgba[i][aDst] = 1.0F;
2703            }
2704         }
2705         break;
2706      case GL_UNSIGNED_BYTE_2_3_3_REV:
2707         {
2708            const GLubyte *ubsrc = (const GLubyte *) src;
2709            GLuint i;
2710            if (!intFormat) {
2711               rs = 1.0F / 7.0F;
2712               gs = 1.0F / 7.0F;
2713               bs = 1.0F / 3.0F;
2714            }
2715            for (i = 0; i < n; i ++) {
2716               GLubyte p = ubsrc[i];
2717               rgba[i][rDst] = ((p     ) & 0x7) * rs;
2718               rgba[i][gDst] = ((p >> 3) & 0x7) * gs;
2719               rgba[i][bDst] = ((p >> 6)      ) * bs;
2720               rgba[i][aDst] = 1.0F;
2721            }
2722         }
2723         break;
2724      case GL_UNSIGNED_SHORT_5_6_5:
2725         if (!intFormat) {
2726            rs = 1.0F / 31.0F;
2727            gs = 1.0F / 63.0F;
2728            bs = 1.0F / 31.0F;
2729         }
2730         if (swapBytes) {
2731            const GLushort *ussrc = (const GLushort *) src;
2732            GLuint i;
2733            for (i = 0; i < n; i ++) {
2734               GLushort p = ussrc[i];
2735               SWAP2BYTE(p);
2736               rgba[i][rDst] = ((p >> 11)       ) * rs;
2737               rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
2738               rgba[i][bDst] = ((p      ) & 0x1f) * bs;
2739               rgba[i][aDst] = 1.0F;
2740            }
2741         }
2742         else {
2743            const GLushort *ussrc = (const GLushort *) src;
2744            GLuint i;
2745            for (i = 0; i < n; i ++) {
2746               GLushort p = ussrc[i];
2747               rgba[i][rDst] = ((p >> 11)       ) * rs;
2748               rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
2749               rgba[i][bDst] = ((p      ) & 0x1f) * bs;
2750               rgba[i][aDst] = 1.0F;
2751            }
2752         }
2753         break;
2754      case GL_UNSIGNED_SHORT_5_6_5_REV:
2755         if (!intFormat) {
2756            rs = 1.0F / 31.0F;
2757            gs = 1.0F / 63.0F;
2758            bs = 1.0F / 31.0F;
2759         }
2760         if (swapBytes) {
2761            const GLushort *ussrc = (const GLushort *) src;
2762            GLuint i;
2763            for (i = 0; i < n; i ++) {
2764               GLushort p = ussrc[i];
2765               SWAP2BYTE(p);
2766               rgba[i][rDst] = ((p      ) & 0x1f) * rs;
2767               rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
2768               rgba[i][bDst] = ((p >> 11)       ) * bs;
2769               rgba[i][aDst] = 1.0F;
2770            }
2771         }
2772         else {
2773            const GLushort *ussrc = (const GLushort *) src;
2774            GLuint i;
2775            for (i = 0; i < n; i ++) {
2776               GLushort p = ussrc[i];
2777               rgba[i][rDst] = ((p      ) & 0x1f) * rs;
2778               rgba[i][gDst] = ((p >>  5) & 0x3f) * gs;
2779               rgba[i][bDst] = ((p >> 11)       ) * bs;
2780               rgba[i][aDst] = 1.0F;
2781            }
2782         }
2783         break;
2784      case GL_UNSIGNED_SHORT_4_4_4_4:
2785         if (!intFormat) {
2786            rs = gs = bs = as = 1.0F / 15.0F;
2787         }
2788         if (swapBytes) {
2789            const GLushort *ussrc = (const GLushort *) src;
2790            GLuint i;
2791            for (i = 0; i < n; i ++) {
2792               GLushort p = ussrc[i];
2793               SWAP2BYTE(p);
2794               rgba[i][rDst] = ((p >> 12)      ) * rs;
2795               rgba[i][gDst] = ((p >>  8) & 0xf) * gs;
2796               rgba[i][bDst] = ((p >>  4) & 0xf) * bs;
2797               rgba[i][aDst] = ((p      ) & 0xf) * as;
2798            }
2799         }
2800         else {
2801            const GLushort *ussrc = (const GLushort *) src;
2802            GLuint i;
2803            for (i = 0; i < n; i ++) {
2804               GLushort p = ussrc[i];
2805               rgba[i][rDst] = ((p >> 12)      ) * rs;
2806               rgba[i][gDst] = ((p >>  8) & 0xf) * gs;
2807               rgba[i][bDst] = ((p >>  4) & 0xf) * bs;
2808               rgba[i][aDst] = ((p      ) & 0xf) * as;
2809            }
2810         }
2811         break;
2812      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2813         if (!intFormat) {
2814            rs = gs = bs = as = 1.0F / 15.0F;
2815         }
2816         if (swapBytes) {
2817            const GLushort *ussrc = (const GLushort *) src;
2818            GLuint i;
2819            for (i = 0; i < n; i ++) {
2820               GLushort p = ussrc[i];
2821               SWAP2BYTE(p);
2822               rgba[i][rDst] = ((p      ) & 0xf) * rs;
2823               rgba[i][gDst] = ((p >>  4) & 0xf) * gs;
2824               rgba[i][bDst] = ((p >>  8) & 0xf) * bs;
2825               rgba[i][aDst] = ((p >> 12)      ) * as;
2826            }
2827         }
2828         else {
2829            const GLushort *ussrc = (const GLushort *) src;
2830            GLuint i;
2831            for (i = 0; i < n; i ++) {
2832               GLushort p = ussrc[i];
2833               rgba[i][rDst] = ((p      ) & 0xf) * rs;
2834               rgba[i][gDst] = ((p >>  4) & 0xf) * gs;
2835               rgba[i][bDst] = ((p >>  8) & 0xf) * bs;
2836               rgba[i][aDst] = ((p >> 12)      ) * as;
2837            }
2838         }
2839         break;
2840      case GL_UNSIGNED_SHORT_5_5_5_1:
2841         if (!intFormat) {
2842            rs = gs = bs = 1.0F / 31.0F;
2843         }
2844         if (swapBytes) {
2845            const GLushort *ussrc = (const GLushort *) src;
2846            GLuint i;
2847            for (i = 0; i < n; i ++) {
2848               GLushort p = ussrc[i];
2849               SWAP2BYTE(p);
2850               rgba[i][rDst] = ((p >> 11)       ) * rs;
2851               rgba[i][gDst] = ((p >>  6) & 0x1f) * gs;
2852               rgba[i][bDst] = ((p >>  1) & 0x1f) * bs;
2853               rgba[i][aDst] = ((p      ) & 0x1)  * as;
2854            }
2855         }
2856         else {
2857            const GLushort *ussrc = (const GLushort *) src;
2858            GLuint i;
2859            for (i = 0; i < n; i ++) {
2860               GLushort p = ussrc[i];
2861               rgba[i][rDst] = ((p >> 11)       ) * rs;
2862               rgba[i][gDst] = ((p >>  6) & 0x1f) * gs;
2863               rgba[i][bDst] = ((p >>  1) & 0x1f) * bs;
2864               rgba[i][aDst] = ((p      ) & 0x1)  * as;
2865            }
2866         }
2867         break;
2868      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2869         if (!intFormat) {
2870            rs = gs = bs = 1.0F / 31.0F;
2871         }
2872         if (swapBytes) {
2873            const GLushort *ussrc = (const GLushort *) src;
2874            GLuint i;
2875            for (i = 0; i < n; i ++) {
2876               GLushort p = ussrc[i];
2877               SWAP2BYTE(p);
2878               rgba[i][rDst] = ((p      ) & 0x1f) * rs;
2879               rgba[i][gDst] = ((p >>  5) & 0x1f) * gs;
2880               rgba[i][bDst] = ((p >> 10) & 0x1f) * bs;
2881               rgba[i][aDst] = ((p >> 15)       ) * as;
2882            }
2883         }
2884         else {
2885            const GLushort *ussrc = (const GLushort *) src;
2886            GLuint i;
2887            for (i = 0; i < n; i ++) {
2888               GLushort p = ussrc[i];
2889               rgba[i][rDst] = ((p      ) & 0x1f) * rs;
2890               rgba[i][gDst] = ((p >>  5) & 0x1f) * gs;
2891               rgba[i][bDst] = ((p >> 10) & 0x1f) * bs;
2892               rgba[i][aDst] = ((p >> 15)       ) * as;
2893            }
2894         }
2895         break;
2896      case GL_UNSIGNED_INT_8_8_8_8:
2897         if (swapBytes) {
2898            const GLuint *uisrc = (const GLuint *) src;
2899            GLuint i;
2900            if (intFormat) {
2901               for (i = 0; i < n; i ++) {
2902                  GLuint p = uisrc[i];
2903                  rgba[i][rDst] = (GLfloat) ((p      ) & 0xff);
2904                  rgba[i][gDst] = (GLfloat) ((p >>  8) & 0xff);
2905                  rgba[i][bDst] = (GLfloat) ((p >> 16) & 0xff);
2906                  rgba[i][aDst] = (GLfloat) ((p >> 24)       );
2907               }
2908            }
2909            else {
2910               for (i = 0; i < n; i ++) {
2911                  GLuint p = uisrc[i];
2912                  rgba[i][rDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
2913                  rgba[i][gDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2914                  rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2915                  rgba[i][aDst] = UBYTE_TO_FLOAT((p >> 24)       );
2916               }
2917            }
2918         }
2919         else {
2920            const GLuint *uisrc = (const GLuint *) src;
2921            GLuint i;
2922            if (intFormat) {
2923               for (i = 0; i < n; i ++) {
2924                  GLuint p = uisrc[i];
2925                  rgba[i][rDst] = (GLfloat) ((p >> 24)       );
2926                  rgba[i][gDst] = (GLfloat) ((p >> 16) & 0xff);
2927                  rgba[i][bDst] = (GLfloat) ((p >>  8) & 0xff);
2928                  rgba[i][aDst] = (GLfloat) ((p      ) & 0xff);
2929               }
2930            }
2931            else {
2932               for (i = 0; i < n; i ++) {
2933                  GLuint p = uisrc[i];
2934                  rgba[i][rDst] = UBYTE_TO_FLOAT((p >> 24)       );
2935                  rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2936                  rgba[i][bDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2937                  rgba[i][aDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
2938               }
2939            }
2940         }
2941         break;
2942      case GL_UNSIGNED_INT_8_8_8_8_REV:
2943         if (swapBytes) {
2944            const GLuint *uisrc = (const GLuint *) src;
2945            GLuint i;
2946            if (intFormat) {
2947               for (i = 0; i < n; i ++) {
2948                  GLuint p = uisrc[i];
2949                  rgba[i][rDst] = (GLfloat) ((p >> 24)       );
2950                  rgba[i][gDst] = (GLfloat) ((p >> 16) & 0xff);
2951                  rgba[i][bDst] = (GLfloat) ((p >>  8) & 0xff);
2952                  rgba[i][aDst] = (GLfloat) ((p      ) & 0xff);
2953               }
2954            }
2955            else {
2956               for (i = 0; i < n; i ++) {
2957                  GLuint p = uisrc[i];
2958                  rgba[i][rDst] = UBYTE_TO_FLOAT((p >> 24)       );
2959                  rgba[i][gDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2960                  rgba[i][bDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2961                  rgba[i][aDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
2962               }
2963            }
2964         }
2965         else {
2966            const GLuint *uisrc = (const GLuint *) src;
2967            GLuint i;
2968            if (intFormat) {
2969               for (i = 0; i < n; i ++) {
2970                  GLuint p = uisrc[i];
2971                  rgba[i][rDst] = (GLfloat) ((p      ) & 0xff);
2972                  rgba[i][gDst] = (GLfloat) ((p >>  8) & 0xff);
2973                  rgba[i][bDst] = (GLfloat) ((p >> 16) & 0xff);
2974                  rgba[i][aDst] = (GLfloat) ((p >> 24)       );
2975               }
2976            }
2977            else {
2978               for (i = 0; i < n; i ++) {
2979                  GLuint p = uisrc[i];
2980                  rgba[i][rDst] = UBYTE_TO_FLOAT((p      ) & 0xff);
2981                  rgba[i][gDst] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2982                  rgba[i][bDst] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2983                  rgba[i][aDst] = UBYTE_TO_FLOAT((p >> 24)       );
2984               }
2985            }
2986         }
2987         break;
2988      case GL_UNSIGNED_INT_10_10_10_2:
2989         if (!intFormat) {
2990            rs = 1.0F / 1023.0F;
2991            gs = 1.0F / 1023.0F;
2992            bs = 1.0F / 1023.0F;
2993            as = 1.0F / 3.0F;
2994         }
2995         if (swapBytes) {
2996            const GLuint *uisrc = (const GLuint *) src;
2997            GLuint i;
2998            for (i = 0; i < n; i ++) {
2999               GLuint p = uisrc[i];
3000               SWAP4BYTE(p);
3001               rgba[i][rDst] = ((p >> 22)        ) * rs;
3002               rgba[i][gDst] = ((p >> 12) & 0x3ff) * gs;
3003               rgba[i][bDst] = ((p >>  2) & 0x3ff) * bs;
3004               rgba[i][aDst] = ((p      ) & 0x3  ) * as;
3005            }
3006         }
3007         else {
3008            const GLuint *uisrc = (const GLuint *) src;
3009            GLuint i;
3010            for (i = 0; i < n; i ++) {
3011               GLuint p = uisrc[i];
3012               rgba[i][rDst] = ((p >> 22)        ) * rs;
3013               rgba[i][gDst] = ((p >> 12) & 0x3ff) * gs;
3014               rgba[i][bDst] = ((p >>  2) & 0x3ff) * bs;
3015               rgba[i][aDst] = ((p      ) & 0x3  ) * as;
3016            }
3017         }
3018         break;
3019      case GL_UNSIGNED_INT_2_10_10_10_REV:
3020         if (!intFormat) {
3021            rs = 1.0F / 1023.0F;
3022            gs = 1.0F / 1023.0F;
3023            bs = 1.0F / 1023.0F;
3024            as = 1.0F / 3.0F;
3025         }
3026         if (swapBytes) {
3027            const GLuint *uisrc = (const GLuint *) src;
3028            GLuint i;
3029            for (i = 0; i < n; i ++) {
3030               GLuint p = uisrc[i];
3031               SWAP4BYTE(p);
3032               rgba[i][rDst] = ((p      ) & 0x3ff) * rs;
3033               rgba[i][gDst] = ((p >> 10) & 0x3ff) * gs;
3034               rgba[i][bDst] = ((p >> 20) & 0x3ff) * bs;
3035               rgba[i][aDst] = ((p >> 30)        ) * as;
3036            }
3037         }
3038         else {
3039            const GLuint *uisrc = (const GLuint *) src;
3040            GLuint i;
3041            for (i = 0; i < n; i ++) {
3042               GLuint p = uisrc[i];
3043               rgba[i][rDst] = ((p      ) & 0x3ff) * rs;
3044               rgba[i][gDst] = ((p >> 10) & 0x3ff) * gs;
3045               rgba[i][bDst] = ((p >> 20) & 0x3ff) * bs;
3046               rgba[i][aDst] = ((p >> 30)        ) * as;
3047            }
3048         }
3049         break;
3050      case GL_UNSIGNED_INT_5_9_9_9_REV:
3051         if (swapBytes) {
3052            const GLuint *uisrc = (const GLuint *) src;
3053            GLuint i;
3054            GLfloat f[3];
3055            for (i = 0; i < n; i ++) {
3056               GLuint p = uisrc[i];
3057               SWAP4BYTE(p);
3058               rgb9e5_to_float3(p, f);
3059               rgba[i][rDst] = f[0];
3060               rgba[i][gDst] = f[1];
3061               rgba[i][bDst] = f[2];
3062               rgba[i][aDst] = 1.0F;
3063            }
3064         }
3065         else {
3066            const GLuint *uisrc = (const GLuint *) src;
3067            GLuint i;
3068            GLfloat f[3];
3069            for (i = 0; i < n; i ++) {
3070               rgb9e5_to_float3(uisrc[i], f);
3071               rgba[i][rDst] = f[0];
3072               rgba[i][gDst] = f[1];
3073               rgba[i][bDst] = f[2];
3074               rgba[i][aDst] = 1.0F;
3075            }
3076         }
3077         break;
3078      case GL_UNSIGNED_INT_10F_11F_11F_REV:
3079         if (swapBytes) {
3080            const GLuint *uisrc = (const GLuint *) src;
3081            GLuint i;
3082            GLfloat f[3];
3083            for (i = 0; i < n; i ++) {
3084               GLuint p = uisrc[i];
3085               SWAP4BYTE(p);
3086               r11g11b10f_to_float3(p, f);
3087               rgba[i][rDst] = f[0];
3088               rgba[i][gDst] = f[1];
3089               rgba[i][bDst] = f[2];
3090               rgba[i][aDst] = 1.0F;
3091            }
3092         }
3093         else {
3094            const GLuint *uisrc = (const GLuint *) src;
3095            GLuint i;
3096            GLfloat f[3];
3097            for (i = 0; i < n; i ++) {
3098               r11g11b10f_to_float3(uisrc[i], f);
3099               rgba[i][rDst] = f[0];
3100               rgba[i][gDst] = f[1];
3101               rgba[i][bDst] = f[2];
3102               rgba[i][aDst] = 1.0F;
3103            }
3104         }
3105         break;
3106      default:
3107         _mesa_problem(NULL, "bad srcType in extract float data");
3108         break;
3109   }
3110#undef PROCESS
3111}
3112
3113
3114static inline GLuint
3115clamp_float_to_uint(GLfloat f)
3116{
3117   return f < 0.0F ? 0 : F_TO_I(f);
3118}
3119
3120
3121static inline GLuint
3122clamp_half_to_uint(GLhalfARB h)
3123{
3124   GLfloat f = _mesa_half_to_float(h);
3125   return f < 0.0F ? 0 : F_TO_I(f);
3126}
3127
3128
3129/**
3130 * \sa extract_float_rgba()
3131 */
3132static void
3133extract_uint_rgba(GLuint n, GLuint rgba[][4],
3134                  GLenum srcFormat, GLenum srcType, const GLvoid *src,
3135                  GLboolean swapBytes)
3136{
3137   GLint rSrc, gSrc, bSrc, aSrc;
3138   GLint stride;
3139   GLint rDst, bDst, gDst, aDst;
3140
3141   ASSERT(srcFormat == GL_RED ||
3142          srcFormat == GL_GREEN ||
3143          srcFormat == GL_BLUE ||
3144          srcFormat == GL_ALPHA ||
3145          srcFormat == GL_LUMINANCE ||
3146          srcFormat == GL_LUMINANCE_ALPHA ||
3147          srcFormat == GL_INTENSITY ||
3148          srcFormat == GL_RG ||
3149          srcFormat == GL_RGB ||
3150          srcFormat == GL_BGR ||
3151          srcFormat == GL_RGBA ||
3152          srcFormat == GL_BGRA ||
3153          srcFormat == GL_ABGR_EXT ||
3154          srcFormat == GL_DU8DV8_ATI ||
3155          srcFormat == GL_DUDV_ATI ||
3156          srcFormat == GL_RED_INTEGER_EXT ||
3157          srcFormat == GL_RG_INTEGER ||
3158          srcFormat == GL_GREEN_INTEGER_EXT ||
3159          srcFormat == GL_BLUE_INTEGER_EXT ||
3160          srcFormat == GL_ALPHA_INTEGER_EXT ||
3161          srcFormat == GL_RGB_INTEGER_EXT ||
3162          srcFormat == GL_RGBA_INTEGER_EXT ||
3163          srcFormat == GL_BGR_INTEGER_EXT ||
3164          srcFormat == GL_BGRA_INTEGER_EXT ||
3165          srcFormat == GL_LUMINANCE_INTEGER_EXT ||
3166          srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
3167
3168   ASSERT(srcType == GL_UNSIGNED_BYTE ||
3169          srcType == GL_BYTE ||
3170          srcType == GL_UNSIGNED_SHORT ||
3171          srcType == GL_SHORT ||
3172          srcType == GL_UNSIGNED_INT ||
3173          srcType == GL_INT ||
3174          srcType == GL_HALF_FLOAT_ARB ||
3175          srcType == GL_FLOAT ||
3176          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3177          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3178          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3179          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3180          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3181          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3182          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3183          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3184          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3185          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3186          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3187          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
3188          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
3189          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
3190
3191   get_component_mapping(srcFormat,
3192                         &rSrc, &gSrc, &bSrc, &aSrc,
3193                         &rDst, &gDst, &bDst, &aDst);
3194
3195   stride = _mesa_components_in_format(srcFormat);
3196
3197#define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT, TYPE, CONVERSION)	\
3198   if ((SRC_INDEX) < 0) {						\
3199      GLuint i;								\
3200      for (i = 0; i < n; i++) {						\
3201         rgba[i][DST_INDEX] = DEFAULT;					\
3202      }									\
3203   }									\
3204   else if (swapBytes) {						\
3205      const TYPE *s = (const TYPE *) src;				\
3206      GLuint i;								\
3207      for (i = 0; i < n; i++) {						\
3208         TYPE value = s[SRC_INDEX];					\
3209         if (sizeof(TYPE) == 2) {					\
3210            SWAP2BYTE(value);						\
3211         }								\
3212         else if (sizeof(TYPE) == 4) {					\
3213            SWAP4BYTE(value);						\
3214         }								\
3215         rgba[i][DST_INDEX] = CONVERSION(value);                        \
3216         s += stride;							\
3217      }									\
3218   }									\
3219   else {								\
3220      const TYPE *s = (const TYPE *) src;				\
3221      GLuint i;								\
3222      for (i = 0; i < n; i++) {						\
3223         rgba[i][DST_INDEX] = CONVERSION(s[SRC_INDEX]);			\
3224         s += stride;							\
3225      }									\
3226   }
3227
3228   switch (srcType) {
3229      case GL_UNSIGNED_BYTE:
3230         PROCESS(rSrc, RCOMP, 0, GLubyte, (GLuint));
3231         PROCESS(gSrc, GCOMP, 0, GLubyte, (GLuint));
3232         PROCESS(bSrc, BCOMP, 0, GLubyte, (GLuint));
3233         PROCESS(aSrc, ACOMP, 1, GLubyte, (GLuint));
3234         break;
3235      case GL_BYTE:
3236         PROCESS(rSrc, RCOMP, 0, GLbyte, (GLuint));
3237         PROCESS(gSrc, GCOMP, 0, GLbyte, (GLuint));
3238         PROCESS(bSrc, BCOMP, 0, GLbyte, (GLuint));
3239         PROCESS(aSrc, ACOMP, 1, GLbyte, (GLuint));
3240         break;
3241      case GL_UNSIGNED_SHORT:
3242         PROCESS(rSrc, RCOMP, 0, GLushort, (GLuint));
3243         PROCESS(gSrc, GCOMP, 0, GLushort, (GLuint));
3244         PROCESS(bSrc, BCOMP, 0, GLushort, (GLuint));
3245         PROCESS(aSrc, ACOMP, 1, GLushort, (GLuint));
3246         break;
3247      case GL_SHORT:
3248         PROCESS(rSrc, RCOMP, 0, GLshort, (GLuint));
3249         PROCESS(gSrc, GCOMP, 0, GLshort, (GLuint));
3250         PROCESS(bSrc, BCOMP, 0, GLshort, (GLuint));
3251         PROCESS(aSrc, ACOMP, 1, GLshort, (GLuint));
3252         break;
3253      case GL_UNSIGNED_INT:
3254         PROCESS(rSrc, RCOMP, 0, GLuint, (GLuint));
3255         PROCESS(gSrc, GCOMP, 0, GLuint, (GLuint));
3256         PROCESS(bSrc, BCOMP, 0, GLuint, (GLuint));
3257         PROCESS(aSrc, ACOMP, 1, GLuint, (GLuint));
3258         break;
3259      case GL_INT:
3260         PROCESS(rSrc, RCOMP, 0, GLint, (GLuint));
3261         PROCESS(gSrc, GCOMP, 0, GLint, (GLuint));
3262         PROCESS(bSrc, BCOMP, 0, GLint, (GLuint));
3263         PROCESS(aSrc, ACOMP, 1, GLint, (GLuint));
3264         break;
3265      case GL_FLOAT:
3266         PROCESS(rSrc, RCOMP, 0, GLfloat, clamp_float_to_uint);
3267         PROCESS(gSrc, GCOMP, 0, GLfloat, clamp_float_to_uint);
3268         PROCESS(bSrc, BCOMP, 0, GLfloat, clamp_float_to_uint);
3269         PROCESS(aSrc, ACOMP, 1, GLfloat, clamp_float_to_uint);
3270         break;
3271      case GL_HALF_FLOAT_ARB:
3272         PROCESS(rSrc, RCOMP, 0, GLhalfARB, clamp_half_to_uint);
3273         PROCESS(gSrc, GCOMP, 0, GLhalfARB, clamp_half_to_uint);
3274         PROCESS(bSrc, BCOMP, 0, GLhalfARB, clamp_half_to_uint);
3275         PROCESS(aSrc, ACOMP, 1, GLhalfARB, clamp_half_to_uint);
3276         break;
3277      case GL_UNSIGNED_BYTE_3_3_2:
3278         {
3279            const GLubyte *ubsrc = (const GLubyte *) src;
3280            GLuint i;
3281            for (i = 0; i < n; i ++) {
3282               GLubyte p = ubsrc[i];
3283               rgba[i][rDst] = ((p >> 5)      );
3284               rgba[i][gDst] = ((p >> 2) & 0x7);
3285               rgba[i][bDst] = ((p     ) & 0x3);
3286               rgba[i][aDst] = 1;
3287            }
3288         }
3289         break;
3290      case GL_UNSIGNED_BYTE_2_3_3_REV:
3291         {
3292            const GLubyte *ubsrc = (const GLubyte *) src;
3293            GLuint i;
3294            for (i = 0; i < n; i ++) {
3295               GLubyte p = ubsrc[i];
3296               rgba[i][rDst] = ((p     ) & 0x7);
3297               rgba[i][gDst] = ((p >> 3) & 0x7);
3298               rgba[i][bDst] = ((p >> 6)      );
3299               rgba[i][aDst] = 1;
3300            }
3301         }
3302         break;
3303      case GL_UNSIGNED_SHORT_5_6_5:
3304         if (swapBytes) {
3305            const GLushort *ussrc = (const GLushort *) src;
3306            GLuint i;
3307            for (i = 0; i < n; i ++) {
3308               GLushort p = ussrc[i];
3309               SWAP2BYTE(p);
3310               rgba[i][rDst] = ((p >> 11)       );
3311               rgba[i][gDst] = ((p >>  5) & 0x3f);
3312               rgba[i][bDst] = ((p      ) & 0x1f);
3313               rgba[i][aDst] = 1;
3314            }
3315         }
3316         else {
3317            const GLushort *ussrc = (const GLushort *) src;
3318            GLuint i;
3319            for (i = 0; i < n; i ++) {
3320               GLushort p = ussrc[i];
3321               rgba[i][rDst] = ((p >> 11)       );
3322               rgba[i][gDst] = ((p >>  5) & 0x3f);
3323               rgba[i][bDst] = ((p      ) & 0x1f);
3324               rgba[i][aDst] = 1;
3325            }
3326         }
3327         break;
3328      case GL_UNSIGNED_SHORT_5_6_5_REV:
3329         if (swapBytes) {
3330            const GLushort *ussrc = (const GLushort *) src;
3331            GLuint i;
3332            for (i = 0; i < n; i ++) {
3333               GLushort p = ussrc[i];
3334               SWAP2BYTE(p);
3335               rgba[i][rDst] = ((p      ) & 0x1f);
3336               rgba[i][gDst] = ((p >>  5) & 0x3f);
3337               rgba[i][bDst] = ((p >> 11)       );
3338               rgba[i][aDst] = 1;
3339            }
3340         }
3341         else {
3342            const GLushort *ussrc = (const GLushort *) src;
3343            GLuint i;
3344            for (i = 0; i < n; i ++) {
3345               GLushort p = ussrc[i];
3346               rgba[i][rDst] = ((p      ) & 0x1f);
3347               rgba[i][gDst] = ((p >>  5) & 0x3f);
3348               rgba[i][bDst] = ((p >> 11)       );
3349               rgba[i][aDst] = 1;
3350            }
3351         }
3352         break;
3353      case GL_UNSIGNED_SHORT_4_4_4_4:
3354         if (swapBytes) {
3355            const GLushort *ussrc = (const GLushort *) src;
3356            GLuint i;
3357            for (i = 0; i < n; i ++) {
3358               GLushort p = ussrc[i];
3359               SWAP2BYTE(p);
3360               rgba[i][rDst] = ((p >> 12)      );
3361               rgba[i][gDst] = ((p >>  8) & 0xf);
3362               rgba[i][bDst] = ((p >>  4) & 0xf);
3363               rgba[i][aDst] = ((p      ) & 0xf);
3364            }
3365         }
3366         else {
3367            const GLushort *ussrc = (const GLushort *) src;
3368            GLuint i;
3369            for (i = 0; i < n; i ++) {
3370               GLushort p = ussrc[i];
3371               rgba[i][rDst] = ((p >> 12)      );
3372               rgba[i][gDst] = ((p >>  8) & 0xf);
3373               rgba[i][bDst] = ((p >>  4) & 0xf);
3374               rgba[i][aDst] = ((p      ) & 0xf);
3375            }
3376         }
3377         break;
3378      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3379         if (swapBytes) {
3380            const GLushort *ussrc = (const GLushort *) src;
3381            GLuint i;
3382            for (i = 0; i < n; i ++) {
3383               GLushort p = ussrc[i];
3384               SWAP2BYTE(p);
3385               rgba[i][rDst] = ((p      ) & 0xf);
3386               rgba[i][gDst] = ((p >>  4) & 0xf);
3387               rgba[i][bDst] = ((p >>  8) & 0xf);
3388               rgba[i][aDst] = ((p >> 12)      );
3389            }
3390         }
3391         else {
3392            const GLushort *ussrc = (const GLushort *) src;
3393            GLuint i;
3394            for (i = 0; i < n; i ++) {
3395               GLushort p = ussrc[i];
3396               rgba[i][rDst] = ((p      ) & 0xf);
3397               rgba[i][gDst] = ((p >>  4) & 0xf);
3398               rgba[i][bDst] = ((p >>  8) & 0xf);
3399               rgba[i][aDst] = ((p >> 12)      );
3400            }
3401         }
3402         break;
3403      case GL_UNSIGNED_SHORT_5_5_5_1:
3404         if (swapBytes) {
3405            const GLushort *ussrc = (const GLushort *) src;
3406            GLuint i;
3407            for (i = 0; i < n; i ++) {
3408               GLushort p = ussrc[i];
3409               SWAP2BYTE(p);
3410               rgba[i][rDst] = ((p >> 11)       );
3411               rgba[i][gDst] = ((p >>  6) & 0x1f);
3412               rgba[i][bDst] = ((p >>  1) & 0x1f);
3413               rgba[i][aDst] = ((p      ) & 0x1 );
3414            }
3415         }
3416         else {
3417            const GLushort *ussrc = (const GLushort *) src;
3418            GLuint i;
3419            for (i = 0; i < n; i ++) {
3420               GLushort p = ussrc[i];
3421               rgba[i][rDst] = ((p >> 11)       );
3422               rgba[i][gDst] = ((p >>  6) & 0x1f);
3423               rgba[i][bDst] = ((p >>  1) & 0x1f);
3424               rgba[i][aDst] = ((p      ) & 0x1 );
3425            }
3426         }
3427         break;
3428      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3429         if (swapBytes) {
3430            const GLushort *ussrc = (const GLushort *) src;
3431            GLuint i;
3432            for (i = 0; i < n; i ++) {
3433               GLushort p = ussrc[i];
3434               SWAP2BYTE(p);
3435               rgba[i][rDst] = ((p      ) & 0x1f);
3436               rgba[i][gDst] = ((p >>  5) & 0x1f);
3437               rgba[i][bDst] = ((p >> 10) & 0x1f);
3438               rgba[i][aDst] = ((p >> 15)       );
3439            }
3440         }
3441         else {
3442            const GLushort *ussrc = (const GLushort *) src;
3443            GLuint i;
3444            for (i = 0; i < n; i ++) {
3445               GLushort p = ussrc[i];
3446               rgba[i][rDst] = ((p      ) & 0x1f);
3447               rgba[i][gDst] = ((p >>  5) & 0x1f);
3448               rgba[i][bDst] = ((p >> 10) & 0x1f);
3449               rgba[i][aDst] = ((p >> 15)       );
3450            }
3451         }
3452         break;
3453      case GL_UNSIGNED_INT_8_8_8_8:
3454         if (swapBytes) {
3455            const GLuint *uisrc = (const GLuint *) src;
3456            GLuint i;
3457            for (i = 0; i < n; i ++) {
3458               GLuint p = uisrc[i];
3459               rgba[i][rDst] = ((p      ) & 0xff);
3460               rgba[i][gDst] = ((p >>  8) & 0xff);
3461               rgba[i][bDst] = ((p >> 16) & 0xff);
3462               rgba[i][aDst] = ((p >> 24)       );
3463            }
3464         }
3465         else {
3466            const GLuint *uisrc = (const GLuint *) src;
3467            GLuint i;
3468            for (i = 0; i < n; i ++) {
3469               GLuint p = uisrc[i];
3470               rgba[i][rDst] = ((p >> 24)       );
3471               rgba[i][gDst] = ((p >> 16) & 0xff);
3472               rgba[i][bDst] = ((p >>  8) & 0xff);
3473               rgba[i][aDst] = ((p      ) & 0xff);
3474            }
3475         }
3476         break;
3477      case GL_UNSIGNED_INT_8_8_8_8_REV:
3478         if (swapBytes) {
3479            const GLuint *uisrc = (const GLuint *) src;
3480            GLuint i;
3481            for (i = 0; i < n; i ++) {
3482               GLuint p = uisrc[i];
3483               rgba[i][rDst] = ((p >> 24)       );
3484               rgba[i][gDst] = ((p >> 16) & 0xff);
3485               rgba[i][bDst] = ((p >>  8) & 0xff);
3486               rgba[i][aDst] = ((p      ) & 0xff);
3487            }
3488         }
3489         else {
3490            const GLuint *uisrc = (const GLuint *) src;
3491            GLuint i;
3492            for (i = 0; i < n; i ++) {
3493               GLuint p = uisrc[i];
3494               rgba[i][rDst] = ((p      ) & 0xff);
3495               rgba[i][gDst] = ((p >>  8) & 0xff);
3496               rgba[i][bDst] = ((p >> 16) & 0xff);
3497               rgba[i][aDst] = ((p >> 24)       );
3498            }
3499         }
3500         break;
3501      case GL_UNSIGNED_INT_10_10_10_2:
3502         if (swapBytes) {
3503            const GLuint *uisrc = (const GLuint *) src;
3504            GLuint i;
3505            for (i = 0; i < n; i ++) {
3506               GLuint p = uisrc[i];
3507               SWAP4BYTE(p);
3508               rgba[i][rDst] = ((p >> 22)        );
3509               rgba[i][gDst] = ((p >> 12) & 0x3ff);
3510               rgba[i][bDst] = ((p >>  2) & 0x3ff);
3511               rgba[i][aDst] = ((p      ) & 0x3  );
3512            }
3513         }
3514         else {
3515            const GLuint *uisrc = (const GLuint *) src;
3516            GLuint i;
3517            for (i = 0; i < n; i ++) {
3518               GLuint p = uisrc[i];
3519               rgba[i][rDst] = ((p >> 22)        );
3520               rgba[i][gDst] = ((p >> 12) & 0x3ff);
3521               rgba[i][bDst] = ((p >>  2) & 0x3ff);
3522               rgba[i][aDst] = ((p      ) & 0x3  );
3523            }
3524         }
3525         break;
3526      case GL_UNSIGNED_INT_2_10_10_10_REV:
3527         if (swapBytes) {
3528            const GLuint *uisrc = (const GLuint *) src;
3529            GLuint i;
3530            for (i = 0; i < n; i ++) {
3531               GLuint p = uisrc[i];
3532               SWAP4BYTE(p);
3533               rgba[i][rDst] = ((p      ) & 0x3ff);
3534               rgba[i][gDst] = ((p >> 10) & 0x3ff);
3535               rgba[i][bDst] = ((p >> 20) & 0x3ff);
3536               rgba[i][aDst] = ((p >> 30)        );
3537            }
3538         }
3539         else {
3540            const GLuint *uisrc = (const GLuint *) src;
3541            GLuint i;
3542            for (i = 0; i < n; i ++) {
3543               GLuint p = uisrc[i];
3544               rgba[i][rDst] = ((p      ) & 0x3ff);
3545               rgba[i][gDst] = ((p >> 10) & 0x3ff);
3546               rgba[i][bDst] = ((p >> 20) & 0x3ff);
3547               rgba[i][aDst] = ((p >> 30)        );
3548            }
3549         }
3550         break;
3551      case GL_UNSIGNED_INT_5_9_9_9_REV:
3552         if (swapBytes) {
3553            const GLuint *uisrc = (const GLuint *) src;
3554            GLuint i;
3555            float f[3];
3556            for (i = 0; i < n; i ++) {
3557               GLuint p = uisrc[i];
3558               SWAP4BYTE(p);
3559               rgb9e5_to_float3(p, f);
3560               rgba[i][rDst] = clamp_float_to_uint(f[0]);
3561               rgba[i][gDst] = clamp_float_to_uint(f[1]);
3562               rgba[i][bDst] = clamp_float_to_uint(f[2]);
3563               rgba[i][aDst] = 1;
3564            }
3565         }
3566         else {
3567            const GLuint *uisrc = (const GLuint *) src;
3568            GLuint i;
3569            float f[3];
3570            for (i = 0; i < n; i ++) {
3571               GLuint p = uisrc[i];
3572               rgb9e5_to_float3(p, f);
3573               rgba[i][rDst] = clamp_float_to_uint(f[0]);
3574               rgba[i][gDst] = clamp_float_to_uint(f[1]);
3575               rgba[i][bDst] = clamp_float_to_uint(f[2]);
3576               rgba[i][aDst] = 1;
3577            }
3578         }
3579         break;
3580      case GL_UNSIGNED_INT_10F_11F_11F_REV:
3581         if (swapBytes) {
3582            const GLuint *uisrc = (const GLuint *) src;
3583            GLuint i;
3584            float f[3];
3585            for (i = 0; i < n; i ++) {
3586               GLuint p = uisrc[i];
3587               SWAP4BYTE(p);
3588               r11g11b10f_to_float3(p, f);
3589               rgba[i][rDst] = clamp_float_to_uint(f[0]);
3590               rgba[i][gDst] = clamp_float_to_uint(f[1]);
3591               rgba[i][bDst] = clamp_float_to_uint(f[2]);
3592               rgba[i][aDst] = 1;
3593            }
3594         }
3595         else {
3596            const GLuint *uisrc = (const GLuint *) src;
3597            GLuint i;
3598            float f[3];
3599            for (i = 0; i < n; i ++) {
3600               GLuint p = uisrc[i];
3601               r11g11b10f_to_float3(p, f);
3602               rgba[i][rDst] = clamp_float_to_uint(f[0]);
3603               rgba[i][gDst] = clamp_float_to_uint(f[1]);
3604               rgba[i][bDst] = clamp_float_to_uint(f[2]);
3605               rgba[i][aDst] = 1;
3606            }
3607         }
3608         break;
3609      default:
3610         _mesa_problem(NULL, "bad srcType in extract uint data");
3611         break;
3612   }
3613#undef PROCESS
3614}
3615
3616
3617
3618/*
3619 * Unpack a row of color image data from a client buffer according to
3620 * the pixel unpacking parameters.
3621 * Return GLubyte values in the specified dest image format.
3622 * This is used by glDrawPixels and glTexImage?D().
3623 * \param ctx - the context
3624 *         n - number of pixels in the span
3625 *         dstFormat - format of destination color array
3626 *         dest - the destination color array
3627 *         srcFormat - source image format
3628 *         srcType - source image  data type
3629 *         source - source image pointer
3630 *         srcPacking - pixel unpacking parameters
3631 *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
3632 *
3633 * XXX perhaps expand this to process whole images someday.
3634 */
3635void
3636_mesa_unpack_color_span_ubyte(struct gl_context *ctx,
3637                              GLuint n, GLenum dstFormat, GLubyte dest[],
3638                              GLenum srcFormat, GLenum srcType,
3639                              const GLvoid *source,
3640                              const struct gl_pixelstore_attrib *srcPacking,
3641                              GLbitfield transferOps )
3642{
3643   GLboolean intFormat = _mesa_is_enum_format_integer(srcFormat);
3644   ASSERT(dstFormat == GL_ALPHA ||
3645          dstFormat == GL_LUMINANCE ||
3646          dstFormat == GL_LUMINANCE_ALPHA ||
3647          dstFormat == GL_INTENSITY ||
3648          dstFormat == GL_RED ||
3649          dstFormat == GL_RG ||
3650          dstFormat == GL_RGB ||
3651          dstFormat == GL_RGBA);
3652
3653   ASSERT(srcFormat == GL_RED ||
3654          srcFormat == GL_GREEN ||
3655          srcFormat == GL_BLUE ||
3656          srcFormat == GL_ALPHA ||
3657          srcFormat == GL_LUMINANCE ||
3658          srcFormat == GL_LUMINANCE_ALPHA ||
3659          srcFormat == GL_INTENSITY ||
3660          srcFormat == GL_RG ||
3661          srcFormat == GL_RGB ||
3662          srcFormat == GL_BGR ||
3663          srcFormat == GL_RGBA ||
3664          srcFormat == GL_BGRA ||
3665          srcFormat == GL_ABGR_EXT ||
3666          srcFormat == GL_COLOR_INDEX);
3667
3668   ASSERT(srcType == GL_BITMAP ||
3669          srcType == GL_UNSIGNED_BYTE ||
3670          srcType == GL_BYTE ||
3671          srcType == GL_UNSIGNED_SHORT ||
3672          srcType == GL_SHORT ||
3673          srcType == GL_UNSIGNED_INT ||
3674          srcType == GL_INT ||
3675          srcType == GL_HALF_FLOAT_ARB ||
3676          srcType == GL_FLOAT ||
3677          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3678          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3679          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3680          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3681          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3682          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3683          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3684          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3685          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3686          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3687          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3688          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
3689          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
3690          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
3691
3692   /* EXT_texture_integer specifies no transfer ops on integer
3693    * types in the resolved issues section. Just set them to 0
3694    * for integer surfaces.
3695    */
3696   if (intFormat)
3697      transferOps = 0;
3698
3699   /* Try simple cases first */
3700   if (transferOps == 0) {
3701      if (srcType == GL_UNSIGNED_BYTE) {
3702         if (dstFormat == GL_RGBA) {
3703            if (srcFormat == GL_RGBA) {
3704               memcpy( dest, source, n * 4 * sizeof(GLubyte) );
3705               return;
3706            }
3707            else if (srcFormat == GL_RGB) {
3708               GLuint i;
3709               const GLubyte *src = (const GLubyte *) source;
3710               GLubyte *dst = dest;
3711               for (i = 0; i < n; i++) {
3712                  dst[0] = src[0];
3713                  dst[1] = src[1];
3714                  dst[2] = src[2];
3715                  dst[3] = 255;
3716                  src += 3;
3717                  dst += 4;
3718               }
3719               return;
3720            }
3721         }
3722         else if (dstFormat == GL_RGB) {
3723            if (srcFormat == GL_RGB) {
3724               memcpy( dest, source, n * 3 * sizeof(GLubyte) );
3725               return;
3726            }
3727            else if (srcFormat == GL_RGBA) {
3728               GLuint i;
3729               const GLubyte *src = (const GLubyte *) source;
3730               GLubyte *dst = dest;
3731               for (i = 0; i < n; i++) {
3732                  dst[0] = src[0];
3733                  dst[1] = src[1];
3734                  dst[2] = src[2];
3735                  src += 4;
3736                  dst += 3;
3737               }
3738               return;
3739            }
3740         }
3741         else if (dstFormat == srcFormat) {
3742            GLint comps = _mesa_components_in_format(srcFormat);
3743            assert(comps > 0);
3744            memcpy( dest, source, n * comps * sizeof(GLubyte) );
3745            return;
3746         }
3747      }
3748   }
3749
3750
3751   /* general solution begins here */
3752   {
3753      GLint dstComponents;
3754      GLint rDst, gDst, bDst, aDst, lDst, iDst;
3755      GLfloat (*rgba)[4] = (GLfloat (*)[4]) malloc(4 * n * sizeof(GLfloat));
3756
3757      if (!rgba) {
3758         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3759         return;
3760      }
3761
3762      dstComponents = _mesa_components_in_format( dstFormat );
3763      /* source & dest image formats should have been error checked by now */
3764      assert(dstComponents > 0);
3765
3766      /*
3767       * Extract image data and convert to RGBA floats
3768       */
3769      if (srcFormat == GL_COLOR_INDEX) {
3770         GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
3771
3772         if (!indexes) {
3773            _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3774            free(rgba);
3775            return;
3776         }
3777
3778         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3779                              srcPacking);
3780
3781	 /* Convert indexes to RGBA */
3782	 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3783	    _mesa_shift_and_offset_ci(ctx, n, indexes);
3784	 }
3785	 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3786
3787         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3788          * with color indexes.
3789          */
3790         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3791
3792         free(indexes);
3793      }
3794      else {
3795         /* non-color index data */
3796         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3797                            srcPacking->SwapBytes);
3798      }
3799
3800      /* Need to clamp if returning GLubytes */
3801      transferOps |= IMAGE_CLAMP_BIT;
3802
3803      if (transferOps) {
3804         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3805      }
3806
3807      get_component_indexes(dstFormat,
3808                            &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
3809
3810      /* Now return the GLubyte data in the requested dstFormat */
3811      if (rDst >= 0) {
3812         GLubyte *dst = dest;
3813         GLuint i;
3814         for (i = 0; i < n; i++) {
3815            CLAMPED_FLOAT_TO_UBYTE(dst[rDst], rgba[i][RCOMP]);
3816            dst += dstComponents;
3817         }
3818      }
3819
3820      if (gDst >= 0) {
3821         GLubyte *dst = dest;
3822         GLuint i;
3823         for (i = 0; i < n; i++) {
3824            CLAMPED_FLOAT_TO_UBYTE(dst[gDst], rgba[i][GCOMP]);
3825            dst += dstComponents;
3826         }
3827      }
3828
3829      if (bDst >= 0) {
3830         GLubyte *dst = dest;
3831         GLuint i;
3832         for (i = 0; i < n; i++) {
3833            CLAMPED_FLOAT_TO_UBYTE(dst[bDst], rgba[i][BCOMP]);
3834            dst += dstComponents;
3835         }
3836      }
3837
3838      if (aDst >= 0) {
3839         GLubyte *dst = dest;
3840         GLuint i;
3841         for (i = 0; i < n; i++) {
3842            CLAMPED_FLOAT_TO_UBYTE(dst[aDst], rgba[i][ACOMP]);
3843            dst += dstComponents;
3844         }
3845      }
3846
3847      if (iDst >= 0) {
3848         GLubyte *dst = dest;
3849         GLuint i;
3850         assert(iDst == 0);
3851         assert(dstComponents == 1);
3852         for (i = 0; i < n; i++) {
3853            /* Intensity comes from red channel */
3854            CLAMPED_FLOAT_TO_UBYTE(dst[i], rgba[i][RCOMP]);
3855         }
3856      }
3857
3858      if (lDst >= 0) {
3859         GLubyte *dst = dest;
3860         GLuint i;
3861         assert(lDst == 0);
3862         for (i = 0; i < n; i++) {
3863            /* Luminance comes from red channel */
3864            CLAMPED_FLOAT_TO_UBYTE(dst[0], rgba[i][RCOMP]);
3865            dst += dstComponents;
3866         }
3867      }
3868
3869      free(rgba);
3870   }
3871}
3872
3873
3874/**
3875 * Same as _mesa_unpack_color_span_ubyte(), but return GLfloat data
3876 * instead of GLubyte.
3877 */
3878void
3879_mesa_unpack_color_span_float( struct gl_context *ctx,
3880                               GLuint n, GLenum dstFormat, GLfloat dest[],
3881                               GLenum srcFormat, GLenum srcType,
3882                               const GLvoid *source,
3883                               const struct gl_pixelstore_attrib *srcPacking,
3884                               GLbitfield transferOps )
3885{
3886   ASSERT(dstFormat == GL_ALPHA ||
3887          dstFormat == GL_LUMINANCE ||
3888          dstFormat == GL_LUMINANCE_ALPHA ||
3889          dstFormat == GL_INTENSITY ||
3890          dstFormat == GL_RED ||
3891          dstFormat == GL_RG ||
3892          dstFormat == GL_RGB ||
3893          dstFormat == GL_RGBA);
3894
3895   ASSERT(srcFormat == GL_RED ||
3896          srcFormat == GL_GREEN ||
3897          srcFormat == GL_BLUE ||
3898          srcFormat == GL_ALPHA ||
3899          srcFormat == GL_LUMINANCE ||
3900          srcFormat == GL_LUMINANCE_ALPHA ||
3901          srcFormat == GL_INTENSITY ||
3902          srcFormat == GL_RG ||
3903          srcFormat == GL_RGB ||
3904          srcFormat == GL_BGR ||
3905          srcFormat == GL_RGBA ||
3906          srcFormat == GL_BGRA ||
3907          srcFormat == GL_ABGR_EXT ||
3908          srcFormat == GL_RED_INTEGER_EXT ||
3909          srcFormat == GL_GREEN_INTEGER_EXT ||
3910          srcFormat == GL_BLUE_INTEGER_EXT ||
3911          srcFormat == GL_ALPHA_INTEGER_EXT ||
3912          srcFormat == GL_RG_INTEGER ||
3913          srcFormat == GL_RGB_INTEGER_EXT ||
3914          srcFormat == GL_RGBA_INTEGER_EXT ||
3915          srcFormat == GL_BGR_INTEGER_EXT ||
3916          srcFormat == GL_BGRA_INTEGER_EXT ||
3917          srcFormat == GL_LUMINANCE_INTEGER_EXT ||
3918          srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT ||
3919          srcFormat == GL_COLOR_INDEX);
3920
3921   ASSERT(srcType == GL_BITMAP ||
3922          srcType == GL_UNSIGNED_BYTE ||
3923          srcType == GL_BYTE ||
3924          srcType == GL_UNSIGNED_SHORT ||
3925          srcType == GL_SHORT ||
3926          srcType == GL_UNSIGNED_INT ||
3927          srcType == GL_INT ||
3928          srcType == GL_HALF_FLOAT_ARB ||
3929          srcType == GL_FLOAT ||
3930          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3931          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3932          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3933          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3934          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3935          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3936          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3937          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3938          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3939          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3940          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3941          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
3942          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
3943          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
3944
3945   /* general solution, no special cases, yet */
3946   {
3947      GLint dstComponents;
3948      GLint rDst, gDst, bDst, aDst, lDst, iDst;
3949      GLfloat (*rgba)[4] = (GLfloat (*)[4]) malloc(4 * n * sizeof(GLfloat));
3950      GLboolean intFormat = _mesa_is_enum_format_integer(srcFormat);
3951
3952      if (!rgba) {
3953         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3954         return;
3955      }
3956
3957      dstComponents = _mesa_components_in_format( dstFormat );
3958      /* source & dest image formats should have been error checked by now */
3959      assert(dstComponents > 0);
3960
3961      /* EXT_texture_integer specifies no transfer ops on integer
3962       * types in the resolved issues section. Just set them to 0
3963       * for integer surfaces.
3964       */
3965      if (intFormat)
3966         transferOps = 0;
3967
3968      /*
3969       * Extract image data and convert to RGBA floats
3970       */
3971      if (srcFormat == GL_COLOR_INDEX) {
3972         GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
3973
3974         if (!indexes) {
3975            _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
3976            free(rgba);
3977            return;
3978         }
3979
3980         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3981                              srcPacking);
3982
3983	 /* Convert indexes to RGBA */
3984	 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3985	    _mesa_shift_and_offset_ci(ctx, n, indexes);
3986	 }
3987	 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3988
3989         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3990          * with color indexes.
3991          */
3992         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3993
3994         free(indexes);
3995      }
3996      else {
3997         /* non-color index data */
3998         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3999                            srcPacking->SwapBytes);
4000      }
4001
4002      if (transferOps) {
4003         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
4004      }
4005
4006      get_component_indexes(dstFormat,
4007                            &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
4008
4009      /* Now pack results in the requested dstFormat */
4010      if (rDst >= 0) {
4011         GLfloat *dst = dest;
4012         GLuint i;
4013         for (i = 0; i < n; i++) {
4014            dst[rDst] = rgba[i][RCOMP];
4015            dst += dstComponents;
4016         }
4017      }
4018
4019      if (gDst >= 0) {
4020         GLfloat *dst = dest;
4021         GLuint i;
4022         for (i = 0; i < n; i++) {
4023            dst[gDst] = rgba[i][GCOMP];
4024            dst += dstComponents;
4025         }
4026      }
4027
4028      if (bDst >= 0) {
4029         GLfloat *dst = dest;
4030         GLuint i;
4031         for (i = 0; i < n; i++) {
4032            dst[bDst] = rgba[i][BCOMP];
4033            dst += dstComponents;
4034         }
4035      }
4036
4037      if (aDst >= 0) {
4038         GLfloat *dst = dest;
4039         GLuint i;
4040         for (i = 0; i < n; i++) {
4041            dst[aDst] = rgba[i][ACOMP];
4042            dst += dstComponents;
4043         }
4044      }
4045
4046      if (iDst >= 0) {
4047         GLfloat *dst = dest;
4048         GLuint i;
4049         assert(iDst == 0);
4050         assert(dstComponents == 1);
4051         for (i = 0; i < n; i++) {
4052            /* Intensity comes from red channel */
4053            dst[i] = rgba[i][RCOMP];
4054         }
4055      }
4056
4057      if (lDst >= 0) {
4058         GLfloat *dst = dest;
4059         GLuint i;
4060         assert(lDst == 0);
4061         for (i = 0; i < n; i++) {
4062            /* Luminance comes from red channel */
4063            dst[0] = rgba[i][RCOMP];
4064            dst += dstComponents;
4065         }
4066      }
4067
4068      free(rgba);
4069   }
4070}
4071
4072
4073/**
4074 * Same as _mesa_unpack_color_span_ubyte(), but return GLuint data
4075 * instead of GLubyte.
4076 * No pixel transfer ops are applied.
4077 */
4078void
4079_mesa_unpack_color_span_uint(struct gl_context *ctx,
4080                             GLuint n, GLenum dstFormat, GLuint *dest,
4081                             GLenum srcFormat, GLenum srcType,
4082                             const GLvoid *source,
4083                             const struct gl_pixelstore_attrib *srcPacking)
4084{
4085   GLuint (*rgba)[4] = (GLuint (*)[4]) malloc(n * 4 * sizeof(GLfloat));
4086
4087   if (!rgba) {
4088      _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4089      return;
4090   }
4091
4092   ASSERT(dstFormat == GL_ALPHA ||
4093          dstFormat == GL_LUMINANCE ||
4094          dstFormat == GL_LUMINANCE_ALPHA ||
4095          dstFormat == GL_INTENSITY ||
4096          dstFormat == GL_RED ||
4097          dstFormat == GL_RG ||
4098          dstFormat == GL_RGB ||
4099          dstFormat == GL_RGBA);
4100
4101   ASSERT(srcFormat == GL_RED ||
4102          srcFormat == GL_GREEN ||
4103          srcFormat == GL_BLUE ||
4104          srcFormat == GL_ALPHA ||
4105          srcFormat == GL_LUMINANCE ||
4106          srcFormat == GL_LUMINANCE_ALPHA ||
4107          srcFormat == GL_INTENSITY ||
4108          srcFormat == GL_RG ||
4109          srcFormat == GL_RGB ||
4110          srcFormat == GL_BGR ||
4111          srcFormat == GL_RGBA ||
4112          srcFormat == GL_BGRA ||
4113          srcFormat == GL_ABGR_EXT ||
4114          srcFormat == GL_RED_INTEGER_EXT ||
4115          srcFormat == GL_GREEN_INTEGER_EXT ||
4116          srcFormat == GL_BLUE_INTEGER_EXT ||
4117          srcFormat == GL_ALPHA_INTEGER_EXT ||
4118          srcFormat == GL_RG_INTEGER ||
4119          srcFormat == GL_RGB_INTEGER_EXT ||
4120          srcFormat == GL_RGBA_INTEGER_EXT ||
4121          srcFormat == GL_BGR_INTEGER_EXT ||
4122          srcFormat == GL_BGRA_INTEGER_EXT ||
4123          srcFormat == GL_LUMINANCE_INTEGER_EXT ||
4124          srcFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT);
4125
4126   ASSERT(srcType == GL_UNSIGNED_BYTE ||
4127          srcType == GL_BYTE ||
4128          srcType == GL_UNSIGNED_SHORT ||
4129          srcType == GL_SHORT ||
4130          srcType == GL_UNSIGNED_INT ||
4131          srcType == GL_INT ||
4132          srcType == GL_HALF_FLOAT_ARB ||
4133          srcType == GL_FLOAT ||
4134          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
4135          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
4136          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
4137          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
4138          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
4139          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
4140          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
4141          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
4142          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
4143          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
4144          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
4145          srcType == GL_UNSIGNED_INT_2_10_10_10_REV ||
4146          srcType == GL_UNSIGNED_INT_5_9_9_9_REV ||
4147          srcType == GL_UNSIGNED_INT_10F_11F_11F_REV);
4148
4149
4150   /* Extract image data as uint[4] pixels */
4151   extract_uint_rgba(n, rgba, srcFormat, srcType, source,
4152                     srcPacking->SwapBytes);
4153
4154   if (dstFormat == GL_RGBA) {
4155      /* simple case */
4156      memcpy(dest, rgba, 4 * sizeof(GLuint) * n);
4157   }
4158   else {
4159      /* general case */
4160      GLint rDst, gDst, bDst, aDst, lDst, iDst;
4161      GLint dstComponents = _mesa_components_in_format( dstFormat );
4162
4163      assert(dstComponents > 0);
4164
4165      get_component_indexes(dstFormat,
4166                            &rDst, &gDst, &bDst, &aDst, &lDst, &iDst);
4167
4168      /* Now pack values in the requested dest format */
4169      if (rDst >= 0) {
4170         GLuint *dst = dest;
4171         GLuint i;
4172         for (i = 0; i < n; i++) {
4173            dst[rDst] = rgba[i][RCOMP];
4174            dst += dstComponents;
4175         }
4176      }
4177
4178      if (gDst >= 0) {
4179         GLuint *dst = dest;
4180         GLuint i;
4181         for (i = 0; i < n; i++) {
4182            dst[gDst] = rgba[i][GCOMP];
4183            dst += dstComponents;
4184         }
4185      }
4186
4187      if (bDst >= 0) {
4188         GLuint *dst = dest;
4189         GLuint i;
4190         for (i = 0; i < n; i++) {
4191            dst[bDst] = rgba[i][BCOMP];
4192            dst += dstComponents;
4193         }
4194      }
4195
4196      if (aDst >= 0) {
4197         GLuint *dst = dest;
4198         GLuint i;
4199         for (i = 0; i < n; i++) {
4200            dst[aDst] = rgba[i][ACOMP];
4201            dst += dstComponents;
4202         }
4203      }
4204
4205      if (iDst >= 0) {
4206         GLuint *dst = dest;
4207         GLuint i;
4208         assert(iDst == 0);
4209         assert(dstComponents == 1);
4210         for (i = 0; i < n; i++) {
4211            /* Intensity comes from red channel */
4212            dst[i] = rgba[i][RCOMP];
4213         }
4214      }
4215
4216      if (lDst >= 0) {
4217         GLuint *dst = dest;
4218         GLuint i;
4219         assert(lDst == 0);
4220         for (i = 0; i < n; i++) {
4221            /* Luminance comes from red channel */
4222            dst[0] = rgba[i][RCOMP];
4223            dst += dstComponents;
4224         }
4225      }
4226   }
4227
4228   free(rgba);
4229}
4230
4231
4232
4233/**
4234 * Similar to _mesa_unpack_color_span_float(), but for dudv data instead of rgba,
4235 * directly return GLbyte data, no transfer ops apply.
4236 */
4237void
4238_mesa_unpack_dudv_span_byte( struct gl_context *ctx,
4239                             GLuint n, GLenum dstFormat, GLbyte dest[],
4240                             GLenum srcFormat, GLenum srcType,
4241                             const GLvoid *source,
4242                             const struct gl_pixelstore_attrib *srcPacking,
4243                             GLbitfield transferOps )
4244{
4245   ASSERT(dstFormat == GL_DUDV_ATI);
4246   ASSERT(srcFormat == GL_DUDV_ATI ||
4247	  srcFormat == GL_DU8DV8_ATI);
4248
4249   ASSERT(srcType == GL_UNSIGNED_BYTE ||
4250          srcType == GL_BYTE ||
4251          srcType == GL_UNSIGNED_SHORT ||
4252          srcType == GL_SHORT ||
4253          srcType == GL_UNSIGNED_INT ||
4254          srcType == GL_INT ||
4255          srcType == GL_HALF_FLOAT_ARB ||
4256          srcType == GL_FLOAT);
4257
4258   /* general solution */
4259   {
4260      GLint dstComponents;
4261      GLbyte *dst = dest;
4262      GLuint i;
4263      GLfloat (*rgba)[4] = (GLfloat (*)[4]) malloc(4 * n * sizeof(GLfloat));
4264
4265      if (!rgba) {
4266         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4267         return;
4268      }
4269
4270      dstComponents = _mesa_components_in_format( dstFormat );
4271      /* source & dest image formats should have been error checked by now */
4272      assert(dstComponents > 0);
4273
4274      /*
4275       * Extract image data and convert to RGBA floats
4276       */
4277      extract_float_rgba(n, rgba, srcFormat, srcType, source,
4278                         srcPacking->SwapBytes);
4279
4280
4281      /* Now determine which color channels we need to produce.
4282       * And determine the dest index (offset) within each color tuple.
4283       */
4284
4285      /* Now pack results in the requested dstFormat */
4286      for (i = 0; i < n; i++) {
4287         /* not sure - need clamp[-1,1] here? */
4288         dst[0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
4289         dst[1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
4290         dst += dstComponents;
4291      }
4292
4293      free(rgba);
4294   }
4295}
4296
4297/*
4298 * Unpack a row of color index data from a client buffer according to
4299 * the pixel unpacking parameters.
4300 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
4301 *
4302 * Args:  ctx - the context
4303 *        n - number of pixels
4304 *        dstType - destination data type
4305 *        dest - destination array
4306 *        srcType - source pixel type
4307 *        source - source data pointer
4308 *        srcPacking - pixel unpacking parameters
4309 *        transferOps - the pixel transfer operations to apply
4310 */
4311void
4312_mesa_unpack_index_span( struct gl_context *ctx, GLuint n,
4313                         GLenum dstType, GLvoid *dest,
4314                         GLenum srcType, const GLvoid *source,
4315                         const struct gl_pixelstore_attrib *srcPacking,
4316                         GLbitfield transferOps )
4317{
4318   ASSERT(srcType == GL_BITMAP ||
4319          srcType == GL_UNSIGNED_BYTE ||
4320          srcType == GL_BYTE ||
4321          srcType == GL_UNSIGNED_SHORT ||
4322          srcType == GL_SHORT ||
4323          srcType == GL_UNSIGNED_INT ||
4324          srcType == GL_INT ||
4325          srcType == GL_HALF_FLOAT_ARB ||
4326          srcType == GL_FLOAT);
4327
4328   ASSERT(dstType == GL_UNSIGNED_BYTE ||
4329          dstType == GL_UNSIGNED_SHORT ||
4330          dstType == GL_UNSIGNED_INT);
4331
4332
4333   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4334
4335   /*
4336    * Try simple cases first
4337    */
4338   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
4339       && dstType == GL_UNSIGNED_BYTE) {
4340      memcpy(dest, source, n * sizeof(GLubyte));
4341   }
4342   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
4343            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
4344      memcpy(dest, source, n * sizeof(GLuint));
4345   }
4346   else {
4347      /*
4348       * general solution
4349       */
4350      GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
4351
4352      if (!indexes) {
4353         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4354         return;
4355      }
4356
4357      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
4358                           srcPacking);
4359
4360      if (transferOps)
4361         _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4362
4363      /* convert to dest type */
4364      switch (dstType) {
4365         case GL_UNSIGNED_BYTE:
4366            {
4367               GLubyte *dst = (GLubyte *) dest;
4368               GLuint i;
4369               for (i = 0; i < n; i++) {
4370                  dst[i] = (GLubyte) (indexes[i] & 0xff);
4371               }
4372            }
4373            break;
4374         case GL_UNSIGNED_SHORT:
4375            {
4376               GLuint *dst = (GLuint *) dest;
4377               GLuint i;
4378               for (i = 0; i < n; i++) {
4379                  dst[i] = (GLushort) (indexes[i] & 0xffff);
4380               }
4381            }
4382            break;
4383         case GL_UNSIGNED_INT:
4384            memcpy(dest, indexes, n * sizeof(GLuint));
4385            break;
4386         default:
4387            _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
4388      }
4389
4390      free(indexes);
4391   }
4392}
4393
4394
4395void
4396_mesa_pack_index_span( struct gl_context *ctx, GLuint n,
4397                       GLenum dstType, GLvoid *dest, const GLuint *source,
4398                       const struct gl_pixelstore_attrib *dstPacking,
4399                       GLbitfield transferOps )
4400{
4401   GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
4402
4403   if (!indexes) {
4404      _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
4405      return;
4406   }
4407
4408   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4409
4410   if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
4411      /* make a copy of input */
4412      memcpy(indexes, source, n * sizeof(GLuint));
4413      _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4414      source = indexes;
4415   }
4416
4417   switch (dstType) {
4418   case GL_UNSIGNED_BYTE:
4419      {
4420         GLubyte *dst = (GLubyte *) dest;
4421         GLuint i;
4422         for (i = 0; i < n; i++) {
4423            *dst++ = (GLubyte) source[i];
4424         }
4425      }
4426      break;
4427   case GL_BYTE:
4428      {
4429         GLbyte *dst = (GLbyte *) dest;
4430         GLuint i;
4431         for (i = 0; i < n; i++) {
4432            dst[i] = (GLbyte) source[i];
4433         }
4434      }
4435      break;
4436   case GL_UNSIGNED_SHORT:
4437      {
4438         GLushort *dst = (GLushort *) dest;
4439         GLuint i;
4440         for (i = 0; i < n; i++) {
4441            dst[i] = (GLushort) source[i];
4442         }
4443         if (dstPacking->SwapBytes) {
4444            _mesa_swap2( (GLushort *) dst, n );
4445         }
4446      }
4447      break;
4448   case GL_SHORT:
4449      {
4450         GLshort *dst = (GLshort *) dest;
4451         GLuint i;
4452         for (i = 0; i < n; i++) {
4453            dst[i] = (GLshort) source[i];
4454         }
4455         if (dstPacking->SwapBytes) {
4456            _mesa_swap2( (GLushort *) dst, n );
4457         }
4458      }
4459      break;
4460   case GL_UNSIGNED_INT:
4461      {
4462         GLuint *dst = (GLuint *) dest;
4463         GLuint i;
4464         for (i = 0; i < n; i++) {
4465            dst[i] = (GLuint) source[i];
4466         }
4467         if (dstPacking->SwapBytes) {
4468            _mesa_swap4( (GLuint *) dst, n );
4469         }
4470      }
4471      break;
4472   case GL_INT:
4473      {
4474         GLint *dst = (GLint *) dest;
4475         GLuint i;
4476         for (i = 0; i < n; i++) {
4477            dst[i] = (GLint) source[i];
4478         }
4479         if (dstPacking->SwapBytes) {
4480            _mesa_swap4( (GLuint *) dst, n );
4481         }
4482      }
4483      break;
4484   case GL_FLOAT:
4485      {
4486         GLfloat *dst = (GLfloat *) dest;
4487         GLuint i;
4488         for (i = 0; i < n; i++) {
4489            dst[i] = (GLfloat) source[i];
4490         }
4491         if (dstPacking->SwapBytes) {
4492            _mesa_swap4( (GLuint *) dst, n );
4493         }
4494      }
4495      break;
4496   case GL_HALF_FLOAT_ARB:
4497      {
4498         GLhalfARB *dst = (GLhalfARB *) dest;
4499         GLuint i;
4500         for (i = 0; i < n; i++) {
4501            dst[i] = _mesa_float_to_half((GLfloat) source[i]);
4502         }
4503         if (dstPacking->SwapBytes) {
4504            _mesa_swap2( (GLushort *) dst, n );
4505         }
4506      }
4507      break;
4508   default:
4509      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4510   }
4511
4512   free(indexes);
4513}
4514
4515
4516/*
4517 * Unpack a row of stencil data from a client buffer according to
4518 * the pixel unpacking parameters.
4519 * This is (or will be) used by glDrawPixels
4520 *
4521 * Args:  ctx - the context
4522 *        n - number of pixels
4523 *        dstType - destination data type
4524 *        dest - destination array
4525 *        srcType - source pixel type
4526 *        source - source data pointer
4527 *        srcPacking - pixel unpacking parameters
4528 *        transferOps - apply offset/bias/lookup ops?
4529 */
4530void
4531_mesa_unpack_stencil_span( struct gl_context *ctx, GLuint n,
4532                           GLenum dstType, GLvoid *dest,
4533                           GLenum srcType, const GLvoid *source,
4534                           const struct gl_pixelstore_attrib *srcPacking,
4535                           GLbitfield transferOps )
4536{
4537   ASSERT(srcType == GL_BITMAP ||
4538          srcType == GL_UNSIGNED_BYTE ||
4539          srcType == GL_BYTE ||
4540          srcType == GL_UNSIGNED_SHORT ||
4541          srcType == GL_SHORT ||
4542          srcType == GL_UNSIGNED_INT ||
4543          srcType == GL_INT ||
4544          srcType == GL_UNSIGNED_INT_24_8_EXT ||
4545          srcType == GL_HALF_FLOAT_ARB ||
4546          srcType == GL_FLOAT ||
4547          srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
4548
4549   ASSERT(dstType == GL_UNSIGNED_BYTE ||
4550          dstType == GL_UNSIGNED_SHORT ||
4551          dstType == GL_UNSIGNED_INT ||
4552          dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
4553
4554   /* only shift and offset apply to stencil */
4555   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
4556
4557   /*
4558    * Try simple cases first
4559    */
4560   if (transferOps == 0 &&
4561       !ctx->Pixel.MapStencilFlag &&
4562       srcType == GL_UNSIGNED_BYTE &&
4563       dstType == GL_UNSIGNED_BYTE) {
4564      memcpy(dest, source, n * sizeof(GLubyte));
4565   }
4566   else if (transferOps == 0 &&
4567            !ctx->Pixel.MapStencilFlag &&
4568            srcType == GL_UNSIGNED_INT &&
4569            dstType == GL_UNSIGNED_INT &&
4570            !srcPacking->SwapBytes) {
4571      memcpy(dest, source, n * sizeof(GLuint));
4572   }
4573   else {
4574      /*
4575       * general solution
4576       */
4577      GLuint *indexes = (GLuint *) malloc(n * sizeof(GLuint));
4578
4579      if (!indexes) {
4580         _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil unpacking");
4581         return;
4582      }
4583
4584      extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
4585                           srcPacking);
4586
4587      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4588         /* shift and offset indexes */
4589         _mesa_shift_and_offset_ci(ctx, n, indexes);
4590      }
4591
4592      if (ctx->Pixel.MapStencilFlag) {
4593         /* Apply stencil lookup table */
4594         const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
4595         GLuint i;
4596         for (i = 0; i < n; i++) {
4597            indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
4598         }
4599      }
4600
4601      /* convert to dest type */
4602      switch (dstType) {
4603         case GL_UNSIGNED_BYTE:
4604            {
4605               GLubyte *dst = (GLubyte *) dest;
4606               GLuint i;
4607               for (i = 0; i < n; i++) {
4608                  dst[i] = (GLubyte) (indexes[i] & 0xff);
4609               }
4610            }
4611            break;
4612         case GL_UNSIGNED_SHORT:
4613            {
4614               GLuint *dst = (GLuint *) dest;
4615               GLuint i;
4616               for (i = 0; i < n; i++) {
4617                  dst[i] = (GLushort) (indexes[i] & 0xffff);
4618               }
4619            }
4620            break;
4621         case GL_UNSIGNED_INT:
4622            memcpy(dest, indexes, n * sizeof(GLuint));
4623            break;
4624         case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
4625            {
4626               GLuint *dst = (GLuint *) dest;
4627               GLuint i;
4628               for (i = 0; i < n; i++) {
4629                  dst[i*2+1] = indexes[i] & 0xff; /* lower 8 bits */
4630               }
4631            }
4632            break;
4633         default:
4634            _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
4635      }
4636
4637      free(indexes);
4638   }
4639}
4640
4641
4642void
4643_mesa_pack_stencil_span( struct gl_context *ctx, GLuint n,
4644                         GLenum dstType, GLvoid *dest, const GLubyte *source,
4645                         const struct gl_pixelstore_attrib *dstPacking )
4646{
4647   GLubyte *stencil = (GLubyte *) malloc(n * sizeof(GLubyte));
4648
4649   if (!stencil) {
4650      _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil packing");
4651      return;
4652   }
4653
4654   if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
4655       ctx->Pixel.MapStencilFlag) {
4656      /* make a copy of input */
4657      memcpy(stencil, source, n * sizeof(GLubyte));
4658      _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
4659      source = stencil;
4660   }
4661
4662   switch (dstType) {
4663   case GL_UNSIGNED_BYTE:
4664      memcpy(dest, source, n);
4665      break;
4666   case GL_BYTE:
4667      {
4668         GLbyte *dst = (GLbyte *) dest;
4669         GLuint i;
4670         for (i=0;i<n;i++) {
4671            dst[i] = (GLbyte) (source[i] & 0x7f);
4672         }
4673      }
4674      break;
4675   case GL_UNSIGNED_SHORT:
4676      {
4677         GLushort *dst = (GLushort *) dest;
4678         GLuint i;
4679         for (i=0;i<n;i++) {
4680            dst[i] = (GLushort) source[i];
4681         }
4682         if (dstPacking->SwapBytes) {
4683            _mesa_swap2( (GLushort *) dst, n );
4684         }
4685      }
4686      break;
4687   case GL_SHORT:
4688      {
4689         GLshort *dst = (GLshort *) dest;
4690         GLuint i;
4691         for (i=0;i<n;i++) {
4692            dst[i] = (GLshort) source[i];
4693         }
4694         if (dstPacking->SwapBytes) {
4695            _mesa_swap2( (GLushort *) dst, n );
4696         }
4697      }
4698      break;
4699   case GL_UNSIGNED_INT:
4700      {
4701         GLuint *dst = (GLuint *) dest;
4702         GLuint i;
4703         for (i=0;i<n;i++) {
4704            dst[i] = (GLuint) source[i];
4705         }
4706         if (dstPacking->SwapBytes) {
4707            _mesa_swap4( (GLuint *) dst, n );
4708         }
4709      }
4710      break;
4711   case GL_INT:
4712      {
4713         GLint *dst = (GLint *) dest;
4714         GLuint i;
4715         for (i=0;i<n;i++) {
4716            dst[i] = (GLint) source[i];
4717         }
4718         if (dstPacking->SwapBytes) {
4719            _mesa_swap4( (GLuint *) dst, n );
4720         }
4721      }
4722      break;
4723   case GL_FLOAT:
4724      {
4725         GLfloat *dst = (GLfloat *) dest;
4726         GLuint i;
4727         for (i=0;i<n;i++) {
4728            dst[i] = (GLfloat) source[i];
4729         }
4730         if (dstPacking->SwapBytes) {
4731            _mesa_swap4( (GLuint *) dst, n );
4732         }
4733      }
4734      break;
4735   case GL_HALF_FLOAT_ARB:
4736      {
4737         GLhalfARB *dst = (GLhalfARB *) dest;
4738         GLuint i;
4739         for (i=0;i<n;i++) {
4740            dst[i] = _mesa_float_to_half( (float) source[i] );
4741         }
4742         if (dstPacking->SwapBytes) {
4743            _mesa_swap2( (GLushort *) dst, n );
4744         }
4745      }
4746      break;
4747   case GL_BITMAP:
4748      if (dstPacking->LsbFirst) {
4749         GLubyte *dst = (GLubyte *) dest;
4750         GLint shift = 0;
4751         GLuint i;
4752         for (i = 0; i < n; i++) {
4753            if (shift == 0)
4754               *dst = 0;
4755            *dst |= ((source[i] != 0) << shift);
4756            shift++;
4757            if (shift == 8) {
4758               shift = 0;
4759               dst++;
4760            }
4761         }
4762      }
4763      else {
4764         GLubyte *dst = (GLubyte *) dest;
4765         GLint shift = 7;
4766         GLuint i;
4767         for (i = 0; i < n; i++) {
4768            if (shift == 7)
4769               *dst = 0;
4770            *dst |= ((source[i] != 0) << shift);
4771            shift--;
4772            if (shift < 0) {
4773               shift = 7;
4774               dst++;
4775            }
4776         }
4777      }
4778      break;
4779   default:
4780      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4781   }
4782
4783   free(stencil);
4784}
4785
4786#define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT)                              \
4787    do {                                                                \
4788        GLuint i;                                                       \
4789        const GLTYPE *src = (const GLTYPE *)source;                     \
4790        for (i = 0; i < n; i++) {                                       \
4791            GLTYPE value = src[i];                                      \
4792            if (srcPacking->SwapBytes) {                                \
4793                if (sizeof(GLTYPE) == 2) {                              \
4794                    SWAP2BYTE(value);                                   \
4795                } else if (sizeof(GLTYPE) == 4) {                       \
4796                    SWAP4BYTE(value);                                   \
4797                }                                                       \
4798            }                                                           \
4799            depthValues[i] = GLTYPE2FLOAT(value);                       \
4800        }                                                               \
4801    } while (0)
4802
4803
4804/**
4805 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
4806 * or GLfloat values.
4807 * The glPixelTransfer (scale/bias) params will be applied.
4808 *
4809 * \param dstType  one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
4810 * \param depthMax  max value for returned GLushort or GLuint values
4811 *                  (ignored for GLfloat).
4812 */
4813void
4814_mesa_unpack_depth_span( struct gl_context *ctx, GLuint n,
4815                         GLenum dstType, GLvoid *dest, GLuint depthMax,
4816                         GLenum srcType, const GLvoid *source,
4817                         const struct gl_pixelstore_attrib *srcPacking )
4818{
4819   GLfloat *depthTemp = NULL, *depthValues;
4820   GLboolean needClamp = GL_FALSE;
4821
4822   /* Look for special cases first.
4823    * Not only are these faster, they're less prone to numeric conversion
4824    * problems.  Otherwise, converting from an int type to a float then
4825    * back to an int type can introduce errors that will show up as
4826    * artifacts in things like depth peeling which uses glCopyTexImage.
4827    */
4828   if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
4829      if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
4830         const GLuint *src = (const GLuint *) source;
4831         GLushort *dst = (GLushort *) dest;
4832         GLuint i;
4833         for (i = 0; i < n; i++) {
4834            dst[i] = src[i] >> 16;
4835         }
4836         return;
4837      }
4838      if (srcType == GL_UNSIGNED_SHORT
4839          && dstType == GL_UNSIGNED_INT
4840          && depthMax == 0xffffffff) {
4841         const GLushort *src = (const GLushort *) source;
4842         GLuint *dst = (GLuint *) dest;
4843         GLuint i;
4844         for (i = 0; i < n; i++) {
4845            dst[i] = src[i] | (src[i] << 16);
4846         }
4847         return;
4848      }
4849      if (srcType == GL_UNSIGNED_INT_24_8
4850          && dstType == GL_UNSIGNED_INT
4851          && depthMax == 0xffffff) {
4852         const GLuint *src = (const GLuint *) source;
4853         GLuint *dst = (GLuint *) dest;
4854         GLuint i;
4855         for (i = 0; i < n; i++) {
4856            dst[i] = src[i] >> 8;
4857         }
4858         return;
4859      }
4860      /* XXX may want to add additional cases here someday */
4861   }
4862
4863   /* general case path follows */
4864
4865   if (dstType == GL_FLOAT) {
4866      depthValues = (GLfloat *) dest;
4867   }
4868   else {
4869      depthTemp = (GLfloat *) malloc(n * sizeof(GLfloat));
4870      if (!depthTemp) {
4871         _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
4872         return;
4873      }
4874
4875      depthValues = depthTemp;
4876   }
4877
4878   /* Convert incoming values to GLfloat.  Some conversions will require
4879    * clamping, below.
4880    */
4881   switch (srcType) {
4882      case GL_BYTE:
4883         DEPTH_VALUES(GLbyte, BYTE_TO_FLOATZ);
4884         needClamp = GL_TRUE;
4885         break;
4886      case GL_UNSIGNED_BYTE:
4887         DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
4888         break;
4889      case GL_SHORT:
4890         DEPTH_VALUES(GLshort, SHORT_TO_FLOATZ);
4891         needClamp = GL_TRUE;
4892         break;
4893      case GL_UNSIGNED_SHORT:
4894         DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
4895         break;
4896      case GL_INT:
4897         DEPTH_VALUES(GLint, INT_TO_FLOAT);
4898         needClamp = GL_TRUE;
4899         break;
4900      case GL_UNSIGNED_INT:
4901         DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
4902         break;
4903      case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
4904         if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
4905             depthMax == 0xffffff &&
4906             ctx->Pixel.DepthScale == 1.0 &&
4907             ctx->Pixel.DepthBias == 0.0) {
4908            const GLuint *src = (const GLuint *) source;
4909            GLuint *zValues = (GLuint *) dest;
4910            GLuint i;
4911            for (i = 0; i < n; i++) {
4912                GLuint value = src[i];
4913                if (srcPacking->SwapBytes) {
4914                    SWAP4BYTE(value);
4915                }
4916                zValues[i] = value & 0xffffff00;
4917            }
4918            free(depthTemp);
4919            return;
4920         }
4921         else {
4922            const GLuint *src = (const GLuint *) source;
4923            const GLfloat scale = 1.0f / 0xffffff;
4924            GLuint i;
4925            for (i = 0; i < n; i++) {
4926                GLuint value = src[i];
4927                if (srcPacking->SwapBytes) {
4928                    SWAP4BYTE(value);
4929                }
4930                depthValues[i] = (value >> 8) * scale;
4931            }
4932         }
4933         break;
4934      case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
4935         {
4936            GLuint i;
4937            const GLfloat *src = (const GLfloat *)source;
4938            for (i = 0; i < n; i++) {
4939               GLfloat value = src[i * 2];
4940               if (srcPacking->SwapBytes) {
4941                  SWAP4BYTE(value);
4942               }
4943               depthValues[i] = value;
4944            }
4945            needClamp = GL_TRUE;
4946         }
4947         break;
4948      case GL_FLOAT:
4949         DEPTH_VALUES(GLfloat, 1*);
4950         needClamp = GL_TRUE;
4951         break;
4952      case GL_HALF_FLOAT_ARB:
4953         {
4954            GLuint i;
4955            const GLhalfARB *src = (const GLhalfARB *) source;
4956            for (i = 0; i < n; i++) {
4957               GLhalfARB value = src[i];
4958               if (srcPacking->SwapBytes) {
4959                  SWAP2BYTE(value);
4960               }
4961               depthValues[i] = _mesa_half_to_float(value);
4962            }
4963            needClamp = GL_TRUE;
4964         }
4965         break;
4966      default:
4967         _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
4968         free(depthTemp);
4969         return;
4970   }
4971
4972   /* apply depth scale and bias */
4973   {
4974      const GLfloat scale = ctx->Pixel.DepthScale;
4975      const GLfloat bias = ctx->Pixel.DepthBias;
4976      if (scale != 1.0 || bias != 0.0) {
4977         GLuint i;
4978         for (i = 0; i < n; i++) {
4979            depthValues[i] = depthValues[i] * scale + bias;
4980         }
4981         needClamp = GL_TRUE;
4982      }
4983   }
4984
4985   /* clamp to [0, 1] */
4986   if (needClamp) {
4987      GLuint i;
4988      for (i = 0; i < n; i++) {
4989         depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
4990      }
4991   }
4992
4993   /*
4994    * Convert values to dstType
4995    */
4996   if (dstType == GL_UNSIGNED_INT) {
4997      GLuint *zValues = (GLuint *) dest;
4998      GLuint i;
4999      if (depthMax <= 0xffffff) {
5000         /* no overflow worries */
5001         for (i = 0; i < n; i++) {
5002            zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
5003         }
5004      }
5005      else {
5006         /* need to use double precision to prevent overflow problems */
5007         for (i = 0; i < n; i++) {
5008            GLdouble z = depthValues[i] * (GLdouble) depthMax;
5009            if (z >= (GLdouble) 0xffffffff)
5010               zValues[i] = 0xffffffff;
5011            else
5012               zValues[i] = (GLuint) z;
5013         }
5014      }
5015   }
5016   else if (dstType == GL_UNSIGNED_SHORT) {
5017      GLushort *zValues = (GLushort *) dest;
5018      GLuint i;
5019      ASSERT(depthMax <= 0xffff);
5020      for (i = 0; i < n; i++) {
5021         zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
5022      }
5023   }
5024   else if (dstType == GL_FLOAT) {
5025      /* Nothing to do. depthValues is pointing to dest. */
5026   }
5027   else if (dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) {
5028      GLfloat *zValues = (GLfloat*) dest;
5029      GLuint i;
5030      for (i = 0; i < n; i++) {
5031         zValues[i*2] = depthValues[i];
5032      }
5033   }
5034   else {
5035      ASSERT(0);
5036   }
5037
5038   free(depthTemp);
5039}
5040
5041
5042/*
5043 * Pack an array of depth values.  The values are floats in [0,1].
5044 */
5045void
5046_mesa_pack_depth_span( struct gl_context *ctx, GLuint n, GLvoid *dest,
5047                       GLenum dstType, const GLfloat *depthSpan,
5048                       const struct gl_pixelstore_attrib *dstPacking )
5049{
5050   GLfloat *depthCopy = (GLfloat *) malloc(n * sizeof(GLfloat));
5051   if (!depthCopy) {
5052      _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
5053      return;
5054   }
5055
5056   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
5057      memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
5058      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
5059      depthSpan = depthCopy;
5060   }
5061
5062   switch (dstType) {
5063   case GL_UNSIGNED_BYTE:
5064      {
5065         GLubyte *dst = (GLubyte *) dest;
5066         GLuint i;
5067         for (i = 0; i < n; i++) {
5068            dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
5069         }
5070      }
5071      break;
5072   case GL_BYTE:
5073      {
5074         GLbyte *dst = (GLbyte *) dest;
5075         GLuint i;
5076         for (i = 0; i < n; i++) {
5077            dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
5078         }
5079      }
5080      break;
5081   case GL_UNSIGNED_SHORT:
5082      {
5083         GLushort *dst = (GLushort *) dest;
5084         GLuint i;
5085         for (i = 0; i < n; i++) {
5086            CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
5087         }
5088         if (dstPacking->SwapBytes) {
5089            _mesa_swap2( (GLushort *) dst, n );
5090         }
5091      }
5092      break;
5093   case GL_SHORT:
5094      {
5095         GLshort *dst = (GLshort *) dest;
5096         GLuint i;
5097         for (i = 0; i < n; i++) {
5098            dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
5099         }
5100         if (dstPacking->SwapBytes) {
5101            _mesa_swap2( (GLushort *) dst, n );
5102         }
5103      }
5104      break;
5105   case GL_UNSIGNED_INT:
5106      {
5107         GLuint *dst = (GLuint *) dest;
5108         GLuint i;
5109         for (i = 0; i < n; i++) {
5110            dst[i] = FLOAT_TO_UINT( depthSpan[i] );
5111         }
5112         if (dstPacking->SwapBytes) {
5113            _mesa_swap4( (GLuint *) dst, n );
5114         }
5115      }
5116      break;
5117   case GL_INT:
5118      {
5119         GLint *dst = (GLint *) dest;
5120         GLuint i;
5121         for (i = 0; i < n; i++) {
5122            dst[i] = FLOAT_TO_INT( depthSpan[i] );
5123         }
5124         if (dstPacking->SwapBytes) {
5125            _mesa_swap4( (GLuint *) dst, n );
5126         }
5127      }
5128      break;
5129   case GL_FLOAT:
5130      {
5131         GLfloat *dst = (GLfloat *) dest;
5132         GLuint i;
5133         for (i = 0; i < n; i++) {
5134            dst[i] = depthSpan[i];
5135         }
5136         if (dstPacking->SwapBytes) {
5137            _mesa_swap4( (GLuint *) dst, n );
5138         }
5139      }
5140      break;
5141   case GL_HALF_FLOAT_ARB:
5142      {
5143         GLhalfARB *dst = (GLhalfARB *) dest;
5144         GLuint i;
5145         for (i = 0; i < n; i++) {
5146            dst[i] = _mesa_float_to_half(depthSpan[i]);
5147         }
5148         if (dstPacking->SwapBytes) {
5149            _mesa_swap2( (GLushort *) dst, n );
5150         }
5151      }
5152      break;
5153   default:
5154      _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
5155   }
5156
5157   free(depthCopy);
5158}
5159
5160
5161
5162/**
5163 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
5164 */
5165void
5166_mesa_pack_depth_stencil_span(struct gl_context *ctx,GLuint n,
5167                              GLenum dstType, GLuint *dest,
5168                              const GLfloat *depthVals,
5169                              const GLubyte *stencilVals,
5170                              const struct gl_pixelstore_attrib *dstPacking)
5171{
5172   GLfloat *depthCopy = (GLfloat *) malloc(n * sizeof(GLfloat));
5173   GLubyte *stencilCopy = (GLubyte *) malloc(n * sizeof(GLubyte));
5174   GLuint i;
5175
5176   if (!depthCopy || !stencilCopy) {
5177      _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
5178      free(depthCopy);
5179      free(stencilCopy);
5180      return;
5181   }
5182
5183   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
5184      memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
5185      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
5186      depthVals = depthCopy;
5187   }
5188
5189   if (ctx->Pixel.IndexShift ||
5190       ctx->Pixel.IndexOffset ||
5191       ctx->Pixel.MapStencilFlag) {
5192      memcpy(stencilCopy, stencilVals, n * sizeof(GLubyte));
5193      _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
5194      stencilVals = stencilCopy;
5195   }
5196
5197   switch (dstType) {
5198   case GL_UNSIGNED_INT_24_8:
5199      for (i = 0; i < n; i++) {
5200         GLuint z = (GLuint) (depthVals[i] * 0xffffff);
5201         dest[i] = (z << 8) | (stencilVals[i] & 0xff);
5202      }
5203      break;
5204   case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
5205      for (i = 0; i < n; i++) {
5206         ((GLfloat*)dest)[i*2] = depthVals[i];
5207         dest[i*2+1] = stencilVals[i] & 0xff;
5208      }
5209      break;
5210   }
5211
5212   if (dstPacking->SwapBytes) {
5213      _mesa_swap4(dest, n);
5214   }
5215
5216   free(depthCopy);
5217   free(stencilCopy);
5218}
5219
5220
5221
5222
5223/**
5224 * Unpack image data.  Apply byte swapping, byte flipping (bitmap).
5225 * Return all image data in a contiguous block.  This is used when we
5226 * compile glDrawPixels, glTexImage, etc into a display list.  We
5227 * need a copy of the data in a standard format.
5228 */
5229void *
5230_mesa_unpack_image( GLuint dimensions,
5231                    GLsizei width, GLsizei height, GLsizei depth,
5232                    GLenum format, GLenum type, const GLvoid *pixels,
5233                    const struct gl_pixelstore_attrib *unpack )
5234{
5235   GLint bytesPerRow, compsPerRow;
5236   GLboolean flipBytes, swap2, swap4;
5237
5238   if (!pixels)
5239      return NULL;  /* not necessarily an error */
5240
5241   if (width <= 0 || height <= 0 || depth <= 0)
5242      return NULL;  /* generate error later */
5243
5244   if (type == GL_BITMAP) {
5245      bytesPerRow = (width + 7) >> 3;
5246      flipBytes = unpack->LsbFirst;
5247      swap2 = swap4 = GL_FALSE;
5248      compsPerRow = 0;
5249   }
5250   else {
5251      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
5252      GLint components = _mesa_components_in_format(format);
5253      GLint bytesPerComp;
5254
5255      if (_mesa_type_is_packed(type))
5256          components = 1;
5257
5258      if (bytesPerPixel <= 0 || components <= 0)
5259         return NULL;   /* bad format or type.  generate error later */
5260      bytesPerRow = bytesPerPixel * width;
5261      bytesPerComp = bytesPerPixel / components;
5262      flipBytes = GL_FALSE;
5263      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
5264      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
5265      compsPerRow = components * width;
5266      assert(compsPerRow >= width);
5267   }
5268
5269   {
5270      GLubyte *destBuffer
5271         = (GLubyte *) malloc(bytesPerRow * height * depth);
5272      GLubyte *dst;
5273      GLint img, row;
5274      if (!destBuffer)
5275         return NULL;   /* generate GL_OUT_OF_MEMORY later */
5276
5277      dst = destBuffer;
5278      for (img = 0; img < depth; img++) {
5279         for (row = 0; row < height; row++) {
5280            const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
5281                               width, height, format, type, img, row, 0);
5282
5283            if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
5284               GLint i;
5285               flipBytes = GL_FALSE;
5286               if (unpack->LsbFirst) {
5287                  GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
5288                  GLubyte dstMask = 128;
5289                  const GLubyte *s = src;
5290                  GLubyte *d = dst;
5291                  *d = 0;
5292                  for (i = 0; i < width; i++) {
5293                     if (*s & srcMask) {
5294                        *d |= dstMask;
5295                     }
5296                     if (srcMask == 128) {
5297                        srcMask = 1;
5298                        s++;
5299                     }
5300                     else {
5301                        srcMask = srcMask << 1;
5302                     }
5303                     if (dstMask == 1) {
5304                        dstMask = 128;
5305                        d++;
5306                        *d = 0;
5307                     }
5308                     else {
5309                        dstMask = dstMask >> 1;
5310                     }
5311                  }
5312               }
5313               else {
5314                  GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
5315                  GLubyte dstMask = 128;
5316                  const GLubyte *s = src;
5317                  GLubyte *d = dst;
5318                  *d = 0;
5319                  for (i = 0; i < width; i++) {
5320                     if (*s & srcMask) {
5321                        *d |= dstMask;
5322                     }
5323                     if (srcMask == 1) {
5324                        srcMask = 128;
5325                        s++;
5326                     }
5327                     else {
5328                        srcMask = srcMask >> 1;
5329                     }
5330                     if (dstMask == 1) {
5331                        dstMask = 128;
5332                        d++;
5333                        *d = 0;
5334                     }
5335                     else {
5336                        dstMask = dstMask >> 1;
5337                     }
5338                  }
5339               }
5340            }
5341            else {
5342               memcpy(dst, src, bytesPerRow);
5343            }
5344
5345            /* byte flipping/swapping */
5346            if (flipBytes) {
5347               flip_bytes((GLubyte *) dst, bytesPerRow);
5348            }
5349            else if (swap2) {
5350               _mesa_swap2((GLushort*) dst, compsPerRow);
5351            }
5352            else if (swap4) {
5353               _mesa_swap4((GLuint*) dst, compsPerRow);
5354            }
5355            dst += bytesPerRow;
5356         }
5357      }
5358      return destBuffer;
5359   }
5360}
5361
5362
5363
5364/**
5365 * If we unpack colors from a luminance surface, we'll get pixel colors
5366 * such as (l, l, l, a).
5367 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
5368 * function will compute L=R+G+B before packing.  The net effect is we'll
5369 * accidentally store luminance values = 3*l.
5370 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
5371 * to be (l,0,0,a).
5372 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
5373 * and INTENSITY.
5374 *
5375 * Finally, we also need to do this when the actual surface format does
5376 * not match the logical surface format.  For example, suppose the user
5377 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
5378 * Again, we'll get pixel values like (l,l,l,a).
5379 */
5380void
5381_mesa_rebase_rgba_float(GLuint n, GLfloat rgba[][4], GLenum baseFormat)
5382{
5383   GLuint i;
5384
5385   switch (baseFormat) {
5386   case GL_ALPHA:
5387      for (i = 0; i < n; i++) {
5388         rgba[i][RCOMP] = 0.0F;
5389         rgba[i][GCOMP] = 0.0F;
5390         rgba[i][BCOMP] = 0.0F;
5391      }
5392      break;
5393   case GL_INTENSITY:
5394      /* fall-through */
5395   case GL_LUMINANCE:
5396      for (i = 0; i < n; i++) {
5397         rgba[i][GCOMP] = 0.0F;
5398         rgba[i][BCOMP] = 0.0F;
5399         rgba[i][ACOMP] = 1.0F;
5400      }
5401      break;
5402   case GL_LUMINANCE_ALPHA:
5403      for (i = 0; i < n; i++) {
5404         rgba[i][GCOMP] = 0.0F;
5405         rgba[i][BCOMP] = 0.0F;
5406      }
5407      break;
5408   default:
5409      /* no-op */
5410      ;
5411   }
5412}
5413
5414
5415/**
5416 * As above, but GLuint components.
5417 */
5418void
5419_mesa_rebase_rgba_uint(GLuint n, GLuint rgba[][4], GLenum baseFormat)
5420{
5421   GLuint i;
5422
5423   switch (baseFormat) {
5424   case GL_ALPHA:
5425      for (i = 0; i < n; i++) {
5426         rgba[i][RCOMP] = 0;
5427         rgba[i][GCOMP] = 0;
5428         rgba[i][BCOMP] = 0;
5429      }
5430      break;
5431   case GL_INTENSITY:
5432      /* fall-through */
5433   case GL_LUMINANCE:
5434      for (i = 0; i < n; i++) {
5435         rgba[i][GCOMP] = 0;
5436         rgba[i][BCOMP] = 0;
5437         rgba[i][ACOMP] = 1;
5438      }
5439      break;
5440   case GL_LUMINANCE_ALPHA:
5441      for (i = 0; i < n; i++) {
5442         rgba[i][GCOMP] = 0;
5443         rgba[i][BCOMP] = 0;
5444      }
5445      break;
5446   default:
5447      /* no-op */
5448      ;
5449   }
5450}
5451
5452
5453