home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / image.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  21.4 KB  |  720 lines

  1. /* $Id: image.c,v 1.19 1997/11/07 03:49:04 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.5
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: image.c,v $
  26.  * Revision 1.19  1997/11/07 03:49:04  brianp
  27.  * more error checking work (but more to be done)
  28.  *
  29.  * Revision 1.18  1997/11/02 20:19:47  brianp
  30.  * added more error checking to gl_unpack_image3D()
  31.  *
  32.  * Revision 1.17  1997/10/16 01:04:51  brianp
  33.  * added code to normalize color, depth values in gl_unpack_image3d()
  34.  *
  35.  * Revision 1.16  1997/09/27 00:15:39  brianp
  36.  * changed parameters to gl_unpack_image()
  37.  *
  38.  * Revision 1.15  1997/08/11 01:23:10  brianp
  39.  * added a pointer cast
  40.  *
  41.  * Revision 1.14  1997/07/24 01:25:18  brianp
  42.  * changed precompiled header symbol from PCH to PC_HEADER
  43.  *
  44.  * Revision 1.13  1997/05/28 03:25:26  brianp
  45.  * added precompiled header (PCH) support
  46.  *
  47.  * Revision 1.12  1997/04/29 01:26:25  brianp
  48.  * added #include "context.h"
  49.  *
  50.  * Revision 1.11  1997/04/20 20:28:49  brianp
  51.  * replaced abort() with gl_problem()
  52.  *
  53.  * Revision 1.10  1997/04/06 17:49:32  brianp
  54.  * image reference count wasn't always initialized to zero (Christopher Lloyd)
  55.  *
  56.  * Revision 1.9  1997/02/09 20:05:03  brianp
  57.  * new arguments for gl_pixel_addr_in_image()
  58.  *
  59.  * Revision 1.8  1997/02/09 18:52:53  brianp
  60.  * added GL_EXT_texture3D support
  61.  *
  62.  * Revision 1.7  1997/01/09 21:25:54  brianp
  63.  * initialize image reference count to zero
  64.  *
  65.  * Revision 1.6  1996/11/13 03:58:31  brianp
  66.  * fixed undefined "format" variable in gl_unpack_image()
  67.  *
  68.  * Revision 1.5  1996/11/10 17:48:03  brianp
  69.  * check if format is GL_DEPTH_COMPONENT or GL_STENCIL_COMPONENT
  70.  *
  71.  * Revision 1.4  1996/11/06 04:23:01  brianp
  72.  * changed gl_unpack_image() components argument to srcFormat
  73.  *
  74.  * Revision 1.3  1996/09/27 01:27:10  brianp
  75.  * removed unused variables
  76.  *
  77.  * Revision 1.2  1996/09/26 22:35:10  brianp
  78.  * fixed a few compiler warnings from IRIX 6 -n32 and -64 compiler
  79.  *
  80.  * Revision 1.1  1996/09/13 01:38:16  brianp
  81.  * Initial revision
  82.  *
  83.  */
  84.  
  85.  
  86. #ifdef PC_HEADER
  87. #include "all.h"
  88. #else
  89. #include <assert.h>
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <string.h>
  93. #include "context.h"
  94. #include "image.h"
  95. #include "macros.h"
  96. #include "pixel.h"
  97. #include "types.h"
  98. #endif
  99.  
  100.  
  101.  
  102. /*
  103.  * Flip the 8 bits in each byte of the given array.
  104.  */
  105. void gl_flip_bytes( GLubyte *p, GLuint n )
  106. {
  107.    register GLuint i, a, b;
  108.  
  109.    for (i=0;i<n;i++) {
  110.       b = (GLuint) p[i];
  111.       a = ((b & 0x01) << 7) |
  112.       ((b & 0x02) << 5) |
  113.       ((b & 0x04) << 3) |
  114.       ((b & 0x08) << 1) |
  115.       ((b & 0x10) >> 1) |
  116.       ((b & 0x20) >> 3) |
  117.       ((b & 0x40) >> 5) |
  118.       ((b & 0x80) >> 7);
  119.       p[i] = (GLubyte) a;
  120.    }
  121. }
  122.  
  123.  
  124. /*
  125.  * Flip the order of the 2 bytes in each word in the given array.
  126.  */
  127. void gl_swap2( GLushort *p, GLuint n )
  128. {
  129.    register GLuint i;
  130.  
  131.    for (i=0;i<n;i++) {
  132.       p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
  133.    }
  134. }
  135.  
  136.  
  137.  
  138. /*
  139.  * Flip the order of the 4 bytes in each word in the given array.
  140.  */
  141. void gl_swap4( GLuint *p, GLuint n )
  142. {
  143.    register GLuint i, a, b;
  144.  
  145.    for (i=0;i<n;i++) {
  146.       b = p[i];
  147.       a =  (b >> 24)
  148.     | ((b >> 8) & 0xff00)
  149.     | ((b << 8) & 0xff0000)
  150.     | ((b << 24) & 0xff000000);
  151.       p[i] = a;
  152.    }
  153. }
  154.  
  155.  
  156.  
  157.  
  158. /*
  159.  * Return the size, in bytes, of the given GL datatype.
  160.  * Return 0 if GL_BITMAP.
  161.  * Return -1 if invalid type enum.
  162.  */
  163. GLint gl_sizeof_type( GLenum type )
  164. {
  165.    switch (type) {
  166.       case GL_BITMAP:
  167.      return 0;
  168.       case GL_UNSIGNED_BYTE:
  169.          return sizeof(GLubyte);
  170.       case GL_BYTE:
  171.      return sizeof(GLbyte);
  172.       case GL_UNSIGNED_SHORT:
  173.      return sizeof(GLushort);
  174.       case GL_SHORT:
  175.      return sizeof(GLshort);
  176.       case GL_UNSIGNED_INT:
  177.      return sizeof(GLuint);
  178.       case GL_INT:
  179.      return sizeof(GLint);
  180.       case GL_FLOAT:
  181.      return sizeof(GLfloat);
  182.       default:
  183.          return -1;
  184.    }
  185. }
  186.  
  187.  
  188.  
  189. /*
  190.  * Return the number of components in a GL enum pixel type.
  191.  * Return -1 if bad format.
  192.  */
  193. GLint gl_components_in_format( GLenum format )
  194. {
  195.    switch (format) {
  196.       case GL_COLOR_INDEX:
  197.       case GL_STENCIL_INDEX:
  198.       case GL_DEPTH_COMPONENT:
  199.       case GL_RED:
  200.       case GL_GREEN:
  201.       case GL_BLUE:
  202.       case GL_ALPHA:
  203.       case GL_LUMINANCE:
  204.          return 1;
  205.       case GL_LUMINANCE_ALPHA:
  206.      return 2;
  207.       case GL_RGB:
  208.      return 3;
  209.       case GL_RGBA:
  210.      return 4;
  211.       default:
  212.          return -1;
  213.    }
  214. }
  215.  
  216.  
  217. /*
  218.  * Return the address of a pixel in an image (actually a volume).
  219.  * Pixel unpacking/packing parameters are observed according to 'packing'.
  220.  * Input:  image - start of image data
  221.  *         width, height - size of image
  222.  *         format - image format
  223.  *         type - pixel component type
  224.  *         packing - GL_TRUE = use packing params
  225.  *                   GL_FALSE = use unpacking params.
  226.  *         img - which image in the volume (0 for 2-D images)
  227.  *         row, column - location of pixel in the image
  228.  * Return:  address of pixel at (image,row,column) in image or NULL if error.
  229.  */
  230. GLvoid *gl_pixel_addr_in_image( struct gl_pixelstore_attrib *packing,
  231.                                 const GLvoid *image, GLsizei width,
  232.                                 GLsizei height, GLenum format, GLenum type,
  233.                                 GLint img, GLint row, GLint column )
  234. {
  235.    GLint bytes_per_comp;   /* bytes per component */
  236.    GLint comp_per_pixel;   /* components per pixel */
  237.    GLint comps_per_row;    /* components per row */
  238.    GLint pixels_per_row;   /* pixels per row */
  239.    GLint bytes_per_image;
  240.    GLint rows_per_image;
  241.    GLint alignment;        /* 1, 2 or 4 */
  242.    GLint skiprows;
  243.    GLint skippixels;
  244.    GLint skipimages;       /* for 3-D */
  245.    GLubyte *pixel_addr;
  246.  
  247.    /* Compute bytes per component */
  248.    bytes_per_comp = gl_sizeof_type( type );
  249.    if (bytes_per_comp<0) {
  250.       return NULL;
  251.    }
  252.  
  253.    /* Compute number of components per pixel */
  254.    comp_per_pixel = gl_components_in_format( format );
  255.    if (comp_per_pixel<0) {
  256.       return NULL;
  257.    }
  258.  
  259.    alignment = packing->Alignment;
  260.    if (packing->RowLength>0) {
  261.       pixels_per_row = packing->RowLength;
  262.    }
  263.    else {
  264.       pixels_per_row = width;
  265.    }
  266.    if (packing->ImageHeight>0) {
  267.       rows_per_image = packing->ImageHeight;
  268.    }
  269.    else {
  270.       rows_per_image = height;
  271.    }
  272.    skiprows = packing->SkipRows;
  273.    skippixels = packing->SkipPixels;
  274.    skipimages = packing->SkipImages;
  275.  
  276.    if (type==GL_BITMAP) {
  277.       /* BITMAP data */
  278.       GLint bytes_per_row;
  279.  
  280.       bytes_per_row = alignment
  281.                     * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
  282.  
  283.       bytes_per_image = bytes_per_row * rows_per_image;
  284.  
  285.       pixel_addr = (GLubyte *) image
  286.                  + (skipimages + img) * bytes_per_image
  287.                  + (skiprows + row) * bytes_per_row
  288.                  + (skippixels + column) / 8;
  289.    }
  290.    else {
  291.       /* Non-BITMAP data */
  292.  
  293.       if (bytes_per_comp>=alignment) {
  294.      comps_per_row = comp_per_pixel * pixels_per_row;
  295.       }
  296.       else {
  297.          GLint bytes_per_row = bytes_per_comp * comp_per_pixel
  298.                              * pixels_per_row;
  299.  
  300.      comps_per_row = alignment / bytes_per_comp
  301.                        * CEILING( bytes_per_row, alignment );
  302.       }
  303.  
  304.       bytes_per_image = bytes_per_comp * comps_per_row * rows_per_image;
  305.  
  306.       /* Copy/unpack pixel data to buffer */
  307.       pixel_addr = (GLubyte *) image
  308.                  + (skipimages + img) * bytes_per_image
  309.                  + (skiprows + row) * bytes_per_comp * comps_per_row
  310.                  + (skippixels + column) * bytes_per_comp * comp_per_pixel;
  311.    }
  312.  
  313.    return (GLvoid *) pixel_addr;
  314. }
  315.  
  316.  
  317.  
  318. /*
  319.  * Unpack a 2-D image from user-supplied address, returning a pointer to
  320.  * a new gl_image struct.
  321.  *
  322.  * Input:  width, height - size in pixels
  323.  *         srcFormat - format of incoming pixel data, ignored if
  324.  *                     srcType BITMAP.
  325.  *         srcType - GL_UNSIGNED_BYTE .. GL_FLOAT
  326.  *         pixels - pointer to unpacked image in client memory space.
  327.  */
  328. struct gl_image *gl_unpack_image( GLcontext *ctx,
  329.                                   GLint width, GLint height,
  330.                                   GLenum srcFormat, GLenum srcType,
  331.                                   const GLvoid *pixels )
  332.    return gl_unpack_image3D( ctx, width, height, 1,
  333.                              srcFormat, srcType, pixels );
  334. }
  335.  
  336.  
  337. /*
  338.  *         destType - store image as GL_UNSIGNED_BYTE, GL_FLOAT, or GL_BITMAP.
  339.  *                    if GL_UNSIGNED_BYTE, srctype must be GL_UNSIGNED_BYTE.
  340.  *                    if GL_BITMAP, srctype must be GL_BITMAP.
  341.  */
  342.  
  343. /* 
  344.  * Unpack a 2-D/3-D image from user-supplied address, returning a pointer to
  345.  * a new gl_image struct.
  346.  * Return NULL if there's an error during unpacking.
  347.  * This function is often called by a higher-level unpack function such
  348.  * as gl_unpack_texsubimage() or gl_unpack_bitmap().
  349.  *
  350.  * Input:  width, height, depth - size in pixels
  351.  *         srcFormat - format of incoming pixel data, ignored
  352.  *                      if srcType and destType is BITMAP.
  353.  *         srcType - GL_UNSIGNED_BYTE .. GL_FLOAT
  354.  *         pixels - pointer to unpacked image.
  355.  */
  356. struct gl_image *gl_unpack_image3D( GLcontext *ctx,
  357.                                     GLint width, GLint height, GLint depth,
  358.                                     GLenum srcFormat, GLenum srcType,
  359.                                     const GLvoid *pixels )
  360. {
  361.    GLint components;
  362.    GLenum destType;
  363.  
  364.    if (srcType==GL_UNSIGNED_BYTE) {
  365.       destType = GL_UNSIGNED_BYTE;
  366.    }
  367.    else if (srcType==GL_BITMAP) {
  368.       destType = GL_BITMAP;
  369.    }
  370.    else {
  371.       destType = GL_FLOAT;
  372.    }
  373.  
  374.    components = gl_components_in_format( srcFormat );
  375.  
  376.    if (components < 0)
  377.       return NULL;
  378.  
  379.    if (srcType==GL_BITMAP || destType==GL_BITMAP) {
  380.       struct gl_image *image;
  381.       GLint bytes, i, width_in_bytes, d;
  382.       GLubyte *buffer, *dst;
  383.       assert( srcType==GL_BITMAP );
  384.       assert( destType==GL_BITMAP );
  385.  
  386.       /* Alloc dest storage */
  387.       if (width > 0 && height > 0 && depth > 0)
  388.          bytes = ((width+7)/8 * height) * depth;
  389.       else
  390.          bytes = 0;
  391.       if (bytes>0 && pixels!=NULL) {
  392.          buffer = (GLubyte *) malloc( bytes );
  393.          if (!buffer) {
  394.             return NULL;
  395.          }
  396.          /* Copy/unpack pixel data to buffer */
  397.          width_in_bytes = CEILING( width, 8 );
  398.          dst = buffer;
  399.          for (d=0; d<depth; d++) {
  400.             for (i=0; i<height; i++) {
  401.                GLvoid *src = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
  402.                                                      width, height,
  403.                                                      GL_COLOR_INDEX, srcType,
  404.                                                      d, i, 0 );
  405.                if (!src) {
  406.                   free(buffer);
  407.                   return NULL;
  408.                }
  409.                MEMCPY( dst, src, width_in_bytes );
  410.                dst += width_in_bytes;
  411.             }
  412.          }
  413.          /* Bit flipping */
  414.          if (ctx->Unpack.LsbFirst) {
  415.             gl_flip_bytes( buffer, bytes );
  416.          }
  417.       }
  418.       else {
  419.          /* a 'null' bitmap */
  420.          buffer = NULL;
  421.       }
  422.  
  423.       image = (struct gl_image *) malloc( sizeof(struct gl_image) );
  424.       if (image) {
  425.          image->Width = width;
  426.          image->Height = height;
  427.          image->Depth = depth;
  428.          image->Components = 0;
  429.          image->Format = GL_COLOR_INDEX;
  430.          image->Type = GL_BITMAP;
  431.          image->Data = buffer;
  432.          image->RefCount = 0;
  433.       }
  434.       else {
  435.          if (buffer)
  436.             free( buffer );
  437.          return NULL;
  438.       }
  439.       return image;
  440.    }
  441.    else if (srcFormat==GL_DEPTH_COMPONENT) {
  442.       /* TODO: pack as GLdepth values (GLushort or GLuint) */
  443.  
  444.    }
  445.    else if (srcFormat==GL_STENCIL_INDEX) {
  446.       /* TODO: pack as GLstencil (GLubyte or GLushort) */
  447.  
  448.    }
  449.    else if (destType==GL_UNSIGNED_BYTE) {
  450.       struct gl_image *image;
  451.       GLint width_in_bytes;
  452.       GLubyte *buffer, *dst;
  453.       GLint i, d;
  454.       assert( srcType==GL_UNSIGNED_BYTE );
  455.  
  456.       width_in_bytes = width * components * sizeof(GLubyte);
  457.       buffer = (GLubyte *) malloc( height * width_in_bytes * depth );
  458.       if (!buffer) {
  459.          return NULL;
  460.       }
  461.       /* Copy/unpack pixel data to buffer */
  462.       dst = buffer;
  463.       for (d=0; d<depth; d++ ) {
  464.          for (i=0;i<height;i++) {
  465.             GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack,
  466.                            pixels, width, height, srcFormat, srcType, d, i, 0 );
  467.             if (!src) {
  468.                free(buffer);
  469.                return NULL;
  470.             }
  471.             MEMCPY( dst, src, width_in_bytes );
  472.             dst += width_in_bytes;
  473.          }
  474.       }
  475.  
  476.       if (ctx->Unpack.LsbFirst) {
  477.          gl_flip_bytes( buffer, height * width_in_bytes * depth );
  478.       }
  479.  
  480.       image = (struct gl_image *) malloc( sizeof(struct gl_image) );
  481.       if (image) {
  482.          image->Width = width;
  483.          image->Height = height;
  484.          image->Depth = depth; 
  485.          image->Components = components;
  486.          image->Format = srcFormat;
  487.          image->Type = GL_UNSIGNED_BYTE;
  488.          image->Data = buffer;
  489.          image->RefCount = 0;
  490.       }
  491.       else {
  492.          free( buffer );
  493.          return NULL;
  494.       }
  495.       return image;
  496.    }
  497.    else if (destType==GL_FLOAT) {
  498.       struct gl_image *image;
  499.       GLfloat *buffer, *dst;
  500.       GLint elems_per_row;
  501.       GLint i, j, d;
  502.       GLboolean normalize;
  503.       elems_per_row = width * components;
  504.       buffer = (GLfloat *) malloc( height * elems_per_row * sizeof(GLfloat) * depth);
  505.       if (!buffer) {
  506.          return NULL;
  507.       }
  508.  
  509.       normalize = (srcFormat != GL_COLOR_INDEX)
  510.                && (srcFormat != GL_STENCIL_INDEX);
  511.  
  512.       dst = buffer;
  513.       /**      img_pixels= pixels;*/
  514.       for (d=0; d<depth; d++) {
  515.          for (i=0;i<height;i++) {
  516.             GLvoid *src = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
  517.                                                   width, height,
  518.                                                   srcFormat, srcType,
  519.                                                   d, i, 0 );
  520.             if (!src) {
  521.                free(buffer);
  522.                return NULL;
  523.             }
  524.  
  525.             switch (srcType) {
  526.                case GL_UNSIGNED_BYTE:
  527.                   if (normalize) {
  528.                      for (j=0;j<elems_per_row;j++) {
  529.                         *dst++ = UBYTE_TO_FLOAT(((GLubyte*)src)[j]);
  530.                      }
  531.                   }
  532.                   else {
  533.                      for (j=0;j<elems_per_row;j++) {
  534.                         *dst++ = (GLfloat) ((GLubyte*)src)[j];
  535.                      }
  536.                   }
  537.                   break;
  538.                case GL_BYTE:
  539.                   if (normalize) {
  540.                      for (j=0;j<elems_per_row;j++) {
  541.                         *dst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
  542.                      }
  543.                   }
  544.                   else {
  545.                      for (j=0;j<elems_per_row;j++) {
  546.                         *dst++ = (GLfloat) ((GLbyte*)src)[j];
  547.                      }
  548.                   }
  549.                   break;
  550.                case GL_UNSIGNED_SHORT:
  551.                   if (ctx->Unpack.SwapBytes) {
  552.                      for (j=0;j<elems_per_row;j++) {
  553.                         GLushort value = ((GLushort*)src)[j];
  554.                         value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
  555.                         if (normalize) {
  556.                            *dst++ = USHORT_TO_FLOAT(value);
  557.                         }
  558.                         else {
  559.                            *dst++ = (GLfloat) value;
  560.                         }
  561.                      }
  562.                   }
  563.                   else {
  564.                      if (normalize) {
  565.                         for (j=0;j<elems_per_row;j++) {
  566.                            *dst++ = USHORT_TO_FLOAT(((GLushort*)src)[j]);
  567.                         }
  568.                      }
  569.                      else {
  570.                         for (j=0;j<elems_per_row;j++) {
  571.                            *dst++ = (GLfloat) ((GLushort*)src)[j];
  572.                         }
  573.                      }
  574.                   }
  575.                   break;
  576.                case GL_SHORT:
  577.                   if (ctx->Unpack.SwapBytes) {
  578.                      for (j=0;j<elems_per_row;j++) {
  579.                         GLshort value = ((GLshort*)src)[j];
  580.                         value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
  581.                         if (normalize) {
  582.                            *dst++ = SHORT_TO_FLOAT(value);
  583.                         }
  584.                         else {
  585.                            *dst++ = (GLfloat) value;
  586.                         }
  587.                      }
  588.                   }
  589.                   else {
  590.                      if (normalize) {
  591.                         for (j=0;j<elems_per_row;j++) {
  592.                            *dst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
  593.                         }
  594.                      }
  595.                      else {
  596.                         for (j=0;j<elems_per_row;j++) {
  597.                            *dst++ = (GLfloat) ((GLshort*)src)[j];
  598.                         }
  599.                      }
  600.                   }
  601.                   break;
  602.                case GL_UNSIGNED_INT:
  603.                   if (ctx->Unpack.SwapBytes) {
  604.                      GLuint value;
  605.                      for (j=0;j<elems_per_row;j++) {
  606.                         value = ((GLuint*)src)[j];
  607.                         value = ((value & 0xff000000) >> 24)
  608.                               | ((value & 0x00ff0000) >> 8)
  609.                               | ((value & 0x0000ff00) << 8)
  610.                               | ((value & 0x000000ff) << 24);
  611.                         if (normalize) {
  612.                            *dst++ = UINT_TO_FLOAT(value);
  613.                         }
  614.                         else {
  615.                            *dst++ = (GLfloat) value;
  616.                         }
  617.                      }
  618.                   }
  619.                   else {
  620.                      if (normalize) {
  621.                         for (j=0;j<elems_per_row;j++) {
  622.                            *dst++ = UINT_TO_FLOAT(((GLuint*)src)[j]);
  623.                         }
  624.                      }
  625.                      else {
  626.                         for (j=0;j<elems_per_row;j++) {
  627.                            *dst++ = (GLfloat) ((GLuint*)src)[j];
  628.                         }
  629.                      }
  630.                   }
  631.                   break;
  632.                case GL_INT:
  633.                   if (ctx->Unpack.SwapBytes) {
  634.                      GLint value;
  635.                      for (j=0;j<elems_per_row;j++) {
  636.                         value = ((GLint*)src)[j];
  637.                         value = ((value & 0xff000000) >> 24)
  638.                               | ((value & 0x00ff0000) >> 8)
  639.                               | ((value & 0x0000ff00) << 8)
  640.                               | ((value & 0x000000ff) << 24);
  641.                         if (normalize) {
  642.                            *dst++ = INT_TO_FLOAT(value);
  643.                         }
  644.                         else {
  645.                            *dst++ = (GLfloat) value;
  646.                         }
  647.                      }
  648.                   }
  649.                   else {
  650.                      if (normalize) {
  651.                         for (j=0;j<elems_per_row;j++) {
  652.                            *dst++ = INT_TO_FLOAT(((GLint*)src)[j]);
  653.                         }
  654.                      }
  655.                      else {
  656.                         for (j=0;j<elems_per_row;j++) {
  657.                            *dst++ = (GLfloat) ((GLint*)src)[j];
  658.                         }
  659.                      }
  660.                   }
  661.                   break;
  662.                case GL_FLOAT:
  663.                   if (ctx->Unpack.SwapBytes) {
  664.                      GLint value;
  665.                      for (j=0;j<elems_per_row;j++) {
  666.                         value = ((GLuint*)src)[j];
  667.                         value = ((value & 0xff000000) >> 24)
  668.                               | ((value & 0x00ff0000) >> 8)
  669.                               | ((value & 0x0000ff00) << 8)
  670.                               | ((value & 0x000000ff) << 24);
  671.                         *dst++ = *((GLfloat*) &value);
  672.                      }
  673.                   }
  674.                   else {
  675.                      MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) );
  676.                      dst += elems_per_row;
  677.                   }
  678.                   break;
  679.                default:
  680.                   gl_problem(ctx, "Bad type in gl_unpack_image3D");
  681.                   return NULL;
  682.             } /*switch*/
  683.          } /* for height */
  684.       } /*for*/
  685.  
  686.       image = (struct gl_image *) malloc( sizeof(struct gl_image) );
  687.       if (image) {
  688.          image->Width = width;
  689.          image->Height = height;
  690.          image->Depth = depth;  
  691.          image->Components = components;
  692.          image->Format = srcFormat;
  693.          image->Type = GL_FLOAT;
  694.          image->Data = buffer;
  695.          image->RefCount = 0;
  696.       }
  697.       else {
  698.          free( buffer );
  699.          return NULL;
  700.       }
  701.       return image;
  702.    }
  703.    else {
  704.       gl_problem(ctx, "Bad dest type in gl_unpack_image3D");
  705.       return NULL;
  706.    }
  707.    return NULL;  /* never get here */
  708. }
  709.  
  710.  
  711.  
  712. void gl_free_image( struct gl_image *image )
  713. {
  714.    if (image->Data) {
  715.       free(image->Data);
  716.    }
  717.    free(image);
  718. }
  719.