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