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