image.c revision 08836341788a9f9d638d9dc8328510ccd18ddeb5
1/* $Id: image.c,v 1.57 2001/03/03 20:33:27 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      GLubyte *src = _mesa_image_address( packing, pixels, width, height,
624                                          GL_COLOR_INDEX, GL_BITMAP,
625                                          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            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            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 = _mesa_image_address( packing, dest, width, height,
719                                          GL_COLOR_INDEX, GL_BITMAP,
720                                          0, row, 0 );
721      if (!dst)
722         return;
723
724      if (packing->SkipPixels == 0) {
725         MEMCPY( dst, src, width_in_bytes );
726         if (packing->LsbFirst) {
727            flip_bytes( dst, width_in_bytes );
728         }
729      }
730      else {
731         /* handling SkipPixels is a bit tricky (no pun intended!) */
732         GLint i;
733         if (packing->LsbFirst) {
734            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
735            GLubyte dstMask = 128;
736            const GLubyte *s = src;
737            GLubyte *d = dst;
738            *d = 0;
739            for (i = 0; i < width; i++) {
740               if (*s & srcMask) {
741                  *d |= dstMask;
742               }
743               if (srcMask == 128) {
744                  srcMask = 1;
745                  s++;
746               }
747               else {
748                  srcMask = srcMask << 1;
749               }
750               if (dstMask == 1) {
751                  dstMask = 128;
752                  d++;
753                  *d = 0;
754               }
755               else {
756                  dstMask = dstMask >> 1;
757               }
758            }
759         }
760         else {
761            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
762            GLubyte dstMask = 128;
763            const GLubyte *s = src;
764            GLubyte *d = dst;
765            *d = 0;
766            for (i = 0; i < width; i++) {
767               if (*s & srcMask) {
768                  *d |= dstMask;
769               }
770               if (srcMask == 1) {
771                  srcMask = 128;
772                  s++;
773               }
774               else {
775                  srcMask = srcMask >> 1;
776               }
777               if (dstMask == 1) {
778                  dstMask = 128;
779                  d++;
780                  *d = 0;
781               }
782               else {
783                  dstMask = dstMask >> 1;
784               }
785            }
786         }
787      }
788      src += width_in_bytes;
789   }
790}
791
792
793
794void
795_mesa_pack_float_rgba_span( GLcontext *ctx,
796                            GLuint n, CONST GLfloat rgbaIn[][4],
797                            GLenum dstFormat, GLenum dstType,
798                            GLvoid *dstAddr,
799                            const struct gl_pixelstore_attrib *dstPacking,
800                            GLuint transferOps )
801{
802   const GLint comps = _mesa_components_in_format(dstFormat);
803   GLfloat luminance[MAX_WIDTH];
804   GLfloat (*rgba)[4];
805   GLuint i;
806
807   if (transferOps) {
808      /* make copy of incoming data */
809      GLfloat rgbaCopy[MAX_WIDTH][4];
810      for (i = 0; i < n; i++) {
811         rgbaCopy[i][0] = rgbaIn[i][0];
812         rgbaCopy[i][1] = rgbaIn[i][1];
813         rgbaCopy[i][2] = rgbaIn[i][2];
814         rgbaCopy[i][3] = rgbaIn[i][3];
815      }
816
817      rgba = (GLfloat (*)[4]) rgbaCopy;
818
819      /* scale & bias */
820      if (transferOps & IMAGE_SCALE_BIAS_BIT) {
821         _mesa_scale_and_bias_rgba(ctx, n, rgba,
822                                   ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
823                                   ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
824                                   ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
825                                   ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
826      }
827      /* color map lookup */
828      if (transferOps & IMAGE_MAP_COLOR_BIT) {
829         _mesa_map_rgba( ctx, n, rgba );
830      }
831      /* GL_COLOR_TABLE lookup */
832      if (transferOps & IMAGE_COLOR_TABLE_BIT) {
833         _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
834      }
835      /* convolution */
836      if (transferOps & IMAGE_CONVOLUTION_BIT) {
837         /* this has to be done in the calling code */
838      }
839      /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
840      if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
841         _mesa_scale_and_bias_rgba(ctx, n, rgba,
842                                   ctx->Pixel.PostConvolutionScale[RCOMP],
843                                   ctx->Pixel.PostConvolutionScale[GCOMP],
844                                   ctx->Pixel.PostConvolutionScale[BCOMP],
845                                   ctx->Pixel.PostConvolutionScale[ACOMP],
846                                   ctx->Pixel.PostConvolutionBias[RCOMP],
847                                   ctx->Pixel.PostConvolutionBias[GCOMP],
848                                   ctx->Pixel.PostConvolutionBias[BCOMP],
849                                   ctx->Pixel.PostConvolutionBias[ACOMP]);
850      }
851      /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
852      if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
853         _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
854      }
855      /* color matrix transform */
856      if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
857         _mesa_transform_rgba(ctx, n, rgba);
858      }
859      /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
860      if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
861         _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
862      }
863      /* update histogram count */
864      if (transferOps & IMAGE_HISTOGRAM_BIT) {
865         _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
866      }
867      /* min/max here */
868      if (transferOps & IMAGE_MIN_MAX_BIT) {
869         _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
870         if (ctx->MinMax.Sink)
871            return;
872      }
873   }
874   else {
875      /* use incoming data, not a copy */
876      rgba = (GLfloat (*)[4]) rgbaIn;
877   }
878
879   /* XXX clamp rgba to [0,1]? */
880
881
882   if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
883      for (i = 0; i < n; i++) {
884         GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
885         luminance[i] = CLAMP(sum, 0.0F, 1.0F);
886      }
887   }
888
889   /*
890    * Pack/store the pixels.  Ugh!  Lots of cases!!!
891    */
892   switch (dstType) {
893      case GL_UNSIGNED_BYTE:
894         {
895            GLubyte *dst = (GLubyte *) dstAddr;
896            switch (dstFormat) {
897               case GL_RED:
898                  for (i=0;i<n;i++)
899                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
900                  break;
901               case GL_GREEN:
902                  for (i=0;i<n;i++)
903                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
904                  break;
905               case GL_BLUE:
906                  for (i=0;i<n;i++)
907                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
908                  break;
909               case GL_ALPHA:
910                  for (i=0;i<n;i++)
911                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
912                  break;
913               case GL_LUMINANCE:
914                  for (i=0;i<n;i++)
915                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
916                  break;
917               case GL_LUMINANCE_ALPHA:
918                  for (i=0;i<n;i++) {
919                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
920                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
921                  }
922                  break;
923               case GL_RGB:
924                  for (i=0;i<n;i++) {
925                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
926                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
927                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
928                  }
929                  break;
930               case GL_RGBA:
931                  for (i=0;i<n;i++) {
932                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
933                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
934                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
935                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
936                  }
937                  break;
938               case GL_BGR:
939                  for (i=0;i<n;i++) {
940                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
941                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
942                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
943                  }
944                  break;
945               case GL_BGRA:
946                  for (i=0;i<n;i++) {
947                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
948                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
949                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
950                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
951                  }
952                  break;
953               case GL_ABGR_EXT:
954                  for (i=0;i<n;i++) {
955                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
956                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
957                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
958                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
959                  }
960                  break;
961               default:
962                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
963            }
964         }
965         break;
966      case GL_BYTE:
967         {
968            GLbyte *dst = (GLbyte *) dstAddr;
969            switch (dstFormat) {
970               case GL_RED:
971                  for (i=0;i<n;i++)
972                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
973                  break;
974               case GL_GREEN:
975                  for (i=0;i<n;i++)
976                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
977                  break;
978               case GL_BLUE:
979                  for (i=0;i<n;i++)
980                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
981                  break;
982               case GL_ALPHA:
983                  for (i=0;i<n;i++)
984                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
985                  break;
986               case GL_LUMINANCE:
987                  for (i=0;i<n;i++)
988                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
989                  break;
990               case GL_LUMINANCE_ALPHA:
991                  for (i=0;i<n;i++) {
992                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
993                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
994                  }
995                  break;
996               case GL_RGB:
997                  for (i=0;i<n;i++) {
998                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
999                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1000                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1001                  }
1002                  break;
1003               case GL_RGBA:
1004                  for (i=0;i<n;i++) {
1005                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1006                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1007                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1008                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1009                  }
1010                  break;
1011               case GL_BGR:
1012                  for (i=0;i<n;i++) {
1013                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1014                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1015                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1016                  }
1017                  break;
1018               case GL_BGRA:
1019                  for (i=0;i<n;i++) {
1020                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1021                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1022                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1023                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1024                  }
1025               case GL_ABGR_EXT:
1026                  for (i=0;i<n;i++) {
1027                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1028                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1029                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1030                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1031                  }
1032                  break;
1033               default:
1034                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1035            }
1036         }
1037         break;
1038      case GL_UNSIGNED_SHORT:
1039         {
1040            GLushort *dst = (GLushort *) dstAddr;
1041            switch (dstFormat) {
1042               case GL_RED:
1043                  for (i=0;i<n;i++)
1044                     dst[i] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1045                  break;
1046               case GL_GREEN:
1047                  for (i=0;i<n;i++)
1048                     dst[i] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1049                  break;
1050               case GL_BLUE:
1051                  for (i=0;i<n;i++)
1052                     dst[i] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
1053                  break;
1054               case GL_ALPHA:
1055                  for (i=0;i<n;i++)
1056                     dst[i] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
1057                  break;
1058               case GL_LUMINANCE:
1059                  for (i=0;i<n;i++)
1060                     dst[i] = FLOAT_TO_USHORT(luminance[i]);
1061                  break;
1062               case GL_LUMINANCE_ALPHA:
1063                  for (i=0;i<n;i++) {
1064                     dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
1065                     dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
1066                  }
1067                  break;
1068               case GL_RGB:
1069                  for (i=0;i<n;i++) {
1070                     dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1071                     dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1072                     dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
1073                  }
1074                  break;
1075               case GL_RGBA:
1076                  for (i=0;i<n;i++) {
1077                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1078                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1079                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
1080                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
1081                  }
1082                  break;
1083               case GL_BGR:
1084                  for (i=0;i<n;i++) {
1085                     dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
1086                     dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1087                     dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1088                  }
1089                  break;
1090               case GL_BGRA:
1091                  for (i=0;i<n;i++) {
1092                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
1093                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1094                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1095                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
1096                  }
1097                  break;
1098               case GL_ABGR_EXT:
1099                  for (i=0;i<n;i++) {
1100                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
1101                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
1102                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
1103                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
1104                  }
1105                  break;
1106               default:
1107                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1108            }
1109            if (dstPacking->SwapBytes) {
1110               _mesa_swap2( (GLushort *) dst, n * comps);
1111            }
1112         }
1113         break;
1114      case GL_SHORT:
1115         {
1116            GLshort *dst = (GLshort *) dstAddr;
1117            switch (dstFormat) {
1118               case GL_RED:
1119                  for (i=0;i<n;i++)
1120                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1121                  break;
1122               case GL_GREEN:
1123                  for (i=0;i<n;i++)
1124                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1125                  break;
1126               case GL_BLUE:
1127                  for (i=0;i<n;i++)
1128                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1129                  break;
1130               case GL_ALPHA:
1131                  for (i=0;i<n;i++)
1132                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1133                  break;
1134               case GL_LUMINANCE:
1135                  for (i=0;i<n;i++)
1136                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
1137                  break;
1138               case GL_LUMINANCE_ALPHA:
1139                  for (i=0;i<n;i++) {
1140                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1141                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1142                  }
1143                  break;
1144               case GL_RGB:
1145                  for (i=0;i<n;i++) {
1146                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1147                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1148                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1149                  }
1150                  break;
1151               case GL_RGBA:
1152                  for (i=0;i<n;i++) {
1153                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1154                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1155                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1156                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1157                  }
1158                  break;
1159               case GL_BGR:
1160                  for (i=0;i<n;i++) {
1161                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1162                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1163                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1164                  }
1165                  break;
1166               case GL_BGRA:
1167                  for (i=0;i<n;i++) {
1168                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1169                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1170                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1171                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1172                  }
1173               case GL_ABGR_EXT:
1174                  for (i=0;i<n;i++) {
1175                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1176                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1177                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1178                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1179                  }
1180                  break;
1181               default:
1182                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1183            }
1184            if (dstPacking->SwapBytes) {
1185               _mesa_swap2( (GLushort *) dst, n * comps );
1186            }
1187         }
1188         break;
1189      case GL_UNSIGNED_INT:
1190         {
1191            GLuint *dst = (GLuint *) dstAddr;
1192            switch (dstFormat) {
1193               case GL_RED:
1194                  for (i=0;i<n;i++)
1195                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1196                  break;
1197               case GL_GREEN:
1198                  for (i=0;i<n;i++)
1199                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1200                  break;
1201               case GL_BLUE:
1202                  for (i=0;i<n;i++)
1203                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1204                  break;
1205               case GL_ALPHA:
1206                  for (i=0;i<n;i++)
1207                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1208                  break;
1209               case GL_LUMINANCE:
1210                  for (i=0;i<n;i++)
1211                     dst[i] = FLOAT_TO_UINT(luminance[i]);
1212                  break;
1213               case GL_LUMINANCE_ALPHA:
1214                  for (i=0;i<n;i++) {
1215                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
1216                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1217                  }
1218                  break;
1219               case GL_RGB:
1220                  for (i=0;i<n;i++) {
1221                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1222                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1223                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1224                  }
1225                  break;
1226               case GL_RGBA:
1227                  for (i=0;i<n;i++) {
1228                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1229                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1230                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1231                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1232                  }
1233                  break;
1234               case GL_BGR:
1235                  for (i=0;i<n;i++) {
1236                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1237                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1238                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1239                  }
1240                  break;
1241               case GL_BGRA:
1242                  for (i=0;i<n;i++) {
1243                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1244                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1245                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1246                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1247                  }
1248                  break;
1249               case GL_ABGR_EXT:
1250                  for (i=0;i<n;i++) {
1251                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1252                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1253                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1254                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1255                  }
1256                  break;
1257               default:
1258                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1259            }
1260            if (dstPacking->SwapBytes) {
1261               _mesa_swap4( (GLuint *) dst, n * comps );
1262            }
1263         }
1264         break;
1265      case GL_INT:
1266         {
1267            GLint *dst = (GLint *) dstAddr;
1268            switch (dstFormat) {
1269               case GL_RED:
1270                  for (i=0;i<n;i++)
1271                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
1272                  break;
1273               case GL_GREEN:
1274                  for (i=0;i<n;i++)
1275                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
1276                  break;
1277               case GL_BLUE:
1278                  for (i=0;i<n;i++)
1279                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
1280                  break;
1281               case GL_ALPHA:
1282                  for (i=0;i<n;i++)
1283                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
1284                  break;
1285               case GL_LUMINANCE:
1286                  for (i=0;i<n;i++)
1287                     dst[i] = FLOAT_TO_INT(luminance[i]);
1288                  break;
1289               case GL_LUMINANCE_ALPHA:
1290                  for (i=0;i<n;i++) {
1291                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
1292                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
1293                  }
1294                  break;
1295               case GL_RGB:
1296                  for (i=0;i<n;i++) {
1297                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1298                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1299                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1300                  }
1301                  break;
1302               case GL_RGBA:
1303                  for (i=0;i<n;i++) {
1304                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1305                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1306                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1307                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1308                  }
1309                  break;
1310               case GL_BGR:
1311                  for (i=0;i<n;i++) {
1312                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1313                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1314                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1315                  }
1316                  break;
1317               case GL_BGRA:
1318                  for (i=0;i<n;i++) {
1319                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1320                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1321                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1322                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1323                  }
1324                  break;
1325               case GL_ABGR_EXT:
1326                  for (i=0;i<n;i++) {
1327                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
1328                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
1329                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
1330                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
1331                  }
1332                  break;
1333               default:
1334                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1335            }
1336            if (dstPacking->SwapBytes) {
1337               _mesa_swap4( (GLuint *) dst, n * comps );
1338            }
1339         }
1340         break;
1341      case GL_FLOAT:
1342         {
1343            GLfloat *dst = (GLfloat *) dstAddr;
1344            switch (dstFormat) {
1345               case GL_RED:
1346                  for (i=0;i<n;i++)
1347                     dst[i] = rgba[i][RCOMP];
1348                  break;
1349               case GL_GREEN:
1350                  for (i=0;i<n;i++)
1351                     dst[i] = rgba[i][GCOMP];
1352                  break;
1353               case GL_BLUE:
1354                  for (i=0;i<n;i++)
1355                     dst[i] = rgba[i][BCOMP];
1356                  break;
1357               case GL_ALPHA:
1358                  for (i=0;i<n;i++)
1359                     dst[i] = rgba[i][ACOMP];
1360                  break;
1361               case GL_LUMINANCE:
1362                  for (i=0;i<n;i++)
1363                     dst[i] = luminance[i];
1364                  break;
1365               case GL_LUMINANCE_ALPHA:
1366                  for (i=0;i<n;i++) {
1367                     dst[i*2+0] = luminance[i];
1368                     dst[i*2+1] = rgba[i][ACOMP];
1369                  }
1370                  break;
1371               case GL_RGB:
1372                  for (i=0;i<n;i++) {
1373                     dst[i*3+0] = rgba[i][RCOMP];
1374                     dst[i*3+1] = rgba[i][GCOMP];
1375                     dst[i*3+2] = rgba[i][BCOMP];
1376                  }
1377                  break;
1378               case GL_RGBA:
1379                  for (i=0;i<n;i++) {
1380                     dst[i*4+0] = rgba[i][RCOMP];
1381                     dst[i*4+1] = rgba[i][GCOMP];
1382                     dst[i*4+2] = rgba[i][BCOMP];
1383                     dst[i*4+3] = rgba[i][ACOMP];
1384                  }
1385                  break;
1386               case GL_BGR:
1387                  for (i=0;i<n;i++) {
1388                     dst[i*3+0] = rgba[i][BCOMP];
1389                     dst[i*3+1] = rgba[i][GCOMP];
1390                     dst[i*3+2] = rgba[i][RCOMP];
1391                  }
1392                  break;
1393               case GL_BGRA:
1394                  for (i=0;i<n;i++) {
1395                     dst[i*4+0] = rgba[i][BCOMP];
1396                     dst[i*4+1] = rgba[i][GCOMP];
1397                     dst[i*4+2] = rgba[i][RCOMP];
1398                     dst[i*4+3] = rgba[i][ACOMP];
1399                  }
1400                  break;
1401               case GL_ABGR_EXT:
1402                  for (i=0;i<n;i++) {
1403                     dst[i*4+0] = rgba[i][ACOMP];
1404                     dst[i*4+1] = rgba[i][BCOMP];
1405                     dst[i*4+2] = rgba[i][GCOMP];
1406                     dst[i*4+3] = rgba[i][RCOMP];
1407                  }
1408                  break;
1409               default:
1410                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1411            }
1412            if (dstPacking->SwapBytes) {
1413               _mesa_swap4( (GLuint *) dst, n * comps );
1414            }
1415         }
1416         break;
1417      case GL_UNSIGNED_BYTE_3_3_2:
1418         if (dstFormat == GL_RGB) {
1419            GLubyte *dst = (GLubyte *) dstAddr;
1420            for (i=0;i<n;i++) {
1421               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F)) << 5)
1422                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 2)
1423                      | (((GLint) (rgba[i][BCOMP] * 3.0F))     );
1424            }
1425         }
1426         break;
1427      case GL_UNSIGNED_BYTE_2_3_3_REV:
1428         if (dstFormat == GL_RGB) {
1429            GLubyte *dst = (GLubyte *) dstAddr;
1430            for (i=0;i<n;i++) {
1431               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F))     )
1432                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 3)
1433                      | (((GLint) (rgba[i][BCOMP] * 3.0F)) << 5);
1434            }
1435         }
1436         break;
1437      case GL_UNSIGNED_SHORT_5_6_5:
1438         if (dstFormat == GL_RGB) {
1439            GLushort *dst = (GLushort *) dstAddr;
1440            for (i=0;i<n;i++) {
1441               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1442                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1443                      | (((GLint) (rgba[i][BCOMP] * 31.0F))      );
1444            }
1445         }
1446         break;
1447      case GL_UNSIGNED_SHORT_5_6_5_REV:
1448         if (dstFormat == GL_RGB) {
1449            GLushort *dst = (GLushort *) dstAddr;
1450            for (i=0;i<n;i++) {
1451               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1452                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1453                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11);
1454            }
1455         }
1456         break;
1457      case GL_UNSIGNED_SHORT_4_4_4_4:
1458         if (dstFormat == GL_RGBA) {
1459            GLushort *dst = (GLushort *) dstAddr;
1460            for (i=0;i<n;i++) {
1461               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12)
1462                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1463                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
1464                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1465            }
1466         }
1467         else if (dstFormat == GL_BGRA) {
1468            GLushort *dst = (GLushort *) dstAddr;
1469            for (i=0;i<n;i++) {
1470               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F)) << 12)
1471                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1472                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  4)
1473                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1474            }
1475         }
1476         else if (dstFormat == GL_ABGR_EXT) {
1477            GLushort *dst = (GLushort *) dstAddr;
1478            for (i=0;i<n;i++) {
1479               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F)) <<  4)
1480                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
1481                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) << 12)
1482                      | (((GLint) (rgba[i][RCOMP] * 15.0F))      );
1483            }
1484         }
1485         break;
1486      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1487         if (dstFormat == GL_RGBA) {
1488            GLushort *dst = (GLushort *) dstAddr;
1489            for (i=0;i<n;i++) {
1490               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F))      )
1491                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1492                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
1493                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1494            }
1495         }
1496         else if (dstFormat == GL_BGRA) {
1497            GLushort *dst = (GLushort *) dstAddr;
1498            for (i=0;i<n;i++) {
1499               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F))      )
1500                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1501                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  8)
1502                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1503            }
1504         }
1505         else if (dstFormat == GL_ABGR_EXT) {
1506            GLushort *dst = (GLushort *) dstAddr;
1507            for (i=0;i<n;i++) {
1508               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F))      )
1509                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
1510                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1511                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12);
1512            }
1513         }
1514         break;
1515      case GL_UNSIGNED_SHORT_5_5_5_1:
1516         if (dstFormat == GL_RGBA) {
1517            GLushort *dst = (GLushort *) dstAddr;
1518            for (i=0;i<n;i++) {
1519               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1520                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1521                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  1)
1522                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1523            }
1524         }
1525         else if (dstFormat == GL_BGRA) {
1526            GLushort *dst = (GLushort *) dstAddr;
1527            for (i=0;i<n;i++) {
1528               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11)
1529                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1530                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) <<  1)
1531                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1532            }
1533         }
1534         else if (dstFormat == GL_ABGR_EXT) {
1535            GLushort *dst = (GLushort *) dstAddr;
1536            for (i=0;i<n;i++) {
1537               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F)) << 11)
1538                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  6)
1539                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  1)
1540                      | (((GLint) (rgba[i][RCOMP] *  1.0F))      );
1541            }
1542         }
1543         break;
1544      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1545         if (dstFormat == GL_RGBA) {
1546            GLushort *dst = (GLushort *) dstAddr;
1547            for (i=0;i<n;i++) {
1548               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1549                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1550                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 10)
1551                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1552            }
1553         }
1554         else if (dstFormat == GL_BGRA) {
1555            GLushort *dst = (GLushort *) dstAddr;
1556            for (i=0;i<n;i++) {
1557               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F))      )
1558                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1559                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) << 10)
1560                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1561            }
1562         }
1563         else if (dstFormat == GL_ABGR_EXT) {
1564            GLushort *dst = (GLushort *) dstAddr;
1565            for (i=0;i<n;i++) {
1566               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F))      )
1567                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  5)
1568                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) << 10)
1569                      | (((GLint) (rgba[i][RCOMP] *  1.0F)) << 15);
1570            }
1571         }
1572         break;
1573      case GL_UNSIGNED_INT_8_8_8_8:
1574         if (dstFormat == GL_RGBA) {
1575            GLuint *dst = (GLuint *) dstAddr;
1576            for (i=0;i<n;i++) {
1577               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24)
1578                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1579                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
1580                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1581            }
1582         }
1583         else if (dstFormat == GL_BGRA) {
1584            GLuint *dst = (GLuint *) dstAddr;
1585            for (i=0;i<n;i++) {
1586               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 24)
1587                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1588                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) <<  8)
1589                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1590            }
1591         }
1592         else if (dstFormat == GL_ABGR_EXT) {
1593            GLuint *dst = (GLuint *) dstAddr;
1594            for (i=0;i<n;i++) {
1595               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24)
1596                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
1597                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1598                      | (((GLuint) (rgba[i][RCOMP] * 255.0F))      );
1599            }
1600         }
1601         break;
1602      case GL_UNSIGNED_INT_8_8_8_8_REV:
1603         if (dstFormat == GL_RGBA) {
1604            GLuint *dst = (GLuint *) dstAddr;
1605            for (i=0;i<n;i++) {
1606               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F))      )
1607                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1608                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
1609                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1610            }
1611         }
1612         else if (dstFormat == GL_BGRA) {
1613            GLuint *dst = (GLuint *) dstAddr;
1614            for (i=0;i<n;i++) {
1615               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F))      )
1616                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1617                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 16)
1618                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1619            }
1620         }
1621         else if (dstFormat == GL_ABGR_EXT) {
1622            GLuint *dst = (GLuint *) dstAddr;
1623            for (i=0;i<n;i++) {
1624               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F))      )
1625                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
1626                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1627                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24);
1628            }
1629         }
1630         break;
1631      case GL_UNSIGNED_INT_10_10_10_2:
1632         if (dstFormat == GL_RGBA) {
1633            GLuint *dst = (GLuint *) dstAddr;
1634            for (i=0;i<n;i++) {
1635               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 22)
1636                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
1637                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) <<  2)
1638                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
1639            }
1640         }
1641         else if (dstFormat == GL_BGRA) {
1642            GLuint *dst = (GLuint *) dstAddr;
1643            for (i=0;i<n;i++) {
1644               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 22)
1645                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
1646                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) <<  2)
1647                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
1648            }
1649         }
1650         else if (dstFormat == GL_ABGR_EXT) {
1651            GLuint *dst = (GLuint *) dstAddr;
1652            for (i=0;i<n;i++) {
1653               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F)) << 22)
1654                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 12)
1655                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) <<  2)
1656                      | (((GLuint) (rgba[i][RCOMP] *    3.0F))      );
1657            }
1658         }
1659         break;
1660      case GL_UNSIGNED_INT_2_10_10_10_REV:
1661         if (dstFormat == GL_RGBA) {
1662            GLuint *dst = (GLuint *) dstAddr;
1663            for (i=0;i<n;i++) {
1664               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F))      )
1665                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
1666                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 20)
1667                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
1668            }
1669         }
1670         else if (dstFormat == GL_BGRA) {
1671            GLuint *dst = (GLuint *) dstAddr;
1672            for (i=0;i<n;i++) {
1673               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F))      )
1674                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
1675                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 20)
1676                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
1677            }
1678         }
1679         else if (dstFormat == GL_ABGR_EXT) {
1680            GLuint *dst = (GLuint *) dstAddr;
1681            for (i=0;i<n;i++) {
1682               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F))      )
1683                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 10)
1684                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 20)
1685                      | (((GLuint) (rgba[i][RCOMP] *    3.0F)) << 30);
1686            }
1687         }
1688         break;
1689      default:
1690         _mesa_problem(ctx, "bad type in _mesa_pack_float_rgba_span");
1691   }
1692}
1693
1694
1695
1696/*
1697 * Pack the given RGBA span into client memory at 'dest' address
1698 * in the given pixel format and type.
1699 * Optionally apply the enabled pixel transfer ops.
1700 * Pack into memory using the given packing params struct.
1701 * This is used by glReadPixels and glGetTexImage?D()
1702 * Input:  ctx - the context
1703 *         n - number of pixels in the span
1704 *         rgba - the pixels
1705 *         format - dest packing format
1706 *         type - dest packing datatype
1707 *         destination - destination packing address
1708 *         packing - pixel packing parameters
1709 *         transferOps - bitmask of IMAGE_*_BIT operations to apply
1710 */
1711void
1712_mesa_pack_rgba_span( GLcontext *ctx,
1713                      GLuint n, CONST GLchan srcRgba[][4],
1714                      GLenum dstFormat, GLenum dstType,
1715                      GLvoid *dstAddr,
1716                      const struct gl_pixelstore_attrib *dstPacking,
1717                      GLuint transferOps)
1718{
1719   ASSERT((ctx->NewState & _NEW_PIXEL) == 0);
1720
1721   /* Test for optimized case first */
1722   if (transferOps == 0 && dstFormat == GL_RGBA && dstType == CHAN_TYPE) {
1723      /* common simple case */
1724      MEMCPY(dstAddr, srcRgba, n * 4 * sizeof(GLchan));
1725   }
1726   else if (transferOps == 0 && dstFormat == GL_RGB && dstType == CHAN_TYPE) {
1727      /* common simple case */
1728      GLint i;
1729      GLchan *dest = (GLchan *) dstAddr;
1730      for (i = 0; i < n; i++) {
1731         dest[0] = srcRgba[i][RCOMP];
1732         dest[1] = srcRgba[i][GCOMP];
1733         dest[2] = srcRgba[i][BCOMP];
1734         dest += 3;
1735      }
1736   }
1737   else {
1738      /* general solution */
1739      GLfloat rgba[MAX_WIDTH][4];
1740      GLuint i;
1741      assert(n <= MAX_WIDTH);
1742      /* convert color components to floating point */
1743      for (i=0;i<n;i++) {
1744         rgba[i][RCOMP] = CHAN_TO_FLOAT(srcRgba[i][RCOMP]);
1745         rgba[i][GCOMP] = CHAN_TO_FLOAT(srcRgba[i][GCOMP]);
1746         rgba[i][BCOMP] = CHAN_TO_FLOAT(srcRgba[i][BCOMP]);
1747         rgba[i][ACOMP] = CHAN_TO_FLOAT(srcRgba[i][ACOMP]);
1748      }
1749      _mesa_pack_float_rgba_span(ctx, n, (const GLfloat (*)[4]) rgba,
1750                                 dstFormat, dstType, dstAddr,
1751                                 dstPacking, transferOps);
1752   }
1753}
1754
1755
1756#define SWAP2BYTE(VALUE)			\
1757   {						\
1758      GLubyte *bytes = (GLubyte *) &(VALUE);	\
1759      GLubyte tmp = bytes[0];			\
1760      bytes[0] = bytes[1];			\
1761      bytes[1] = tmp;				\
1762   }
1763
1764#define SWAP4BYTE(VALUE)			\
1765   {						\
1766      GLubyte *bytes = (GLubyte *) &(VALUE);	\
1767      GLubyte tmp = bytes[0];			\
1768      bytes[0] = bytes[3];			\
1769      bytes[3] = tmp;				\
1770      tmp = bytes[1];				\
1771      bytes[1] = bytes[2];			\
1772      bytes[2] = tmp;				\
1773   }
1774
1775
1776static void
1777extract_uint_indexes(GLuint n, GLuint indexes[],
1778                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
1779                     const struct gl_pixelstore_attrib *unpack )
1780{
1781   assert(srcFormat == GL_COLOR_INDEX);
1782
1783   ASSERT(srcType == GL_BITMAP ||
1784          srcType == GL_UNSIGNED_BYTE ||
1785          srcType == GL_BYTE ||
1786          srcType == GL_UNSIGNED_SHORT ||
1787          srcType == GL_SHORT ||
1788          srcType == GL_UNSIGNED_INT ||
1789          srcType == GL_INT ||
1790          srcType == GL_FLOAT);
1791
1792   switch (srcType) {
1793      case GL_BITMAP:
1794         {
1795            GLubyte *ubsrc = (GLubyte *) src;
1796            if (unpack->LsbFirst) {
1797               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
1798               GLuint i;
1799               for (i = 0; i < n; i++) {
1800                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
1801                  if (mask == 128) {
1802                     mask = 1;
1803                     ubsrc++;
1804                  }
1805                  else {
1806                     mask = mask << 1;
1807                  }
1808               }
1809            }
1810            else {
1811               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
1812               GLuint i;
1813               for (i = 0; i < n; i++) {
1814                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
1815                  if (mask == 1) {
1816                     mask = 128;
1817                     ubsrc++;
1818                  }
1819                  else {
1820                     mask = mask >> 1;
1821                  }
1822               }
1823            }
1824         }
1825         break;
1826      case GL_UNSIGNED_BYTE:
1827         {
1828            GLuint i;
1829            const GLubyte *s = (const GLubyte *) src;
1830            for (i = 0; i < n; i++)
1831               indexes[i] = s[i];
1832         }
1833         break;
1834      case GL_BYTE:
1835         {
1836            GLuint i;
1837            const GLbyte *s = (const GLbyte *) src;
1838            for (i = 0; i < n; i++)
1839               indexes[i] = s[i];
1840         }
1841         break;
1842      case GL_UNSIGNED_SHORT:
1843         {
1844            GLuint i;
1845            const GLushort *s = (const GLushort *) src;
1846            if (unpack->SwapBytes) {
1847               for (i = 0; i < n; i++) {
1848                  GLushort value = s[i];
1849                  SWAP2BYTE(value);
1850                  indexes[i] = value;
1851               }
1852            }
1853            else {
1854               for (i = 0; i < n; i++)
1855                  indexes[i] = s[i];
1856            }
1857         }
1858         break;
1859      case GL_SHORT:
1860         {
1861            GLuint i;
1862            const GLshort *s = (const GLshort *) src;
1863            if (unpack->SwapBytes) {
1864               for (i = 0; i < n; i++) {
1865                  GLshort value = s[i];
1866                  SWAP2BYTE(value);
1867                  indexes[i] = value;
1868               }
1869            }
1870            else {
1871               for (i = 0; i < n; i++)
1872                  indexes[i] = s[i];
1873            }
1874         }
1875         break;
1876      case GL_UNSIGNED_INT:
1877         {
1878            GLuint i;
1879            const GLuint *s = (const GLuint *) src;
1880            if (unpack->SwapBytes) {
1881               for (i = 0; i < n; i++) {
1882                  GLuint value = s[i];
1883                  SWAP4BYTE(value);
1884                  indexes[i] = value;
1885               }
1886            }
1887            else {
1888               for (i = 0; i < n; i++)
1889                  indexes[i] = s[i];
1890            }
1891         }
1892         break;
1893      case GL_INT:
1894         {
1895            GLuint i;
1896            const GLint *s = (const GLint *) src;
1897            if (unpack->SwapBytes) {
1898               for (i = 0; i < n; i++) {
1899                  GLint value = s[i];
1900                  SWAP4BYTE(value);
1901                  indexes[i] = value;
1902               }
1903            }
1904            else {
1905               for (i = 0; i < n; i++)
1906                  indexes[i] = s[i];
1907            }
1908         }
1909         break;
1910      case GL_FLOAT:
1911         {
1912            GLuint i;
1913            const GLfloat *s = (const GLfloat *) src;
1914            if (unpack->SwapBytes) {
1915               for (i = 0; i < n; i++) {
1916                  GLfloat value = s[i];
1917                  SWAP4BYTE(value);
1918                  indexes[i] = (GLuint) value;
1919               }
1920            }
1921            else {
1922               for (i = 0; i < n; i++)
1923                  indexes[i] = (GLuint) s[i];
1924            }
1925         }
1926         break;
1927      default:
1928         _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
1929         return;
1930   }
1931}
1932
1933
1934
1935/*
1936 * This function extracts floating point RGBA values from arbitrary
1937 * image data.  srcFormat and srcType are the format and type parameters
1938 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1939 *
1940 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1941 * implements the "Conversion to floating point", "Conversion to RGB",
1942 * and "Final Expansion to RGBA" operations.
1943 *
1944 * Args:  n - number of pixels
1945 *        rgba - output colors
1946 *        srcFormat - format of incoming data
1947 *        srcType - datatype of incoming data
1948 *        src - source data pointer
1949 *        swapBytes - perform byteswapping of incoming data?
1950 */
1951static void
1952extract_float_rgba(GLuint n, GLfloat rgba[][4],
1953                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
1954                   GLboolean swapBytes)
1955{
1956   GLint redIndex, greenIndex, blueIndex, alphaIndex;
1957   GLint stride;
1958   GLint rComp, bComp, gComp, aComp;
1959
1960   ASSERT(srcFormat == GL_RED ||
1961          srcFormat == GL_GREEN ||
1962          srcFormat == GL_BLUE ||
1963          srcFormat == GL_ALPHA ||
1964          srcFormat == GL_LUMINANCE ||
1965          srcFormat == GL_LUMINANCE_ALPHA ||
1966          srcFormat == GL_INTENSITY ||
1967          srcFormat == GL_RGB ||
1968          srcFormat == GL_BGR ||
1969          srcFormat == GL_RGBA ||
1970          srcFormat == GL_BGRA ||
1971          srcFormat == GL_ABGR_EXT);
1972
1973   ASSERT(srcType == GL_UNSIGNED_BYTE ||
1974          srcType == GL_BYTE ||
1975          srcType == GL_UNSIGNED_SHORT ||
1976          srcType == GL_SHORT ||
1977          srcType == GL_UNSIGNED_INT ||
1978          srcType == GL_INT ||
1979          srcType == GL_FLOAT ||
1980          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
1981          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
1982          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
1983          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
1984          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
1985          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
1986          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
1987          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
1988          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1989          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
1990          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
1991          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
1992
1993   rComp = gComp = bComp = aComp = -1;
1994
1995   switch (srcFormat) {
1996      case GL_RED:
1997         redIndex = 0;
1998         greenIndex = blueIndex = alphaIndex = -1;
1999         stride = 1;
2000         break;
2001      case GL_GREEN:
2002         greenIndex = 0;
2003         redIndex = blueIndex = alphaIndex = -1;
2004         stride = 1;
2005         break;
2006      case GL_BLUE:
2007         blueIndex = 0;
2008         redIndex = greenIndex = alphaIndex = -1;
2009         stride = 1;
2010         break;
2011      case GL_ALPHA:
2012         redIndex = greenIndex = blueIndex = -1;
2013         alphaIndex = 0;
2014         stride = 1;
2015         break;
2016      case GL_LUMINANCE:
2017         redIndex = greenIndex = blueIndex = 0;
2018         alphaIndex = -1;
2019         stride = 1;
2020         break;
2021      case GL_LUMINANCE_ALPHA:
2022         redIndex = greenIndex = blueIndex = 0;
2023         alphaIndex = 1;
2024         stride = 2;
2025         break;
2026      case GL_INTENSITY:
2027         redIndex = 0;
2028         greenIndex = blueIndex = alphaIndex = -1;
2029         stride = 1;
2030         break;
2031      case GL_RGB:
2032         redIndex = 0;
2033         greenIndex = 1;
2034         blueIndex = 2;
2035         alphaIndex = -1;
2036         stride = 3;
2037         break;
2038      case GL_BGR:
2039         redIndex = 2;
2040         greenIndex = 1;
2041         blueIndex = 0;
2042         alphaIndex = -1;
2043         stride = 3;
2044         break;
2045      case GL_RGBA:
2046         redIndex = 0;
2047         greenIndex = 1;
2048         blueIndex = 2;
2049         alphaIndex = 3;
2050         rComp = 0;
2051         gComp = 1;
2052         bComp = 2;
2053         aComp = 3;
2054         stride = 4;
2055         break;
2056      case GL_BGRA:
2057         redIndex = 2;
2058         greenIndex = 1;
2059         blueIndex = 0;
2060         alphaIndex = 3;
2061         rComp = 2;
2062         gComp = 1;
2063         bComp = 0;
2064         aComp = 3;
2065         stride = 4;
2066         break;
2067      case GL_ABGR_EXT:
2068         redIndex = 3;
2069         greenIndex = 2;
2070         blueIndex = 1;
2071         alphaIndex = 0;
2072         rComp = 3;
2073         gComp = 2;
2074         bComp = 1;
2075         aComp = 0;
2076         stride = 4;
2077         break;
2078      default:
2079         _mesa_problem(NULL, "bad srcFormat in extract float data");
2080         return;
2081   }
2082
2083
2084#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)		\
2085   if ((INDEX) < 0) {							\
2086      GLuint i;								\
2087      for (i = 0; i < n; i++) {						\
2088         rgba[i][CHANNEL] = DEFAULT;					\
2089      }									\
2090   }									\
2091   else if (swapBytes) {						\
2092      const TYPE *s = (const TYPE *) src;				\
2093      GLuint i;								\
2094      for (i = 0; i < n; i++) {						\
2095         TYPE value = s[INDEX];						\
2096         if (sizeof(TYPE) == 2) {					\
2097            SWAP2BYTE(value);						\
2098         }								\
2099         else if (sizeof(TYPE) == 4) {					\
2100            SWAP4BYTE(value);						\
2101         }								\
2102         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);		\
2103         s += stride;							\
2104      }									\
2105   }									\
2106   else {								\
2107      const TYPE *s = (const TYPE *) src;				\
2108      GLuint i;								\
2109      for (i = 0; i < n; i++) {						\
2110         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);		\
2111         s += stride;							\
2112      }									\
2113   }
2114
2115   switch (srcType) {
2116      case GL_UNSIGNED_BYTE:
2117         PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2118         PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2119         PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2120         PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
2121         break;
2122      case GL_BYTE:
2123         PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2124         PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2125         PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2126         PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
2127         break;
2128      case GL_UNSIGNED_SHORT:
2129         PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2130         PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2131         PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2132         PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
2133         break;
2134      case GL_SHORT:
2135         PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2136         PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2137         PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2138         PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
2139         break;
2140      case GL_UNSIGNED_INT:
2141         PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2142         PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2143         PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2144         PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
2145         break;
2146      case GL_INT:
2147         PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
2148         PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
2149         PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
2150         PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
2151         break;
2152      case GL_FLOAT:
2153         PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
2154         PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
2155         PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
2156         PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
2157         break;
2158      case GL_UNSIGNED_BYTE_3_3_2:
2159         {
2160            const GLubyte *ubsrc = (const GLubyte *) src;
2161            GLuint i;
2162            for (i = 0; i < n; i ++) {
2163               GLubyte p = ubsrc[i];
2164               rgba[i][RCOMP] = ((p >> 5)      ) * (1.0F / 7.0F);
2165               rgba[i][GCOMP] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
2166               rgba[i][BCOMP] = ((p     ) & 0x3) * (1.0F / 3.0F);
2167               rgba[i][ACOMP] = 1.0F;
2168            }
2169         }
2170         break;
2171      case GL_UNSIGNED_BYTE_2_3_3_REV:
2172         {
2173            const GLubyte *ubsrc = (const GLubyte *) src;
2174            GLuint i;
2175            for (i = 0; i < n; i ++) {
2176               GLubyte p = ubsrc[i];
2177               rgba[i][RCOMP] = ((p     ) & 0x7) * (1.0F / 7.0F);
2178               rgba[i][GCOMP] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
2179               rgba[i][BCOMP] = ((p >> 6)      ) * (1.0F / 3.0F);
2180               rgba[i][ACOMP] = 1.0F;
2181            }
2182         }
2183         break;
2184      case GL_UNSIGNED_SHORT_5_6_5:
2185         if (swapBytes) {
2186            const GLushort *ussrc = (const GLushort *) src;
2187            GLuint i;
2188            for (i = 0; i < n; i ++) {
2189               GLushort p = ussrc[i];
2190               SWAP2BYTE(p);
2191               rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
2192               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2193               rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2194               rgba[i][ACOMP] = 1.0F;
2195            }
2196         }
2197         else {
2198            const GLushort *ussrc = (const GLushort *) src;
2199            GLuint i;
2200            for (i = 0; i < n; i ++) {
2201               GLushort p = ussrc[i];
2202               rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
2203               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2204               rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2205               rgba[i][ACOMP] = 1.0F;
2206            }
2207         }
2208         break;
2209      case GL_UNSIGNED_SHORT_5_6_5_REV:
2210         if (swapBytes) {
2211            const GLushort *ussrc = (const GLushort *) src;
2212            GLuint i;
2213            for (i = 0; i < n; i ++) {
2214               GLushort p = ussrc[i];
2215               SWAP2BYTE(p);
2216               rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2217               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2218               rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
2219               rgba[i][ACOMP] = 1.0F;
2220            }
2221         }
2222         else {
2223            const GLushort *ussrc = (const GLushort *) src;
2224            GLuint i;
2225            for (i = 0; i < n; i ++) {
2226               GLushort p = ussrc[i];
2227               rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2228               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2229               rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
2230               rgba[i][ACOMP] = 1.0F;
2231            }
2232         }
2233         break;
2234      case GL_UNSIGNED_SHORT_4_4_4_4:
2235         if (swapBytes) {
2236            const GLushort *ussrc = (const GLushort *) src;
2237            GLuint i;
2238            for (i = 0; i < n; i ++) {
2239               GLushort p = ussrc[i];
2240               SWAP2BYTE(p);
2241               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2242               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2243               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2244               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2245            }
2246         }
2247         else {
2248            const GLushort *ussrc = (const GLushort *) src;
2249            GLuint i;
2250            for (i = 0; i < n; i ++) {
2251               GLushort p = ussrc[i];
2252               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2253               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2254               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2255               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2256            }
2257         }
2258         break;
2259      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2260         if (swapBytes) {
2261            const GLushort *ussrc = (const GLushort *) src;
2262            GLuint i;
2263            for (i = 0; i < n; i ++) {
2264               GLushort p = ussrc[i];
2265               SWAP2BYTE(p);
2266               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2267               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2268               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2269               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2270            }
2271         }
2272         else {
2273            const GLushort *ussrc = (const GLushort *) src;
2274            GLuint i;
2275            for (i = 0; i < n; i ++) {
2276               GLushort p = ussrc[i];
2277               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2278               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2279               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2280               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2281            }
2282         }
2283         break;
2284      case GL_UNSIGNED_SHORT_5_5_5_1:
2285         if (swapBytes) {
2286            const GLushort *ussrc = (const GLushort *) src;
2287            GLuint i;
2288            for (i = 0; i < n; i ++) {
2289               GLushort p = ussrc[i];
2290               SWAP2BYTE(p);
2291               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2292               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2293               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2294               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2295            }
2296         }
2297         else {
2298            const GLushort *ussrc = (const GLushort *) src;
2299            GLuint i;
2300            for (i = 0; i < n; i ++) {
2301               GLushort p = ussrc[i];
2302               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2303               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2304               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2305               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2306            }
2307         }
2308         break;
2309      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2310         if (swapBytes) {
2311            const GLushort *ussrc = (const GLushort *) src;
2312            GLuint i;
2313            for (i = 0; i < n; i ++) {
2314               GLushort p = ussrc[i];
2315               SWAP2BYTE(p);
2316               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2317               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2318               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2319               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2320            }
2321         }
2322         else {
2323            const GLushort *ussrc = (const GLushort *) src;
2324            GLuint i;
2325            for (i = 0; i < n; i ++) {
2326               GLushort p = ussrc[i];
2327               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2328               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2329               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2330               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2331            }
2332         }
2333         break;
2334      case GL_UNSIGNED_INT_8_8_8_8:
2335         if (swapBytes) {
2336            const GLuint *uisrc = (const GLuint *) src;
2337            GLuint i;
2338            for (i = 0; i < n; i ++) {
2339               GLuint p = uisrc[i];
2340               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2341               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2342               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2343               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
2344            }
2345         }
2346         else {
2347            const GLuint *uisrc = (const GLuint *) src;
2348            GLuint i;
2349            for (i = 0; i < n; i ++) {
2350               GLuint p = uisrc[i];
2351               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
2352               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2353               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2354               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2355            }
2356         }
2357         break;
2358      case GL_UNSIGNED_INT_8_8_8_8_REV:
2359         if (swapBytes) {
2360            const GLuint *uisrc = (const GLuint *) src;
2361            GLuint i;
2362            for (i = 0; i < n; i ++) {
2363               GLuint p = uisrc[i];
2364               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
2365               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2366               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2367               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2368            }
2369         }
2370         else {
2371            const GLuint *uisrc = (const GLuint *) src;
2372            GLuint i;
2373            for (i = 0; i < n; i ++) {
2374               GLuint p = uisrc[i];
2375               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2376               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2377               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2378               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
2379            }
2380         }
2381         break;
2382      case GL_UNSIGNED_INT_10_10_10_2:
2383         if (swapBytes) {
2384            const GLuint *uisrc = (const GLuint *) src;
2385            GLuint i;
2386            for (i = 0; i < n; i ++) {
2387               GLuint p = uisrc[i];
2388               SWAP4BYTE(p);
2389               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2390               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2391               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2392               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2393            }
2394         }
2395         else {
2396            const GLuint *uisrc = (const GLuint *) src;
2397            GLuint i;
2398            for (i = 0; i < n; i ++) {
2399               GLuint p = uisrc[i];
2400               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2401               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2402               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2403               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2404            }
2405         }
2406         break;
2407      case GL_UNSIGNED_INT_2_10_10_10_REV:
2408         if (swapBytes) {
2409            const GLuint *uisrc = (const GLuint *) src;
2410            GLuint i;
2411            for (i = 0; i < n; i ++) {
2412               GLuint p = uisrc[i];
2413               SWAP4BYTE(p);
2414               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2415               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2416               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2417               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2418            }
2419         }
2420         else {
2421            const GLuint *uisrc = (const GLuint *) src;
2422            GLuint i;
2423            for (i = 0; i < n; i ++) {
2424               GLuint p = uisrc[i];
2425               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2426               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2427               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2428               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2429            }
2430         }
2431         break;
2432      default:
2433         _mesa_problem(NULL, "bad srcType in extract float data");
2434         break;
2435   }
2436}
2437
2438
2439
2440/*
2441 * Unpack a row of color image data from a client buffer according to
2442 * the pixel unpacking parameters.
2443 * Return GLubyte values in the specified dest image format.
2444 * This is (or will be) used by glDrawPixels and glTexImage?D().
2445 * Input:  ctx - the context
2446 *         n - number of pixels in the span
2447 *         dstFormat - format of destination color array
2448 *         dest - the destination color array
2449 *         srcFormat - source image format
2450 *         srcType - source image  datatype
2451 *         source - source image pointer
2452 *         srcPacking - pixel unpacking parameters
2453 *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
2454 *
2455 * XXX perhaps expand this to process whole images someday.
2456 */
2457void
2458_mesa_unpack_chan_color_span( GLcontext *ctx,
2459                              GLuint n, GLenum dstFormat, GLchan dest[],
2460                              GLenum srcFormat, GLenum srcType,
2461                              const GLvoid *source,
2462                              const struct gl_pixelstore_attrib *srcPacking,
2463                              GLuint transferOps )
2464{
2465   ASSERT(dstFormat == GL_ALPHA ||
2466          dstFormat == GL_LUMINANCE ||
2467          dstFormat == GL_LUMINANCE_ALPHA ||
2468          dstFormat == GL_INTENSITY ||
2469          dstFormat == GL_RGB ||
2470          dstFormat == GL_RGBA ||
2471          dstFormat == GL_COLOR_INDEX);
2472
2473   ASSERT(srcFormat == GL_RED ||
2474          srcFormat == GL_GREEN ||
2475          srcFormat == GL_BLUE ||
2476          srcFormat == GL_ALPHA ||
2477          srcFormat == GL_LUMINANCE ||
2478          srcFormat == GL_LUMINANCE_ALPHA ||
2479          srcFormat == GL_INTENSITY ||
2480          srcFormat == GL_RGB ||
2481          srcFormat == GL_BGR ||
2482          srcFormat == GL_RGBA ||
2483          srcFormat == GL_BGRA ||
2484          srcFormat == GL_ABGR_EXT ||
2485          srcFormat == GL_COLOR_INDEX);
2486
2487   ASSERT(srcType == GL_BITMAP ||
2488          srcType == GL_UNSIGNED_BYTE ||
2489          srcType == GL_BYTE ||
2490          srcType == GL_UNSIGNED_SHORT ||
2491          srcType == GL_SHORT ||
2492          srcType == GL_UNSIGNED_INT ||
2493          srcType == GL_INT ||
2494          srcType == GL_FLOAT ||
2495          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2496          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2497          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2498          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2499          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2500          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2501          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2502          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2503          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2504          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2505          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2506          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2507
2508   /* Try simple cases first */
2509   if (transferOps == 0 && srcType == CHAN_TYPE) {
2510      if (dstFormat == GL_RGBA) {
2511         if (srcFormat == GL_RGBA) {
2512            MEMCPY( dest, source, n * 4 * sizeof(GLchan) );
2513            return;
2514         }
2515         else if (srcFormat == GL_RGB) {
2516            GLuint i;
2517            const GLchan *src = (const GLchan *) source;
2518            GLchan *dst = dest;
2519            for (i = 0; i < n; i++) {
2520               dst[0] = src[0];
2521               dst[1] = src[1];
2522               dst[2] = src[2];
2523               dst[3] = CHAN_MAX;
2524               src += 3;
2525               dst += 4;
2526            }
2527            return;
2528         }
2529      }
2530      else if (dstFormat == GL_RGB) {
2531         if (srcFormat == GL_RGB) {
2532            MEMCPY( dest, source, n * 3 * sizeof(GLchan) );
2533            return;
2534         }
2535         else if (srcFormat == GL_RGBA) {
2536            GLuint i;
2537            const GLchan *src = (const GLchan *) source;
2538            GLchan *dst = dest;
2539            for (i = 0; i < n; i++) {
2540               dst[0] = src[0];
2541               dst[1] = src[1];
2542               dst[2] = src[2];
2543               src += 4;
2544               dst += 3;
2545            }
2546            return;
2547         }
2548      }
2549      else if (dstFormat == srcFormat) {
2550         GLint comps = _mesa_components_in_format(srcFormat);
2551         assert(comps > 0);
2552         MEMCPY( dest, source, n * comps * sizeof(GLchan) );
2553         return;
2554      }
2555   }
2556
2557
2558   /* general solution begins here */
2559   {
2560      GLfloat rgba[MAX_WIDTH][4];
2561      GLint dstComponents;
2562      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2563      GLint dstLuminanceIndex, dstIntensityIndex;
2564
2565      dstComponents = _mesa_components_in_format( dstFormat );
2566      /* source & dest image formats should have been error checked by now */
2567      assert(dstComponents > 0);
2568
2569      /*
2570       * Extract image data and convert to RGBA floats
2571       */
2572      assert(n <= MAX_WIDTH);
2573      if (srcFormat == GL_COLOR_INDEX) {
2574         GLuint indexes[MAX_WIDTH];
2575         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2576                              srcPacking);
2577
2578         if (dstFormat == GL_COLOR_INDEX
2579             && (transferOps & IMAGE_MAP_COLOR_BIT)) {
2580            _mesa_map_ci(ctx, n, indexes);
2581         }
2582         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2583            _mesa_shift_and_offset_ci(ctx, n, indexes);
2584         }
2585
2586         if (dstFormat == GL_COLOR_INDEX) {
2587            /* convert to GLchan and return */
2588            GLuint i;
2589            for (i = 0; i < n; i++) {
2590               dest[i] = (GLchan) (indexes[i] & 0xff);
2591            }
2592            return;
2593         }
2594         else {
2595            /* Convert indexes to RGBA */
2596            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
2597         }
2598      }
2599      else {
2600         extract_float_rgba(n, rgba, srcFormat, srcType, source,
2601                            srcPacking->SwapBytes);
2602
2603         /* scale and bias colors */
2604         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
2605            _mesa_scale_and_bias_rgba(ctx, n, rgba,
2606                                   ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
2607                                   ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
2608                                   ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
2609                                   ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
2610         }
2611         /* color map lookup */
2612         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2613            _mesa_map_rgba(ctx, n, rgba);
2614         }
2615      }
2616
2617      if (transferOps) {
2618         /* GL_COLOR_TABLE lookup */
2619         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
2620            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
2621         }
2622         /* convolution */
2623         if (transferOps & IMAGE_CONVOLUTION_BIT) {
2624            /* this has to be done in the calling code */
2625         }
2626         /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
2627         if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
2628            _mesa_scale_and_bias_rgba(ctx, n, rgba,
2629                                      ctx->Pixel.PostConvolutionScale[RCOMP],
2630                                      ctx->Pixel.PostConvolutionScale[GCOMP],
2631                                      ctx->Pixel.PostConvolutionScale[BCOMP],
2632                                      ctx->Pixel.PostConvolutionScale[ACOMP],
2633                                      ctx->Pixel.PostConvolutionBias[RCOMP],
2634                                      ctx->Pixel.PostConvolutionBias[GCOMP],
2635                                      ctx->Pixel.PostConvolutionBias[BCOMP],
2636                                      ctx->Pixel.PostConvolutionBias[ACOMP]);
2637         }
2638         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
2639         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
2640            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
2641         }
2642         /* color matrix transform */
2643         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
2644            _mesa_transform_rgba(ctx, n, rgba);
2645         }
2646         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
2647         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
2648            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
2649         }
2650         /* update histogram count */
2651         if (transferOps & IMAGE_HISTOGRAM_BIT) {
2652            _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
2653         }
2654         /* min/max here */
2655         if (transferOps & IMAGE_MIN_MAX_BIT) {
2656            _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
2657         }
2658      }
2659
2660      /* clamp to [0,1] */
2661      {
2662         GLuint i;
2663         for (i = 0; i < n; i++) {
2664            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2665            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2666            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2667            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2668         }
2669      }
2670
2671      /* Now determine which color channels we need to produce.
2672       * And determine the dest index (offset) within each color tuple.
2673       */
2674      switch (dstFormat) {
2675         case GL_ALPHA:
2676            dstAlphaIndex = 0;
2677            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2678            dstLuminanceIndex = dstIntensityIndex = -1;
2679            break;
2680         case GL_LUMINANCE:
2681            dstLuminanceIndex = 0;
2682            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2683            dstIntensityIndex = -1;
2684            break;
2685         case GL_LUMINANCE_ALPHA:
2686            dstLuminanceIndex = 0;
2687            dstAlphaIndex = 1;
2688            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2689            dstIntensityIndex = -1;
2690            break;
2691         case GL_INTENSITY:
2692            dstIntensityIndex = 0;
2693            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2694            dstLuminanceIndex = -1;
2695            break;
2696         case GL_RGB:
2697            dstRedIndex = 0;
2698            dstGreenIndex = 1;
2699            dstBlueIndex = 2;
2700            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2701            break;
2702         case GL_RGBA:
2703            dstRedIndex = 0;
2704            dstGreenIndex = 1;
2705            dstBlueIndex = 2;
2706            dstAlphaIndex = 3;
2707            dstLuminanceIndex = dstIntensityIndex = -1;
2708            break;
2709         default:
2710            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_chan_span()");
2711            return;
2712      }
2713
2714
2715      /* Now return the GLchan data in the requested dstFormat */
2716
2717      if (dstRedIndex >= 0) {
2718         GLchan *dst = dest;
2719         GLuint i;
2720         for (i = 0; i < n; i++) {
2721            CLAMPED_FLOAT_TO_CHAN(dst[dstRedIndex], rgba[i][RCOMP]);
2722            dst += dstComponents;
2723         }
2724      }
2725
2726      if (dstGreenIndex >= 0) {
2727         GLchan *dst = dest;
2728         GLuint i;
2729         for (i = 0; i < n; i++) {
2730            CLAMPED_FLOAT_TO_CHAN(dst[dstGreenIndex], rgba[i][GCOMP]);
2731            dst += dstComponents;
2732         }
2733      }
2734
2735      if (dstBlueIndex >= 0) {
2736         GLchan *dst = dest;
2737         GLuint i;
2738         for (i = 0; i < n; i++) {
2739            CLAMPED_FLOAT_TO_CHAN(dst[dstBlueIndex], rgba[i][BCOMP]);
2740            dst += dstComponents;
2741         }
2742      }
2743
2744      if (dstAlphaIndex >= 0) {
2745         GLchan *dst = dest;
2746         GLuint i;
2747         for (i = 0; i < n; i++) {
2748            CLAMPED_FLOAT_TO_CHAN(dst[dstAlphaIndex], rgba[i][ACOMP]);
2749            dst += dstComponents;
2750         }
2751      }
2752
2753      if (dstIntensityIndex >= 0) {
2754         GLchan *dst = dest;
2755         GLuint i;
2756         assert(dstIntensityIndex == 0);
2757         assert(dstComponents == 1);
2758         for (i = 0; i < n; i++) {
2759            /* Intensity comes from red channel */
2760            CLAMPED_FLOAT_TO_CHAN(dst[i], rgba[i][RCOMP]);
2761         }
2762      }
2763
2764      if (dstLuminanceIndex >= 0) {
2765         GLchan *dst = dest;
2766         GLuint i;
2767         assert(dstLuminanceIndex == 0);
2768         for (i = 0; i < n; i++) {
2769            /* Luminance comes from red channel */
2770            CLAMPED_FLOAT_TO_CHAN(dst[0], rgba[i][RCOMP]);
2771            dst += dstComponents;
2772         }
2773      }
2774   }
2775}
2776
2777
2778void
2779_mesa_unpack_float_color_span( GLcontext *ctx,
2780                               GLuint n, GLenum dstFormat, GLfloat dest[],
2781                               GLenum srcFormat, GLenum srcType,
2782                               const GLvoid *source,
2783                               const struct gl_pixelstore_attrib *srcPacking,
2784                               GLuint transferOps, GLboolean clamp )
2785{
2786   ASSERT(dstFormat == GL_ALPHA ||
2787          dstFormat == GL_LUMINANCE ||
2788          dstFormat == GL_LUMINANCE_ALPHA ||
2789          dstFormat == GL_INTENSITY ||
2790          dstFormat == GL_RGB ||
2791          dstFormat == GL_RGBA ||
2792          dstFormat == GL_COLOR_INDEX);
2793
2794   ASSERT(srcFormat == GL_RED ||
2795          srcFormat == GL_GREEN ||
2796          srcFormat == GL_BLUE ||
2797          srcFormat == GL_ALPHA ||
2798          srcFormat == GL_LUMINANCE ||
2799          srcFormat == GL_LUMINANCE_ALPHA ||
2800          srcFormat == GL_INTENSITY ||
2801          srcFormat == GL_RGB ||
2802          srcFormat == GL_BGR ||
2803          srcFormat == GL_RGBA ||
2804          srcFormat == GL_BGRA ||
2805          srcFormat == GL_ABGR_EXT ||
2806          srcFormat == GL_COLOR_INDEX);
2807
2808   ASSERT(srcType == GL_BITMAP ||
2809          srcType == GL_UNSIGNED_BYTE ||
2810          srcType == GL_BYTE ||
2811          srcType == GL_UNSIGNED_SHORT ||
2812          srcType == GL_SHORT ||
2813          srcType == GL_UNSIGNED_INT ||
2814          srcType == GL_INT ||
2815          srcType == GL_FLOAT ||
2816          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2817          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2818          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2819          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2820          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2821          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2822          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2823          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2824          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2825          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2826          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2827          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2828
2829   /* general solution, no special cases, yet */
2830   {
2831      GLfloat rgba[MAX_WIDTH][4];
2832      GLint dstComponents;
2833      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2834      GLint dstLuminanceIndex, dstIntensityIndex;
2835
2836      dstComponents = _mesa_components_in_format( dstFormat );
2837      /* source & dest image formats should have been error checked by now */
2838      assert(dstComponents > 0);
2839
2840      /*
2841       * Extract image data and convert to RGBA floats
2842       */
2843      assert(n <= MAX_WIDTH);
2844      if (srcFormat == GL_COLOR_INDEX) {
2845         GLuint indexes[MAX_WIDTH];
2846         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2847                              srcPacking);
2848
2849         if (dstFormat == GL_COLOR_INDEX
2850             && (transferOps & IMAGE_MAP_COLOR_BIT)) {
2851            _mesa_map_ci(ctx, n, indexes);
2852         }
2853         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2854            _mesa_shift_and_offset_ci(ctx, n, indexes);
2855         }
2856
2857         if (dstFormat == GL_COLOR_INDEX) {
2858            /* convert to GLchan and return */
2859            GLuint i;
2860            for (i = 0; i < n; i++) {
2861               dest[i] = (GLchan) (indexes[i] & 0xff);
2862            }
2863            return;
2864         }
2865         else {
2866            /* Convert indexes to RGBA */
2867            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
2868         }
2869      }
2870      else {
2871         extract_float_rgba(n, rgba, srcFormat, srcType, source,
2872                            srcPacking->SwapBytes);
2873
2874         /* scale and bias colors */
2875         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
2876            _mesa_scale_and_bias_rgba(ctx, n, rgba,
2877                                   ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
2878                                   ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
2879                                   ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
2880                                   ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
2881         }
2882         /* color map lookup */
2883         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2884            _mesa_map_rgba(ctx, n, rgba);
2885         }
2886      }
2887
2888      if (transferOps) {
2889         /* GL_COLOR_TABLE lookup */
2890         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
2891            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
2892         }
2893         /* convolution */
2894         if (transferOps & IMAGE_CONVOLUTION_BIT) {
2895            /* XXX to do */
2896         }
2897         /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
2898         if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
2899            _mesa_scale_and_bias_rgba(ctx, n, rgba,
2900                                      ctx->Pixel.PostConvolutionScale[RCOMP],
2901                                      ctx->Pixel.PostConvolutionScale[GCOMP],
2902                                      ctx->Pixel.PostConvolutionScale[BCOMP],
2903                                      ctx->Pixel.PostConvolutionScale[ACOMP],
2904                                      ctx->Pixel.PostConvolutionBias[RCOMP],
2905                                      ctx->Pixel.PostConvolutionBias[GCOMP],
2906                                      ctx->Pixel.PostConvolutionBias[BCOMP],
2907                                      ctx->Pixel.PostConvolutionBias[ACOMP]);
2908         }
2909         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
2910         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
2911            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
2912         }
2913         /* color matrix transform */
2914         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
2915            _mesa_transform_rgba(ctx, n, rgba);
2916         }
2917         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
2918         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
2919            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
2920         }
2921         /* update histogram count */
2922         if (transferOps & IMAGE_HISTOGRAM_BIT) {
2923            _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
2924         }
2925         /* min/max here */
2926         if (transferOps & IMAGE_MIN_MAX_BIT) {
2927            _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
2928         }
2929      }
2930
2931      /* clamp to [0,1] */
2932      if (clamp) {
2933         GLuint i;
2934         for (i = 0; i < n; i++) {
2935            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2936            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2937            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2938            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2939         }
2940      }
2941
2942      /* Now determine which color channels we need to produce.
2943       * And determine the dest index (offset) within each color tuple.
2944       */
2945      switch (dstFormat) {
2946         case GL_ALPHA:
2947            dstAlphaIndex = 0;
2948            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2949            dstLuminanceIndex = dstIntensityIndex = -1;
2950            break;
2951         case GL_LUMINANCE:
2952            dstLuminanceIndex = 0;
2953            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2954            dstIntensityIndex = -1;
2955            break;
2956         case GL_LUMINANCE_ALPHA:
2957            dstLuminanceIndex = 0;
2958            dstAlphaIndex = 1;
2959            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2960            dstIntensityIndex = -1;
2961            break;
2962         case GL_INTENSITY:
2963            dstIntensityIndex = 0;
2964            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2965            dstLuminanceIndex = -1;
2966            break;
2967         case GL_RGB:
2968            dstRedIndex = 0;
2969            dstGreenIndex = 1;
2970            dstBlueIndex = 2;
2971            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2972            break;
2973         case GL_RGBA:
2974            dstRedIndex = 0;
2975            dstGreenIndex = 1;
2976            dstBlueIndex = 2;
2977            dstAlphaIndex = 3;
2978            dstLuminanceIndex = dstIntensityIndex = -1;
2979            break;
2980         default:
2981            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_float_color_span()");
2982            return;
2983      }
2984
2985      /* Now pack results in the requested dstFormat */
2986      if (dstRedIndex >= 0) {
2987         GLfloat *dst = dest;
2988         GLuint i;
2989         for (i = 0; i < n; i++) {
2990            dst[dstRedIndex] = rgba[i][RCOMP];
2991            dst += dstComponents;
2992         }
2993      }
2994
2995      if (dstGreenIndex >= 0) {
2996         GLfloat *dst = dest;
2997         GLuint i;
2998         for (i = 0; i < n; i++) {
2999            dst[dstGreenIndex] = rgba[i][GCOMP];
3000            dst += dstComponents;
3001         }
3002      }
3003
3004      if (dstBlueIndex >= 0) {
3005         GLfloat *dst = dest;
3006         GLuint i;
3007         for (i = 0; i < n; i++) {
3008            dst[dstBlueIndex] = rgba[i][BCOMP];
3009            dst += dstComponents;
3010         }
3011      }
3012
3013      if (dstAlphaIndex >= 0) {
3014         GLfloat *dst = dest;
3015         GLuint i;
3016         for (i = 0; i < n; i++) {
3017            dst[dstAlphaIndex] = rgba[i][ACOMP];
3018            dst += dstComponents;
3019         }
3020      }
3021
3022      if (dstIntensityIndex >= 0) {
3023         GLfloat *dst = dest;
3024         GLuint i;
3025         assert(dstIntensityIndex == 0);
3026         assert(dstComponents == 1);
3027         for (i = 0; i < n; i++) {
3028            /* Intensity comes from red channel */
3029            dst[i] = rgba[i][RCOMP];
3030         }
3031      }
3032
3033      if (dstLuminanceIndex >= 0) {
3034         GLfloat *dst = dest;
3035         GLuint i;
3036         assert(dstLuminanceIndex == 0);
3037         for (i = 0; i < n; i++) {
3038            /* Luminance comes from red channel */
3039            dst[0] = rgba[i][RCOMP];
3040            dst += dstComponents;
3041         }
3042      }
3043   }
3044}
3045
3046
3047
3048
3049/*
3050 * Unpack a row of color index data from a client buffer according to
3051 * the pixel unpacking parameters.
3052 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3053 *
3054 * Args:  ctx - the context
3055 *        n - number of pixels
3056 *        dstType - destination datatype
3057 *        dest - destination array
3058 *        srcType - source pixel type
3059 *        source - source data pointer
3060 *        srcPacking - pixel unpacking parameters
3061 *        transferOps - the pixel transfer operations to apply
3062 */
3063void
3064_mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
3065                         GLenum dstType, GLvoid *dest,
3066                         GLenum srcType, const GLvoid *source,
3067                         const struct gl_pixelstore_attrib *srcPacking,
3068                         GLuint transferOps )
3069{
3070   ASSERT(srcType == GL_BITMAP ||
3071          srcType == GL_UNSIGNED_BYTE ||
3072          srcType == GL_BYTE ||
3073          srcType == GL_UNSIGNED_SHORT ||
3074          srcType == GL_SHORT ||
3075          srcType == GL_UNSIGNED_INT ||
3076          srcType == GL_INT ||
3077          srcType == GL_FLOAT);
3078
3079   ASSERT(dstType == GL_UNSIGNED_BYTE ||
3080          dstType == GL_UNSIGNED_SHORT ||
3081          dstType == GL_UNSIGNED_INT);
3082
3083
3084   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3085
3086   /*
3087    * Try simple cases first
3088    */
3089   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
3090       && dstType == GL_UNSIGNED_BYTE) {
3091      MEMCPY(dest, source, n * sizeof(GLubyte));
3092   }
3093   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
3094            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
3095      MEMCPY(dest, source, n * sizeof(GLuint));
3096   }
3097   else {
3098      /*
3099       * general solution
3100       */
3101      GLuint indexes[MAX_WIDTH];
3102      assert(n <= MAX_WIDTH);
3103
3104      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
3105                           srcPacking);
3106
3107      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3108         /* shift and offset indexes */
3109         _mesa_shift_and_offset_ci(ctx, n, indexes);
3110      }
3111      if (transferOps & IMAGE_MAP_COLOR_BIT) {
3112         /* Apply lookup table */
3113         _mesa_map_ci(ctx, n, indexes);
3114      }
3115
3116      /* convert to dest type */
3117      switch (dstType) {
3118         case GL_UNSIGNED_BYTE:
3119            {
3120               GLubyte *dst = (GLubyte *) dest;
3121               GLuint i;
3122               for (i = 0; i < n; i++) {
3123                  dst[i] = (GLubyte) (indexes[i] & 0xff);
3124               }
3125            }
3126            break;
3127         case GL_UNSIGNED_SHORT:
3128            {
3129               GLuint *dst = (GLuint *) dest;
3130               GLuint i;
3131               for (i = 0; i < n; i++) {
3132                  dst[i] = (GLushort) (indexes[i] & 0xffff);
3133               }
3134            }
3135            break;
3136         case GL_UNSIGNED_INT:
3137            MEMCPY(dest, indexes, n * sizeof(GLuint));
3138            break;
3139         default:
3140            _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
3141      }
3142   }
3143}
3144
3145
3146void
3147_mesa_pack_index_span( const GLcontext *ctx, GLuint n,
3148                       GLenum dstType, GLvoid *dest, const GLuint *source,
3149                       const struct gl_pixelstore_attrib *dstPacking,
3150                       GLuint transferOps )
3151{
3152   GLuint indexes[MAX_WIDTH];
3153
3154   ASSERT(n <= MAX_WIDTH);
3155
3156   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3157
3158   if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
3159      /* make a copy of input */
3160      MEMCPY(indexes, source, n * sizeof(GLuint));
3161      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3162         _mesa_shift_and_offset_ci( ctx, n, indexes);
3163      }
3164      if (transferOps & IMAGE_MAP_COLOR_BIT) {
3165         _mesa_map_ci(ctx, n, indexes);
3166      }
3167      source = indexes;
3168   }
3169
3170   switch (dstType) {
3171   case GL_UNSIGNED_BYTE:
3172      {
3173         GLubyte *dst = (GLubyte *) dest;
3174         GLuint i;
3175         for (i = 0; i < n; i++) {
3176            *dst++ = (GLubyte) source[i];
3177         }
3178      }
3179      break;
3180   case GL_BYTE:
3181      {
3182         GLbyte *dst = (GLbyte *) dest;
3183         GLuint i;
3184         for (i = 0; i < n; i++) {
3185            dst[i] = (GLbyte) indexes[i];
3186         }
3187      }
3188      break;
3189   case GL_UNSIGNED_SHORT:
3190      {
3191         GLushort *dst = (GLushort *) dest;
3192         GLuint i;
3193         for (i = 0; i < n; i++) {
3194            dst[i] = (GLushort) indexes[i];
3195         }
3196         if (dstPacking->SwapBytes) {
3197            _mesa_swap2( (GLushort *) dst, n );
3198         }
3199      }
3200      break;
3201   case GL_SHORT:
3202      {
3203         GLshort *dst = (GLshort *) dest;
3204         GLuint i;
3205         for (i = 0; i < n; i++) {
3206            dst[i] = (GLshort) indexes[i];
3207         }
3208         if (dstPacking->SwapBytes) {
3209            _mesa_swap2( (GLushort *) dst, n );
3210         }
3211      }
3212      break;
3213   case GL_UNSIGNED_INT:
3214      {
3215         GLuint *dst = (GLuint *) dest;
3216         GLuint i;
3217         for (i = 0; i < n; i++) {
3218            dst[i] = (GLuint) indexes[i];
3219         }
3220         if (dstPacking->SwapBytes) {
3221            _mesa_swap4( (GLuint *) dst, n );
3222         }
3223      }
3224      break;
3225   case GL_INT:
3226      {
3227         GLint *dst = (GLint *) dest;
3228         GLuint i;
3229         for (i = 0; i < n; i++) {
3230            dst[i] = (GLint) indexes[i];
3231         }
3232         if (dstPacking->SwapBytes) {
3233            _mesa_swap4( (GLuint *) dst, n );
3234         }
3235      }
3236      break;
3237   case GL_FLOAT:
3238      {
3239         GLfloat *dst = (GLfloat *) dest;
3240         GLuint i;
3241         for (i = 0; i < n; i++) {
3242            dst[i] = (GLfloat) indexes[i];
3243         }
3244         if (dstPacking->SwapBytes) {
3245            _mesa_swap4( (GLuint *) dst, n );
3246         }
3247      }
3248      break;
3249   default:
3250      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
3251   }
3252}
3253
3254
3255
3256/*
3257 * Unpack a row of stencil data from a client buffer according to
3258 * the pixel unpacking parameters.
3259 * This is (or will be) used by glDrawPixels
3260 *
3261 * Args:  ctx - the context
3262 *        n - number of pixels
3263 *        dstType - destination datatype
3264 *        dest - destination array
3265 *        srcType - source pixel type
3266 *        source - source data pointer
3267 *        srcPacking - pixel unpacking parameters
3268 *        transferOps - apply offset/bias/lookup ops?
3269 */
3270void
3271_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
3272                           GLenum dstType, GLvoid *dest,
3273                           GLenum srcType, const GLvoid *source,
3274                           const struct gl_pixelstore_attrib *srcPacking,
3275                           GLuint transferOps )
3276{
3277   ASSERT(srcType == GL_BITMAP ||
3278          srcType == GL_UNSIGNED_BYTE ||
3279          srcType == GL_BYTE ||
3280          srcType == GL_UNSIGNED_SHORT ||
3281          srcType == GL_SHORT ||
3282          srcType == GL_UNSIGNED_INT ||
3283          srcType == GL_INT ||
3284          srcType == GL_FLOAT);
3285
3286   ASSERT(dstType == GL_UNSIGNED_BYTE ||
3287          dstType == GL_UNSIGNED_SHORT ||
3288          dstType == GL_UNSIGNED_INT);
3289
3290   /* only shift and offset apply to stencil */
3291   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
3292
3293   /*
3294    * Try simple cases first
3295    */
3296   if (transferOps == 0 &&
3297       srcType == GL_UNSIGNED_BYTE &&
3298       dstType == GL_UNSIGNED_BYTE) {
3299      MEMCPY(dest, source, n * sizeof(GLubyte));
3300   }
3301   else if (transferOps == 0 &&
3302            srcType == GL_UNSIGNED_INT &&
3303            dstType == GL_UNSIGNED_INT &&
3304            !srcPacking->SwapBytes) {
3305      MEMCPY(dest, source, n * sizeof(GLuint));
3306   }
3307   else {
3308      /*
3309       * general solution
3310       */
3311      GLuint indexes[MAX_WIDTH];
3312      assert(n <= MAX_WIDTH);
3313
3314      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
3315                           srcPacking);
3316
3317      if (transferOps) {
3318         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3319            /* shift and offset indexes */
3320            _mesa_shift_and_offset_ci(ctx, n, indexes);
3321         }
3322
3323         if (ctx->Pixel.MapStencilFlag) {
3324            /* Apply stencil lookup table */
3325            GLuint mask = ctx->Pixel.MapStoSsize - 1;
3326            GLuint i;
3327            for (i=0;i<n;i++) {
3328               indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
3329            }
3330         }
3331      }
3332
3333      /* convert to dest type */
3334      switch (dstType) {
3335         case GL_UNSIGNED_BYTE:
3336            {
3337               GLubyte *dst = (GLubyte *) dest;
3338               GLuint i;
3339               for (i = 0; i < n; i++) {
3340                  dst[i] = (GLubyte) (indexes[i] & 0xff);
3341               }
3342            }
3343            break;
3344         case GL_UNSIGNED_SHORT:
3345            {
3346               GLuint *dst = (GLuint *) dest;
3347               GLuint i;
3348               for (i = 0; i < n; i++) {
3349                  dst[i] = (GLushort) (indexes[i] & 0xffff);
3350               }
3351            }
3352            break;
3353         case GL_UNSIGNED_INT:
3354            MEMCPY(dest, indexes, n * sizeof(GLuint));
3355            break;
3356         default:
3357            _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
3358      }
3359   }
3360}
3361
3362
3363void
3364_mesa_pack_stencil_span( const GLcontext *ctx, GLuint n,
3365                         GLenum dstType, GLvoid *dest, const GLstencil *source,
3366                         const struct gl_pixelstore_attrib *dstPacking )
3367{
3368   GLstencil stencil[MAX_WIDTH];
3369
3370   ASSERT(n <= MAX_WIDTH);
3371
3372   if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
3373       ctx->Pixel.MapStencilFlag) {
3374      /* make a copy of input */
3375      MEMCPY(stencil, source, n * sizeof(GLstencil));
3376      if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
3377         _mesa_shift_and_offset_stencil( ctx, n, stencil );
3378      }
3379      if (ctx->Pixel.MapStencilFlag) {
3380         _mesa_map_stencil( ctx, n, stencil );
3381      }
3382      source = stencil;
3383   }
3384
3385   switch (dstType) {
3386   case GL_UNSIGNED_BYTE:
3387      if (sizeof(GLstencil) == 8) {
3388         MEMCPY( dest, source, n );
3389      }
3390      else {
3391         GLubyte *dst = (GLubyte *) dest;
3392         GLuint i;
3393         for (i=0;i<n;i++) {
3394            dst[i] = (GLubyte) source[i];
3395         }
3396      }
3397      break;
3398   case GL_BYTE:
3399      if (sizeof(GLstencil) == 8) {
3400         MEMCPY( dest, source, n );
3401      }
3402      else {
3403         GLbyte *dst = (GLbyte *) dest;
3404         GLuint i;
3405         for (i=0;i<n;i++) {
3406            dst[i] = (GLbyte) source[i];
3407         }
3408      }
3409      break;
3410   case GL_UNSIGNED_SHORT:
3411      {
3412         GLushort *dst = (GLushort *) dest;
3413         GLuint i;
3414         for (i=0;i<n;i++) {
3415            dst[i] = (GLushort) source[i];
3416         }
3417         if (dstPacking->SwapBytes) {
3418            _mesa_swap2( (GLushort *) dst, n );
3419         }
3420      }
3421      break;
3422   case GL_SHORT:
3423      {
3424         GLshort *dst = (GLshort *) dest;
3425         GLuint i;
3426         for (i=0;i<n;i++) {
3427            dst[i] = (GLshort) source[i];
3428         }
3429         if (dstPacking->SwapBytes) {
3430            _mesa_swap2( (GLushort *) dst, n );
3431         }
3432      }
3433      break;
3434   case GL_UNSIGNED_INT:
3435      {
3436         GLuint *dst = (GLuint *) dest;
3437         GLuint i;
3438         for (i=0;i<n;i++) {
3439            dst[i] = (GLuint) source[i];
3440         }
3441         if (dstPacking->SwapBytes) {
3442            _mesa_swap4( (GLuint *) dst, n );
3443         }
3444      }
3445      break;
3446   case GL_INT:
3447      {
3448         GLint *dst = (GLint *) dest;
3449         GLuint i;
3450         for (i=0;i<n;i++) {
3451            *dst++ = (GLint) source[i];
3452         }
3453         if (dstPacking->SwapBytes) {
3454            _mesa_swap4( (GLuint *) dst, n );
3455         }
3456      }
3457      break;
3458   case GL_FLOAT:
3459      {
3460         GLfloat *dst = (GLfloat *) dest;
3461         GLuint i;
3462         for (i=0;i<n;i++) {
3463            dst[i] = (GLfloat) source[i];
3464         }
3465         if (dstPacking->SwapBytes) {
3466            _mesa_swap4( (GLuint *) dst, n );
3467         }
3468      }
3469      break;
3470   case GL_BITMAP:
3471      if (dstPacking->LsbFirst) {
3472         GLubyte *dst = (GLubyte *) dest;
3473         GLint shift = 0;
3474         GLuint i;
3475         for (i = 0; i < n; i++) {
3476            if (shift == 0)
3477               *dst = 0;
3478            *dst |= ((source[i] != 0) << shift);
3479            shift++;
3480            if (shift == 8) {
3481               shift = 0;
3482               dst++;
3483            }
3484         }
3485      }
3486      else {
3487         GLubyte *dst = (GLubyte *) dest;
3488         GLint shift = 7;
3489         GLuint i;
3490         for (i = 0; i < n; i++) {
3491            if (shift == 7)
3492               *dst = 0;
3493            *dst |= ((source[i] != 0) << shift);
3494            shift--;
3495            if (shift < 0) {
3496               shift = 7;
3497               dst++;
3498            }
3499         }
3500      }
3501      break;
3502   default:
3503      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
3504   }
3505}
3506
3507
3508
3509void
3510_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLfloat *dest,
3511                         GLenum srcType, const GLvoid *source,
3512                         const struct gl_pixelstore_attrib *srcPacking )
3513{
3514   switch (srcType) {
3515      case GL_BYTE:
3516         {
3517            GLuint i;
3518            const GLubyte *src = (const GLubyte *) source;
3519            for (i = 0; i < n; i++) {
3520               dest[i] = BYTE_TO_FLOAT(src[i]);
3521            }
3522         }
3523         break;
3524      case GL_UNSIGNED_BYTE:
3525         {
3526            GLuint i;
3527            const GLubyte *src = (const GLubyte *) source;
3528            for (i = 0; i < n; i++) {
3529               dest[i] = UBYTE_TO_FLOAT(src[i]);
3530            }
3531         }
3532         break;
3533      case GL_SHORT:
3534         {
3535            GLuint i;
3536            const GLshort *src = (const GLshort *) source;
3537            for (i = 0; i < n; i++) {
3538               dest[i] = SHORT_TO_FLOAT(src[i]);
3539            }
3540         }
3541         break;
3542      case GL_UNSIGNED_SHORT:
3543         {
3544            GLuint i;
3545            const GLushort *src = (const GLushort *) source;
3546            for (i = 0; i < n; i++) {
3547               dest[i] = USHORT_TO_FLOAT(src[i]);
3548            }
3549         }
3550         break;
3551      case GL_INT:
3552         {
3553            GLuint i;
3554            const GLint *src = (const GLint *) source;
3555            for (i = 0; i < n; i++) {
3556               dest[i] = INT_TO_FLOAT(src[i]);
3557            }
3558         }
3559         break;
3560      case GL_UNSIGNED_INT:
3561         {
3562            GLuint i;
3563            const GLuint *src = (const GLuint *) source;
3564            for (i = 0; i < n; i++) {
3565               dest[i] = UINT_TO_FLOAT(src[i]);
3566            }
3567         }
3568         break;
3569      case GL_FLOAT:
3570         MEMCPY(dest, source, n * sizeof(GLfloat));
3571         break;
3572      default:
3573         _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
3574         return;
3575   }
3576
3577
3578   /* apply depth scale and bias and clamp to [0,1] */
3579   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
3580      GLuint i;
3581      for (i = 0; i < n; i++) {
3582         GLfloat d = dest[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
3583         dest[i] = CLAMP(d, 0.0F, 1.0F);
3584      }
3585   }
3586}
3587
3588
3589
3590/*
3591 * Pack an array of depth values.  The values are floats in [0,1].
3592 */
3593void
3594_mesa_pack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
3595                       GLenum dstType, const GLfloat *depthSpan,
3596                       const struct gl_pixelstore_attrib *dstPacking )
3597{
3598   GLfloat depthCopy[MAX_WIDTH];
3599   const GLboolean bias_or_scale = ctx->Pixel.DepthBias != 0.0 ||
3600                                   ctx->Pixel.DepthScale != 1.0;
3601
3602   ASSERT(n <= MAX_WIDTH);
3603
3604   if (bias_or_scale) {
3605      GLuint i;
3606      for (i = 0; i < n; i++) {
3607         GLfloat d;
3608         d = depthSpan[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
3609         depthCopy[i] = CLAMP(d, 0.0F, 1.0F);
3610      }
3611      depthSpan = depthCopy;
3612   }
3613
3614   switch (dstType) {
3615   case GL_UNSIGNED_BYTE:
3616      {
3617         GLubyte *dst = (GLubyte *) dest;
3618         GLuint i;
3619         for (i = 0; i < n; i++) {
3620            dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
3621         }
3622      }
3623      break;
3624   case GL_BYTE:
3625      {
3626         GLbyte *dst = (GLbyte *) dest;
3627         GLuint i;
3628         for (i = 0; i < n; i++) {
3629            dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
3630         }
3631      }
3632      break;
3633   case GL_UNSIGNED_SHORT:
3634      {
3635         GLushort *dst = (GLushort *) dest;
3636         GLuint i;
3637         for (i = 0; i < n; i++) {
3638            dst[i] = FLOAT_TO_USHORT( depthSpan[i] );
3639         }
3640         if (dstPacking->SwapBytes) {
3641            _mesa_swap2( (GLushort *) dst, n );
3642         }
3643      }
3644      break;
3645   case GL_SHORT:
3646      {
3647         GLshort *dst = (GLshort *) dest;
3648         GLuint i;
3649         for (i = 0; i < n; i++) {
3650            dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
3651         }
3652         if (dstPacking->SwapBytes) {
3653            _mesa_swap2( (GLushort *) dst, n );
3654         }
3655      }
3656      break;
3657   case GL_UNSIGNED_INT:
3658      {
3659         GLuint *dst = (GLuint *) dest;
3660         GLuint i;
3661         for (i = 0; i < n; i++) {
3662            dst[i] = FLOAT_TO_UINT( depthSpan[i] );
3663         }
3664         if (dstPacking->SwapBytes) {
3665            _mesa_swap4( (GLuint *) dst, n );
3666         }
3667      }
3668      break;
3669   case GL_INT:
3670      {
3671         GLint *dst = (GLint *) dest;
3672         GLuint i;
3673         for (i = 0; i < n; i++) {
3674            dst[i] = FLOAT_TO_INT( depthSpan[i] );
3675         }
3676         if (dstPacking->SwapBytes) {
3677            _mesa_swap4( (GLuint *) dst, n );
3678         }
3679      }
3680      break;
3681   case GL_FLOAT:
3682      {
3683         GLfloat *dst = (GLfloat *) dest;
3684         GLuint i;
3685         for (i = 0; i < n; i++) {
3686            dst[i] = depthSpan[i];
3687         }
3688         if (dstPacking->SwapBytes) {
3689            _mesa_swap4( (GLuint *) dst, n );
3690         }
3691      }
3692      break;
3693   default:
3694      _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
3695   }
3696}
3697
3698
3699
3700
3701/*
3702 * Unpack image data.  Apply byteswapping, byte flipping (bitmap).
3703 * Return all image data in a contiguous block.
3704 */
3705void *
3706_mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
3707                    GLenum format, GLenum type, const GLvoid *pixels,
3708                    const struct gl_pixelstore_attrib *unpack )
3709{
3710   GLint bytesPerRow, compsPerRow;
3711   GLboolean flipBytes, swap2, swap4;
3712
3713   if (!pixels)
3714      return NULL;  /* not necessarily an error */
3715
3716   if (width <= 0 || height <= 0 || depth <= 0)
3717      return NULL;  /* generate error later */
3718
3719   if (format == GL_BITMAP) {
3720      bytesPerRow = (width + 7) >> 3;
3721      flipBytes = !unpack->LsbFirst;
3722      swap2 = swap4 = GL_FALSE;
3723      compsPerRow = 0;
3724   }
3725   else {
3726      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
3727      const GLint components = _mesa_components_in_format(format);
3728      GLint bytesPerComp;
3729      if (bytesPerPixel <= 0 || components <= 0)
3730         return NULL;   /* bad format or type.  generate error later */
3731      bytesPerRow = bytesPerPixel * width;
3732      bytesPerComp = bytesPerPixel / components;
3733      flipBytes = GL_FALSE;
3734      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
3735      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
3736      compsPerRow = components * width;
3737      assert(compsPerRow >= width);
3738   }
3739
3740   {
3741      GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
3742      GLubyte *dst;
3743      GLint img, row;
3744      if (!destBuffer)
3745         return NULL;   /* generate GL_OUT_OF_MEMORY later */
3746
3747      dst = destBuffer;
3748      for (img = 0; img < depth; img++) {
3749         for (row = 0; row < height; row++) {
3750            const GLvoid *src = _mesa_image_address(unpack, pixels,
3751                               width, height, format, type, img, row, 0);
3752            MEMCPY(dst, src, bytesPerRow);
3753            /* byte flipping/swapping */
3754            if (flipBytes) {
3755               flip_bytes((GLubyte *) dst, bytesPerRow);
3756            }
3757            else if (swap2) {
3758               _mesa_swap2((GLushort*) dst, compsPerRow);
3759            }
3760            else if (swap4) {
3761               _mesa_swap4((GLuint*) dst, compsPerRow);
3762            }
3763            dst += bytesPerRow;
3764         }
3765      }
3766      return destBuffer;
3767   }
3768}
3769