Ignore:
Timestamp:
May 23, 2000, 10:41:28 PM (25 years ago)
Author:
jeroen
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/opengl/mesa/image.c

    r2962 r3598  
    1 /* $Id: image.c,v 1.2 2000-03-01 18:49:30 jeroen Exp $ */
     1/* $Id: image.c,v 1.3 2000-05-23 20:40:37 jeroen Exp $ */
    22
    33/*
    44 * Mesa 3-D graphics library
    5  * Version:  3.1
     5 * Version:  3.3
    66 *
    77 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
     
    2626
    2727
    28 
    2928#ifdef PC_HEADER
    3029#include "all.h"
    3130#else
    32 #ifndef XFree86Server
    33 #include <assert.h>
    34 #include <stdlib.h>
    35 #include <string.h>
    36 #else
    37 #include "GL/xf86glx.h"
    38 #endif
     31#include "glheader.h"
    3932#include "types.h"
    4033#include "context.h"
     34#include "mem.h"
    4135#include "image.h"
    4236#include "macros.h"
     
    4842
    4943/*
     44 * These are the image packing parameters for Mesa's internal images.
     45 * That is, _mesa_unpack_image() returns image data in this format.
     46 * When we execute image commands (glDrawPixels, glTexImage, etc)
     47 * from within display lists we have to be sure to set the current
     48 * unpacking params to these values!
     49 */
     50const struct gl_pixelstore_attrib _mesa_native_packing = {
     51   1,            /* Alignment */
     52   0,            /* RowLength */
     53   0,            /* SkipPixels */
     54   0,            /* SkipRows */
     55   0,            /* ImageHeight */
     56   0,            /* SkipImages */
     57   GL_FALSE,     /* SwapBytes */
     58   GL_FALSE      /* LsbFirst */
     59};
     60
     61
     62
     63/*
    5064 * Flip the 8 bits in each byte of the given array.
    5165 */
    52 void gl_flip_bytes( GLubyte *p, GLuint n )
     66static void
     67flip_bytes( GLubyte *p, GLuint n )
    5368{
    5469   register GLuint i, a, b;
     
    5772      b = (GLuint) p[i];
    5873      a = ((b & 0x01) << 7) |
    59           ((b & 0x02) << 5) |
    60           ((b & 0x04) << 3) |
    61           ((b & 0x08) << 1) |
    62           ((b & 0x10) >> 1) |
    63           ((b & 0x20) >> 3) |
    64           ((b & 0x40) >> 5) |
    65           ((b & 0x80) >> 7);
     74          ((b & 0x02) << 5) |
     75          ((b & 0x04) << 3) |
     76          ((b & 0x08) << 1) |
     77          ((b & 0x10) >> 1) |
     78          ((b & 0x20) >> 3) |
     79          ((b & 0x40) >> 5) |
     80          ((b & 0x80) >> 7);
    6681      p[i] = (GLubyte) a;
    6782   }
     
    7287 * Flip the order of the 2 bytes in each word in the given array.
    7388 */
    74 void gl_swap2( GLushort *p, GLuint n )
     89void
     90_mesa_swap2( GLushort *p, GLuint n )
    7591{
    7692   register GLuint i;
     
    86102 * Flip the order of the 4 bytes in each word in the given array.
    87103 */
    88 void gl_swap4( GLuint *p, GLuint n )
     104void
     105_mesa_swap4( GLuint *p, GLuint n )
    89106{
    90107   register GLuint i, a, b;
     
    93110      b = p[i];
    94111      a =  (b >> 24)
    95         | ((b >> 8) & 0xff00)
    96         | ((b << 8) & 0xff0000)
    97         | ((b << 24) & 0xff000000);
     112        | ((b >> 8) & 0xff00)
     113        | ((b << 8) & 0xff0000)
     114        | ((b << 24) & 0xff000000);
    98115      p[i] = a;
    99116   }
     
    108125 * Return -1 if invalid type enum.
    109126 */
    110 GLint gl_sizeof_type( GLenum type )
     127GLint _mesa_sizeof_type( GLenum type )
    111128{
    112129   switch (type) {
    113130      case GL_BITMAP:
    114         return 0;
     131        return 0;
    115132      case GL_UNSIGNED_BYTE:
    116133         return sizeof(GLubyte);
    117134      case GL_BYTE:
    118         return sizeof(GLbyte);
     135        return sizeof(GLbyte);
    119136      case GL_UNSIGNED_SHORT:
    120         return sizeof(GLushort);
     137        return sizeof(GLushort);
    121138      case GL_SHORT:
    122         return sizeof(GLshort);
     139        return sizeof(GLshort);
    123140      case GL_UNSIGNED_INT:
    124         return sizeof(GLuint);
     141        return sizeof(GLuint);
    125142      case GL_INT:
    126         return sizeof(GLint);
     143        return sizeof(GLint);
    127144      case GL_FLOAT:
    128         return sizeof(GLfloat);
     145        return sizeof(GLfloat);
    129146      default:
    130147         return -1;
     
    134151
    135152/*
    136  * Same as gl_sizeof_packed_type() but we also accept the
     153 * Same as _mesa_sizeof_packed_type() but we also accept the
    137154 * packed pixel format datatypes.
    138155 */
    139 GLint gl_sizeof_packed_type( GLenum type )
     156GLint _mesa_sizeof_packed_type( GLenum type )
    140157{
    141158   switch (type) {
    142159      case GL_BITMAP:
    143         return 0;
     160        return 0;
    144161      case GL_UNSIGNED_BYTE:
    145162         return sizeof(GLubyte);
    146163      case GL_BYTE:
    147         return sizeof(GLbyte);
     164        return sizeof(GLbyte);
    148165      case GL_UNSIGNED_SHORT:
    149         return sizeof(GLushort);
     166        return sizeof(GLushort);
    150167      case GL_SHORT:
    151         return sizeof(GLshort);
     168        return sizeof(GLshort);
    152169      case GL_UNSIGNED_INT:
    153         return sizeof(GLuint);
     170        return sizeof(GLuint);
    154171      case GL_INT:
    155         return sizeof(GLint);
     172        return sizeof(GLint);
    156173      case GL_FLOAT:
    157         return sizeof(GLfloat);
     174        return sizeof(GLfloat);
    158175      case GL_UNSIGNED_BYTE_3_3_2:
    159176         return sizeof(GLubyte);
     
    191208 * Return -1 if bad format.
    192209 */
    193 GLint gl_components_in_format( GLenum format )
     210GLint _mesa_components_in_format( GLenum format )
    194211{
    195212   switch (format) {
     
    208225      case GL_ALPHA:
    209226      case GL_LUMINANCE:
    210       case GL_INTENSITY:
    211227         return 1;
    212228      case GL_LUMINANCE_ALPHA:
    213         return 2;
     229        return 2;
    214230      case GL_RGB:
    215         return 3;
     231        return 3;
    216232      case GL_RGBA:
    217         return 4;
     233        return 4;
    218234      case GL_BGR:
    219         return 3;
     235        return 3;
    220236      case GL_BGRA:
    221         return 4;
     237        return 4;
    222238      case GL_ABGR_EXT:
    223239         return 4;
     
    232248 * Return -1 if bad format or type.
    233249 */
    234 GLint gl_bytes_per_pixel( GLenum format, GLenum type )
     250GLint _mesa_bytes_per_pixel( GLenum format, GLenum type )
    235251{
    236    GLint comps = gl_components_in_format( format );
     252   GLint comps = _mesa_components_in_format( format );
    237253   if (comps < 0)
    238254      return -1;
     
    290306 * Return GL_TRUE for legal, GL_FALSE for illegal.
    291307 */
    292 GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type )
     308GLboolean
     309_mesa_is_legal_format_and_type( GLenum format, GLenum type )
    293310{
    294311   switch (format) {
     
    388405 * Return:  address of pixel at (image,row,column) in image or NULL if error.
    389406 */
    390 GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
    391                                 const GLvoid *image, GLsizei width,
    392                                 GLsizei height, GLenum format, GLenum type,
    393                                 GLint img, GLint row, GLint column )
     407GLvoid *
     408_mesa_image_address( const struct gl_pixelstore_attrib *packing,
     409                     const GLvoid *image, GLsizei width,
     410                     GLsizei height, GLenum format, GLenum type,
     411                     GLint img, GLint row, GLint column )
    394412{
    395413   GLint alignment;        /* 1, 2 or 4 */
     
    426444
    427445      /* Compute bytes per component */
    428       bytes_per_comp = gl_sizeof_packed_type( type );
     446      bytes_per_comp = _mesa_sizeof_packed_type( type );
    429447      if (bytes_per_comp<0) {
    430448         return NULL;
     
    432450
    433451      /* Compute number of components per pixel */
    434       comp_per_pixel = gl_components_in_format( format );
     452      comp_per_pixel = _mesa_components_in_format( format );
    435453      if (comp_per_pixel<0 && type != GL_BITMAP) {
    436454         return NULL;
     
    451469      GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
    452470
    453       bytes_per_pixel = gl_bytes_per_pixel( format, type );
     471      bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
    454472
    455473      /* The pixel type and format should have been error checked earlier */
    456       assert(bytes_per_pixel > 0);
     474      ASSERT(bytes_per_pixel > 0);
    457475
    458476      bytes_per_row = pixels_per_row * bytes_per_pixel;
     
    478496
    479497/*
    480  * Allocate a new gl_image.  All fields are initialized to zero.
     498 * Compute the stride between image rows (in bytes) for the given
     499 * pixel packing parameters and image width, format and type.
    481500 */
    482 static struct gl_image *alloc_image( void )
     501GLint
     502_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
     503                        GLint width, GLenum format, GLenum type )
    483504{
    484    return CALLOC_STRUCT(gl_image);
    485 }
    486 
    487 
    488 
    489 /*
    490  * Allocate a new gl_image with the error flag set.
    491  */
    492 static struct gl_image *alloc_error_image( GLint width, GLint height,
    493                                            GLint depth, GLenum format,
    494                                            GLenum type )
    495 {
    496    struct gl_image *image = alloc_image();
    497    if (image) {
    498       image->Width = width;
    499       image->Height = height;
    500       image->Depth = depth;
    501       image->Format = format;
    502       image->Type = type;
    503       image->ErrorFlag = GL_TRUE;
    504    }
    505    return image;
    506 }
    507 
    508 
    509 
    510 /*
    511  * Free a gl_image.
    512  */
    513 void gl_free_image( struct gl_image *image )
    514 {
    515    if (image->Data) {
    516       FREE(image->Data);
    517    }
    518    FREE(image);
    519 }
    520 
    521 
    522 
    523 /*
    524  * Do error checking on an image.  If there's an error, register it and
    525  * return GL_TRUE, else return GL_FALSE.
    526  */
    527 GLboolean gl_image_error_test( GLcontext *ctx, const struct gl_image *image,
    528                                const char *msg )
    529 {
    530    if (!image) {
    531       gl_error( ctx, GL_OUT_OF_MEMORY, msg );
    532       return GL_TRUE;
    533    }
    534    if (image->Width <= 0 || image->Height <= 0 || image->Depth <= 0) {
    535       gl_error( ctx, GL_INVALID_VALUE, msg );
    536       return GL_TRUE;
    537    }
    538    else if (!gl_is_legal_format_and_type(image->Format, image->Type)) {
    539       return GL_TRUE;
     505   ASSERT(packing);
     506   if (type == GL_BITMAP) {
     507      /* BITMAP data */
     508      if (packing->RowLength == 0) {
     509         GLint bytes = (width + 7) / 8;
     510         return bytes;
     511      }
     512      else {
     513         GLint bytes = (packing->RowLength + 7) / 8;
     514         return bytes;
     515      }
    540516   }
    541517   else {
    542       return GL_FALSE;
    543    }
    544 }
    545 
    546 
    547 
    548 /*
    549  * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats.
    550  * Input:  type - datatype of src depth image
    551  * Return pointer to a new gl_image structure.
    552  *
    553  * Notes:  if the source image type is GLushort then the gl_image will
    554  * also store GLushorts.  If the src image type is GLuint then the gl_image
    555  * will also store GLuints.  For all other src image types the gl_image
    556  * will store GLfloats.  The integer cases can later be optimized.
    557  */
    558 static struct gl_image *
    559 unpack_depth_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
    560                     const GLvoid *pixels,
    561                     const struct gl_pixelstore_attrib *packing)
    562 
    563 {
    564    struct gl_image *image;
    565    GLfloat *fDst;
    566    GLushort *sDst;
    567    GLuint *iDst;
    568    GLint i, j;
    569    GLboolean errorType;
    570 
    571    errorType = type != GL_BYTE &&
    572                type != GL_UNSIGNED_BYTE &&
    573                type != GL_SHORT &&
    574                type != GL_UNSIGNED_SHORT &&
    575                type != GL_INT &&
    576                type != GL_UNSIGNED_INT &&
    577                type != GL_FLOAT;
    578 
    579    image = alloc_image();
    580    if (image) {
    581       image->Width = width;
    582       image->Height = height;
    583       image->Depth = 1;
    584       image->Components = 1;
    585       image->Format = GL_DEPTH_COMPONENT;
    586       if (errorType) {
    587          image->Type = type;
    588          image->Data = NULL;
    589       }
    590       if (type==GL_UNSIGNED_SHORT) {
    591          image->Type = GL_UNSIGNED_SHORT;
    592          image->Data = MALLOC( width * height * sizeof(GLushort));
    593       }
    594       else if (type==GL_UNSIGNED_INT) {
    595          image->Type = GL_UNSIGNED_INT;
    596          image->Data = MALLOC( width * height * sizeof(GLuint));
     518      /* Non-BITMAP data */
     519      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
     520      if (bytesPerPixel <= 0)
     521         return -1;  /* error */
     522      if (packing->RowLength == 0) {
     523         GLint bytes = bytesPerPixel * width;
     524         return bytes;
    597525      }
    598526      else {
    599          image->Type = GL_FLOAT;
    600          image->Data = MALLOC( width * height * sizeof(GLfloat));
    601       }
    602       image->RefCount = 0;
    603       if (!image->Data)
    604          return image;
    605    }
    606    else {
    607       return NULL;
    608    }
    609 
    610    if (errorType)
    611       return image;
    612 
    613    fDst = (GLfloat *) image->Data;
    614    sDst = (GLushort *) image->Data;
    615    iDst = (GLuint *) image->Data;
    616 
    617    for (i=0;i<height;i++) {
    618       GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
    619                                             width, height,
    620                                             GL_DEPTH_COMPONENT, type,
    621                                             0, i, 0 );
    622       if (!src) {
    623          return image;
    624       }
    625 
    626       switch (type) {
    627          case GL_BYTE:
    628             assert(image->Type == GL_FLOAT);
    629             for (j=0; j<width; j++) {
    630                *fDst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
    631             }
    632             break;
    633          case GL_UNSIGNED_BYTE:
    634             assert(image->Type == GL_FLOAT);
    635             for (j=0; j<width; j++) {
    636                *fDst++ = UBYTE_TO_FLOAT(((GLubyte*)src)[j]);
    637             }
    638             break;
    639          case GL_UNSIGNED_SHORT:
    640             assert(image->Type == GL_UNSIGNED_SHORT);
    641             MEMCPY( sDst, src, width * sizeof(GLushort) );
    642             if (packing->SwapBytes) {
    643                gl_swap2( sDst, width );
    644             }
    645             sDst += width;
    646             break;
    647          case GL_SHORT:
    648             assert(image->Type == GL_FLOAT);
    649             if (packing->SwapBytes) {
    650                for (j=0;j<width;j++) {
    651                   GLshort value = ((GLshort*)src)[j];
    652                   value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
    653                   *fDst++ = SHORT_TO_FLOAT(value);
    654                }
    655             }
    656             else {
    657                for (j=0;j<width;j++) {
    658                   *fDst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
    659                }
    660             }
    661             break;
    662          case GL_INT:
    663             assert(image->Type == GL_FLOAT);
    664             if (packing->SwapBytes) {
    665                for (j=0;j<width;j++) {
    666                   GLint value = ((GLint*)src)[j];
    667                   value = ((value >> 24) & 0x000000ff) |
    668                           ((value >> 8)  & 0x0000ff00) |
    669                           ((value << 8)  & 0x00ff0000) |
    670                           ((value << 24) & 0xff000000);
    671                   *fDst++ = INT_TO_FLOAT(value);
    672                }
    673             }
    674             else {
    675                for (j=0;j<width;j++) {
    676                   *fDst++ = INT_TO_FLOAT(((GLint*)src)[j]);
    677                }
    678             }
    679             iDst += width;
    680             break;
    681          case GL_UNSIGNED_INT:
    682             assert(image->Type == GL_UNSIGNED_INT);
    683             MEMCPY( iDst, src, width * sizeof(GLuint) );
    684             if (packing->SwapBytes) {
    685                gl_swap4( iDst, width );
    686             }
    687             iDst += width;
    688             break;
    689          case GL_FLOAT:
    690             assert(image->Type == GL_FLOAT);
    691             MEMCPY( fDst, src, width * sizeof(GLfloat) );
    692             if (packing->SwapBytes) {
    693                gl_swap4( (GLuint*) fDst, width );
    694             }
    695             fDst += width;
    696             break;
    697          default:
    698             gl_problem(ctx, "unpack_depth_image type" );
    699             return image;
    700       }
    701    }
    702 
    703    return image;
    704 }
    705 
    706 
    707 
    708 /*
    709  * Unpack a stencil image.  Store as GLubytes in a gl_image structure.
    710  * Return:  pointer to new gl_image structure.
    711  */
    712 static struct gl_image *
    713 unpack_stencil_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
    714                       const GLvoid *pixels,
    715                       const struct gl_pixelstore_attrib *packing )
    716 {
    717    struct gl_image *image;
    718    GLubyte *dst;
    719    GLint i, j;
    720    GLboolean errorType;
    721 
    722    assert(sizeof(GLstencil) == sizeof(GLubyte));
    723 
    724    errorType = type != GL_BYTE &&
    725                type != GL_UNSIGNED_BYTE &&
    726                type != GL_SHORT &&
    727                type != GL_UNSIGNED_SHORT &&
    728                type != GL_INT &&
    729                type != GL_UNSIGNED_INT &&
    730                type != GL_FLOAT &&
    731                type != GL_BITMAP;
    732 
    733    image = alloc_image();
    734    if (image) {
    735       image->Width = width;
    736       image->Height = height;
    737       image->Depth = 1;
    738       image->Components = 1;
    739       image->Format = GL_STENCIL_INDEX;
    740       if (errorType) {
    741          image->Type = type;
    742          image->Data = NULL;
    743       }
    744       else {
    745          image->Type = GL_UNSIGNED_BYTE;
    746          image->Data = MALLOC( width * height * sizeof(GLubyte));
    747       }
    748       image->RefCount = 0;
    749       if (!image->Data)
    750          return image;
    751    }
    752    else {
    753       return NULL;
    754    }
    755 
    756    if (errorType)
    757       return image; /* error will be generated later */
    758 
    759    dst = (GLubyte *) image->Data;
    760 
    761    for (i=0;i<height;i++) {
    762       GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
    763                                             width, height,
    764                                             GL_STENCIL_INDEX, type,
    765                                             0, i, 0 );
    766       if (!src) {
    767          return image;
    768       }
    769 
    770       switch (type) {
    771          case GL_UNSIGNED_BYTE:
    772          case GL_BYTE:
    773             MEMCPY( dst, src, width * sizeof(GLubyte) );
    774             dst += width * sizeof(GLubyte);
    775             break;
    776          case GL_UNSIGNED_SHORT:
    777          case GL_SHORT:
    778             if (packing->SwapBytes) {
    779                /* grab upper byte */
    780                for (j=0; j < width; j++) {
    781                   *dst++ = (((GLushort*)src)[j] & 0xff00) >> 8;
    782                }
    783             }
    784             else {
    785                for (j=0; j < width; j++) {
    786                   *dst++ = (((GLushort*)src)[j]) & 0xff;
    787                }
    788             }
    789             break;
    790          case GL_INT:
    791             if (packing->SwapBytes) {
    792                /* grab upper byte */
    793                for (j=0; j < width; j++) {
    794                   *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
    795                }
    796             }
    797             else {
    798                for (j=0; j < width; j++) {
    799                   *dst++ = (((GLuint*)src)[j]) & 0xff;
    800                }
    801             }
    802             break;
    803          case GL_UNSIGNED_INT:
    804             if (packing->SwapBytes) {
    805                /* grab upper byte */
    806                for (j=0; j < width; j++) {
    807                   *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
    808                }
    809             }
    810             else {
    811                for (j=0; j < width; j++) {
    812                   *dst++ = (((GLuint*)src)[j]) & 0xff;
    813                }
    814             }
    815             break;
    816          case GL_FLOAT:
    817             if (packing->SwapBytes) {
    818                for (j=0; j < width; j++) {
    819                   GLfloat fvalue;
    820                   GLint value = ((GLuint*)src)[j];
    821                   value = ((value & 0xff000000) >> 24)
    822                      | ((value & 0x00ff0000) >> 8)
    823                      | ((value & 0x0000ff00) << 8)
    824                      | ((value & 0x000000ff) << 24);
    825                   fvalue = *((GLfloat*) &value);
    826                   *dst++ = ((GLint) fvalue) & 0xff;
    827                }
    828             }
    829             else {
    830                for (j=0; j < width; j++) {
    831                   GLfloat fvalue = ((GLfloat *)src)[j];
    832                   *dst++ = ((GLint) fvalue) & 0xff;
    833                }
    834             }
    835             break;
    836          default:
    837             gl_problem(ctx, "unpack_stencil_image type" );
    838             return image;
    839       }
    840    }
    841 
    842    return image;
    843 }
    844 
    845 
    846 
    847 /*
    848  * Unpack a bitmap, return a new gl_image struct.
    849  */
    850 static struct gl_image *
    851 unpack_bitmap( GLenum format, GLint width, GLint height,
    852                const GLvoid *pixels,
    853                const struct gl_pixelstore_attrib *packing )
    854 {
    855    struct gl_image *image;
    856    GLint bytes, i, width_in_bytes;
    857    GLubyte *buffer, *dst;
    858 
    859    assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX);
    860 
    861    /* Alloc dest storage */
    862    bytes = ((width+7)/8 * height);
    863    if (bytes>0 && pixels!=NULL) {
    864       buffer = (GLubyte *) MALLOC( bytes );
    865       if (!buffer) {
    866          return NULL;
    867       }
    868       /* Copy/unpack pixel data to buffer */
    869       width_in_bytes = CEILING( width, 8 );
    870       dst = buffer;
    871       for (i=0; i<height; i++) {
    872          GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
    873                                                width, height,
    874                                                GL_COLOR_INDEX, GL_BITMAP,
    875                                                0, i, 0 );
    876          if (!src) {
    877             FREE(buffer);
    878             return NULL;
    879          }
    880          MEMCPY( dst, src, width_in_bytes );
    881          dst += width_in_bytes;
    882       }
    883       /* Bit flipping */
    884       if (packing->LsbFirst) {
    885          gl_flip_bytes( buffer, bytes );
    886       }
    887    }
    888    else {
    889       /* a 'null' bitmap */
    890       buffer = NULL;
    891    }
    892 
    893    image = alloc_image();
    894    if (image) {
    895       image->Width = width;
    896       image->Height = height;
    897       image->Depth = 1;
    898       image->Components = 0;
    899       image->Format = format;
    900       image->Type = GL_BITMAP;
    901       image->Data = buffer;
    902       image->RefCount = 0;
    903    }
    904    else {
    905       FREE( buffer );
    906       return NULL;
    907    }
    908 
    909    return image;
     527         GLint bytes = bytesPerPixel * packing->RowLength;
     528         return bytes;
     529      }
     530   }
    910531}
    911532
     
    916537 * current pixel unpack settings.
    917538 */
    918 void gl_unpack_polygon_stipple( const GLcontext *ctx,
    919                                 const GLubyte *pattern, GLuint dest[32] )
     539void
     540_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
     541                              const struct gl_pixelstore_attrib *unpacking )
    920542{
    921    GLint i;
    922    for (i = 0; i < 32; i++) {
    923       GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack, pattern,
    924                                   32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
    925       dest[i] = (src[0] << 24)
    926               | (src[1] << 16)
    927               | (src[2] <<  8)
    928               | (src[3]      );
    929    }
    930 
    931    /* Bit flipping within each byte */
    932    if (ctx->Unpack.LsbFirst) {
    933       gl_flip_bytes( (GLubyte *) dest, 32 * 4 );
     543   GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap( 32, 32, pattern, unpacking );
     544   if (ptrn) {
     545      /* Convert pattern from GLubytes to GLuints and handle big/little
     546       * endian differences
     547       */
     548      GLubyte *p = ptrn;
     549      GLint i;
     550      for (i = 0; i < 32; i++) {
     551         dest[i] = (p[0] << 24)
     552                 | (p[1] << 16)
     553                 | (p[2] <<  8)
     554                 | (p[3]      );
     555         p += 4;
     556      }
     557      FREE(ptrn);
    934558   }
    935559}
     
    941565 * settings.
    942566 */
    943 void gl_pack_polygon_stipple( const GLcontext *ctx,
    944                               const GLuint pattern[32],
    945                               GLubyte *dest )
     567void
     568_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
     569                            const struct gl_pixelstore_attrib *packing )
    946570{
     571   /* Convert pattern from GLuints to GLubytes to handle big/little
     572    * endian differences.
     573    */
     574   GLubyte ptrn[32*4];
    947575   GLint i;
    948576   for (i = 0; i < 32; i++) {
    949       GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image( &ctx->Pack, dest,
    950                                   32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
    951       dst[0] = (pattern[i] >> 24) & 0xff;
    952       dst[1] = (pattern[i] >> 16) & 0xff;
    953       dst[2] = (pattern[i] >>  8) & 0xff;
    954       dst[3] = (pattern[i]      ) & 0xff;
    955 
    956       /* Bit flipping within each byte */
    957       if (ctx->Pack.LsbFirst) {
    958          gl_flip_bytes( (GLubyte *) dst, 4 );
    959       }
    960    }
    961 }
    962 
    963 
    964 
    965 /*
    966  * Unpack an RGBA or CI image and store it as unsigned bytes
    967  */
    968 static struct gl_image *
    969 unpack_ubyte_image( GLint width, GLint height,
    970                     GLint depth, GLenum format, const GLvoid *pixels,
    971                     const struct gl_pixelstore_attrib *packing )
    972 {
    973    struct gl_image *image;
    974    GLint width_in_bytes;
    975    GLint components;
    976    GLubyte *buffer, *dst;
    977    GLint i, d;
    978 
    979    components = gl_components_in_format( format );
    980 
    981    width_in_bytes = width * components * sizeof(GLubyte);
    982    buffer = (GLubyte *) MALLOC( height * width_in_bytes * depth );
    983    if (!buffer) {
    984       return NULL;
    985    }
    986 
    987    /* Copy/unpack pixel data to buffer */
    988    dst = buffer;
    989    for (d=0; d<depth; d++ ) {
    990       for (i=0;i<height;i++) {
    991          GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( packing,
    992                        pixels, width, height, format, GL_UNSIGNED_BYTE,
    993                        d, i, 0 );
    994          if (!src) {
    995             FREE(buffer);
    996             return NULL;
    997          }
    998          MEMCPY( dst, src, width_in_bytes );
    999          dst += width_in_bytes;
    1000       }
    1001    }
    1002 
    1003    if (format == GL_BGR) {
    1004       /* swap order of every ubyte triplet from BGR to RGB */
    1005       for (i=0; i<width*height; i++) {
    1006          GLubyte b = buffer[i*3+0];
    1007          GLubyte r = buffer[i*3+2];
    1008          buffer[i*3+0] = r;
    1009          buffer[i*3+2] = b;
    1010       }
    1011    }
    1012    else if (format == GL_BGRA) {
    1013       /* swap order of every ubyte quadruplet from BGRA to RGBA */
    1014       for (i=0; i<width*height; i++) {
    1015          GLubyte b = buffer[i*4+0];
    1016          GLubyte r = buffer[i*4+2];
    1017          buffer[i*4+0] = r;
    1018          buffer[i*4+2] = b;
    1019       }
    1020    }
    1021    else if (format == GL_ABGR_EXT) {
    1022       /* swap order of every ubyte quadruplet from ABGR to RGBA */
    1023       for (i=0; i<width*height; i++) {
    1024          GLubyte a = buffer[i*4+0];
    1025          GLubyte b = buffer[i*4+1];
    1026          GLubyte g = buffer[i*4+2];
    1027          GLubyte r = buffer[i*4+3];
    1028          buffer[i*4+0] = r;
    1029          buffer[i*4+1] = g;
    1030          buffer[i*4+2] = b;
    1031          buffer[i*4+3] = a;
    1032       }
    1033    }
    1034 
    1035 
    1036    image = alloc_image();
    1037    if (image) {
    1038       image->Width = width;
    1039       image->Height = height;
    1040       image->Depth = depth;
    1041       image->Components = components;
    1042       if (format == GL_BGR)
    1043          image->Format = GL_RGB;
    1044       else if (format == GL_BGRA)
    1045          image->Format = GL_RGBA;
    1046       else if (format == GL_ABGR_EXT)
    1047          image->Format = GL_RGBA;
    1048       else
    1049          image->Format = format;
    1050       image->Type = GL_UNSIGNED_BYTE;
    1051       image->Data = buffer;
    1052       image->RefCount = 0;
    1053    }
    1054    else {
    1055       FREE( buffer );
    1056    }
    1057 
    1058    return image;
    1059 }
    1060 
    1061 
    1062 
    1063 /*
    1064  * Unpack a color image storing image as GLfloats
    1065  */
    1066 static struct gl_image *
    1067 unpack_float_image( GLcontext *ctx, GLint width, GLint height, GLint depth,
    1068                     GLenum format, GLenum type, const GLvoid *pixels,
    1069                     const struct gl_pixelstore_attrib *packing )
    1070 {
    1071    struct gl_image *image;
    1072    GLfloat *dst;
    1073    GLint elems_per_row;
    1074    GLint components;
    1075    GLint i, j, d;
    1076    GLboolean normalize;
    1077 
    1078    assert(type != GL_BITMAP);
    1079 
    1080    components = gl_components_in_format( format );
    1081    assert(components > 0);  /* should have been caught earlier */
    1082 
    1083    if (!gl_is_legal_format_and_type( format, type )) {
    1084       /* bad pixel type for format, make dummy image */
    1085       image = alloc_image();
    1086       if (image) {
    1087          image->Width = width;
    1088          image->Height = height;
    1089          image->Depth = depth;
    1090          image->Components = components;
    1091          image->Format = format;
    1092          image->Type = type;
    1093          image->Data = NULL;
    1094          image->RefCount = 0;
    1095       }
    1096       return image;
    1097    }
    1098 
    1099    elems_per_row = width * components;
    1100 
    1101    image = alloc_image();
    1102    if (image) {
    1103       image->Width = width;
    1104       image->Height = height;
    1105       image->Depth = depth;
    1106       image->Components = components;
    1107       if (format == GL_BGR)
    1108          image->Format = GL_RGB;
    1109       else if (format == GL_BGRA)
    1110          image->Format = GL_RGBA;
    1111       else if (format == GL_ABGR_EXT)
    1112          image->Format = GL_RGBA;
    1113       else
    1114          image->Format = format;
    1115       image->Type = GL_FLOAT;
    1116       image->Data = MALLOC( elems_per_row * height * depth * sizeof(GLfloat));
    1117       image->RefCount = 0;
    1118       if (!image->Data)
    1119          return image;
    1120    }
    1121    else {
    1122       return NULL;
    1123    }
    1124 
    1125    normalize = (format != GL_COLOR_INDEX) && (format != GL_STENCIL_INDEX);
    1126 
    1127    dst = (GLfloat *) image->Data;
    1128 
    1129    for (d=0; d<depth; d++) {
    1130       for (i=0;i<height;i++) {
    1131          GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
    1132                                                width, height,
    1133                                                format, type,
    1134                                                d, i, 0 );
    1135          if (!src) {
    1136             return image;
    1137          }
    1138 
    1139          switch (type) {
    1140             case GL_UNSIGNED_BYTE:
    1141                {
    1142                   GLubyte *ubsrc = (GLubyte *) src;
    1143                   if (normalize) {
    1144                      for (j=0;j<elems_per_row;j++) {
    1145                         *dst++ = UBYTE_TO_FLOAT(ubsrc[j]);
    1146                      }
    1147                   }
    1148                   else {
    1149                      for (j=0;j<elems_per_row;j++) {
    1150                         *dst++ = (GLfloat) ubsrc[j];
    1151                      }
    1152                   }
    1153                }
    1154                break;
    1155             case GL_BYTE:
    1156                if (normalize) {
    1157                   for (j=0;j<elems_per_row;j++) {
    1158                      *dst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
    1159                   }
    1160                }
    1161                else {
    1162                   for (j=0;j<elems_per_row;j++) {
    1163                      *dst++ = (GLfloat) ((GLbyte*)src)[j];
    1164                   }
    1165                }
    1166                break;
    1167             case GL_UNSIGNED_SHORT:
    1168                if (packing->SwapBytes) {
    1169                   for (j=0;j<elems_per_row;j++) {
    1170                      GLushort value = ((GLushort*)src)[j];
    1171                      value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
    1172                      if (normalize) {
    1173                         *dst++ = USHORT_TO_FLOAT(value);
    1174                      }
    1175                      else {
    1176                         *dst++ = (GLfloat) value;
    1177                      }
    1178                   }
    1179                }
    1180                else {
    1181                   if (normalize) {
    1182                      for (j=0;j<elems_per_row;j++) {
    1183                         *dst++ = USHORT_TO_FLOAT(((GLushort*)src)[j]);
    1184                      }
    1185                   }
    1186                   else {
    1187                      for (j=0;j<elems_per_row;j++) {
    1188                         *dst++ = (GLfloat) ((GLushort*)src)[j];
    1189                      }
    1190                   }
    1191                }
    1192                break;
    1193             case GL_SHORT:
    1194                if (packing->SwapBytes) {
    1195                   for (j=0;j<elems_per_row;j++) {
    1196                      GLshort value = ((GLshort*)src)[j];
    1197                      value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
    1198                      if (normalize) {
    1199                         *dst++ = SHORT_TO_FLOAT(value);
    1200                      }
    1201                      else {
    1202                         *dst++ = (GLfloat) value;
    1203                      }
    1204                   }
    1205                }
    1206                else {
    1207                   if (normalize) {
    1208                      for (j=0;j<elems_per_row;j++) {
    1209                         *dst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
    1210                      }
    1211                   }
    1212                   else {
    1213                      for (j=0;j<elems_per_row;j++) {
    1214                         *dst++ = (GLfloat) ((GLshort*)src)[j];
    1215                      }
    1216                   }
    1217                }
    1218                break;
    1219             case GL_UNSIGNED_INT:
    1220                if (packing->SwapBytes) {
    1221                   GLuint value;
    1222                   for (j=0;j<elems_per_row;j++) {
    1223                      value = ((GLuint*)src)[j];
    1224                      value = ((value & 0xff000000) >> 24)
    1225                            | ((value & 0x00ff0000) >> 8)
    1226                            | ((value & 0x0000ff00) << 8)
    1227                            | ((value & 0x000000ff) << 24);
    1228                      if (normalize) {
    1229                         *dst++ = UINT_TO_FLOAT(value);
    1230                      }
    1231                      else {
    1232                         *dst++ = (GLfloat) value;
    1233                      }
    1234                   }
    1235                }
    1236                else {
    1237                   if (normalize) {
    1238                      for (j=0;j<elems_per_row;j++) {
    1239                         *dst++ = UINT_TO_FLOAT(((GLuint*)src)[j]);
    1240                      }
    1241                   }
    1242                   else {
    1243                      for (j=0;j<elems_per_row;j++) {
    1244                         *dst++ = (GLfloat) ((GLuint*)src)[j];
    1245                      }
    1246                   }
    1247                }
    1248                break;
    1249             case GL_INT:
    1250                if (packing->SwapBytes) {
    1251                   GLint value;
    1252                   for (j=0;j<elems_per_row;j++) {
    1253                      value = ((GLint*)src)[j];
    1254                      value = ((value & 0xff000000) >> 24)
    1255                            | ((value & 0x00ff0000) >> 8)
    1256                            | ((value & 0x0000ff00) << 8)
    1257                            | ((value & 0x000000ff) << 24);
    1258                      if (normalize) {
    1259                         *dst++ = INT_TO_FLOAT(value);
    1260                      }
    1261                      else {
    1262                         *dst++ = (GLfloat) value;
    1263                      }
    1264                   }
    1265                }
    1266                else {
    1267                   if (normalize) {
    1268                      for (j=0;j<elems_per_row;j++) {
    1269                         *dst++ = INT_TO_FLOAT(((GLint*)src)[j]);
    1270                      }
    1271                   }
    1272                   else {
    1273                      for (j=0;j<elems_per_row;j++) {
    1274                         *dst++ = (GLfloat) ((GLint*)src)[j];
    1275                      }
    1276                   }
    1277                }
    1278                break;
    1279             case GL_FLOAT:
    1280                if (packing->SwapBytes) {
    1281                   GLint value;
    1282                   for (j=0;j<elems_per_row;j++) {
    1283                      value = ((GLuint*)src)[j];
    1284                      value = ((value & 0xff000000) >> 24)
    1285                            | ((value & 0x00ff0000) >> 8)
    1286                            | ((value & 0x0000ff00) << 8)
    1287                            | ((value & 0x000000ff) << 24);
    1288                      *dst++ = *((GLfloat*) &value);
    1289                   }
    1290                }
    1291                else {
    1292                   MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) );
    1293                   dst += elems_per_row;
    1294                }
    1295                break;
    1296             case GL_UNSIGNED_BYTE_3_3_2:
    1297                {
    1298                   GLubyte *ubsrc = (GLubyte *) src;
    1299                   for (j=0;j<width;j++) {
    1300                      GLubyte p = ubsrc[j];
    1301                      *dst++ = ((p >> 5)      ) * (1.0F / 7.0F); /* red */
    1302                      *dst++ = ((p >> 2) & 0x7) * (1.0F / 7.0F); /* green */
    1303                      *dst++ = ((p     ) & 0x3) * (1.0F / 3.0F); /* blue */
    1304                   }
    1305                }
    1306                break;
    1307             case GL_UNSIGNED_BYTE_2_3_3_REV:
    1308                {
    1309                   GLubyte *ubsrc = (GLubyte *) src;
    1310                   for (j=0;j<width;j++) {
    1311                      GLubyte p = ubsrc[j];
    1312                      *dst++ = ((p     ) & 0x7) * (1.0F / 7.0F); /* red */
    1313                      *dst++ = ((p >> 3) & 0x7) * (1.0F / 7.0F); /* green */
    1314                      *dst++ = ((p >> 6)      ) * (1.0F / 3.0F); /* blue */
    1315                   }
    1316                }
    1317                break;
    1318             case GL_UNSIGNED_SHORT_5_6_5:
    1319                {
    1320                   GLushort *ussrc = (GLushort *) src;
    1321                   for (j=0;j<width;j++) {
    1322                      GLushort p = ussrc[j];
    1323                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* red */
    1324                      *dst++ = ((p >>  5) & 0x3f) * (1.0F / 63.0F); /* green */
    1325                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* blue */
    1326                   }
    1327                }
    1328                break;
    1329             case GL_UNSIGNED_SHORT_5_6_5_REV:
    1330                {
    1331                   GLushort *ussrc = (GLushort *) src;
    1332                   for (j=0;j<width;j++) {
    1333                      GLushort p = ussrc[j];
    1334                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* red */
    1335                      *dst++ = ((p >>  5) & 0x3f) * (1.0F / 63.0F); /* green */
    1336                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* blue */
    1337                   }
    1338                }
    1339                break;
    1340             case GL_UNSIGNED_SHORT_4_4_4_4:
    1341                {
    1342                   GLushort *ussrc = (GLushort *) src;
    1343                   for (j=0;j<width;j++) {
    1344                      GLushort p = ussrc[j];
    1345                      *dst++ = ((p >> 12)      ) * (1.0F / 15.0F); /* red */
    1346                      *dst++ = ((p >>  8) & 0xf) * (1.0F / 15.0F); /* green */
    1347                      *dst++ = ((p >>  4) & 0xf) * (1.0F / 15.0F); /* blue */
    1348                      *dst++ = ((p      ) & 0xf) * (1.0F / 15.0F); /* alpha */
    1349                   }
    1350                }
    1351                break;
    1352             case GL_UNSIGNED_SHORT_4_4_4_4_REV:
    1353                {
    1354                   GLushort *ussrc = (GLushort *) src;
    1355                   for (j=0;j<width;j++) {
    1356                      GLushort p = ussrc[j];
    1357                      *dst++ = ((p      ) & 0xf) * (1.0F / 15.0F); /* red */
    1358                      *dst++ = ((p >>  4) & 0xf) * (1.0F / 15.0F); /* green */
    1359                      *dst++ = ((p >>  8) & 0xf) * (1.0F / 15.0F); /* blue */
    1360                      *dst++ = ((p >> 12)      ) * (1.0F / 15.0F); /* alpha */
    1361                   }
    1362                }
    1363                break;
    1364             case GL_UNSIGNED_SHORT_5_5_5_1:
    1365                {
    1366                   GLushort *ussrc = (GLushort *) src;
    1367                   for (j=0;j<width;j++) {
    1368                      GLushort p = ussrc[j];
    1369                      *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* red */
    1370                      *dst++ = ((p >>  6) & 0x1f) * (1.0F / 31.0F); /* green */
    1371                      *dst++ = ((p >>  1) & 0x1f) * (1.0F / 31.0F); /* blue */
    1372                      *dst++ = ((p      ) & 0x1)  * (1.0F /  1.0F); /* alpha */
    1373                   }
    1374                }
    1375                break;
    1376             case GL_UNSIGNED_SHORT_1_5_5_5_REV:
    1377                {
    1378                   GLushort *ussrc = (GLushort *) src;
    1379                   for (j=0;j<width;j++) {
    1380                      GLushort p = ussrc[j];
    1381                      *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* red */
    1382                      *dst++ = ((p >>  5) & 0x1f) * (1.0F / 31.0F); /* green */
    1383                      *dst++ = ((p >> 10) & 0x1f) * (1.0F / 31.0F); /* blue */
    1384                      *dst++ = ((p >> 15)       ) * (1.0F /  1.0F); /* alpha */
    1385                   }
    1386                }
    1387                break;
    1388             case GL_UNSIGNED_INT_8_8_8_8:
    1389                {
    1390                   GLuint *uisrc = (GLuint *) src;
    1391                   for (j=0;j<width;j++) {
    1392                      GLuint p = uisrc[j];
    1393                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
    1394                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
    1395                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
    1396                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
    1397                   }
    1398                }
    1399                break;
    1400             case GL_UNSIGNED_INT_8_8_8_8_REV:
    1401                {
    1402                   GLuint *uisrc = (GLuint *) src;
    1403                   for (j=0;j<width;j++) {
    1404                      GLuint p = uisrc[j];
    1405                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
    1406                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
    1407                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
    1408                      *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
    1409                   }
    1410                }
    1411                break;
    1412             case GL_UNSIGNED_INT_10_10_10_2:
    1413                {
    1414                   GLuint *uisrc = (GLuint *) src;
    1415                   for (j=0;j<width;j++) {
    1416                      GLuint p = uisrc[j];
    1417                      *dst++ = ((p >> 22)        ) * (1.0F / 1023.0F); /* r */
    1418                      *dst++ = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F); /* g */
    1419                      *dst++ = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F); /* b */
    1420                      *dst++ = ((p      ) & 0x3  ) * (1.0F /    3.0F); /* a */
    1421                   }
    1422                }
    1423                break;
    1424             case GL_UNSIGNED_INT_2_10_10_10_REV:
    1425                {
    1426                   GLuint *uisrc = (GLuint *) src;
    1427                   for (j=0;j<width;j++) {
    1428                      GLuint p = uisrc[j];
    1429                      *dst++ = ((p      ) & 0x3ff) * (1.0F / 1023.0F); /* r*/
    1430                      *dst++ = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F); /* g */
    1431                      *dst++ = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F); /* b */
    1432                      *dst++ = ((p >> 30)        ) * (1.0F /    3.0F); /* a */
    1433                   }
    1434                }
    1435                break;
    1436             default:
    1437                gl_problem(ctx, "unpack_float_image type" );
    1438                return image;
    1439          }
    1440       }
    1441    }
    1442 
    1443    if (format == GL_BGR) {
    1444       /* swap order of every float triplet from BGR to RGBA */
    1445       GLfloat *buffer = (GLfloat *) image->Data;
    1446       for (i=0; i<width*height*depth; i++) {
    1447          GLfloat b = buffer[i*3+0];
    1448          GLfloat r = buffer[i*3+2];
    1449          buffer[i*3+0] = r;
    1450          buffer[i*3+2] = b;
    1451       }
    1452    }
    1453    else if (format == GL_BGRA) {
    1454       /* swap order of every float quadruplet from BGRA to RGBA */
    1455       GLfloat *buffer = (GLfloat *) image->Data;
    1456       for (i=0; i<width*height*depth; i++) {
    1457          GLfloat b = buffer[i*4+0];
    1458          GLfloat r = buffer[i*4+2];
    1459          buffer[i*4+0] = r;
    1460          buffer[i*4+2] = b;
    1461       }
    1462    }
    1463    else if (format == GL_ABGR_EXT) {
    1464       /* swap order of every float quadruplet from ABGR to RGBA */
    1465       GLfloat *buffer = (GLfloat *) image->Data;
    1466       for (i=0; i<width*height*depth; i++) {
    1467          GLfloat a = buffer[i*4+0];
    1468          GLfloat b = buffer[i*4+1];
    1469          GLfloat g = buffer[i*4+2];
    1470          GLfloat r = buffer[i*4+3];
    1471          buffer[i*4+0] = r;
    1472          buffer[i*4+1] = g;
    1473          buffer[i*4+2] = b;
    1474          buffer[i*4+3] = a;
    1475       }
    1476    }
    1477 
    1478    return image;
    1479 }
    1480 
    1481 
    1482 
    1483 /*
    1484  * Unpack a bitmap image, using current glPixelStore parameters,
    1485  * making a new gl_image.
    1486  */
    1487 struct gl_image *gl_unpack_bitmap( GLcontext *ctx,
    1488                                    GLsizei width, GLsizei height,
    1489                                    const GLubyte *bitmap,
    1490                                    const struct gl_pixelstore_attrib *packing )
    1491 {
    1492    return gl_unpack_image( ctx, width, height,
    1493                            GL_COLOR_INDEX, GL_BITMAP, bitmap, packing );
    1494 }
    1495 
    1496 
    1497 
    1498 /*
    1499  * Unpack a 2-D image from user's buffer.  Return pointer to new
    1500  * gl_image struct.
    1501  *
    1502  * Input:  width, height - size in pixels
    1503  *         format - format of incoming pixel data
    1504  *         type - datatype of incoming pixel data
    1505  *         pixels - pointer to unpacked image in user buffer
    1506  */
    1507 struct gl_image *gl_unpack_image( GLcontext *ctx,
    1508                                   GLint width, GLint height,
    1509                                   GLenum format, GLenum type,
    1510                                   const GLvoid *pixels,
    1511                                   const struct gl_pixelstore_attrib *packing )
    1512 {
    1513    return gl_unpack_image3D( ctx, width, height, 1,
    1514                              format, type, pixels, packing );
    1515 }
    1516 
    1517 
    1518 
    1519 /*
    1520  * Unpack a 1, 2 or 3-D image from user-supplied address, returning a
    1521  * pointer to a new gl_image struct.
    1522  * This function is always called by a higher-level unpack function such
    1523  * as gl_unpack_texsubimage() or gl_unpack_bitmap().
    1524  *
    1525  * Input:  width, height, depth - size in pixels
    1526  *         format - format of incoming pixel data
    1527  *         type - datatype of incoming pixel data
    1528  *         pixels - pointer to unpacked image.
    1529  */
    1530 struct gl_image *gl_unpack_image3D( GLcontext *ctx,
    1531                                     GLint width, GLint height, GLint depth,
    1532                                     GLenum format, GLenum type,
    1533                                     const GLvoid *pixels,
    1534                                     const struct gl_pixelstore_attrib *packing)
    1535 {
    1536    if (width <= 0 || height <= 0 || depth <= 0) {
    1537       return alloc_error_image(width, height, depth, format, type);
    1538    }
    1539 
    1540    if (type==GL_BITMAP) {
    1541       if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
    1542          return alloc_error_image(width, height, depth, format, type);
    1543       }
    1544       else {
    1545          return unpack_bitmap( format, width, height, pixels, packing );
    1546       }
    1547    }
    1548    else if (format==GL_DEPTH_COMPONENT) {
    1549       /* TODO: pack as GLdepth values (GLushort or GLuint) */
    1550       return unpack_depth_image( ctx, type, width, height, pixels, packing );
    1551    }
    1552    else if (format==GL_STENCIL_INDEX) {
    1553       /* TODO: pack as GLstencil (GLubyte or GLushort) */
    1554       return unpack_stencil_image( ctx, type, width, height, pixels, packing );
    1555    }
    1556    else if (type==GL_UNSIGNED_BYTE) {
    1557       /* upack, convert to GLubytes */
    1558       return unpack_ubyte_image( width, height, depth, format, pixels, packing );
    1559    }
    1560    else {
    1561       /* upack, convert to floats */
    1562       return unpack_float_image( ctx, width, height, depth,
    1563                                  format, type, pixels, packing );
    1564    }
    1565 
    1566    /* never get here */
    1567    /*return NULL;*/
    1568 }
    1569 
    1570 
    1571 /*
    1572  * Apply pixel-transfer operations (scale, bias, mapping) to a single row
    1573  * of a gl_image.  Put resulting color components into result array.
    1574  */
    1575 void gl_scale_bias_map_image_data( const GLcontext *ctx,
    1576                                    const struct gl_image *image,
    1577                                    GLint row, GLubyte result[] )
    1578 {
    1579    GLint start, i;
    1580 
    1581    assert(ctx);
    1582    assert(image);
    1583    assert(result);
    1584    assert(row >= 0);
    1585 
    1586    start = row * image->Width * image->Components;
    1587 
    1588    for (i=0; i < image->Width; i++) {
    1589       GLint pos = start+i;
    1590       GLfloat red, green, blue, alpha;
    1591       if (image->Type == GL_UNSIGNED_BYTE) {
    1592          const GLubyte *data = (GLubyte *) image->Data;
    1593          switch (image->Format) {
    1594             case GL_RED:
    1595                red   = data[pos] * (1.0F/255.0F);
    1596                green = 0;
    1597                blue  = 0;
    1598                alpha = 0;
    1599                break;
    1600             case GL_RGB:
    1601                red   = data[pos*3+0] * (1.0F/255.0F);
    1602                green = data[pos*3+1] * (1.0F/255.0F);
    1603                blue  = data[pos*3+2] * (1.0F/255.0F);
    1604                alpha = 0;
    1605                break;
    1606             default:
    1607                gl_problem(ctx, "bad image format in gl_scale...image_data");
    1608                return;
    1609          }
    1610       }
    1611       else if (image->Type == GL_FLOAT) {
    1612          const GLubyte *data = (GLubyte *) image->Data;
    1613          switch (image->Format) {
    1614             case GL_RED:
    1615                red   = data[pos];
    1616                green = 0;
    1617                blue  = 0;
    1618                alpha = 0;
    1619                break;
    1620             case GL_RGB:
    1621                red   = data[pos*3+0];
    1622                green = data[pos*3+1];
    1623                blue  = data[pos*3+2];
    1624                alpha = 0;
    1625                break;
    1626             default:
    1627                gl_problem(ctx, "bad image format in gl_scale...image_data");
    1628                return;
    1629          }
    1630       }
    1631       else {
    1632          gl_problem(ctx, "Bad image type in gl_scale_...image_data");
    1633          return;
    1634       }
    1635 
    1636       assert(red   >= 0.0 && red   <= 1.0);
    1637       assert(green >= 0.0 && green <= 1.0);
    1638       assert(blue  >= 0.0 && blue  <= 1.0);
    1639       assert(alpha >= 0.0 && alpha <= 1.0);
    1640 
    1641       /*
    1642       if (scale or bias) {
    1643 
    1644 
    1645       }
    1646       if (mapping) {
    1647 
    1648       }
    1649       */
    1650 
    1651       result[i*4+0] = (GLubyte) (red   * 255.0);
    1652       result[i*4+1] = (GLubyte) (green * 255.0);
    1653       result[i*4+2] = (GLubyte) (blue  * 255.0);
    1654       result[i*4+3] = (GLubyte) (alpha * 255.0);
    1655    }
     577      ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
     578      ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
     579      ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
     580      ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
     581   }
     582
     583   _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
    1656584}
    1657585
     
    1673601 *         applyTransferOps - apply scale/bias/lookup-table ops?
    1674602 */
    1675 void gl_pack_rgba_span( const GLcontext *ctx,
    1676                         GLuint n, CONST GLubyte rgba[][4],
    1677                         GLenum format, GLenum type, GLvoid *destination,
    1678                         const struct gl_pixelstore_attrib *packing,
    1679                         GLboolean applyTransferOps )
     603void
     604_mesa_pack_rgba_span( const GLcontext *ctx,
     605                      GLuint n, CONST GLubyte rgba[][4],
     606                      GLenum format, GLenum type, GLvoid *destination,
     607                      const struct gl_pixelstore_attrib *packing,
     608                      GLboolean applyTransferOps )
    1680609{
     610   applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA || ctx->Pixel.MapColorFlag);
     611
    1681612   /* Test for optimized case first */
    1682    if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
    1683        format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
     613   if (!applyTransferOps && format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
    1684614      /* common simple case */
    1685615      MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) );
    1686616   }
    1687    else if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
    1688        format == GL_RGB && type == GL_UNSIGNED_BYTE) {
     617   else if (!applyTransferOps && format == GL_RGB && type == GL_UNSIGNED_BYTE) {
    1689618      /* common simple case */
    1690619      GLint i;
     
    1698627   }
    1699628   else {
     629      /* general solution */
    1700630      GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH];
    1701631      GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH];
     
    1704634      const GLfloat bscale = 1.0F / 255.0F;
    1705635      const GLfloat ascale = 1.0F / 255.0F;
    1706       const GLint comps = gl_components_in_format(format);
     636      const GLint comps = _mesa_components_in_format(format);
    1707637      GLuint i;
    1708638
    1709       assert(n <= MAX_WIDTH);
     639      ASSERT(n <= MAX_WIDTH);
    1710640
    1711641      /* convert color components to floating point */
     
    1809739                     break;
    1810740                  default:
    1811                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
    1812                }
    1813             }
    1814             break;
    1815         case GL_BYTE:
     741                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
     742               }
     743            }
     744            break;
     745        case GL_BYTE:
    1816746            {
    1817747               GLbyte *dst = (GLbyte *) destination;
     
    1881811                     break;
    1882812                  default:
    1883                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
    1884                }
    1885             }
    1886             break;
    1887         case GL_UNSIGNED_SHORT:
     813                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
     814               }
     815            }
     816            break;
     817        case GL_UNSIGNED_SHORT:
    1888818            {
    1889819               GLushort *dst = (GLushort *) destination;
     
    1954884                     break;
    1955885                  default:
    1956                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
     886                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
    1957887               }
    1958888               if (packing->SwapBytes) {
    1959                   gl_swap2( (GLushort *) dst, n * comps);
    1960                }
    1961             }
    1962             break;
    1963         case GL_SHORT:
     889                  _mesa_swap2( (GLushort *) dst, n * comps);
     890               }
     891            }
     892            break;
     893        case GL_SHORT:
    1964894            {
    1965895               GLshort *dst = (GLshort *) destination;
     
    2029959                     break;
    2030960                  default:
    2031                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
     961                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
    2032962               }
    2033963               if (packing->SwapBytes) {
    2034                   gl_swap2( (GLushort *) dst, n * comps );
    2035                }
    2036             }
    2037             break;
    2038         case GL_UNSIGNED_INT:
     964                  _mesa_swap2( (GLushort *) dst, n * comps );
     965               }
     966            }
     967            break;
     968        case GL_UNSIGNED_INT:
    2039969            {
    2040970               GLuint *dst = (GLuint *) destination;
     
    21051035                     break;
    21061036                  default:
    2107                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
     1037                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
    21081038               }
    21091039               if (packing->SwapBytes) {
    2110                   gl_swap4( (GLuint *) dst, n * comps );
    2111                }
    2112             }
    2113             break;
    2114         case GL_INT:
    2115             {
     1040                  _mesa_swap4( (GLuint *) dst, n * comps );
     1041               }
     1042            }
     1043            break;
     1044        case GL_INT:
     1045            {
    21161046               GLint *dst = (GLint *) destination;
    21171047               switch (format) {
     
    21811111                     break;
    21821112                  default:
    2183                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
    2184                }
    2185                if (packing->SwapBytes) {
    2186                   gl_swap4( (GLuint *) dst, n * comps );
    2187                }
    2188             }
    2189             break;
    2190         case GL_FLOAT:
    2191             {
     1113                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
     1114               }
     1115               if (packing->SwapBytes) {
     1116                  _mesa_swap4( (GLuint *) dst, n * comps );
     1117               }
     1118            }
     1119            break;
     1120        case GL_FLOAT:
     1121            {
    21921122               GLfloat *dst = (GLfloat *) destination;
    21931123               switch (format) {
     
    22571187                     break;
    22581188                  default:
    2259                      gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
    2260                }
    2261                if (packing->SwapBytes) {
    2262                   gl_swap4( (GLuint *) dst, n * comps );
    2263                }
    2264             }
    2265             break;
     1189                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
     1190               }
     1191               if (packing->SwapBytes) {
     1192                  _mesa_swap4( (GLuint *) dst, n * comps );
     1193               }
     1194            }
     1195            break;
    22661196         case GL_UNSIGNED_BYTE_3_3_2:
    22671197            if (format == GL_RGB) {
     
    24651395            break;
    24661396         default:
    2467             gl_problem( ctx, "bad type in gl_pack_rgba_span" );
     1397            gl_problem( ctx, "bad type in _mesa_pack_rgba_span" );
    24681398      }
    24691399   }
     
    24711401
    24721402
    2473 
    2474 /*
    2475  * New (3.3) functions
    2476  */
    2477 
    2478 #define SWAP2BYTE(VALUE)                        \
    2479    {                                            \
    2480       GLubyte *bytes = (GLubyte *) &(VALUE);    \
    2481       GLubyte tmp = bytes[0];                   \
    2482       bytes[0] = bytes[1];                      \
    2483       bytes[1] = tmp;                           \
    2484    }
    2485 
    2486 #define SWAP4BYTE(VALUE)                        \
    2487    {                                            \
    2488       GLubyte *bytes = (GLubyte *) &(VALUE);    \
    2489       GLubyte tmp = bytes[0];                   \
    2490       bytes[0] = bytes[3];                      \
    2491       bytes[3] = tmp;                           \
    2492       tmp = bytes[1];                           \
    2493       bytes[1] = bytes[2];                      \
    2494       bytes[2] = tmp;                           \
     1403#define SWAP2BYTE(VALUE)                        \
     1404   {                                            \
     1405      GLubyte *bytes = (GLubyte *) &(VALUE);    \
     1406      GLubyte tmp = bytes[0];                   \
     1407      bytes[0] = bytes[1];                      \
     1408      bytes[1] = tmp;                           \
     1409   }
     1410
     1411#define SWAP4BYTE(VALUE)                        \
     1412   {                                            \
     1413      GLubyte *bytes = (GLubyte *) &(VALUE);    \
     1414      GLubyte tmp = bytes[0];                   \
     1415      bytes[0] = bytes[3];                      \
     1416      bytes[3] = tmp;                           \
     1417      tmp = bytes[1];                           \
     1418      bytes[1] = bytes[2];                      \
     1419      bytes[2] = tmp;                           \
    24951420   }
    24961421
     
    25011426                     const struct gl_pixelstore_attrib *unpack )
    25021427{
    2503    assert(srcFormat == GL_COLOR_INDEX);
     1428   ASSERT(srcFormat == GL_COLOR_INDEX);
    25041429
    25051430   ASSERT(srcType == GL_BITMAP ||
     
    26381563                  GLfloat value = s[i];
    26391564                  SWAP4BYTE(value);
    2640                   indexes[i] = value;
     1565                  indexes[i] = (GLuint) value;
    26411566               }
    26421567            }
    26431568            else {
    26441569               for (i = 0; i < n; i++)
    2645                   indexes[i] = s[i];
     1570                  indexes[i] = (GLuint) s[i];
    26461571            }
    26471572         }
     
    26801605   GLint rComp, bComp, gComp, aComp;
    26811606
    2682    if (0)
    2683    {
    2684       int i;
    2685       for (i = 0; i<n;i++) {
    2686          rgba[i][0] = rgba[i][1] = rgba[i][2] = rgba[i][3] = 0;
    2687       }
    2688       return;
    2689    }
    2690 
    2691 
    26921607   ASSERT(srcFormat == GL_RED ||
    26931608          srcFormat == GL_GREEN ||
     
    27231638          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
    27241639
     1640   rComp = gComp = bComp = aComp = -1;
     1641
    27251642   switch (srcFormat) {
    27261643      case GL_RED:
     
    28111728   }
    28121729
    2813    assert(redIndex >= -1 && redIndex <= 4);
    2814    assert(greenIndex >= -1 && greenIndex <= 4);
    2815    assert(blueIndex >= -1 && blueIndex <= 4);
    2816    assert(alphaIndex >= -1 && alphaIndex <= 4);
    2817 
    2818 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)              \
    2819    if ((INDEX) < 0) {                                                   \
    2820       GLuint i;                                                         \
    2821       for (i = 0; i < n; i++) {                                         \
    2822          rgba[i][CHANNEL] = DEFAULT;                                    \
    2823       }                                                                 \
    2824    }                                                                    \
    2825    else if (swapBytes) {                                                \
    2826       const TYPE *s = (const TYPE *) src;                               \
    2827       GLuint i;                                                         \
    2828       for (i = 0; i < n; i++) {                                         \
    2829          TYPE value = s[INDEX];                                         \
    2830          if (sizeof(TYPE) == 2) {                                       \
    2831             SWAP2BYTE(value);                                           \
    2832          }                                                              \
    2833          else if (sizeof(TYPE) == 4) {                                  \
    2834             SWAP4BYTE(value);                                           \
    2835          }                                                              \
    2836          rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);                \
    2837          s += stride;                                                   \
    2838       }                                                                 \
    2839    }                                                                    \
    2840    else {                                                               \
    2841       const TYPE *s = (const TYPE *) src;                               \
    2842       GLuint i;                                                         \
    2843       for (i = 0; i < n; i++) {                                         \
    2844          rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);             \
    2845          s += stride;                                                   \
    2846       }                                                                 \
     1730
     1731#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)              \
     1732   if ((INDEX) < 0) {                                                   \
     1733      GLuint i;                                                         \
     1734      for (i = 0; i < n; i++) {                                         \
     1735         rgba[i][CHANNEL] = DEFAULT;                                    \
     1736      }                                                                 \
     1737   }                                                                    \
     1738   else if (swapBytes) {                                                \
     1739      const TYPE *s = (const TYPE *) src;                               \
     1740      GLuint i;                                                         \
     1741      for (i = 0; i < n; i++) {                                         \
     1742         TYPE value = s[INDEX];                                         \
     1743         if (sizeof(TYPE) == 2) {                                       \
     1744            SWAP2BYTE(value);                                           \
     1745         }                                                              \
     1746         else if (sizeof(TYPE) == 4) {                                  \
     1747            SWAP4BYTE(value);                                           \
     1748         }                                                              \
     1749         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);                \
     1750         s += stride;                                                   \
     1751      }                                                                 \
     1752   }                                                                    \
     1753   else {                                                               \
     1754      const TYPE *s = (const TYPE *) src;                               \
     1755      GLuint i;                                                         \
     1756      for (i = 0; i < n; i++) {                                         \
     1757         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);             \
     1758         s += stride;                                                   \
     1759      }                                                                 \
    28471760   }
    28481761
     
    31212034               GLuint p = uisrc[i];
    31222035               SWAP4BYTE(p);
    3123                rgba[i][rComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
    3124                rgba[i][gComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
    3125                rgba[i][bComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
    3126                rgba[i][aComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
     2036               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
     2037               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
     2038               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
     2039               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
    31272040            }
    31282041         }
     
    31322045            for (i = 0; i < n; i ++) {
    31332046               GLuint p = uisrc[i];
    3134                rgba[i][rComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
    3135                rgba[i][gComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
    3136                rgba[i][bComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
    3137                rgba[i][aComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
     2047               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
     2048               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
     2049               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
     2050               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
    31382051            }
    31392052         }
     
    32452158
    32462159   applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
     2160                        ctx->Pixel.MapColorFlag ||
    32472161                        ctx->Pixel.MapColorFlag);
    32482162
     
    32892203      }
    32902204      else if (dstFormat == srcFormat) {
    3291          GLint comps = gl_components_in_format(srcFormat);
    3292          assert(comps > 0);
     2205         GLint comps = _mesa_components_in_format(srcFormat);
     2206         ASSERT(comps > 0);
    32932207         MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
    32942208         return;
     
    33042218      GLint dstLuminanceIndex, dstIntensityIndex;
    33052219
    3306       dstComponents = gl_components_in_format( dstFormat );
     2220      dstComponents = _mesa_components_in_format( dstFormat );
    33072221      /* source & dest image formats should have been error checked by now */
    3308       assert(dstComponents > 0);
     2222      ASSERT(dstComponents > 0);
    33092223
    33102224      /*
    33112225       * Extract image data and convert to RGBA floats
    33122226       */
    3313       assert(n <= MAX_WIDTH);
     2227      ASSERT(n <= MAX_WIDTH);
    33142228      if (srcFormat == GL_COLOR_INDEX) {
    33152229         GLuint indexes[MAX_WIDTH];
     
    34182332            dstLuminanceIndex = dstIntensityIndex = -1;
    34192333            break;
    3420          case GL_COLOR_INDEX:
    3421             assert(0);
    3422             break;
    34232334         default:
    34242335            gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
     2336            return;
    34252337      }
    34262338
    34272339
    34282340      /* Now return the GLubyte data in the requested dstFormat */
     2341
    34292342      if (dstRedIndex >= 0) {
    34302343         GLubyte *dst = dest;
     
    34662379         GLubyte *dst = dest;
    34672380         GLuint i;
    3468          assert(dstIntensityIndex == 0);
    3469          assert(dstComponents == 1);
     2381         ASSERT(dstIntensityIndex == 0);
     2382         ASSERT(dstComponents == 1);
    34702383         for (i = 0; i < n; i++) {
    34712384            /* Intensity comes from red channel */
     
    34772390         GLubyte *dst = dest;
    34782391         GLuint i;
    3479          assert(dstLuminanceIndex == 0);
     2392         ASSERT(dstLuminanceIndex == 0);
    34802393         for (i = 0; i < n; i++) {
    34812394            /* Luminance comes from red channel */
     
    35422455       */
    35432456      GLuint indexes[MAX_WIDTH];
    3544       assert(n <= MAX_WIDTH);
     2457      ASSERT(n <= MAX_WIDTH);
    35452458
    35462459      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
     
    35872500   }
    35882501}
     2502
     2503
     2504/*
     2505 * Unpack a row of stencil data from a client buffer according to
     2506 * the pixel unpacking parameters.  Apply pixel transfer ops if enabled
     2507 * and applyTransferOps is true.
     2508 * This is (or will be) used by glDrawPixels
     2509 *
     2510 * Args:  ctx - the context
     2511 *        n - number of pixels
     2512 *        dstType - destination datatype
     2513 *        dest - destination array
     2514 *        srcType - source pixel type
     2515 *        source - source data pointer
     2516 *        unpacking - pixel unpacking parameters
     2517 *        applyTransferOps - apply offset/bias/lookup ops?
     2518 */
     2519void
     2520_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
     2521                           GLenum dstType, GLvoid *dest,
     2522                           GLenum srcType, const GLvoid *source,
     2523                           const struct gl_pixelstore_attrib *unpacking,
     2524                           GLboolean applyTransferOps )
     2525{
     2526   ASSERT(srcType == GL_BITMAP ||
     2527          srcType == GL_UNSIGNED_BYTE ||
     2528          srcType == GL_BYTE ||
     2529          srcType == GL_UNSIGNED_SHORT ||
     2530          srcType == GL_SHORT ||
     2531          srcType == GL_UNSIGNED_INT ||
     2532          srcType == GL_INT ||
     2533          srcType == GL_FLOAT);
     2534
     2535   ASSERT(dstType == GL_UNSIGNED_BYTE ||
     2536          dstType == GL_UNSIGNED_SHORT ||
     2537          dstType == GL_UNSIGNED_INT);
     2538
     2539   applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
     2540
     2541   /*
     2542    * Try simple cases first
     2543    */
     2544   if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
     2545       && dstType == GL_UNSIGNED_BYTE) {
     2546      MEMCPY(dest, source, n * sizeof(GLubyte));
     2547   }
     2548   else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
     2549            && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
     2550      MEMCPY(dest, source, n * sizeof(GLuint));
     2551   }
     2552   else {
     2553      /*
     2554       * general solution
     2555       */
     2556      GLuint indexes[MAX_WIDTH];
     2557      ASSERT(n <= MAX_WIDTH);
     2558
     2559      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
     2560                           unpacking);
     2561
     2562      if (applyTransferOps) {
     2563         if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
     2564            /* shift and offset indexes */
     2565            gl_shift_and_offset_ci(ctx, n, indexes);
     2566         }
     2567
     2568         if (ctx->Pixel.MapStencilFlag) {
     2569            /* Apply stencil lookup table */
     2570            GLuint mask = ctx->Pixel.MapStoSsize - 1;
     2571            GLuint i;
     2572            for (i=0;i<n;i++) {
     2573               indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
     2574            }
     2575         }
     2576      }
     2577
     2578      /* convert to dest type */
     2579      switch (dstType) {
     2580         case GL_UNSIGNED_BYTE:
     2581            {
     2582               GLubyte *dst = (GLubyte *) dest;
     2583               GLuint i;
     2584               for (i = 0; i < n; i++) {
     2585                  dst[i] = (GLubyte) (indexes[i] & 0xff);
     2586               }
     2587            }
     2588            break;
     2589         case GL_UNSIGNED_SHORT:
     2590            {
     2591               GLuint *dst = (GLuint *) dest;
     2592               GLuint i;
     2593               for (i = 0; i < n; i++) {
     2594                  dst[i] = (GLushort) (indexes[i] & 0xffff);
     2595               }
     2596            }
     2597            break;
     2598         case GL_UNSIGNED_INT:
     2599            MEMCPY(dest, indexes, n * sizeof(GLuint));
     2600            break;
     2601         default:
     2602            gl_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
     2603      }
     2604   }
     2605}
     2606
     2607
     2608
     2609void
     2610_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
     2611                         GLenum srcType, const GLvoid *source,
     2612                         const struct gl_pixelstore_attrib *unpacking,
     2613                         GLboolean applyTransferOps )
     2614{
     2615   GLfloat *depth = (GLfloat *)MALLOC(n * sizeof(GLfloat));
     2616   if (!depth)
     2617      return;
     2618
     2619   switch (srcType) {
     2620      case GL_BYTE:
     2621         {
     2622            GLuint i;
     2623            const GLubyte *src = (const GLubyte *) source;
     2624            for (i = 0; i < n; i++) {
     2625               depth[i] = BYTE_TO_FLOAT(src[i]);
     2626            }
     2627         }
     2628         break;
     2629      case GL_UNSIGNED_BYTE:
     2630         {
     2631            GLuint i;
     2632            const GLubyte *src = (const GLubyte *) source;
     2633            for (i = 0; i < n; i++) {
     2634               depth[i] = UBYTE_TO_FLOAT(src[i]);
     2635            }
     2636         }
     2637         break;
     2638      case GL_SHORT:
     2639         {
     2640            GLuint i;
     2641            const GLshort *src = (const GLshort *) source;
     2642            for (i = 0; i < n; i++) {
     2643               depth[i] = SHORT_TO_FLOAT(src[i]);
     2644            }
     2645         }
     2646         break;
     2647      case GL_UNSIGNED_SHORT:
     2648         {
     2649            GLuint i;
     2650            const GLushort *src = (const GLushort *) source;
     2651            for (i = 0; i < n; i++) {
     2652               depth[i] = USHORT_TO_FLOAT(src[i]);
     2653            }
     2654         }
     2655         break;
     2656      case GL_INT:
     2657         {
     2658            GLuint i;
     2659            const GLint *src = (const GLint *) source;
     2660            for (i = 0; i < n; i++) {
     2661               depth[i] = INT_TO_FLOAT(src[i]);
     2662            }
     2663         }
     2664         break;
     2665      case GL_UNSIGNED_INT:
     2666         {
     2667            GLuint i;
     2668            const GLuint *src = (const GLuint *) source;
     2669            for (i = 0; i < n; i++) {
     2670               depth[i] = UINT_TO_FLOAT(src[i]);
     2671            }
     2672         }
     2673         break;
     2674      case GL_FLOAT:
     2675         MEMCPY(depth, source, n * sizeof(GLfloat));
     2676         break;
     2677      default:
     2678         gl_problem(NULL, "bad type in _mesa_unpack_depth_span()");
     2679         return;
     2680   }
     2681
     2682
     2683   /* apply depth scale and bias */
     2684   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
     2685      GLuint i;
     2686      for (i = 0; i < n; i++) {
     2687         depth[i] = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
     2688      }
     2689   }
     2690
     2691   /* clamp depth values to [0,1] and convert from floats to integers */
     2692   {
     2693      const GLfloat zs = ctx->Visual->DepthMaxF;
     2694      GLuint i;
     2695      for (i = 0; i < n; i++) {
     2696         dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
     2697      }
     2698   }
     2699
     2700   FREE(depth);
     2701}
     2702
    35892703
    35902704
     
    36142728   }
    36152729   else {
    3616       const GLint bytesPerPixel = gl_bytes_per_pixel(format, type);
    3617       const GLint components = gl_components_in_format(format);
     2730      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
     2731      const GLint components = _mesa_components_in_format(format);
    36182732      GLint bytesPerComp;
    36192733      if (bytesPerPixel <= 0 || components <= 0)
     
    36252739      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
    36262740      compsPerRow = components * width;
    3627       assert(compsPerRow >= width);
     2741      ASSERT(compsPerRow >= width);
    36282742   }
    36292743
     
    36382752      for (img = 0; img < depth; img++) {
    36392753         for (row = 0; row < height; row++) {
    3640             const GLvoid *src = gl_pixel_addr_in_image(unpack, pixels,
     2754            const GLvoid *src = _mesa_image_address(unpack, pixels,
    36412755                               width, height, format, type, img, row, 0);
    36422756            MEMCPY(dst, src, bytesPerRow);
    36432757            /* byte flipping/swapping */
    36442758            if (flipBytes) {
    3645                gl_flip_bytes((GLubyte *) dst, bytesPerRow);
     2759               flip_bytes((GLubyte *) dst, bytesPerRow);
    36462760            }
    36472761            else if (swap2) {
    3648                gl_swap2((GLushort*) dst, compsPerRow);
     2762               _mesa_swap2((GLushort*) dst, compsPerRow);
    36492763            }
    36502764            else if (swap4) {
    3651                gl_swap4((GLuint*) dst, compsPerRow);
     2765               _mesa_swap4((GLuint*) dst, compsPerRow);
    36522766            }
    36532767            dst += bytesPerRow;
     
    36572771   }
    36582772}
     2773
     2774
     2775/*
     2776 * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
     2777 * order with row alignment = 1 byte.
     2778 */
     2779GLvoid *
     2780_mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
     2781                     const struct gl_pixelstore_attrib *packing )
     2782{
     2783   GLint bytes, row, width_in_bytes;
     2784   GLubyte *buffer, *dst;
     2785
     2786   if (!pixels)
     2787      return NULL;
     2788
     2789   /* Alloc dest storage */
     2790   bytes = ((width + 7) / 8 * height);
     2791   buffer = (GLubyte *) MALLOC( bytes );
     2792   if (!buffer)
     2793      return NULL;
     2794
     2795
     2796   width_in_bytes = CEILING( width, 8 );
     2797   dst = buffer;
     2798   for (row = 0; row < height; row++) {
     2799      GLubyte *src = (GLubyte *)_mesa_image_address( packing, pixels, width, height,
     2800                                          GL_COLOR_INDEX, GL_BITMAP,
     2801                                          0, row, 0 );
     2802      if (!src) {
     2803         FREE(buffer);
     2804         return NULL;
     2805      }
     2806
     2807      if (packing->SkipPixels == 0) {
     2808         MEMCPY( dst, src, width_in_bytes );
     2809         if (packing->LsbFirst) {
     2810            flip_bytes( dst, width_in_bytes );
     2811         }
     2812      }
     2813      else {
     2814         /* handling SkipPixels is a bit tricky (no pun intended!) */
     2815         GLint i;
     2816         if (packing->LsbFirst) {
     2817            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
     2818            GLubyte dstMask = 128;
     2819            GLubyte *s = src;
     2820            GLubyte *d = dst;
     2821            *d = 0;
     2822            for (i = 0; i < width; i++) {
     2823               if (*s & srcMask) {
     2824                  *d |= dstMask;
     2825               }
     2826               if (srcMask == 128) {
     2827                  srcMask = 1;
     2828                  s++;
     2829               }
     2830               else {
     2831                  srcMask = srcMask << 1;
     2832               }
     2833               if (dstMask == 1) {
     2834                  dstMask = 128;
     2835                  d++;
     2836                  *d = 0;
     2837               }
     2838               else {
     2839                  dstMask = dstMask >> 1;
     2840               }
     2841            }
     2842         }
     2843         else {
     2844            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
     2845            GLubyte dstMask = 128;
     2846            GLubyte *s = src;
     2847            GLubyte *d = dst;
     2848            *d = 0;
     2849            for (i = 0; i < width; i++) {
     2850               if (*s & srcMask) {
     2851                  *d |= dstMask;
     2852               }
     2853               if (srcMask == 1) {
     2854                  srcMask = 128;
     2855                  s++;
     2856               }
     2857               else {
     2858                  srcMask = srcMask >> 1;
     2859               }
     2860               if (dstMask == 1) {
     2861                  dstMask = 128;
     2862                  d++;
     2863                  *d = 0;
     2864               }
     2865               else {
     2866                  dstMask = dstMask >> 1;
     2867               }
     2868            }
     2869         }
     2870      }
     2871      dst += width_in_bytes;
     2872   }
     2873
     2874   return buffer;
     2875}
     2876
     2877
     2878/*
     2879 * Pack bitmap data.
     2880 */
     2881void
     2882_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
     2883                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
     2884{
     2885   GLint row, width_in_bytes;
     2886   const GLubyte *src;
     2887
     2888   if (!source)
     2889      return;
     2890
     2891   width_in_bytes = CEILING( width, 8 );
     2892   src = source;
     2893   for (row = 0; row < height; row++) {
     2894      GLubyte *dst = (GLubyte *)_mesa_image_address( packing, dest, width, height,
     2895                                          GL_COLOR_INDEX, GL_BITMAP,
     2896                                          0, row, 0 );
     2897      if (!dst)
     2898         return;
     2899
     2900      if (packing->SkipPixels == 0) {
     2901         MEMCPY( dst, src, width_in_bytes );
     2902         if (packing->LsbFirst) {
     2903            flip_bytes( dst, width_in_bytes );
     2904         }
     2905      }
     2906      else {
     2907         /* handling SkipPixels is a bit tricky (no pun intended!) */
     2908         GLint i;
     2909         if (packing->LsbFirst) {
     2910            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
     2911            GLubyte dstMask = 128;
     2912            const GLubyte *s = src;
     2913            GLubyte *d = dst;
     2914            *d = 0;
     2915            for (i = 0; i < width; i++) {
     2916               if (*s & srcMask) {
     2917                  *d |= dstMask;
     2918               }
     2919               if (srcMask == 128) {
     2920                  srcMask = 1;
     2921                  s++;
     2922               }
     2923               else {
     2924                  srcMask = srcMask << 1;
     2925               }
     2926               if (dstMask == 1) {
     2927                  dstMask = 128;
     2928                  d++;
     2929                  *d = 0;
     2930               }
     2931               else {
     2932                  dstMask = dstMask >> 1;
     2933               }
     2934            }
     2935         }
     2936         else {
     2937            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
     2938            GLubyte dstMask = 128;
     2939            const GLubyte *s = src;
     2940            GLubyte *d = dst;
     2941            *d = 0;
     2942            for (i = 0; i < width; i++) {
     2943               if (*s & srcMask) {
     2944                  *d |= dstMask;
     2945               }
     2946               if (srcMask == 1) {
     2947                  srcMask = 128;
     2948                  s++;
     2949               }
     2950               else {
     2951                  srcMask = srcMask >> 1;
     2952               }
     2953               if (dstMask == 1) {
     2954                  dstMask = 128;
     2955                  d++;
     2956                  *d = 0;
     2957               }
     2958               else {
     2959                  dstMask = dstMask >> 1;
     2960               }
     2961            }
     2962         }
     2963      }
     2964      src += width_in_bytes;
     2965   }
     2966}
Note: See TracChangeset for help on using the changeset viewer.