image.c revision 179870a5b806a3ee84cb56fa20c3a003f9fc5b97
1/* $Id: image.c,v 1.26 2000/04/12 18:54:48 brianp Exp $ */
2
3/*
4 * Mesa 3-D graphics library
5 * Version:  3.3
6 *
7 * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28#ifdef PC_HEADER
29#include "all.h"
30#else
31#include "glheader.h"
32#include "context.h"
33#include "image.h"
34#include "macros.h"
35#include "mem.h"
36#include "mmath.h"
37#include "pixel.h"
38#include "types.h"
39#endif
40
41
42
43/*
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/*
64 * Flip the 8 bits in each byte of the given array.
65 */
66static void
67flip_bytes( GLubyte *p, GLuint n )
68{
69   register GLuint i, a, b;
70
71   for (i=0;i<n;i++) {
72      b = (GLuint) p[i];
73      a = ((b & 0x01) << 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);
81      p[i] = (GLubyte) a;
82   }
83}
84
85
86/*
87 * Flip the order of the 2 bytes in each word in the given array.
88 */
89void
90_mesa_swap2( GLushort *p, GLuint n )
91{
92   register GLuint i;
93
94   for (i=0;i<n;i++) {
95      p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
96   }
97}
98
99
100
101/*
102 * Flip the order of the 4 bytes in each word in the given array.
103 */
104void
105_mesa_swap4( GLuint *p, GLuint n )
106{
107   register GLuint i, a, b;
108
109   for (i=0;i<n;i++) {
110      b = p[i];
111      a =  (b >> 24)
112	| ((b >> 8) & 0xff00)
113	| ((b << 8) & 0xff0000)
114	| ((b << 24) & 0xff000000);
115      p[i] = a;
116   }
117}
118
119
120
121
122/*
123 * Return the size, in bytes, of the given GL datatype.
124 * Return 0 if GL_BITMAP.
125 * Return -1 if invalid type enum.
126 */
127GLint _mesa_sizeof_type( GLenum type )
128{
129   switch (type) {
130      case GL_BITMAP:
131	 return 0;
132      case GL_UNSIGNED_BYTE:
133         return sizeof(GLubyte);
134      case GL_BYTE:
135	 return sizeof(GLbyte);
136      case GL_UNSIGNED_SHORT:
137	 return sizeof(GLushort);
138      case GL_SHORT:
139	 return sizeof(GLshort);
140      case GL_UNSIGNED_INT:
141	 return sizeof(GLuint);
142      case GL_INT:
143	 return sizeof(GLint);
144      case GL_FLOAT:
145	 return sizeof(GLfloat);
146      default:
147         return -1;
148   }
149}
150
151
152/*
153 * Same as _mesa_sizeof_packed_type() but we also accept the
154 * packed pixel format datatypes.
155 */
156GLint _mesa_sizeof_packed_type( GLenum type )
157{
158   switch (type) {
159      case GL_BITMAP:
160	 return 0;
161      case GL_UNSIGNED_BYTE:
162         return sizeof(GLubyte);
163      case GL_BYTE:
164	 return sizeof(GLbyte);
165      case GL_UNSIGNED_SHORT:
166	 return sizeof(GLushort);
167      case GL_SHORT:
168	 return sizeof(GLshort);
169      case GL_UNSIGNED_INT:
170	 return sizeof(GLuint);
171      case GL_INT:
172	 return sizeof(GLint);
173      case GL_FLOAT:
174	 return sizeof(GLfloat);
175      case GL_UNSIGNED_BYTE_3_3_2:
176         return sizeof(GLubyte);
177      case GL_UNSIGNED_BYTE_2_3_3_REV:
178         return sizeof(GLubyte);
179      case GL_UNSIGNED_SHORT_5_6_5:
180         return sizeof(GLshort);
181      case GL_UNSIGNED_SHORT_5_6_5_REV:
182         return sizeof(GLshort);
183      case GL_UNSIGNED_SHORT_4_4_4_4:
184         return sizeof(GLshort);
185      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
186         return sizeof(GLshort);
187      case GL_UNSIGNED_SHORT_5_5_5_1:
188         return sizeof(GLshort);
189      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
190         return sizeof(GLshort);
191      case GL_UNSIGNED_INT_8_8_8_8:
192         return sizeof(GLuint);
193      case GL_UNSIGNED_INT_8_8_8_8_REV:
194         return sizeof(GLuint);
195      case GL_UNSIGNED_INT_10_10_10_2:
196         return sizeof(GLuint);
197      case GL_UNSIGNED_INT_2_10_10_10_REV:
198         return sizeof(GLuint);
199      default:
200         return -1;
201   }
202}
203
204
205
206/*
207 * Return the number of components in a GL enum pixel type.
208 * Return -1 if bad format.
209 */
210GLint _mesa_components_in_format( GLenum format )
211{
212   switch (format) {
213      case GL_COLOR_INDEX:
214      case GL_COLOR_INDEX1_EXT:
215      case GL_COLOR_INDEX2_EXT:
216      case GL_COLOR_INDEX4_EXT:
217      case GL_COLOR_INDEX8_EXT:
218      case GL_COLOR_INDEX12_EXT:
219      case GL_COLOR_INDEX16_EXT:
220      case GL_STENCIL_INDEX:
221      case GL_DEPTH_COMPONENT:
222      case GL_RED:
223      case GL_GREEN:
224      case GL_BLUE:
225      case GL_ALPHA:
226      case GL_LUMINANCE:
227      case GL_INTENSITY:
228         return 1;
229      case GL_LUMINANCE_ALPHA:
230	 return 2;
231      case GL_RGB:
232	 return 3;
233      case GL_RGBA:
234	 return 4;
235      case GL_BGR:
236	 return 3;
237      case GL_BGRA:
238	 return 4;
239      case GL_ABGR_EXT:
240         return 4;
241      default:
242         return -1;
243   }
244}
245
246
247/*
248 * Return bytes per pixel for given format and type
249 * Return -1 if bad format or type.
250 */
251GLint _mesa_bytes_per_pixel( GLenum format, GLenum type )
252{
253   GLint comps = _mesa_components_in_format( format );
254   if (comps < 0)
255      return -1;
256
257   switch (type) {
258      case GL_BITMAP:
259         return 0;  /* special case */
260      case GL_BYTE:
261      case GL_UNSIGNED_BYTE:
262         return comps * sizeof(GLubyte);
263      case GL_SHORT:
264      case GL_UNSIGNED_SHORT:
265         return comps * sizeof(GLshort);
266      case GL_INT:
267      case GL_UNSIGNED_INT:
268         return comps * sizeof(GLint);
269      case GL_FLOAT:
270         return comps * sizeof(GLfloat);
271      case GL_UNSIGNED_BYTE_3_3_2:
272      case GL_UNSIGNED_BYTE_2_3_3_REV:
273         if (format == GL_RGB || format == GL_BGR)
274            return sizeof(GLubyte);
275         else
276            return -1;  /* error */
277      case GL_UNSIGNED_SHORT_5_6_5:
278      case GL_UNSIGNED_SHORT_5_6_5_REV:
279         if (format == GL_RGB || format == GL_BGR)
280            return sizeof(GLshort);
281         else
282            return -1;  /* error */
283      case GL_UNSIGNED_SHORT_4_4_4_4:
284      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
285      case GL_UNSIGNED_SHORT_5_5_5_1:
286      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
287         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
288            return sizeof(GLushort);
289         else
290            return -1;
291      case GL_UNSIGNED_INT_8_8_8_8:
292      case GL_UNSIGNED_INT_8_8_8_8_REV:
293      case GL_UNSIGNED_INT_10_10_10_2:
294      case GL_UNSIGNED_INT_2_10_10_10_REV:
295         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
296            return sizeof(GLuint);
297         else
298            return -1;
299      default:
300         return -1;
301   }
302}
303
304
305/*
306 * Test if the given pixel format and type are legal.
307 * Return GL_TRUE for legal, GL_FALSE for illegal.
308 */
309GLboolean
310_mesa_is_legal_format_and_type( GLenum format, GLenum type )
311{
312   switch (format) {
313      case GL_COLOR_INDEX:
314      case GL_STENCIL_INDEX:
315         switch (type) {
316            case GL_BITMAP:
317            case GL_BYTE:
318            case GL_UNSIGNED_BYTE:
319            case GL_SHORT:
320            case GL_UNSIGNED_SHORT:
321            case GL_INT:
322            case GL_UNSIGNED_INT:
323            case GL_FLOAT:
324               return GL_TRUE;
325            default:
326               return GL_FALSE;
327         }
328      case GL_RED:
329      case GL_GREEN:
330      case GL_BLUE:
331      case GL_ALPHA:
332      case GL_LUMINANCE:
333      case GL_LUMINANCE_ALPHA:
334      case GL_DEPTH_COMPONENT:
335      case GL_BGR:
336         switch (type) {
337            case GL_BYTE:
338            case GL_UNSIGNED_BYTE:
339            case GL_SHORT:
340            case GL_UNSIGNED_SHORT:
341            case GL_INT:
342            case GL_UNSIGNED_INT:
343            case GL_FLOAT:
344               return GL_TRUE;
345            default:
346               return GL_FALSE;
347         }
348      case GL_RGB:
349         switch (type) {
350            case GL_BYTE:
351            case GL_UNSIGNED_BYTE:
352            case GL_SHORT:
353            case GL_UNSIGNED_SHORT:
354            case GL_INT:
355            case GL_UNSIGNED_INT:
356            case GL_FLOAT:
357            case GL_UNSIGNED_BYTE_3_3_2:
358            case GL_UNSIGNED_BYTE_2_3_3_REV:
359            case GL_UNSIGNED_SHORT_5_6_5:
360            case GL_UNSIGNED_SHORT_5_6_5_REV:
361               return GL_TRUE;
362            default:
363               return GL_FALSE;
364         }
365      case GL_RGBA:
366      case GL_BGRA:
367      case GL_ABGR_EXT:
368         switch (type) {
369            case GL_BYTE:
370            case GL_UNSIGNED_BYTE:
371            case GL_SHORT:
372            case GL_UNSIGNED_SHORT:
373            case GL_INT:
374            case GL_UNSIGNED_INT:
375            case GL_FLOAT:
376            case GL_UNSIGNED_SHORT_4_4_4_4:
377            case GL_UNSIGNED_SHORT_4_4_4_4_REV:
378            case GL_UNSIGNED_SHORT_5_5_5_1:
379            case GL_UNSIGNED_SHORT_1_5_5_5_REV:
380            case GL_UNSIGNED_INT_8_8_8_8:
381            case GL_UNSIGNED_INT_8_8_8_8_REV:
382            case GL_UNSIGNED_INT_10_10_10_2:
383            case GL_UNSIGNED_INT_2_10_10_10_REV:
384               return GL_TRUE;
385            default:
386               return GL_FALSE;
387         }
388      default:
389         ; /* fall-through */
390   }
391   return GL_FALSE;
392}
393
394
395
396/*
397 * Return the address of a pixel in an image (actually a volume).
398 * Pixel unpacking/packing parameters are observed according to 'packing'.
399 * Input:  image - start of image data
400 *         width, height - size of image
401 *         format - image format
402 *         type - pixel component type
403 *         packing - the pixelstore attributes
404 *         img - which image in the volume (0 for 1D or 2D images)
405 *         row, column - location of pixel in the image
406 * Return:  address of pixel at (image,row,column) in image or NULL if error.
407 */
408GLvoid *
409_mesa_image_address( const struct gl_pixelstore_attrib *packing,
410                     const GLvoid *image, GLsizei width,
411                     GLsizei height, GLenum format, GLenum type,
412                     GLint img, GLint row, GLint column )
413{
414   GLint alignment;        /* 1, 2 or 4 */
415   GLint pixels_per_row;
416   GLint rows_per_image;
417   GLint skiprows;
418   GLint skippixels;
419   GLint skipimages;       /* for 3-D volume images */
420   GLubyte *pixel_addr;
421
422   alignment = packing->Alignment;
423   if (packing->RowLength > 0) {
424      pixels_per_row = packing->RowLength;
425   }
426   else {
427      pixels_per_row = width;
428   }
429   if (packing->ImageHeight > 0) {
430      rows_per_image = packing->ImageHeight;
431   }
432   else {
433      rows_per_image = height;
434   }
435   skiprows = packing->SkipRows;
436   skippixels = packing->SkipPixels;
437   skipimages = packing->SkipImages;
438
439   if (type==GL_BITMAP) {
440      /* BITMAP data */
441      GLint comp_per_pixel;   /* components per pixel */
442      GLint bytes_per_comp;   /* bytes per component */
443      GLint bytes_per_row;
444      GLint bytes_per_image;
445
446      /* Compute bytes per component */
447      bytes_per_comp = _mesa_sizeof_packed_type( type );
448      if (bytes_per_comp<0) {
449         return NULL;
450      }
451
452      /* Compute number of components per pixel */
453      comp_per_pixel = _mesa_components_in_format( format );
454      if (comp_per_pixel<0 && type != GL_BITMAP) {
455         return NULL;
456      }
457
458      bytes_per_row = alignment
459                    * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
460
461      bytes_per_image = bytes_per_row * rows_per_image;
462
463      pixel_addr = (GLubyte *) image
464                 + (skipimages + img) * bytes_per_image
465                 + (skiprows + row) * bytes_per_row
466                 + (skippixels + column) / 8;
467   }
468   else {
469      /* Non-BITMAP data */
470      GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
471
472      bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
473
474      /* The pixel type and format should have been error checked earlier */
475      assert(bytes_per_pixel > 0);
476
477      bytes_per_row = pixels_per_row * bytes_per_pixel;
478      remainder = bytes_per_row % alignment;
479      if (remainder > 0)
480         bytes_per_row += (alignment - remainder);
481
482      ASSERT(bytes_per_row % alignment == 0);
483
484      bytes_per_image = bytes_per_row * rows_per_image;
485
486      /* compute final pixel address */
487      pixel_addr = (GLubyte *) image
488                 + (skipimages + img) * bytes_per_image
489                 + (skiprows + row) * bytes_per_row
490                 + (skippixels + column) * bytes_per_pixel;
491   }
492
493   return (GLvoid *) pixel_addr;
494}
495
496
497
498/*
499 * Compute the stride between image rows (in bytes) for the given
500 * pixel packing parameters and image width, format and type.
501 */
502GLint
503_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
504                        GLint width, GLenum format, GLenum type )
505{
506   ASSERT(packing);
507   if (type == GL_BITMAP) {
508      /* BITMAP data */
509      if (packing->RowLength == 0) {
510         GLint bytes = (width + 7) / 8;
511         return bytes;
512      }
513      else {
514         GLint bytes = (packing->RowLength + 7) / 8;
515         return bytes;
516      }
517   }
518   else {
519      /* Non-BITMAP data */
520      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
521      if (bytesPerPixel <= 0)
522         return -1;  /* error */
523      if (packing->RowLength == 0) {
524         GLint bytes = bytesPerPixel * width;
525         return bytes;
526      }
527      else {
528         GLint bytes = bytesPerPixel * packing->RowLength;
529         return bytes;
530      }
531   }
532}
533
534
535
536/*
537 * Unpack a 32x32 pixel polygon stipple from user memory using the
538 * current pixel unpack settings.
539 */
540void
541_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
542                              const struct gl_pixelstore_attrib *unpacking )
543{
544   GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap( 32, 32, pattern, unpacking );
545   if (ptrn) {
546      /* Convert pattern from GLubytes to GLuints and handle big/little
547       * endian differences
548       */
549      GLubyte *p = ptrn;
550      GLint i;
551      for (i = 0; i < 32; i++) {
552         dest[i] = (p[0] << 24)
553                 | (p[1] << 16)
554                 | (p[2] <<  8)
555                 | (p[3]      );
556         p += 4;
557      }
558      FREE(ptrn);
559   }
560}
561
562
563
564/*
565 * Pack polygon stipple into user memory given current pixel packing
566 * settings.
567 */
568void
569_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
570                            const struct gl_pixelstore_attrib *packing )
571{
572   /* Convert pattern from GLuints to GLubytes to handle big/little
573    * endian differences.
574    */
575   GLubyte ptrn[32*4];
576   GLint i;
577   for (i = 0; i < 32; i++) {
578      ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
579      ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
580      ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
581      ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
582   }
583
584   _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
585}
586
587
588
589/*
590 * Pack the given RGBA span into client memory at 'dest' address
591 * in the given pixel format and type.
592 * Optionally apply the enabled pixel transfer ops.
593 * Pack into memory using the given packing params struct.
594 * This is used by glReadPixels and glGetTexImage?D()
595 * Input:  ctx - the context
596 *         n - number of pixels in the span
597 *         rgba - the pixels
598 *         format - dest packing format
599 *         type - dest packing datatype
600 *         destination - destination packing address
601 *         packing - pixel packing parameters
602 *         applyTransferOps - apply scale/bias/lookup-table ops?
603 */
604void
605_mesa_pack_rgba_span( const GLcontext *ctx,
606                      GLuint n, CONST GLubyte srcRgba[][4],
607                      GLenum format, GLenum type, GLvoid *destination,
608                      const struct gl_pixelstore_attrib *packing,
609                      GLboolean applyTransferOps )
610{
611   applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
612                        ctx->Pixel.MapColorFlag ||
613                        ctx->ColorMatrix.type != MATRIX_IDENTITY ||
614                        ctx->Pixel.ScaleOrBiasRGBApcm ||
615                        ctx->Pixel.ColorTableEnabled);
616
617   /* Test for optimized case first */
618   if (!applyTransferOps && format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
619      /* common simple case */
620      MEMCPY( destination, srcRgba, n * 4 * sizeof(GLubyte) );
621   }
622   else if (!applyTransferOps && format == GL_RGB && type == GL_UNSIGNED_BYTE) {
623      /* common simple case */
624      GLint i;
625      GLubyte *dest = (GLubyte *) destination;
626      for (i = 0; i < n; i++) {
627         dest[0] = srcRgba[i][RCOMP];
628         dest[1] = srcRgba[i][GCOMP];
629         dest[2] = srcRgba[i][BCOMP];
630         dest += 3;
631      }
632   }
633   else {
634      /* general solution */
635      GLfloat rgba[MAX_WIDTH][4], luminance[MAX_WIDTH];
636      const GLfloat rscale = 1.0F / 255.0F;
637      const GLfloat gscale = 1.0F / 255.0F;
638      const GLfloat bscale = 1.0F / 255.0F;
639      const GLfloat ascale = 1.0F / 255.0F;
640      const GLint comps = _mesa_components_in_format(format);
641      GLuint i;
642
643      assert(n <= MAX_WIDTH);
644
645      /* convert color components to floating point */
646      for (i=0;i<n;i++) {
647         rgba[i][RCOMP] = srcRgba[i][RCOMP] * rscale;
648         rgba[i][GCOMP] = srcRgba[i][GCOMP] * gscale;
649         rgba[i][BCOMP] = srcRgba[i][BCOMP] * bscale;
650         rgba[i][ACOMP] = srcRgba[i][ACOMP] * ascale;
651      }
652
653      /*
654       * Apply scale, bias and lookup-tables if enabled.
655       */
656      if (applyTransferOps) {
657         /* scale & bias */
658         if (ctx->Pixel.ScaleOrBiasRGBA) {
659            _mesa_scale_and_bias_rgba( ctx, n, rgba );
660         }
661         /* color table lookup */
662         if (ctx->Pixel.MapColorFlag) {
663            _mesa_map_rgba( ctx, n, rgba );
664         }
665         /* color matrix */
666         if (ctx->ColorMatrix.type != MATRIX_IDENTITY ||
667             ctx->Pixel.ScaleOrBiasRGBApcm) {
668            _mesa_transform_rgba(ctx, n, rgba);
669         }
670         /* GL_SGI_color_table lookup */
671         if (ctx->Pixel.ColorTableEnabled) {
672            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
673         }
674      }
675
676      if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
677         for (i=0;i<n;i++) {
678            GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
679            luminance[i] = CLAMP( sum, 0.0F, 1.0F );
680         }
681      }
682
683      /*
684       * Pack/store the pixels.  Ugh!  Lots of cases!!!
685       */
686      switch (type) {
687         case GL_UNSIGNED_BYTE:
688            {
689               GLubyte *dst = (GLubyte *) destination;
690               switch (format) {
691                  case GL_RED:
692                     for (i=0;i<n;i++)
693                        dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
694                     break;
695                  case GL_GREEN:
696                     for (i=0;i<n;i++)
697                        dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
698                     break;
699                  case GL_BLUE:
700                     for (i=0;i<n;i++)
701                        dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
702                     break;
703                  case GL_ALPHA:
704                     for (i=0;i<n;i++)
705                        dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
706                     break;
707                  case GL_LUMINANCE:
708                     for (i=0;i<n;i++)
709                        dst[i] = FLOAT_TO_UBYTE(luminance[i]);
710                     break;
711                  case GL_LUMINANCE_ALPHA:
712                     for (i=0;i<n;i++) {
713                        dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
714                        dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
715                     }
716                     break;
717                  case GL_RGB:
718                     for (i=0;i<n;i++) {
719                        dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
720                        dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
721                        dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
722                     }
723                     break;
724                  case GL_RGBA:
725                     for (i=0;i<n;i++) {
726                        dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
727                        dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
728                        dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
729                        dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
730                     }
731                     break;
732                  case GL_BGR:
733                     for (i=0;i<n;i++) {
734                        dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
735                        dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
736                        dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
737                     }
738                     break;
739                  case GL_BGRA:
740                     for (i=0;i<n;i++) {
741                        dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
742                        dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
743                        dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
744                        dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
745                     }
746                     break;
747                  case GL_ABGR_EXT:
748                     for (i=0;i<n;i++) {
749                        dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
750                        dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
751                        dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
752                        dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
753                     }
754                     break;
755                  default:
756                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
757               }
758	    }
759	    break;
760	 case GL_BYTE:
761            {
762               GLbyte *dst = (GLbyte *) destination;
763               switch (format) {
764                  case GL_RED:
765                     for (i=0;i<n;i++)
766                        dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
767                     break;
768                  case GL_GREEN:
769                     for (i=0;i<n;i++)
770                        dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
771                     break;
772                  case GL_BLUE:
773                     for (i=0;i<n;i++)
774                        dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
775                     break;
776                  case GL_ALPHA:
777                     for (i=0;i<n;i++)
778                        dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
779                     break;
780                  case GL_LUMINANCE:
781                     for (i=0;i<n;i++)
782                        dst[i] = FLOAT_TO_BYTE(luminance[i]);
783                     break;
784                  case GL_LUMINANCE_ALPHA:
785                     for (i=0;i<n;i++) {
786                        dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
787                        dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
788                     }
789                     break;
790                  case GL_RGB:
791                     for (i=0;i<n;i++) {
792                        dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
793                        dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
794                        dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
795                     }
796                     break;
797                  case GL_RGBA:
798                     for (i=0;i<n;i++) {
799                        dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
800                        dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
801                        dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
802                        dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
803                     }
804                     break;
805                  case GL_BGR:
806                     for (i=0;i<n;i++) {
807                        dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
808                        dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
809                        dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
810                     }
811                     break;
812                  case GL_BGRA:
813                     for (i=0;i<n;i++) {
814                        dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
815                        dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
816                        dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
817                        dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
818                     }
819                  case GL_ABGR_EXT:
820                     for (i=0;i<n;i++) {
821                        dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
822                        dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
823                        dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
824                        dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
825                     }
826                     break;
827                  default:
828                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
829               }
830            }
831	    break;
832	 case GL_UNSIGNED_SHORT:
833            {
834               GLushort *dst = (GLushort *) destination;
835               switch (format) {
836                  case GL_RED:
837                     for (i=0;i<n;i++)
838                        dst[i] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
839                     break;
840                  case GL_GREEN:
841                     for (i=0;i<n;i++)
842                        dst[i] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
843                     break;
844                  case GL_BLUE:
845                     for (i=0;i<n;i++)
846                        dst[i] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
847                     break;
848                  case GL_ALPHA:
849                     for (i=0;i<n;i++)
850                        dst[i] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
851                     break;
852                  case GL_LUMINANCE:
853                     for (i=0;i<n;i++)
854                        dst[i] = FLOAT_TO_USHORT(luminance[i]);
855                     break;
856                  case GL_LUMINANCE_ALPHA:
857                     for (i=0;i<n;i++) {
858                        dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
859                        dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
860                     }
861                     break;
862                  case GL_RGB:
863                     for (i=0;i<n;i++) {
864                        dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
865                        dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
866                        dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
867                     }
868                     break;
869                  case GL_RGBA:
870                     for (i=0;i<n;i++) {
871                        dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
872                        dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
873                        dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
874                        dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
875                     }
876                     break;
877                  case GL_BGR:
878                     for (i=0;i<n;i++) {
879                        dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
880                        dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
881                        dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
882                     }
883                     break;
884                  case GL_BGRA:
885                     for (i=0;i<n;i++) {
886                        dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
887                        dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
888                        dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
889                        dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
890                     }
891                     break;
892                  case GL_ABGR_EXT:
893                     for (i=0;i<n;i++) {
894                        dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
895                        dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
896                        dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
897                        dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
898                     }
899                     break;
900                  default:
901                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
902               }
903               if (packing->SwapBytes) {
904                  _mesa_swap2( (GLushort *) dst, n * comps);
905               }
906            }
907	    break;
908	 case GL_SHORT:
909            {
910               GLshort *dst = (GLshort *) destination;
911               switch (format) {
912                  case GL_RED:
913                     for (i=0;i<n;i++)
914                        dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
915                     break;
916                  case GL_GREEN:
917                     for (i=0;i<n;i++)
918                        dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
919                     break;
920                  case GL_BLUE:
921                     for (i=0;i<n;i++)
922                        dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
923                     break;
924                  case GL_ALPHA:
925                     for (i=0;i<n;i++)
926                        dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
927                     break;
928                  case GL_LUMINANCE:
929                     for (i=0;i<n;i++)
930                        dst[i] = FLOAT_TO_SHORT(luminance[i]);
931                     break;
932                  case GL_LUMINANCE_ALPHA:
933                     for (i=0;i<n;i++) {
934                        dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
935                        dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
936                     }
937                     break;
938                  case GL_RGB:
939                     for (i=0;i<n;i++) {
940                        dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
941                        dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
942                        dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
943                     }
944                     break;
945                  case GL_RGBA:
946                     for (i=0;i<n;i++) {
947                        dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
948                        dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
949                        dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
950                        dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
951                     }
952                     break;
953                  case GL_BGR:
954                     for (i=0;i<n;i++) {
955                        dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
956                        dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
957                        dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
958                     }
959                     break;
960                  case GL_BGRA:
961                     for (i=0;i<n;i++) {
962                        dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
963                        dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
964                        dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
965                        dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
966                     }
967                  case GL_ABGR_EXT:
968                     for (i=0;i<n;i++) {
969                        dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
970                        dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
971                        dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
972                        dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
973                     }
974                     break;
975                  default:
976                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
977               }
978               if (packing->SwapBytes) {
979                  _mesa_swap2( (GLushort *) dst, n * comps );
980               }
981            }
982	    break;
983	 case GL_UNSIGNED_INT:
984            {
985               GLuint *dst = (GLuint *) destination;
986               switch (format) {
987                  case GL_RED:
988                     for (i=0;i<n;i++)
989                        dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
990                     break;
991                  case GL_GREEN:
992                     for (i=0;i<n;i++)
993                        dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
994                     break;
995                  case GL_BLUE:
996                     for (i=0;i<n;i++)
997                        dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
998                     break;
999                  case GL_ALPHA:
1000                     for (i=0;i<n;i++)
1001                        dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1002                     break;
1003                  case GL_LUMINANCE:
1004                     for (i=0;i<n;i++)
1005                        dst[i] = FLOAT_TO_UINT(luminance[i]);
1006                     break;
1007                  case GL_LUMINANCE_ALPHA:
1008                     for (i=0;i<n;i++) {
1009                        dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
1010                        dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1011                     }
1012                     break;
1013                  case GL_RGB:
1014                     for (i=0;i<n;i++) {
1015                        dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1016                        dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1017                        dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1018                     }
1019                     break;
1020                  case GL_RGBA:
1021                     for (i=0;i<n;i++) {
1022                        dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1023                        dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1024                        dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1025                        dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1026                     }
1027                     break;
1028                  case GL_BGR:
1029                     for (i=0;i<n;i++) {
1030                        dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1031                        dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1032                        dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1033                     }
1034                     break;
1035                  case GL_BGRA:
1036                     for (i=0;i<n;i++) {
1037                        dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1038                        dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1039                        dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1040                        dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1041                     }
1042                     break;
1043                  case GL_ABGR_EXT:
1044                     for (i=0;i<n;i++) {
1045                        dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1046                        dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1047                        dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1048                        dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1049                     }
1050                     break;
1051                  default:
1052                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1053               }
1054               if (packing->SwapBytes) {
1055                  _mesa_swap4( (GLuint *) dst, n * comps );
1056               }
1057            }
1058	    break;
1059	 case GL_INT:
1060	    {
1061               GLint *dst = (GLint *) destination;
1062               switch (format) {
1063                  case GL_RED:
1064                     for (i=0;i<n;i++)
1065                        dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
1066                     break;
1067                  case GL_GREEN:
1068                     for (i=0;i<n;i++)
1069                        dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
1070                     break;
1071                  case GL_BLUE:
1072                     for (i=0;i<n;i++)
1073                        dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
1074                     break;
1075                  case GL_ALPHA:
1076                     for (i=0;i<n;i++)
1077                        dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
1078                     break;
1079                  case GL_LUMINANCE:
1080                     for (i=0;i<n;i++)
1081                        dst[i] = FLOAT_TO_INT(luminance[i]);
1082                     break;
1083                  case GL_LUMINANCE_ALPHA:
1084                     for (i=0;i<n;i++) {
1085                        dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
1086                        dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
1087                     }
1088                     break;
1089                  case GL_RGB:
1090                     for (i=0;i<n;i++) {
1091                        dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1092                        dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1093                        dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1094                     }
1095                     break;
1096                  case GL_RGBA:
1097                     for (i=0;i<n;i++) {
1098                        dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1099                        dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1100                        dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1101                        dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1102                     }
1103                     break;
1104                  case GL_BGR:
1105                     for (i=0;i<n;i++) {
1106                        dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1107                        dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1108                        dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1109                     }
1110                     break;
1111                  case GL_BGRA:
1112                     for (i=0;i<n;i++) {
1113                        dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1114                        dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1115                        dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1116                        dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1117                     }
1118                     break;
1119                  case GL_ABGR_EXT:
1120                     for (i=0;i<n;i++) {
1121                        dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
1122                        dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
1123                        dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
1124                        dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
1125                     }
1126                     break;
1127                  default:
1128                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1129               }
1130	       if (packing->SwapBytes) {
1131		  _mesa_swap4( (GLuint *) dst, n * comps );
1132	       }
1133	    }
1134	    break;
1135	 case GL_FLOAT:
1136	    {
1137               GLfloat *dst = (GLfloat *) destination;
1138               switch (format) {
1139                  case GL_RED:
1140                     for (i=0;i<n;i++)
1141                        dst[i] = rgba[i][RCOMP];
1142                     break;
1143                  case GL_GREEN:
1144                     for (i=0;i<n;i++)
1145                        dst[i] = rgba[i][GCOMP];
1146                     break;
1147                  case GL_BLUE:
1148                     for (i=0;i<n;i++)
1149                        dst[i] = rgba[i][BCOMP];
1150                     break;
1151                  case GL_ALPHA:
1152                     for (i=0;i<n;i++)
1153                        dst[i] = rgba[i][ACOMP];
1154                     break;
1155                  case GL_LUMINANCE:
1156                     for (i=0;i<n;i++)
1157                        dst[i] = luminance[i];
1158                     break;
1159                  case GL_LUMINANCE_ALPHA:
1160                     for (i=0;i<n;i++) {
1161                        dst[i*2+0] = luminance[i];
1162                        dst[i*2+1] = rgba[i][ACOMP];
1163                     }
1164                     break;
1165                  case GL_RGB:
1166                     for (i=0;i<n;i++) {
1167                        dst[i*3+0] = rgba[i][RCOMP];
1168                        dst[i*3+1] = rgba[i][GCOMP];
1169                        dst[i*3+2] = rgba[i][BCOMP];
1170                     }
1171                     break;
1172                  case GL_RGBA:
1173                     for (i=0;i<n;i++) {
1174                        dst[i*4+0] = rgba[i][RCOMP];
1175                        dst[i*4+1] = rgba[i][GCOMP];
1176                        dst[i*4+2] = rgba[i][BCOMP];
1177                        dst[i*4+3] = rgba[i][ACOMP];
1178                     }
1179                     break;
1180                  case GL_BGR:
1181                     for (i=0;i<n;i++) {
1182                        dst[i*3+0] = rgba[i][BCOMP];
1183                        dst[i*3+1] = rgba[i][GCOMP];
1184                        dst[i*3+2] = rgba[i][RCOMP];
1185                     }
1186                     break;
1187                  case GL_BGRA:
1188                     for (i=0;i<n;i++) {
1189                        dst[i*4+0] = rgba[i][BCOMP];
1190                        dst[i*4+1] = rgba[i][GCOMP];
1191                        dst[i*4+2] = rgba[i][RCOMP];
1192                        dst[i*4+3] = rgba[i][ACOMP];
1193                     }
1194                     break;
1195                  case GL_ABGR_EXT:
1196                     for (i=0;i<n;i++) {
1197                        dst[i*4+0] = rgba[i][ACOMP];
1198                        dst[i*4+1] = rgba[i][BCOMP];
1199                        dst[i*4+2] = rgba[i][GCOMP];
1200                        dst[i*4+3] = rgba[i][RCOMP];
1201                     }
1202                     break;
1203                  default:
1204                     gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1205               }
1206	       if (packing->SwapBytes) {
1207		  _mesa_swap4( (GLuint *) dst, n * comps );
1208	       }
1209	    }
1210	    break;
1211         case GL_UNSIGNED_BYTE_3_3_2:
1212            if (format == GL_RGB) {
1213               GLubyte *dst = (GLubyte *) destination;
1214               for (i=0;i<n;i++) {
1215                  dst[i] = (((GLint) (rgba[i][RCOMP]   * 7.0F)) << 5)
1216                         | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 2)
1217                         | (((GLint) (rgba[i][BCOMP]  * 3.0F))     );
1218               }
1219            }
1220            break;
1221         case GL_UNSIGNED_BYTE_2_3_3_REV:
1222            if (format == GL_RGB) {
1223               GLubyte *dst = (GLubyte *) destination;
1224               for (i=0;i<n;i++) {
1225                  dst[i] = (((GLint) (rgba[i][RCOMP]   * 7.0F))     )
1226                         | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 3)
1227                         | (((GLint) (rgba[i][BCOMP]  * 3.0F)) << 5);
1228               }
1229            }
1230            break;
1231         case GL_UNSIGNED_SHORT_5_6_5:
1232            if (format == GL_RGB) {
1233               GLushort *dst = (GLushort *) destination;
1234               for (i=0;i<n;i++) {
1235                  dst[i] = (((GLint) (rgba[i][RCOMP]   * 31.0F)) << 11)
1236                         | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1237                         | (((GLint) (rgba[i][BCOMP]  * 31.0F))      );
1238               }
1239            }
1240            break;
1241         case GL_UNSIGNED_SHORT_5_6_5_REV:
1242            if (format == GL_RGB) {
1243               GLushort *dst = (GLushort *) destination;
1244               for (i=0;i<n;i++) {
1245                  dst[i] = (((GLint) (rgba[i][RCOMP]   * 31.0F))      )
1246                         | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1247                         | (((GLint) (rgba[i][BCOMP]  * 31.0F)) << 11);
1248               }
1249            }
1250            break;
1251         case GL_UNSIGNED_SHORT_4_4_4_4:
1252            if (format == GL_RGB) {
1253               GLushort *dst = (GLushort *) destination;
1254               for (i=0;i<n;i++) {
1255                  dst[i] = (((GLint) (rgba[i][RCOMP]   * 15.0F)) << 12)
1256                         | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1257                         | (((GLint) (rgba[i][BCOMP]  * 15.0F)) <<  4)
1258                         | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1259               }
1260            }
1261            break;
1262         case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1263            if (format == GL_RGB) {
1264               GLushort *dst = (GLushort *) destination;
1265               for (i=0;i<n;i++) {
1266                  dst[i] = (((GLint) (rgba[i][RCOMP]   * 15.0F))      )
1267                         | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1268                         | (((GLint) (rgba[i][BCOMP]  * 15.0F)) <<  8)
1269                         | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1270               }
1271            }
1272            break;
1273         case GL_UNSIGNED_SHORT_5_5_5_1:
1274            if (format == GL_RGB) {
1275               GLushort *dst = (GLushort *) destination;
1276               for (i=0;i<n;i++) {
1277                  dst[i] = (((GLint) (rgba[i][RCOMP]   * 31.0F)) << 11)
1278                         | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1279                         | (((GLint) (rgba[i][BCOMP]  * 31.0F)) <<  1)
1280                         | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1281               }
1282            }
1283            break;
1284         case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1285            if (format == GL_RGB) {
1286               GLushort *dst = (GLushort *) destination;
1287               for (i=0;i<n;i++) {
1288                  dst[i] = (((GLint) (rgba[i][RCOMP]   * 31.0F))      )
1289                         | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1290                         | (((GLint) (rgba[i][BCOMP]  * 31.0F)) << 10)
1291                         | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1292               }
1293            }
1294            break;
1295         case GL_UNSIGNED_INT_8_8_8_8:
1296            if (format == GL_RGBA) {
1297               GLuint *dst = (GLuint *) destination;
1298               for (i=0;i<n;i++) {
1299                  dst[i] = (((GLuint) (rgba[i][RCOMP]   * 255.0F)) << 24)
1300                         | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1301                         | (((GLuint) (rgba[i][BCOMP]  * 255.0F)) <<  8)
1302                         | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1303               }
1304            }
1305            else if (format == GL_BGRA) {
1306               GLuint *dst = (GLuint *) destination;
1307               for (i=0;i<n;i++) {
1308                  dst[i] = (((GLuint) (rgba[i][BCOMP]  * 255.0F)) << 24)
1309                         | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1310                         | (((GLuint) (rgba[i][RCOMP]   * 255.0F)) <<  8)
1311                         | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1312               }
1313            }
1314            else if (format == GL_ABGR_EXT) {
1315               GLuint *dst = (GLuint *) destination;
1316               for (i=0;i<n;i++) {
1317                  dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24)
1318                         | (((GLuint) (rgba[i][BCOMP]  * 255.0F)) << 16)
1319                         | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1320                         | (((GLuint) (rgba[i][RCOMP]   * 255.0F))      );
1321               }
1322            }
1323            break;
1324         case GL_UNSIGNED_INT_8_8_8_8_REV:
1325            if (format == GL_RGBA) {
1326               GLuint *dst = (GLuint *) destination;
1327               for (i=0;i<n;i++) {
1328                  dst[i] = (((GLuint) (rgba[i][RCOMP]   * 255.0F))      )
1329                         | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1330                         | (((GLuint) (rgba[i][BCOMP]  * 255.0F)) << 16)
1331                         | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1332               }
1333            }
1334            else if (format == GL_BGRA) {
1335               GLuint *dst = (GLuint *) destination;
1336               for (i=0;i<n;i++) {
1337                  dst[i] = (((GLuint) (rgba[i][BCOMP]  * 255.0F))      )
1338                         | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1339                         | (((GLuint) (rgba[i][RCOMP]   * 255.0F)) << 16)
1340                         | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1341               }
1342            }
1343            else if (format == GL_ABGR_EXT) {
1344               GLuint *dst = (GLuint *) destination;
1345               for (i=0;i<n;i++) {
1346                  dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F))      )
1347                         | (((GLuint) (rgba[i][BCOMP]  * 255.0F)) <<  8)
1348                         | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1349                         | (((GLuint) (rgba[i][RCOMP]   * 255.0F)) << 24);
1350               }
1351            }
1352            break;
1353         case GL_UNSIGNED_INT_10_10_10_2:
1354            if (format == GL_RGBA) {
1355               GLuint *dst = (GLuint *) destination;
1356               for (i=0;i<n;i++) {
1357                  dst[i] = (((GLuint) (rgba[i][RCOMP]   * 1023.0F)) << 22)
1358                         | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
1359                         | (((GLuint) (rgba[i][BCOMP]  * 1023.0F)) <<  2)
1360                         | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
1361               }
1362            }
1363            else if (format == GL_BGRA) {
1364               GLuint *dst = (GLuint *) destination;
1365               for (i=0;i<n;i++) {
1366                  dst[i] = (((GLuint) (rgba[i][BCOMP]  * 1023.0F)) << 22)
1367                         | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
1368                         | (((GLuint) (rgba[i][RCOMP]   * 1023.0F)) <<  2)
1369                         | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
1370               }
1371            }
1372            else if (format == GL_ABGR_EXT) {
1373               GLuint *dst = (GLuint *) destination;
1374               for (i=0;i<n;i++) {
1375                  dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F)) << 22)
1376                         | (((GLuint) (rgba[i][BCOMP]  * 1023.0F)) << 12)
1377                         | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) <<  2)
1378                         | (((GLuint) (rgba[i][RCOMP]   *    3.0F))      );
1379               }
1380            }
1381            break;
1382         case GL_UNSIGNED_INT_2_10_10_10_REV:
1383            if (format == GL_RGBA) {
1384               GLuint *dst = (GLuint *) destination;
1385               for (i=0;i<n;i++) {
1386                  dst[i] = (((GLuint) (rgba[i][RCOMP]   * 1023.0F))      )
1387                         | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
1388                         | (((GLuint) (rgba[i][BCOMP]  * 1023.0F)) << 20)
1389                         | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
1390               }
1391            }
1392            else if (format == GL_BGRA) {
1393               GLuint *dst = (GLuint *) destination;
1394               for (i=0;i<n;i++) {
1395                  dst[i] = (((GLuint) (rgba[i][BCOMP]  * 1023.0F))      )
1396                         | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
1397                         | (((GLuint) (rgba[i][RCOMP]   * 1023.0F)) << 20)
1398                         | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
1399               }
1400            }
1401            else if (format == GL_ABGR_EXT) {
1402               GLuint *dst = (GLuint *) destination;
1403               for (i=0;i<n;i++) {
1404                  dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F))      )
1405                         | (((GLuint) (rgba[i][BCOMP]  * 1023.0F)) << 10)
1406                         | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 20)
1407                         | (((GLuint) (rgba[i][RCOMP]   *    3.0F)) << 30);
1408               }
1409            }
1410            break;
1411         default:
1412            gl_problem( ctx, "bad type in _mesa_pack_rgba_span" );
1413      }
1414   }
1415}
1416
1417
1418
1419#define SWAP2BYTE(VALUE)			\
1420   {						\
1421      GLubyte *bytes = (GLubyte *) &(VALUE);	\
1422      GLubyte tmp = bytes[0];			\
1423      bytes[0] = bytes[1];			\
1424      bytes[1] = tmp;				\
1425   }
1426
1427#define SWAP4BYTE(VALUE)			\
1428   {						\
1429      GLubyte *bytes = (GLubyte *) &(VALUE);	\
1430      GLubyte tmp = bytes[0];			\
1431      bytes[0] = bytes[3];			\
1432      bytes[3] = tmp;				\
1433      tmp = bytes[1];				\
1434      bytes[1] = bytes[2];			\
1435      bytes[2] = tmp;				\
1436   }
1437
1438
1439static void
1440extract_uint_indexes(GLuint n, GLuint indexes[],
1441                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
1442                     const struct gl_pixelstore_attrib *unpack )
1443{
1444   assert(srcFormat == GL_COLOR_INDEX);
1445
1446   ASSERT(srcType == GL_BITMAP ||
1447          srcType == GL_UNSIGNED_BYTE ||
1448          srcType == GL_BYTE ||
1449          srcType == GL_UNSIGNED_SHORT ||
1450          srcType == GL_SHORT ||
1451          srcType == GL_UNSIGNED_INT ||
1452          srcType == GL_INT ||
1453          srcType == GL_FLOAT);
1454
1455   switch (srcType) {
1456      case GL_BITMAP:
1457         {
1458            GLubyte *ubsrc = (GLubyte *) src;
1459            if (unpack->LsbFirst) {
1460               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
1461               GLuint i;
1462               for (i = 0; i < n; i++) {
1463                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
1464                  if (mask == 128) {
1465                     mask = 1;
1466                     ubsrc++;
1467                  }
1468                  else {
1469                     mask = mask << 1;
1470                  }
1471               }
1472            }
1473            else {
1474               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
1475               GLuint i;
1476               for (i = 0; i < n; i++) {
1477                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
1478                  if (mask == 1) {
1479                     mask = 128;
1480                     ubsrc++;
1481                  }
1482                  else {
1483                     mask = mask >> 1;
1484                  }
1485               }
1486            }
1487         }
1488         break;
1489      case GL_UNSIGNED_BYTE:
1490         {
1491            GLuint i;
1492            const GLubyte *s = (const GLubyte *) src;
1493            for (i = 0; i < n; i++)
1494               indexes[i] = s[i];
1495         }
1496         break;
1497      case GL_BYTE:
1498         {
1499            GLuint i;
1500            const GLbyte *s = (const GLbyte *) src;
1501            for (i = 0; i < n; i++)
1502               indexes[i] = s[i];
1503         }
1504         break;
1505      case GL_UNSIGNED_SHORT:
1506         {
1507            GLuint i;
1508            const GLushort *s = (const GLushort *) src;
1509            if (unpack->SwapBytes) {
1510               for (i = 0; i < n; i++) {
1511                  GLushort value = s[i];
1512                  SWAP2BYTE(value);
1513                  indexes[i] = value;
1514               }
1515            }
1516            else {
1517               for (i = 0; i < n; i++)
1518                  indexes[i] = s[i];
1519            }
1520         }
1521         break;
1522      case GL_SHORT:
1523         {
1524            GLuint i;
1525            const GLshort *s = (const GLshort *) src;
1526            if (unpack->SwapBytes) {
1527               for (i = 0; i < n; i++) {
1528                  GLshort value = s[i];
1529                  SWAP2BYTE(value);
1530                  indexes[i] = value;
1531               }
1532            }
1533            else {
1534               for (i = 0; i < n; i++)
1535                  indexes[i] = s[i];
1536            }
1537         }
1538         break;
1539      case GL_UNSIGNED_INT:
1540         {
1541            GLuint i;
1542            const GLuint *s = (const GLuint *) src;
1543            if (unpack->SwapBytes) {
1544               for (i = 0; i < n; i++) {
1545                  GLuint value = s[i];
1546                  SWAP4BYTE(value);
1547                  indexes[i] = value;
1548               }
1549            }
1550            else {
1551               for (i = 0; i < n; i++)
1552                  indexes[i] = s[i];
1553            }
1554         }
1555         break;
1556      case GL_INT:
1557         {
1558            GLuint i;
1559            const GLint *s = (const GLint *) src;
1560            if (unpack->SwapBytes) {
1561               for (i = 0; i < n; i++) {
1562                  GLint value = s[i];
1563                  SWAP4BYTE(value);
1564                  indexes[i] = value;
1565               }
1566            }
1567            else {
1568               for (i = 0; i < n; i++)
1569                  indexes[i] = s[i];
1570            }
1571         }
1572         break;
1573      case GL_FLOAT:
1574         {
1575            GLuint i;
1576            const GLfloat *s = (const GLfloat *) src;
1577            if (unpack->SwapBytes) {
1578               for (i = 0; i < n; i++) {
1579                  GLfloat value = s[i];
1580                  SWAP4BYTE(value);
1581                  indexes[i] = (GLuint) value;
1582               }
1583            }
1584            else {
1585               for (i = 0; i < n; i++)
1586                  indexes[i] = (GLuint) s[i];
1587            }
1588         }
1589         break;
1590      default:
1591         gl_problem(NULL, "bad srcType in extract_uint_indexes");
1592         return;
1593   }
1594}
1595
1596
1597
1598/*
1599 * This function extracts floating point RGBA values from arbitrary
1600 * image data.  srcFormat and srcType are the format and type parameters
1601 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1602 *
1603 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1604 * implements the "Conversion to floating point", "Conversion to RGB",
1605 * and "Final Expansion to RGBA" operations.
1606 *
1607 * Args:  n - number of pixels
1608 *        rgba - output colors
1609 *        srcFormat - format of incoming data
1610 *        srcType - datatype of incoming data
1611 *        src - source data pointer
1612 *        swapBytes - perform byteswapping of incoming data?
1613 */
1614static void
1615extract_float_rgba(GLuint n, GLfloat rgba[][4],
1616                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
1617                   GLboolean swapBytes)
1618{
1619   GLint redIndex, greenIndex, blueIndex, alphaIndex;
1620   GLint stride;
1621   GLint rComp, bComp, gComp, aComp;
1622
1623   ASSERT(srcFormat == GL_RED ||
1624          srcFormat == GL_GREEN ||
1625          srcFormat == GL_BLUE ||
1626          srcFormat == GL_ALPHA ||
1627          srcFormat == GL_LUMINANCE ||
1628          srcFormat == GL_LUMINANCE_ALPHA ||
1629          srcFormat == GL_INTENSITY ||
1630          srcFormat == GL_RGB ||
1631          srcFormat == GL_BGR ||
1632          srcFormat == GL_RGBA ||
1633          srcFormat == GL_BGRA ||
1634          srcFormat == GL_ABGR_EXT);
1635
1636   ASSERT(srcType == GL_UNSIGNED_BYTE ||
1637          srcType == GL_BYTE ||
1638          srcType == GL_UNSIGNED_SHORT ||
1639          srcType == GL_SHORT ||
1640          srcType == GL_UNSIGNED_INT ||
1641          srcType == GL_INT ||
1642          srcType == GL_FLOAT ||
1643          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
1644          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
1645          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
1646          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
1647          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
1648          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
1649          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
1650          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
1651          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1652          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
1653          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
1654          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
1655
1656   rComp = gComp = bComp = aComp = -1;
1657
1658   switch (srcFormat) {
1659      case GL_RED:
1660         redIndex = 0;
1661         greenIndex = blueIndex = alphaIndex = -1;
1662         stride = 1;
1663         break;
1664      case GL_GREEN:
1665         greenIndex = 0;
1666         redIndex = blueIndex = alphaIndex = -1;
1667         stride = 1;
1668         break;
1669      case GL_BLUE:
1670         blueIndex = 0;
1671         redIndex = greenIndex = alphaIndex = -1;
1672         stride = 1;
1673         break;
1674      case GL_ALPHA:
1675         redIndex = greenIndex = blueIndex = -1;
1676         alphaIndex = 0;
1677         stride = 1;
1678         break;
1679      case GL_LUMINANCE:
1680         redIndex = greenIndex = blueIndex = 0;
1681         alphaIndex = -1;
1682         stride = 1;
1683         break;
1684      case GL_LUMINANCE_ALPHA:
1685         redIndex = greenIndex = blueIndex = 0;
1686         alphaIndex = 1;
1687         stride = 2;
1688         break;
1689      case GL_INTENSITY:
1690         redIndex = 0;
1691         greenIndex = blueIndex = alphaIndex = -1;
1692         stride = 1;
1693         break;
1694      case GL_RGB:
1695         redIndex = 0;
1696         greenIndex = 1;
1697         blueIndex = 2;
1698         alphaIndex = -1;
1699         stride = 3;
1700         break;
1701      case GL_BGR:
1702         redIndex = 2;
1703         greenIndex = 1;
1704         blueIndex = 0;
1705         alphaIndex = -1;
1706         stride = 3;
1707         break;
1708      case GL_RGBA:
1709         redIndex = 0;
1710         greenIndex = 1;
1711         blueIndex = 2;
1712         alphaIndex = 3;
1713         rComp = 0;
1714         gComp = 1;
1715         bComp = 2;
1716         aComp = 3;
1717         stride = 4;
1718         break;
1719      case GL_BGRA:
1720         redIndex = 2;
1721         greenIndex = 1;
1722         blueIndex = 0;
1723         alphaIndex = 3;
1724         rComp = 2;
1725         gComp = 1;
1726         bComp = 0;
1727         aComp = 3;
1728         stride = 4;
1729         break;
1730      case GL_ABGR_EXT:
1731         redIndex = 3;
1732         greenIndex = 2;
1733         blueIndex = 1;
1734         alphaIndex = 0;
1735         rComp = 3;
1736         gComp = 2;
1737         bComp = 1;
1738         aComp = 0;
1739         stride = 4;
1740         break;
1741      default:
1742         gl_problem(NULL, "bad srcFormat in extract float data");
1743         return;
1744   }
1745
1746
1747#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)		\
1748   if ((INDEX) < 0) {							\
1749      GLuint i;								\
1750      for (i = 0; i < n; i++) {						\
1751         rgba[i][CHANNEL] = DEFAULT;					\
1752      }									\
1753   }									\
1754   else if (swapBytes) {						\
1755      const TYPE *s = (const TYPE *) src;				\
1756      GLuint i;								\
1757      for (i = 0; i < n; i++) {						\
1758         TYPE value = s[INDEX];						\
1759         if (sizeof(TYPE) == 2) {					\
1760            SWAP2BYTE(value);						\
1761         }								\
1762         else if (sizeof(TYPE) == 4) {					\
1763            SWAP4BYTE(value);						\
1764         }								\
1765         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);		\
1766         s += stride;							\
1767      }									\
1768   }									\
1769   else {								\
1770      const TYPE *s = (const TYPE *) src;				\
1771      GLuint i;								\
1772      for (i = 0; i < n; i++) {						\
1773         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);		\
1774         s += stride;							\
1775      }									\
1776   }
1777
1778   switch (srcType) {
1779      case GL_UNSIGNED_BYTE:
1780         PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1781         PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1782         PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1783         PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
1784         break;
1785      case GL_BYTE:
1786         PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1787         PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1788         PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1789         PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
1790         break;
1791      case GL_UNSIGNED_SHORT:
1792         PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1793         PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1794         PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1795         PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
1796         break;
1797      case GL_SHORT:
1798         PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1799         PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1800         PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1801         PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
1802         break;
1803      case GL_UNSIGNED_INT:
1804         PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1805         PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1806         PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1807         PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
1808         break;
1809      case GL_INT:
1810         PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
1811         PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
1812         PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
1813         PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
1814         break;
1815      case GL_FLOAT:
1816         PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
1817         PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
1818         PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
1819         PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
1820         break;
1821      case GL_UNSIGNED_BYTE_3_3_2:
1822         {
1823            const GLubyte *ubsrc = (const GLubyte *) src;
1824            GLuint i;
1825            for (i = 0; i < n; i ++) {
1826               GLubyte p = ubsrc[i];
1827               rgba[i][RCOMP] = ((p >> 5)      ) * (1.0F / 7.0F);
1828               rgba[i][GCOMP] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
1829               rgba[i][BCOMP] = ((p     ) & 0x3) * (1.0F / 3.0F);
1830               rgba[i][ACOMP] = 1.0F;
1831            }
1832         }
1833         break;
1834      case GL_UNSIGNED_BYTE_2_3_3_REV:
1835         {
1836            const GLubyte *ubsrc = (const GLubyte *) src;
1837            GLuint i;
1838            for (i = 0; i < n; i ++) {
1839               GLubyte p = ubsrc[i];
1840               rgba[i][RCOMP] = ((p     ) & 0x7) * (1.0F / 7.0F);
1841               rgba[i][GCOMP] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
1842               rgba[i][BCOMP] = ((p >> 6)      ) * (1.0F / 3.0F);
1843               rgba[i][ACOMP] = 1.0F;
1844            }
1845         }
1846         break;
1847      case GL_UNSIGNED_SHORT_5_6_5:
1848         if (swapBytes) {
1849            const GLushort *ussrc = (const GLushort *) src;
1850            GLuint i;
1851            for (i = 0; i < n; i ++) {
1852               GLushort p = ussrc[i];
1853               SWAP2BYTE(p);
1854               rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1855               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1856               rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1857               rgba[i][ACOMP] = 1.0F;
1858            }
1859         }
1860         else {
1861            const GLushort *ussrc = (const GLushort *) src;
1862            GLuint i;
1863            for (i = 0; i < n; i ++) {
1864               GLushort p = ussrc[i];
1865               rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1866               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1867               rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1868               rgba[i][ACOMP] = 1.0F;
1869            }
1870         }
1871         break;
1872      case GL_UNSIGNED_SHORT_5_6_5_REV:
1873         if (swapBytes) {
1874            const GLushort *ussrc = (const GLushort *) src;
1875            GLuint i;
1876            for (i = 0; i < n; i ++) {
1877               GLushort p = ussrc[i];
1878               SWAP2BYTE(p);
1879               rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1880               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1881               rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1882               rgba[i][ACOMP] = 1.0F;
1883            }
1884         }
1885         else {
1886            const GLushort *ussrc = (const GLushort *) src;
1887            GLuint i;
1888            for (i = 0; i < n; i ++) {
1889               GLushort p = ussrc[i];
1890               rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1891               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1892               rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1893               rgba[i][ACOMP] = 1.0F;
1894            }
1895         }
1896         break;
1897      case GL_UNSIGNED_SHORT_4_4_4_4:
1898         if (swapBytes) {
1899            const GLushort *ussrc = (const GLushort *) src;
1900            GLuint i;
1901            for (i = 0; i < n; i ++) {
1902               GLushort p = ussrc[i];
1903               SWAP2BYTE(p);
1904               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1905               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1906               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1907               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1908            }
1909         }
1910         else {
1911            const GLushort *ussrc = (const GLushort *) src;
1912            GLuint i;
1913            for (i = 0; i < n; i ++) {
1914               GLushort p = ussrc[i];
1915               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1916               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1917               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1918               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1919            }
1920         }
1921         break;
1922      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1923         if (swapBytes) {
1924            const GLushort *ussrc = (const GLushort *) src;
1925            GLuint i;
1926            for (i = 0; i < n; i ++) {
1927               GLushort p = ussrc[i];
1928               SWAP2BYTE(p);
1929               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1930               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1931               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1932               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1933            }
1934         }
1935         else {
1936            const GLushort *ussrc = (const GLushort *) src;
1937            GLuint i;
1938            for (i = 0; i < n; i ++) {
1939               GLushort p = ussrc[i];
1940               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1941               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1942               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1943               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1944            }
1945         }
1946         break;
1947      case GL_UNSIGNED_SHORT_5_5_5_1:
1948         if (swapBytes) {
1949            const GLushort *ussrc = (const GLushort *) src;
1950            GLuint i;
1951            for (i = 0; i < n; i ++) {
1952               GLushort p = ussrc[i];
1953               SWAP2BYTE(p);
1954               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
1955               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
1956               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
1957               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
1958            }
1959         }
1960         else {
1961            const GLushort *ussrc = (const GLushort *) src;
1962            GLuint i;
1963            for (i = 0; i < n; i ++) {
1964               GLushort p = ussrc[i];
1965               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
1966               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
1967               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
1968               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
1969            }
1970         }
1971         break;
1972      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1973         if (swapBytes) {
1974            const GLushort *ussrc = (const GLushort *) src;
1975            GLuint i;
1976            for (i = 0; i < n; i ++) {
1977               GLushort p = ussrc[i];
1978               SWAP2BYTE(p);
1979               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1980               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
1981               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
1982               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
1983            }
1984         }
1985         else {
1986            const GLushort *ussrc = (const GLushort *) src;
1987            GLuint i;
1988            for (i = 0; i < n; i ++) {
1989               GLushort p = ussrc[i];
1990               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1991               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
1992               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
1993               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
1994            }
1995         }
1996         break;
1997      case GL_UNSIGNED_INT_8_8_8_8:
1998         if (swapBytes) {
1999            const GLuint *uisrc = (const GLuint *) src;
2000            GLuint i;
2001            for (i = 0; i < n; i ++) {
2002               GLuint p = uisrc[i];
2003               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2004               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2005               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2006               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2007            }
2008         }
2009         else {
2010            const GLuint *uisrc = (const GLuint *) src;
2011            GLuint i;
2012            for (i = 0; i < n; i ++) {
2013               GLuint p = uisrc[i];
2014               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2015               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2016               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2017               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2018            }
2019         }
2020         break;
2021      case GL_UNSIGNED_INT_8_8_8_8_REV:
2022         if (swapBytes) {
2023            const GLuint *uisrc = (const GLuint *) src;
2024            GLuint i;
2025            for (i = 0; i < n; i ++) {
2026               GLuint p = uisrc[i];
2027               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2028               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2029               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2030               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2031            }
2032         }
2033         else {
2034            const GLuint *uisrc = (const GLuint *) src;
2035            GLuint i;
2036            for (i = 0; i < n; i ++) {
2037               GLuint p = uisrc[i];
2038               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2039               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2040               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2041               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2042            }
2043         }
2044         break;
2045      case GL_UNSIGNED_INT_10_10_10_2:
2046         if (swapBytes) {
2047            const GLuint *uisrc = (const GLuint *) src;
2048            GLuint i;
2049            for (i = 0; i < n; i ++) {
2050               GLuint p = uisrc[i];
2051               SWAP4BYTE(p);
2052               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2053               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2054               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2055               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2056            }
2057         }
2058         else {
2059            const GLuint *uisrc = (const GLuint *) src;
2060            GLuint i;
2061            for (i = 0; i < n; i ++) {
2062               GLuint p = uisrc[i];
2063               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2064               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2065               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2066               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2067            }
2068         }
2069         break;
2070      case GL_UNSIGNED_INT_2_10_10_10_REV:
2071         if (swapBytes) {
2072            const GLuint *uisrc = (const GLuint *) src;
2073            GLuint i;
2074            for (i = 0; i < n; i ++) {
2075               GLuint p = uisrc[i];
2076               SWAP4BYTE(p);
2077               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2078               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2079               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2080               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2081            }
2082         }
2083         else {
2084            const GLuint *uisrc = (const GLuint *) src;
2085            GLuint i;
2086            for (i = 0; i < n; i ++) {
2087               GLuint p = uisrc[i];
2088               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2089               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2090               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2091               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2092            }
2093         }
2094         break;
2095      default:
2096         gl_problem(NULL, "bad srcType in extract float data");
2097         break;
2098   }
2099}
2100
2101
2102
2103/*
2104 * Unpack a row of color image data from a client buffer according to
2105 * the pixel unpacking parameters.  Apply any enabled pixel transfer
2106 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
2107 * Return GLubyte values in the specified dest image format.
2108 * This is (or will be) used by glDrawPixels and glTexImage?D().
2109 * Input:  ctx - the context
2110 *         n - number of pixels in the span
2111 *         dstFormat - format of destination color array
2112 *         dest - the destination color array
2113 *         srcFormat - source image format
2114 *         srcType - source image  datatype
2115 *         source - source image pointer
2116 *         unpacking - pixel unpacking parameters
2117 *         applyTransferOps - apply scale/bias/lookup-table ops?
2118 *
2119 * XXX perhaps expand this to process whole images someday.
2120 */
2121void
2122_mesa_unpack_ubyte_color_span( const GLcontext *ctx,
2123                               GLuint n, GLenum dstFormat, GLubyte dest[],
2124                               GLenum srcFormat, GLenum srcType,
2125                               const GLvoid *source,
2126                               const struct gl_pixelstore_attrib *unpacking,
2127                               GLboolean applyTransferOps )
2128{
2129   ASSERT(dstFormat == GL_ALPHA ||
2130          dstFormat == GL_LUMINANCE ||
2131          dstFormat == GL_LUMINANCE_ALPHA ||
2132          dstFormat == GL_INTENSITY ||
2133          dstFormat == GL_RGB ||
2134          dstFormat == GL_RGBA ||
2135          dstFormat == GL_COLOR_INDEX);
2136
2137   ASSERT(srcFormat == GL_RED ||
2138          srcFormat == GL_GREEN ||
2139          srcFormat == GL_BLUE ||
2140          srcFormat == GL_ALPHA ||
2141          srcFormat == GL_LUMINANCE ||
2142          srcFormat == GL_LUMINANCE_ALPHA ||
2143          srcFormat == GL_INTENSITY ||
2144          srcFormat == GL_RGB ||
2145          srcFormat == GL_BGR ||
2146          srcFormat == GL_RGBA ||
2147          srcFormat == GL_BGRA ||
2148          srcFormat == GL_ABGR_EXT ||
2149          srcFormat == GL_COLOR_INDEX);
2150
2151   ASSERT(srcType == GL_BITMAP ||
2152          srcType == GL_UNSIGNED_BYTE ||
2153          srcType == GL_BYTE ||
2154          srcType == GL_UNSIGNED_SHORT ||
2155          srcType == GL_SHORT ||
2156          srcType == GL_UNSIGNED_INT ||
2157          srcType == GL_INT ||
2158          srcType == GL_FLOAT ||
2159          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2160          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2161          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2162          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2163          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2164          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2165          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2166          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2167          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2168          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2169          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2170          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2171
2172   /* this is intended for RGBA mode only */
2173   assert(ctx->Visual->RGBAflag);
2174
2175   applyTransferOps &= (ctx->Pixel.ScaleOrBiasRGBA ||
2176                        ctx->Pixel.MapColorFlag ||
2177                        ctx->ColorMatrix.type != MATRIX_IDENTITY ||
2178                        ctx->Pixel.ScaleOrBiasRGBApcm ||
2179                        ctx->Pixel.ColorTableEnabled);
2180
2181   /* Try simple cases first */
2182   if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE) {
2183      if (dstFormat == GL_RGBA) {
2184         if (srcFormat == GL_RGBA) {
2185            MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
2186            return;
2187         }
2188         else if (srcFormat == GL_RGB) {
2189            GLuint i;
2190            const GLubyte *src = (const GLubyte *) source;
2191            GLubyte *dst = dest;
2192            for (i = 0; i < n; i++) {
2193               dst[0] = src[0];
2194               dst[1] = src[1];
2195               dst[2] = src[2];
2196               dst[3] = 255;
2197               src += 3;
2198               dst += 4;
2199            }
2200            return;
2201         }
2202      }
2203      else if (dstFormat == GL_RGB) {
2204         if (srcFormat == GL_RGB) {
2205            MEMCPY( dest, source, n * 3 * sizeof(GLubyte) );
2206            return;
2207         }
2208         else if (srcFormat == GL_RGBA) {
2209            GLuint i;
2210            const GLubyte *src = (const GLubyte *) source;
2211            GLubyte *dst = dest;
2212            for (i = 0; i < n; i++) {
2213               dst[0] = src[0];
2214               dst[1] = src[1];
2215               dst[2] = src[2];
2216               src += 4;
2217               dst += 3;
2218            }
2219            return;
2220         }
2221      }
2222      else if (dstFormat == srcFormat) {
2223         GLint comps = _mesa_components_in_format(srcFormat);
2224         assert(comps > 0);
2225         MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
2226         return;
2227      }
2228   }
2229
2230
2231   /* general solution begins here */
2232   {
2233      GLfloat rgba[MAX_WIDTH][4];
2234      GLint dstComponents;
2235      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2236      GLint dstLuminanceIndex, dstIntensityIndex;
2237
2238      dstComponents = _mesa_components_in_format( dstFormat );
2239      /* source & dest image formats should have been error checked by now */
2240      assert(dstComponents > 0);
2241
2242      /*
2243       * Extract image data and convert to RGBA floats
2244       */
2245      assert(n <= MAX_WIDTH);
2246      if (srcFormat == GL_COLOR_INDEX) {
2247         GLuint indexes[MAX_WIDTH];
2248         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2249                              unpacking);
2250
2251         /* shift and offset indexes */
2252         _mesa_shift_and_offset_ci(ctx, n, indexes);
2253
2254         if (dstFormat == GL_COLOR_INDEX) {
2255            if (applyTransferOps) {
2256               if (ctx->Pixel.MapColorFlag) {
2257                  /* Apply lookup table */
2258                  _mesa_map_ci(ctx, n, indexes);
2259               }
2260               if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2261                  _mesa_shift_and_offset_ci(ctx, n, indexes);
2262               }
2263            }
2264            /* convert to GLubyte and return */
2265            {
2266               GLuint i;
2267               for (i = 0; i < n; i++) {
2268                  dest[i] = (GLubyte) (indexes[i] & 0xff);
2269               }
2270            }
2271         }
2272         else {
2273            /* Convert indexes to RGBA */
2274            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
2275         }
2276      }
2277      else {
2278         extract_float_rgba(n, rgba, srcFormat, srcType, source,
2279                            unpacking->SwapBytes);
2280
2281         if (applyTransferOps) {
2282            /* scale and bias colors */
2283            _mesa_scale_and_bias_rgba(ctx, n, rgba);
2284            /* color table lookup */
2285            if (ctx->Pixel.MapColorFlag) {
2286               _mesa_map_rgba(ctx, n, rgba);
2287            }
2288            /* color matrix transform */
2289            if (ctx->ColorMatrix.type != MATRIX_IDENTITY ||
2290                ctx->Pixel.ScaleOrBiasRGBApcm) {
2291               _mesa_transform_rgba(ctx, n, rgba);
2292            }
2293            /* GL_SGI_color_table lookup */
2294            if (ctx->Pixel.ColorTableEnabled) {
2295               _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
2296            }
2297         }
2298      }
2299
2300      /* clamp to [0,1] */
2301      {
2302         GLuint i;
2303         for (i = 0; i < n; i++) {
2304            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2305            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2306            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2307            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2308         }
2309      }
2310
2311      /* Now determine which color channels we need to produce.
2312       * And determine the dest index (offset) within each color tuple.
2313       */
2314      switch (dstFormat) {
2315         case GL_ALPHA:
2316            dstAlphaIndex = 0;
2317            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2318            dstLuminanceIndex = dstIntensityIndex = -1;
2319            break;
2320         case GL_LUMINANCE:
2321            dstLuminanceIndex = 0;
2322            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2323            dstIntensityIndex = -1;
2324            break;
2325         case GL_LUMINANCE_ALPHA:
2326            dstLuminanceIndex = 0;
2327            dstAlphaIndex = 1;
2328            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2329            dstIntensityIndex = -1;
2330            break;
2331         case GL_INTENSITY:
2332            dstIntensityIndex = 0;
2333            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2334            dstLuminanceIndex = -1;
2335            break;
2336         case GL_RGB:
2337            dstRedIndex = 0;
2338            dstGreenIndex = 1;
2339            dstBlueIndex = 2;
2340            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2341            break;
2342         case GL_RGBA:
2343            dstRedIndex = 0;
2344            dstGreenIndex = 1;
2345            dstBlueIndex = 2;
2346            dstAlphaIndex = 3;
2347            dstLuminanceIndex = dstIntensityIndex = -1;
2348            break;
2349         default:
2350            gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
2351            return;
2352      }
2353
2354
2355      /* Now return the GLubyte data in the requested dstFormat */
2356
2357      if (dstRedIndex >= 0) {
2358         GLubyte *dst = dest;
2359         GLuint i;
2360         for (i = 0; i < n; i++) {
2361            dst[dstRedIndex] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2362            dst += dstComponents;
2363         }
2364      }
2365
2366      if (dstGreenIndex >= 0) {
2367         GLubyte *dst = dest;
2368         GLuint i;
2369         for (i = 0; i < n; i++) {
2370            dst[dstGreenIndex] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2371            dst += dstComponents;
2372         }
2373      }
2374
2375      if (dstBlueIndex >= 0) {
2376         GLubyte *dst = dest;
2377         GLuint i;
2378         for (i = 0; i < n; i++) {
2379            dst[dstBlueIndex] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2380            dst += dstComponents;
2381         }
2382      }
2383
2384      if (dstAlphaIndex >= 0) {
2385         GLubyte *dst = dest;
2386         GLuint i;
2387         for (i = 0; i < n; i++) {
2388            dst[dstAlphaIndex] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2389            dst += dstComponents;
2390         }
2391      }
2392
2393      if (dstIntensityIndex >= 0) {
2394         GLubyte *dst = dest;
2395         GLuint i;
2396         assert(dstIntensityIndex == 0);
2397         assert(dstComponents == 1);
2398         for (i = 0; i < n; i++) {
2399            /* Intensity comes from red channel */
2400            dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2401         }
2402      }
2403
2404      if (dstLuminanceIndex >= 0) {
2405         GLubyte *dst = dest;
2406         GLuint i;
2407         assert(dstLuminanceIndex == 0);
2408         for (i = 0; i < n; i++) {
2409            /* Luminance comes from red channel */
2410            dst[0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2411            dst += dstComponents;
2412         }
2413      }
2414   }
2415}
2416
2417
2418
2419/*
2420 * Unpack a row of color index data from a client buffer according to
2421 * the pixel unpacking parameters.  Apply pixel transfer ops if enabled
2422 * and applyTransferOps is true.
2423 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2424 *
2425 * Args:  ctx - the context
2426 *        n - number of pixels
2427 *        dstType - destination datatype
2428 *        dest - destination array
2429 *        srcType - source pixel type
2430 *        source - source data pointer
2431 *        unpacking - pixel unpacking parameters
2432 *        applyTransferOps - apply offset/bias/lookup ops?
2433 */
2434void
2435_mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
2436                         GLenum dstType, GLvoid *dest,
2437                         GLenum srcType, const GLvoid *source,
2438                         const struct gl_pixelstore_attrib *unpacking,
2439                         GLboolean applyTransferOps )
2440{
2441   ASSERT(srcType == GL_BITMAP ||
2442          srcType == GL_UNSIGNED_BYTE ||
2443          srcType == GL_BYTE ||
2444          srcType == GL_UNSIGNED_SHORT ||
2445          srcType == GL_SHORT ||
2446          srcType == GL_UNSIGNED_INT ||
2447          srcType == GL_INT ||
2448          srcType == GL_FLOAT);
2449
2450   ASSERT(dstType == GL_UNSIGNED_BYTE ||
2451          dstType == GL_UNSIGNED_SHORT ||
2452          dstType == GL_UNSIGNED_INT);
2453
2454   applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
2455
2456   /*
2457    * Try simple cases first
2458    */
2459   if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
2460       && dstType == GL_UNSIGNED_BYTE) {
2461      MEMCPY(dest, source, n * sizeof(GLubyte));
2462   }
2463   else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
2464            && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2465      MEMCPY(dest, source, n * sizeof(GLuint));
2466   }
2467   else {
2468      /*
2469       * general solution
2470       */
2471      GLuint indexes[MAX_WIDTH];
2472      assert(n <= MAX_WIDTH);
2473
2474      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2475                           unpacking);
2476
2477      if (applyTransferOps) {
2478         if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2479            /* shift and offset indexes */
2480            _mesa_shift_and_offset_ci(ctx, n, indexes);
2481         }
2482         if (ctx->Pixel.MapColorFlag) {
2483            /* Apply lookup table */
2484            _mesa_map_ci(ctx, n, indexes);
2485         }
2486      }
2487
2488      /* convert to dest type */
2489      switch (dstType) {
2490         case GL_UNSIGNED_BYTE:
2491            {
2492               GLubyte *dst = (GLubyte *) dest;
2493               GLuint i;
2494               for (i = 0; i < n; i++) {
2495                  dst[i] = (GLubyte) (indexes[i] & 0xff);
2496               }
2497            }
2498            break;
2499         case GL_UNSIGNED_SHORT:
2500            {
2501               GLuint *dst = (GLuint *) dest;
2502               GLuint i;
2503               for (i = 0; i < n; i++) {
2504                  dst[i] = (GLushort) (indexes[i] & 0xffff);
2505               }
2506            }
2507            break;
2508         case GL_UNSIGNED_INT:
2509            MEMCPY(dest, indexes, n * sizeof(GLuint));
2510            break;
2511         default:
2512            gl_problem(ctx, "bad dstType in _mesa_unpack_index_span");
2513      }
2514   }
2515}
2516
2517
2518/*
2519 * Unpack a row of stencil data from a client buffer according to
2520 * the pixel unpacking parameters.  Apply pixel transfer ops if enabled
2521 * and applyTransferOps is true.
2522 * This is (or will be) used by glDrawPixels
2523 *
2524 * Args:  ctx - the context
2525 *        n - number of pixels
2526 *        dstType - destination datatype
2527 *        dest - destination array
2528 *        srcType - source pixel type
2529 *        source - source data pointer
2530 *        unpacking - pixel unpacking parameters
2531 *        applyTransferOps - apply offset/bias/lookup ops?
2532 */
2533void
2534_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
2535                           GLenum dstType, GLvoid *dest,
2536                           GLenum srcType, const GLvoid *source,
2537                           const struct gl_pixelstore_attrib *unpacking,
2538                           GLboolean applyTransferOps )
2539{
2540   ASSERT(srcType == GL_BITMAP ||
2541          srcType == GL_UNSIGNED_BYTE ||
2542          srcType == GL_BYTE ||
2543          srcType == GL_UNSIGNED_SHORT ||
2544          srcType == GL_SHORT ||
2545          srcType == GL_UNSIGNED_INT ||
2546          srcType == GL_INT ||
2547          srcType == GL_FLOAT);
2548
2549   ASSERT(dstType == GL_UNSIGNED_BYTE ||
2550          dstType == GL_UNSIGNED_SHORT ||
2551          dstType == GL_UNSIGNED_INT);
2552
2553   applyTransferOps &= (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset || ctx->Pixel.MapColorFlag);
2554
2555   /*
2556    * Try simple cases first
2557    */
2558   if (!applyTransferOps && srcType == GL_UNSIGNED_BYTE
2559       && dstType == GL_UNSIGNED_BYTE) {
2560      MEMCPY(dest, source, n * sizeof(GLubyte));
2561   }
2562   else if (!applyTransferOps && srcType == GL_UNSIGNED_INT
2563            && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2564      MEMCPY(dest, source, n * sizeof(GLuint));
2565   }
2566   else {
2567      /*
2568       * general solution
2569       */
2570      GLuint indexes[MAX_WIDTH];
2571      assert(n <= MAX_WIDTH);
2572
2573      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2574                           unpacking);
2575
2576      if (applyTransferOps) {
2577         if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
2578            /* shift and offset indexes */
2579            _mesa_shift_and_offset_ci(ctx, n, indexes);
2580         }
2581
2582         if (ctx->Pixel.MapStencilFlag) {
2583            /* Apply stencil lookup table */
2584            GLuint mask = ctx->Pixel.MapStoSsize - 1;
2585            GLuint i;
2586            for (i=0;i<n;i++) {
2587               indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
2588            }
2589         }
2590      }
2591
2592      /* convert to dest type */
2593      switch (dstType) {
2594         case GL_UNSIGNED_BYTE:
2595            {
2596               GLubyte *dst = (GLubyte *) dest;
2597               GLuint i;
2598               for (i = 0; i < n; i++) {
2599                  dst[i] = (GLubyte) (indexes[i] & 0xff);
2600               }
2601            }
2602            break;
2603         case GL_UNSIGNED_SHORT:
2604            {
2605               GLuint *dst = (GLuint *) dest;
2606               GLuint i;
2607               for (i = 0; i < n; i++) {
2608                  dst[i] = (GLushort) (indexes[i] & 0xffff);
2609               }
2610            }
2611            break;
2612         case GL_UNSIGNED_INT:
2613            MEMCPY(dest, indexes, n * sizeof(GLuint));
2614            break;
2615         default:
2616            gl_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
2617      }
2618   }
2619}
2620
2621
2622
2623void
2624_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
2625                         GLenum srcType, const GLvoid *source,
2626                         const struct gl_pixelstore_attrib *unpacking,
2627                         GLboolean applyTransferOps )
2628{
2629   GLfloat *depth = MALLOC(n * sizeof(GLfloat));
2630   if (!depth)
2631      return;
2632
2633   switch (srcType) {
2634      case GL_BYTE:
2635         {
2636            GLuint i;
2637            const GLubyte *src = (const GLubyte *) source;
2638            for (i = 0; i < n; i++) {
2639               depth[i] = BYTE_TO_FLOAT(src[i]);
2640            }
2641         }
2642         break;
2643      case GL_UNSIGNED_BYTE:
2644         {
2645            GLuint i;
2646            const GLubyte *src = (const GLubyte *) source;
2647            for (i = 0; i < n; i++) {
2648               depth[i] = UBYTE_TO_FLOAT(src[i]);
2649            }
2650         }
2651         break;
2652      case GL_SHORT:
2653         {
2654            GLuint i;
2655            const GLshort *src = (const GLshort *) source;
2656            for (i = 0; i < n; i++) {
2657               depth[i] = SHORT_TO_FLOAT(src[i]);
2658            }
2659         }
2660         break;
2661      case GL_UNSIGNED_SHORT:
2662         {
2663            GLuint i;
2664            const GLushort *src = (const GLushort *) source;
2665            for (i = 0; i < n; i++) {
2666               depth[i] = USHORT_TO_FLOAT(src[i]);
2667            }
2668         }
2669         break;
2670      case GL_INT:
2671         {
2672            GLuint i;
2673            const GLint *src = (const GLint *) source;
2674            for (i = 0; i < n; i++) {
2675               depth[i] = INT_TO_FLOAT(src[i]);
2676            }
2677         }
2678         break;
2679      case GL_UNSIGNED_INT:
2680         {
2681            GLuint i;
2682            const GLuint *src = (const GLuint *) source;
2683            for (i = 0; i < n; i++) {
2684               depth[i] = UINT_TO_FLOAT(src[i]);
2685            }
2686         }
2687         break;
2688      case GL_FLOAT:
2689         MEMCPY(depth, source, n * sizeof(GLfloat));
2690         break;
2691      default:
2692         gl_problem(NULL, "bad type in _mesa_unpack_depth_span()");
2693         return;
2694   }
2695
2696
2697   /* apply depth scale and bias */
2698   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
2699      GLuint i;
2700      for (i = 0; i < n; i++) {
2701         depth[i] = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
2702      }
2703   }
2704
2705   /* clamp depth values to [0,1] and convert from floats to integers */
2706   {
2707      const GLfloat zs = ctx->Visual->DepthMaxF;
2708      GLuint i;
2709      for (i = 0; i < n; i++) {
2710         dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
2711      }
2712   }
2713
2714   FREE(depth);
2715}
2716
2717
2718
2719/*
2720 * Unpack image data.  Apply byteswapping, byte flipping (bitmap).
2721 * Return all image data in a contiguous block.
2722 */
2723void *
2724_mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
2725                    GLenum format, GLenum type, const GLvoid *pixels,
2726                    const struct gl_pixelstore_attrib *unpack )
2727{
2728   GLint bytesPerRow, compsPerRow;
2729   GLboolean flipBytes, swap2, swap4;
2730
2731   if (!pixels)
2732      return NULL;  /* not necessarily an error */
2733
2734   if (width <= 0 || height <= 0 || depth <= 0)
2735      return NULL;  /* generate error later */
2736
2737   if (format == GL_BITMAP) {
2738      bytesPerRow = (width + 7) >> 3;
2739      flipBytes = !unpack->LsbFirst;
2740      swap2 = swap4 = GL_FALSE;
2741      compsPerRow = 0;
2742   }
2743   else {
2744      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
2745      const GLint components = _mesa_components_in_format(format);
2746      GLint bytesPerComp;
2747      if (bytesPerPixel <= 0 || components <= 0)
2748         return NULL;   /* bad format or type.  generate error later */
2749      bytesPerRow = bytesPerPixel * width;
2750      bytesPerComp = bytesPerPixel / components;
2751      flipBytes = GL_FALSE;
2752      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
2753      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
2754      compsPerRow = components * width;
2755      assert(compsPerRow >= width);
2756   }
2757
2758   {
2759      GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
2760      GLubyte *dst;
2761      GLint img, row;
2762      if (!destBuffer)
2763         return NULL;   /* generate GL_OUT_OF_MEMORY later */
2764
2765      dst = destBuffer;
2766      for (img = 0; img < depth; img++) {
2767         for (row = 0; row < height; row++) {
2768            const GLvoid *src = _mesa_image_address(unpack, pixels,
2769                               width, height, format, type, img, row, 0);
2770            MEMCPY(dst, src, bytesPerRow);
2771            /* byte flipping/swapping */
2772            if (flipBytes) {
2773               flip_bytes((GLubyte *) dst, bytesPerRow);
2774            }
2775            else if (swap2) {
2776               _mesa_swap2((GLushort*) dst, compsPerRow);
2777            }
2778            else if (swap4) {
2779               _mesa_swap4((GLuint*) dst, compsPerRow);
2780            }
2781            dst += bytesPerRow;
2782         }
2783      }
2784      return destBuffer;
2785   }
2786}
2787
2788
2789/*
2790 * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
2791 * order with row alignment = 1 byte.
2792 */
2793GLvoid *
2794_mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
2795                     const struct gl_pixelstore_attrib *packing )
2796{
2797   GLint bytes, row, width_in_bytes;
2798   GLubyte *buffer, *dst;
2799
2800   if (!pixels)
2801      return NULL;
2802
2803   /* Alloc dest storage */
2804   bytes = ((width + 7) / 8 * height);
2805   buffer = (GLubyte *) MALLOC( bytes );
2806   if (!buffer)
2807      return NULL;
2808
2809
2810   width_in_bytes = CEILING( width, 8 );
2811   dst = buffer;
2812   for (row = 0; row < height; row++) {
2813      GLubyte *src = _mesa_image_address( packing, pixels, width, height,
2814                                          GL_COLOR_INDEX, GL_BITMAP,
2815                                          0, row, 0 );
2816      if (!src) {
2817         FREE(buffer);
2818         return NULL;
2819      }
2820
2821      if (packing->SkipPixels == 0) {
2822         MEMCPY( dst, src, width_in_bytes );
2823         if (packing->LsbFirst) {
2824            flip_bytes( dst, width_in_bytes );
2825         }
2826      }
2827      else {
2828         /* handling SkipPixels is a bit tricky (no pun intended!) */
2829         GLint i;
2830         if (packing->LsbFirst) {
2831            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
2832            GLubyte dstMask = 128;
2833            GLubyte *s = src;
2834            GLubyte *d = dst;
2835            *d = 0;
2836            for (i = 0; i < width; i++) {
2837               if (*s & srcMask) {
2838                  *d |= dstMask;
2839               }
2840               if (srcMask == 128) {
2841                  srcMask = 1;
2842                  s++;
2843               }
2844               else {
2845                  srcMask = srcMask << 1;
2846               }
2847               if (dstMask == 1) {
2848                  dstMask = 128;
2849                  d++;
2850                  *d = 0;
2851               }
2852               else {
2853                  dstMask = dstMask >> 1;
2854               }
2855            }
2856         }
2857         else {
2858            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2859            GLubyte dstMask = 128;
2860            GLubyte *s = src;
2861            GLubyte *d = dst;
2862            *d = 0;
2863            for (i = 0; i < width; i++) {
2864               if (*s & srcMask) {
2865                  *d |= dstMask;
2866               }
2867               if (srcMask == 1) {
2868                  srcMask = 128;
2869                  s++;
2870               }
2871               else {
2872                  srcMask = srcMask >> 1;
2873               }
2874               if (dstMask == 1) {
2875                  dstMask = 128;
2876                  d++;
2877                  *d = 0;
2878               }
2879               else {
2880                  dstMask = dstMask >> 1;
2881               }
2882            }
2883         }
2884      }
2885      dst += width_in_bytes;
2886   }
2887
2888   return buffer;
2889}
2890
2891
2892/*
2893 * Pack bitmap data.
2894 */
2895void
2896_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
2897                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
2898{
2899   GLint row, width_in_bytes;
2900   const GLubyte *src;
2901
2902   if (!source)
2903      return;
2904
2905   width_in_bytes = CEILING( width, 8 );
2906   src = source;
2907   for (row = 0; row < height; row++) {
2908      GLubyte *dst = _mesa_image_address( packing, dest, width, height,
2909                                          GL_COLOR_INDEX, GL_BITMAP,
2910                                          0, row, 0 );
2911      if (!dst)
2912         return;
2913
2914      if (packing->SkipPixels == 0) {
2915         MEMCPY( dst, src, width_in_bytes );
2916         if (packing->LsbFirst) {
2917            flip_bytes( dst, width_in_bytes );
2918         }
2919      }
2920      else {
2921         /* handling SkipPixels is a bit tricky (no pun intended!) */
2922         GLint i;
2923         if (packing->LsbFirst) {
2924            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
2925            GLubyte dstMask = 128;
2926            const GLubyte *s = src;
2927            GLubyte *d = dst;
2928            *d = 0;
2929            for (i = 0; i < width; i++) {
2930               if (*s & srcMask) {
2931                  *d |= dstMask;
2932               }
2933               if (srcMask == 128) {
2934                  srcMask = 1;
2935                  s++;
2936               }
2937               else {
2938                  srcMask = srcMask << 1;
2939               }
2940               if (dstMask == 1) {
2941                  dstMask = 128;
2942                  d++;
2943                  *d = 0;
2944               }
2945               else {
2946                  dstMask = dstMask >> 1;
2947               }
2948            }
2949         }
2950         else {
2951            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
2952            GLubyte dstMask = 128;
2953            const GLubyte *s = src;
2954            GLubyte *d = dst;
2955            *d = 0;
2956            for (i = 0; i < width; i++) {
2957               if (*s & srcMask) {
2958                  *d |= dstMask;
2959               }
2960               if (srcMask == 1) {
2961                  srcMask = 128;
2962                  s++;
2963               }
2964               else {
2965                  srcMask = srcMask >> 1;
2966               }
2967               if (dstMask == 1) {
2968                  dstMask = 128;
2969                  d++;
2970                  *d = 0;
2971               }
2972               else {
2973                  dstMask = dstMask >> 1;
2974               }
2975            }
2976         }
2977      }
2978      src += width_in_bytes;
2979   }
2980}
2981