Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  5.  * Copyright (C) 2009-2010  VMware, Inc.  All Rights Reserved.
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a
  8.  * copy of this software and associated documentation files (the "Software"),
  9.  * to deal in the Software without restriction, including without limitation
  10.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11.  * and/or sell copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included
  15.  * in all copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * THEA AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  21.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25.  
  26. /**
  27.  * \file pack.c
  28.  * Image and pixel span packing and unpacking.
  29.  */
  30.  
  31.  
  32. /*
  33.  * XXX: MSVC takes forever to compile this module for x86_64 unless we disable
  34.  * this global optimization.
  35.  *
  36.  * See also:
  37.  * - http://msdn.microsoft.com/en-us/library/1yk3ydd7.aspx
  38.  * - http://msdn.microsoft.com/en-us/library/chh3fb0k.aspx
  39.  */
  40. #if defined(_MSC_VER) && defined(_M_X64)
  41. #  pragma optimize( "g", off )
  42. #endif
  43.  
  44.  
  45. #include "glheader.h"
  46. #include "colormac.h"
  47. #include "enums.h"
  48. #include "image.h"
  49. #include "imports.h"
  50. #include "macros.h"
  51. #include "mtypes.h"
  52. #include "pack.h"
  53. #include "pixeltransfer.h"
  54. #include "imports.h"
  55. #include "glformats.h"
  56. #include "format_utils.h"
  57. #include "format_pack.h"
  58.  
  59.  
  60. /**
  61.  * Flip the 8 bits in each byte of the given array.
  62.  *
  63.  * \param p array.
  64.  * \param n number of bytes.
  65.  *
  66.  * \todo try this trick to flip bytes someday:
  67.  * \code
  68.  *  v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
  69.  *  v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
  70.  *  v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
  71.  * \endcode
  72.  */
  73. static void
  74. flip_bytes( GLubyte *p, GLuint n )
  75. {
  76.    GLuint i, a, b;
  77.    for (i = 0; i < n; i++) {
  78.       b = (GLuint) p[i];        /* words are often faster than bytes */
  79.       a = ((b & 0x01) << 7) |
  80.           ((b & 0x02) << 5) |
  81.           ((b & 0x04) << 3) |
  82.           ((b & 0x08) << 1) |
  83.           ((b & 0x10) >> 1) |
  84.           ((b & 0x20) >> 3) |
  85.           ((b & 0x40) >> 5) |
  86.           ((b & 0x80) >> 7);
  87.       p[i] = (GLubyte) a;
  88.    }
  89. }
  90.  
  91.  
  92.  
  93. /*
  94.  * Unpack a 32x32 pixel polygon stipple from user memory using the
  95.  * current pixel unpack settings.
  96.  */
  97. void
  98. _mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
  99.                               const struct gl_pixelstore_attrib *unpacking )
  100. {
  101.    GLubyte *ptrn = (GLubyte *) _mesa_unpack_image(2, 32, 32, 1, GL_COLOR_INDEX,
  102.                                                   GL_BITMAP, pattern, unpacking);
  103.    if (ptrn) {
  104.       /* Convert pattern from GLubytes to GLuints and handle big/little
  105.        * endian differences
  106.        */
  107.       GLubyte *p = ptrn;
  108.       GLint i;
  109.       for (i = 0; i < 32; i++) {
  110.          dest[i] = (p[0] << 24)
  111.                  | (p[1] << 16)
  112.                  | (p[2] <<  8)
  113.                  | (p[3]      );
  114.          p += 4;
  115.       }
  116.       free(ptrn);
  117.    }
  118. }
  119.  
  120.  
  121. /*
  122.  * Pack polygon stipple into user memory given current pixel packing
  123.  * settings.
  124.  */
  125. void
  126. _mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
  127.                             const struct gl_pixelstore_attrib *packing )
  128. {
  129.    /* Convert pattern from GLuints to GLubytes to handle big/little
  130.     * endian differences.
  131.     */
  132.    GLubyte ptrn[32*4];
  133.    GLint i;
  134.    for (i = 0; i < 32; i++) {
  135.       ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
  136.       ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
  137.       ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
  138.       ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
  139.    }
  140.  
  141.    _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
  142. }
  143.  
  144.  
  145. /*
  146.  * Pack bitmap data.
  147.  */
  148. void
  149. _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
  150.                    GLubyte *dest, const struct gl_pixelstore_attrib *packing )
  151. {
  152.    GLint row, width_in_bytes;
  153.    const GLubyte *src;
  154.  
  155.    if (!source)
  156.       return;
  157.  
  158.    width_in_bytes = DIV_ROUND_UP( width, 8 );
  159.    src = source;
  160.    for (row = 0; row < height; row++) {
  161.       GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
  162.                        width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
  163.       if (!dst)
  164.          return;
  165.  
  166.       if ((packing->SkipPixels & 7) == 0) {
  167.          memcpy( dst, src, width_in_bytes );
  168.          if (packing->LsbFirst) {
  169.             flip_bytes( dst, width_in_bytes );
  170.          }
  171.       }
  172.       else {
  173.          /* handling SkipPixels is a bit tricky (no pun intended!) */
  174.          GLint i;
  175.          if (packing->LsbFirst) {
  176.             GLubyte srcMask = 128;
  177.             GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
  178.             const GLubyte *s = src;
  179.             GLubyte *d = dst;
  180.             *d = 0;
  181.             for (i = 0; i < width; i++) {
  182.                if (*s & srcMask) {
  183.                   *d |= dstMask;
  184.                }
  185.                if (srcMask == 1) {
  186.                   srcMask = 128;
  187.                   s++;
  188.                }
  189.                else {
  190.                   srcMask = srcMask >> 1;
  191.                }
  192.                if (dstMask == 128) {
  193.                   dstMask = 1;
  194.                   d++;
  195.                   *d = 0;
  196.                }
  197.                else {
  198.                   dstMask = dstMask << 1;
  199.                }
  200.             }
  201.          }
  202.          else {
  203.             GLubyte srcMask = 128;
  204.             GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
  205.             const GLubyte *s = src;
  206.             GLubyte *d = dst;
  207.             *d = 0;
  208.             for (i = 0; i < width; i++) {
  209.                if (*s & srcMask) {
  210.                   *d |= dstMask;
  211.                }
  212.                if (srcMask == 1) {
  213.                   srcMask = 128;
  214.                   s++;
  215.                }
  216.                else {
  217.                   srcMask = srcMask >> 1;
  218.                }
  219.                if (dstMask == 1) {
  220.                   dstMask = 128;
  221.                   d++;
  222.                   *d = 0;
  223.                }
  224.                else {
  225.                   dstMask = dstMask >> 1;
  226.                }
  227.             }
  228.          }
  229.       }
  230.       src += width_in_bytes;
  231.    }
  232. }
  233.  
  234.  
  235. #define SWAP2BYTE(VALUE)                        \
  236.    {                                            \
  237.       GLubyte *bytes = (GLubyte *) &(VALUE);    \
  238.       GLubyte tmp = bytes[0];                   \
  239.       bytes[0] = bytes[1];                      \
  240.       bytes[1] = tmp;                           \
  241.    }
  242.  
  243. #define SWAP4BYTE(VALUE)                        \
  244.    {                                            \
  245.       GLubyte *bytes = (GLubyte *) &(VALUE);    \
  246.       GLubyte tmp = bytes[0];                   \
  247.       bytes[0] = bytes[3];                      \
  248.       bytes[3] = tmp;                           \
  249.       tmp = bytes[1];                           \
  250.       bytes[1] = bytes[2];                      \
  251.       bytes[2] = tmp;                           \
  252.    }
  253.  
  254.  
  255. static void
  256. extract_uint_indexes(GLuint n, GLuint indexes[],
  257.                      GLenum srcFormat, GLenum srcType, const GLvoid *src,
  258.                      const struct gl_pixelstore_attrib *unpack )
  259. {
  260.    assert(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
  261.  
  262.    assert(srcType == GL_BITMAP ||
  263.           srcType == GL_UNSIGNED_BYTE ||
  264.           srcType == GL_BYTE ||
  265.           srcType == GL_UNSIGNED_SHORT ||
  266.           srcType == GL_SHORT ||
  267.           srcType == GL_UNSIGNED_INT ||
  268.           srcType == GL_INT ||
  269.           srcType == GL_UNSIGNED_INT_24_8_EXT ||
  270.           srcType == GL_HALF_FLOAT_ARB ||
  271.           srcType == GL_HALF_FLOAT_OES ||
  272.           srcType == GL_FLOAT ||
  273.           srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
  274.  
  275.    switch (srcType) {
  276.       case GL_BITMAP:
  277.          {
  278.             GLubyte *ubsrc = (GLubyte *) src;
  279.             if (unpack->LsbFirst) {
  280.                GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
  281.                GLuint i;
  282.                for (i = 0; i < n; i++) {
  283.                   indexes[i] = (*ubsrc & mask) ? 1 : 0;
  284.                   if (mask == 128) {
  285.                      mask = 1;
  286.                      ubsrc++;
  287.                   }
  288.                   else {
  289.                      mask = mask << 1;
  290.                   }
  291.                }
  292.             }
  293.             else {
  294.                GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
  295.                GLuint i;
  296.                for (i = 0; i < n; i++) {
  297.                   indexes[i] = (*ubsrc & mask) ? 1 : 0;
  298.                   if (mask == 1) {
  299.                      mask = 128;
  300.                      ubsrc++;
  301.                   }
  302.                   else {
  303.                      mask = mask >> 1;
  304.                   }
  305.                }
  306.             }
  307.          }
  308.          break;
  309.       case GL_UNSIGNED_BYTE:
  310.          {
  311.             GLuint i;
  312.             const GLubyte *s = (const GLubyte *) src;
  313.             for (i = 0; i < n; i++)
  314.                indexes[i] = s[i];
  315.          }
  316.          break;
  317.       case GL_BYTE:
  318.          {
  319.             GLuint i;
  320.             const GLbyte *s = (const GLbyte *) src;
  321.             for (i = 0; i < n; i++)
  322.                indexes[i] = s[i];
  323.          }
  324.          break;
  325.       case GL_UNSIGNED_SHORT:
  326.          {
  327.             GLuint i;
  328.             const GLushort *s = (const GLushort *) src;
  329.             if (unpack->SwapBytes) {
  330.                for (i = 0; i < n; i++) {
  331.                   GLushort value = s[i];
  332.                   SWAP2BYTE(value);
  333.                   indexes[i] = value;
  334.                }
  335.             }
  336.             else {
  337.                for (i = 0; i < n; i++)
  338.                   indexes[i] = s[i];
  339.             }
  340.          }
  341.          break;
  342.       case GL_SHORT:
  343.          {
  344.             GLuint i;
  345.             const GLshort *s = (const GLshort *) src;
  346.             if (unpack->SwapBytes) {
  347.                for (i = 0; i < n; i++) {
  348.                   GLshort value = s[i];
  349.                   SWAP2BYTE(value);
  350.                   indexes[i] = value;
  351.                }
  352.             }
  353.             else {
  354.                for (i = 0; i < n; i++)
  355.                   indexes[i] = s[i];
  356.             }
  357.          }
  358.          break;
  359.       case GL_UNSIGNED_INT:
  360.          {
  361.             GLuint i;
  362.             const GLuint *s = (const GLuint *) src;
  363.             if (unpack->SwapBytes) {
  364.                for (i = 0; i < n; i++) {
  365.                   GLuint value = s[i];
  366.                   SWAP4BYTE(value);
  367.                   indexes[i] = value;
  368.                }
  369.             }
  370.             else {
  371.                for (i = 0; i < n; i++)
  372.                   indexes[i] = s[i];
  373.             }
  374.          }
  375.          break;
  376.       case GL_INT:
  377.          {
  378.             GLuint i;
  379.             const GLint *s = (const GLint *) src;
  380.             if (unpack->SwapBytes) {
  381.                for (i = 0; i < n; i++) {
  382.                   GLint value = s[i];
  383.                   SWAP4BYTE(value);
  384.                   indexes[i] = value;
  385.                }
  386.             }
  387.             else {
  388.                for (i = 0; i < n; i++)
  389.                   indexes[i] = s[i];
  390.             }
  391.          }
  392.          break;
  393.       case GL_FLOAT:
  394.          {
  395.             GLuint i;
  396.             const GLfloat *s = (const GLfloat *) src;
  397.             if (unpack->SwapBytes) {
  398.                for (i = 0; i < n; i++) {
  399.                   GLfloat value = s[i];
  400.                   SWAP4BYTE(value);
  401.                   indexes[i] = (GLuint) value;
  402.                }
  403.             }
  404.             else {
  405.                for (i = 0; i < n; i++)
  406.                   indexes[i] = (GLuint) s[i];
  407.             }
  408.          }
  409.          break;
  410.       case GL_HALF_FLOAT_ARB:
  411.       case GL_HALF_FLOAT_OES:
  412.          {
  413.             GLuint i;
  414.             const GLhalfARB *s = (const GLhalfARB *) src;
  415.             if (unpack->SwapBytes) {
  416.                for (i = 0; i < n; i++) {
  417.                   GLhalfARB value = s[i];
  418.                   SWAP2BYTE(value);
  419.                   indexes[i] = (GLuint) _mesa_half_to_float(value);
  420.                }
  421.             }
  422.             else {
  423.                for (i = 0; i < n; i++)
  424.                   indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
  425.             }
  426.          }
  427.          break;
  428.       case GL_UNSIGNED_INT_24_8_EXT:
  429.          {
  430.             GLuint i;
  431.             const GLuint *s = (const GLuint *) src;
  432.             if (unpack->SwapBytes) {
  433.                for (i = 0; i < n; i++) {
  434.                   GLuint value = s[i];
  435.                   SWAP4BYTE(value);
  436.                   indexes[i] = value & 0xff;  /* lower 8 bits */
  437.                }
  438.             }
  439.             else {
  440.                for (i = 0; i < n; i++)
  441.                   indexes[i] = s[i] & 0xff;  /* lower 8 bits */
  442.             }
  443.          }
  444.          break;
  445.       case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
  446.          {
  447.             GLuint i;
  448.             const GLuint *s = (const GLuint *) src;
  449.             if (unpack->SwapBytes) {
  450.                for (i = 0; i < n; i++) {
  451.                   GLuint value = s[i*2+1];
  452.                   SWAP4BYTE(value);
  453.                   indexes[i] = value & 0xff;  /* lower 8 bits */
  454.                }
  455.             }
  456.             else {
  457.                for (i = 0; i < n; i++)
  458.                   indexes[i] = s[i*2+1] & 0xff;  /* lower 8 bits */
  459.             }
  460.          }
  461.          break;
  462.  
  463.       default:
  464.          _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
  465.          return;
  466.    }
  467. }
  468.  
  469.  
  470. static inline GLuint
  471. clamp_float_to_uint(GLfloat f)
  472. {
  473.    return f < 0.0F ? 0 : F_TO_I(f);
  474. }
  475.  
  476.  
  477. static inline GLuint
  478. clamp_half_to_uint(GLhalfARB h)
  479. {
  480.    GLfloat f = _mesa_half_to_float(h);
  481.    return f < 0.0F ? 0 : F_TO_I(f);
  482. }
  483.  
  484.  
  485. /*
  486.  * Unpack a row of stencil data from a client buffer according to
  487.  * the pixel unpacking parameters.
  488.  * This is (or will be) used by glDrawPixels
  489.  *
  490.  * Args:  ctx - the context
  491.  *        n - number of pixels
  492.  *        dstType - destination data type
  493.  *        dest - destination array
  494.  *        srcType - source pixel type
  495.  *        source - source data pointer
  496.  *        srcPacking - pixel unpacking parameters
  497.  *        transferOps - apply offset/bias/lookup ops?
  498.  */
  499. void
  500. _mesa_unpack_stencil_span( struct gl_context *ctx, GLuint n,
  501.                            GLenum dstType, GLvoid *dest,
  502.                            GLenum srcType, const GLvoid *source,
  503.                            const struct gl_pixelstore_attrib *srcPacking,
  504.                            GLbitfield transferOps )
  505. {
  506.    assert(srcType == GL_BITMAP ||
  507.           srcType == GL_UNSIGNED_BYTE ||
  508.           srcType == GL_BYTE ||
  509.           srcType == GL_UNSIGNED_SHORT ||
  510.           srcType == GL_SHORT ||
  511.           srcType == GL_UNSIGNED_INT ||
  512.           srcType == GL_INT ||
  513.           srcType == GL_UNSIGNED_INT_24_8_EXT ||
  514.           srcType == GL_HALF_FLOAT_ARB ||
  515.           srcType == GL_HALF_FLOAT_OES ||
  516.           srcType == GL_FLOAT ||
  517.           srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
  518.  
  519.    assert(dstType == GL_UNSIGNED_BYTE ||
  520.           dstType == GL_UNSIGNED_SHORT ||
  521.           dstType == GL_UNSIGNED_INT ||
  522.           dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
  523.  
  524.    /* only shift and offset apply to stencil */
  525.    transferOps &= IMAGE_SHIFT_OFFSET_BIT;
  526.  
  527.    /*
  528.     * Try simple cases first
  529.     */
  530.    if (transferOps == 0 &&
  531.        !ctx->Pixel.MapStencilFlag &&
  532.        srcType == GL_UNSIGNED_BYTE &&
  533.        dstType == GL_UNSIGNED_BYTE) {
  534.       memcpy(dest, source, n * sizeof(GLubyte));
  535.    }
  536.    else if (transferOps == 0 &&
  537.             !ctx->Pixel.MapStencilFlag &&
  538.             srcType == GL_UNSIGNED_INT &&
  539.             dstType == GL_UNSIGNED_INT &&
  540.             !srcPacking->SwapBytes) {
  541.       memcpy(dest, source, n * sizeof(GLuint));
  542.    }
  543.    else {
  544.       /*
  545.        * general solution
  546.        */
  547.       GLuint *indexes = malloc(n * sizeof(GLuint));
  548.  
  549.       if (!indexes) {
  550.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil unpacking");
  551.          return;
  552.       }
  553.  
  554.       extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
  555.                            srcPacking);
  556.  
  557.       if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
  558.          /* shift and offset indexes */
  559.          _mesa_shift_and_offset_ci(ctx, n, indexes);
  560.       }
  561.  
  562.       if (ctx->Pixel.MapStencilFlag) {
  563.          /* Apply stencil lookup table */
  564.          const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
  565.          GLuint i;
  566.          for (i = 0; i < n; i++) {
  567.             indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
  568.          }
  569.       }
  570.  
  571.       /* convert to dest type */
  572.       switch (dstType) {
  573.          case GL_UNSIGNED_BYTE:
  574.             {
  575.                GLubyte *dst = (GLubyte *) dest;
  576.                GLuint i;
  577.                for (i = 0; i < n; i++) {
  578.                   dst[i] = (GLubyte) (indexes[i] & 0xff);
  579.                }
  580.             }
  581.             break;
  582.          case GL_UNSIGNED_SHORT:
  583.             {
  584.                GLuint *dst = (GLuint *) dest;
  585.                GLuint i;
  586.                for (i = 0; i < n; i++) {
  587.                   dst[i] = (GLushort) (indexes[i] & 0xffff);
  588.                }
  589.             }
  590.             break;
  591.          case GL_UNSIGNED_INT:
  592.             memcpy(dest, indexes, n * sizeof(GLuint));
  593.             break;
  594.          case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
  595.             {
  596.                GLuint *dst = (GLuint *) dest;
  597.                GLuint i;
  598.                for (i = 0; i < n; i++) {
  599.                   dst[i*2+1] = indexes[i] & 0xff; /* lower 8 bits */
  600.                }
  601.             }
  602.             break;
  603.          default:
  604.             _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
  605.       }
  606.  
  607.       free(indexes);
  608.    }
  609. }
  610.  
  611.  
  612. void
  613. _mesa_pack_stencil_span( struct gl_context *ctx, GLuint n,
  614.                          GLenum dstType, GLvoid *dest, const GLubyte *source,
  615.                          const struct gl_pixelstore_attrib *dstPacking )
  616. {
  617.    GLubyte *stencil = malloc(n * sizeof(GLubyte));
  618.  
  619.    if (!stencil) {
  620.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "stencil packing");
  621.       return;
  622.    }
  623.  
  624.    if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
  625.        ctx->Pixel.MapStencilFlag) {
  626.       /* make a copy of input */
  627.       memcpy(stencil, source, n * sizeof(GLubyte));
  628.       _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
  629.       source = stencil;
  630.    }
  631.  
  632.    switch (dstType) {
  633.    case GL_UNSIGNED_BYTE:
  634.       memcpy(dest, source, n);
  635.       break;
  636.    case GL_BYTE:
  637.       {
  638.          GLbyte *dst = (GLbyte *) dest;
  639.          GLuint i;
  640.          for (i=0;i<n;i++) {
  641.             dst[i] = (GLbyte) (source[i] & 0x7f);
  642.          }
  643.       }
  644.       break;
  645.    case GL_UNSIGNED_SHORT:
  646.       {
  647.          GLushort *dst = (GLushort *) dest;
  648.          GLuint i;
  649.          for (i=0;i<n;i++) {
  650.             dst[i] = (GLushort) source[i];
  651.          }
  652.          if (dstPacking->SwapBytes) {
  653.             _mesa_swap2( (GLushort *) dst, n );
  654.          }
  655.       }
  656.       break;
  657.    case GL_SHORT:
  658.       {
  659.          GLshort *dst = (GLshort *) dest;
  660.          GLuint i;
  661.          for (i=0;i<n;i++) {
  662.             dst[i] = (GLshort) source[i];
  663.          }
  664.          if (dstPacking->SwapBytes) {
  665.             _mesa_swap2( (GLushort *) dst, n );
  666.          }
  667.       }
  668.       break;
  669.    case GL_UNSIGNED_INT:
  670.       {
  671.          GLuint *dst = (GLuint *) dest;
  672.          GLuint i;
  673.          for (i=0;i<n;i++) {
  674.             dst[i] = (GLuint) source[i];
  675.          }
  676.          if (dstPacking->SwapBytes) {
  677.             _mesa_swap4( (GLuint *) dst, n );
  678.          }
  679.       }
  680.       break;
  681.    case GL_INT:
  682.       {
  683.          GLint *dst = (GLint *) dest;
  684.          GLuint i;
  685.          for (i=0;i<n;i++) {
  686.             dst[i] = (GLint) source[i];
  687.          }
  688.          if (dstPacking->SwapBytes) {
  689.             _mesa_swap4( (GLuint *) dst, n );
  690.          }
  691.       }
  692.       break;
  693.    case GL_FLOAT:
  694.       {
  695.          GLfloat *dst = (GLfloat *) dest;
  696.          GLuint i;
  697.          for (i=0;i<n;i++) {
  698.             dst[i] = (GLfloat) source[i];
  699.          }
  700.          if (dstPacking->SwapBytes) {
  701.             _mesa_swap4( (GLuint *) dst, n );
  702.          }
  703.       }
  704.       break;
  705.    case GL_HALF_FLOAT_ARB:
  706.    case GL_HALF_FLOAT_OES:
  707.       {
  708.          GLhalfARB *dst = (GLhalfARB *) dest;
  709.          GLuint i;
  710.          for (i=0;i<n;i++) {
  711.             dst[i] = _mesa_float_to_half( (float) source[i] );
  712.          }
  713.          if (dstPacking->SwapBytes) {
  714.             _mesa_swap2( (GLushort *) dst, n );
  715.          }
  716.       }
  717.       break;
  718.    case GL_BITMAP:
  719.       if (dstPacking->LsbFirst) {
  720.          GLubyte *dst = (GLubyte *) dest;
  721.          GLint shift = 0;
  722.          GLuint i;
  723.          for (i = 0; i < n; i++) {
  724.             if (shift == 0)
  725.                *dst = 0;
  726.             *dst |= ((source[i] != 0) << shift);
  727.             shift++;
  728.             if (shift == 8) {
  729.                shift = 0;
  730.                dst++;
  731.             }
  732.          }
  733.       }
  734.       else {
  735.          GLubyte *dst = (GLubyte *) dest;
  736.          GLint shift = 7;
  737.          GLuint i;
  738.          for (i = 0; i < n; i++) {
  739.             if (shift == 7)
  740.                *dst = 0;
  741.             *dst |= ((source[i] != 0) << shift);
  742.             shift--;
  743.             if (shift < 0) {
  744.                shift = 7;
  745.                dst++;
  746.             }
  747.          }
  748.       }
  749.       break;
  750.    default:
  751.       _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
  752.    }
  753.  
  754.    free(stencil);
  755. }
  756.  
  757. #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT)                              \
  758.     do {                                                                \
  759.         GLuint i;                                                       \
  760.         const GLTYPE *src = (const GLTYPE *)source;                     \
  761.         for (i = 0; i < n; i++) {                                       \
  762.             GLTYPE value = src[i];                                      \
  763.             if (srcPacking->SwapBytes) {                                \
  764.                 if (sizeof(GLTYPE) == 2) {                              \
  765.                     SWAP2BYTE(value);                                   \
  766.                 } else if (sizeof(GLTYPE) == 4) {                       \
  767.                     SWAP4BYTE(value);                                   \
  768.                 }                                                       \
  769.             }                                                           \
  770.             depthValues[i] = GLTYPE2FLOAT(value);                       \
  771.         }                                                               \
  772.     } while (0)
  773.  
  774.  
  775. /**
  776.  * Unpack a row of depth/z values from memory, returning GLushort, GLuint
  777.  * or GLfloat values.
  778.  * The glPixelTransfer (scale/bias) params will be applied.
  779.  *
  780.  * \param dstType  one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
  781.  * \param depthMax  max value for returned GLushort or GLuint values
  782.  *                  (ignored for GLfloat).
  783.  */
  784. void
  785. _mesa_unpack_depth_span( struct gl_context *ctx, GLuint n,
  786.                          GLenum dstType, GLvoid *dest, GLuint depthMax,
  787.                          GLenum srcType, const GLvoid *source,
  788.                          const struct gl_pixelstore_attrib *srcPacking )
  789. {
  790.    GLfloat *depthTemp = NULL, *depthValues;
  791.    GLboolean needClamp = GL_FALSE;
  792.  
  793.    /* Look for special cases first.
  794.     * Not only are these faster, they're less prone to numeric conversion
  795.     * problems.  Otherwise, converting from an int type to a float then
  796.     * back to an int type can introduce errors that will show up as
  797.     * artifacts in things like depth peeling which uses glCopyTexImage.
  798.     */
  799.    if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
  800.       if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
  801.          const GLuint *src = (const GLuint *) source;
  802.          GLushort *dst = (GLushort *) dest;
  803.          GLuint i;
  804.          for (i = 0; i < n; i++) {
  805.             dst[i] = src[i] >> 16;
  806.          }
  807.          return;
  808.       }
  809.       if (srcType == GL_UNSIGNED_SHORT
  810.           && dstType == GL_UNSIGNED_INT
  811.           && depthMax == 0xffffffff) {
  812.          const GLushort *src = (const GLushort *) source;
  813.          GLuint *dst = (GLuint *) dest;
  814.          GLuint i;
  815.          for (i = 0; i < n; i++) {
  816.             dst[i] = src[i] | (src[i] << 16);
  817.          }
  818.          return;
  819.       }
  820.       if (srcType == GL_UNSIGNED_INT_24_8
  821.           && dstType == GL_UNSIGNED_INT
  822.           && depthMax == 0xffffff) {
  823.          const GLuint *src = (const GLuint *) source;
  824.          GLuint *dst = (GLuint *) dest;
  825.          GLuint i;
  826.          for (i = 0; i < n; i++) {
  827.             dst[i] = src[i] >> 8;
  828.          }
  829.          return;
  830.       }
  831.       /* XXX may want to add additional cases here someday */
  832.    }
  833.  
  834.    /* general case path follows */
  835.  
  836.    if (dstType == GL_FLOAT) {
  837.       depthValues = (GLfloat *) dest;
  838.    }
  839.    else {
  840.       depthTemp = malloc(n * sizeof(GLfloat));
  841.       if (!depthTemp) {
  842.          _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
  843.          return;
  844.       }
  845.  
  846.       depthValues = depthTemp;
  847.    }
  848.  
  849.    /* Convert incoming values to GLfloat.  Some conversions will require
  850.     * clamping, below.
  851.     */
  852.    switch (srcType) {
  853.       case GL_BYTE:
  854.          DEPTH_VALUES(GLbyte, BYTE_TO_FLOATZ);
  855.          needClamp = GL_TRUE;
  856.          break;
  857.       case GL_UNSIGNED_BYTE:
  858.          DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
  859.          break;
  860.       case GL_SHORT:
  861.          DEPTH_VALUES(GLshort, SHORT_TO_FLOATZ);
  862.          needClamp = GL_TRUE;
  863.          break;
  864.       case GL_UNSIGNED_SHORT:
  865.          DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
  866.          break;
  867.       case GL_INT:
  868.          DEPTH_VALUES(GLint, INT_TO_FLOAT);
  869.          needClamp = GL_TRUE;
  870.          break;
  871.       case GL_UNSIGNED_INT:
  872.          DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
  873.          break;
  874.       case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
  875.          if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
  876.              depthMax == 0xffffff &&
  877.              ctx->Pixel.DepthScale == 1.0 &&
  878.              ctx->Pixel.DepthBias == 0.0) {
  879.             const GLuint *src = (const GLuint *) source;
  880.             GLuint *zValues = (GLuint *) dest;
  881.             GLuint i;
  882.             for (i = 0; i < n; i++) {
  883.                 GLuint value = src[i];
  884.                 if (srcPacking->SwapBytes) {
  885.                     SWAP4BYTE(value);
  886.                 }
  887.                 zValues[i] = value & 0xffffff00;
  888.             }
  889.             free(depthTemp);
  890.             return;
  891.          }
  892.          else {
  893.             const GLuint *src = (const GLuint *) source;
  894.             const GLfloat scale = 1.0f / 0xffffff;
  895.             GLuint i;
  896.             for (i = 0; i < n; i++) {
  897.                 GLuint value = src[i];
  898.                 if (srcPacking->SwapBytes) {
  899.                     SWAP4BYTE(value);
  900.                 }
  901.                 depthValues[i] = (value >> 8) * scale;
  902.             }
  903.          }
  904.          break;
  905.       case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
  906.          {
  907.             GLuint i;
  908.             const GLfloat *src = (const GLfloat *)source;
  909.             for (i = 0; i < n; i++) {
  910.                GLfloat value = src[i * 2];
  911.                if (srcPacking->SwapBytes) {
  912.                   SWAP4BYTE(value);
  913.                }
  914.                depthValues[i] = value;
  915.             }
  916.             needClamp = GL_TRUE;
  917.          }
  918.          break;
  919.       case GL_FLOAT:
  920.          DEPTH_VALUES(GLfloat, 1*);
  921.          needClamp = GL_TRUE;
  922.          break;
  923.       case GL_HALF_FLOAT_ARB:
  924.       case GL_HALF_FLOAT_OES:
  925.          {
  926.             GLuint i;
  927.             const GLhalfARB *src = (const GLhalfARB *) source;
  928.             for (i = 0; i < n; i++) {
  929.                GLhalfARB value = src[i];
  930.                if (srcPacking->SwapBytes) {
  931.                   SWAP2BYTE(value);
  932.                }
  933.                depthValues[i] = _mesa_half_to_float(value);
  934.             }
  935.             needClamp = GL_TRUE;
  936.          }
  937.          break;
  938.       default:
  939.          _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
  940.          free(depthTemp);
  941.          return;
  942.    }
  943.  
  944.    /* apply depth scale and bias */
  945.    {
  946.       const GLfloat scale = ctx->Pixel.DepthScale;
  947.       const GLfloat bias = ctx->Pixel.DepthBias;
  948.       if (scale != 1.0 || bias != 0.0) {
  949.          GLuint i;
  950.          for (i = 0; i < n; i++) {
  951.             depthValues[i] = depthValues[i] * scale + bias;
  952.          }
  953.          needClamp = GL_TRUE;
  954.       }
  955.    }
  956.  
  957.    /* clamp to [0, 1] */
  958.    if (needClamp) {
  959.       GLuint i;
  960.       for (i = 0; i < n; i++) {
  961.          depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
  962.       }
  963.    }
  964.  
  965.    /*
  966.     * Convert values to dstType
  967.     */
  968.    if (dstType == GL_UNSIGNED_INT) {
  969.       GLuint *zValues = (GLuint *) dest;
  970.       GLuint i;
  971.       if (depthMax <= 0xffffff) {
  972.          /* no overflow worries */
  973.          for (i = 0; i < n; i++) {
  974.             zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
  975.          }
  976.       }
  977.       else {
  978.          /* need to use double precision to prevent overflow problems */
  979.          for (i = 0; i < n; i++) {
  980.             GLdouble z = depthValues[i] * (GLdouble) depthMax;
  981.             if (z >= (GLdouble) 0xffffffff)
  982.                zValues[i] = 0xffffffff;
  983.             else
  984.                zValues[i] = (GLuint) z;
  985.          }
  986.       }
  987.    }
  988.    else if (dstType == GL_UNSIGNED_SHORT) {
  989.       GLushort *zValues = (GLushort *) dest;
  990.       GLuint i;
  991.       assert(depthMax <= 0xffff);
  992.       for (i = 0; i < n; i++) {
  993.          zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
  994.       }
  995.    }
  996.    else if (dstType == GL_FLOAT) {
  997.       /* Nothing to do. depthValues is pointing to dest. */
  998.    }
  999.    else if (dstType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV) {
  1000.       GLfloat *zValues = (GLfloat*) dest;
  1001.       GLuint i;
  1002.       for (i = 0; i < n; i++) {
  1003.          zValues[i*2] = depthValues[i];
  1004.       }
  1005.    }
  1006.    else {
  1007.       assert(0);
  1008.    }
  1009.  
  1010.    free(depthTemp);
  1011. }
  1012.  
  1013.  
  1014. /*
  1015.  * Pack an array of depth values.  The values are floats in [0,1].
  1016.  */
  1017. void
  1018. _mesa_pack_depth_span( struct gl_context *ctx, GLuint n, GLvoid *dest,
  1019.                        GLenum dstType, const GLfloat *depthSpan,
  1020.                        const struct gl_pixelstore_attrib *dstPacking )
  1021. {
  1022.    GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
  1023.    if (!depthCopy) {
  1024.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
  1025.       return;
  1026.    }
  1027.  
  1028.    if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
  1029.       memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
  1030.       _mesa_scale_and_bias_depth(ctx, n, depthCopy);
  1031.       depthSpan = depthCopy;
  1032.    }
  1033.  
  1034.    switch (dstType) {
  1035.    case GL_UNSIGNED_BYTE:
  1036.       {
  1037.          GLubyte *dst = (GLubyte *) dest;
  1038.          GLuint i;
  1039.          for (i = 0; i < n; i++) {
  1040.             dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
  1041.          }
  1042.       }
  1043.       break;
  1044.    case GL_BYTE:
  1045.       {
  1046.          GLbyte *dst = (GLbyte *) dest;
  1047.          GLuint i;
  1048.          for (i = 0; i < n; i++) {
  1049.             dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
  1050.          }
  1051.       }
  1052.       break;
  1053.    case GL_UNSIGNED_SHORT:
  1054.       {
  1055.          GLushort *dst = (GLushort *) dest;
  1056.          GLuint i;
  1057.          for (i = 0; i < n; i++) {
  1058.             CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
  1059.          }
  1060.          if (dstPacking->SwapBytes) {
  1061.             _mesa_swap2( (GLushort *) dst, n );
  1062.          }
  1063.       }
  1064.       break;
  1065.    case GL_SHORT:
  1066.       {
  1067.          GLshort *dst = (GLshort *) dest;
  1068.          GLuint i;
  1069.          for (i = 0; i < n; i++) {
  1070.             dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
  1071.          }
  1072.          if (dstPacking->SwapBytes) {
  1073.             _mesa_swap2( (GLushort *) dst, n );
  1074.          }
  1075.       }
  1076.       break;
  1077.    case GL_UNSIGNED_INT:
  1078.       {
  1079.          GLuint *dst = (GLuint *) dest;
  1080.          GLuint i;
  1081.          for (i = 0; i < n; i++) {
  1082.             dst[i] = FLOAT_TO_UINT( depthSpan[i] );
  1083.          }
  1084.          if (dstPacking->SwapBytes) {
  1085.             _mesa_swap4( (GLuint *) dst, n );
  1086.          }
  1087.       }
  1088.       break;
  1089.    case GL_INT:
  1090.       {
  1091.          GLint *dst = (GLint *) dest;
  1092.          GLuint i;
  1093.          for (i = 0; i < n; i++) {
  1094.             dst[i] = FLOAT_TO_INT( depthSpan[i] );
  1095.          }
  1096.          if (dstPacking->SwapBytes) {
  1097.             _mesa_swap4( (GLuint *) dst, n );
  1098.          }
  1099.       }
  1100.       break;
  1101.    case GL_FLOAT:
  1102.       {
  1103.          GLfloat *dst = (GLfloat *) dest;
  1104.          GLuint i;
  1105.          for (i = 0; i < n; i++) {
  1106.             dst[i] = depthSpan[i];
  1107.          }
  1108.          if (dstPacking->SwapBytes) {
  1109.             _mesa_swap4( (GLuint *) dst, n );
  1110.          }
  1111.       }
  1112.       break;
  1113.    case GL_HALF_FLOAT_ARB:
  1114.    case GL_HALF_FLOAT_OES:
  1115.       {
  1116.          GLhalfARB *dst = (GLhalfARB *) dest;
  1117.          GLuint i;
  1118.          for (i = 0; i < n; i++) {
  1119.             dst[i] = _mesa_float_to_half(depthSpan[i]);
  1120.          }
  1121.          if (dstPacking->SwapBytes) {
  1122.             _mesa_swap2( (GLushort *) dst, n );
  1123.          }
  1124.       }
  1125.       break;
  1126.    default:
  1127.       _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
  1128.    }
  1129.  
  1130.    free(depthCopy);
  1131. }
  1132.  
  1133.  
  1134.  
  1135. /**
  1136.  * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
  1137.  */
  1138. void
  1139. _mesa_pack_depth_stencil_span(struct gl_context *ctx,GLuint n,
  1140.                               GLenum dstType, GLuint *dest,
  1141.                               const GLfloat *depthVals,
  1142.                               const GLubyte *stencilVals,
  1143.                               const struct gl_pixelstore_attrib *dstPacking)
  1144. {
  1145.    GLfloat *depthCopy = malloc(n * sizeof(GLfloat));
  1146.    GLubyte *stencilCopy = malloc(n * sizeof(GLubyte));
  1147.    GLuint i;
  1148.  
  1149.    if (!depthCopy || !stencilCopy) {
  1150.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel packing");
  1151.       free(depthCopy);
  1152.       free(stencilCopy);
  1153.       return;
  1154.    }
  1155.  
  1156.    if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
  1157.       memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
  1158.       _mesa_scale_and_bias_depth(ctx, n, depthCopy);
  1159.       depthVals = depthCopy;
  1160.    }
  1161.  
  1162.    if (ctx->Pixel.IndexShift ||
  1163.        ctx->Pixel.IndexOffset ||
  1164.        ctx->Pixel.MapStencilFlag) {
  1165.       memcpy(stencilCopy, stencilVals, n * sizeof(GLubyte));
  1166.       _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
  1167.       stencilVals = stencilCopy;
  1168.    }
  1169.  
  1170.    switch (dstType) {
  1171.    case GL_UNSIGNED_INT_24_8:
  1172.       for (i = 0; i < n; i++) {
  1173.          GLuint z = (GLuint) (depthVals[i] * 0xffffff);
  1174.          dest[i] = (z << 8) | (stencilVals[i] & 0xff);
  1175.       }
  1176.       break;
  1177.    case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
  1178.       for (i = 0; i < n; i++) {
  1179.          ((GLfloat*)dest)[i*2] = depthVals[i];
  1180.          dest[i*2+1] = stencilVals[i] & 0xff;
  1181.       }
  1182.       break;
  1183.    }
  1184.  
  1185.    if (dstPacking->SwapBytes) {
  1186.       _mesa_swap4(dest, n);
  1187.    }
  1188.  
  1189.    free(depthCopy);
  1190.    free(stencilCopy);
  1191. }
  1192.  
  1193.  
  1194.  
  1195. /**
  1196.  * Unpack image data.  Apply byte swapping, byte flipping (bitmap).
  1197.  * Return all image data in a contiguous block.  This is used when we
  1198.  * compile glDrawPixels, glTexImage, etc into a display list.  We
  1199.  * need a copy of the data in a standard format.
  1200.  */
  1201. void *
  1202. _mesa_unpack_image( GLuint dimensions,
  1203.                     GLsizei width, GLsizei height, GLsizei depth,
  1204.                     GLenum format, GLenum type, const GLvoid *pixels,
  1205.                     const struct gl_pixelstore_attrib *unpack )
  1206. {
  1207.    GLint bytesPerRow, compsPerRow;
  1208.    GLboolean flipBytes, swap2, swap4;
  1209.  
  1210.    if (!pixels)
  1211.       return NULL;  /* not necessarily an error */
  1212.  
  1213.    if (width <= 0 || height <= 0 || depth <= 0)
  1214.       return NULL;  /* generate error later */
  1215.  
  1216.    if (type == GL_BITMAP) {
  1217.       bytesPerRow = (width + 7) >> 3;
  1218.       flipBytes = unpack->LsbFirst;
  1219.       swap2 = swap4 = GL_FALSE;
  1220.       compsPerRow = 0;
  1221.    }
  1222.    else {
  1223.       const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
  1224.       GLint components = _mesa_components_in_format(format);
  1225.       GLint bytesPerComp;
  1226.  
  1227.       if (_mesa_type_is_packed(type))
  1228.           components = 1;
  1229.  
  1230.       if (bytesPerPixel <= 0 || components <= 0)
  1231.          return NULL;   /* bad format or type.  generate error later */
  1232.       bytesPerRow = bytesPerPixel * width;
  1233.       bytesPerComp = bytesPerPixel / components;
  1234.       flipBytes = GL_FALSE;
  1235.       swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
  1236.       swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
  1237.       compsPerRow = components * width;
  1238.       assert(compsPerRow >= width);
  1239.    }
  1240.  
  1241.    {
  1242.       GLubyte *destBuffer
  1243.          = malloc(bytesPerRow * height * depth);
  1244.       GLubyte *dst;
  1245.       GLint img, row;
  1246.       if (!destBuffer)
  1247.          return NULL;   /* generate GL_OUT_OF_MEMORY later */
  1248.  
  1249.       dst = destBuffer;
  1250.       for (img = 0; img < depth; img++) {
  1251.          for (row = 0; row < height; row++) {
  1252.             const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
  1253.                                width, height, format, type, img, row, 0);
  1254.  
  1255.             if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
  1256.                GLint i;
  1257.                flipBytes = GL_FALSE;
  1258.                if (unpack->LsbFirst) {
  1259.                   GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
  1260.                   GLubyte dstMask = 128;
  1261.                   const GLubyte *s = src;
  1262.                   GLubyte *d = dst;
  1263.                   *d = 0;
  1264.                   for (i = 0; i < width; i++) {
  1265.                      if (*s & srcMask) {
  1266.                         *d |= dstMask;
  1267.                      }      
  1268.                      if (srcMask == 128) {
  1269.                         srcMask = 1;
  1270.                         s++;
  1271.                      }
  1272.                      else {
  1273.                         srcMask = srcMask << 1;
  1274.                      }
  1275.                      if (dstMask == 1) {
  1276.                         dstMask = 128;
  1277.                         d++;
  1278.                         *d = 0;
  1279.                      }
  1280.                      else {
  1281.                         dstMask = dstMask >> 1;
  1282.                      }
  1283.                   }
  1284.                }
  1285.                else {
  1286.                   GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
  1287.                   GLubyte dstMask = 128;
  1288.                   const GLubyte *s = src;
  1289.                   GLubyte *d = dst;
  1290.                   *d = 0;
  1291.                   for (i = 0; i < width; i++) {
  1292.                      if (*s & srcMask) {
  1293.                         *d |= dstMask;
  1294.                      }
  1295.                      if (srcMask == 1) {
  1296.                         srcMask = 128;
  1297.                         s++;
  1298.                      }
  1299.                      else {
  1300.                         srcMask = srcMask >> 1;
  1301.                      }
  1302.                      if (dstMask == 1) {
  1303.                         dstMask = 128;
  1304.                         d++;
  1305.                         *d = 0;
  1306.                      }
  1307.                      else {
  1308.                         dstMask = dstMask >> 1;
  1309.                      }      
  1310.                   }
  1311.                }
  1312.             }
  1313.             else {
  1314.                memcpy(dst, src, bytesPerRow);
  1315.             }
  1316.  
  1317.             /* byte flipping/swapping */
  1318.             if (flipBytes) {
  1319.                flip_bytes((GLubyte *) dst, bytesPerRow);
  1320.             }
  1321.             else if (swap2) {
  1322.                _mesa_swap2((GLushort*) dst, compsPerRow);
  1323.             }
  1324.             else if (swap4) {
  1325.                _mesa_swap4((GLuint*) dst, compsPerRow);
  1326.             }
  1327.             dst += bytesPerRow;
  1328.          }
  1329.       }
  1330.       return destBuffer;
  1331.    }
  1332. }
  1333.  
  1334. void
  1335. _mesa_pack_luminance_from_rgba_float(GLuint n, GLfloat rgba[][4],
  1336.                                      GLvoid *dstAddr, GLenum dst_format,
  1337.                                      GLbitfield transferOps)
  1338. {
  1339.    int i;
  1340.    GLfloat *dst = (GLfloat *) dstAddr;
  1341.  
  1342.    switch (dst_format) {
  1343.    case GL_LUMINANCE:
  1344.       if (transferOps & IMAGE_CLAMP_BIT) {
  1345.          for (i = 0; i < n; i++) {
  1346.             GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
  1347.             dst[i] = CLAMP(sum, 0.0F, 1.0F);
  1348.          }
  1349.       } else {
  1350.          for (i = 0; i < n; i++) {
  1351.             dst[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
  1352.          }
  1353.       }
  1354.       return;
  1355.    case GL_LUMINANCE_ALPHA:
  1356.       if (transferOps & IMAGE_CLAMP_BIT) {
  1357.          for (i = 0; i < n; i++) {
  1358.             GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
  1359.             dst[2*i] = CLAMP(sum, 0.0F, 1.0F);
  1360.             dst[2*i+1] = rgba[i][ACOMP];
  1361.          }
  1362.       } else {
  1363.          for (i = 0; i < n; i++) {
  1364.             dst[2*i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
  1365.             dst[2*i+1] = rgba[i][ACOMP];
  1366.          }
  1367.       }
  1368.       return;
  1369.    default:
  1370.       assert(!"Unsupported format");
  1371.    }
  1372. }
  1373.  
  1374. static int32_t
  1375. clamp_sint64_to_sint32(int64_t src)
  1376. {
  1377.    return CLAMP(src, INT32_MIN, INT32_MAX);
  1378. }
  1379.  
  1380. static int32_t
  1381. clamp_sint64_to_uint32(int64_t src)
  1382. {
  1383.    return CLAMP(src, 0, UINT32_MAX);
  1384. }
  1385.  
  1386. static int32_t
  1387. clamp_uint64_to_uint32(uint64_t src)
  1388. {
  1389.    return MIN2(src, UINT32_MAX);
  1390. }
  1391.  
  1392. static int32_t
  1393. clamp_uint64_to_sint32(uint64_t src)
  1394. {
  1395.    return MIN2(src, INT32_MAX);
  1396. }
  1397.  
  1398. static int32_t
  1399. convert_integer_luminance64(int64_t src64, int bits,
  1400.                             bool dst_is_signed, bool src_is_signed)
  1401. {
  1402.    int32_t src32;
  1403.  
  1404.    /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
  1405.     * any signed<->unsigned conversion too.
  1406.     */
  1407.    if (src_is_signed && dst_is_signed)
  1408.       src32 = clamp_sint64_to_sint32(src64);
  1409.    else if (src_is_signed && !dst_is_signed)
  1410.       src32 = clamp_sint64_to_uint32(src64);
  1411.    else if (!src_is_signed && dst_is_signed)
  1412.       src32 = clamp_uint64_to_sint32(src64);
  1413.    else
  1414.       src32 = clamp_uint64_to_uint32(src64);
  1415.  
  1416.    /* If the dst type is < 32-bit, we need an extra clamp */
  1417.    if (bits == 32) {
  1418.       return src32;
  1419.    } else {
  1420.       if (dst_is_signed)
  1421.          return _mesa_signed_to_signed(src32, bits);
  1422.       else
  1423.          return _mesa_unsigned_to_unsigned(src32, bits);
  1424.    }
  1425. }
  1426.  
  1427. static int32_t
  1428. convert_integer(int32_t src, int bits, bool dst_is_signed, bool src_is_signed)
  1429. {
  1430.    if (src_is_signed && dst_is_signed)
  1431.       return _mesa_signed_to_signed(src, bits);
  1432.    else if (src_is_signed && !dst_is_signed)
  1433.       return _mesa_signed_to_unsigned(src, bits);
  1434.    else if (!src_is_signed && dst_is_signed)
  1435.       return _mesa_unsigned_to_signed(src, bits);
  1436.    else
  1437.       return _mesa_unsigned_to_unsigned(src, bits);
  1438. }
  1439.  
  1440. void
  1441. _mesa_pack_luminance_from_rgba_integer(GLuint n,
  1442.                                        GLuint rgba[][4], bool rgba_is_signed,
  1443.                                        GLvoid *dstAddr,
  1444.                                        GLenum dst_format,
  1445.                                        GLenum dst_type)
  1446. {
  1447.    int i;
  1448.    int64_t lum64;
  1449.    int32_t lum32, alpha;
  1450.    bool dst_is_signed;
  1451.    int dst_bits;
  1452.  
  1453.    assert(dst_format == GL_LUMINANCE_INTEGER_EXT ||
  1454.           dst_format == GL_LUMINANCE_ALPHA_INTEGER_EXT);
  1455.  
  1456.    /* We first compute luminance values as a 64-bit addition of the
  1457.     * 32-bit R,G,B components, then we clamp the result to the dst type size.
  1458.     *
  1459.     * Notice that this operation involves casting the 32-bit R,G,B components
  1460.     * to 64-bit before the addition. Since rgba is defined as a GLuint array
  1461.     * we need to be careful when rgba packs signed data and make sure
  1462.     * that we cast to a 32-bit signed integer values before casting them to
  1463.     * 64-bit signed integers.
  1464.     */
  1465.    dst_is_signed = (dst_type == GL_BYTE || dst_type == GL_SHORT ||
  1466.                     dst_type == GL_INT);
  1467.  
  1468.    dst_bits = _mesa_sizeof_type(dst_type) * 8;
  1469.    assert(dst_bits > 0);
  1470.  
  1471.    switch (dst_format) {
  1472.    case GL_LUMINANCE_INTEGER_EXT:
  1473.       for (i = 0; i < n; i++) {
  1474.          if (!rgba_is_signed) {
  1475.             lum64 = (uint64_t) rgba[i][RCOMP] +
  1476.                     (uint64_t) rgba[i][GCOMP] +
  1477.                     (uint64_t) rgba[i][BCOMP];
  1478.          } else {
  1479.             lum64 = (int64_t) ((int32_t) rgba[i][RCOMP]) +
  1480.                     (int64_t) ((int32_t) rgba[i][GCOMP]) +
  1481.                     (int64_t) ((int32_t) rgba[i][BCOMP]);
  1482.          }
  1483.          lum32 = convert_integer_luminance64(lum64, dst_bits,
  1484.                                              dst_is_signed, rgba_is_signed);
  1485.          switch (dst_type) {
  1486.          case GL_BYTE:
  1487.          case GL_UNSIGNED_BYTE: {
  1488.             GLbyte *dst = (GLbyte *) dstAddr;
  1489.             dst[i] = lum32;
  1490.             break;
  1491.          }
  1492.          case GL_SHORT:
  1493.          case GL_UNSIGNED_SHORT: {
  1494.             GLshort *dst = (GLshort *) dstAddr;
  1495.             dst[i] = lum32;
  1496.             break;
  1497.          }
  1498.          case GL_INT:
  1499.          case GL_UNSIGNED_INT: {
  1500.             GLint *dst = (GLint *) dstAddr;
  1501.             dst[i] = lum32;
  1502.             break;
  1503.          }
  1504.          }
  1505.       }
  1506.       return;
  1507.    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
  1508.       for (i = 0; i < n; i++) {
  1509.          if (!rgba_is_signed) {
  1510.             lum64 = (uint64_t) rgba[i][RCOMP] +
  1511.                     (uint64_t) rgba[i][GCOMP] +
  1512.                     (uint64_t) rgba[i][BCOMP];
  1513.          } else {
  1514.             lum64 = (int64_t) ((int32_t) rgba[i][RCOMP]) +
  1515.                     (int64_t) ((int32_t) rgba[i][GCOMP]) +
  1516.                     (int64_t) ((int32_t) rgba[i][BCOMP]);
  1517.          }
  1518.          lum32 = convert_integer_luminance64(lum64, dst_bits,
  1519.                                              dst_is_signed, rgba_is_signed);
  1520.          alpha = convert_integer(rgba[i][ACOMP], dst_bits,
  1521.                                  dst_is_signed, rgba_is_signed);
  1522.          switch (dst_type) {
  1523.          case GL_BYTE:
  1524.          case GL_UNSIGNED_BYTE: {
  1525.             GLbyte *dst = (GLbyte *) dstAddr;
  1526.             dst[2*i] = lum32;
  1527.             dst[2*i+1] = alpha;
  1528.             break;
  1529.          }
  1530.          case GL_SHORT:
  1531.          case GL_UNSIGNED_SHORT: {
  1532.             GLshort *dst = (GLshort *) dstAddr;
  1533.             dst[i] = lum32;
  1534.             dst[2*i+1] = alpha;
  1535.             break;
  1536.          }
  1537.          case GL_INT:
  1538.          case GL_UNSIGNED_INT: {
  1539.             GLint *dst = (GLint *) dstAddr;
  1540.             dst[i] = lum32;
  1541.             dst[2*i+1] = alpha;
  1542.             break;
  1543.          }
  1544.          }
  1545.       }
  1546.       return;
  1547.    }
  1548. }
  1549.  
  1550. GLfloat *
  1551. _mesa_unpack_color_index_to_rgba_float(struct gl_context *ctx, GLuint dims,
  1552.                                        const void *src, GLenum srcFormat, GLenum srcType,
  1553.                                        int srcWidth, int srcHeight, int srcDepth,
  1554.                                        const struct gl_pixelstore_attrib *srcPacking,
  1555.                                        GLbitfield transferOps)
  1556. {
  1557.    int count, img;
  1558.    GLuint *indexes;
  1559.    GLfloat *rgba, *dstPtr;
  1560.  
  1561.    count = srcWidth * srcHeight;
  1562.    indexes = malloc(count * sizeof(GLuint));
  1563.    if (!indexes) {
  1564.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
  1565.       return NULL;
  1566.    }
  1567.  
  1568.    rgba = malloc(4 * count * srcDepth * sizeof(GLfloat));
  1569.    if (!rgba) {
  1570.       free(indexes);
  1571.       _mesa_error(ctx, GL_OUT_OF_MEMORY, "pixel unpacking");
  1572.       return NULL;
  1573.    }
  1574.  
  1575.    /* Convert indexes to RGBA float */
  1576.    dstPtr = rgba;
  1577.    for (img = 0; img < srcDepth; img++) {
  1578.       const GLubyte *srcPtr =
  1579.          (const GLubyte *) _mesa_image_address(dims, srcPacking, src,
  1580.                                                srcWidth, srcHeight,
  1581.                                                srcFormat, srcType,
  1582.                                                img, 0, 0);
  1583.  
  1584.       extract_uint_indexes(count, indexes, srcFormat, srcType, srcPtr, srcPacking);
  1585.  
  1586.       if (transferOps & IMAGE_SHIFT_OFFSET_BIT)
  1587.          _mesa_shift_and_offset_ci(ctx, count, indexes);
  1588.  
  1589.       _mesa_map_ci_to_rgba(ctx, count, indexes, (float (*)[4])dstPtr);
  1590.  
  1591.       /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
  1592.        * with color indexes.
  1593.        */
  1594.       transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
  1595.       _mesa_apply_rgba_transfer_ops(ctx, transferOps, count, (float (*)[4])dstPtr);
  1596.  
  1597.       dstPtr += srcHeight * srcWidth * 4;
  1598.    }
  1599.  
  1600.    free(indexes);
  1601.  
  1602.    return rgba;
  1603. }
  1604.  
  1605. GLubyte *
  1606. _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context *ctx, GLuint dims,
  1607.                                        const void *src, GLenum srcFormat, GLenum srcType,
  1608.                                        int srcWidth, int srcHeight, int srcDepth,
  1609.                                        const struct gl_pixelstore_attrib *srcPacking,
  1610.                                        GLbitfield transferOps)
  1611. {
  1612.    GLfloat *rgba;
  1613.    GLubyte *dst;
  1614.    int count, i;
  1615.  
  1616.    transferOps |= IMAGE_CLAMP_BIT;
  1617.    rgba = _mesa_unpack_color_index_to_rgba_float(ctx, dims,
  1618.                                                  src, srcFormat, srcType,
  1619.                                                  srcWidth, srcHeight, srcDepth,
  1620.                                                  srcPacking, transferOps);
  1621.  
  1622.    count = srcWidth * srcHeight * srcDepth;
  1623.    dst = malloc(count * 4 * sizeof(GLubyte));
  1624.    for (i = 0; i < count; i++) {
  1625.       CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 0], rgba[i * 4 + 0]);
  1626.       CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 1], rgba[i * 4 + 1]);
  1627.       CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 2], rgba[i * 4 + 2]);
  1628.       CLAMPED_FLOAT_TO_UBYTE(dst[i * 4 + 3], rgba[i * 4 + 3]);
  1629.    }
  1630.  
  1631.    free(rgba);
  1632.  
  1633.    return dst;
  1634. }
  1635.