home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / src / image.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  124KB  |  3,659 lines

  1. /* $Id: image.c,v 1.13 1999/11/08 07:36:44 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.1
  6.  * 
  7.  * Copyright (C) 1999  Brian Paul   All Rights Reserved.
  8.  * 
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  * 
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  * 
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27.  
  28.  
  29. #ifdef PC_HEADER
  30. #include "all.h"
  31. #else
  32. #ifndef XFree86Server
  33. #include <assert.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #else
  37. #include "GL/xf86glx.h"
  38. #endif
  39. #include "context.h"
  40. #include "image.h"
  41. #include "macros.h"
  42. #include "mmath.h"
  43. #include "pixel.h"
  44. #include "types.h"
  45. #endif
  46.  
  47.  
  48.  
  49. /*
  50.  * Flip the 8 bits in each byte of the given array.
  51.  */
  52. void gl_flip_bytes( GLubyte *p, GLuint n )
  53. {
  54.    register GLuint i, a, b;
  55.  
  56.    for (i=0;i<n;i++) {
  57.       b = (GLuint) p[i];
  58.       a = ((b & 0x01) << 7) |
  59.       ((b & 0x02) << 5) |
  60.       ((b & 0x04) << 3) |
  61.       ((b & 0x08) << 1) |
  62.       ((b & 0x10) >> 1) |
  63.       ((b & 0x20) >> 3) |
  64.       ((b & 0x40) >> 5) |
  65.       ((b & 0x80) >> 7);
  66.       p[i] = (GLubyte) a;
  67.    }
  68. }
  69.  
  70.  
  71. /*
  72.  * Flip the order of the 2 bytes in each word in the given array.
  73.  */
  74. void gl_swap2( GLushort *p, GLuint n )
  75. {
  76.    register GLuint i;
  77.  
  78.    for (i=0;i<n;i++) {
  79.       p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
  80.    }
  81. }
  82.  
  83.  
  84.  
  85. /*
  86.  * Flip the order of the 4 bytes in each word in the given array.
  87.  */
  88. void gl_swap4( GLuint *p, GLuint n )
  89. {
  90.    register GLuint i, a, b;
  91.  
  92.    for (i=0;i<n;i++) {
  93.       b = p[i];
  94.       a =  (b >> 24)
  95.     | ((b >> 8) & 0xff00)
  96.     | ((b << 8) & 0xff0000)
  97.     | ((b << 24) & 0xff000000);
  98.       p[i] = a;
  99.    }
  100. }
  101.  
  102.  
  103.  
  104.  
  105. /*
  106.  * Return the size, in bytes, of the given GL datatype.
  107.  * Return 0 if GL_BITMAP.
  108.  * Return -1 if invalid type enum.
  109.  */
  110. GLint gl_sizeof_type( GLenum type )
  111. {
  112.    switch (type) {
  113.       case GL_BITMAP:
  114.      return 0;
  115.       case GL_UNSIGNED_BYTE:
  116.          return sizeof(GLubyte);
  117.       case GL_BYTE:
  118.      return sizeof(GLbyte);
  119.       case GL_UNSIGNED_SHORT:
  120.      return sizeof(GLushort);
  121.       case GL_SHORT:
  122.      return sizeof(GLshort);
  123.       case GL_UNSIGNED_INT:
  124.      return sizeof(GLuint);
  125.       case GL_INT:
  126.      return sizeof(GLint);
  127.       case GL_FLOAT:
  128.      return sizeof(GLfloat);
  129.       default:
  130.          return -1;
  131.    }
  132. }
  133.  
  134.  
  135. /*
  136.  * Same as gl_sizeof_packed_type() but we also accept the
  137.  * packed pixel format datatypes.
  138.  */
  139. GLint gl_sizeof_packed_type( GLenum type )
  140. {
  141.    switch (type) {
  142.       case GL_BITMAP:
  143.      return 0;
  144.       case GL_UNSIGNED_BYTE:
  145.          return sizeof(GLubyte);
  146.       case GL_BYTE:
  147.      return sizeof(GLbyte);
  148.       case GL_UNSIGNED_SHORT:
  149.      return sizeof(GLushort);
  150.       case GL_SHORT:
  151.      return sizeof(GLshort);
  152.       case GL_UNSIGNED_INT:
  153.      return sizeof(GLuint);
  154.       case GL_INT:
  155.      return sizeof(GLint);
  156.       case GL_FLOAT:
  157.      return sizeof(GLfloat);
  158.       case GL_UNSIGNED_BYTE_3_3_2:
  159.          return sizeof(GLubyte);
  160.       case GL_UNSIGNED_BYTE_2_3_3_REV:
  161.          return sizeof(GLubyte);
  162.       case GL_UNSIGNED_SHORT_5_6_5:
  163.          return sizeof(GLshort);
  164.       case GL_UNSIGNED_SHORT_5_6_5_REV:
  165.          return sizeof(GLshort);
  166.       case GL_UNSIGNED_SHORT_4_4_4_4:
  167.          return sizeof(GLshort);
  168.       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  169.          return sizeof(GLshort);
  170.       case GL_UNSIGNED_SHORT_5_5_5_1:
  171.          return sizeof(GLshort);
  172.       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  173.          return sizeof(GLshort);
  174.       case GL_UNSIGNED_INT_8_8_8_8:
  175.          return sizeof(GLuint);
  176.       case GL_UNSIGNED_INT_8_8_8_8_REV:
  177.          return sizeof(GLuint);
  178.       case GL_UNSIGNED_INT_10_10_10_2:
  179.          return sizeof(GLuint);
  180.       case GL_UNSIGNED_INT_2_10_10_10_REV:
  181.          return sizeof(GLuint);
  182.       default:
  183.          return -1;
  184.    }
  185. }
  186.  
  187.  
  188.  
  189. /*
  190.  * Return the number of components in a GL enum pixel type.
  191.  * Return -1 if bad format.
  192.  */
  193. GLint gl_components_in_format( GLenum format )
  194. {
  195.    switch (format) {
  196.       case GL_COLOR_INDEX:
  197.       case GL_COLOR_INDEX1_EXT:
  198.       case GL_COLOR_INDEX2_EXT:
  199.       case GL_COLOR_INDEX4_EXT:
  200.       case GL_COLOR_INDEX8_EXT:
  201.       case GL_COLOR_INDEX12_EXT:
  202.       case GL_COLOR_INDEX16_EXT:
  203.       case GL_STENCIL_INDEX:
  204.       case GL_DEPTH_COMPONENT:
  205.       case GL_RED:
  206.       case GL_GREEN:
  207.       case GL_BLUE:
  208.       case GL_ALPHA:
  209.       case GL_LUMINANCE:
  210.       case GL_INTENSITY:
  211.          return 1;
  212.       case GL_LUMINANCE_ALPHA:
  213.      return 2;
  214.       case GL_RGB:
  215.      return 3;
  216.       case GL_RGBA:
  217.      return 4;
  218.       case GL_BGR:
  219.      return 3;
  220.       case GL_BGRA:
  221.      return 4;
  222.       case GL_ABGR_EXT:
  223.          return 4;
  224.       default:
  225.          return -1;
  226.    }
  227. }
  228.  
  229.  
  230. /*
  231.  * Return bytes per pixel for given format and type
  232.  * Return -1 if bad format or type.
  233.  */
  234. GLint gl_bytes_per_pixel( GLenum format, GLenum type )
  235. {
  236.    GLint comps = gl_components_in_format( format );
  237.    if (comps < 0)
  238.       return -1;
  239.  
  240.    switch (type) {
  241.       case GL_BITMAP:
  242.          return 0;  /* special case */
  243.       case GL_BYTE:
  244.       case GL_UNSIGNED_BYTE:
  245.          return comps * sizeof(GLubyte);
  246.       case GL_SHORT:
  247.       case GL_UNSIGNED_SHORT:
  248.          return comps * sizeof(GLshort);
  249.       case GL_INT:
  250.       case GL_UNSIGNED_INT:
  251.          return comps * sizeof(GLint);
  252.       case GL_FLOAT:
  253.          return comps * sizeof(GLfloat);
  254.       case GL_UNSIGNED_BYTE_3_3_2:
  255.       case GL_UNSIGNED_BYTE_2_3_3_REV:
  256.          if (format == GL_RGB || format == GL_BGR)
  257.             return sizeof(GLubyte);
  258.          else
  259.             return -1;  /* error */
  260.       case GL_UNSIGNED_SHORT_5_6_5:
  261.       case GL_UNSIGNED_SHORT_5_6_5_REV:
  262.          if (format == GL_RGB || format == GL_BGR)
  263.             return sizeof(GLshort);
  264.          else
  265.             return -1;  /* error */
  266.       case GL_UNSIGNED_SHORT_4_4_4_4:
  267.       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  268.       case GL_UNSIGNED_SHORT_5_5_5_1:
  269.       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  270.          if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
  271.             return sizeof(GLushort);
  272.          else
  273.             return -1;
  274.       case GL_UNSIGNED_INT_8_8_8_8:
  275.       case GL_UNSIGNED_INT_8_8_8_8_REV:
  276.       case GL_UNSIGNED_INT_10_10_10_2:
  277.       case GL_UNSIGNED_INT_2_10_10_10_REV:
  278.          if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
  279.             return sizeof(GLuint);
  280.          else
  281.             return -1;
  282.       default:
  283.          return -1;
  284.    }
  285. }
  286.  
  287.  
  288. /*
  289.  * Test if the given pixel format and type are legal.
  290.  * Return GL_TRUE for legal, GL_FALSE for illegal.
  291.  */
  292. GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type )
  293. {
  294.    switch (format) {
  295.       case GL_COLOR_INDEX:
  296.       case GL_STENCIL_INDEX:
  297.          switch (type) {
  298.             case GL_BITMAP:
  299.             case GL_BYTE:
  300.             case GL_UNSIGNED_BYTE:
  301.             case GL_SHORT:
  302.             case GL_UNSIGNED_SHORT:
  303.             case GL_INT:
  304.             case GL_UNSIGNED_INT:
  305.             case GL_FLOAT:
  306.                return GL_TRUE;
  307.             default:
  308.                return GL_FALSE;
  309.          }
  310.       case GL_RED:
  311.       case GL_GREEN:
  312.       case GL_BLUE:
  313.       case GL_ALPHA:
  314.       case GL_LUMINANCE:
  315.       case GL_LUMINANCE_ALPHA:
  316.       case GL_DEPTH_COMPONENT:
  317.       case GL_BGR:
  318.          switch (type) {
  319.             case GL_BYTE:
  320.             case GL_UNSIGNED_BYTE:
  321.             case GL_SHORT:
  322.             case GL_UNSIGNED_SHORT:
  323.             case GL_INT:
  324.             case GL_UNSIGNED_INT:
  325.             case GL_FLOAT:
  326.                return GL_TRUE;
  327.             default:
  328.                return GL_FALSE;
  329.          }
  330.       case GL_RGB:
  331.          switch (type) {
  332.             case GL_BYTE:
  333.             case GL_UNSIGNED_BYTE:
  334.             case GL_SHORT:
  335.             case GL_UNSIGNED_SHORT:
  336.             case GL_INT:
  337.             case GL_UNSIGNED_INT:
  338.             case GL_FLOAT:
  339.             case GL_UNSIGNED_BYTE_3_3_2:
  340.             case GL_UNSIGNED_BYTE_2_3_3_REV:
  341.             case GL_UNSIGNED_SHORT_5_6_5:
  342.             case GL_UNSIGNED_SHORT_5_6_5_REV:
  343.                return GL_TRUE;
  344.             default:
  345.                return GL_FALSE;
  346.          }
  347.       case GL_RGBA:
  348.       case GL_BGRA:
  349.       case GL_ABGR_EXT:
  350.          switch (type) {
  351.             case GL_BYTE:
  352.             case GL_UNSIGNED_BYTE:
  353.             case GL_SHORT:
  354.             case GL_UNSIGNED_SHORT:
  355.             case GL_INT:
  356.             case GL_UNSIGNED_INT:
  357.             case GL_FLOAT:
  358.             case GL_UNSIGNED_SHORT_4_4_4_4:
  359.             case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  360.             case GL_UNSIGNED_SHORT_5_5_5_1:
  361.             case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  362.             case GL_UNSIGNED_INT_8_8_8_8:
  363.             case GL_UNSIGNED_INT_8_8_8_8_REV:
  364.             case GL_UNSIGNED_INT_10_10_10_2:
  365.             case GL_UNSIGNED_INT_2_10_10_10_REV:
  366.                return GL_TRUE;
  367.             default:
  368.                return GL_FALSE;
  369.          }
  370.       default:
  371.          ; /* fall-through */
  372.    }
  373.    return GL_FALSE;
  374. }
  375.  
  376.  
  377.  
  378. /*
  379.  * Return the address of a pixel in an image (actually a volume).
  380.  * Pixel unpacking/packing parameters are observed according to 'packing'.
  381.  * Input:  image - start of image data
  382.  *         width, height - size of image
  383.  *         format - image format
  384.  *         type - pixel component type
  385.  *         packing - the pixelstore attributes
  386.  *         img - which image in the volume (0 for 1D or 2D images)
  387.  *         row, column - location of pixel in the image
  388.  * Return:  address of pixel at (image,row,column) in image or NULL if error.
  389.  */
  390. GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
  391.                                 const GLvoid *image, GLsizei width,
  392.                                 GLsizei height, GLenum format, GLenum type,
  393.                                 GLint img, GLint row, GLint column )
  394. {
  395.    GLint alignment;        /* 1, 2 or 4 */
  396.    GLint pixels_per_row;
  397.    GLint rows_per_image;
  398.    GLint skiprows;
  399.    GLint skippixels;
  400.    GLint skipimages;       /* for 3-D volume images */
  401.    GLubyte *pixel_addr;
  402.  
  403.    alignment = packing->Alignment;
  404.    if (packing->RowLength > 0) {
  405.       pixels_per_row = packing->RowLength;
  406.    }
  407.    else {
  408.       pixels_per_row = width;
  409.    }
  410.    if (packing->ImageHeight > 0) {
  411.       rows_per_image = packing->ImageHeight;
  412.    }
  413.    else {
  414.       rows_per_image = height;
  415.    }
  416.    skiprows = packing->SkipRows;
  417.    skippixels = packing->SkipPixels;
  418.    skipimages = packing->SkipImages;
  419.  
  420.    if (type==GL_BITMAP) {
  421.       /* BITMAP data */
  422.       GLint comp_per_pixel;   /* components per pixel */
  423.       GLint bytes_per_comp;   /* bytes per component */
  424.       GLint bytes_per_row;
  425.       GLint bytes_per_image;
  426.  
  427.       /* Compute bytes per component */
  428.       bytes_per_comp = gl_sizeof_packed_type( type );
  429.       if (bytes_per_comp<0) {
  430.          return NULL;
  431.       }
  432.  
  433.       /* Compute number of components per pixel */
  434.       comp_per_pixel = gl_components_in_format( format );
  435.       if (comp_per_pixel<0 && type != GL_BITMAP) {
  436.          return NULL;
  437.       }
  438.  
  439.       bytes_per_row = alignment
  440.                     * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
  441.  
  442.       bytes_per_image = bytes_per_row * rows_per_image;
  443.  
  444.       pixel_addr = (GLubyte *) image
  445.                  + (skipimages + img) * bytes_per_image
  446.                  + (skiprows + row) * bytes_per_row
  447.                  + (skippixels + column) / 8;
  448.    }
  449.    else {
  450.       /* Non-BITMAP data */
  451.       GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
  452.  
  453.       bytes_per_pixel = gl_bytes_per_pixel( format, type );
  454.  
  455.       /* The pixel type and format should have been error checked earlier */
  456.       assert(bytes_per_pixel > 0);
  457.  
  458.       bytes_per_row = pixels_per_row * bytes_per_pixel;
  459.       remainder = bytes_per_row % alignment;
  460.       if (remainder > 0)
  461.          bytes_per_row += (alignment - remainder);
  462.  
  463.       ASSERT(bytes_per_row % alignment == 0);
  464.  
  465.       bytes_per_image = bytes_per_row * rows_per_image;
  466.  
  467.       /* compute final pixel address */
  468.       pixel_addr = (GLubyte *) image
  469.                  + (skipimages + img) * bytes_per_image
  470.                  + (skiprows + row) * bytes_per_row
  471.                  + (skippixels + column) * bytes_per_pixel;
  472.    }
  473.  
  474.    return (GLvoid *) pixel_addr;
  475. }
  476.  
  477.  
  478.  
  479. /*
  480.  * Allocate a new gl_image.  All fields are initialized to zero.
  481.  */
  482. static struct gl_image *alloc_image( void )
  483. {
  484.    return CALLOC_STRUCT(gl_image);
  485. }
  486.  
  487.  
  488.  
  489. /*
  490.  * Allocate a new gl_image with the error flag set.
  491.  */
  492. static struct gl_image *alloc_error_image( GLint width, GLint height,
  493.                                            GLint depth, GLenum format,
  494.                                            GLenum type )
  495. {
  496.    struct gl_image *image = alloc_image();
  497.    if (image) {
  498.       image->Width = width;
  499.       image->Height = height;
  500.       image->Depth = depth;
  501.       image->Format = format;
  502.       image->Type = type;
  503.       image->ErrorFlag = GL_TRUE;
  504.    }
  505.    return image;
  506. }
  507.  
  508.  
  509.  
  510. /*
  511.  * Free a gl_image.
  512.  */
  513. void gl_free_image( struct gl_image *image )
  514. {
  515.    if (image->Data) {
  516.       FREE(image->Data);
  517.    }
  518.    FREE(image);
  519. }
  520.  
  521.  
  522.  
  523. /*
  524.  * Do error checking on an image.  If there's an error, register it and
  525.  * return GL_TRUE, else return GL_FALSE.
  526.  */
  527. GLboolean gl_image_error_test( GLcontext *ctx, const struct gl_image *image,
  528.                                const char *msg )
  529. {
  530.    if (!image) {
  531.       gl_error( ctx, GL_OUT_OF_MEMORY, msg );        
  532.       return GL_TRUE;
  533.    }
  534.    if (image->Width <= 0 || image->Height <= 0 || image->Depth <= 0) {
  535.       gl_error( ctx, GL_INVALID_VALUE, msg );
  536.       return GL_TRUE;
  537.    }
  538.    else if (!gl_is_legal_format_and_type(image->Format, image->Type)) {
  539.       return GL_TRUE;
  540.    }
  541.    else {
  542.       return GL_FALSE;
  543.    }
  544. }
  545.  
  546.  
  547.  
  548. /*
  549.  * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats.
  550.  * Input:  type - datatype of src depth image
  551.  * Return pointer to a new gl_image structure.
  552.  *
  553.  * Notes:  if the source image type is GLushort then the gl_image will
  554.  * also store GLushorts.  If the src image type is GLuint then the gl_image
  555.  * will also store GLuints.  For all other src image types the gl_image
  556.  * will store GLfloats.  The integer cases can later be optimized.
  557.  */
  558. static struct gl_image *
  559. unpack_depth_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
  560.                     const GLvoid *pixels,
  561.                     const struct gl_pixelstore_attrib *packing)
  562.  
  563. {
  564.    struct gl_image *image;
  565.    GLfloat *fDst;
  566.    GLushort *sDst;
  567.    GLuint *iDst;
  568.    GLint i, j;
  569.    GLboolean errorType;
  570.  
  571.    errorType = type != GL_BYTE &&
  572.                type != GL_UNSIGNED_BYTE &&
  573.                type != GL_SHORT &&
  574.                type != GL_UNSIGNED_SHORT &&
  575.                type != GL_INT &&
  576.                type != GL_UNSIGNED_INT &&
  577.                type != GL_FLOAT;
  578.  
  579.    image = alloc_image();
  580.    if (image) {
  581.       image->Width = width;
  582.       image->Height = height;
  583.       image->Depth = 1;
  584.       image->Components = 1;
  585.       image->Format = GL_DEPTH_COMPONENT;
  586.       if (errorType) {
  587.          image->Type = type;
  588.          image->Data = NULL;
  589.       }
  590.       if (type==GL_UNSIGNED_SHORT) {
  591.          image->Type = GL_UNSIGNED_SHORT;
  592.          image->Data = MALLOC( width * height * sizeof(GLushort));
  593.       }
  594.       else if (type==GL_UNSIGNED_INT) {
  595.          image->Type = GL_UNSIGNED_INT;
  596.          image->Data = MALLOC( width * height * sizeof(GLuint));
  597.       }
  598.       else {
  599.          image->Type = GL_FLOAT;
  600.          image->Data = MALLOC( width * height * sizeof(GLfloat));
  601.       }
  602.       image->RefCount = 0;
  603.       if (!image->Data)
  604.          return image;
  605.    }
  606.    else {
  607.       return NULL;
  608.    }
  609.  
  610.    if (errorType)
  611.       return image;
  612.  
  613.    fDst = (GLfloat *) image->Data;
  614.    sDst = (GLushort *) image->Data;
  615.    iDst = (GLuint *) image->Data;
  616.  
  617.    for (i=0;i<height;i++) {
  618.       GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
  619.                                             width, height,
  620.                                             GL_DEPTH_COMPONENT, type,
  621.                                             0, i, 0 );
  622.       if (!src) {
  623.          return image;
  624.       }
  625.  
  626.       switch (type) {
  627.          case GL_BYTE:
  628.             assert(image->Type == GL_FLOAT);
  629.             for (j=0; j<width; j++) {
  630.                *fDst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
  631.             }
  632.             break;
  633.          case GL_UNSIGNED_BYTE:
  634.             assert(image->Type == GL_FLOAT);
  635.             for (j=0; j<width; j++) {
  636.                *fDst++ = UBYTE_TO_FLOAT(((GLubyte*)src)[j]);
  637.             }
  638.             break;
  639.          case GL_UNSIGNED_SHORT:
  640.             assert(image->Type == GL_UNSIGNED_SHORT);
  641.             MEMCPY( sDst, src, width * sizeof(GLushort) );
  642.             if (packing->SwapBytes) {
  643.                gl_swap2( sDst, width );
  644.             }
  645.             sDst += width;
  646.             break;
  647.          case GL_SHORT:
  648.             assert(image->Type == GL_FLOAT);
  649.             if (packing->SwapBytes) {
  650.                for (j=0;j<width;j++) {
  651.                   GLshort value = ((GLshort*)src)[j];
  652.                   value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
  653.                   *fDst++ = SHORT_TO_FLOAT(value);
  654.                }
  655.             }
  656.             else {
  657.                for (j=0;j<width;j++) {
  658.                   *fDst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
  659.                }
  660.             }
  661.             break;
  662.          case GL_INT:
  663.             assert(image->Type == GL_FLOAT);
  664.             if (packing->SwapBytes) {
  665.                for (j=0;j<width;j++) {
  666.                   GLint value = ((GLint*)src)[j];
  667.                   value = ((value >> 24) & 0x000000ff) |
  668.                           ((value >> 8)  & 0x0000ff00) |
  669.                           ((value << 8)  & 0x00ff0000) |
  670.                           ((value << 24) & 0xff000000);
  671.                   *fDst++ = INT_TO_FLOAT(value);
  672.                }
  673.             }
  674.             else {
  675.                for (j=0;j<width;j++) {
  676.                   *fDst++ = INT_TO_FLOAT(((GLint*)src)[j]);
  677.                }
  678.             }
  679.             iDst += width;
  680.             break;
  681.          case GL_UNSIGNED_INT:
  682.             assert(image->Type == GL_UNSIGNED_INT);
  683.             MEMCPY( iDst, src, width * sizeof(GLuint) );
  684.             if (packing->SwapBytes) {
  685.                gl_swap4( iDst, width );
  686.             }
  687.             iDst += width;
  688.             break;
  689.          case GL_FLOAT:
  690.             assert(image->Type == GL_FLOAT);
  691.             MEMCPY( fDst, src, width * sizeof(GLfloat) );
  692.             if (packing->SwapBytes) {
  693.                gl_swap4( (GLuint*) fDst, width );
  694.             }
  695.             fDst += width;
  696.             break;
  697.          default:
  698.             gl_problem(ctx, "unpack_depth_image type" );
  699.             return image;
  700.       }
  701.    }
  702.  
  703.    return image;
  704. }
  705.  
  706.  
  707.  
  708. /*
  709.  * Unpack a stencil image.  Store as GLubytes in a gl_image structure.
  710.  * Return:  pointer to new gl_image structure.
  711.  */
  712. static struct gl_image *
  713. unpack_stencil_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
  714.                       const GLvoid *pixels,
  715.                       const struct gl_pixelstore_attrib *packing )
  716. {
  717.    struct gl_image *image;
  718.    GLubyte *dst;
  719.    GLint i, j;
  720.    GLboolean errorType;
  721.  
  722.    assert(sizeof(GLstencil) == sizeof(GLubyte));
  723.  
  724.    errorType = type != GL_BYTE &&
  725.                type != GL_UNSIGNED_BYTE &&
  726.                type != GL_SHORT &&
  727.                type != GL_UNSIGNED_SHORT &&
  728.                type != GL_INT &&
  729.                type != GL_UNSIGNED_INT &&
  730.                type != GL_FLOAT &&
  731.                type != GL_BITMAP;
  732.  
  733.    image = alloc_image();
  734.    if (image) {
  735.       image->Width = width;
  736.       image->Height = height;
  737.       image->Depth = 1;
  738.       image->Components = 1;
  739.       image->Format = GL_STENCIL_INDEX;
  740.       if (errorType) {
  741.          image->Type = type;
  742.          image->Data = NULL;
  743.       }
  744.       else {
  745.          image->Type = GL_UNSIGNED_BYTE;
  746.          image->Data = MALLOC( width * height * sizeof(GLubyte));
  747.       }
  748.       image->RefCount = 0;
  749.       if (!image->Data)
  750.          return image;
  751.    }
  752.    else {
  753.       return NULL;
  754.    }
  755.  
  756.    if (errorType)
  757.       return image; /* error will be generated later */
  758.  
  759.    dst = (GLubyte *) image->Data;
  760.  
  761.    for (i=0;i<height;i++) {
  762.       GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
  763.                                             width, height,
  764.                                             GL_STENCIL_INDEX, type,
  765.                                             0, i, 0 );
  766.       if (!src) {
  767.          return image;
  768.       }
  769.  
  770.       switch (type) {
  771.          case GL_UNSIGNED_BYTE:
  772.          case GL_BYTE:
  773.             MEMCPY( dst, src, width * sizeof(GLubyte) );
  774.             dst += width * sizeof(GLubyte);
  775.             break;
  776.          case GL_UNSIGNED_SHORT:
  777.          case GL_SHORT:
  778.             if (packing->SwapBytes) {
  779.                /* grab upper byte */
  780.                for (j=0; j < width; j++) {
  781.                   *dst++ = (((GLushort*)src)[j] & 0xff00) >> 8;
  782.                }
  783.             }
  784.             else {
  785.                for (j=0; j < width; j++) {
  786.                   *dst++ = (((GLushort*)src)[j]) & 0xff;
  787.                }
  788.             }
  789.             break;
  790.          case GL_INT:
  791.             if (packing->SwapBytes) {
  792.                /* grab upper byte */
  793.                for (j=0; j < width; j++) {
  794.                   *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
  795.                }
  796.             }
  797.             else {
  798.                for (j=0; j < width; j++) {
  799.                   *dst++ = (((GLuint*)src)[j]) & 0xff;
  800.                }
  801.             }
  802.             break;
  803.          case GL_UNSIGNED_INT:
  804.             if (packing->SwapBytes) {
  805.                /* grab upper byte */
  806.                for (j=0; j < width; j++) {
  807.                   *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
  808.                }
  809.             }
  810.             else {
  811.                for (j=0; j < width; j++) {
  812.                   *dst++ = (((GLuint*)src)[j]) & 0xff;
  813.                }
  814.             }
  815.             break;
  816.          case GL_FLOAT:
  817.             if (packing->SwapBytes) {
  818.                for (j=0; j < width; j++) {
  819.                   GLfloat fvalue;
  820.                   GLint value = ((GLuint*)src)[j];
  821.                   value = ((value & 0xff000000) >> 24)
  822.                      | ((value & 0x00ff0000) >> 8)
  823.                      | ((value & 0x0000ff00) << 8)
  824.                      | ((value & 0x000000ff) << 24);
  825.                   fvalue = *((GLfloat*) &value);
  826.                   *dst++ = ((GLint) fvalue) & 0xff;
  827.                }
  828.             }
  829.             else {
  830.                for (j=0; j < width; j++) {
  831.                   GLfloat fvalue = ((GLfloat *)src)[j];
  832.                   *dst++ = ((GLint) fvalue) & 0xff;
  833.                }
  834.             }
  835.             break;
  836.          default:
  837.             gl_problem(ctx, "unpack_stencil_image type" );
  838.             return image;
  839.       }
  840.    }
  841.  
  842.    return image;
  843. }
  844.  
  845.  
  846.  
  847. /*
  848.  * Unpack a bitmap, return a new gl_image struct.
  849.  */
  850. static struct gl_image *
  851. unpack_bitmap( GLenum format, GLint width, GLint height,
  852.                const GLvoid *pixels,
  853.                const struct gl_pixelstore_attrib *packing )
  854. {
  855.    struct gl_image *image;
  856.    GLint bytes, i, width_in_bytes;
  857.    GLubyte *buffer, *dst;
  858.  
  859.    assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX);
  860.  
  861.    /* Alloc dest storage */
  862.    bytes = ((width+7)/8 * height);
  863.    if (bytes>0 && pixels!=NULL) {
  864.       buffer = (GLubyte *) MALLOC( bytes );
  865.       if (!buffer) {
  866.          return NULL;
  867.       }
  868.       /* Copy/unpack pixel data to buffer */
  869.       width_in_bytes = CEILING( width, 8 );
  870.       dst = buffer;
  871.       for (i=0; i<height; i++) {
  872.          GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
  873.                                                width, height,
  874.                                                GL_COLOR_INDEX, GL_BITMAP,
  875.                                                0, i, 0 );
  876.          if (!src) {
  877.             FREE(buffer);
  878.             return NULL;
  879.          }
  880.          MEMCPY( dst, src, width_in_bytes );
  881.          dst += width_in_bytes;
  882.       }
  883.       /* Bit flipping */
  884.       if (packing->LsbFirst) {
  885.          gl_flip_bytes( buffer, bytes );
  886.       }
  887.    }
  888.    else {
  889.       /* a 'null' bitmap */
  890.       buffer = NULL;
  891.    }
  892.    
  893.    image = alloc_image();
  894.    if (image) {
  895.       image->Width = width;
  896.       image->Height = height;
  897.       image->Depth = 1;
  898.       image->Components = 0;
  899.       image->Format = format;
  900.       image->Type = GL_BITMAP;
  901.       image->Data = buffer;
  902.       image->RefCount = 0;
  903.    }
  904.    else {
  905.       FREE( buffer );
  906.       return NULL;
  907.    }
  908.  
  909.    return image;
  910. }
  911.  
  912.  
  913.  
  914. /*
  915.  * Unpack a 32x32 pixel polygon stipple from user memory using the
  916.  * current pixel unpack settings.
  917.  */
  918. void gl_unpack_polygon_stipple( const GLcontext *ctx,
  919.                                 const GLubyte *pattern, GLuint dest[32] )
  920. {
  921.    GLint i;
  922.    for (i = 0; i < 32; i++) {
  923.       GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack, pattern,
  924.                                   32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
  925.       dest[i] = (src[0] << 24)
  926.               | (src[1] << 16)
  927.               | (src[2] <<  8)
  928.               | (src[3]      );
  929.    }
  930.  
  931.    /* Bit flipping within each byte */
  932.    if (ctx->Unpack.LsbFirst) {
  933.       gl_flip_bytes( (GLubyte *) dest, 32 * 4 );
  934.    }
  935. }
  936.  
  937.  
  938.  
  939. /*
  940.  * Pack polygon stipple into user memory given current pixel packing
  941.  * settings.
  942.  */
  943. void gl_pack_polygon_stipple( const GLcontext *ctx,
  944.                               const GLuint pattern[32],
  945.                               GLubyte *dest )
  946. {
  947.    GLint i;
  948.    for (i = 0; i < 32; i++) {
  949.       GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image( &ctx->Pack, dest,
  950.                                   32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
  951.       dst[0] = (pattern[i] >> 24) & 0xff;
  952.       dst[1] = (pattern[i] >> 16) & 0xff;
  953.       dst[2] = (pattern[i] >>  8) & 0xff;
  954.       dst[3] = (pattern[i]      ) & 0xff;
  955.  
  956.       /* Bit flipping within each byte */
  957.       if (ctx->Pack.LsbFirst) {
  958.          gl_flip_bytes( (GLubyte *) dst, 4 );
  959.       }
  960.    }
  961. }
  962.  
  963.  
  964.  
  965. /*
  966.  * Unpack an RGBA or CI image and store it as unsigned bytes
  967.  */
  968. static struct gl_image *
  969. unpack_ubyte_image( GLint width, GLint height,
  970.                     GLint depth, GLenum format, const GLvoid *pixels,
  971.                     const struct gl_pixelstore_attrib *packing )
  972. {
  973.    struct gl_image *image;
  974.    GLint width_in_bytes;
  975.    GLint components;
  976.    GLubyte *buffer, *dst;
  977.    GLint i, d;
  978.  
  979.    components = gl_components_in_format( format );
  980.  
  981.    width_in_bytes = width * components * sizeof(GLubyte);
  982.    buffer = (GLubyte *) MALLOC( height * width_in_bytes * depth );
  983.    if (!buffer) {
  984.       return NULL;
  985.    }
  986.  
  987.    /* Copy/unpack pixel data to buffer */
  988.    dst = buffer;
  989.    for (d=0; d<depth; d++ ) {
  990.       for (i=0;i<height;i++) {
  991.          GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( packing,
  992.                        pixels, width, height, format, GL_UNSIGNED_BYTE,
  993.                        d, i, 0 );
  994.          if (!src) {
  995.             FREE(buffer);
  996.             return NULL;
  997.          }
  998.          MEMCPY( dst, src, width_in_bytes );
  999.          dst += width_in_bytes;
  1000.       }
  1001.    }
  1002.    
  1003.    if (format == GL_BGR) {
  1004.       /* swap order of every ubyte triplet from BGR to RGB */
  1005.       for (i=0; i<width*height; i++) {
  1006.          GLubyte b = buffer[i*3+0];
  1007.          GLubyte r = buffer[i*3+2];
  1008.          buffer[i*3+0] = r;
  1009.          buffer[i*3+2] = b;
  1010.       }
  1011.    }
  1012.    else if (format == GL_BGRA) {
  1013.       /* swap order of every ubyte quadruplet from BGRA to RGBA */
  1014.       for (i=0; i<width*height; i++) {
  1015.          GLubyte b = buffer[i*4+0];
  1016.          GLubyte r = buffer[i*4+2];
  1017.          buffer[i*4+0] = r;
  1018.          buffer[i*4+2] = b;
  1019.       }
  1020.    }
  1021.    else if (format == GL_ABGR_EXT) {
  1022.       /* swap order of every ubyte quadruplet from ABGR to RGBA */
  1023.       for (i=0; i<width*height; i++) {
  1024.          GLubyte a = buffer[i*4+0];
  1025.          GLubyte b = buffer[i*4+1];
  1026.          GLubyte g = buffer[i*4+2];
  1027.          GLubyte r = buffer[i*4+3];
  1028.          buffer[i*4+0] = r;
  1029.          buffer[i*4+1] = g;
  1030.          buffer[i*4+2] = b;
  1031.          buffer[i*4+3] = a;
  1032.       }
  1033.    }
  1034.  
  1035.  
  1036.    image = alloc_image();
  1037.    if (image) {
  1038.       image->Width = width;
  1039.       image->Height = height;
  1040.       image->Depth = depth;
  1041.       image->Components = components;
  1042.       if (format == GL_BGR)
  1043.          image->Format = GL_RGB;
  1044.       else if (format == GL_BGRA)
  1045.          image->Format = GL_RGBA;
  1046.       else if (format == GL_ABGR_EXT)
  1047.          image->Format = GL_RGBA;
  1048.       else
  1049.          image->Format = format;
  1050.       image->Type = GL_UNSIGNED_BYTE;
  1051.       image->Data = buffer;
  1052.       image->RefCount = 0;
  1053.    }
  1054.    else {
  1055.       FREE( buffer );
  1056.    }
  1057.  
  1058.    return image;
  1059. }
  1060.  
  1061.  
  1062.  
  1063. /*
  1064.  * Unpack a color image storing image as GLfloats
  1065.  */
  1066. static struct gl_image *
  1067. unpack_float_image( GLcontext *ctx, GLint width, GLint height, GLint depth,
  1068.                     GLenum format, GLenum type, const GLvoid *pixels,
  1069.                     const struct gl_pixelstore_attrib *packing )
  1070. {
  1071.    struct gl_image *image;
  1072.    GLfloat *dst;
  1073.    GLint elems_per_row;
  1074.    GLint components;
  1075.    GLint i, j, d;
  1076.    GLboolean normalize;
  1077.  
  1078.    assert(type != GL_BITMAP);
  1079.  
  1080.    components = gl_components_in_format( format );
  1081.    assert(components > 0);  /* should have been caught earlier */
  1082.  
  1083.    if (!gl_is_legal_format_and_type( format, type )) {
  1084.       /* bad pixel type for format, make dummy image */
  1085.       image = alloc_image();
  1086.       if (image) {
  1087.          image->Width = width;
  1088.          image->Height = height;
  1089.          image->Depth = depth;
  1090.          image->Components = components;
  1091.          image->Format = format;
  1092.          image->Type = type;
  1093.          image->Data = NULL;
  1094.          image->RefCount = 0;
  1095.       }
  1096.       return image;
  1097.    }
  1098.  
  1099.    elems_per_row = width * components;
  1100.  
  1101.    image = alloc_image();
  1102.    if (image) {
  1103.       image->Width = width;
  1104.       image->Height = height;
  1105.       image->Depth = depth;
  1106.       image->Components = components;
  1107.       if (format == GL_BGR)
  1108.          image->Format = GL_RGB;
  1109.       else if (format == GL_BGRA)
  1110.          image->Format = GL_RGBA;
  1111.       else if (format == GL_ABGR_EXT)
  1112.          image->Format = GL_RGBA;
  1113.       else
  1114.          image->Format = format;
  1115.       image->Type = GL_FLOAT;
  1116.       image->Data = MALLOC( elems_per_row * height * depth * sizeof(GLfloat));
  1117.       image->RefCount = 0;
  1118.       if (!image->Data)
  1119.          return image;
  1120.    }
  1121.    else {
  1122.       return NULL;
  1123.    }
  1124.  
  1125.    normalize = (format != GL_COLOR_INDEX) && (format != GL_STENCIL_INDEX);
  1126.  
  1127.    dst = (GLfloat *) image->Data;
  1128.  
  1129.    for (d=0; d<depth; d++) {
  1130.       for (i=0;i<height;i++) {
  1131.          GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
  1132.                                                width, height,
  1133.                                                format, type,
  1134.                                                d, i, 0 );
  1135.          if (!src) {
  1136.             return image;
  1137.          }
  1138.  
  1139.          switch (type) {
  1140.             case GL_UNSIGNED_BYTE:
  1141.                {
  1142.                   GLubyte *ubsrc = (GLubyte *) src;
  1143.                   if (normalize) {
  1144.                      for (j=0;j<elems_per_row;j++) {
  1145.                         *dst++ = UBYTE_TO_FLOAT(ubsrc[j]);
  1146.                      }
  1147.                   }
  1148.                   else {
  1149.                      for (j=0;j<elems_per_row;j++) {
  1150.                         *dst++ = (GLfloat) ubsrc[j];
  1151.                      }
  1152.                   }
  1153.                }
  1154.                break;
  1155.             case GL_BYTE:
  1156.                if (normalize) {
  1157.                   for (j=0;j<elems_per_row;j++) {
  1158.                      *dst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
  1159.                   }
  1160.                }
  1161.                else {
  1162.                   for (j=0;j<elems_per_row;j++) {
  1163.                      *dst++ = (GLfloat) ((GLbyte*)src)[j];
  1164.                   }
  1165.                }
  1166.                break;
  1167.             case GL_UNSIGNED_SHORT:
  1168.                if (packing->SwapBytes) {
  1169.                   for (j=0;j<elems_per_row;j++) {
  1170.                      GLushort value = ((GLushort*)src)[j];
  1171.                      value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
  1172.                      if (normalize) {
  1173.                         *dst++ = USHORT_TO_FLOAT(value);
  1174.                      }
  1175.                      else {
  1176.                         *dst++ = (GLfloat) value;
  1177.                      }
  1178.                   }
  1179.                }
  1180.                else {
  1181.                   if (normalize) {
  1182.                      for (j=0;j<elems_per_row;j++) {
  1183.                         *dst++ = USHORT_TO_FLOAT(((GLushort*)src)[j]);
  1184.                      }
  1185.                   }
  1186.                   else {
  1187.                      for (j=0;j<elems_per_row;j++) {
  1188.                         *dst++ = (GLfloat) ((GLushort*)src)[j];
  1189.                      }
  1190.                   }
  1191.                }
  1192.                break;
  1193.             case GL_SHORT:
  1194.                if (packing->SwapBytes) {
  1195.                   for (j=0;j<elems_per_row;j++) {
  1196.                      GLshort value = ((GLshort*)src)[j];
  1197.                      value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
  1198.                      if (normalize) {
  1199.                         *dst++ = SHORT_TO_FLOAT(value);
  1200.                      }
  1201.                      else {
  1202.                         *dst++ = (GLfloat) value;
  1203.                      }
  1204.                   }
  1205.                }
  1206.                else {
  1207.                   if (normalize) {
  1208.                      for (j=0;j<elems_per_row;j++) {
  1209.                         *dst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
  1210.                      }
  1211.                   }
  1212.                   else {
  1213.                      for (j=0;j<elems_per_row;j++) {
  1214.                         *dst++ = (GLfloat) ((GLshort*)src)[j];
  1215.                      }
  1216.                   }
  1217.                }
  1218.                break;
  1219.             case GL_UNSIGNED_INT:
  1220.                if (packing->SwapBytes) {
  1221.                   GLuint value;
  1222.                   for (j=0;j<elems_per_row;j++) {
  1223.                      value = ((GLuint*)src)[j];
  1224.                      value = ((value & 0xff000000) >> 24)
  1225.                            | ((value & 0x00ff0000) >> 8)
  1226.                            | ((value & 0x0000ff00) << 8)
  1227.                            | ((value & 0x000000ff) << 24);
  1228.                      if (normalize) {
  1229.                         *dst++ = UINT_TO_FLOAT(value);
  1230.                      }
  1231.                      else {
  1232.                         *dst++ = (GLfloat) value;
  1233.                      }
  1234.                   }
  1235.                }
  1236.                else {
  1237.                   if (normalize) {
  1238.                      for (j=0;j<elems_per_row;j++) {
  1239.                         *dst++ = UINT_TO_FLOAT(((GLuint*)src)[j]);
  1240.                      }
  1241.                   }
  1242.                   else {
  1243.                      for (j=0;j<elems_per_row;j++) {
  1244.                         *dst++ = (GLfloat) ((GLuint*)src)[j];
  1245.                      }
  1246.                   }
  1247.                }
  1248.                break;
  1249.             case GL_INT:
  1250.                if (packing->SwapBytes) {
  1251.                   GLint value;
  1252.                   for (j=0;j<elems_per_row;j++) {
  1253.                      value = ((GLint*)src)[j];
  1254.                      value = ((value & 0xff000000) >> 24)
  1255.                            | ((value & 0x00ff0000) >> 8)
  1256.                            | ((value & 0x0000ff00) << 8)
  1257.                            | ((value & 0x000000ff) << 24);
  1258.                      if (normalize) {
  1259.                         *dst++ = INT_TO_FLOAT(value);
  1260.                      }
  1261.                      else {
  1262.                         *dst++ = (GLfloat) value;
  1263.                      }
  1264.                   }
  1265.                }
  1266.                else {
  1267.                   if (normalize) {
  1268.                      for (j=0;j<elems_per_row;j++) {
  1269.                         *dst++ = INT_TO_FLOAT(((GLint*)src)[j]);
  1270.                      }
  1271.                   }
  1272.                   else {
  1273.                      for (j=0;j<elems_per_row;j++) {
  1274.                         *dst++ = (GLfloat) ((GLint*)src)[j];
  1275.                      }
  1276.                   }
  1277.                }
  1278.                break;
  1279.             case GL_FLOAT:
  1280.                if (packing->SwapBytes) {
  1281.                   GLint value;
  1282.                   for (j=0;j<elems_per_row;j++) {
  1283.                      value = ((GLuint*)src)[j];
  1284.                      value = ((value & 0xff000000) >> 24)
  1285.                            | ((value & 0x00ff0000) >> 8)
  1286.                            | ((value & 0x0000ff00) << 8)
  1287.                            | ((value & 0x000000ff) << 24);
  1288.                      *dst++ = *((GLfloat*) &value);
  1289.                   }
  1290.                }
  1291.                else {
  1292.                   MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) );
  1293.                   dst += elems_per_row;
  1294.                }
  1295.                break;
  1296.             case GL_UNSIGNED_BYTE_3_3_2:
  1297.                {
  1298.                   GLubyte *ubsrc = (GLubyte *) src;
  1299.                   for (j=0;j<width;j++) {
  1300.                      GLubyte p = ubsrc[j];
  1301.                      *dst++ = ((p >> 5)      ) * (1.0F / 7.0F); /* red */
  1302.                      *dst++ = ((p >> 2) & 0x7) * (1.0F / 7.0F); /* green */
  1303.                      *dst++ = ((p     ) & 0x3) * (1.0F / 3.0F); /* blue */
  1304.                   }
  1305.                }
  1306.                break;
  1307.             case GL_UNSIGNED_BYTE_2_3_3_REV:
  1308.                {
  1309.                   GLubyte *ubsrc = (GLubyte *) src;
  1310.                   for (j=0;j<width;j++) {
  1311.                      GLubyte p = ubsrc[j];
  1312.                      *dst++ = ((p     ) & 0x7) * (1.0F / 7.0F); /* red */
  1313.                      *dst++ = ((p >> 3) & 0x7) * (1.0F / 7.0F); /* green */
  1314.                      *dst++ = ((p >> 6)      ) * (1.0F / 3.0F); /* blue */
  1315.                   }
  1316.                }
  1317.                break;
  1318.             case GL_UNSIGNED_SHORT_5_6_5:
  1319.                {
  1320.                   GLushort *ussrc = (GLushort *) src;
  1321.                   for (j=0;j<width;j++) {
  1322.                      GLushort p = ussrc[j];
  1323.                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* red */
  1324.                      *dst++ = ((p >>  5) & 0x3f) * (1.0F / 63.0F); /* green */
  1325.                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* blue */
  1326.                   }
  1327.                }
  1328.                break;
  1329.             case GL_UNSIGNED_SHORT_5_6_5_REV:
  1330.                {
  1331.                   GLushort *ussrc = (GLushort *) src;
  1332.                   for (j=0;j<width;j++) {
  1333.                      GLushort p = ussrc[j];
  1334.                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* red */
  1335.                      *dst++ = ((p >>  5) & 0x3f) * (1.0F / 63.0F); /* green */
  1336.                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* blue */
  1337.                   }
  1338.                }
  1339.                break;
  1340.         case GL_UNSIGNED_SHORT_4_4_4_4:
  1341.                {
  1342.                   GLushort *ussrc = (GLushort *) src;
  1343.                   for (j=0;j<width;j++) {
  1344.                      GLushort p = ussrc[j];
  1345.                      *dst++ = ((p >> 12)      ) * (1.0F / 15.0F); /* red */
  1346.                      *dst++ = ((p >>  8) & 0xf) * (1.0F / 15.0F); /* green */
  1347.                      *dst++ = ((p >>  4) & 0xf) * (1.0F / 15.0F); /* blue */
  1348.                      *dst++ = ((p      ) & 0xf) * (1.0F / 15.0F); /* alpha */
  1349.                   }
  1350.                }
  1351.                break;
  1352.         case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  1353.                {
  1354.                   GLushort *ussrc = (GLushort *) src;
  1355.                   for (j=0;j<width;j++) {
  1356.                      GLushort p = ussrc[j];
  1357.                      *dst++ = ((p      ) & 0xf) * (1.0F / 15.0F); /* red */
  1358.                      *dst++ = ((p >>  4) & 0xf) * (1.0F / 15.0F); /* green */
  1359.                      *dst++ = ((p >>  8) & 0xf) * (1.0F / 15.0F); /* blue */
  1360.                      *dst++ = ((p >> 12)      ) * (1.0F / 15.0F); /* alpha */
  1361.                   }
  1362.                }
  1363.                break;
  1364.         case GL_UNSIGNED_SHORT_5_5_5_1:
  1365.                {
  1366.                   GLushort *ussrc = (GLushort *) src;
  1367.                   for (j=0;j<width;j++) {
  1368.                      GLushort p = ussrc[j];
  1369.                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* red */
  1370.                      *dst++ = ((p >>  6) & 0x1f) * (1.0F / 31.0F); /* green */
  1371.                      *dst++ = ((p >>  1) & 0x1f) * (1.0F / 31.0F); /* blue */
  1372.                      *dst++ = ((p      ) & 0x1)  * (1.0F /  1.0F); /* alpha */
  1373.                   }
  1374.                }
  1375.                break;
  1376.         case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  1377.                {
  1378.                   GLushort *ussrc = (GLushort *) src;
  1379.                   for (j=0;j<width;j++) {
  1380.                      GLushort p = ussrc[j];
  1381.                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* red */
  1382.                      *dst++ = ((p >>  5) & 0x1f) * (1.0F / 31.0F); /* green */
  1383.                      *dst++ = ((p >> 10) & 0x1f) * (1.0F / 31.0F); /* blue */
  1384.                      *dst++ = ((p >> 15)       ) * (1.0F /  1.0F); /* alpha */
  1385.                   }
  1386.                }
  1387.                break;
  1388.         case GL_UNSIGNED_INT_8_8_8_8:
  1389.                {
  1390.                   GLuint *uisrc = (GLuint *) src;
  1391.                   for (j=0;j<width;j++) {
  1392.                      GLuint p = uisrc[j];
  1393.                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
  1394.                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
  1395.                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
  1396.                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
  1397.                   }
  1398.                }
  1399.                break;
  1400.         case GL_UNSIGNED_INT_8_8_8_8_REV:
  1401.                {
  1402.                   GLuint *uisrc = (GLuint *) src;
  1403.                   for (j=0;j<width;j++) {
  1404.                      GLuint p = uisrc[j];
  1405.                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
  1406.                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
  1407.                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
  1408.                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       ); 
  1409.                   }
  1410.                }
  1411.                break;
  1412.         case GL_UNSIGNED_INT_10_10_10_2:
  1413.                {
  1414.                   GLuint *uisrc = (GLuint *) src;
  1415.                   for (j=0;j<width;j++) {
  1416.                      GLuint p = uisrc[j];
  1417.                      *dst++ = ((p >> 22)        ) * (1.0F / 1023.0F); /* r */
  1418.                      *dst++ = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F); /* g */
  1419.                      *dst++ = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F); /* b */
  1420.                      *dst++ = ((p      ) & 0x3  ) * (1.0F /    3.0F); /* a */
  1421.                   }
  1422.                }
  1423.                break;
  1424.         case GL_UNSIGNED_INT_2_10_10_10_REV:
  1425.                {
  1426.                   GLuint *uisrc = (GLuint *) src;
  1427.                   for (j=0;j<width;j++) {
  1428.                      GLuint p = uisrc[j];
  1429.                      *dst++ = ((p      ) & 0x3ff) * (1.0F / 1023.0F); /* r*/
  1430.                      *dst++ = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F); /* g */
  1431.                      *dst++ = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F); /* b */
  1432.                      *dst++ = ((p >> 30)        ) * (1.0F /    3.0F); /* a */
  1433.                   }
  1434.                }
  1435.                break;
  1436.             default:
  1437.                gl_problem(ctx, "unpack_float_image type" );
  1438.                return image;
  1439.          }
  1440.       }
  1441.    }
  1442.  
  1443.    if (format == GL_BGR) {
  1444.       /* swap order of every float triplet from BGR to RGBA */
  1445.       GLfloat *buffer = (GLfloat *) image->Data;
  1446.       for (i=0; i<width*height*depth; i++) {
  1447.          GLfloat b = buffer[i*3+0];
  1448.          GLfloat r = buffer[i*3+2];
  1449.          buffer[i*3+0] = r;
  1450.          buffer[i*3+2] = b;
  1451.       }
  1452.    }
  1453.    else if (format == GL_BGRA) {
  1454.       /* swap order of every float quadruplet from BGRA to RGBA */
  1455.       GLfloat *buffer = (GLfloat *) image->Data;
  1456.       for (i=0; i<width*height*depth; i++) {
  1457.          GLfloat b = buffer[i*4+0];
  1458.          GLfloat r = buffer[i*4+2];
  1459.          buffer[i*4+0] = r;
  1460.          buffer[i*4+2] = b;
  1461.       }
  1462.    }
  1463.    else if (format == GL_ABGR_EXT) {
  1464.       /* swap order of every float quadruplet from ABGR to RGBA */
  1465.       GLfloat *buffer = (GLfloat *) image->Data;
  1466.       for (i=0; i<width*height*depth; i++) {
  1467.          GLfloat a = buffer[i*4+0];
  1468.          GLfloat b = buffer[i*4+1];
  1469.          GLfloat g = buffer[i*4+2];
  1470.          GLfloat r = buffer[i*4+3];
  1471.          buffer[i*4+0] = r;
  1472.          buffer[i*4+1] = g;
  1473.          buffer[i*4+2] = b;
  1474.          buffer[i*4+3] = a;
  1475.       }
  1476.    }
  1477.  
  1478.    return image;
  1479. }
  1480.  
  1481.  
  1482.  
  1483. /*
  1484.  * Unpack a bitmap image, using current glPixelStore parameters,
  1485.  * making a new gl_image.
  1486.  */
  1487. struct gl_image *gl_unpack_bitmap( GLcontext *ctx,
  1488.                                    GLsizei width, GLsizei height,
  1489.                                    const GLubyte *bitmap,
  1490.                                    const struct gl_pixelstore_attrib *packing )
  1491. {
  1492.    return gl_unpack_image( ctx, width, height,
  1493.                            GL_COLOR_INDEX, GL_BITMAP, bitmap, packing );
  1494. }
  1495.  
  1496.  
  1497.  
  1498. /*
  1499.  * Unpack a 2-D image from user's buffer.  Return pointer to new
  1500.  * gl_image struct.
  1501.  *
  1502.  * Input:  width, height - size in pixels
  1503.  *         format - format of incoming pixel data
  1504.  *         type - datatype of incoming pixel data
  1505.  *         pixels - pointer to unpacked image in user buffer
  1506.  */
  1507. struct gl_image *gl_unpack_image( GLcontext *ctx,
  1508.                                   GLint width, GLint height,
  1509.                                   GLenum format, GLenum type,
  1510.                                   const GLvoid *pixels,
  1511.                                   const struct gl_pixelstore_attrib *packing )
  1512.    return gl_unpack_image3D( ctx, width, height, 1,
  1513.                              format, type, pixels, packing );
  1514. }
  1515.  
  1516.  
  1517.  
  1518. /* 
  1519.  * Unpack a 1, 2 or 3-D image from user-supplied address, returning a
  1520.  * pointer to a new gl_image struct.
  1521.  * This function is always called by a higher-level unpack function such
  1522.  * as gl_unpack_texsubimage() or gl_unpack_bitmap().
  1523.  *
  1524.  * Input:  width, height, depth - size in pixels
  1525.  *         format - format of incoming pixel data
  1526.  *         type - datatype of incoming pixel data
  1527.  *         pixels - pointer to unpacked image.
  1528.  */
  1529. struct gl_image *gl_unpack_image3D( GLcontext *ctx,
  1530.                                     GLint width, GLint height, GLint depth,
  1531.                                     GLenum format, GLenum type,
  1532.                                     const GLvoid *pixels,
  1533.                                     const struct gl_pixelstore_attrib *packing)
  1534. {
  1535.    if (width <= 0 || height <= 0 || depth <= 0) {
  1536.       return alloc_error_image(width, height, depth, format, type);
  1537.    }
  1538.  
  1539.    if (type==GL_BITMAP) {
  1540.       if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
  1541.          return alloc_error_image(width, height, depth, format, type);
  1542.       }
  1543.       else {
  1544.          return unpack_bitmap( format, width, height, pixels, packing );
  1545.       }
  1546.    }
  1547.    else if (format==GL_DEPTH_COMPONENT) {
  1548.       /* TODO: pack as GLdepth values (GLushort or GLuint) */
  1549.       return unpack_depth_image( ctx, type, width, height, pixels, packing );
  1550.    }
  1551.    else if (format==GL_STENCIL_INDEX) {
  1552.       /* TODO: pack as GLstencil (GLubyte or GLushort) */
  1553.       return unpack_stencil_image( ctx, type, width, height, pixels, packing );
  1554.    }
  1555.    else if (type==GL_UNSIGNED_BYTE) {
  1556.       /* upack, convert to GLubytes */
  1557.       return unpack_ubyte_image( width, height, depth, format, pixels, packing );
  1558.    }
  1559.    else {
  1560.       /* upack, convert to floats */
  1561.       return unpack_float_image( ctx, width, height, depth,
  1562.                                  format, type, pixels, packing );
  1563.    }
  1564.  
  1565.    /* never get here */
  1566.    /*return NULL;*/
  1567. }
  1568.  
  1569.  
  1570. /*
  1571.  * Apply pixel-transfer operations (scale, bias, mapping) to a single row
  1572.  * of a gl_image.  Put resulting color components into result array.
  1573.  */
  1574. void gl_scale_bias_map_image_data( const GLcontext *ctx,
  1575.                                    const struct gl_image *image,
  1576.                                    GLint row, GLubyte result[] )
  1577. {
  1578.    GLint start, i;
  1579.  
  1580.    assert(ctx);
  1581.    assert(image);
  1582.    assert(result);
  1583.    assert(row >= 0);
  1584.  
  1585.    start = row * image->Width * image->Components;
  1586.  
  1587.    for (i=0; i < image->Width; i++) {
  1588.       GLint pos = start+i;
  1589.       GLfloat red, green, blue, alpha;
  1590.       if (image->Type == GL_UNSIGNED_BYTE) {
  1591.          const GLubyte *data = (GLubyte *) image->Data;
  1592.          switch (image->Format) {
  1593.             case GL_RED:
  1594.                red   = data[pos] * (1.0F/255.0F);
  1595.                green = 0;
  1596.                blue  = 0;
  1597.                alpha = 0;
  1598.                break;
  1599.             case GL_RGB:
  1600.                red   = data[pos*3+0] * (1.0F/255.0F);
  1601.                green = data[pos*3+1] * (1.0F/255.0F);
  1602.                blue  = data[pos*3+2] * (1.0F/255.0F);
  1603.                alpha = 0;
  1604.                break;
  1605.             default:
  1606.                gl_problem(ctx, "bad image format in gl_scale...image_data");
  1607.                return;
  1608.          }
  1609.       }
  1610.       else if (image->Type == GL_FLOAT) {
  1611.          const GLubyte *data = (GLubyte *) image->Data;
  1612.          switch (image->Format) {
  1613.             case GL_RED:
  1614.                red   = data[pos];
  1615.                green = 0;
  1616.                blue  = 0;
  1617.                alpha = 0;
  1618.                break;
  1619.             case GL_RGB:
  1620.                red   = data[pos*3+0];
  1621.                green = data[pos*3+1];
  1622.                blue  = data[pos*3+2];
  1623.                alpha = 0;
  1624.                break;
  1625.             default:
  1626.                gl_problem(ctx, "bad image format in gl_scale...image_data");
  1627.                return;
  1628.          }
  1629.       }
  1630.       else {
  1631.          gl_problem(ctx, "Bad image type in gl_scale_...image_data");
  1632.          return;
  1633.       }
  1634.  
  1635.       assert(red   >= 0.0 && red   <= 1.0);
  1636.       assert(green >= 0.0 && green <= 1.0);
  1637.       assert(blue  >= 0.0 && blue  <= 1.0);
  1638.       assert(alpha >= 0.0 && alpha <= 1.0);
  1639.  
  1640.       /*
  1641.       if (scale or bias) {
  1642.  
  1643.  
  1644.       }
  1645.       if (mapping) {
  1646.  
  1647.       }
  1648.       */
  1649.  
  1650.       result[i*4+0] = (GLubyte) (red   * 255.0);
  1651.       result[i*4+1] = (GLubyte) (green * 255.0);
  1652.       result[i*4+2] = (GLubyte) (blue  * 255.0);
  1653.       result[i*4+3] = (GLubyte) (alpha * 255.0);
  1654.    }
  1655. }
  1656.  
  1657.  
  1658.  
  1659. /*
  1660.  * Pack the given RGBA span into client memory at 'dest' address
  1661.  * in the given pixel format and type.
  1662.  * Optionally apply the enabled pixel transfer ops.
  1663.  * Pack into memory using the given packing params struct.
  1664.  * This is used by glReadPixels and glGetTexImage?D()
  1665.  * Input:  ctx - the context
  1666.  *         n - number of pixels in the span
  1667.  *         rgba - the pixels
  1668.  *         format - dest packing format
  1669.  *         type - dest packing datatype
  1670.  *         destination - destination packing address
  1671.  *         packing - pixel packing parameters
  1672.  *         applyTransferOps - apply scale/bias/lookup-table ops?
  1673.  */
  1674. void gl_pack_rgba_span( const GLcontext *ctx,
  1675.                         GLuint n, CONST GLubyte rgba[][4],
  1676.                         GLenum format, GLenum type, GLvoid *destination,
  1677.                         const struct gl_pixelstore_attrib *packing,
  1678.                         GLboolean applyTransferOps )
  1679. {
  1680.    /* Test for optimized case first */
  1681.    if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
  1682.        format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
  1683.       /* common simple case */
  1684.       MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) );
  1685.    }
  1686.    else if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
  1687.        format == GL_RGB && type == GL_UNSIGNED_BYTE) {
  1688.       /* common simple case */
  1689.       GLint i;
  1690.       GLubyte *dest = (GLubyte *) destination;
  1691.       for (i = 0; i < n; i++) {
  1692.          dest[0] = rgba[i][RCOMP];
  1693.          dest[1] = rgba[i][GCOMP];
  1694.          dest[2] = rgba[i][BCOMP];
  1695.          dest += 3;
  1696.       }
  1697.    }
  1698.    else {
  1699.       GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH];
  1700.       GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH];
  1701.       const GLfloat rscale = 1.0F / 255.0F;
  1702.       const GLfloat gscale = 1.0F / 255.0F;
  1703.       const GLfloat bscale = 1.0F / 255.0F;
  1704.       const GLfloat ascale = 1.0F / 255.0F;
  1705.       const GLint comps = gl_components_in_format(format);
  1706.       GLuint i;
  1707.  
  1708.       assert(n <= MAX_WIDTH);
  1709.  
  1710.       /* convert color components to floating point */
  1711.       for (i=0;i<n;i++) {
  1712.          red[i]   = rgba[i][RCOMP] * rscale;
  1713.          green[i] = rgba[i][GCOMP] * gscale;
  1714.          blue[i]  = rgba[i][BCOMP] * bscale;
  1715.          alpha[i] = rgba[i][ACOMP] * ascale;
  1716.       }
  1717.  
  1718.       /*
  1719.        * Apply scale, bias and lookup-tables if enabled.
  1720.        */
  1721.       if (applyTransferOps) {
  1722.          if (ctx->Pixel.ScaleOrBiasRGBA) {
  1723.             gl_scale_and_bias_color( ctx, n, red, green, blue, alpha );
  1724.          }
  1725.          if (ctx->Pixel.MapColorFlag) {
  1726.             gl_map_color( ctx, n, red, green, blue, alpha );
  1727.          }
  1728.       }
  1729.  
  1730.       if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
  1731.          for (i=0;i<n;i++) {
  1732.             GLfloat sum = red[i] + green[i] + blue[i];
  1733.             luminance[i] = CLAMP( sum, 0.0F, 1.0F );
  1734.          }
  1735.       }
  1736.  
  1737.       /*
  1738.        * Pack/store the pixels.  Ugh!  Lots of cases!!!
  1739.        */
  1740.       switch (type) {
  1741.          case GL_UNSIGNED_BYTE:
  1742.             {
  1743.                GLubyte *dst = (GLubyte *) destination;
  1744.                switch (format) {
  1745.                   case GL_RED:
  1746.                      for (i=0;i<n;i++)
  1747.                         dst[i] = FLOAT_TO_UBYTE(red[i]);
  1748.                      break;
  1749.                   case GL_GREEN:
  1750.                      for (i=0;i<n;i++)
  1751.                         dst[i] = FLOAT_TO_UBYTE(green[i]);
  1752.                      break;
  1753.                   case GL_BLUE:
  1754.                      for (i=0;i<n;i++)
  1755.                         dst[i] = FLOAT_TO_UBYTE(blue[i]);
  1756.                      break;
  1757.                   case GL_ALPHA:
  1758.                      for (i=0;i<n;i++)
  1759.                         dst[i] = FLOAT_TO_UBYTE(alpha[i]);
  1760.                      break;
  1761.                   case GL_LUMINANCE:
  1762.                      for (i=0;i<n;i++)
  1763.                         dst[i] = FLOAT_TO_UBYTE(luminance[i]);
  1764.                      break;
  1765.                   case GL_LUMINANCE_ALPHA:
  1766.                      for (i=0;i<n;i++) {
  1767.                         dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
  1768.                         dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
  1769.                      }
  1770.                      break;
  1771.                   case GL_RGB:
  1772.                      for (i=0;i<n;i++) {
  1773.                         dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
  1774.                         dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
  1775.                         dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
  1776.                      }
  1777.                      break;
  1778.                   case GL_RGBA:
  1779.                      for (i=0;i<n;i++) {
  1780.                         dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
  1781.                         dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
  1782.                         dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
  1783.                         dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
  1784.                      }
  1785.                      break;
  1786.                   case GL_BGR:
  1787.                      for (i=0;i<n;i++) {
  1788.                         dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
  1789.                         dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
  1790.                         dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
  1791.                      }
  1792.                      break;
  1793.                   case GL_BGRA:
  1794.                      for (i=0;i<n;i++) {
  1795.                         dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
  1796.                         dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
  1797.                         dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
  1798.                         dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
  1799.                      }
  1800.                      break;
  1801.                   case GL_ABGR_EXT:
  1802.                      for (i=0;i<n;i++) {
  1803.                         dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
  1804.                         dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
  1805.                         dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
  1806.                         dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
  1807.                      }
  1808.                      break;
  1809.                   default:
  1810.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1811.                }
  1812.         }
  1813.         break;
  1814.      case GL_BYTE:
  1815.             {
  1816.                GLbyte *dst = (GLbyte *) destination;
  1817.                switch (format) {
  1818.                   case GL_RED:
  1819.                      for (i=0;i<n;i++)
  1820.                         dst[i] = FLOAT_TO_BYTE(red[i]);
  1821.                      break;
  1822.                   case GL_GREEN:
  1823.                      for (i=0;i<n;i++)
  1824.                         dst[i] = FLOAT_TO_BYTE(green[i]);
  1825.                      break;
  1826.                   case GL_BLUE:
  1827.                      for (i=0;i<n;i++)
  1828.                         dst[i] = FLOAT_TO_BYTE(blue[i]);
  1829.                      break;
  1830.                   case GL_ALPHA:
  1831.                      for (i=0;i<n;i++)
  1832.                         dst[i] = FLOAT_TO_BYTE(alpha[i]);
  1833.                      break;
  1834.                   case GL_LUMINANCE:
  1835.                      for (i=0;i<n;i++)
  1836.                         dst[i] = FLOAT_TO_BYTE(luminance[i]);
  1837.                      break;
  1838.                   case GL_LUMINANCE_ALPHA:
  1839.                      for (i=0;i<n;i++) {
  1840.                         dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
  1841.                         dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
  1842.                      }
  1843.                      break;
  1844.                   case GL_RGB:
  1845.                      for (i=0;i<n;i++) {
  1846.                         dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
  1847.                         dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
  1848.                         dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
  1849.                      }
  1850.                      break;
  1851.                   case GL_RGBA:
  1852.                      for (i=0;i<n;i++) {
  1853.                         dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
  1854.                         dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
  1855.                         dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
  1856.                         dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
  1857.                      }
  1858.                      break;
  1859.                   case GL_BGR:
  1860.                      for (i=0;i<n;i++) {
  1861.                         dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
  1862.                         dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
  1863.                         dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
  1864.                      }
  1865.                      break;
  1866.                   case GL_BGRA:
  1867.                      for (i=0;i<n;i++) {
  1868.                         dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
  1869.                         dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
  1870.                         dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
  1871.                         dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
  1872.                      }
  1873.                   case GL_ABGR_EXT:
  1874.                      for (i=0;i<n;i++) {
  1875.                         dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
  1876.                         dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
  1877.                         dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
  1878.                         dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
  1879.                      }
  1880.                      break;
  1881.                   default:
  1882.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1883.                }
  1884.             }
  1885.         break;
  1886.      case GL_UNSIGNED_SHORT:
  1887.             {
  1888.                GLushort *dst = (GLushort *) destination;
  1889.                switch (format) {
  1890.                   case GL_RED:
  1891.                      for (i=0;i<n;i++)
  1892.                         dst[i] = FLOAT_TO_USHORT(red[i]);
  1893.                      break;
  1894.                   case GL_GREEN:
  1895.                      for (i=0;i<n;i++)
  1896.                         dst[i] = FLOAT_TO_USHORT(green[i]);
  1897.                      break;
  1898.                   case GL_BLUE:
  1899.                      for (i=0;i<n;i++)
  1900.                         dst[i] = FLOAT_TO_USHORT(blue[i]);
  1901.                      break;
  1902.                   case GL_ALPHA:
  1903.                      for (i=0;i<n;i++)
  1904.                         dst[i] = FLOAT_TO_USHORT(alpha[i]);
  1905.                      break;
  1906.                   case GL_LUMINANCE:
  1907.                      for (i=0;i<n;i++)
  1908.                         dst[i] = FLOAT_TO_USHORT(luminance[i]);
  1909.                      break;
  1910.                   case GL_LUMINANCE_ALPHA:
  1911.                      for (i=0;i<n;i++) {
  1912.                         dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
  1913.                         dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
  1914.                      }
  1915.                      break;
  1916.                   case GL_RGB:
  1917.                      for (i=0;i<n;i++) {
  1918.                         dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
  1919.                         dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
  1920.                         dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
  1921.                      }
  1922.                      break;
  1923.                   case GL_RGBA:
  1924.                      for (i=0;i<n;i++) {
  1925.                         dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
  1926.                         dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
  1927.                         dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
  1928.                         dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
  1929.                      }
  1930.                      break;
  1931.                   case GL_BGR:
  1932.                      for (i=0;i<n;i++) {
  1933.                         dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
  1934.                         dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
  1935.                         dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
  1936.                      }
  1937.                      break;
  1938.                   case GL_BGRA:
  1939.                      for (i=0;i<n;i++) {
  1940.                         dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
  1941.                         dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
  1942.                         dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
  1943.                         dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
  1944.                      }
  1945.                      break;
  1946.                   case GL_ABGR_EXT:
  1947.                      for (i=0;i<n;i++) {
  1948.                         dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
  1949.                         dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
  1950.                         dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
  1951.                         dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
  1952.                      }
  1953.                      break;
  1954.                   default:
  1955.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  1956.                }
  1957.                if (packing->SwapBytes) {
  1958.                   gl_swap2( (GLushort *) dst, n * comps);
  1959.                }
  1960.             }
  1961.         break;
  1962.      case GL_SHORT:
  1963.             {
  1964.                GLshort *dst = (GLshort *) destination;
  1965.                switch (format) {
  1966.                   case GL_RED:
  1967.                      for (i=0;i<n;i++)
  1968.                         dst[i] = FLOAT_TO_SHORT(red[i]);
  1969.                      break;
  1970.                   case GL_GREEN:
  1971.                      for (i=0;i<n;i++)
  1972.                         dst[i] = FLOAT_TO_SHORT(green[i]);
  1973.                      break;
  1974.                   case GL_BLUE:
  1975.                      for (i=0;i<n;i++)
  1976.                         dst[i] = FLOAT_TO_SHORT(blue[i]);
  1977.                      break;
  1978.                   case GL_ALPHA:
  1979.                      for (i=0;i<n;i++)
  1980.                         dst[i] = FLOAT_TO_SHORT(alpha[i]);
  1981.                      break;
  1982.                   case GL_LUMINANCE:
  1983.                      for (i=0;i<n;i++)
  1984.                         dst[i] = FLOAT_TO_SHORT(luminance[i]);
  1985.                      break;
  1986.                   case GL_LUMINANCE_ALPHA:
  1987.                      for (i=0;i<n;i++) {
  1988.                         dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
  1989.                         dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
  1990.                      }
  1991.                      break;
  1992.                   case GL_RGB:
  1993.                      for (i=0;i<n;i++) {
  1994.                         dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
  1995.                         dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
  1996.                         dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
  1997.                      }
  1998.                      break;
  1999.                   case GL_RGBA:
  2000.                      for (i=0;i<n;i++) {
  2001.                         dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
  2002.                         dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
  2003.                         dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
  2004.                         dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
  2005.                      }
  2006.                      break;
  2007.                   case GL_BGR:
  2008.                      for (i=0;i<n;i++) {
  2009.                         dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
  2010.                         dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
  2011.                         dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
  2012.                      }
  2013.                      break;
  2014.                   case GL_BGRA:
  2015.                      for (i=0;i<n;i++) {
  2016.                         dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
  2017.                         dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
  2018.                         dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
  2019.                         dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
  2020.                      }
  2021.                   case GL_ABGR_EXT:
  2022.                      for (i=0;i<n;i++) {
  2023.                         dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
  2024.                         dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
  2025.                         dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
  2026.                         dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
  2027.                      }
  2028.                      break;
  2029.                   default:
  2030.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  2031.                }
  2032.                if (packing->SwapBytes) {
  2033.                   gl_swap2( (GLushort *) dst, n * comps );
  2034.                }
  2035.             }
  2036.         break;
  2037.      case GL_UNSIGNED_INT:
  2038.             {
  2039.                GLuint *dst = (GLuint *) destination;
  2040.                switch (format) {
  2041.                   case GL_RED:
  2042.                      for (i=0;i<n;i++)
  2043.                         dst[i] = FLOAT_TO_UINT(red[i]);
  2044.                      break;
  2045.                   case GL_GREEN:
  2046.                      for (i=0;i<n;i++)
  2047.                         dst[i] = FLOAT_TO_UINT(green[i]);
  2048.                      break;
  2049.                   case GL_BLUE:
  2050.                      for (i=0;i<n;i++)
  2051.                         dst[i] = FLOAT_TO_UINT(blue[i]);
  2052.                      break;
  2053.                   case GL_ALPHA:
  2054.                      for (i=0;i<n;i++)
  2055.                         dst[i] = FLOAT_TO_UINT(alpha[i]);
  2056.                      break;
  2057.                   case GL_LUMINANCE:
  2058.                      for (i=0;i<n;i++)
  2059.                         dst[i] = FLOAT_TO_UINT(luminance[i]);
  2060.                      break;
  2061.                   case GL_LUMINANCE_ALPHA:
  2062.                      for (i=0;i<n;i++) {
  2063.                         dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
  2064.                         dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
  2065.                      }
  2066.                      break;
  2067.                   case GL_RGB:
  2068.                      for (i=0;i<n;i++) {
  2069.                         dst[i*3+0] = FLOAT_TO_UINT(red[i]);
  2070.                         dst[i*3+1] = FLOAT_TO_UINT(green[i]);
  2071.                         dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
  2072.                      }
  2073.                      break;
  2074.                   case GL_RGBA:
  2075.                      for (i=0;i<n;i++) {
  2076.                         dst[i*4+0] = FLOAT_TO_UINT(red[i]);
  2077.                         dst[i*4+1] = FLOAT_TO_UINT(green[i]);
  2078.                         dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
  2079.                         dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
  2080.                      }
  2081.                      break;
  2082.                   case GL_BGR:
  2083.                      for (i=0;i<n;i++) {
  2084.                         dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
  2085.                         dst[i*3+1] = FLOAT_TO_UINT(green[i]);
  2086.                         dst[i*3+2] = FLOAT_TO_UINT(red[i]);
  2087.                      }
  2088.                      break;
  2089.                   case GL_BGRA:
  2090.                      for (i=0;i<n;i++) {
  2091.                         dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
  2092.                         dst[i*4+1] = FLOAT_TO_UINT(green[i]);
  2093.                         dst[i*4+2] = FLOAT_TO_UINT(red[i]);
  2094.                         dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
  2095.                      }
  2096.                      break;
  2097.                   case GL_ABGR_EXT:
  2098.                      for (i=0;i<n;i++) {
  2099.                         dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
  2100.                         dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
  2101.                         dst[i*4+2] = FLOAT_TO_UINT(green[i]);
  2102.                         dst[i*4+3] = FLOAT_TO_UINT(red[i]);
  2103.                      }
  2104.                      break;
  2105.                   default:
  2106.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  2107.                }
  2108.                if (packing->SwapBytes) {
  2109.                   gl_swap4( (GLuint *) dst, n * comps );
  2110.                }
  2111.             }
  2112.         break;
  2113.      case GL_INT:
  2114.         {
  2115.                GLint *dst = (GLint *) destination;
  2116.                switch (format) {
  2117.                   case GL_RED:
  2118.                      for (i=0;i<n;i++)
  2119.                         dst[i] = FLOAT_TO_INT(red[i]);
  2120.                      break;
  2121.                   case GL_GREEN:
  2122.                      for (i=0;i<n;i++)
  2123.                         dst[i] = FLOAT_TO_INT(green[i]);
  2124.                      break;
  2125.                   case GL_BLUE:
  2126.                      for (i=0;i<n;i++)
  2127.                         dst[i] = FLOAT_TO_INT(blue[i]);
  2128.                      break;
  2129.                   case GL_ALPHA:
  2130.                      for (i=0;i<n;i++)
  2131.                         dst[i] = FLOAT_TO_INT(alpha[i]);
  2132.                      break;
  2133.                   case GL_LUMINANCE:
  2134.                      for (i=0;i<n;i++)
  2135.                         dst[i] = FLOAT_TO_INT(luminance[i]);
  2136.                      break;
  2137.                   case GL_LUMINANCE_ALPHA:
  2138.                      for (i=0;i<n;i++) {
  2139.                         dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
  2140.                         dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
  2141.                      }
  2142.                      break;
  2143.                   case GL_RGB:
  2144.                      for (i=0;i<n;i++) {
  2145.                         dst[i*3+0] = FLOAT_TO_INT(red[i]);
  2146.                         dst[i*3+1] = FLOAT_TO_INT(green[i]);
  2147.                         dst[i*3+2] = FLOAT_TO_INT(blue[i]);
  2148.                      }
  2149.                      break;
  2150.                   case GL_RGBA:
  2151.                      for (i=0;i<n;i++) {
  2152.                         dst[i*4+0] = FLOAT_TO_INT(red[i]);
  2153.                         dst[i*4+1] = FLOAT_TO_INT(green[i]);
  2154.                         dst[i*4+2] = FLOAT_TO_INT(blue[i]);
  2155.                         dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
  2156.                      }
  2157.                      break;
  2158.                   case GL_BGR:
  2159.                      for (i=0;i<n;i++) {
  2160.                         dst[i*3+0] = FLOAT_TO_INT(blue[i]);
  2161.                         dst[i*3+1] = FLOAT_TO_INT(green[i]);
  2162.                         dst[i*3+2] = FLOAT_TO_INT(red[i]);
  2163.                      }
  2164.                      break;
  2165.                   case GL_BGRA:
  2166.                      for (i=0;i<n;i++) {
  2167.                         dst[i*4+0] = FLOAT_TO_INT(blue[i]);
  2168.                         dst[i*4+1] = FLOAT_TO_INT(green[i]);
  2169.                         dst[i*4+2] = FLOAT_TO_INT(red[i]);
  2170.                         dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
  2171.                      }
  2172.                      break;
  2173.                   case GL_ABGR_EXT:
  2174.                      for (i=0;i<n;i++) {
  2175.                         dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
  2176.                         dst[i*4+1] = FLOAT_TO_INT(blue[i]);
  2177.                         dst[i*4+2] = FLOAT_TO_INT(green[i]);
  2178.                         dst[i*4+3] = FLOAT_TO_INT(red[i]);
  2179.                      }
  2180.                      break;
  2181.                   default:
  2182.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  2183.                }
  2184.            if (packing->SwapBytes) {
  2185.           gl_swap4( (GLuint *) dst, n * comps );
  2186.            }
  2187.         }
  2188.         break;
  2189.      case GL_FLOAT:
  2190.         {
  2191.                GLfloat *dst = (GLfloat *) destination;
  2192.                switch (format) {
  2193.                   case GL_RED:
  2194.                      for (i=0;i<n;i++)
  2195.                         dst[i] = red[i];
  2196.                      break;
  2197.                   case GL_GREEN:
  2198.                      for (i=0;i<n;i++)
  2199.                         dst[i] = green[i];
  2200.                      break;
  2201.                   case GL_BLUE:
  2202.                      for (i=0;i<n;i++)
  2203.                         dst[i] = blue[i];
  2204.                      break;
  2205.                   case GL_ALPHA:
  2206.                      for (i=0;i<n;i++)
  2207.                         dst[i] = alpha[i];
  2208.                      break;
  2209.                   case GL_LUMINANCE:
  2210.                      for (i=0;i<n;i++)
  2211.                         dst[i] = luminance[i];
  2212.                      break;
  2213.                   case GL_LUMINANCE_ALPHA:
  2214.                      for (i=0;i<n;i++) {
  2215.                         dst[i*2+0] = luminance[i];
  2216.                         dst[i*2+1] = alpha[i];
  2217.                      }
  2218.                      break;
  2219.                   case GL_RGB:
  2220.                      for (i=0;i<n;i++) {
  2221.                         dst[i*3+0] = red[i];
  2222.                         dst[i*3+1] = green[i];
  2223.                         dst[i*3+2] = blue[i];
  2224.                      }
  2225.                      break;
  2226.                   case GL_RGBA:
  2227.                      for (i=0;i<n;i++) {
  2228.                         dst[i*4+0] = red[i];
  2229.                         dst[i*4+1] = green[i];
  2230.                         dst[i*4+2] = blue[i];
  2231.                         dst[i*4+3] = alpha[i];
  2232.                      }
  2233.                      break;
  2234.                   case GL_BGR:
  2235.                      for (i=0;i<n;i++) {
  2236.                         dst[i*3+0] = blue[i];
  2237.                         dst[i*3+1] = green[i];
  2238.                         dst[i*3+2] = red[i];
  2239.                      }
  2240.                      break;
  2241.                   case GL_BGRA:
  2242.                      for (i=0;i<n;i++) {
  2243.                         dst[i*4+0] = blue[i];
  2244.                         dst[i*4+1] = green[i];
  2245.                         dst[i*4+2] = red[i];
  2246.                         dst[i*4+3] = alpha[i];
  2247.                      }
  2248.                      break;
  2249.                   case GL_ABGR_EXT:
  2250.                      for (i=0;i<n;i++) {
  2251.                         dst[i*4+0] = alpha[i];
  2252.                         dst[i*4+1] = blue[i];
  2253.                         dst[i*4+2] = green[i];
  2254.                         dst[i*4+3] = red[i];
  2255.                      }
  2256.                      break;
  2257.                   default:
  2258.                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
  2259.                }
  2260.            if (packing->SwapBytes) {
  2261.           gl_swap4( (GLuint *) dst, n * comps );
  2262.            }
  2263.         }
  2264.         break;
  2265.          case GL_UNSIGNED_BYTE_3_3_2:
  2266.             if (format == GL_RGB) {
  2267.                GLubyte *dst = (GLubyte *) destination;
  2268.                for (i=0;i<n;i++) {
  2269.                   dst[i] = (((GLint) (red[i]   * 7.0F)) << 5)
  2270.                          | (((GLint) (green[i] * 7.0F)) << 2)
  2271.                          | (((GLint) (blue[i]  * 3.0F))     );
  2272.                }
  2273.             }
  2274.             break;
  2275.          case GL_UNSIGNED_BYTE_2_3_3_REV:
  2276.             if (format == GL_RGB) {
  2277.                GLubyte *dst = (GLubyte *) destination;
  2278.                for (i=0;i<n;i++) {
  2279.                   dst[i] = (((GLint) (red[i]   * 7.0F))     )
  2280.                          | (((GLint) (green[i] * 7.0F)) << 3)
  2281.                          | (((GLint) (blue[i]  * 3.0F)) << 5);
  2282.                }
  2283.             }
  2284.             break;
  2285.          case GL_UNSIGNED_SHORT_5_6_5:
  2286.             if (format == GL_RGB) {
  2287.                GLushort *dst = (GLushort *) destination;
  2288.                for (i=0;i<n;i++) {
  2289.                   dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
  2290.                          | (((GLint) (green[i] * 63.0F)) <<  5)
  2291.                          | (((GLint) (blue[i]  * 31.0F))      );
  2292.                }
  2293.             }
  2294.             break;
  2295.          case GL_UNSIGNED_SHORT_5_6_5_REV:
  2296.             if (format == GL_RGB) {
  2297.                GLushort *dst = (GLushort *) destination;
  2298.                for (i=0;i<n;i++) {
  2299.                   dst[i] = (((GLint) (red[i]   * 31.0F))      )
  2300.                          | (((GLint) (green[i] * 63.0F)) <<  5)
  2301.                          | (((GLint) (blue[i]  * 31.0F)) << 11);
  2302.                }
  2303.             }
  2304.             break;
  2305.          case GL_UNSIGNED_SHORT_4_4_4_4:
  2306.             if (format == GL_RGB) {
  2307.                GLushort *dst = (GLushort *) destination;
  2308.                for (i=0;i<n;i++) {
  2309.                   dst[i] = (((GLint) (red[i]   * 15.0F)) << 12)
  2310.                          | (((GLint) (green[i] * 15.0F)) <<  8)
  2311.                          | (((GLint) (blue[i]  * 15.0F)) <<  4)
  2312.                          | (((GLint) (alpha[i] * 15.0F))      );
  2313.                }
  2314.             }
  2315.             break;
  2316.          case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  2317.             if (format == GL_RGB) {
  2318.                GLushort *dst = (GLushort *) destination;
  2319.                for (i=0;i<n;i++) {
  2320.                   dst[i] = (((GLint) (red[i]   * 15.0F))      )
  2321.                          | (((GLint) (green[i] * 15.0F)) <<  4)
  2322.                          | (((GLint) (blue[i]  * 15.0F)) <<  8)
  2323.                          | (((GLint) (alpha[i] * 15.0F)) << 12);
  2324.                }
  2325.             }
  2326.             break;
  2327.          case GL_UNSIGNED_SHORT_5_5_5_1:
  2328.             if (format == GL_RGB) {
  2329.                GLushort *dst = (GLushort *) destination;
  2330.                for (i=0;i<n;i++) {
  2331.                   dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
  2332.                          | (((GLint) (green[i] * 31.0F)) <<  6)
  2333.                          | (((GLint) (blue[i]  * 31.0F)) <<  1)
  2334.                          | (((GLint) (alpha[i] *  1.0F))      );
  2335.                }
  2336.             }
  2337.             break;
  2338.          case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  2339.             if (format == GL_RGB) {
  2340.                GLushort *dst = (GLushort *) destination;
  2341.                for (i=0;i<n;i++) {
  2342.                   dst[i] = (((GLint) (red[i]   * 31.0F))      )
  2343.                          | (((GLint) (green[i] * 31.0F)) <<  5)
  2344.                          | (((GLint) (blue[i]  * 31.0F)) << 10)
  2345.                          | (((GLint) (alpha[i] *  1.0F)) << 15);
  2346.                }
  2347.             }
  2348.             break;
  2349.          case GL_UNSIGNED_INT_8_8_8_8:
  2350.             if (format == GL_RGBA) {
  2351.                GLuint *dst = (GLuint *) destination;
  2352.                for (i=0;i<n;i++) {
  2353.                   dst[i] = (((GLuint) (red[i]   * 255.0F)) << 24)
  2354.                          | (((GLuint) (green[i] * 255.0F)) << 16)
  2355.                          | (((GLuint) (blue[i]  * 255.0F)) <<  8)
  2356.                          | (((GLuint) (alpha[i] * 255.0F))      );
  2357.                }
  2358.             }
  2359.             else if (format == GL_BGRA) {
  2360.                GLuint *dst = (GLuint *) destination;
  2361.                for (i=0;i<n;i++) {
  2362.                   dst[i] = (((GLuint) (blue[i]  * 255.0F)) << 24)
  2363.                          | (((GLuint) (green[i] * 255.0F)) << 16)
  2364.                          | (((GLuint) (red[i]   * 255.0F)) <<  8)
  2365.                          | (((GLuint) (alpha[i] * 255.0F))      );
  2366.                }
  2367.             }
  2368.             else if (format == GL_ABGR_EXT) {
  2369.                GLuint *dst = (GLuint *) destination;
  2370.                for (i=0;i<n;i++) {
  2371.                   dst[i] = (((GLuint) (alpha[i] * 255.0F)) << 24)
  2372.                          | (((GLuint) (blue[i]  * 255.0F)) << 16)
  2373.                          | (((GLuint) (green[i] * 255.0F)) <<  8)
  2374.                          | (((GLuint) (red[i]   * 255.0F))      );
  2375.                }
  2376.             }
  2377.             break;
  2378.          case GL_UNSIGNED_INT_8_8_8_8_REV:
  2379.             if (format == GL_RGBA) {
  2380.                GLuint *dst = (GLuint *) destination;
  2381.                for (i=0;i<n;i++) {
  2382.                   dst[i] = (((GLuint) (red[i]   * 255.0F))      )
  2383.                          | (((GLuint) (green[i] * 255.0F)) <<  8)
  2384.                          | (((GLuint) (blue[i]  * 255.0F)) << 16)
  2385.                          | (((GLuint) (alpha[i] * 255.0F)) << 24);
  2386.                }
  2387.             }
  2388.             else if (format == GL_BGRA) {
  2389.                GLuint *dst = (GLuint *) destination;
  2390.                for (i=0;i<n;i++) {
  2391.                   dst[i] = (((GLuint) (blue[i]  * 255.0F))      )
  2392.                          | (((GLuint) (green[i] * 255.0F)) <<  8)
  2393.                          | (((GLuint) (red[i]   * 255.0F)) << 16)
  2394.                          | (((GLuint) (alpha[i] * 255.0F)) << 24);
  2395.                }
  2396.             }
  2397.             else if (format == GL_ABGR_EXT) {
  2398.                GLuint *dst = (GLuint *) destination;
  2399.                for (i=0;i<n;i++) {
  2400.                   dst[i] = (((GLuint) (alpha[i] * 255.0F))      )
  2401.                          | (((GLuint) (blue[i]  * 255.0F)) <<  8)
  2402.                          | (((GLuint) (green[i] * 255.0F)) << 16)
  2403.                          | (((GLuint) (red[i]   * 255.0F)) << 24);
  2404.                }
  2405.             }
  2406.             break;
  2407.          case GL_UNSIGNED_INT_10_10_10_2:
  2408.             if (format == GL_RGBA) {
  2409.                GLuint *dst = (GLuint *) destination;
  2410.                for (i=0;i<n;i++) {
  2411.                   dst[i] = (((GLuint) (red[i]   * 1023.0F)) << 22)
  2412.                          | (((GLuint) (green[i] * 1023.0F)) << 12)
  2413.                          | (((GLuint) (blue[i]  * 1023.0F)) <<  2)
  2414.                          | (((GLuint) (alpha[i] *    3.0F))      );
  2415.                }
  2416.             }
  2417.             else if (format == GL_BGRA) {
  2418.                GLuint *dst = (GLuint *) destination;
  2419.                for (i=0;i<n;i++) {
  2420.                   dst[i] = (((GLuint) (blue[i]  * 1023.0F)) << 22)
  2421.                          | (((GLuint) (green[i] * 1023.0F)) << 12)
  2422.                          | (((GLuint) (red[i]   * 1023.0F)) <<  2)
  2423.                          | (((GLuint) (alpha[i] *    3.0F))      );
  2424.                }
  2425.             }
  2426.             else if (format == GL_ABGR_EXT) {
  2427.                GLuint *dst = (GLuint *) destination;
  2428.                for (i=0;i<n;i++) {
  2429.                   dst[i] = (((GLuint) (alpha[i] * 1023.0F)) << 22)
  2430.                          | (((GLuint) (blue[i]  * 1023.0F)) << 12)
  2431.                          | (((GLuint) (green[i] * 1023.0F)) <<  2)
  2432.                          | (((GLuint) (red[i]   *    3.0F))      );
  2433.                }
  2434.             }
  2435.             break;
  2436.          case GL_UNSIGNED_INT_2_10_10_10_REV:
  2437.             if (format == GL_RGBA) {
  2438.                GLuint *dst = (GLuint *) destination;
  2439.                for (i=0;i<n;i++) {
  2440.                   dst[i] = (((GLuint) (red[i]   * 1023.0F))      )
  2441.                          | (((GLuint) (green[i] * 1023.0F)) << 10)
  2442.                          | (((GLuint) (blue[i]  * 1023.0F)) << 20)
  2443.                          | (((GLuint) (alpha[i] *    3.0F)) << 30);
  2444.                }
  2445.             }
  2446.             else if (format == GL_BGRA) {
  2447.                GLuint *dst = (GLuint *) destination;
  2448.                for (i=0;i<n;i++) {
  2449.                   dst[i] = (((GLuint) (blue[i]  * 1023.0F))      )
  2450.                          | (((GLuint) (green[i] * 1023.0F)) << 10)
  2451.                          | (((GLuint) (red[i]   * 1023.0F)) << 20)
  2452.                          | (((GLuint) (alpha[i] *    3.0F)) << 30);
  2453.                }
  2454.             }
  2455.             else if (format == GL_ABGR_EXT) {
  2456.                GLuint *dst = (GLuint *) destination;
  2457.                for (i=0;i<n;i++) {
  2458.                   dst[i] = (((GLuint) (alpha[i] * 1023.0F))      )
  2459.                          | (((GLuint) (blue[i]  * 1023.0F)) << 10)
  2460.                          | (((GLuint) (green[i] * 1023.0F)) << 20)
  2461.                          | (((GLuint) (red[i]   *    3.0F)) << 30);
  2462.                }
  2463.             }
  2464.             break;
  2465.          default:
  2466.             gl_problem( ctx, "bad type in gl_pack_rgba_span" );
  2467.       }
  2468.    }
  2469. }
  2470.  
  2471.  
  2472.  
  2473. /*
  2474.  * New (3.3) functions
  2475.  */
  2476.  
  2477. #define SWAP2BYTE(VALUE)            \
  2478.    {                        \
  2479.       GLubyte *bytes = (GLubyte *) &(VALUE);    \
  2480.       GLubyte tmp = bytes[0];            \
  2481.       bytes[0] = bytes[1];            \
  2482.       bytes[1] = tmp;                \
  2483.    }
  2484.  
  2485. #define SWAP4BYTE(VALUE)            \
  2486.    {                        \
  2487.       GLubyte *bytes = (GLubyte *) &(VALUE);    \
  2488.       GLubyte tmp = bytes[0];            \
  2489.       bytes[0] = bytes[3];            \
  2490.       bytes[3] = tmp;                \
  2491.       tmp = bytes[1];                \
  2492.       bytes[1] = bytes[2];            \
  2493.       bytes[2] = tmp;                \
  2494.    }
  2495.  
  2496.  
  2497. static void
  2498. extract_uint_indexes(GLuint n, GLuint indexes[],
  2499.                      GLenum srcFormat, GLenum srcType, const GLvoid *src,
  2500.                      const struct gl_pixelstore_attrib *unpack )
  2501. {
  2502.    assert(srcFormat == GL_COLOR_INDEX);
  2503.  
  2504.    ASSERT(srcType == GL_BITMAP ||
  2505.           srcType == GL_UNSIGNED_BYTE ||
  2506.           srcType == GL_BYTE ||
  2507.           srcType == GL_UNSIGNED_SHORT ||
  2508.           srcType == GL_SHORT ||
  2509.           srcType == GL_UNSIGNED_INT ||
  2510.           srcType == GL_INT ||
  2511.           srcType == GL_FLOAT);
  2512.  
  2513.    switch (srcType) {
  2514.       case GL_BITMAP:
  2515.          {
  2516.             GLubyte *ubsrc = (GLubyte *) src;
  2517.             if (unpack->LsbFirst) {
  2518.                GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
  2519.                GLuint i;
  2520.                for (i = 0; i < n; i++) {
  2521.                   indexes[i] = (*ubsrc & mask) ? 1 : 0;
  2522.                   if (mask == 128) {
  2523.                      mask = 1;
  2524.                      ubsrc++;
  2525.                   }
  2526.                   else {
  2527.                      mask = mask << 1;
  2528.                   }
  2529.                }
  2530.             }
  2531.             else {
  2532.                GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
  2533.                GLuint i;
  2534.                for (i = 0; i < n; i++) {
  2535.                   indexes[i] = (*ubsrc & mask) ? 1 : 0;
  2536.                   if (mask == 1) {
  2537.                      mask = 128;
  2538.                      ubsrc++;
  2539.                   }
  2540.                   else {
  2541.                      mask = mask >> 1;
  2542.                   }
  2543.                }
  2544.             }
  2545.          }
  2546.          break;
  2547.       case GL_UNSIGNED_BYTE:
  2548.          {
  2549.             GLuint i;
  2550.             const GLubyte *s = (const GLubyte *) src;
  2551.             for (i = 0; i < n; i++)
  2552.                indexes[i] = s[i];
  2553.          }
  2554.          break;
  2555.       case GL_BYTE:
  2556.          {
  2557.             GLuint i;
  2558.             const GLbyte *s = (const GLbyte *) src;
  2559.             for (i = 0; i < n; i++)
  2560.                indexes[i] = s[i];
  2561.          }
  2562.          break;
  2563.       case GL_UNSIGNED_SHORT:
  2564.          {
  2565.             GLuint i;
  2566.             const GLushort *s = (const GLushort *) src;
  2567.             if (unpack->SwapBytes) {
  2568.                for (i = 0; i < n; i++) {
  2569.                   GLushort value = s[i];
  2570.                   SWAP2BYTE(value);
  2571.                   indexes[i] = value;
  2572.                }
  2573.             }
  2574.             else {
  2575.                for (i = 0; i < n; i++)
  2576.                   indexes[i] = s[i];
  2577.             }
  2578.          }
  2579.          break;
  2580.       case GL_SHORT:
  2581.          {
  2582.             GLuint i;
  2583.             const GLshort *s = (const GLshort *) src;
  2584.             if (unpack->SwapBytes) {
  2585.                for (i = 0; i < n; i++) {
  2586.                   GLshort value = s[i];
  2587.                   SWAP2BYTE(value);
  2588.                   indexes[i] = value;
  2589.                }
  2590.             }
  2591.             else {
  2592.                for (i = 0; i < n; i++)
  2593.                   indexes[i] = s[i];
  2594.             }
  2595.          }
  2596.          break;
  2597.       case GL_UNSIGNED_INT:
  2598.          {
  2599.             GLuint i;
  2600.             const GLuint *s = (const GLuint *) src;
  2601.             if (unpack->SwapBytes) {
  2602.                for (i = 0; i < n; i++) {
  2603.                   GLuint value = s[i];
  2604.                   SWAP4BYTE(value);
  2605.                   indexes[i] = value;
  2606.                }
  2607.             }
  2608.             else {
  2609.                for (i = 0; i < n; i++)
  2610.                   indexes[i] = s[i];
  2611.             }
  2612.          }
  2613.          break;
  2614.       case GL_INT:
  2615.          {
  2616.             GLuint i;
  2617.             const GLint *s = (const GLint *) src;
  2618.             if (unpack->SwapBytes) {
  2619.                for (i = 0; i < n; i++) {
  2620.                   GLint value = s[i];
  2621.                   SWAP4BYTE(value);
  2622.                   indexes[i] = value;
  2623.                }
  2624.             }
  2625.             else {
  2626.                for (i = 0; i < n; i++)
  2627.                   indexes[i] = s[i];
  2628.             }
  2629.          }
  2630.          break;
  2631.       case GL_FLOAT:
  2632.          {
  2633.             GLuint i;
  2634.             const GLfloat *s = (const GLfloat *) src;
  2635.             if (unpack->SwapBytes) {
  2636.                for (i = 0; i < n; i++) {
  2637.                   GLfloat value = s[i];
  2638.                   SWAP4BYTE(value);
  2639.                   indexes[i] = value;
  2640.                }
  2641.             }
  2642.             else {
  2643.                for (i = 0; i < n; i++)
  2644.                   indexes[i] = s[i];
  2645.             }
  2646.          }
  2647.          break;
  2648.       default:
  2649.          gl_problem(NULL, "bad srcType in extract_uint_indexes");
  2650.          return;
  2651.    }
  2652. }
  2653.  
  2654.  
  2655.  
  2656. /*
  2657.  * This function extracts floating point RGBA values from arbitrary
  2658.  * image data.  srcFormat and srcType are the format and type parameters
  2659.  * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
  2660.  *
  2661.  * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
  2662.  * implements the "Conversion to floating point", "Conversion to RGB",
  2663.  * and "Final Expansion to RGBA" operations.
  2664.  *
  2665.  * Args:  n - number of pixels
  2666.  *        rgba - output colors
  2667.  *        srcFormat - format of incoming data
  2668.  *        srcType - datatype of incoming data
  2669.  *        src - source data pointer
  2670.  *        swapBytes - perform byteswapping of incoming data?
  2671.  */
  2672. static void
  2673. extract_float_rgba(GLuint n, GLfloat rgba[][4],
  2674.                    GLenum srcFormat, GLenum srcType, const GLvoid *src,
  2675.                    GLboolean swapBytes)
  2676. {
  2677.    GLint redIndex, greenIndex, blueIndex, alphaIndex;
  2678.    GLint stride;
  2679.    GLint rComp, bComp, gComp, aComp;
  2680.  
  2681.    if (0)
  2682.    {
  2683.       int i;
  2684.       for (i = 0; i<n;i++) {
  2685.          rgba[i][0] = rgba[i][1] = rgba[i][2] = rgba[i][3] = 0;
  2686.       }
  2687.       return;
  2688.    }
  2689.  
  2690.  
  2691.    ASSERT(srcFormat == GL_RED ||
  2692.           srcFormat == GL_GREEN ||
  2693.           srcFormat == GL_BLUE ||
  2694.           srcFormat == GL_ALPHA ||
  2695.           srcFormat == GL_LUMINANCE ||
  2696.           srcFormat == GL_LUMINANCE_ALPHA ||
  2697.           srcFormat == GL_INTENSITY ||
  2698.           srcFormat == GL_RGB ||
  2699.           srcFormat == GL_BGR ||
  2700.           srcFormat == GL_RGBA ||
  2701.           srcFormat == GL_BGRA ||
  2702.           srcFormat == GL_ABGR_EXT);
  2703.  
  2704.    ASSERT(srcType == GL_UNSIGNED_BYTE ||
  2705.           srcType == GL_BYTE ||
  2706.           srcType == GL_UNSIGNED_SHORT ||
  2707.           srcType == GL_SHORT ||
  2708.           srcType == GL_UNSIGNED_INT ||
  2709.           srcType == GL_INT ||
  2710.           srcType == GL_FLOAT ||
  2711.           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
  2712.           srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
  2713.           srcType == GL_UNSIGNED_SHORT_5_6_5 ||
  2714.           srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
  2715.           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
  2716.           srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
  2717.           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
  2718.           srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
  2719.           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
  2720.           srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
  2721.           srcType == GL_UNSIGNED_INT_10_10_10_2 ||
  2722.           srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
  2723.  
  2724.    switch (srcFormat) {
  2725.       case GL_RED:
  2726.          redIndex = 0;
  2727.          greenIndex = blueIndex = alphaIndex = -1;
  2728.          stride = 1;
  2729.          break;
  2730.       case GL_GREEN:
  2731.          greenIndex = 0;
  2732.          redIndex = blueIndex = alphaIndex = -1;
  2733.          stride = 1;
  2734.          break;
  2735.       case GL_BLUE:
  2736.          blueIndex = 0;
  2737.          redIndex = greenIndex = alphaIndex = -1;
  2738.          stride = 1;
  2739.          break;
  2740.       case GL_ALPHA:
  2741.          redIndex = greenIndex = blueIndex = -1;
  2742.          alphaIndex = 0;
  2743.          stride = 1;
  2744.          break;
  2745.       case GL_LUMINANCE: 
  2746.          redIndex = greenIndex = blueIndex = 0;
  2747.          alphaIndex = -1;
  2748.          stride = 1;
  2749.          break;
  2750.       case GL_LUMINANCE_ALPHA:
  2751.          redIndex = greenIndex = blueIndex = 0;
  2752.          alphaIndex = 1;
  2753.          stride = 2;
  2754.          break;
  2755.       case GL_INTENSITY:
  2756.          redIndex = 0;
  2757.          greenIndex = blueIndex = alphaIndex = -1;
  2758.          stride = 1;
  2759.          break;
  2760.       case GL_RGB:
  2761.          redIndex = 0;
  2762.          greenIndex = 1;
  2763.          blueIndex = 2;
  2764.          alphaIndex = -1;
  2765.          stride = 3;
  2766.          break;
  2767.       case GL_BGR:
  2768.          redIndex = 2;
  2769.          greenIndex = 1;
  2770.          blueIndex = 0;
  2771.          alphaIndex = -1;
  2772.          stride = 3;
  2773.          break;
  2774.       case GL_RGBA:
  2775.          redIndex = 0;
  2776.          greenIndex = 1;
  2777.          blueIndex = 2;
  2778.          alphaIndex = 3;
  2779.          rComp = 0;
  2780.          gComp = 1;
  2781.          bComp = 2;
  2782.          aComp = 3;
  2783.          stride = 4;
  2784.          break;
  2785.       case GL_BGRA:
  2786.          redIndex = 2;
  2787.          greenIndex = 1;
  2788.          blueIndex = 0;
  2789.          alphaIndex = 3;
  2790.          rComp = 2;
  2791.          gComp = 1;
  2792.          bComp = 0;
  2793.          aComp = 3;
  2794.          stride = 4;
  2795.          break;
  2796.       case GL_ABGR_EXT:
  2797.          redIndex = 3;
  2798.          greenIndex = 2;
  2799.          blueIndex = 1;
  2800.          alphaIndex = 0;
  2801.          rComp = 3;
  2802.          gComp = 2;
  2803.          bComp = 1;
  2804.          aComp = 0;
  2805.          stride = 4;
  2806.          break;
  2807.       default:
  2808.          gl_problem(NULL, "bad srcFormat in extract float data");
  2809.          return;
  2810.    }
  2811.  
  2812.    assert(redIndex >= -1 && redIndex <= 4);
  2813.    assert(greenIndex >= -1 && greenIndex <= 4);
  2814.    assert(blueIndex >= -1 && blueIndex <= 4);
  2815.    assert(alphaIndex >= -1 && alphaIndex <= 4);
  2816.  
  2817. #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)        \
  2818.    if ((INDEX) < 0) {                            \
  2819.       GLuint i;                                \
  2820.       for (i = 0; i < n; i++) {                        \
  2821.          rgba[i][CHANNEL] = DEFAULT;                    \
  2822.       }                                    \
  2823.    }                                    \
  2824.    else if (swapBytes) {                        \
  2825.       const TYPE *s = (const TYPE *) src;                \
  2826.       GLuint i;                                \
  2827.       for (i = 0; i < n; i++) {                        \
  2828.          TYPE value = s[INDEX];                        \
  2829.          if (sizeof(TYPE) == 2) {                    \
  2830.             SWAP2BYTE(value);                        \
  2831.          }                                \
  2832.          else if (sizeof(TYPE) == 4) {                    \
  2833.             SWAP4BYTE(value);                        \
  2834.          }                                \
  2835.          rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);        \
  2836.          s += stride;                            \
  2837.       }                                    \
  2838.    }                                    \
  2839.    else {                                \
  2840.       const TYPE *s = (const TYPE *) src;                \
  2841.       GLuint i;                                \
  2842.       for (i = 0; i < n; i++) {                        \
  2843.          rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);        \
  2844.          s += stride;                            \
  2845.       }                                    \
  2846.    }
  2847.  
  2848.    switch (srcType) {
  2849.       case GL_UNSIGNED_BYTE:
  2850.          PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
  2851.          PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
  2852.          PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
  2853.          PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
  2854.          break;
  2855.       case GL_BYTE:
  2856.          PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
  2857.          PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
  2858.          PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
  2859.          PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
  2860.          break;
  2861.       case GL_UNSIGNED_SHORT:
  2862.          PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
  2863.          PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
  2864.          PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
  2865.          PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
  2866.          break;
  2867.       case GL_SHORT:
  2868.          PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
  2869.          PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
  2870.          PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
  2871.          PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
  2872.          break;
  2873.       case GL_UNSIGNED_INT:
  2874.          PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
  2875.          PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
  2876.          PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
  2877.          PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
  2878.          break;
  2879.       case GL_INT:
  2880.          PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
  2881.          PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
  2882.          PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
  2883.          PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
  2884.          break;
  2885.       case GL_FLOAT:
  2886.          PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
  2887.          PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
  2888.          PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
  2889.          PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
  2890.          break;
  2891.       case GL_UNSIGNED_BYTE_3_3_2:
  2892.          {
  2893.             const GLubyte *ubsrc = (const GLubyte *) src;
  2894.             GLuint i;
  2895.             for (i = 0; i < n; i ++) {
  2896.                GLubyte p = ubsrc[i];
  2897.                rgba[i][RCOMP] = ((p >> 5)      ) * (1.0F / 7.0F);
  2898.                rgba[i][GCOMP] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
  2899.                rgba[i][BCOMP] = ((p     ) & 0x3) * (1.0F / 3.0F);
  2900.                rgba[i][ACOMP] = 1.0F;
  2901.             }
  2902.          }
  2903.          break;
  2904.       case GL_UNSIGNED_BYTE_2_3_3_REV:
  2905.          {
  2906.             const GLubyte *ubsrc = (const GLubyte *) src;
  2907.             GLuint i;
  2908.             for (i = 0; i < n; i ++) {
  2909.                GLubyte p = ubsrc[i];
  2910.                rgba[i][RCOMP] = ((p     ) & 0x7) * (1.0F / 7.0F);
  2911.                rgba[i][GCOMP] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
  2912.                rgba[i][BCOMP] = ((p >> 6)      ) * (1.0F / 3.0F);
  2913.                rgba[i][ACOMP] = 1.0F;
  2914.             }
  2915.          }
  2916.          break;
  2917.       case GL_UNSIGNED_SHORT_5_6_5:
  2918.          if (swapBytes) {
  2919.             const GLushort *ussrc = (const GLushort *) src;
  2920.             GLuint i;
  2921.             for (i = 0; i < n; i ++) {
  2922.                GLushort p = ussrc[i];
  2923.                SWAP2BYTE(p);
  2924.                rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
  2925.                rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
  2926.                rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
  2927.                rgba[i][ACOMP] = 1.0F;
  2928.             }
  2929.          }
  2930.          else {
  2931.             const GLushort *ussrc = (const GLushort *) src;
  2932.             GLuint i;
  2933.             for (i = 0; i < n; i ++) {
  2934.                GLushort p = ussrc[i];
  2935.                rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
  2936.                rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
  2937.                rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
  2938.                rgba[i][ACOMP] = 1.0F;
  2939.             }
  2940.          }
  2941.          break;
  2942.       case GL_UNSIGNED_SHORT_5_6_5_REV:
  2943.          if (swapBytes) {
  2944.             const GLushort *ussrc = (const GLushort *) src;
  2945.             GLuint i;
  2946.             for (i = 0; i < n; i ++) {
  2947.                GLushort p = ussrc[i];
  2948.                SWAP2BYTE(p);
  2949.                rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
  2950.                rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
  2951.                rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
  2952.                rgba[i][ACOMP] = 1.0F;
  2953.             }
  2954.          }
  2955.          else {
  2956.             const GLushort *ussrc = (const GLushort *) src;
  2957.             GLuint i;
  2958.             for (i = 0; i < n; i ++) {
  2959.                GLushort p = ussrc[i];
  2960.                rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
  2961.                rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
  2962.                rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
  2963.                rgba[i][ACOMP] = 1.0F;
  2964.             }
  2965.          }
  2966.          break;
  2967.       case GL_UNSIGNED_SHORT_4_4_4_4:
  2968.          if (swapBytes) {
  2969.             const GLushort *ussrc = (const GLushort *) src;
  2970.             GLuint i;
  2971.             for (i = 0; i < n; i ++) {
  2972.                GLushort p = ussrc[i];
  2973.                SWAP2BYTE(p);
  2974.                rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
  2975.                rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
  2976.                rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
  2977.                rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
  2978.             }
  2979.          }
  2980.          else {
  2981.             const GLushort *ussrc = (const GLushort *) src;
  2982.             GLuint i;
  2983.             for (i = 0; i < n; i ++) {
  2984.                GLushort p = ussrc[i];
  2985.                rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
  2986.                rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
  2987.                rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
  2988.                rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
  2989.             }
  2990.          }
  2991.          break;
  2992.       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  2993.          if (swapBytes) {
  2994.             const GLushort *ussrc = (const GLushort *) src;
  2995.             GLuint i;
  2996.             for (i = 0; i < n; i ++) {
  2997.                GLushort p = ussrc[i];
  2998.                SWAP2BYTE(p);
  2999.                rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
  3000.                rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
  3001.                rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
  3002.                rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
  3003.             }
  3004.          }
  3005.          else {
  3006.             const GLushort *ussrc = (const GLushort *) src;
  3007.             GLuint i;
  3008.             for (i = 0; i < n; i ++) {
  3009.                GLushort p = ussrc[i];
  3010.                rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
  3011.                rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
  3012.                rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
  3013.                rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
  3014.             }
  3015.          }
  3016.          break;
  3017.       case GL_UNSIGNED_SHORT_5_5_5_1:
  3018.          if (swapBytes) {
  3019.             const GLushort *ussrc = (const GLushort *) src;
  3020.             GLuint i;
  3021.             for (i = 0; i < n; i ++) {
  3022.                GLushort p = ussrc[i];
  3023.                SWAP2BYTE(p);
  3024.                rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
  3025.                rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
  3026.                rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
  3027.                rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
  3028.             }
  3029.          }
  3030.          else {
  3031.             const GLushort *ussrc = (const GLushort *) src;
  3032.             GLuint i;
  3033.             for (i = 0; i < n; i ++) {
  3034.                GLushort p = ussrc[i];
  3035.                rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
  3036.                rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
  3037.                rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
  3038.                rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
  3039.             }
  3040.          }
  3041.          break;
  3042.       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  3043.          if (swapBytes) {
  3044.             const GLushort *ussrc = (const GLushort *) src;
  3045.             GLuint i;
  3046.             for (i = 0; i < n; i ++) {
  3047.                GLushort p = ussrc[i];
  3048.                SWAP2BYTE(p);
  3049.                rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
  3050.                rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
  3051.                rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
  3052.                rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
  3053.             }
  3054.          }
  3055.          else {
  3056.             const GLushort *ussrc = (const GLushort *) src;
  3057.             GLuint i;
  3058.             for (i = 0; i < n; i ++) {
  3059.                GLushort p = ussrc[i];
  3060.                rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
  3061.                rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
  3062.                rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
  3063.                rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
  3064.             }
  3065.          }
  3066.          break;
  3067.       case GL_UNSIGNED_INT_8_8_8_8:
  3068.          if (swapBytes) {
  3069.             const GLuint *uisrc = (const GLuint *) src;
  3070.             GLuint i;
  3071.             for (i = 0; i < n; i ++) {
  3072.                GLuint p = uisrc[i];
  3073.                rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
  3074.                rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
  3075.                rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
  3076.                rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
  3077.             }
  3078.          }
  3079.          else {
  3080.             const GLuint *uisrc = (const GLuint *) src;
  3081.             GLuint i;
  3082.             for (i = 0; i < n; i ++) {
  3083.                GLuint p = uisrc[i];
  3084.                rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
  3085.                rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
  3086.                rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
  3087.                rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
  3088.             }
  3089.          }
  3090.          break;
  3091.       case GL_UNSIGNED_INT_8_8_8_8_REV:
  3092.          if (swapBytes) {
  3093.             const GLuint *uisrc = (const GLuint *) src;
  3094.             GLuint i;
  3095.             for (i = 0; i < n; i ++) {
  3096.                GLuint p = uisrc[i];
  3097.                rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       ); 
  3098.                rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
  3099.                rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
  3100.                rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
  3101.             }
  3102.          }
  3103.          else {
  3104.             const GLuint *uisrc = (const GLuint *) src;
  3105.             GLuint i;
  3106.             for (i = 0; i < n; i ++) {
  3107.                GLuint p = uisrc[i];
  3108.                rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
  3109.                rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
  3110.                rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
  3111.                rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       ); 
  3112.             }
  3113.          }
  3114.          break;
  3115.       case GL_UNSIGNED_INT_10_10_10_2:
  3116.          if (swapBytes) {
  3117.             const GLuint *uisrc = (const GLuint *) src;
  3118.             GLuint i;
  3119.             for (i = 0; i < n; i ++) {
  3120.                GLuint p = uisrc[i];
  3121.                SWAP4BYTE(p);
  3122.                rgba[i][rComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
  3123.                rgba[i][gComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
  3124.                rgba[i][bComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
  3125.                rgba[i][aComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
  3126.             }
  3127.          }
  3128.          else {
  3129.             const GLuint *uisrc = (const GLuint *) src;
  3130.             GLuint i;
  3131.             for (i = 0; i < n; i ++) {
  3132.                GLuint p = uisrc[i];
  3133.                rgba[i][rComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
  3134.                rgba[i][gComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
  3135.                rgba[i][bComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
  3136.                rgba[i][aComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
  3137.             }
  3138.          }
  3139.          break;
  3140.       case GL_UNSIGNED_INT_2_10_10_10_REV:
  3141.          if (swapBytes) {
  3142.             const GLuint *uisrc = (const GLuint *) src;
  3143.             GLuint i;
  3144.             for (i = 0; i < n; i ++) {
  3145.                GLuint p = uisrc[i];
  3146.                SWAP4BYTE(p);
  3147.                rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
  3148.                rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
  3149.                rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
  3150.                rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
  3151.             }
  3152.          }
  3153.          else {
  3154.             const GLuint *uisrc = (const GLuint *) src;
  3155.             GLuint i;
  3156.             for (i = 0; i < n; i ++) {
  3157.                GLuint p = uisrc[i];
  3158.                rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
  3159.                rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
  3160.                rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
  3161.                rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
  3162.             }
  3163.          }
  3164.          break;
  3165.       default:
  3166.          gl_problem(NULL, "bad srcType in extract float data");
  3167.          break;
  3168.    }
  3169. }
  3170.  
  3171.  
  3172.  
  3173. /*
  3174.  * Unpack a row of color image data from a client buffer according to
  3175.  * the pixel unpacking parameters.  Apply any enabled pixel transfer
  3176.  * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
  3177.  * Return GLubyte values in the specified dest image format.
  3178.  * This is (or will be) used by glDrawPixels and glTexImage?D().
  3179.  * Input:  ctx - the context
  3180.  *         n - number of pixels in the span
  3181.  *         dstFormat - format of destination color array
  3182.  *         dest - the destination color array
  3183.  *         srcFormat - source image format
  3184.  *         srcType - source image  datatype
  3185.  *         source - source image pointer
  3186.  *         unpacking - pixel unpacking parameters
  3187.  *         applyTransferOps - apply scale/bias/lookup-table ops?
  3188.  *
  3189.  * XXX perhaps expand this to process whole images someday.
  3190.  */
  3191. void
  3192. _mesa_unpack_ubyte_color_span( const GLcontext *ctx,
  3193.                                GLuint n, GLenum dstFormat, GLubyte dest[],
  3194.                                GLenum srcFormat, GLenum srcType,
  3195.                                const GLvoid *source,
  3196.                                const struct gl_pixelstore_attrib *unpacking,
  3197.                                GLboolean applyTransferOps )
  3198. {
  3199.    ASSERT(dstFormat == GL_ALPHA ||
  3200.           dstFormat == GL_LUMINANCE || 
  3201.           dstFormat == GL_LUMINANCE_ALPHA ||
  3202.           dstFormat == GL_INTENSITY ||
  3203.           dstFormat == GL_RGB ||
  3204.           dstFormat == GL_RGBA ||
  3205.           dstFormat == GL_COLOR_INDEX);
  3206.  
  3207.    ASSERT(srcFormat == GL_RED ||
  3208.           srcFormat == GL_GREEN ||
  3209.           srcFormat == GL_BLUE ||
  3210.           srcFormat == GL_ALPHA ||
  3211.           srcFormat == GL_LUMINANCE ||
  3212.           srcFormat == GL_LUMINANCE_ALPHA ||
  3213.           srcFormat == GL_INTENSITY ||
  3214.           srcFormat == GL_RGB ||
  3215.           srcFormat == GL_BGR ||
  3216.           srcFormat == GL_RGBA ||
  3217.           srcFormat == GL_BGRA ||
  3218.           srcFormat == GL_ABGR_EXT ||
  3219.           srcFormat == GL_COLOR_INDEX);
  3220.  
  3221.    ASSERT(srcType == GL_BITMAP ||
  3222.           srcType == GL_UNSIGNED_BYTE ||
  3223.           srcType == GL_BYTE ||
  3224.           srcType == GL_UNSIGNED_SHORT ||
  3225.           srcType == GL_SHORT ||
  3226.           srcType == GL_UNSIGNED_INT ||
  3227.           srcType == GL_INT ||
  3228.           srcType == GL_FLOAT ||
  3229.           srcType == GL_UNSIGNED_BYTE_3_3_2 ||
  3230.           srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
  3231.           srcType == GL_UNSIGNED_SHORT_5_6_5 ||
  3232.           srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
  3233.           srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
  3234.           srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
  3235.           srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
  3236.           srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
  3237.           srcType == GL_UNSIGNED_INT_8_8_8_8 ||
  3238.           srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
  3239.           srcType == GL_UNSIGNED_INT_10_10_10_2 ||
  3240.           srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
  3241.  
  3242.    /* this is intended for RGBA mode */
  3243.    ASSERT(ctx->Visual->RGBAflag);
  3244.  
  3245.    applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
  3246.                         ctx->Pixel.MapColorFlag);
  3247.  
  3248.    /* Try simple cases first */
  3249.    if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE) {
  3250.       if (dstFormat == GL_RGBA) {
  3251.          if (srcFormat == GL_RGBA) {
  3252.             MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
  3253.             return;
  3254.          }
  3255.          else if (srcFormat == GL_RGB) {
  3256.             GLuint i;
  3257.             const GLubyte *src = (const GLubyte *) source;
  3258.             GLubyte *dst = dest;
  3259.             for (i = 0; i < n; i++) {
  3260.                dst[0] = src[0];
  3261.                dst[1] = src[1];
  3262.                dst[2] = src[2];
  3263.                dst[3] = 255;
  3264.                src += 3;
  3265.                dst += 4;
  3266.             }
  3267.             return;
  3268.          }
  3269.       }
  3270.       else if (dstFormat == GL_RGB) {
  3271.          if (srcFormat == GL_RGB) {
  3272.             MEMCPY( dest, source, n * 3 * sizeof(GLubyte) );
  3273.             return;
  3274.          }
  3275.          else if (srcFormat == GL_RGBA) {
  3276.             GLuint i;
  3277.             const GLubyte *src = (const GLubyte *) source;
  3278.             GLubyte *dst = dest;
  3279.             for (i = 0; i < n; i++) {
  3280.                dst[0] = src[0];
  3281.                dst[1] = src[1];
  3282.                dst[2] = src[2];
  3283.                src += 4;
  3284.                dst += 3;
  3285.             }
  3286.             return;
  3287.          }
  3288.       }
  3289.       else if (dstFormat == srcFormat) {
  3290.          GLint comps = gl_components_in_format(srcFormat);
  3291.          assert(comps > 0);
  3292.          MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
  3293.          return;
  3294.       }
  3295.    }
  3296.  
  3297.  
  3298.    {
  3299.       /* general solution */
  3300.       GLfloat rgba[MAX_WIDTH][4];
  3301.       GLint dstComponents;
  3302.       GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
  3303.       GLint dstLuminanceIndex, dstIntensityIndex;
  3304.  
  3305.       dstComponents = gl_components_in_format( dstFormat );
  3306.       /* source & dest image formats should have been error checked by now */
  3307.       assert(dstComponents > 0);
  3308.  
  3309.       /*
  3310.        * Extract image data and convert to RGBA floats
  3311.        */
  3312.       assert(n <= MAX_WIDTH);
  3313.       if (srcFormat == GL_COLOR_INDEX) {
  3314.          GLuint indexes[MAX_WIDTH];
  3315.          extract_uint_indexes(n, indexes, srcFormat, srcType, source,
  3316.                               unpacking);
  3317.  
  3318.          /* shift and offset indexes */
  3319.          gl_shift_and_offset_ci(ctx, n, indexes);
  3320.  
  3321.          if (dstFormat == GL_COLOR_INDEX) {
  3322.             if (applyTransferOps) {
  3323.                if (ctx->Pixel.MapColorFlag) {
  3324.                   /* Apply lookup table */
  3325.                   gl_map_ci(ctx, n, indexes);
  3326.                }
  3327.  
  3328.                if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
  3329.  
  3330.                }
  3331.             }
  3332.  
  3333.             /* convert to GLubyte and return */
  3334.             {
  3335.                GLuint i;
  3336.                for (i = 0; i < n; i++) {
  3337.                   dest[i] = (GLubyte) (indexes[i] & 0xff);
  3338.                }
  3339.             }
  3340.          }
  3341.          else {
  3342.             /* Convert indexes to RGBA */
  3343.             gl_map_ci_to_rgba_float(ctx, n, indexes, rgba);
  3344.          }
  3345.       }
  3346.       else {
  3347.          extract_float_rgba(n, rgba, srcFormat, srcType, source,
  3348.                             unpacking->SwapBytes);
  3349.  
  3350.          if (applyTransferOps) {
  3351.             /* scale and bias colors */
  3352.             gl_scale_and_bias_rgba_float(ctx, n, rgba);
  3353.  
  3354.             /* color table lookup */
  3355.             if (ctx->Pixel.MapColorFlag) {
  3356.                gl_map_rgba_float(ctx, n, rgba);
  3357.             }
  3358.          }
  3359.       }
  3360.  
  3361.  
  3362.       /*
  3363.        * XXX This is where more color table lookups, convolution,
  3364.        * histograms, minmax, color matrix, etc would take place if
  3365.        * implemented.
  3366.        * See figure 3.7 in the OpenGL 1.2 specification for more info.
  3367.        */
  3368.  
  3369.  
  3370.       /* clamp to [0,1] */
  3371.       {
  3372.          GLuint i;
  3373.          for (i = 0; i < n; i++) {
  3374.             rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
  3375.             rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
  3376.             rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
  3377.             rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
  3378.          }
  3379.       }
  3380.  
  3381.       /* Now determine which color channels we need to produce.
  3382.        * And determine the dest index (offset) within each color tuple.
  3383.        */
  3384.       switch (dstFormat) {
  3385.          case GL_ALPHA:
  3386.             dstAlphaIndex = 0;
  3387.             dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
  3388.             dstLuminanceIndex = dstIntensityIndex = -1;
  3389.             break;
  3390.          case GL_LUMINANCE: 
  3391.             dstLuminanceIndex = 0;
  3392.             dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
  3393.             dstIntensityIndex = -1;
  3394.             break;
  3395.          case GL_LUMINANCE_ALPHA:
  3396.             dstLuminanceIndex = 0;
  3397.             dstAlphaIndex = 1;
  3398.             dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
  3399.             dstIntensityIndex = -1;
  3400.             break;
  3401.          case GL_INTENSITY:
  3402.             dstIntensityIndex = 0;
  3403.             dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
  3404.             dstLuminanceIndex = -1;
  3405.             break;
  3406.          case GL_RGB:
  3407.             dstRedIndex = 0;
  3408.             dstGreenIndex = 1;
  3409.             dstBlueIndex = 2;
  3410.             dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
  3411.             break;
  3412.          case GL_RGBA:
  3413.             dstRedIndex = 0;
  3414.             dstGreenIndex = 1;
  3415.             dstBlueIndex = 2;
  3416.             dstAlphaIndex = 3;
  3417.             dstLuminanceIndex = dstIntensityIndex = -1;
  3418.             break;
  3419.          case GL_COLOR_INDEX:
  3420.             assert(0);
  3421.             break;
  3422.          default:
  3423.             gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
  3424.       }
  3425.  
  3426.  
  3427.       /* Now return the GLubyte data in the requested dstFormat */
  3428.       if (dstRedIndex >= 0) {
  3429.          GLubyte *dst = dest;
  3430.          GLuint i;
  3431.          for (i = 0; i < n; i++) {
  3432.             dst[dstRedIndex] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
  3433.             dst += dstComponents;
  3434.          }
  3435.       }
  3436.  
  3437.       if (dstGreenIndex >= 0) {
  3438.          GLubyte *dst = dest;
  3439.          GLuint i;
  3440.          for (i = 0; i < n; i++) {
  3441.             dst[dstGreenIndex] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
  3442.             dst += dstComponents;
  3443.          }
  3444.       }
  3445.  
  3446.       if (dstBlueIndex >= 0) {
  3447.          GLubyte *dst = dest;
  3448.          GLuint i;
  3449.          for (i = 0; i < n; i++) {
  3450.             dst[dstBlueIndex] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
  3451.             dst += dstComponents;
  3452.          }
  3453.       }
  3454.  
  3455.       if (dstAlphaIndex >= 0) {
  3456.          GLubyte *dst = dest;
  3457.          GLuint i;
  3458.          for (i = 0; i < n; i++) {
  3459.             dst[dstAlphaIndex] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
  3460.             dst += dstComponents;
  3461.          }
  3462.       }
  3463.  
  3464.       if (dstIntensityIndex >= 0) {
  3465.          GLubyte *dst = dest;
  3466.          GLuint i;
  3467.          assert(dstIntensityIndex == 0);
  3468.          assert(dstComponents == 1);
  3469.          for (i = 0; i < n; i++) {
  3470.             /* Intensity comes from red channel */
  3471.             dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
  3472.          }
  3473.       }
  3474.  
  3475.       if (dstLuminanceIndex >= 0) {
  3476.          GLubyte *dst = dest;
  3477.          GLuint i;
  3478.          assert(dstLuminanceIndex == 0);
  3479.          for (i = 0; i < n; i++) {
  3480.             /* Luminance comes from red channel */
  3481.             dst[0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
  3482.             dst += dstComponents;
  3483.          }
  3484.       }
  3485.    }
  3486. }
  3487.  
  3488.  
  3489.  
  3490. /*
  3491.  * Unpack a row of color index data from a client buffer according to
  3492.  * the pixel unpacking parameters.  Apply pixel transfer ops if enabled
  3493.  * and applyTransferOps is true.
  3494.  * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
  3495.  *
  3496.  * Args:  ctx - the context
  3497.  *        n - number of pixels
  3498.  *        dstType - destination datatype
  3499.  *        dest - destination array
  3500.  *        srcType - source pixel type
  3501.  *        source - source data pointer
  3502.  *        unpacking - pixel unpacking parameters
  3503.  *        applyTransferOps - apply offset/bias/lookup ops?
  3504.  */
  3505. void
  3506. _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
  3507.                          GLenum dstType, GLvoid *dest,
  3508.                          GLenum srcType, const GLvoid *source,
  3509.                          const struct gl_pixelstore_attrib *unpacking,
  3510.                          GLboolean applyTransferOps )
  3511. {
  3512.    ASSERT(srcType == GL_BITMAP ||
  3513.           srcType == GL_UNSIGNED_BYTE ||
  3514.           srcType == GL_BYTE ||
  3515.           srcType == GL_UNSIGNED_SHORT ||
  3516.           srcType == GL_SHORT ||
  3517.           srcType == GL_UNSIGNED_INT ||
  3518.           srcType == GL_INT ||
  3519.           srcType == GL_FLOAT);
  3520.  
  3521.    ASSERT(dstType == GL_UNSIGNED_BYTE ||
  3522.           dstType == GL_UNSIGNED_SHORT ||
  3523.           dstType == GL_UNSIGNED_INT);
  3524.  
  3525.    applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
  3526.  
  3527.    /*
  3528.     * Try simple cases first
  3529.     */
  3530.    if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
  3531.        && dstType == GL_UNSIGNED_BYTE) {
  3532.       MEMCPY(dest, source, n * sizeof(GLubyte));
  3533.    }
  3534.    else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
  3535.             && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
  3536.       MEMCPY(dest, source, n * sizeof(GLuint));
  3537.    }
  3538.    else {
  3539.       /*
  3540.        * general solution
  3541.        */
  3542.       GLuint indexes[MAX_WIDTH];
  3543.       assert(n <= MAX_WIDTH);
  3544.  
  3545.       extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
  3546.                            unpacking);
  3547.  
  3548.       if (applyTransferOps) {
  3549.          if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
  3550.             /* shift and offset indexes */
  3551.             gl_shift_and_offset_ci(ctx, n, indexes);
  3552.          }
  3553.  
  3554.          if (ctx->Pixel.MapColorFlag) {
  3555.             /* Apply lookup table */
  3556.             gl_map_ci(ctx, n, indexes);
  3557.          }
  3558.       }
  3559.  
  3560.       /* convert to dest type */
  3561.       switch (dstType) {
  3562.          case GL_UNSIGNED_BYTE:
  3563.             {
  3564.                GLubyte *dst = (GLubyte *) dest;
  3565.                GLuint i;
  3566.                for (i = 0; i < n; i++) {
  3567.                   dst[i] = (GLubyte) (indexes[i] & 0xff);
  3568.                }
  3569.             }
  3570.             break;
  3571.          case GL_UNSIGNED_SHORT:
  3572.             {
  3573.                GLuint *dst = (GLuint *) dest;
  3574.                GLuint i;
  3575.                for (i = 0; i < n; i++) {
  3576.                   dst[i] = (GLushort) (indexes[i] & 0xffff);
  3577.                }
  3578.             }
  3579.             break;
  3580.          case GL_UNSIGNED_INT:
  3581.             MEMCPY(dest, indexes, n * sizeof(GLuint));
  3582.             break;
  3583.          default:
  3584.             gl_problem(ctx, "bad dstType in _mesa_unpack_index_span");
  3585.       }
  3586.    }
  3587. }
  3588.  
  3589.  
  3590. /*
  3591.  * Unpack image data.  Apply byteswapping, byte flipping (bitmap).
  3592.  * Return all image data in a contiguous block.
  3593.  */
  3594. void *
  3595. _mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
  3596.                     GLenum format, GLenum type, const GLvoid *pixels,
  3597.                     const struct gl_pixelstore_attrib *unpack )
  3598. {
  3599.    GLint bytesPerRow, compsPerRow;
  3600.    GLboolean flipBytes, swap2, swap4;
  3601.  
  3602.    if (!pixels)
  3603.       return NULL;  /* not necessarily an error */
  3604.  
  3605.    if (width <= 0 || height <= 0 || depth <= 0)
  3606.       return NULL;  /* generate error later */
  3607.  
  3608.    if (format == GL_BITMAP) {
  3609.       bytesPerRow = (width + 7) >> 3;
  3610.       flipBytes = !unpack->LsbFirst;
  3611.       swap2 = swap4 = GL_FALSE;
  3612.       compsPerRow = 0;
  3613.    }
  3614.    else {
  3615.       const GLint bytesPerPixel = gl_bytes_per_pixel(format, type);
  3616.       const GLint components = gl_components_in_format(format);
  3617.       GLint bytesPerComp;
  3618.       if (bytesPerPixel <= 0 || components <= 0)
  3619.          return NULL;   /* bad format or type.  generate error later */
  3620.       bytesPerRow = bytesPerPixel * width;
  3621.       bytesPerComp = bytesPerPixel / components;
  3622.       flipBytes = GL_FALSE;
  3623.       swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
  3624.       swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
  3625.       compsPerRow = components * width;
  3626.       assert(compsPerRow >= width);
  3627.    }
  3628.  
  3629.    {
  3630.       GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
  3631.       GLubyte *dst;
  3632.       GLint img, row;
  3633.       if (!destBuffer)
  3634.          return NULL;   /* generate GL_OUT_OF_MEMORY later */
  3635.  
  3636.       dst = destBuffer;
  3637.       for (img = 0; img < depth; img++) {
  3638.          for (row = 0; row < height; row++) {
  3639.             const GLvoid *src = gl_pixel_addr_in_image(unpack, pixels,
  3640.                                width, height, format, type, img, row, 0);
  3641.             MEMCPY(dst, src, bytesPerRow);
  3642.             /* byte flipping/swapping */
  3643.             if (flipBytes) {
  3644.                gl_flip_bytes((GLubyte *) dst, bytesPerRow);
  3645.             }
  3646.             else if (swap2) {
  3647.                gl_swap2((GLushort*) dst, compsPerRow);
  3648.             }
  3649.             else if (swap4) {
  3650.                gl_swap4((GLuint*) dst, compsPerRow);
  3651.             }
  3652.             dst += bytesPerRow;
  3653.          }
  3654.       }
  3655.       return destBuffer;
  3656.    }
  3657. }
  3658.