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