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