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