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