Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Mesa 3-D graphics library
  3.  * Version:  7.5
  4.  *
  5.  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  6.  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  7.  *
  8.  * Permission is hereby granted, free of charge, to any person obtaining a
  9.  * copy of this software and associated documentation files (the "Software"),
  10.  * to deal in the Software without restriction, including without limitation
  11.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12.  * and/or sell copies of the Software, and to permit persons to whom the
  13.  * Software is furnished to do so, subject to the following conditions:
  14.  *
  15.  * The above copyright notice and this permission notice shall be included
  16.  * in all copies or substantial portions of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  21.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  22.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  */
  25.  
  26.  
  27. /**
  28.  * \file image.c
  29.  * Image handling.
  30.  */
  31.  
  32.  
  33. #include "glheader.h"
  34. #include "colormac.h"
  35. #include "image.h"
  36. #include "imports.h"
  37. #include "macros.h"
  38.  
  39.  
  40. /**
  41.  * NOTE:
  42.  * Normally, BYTE_TO_FLOAT(0) returns 0.00392  That causes problems when
  43.  * we later convert the float to a packed integer value (such as for
  44.  * GL_RGB5_A1) because we'll wind up with a non-zero value.
  45.  *
  46.  * We redefine the macros here so zero is handled correctly.
  47.  */
  48. #undef BYTE_TO_FLOAT
  49. #define BYTE_TO_FLOAT(B)    ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
  50.  
  51. #undef SHORT_TO_FLOAT
  52. #define SHORT_TO_FLOAT(S)   ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
  53.  
  54.  
  55.  
  56. /** Compute ceiling of integer quotient of A divided by B. */
  57. #define CEILING( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
  58.  
  59.  
  60. /**
  61.  * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
  62.  */
  63. GLboolean
  64. _mesa_type_is_packed(GLenum type)
  65. {
  66.    switch (type) {
  67.    case GL_UNSIGNED_BYTE_3_3_2:
  68.    case GL_UNSIGNED_BYTE_2_3_3_REV:
  69.    case GL_UNSIGNED_SHORT_5_6_5:
  70.    case GL_UNSIGNED_SHORT_5_6_5_REV:
  71.    case GL_UNSIGNED_SHORT_4_4_4_4:
  72.    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  73.    case GL_UNSIGNED_SHORT_5_5_5_1:
  74.    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  75.    case GL_UNSIGNED_INT_8_8_8_8:
  76.    case GL_UNSIGNED_INT_8_8_8_8_REV:
  77.    case GL_UNSIGNED_INT_10_10_10_2:
  78.    case GL_UNSIGNED_INT_2_10_10_10_REV:
  79.    case GL_UNSIGNED_SHORT_8_8_MESA:
  80.    case GL_UNSIGNED_SHORT_8_8_REV_MESA:
  81.    case GL_UNSIGNED_INT_24_8_EXT:
  82.       return GL_TRUE;
  83.    }
  84.  
  85.    return GL_FALSE;
  86. }
  87.  
  88.  
  89.  
  90. /**
  91.  * Flip the order of the 2 bytes in each word in the given array.
  92.  *
  93.  * \param p array.
  94.  * \param n number of words.
  95.  */
  96. void
  97. _mesa_swap2( GLushort *p, GLuint n )
  98. {
  99.    GLuint i;
  100.    for (i = 0; i < n; i++) {
  101.       p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
  102.    }
  103. }
  104.  
  105.  
  106.  
  107. /*
  108.  * Flip the order of the 4 bytes in each word in the given array.
  109.  */
  110. void
  111. _mesa_swap4( GLuint *p, GLuint n )
  112. {
  113.    GLuint i, a, b;
  114.    for (i = 0; i < n; i++) {
  115.       b = p[i];
  116.       a =  (b >> 24)
  117.         | ((b >> 8) & 0xff00)
  118.         | ((b << 8) & 0xff0000)
  119.         | ((b << 24) & 0xff000000);
  120.       p[i] = a;
  121.    }
  122. }
  123.  
  124.  
  125. /**
  126.  * Get the size of a GL data type.
  127.  *
  128.  * \param type GL data type.
  129.  *
  130.  * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
  131.  * if an invalid type enum.
  132.  */
  133. GLint
  134. _mesa_sizeof_type( GLenum type )
  135. {
  136.    switch (type) {
  137.       case GL_BITMAP:
  138.          return 0;
  139.       case GL_UNSIGNED_BYTE:
  140.          return sizeof(GLubyte);
  141.       case GL_BYTE:
  142.          return sizeof(GLbyte);
  143.       case GL_UNSIGNED_SHORT:
  144.          return sizeof(GLushort);
  145.       case GL_SHORT:
  146.          return sizeof(GLshort);
  147.       case GL_UNSIGNED_INT:
  148.          return sizeof(GLuint);
  149.       case GL_INT:
  150.          return sizeof(GLint);
  151.       case GL_FLOAT:
  152.          return sizeof(GLfloat);
  153.       case GL_DOUBLE:
  154.          return sizeof(GLdouble);
  155.       case GL_HALF_FLOAT_ARB:
  156.          return sizeof(GLhalfARB);
  157.       case GL_FIXED:
  158.          return sizeof(GLfixed);
  159.       default:
  160.          return -1;
  161.    }
  162. }
  163.  
  164.  
  165. /**
  166.  * Same as _mesa_sizeof_type() but also accepting the packed pixel
  167.  * format data types.
  168.  */
  169. GLint
  170. _mesa_sizeof_packed_type( GLenum type )
  171. {
  172.    switch (type) {
  173.       case GL_BITMAP:
  174.          return 0;
  175.       case GL_UNSIGNED_BYTE:
  176.          return sizeof(GLubyte);
  177.       case GL_BYTE:
  178.          return sizeof(GLbyte);
  179.       case GL_UNSIGNED_SHORT:
  180.          return sizeof(GLushort);
  181.       case GL_SHORT:
  182.          return sizeof(GLshort);
  183.       case GL_UNSIGNED_INT:
  184.          return sizeof(GLuint);
  185.       case GL_INT:
  186.          return sizeof(GLint);
  187.       case GL_HALF_FLOAT_ARB:
  188.          return sizeof(GLhalfARB);
  189.       case GL_FLOAT:
  190.          return sizeof(GLfloat);
  191.       case GL_UNSIGNED_BYTE_3_3_2:
  192.          return sizeof(GLubyte);
  193.       case GL_UNSIGNED_BYTE_2_3_3_REV:
  194.          return sizeof(GLubyte);
  195.       case GL_UNSIGNED_SHORT_5_6_5:
  196.          return sizeof(GLushort);
  197.       case GL_UNSIGNED_SHORT_5_6_5_REV:
  198.          return sizeof(GLushort);
  199.       case GL_UNSIGNED_SHORT_4_4_4_4:
  200.          return sizeof(GLushort);
  201.       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  202.          return sizeof(GLushort);
  203.       case GL_UNSIGNED_SHORT_5_5_5_1:
  204.          return sizeof(GLushort);
  205.       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  206.          return sizeof(GLushort);
  207.       case GL_UNSIGNED_INT_8_8_8_8:
  208.          return sizeof(GLuint);
  209.       case GL_UNSIGNED_INT_8_8_8_8_REV:
  210.          return sizeof(GLuint);
  211.       case GL_UNSIGNED_INT_10_10_10_2:
  212.          return sizeof(GLuint);
  213.       case GL_UNSIGNED_INT_2_10_10_10_REV:
  214.          return sizeof(GLuint);
  215.       case GL_UNSIGNED_SHORT_8_8_MESA:
  216.       case GL_UNSIGNED_SHORT_8_8_REV_MESA:
  217.          return sizeof(GLushort);      
  218.       case GL_UNSIGNED_INT_24_8_EXT:
  219.          return sizeof(GLuint);
  220.       default:
  221.          return -1;
  222.    }
  223. }
  224.  
  225.  
  226. /**
  227.  * Get the number of components in a pixel format.
  228.  *
  229.  * \param format pixel format.
  230.  *
  231.  * \return the number of components in the given format, or -1 if a bad format.
  232.  */
  233. GLint
  234. _mesa_components_in_format( GLenum format )
  235. {
  236.    switch (format) {
  237.       case GL_COLOR_INDEX:
  238.       case GL_COLOR_INDEX1_EXT:
  239.       case GL_COLOR_INDEX2_EXT:
  240.       case GL_COLOR_INDEX4_EXT:
  241.       case GL_COLOR_INDEX8_EXT:
  242.       case GL_COLOR_INDEX12_EXT:
  243.       case GL_COLOR_INDEX16_EXT:
  244.       case GL_STENCIL_INDEX:
  245.       case GL_DEPTH_COMPONENT:
  246.       case GL_RED:
  247.       case GL_RED_INTEGER_EXT:
  248.       case GL_GREEN:
  249.       case GL_GREEN_INTEGER_EXT:
  250.       case GL_BLUE:
  251.       case GL_BLUE_INTEGER_EXT:
  252.       case GL_ALPHA:
  253.       case GL_ALPHA_INTEGER_EXT:
  254.       case GL_LUMINANCE:
  255.       case GL_LUMINANCE_INTEGER_EXT:
  256.       case GL_INTENSITY:
  257.          return 1;
  258.       case GL_LUMINANCE_ALPHA:
  259.       case GL_LUMINANCE_ALPHA_INTEGER_EXT:
  260.       case GL_RG:
  261.          return 2;
  262.       case GL_RGB:
  263.       case GL_RGB_INTEGER_EXT:
  264.          return 3;
  265.       case GL_RGBA:
  266.       case GL_RGBA_INTEGER_EXT:
  267.          return 4;
  268.       case GL_BGR:
  269.          return 3;
  270.       case GL_BGRA:
  271.          return 4;
  272.       case GL_ABGR_EXT:
  273.          return 4;
  274.       case GL_YCBCR_MESA:
  275.          return 2;
  276.       case GL_DEPTH_STENCIL_EXT:
  277.          return 2;
  278.       case GL_DUDV_ATI:
  279.       case GL_DU8DV8_ATI:
  280.          return 2;
  281.       default:
  282.          return -1;
  283.    }
  284. }
  285.  
  286.  
  287. /**
  288.  * Get the bytes per pixel of pixel format type pair.
  289.  *
  290.  * \param format pixel format.
  291.  * \param type pixel type.
  292.  *
  293.  * \return bytes per pixel, or -1 if a bad format or type was given.
  294.  */
  295. GLint
  296. _mesa_bytes_per_pixel( GLenum format, GLenum type )
  297. {
  298.    GLint comps = _mesa_components_in_format( format );
  299.    if (comps < 0)
  300.       return -1;
  301.  
  302.    switch (type) {
  303.       case GL_BITMAP:
  304.          return 0;  /* special case */
  305.       case GL_BYTE:
  306.       case GL_UNSIGNED_BYTE:
  307.          return comps * sizeof(GLubyte);
  308.       case GL_SHORT:
  309.       case GL_UNSIGNED_SHORT:
  310.          return comps * sizeof(GLshort);
  311.       case GL_INT:
  312.       case GL_UNSIGNED_INT:
  313.          return comps * sizeof(GLint);
  314.       case GL_FLOAT:
  315.          return comps * sizeof(GLfloat);
  316.       case GL_HALF_FLOAT_ARB:
  317.          return comps * sizeof(GLhalfARB);
  318.       case GL_UNSIGNED_BYTE_3_3_2:
  319.       case GL_UNSIGNED_BYTE_2_3_3_REV:
  320.          if (format == GL_RGB || format == GL_BGR ||
  321.              format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
  322.             return sizeof(GLubyte);
  323.          else
  324.             return -1;  /* error */
  325.       case GL_UNSIGNED_SHORT_5_6_5:
  326.       case GL_UNSIGNED_SHORT_5_6_5_REV:
  327.          if (format == GL_RGB || format == GL_BGR ||
  328.              format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT)
  329.             return sizeof(GLushort);
  330.          else
  331.             return -1;  /* error */
  332.       case GL_UNSIGNED_SHORT_4_4_4_4:
  333.       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  334.       case GL_UNSIGNED_SHORT_5_5_5_1:
  335.       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  336.          if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
  337.              format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
  338.             return sizeof(GLushort);
  339.          else
  340.             return -1;
  341.       case GL_UNSIGNED_INT_8_8_8_8:
  342.       case GL_UNSIGNED_INT_8_8_8_8_REV:
  343.       case GL_UNSIGNED_INT_10_10_10_2:
  344.       case GL_UNSIGNED_INT_2_10_10_10_REV:
  345.          if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT ||
  346.              format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT)
  347.             return sizeof(GLuint);
  348.          else
  349.             return -1;
  350.       case GL_UNSIGNED_SHORT_8_8_MESA:
  351.       case GL_UNSIGNED_SHORT_8_8_REV_MESA:
  352.          if (format == GL_YCBCR_MESA)
  353.             return sizeof(GLushort);
  354.          else
  355.             return -1;
  356.       case GL_UNSIGNED_INT_24_8_EXT:
  357.          if (format == GL_DEPTH_STENCIL_EXT)
  358.             return sizeof(GLuint);
  359.          else
  360.             return -1;
  361.       default:
  362.          return -1;
  363.    }
  364. }
  365.  
  366.  
  367. /**
  368.  * Test for a legal pixel format and type.
  369.  *
  370.  * \param format pixel format.
  371.  * \param type pixel type.
  372.  *
  373.  * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
  374.  * otherwise.
  375.  */
  376. GLboolean
  377. _mesa_is_legal_format_and_type(const struct gl_context *ctx,
  378.                                GLenum format, GLenum type)
  379. {
  380.    switch (format) {
  381.       case GL_COLOR_INDEX:
  382.       case GL_STENCIL_INDEX:
  383.          switch (type) {
  384.             case GL_BITMAP:
  385.             case GL_BYTE:
  386.             case GL_UNSIGNED_BYTE:
  387.             case GL_SHORT:
  388.             case GL_UNSIGNED_SHORT:
  389.             case GL_INT:
  390.             case GL_UNSIGNED_INT:
  391.             case GL_FLOAT:
  392.                return GL_TRUE;
  393.             case GL_HALF_FLOAT_ARB:
  394.                return ctx->Extensions.ARB_half_float_pixel;
  395.             default:
  396.                return GL_FALSE;
  397.          }
  398.       case GL_RED:
  399.       case GL_GREEN:
  400.       case GL_BLUE:
  401.       case GL_ALPHA:
  402. #if 0 /* not legal!  see table 3.6 of the 1.5 spec */
  403.       case GL_INTENSITY:
  404. #endif
  405.       case GL_LUMINANCE:
  406.       case GL_LUMINANCE_ALPHA:
  407.       case GL_DEPTH_COMPONENT:
  408.          switch (type) {
  409.             case GL_BYTE:
  410.             case GL_UNSIGNED_BYTE:
  411.             case GL_SHORT:
  412.             case GL_UNSIGNED_SHORT:
  413.             case GL_INT:
  414.             case GL_UNSIGNED_INT:
  415.             case GL_FLOAT:
  416.                return GL_TRUE;
  417.             case GL_HALF_FLOAT_ARB:
  418.                return ctx->Extensions.ARB_half_float_pixel;
  419.             default:
  420.                return GL_FALSE;
  421.          }
  422.       case GL_RG:
  423.          if (!ctx->Extensions.ARB_texture_rg)
  424.             return GL_FALSE;
  425.  
  426.          switch (type) {
  427.             case GL_BYTE:
  428.             case GL_UNSIGNED_BYTE:
  429.             case GL_SHORT:
  430.             case GL_UNSIGNED_SHORT:
  431.             case GL_INT:
  432.             case GL_UNSIGNED_INT:
  433.             case GL_FLOAT:
  434.                return GL_TRUE;
  435.             case GL_HALF_FLOAT_ARB:
  436.                return ctx->Extensions.ARB_half_float_pixel;
  437.             default:
  438.                return GL_FALSE;
  439.          }
  440.       case GL_RGB:
  441.          switch (type) {
  442.             case GL_BYTE:
  443.             case GL_UNSIGNED_BYTE:
  444.             case GL_SHORT:
  445.             case GL_UNSIGNED_SHORT:
  446.             case GL_INT:
  447.             case GL_UNSIGNED_INT:
  448.             case GL_FLOAT:
  449.             case GL_UNSIGNED_BYTE_3_3_2:
  450.             case GL_UNSIGNED_BYTE_2_3_3_REV:
  451.             case GL_UNSIGNED_SHORT_5_6_5:
  452.             case GL_UNSIGNED_SHORT_5_6_5_REV:
  453.                return GL_TRUE;
  454.             case GL_HALF_FLOAT_ARB:
  455.                return ctx->Extensions.ARB_half_float_pixel;
  456.             default:
  457.                return GL_FALSE;
  458.          }
  459.       case GL_BGR:
  460.          switch (type) {
  461.             /* NOTE: no packed types are supported with BGR.  That's
  462.              * intentional, according to the GL spec.
  463.              */
  464.             case GL_BYTE:
  465.             case GL_UNSIGNED_BYTE:
  466.             case GL_SHORT:
  467.             case GL_UNSIGNED_SHORT:
  468.             case GL_INT:
  469.             case GL_UNSIGNED_INT:
  470.             case GL_FLOAT:
  471.                return GL_TRUE;
  472.             case GL_HALF_FLOAT_ARB:
  473.                return ctx->Extensions.ARB_half_float_pixel;
  474.             default:
  475.                return GL_FALSE;
  476.          }
  477.       case GL_RGBA:
  478.       case GL_BGRA:
  479.       case GL_ABGR_EXT:
  480.          switch (type) {
  481.             case GL_BYTE:
  482.             case GL_UNSIGNED_BYTE:
  483.             case GL_SHORT:
  484.             case GL_UNSIGNED_SHORT:
  485.             case GL_INT:
  486.             case GL_UNSIGNED_INT:
  487.             case GL_FLOAT:
  488.             case GL_UNSIGNED_SHORT_4_4_4_4:
  489.             case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  490.             case GL_UNSIGNED_SHORT_5_5_5_1:
  491.             case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  492.             case GL_UNSIGNED_INT_8_8_8_8:
  493.             case GL_UNSIGNED_INT_8_8_8_8_REV:
  494.             case GL_UNSIGNED_INT_10_10_10_2:
  495.             case GL_UNSIGNED_INT_2_10_10_10_REV:
  496.                return GL_TRUE;
  497.             case GL_HALF_FLOAT_ARB:
  498.                return ctx->Extensions.ARB_half_float_pixel;
  499.             default:
  500.                return GL_FALSE;
  501.          }
  502.       case GL_YCBCR_MESA:
  503.          if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
  504.              type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
  505.             return GL_TRUE;
  506.          else
  507.             return GL_FALSE;
  508.       case GL_DEPTH_STENCIL_EXT:
  509.          if (ctx->Extensions.EXT_packed_depth_stencil
  510.              && type == GL_UNSIGNED_INT_24_8_EXT)
  511.             return GL_TRUE;
  512.          else
  513.             return GL_FALSE;
  514.       case GL_DUDV_ATI:
  515.       case GL_DU8DV8_ATI:
  516.          switch (type) {
  517.             case GL_BYTE:
  518.             case GL_UNSIGNED_BYTE:
  519.             case GL_SHORT:
  520.             case GL_UNSIGNED_SHORT:
  521.             case GL_INT:
  522.             case GL_UNSIGNED_INT:
  523.             case GL_FLOAT:
  524.                return GL_TRUE;
  525.             default:
  526.                return GL_FALSE;
  527.          }
  528.  
  529.       /* integer-valued formats */
  530.       case GL_RED_INTEGER_EXT:
  531.       case GL_GREEN_INTEGER_EXT:
  532.       case GL_BLUE_INTEGER_EXT:
  533.       case GL_ALPHA_INTEGER_EXT:
  534.          switch (type) {
  535.             case GL_BYTE:
  536.             case GL_UNSIGNED_BYTE:
  537.             case GL_SHORT:
  538.             case GL_UNSIGNED_SHORT:
  539.             case GL_INT:
  540.             case GL_UNSIGNED_INT:
  541.                return ctx->Extensions.EXT_texture_integer;
  542.             default:
  543.                return GL_FALSE;
  544.          }
  545.  
  546.       case GL_RGB_INTEGER_EXT:
  547.          switch (type) {
  548.             case GL_BYTE:
  549.             case GL_UNSIGNED_BYTE:
  550.             case GL_SHORT:
  551.             case GL_UNSIGNED_SHORT:
  552.             case GL_INT:
  553.             case GL_UNSIGNED_INT:
  554.             case GL_UNSIGNED_BYTE_3_3_2:
  555.             case GL_UNSIGNED_BYTE_2_3_3_REV:
  556.             case GL_UNSIGNED_SHORT_5_6_5:
  557.             case GL_UNSIGNED_SHORT_5_6_5_REV:
  558.                return ctx->Extensions.EXT_texture_integer;
  559.             default:
  560.                return GL_FALSE;
  561.          }
  562.  
  563.       case GL_BGR_INTEGER_EXT:
  564.          switch (type) {
  565.             case GL_BYTE:
  566.             case GL_UNSIGNED_BYTE:
  567.             case GL_SHORT:
  568.             case GL_UNSIGNED_SHORT:
  569.             case GL_INT:
  570.             case GL_UNSIGNED_INT:
  571.             /* NOTE: no packed formats w/ BGR format */
  572.                return ctx->Extensions.EXT_texture_integer;
  573.             default:
  574.                return GL_FALSE;
  575.          }
  576.  
  577.       case GL_RGBA_INTEGER_EXT:
  578.       case GL_BGRA_INTEGER_EXT:
  579.          switch (type) {
  580.             case GL_BYTE:
  581.             case GL_UNSIGNED_BYTE:
  582.             case GL_SHORT:
  583.             case GL_UNSIGNED_SHORT:
  584.             case GL_INT:
  585.             case GL_UNSIGNED_INT:
  586.             case GL_UNSIGNED_SHORT_4_4_4_4:
  587.             case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  588.             case GL_UNSIGNED_SHORT_5_5_5_1:
  589.             case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  590.             case GL_UNSIGNED_INT_8_8_8_8:
  591.             case GL_UNSIGNED_INT_8_8_8_8_REV:
  592.             case GL_UNSIGNED_INT_10_10_10_2:
  593.             case GL_UNSIGNED_INT_2_10_10_10_REV:
  594.                return ctx->Extensions.EXT_texture_integer;
  595.             default:
  596.                return GL_FALSE;
  597.          }
  598.  
  599.       case GL_LUMINANCE_INTEGER_EXT:
  600.       case GL_LUMINANCE_ALPHA_INTEGER_EXT:
  601.          switch (type) {
  602.             case GL_BYTE:
  603.             case GL_UNSIGNED_BYTE:
  604.             case GL_SHORT:
  605.             case GL_UNSIGNED_SHORT:
  606.             case GL_INT:
  607.             case GL_UNSIGNED_INT:
  608.                return ctx->Extensions.EXT_texture_integer;
  609.             default:
  610.                return GL_FALSE;
  611.          }
  612.  
  613.       default:
  614.          ; /* fall-through */
  615.    }
  616.    return GL_FALSE;
  617. }
  618.  
  619.  
  620. /**
  621.  * Test if the given image format is a color/RGBA format (i.e., not color
  622.  * index, depth, stencil, etc).
  623.  * \param format  the image format value (may by an internal texture format)
  624.  * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
  625.  */
  626. GLboolean
  627. _mesa_is_color_format(GLenum format)
  628. {
  629.    switch (format) {
  630.       case GL_RED:
  631.       case GL_GREEN:
  632.       case GL_BLUE:
  633.       case GL_ALPHA:
  634.       case GL_ALPHA4:
  635.       case GL_ALPHA8:
  636.       case GL_ALPHA12:
  637.       case GL_ALPHA16:
  638.       case 1:
  639.       case GL_LUMINANCE:
  640.       case GL_LUMINANCE4:
  641.       case GL_LUMINANCE8:
  642.       case GL_LUMINANCE12:
  643.       case GL_LUMINANCE16:
  644.       case 2:
  645.       case GL_LUMINANCE_ALPHA:
  646.       case GL_LUMINANCE4_ALPHA4:
  647.       case GL_LUMINANCE6_ALPHA2:
  648.       case GL_LUMINANCE8_ALPHA8:
  649.       case GL_LUMINANCE12_ALPHA4:
  650.       case GL_LUMINANCE12_ALPHA12:
  651.       case GL_LUMINANCE16_ALPHA16:
  652.       case GL_INTENSITY:
  653.       case GL_INTENSITY4:
  654.       case GL_INTENSITY8:
  655.       case GL_INTENSITY12:
  656.       case GL_INTENSITY16:
  657.       case GL_R8:
  658.       case GL_R16:
  659.       case GL_RG:
  660.       case GL_RG8:
  661.       case GL_RG16:
  662.       case 3:
  663.       case GL_RGB:
  664.       case GL_BGR:
  665.       case GL_R3_G3_B2:
  666.       case GL_RGB4:
  667.       case GL_RGB5:
  668.       case GL_RGB8:
  669.       case GL_RGB10:
  670.       case GL_RGB12:
  671.       case GL_RGB16:
  672.       case 4:
  673.       case GL_ABGR_EXT:
  674.       case GL_RGBA:
  675.       case GL_BGRA:
  676.       case GL_RGBA2:
  677.       case GL_RGBA4:
  678.       case GL_RGB5_A1:
  679.       case GL_RGBA8:
  680.       case GL_RGB10_A2:
  681.       case GL_RGBA12:
  682.       case GL_RGBA16:
  683.       /* float texture formats */
  684.       case GL_ALPHA16F_ARB:
  685.       case GL_ALPHA32F_ARB:
  686.       case GL_LUMINANCE16F_ARB:
  687.       case GL_LUMINANCE32F_ARB:
  688.       case GL_LUMINANCE_ALPHA16F_ARB:
  689.       case GL_LUMINANCE_ALPHA32F_ARB:
  690.       case GL_INTENSITY16F_ARB:
  691.       case GL_INTENSITY32F_ARB:
  692.       case GL_R16F:
  693.       case GL_R32F:
  694.       case GL_RG16F:
  695.       case GL_RG32F:
  696.       case GL_RGB16F_ARB:
  697.       case GL_RGB32F_ARB:
  698.       case GL_RGBA16F_ARB:
  699.       case GL_RGBA32F_ARB:
  700.       /* compressed formats */
  701.       case GL_COMPRESSED_ALPHA:
  702.       case GL_COMPRESSED_LUMINANCE:
  703.       case GL_COMPRESSED_LUMINANCE_ALPHA:
  704.       case GL_COMPRESSED_INTENSITY:
  705.       case GL_COMPRESSED_RED:
  706.       case GL_COMPRESSED_RG:
  707.       case GL_COMPRESSED_RGB:
  708.       case GL_COMPRESSED_RGBA:
  709.       case GL_RGB_S3TC:
  710.       case GL_RGB4_S3TC:
  711.       case GL_RGBA_S3TC:
  712.       case GL_RGBA4_S3TC:
  713.       case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  714.       case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  715.       case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  716.       case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  717.       case GL_COMPRESSED_RGB_FXT1_3DFX:
  718.       case GL_COMPRESSED_RGBA_FXT1_3DFX:
  719. #if FEATURE_EXT_texture_sRGB
  720.       case GL_SRGB_EXT:
  721.       case GL_SRGB8_EXT:
  722.       case GL_SRGB_ALPHA_EXT:
  723.       case GL_SRGB8_ALPHA8_EXT:
  724.       case GL_SLUMINANCE_ALPHA_EXT:
  725.       case GL_SLUMINANCE8_ALPHA8_EXT:
  726.       case GL_SLUMINANCE_EXT:
  727.       case GL_SLUMINANCE8_EXT:
  728.       case GL_COMPRESSED_SRGB_EXT:
  729.       case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
  730.       case GL_COMPRESSED_SRGB_ALPHA_EXT:
  731.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
  732.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
  733.       case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
  734.       case GL_COMPRESSED_SLUMINANCE_EXT:
  735.       case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
  736. #endif /* FEATURE_EXT_texture_sRGB */
  737.       case GL_COMPRESSED_RED_RGTC1:
  738.       case GL_COMPRESSED_SIGNED_RED_RGTC1:
  739.       case GL_COMPRESSED_RG_RGTC2:
  740.       case GL_COMPRESSED_SIGNED_RG_RGTC2:
  741.       /* signed, normalized texture formats */
  742.       case GL_RGBA_SNORM:
  743.       case GL_RGBA8_SNORM:
  744.       /* generic integer formats */
  745.       case GL_RED_INTEGER_EXT:
  746.       case GL_GREEN_INTEGER_EXT:
  747.       case GL_BLUE_INTEGER_EXT:
  748.       case GL_ALPHA_INTEGER_EXT:
  749.       case GL_RGB_INTEGER_EXT:
  750.       case GL_RGBA_INTEGER_EXT:
  751.       case GL_BGR_INTEGER_EXT:
  752.       case GL_BGRA_INTEGER_EXT:
  753.       case GL_LUMINANCE_INTEGER_EXT:
  754.       case GL_LUMINANCE_ALPHA_INTEGER_EXT:
  755.       /* sized integer formats */
  756.       case GL_RGBA32UI_EXT:
  757.       case GL_RGB32UI_EXT:
  758.       case GL_ALPHA32UI_EXT:
  759.       case GL_INTENSITY32UI_EXT:
  760.       case GL_LUMINANCE32UI_EXT:
  761.       case GL_LUMINANCE_ALPHA32UI_EXT:
  762.       case GL_RGBA16UI_EXT:
  763.       case GL_RGB16UI_EXT:
  764.       case GL_ALPHA16UI_EXT:
  765.       case GL_INTENSITY16UI_EXT:
  766.       case GL_LUMINANCE16UI_EXT:
  767.       case GL_LUMINANCE_ALPHA16UI_EXT:
  768.       case GL_RGBA8UI_EXT:
  769.       case GL_RGB8UI_EXT:
  770.       case GL_ALPHA8UI_EXT:
  771.       case GL_INTENSITY8UI_EXT:
  772.       case GL_LUMINANCE8UI_EXT:
  773.       case GL_LUMINANCE_ALPHA8UI_EXT:
  774.       case GL_RGBA32I_EXT:
  775.       case GL_RGB32I_EXT:
  776.       case GL_ALPHA32I_EXT:
  777.       case GL_INTENSITY32I_EXT:
  778.       case GL_LUMINANCE32I_EXT:
  779.       case GL_LUMINANCE_ALPHA32I_EXT:
  780.       case GL_RGBA16I_EXT:
  781.       case GL_RGB16I_EXT:
  782.       case GL_ALPHA16I_EXT:
  783.       case GL_INTENSITY16I_EXT:
  784.       case GL_LUMINANCE16I_EXT:
  785.       case GL_LUMINANCE_ALPHA16I_EXT:
  786.       case GL_RGBA8I_EXT:
  787.       case GL_RGB8I_EXT:
  788.       case GL_ALPHA8I_EXT:
  789.       case GL_INTENSITY8I_EXT:
  790.       case GL_LUMINANCE8I_EXT:
  791.       case GL_LUMINANCE_ALPHA8I_EXT:
  792.          return GL_TRUE;
  793.       case GL_YCBCR_MESA:  /* not considered to be RGB */
  794.          /* fall-through */
  795.       default:
  796.          return GL_FALSE;
  797.    }
  798. }
  799.  
  800.  
  801. /**
  802.  * Test if the given image format is a color index format.
  803.  */
  804. GLboolean
  805. _mesa_is_index_format(GLenum format)
  806. {
  807.    switch (format) {
  808.       case GL_COLOR_INDEX:
  809.       case GL_COLOR_INDEX1_EXT:
  810.       case GL_COLOR_INDEX2_EXT:
  811.       case GL_COLOR_INDEX4_EXT:
  812.       case GL_COLOR_INDEX8_EXT:
  813.       case GL_COLOR_INDEX12_EXT:
  814.       case GL_COLOR_INDEX16_EXT:
  815.          return GL_TRUE;
  816.       default:
  817.          return GL_FALSE;
  818.    }
  819. }
  820.  
  821.  
  822. /**
  823.  * Test if the given image format is a depth component format.
  824.  */
  825. GLboolean
  826. _mesa_is_depth_format(GLenum format)
  827. {
  828.    switch (format) {
  829.       case GL_DEPTH_COMPONENT:
  830.       case GL_DEPTH_COMPONENT16:
  831.       case GL_DEPTH_COMPONENT24:
  832.       case GL_DEPTH_COMPONENT32:
  833.          return GL_TRUE;
  834.       default:
  835.          return GL_FALSE;
  836.    }
  837. }
  838.  
  839.  
  840. /**
  841.  * Test if the given image format is a stencil format.
  842.  */
  843. GLboolean
  844. _mesa_is_stencil_format(GLenum format)
  845. {
  846.    switch (format) {
  847.       case GL_STENCIL_INDEX:
  848.       case GL_DEPTH_STENCIL:
  849.          return GL_TRUE;
  850.       default:
  851.          return GL_FALSE;
  852.    }
  853. }
  854.  
  855.  
  856. /**
  857.  * Test if the given image format is a YCbCr format.
  858.  */
  859. GLboolean
  860. _mesa_is_ycbcr_format(GLenum format)
  861. {
  862.    switch (format) {
  863.       case GL_YCBCR_MESA:
  864.          return GL_TRUE;
  865.       default:
  866.          return GL_FALSE;
  867.    }
  868. }
  869.  
  870.  
  871. /**
  872.  * Test if the given image format is a depth+stencil format.
  873.  */
  874. GLboolean
  875. _mesa_is_depthstencil_format(GLenum format)
  876. {
  877.    switch (format) {
  878.       case GL_DEPTH24_STENCIL8_EXT:
  879.       case GL_DEPTH_STENCIL_EXT:
  880.          return GL_TRUE;
  881.       default:
  882.          return GL_FALSE;
  883.    }
  884. }
  885.  
  886.  
  887. /**
  888.  * Test if the given image format is a depth or stencil format.
  889.  */
  890. GLboolean
  891. _mesa_is_depth_or_stencil_format(GLenum format)
  892. {
  893.    switch (format) {
  894.       case GL_DEPTH_COMPONENT:
  895.       case GL_DEPTH_COMPONENT16:
  896.       case GL_DEPTH_COMPONENT24:
  897.       case GL_DEPTH_COMPONENT32:
  898.       case GL_STENCIL_INDEX:
  899.       case GL_STENCIL_INDEX1_EXT:
  900.       case GL_STENCIL_INDEX4_EXT:
  901.       case GL_STENCIL_INDEX8_EXT:
  902.       case GL_STENCIL_INDEX16_EXT:
  903.       case GL_DEPTH_STENCIL_EXT:
  904.       case GL_DEPTH24_STENCIL8_EXT:
  905.          return GL_TRUE;
  906.       default:
  907.          return GL_FALSE;
  908.    }
  909. }
  910.  
  911.  
  912. /**
  913.  * Test if the given image format is a dudv format.
  914.  */
  915. GLboolean
  916. _mesa_is_dudv_format(GLenum format)
  917. {
  918.    switch (format) {
  919.       case GL_DUDV_ATI:
  920.       case GL_DU8DV8_ATI:
  921.          return GL_TRUE;
  922.       default:
  923.          return GL_FALSE;
  924.    }
  925. }
  926.  
  927.  
  928. /**
  929.  * Test if the given format is an integer (non-normalized) format.
  930.  */
  931. GLboolean
  932. _mesa_is_integer_format(GLenum format)
  933. {
  934.    switch (format) {
  935.    /* generic integer formats */
  936.    case GL_RED_INTEGER_EXT:
  937.    case GL_GREEN_INTEGER_EXT:
  938.    case GL_BLUE_INTEGER_EXT:
  939.    case GL_ALPHA_INTEGER_EXT:
  940.    case GL_RGB_INTEGER_EXT:
  941.    case GL_RGBA_INTEGER_EXT:
  942.    case GL_BGR_INTEGER_EXT:
  943.    case GL_BGRA_INTEGER_EXT:
  944.    case GL_LUMINANCE_INTEGER_EXT:
  945.    case GL_LUMINANCE_ALPHA_INTEGER_EXT:
  946.    /* specific integer formats */
  947.    case GL_RGBA32UI_EXT:
  948.    case GL_RGB32UI_EXT:
  949.    case GL_ALPHA32UI_EXT:
  950.    case GL_INTENSITY32UI_EXT:
  951.    case GL_LUMINANCE32UI_EXT:
  952.    case GL_LUMINANCE_ALPHA32UI_EXT:
  953.    case GL_RGBA16UI_EXT:
  954.    case GL_RGB16UI_EXT:
  955.    case GL_ALPHA16UI_EXT:
  956.    case GL_INTENSITY16UI_EXT:
  957.    case GL_LUMINANCE16UI_EXT:
  958.    case GL_LUMINANCE_ALPHA16UI_EXT:
  959.    case GL_RGBA8UI_EXT:
  960.    case GL_RGB8UI_EXT:
  961.    case GL_ALPHA8UI_EXT:
  962.    case GL_INTENSITY8UI_EXT:
  963.    case GL_LUMINANCE8UI_EXT:
  964.    case GL_LUMINANCE_ALPHA8UI_EXT:
  965.    case GL_RGBA32I_EXT:
  966.    case GL_RGB32I_EXT:
  967.    case GL_ALPHA32I_EXT:
  968.    case GL_INTENSITY32I_EXT:
  969.    case GL_LUMINANCE32I_EXT:
  970.    case GL_LUMINANCE_ALPHA32I_EXT:
  971.    case GL_RGBA16I_EXT:
  972.    case GL_RGB16I_EXT:
  973.    case GL_ALPHA16I_EXT:
  974.    case GL_INTENSITY16I_EXT:
  975.    case GL_LUMINANCE16I_EXT:
  976.    case GL_LUMINANCE_ALPHA16I_EXT:
  977.    case GL_RGBA8I_EXT:
  978.    case GL_RGB8I_EXT:
  979.    case GL_ALPHA8I_EXT:
  980.    case GL_INTENSITY8I_EXT:
  981.    case GL_LUMINANCE8I_EXT:
  982.    case GL_LUMINANCE_ALPHA8I_EXT:
  983.       return GL_TRUE;
  984.    default:
  985.       return GL_FALSE;
  986.    }
  987. }
  988.  
  989.  
  990. /**
  991.  * Test if an image format is a supported compressed format.
  992.  * \param format the internal format token provided by the user.
  993.  * \return GL_TRUE if compressed, GL_FALSE if uncompressed
  994.  */
  995. GLboolean
  996. _mesa_is_compressed_format(struct gl_context *ctx, GLenum format)
  997. {
  998.    switch (format) {
  999.    case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  1000.    case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  1001.    case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  1002.    case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  1003.       return ctx->Extensions.EXT_texture_compression_s3tc;
  1004.    case GL_RGB_S3TC:
  1005.    case GL_RGB4_S3TC:
  1006.    case GL_RGBA_S3TC:
  1007.    case GL_RGBA4_S3TC:
  1008.       return ctx->Extensions.S3_s3tc;
  1009.    case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
  1010.    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
  1011.    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
  1012.    case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
  1013.       return ctx->Extensions.EXT_texture_sRGB
  1014.          && ctx->Extensions.EXT_texture_compression_s3tc;
  1015.    case GL_COMPRESSED_RGB_FXT1_3DFX:
  1016.    case GL_COMPRESSED_RGBA_FXT1_3DFX:
  1017.       return ctx->Extensions.TDFX_texture_compression_FXT1;
  1018.    case GL_COMPRESSED_RED_RGTC1:
  1019.    case GL_COMPRESSED_SIGNED_RED_RGTC1:
  1020.    case GL_COMPRESSED_RG_RGTC2:
  1021.    case GL_COMPRESSED_SIGNED_RG_RGTC2:
  1022.       return ctx->Extensions.ARB_texture_compression_rgtc;
  1023.    default:
  1024.       return GL_FALSE;
  1025.    }
  1026. }
  1027.  
  1028.  
  1029. /**
  1030.  * Return the address of a specific pixel in an image (1D, 2D or 3D).
  1031.  *
  1032.  * Pixel unpacking/packing parameters are observed according to \p packing.
  1033.  *
  1034.  * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
  1035.  * \param image  starting address of image data
  1036.  * \param width  the image width
  1037.  * \param height  theimage height
  1038.  * \param format  the pixel format
  1039.  * \param type  the pixel data type
  1040.  * \param packing  the pixelstore attributes
  1041.  * \param img  which image in the volume (0 for 1D or 2D images)
  1042.  * \param row  row of pixel in the image (0 for 1D images)
  1043.  * \param column column of pixel in the image
  1044.  *
  1045.  * \return address of pixel on success, or NULL on error.
  1046.  *
  1047.  * \sa gl_pixelstore_attrib.
  1048.  */
  1049. GLvoid *
  1050. _mesa_image_address( GLuint dimensions,
  1051.                      const struct gl_pixelstore_attrib *packing,
  1052.                      const GLvoid *image,
  1053.                      GLsizei width, GLsizei height,
  1054.                      GLenum format, GLenum type,
  1055.                      GLint img, GLint row, GLint column )
  1056. {
  1057.    GLint alignment;        /* 1, 2 or 4 */
  1058.    GLint pixels_per_row;
  1059.    GLint rows_per_image;
  1060.    GLint skiprows;
  1061.    GLint skippixels;
  1062.    GLint skipimages;       /* for 3-D volume images */
  1063.    GLubyte *pixel_addr;
  1064.  
  1065.    ASSERT(dimensions >= 1 && dimensions <= 3);
  1066.  
  1067.    alignment = packing->Alignment;
  1068.    if (packing->RowLength > 0) {
  1069.       pixels_per_row = packing->RowLength;
  1070.    }
  1071.    else {
  1072.       pixels_per_row = width;
  1073.    }
  1074.    if (packing->ImageHeight > 0) {
  1075.       rows_per_image = packing->ImageHeight;
  1076.    }
  1077.    else {
  1078.       rows_per_image = height;
  1079.    }
  1080.  
  1081.    skippixels = packing->SkipPixels;
  1082.    /* Note: SKIP_ROWS _is_ used for 1D images */
  1083.    skiprows = packing->SkipRows;
  1084.    /* Note: SKIP_IMAGES is only used for 3D images */
  1085.    skipimages = (dimensions == 3) ? packing->SkipImages : 0;
  1086.  
  1087.    if (type == GL_BITMAP) {
  1088.       /* BITMAP data */
  1089.       GLint comp_per_pixel;   /* components per pixel */
  1090.       GLint bytes_per_comp;   /* bytes per component */
  1091.       GLint bytes_per_row;
  1092.       GLint bytes_per_image;
  1093.  
  1094.       /* Compute bytes per component */
  1095.       bytes_per_comp = _mesa_sizeof_packed_type( type );
  1096.       if (bytes_per_comp < 0) {
  1097.          return NULL;
  1098.       }
  1099.  
  1100.       /* Compute number of components per pixel */
  1101.       comp_per_pixel = _mesa_components_in_format( format );
  1102.       if (comp_per_pixel < 0) {
  1103.          return NULL;
  1104.       }
  1105.  
  1106.       bytes_per_row = alignment
  1107.                     * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
  1108.  
  1109.       bytes_per_image = bytes_per_row * rows_per_image;
  1110.  
  1111.       pixel_addr = (GLubyte *) image
  1112.                  + (skipimages + img) * bytes_per_image
  1113.                  + (skiprows + row) * bytes_per_row
  1114.                  + (skippixels + column) / 8;
  1115.    }
  1116.    else {
  1117.       /* Non-BITMAP data */
  1118.       GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
  1119.       GLint topOfImage;
  1120.  
  1121.       bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
  1122.  
  1123.       /* The pixel type and format should have been error checked earlier */
  1124.       assert(bytes_per_pixel > 0);
  1125.  
  1126.       bytes_per_row = pixels_per_row * bytes_per_pixel;
  1127.       remainder = bytes_per_row % alignment;
  1128.       if (remainder > 0)
  1129.          bytes_per_row += (alignment - remainder);
  1130.  
  1131.       ASSERT(bytes_per_row % alignment == 0);
  1132.  
  1133.       bytes_per_image = bytes_per_row * rows_per_image;
  1134.  
  1135.       if (packing->Invert) {
  1136.          /* set pixel_addr to the last row */
  1137.          topOfImage = bytes_per_row * (height - 1);
  1138.          bytes_per_row = -bytes_per_row;
  1139.       }
  1140.       else {
  1141.          topOfImage = 0;
  1142.       }
  1143.  
  1144.       /* compute final pixel address */
  1145.       pixel_addr = (GLubyte *) image
  1146.                  + (skipimages + img) * bytes_per_image
  1147.                  + topOfImage
  1148.                  + (skiprows + row) * bytes_per_row
  1149.                  + (skippixels + column) * bytes_per_pixel;
  1150.    }
  1151.  
  1152.    return (GLvoid *) pixel_addr;
  1153. }
  1154.  
  1155.  
  1156. GLvoid *
  1157. _mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
  1158.                        const GLvoid *image,
  1159.                        GLsizei width,
  1160.                        GLenum format, GLenum type,
  1161.                        GLint column )
  1162. {
  1163.    return _mesa_image_address(1, packing, image, width, 1,
  1164.                               format, type, 0, 0, column);
  1165. }
  1166.  
  1167.  
  1168. GLvoid *
  1169. _mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
  1170.                        const GLvoid *image,
  1171.                        GLsizei width, GLsizei height,
  1172.                        GLenum format, GLenum type,
  1173.                        GLint row, GLint column )
  1174. {
  1175.    return _mesa_image_address(2, packing, image, width, height,
  1176.                               format, type, 0, row, column);
  1177. }
  1178.  
  1179.  
  1180. GLvoid *
  1181. _mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
  1182.                        const GLvoid *image,
  1183.                        GLsizei width, GLsizei height,
  1184.                        GLenum format, GLenum type,
  1185.                        GLint img, GLint row, GLint column )
  1186. {
  1187.    return _mesa_image_address(3, packing, image, width, height,
  1188.                               format, type, img, row, column);
  1189. }
  1190.  
  1191.  
  1192.  
  1193. /**
  1194.  * Compute the stride (in bytes) between image rows.
  1195.  *
  1196.  * \param packing the pixelstore attributes
  1197.  * \param width image width.
  1198.  * \param format pixel format.
  1199.  * \param type pixel data type.
  1200.  *
  1201.  * \return the stride in bytes for the given parameters, or -1 if error
  1202.  */
  1203. GLint
  1204. _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
  1205.                         GLint width, GLenum format, GLenum type )
  1206. {
  1207.    GLint bytesPerRow, remainder;
  1208.  
  1209.    ASSERT(packing);
  1210.  
  1211.    if (type == GL_BITMAP) {
  1212.       if (packing->RowLength == 0) {
  1213.          bytesPerRow = (width + 7) / 8;
  1214.       }
  1215.       else {
  1216.          bytesPerRow = (packing->RowLength + 7) / 8;
  1217.       }
  1218.    }
  1219.    else {
  1220.       /* Non-BITMAP data */
  1221.       const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
  1222.       if (bytesPerPixel <= 0)
  1223.          return -1;  /* error */
  1224.       if (packing->RowLength == 0) {
  1225.          bytesPerRow = bytesPerPixel * width;
  1226.       }
  1227.       else {
  1228.          bytesPerRow = bytesPerPixel * packing->RowLength;
  1229.       }
  1230.    }
  1231.  
  1232.    remainder = bytesPerRow % packing->Alignment;
  1233.    if (remainder > 0) {
  1234.       bytesPerRow += (packing->Alignment - remainder);
  1235.    }
  1236.  
  1237.    if (packing->Invert) {
  1238.       /* negate the bytes per row (negative row stride) */
  1239.       bytesPerRow = -bytesPerRow;
  1240.    }
  1241.  
  1242.    return bytesPerRow;
  1243. }
  1244.  
  1245.  
  1246. /*
  1247.  * Compute the stride between images in a 3D texture (in bytes) for the given
  1248.  * pixel packing parameters and image width, format and type.
  1249.  */
  1250. GLint
  1251. _mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
  1252.                           GLint width, GLint height,
  1253.                           GLenum format, GLenum type )
  1254. {
  1255.    GLint bytesPerRow, bytesPerImage, remainder;
  1256.  
  1257.    ASSERT(packing);
  1258.  
  1259.    if (type == GL_BITMAP) {
  1260.       if (packing->RowLength == 0) {
  1261.          bytesPerRow = (width + 7) / 8;
  1262.       }
  1263.       else {
  1264.          bytesPerRow = (packing->RowLength + 7) / 8;
  1265.       }
  1266.    }
  1267.    else {
  1268.       const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
  1269.  
  1270.       if (bytesPerPixel <= 0)
  1271.          return -1;  /* error */
  1272.       if (packing->RowLength == 0) {
  1273.          bytesPerRow = bytesPerPixel * width;
  1274.       }
  1275.       else {
  1276.          bytesPerRow = bytesPerPixel * packing->RowLength;
  1277.       }
  1278.    }
  1279.  
  1280.    remainder = bytesPerRow % packing->Alignment;
  1281.    if (remainder > 0)
  1282.       bytesPerRow += (packing->Alignment - remainder);
  1283.  
  1284.    if (packing->ImageHeight == 0)
  1285.       bytesPerImage = bytesPerRow * height;
  1286.    else
  1287.       bytesPerImage = bytesPerRow * packing->ImageHeight;
  1288.  
  1289.    return bytesPerImage;
  1290. }
  1291.  
  1292.  
  1293.  
  1294. /**
  1295.  * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
  1296.  * This is typically used to convert a bitmap into a GLubyte/pixel texture.
  1297.  * "On" bits will set texels to \p onValue.
  1298.  * "Off" bits will not modify texels.
  1299.  * \param width  src bitmap width in pixels
  1300.  * \param height  src bitmap height in pixels
  1301.  * \param unpack  bitmap unpacking state
  1302.  * \param bitmap  the src bitmap data
  1303.  * \param destBuffer  start of dest buffer
  1304.  * \param destStride  row stride in dest buffer
  1305.  * \param onValue  if bit is 1, set destBuffer pixel to this value
  1306.  */
  1307. void
  1308. _mesa_expand_bitmap(GLsizei width, GLsizei height,
  1309.                     const struct gl_pixelstore_attrib *unpack,
  1310.                     const GLubyte *bitmap,
  1311.                     GLubyte *destBuffer, GLint destStride,
  1312.                     GLubyte onValue)
  1313. {
  1314.    const GLubyte *srcRow = (const GLubyte *)
  1315.       _mesa_image_address2d(unpack, bitmap, width, height,
  1316.                             GL_COLOR_INDEX, GL_BITMAP, 0, 0);
  1317.    const GLint srcStride = _mesa_image_row_stride(unpack, width,
  1318.                                                   GL_COLOR_INDEX, GL_BITMAP);
  1319.    GLint row, col;
  1320.  
  1321. #define SET_PIXEL(COL, ROW) \
  1322.    destBuffer[(ROW) * destStride + (COL)] = onValue;
  1323.  
  1324.    for (row = 0; row < height; row++) {
  1325.       const GLubyte *src = srcRow;
  1326.  
  1327.       if (unpack->LsbFirst) {
  1328.          /* Lsb first */
  1329.          GLubyte mask = 1U << (unpack->SkipPixels & 0x7);
  1330.          for (col = 0; col < width; col++) {
  1331.  
  1332.             if (*src & mask) {
  1333.                SET_PIXEL(col, row);
  1334.             }
  1335.  
  1336.             if (mask == 128U) {
  1337.                src++;
  1338.                mask = 1U;
  1339.             }
  1340.             else {
  1341.                mask = mask << 1;
  1342.             }
  1343.          }
  1344.  
  1345.          /* get ready for next row */
  1346.          if (mask != 1)
  1347.             src++;
  1348.       }
  1349.       else {
  1350.          /* Msb first */
  1351.          GLubyte mask = 128U >> (unpack->SkipPixels & 0x7);
  1352.          for (col = 0; col < width; col++) {
  1353.  
  1354.             if (*src & mask) {
  1355.                SET_PIXEL(col, row);
  1356.             }
  1357.  
  1358.             if (mask == 1U) {
  1359.                src++;
  1360.                mask = 128U;
  1361.             }
  1362.             else {
  1363.                mask = mask >> 1;
  1364.             }
  1365.          }
  1366.  
  1367.          /* get ready for next row */
  1368.          if (mask != 128)
  1369.             src++;
  1370.       }
  1371.  
  1372.       srcRow += srcStride;
  1373.    } /* row */
  1374.  
  1375. #undef SET_PIXEL
  1376. }
  1377.  
  1378.  
  1379.  
  1380.  
  1381. /**
  1382.  * Convert an array of RGBA colors from one datatype to another.
  1383.  * NOTE: src may equal dst.  In that case, we use a temporary buffer.
  1384.  */
  1385. void
  1386. _mesa_convert_colors(GLenum srcType, const GLvoid *src,
  1387.                      GLenum dstType, GLvoid *dst,
  1388.                      GLuint count, const GLubyte mask[])
  1389. {
  1390.    GLuint tempBuffer[MAX_WIDTH][4];
  1391.    const GLboolean useTemp = (src == dst);
  1392.  
  1393.    ASSERT(srcType != dstType);
  1394.  
  1395.    switch (srcType) {
  1396.    case GL_UNSIGNED_BYTE:
  1397.       if (dstType == GL_UNSIGNED_SHORT) {
  1398.          const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
  1399.          GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
  1400.          GLuint i;
  1401.          for (i = 0; i < count; i++) {
  1402.             if (!mask || mask[i]) {
  1403.                dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
  1404.                dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
  1405.                dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
  1406.                dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
  1407.             }
  1408.          }
  1409.          if (useTemp)
  1410.             memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
  1411.       }
  1412.       else {
  1413.          const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
  1414.          GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
  1415.          GLuint i;
  1416.          ASSERT(dstType == GL_FLOAT);
  1417.          for (i = 0; i < count; i++) {
  1418.             if (!mask || mask[i]) {
  1419.                dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
  1420.                dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
  1421.                dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
  1422.                dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
  1423.             }
  1424.          }
  1425.          if (useTemp)
  1426.             memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
  1427.       }
  1428.       break;
  1429.    case GL_UNSIGNED_SHORT:
  1430.       if (dstType == GL_UNSIGNED_BYTE) {
  1431.          const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
  1432.          GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
  1433.          GLuint i;
  1434.          for (i = 0; i < count; i++) {
  1435.             if (!mask || mask[i]) {
  1436.                dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
  1437.                dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
  1438.                dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
  1439.                dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
  1440.             }
  1441.          }
  1442.          if (useTemp)
  1443.             memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
  1444.       }
  1445.       else {
  1446.          const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
  1447.          GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
  1448.          GLuint i;
  1449.          ASSERT(dstType == GL_FLOAT);
  1450.          for (i = 0; i < count; i++) {
  1451.             if (!mask || mask[i]) {
  1452.                dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
  1453.                dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
  1454.                dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
  1455.                dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
  1456.             }
  1457.          }
  1458.          if (useTemp)
  1459.             memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
  1460.       }
  1461.       break;
  1462.    case GL_FLOAT:
  1463.       if (dstType == GL_UNSIGNED_BYTE) {
  1464.          const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
  1465.          GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
  1466.          GLuint i;
  1467.          for (i = 0; i < count; i++) {
  1468.             if (!mask || mask[i]) {
  1469.                UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
  1470.                UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
  1471.                UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
  1472.                UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
  1473.             }
  1474.          }
  1475.          if (useTemp)
  1476.             memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
  1477.       }
  1478.       else {
  1479.          const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
  1480.          GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
  1481.          GLuint i;
  1482.          ASSERT(dstType == GL_UNSIGNED_SHORT);
  1483.          for (i = 0; i < count; i++) {
  1484.             if (!mask || mask[i]) {
  1485.                UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
  1486.                UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
  1487.                UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
  1488.                UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
  1489.             }
  1490.          }
  1491.          if (useTemp)
  1492.             memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
  1493.       }
  1494.       break;
  1495.    default:
  1496.       _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
  1497.    }
  1498. }
  1499.  
  1500.  
  1501.  
  1502.  
  1503. /**
  1504.  * Perform basic clipping for glDrawPixels.  The image's position and size
  1505.  * and the unpack SkipPixels and SkipRows are adjusted so that the image
  1506.  * region is entirely within the window and scissor bounds.
  1507.  * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
  1508.  * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
  1509.  * we'll actually write.  Beforehand, *destY-1 is the first drawing row.
  1510.  *
  1511.  * \return  GL_TRUE if image is ready for drawing or
  1512.  *          GL_FALSE if image was completely clipped away (draw nothing)
  1513.  */
  1514. GLboolean
  1515. _mesa_clip_drawpixels(const struct gl_context *ctx,
  1516.                       GLint *destX, GLint *destY,
  1517.                       GLsizei *width, GLsizei *height,
  1518.                       struct gl_pixelstore_attrib *unpack)
  1519. {
  1520.    const struct gl_framebuffer *buffer = ctx->DrawBuffer;
  1521.  
  1522.    if (unpack->RowLength == 0) {
  1523.       unpack->RowLength = *width;
  1524.    }
  1525.  
  1526.    ASSERT(ctx->Pixel.ZoomX == 1.0F);
  1527.    ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
  1528.  
  1529.    /* left clipping */
  1530.    if (*destX < buffer->_Xmin) {
  1531.       unpack->SkipPixels += (buffer->_Xmin - *destX);
  1532.       *width -= (buffer->_Xmin - *destX);
  1533.       *destX = buffer->_Xmin;
  1534.    }
  1535.    /* right clipping */
  1536.    if (*destX + *width > buffer->_Xmax)
  1537.       *width -= (*destX + *width - buffer->_Xmax);
  1538.  
  1539.    if (*width <= 0)
  1540.       return GL_FALSE;
  1541.  
  1542.    if (ctx->Pixel.ZoomY == 1.0F) {
  1543.       /* bottom clipping */
  1544.       if (*destY < buffer->_Ymin) {
  1545.          unpack->SkipRows += (buffer->_Ymin - *destY);
  1546.          *height -= (buffer->_Ymin - *destY);
  1547.          *destY = buffer->_Ymin;
  1548.       }
  1549.       /* top clipping */
  1550.       if (*destY + *height > buffer->_Ymax)
  1551.          *height -= (*destY + *height - buffer->_Ymax);
  1552.    }
  1553.    else { /* upside down */
  1554.       /* top clipping */
  1555.       if (*destY > buffer->_Ymax) {
  1556.          unpack->SkipRows += (*destY - buffer->_Ymax);
  1557.          *height -= (*destY - buffer->_Ymax);
  1558.          *destY = buffer->_Ymax;
  1559.       }
  1560.       /* bottom clipping */
  1561.       if (*destY - *height < buffer->_Ymin)
  1562.          *height -= (buffer->_Ymin - (*destY - *height));
  1563.       /* adjust destY so it's the first row to write to */
  1564.       (*destY)--;
  1565.    }
  1566.  
  1567.    if (*height <= 0)
  1568.       return GL_FALSE;
  1569.  
  1570.    return GL_TRUE;
  1571. }
  1572.  
  1573.  
  1574. /**
  1575.  * Perform clipping for glReadPixels.  The image's window position
  1576.  * and size, and the pack skipPixels, skipRows and rowLength are adjusted
  1577.  * so that the image region is entirely within the window bounds.
  1578.  * Note: this is different from _mesa_clip_drawpixels() in that the
  1579.  * scissor box is ignored, and we use the bounds of the current readbuffer
  1580.  * surface.
  1581.  *
  1582.  * \return  GL_TRUE if image is ready for drawing or
  1583.  *          GL_FALSE if image was completely clipped away (draw nothing)
  1584.  */
  1585. GLboolean
  1586. _mesa_clip_readpixels(const struct gl_context *ctx,
  1587.                       GLint *srcX, GLint *srcY,
  1588.                       GLsizei *width, GLsizei *height,
  1589.                       struct gl_pixelstore_attrib *pack)
  1590. {
  1591.    const struct gl_framebuffer *buffer = ctx->ReadBuffer;
  1592.  
  1593.    if (pack->RowLength == 0) {
  1594.       pack->RowLength = *width;
  1595.    }
  1596.  
  1597.    /* left clipping */
  1598.    if (*srcX < 0) {
  1599.       pack->SkipPixels += (0 - *srcX);
  1600.       *width -= (0 - *srcX);
  1601.       *srcX = 0;
  1602.    }
  1603.    /* right clipping */
  1604.    if (*srcX + *width > (GLsizei) buffer->Width)
  1605.       *width -= (*srcX + *width - buffer->Width);
  1606.  
  1607.    if (*width <= 0)
  1608.       return GL_FALSE;
  1609.  
  1610.    /* bottom clipping */
  1611.    if (*srcY < 0) {
  1612.       pack->SkipRows += (0 - *srcY);
  1613.       *height -= (0 - *srcY);
  1614.       *srcY = 0;
  1615.    }
  1616.    /* top clipping */
  1617.    if (*srcY + *height > (GLsizei) buffer->Height)
  1618.       *height -= (*srcY + *height - buffer->Height);
  1619.  
  1620.    if (*height <= 0)
  1621.       return GL_FALSE;
  1622.  
  1623.    return GL_TRUE;
  1624. }
  1625.  
  1626.  
  1627. /**
  1628.  * Do clipping for a glCopyTexSubImage call.
  1629.  * The framebuffer source region might extend outside the framebuffer
  1630.  * bounds.  Clip the source region against the framebuffer bounds and
  1631.  * adjust the texture/dest position and size accordingly.
  1632.  *
  1633.  * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
  1634.  */
  1635. GLboolean
  1636. _mesa_clip_copytexsubimage(const struct gl_context *ctx,
  1637.                            GLint *destX, GLint *destY,
  1638.                            GLint *srcX, GLint *srcY,
  1639.                            GLsizei *width, GLsizei *height)
  1640. {
  1641.    const struct gl_framebuffer *fb = ctx->ReadBuffer;
  1642.    const GLint srcX0 = *srcX, srcY0 = *srcY;
  1643.  
  1644.    if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
  1645.                             srcX, srcY, width, height)) {
  1646.       *destX = *destX + *srcX - srcX0;
  1647.       *destY = *destY + *srcY - srcY0;
  1648.  
  1649.       return GL_TRUE;
  1650.    }
  1651.    else {
  1652.       return GL_FALSE;
  1653.    }
  1654. }
  1655.  
  1656.  
  1657.  
  1658. /**
  1659.  * Clip the rectangle defined by (x, y, width, height) against the bounds
  1660.  * specified by [xmin, xmax) and [ymin, ymax).
  1661.  * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
  1662.  */
  1663. GLboolean
  1664. _mesa_clip_to_region(GLint xmin, GLint ymin,
  1665.                      GLint xmax, GLint ymax,
  1666.                      GLint *x, GLint *y,
  1667.                      GLsizei *width, GLsizei *height )
  1668. {
  1669.    /* left clipping */
  1670.    if (*x < xmin) {
  1671.       *width -= (xmin - *x);
  1672.       *x = xmin;
  1673.    }
  1674.  
  1675.    /* right clipping */
  1676.    if (*x + *width > xmax)
  1677.       *width -= (*x + *width - xmax);
  1678.  
  1679.    if (*width <= 0)
  1680.       return GL_FALSE;
  1681.  
  1682.    /* bottom (or top) clipping */
  1683.    if (*y < ymin) {
  1684.       *height -= (ymin - *y);
  1685.       *y = ymin;
  1686.    }
  1687.  
  1688.    /* top (or bottom) clipping */
  1689.    if (*y + *height > ymax)
  1690.       *height -= (*y + *height - ymax);
  1691.  
  1692.    if (*height <= 0)
  1693.       return GL_FALSE;
  1694.  
  1695.    return GL_TRUE;
  1696. }
  1697.  
  1698.  
  1699. /**
  1700.  * Clip dst coords against Xmax (or Ymax).
  1701.  */
  1702. static INLINE void
  1703. clip_right_or_top(GLint *srcX0, GLint *srcX1,
  1704.                   GLint *dstX0, GLint *dstX1,
  1705.                   GLint maxValue)
  1706. {
  1707.    GLfloat t, bias;
  1708.  
  1709.    if (*dstX1 > maxValue) {
  1710.       /* X1 outside right edge */
  1711.       ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */
  1712.       t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
  1713.       /* chop off [t, 1] part */
  1714.       ASSERT(t >= 0.0 && t <= 1.0);
  1715.       *dstX1 = maxValue;
  1716.       bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
  1717.       *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
  1718.    }
  1719.    else if (*dstX0 > maxValue) {
  1720.       /* X0 outside right edge */
  1721.       ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */
  1722.       t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
  1723.       /* chop off [t, 1] part */
  1724.       ASSERT(t >= 0.0 && t <= 1.0);
  1725.       *dstX0 = maxValue;
  1726.       bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F;
  1727.       *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
  1728.    }
  1729. }
  1730.  
  1731.  
  1732. /**
  1733.  * Clip dst coords against Xmin (or Ymin).
  1734.  */
  1735. static INLINE void
  1736. clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
  1737.                     GLint *dstX0, GLint *dstX1,
  1738.                     GLint minValue)
  1739. {
  1740.    GLfloat t, bias;
  1741.  
  1742.    if (*dstX0 < minValue) {
  1743.       /* X0 outside left edge */
  1744.       ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */
  1745.       t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
  1746.       /* chop off [0, t] part */
  1747.       ASSERT(t >= 0.0 && t <= 1.0);
  1748.       *dstX0 = minValue;
  1749.       bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; /* flipped??? */
  1750.       *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
  1751.    }
  1752.    else if (*dstX1 < minValue) {
  1753.       /* X1 outside left edge */
  1754.       ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */
  1755.       t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
  1756.       /* chop off [0, t] part */
  1757.       ASSERT(t >= 0.0 && t <= 1.0);
  1758.       *dstX1 = minValue;
  1759.       bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F;
  1760.       *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
  1761.    }
  1762. }
  1763.  
  1764.  
  1765. /**
  1766.  * Do clipping of blit src/dest rectangles.
  1767.  * The dest rect is clipped against both the buffer bounds and scissor bounds.
  1768.  * The src rect is just clipped against the buffer bounds.
  1769.  *
  1770.  * When either the src or dest rect is clipped, the other is also clipped
  1771.  * proportionately!
  1772.  *
  1773.  * Note that X0 need not be less than X1 (same for Y) for either the source
  1774.  * and dest rects.  That makes the clipping a little trickier.
  1775.  *
  1776.  * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
  1777.  */
  1778. GLboolean
  1779. _mesa_clip_blit(struct gl_context *ctx,
  1780.                 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
  1781.                 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
  1782. {
  1783.    const GLint srcXmin = 0;
  1784.    const GLint srcXmax = ctx->ReadBuffer->Width;
  1785.    const GLint srcYmin = 0;
  1786.    const GLint srcYmax = ctx->ReadBuffer->Height;
  1787.  
  1788.    /* these include scissor bounds */
  1789.    const GLint dstXmin = ctx->DrawBuffer->_Xmin;
  1790.    const GLint dstXmax = ctx->DrawBuffer->_Xmax;
  1791.    const GLint dstYmin = ctx->DrawBuffer->_Ymin;
  1792.    const GLint dstYmax = ctx->DrawBuffer->_Ymax;
  1793.  
  1794.    /*
  1795.    printf("PreClipX:  src: %d .. %d  dst: %d .. %d\n",
  1796.           *srcX0, *srcX1, *dstX0, *dstX1);
  1797.    printf("PreClipY:  src: %d .. %d  dst: %d .. %d\n",
  1798.           *srcY0, *srcY1, *dstY0, *dstY1);
  1799.    */
  1800.  
  1801.    /* trivial rejection tests */
  1802.    if (*dstX0 == *dstX1)
  1803.       return GL_FALSE; /* no width */
  1804.    if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
  1805.       return GL_FALSE; /* totally out (left) of bounds */
  1806.    if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
  1807.       return GL_FALSE; /* totally out (right) of bounds */
  1808.  
  1809.    if (*dstY0 == *dstY1)
  1810.       return GL_FALSE;
  1811.    if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
  1812.       return GL_FALSE;
  1813.    if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
  1814.       return GL_FALSE;
  1815.  
  1816.    if (*srcX0 == *srcX1)
  1817.       return GL_FALSE;
  1818.    if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
  1819.       return GL_FALSE;
  1820.    if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
  1821.       return GL_FALSE;
  1822.  
  1823.    if (*srcY0 == *srcY1)
  1824.       return GL_FALSE;
  1825.    if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
  1826.       return GL_FALSE;
  1827.    if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
  1828.       return GL_FALSE;
  1829.  
  1830.    /*
  1831.     * dest clip
  1832.     */
  1833.    clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
  1834.    clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
  1835.    clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
  1836.    clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
  1837.  
  1838.    /*
  1839.     * src clip (just swap src/dst values from above)
  1840.     */
  1841.    clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
  1842.    clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
  1843.    clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
  1844.    clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
  1845.  
  1846.    /*
  1847.    printf("PostClipX: src: %d .. %d  dst: %d .. %d\n",
  1848.           *srcX0, *srcX1, *dstX0, *dstX1);
  1849.    printf("PostClipY: src: %d .. %d  dst: %d .. %d\n",
  1850.           *srcY0, *srcY1, *dstY0, *dstY1);
  1851.    */
  1852.  
  1853.    ASSERT(*dstX0 >= dstXmin);
  1854.    ASSERT(*dstX0 <= dstXmax);
  1855.    ASSERT(*dstX1 >= dstXmin);
  1856.    ASSERT(*dstX1 <= dstXmax);
  1857.  
  1858.    ASSERT(*dstY0 >= dstYmin);
  1859.    ASSERT(*dstY0 <= dstYmax);
  1860.    ASSERT(*dstY1 >= dstYmin);
  1861.    ASSERT(*dstY1 <= dstYmax);
  1862.  
  1863.    ASSERT(*srcX0 >= srcXmin);
  1864.    ASSERT(*srcX0 <= srcXmax);
  1865.    ASSERT(*srcX1 >= srcXmin);
  1866.    ASSERT(*srcX1 <= srcXmax);
  1867.  
  1868.    ASSERT(*srcY0 >= srcYmin);
  1869.    ASSERT(*srcY0 <= srcYmax);
  1870.    ASSERT(*srcY1 >= srcYmin);
  1871.    ASSERT(*srcY1 <= srcYmax);
  1872.  
  1873.    return GL_TRUE;
  1874. }
  1875.