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