image.c revision 03bafd1f9fa000abdb794b2ae344a68840c83201
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.1
4 *
5 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26/**
27 * \file image.c
28 * Image handling.
29 */
30
31
32#include "glheader.h"
33#include "colormac.h"
34#include "context.h"
35#include "image.h"
36#include "imports.h"
37#include "macros.h"
38#include "pixel.h"
39
40
41/**
42 * NOTE:
43 * Normally, BYTE_TO_FLOAT(0) returns 0.00392  That causes problems when
44 * we later convert the float to a packed integer value (such as for
45 * GL_RGB5_A1) because we'll wind up with a non-zero value.
46 *
47 * We redefine the macros here so zero is handled correctly.
48 */
49#undef BYTE_TO_FLOAT
50#define BYTE_TO_FLOAT(B)    ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
51
52#undef SHORT_TO_FLOAT
53#define SHORT_TO_FLOAT(S)   ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
54
55
56
57/** Compute ceiling of integer quotient of A divided by B. */
58#define CEILING( A, B )  ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
59
60
61/**
62 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
63 */
64static GLboolean
65_mesa_type_is_packed(GLenum type)
66{
67   switch (type) {
68   case GL_UNSIGNED_BYTE_3_3_2:
69   case GL_UNSIGNED_BYTE_2_3_3_REV:
70   case GL_UNSIGNED_SHORT_5_6_5:
71   case GL_UNSIGNED_SHORT_5_6_5_REV:
72   case GL_UNSIGNED_SHORT_4_4_4_4:
73   case GL_UNSIGNED_SHORT_4_4_4_4_REV:
74   case GL_UNSIGNED_SHORT_5_5_5_1:
75   case GL_UNSIGNED_SHORT_1_5_5_5_REV:
76   case GL_UNSIGNED_INT_8_8_8_8:
77   case GL_UNSIGNED_INT_8_8_8_8_REV:
78   case GL_UNSIGNED_INT_10_10_10_2:
79   case GL_UNSIGNED_INT_2_10_10_10_REV:
80   case GL_UNSIGNED_SHORT_8_8_MESA:
81   case GL_UNSIGNED_SHORT_8_8_REV_MESA:
82   case GL_UNSIGNED_INT_24_8_EXT:
83      return GL_TRUE;
84   }
85
86   return GL_FALSE;
87}
88
89/**
90 * Flip the 8 bits in each byte of the given array.
91 *
92 * \param p array.
93 * \param n number of bytes.
94 *
95 * \todo try this trick to flip bytes someday:
96 * \code
97 *  v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
98 *  v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
99 *  v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
100 * \endcode
101 */
102static void
103flip_bytes( GLubyte *p, GLuint n )
104{
105   GLuint i, a, b;
106   for (i = 0; i < n; i++) {
107      b = (GLuint) p[i];        /* words are often faster than bytes */
108      a = ((b & 0x01) << 7) |
109	  ((b & 0x02) << 5) |
110	  ((b & 0x04) << 3) |
111	  ((b & 0x08) << 1) |
112	  ((b & 0x10) >> 1) |
113	  ((b & 0x20) >> 3) |
114	  ((b & 0x40) >> 5) |
115	  ((b & 0x80) >> 7);
116      p[i] = (GLubyte) a;
117   }
118}
119
120
121/**
122 * Flip the order of the 2 bytes in each word in the given array.
123 *
124 * \param p array.
125 * \param n number of words.
126 */
127void
128_mesa_swap2( GLushort *p, GLuint n )
129{
130   GLuint i;
131   for (i = 0; i < n; i++) {
132      p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
133   }
134}
135
136
137
138/*
139 * Flip the order of the 4 bytes in each word in the given array.
140 */
141void
142_mesa_swap4( GLuint *p, GLuint n )
143{
144   GLuint i, a, b;
145   for (i = 0; i < n; i++) {
146      b = p[i];
147      a =  (b >> 24)
148	| ((b >> 8) & 0xff00)
149	| ((b << 8) & 0xff0000)
150	| ((b << 24) & 0xff000000);
151      p[i] = a;
152   }
153}
154
155
156/**
157 * Get the size of a GL data type.
158 *
159 * \param type GL data type.
160 *
161 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
162 * if an invalid type enum.
163 */
164GLint
165_mesa_sizeof_type( GLenum type )
166{
167   switch (type) {
168      case GL_BITMAP:
169	 return 0;
170      case GL_UNSIGNED_BYTE:
171         return sizeof(GLubyte);
172      case GL_BYTE:
173	 return sizeof(GLbyte);
174      case GL_UNSIGNED_SHORT:
175	 return sizeof(GLushort);
176      case GL_SHORT:
177	 return sizeof(GLshort);
178      case GL_UNSIGNED_INT:
179	 return sizeof(GLuint);
180      case GL_INT:
181	 return sizeof(GLint);
182      case GL_FLOAT:
183	 return sizeof(GLfloat);
184      case GL_HALF_FLOAT_ARB:
185	 return sizeof(GLhalfARB);
186      default:
187         return -1;
188   }
189}
190
191
192/**
193 * Same as _mesa_sizeof_type() but also accepting the packed pixel
194 * format data types.
195 */
196GLint
197_mesa_sizeof_packed_type( GLenum type )
198{
199   switch (type) {
200      case GL_BITMAP:
201	 return 0;
202      case GL_UNSIGNED_BYTE:
203         return sizeof(GLubyte);
204      case GL_BYTE:
205	 return sizeof(GLbyte);
206      case GL_UNSIGNED_SHORT:
207	 return sizeof(GLushort);
208      case GL_SHORT:
209	 return sizeof(GLshort);
210      case GL_UNSIGNED_INT:
211	 return sizeof(GLuint);
212      case GL_INT:
213	 return sizeof(GLint);
214      case GL_HALF_FLOAT_ARB:
215	 return sizeof(GLhalfARB);
216      case GL_FLOAT:
217	 return sizeof(GLfloat);
218      case GL_UNSIGNED_BYTE_3_3_2:
219         return sizeof(GLubyte);
220      case GL_UNSIGNED_BYTE_2_3_3_REV:
221         return sizeof(GLubyte);
222      case GL_UNSIGNED_SHORT_5_6_5:
223         return sizeof(GLushort);
224      case GL_UNSIGNED_SHORT_5_6_5_REV:
225         return sizeof(GLushort);
226      case GL_UNSIGNED_SHORT_4_4_4_4:
227         return sizeof(GLushort);
228      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
229         return sizeof(GLushort);
230      case GL_UNSIGNED_SHORT_5_5_5_1:
231         return sizeof(GLushort);
232      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
233         return sizeof(GLushort);
234      case GL_UNSIGNED_INT_8_8_8_8:
235         return sizeof(GLuint);
236      case GL_UNSIGNED_INT_8_8_8_8_REV:
237         return sizeof(GLuint);
238      case GL_UNSIGNED_INT_10_10_10_2:
239         return sizeof(GLuint);
240      case GL_UNSIGNED_INT_2_10_10_10_REV:
241         return sizeof(GLuint);
242      case GL_UNSIGNED_SHORT_8_8_MESA:
243      case GL_UNSIGNED_SHORT_8_8_REV_MESA:
244         return sizeof(GLushort);
245      case GL_UNSIGNED_INT_24_8_EXT:
246         return sizeof(GLuint);
247      default:
248         return -1;
249   }
250}
251
252
253/**
254 * Get the number of components in a pixel format.
255 *
256 * \param format pixel format.
257 *
258 * \return the number of components in the given format, or -1 if a bad format.
259 */
260GLint
261_mesa_components_in_format( GLenum format )
262{
263   switch (format) {
264      case GL_COLOR_INDEX:
265      case GL_COLOR_INDEX1_EXT:
266      case GL_COLOR_INDEX2_EXT:
267      case GL_COLOR_INDEX4_EXT:
268      case GL_COLOR_INDEX8_EXT:
269      case GL_COLOR_INDEX12_EXT:
270      case GL_COLOR_INDEX16_EXT:
271      case GL_STENCIL_INDEX:
272      case GL_DEPTH_COMPONENT:
273      case GL_RED:
274      case GL_GREEN:
275      case GL_BLUE:
276      case GL_ALPHA:
277      case GL_LUMINANCE:
278      case GL_INTENSITY:
279         return 1;
280      case GL_LUMINANCE_ALPHA:
281	 return 2;
282      case GL_RGB:
283	 return 3;
284      case GL_RGBA:
285	 return 4;
286      case GL_BGR:
287	 return 3;
288      case GL_BGRA:
289	 return 4;
290      case GL_ABGR_EXT:
291         return 4;
292      case GL_YCBCR_MESA:
293         return 2;
294      case GL_DEPTH_STENCIL_EXT:
295         return 2;
296      default:
297         return -1;
298   }
299}
300
301
302/**
303 * Get the bytes per pixel of pixel format type pair.
304 *
305 * \param format pixel format.
306 * \param type pixel type.
307 *
308 * \return bytes per pixel, or -1 if a bad format or type was given.
309 */
310GLint
311_mesa_bytes_per_pixel( GLenum format, GLenum type )
312{
313   GLint comps = _mesa_components_in_format( format );
314   if (comps < 0)
315      return -1;
316
317   switch (type) {
318      case GL_BITMAP:
319         return 0;  /* special case */
320      case GL_BYTE:
321      case GL_UNSIGNED_BYTE:
322         return comps * sizeof(GLubyte);
323      case GL_SHORT:
324      case GL_UNSIGNED_SHORT:
325         return comps * sizeof(GLshort);
326      case GL_INT:
327      case GL_UNSIGNED_INT:
328         return comps * sizeof(GLint);
329      case GL_FLOAT:
330         return comps * sizeof(GLfloat);
331      case GL_HALF_FLOAT_ARB:
332         return comps * sizeof(GLhalfARB);
333      case GL_UNSIGNED_BYTE_3_3_2:
334      case GL_UNSIGNED_BYTE_2_3_3_REV:
335         if (format == GL_RGB || format == GL_BGR)
336            return sizeof(GLubyte);
337         else
338            return -1;  /* error */
339      case GL_UNSIGNED_SHORT_5_6_5:
340      case GL_UNSIGNED_SHORT_5_6_5_REV:
341         if (format == GL_RGB || format == GL_BGR)
342            return sizeof(GLushort);
343         else
344            return -1;  /* error */
345      case GL_UNSIGNED_SHORT_4_4_4_4:
346      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
347      case GL_UNSIGNED_SHORT_5_5_5_1:
348      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
349         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
350            return sizeof(GLushort);
351         else
352            return -1;
353      case GL_UNSIGNED_INT_8_8_8_8:
354      case GL_UNSIGNED_INT_8_8_8_8_REV:
355      case GL_UNSIGNED_INT_10_10_10_2:
356      case GL_UNSIGNED_INT_2_10_10_10_REV:
357         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
358            return sizeof(GLuint);
359         else
360            return -1;
361      case GL_UNSIGNED_SHORT_8_8_MESA:
362      case GL_UNSIGNED_SHORT_8_8_REV_MESA:
363         if (format == GL_YCBCR_MESA)
364            return sizeof(GLushort);
365         else
366            return -1;
367      case GL_UNSIGNED_INT_24_8_EXT:
368         if (format == GL_DEPTH_STENCIL_EXT)
369            return sizeof(GLuint);
370         else
371            return -1;
372      default:
373         return -1;
374   }
375}
376
377
378/**
379 * Test for a legal pixel format and type.
380 *
381 * \param format pixel format.
382 * \param type pixel type.
383 *
384 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
385 * otherwise.
386 */
387GLboolean
388_mesa_is_legal_format_and_type( GLcontext *ctx, GLenum format, GLenum type )
389{
390   switch (format) {
391      case GL_COLOR_INDEX:
392      case GL_STENCIL_INDEX:
393         switch (type) {
394            case GL_BITMAP:
395            case GL_BYTE:
396            case GL_UNSIGNED_BYTE:
397            case GL_SHORT:
398            case GL_UNSIGNED_SHORT:
399            case GL_INT:
400            case GL_UNSIGNED_INT:
401            case GL_FLOAT:
402               return GL_TRUE;
403            case GL_HALF_FLOAT_ARB:
404               return ctx->Extensions.ARB_half_float_pixel;
405            default:
406               return GL_FALSE;
407         }
408      case GL_RED:
409      case GL_GREEN:
410      case GL_BLUE:
411      case GL_ALPHA:
412#if 0 /* not legal!  see table 3.6 of the 1.5 spec */
413      case GL_INTENSITY:
414#endif
415      case GL_LUMINANCE:
416      case GL_LUMINANCE_ALPHA:
417      case GL_DEPTH_COMPONENT:
418         switch (type) {
419            case GL_BYTE:
420            case GL_UNSIGNED_BYTE:
421            case GL_SHORT:
422            case GL_UNSIGNED_SHORT:
423            case GL_INT:
424            case GL_UNSIGNED_INT:
425            case GL_FLOAT:
426               return GL_TRUE;
427            case GL_HALF_FLOAT_ARB:
428               return ctx->Extensions.ARB_half_float_pixel;
429            default:
430               return GL_FALSE;
431         }
432      case GL_RGB:
433         switch (type) {
434            case GL_BYTE:
435            case GL_UNSIGNED_BYTE:
436            case GL_SHORT:
437            case GL_UNSIGNED_SHORT:
438            case GL_INT:
439            case GL_UNSIGNED_INT:
440            case GL_FLOAT:
441            case GL_UNSIGNED_BYTE_3_3_2:
442            case GL_UNSIGNED_BYTE_2_3_3_REV:
443            case GL_UNSIGNED_SHORT_5_6_5:
444            case GL_UNSIGNED_SHORT_5_6_5_REV:
445               return GL_TRUE;
446            case GL_HALF_FLOAT_ARB:
447               return ctx->Extensions.ARB_half_float_pixel;
448            default:
449               return GL_FALSE;
450         }
451      case GL_BGR:
452         switch (type) {
453            /* NOTE: no packed types are supported with BGR.  That's
454             * intentional, according to the GL spec.
455             */
456            case GL_BYTE:
457            case GL_UNSIGNED_BYTE:
458            case GL_SHORT:
459            case GL_UNSIGNED_SHORT:
460            case GL_INT:
461            case GL_UNSIGNED_INT:
462            case GL_FLOAT:
463               return GL_TRUE;
464            case GL_HALF_FLOAT_ARB:
465               return ctx->Extensions.ARB_half_float_pixel;
466            default:
467               return GL_FALSE;
468         }
469      case GL_RGBA:
470      case GL_BGRA:
471      case GL_ABGR_EXT:
472         switch (type) {
473            case GL_BYTE:
474            case GL_UNSIGNED_BYTE:
475            case GL_SHORT:
476            case GL_UNSIGNED_SHORT:
477            case GL_INT:
478            case GL_UNSIGNED_INT:
479            case GL_FLOAT:
480            case GL_UNSIGNED_SHORT_4_4_4_4:
481            case GL_UNSIGNED_SHORT_4_4_4_4_REV:
482            case GL_UNSIGNED_SHORT_5_5_5_1:
483            case GL_UNSIGNED_SHORT_1_5_5_5_REV:
484            case GL_UNSIGNED_INT_8_8_8_8:
485            case GL_UNSIGNED_INT_8_8_8_8_REV:
486            case GL_UNSIGNED_INT_10_10_10_2:
487            case GL_UNSIGNED_INT_2_10_10_10_REV:
488               return GL_TRUE;
489            case GL_HALF_FLOAT_ARB:
490               return ctx->Extensions.ARB_half_float_pixel;
491            default:
492               return GL_FALSE;
493         }
494      case GL_YCBCR_MESA:
495         if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
496             type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
497            return GL_TRUE;
498         else
499            return GL_FALSE;
500      case GL_DEPTH_STENCIL_EXT:
501         if (ctx->Extensions.EXT_packed_depth_stencil
502             && type == GL_UNSIGNED_INT_24_8_EXT)
503            return GL_TRUE;
504         else
505            return GL_FALSE;
506      default:
507         ; /* fall-through */
508   }
509   return GL_FALSE;
510}
511
512
513/**
514 * Return the address of a specific pixel in an image (1D, 2D or 3D).
515 *
516 * Pixel unpacking/packing parameters are observed according to \p packing.
517 *
518 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
519 * \param image  starting address of image data
520 * \param width  the image width
521 * \param height  theimage height
522 * \param format  the pixel format
523 * \param type  the pixel data type
524 * \param packing  the pixelstore attributes
525 * \param img  which image in the volume (0 for 1D or 2D images)
526 * \param row  row of pixel in the image (0 for 1D images)
527 * \param column column of pixel in the image
528 *
529 * \return address of pixel on success, or NULL on error.
530 *
531 * \sa gl_pixelstore_attrib.
532 */
533GLvoid *
534_mesa_image_address( GLuint dimensions,
535                     const struct gl_pixelstore_attrib *packing,
536                     const GLvoid *image,
537                     GLsizei width, GLsizei height,
538                     GLenum format, GLenum type,
539                     GLint img, GLint row, GLint column )
540{
541   GLint alignment;        /* 1, 2 or 4 */
542   GLint pixels_per_row;
543   GLint rows_per_image;
544   GLint skiprows;
545   GLint skippixels;
546   GLint skipimages;       /* for 3-D volume images */
547   GLubyte *pixel_addr;
548
549   ASSERT(dimensions >= 1 && dimensions <= 3);
550
551   alignment = packing->Alignment;
552   if (packing->RowLength > 0) {
553      pixels_per_row = packing->RowLength;
554   }
555   else {
556      pixels_per_row = width;
557   }
558   if (packing->ImageHeight > 0) {
559      rows_per_image = packing->ImageHeight;
560   }
561   else {
562      rows_per_image = height;
563   }
564
565   skippixels = packing->SkipPixels;
566   /* Note: SKIP_ROWS _is_ used for 1D images */
567   skiprows = packing->SkipRows;
568   /* Note: SKIP_IMAGES is only used for 3D images */
569   skipimages = (dimensions == 3) ? packing->SkipImages : 0;
570
571   if (type == GL_BITMAP) {
572      /* BITMAP data */
573      GLint comp_per_pixel;   /* components per pixel */
574      GLint bytes_per_comp;   /* bytes per component */
575      GLint bytes_per_row;
576      GLint bytes_per_image;
577
578      /* Compute bytes per component */
579      bytes_per_comp = _mesa_sizeof_packed_type( type );
580      if (bytes_per_comp < 0) {
581         return NULL;
582      }
583
584      /* Compute number of components per pixel */
585      comp_per_pixel = _mesa_components_in_format( format );
586      if (comp_per_pixel < 0) {
587         return NULL;
588      }
589
590      bytes_per_row = alignment
591                    * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
592
593      bytes_per_image = bytes_per_row * rows_per_image;
594
595      pixel_addr = (GLubyte *) image
596                 + (skipimages + img) * bytes_per_image
597                 + (skiprows + row) * bytes_per_row
598                 + (skippixels + column) / 8;
599   }
600   else {
601      /* Non-BITMAP data */
602      GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
603      GLint topOfImage;
604
605      bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
606
607      /* The pixel type and format should have been error checked earlier */
608      assert(bytes_per_pixel > 0);
609
610      bytes_per_row = pixels_per_row * bytes_per_pixel;
611      remainder = bytes_per_row % alignment;
612      if (remainder > 0)
613         bytes_per_row += (alignment - remainder);
614
615      ASSERT(bytes_per_row % alignment == 0);
616
617      bytes_per_image = bytes_per_row * rows_per_image;
618
619      if (packing->Invert) {
620         /* set pixel_addr to the last row */
621         topOfImage = bytes_per_row * (height - 1);
622         bytes_per_row = -bytes_per_row;
623      }
624      else {
625         topOfImage = 0;
626      }
627
628      /* compute final pixel address */
629      pixel_addr = (GLubyte *) image
630                 + (skipimages + img) * bytes_per_image
631                 + topOfImage
632                 + (skiprows + row) * bytes_per_row
633                 + (skippixels + column) * bytes_per_pixel;
634   }
635
636   return (GLvoid *) pixel_addr;
637}
638
639
640GLvoid *
641_mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
642                       const GLvoid *image,
643                       GLsizei width,
644                       GLenum format, GLenum type,
645                       GLint column )
646{
647   return _mesa_image_address(1, packing, image, width, 1,
648                              format, type, 0, 0, column);
649}
650
651
652GLvoid *
653_mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
654                       const GLvoid *image,
655                       GLsizei width, GLsizei height,
656                       GLenum format, GLenum type,
657                       GLint row, GLint column )
658{
659   return _mesa_image_address(2, packing, image, width, height,
660                              format, type, 0, row, column);
661}
662
663
664GLvoid *
665_mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
666                       const GLvoid *image,
667                       GLsizei width, GLsizei height,
668                       GLenum format, GLenum type,
669                       GLint img, GLint row, GLint column )
670{
671   return _mesa_image_address(3, packing, image, width, height,
672                              format, type, img, row, column);
673}
674
675
676
677/**
678 * Compute the stride (in bytes) between image rows.
679 *
680 * \param packing the pixelstore attributes
681 * \param width image width.
682 * \param format pixel format.
683 * \param type pixel data type.
684 *
685 * \return the stride in bytes for the given parameters, or -1 if error
686 */
687GLint
688_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
689                        GLint width, GLenum format, GLenum type )
690{
691   GLint bytesPerRow, remainder;
692
693   ASSERT(packing);
694
695   if (type == GL_BITMAP) {
696      if (packing->RowLength == 0) {
697         bytesPerRow = (width + 7) / 8;
698      }
699      else {
700         bytesPerRow = (packing->RowLength + 7) / 8;
701      }
702   }
703   else {
704      /* Non-BITMAP data */
705      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
706      if (bytesPerPixel <= 0)
707         return -1;  /* error */
708      if (packing->RowLength == 0) {
709         bytesPerRow = bytesPerPixel * width;
710      }
711      else {
712         bytesPerRow = bytesPerPixel * packing->RowLength;
713      }
714   }
715
716   remainder = bytesPerRow % packing->Alignment;
717   if (remainder > 0) {
718      bytesPerRow += (packing->Alignment - remainder);
719   }
720
721   if (packing->Invert) {
722      /* negate the bytes per row (negative row stride) */
723      bytesPerRow = -bytesPerRow;
724   }
725
726   return bytesPerRow;
727}
728
729
730#if _HAVE_FULL_GL
731
732/*
733 * Compute the stride between images in a 3D texture (in bytes) for the given
734 * pixel packing parameters and image width, format and type.
735 */
736GLint
737_mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
738                          GLint width, GLint height,
739                          GLenum format, GLenum type )
740{
741   ASSERT(packing);
742   ASSERT(type != GL_BITMAP);
743
744   {
745      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
746      GLint bytesPerRow, bytesPerImage, remainder;
747
748      if (bytesPerPixel <= 0)
749         return -1;  /* error */
750      if (packing->RowLength == 0) {
751         bytesPerRow = bytesPerPixel * width;
752      }
753      else {
754         bytesPerRow = bytesPerPixel * packing->RowLength;
755      }
756      remainder = bytesPerRow % packing->Alignment;
757      if (remainder > 0)
758         bytesPerRow += (packing->Alignment - remainder);
759
760      if (packing->ImageHeight == 0)
761         bytesPerImage = bytesPerRow * height;
762      else
763         bytesPerImage = bytesPerRow * packing->ImageHeight;
764
765      return bytesPerImage;
766   }
767}
768
769
770/*
771 * Unpack a 32x32 pixel polygon stipple from user memory using the
772 * current pixel unpack settings.
773 */
774void
775_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
776                              const struct gl_pixelstore_attrib *unpacking )
777{
778   GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap(32, 32, pattern, unpacking);
779   if (ptrn) {
780      /* Convert pattern from GLubytes to GLuints and handle big/little
781       * endian differences
782       */
783      GLubyte *p = ptrn;
784      GLint i;
785      for (i = 0; i < 32; i++) {
786         dest[i] = (p[0] << 24)
787                 | (p[1] << 16)
788                 | (p[2] <<  8)
789                 | (p[3]      );
790         p += 4;
791      }
792      _mesa_free(ptrn);
793   }
794}
795
796
797/*
798 * Pack polygon stipple into user memory given current pixel packing
799 * settings.
800 */
801void
802_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
803                            const struct gl_pixelstore_attrib *packing )
804{
805   /* Convert pattern from GLuints to GLubytes to handle big/little
806    * endian differences.
807    */
808   GLubyte ptrn[32*4];
809   GLint i;
810   for (i = 0; i < 32; i++) {
811      ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
812      ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
813      ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
814      ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
815   }
816
817   _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
818}
819
820
821/*
822 * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
823 * order with row alignment = 1 byte.
824 */
825GLvoid *
826_mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
827                     const struct gl_pixelstore_attrib *packing )
828{
829   GLint bytes, row, width_in_bytes;
830   GLubyte *buffer, *dst;
831
832   if (!pixels)
833      return NULL;
834
835   /* Alloc dest storage */
836   bytes = ((width + 7) / 8 * height);
837   buffer = (GLubyte *) _mesa_malloc( bytes );
838   if (!buffer)
839      return NULL;
840
841   width_in_bytes = CEILING( width, 8 );
842   dst = buffer;
843   for (row = 0; row < height; row++) {
844      const GLubyte *src = (const GLubyte *)
845         _mesa_image_address2d(packing, pixels, width, height,
846                               GL_COLOR_INDEX, GL_BITMAP, row, 0);
847      if (!src) {
848         _mesa_free(buffer);
849         return NULL;
850      }
851
852      if ((packing->SkipPixels & 7) == 0) {
853         _mesa_memcpy( dst, src, width_in_bytes );
854         if (packing->LsbFirst) {
855            flip_bytes( dst, width_in_bytes );
856         }
857      }
858      else {
859         /* handling SkipPixels is a bit tricky (no pun intended!) */
860         GLint i;
861         if (packing->LsbFirst) {
862            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
863            GLubyte dstMask = 128;
864            const GLubyte *s = src;
865            GLubyte *d = dst;
866            *d = 0;
867            for (i = 0; i < width; i++) {
868               if (*s & srcMask) {
869                  *d |= dstMask;
870               }
871               if (srcMask == 128) {
872                  srcMask = 1;
873                  s++;
874               }
875               else {
876                  srcMask = srcMask << 1;
877               }
878               if (dstMask == 1) {
879                  dstMask = 128;
880                  d++;
881                  *d = 0;
882               }
883               else {
884                  dstMask = dstMask >> 1;
885               }
886            }
887         }
888         else {
889            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
890            GLubyte dstMask = 128;
891            const GLubyte *s = src;
892            GLubyte *d = dst;
893            *d = 0;
894            for (i = 0; i < width; i++) {
895               if (*s & srcMask) {
896                  *d |= dstMask;
897               }
898               if (srcMask == 1) {
899                  srcMask = 128;
900                  s++;
901               }
902               else {
903                  srcMask = srcMask >> 1;
904               }
905               if (dstMask == 1) {
906                  dstMask = 128;
907                  d++;
908                  *d = 0;
909               }
910               else {
911                  dstMask = dstMask >> 1;
912               }
913            }
914         }
915      }
916      dst += width_in_bytes;
917   }
918
919   return buffer;
920}
921
922
923/*
924 * Pack bitmap data.
925 */
926void
927_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
928                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
929{
930   GLint row, width_in_bytes;
931   const GLubyte *src;
932
933   if (!source)
934      return;
935
936   width_in_bytes = CEILING( width, 8 );
937   src = source;
938   for (row = 0; row < height; row++) {
939      GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
940                       width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
941      if (!dst)
942         return;
943
944      if ((packing->SkipPixels & 7) == 0) {
945         _mesa_memcpy( dst, src, width_in_bytes );
946         if (packing->LsbFirst) {
947            flip_bytes( dst, width_in_bytes );
948         }
949      }
950      else {
951         /* handling SkipPixels is a bit tricky (no pun intended!) */
952         GLint i;
953         if (packing->LsbFirst) {
954            GLubyte srcMask = 128;
955            GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
956            const GLubyte *s = src;
957            GLubyte *d = dst;
958            *d = 0;
959            for (i = 0; i < width; i++) {
960               if (*s & srcMask) {
961                  *d |= dstMask;
962               }
963               if (srcMask == 1) {
964                  srcMask = 128;
965                  s++;
966               }
967               else {
968                  srcMask = srcMask >> 1;
969               }
970               if (dstMask == 128) {
971                  dstMask = 1;
972                  d++;
973                  *d = 0;
974               }
975               else {
976                  dstMask = dstMask << 1;
977               }
978            }
979         }
980         else {
981            GLubyte srcMask = 128;
982            GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
983            const GLubyte *s = src;
984            GLubyte *d = dst;
985            *d = 0;
986            for (i = 0; i < width; i++) {
987               if (*s & srcMask) {
988                  *d |= dstMask;
989               }
990               if (srcMask == 1) {
991                  srcMask = 128;
992                  s++;
993               }
994               else {
995                  srcMask = srcMask >> 1;
996               }
997               if (dstMask == 1) {
998                  dstMask = 128;
999                  d++;
1000                  *d = 0;
1001               }
1002               else {
1003                  dstMask = dstMask >> 1;
1004               }
1005            }
1006         }
1007      }
1008      src += width_in_bytes;
1009   }
1010}
1011
1012
1013/**********************************************************************/
1014/*****                  Pixel processing functions               ******/
1015/**********************************************************************/
1016
1017/*
1018 * Apply scale and bias factors to an array of RGBA pixels.
1019 */
1020void
1021_mesa_scale_and_bias_rgba(GLuint n, GLfloat rgba[][4],
1022                          GLfloat rScale, GLfloat gScale,
1023                          GLfloat bScale, GLfloat aScale,
1024                          GLfloat rBias, GLfloat gBias,
1025                          GLfloat bBias, GLfloat aBias)
1026{
1027   if (rScale != 1.0 || rBias != 0.0) {
1028      GLuint i;
1029      for (i = 0; i < n; i++) {
1030         rgba[i][RCOMP] = rgba[i][RCOMP] * rScale + rBias;
1031      }
1032   }
1033   if (gScale != 1.0 || gBias != 0.0) {
1034      GLuint i;
1035      for (i = 0; i < n; i++) {
1036         rgba[i][GCOMP] = rgba[i][GCOMP] * gScale + gBias;
1037      }
1038   }
1039   if (bScale != 1.0 || bBias != 0.0) {
1040      GLuint i;
1041      for (i = 0; i < n; i++) {
1042         rgba[i][BCOMP] = rgba[i][BCOMP] * bScale + bBias;
1043      }
1044   }
1045   if (aScale != 1.0 || aBias != 0.0) {
1046      GLuint i;
1047      for (i = 0; i < n; i++) {
1048         rgba[i][ACOMP] = rgba[i][ACOMP] * aScale + aBias;
1049      }
1050   }
1051}
1052
1053
1054/*
1055 * Apply pixel mapping to an array of floating point RGBA pixels.
1056 */
1057void
1058_mesa_map_rgba( const GLcontext *ctx, GLuint n, GLfloat rgba[][4] )
1059{
1060   const GLfloat rscale = (GLfloat) (ctx->PixelMaps.RtoR.Size - 1);
1061   const GLfloat gscale = (GLfloat) (ctx->PixelMaps.GtoG.Size - 1);
1062   const GLfloat bscale = (GLfloat) (ctx->PixelMaps.BtoB.Size - 1);
1063   const GLfloat ascale = (GLfloat) (ctx->PixelMaps.AtoA.Size - 1);
1064   const GLfloat *rMap = ctx->PixelMaps.RtoR.Map;
1065   const GLfloat *gMap = ctx->PixelMaps.GtoG.Map;
1066   const GLfloat *bMap = ctx->PixelMaps.BtoB.Map;
1067   const GLfloat *aMap = ctx->PixelMaps.AtoA.Map;
1068   GLuint i;
1069   for (i=0;i<n;i++) {
1070      GLfloat r = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1071      GLfloat g = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1072      GLfloat b = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1073      GLfloat a = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1074      rgba[i][RCOMP] = rMap[IROUND(r * rscale)];
1075      rgba[i][GCOMP] = gMap[IROUND(g * gscale)];
1076      rgba[i][BCOMP] = bMap[IROUND(b * bscale)];
1077      rgba[i][ACOMP] = aMap[IROUND(a * ascale)];
1078   }
1079}
1080
1081
1082/*
1083 * Apply the color matrix and post color matrix scaling and biasing.
1084 */
1085void
1086_mesa_transform_rgba(const GLcontext *ctx, GLuint n, GLfloat rgba[][4])
1087{
1088   const GLfloat rs = ctx->Pixel.PostColorMatrixScale[0];
1089   const GLfloat rb = ctx->Pixel.PostColorMatrixBias[0];
1090   const GLfloat gs = ctx->Pixel.PostColorMatrixScale[1];
1091   const GLfloat gb = ctx->Pixel.PostColorMatrixBias[1];
1092   const GLfloat bs = ctx->Pixel.PostColorMatrixScale[2];
1093   const GLfloat bb = ctx->Pixel.PostColorMatrixBias[2];
1094   const GLfloat as = ctx->Pixel.PostColorMatrixScale[3];
1095   const GLfloat ab = ctx->Pixel.PostColorMatrixBias[3];
1096   const GLfloat *m = ctx->ColorMatrixStack.Top->m;
1097   GLuint i;
1098   for (i = 0; i < n; i++) {
1099      const GLfloat r = rgba[i][RCOMP];
1100      const GLfloat g = rgba[i][GCOMP];
1101      const GLfloat b = rgba[i][BCOMP];
1102      const GLfloat a = rgba[i][ACOMP];
1103      rgba[i][RCOMP] = (m[0] * r + m[4] * g + m[ 8] * b + m[12] * a) * rs + rb;
1104      rgba[i][GCOMP] = (m[1] * r + m[5] * g + m[ 9] * b + m[13] * a) * gs + gb;
1105      rgba[i][BCOMP] = (m[2] * r + m[6] * g + m[10] * b + m[14] * a) * bs + bb;
1106      rgba[i][ACOMP] = (m[3] * r + m[7] * g + m[11] * b + m[15] * a) * as + ab;
1107   }
1108}
1109
1110
1111/**
1112 * Apply a color table lookup to an array of floating point RGBA colors.
1113 */
1114void
1115_mesa_lookup_rgba_float(const struct gl_color_table *table,
1116                        GLuint n, GLfloat rgba[][4])
1117{
1118   const GLint max = table->Size - 1;
1119   const GLfloat scale = (GLfloat) max;
1120   const GLfloat *lut = table->TableF;
1121   GLuint i;
1122
1123   if (!table->TableF || table->Size == 0)
1124      return;
1125
1126   switch (table->_BaseFormat) {
1127      case GL_INTENSITY:
1128         /* replace RGBA with I */
1129         for (i = 0; i < n; i++) {
1130            GLint j = IROUND(rgba[i][RCOMP] * scale);
1131            GLfloat c = lut[CLAMP(j, 0, max)];
1132            rgba[i][RCOMP] =
1133            rgba[i][GCOMP] =
1134            rgba[i][BCOMP] =
1135            rgba[i][ACOMP] = c;
1136         }
1137         break;
1138      case GL_LUMINANCE:
1139         /* replace RGB with L */
1140         for (i = 0; i < n; i++) {
1141            GLint j = IROUND(rgba[i][RCOMP] * scale);
1142            GLfloat c = lut[CLAMP(j, 0, max)];
1143            rgba[i][RCOMP] =
1144            rgba[i][GCOMP] =
1145            rgba[i][BCOMP] = c;
1146         }
1147         break;
1148      case GL_ALPHA:
1149         /* replace A with A */
1150         for (i = 0; i < n; i++) {
1151            GLint j = IROUND(rgba[i][ACOMP] * scale);
1152            rgba[i][ACOMP] = lut[CLAMP(j, 0, max)];
1153         }
1154         break;
1155      case GL_LUMINANCE_ALPHA:
1156         /* replace RGBA with LLLA */
1157         for (i = 0; i < n; i++) {
1158            GLint jL = IROUND(rgba[i][RCOMP] * scale);
1159            GLint jA = IROUND(rgba[i][ACOMP] * scale);
1160            GLfloat luminance, alpha;
1161            jL = CLAMP(jL, 0, max);
1162            jA = CLAMP(jA, 0, max);
1163            luminance = lut[jL * 2 + 0];
1164            alpha     = lut[jA * 2 + 1];
1165            rgba[i][RCOMP] =
1166            rgba[i][GCOMP] =
1167            rgba[i][BCOMP] = luminance;
1168            rgba[i][ACOMP] = alpha;;
1169         }
1170         break;
1171      case GL_RGB:
1172         /* replace RGB with RGB */
1173         for (i = 0; i < n; i++) {
1174            GLint jR = IROUND(rgba[i][RCOMP] * scale);
1175            GLint jG = IROUND(rgba[i][GCOMP] * scale);
1176            GLint jB = IROUND(rgba[i][BCOMP] * scale);
1177            jR = CLAMP(jR, 0, max);
1178            jG = CLAMP(jG, 0, max);
1179            jB = CLAMP(jB, 0, max);
1180            rgba[i][RCOMP] = lut[jR * 3 + 0];
1181            rgba[i][GCOMP] = lut[jG * 3 + 1];
1182            rgba[i][BCOMP] = lut[jB * 3 + 2];
1183         }
1184         break;
1185      case GL_RGBA:
1186         /* replace RGBA with RGBA */
1187         for (i = 0; i < n; i++) {
1188            GLint jR = IROUND(rgba[i][RCOMP] * scale);
1189            GLint jG = IROUND(rgba[i][GCOMP] * scale);
1190            GLint jB = IROUND(rgba[i][BCOMP] * scale);
1191            GLint jA = IROUND(rgba[i][ACOMP] * scale);
1192            jR = CLAMP(jR, 0, max);
1193            jG = CLAMP(jG, 0, max);
1194            jB = CLAMP(jB, 0, max);
1195            jA = CLAMP(jA, 0, max);
1196            rgba[i][RCOMP] = lut[jR * 4 + 0];
1197            rgba[i][GCOMP] = lut[jG * 4 + 1];
1198            rgba[i][BCOMP] = lut[jB * 4 + 2];
1199            rgba[i][ACOMP] = lut[jA * 4 + 3];
1200         }
1201         break;
1202      default:
1203         _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba_float");
1204         return;
1205   }
1206}
1207
1208
1209
1210/**
1211 * Apply a color table lookup to an array of ubyte/RGBA colors.
1212 */
1213void
1214_mesa_lookup_rgba_ubyte(const struct gl_color_table *table,
1215                        GLuint n, GLubyte rgba[][4])
1216{
1217   const GLubyte *lut = table->TableUB;
1218   const GLfloat scale = (GLfloat) (table->Size - 1) / (GLfloat)255.0;
1219   GLuint i;
1220
1221   if (!table->TableUB || table->Size == 0)
1222      return;
1223
1224   switch (table->_BaseFormat) {
1225   case GL_INTENSITY:
1226      /* replace RGBA with I */
1227      if (table->Size == 256) {
1228         for (i = 0; i < n; i++) {
1229            const GLubyte c = lut[rgba[i][RCOMP]];
1230            rgba[i][RCOMP] =
1231            rgba[i][GCOMP] =
1232            rgba[i][BCOMP] =
1233            rgba[i][ACOMP] = c;
1234         }
1235      }
1236      else {
1237         for (i = 0; i < n; i++) {
1238            GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1239            rgba[i][RCOMP] =
1240            rgba[i][GCOMP] =
1241            rgba[i][BCOMP] =
1242            rgba[i][ACOMP] = lut[j];
1243         }
1244      }
1245      break;
1246   case GL_LUMINANCE:
1247      /* replace RGB with L */
1248      if (table->Size == 256) {
1249         for (i = 0; i < n; i++) {
1250            const GLubyte c = lut[rgba[i][RCOMP]];
1251            rgba[i][RCOMP] =
1252            rgba[i][GCOMP] =
1253            rgba[i][BCOMP] = c;
1254         }
1255      }
1256      else {
1257         for (i = 0; i < n; i++) {
1258            GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1259            rgba[i][RCOMP] =
1260            rgba[i][GCOMP] =
1261            rgba[i][BCOMP] = lut[j];
1262         }
1263      }
1264      break;
1265   case GL_ALPHA:
1266      /* replace A with A */
1267      if (table->Size == 256) {
1268         for (i = 0; i < n; i++) {
1269            rgba[i][ACOMP] = lut[rgba[i][ACOMP]];
1270         }
1271      }
1272      else {
1273         for (i = 0; i < n; i++) {
1274            GLint j = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1275            rgba[i][ACOMP] = lut[j];
1276         }
1277      }
1278      break;
1279   case GL_LUMINANCE_ALPHA:
1280      /* replace RGBA with LLLA */
1281      if (table->Size == 256) {
1282         for (i = 0; i < n; i++) {
1283            GLubyte l = lut[rgba[i][RCOMP] * 2 + 0];
1284            GLubyte a = lut[rgba[i][ACOMP] * 2 + 1];;
1285            rgba[i][RCOMP] =
1286            rgba[i][GCOMP] =
1287            rgba[i][BCOMP] = l;
1288            rgba[i][ACOMP] = a;
1289         }
1290      }
1291      else {
1292         for (i = 0; i < n; i++) {
1293            GLint jL = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1294            GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1295            GLubyte luminance = lut[jL * 2 + 0];
1296            GLubyte alpha     = lut[jA * 2 + 1];
1297            rgba[i][RCOMP] =
1298            rgba[i][GCOMP] =
1299            rgba[i][BCOMP] = luminance;
1300            rgba[i][ACOMP] = alpha;
1301         }
1302      }
1303      break;
1304   case GL_RGB:
1305      if (table->Size == 256) {
1306         for (i = 0; i < n; i++) {
1307            rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 3 + 0];
1308            rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 3 + 1];
1309            rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 3 + 2];
1310         }
1311      }
1312      else {
1313         for (i = 0; i < n; i++) {
1314            GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1315            GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
1316            GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
1317            rgba[i][RCOMP] = lut[jR * 3 + 0];
1318            rgba[i][GCOMP] = lut[jG * 3 + 1];
1319            rgba[i][BCOMP] = lut[jB * 3 + 2];
1320         }
1321      }
1322      break;
1323   case GL_RGBA:
1324      if (table->Size == 256) {
1325         for (i = 0; i < n; i++) {
1326            rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 4 + 0];
1327            rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 4 + 1];
1328            rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 4 + 2];
1329            rgba[i][ACOMP] = lut[rgba[i][ACOMP] * 4 + 3];
1330         }
1331      }
1332      else {
1333         for (i = 0; i < n; i++) {
1334            GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1335            GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
1336            GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
1337            GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1338            CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], lut[jR * 4 + 0]);
1339            CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], lut[jG * 4 + 1]);
1340            CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], lut[jB * 4 + 2]);
1341            CLAMPED_FLOAT_TO_CHAN(rgba[i][ACOMP], lut[jA * 4 + 3]);
1342         }
1343      }
1344      break;
1345   default:
1346      _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba_chan");
1347      return;
1348   }
1349}
1350
1351
1352
1353/*
1354 * Map color indexes to float rgba values.
1355 */
1356void
1357_mesa_map_ci_to_rgba( const GLcontext *ctx, GLuint n,
1358                      const GLuint index[], GLfloat rgba[][4] )
1359{
1360   GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1361   GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1362   GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1363   GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1364   const GLfloat *rMap = ctx->PixelMaps.ItoR.Map;
1365   const GLfloat *gMap = ctx->PixelMaps.ItoG.Map;
1366   const GLfloat *bMap = ctx->PixelMaps.ItoB.Map;
1367   const GLfloat *aMap = ctx->PixelMaps.ItoA.Map;
1368   GLuint i;
1369   for (i=0;i<n;i++) {
1370      rgba[i][RCOMP] = rMap[index[i] & rmask];
1371      rgba[i][GCOMP] = gMap[index[i] & gmask];
1372      rgba[i][BCOMP] = bMap[index[i] & bmask];
1373      rgba[i][ACOMP] = aMap[index[i] & amask];
1374   }
1375}
1376
1377
1378/**
1379 * Map ubyte color indexes to ubyte/RGBA values.
1380 */
1381void
1382_mesa_map_ci8_to_rgba8(const GLcontext *ctx, GLuint n, const GLubyte index[],
1383                       GLubyte rgba[][4])
1384{
1385   GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1386   GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1387   GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1388   GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1389   const GLubyte *rMap = ctx->PixelMaps.ItoR.Map8;
1390   const GLubyte *gMap = ctx->PixelMaps.ItoG.Map8;
1391   const GLubyte *bMap = ctx->PixelMaps.ItoB.Map8;
1392   const GLubyte *aMap = ctx->PixelMaps.ItoA.Map8;
1393   GLuint i;
1394   for (i=0;i<n;i++) {
1395      rgba[i][RCOMP] = rMap[index[i] & rmask];
1396      rgba[i][GCOMP] = gMap[index[i] & gmask];
1397      rgba[i][BCOMP] = bMap[index[i] & bmask];
1398      rgba[i][ACOMP] = aMap[index[i] & amask];
1399   }
1400}
1401
1402
1403void
1404_mesa_scale_and_bias_depth(const GLcontext *ctx, GLuint n,
1405                           GLfloat depthValues[])
1406{
1407   const GLfloat scale = ctx->Pixel.DepthScale;
1408   const GLfloat bias = ctx->Pixel.DepthBias;
1409   GLuint i;
1410   for (i = 0; i < n; i++) {
1411      GLfloat d = depthValues[i] * scale + bias;
1412      depthValues[i] = CLAMP(d, 0.0F, 1.0F);
1413   }
1414}
1415
1416
1417void
1418_mesa_scale_and_bias_depth_uint(const GLcontext *ctx, GLuint n,
1419                                GLuint depthValues[])
1420{
1421   const GLdouble max = (double) 0xffffffff;
1422   const GLdouble scale = ctx->Pixel.DepthScale;
1423   const GLdouble bias = ctx->Pixel.DepthBias * max;
1424   GLuint i;
1425   for (i = 0; i < n; i++) {
1426      GLdouble d = (GLdouble) depthValues[i] * scale + bias;
1427      d = CLAMP(d, 0.0, max);
1428      depthValues[i] = (GLuint) d;
1429   }
1430}
1431
1432
1433
1434/*
1435 * Update the min/max values from an array of fragment colors.
1436 */
1437static void
1438update_minmax(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
1439{
1440   GLuint i;
1441   for (i = 0; i < n; i++) {
1442      /* update mins */
1443      if (rgba[i][RCOMP] < ctx->MinMax.Min[RCOMP])
1444         ctx->MinMax.Min[RCOMP] = rgba[i][RCOMP];
1445      if (rgba[i][GCOMP] < ctx->MinMax.Min[GCOMP])
1446         ctx->MinMax.Min[GCOMP] = rgba[i][GCOMP];
1447      if (rgba[i][BCOMP] < ctx->MinMax.Min[BCOMP])
1448         ctx->MinMax.Min[BCOMP] = rgba[i][BCOMP];
1449      if (rgba[i][ACOMP] < ctx->MinMax.Min[ACOMP])
1450         ctx->MinMax.Min[ACOMP] = rgba[i][ACOMP];
1451
1452      /* update maxs */
1453      if (rgba[i][RCOMP] > ctx->MinMax.Max[RCOMP])
1454         ctx->MinMax.Max[RCOMP] = rgba[i][RCOMP];
1455      if (rgba[i][GCOMP] > ctx->MinMax.Max[GCOMP])
1456         ctx->MinMax.Max[GCOMP] = rgba[i][GCOMP];
1457      if (rgba[i][BCOMP] > ctx->MinMax.Max[BCOMP])
1458         ctx->MinMax.Max[BCOMP] = rgba[i][BCOMP];
1459      if (rgba[i][ACOMP] > ctx->MinMax.Max[ACOMP])
1460         ctx->MinMax.Max[ACOMP] = rgba[i][ACOMP];
1461   }
1462}
1463
1464
1465/*
1466 * Update the histogram values from an array of fragment colors.
1467 */
1468static void
1469update_histogram(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
1470{
1471   const GLint max = ctx->Histogram.Width - 1;
1472   GLfloat w = (GLfloat) max;
1473   GLuint i;
1474
1475   if (ctx->Histogram.Width == 0)
1476      return;
1477
1478   for (i = 0; i < n; i++) {
1479      GLint ri = IROUND(rgba[i][RCOMP] * w);
1480      GLint gi = IROUND(rgba[i][GCOMP] * w);
1481      GLint bi = IROUND(rgba[i][BCOMP] * w);
1482      GLint ai = IROUND(rgba[i][ACOMP] * w);
1483      ri = CLAMP(ri, 0, max);
1484      gi = CLAMP(gi, 0, max);
1485      bi = CLAMP(bi, 0, max);
1486      ai = CLAMP(ai, 0, max);
1487      ctx->Histogram.Count[ri][RCOMP]++;
1488      ctx->Histogram.Count[gi][GCOMP]++;
1489      ctx->Histogram.Count[bi][BCOMP]++;
1490      ctx->Histogram.Count[ai][ACOMP]++;
1491   }
1492}
1493
1494
1495/**
1496 * Apply various pixel transfer operations to an array of RGBA pixels
1497 * as indicated by the transferOps bitmask
1498 */
1499void
1500_mesa_apply_rgba_transfer_ops(GLcontext *ctx, GLbitfield transferOps,
1501                              GLuint n, GLfloat rgba[][4])
1502{
1503   /* scale & bias */
1504   if (transferOps & IMAGE_SCALE_BIAS_BIT) {
1505      _mesa_scale_and_bias_rgba(n, rgba,
1506                                ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
1507                                ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
1508                                ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
1509                                ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
1510   }
1511   /* color map lookup */
1512   if (transferOps & IMAGE_MAP_COLOR_BIT) {
1513      _mesa_map_rgba( ctx, n, rgba );
1514   }
1515   /* GL_COLOR_TABLE lookup */
1516   if (transferOps & IMAGE_COLOR_TABLE_BIT) {
1517      _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_PRECONVOLUTION], n, rgba);
1518   }
1519   /* convolution */
1520   if (transferOps & IMAGE_CONVOLUTION_BIT) {
1521      /* this has to be done in the calling code */
1522      _mesa_problem(ctx, "IMAGE_CONVOLUTION_BIT set in _mesa_apply_transfer_ops");
1523   }
1524   /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
1525   if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
1526      _mesa_scale_and_bias_rgba(n, rgba,
1527                                ctx->Pixel.PostConvolutionScale[RCOMP],
1528                                ctx->Pixel.PostConvolutionScale[GCOMP],
1529                                ctx->Pixel.PostConvolutionScale[BCOMP],
1530                                ctx->Pixel.PostConvolutionScale[ACOMP],
1531                                ctx->Pixel.PostConvolutionBias[RCOMP],
1532                                ctx->Pixel.PostConvolutionBias[GCOMP],
1533                                ctx->Pixel.PostConvolutionBias[BCOMP],
1534                                ctx->Pixel.PostConvolutionBias[ACOMP]);
1535   }
1536   /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
1537   if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
1538      _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_POSTCONVOLUTION], n, rgba);
1539   }
1540   /* color matrix transform */
1541   if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
1542      _mesa_transform_rgba(ctx, n, rgba);
1543   }
1544   /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
1545   if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
1546      _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX], n, rgba);
1547   }
1548   /* update histogram count */
1549   if (transferOps & IMAGE_HISTOGRAM_BIT) {
1550      update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
1551   }
1552   /* update min/max values */
1553   if (transferOps & IMAGE_MIN_MAX_BIT) {
1554      update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
1555   }
1556   /* clamping to [0,1] */
1557   if (transferOps & IMAGE_CLAMP_BIT) {
1558      GLuint i;
1559      for (i = 0; i < n; i++) {
1560         rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1561         rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1562         rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1563         rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1564      }
1565   }
1566}
1567
1568
1569/*
1570 * Apply color index shift and offset to an array of pixels.
1571 */
1572static void
1573shift_and_offset_ci( const GLcontext *ctx, GLuint n, GLuint indexes[] )
1574{
1575   GLint shift = ctx->Pixel.IndexShift;
1576   GLint offset = ctx->Pixel.IndexOffset;
1577   GLuint i;
1578   if (shift > 0) {
1579      for (i=0;i<n;i++) {
1580         indexes[i] = (indexes[i] << shift) + offset;
1581      }
1582   }
1583   else if (shift < 0) {
1584      shift = -shift;
1585      for (i=0;i<n;i++) {
1586         indexes[i] = (indexes[i] >> shift) + offset;
1587      }
1588   }
1589   else {
1590      for (i=0;i<n;i++) {
1591         indexes[i] = indexes[i] + offset;
1592      }
1593   }
1594}
1595
1596
1597
1598/**
1599 * Apply color index shift, offset and table lookup to an array
1600 * of color indexes;
1601 */
1602void
1603_mesa_apply_ci_transfer_ops(const GLcontext *ctx, GLbitfield transferOps,
1604                            GLuint n, GLuint indexes[])
1605{
1606   if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
1607      shift_and_offset_ci(ctx, n, indexes);
1608   }
1609   if (transferOps & IMAGE_MAP_COLOR_BIT) {
1610      const GLuint mask = ctx->PixelMaps.ItoI.Size - 1;
1611      GLuint i;
1612      for (i = 0; i < n; i++) {
1613         const GLuint j = indexes[i] & mask;
1614         indexes[i] = IROUND(ctx->PixelMaps.ItoI.Map[j]);
1615      }
1616   }
1617}
1618
1619
1620/**
1621 * Apply stencil index shift, offset and table lookup to an array
1622 * of stencil values.
1623 */
1624void
1625_mesa_apply_stencil_transfer_ops(const GLcontext *ctx, GLuint n,
1626                                 GLstencil stencil[])
1627{
1628   if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
1629      const GLint offset = ctx->Pixel.IndexOffset;
1630      GLint shift = ctx->Pixel.IndexShift;
1631      GLuint i;
1632      if (shift > 0) {
1633         for (i = 0; i < n; i++) {
1634            stencil[i] = (stencil[i] << shift) + offset;
1635         }
1636      }
1637      else if (shift < 0) {
1638         shift = -shift;
1639         for (i = 0; i < n; i++) {
1640            stencil[i] = (stencil[i] >> shift) + offset;
1641         }
1642      }
1643      else {
1644         for (i = 0; i < n; i++) {
1645            stencil[i] = stencil[i] + offset;
1646         }
1647      }
1648   }
1649   if (ctx->Pixel.MapStencilFlag) {
1650      GLuint mask = ctx->PixelMaps.StoS.Size - 1;
1651      GLuint i;
1652      for (i = 0; i < n; i++) {
1653         stencil[i] = ctx->PixelMaps.StoS.Map[ stencil[i] & mask ];
1654      }
1655   }
1656}
1657
1658
1659/**
1660 * Used to pack an array [][4] of RGBA float colors as specified
1661 * by the dstFormat, dstType and dstPacking.  Used by glReadPixels,
1662 * glGetConvolutionFilter(), etc.
1663 * Incoming colors will be clamped to [0,1] if needed.
1664 * Note: the rgba values will be modified by this function when any pixel
1665 * transfer ops are enabled.
1666 */
1667void
1668_mesa_pack_rgba_span_float(GLcontext *ctx, GLuint n, GLfloat rgba[][4],
1669                           GLenum dstFormat, GLenum dstType,
1670                           GLvoid *dstAddr,
1671                           const struct gl_pixelstore_attrib *dstPacking,
1672                           GLbitfield transferOps)
1673{
1674   GLfloat luminance[MAX_WIDTH];
1675   const GLint comps = _mesa_components_in_format(dstFormat);
1676   GLuint i;
1677
1678   if (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE) {
1679      /* need to clamp to [0, 1] */
1680      transferOps |= IMAGE_CLAMP_BIT;
1681   }
1682
1683   if (transferOps) {
1684      _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
1685      if ((transferOps & IMAGE_MIN_MAX_BIT) && ctx->MinMax.Sink) {
1686         return;
1687      }
1688   }
1689
1690   if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
1691      /* compute luminance values */
1692      if (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE) {
1693         for (i = 0; i < n; i++) {
1694            GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1695            luminance[i] = CLAMP(sum, 0.0F, 1.0F);
1696         }
1697      }
1698      else {
1699         for (i = 0; i < n; i++) {
1700            luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1701         }
1702      }
1703   }
1704
1705   /*
1706    * Pack/store the pixels.  Ugh!  Lots of cases!!!
1707    */
1708   switch (dstType) {
1709      case GL_UNSIGNED_BYTE:
1710         {
1711            GLubyte *dst = (GLubyte *) dstAddr;
1712            switch (dstFormat) {
1713               case GL_RED:
1714                  for (i=0;i<n;i++)
1715                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1716                  break;
1717               case GL_GREEN:
1718                  for (i=0;i<n;i++)
1719                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1720                  break;
1721               case GL_BLUE:
1722                  for (i=0;i<n;i++)
1723                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1724                  break;
1725               case GL_ALPHA:
1726                  for (i=0;i<n;i++)
1727                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1728                  break;
1729               case GL_LUMINANCE:
1730                  for (i=0;i<n;i++)
1731                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1732                  break;
1733               case GL_LUMINANCE_ALPHA:
1734                  for (i=0;i<n;i++) {
1735                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1736                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1737                  }
1738                  break;
1739               case GL_RGB:
1740                  for (i=0;i<n;i++) {
1741                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1742                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1743                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1744                  }
1745                  break;
1746               case GL_RGBA:
1747                  for (i=0;i<n;i++) {
1748                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1749                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1750                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1751                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1752                  }
1753                  break;
1754               case GL_BGR:
1755                  for (i=0;i<n;i++) {
1756                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1757                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1758                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1759                  }
1760                  break;
1761               case GL_BGRA:
1762                  for (i=0;i<n;i++) {
1763                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1764                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1765                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1766                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1767                  }
1768                  break;
1769               case GL_ABGR_EXT:
1770                  for (i=0;i<n;i++) {
1771                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1772                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1773                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1774                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1775                  }
1776                  break;
1777               default:
1778                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1779            }
1780         }
1781         break;
1782      case GL_BYTE:
1783         {
1784            GLbyte *dst = (GLbyte *) dstAddr;
1785            switch (dstFormat) {
1786               case GL_RED:
1787                  for (i=0;i<n;i++)
1788                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1789                  break;
1790               case GL_GREEN:
1791                  for (i=0;i<n;i++)
1792                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1793                  break;
1794               case GL_BLUE:
1795                  for (i=0;i<n;i++)
1796                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1797                  break;
1798               case GL_ALPHA:
1799                  for (i=0;i<n;i++)
1800                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1801                  break;
1802               case GL_LUMINANCE:
1803                  for (i=0;i<n;i++)
1804                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
1805                  break;
1806               case GL_LUMINANCE_ALPHA:
1807                  for (i=0;i<n;i++) {
1808                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1809                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1810                  }
1811                  break;
1812               case GL_RGB:
1813                  for (i=0;i<n;i++) {
1814                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1815                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1816                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1817                  }
1818                  break;
1819               case GL_RGBA:
1820                  for (i=0;i<n;i++) {
1821                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1822                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1823                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1824                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1825                  }
1826                  break;
1827               case GL_BGR:
1828                  for (i=0;i<n;i++) {
1829                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1830                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1831                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1832                  }
1833                  break;
1834               case GL_BGRA:
1835                  for (i=0;i<n;i++) {
1836                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1837                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1838                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1839                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1840                  }
1841		  break;
1842               case GL_ABGR_EXT:
1843                  for (i=0;i<n;i++) {
1844                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1845                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1846                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1847                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1848                  }
1849                  break;
1850               default:
1851                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1852            }
1853         }
1854         break;
1855      case GL_UNSIGNED_SHORT:
1856         {
1857            GLushort *dst = (GLushort *) dstAddr;
1858            switch (dstFormat) {
1859               case GL_RED:
1860                  for (i=0;i<n;i++)
1861                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
1862                  break;
1863               case GL_GREEN:
1864                  for (i=0;i<n;i++)
1865                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
1866                  break;
1867               case GL_BLUE:
1868                  for (i=0;i<n;i++)
1869                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
1870                  break;
1871               case GL_ALPHA:
1872                  for (i=0;i<n;i++)
1873                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
1874                  break;
1875               case GL_LUMINANCE:
1876                  for (i=0;i<n;i++)
1877                     UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
1878                  break;
1879               case GL_LUMINANCE_ALPHA:
1880                  for (i=0;i<n;i++) {
1881                     UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
1882                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
1883                  }
1884                  break;
1885               case GL_RGB:
1886                  for (i=0;i<n;i++) {
1887                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
1888                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1889                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
1890                  }
1891                  break;
1892               case GL_RGBA:
1893                  for (i=0;i<n;i++) {
1894                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
1895                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1896                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
1897                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1898                  }
1899                  break;
1900               case GL_BGR:
1901                  for (i=0;i<n;i++) {
1902                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
1903                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1904                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
1905                  }
1906                  break;
1907               case GL_BGRA:
1908                  for (i=0;i<n;i++) {
1909                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
1910                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1911                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
1912                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1913                  }
1914                  break;
1915               case GL_ABGR_EXT:
1916                  for (i=0;i<n;i++) {
1917                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
1918                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
1919                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
1920                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
1921                  }
1922                  break;
1923               default:
1924                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1925            }
1926         }
1927         break;
1928      case GL_SHORT:
1929         {
1930            GLshort *dst = (GLshort *) dstAddr;
1931            switch (dstFormat) {
1932               case GL_RED:
1933                  for (i=0;i<n;i++)
1934                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1935                  break;
1936               case GL_GREEN:
1937                  for (i=0;i<n;i++)
1938                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1939                  break;
1940               case GL_BLUE:
1941                  for (i=0;i<n;i++)
1942                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1943                  break;
1944               case GL_ALPHA:
1945                  for (i=0;i<n;i++)
1946                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1947                  break;
1948               case GL_LUMINANCE:
1949                  for (i=0;i<n;i++)
1950                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
1951                  break;
1952               case GL_LUMINANCE_ALPHA:
1953                  for (i=0;i<n;i++) {
1954                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1955                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1956                  }
1957                  break;
1958               case GL_RGB:
1959                  for (i=0;i<n;i++) {
1960                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1961                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1962                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1963                  }
1964                  break;
1965               case GL_RGBA:
1966                  for (i=0;i<n;i++) {
1967                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1968                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1969                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1970                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1971                  }
1972                  break;
1973               case GL_BGR:
1974                  for (i=0;i<n;i++) {
1975                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1976                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1977                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1978                  }
1979                  break;
1980               case GL_BGRA:
1981                  for (i=0;i<n;i++) {
1982                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1983                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1984                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1985                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1986                  }
1987		  break;
1988               case GL_ABGR_EXT:
1989                  for (i=0;i<n;i++) {
1990                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1991                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1992                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1993                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1994                  }
1995                  break;
1996               default:
1997                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1998            }
1999         }
2000         break;
2001      case GL_UNSIGNED_INT:
2002         {
2003            GLuint *dst = (GLuint *) dstAddr;
2004            switch (dstFormat) {
2005               case GL_RED:
2006                  for (i=0;i<n;i++)
2007                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2008                  break;
2009               case GL_GREEN:
2010                  for (i=0;i<n;i++)
2011                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2012                  break;
2013               case GL_BLUE:
2014                  for (i=0;i<n;i++)
2015                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2016                  break;
2017               case GL_ALPHA:
2018                  for (i=0;i<n;i++)
2019                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2020                  break;
2021               case GL_LUMINANCE:
2022                  for (i=0;i<n;i++)
2023                     dst[i] = FLOAT_TO_UINT(luminance[i]);
2024                  break;
2025               case GL_LUMINANCE_ALPHA:
2026                  for (i=0;i<n;i++) {
2027                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2028                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2029                  }
2030                  break;
2031               case GL_RGB:
2032                  for (i=0;i<n;i++) {
2033                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2034                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2035                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2036                  }
2037                  break;
2038               case GL_RGBA:
2039                  for (i=0;i<n;i++) {
2040                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2041                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2042                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2043                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2044                  }
2045                  break;
2046               case GL_BGR:
2047                  for (i=0;i<n;i++) {
2048                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2049                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2050                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2051                  }
2052                  break;
2053               case GL_BGRA:
2054                  for (i=0;i<n;i++) {
2055                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2056                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2057                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2058                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2059                  }
2060                  break;
2061               case GL_ABGR_EXT:
2062                  for (i=0;i<n;i++) {
2063                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2064                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2065                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2066                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2067                  }
2068                  break;
2069               default:
2070                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2071            }
2072         }
2073         break;
2074      case GL_INT:
2075         {
2076            GLint *dst = (GLint *) dstAddr;
2077            switch (dstFormat) {
2078               case GL_RED:
2079                  for (i=0;i<n;i++)
2080                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
2081                  break;
2082               case GL_GREEN:
2083                  for (i=0;i<n;i++)
2084                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
2085                  break;
2086               case GL_BLUE:
2087                  for (i=0;i<n;i++)
2088                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
2089                  break;
2090               case GL_ALPHA:
2091                  for (i=0;i<n;i++)
2092                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
2093                  break;
2094               case GL_LUMINANCE:
2095                  for (i=0;i<n;i++)
2096                     dst[i] = FLOAT_TO_INT(luminance[i]);
2097                  break;
2098               case GL_LUMINANCE_ALPHA:
2099                  for (i=0;i<n;i++) {
2100                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2101                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
2102                  }
2103                  break;
2104               case GL_RGB:
2105                  for (i=0;i<n;i++) {
2106                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2107                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2108                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2109                  }
2110                  break;
2111               case GL_RGBA:
2112                  for (i=0;i<n;i++) {
2113                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2114                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2115                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2116                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2117                  }
2118                  break;
2119               case GL_BGR:
2120                  for (i=0;i<n;i++) {
2121                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2122                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2123                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2124                  }
2125                  break;
2126               case GL_BGRA:
2127                  for (i=0;i<n;i++) {
2128                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2129                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2130                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2131                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2132                  }
2133                  break;
2134               case GL_ABGR_EXT:
2135                  for (i=0;i<n;i++) {
2136                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
2137                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
2138                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
2139                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
2140                  }
2141                  break;
2142               default:
2143                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2144            }
2145         }
2146         break;
2147      case GL_FLOAT:
2148         {
2149            GLfloat *dst = (GLfloat *) dstAddr;
2150            switch (dstFormat) {
2151               case GL_RED:
2152                  for (i=0;i<n;i++)
2153                     dst[i] = rgba[i][RCOMP];
2154                  break;
2155               case GL_GREEN:
2156                  for (i=0;i<n;i++)
2157                     dst[i] = rgba[i][GCOMP];
2158                  break;
2159               case GL_BLUE:
2160                  for (i=0;i<n;i++)
2161                     dst[i] = rgba[i][BCOMP];
2162                  break;
2163               case GL_ALPHA:
2164                  for (i=0;i<n;i++)
2165                     dst[i] = rgba[i][ACOMP];
2166                  break;
2167               case GL_LUMINANCE:
2168                  for (i=0;i<n;i++)
2169                     dst[i] = luminance[i];
2170                  break;
2171               case GL_LUMINANCE_ALPHA:
2172                  for (i=0;i<n;i++) {
2173                     dst[i*2+0] = luminance[i];
2174                     dst[i*2+1] = rgba[i][ACOMP];
2175                  }
2176                  break;
2177               case GL_RGB:
2178                  for (i=0;i<n;i++) {
2179                     dst[i*3+0] = rgba[i][RCOMP];
2180                     dst[i*3+1] = rgba[i][GCOMP];
2181                     dst[i*3+2] = rgba[i][BCOMP];
2182                  }
2183                  break;
2184               case GL_RGBA:
2185                  for (i=0;i<n;i++) {
2186                     dst[i*4+0] = rgba[i][RCOMP];
2187                     dst[i*4+1] = rgba[i][GCOMP];
2188                     dst[i*4+2] = rgba[i][BCOMP];
2189                     dst[i*4+3] = rgba[i][ACOMP];
2190                  }
2191                  break;
2192               case GL_BGR:
2193                  for (i=0;i<n;i++) {
2194                     dst[i*3+0] = rgba[i][BCOMP];
2195                     dst[i*3+1] = rgba[i][GCOMP];
2196                     dst[i*3+2] = rgba[i][RCOMP];
2197                  }
2198                  break;
2199               case GL_BGRA:
2200                  for (i=0;i<n;i++) {
2201                     dst[i*4+0] = rgba[i][BCOMP];
2202                     dst[i*4+1] = rgba[i][GCOMP];
2203                     dst[i*4+2] = rgba[i][RCOMP];
2204                     dst[i*4+3] = rgba[i][ACOMP];
2205                  }
2206                  break;
2207               case GL_ABGR_EXT:
2208                  for (i=0;i<n;i++) {
2209                     dst[i*4+0] = rgba[i][ACOMP];
2210                     dst[i*4+1] = rgba[i][BCOMP];
2211                     dst[i*4+2] = rgba[i][GCOMP];
2212                     dst[i*4+3] = rgba[i][RCOMP];
2213                  }
2214                  break;
2215               default:
2216                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2217            }
2218         }
2219         break;
2220      case GL_HALF_FLOAT_ARB:
2221         {
2222            GLhalfARB *dst = (GLhalfARB *) dstAddr;
2223            switch (dstFormat) {
2224               case GL_RED:
2225                  for (i=0;i<n;i++)
2226                     dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
2227                  break;
2228               case GL_GREEN:
2229                  for (i=0;i<n;i++)
2230                     dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
2231                  break;
2232               case GL_BLUE:
2233                  for (i=0;i<n;i++)
2234                     dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
2235                  break;
2236               case GL_ALPHA:
2237                  for (i=0;i<n;i++)
2238                     dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
2239                  break;
2240               case GL_LUMINANCE:
2241                  for (i=0;i<n;i++)
2242                     dst[i] = _mesa_float_to_half(luminance[i]);
2243                  break;
2244               case GL_LUMINANCE_ALPHA:
2245                  for (i=0;i<n;i++) {
2246                     dst[i*2+0] = _mesa_float_to_half(luminance[i]);
2247                     dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
2248                  }
2249                  break;
2250               case GL_RGB:
2251                  for (i=0;i<n;i++) {
2252                     dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2253                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2254                     dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2255                  }
2256                  break;
2257               case GL_RGBA:
2258                  for (i=0;i<n;i++) {
2259                     dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2260                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2261                     dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2262                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2263                  }
2264                  break;
2265               case GL_BGR:
2266                  for (i=0;i<n;i++) {
2267                     dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2268                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2269                     dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2270                  }
2271                  break;
2272               case GL_BGRA:
2273                  for (i=0;i<n;i++) {
2274                     dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2275                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2276                     dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2277                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2278                  }
2279                  break;
2280               case GL_ABGR_EXT:
2281                  for (i=0;i<n;i++) {
2282                     dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
2283                     dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
2284                     dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
2285                     dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
2286                  }
2287                  break;
2288               default:
2289                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2290            }
2291         }
2292         break;
2293      case GL_UNSIGNED_BYTE_3_3_2:
2294         if (dstFormat == GL_RGB) {
2295            GLubyte *dst = (GLubyte *) dstAddr;
2296            for (i=0;i<n;i++) {
2297               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F)) << 5)
2298                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 2)
2299                      | (((GLint) (rgba[i][BCOMP] * 3.0F))     );
2300            }
2301         }
2302         break;
2303      case GL_UNSIGNED_BYTE_2_3_3_REV:
2304         if (dstFormat == GL_RGB) {
2305            GLubyte *dst = (GLubyte *) dstAddr;
2306            for (i=0;i<n;i++) {
2307               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F))     )
2308                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 3)
2309                      | (((GLint) (rgba[i][BCOMP] * 3.0F)) << 6);
2310            }
2311         }
2312         break;
2313      case GL_UNSIGNED_SHORT_5_6_5:
2314         if (dstFormat == GL_RGB) {
2315            GLushort *dst = (GLushort *) dstAddr;
2316            for (i=0;i<n;i++) {
2317               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
2318                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
2319                      | (((GLint) (rgba[i][BCOMP] * 31.0F))      );
2320            }
2321         }
2322         break;
2323      case GL_UNSIGNED_SHORT_5_6_5_REV:
2324         if (dstFormat == GL_RGB) {
2325            GLushort *dst = (GLushort *) dstAddr;
2326            for (i=0;i<n;i++) {
2327               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
2328                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
2329                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11);
2330            }
2331         }
2332         break;
2333      case GL_UNSIGNED_SHORT_4_4_4_4:
2334         if (dstFormat == GL_RGBA) {
2335            GLushort *dst = (GLushort *) dstAddr;
2336            for (i=0;i<n;i++) {
2337               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12)
2338                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
2339                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
2340                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
2341            }
2342         }
2343         else if (dstFormat == GL_BGRA) {
2344            GLushort *dst = (GLushort *) dstAddr;
2345            for (i=0;i<n;i++) {
2346               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F)) << 12)
2347                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
2348                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  4)
2349                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
2350            }
2351         }
2352         else if (dstFormat == GL_ABGR_EXT) {
2353            GLushort *dst = (GLushort *) dstAddr;
2354            for (i=0;i<n;i++) {
2355               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12)
2356                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
2357                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
2358                      | (((GLint) (rgba[i][RCOMP] * 15.0F))      );
2359            }
2360         }
2361         break;
2362      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2363         if (dstFormat == GL_RGBA) {
2364            GLushort *dst = (GLushort *) dstAddr;
2365            for (i=0;i<n;i++) {
2366               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F))      )
2367                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
2368                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
2369                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
2370            }
2371         }
2372         else if (dstFormat == GL_BGRA) {
2373            GLushort *dst = (GLushort *) dstAddr;
2374            for (i=0;i<n;i++) {
2375               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F))      )
2376                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
2377                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  8)
2378                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
2379            }
2380         }
2381         else if (dstFormat == GL_ABGR_EXT) {
2382            GLushort *dst = (GLushort *) dstAddr;
2383            for (i=0;i<n;i++) {
2384               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F))      )
2385                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
2386                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
2387                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12);
2388            }
2389         }
2390         break;
2391      case GL_UNSIGNED_SHORT_5_5_5_1:
2392         if (dstFormat == GL_RGBA) {
2393            GLushort *dst = (GLushort *) dstAddr;
2394            for (i=0;i<n;i++) {
2395               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
2396                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
2397                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  1)
2398                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
2399            }
2400         }
2401         else if (dstFormat == GL_BGRA) {
2402            GLushort *dst = (GLushort *) dstAddr;
2403            for (i=0;i<n;i++) {
2404               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11)
2405                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
2406                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) <<  1)
2407                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
2408            }
2409         }
2410         else if (dstFormat == GL_ABGR_EXT) {
2411            GLushort *dst = (GLushort *) dstAddr;
2412            for (i=0;i<n;i++) {
2413               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F)) << 11)
2414                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  6)
2415                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  1)
2416                      | (((GLint) (rgba[i][RCOMP] *  1.0F))      );
2417            }
2418         }
2419         break;
2420      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2421         if (dstFormat == GL_RGBA) {
2422            GLushort *dst = (GLushort *) dstAddr;
2423            for (i=0;i<n;i++) {
2424               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
2425                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
2426                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 10)
2427                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
2428            }
2429         }
2430         else if (dstFormat == GL_BGRA) {
2431            GLushort *dst = (GLushort *) dstAddr;
2432            for (i=0;i<n;i++) {
2433               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F))      )
2434                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
2435                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) << 10)
2436                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
2437            }
2438         }
2439         else if (dstFormat == GL_ABGR_EXT) {
2440            GLushort *dst = (GLushort *) dstAddr;
2441            for (i=0;i<n;i++) {
2442               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F))      )
2443                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  5)
2444                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) << 10)
2445                      | (((GLint) (rgba[i][RCOMP] *  1.0F)) << 15);
2446            }
2447         }
2448         break;
2449      case GL_UNSIGNED_INT_8_8_8_8:
2450         if (dstFormat == GL_RGBA) {
2451            GLuint *dst = (GLuint *) dstAddr;
2452            for (i=0;i<n;i++) {
2453               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24)
2454                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
2455                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
2456                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
2457            }
2458         }
2459         else if (dstFormat == GL_BGRA) {
2460            GLuint *dst = (GLuint *) dstAddr;
2461            for (i=0;i<n;i++) {
2462               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 24)
2463                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
2464                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) <<  8)
2465                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
2466            }
2467         }
2468         else if (dstFormat == GL_ABGR_EXT) {
2469            GLuint *dst = (GLuint *) dstAddr;
2470            for (i=0;i<n;i++) {
2471               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24)
2472                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
2473                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
2474                      | (((GLuint) (rgba[i][RCOMP] * 255.0F))      );
2475            }
2476         }
2477         break;
2478      case GL_UNSIGNED_INT_8_8_8_8_REV:
2479         if (dstFormat == GL_RGBA) {
2480            GLuint *dst = (GLuint *) dstAddr;
2481            for (i=0;i<n;i++) {
2482               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F))      )
2483                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
2484                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
2485                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
2486            }
2487         }
2488         else if (dstFormat == GL_BGRA) {
2489            GLuint *dst = (GLuint *) dstAddr;
2490            for (i=0;i<n;i++) {
2491               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F))      )
2492                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
2493                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 16)
2494                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
2495            }
2496         }
2497         else if (dstFormat == GL_ABGR_EXT) {
2498            GLuint *dst = (GLuint *) dstAddr;
2499            for (i=0;i<n;i++) {
2500               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F))      )
2501                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
2502                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
2503                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24);
2504            }
2505         }
2506         break;
2507      case GL_UNSIGNED_INT_10_10_10_2:
2508         if (dstFormat == GL_RGBA) {
2509            GLuint *dst = (GLuint *) dstAddr;
2510            for (i=0;i<n;i++) {
2511               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 22)
2512                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
2513                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) <<  2)
2514                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
2515            }
2516         }
2517         else if (dstFormat == GL_BGRA) {
2518            GLuint *dst = (GLuint *) dstAddr;
2519            for (i=0;i<n;i++) {
2520               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 22)
2521                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
2522                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) <<  2)
2523                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
2524            }
2525         }
2526         else if (dstFormat == GL_ABGR_EXT) {
2527            GLuint *dst = (GLuint *) dstAddr;
2528            for (i=0;i<n;i++) {
2529               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F)) << 22)
2530                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 12)
2531                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) <<  2)
2532                      | (((GLuint) (rgba[i][RCOMP] *    3.0F))      );
2533            }
2534         }
2535         break;
2536      case GL_UNSIGNED_INT_2_10_10_10_REV:
2537         if (dstFormat == GL_RGBA) {
2538            GLuint *dst = (GLuint *) dstAddr;
2539            for (i=0;i<n;i++) {
2540               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F))      )
2541                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
2542                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 20)
2543                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
2544            }
2545         }
2546         else if (dstFormat == GL_BGRA) {
2547            GLuint *dst = (GLuint *) dstAddr;
2548            for (i=0;i<n;i++) {
2549               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F))      )
2550                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
2551                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 20)
2552                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
2553            }
2554         }
2555         else if (dstFormat == GL_ABGR_EXT) {
2556            GLuint *dst = (GLuint *) dstAddr;
2557            for (i=0;i<n;i++) {
2558               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F))      )
2559                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 10)
2560                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 20)
2561                      | (((GLuint) (rgba[i][RCOMP] *    3.0F)) << 30);
2562            }
2563         }
2564         break;
2565      default:
2566         _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2567         return;
2568   }
2569
2570   if (dstPacking->SwapBytes) {
2571      GLint swapSize = _mesa_sizeof_packed_type(dstType);
2572      if (swapSize == 2) {
2573         if (dstPacking->SwapBytes) {
2574            _mesa_swap2((GLushort *) dstAddr, n * comps);
2575         }
2576      }
2577      else if (swapSize == 4) {
2578         if (dstPacking->SwapBytes) {
2579            _mesa_swap4((GLuint *) dstAddr, n * comps);
2580         }
2581      }
2582   }
2583}
2584
2585
2586#define SWAP2BYTE(VALUE)			\
2587   {						\
2588      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2589      GLubyte tmp = bytes[0];			\
2590      bytes[0] = bytes[1];			\
2591      bytes[1] = tmp;				\
2592   }
2593
2594#define SWAP4BYTE(VALUE)			\
2595   {						\
2596      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2597      GLubyte tmp = bytes[0];			\
2598      bytes[0] = bytes[3];			\
2599      bytes[3] = tmp;				\
2600      tmp = bytes[1];				\
2601      bytes[1] = bytes[2];			\
2602      bytes[2] = tmp;				\
2603   }
2604
2605
2606static void
2607extract_uint_indexes(GLuint n, GLuint indexes[],
2608                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
2609                     const struct gl_pixelstore_attrib *unpack )
2610{
2611   ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2612
2613   ASSERT(srcType == GL_BITMAP ||
2614          srcType == GL_UNSIGNED_BYTE ||
2615          srcType == GL_BYTE ||
2616          srcType == GL_UNSIGNED_SHORT ||
2617          srcType == GL_SHORT ||
2618          srcType == GL_UNSIGNED_INT ||
2619          srcType == GL_INT ||
2620          srcType == GL_UNSIGNED_INT_24_8_EXT ||
2621          srcType == GL_HALF_FLOAT_ARB ||
2622          srcType == GL_FLOAT);
2623
2624   switch (srcType) {
2625      case GL_BITMAP:
2626         {
2627            GLubyte *ubsrc = (GLubyte *) src;
2628            if (unpack->LsbFirst) {
2629               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2630               GLuint i;
2631               for (i = 0; i < n; i++) {
2632                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2633                  if (mask == 128) {
2634                     mask = 1;
2635                     ubsrc++;
2636                  }
2637                  else {
2638                     mask = mask << 1;
2639                  }
2640               }
2641            }
2642            else {
2643               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2644               GLuint i;
2645               for (i = 0; i < n; i++) {
2646                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2647                  if (mask == 1) {
2648                     mask = 128;
2649                     ubsrc++;
2650                  }
2651                  else {
2652                     mask = mask >> 1;
2653                  }
2654               }
2655            }
2656         }
2657         break;
2658      case GL_UNSIGNED_BYTE:
2659         {
2660            GLuint i;
2661            const GLubyte *s = (const GLubyte *) src;
2662            for (i = 0; i < n; i++)
2663               indexes[i] = s[i];
2664         }
2665         break;
2666      case GL_BYTE:
2667         {
2668            GLuint i;
2669            const GLbyte *s = (const GLbyte *) src;
2670            for (i = 0; i < n; i++)
2671               indexes[i] = s[i];
2672         }
2673         break;
2674      case GL_UNSIGNED_SHORT:
2675         {
2676            GLuint i;
2677            const GLushort *s = (const GLushort *) src;
2678            if (unpack->SwapBytes) {
2679               for (i = 0; i < n; i++) {
2680                  GLushort value = s[i];
2681                  SWAP2BYTE(value);
2682                  indexes[i] = value;
2683               }
2684            }
2685            else {
2686               for (i = 0; i < n; i++)
2687                  indexes[i] = s[i];
2688            }
2689         }
2690         break;
2691      case GL_SHORT:
2692         {
2693            GLuint i;
2694            const GLshort *s = (const GLshort *) src;
2695            if (unpack->SwapBytes) {
2696               for (i = 0; i < n; i++) {
2697                  GLshort value = s[i];
2698                  SWAP2BYTE(value);
2699                  indexes[i] = value;
2700               }
2701            }
2702            else {
2703               for (i = 0; i < n; i++)
2704                  indexes[i] = s[i];
2705            }
2706         }
2707         break;
2708      case GL_UNSIGNED_INT:
2709         {
2710            GLuint i;
2711            const GLuint *s = (const GLuint *) src;
2712            if (unpack->SwapBytes) {
2713               for (i = 0; i < n; i++) {
2714                  GLuint value = s[i];
2715                  SWAP4BYTE(value);
2716                  indexes[i] = value;
2717               }
2718            }
2719            else {
2720               for (i = 0; i < n; i++)
2721                  indexes[i] = s[i];
2722            }
2723         }
2724         break;
2725      case GL_INT:
2726         {
2727            GLuint i;
2728            const GLint *s = (const GLint *) src;
2729            if (unpack->SwapBytes) {
2730               for (i = 0; i < n; i++) {
2731                  GLint value = s[i];
2732                  SWAP4BYTE(value);
2733                  indexes[i] = value;
2734               }
2735            }
2736            else {
2737               for (i = 0; i < n; i++)
2738                  indexes[i] = s[i];
2739            }
2740         }
2741         break;
2742      case GL_FLOAT:
2743         {
2744            GLuint i;
2745            const GLfloat *s = (const GLfloat *) src;
2746            if (unpack->SwapBytes) {
2747               for (i = 0; i < n; i++) {
2748                  GLfloat value = s[i];
2749                  SWAP4BYTE(value);
2750                  indexes[i] = (GLuint) value;
2751               }
2752            }
2753            else {
2754               for (i = 0; i < n; i++)
2755                  indexes[i] = (GLuint) s[i];
2756            }
2757         }
2758         break;
2759      case GL_HALF_FLOAT_ARB:
2760         {
2761            GLuint i;
2762            const GLhalfARB *s = (const GLhalfARB *) src;
2763            if (unpack->SwapBytes) {
2764               for (i = 0; i < n; i++) {
2765                  GLhalfARB value = s[i];
2766                  SWAP2BYTE(value);
2767                  indexes[i] = (GLuint) _mesa_half_to_float(value);
2768               }
2769            }
2770            else {
2771               for (i = 0; i < n; i++)
2772                  indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
2773            }
2774         }
2775         break;
2776      case GL_UNSIGNED_INT_24_8_EXT:
2777         {
2778            GLuint i;
2779            const GLuint *s = (const GLuint *) src;
2780            if (unpack->SwapBytes) {
2781               for (i = 0; i < n; i++) {
2782                  GLuint value = s[i];
2783                  SWAP4BYTE(value);
2784                  indexes[i] = value & 0xff;  /* lower 8 bits */
2785               }
2786            }
2787            else {
2788               for (i = 0; i < n; i++)
2789                  indexes[i] = s[i] & 0xfff;  /* lower 8 bits */
2790            }
2791         }
2792         break;
2793
2794      default:
2795         _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
2796         return;
2797   }
2798}
2799
2800
2801/*
2802 * This function extracts floating point RGBA values from arbitrary
2803 * image data.  srcFormat and srcType are the format and type parameters
2804 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2805 *
2806 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2807 * implements the "Conversion to floating point", "Conversion to RGB",
2808 * and "Final Expansion to RGBA" operations.
2809 *
2810 * Args:  n - number of pixels
2811 *        rgba - output colors
2812 *        srcFormat - format of incoming data
2813 *        srcType - data type of incoming data
2814 *        src - source data pointer
2815 *        swapBytes - perform byteswapping of incoming data?
2816 */
2817static void
2818extract_float_rgba(GLuint n, GLfloat rgba[][4],
2819                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
2820                   GLboolean swapBytes)
2821{
2822   GLint redIndex, greenIndex, blueIndex, alphaIndex;
2823   GLint stride;
2824   GLint rComp, bComp, gComp, aComp;
2825
2826   ASSERT(srcFormat == GL_RED ||
2827          srcFormat == GL_GREEN ||
2828          srcFormat == GL_BLUE ||
2829          srcFormat == GL_ALPHA ||
2830          srcFormat == GL_LUMINANCE ||
2831          srcFormat == GL_LUMINANCE_ALPHA ||
2832          srcFormat == GL_INTENSITY ||
2833          srcFormat == GL_RGB ||
2834          srcFormat == GL_BGR ||
2835          srcFormat == GL_RGBA ||
2836          srcFormat == GL_BGRA ||
2837          srcFormat == GL_ABGR_EXT);
2838
2839   ASSERT(srcType == GL_UNSIGNED_BYTE ||
2840          srcType == GL_BYTE ||
2841          srcType == GL_UNSIGNED_SHORT ||
2842          srcType == GL_SHORT ||
2843          srcType == GL_UNSIGNED_INT ||
2844          srcType == GL_INT ||
2845          srcType == GL_HALF_FLOAT_ARB ||
2846          srcType == GL_FLOAT ||
2847          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2848          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2849          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2850          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2851          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2852          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2853          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2854          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2855          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2856          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2857          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2858          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2859
2860   rComp = gComp = bComp = aComp = -1;
2861
2862   switch (srcFormat) {
2863      case GL_RED:
2864         redIndex = 0;
2865         greenIndex = blueIndex = alphaIndex = -1;
2866         stride = 1;
2867         break;
2868      case GL_GREEN:
2869         greenIndex = 0;
2870         redIndex = blueIndex = alphaIndex = -1;
2871         stride = 1;
2872         break;
2873      case GL_BLUE:
2874         blueIndex = 0;
2875         redIndex = greenIndex = alphaIndex = -1;
2876         stride = 1;
2877         break;
2878      case GL_ALPHA:
2879         redIndex = greenIndex = blueIndex = -1;
2880         alphaIndex = 0;
2881         stride = 1;
2882         break;
2883      case GL_LUMINANCE:
2884         redIndex = greenIndex = blueIndex = 0;
2885         alphaIndex = -1;
2886         stride = 1;
2887         break;
2888      case GL_LUMINANCE_ALPHA:
2889         redIndex = greenIndex = blueIndex = 0;
2890         alphaIndex = 1;
2891         stride = 2;
2892         break;
2893      case GL_INTENSITY:
2894         redIndex = greenIndex = blueIndex = alphaIndex = 0;
2895         stride = 1;
2896         break;
2897      case GL_RGB:
2898         redIndex = 0;
2899         greenIndex = 1;
2900         blueIndex = 2;
2901         alphaIndex = -1;
2902         rComp = 0;
2903         gComp = 1;
2904         bComp = 2;
2905         aComp = 3;
2906         stride = 3;
2907         break;
2908      case GL_BGR:
2909         redIndex = 2;
2910         greenIndex = 1;
2911         blueIndex = 0;
2912         alphaIndex = -1;
2913         rComp = 2;
2914         gComp = 1;
2915         bComp = 0;
2916         aComp = 3;
2917         stride = 3;
2918         break;
2919      case GL_RGBA:
2920         redIndex = 0;
2921         greenIndex = 1;
2922         blueIndex = 2;
2923         alphaIndex = 3;
2924         rComp = 0;
2925         gComp = 1;
2926         bComp = 2;
2927         aComp = 3;
2928         stride = 4;
2929         break;
2930      case GL_BGRA:
2931         redIndex = 2;
2932         greenIndex = 1;
2933         blueIndex = 0;
2934         alphaIndex = 3;
2935         rComp = 2;
2936         gComp = 1;
2937         bComp = 0;
2938         aComp = 3;
2939         stride = 4;
2940         break;
2941      case GL_ABGR_EXT:
2942         redIndex = 3;
2943         greenIndex = 2;
2944         blueIndex = 1;
2945         alphaIndex = 0;
2946         rComp = 3;
2947         gComp = 2;
2948         bComp = 1;
2949         aComp = 0;
2950         stride = 4;
2951         break;
2952      default:
2953         _mesa_problem(NULL, "bad srcFormat in extract float data");
2954         return;
2955   }
2956
2957
2958#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)		\
2959   if ((INDEX) < 0) {							\
2960      GLuint i;								\
2961      for (i = 0; i < n; i++) {						\
2962         rgba[i][CHANNEL] = DEFAULT;					\
2963      }									\
2964   }									\
2965   else if (swapBytes) {						\
2966      const TYPE *s = (const TYPE *) src;				\
2967      GLuint i;								\
2968      for (i = 0; i < n; i++) {						\
2969         TYPE value = s[INDEX];						\
2970         if (sizeof(TYPE) == 2) {					\
2971            SWAP2BYTE(value);						\
2972         }								\
2973         else if (sizeof(TYPE) == 4) {					\
2974            SWAP4BYTE(value);						\
2975         }								\
2976         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);		\
2977         s += stride;							\
2978      }									\
2979   }									\
2980   else {								\
2981      const TYPE *s = (const TYPE *) src;				\
2982      GLuint i;								\
2983      for (i = 0; i < n; i++) {						\
2984         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);		\
2985         s += stride;							\
2986      }									\
2987   }
2988
2989   switch (srcType) {
2990      case GL_UNSIGNED_BYTE:
2991         PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2992         PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2993         PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2994         PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
2995         break;
2996      case GL_BYTE:
2997         PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2998         PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2999         PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3000         PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
3001         break;
3002      case GL_UNSIGNED_SHORT:
3003         PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3004         PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3005         PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3006         PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
3007         break;
3008      case GL_SHORT:
3009         PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3010         PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3011         PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3012         PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
3013         break;
3014      case GL_UNSIGNED_INT:
3015         PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3016         PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3017         PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3018         PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
3019         break;
3020      case GL_INT:
3021         PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
3022         PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
3023         PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
3024         PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
3025         break;
3026      case GL_FLOAT:
3027         PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
3028         PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
3029         PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
3030         PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
3031         break;
3032      case GL_HALF_FLOAT_ARB:
3033         PROCESS(redIndex,   RCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3034         PROCESS(greenIndex, GCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3035         PROCESS(blueIndex,  BCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3036         PROCESS(alphaIndex, ACOMP, 1.0F, GLhalfARB, _mesa_half_to_float);
3037         break;
3038      case GL_UNSIGNED_BYTE_3_3_2:
3039         {
3040            const GLubyte *ubsrc = (const GLubyte *) src;
3041            GLuint i;
3042            for (i = 0; i < n; i ++) {
3043               GLubyte p = ubsrc[i];
3044               rgba[i][rComp] = ((p >> 5)      ) * (1.0F / 7.0F);
3045               rgba[i][gComp] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
3046               rgba[i][bComp] = ((p     ) & 0x3) * (1.0F / 3.0F);
3047               rgba[i][aComp] = 1.0F;
3048            }
3049         }
3050         break;
3051      case GL_UNSIGNED_BYTE_2_3_3_REV:
3052         {
3053            const GLubyte *ubsrc = (const GLubyte *) src;
3054            GLuint i;
3055            for (i = 0; i < n; i ++) {
3056               GLubyte p = ubsrc[i];
3057               rgba[i][rComp] = ((p     ) & 0x7) * (1.0F / 7.0F);
3058               rgba[i][gComp] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
3059               rgba[i][bComp] = ((p >> 6)      ) * (1.0F / 3.0F);
3060               rgba[i][aComp] = 1.0F;
3061            }
3062         }
3063         break;
3064      case GL_UNSIGNED_SHORT_5_6_5:
3065         if (swapBytes) {
3066            const GLushort *ussrc = (const GLushort *) src;
3067            GLuint i;
3068            for (i = 0; i < n; i ++) {
3069               GLushort p = ussrc[i];
3070               SWAP2BYTE(p);
3071               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3072               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
3073               rgba[i][bComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3074               rgba[i][aComp] = 1.0F;
3075            }
3076         }
3077         else {
3078            const GLushort *ussrc = (const GLushort *) src;
3079            GLuint i;
3080            for (i = 0; i < n; i ++) {
3081               GLushort p = ussrc[i];
3082               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3083               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
3084               rgba[i][bComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3085               rgba[i][aComp] = 1.0F;
3086            }
3087         }
3088         break;
3089      case GL_UNSIGNED_SHORT_5_6_5_REV:
3090         if (swapBytes) {
3091            const GLushort *ussrc = (const GLushort *) src;
3092            GLuint i;
3093            for (i = 0; i < n; i ++) {
3094               GLushort p = ussrc[i];
3095               SWAP2BYTE(p);
3096               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3097               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
3098               rgba[i][bComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3099               rgba[i][aComp] = 1.0F;
3100            }
3101         }
3102         else {
3103            const GLushort *ussrc = (const GLushort *) src;
3104            GLuint i;
3105            for (i = 0; i < n; i ++) {
3106               GLushort p = ussrc[i];
3107               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3108               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
3109               rgba[i][bComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3110               rgba[i][aComp] = 1.0F;
3111            }
3112         }
3113         break;
3114      case GL_UNSIGNED_SHORT_4_4_4_4:
3115         if (swapBytes) {
3116            const GLushort *ussrc = (const GLushort *) src;
3117            GLuint i;
3118            for (i = 0; i < n; i ++) {
3119               GLushort p = ussrc[i];
3120               SWAP2BYTE(p);
3121               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
3122               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
3123               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
3124               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
3125            }
3126         }
3127         else {
3128            const GLushort *ussrc = (const GLushort *) src;
3129            GLuint i;
3130            for (i = 0; i < n; i ++) {
3131               GLushort p = ussrc[i];
3132               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
3133               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
3134               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
3135               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
3136            }
3137         }
3138         break;
3139      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3140         if (swapBytes) {
3141            const GLushort *ussrc = (const GLushort *) src;
3142            GLuint i;
3143            for (i = 0; i < n; i ++) {
3144               GLushort p = ussrc[i];
3145               SWAP2BYTE(p);
3146               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
3147               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
3148               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
3149               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
3150            }
3151         }
3152         else {
3153            const GLushort *ussrc = (const GLushort *) src;
3154            GLuint i;
3155            for (i = 0; i < n; i ++) {
3156               GLushort p = ussrc[i];
3157               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
3158               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
3159               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
3160               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
3161            }
3162         }
3163         break;
3164      case GL_UNSIGNED_SHORT_5_5_5_1:
3165         if (swapBytes) {
3166            const GLushort *ussrc = (const GLushort *) src;
3167            GLuint i;
3168            for (i = 0; i < n; i ++) {
3169               GLushort p = ussrc[i];
3170               SWAP2BYTE(p);
3171               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3172               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
3173               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
3174               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
3175            }
3176         }
3177         else {
3178            const GLushort *ussrc = (const GLushort *) src;
3179            GLuint i;
3180            for (i = 0; i < n; i ++) {
3181               GLushort p = ussrc[i];
3182               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3183               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
3184               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
3185               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
3186            }
3187         }
3188         break;
3189      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3190         if (swapBytes) {
3191            const GLushort *ussrc = (const GLushort *) src;
3192            GLuint i;
3193            for (i = 0; i < n; i ++) {
3194               GLushort p = ussrc[i];
3195               SWAP2BYTE(p);
3196               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3197               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
3198               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3199               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
3200            }
3201         }
3202         else {
3203            const GLushort *ussrc = (const GLushort *) src;
3204            GLuint i;
3205            for (i = 0; i < n; i ++) {
3206               GLushort p = ussrc[i];
3207               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3208               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
3209               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3210               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
3211            }
3212         }
3213         break;
3214      case GL_UNSIGNED_INT_8_8_8_8:
3215         if (swapBytes) {
3216            const GLuint *uisrc = (const GLuint *) src;
3217            GLuint i;
3218            for (i = 0; i < n; i ++) {
3219               GLuint p = uisrc[i];
3220               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
3221               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3222               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3223               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
3224            }
3225         }
3226         else {
3227            const GLuint *uisrc = (const GLuint *) src;
3228            GLuint i;
3229            for (i = 0; i < n; i ++) {
3230               GLuint p = uisrc[i];
3231               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
3232               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3233               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3234               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
3235            }
3236         }
3237         break;
3238      case GL_UNSIGNED_INT_8_8_8_8_REV:
3239         if (swapBytes) {
3240            const GLuint *uisrc = (const GLuint *) src;
3241            GLuint i;
3242            for (i = 0; i < n; i ++) {
3243               GLuint p = uisrc[i];
3244               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
3245               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3246               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3247               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
3248            }
3249         }
3250         else {
3251            const GLuint *uisrc = (const GLuint *) src;
3252            GLuint i;
3253            for (i = 0; i < n; i ++) {
3254               GLuint p = uisrc[i];
3255               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
3256               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3257               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3258               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
3259            }
3260         }
3261         break;
3262      case GL_UNSIGNED_INT_10_10_10_2:
3263         if (swapBytes) {
3264            const GLuint *uisrc = (const GLuint *) src;
3265            GLuint i;
3266            for (i = 0; i < n; i ++) {
3267               GLuint p = uisrc[i];
3268               SWAP4BYTE(p);
3269               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
3270               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3271               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
3272               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
3273            }
3274         }
3275         else {
3276            const GLuint *uisrc = (const GLuint *) src;
3277            GLuint i;
3278            for (i = 0; i < n; i ++) {
3279               GLuint p = uisrc[i];
3280               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
3281               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3282               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
3283               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
3284            }
3285         }
3286         break;
3287      case GL_UNSIGNED_INT_2_10_10_10_REV:
3288         if (swapBytes) {
3289            const GLuint *uisrc = (const GLuint *) src;
3290            GLuint i;
3291            for (i = 0; i < n; i ++) {
3292               GLuint p = uisrc[i];
3293               SWAP4BYTE(p);
3294               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
3295               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3296               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3297               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
3298            }
3299         }
3300         else {
3301            const GLuint *uisrc = (const GLuint *) src;
3302            GLuint i;
3303            for (i = 0; i < n; i ++) {
3304               GLuint p = uisrc[i];
3305               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
3306               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3307               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3308               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
3309            }
3310         }
3311         break;
3312      default:
3313         _mesa_problem(NULL, "bad srcType in extract float data");
3314         break;
3315   }
3316}
3317
3318
3319/*
3320 * Unpack a row of color image data from a client buffer according to
3321 * the pixel unpacking parameters.
3322 * Return GLchan values in the specified dest image format.
3323 * This is used by glDrawPixels and glTexImage?D().
3324 * \param ctx - the context
3325 *         n - number of pixels in the span
3326 *         dstFormat - format of destination color array
3327 *         dest - the destination color array
3328 *         srcFormat - source image format
3329 *         srcType - source image  data type
3330 *         source - source image pointer
3331 *         srcPacking - pixel unpacking parameters
3332 *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
3333 *
3334 * XXX perhaps expand this to process whole images someday.
3335 */
3336void
3337_mesa_unpack_color_span_chan( GLcontext *ctx,
3338                              GLuint n, GLenum dstFormat, GLchan dest[],
3339                              GLenum srcFormat, GLenum srcType,
3340                              const GLvoid *source,
3341                              const struct gl_pixelstore_attrib *srcPacking,
3342                              GLbitfield transferOps )
3343{
3344   ASSERT(dstFormat == GL_ALPHA ||
3345          dstFormat == GL_LUMINANCE ||
3346          dstFormat == GL_LUMINANCE_ALPHA ||
3347          dstFormat == GL_INTENSITY ||
3348          dstFormat == GL_RGB ||
3349          dstFormat == GL_RGBA ||
3350          dstFormat == GL_COLOR_INDEX);
3351
3352   ASSERT(srcFormat == GL_RED ||
3353          srcFormat == GL_GREEN ||
3354          srcFormat == GL_BLUE ||
3355          srcFormat == GL_ALPHA ||
3356          srcFormat == GL_LUMINANCE ||
3357          srcFormat == GL_LUMINANCE_ALPHA ||
3358          srcFormat == GL_INTENSITY ||
3359          srcFormat == GL_RGB ||
3360          srcFormat == GL_BGR ||
3361          srcFormat == GL_RGBA ||
3362          srcFormat == GL_BGRA ||
3363          srcFormat == GL_ABGR_EXT ||
3364          srcFormat == GL_COLOR_INDEX);
3365
3366   ASSERT(srcType == GL_BITMAP ||
3367          srcType == GL_UNSIGNED_BYTE ||
3368          srcType == GL_BYTE ||
3369          srcType == GL_UNSIGNED_SHORT ||
3370          srcType == GL_SHORT ||
3371          srcType == GL_UNSIGNED_INT ||
3372          srcType == GL_INT ||
3373          srcType == GL_HALF_FLOAT_ARB ||
3374          srcType == GL_FLOAT ||
3375          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3376          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3377          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3378          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3379          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3380          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3381          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3382          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3383          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3384          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3385          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3386          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3387
3388   /* Try simple cases first */
3389   if (transferOps == 0) {
3390      if (srcType == CHAN_TYPE) {
3391         if (dstFormat == GL_RGBA) {
3392            if (srcFormat == GL_RGBA) {
3393               _mesa_memcpy( dest, source, n * 4 * sizeof(GLchan) );
3394               return;
3395            }
3396            else if (srcFormat == GL_RGB) {
3397               GLuint i;
3398               const GLchan *src = (const GLchan *) source;
3399               GLchan *dst = dest;
3400               for (i = 0; i < n; i++) {
3401                  dst[0] = src[0];
3402                  dst[1] = src[1];
3403                  dst[2] = src[2];
3404                  dst[3] = CHAN_MAX;
3405                  src += 3;
3406                  dst += 4;
3407               }
3408               return;
3409            }
3410         }
3411         else if (dstFormat == GL_RGB) {
3412            if (srcFormat == GL_RGB) {
3413               _mesa_memcpy( dest, source, n * 3 * sizeof(GLchan) );
3414               return;
3415            }
3416            else if (srcFormat == GL_RGBA) {
3417               GLuint i;
3418               const GLchan *src = (const GLchan *) source;
3419               GLchan *dst = dest;
3420               for (i = 0; i < n; i++) {
3421                  dst[0] = src[0];
3422                  dst[1] = src[1];
3423                  dst[2] = src[2];
3424                  src += 4;
3425                  dst += 3;
3426               }
3427               return;
3428            }
3429         }
3430         else if (dstFormat == srcFormat) {
3431            GLint comps = _mesa_components_in_format(srcFormat);
3432            assert(comps > 0);
3433            _mesa_memcpy( dest, source, n * comps * sizeof(GLchan) );
3434            return;
3435         }
3436      }
3437      /*
3438       * Common situation, loading 8bit RGBA/RGB source images
3439       * into 16/32 bit destination. (OSMesa16/32)
3440       */
3441      else if (srcType == GL_UNSIGNED_BYTE) {
3442         if (dstFormat == GL_RGBA) {
3443            if (srcFormat == GL_RGB) {
3444               GLuint i;
3445               const GLubyte *src = (const GLubyte *) source;
3446               GLchan *dst = dest;
3447               for (i = 0; i < n; i++) {
3448                  dst[0] = UBYTE_TO_CHAN(src[0]);
3449                  dst[1] = UBYTE_TO_CHAN(src[1]);
3450                  dst[2] = UBYTE_TO_CHAN(src[2]);
3451                  dst[3] = CHAN_MAX;
3452                  src += 3;
3453                  dst += 4;
3454               }
3455               return;
3456            }
3457            else if (srcFormat == GL_RGBA) {
3458               GLuint i;
3459               const GLubyte *src = (const GLubyte *) source;
3460               GLchan *dst = dest;
3461               for (i = 0; i < n; i++) {
3462                  dst[0] = UBYTE_TO_CHAN(src[0]);
3463                  dst[1] = UBYTE_TO_CHAN(src[1]);
3464                  dst[2] = UBYTE_TO_CHAN(src[2]);
3465                  dst[3] = UBYTE_TO_CHAN(src[3]);
3466                  src += 4;
3467                  dst += 4;
3468               }
3469               return;
3470             }
3471         }
3472         else if (dstFormat == GL_RGB) {
3473            if (srcFormat == GL_RGB) {
3474               GLuint i;
3475               const GLubyte *src = (const GLubyte *) source;
3476               GLchan *dst = dest;
3477               for (i = 0; i < n; i++) {
3478                  dst[0] = UBYTE_TO_CHAN(src[0]);
3479                  dst[1] = UBYTE_TO_CHAN(src[1]);
3480                  dst[2] = UBYTE_TO_CHAN(src[2]);
3481                  src += 3;
3482                  dst += 3;
3483               }
3484               return;
3485            }
3486            else if (srcFormat == GL_RGBA) {
3487               GLuint i;
3488               const GLubyte *src = (const GLubyte *) source;
3489               GLchan *dst = dest;
3490               for (i = 0; i < n; i++) {
3491                  dst[0] = UBYTE_TO_CHAN(src[0]);
3492                  dst[1] = UBYTE_TO_CHAN(src[1]);
3493                  dst[2] = UBYTE_TO_CHAN(src[2]);
3494                  src += 4;
3495                  dst += 3;
3496               }
3497               return;
3498            }
3499         }
3500      }
3501   }
3502
3503
3504   /* general solution begins here */
3505   {
3506      GLint dstComponents;
3507      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3508      GLint dstLuminanceIndex, dstIntensityIndex;
3509      GLfloat rgba[MAX_WIDTH][4];
3510
3511      dstComponents = _mesa_components_in_format( dstFormat );
3512      /* source & dest image formats should have been error checked by now */
3513      assert(dstComponents > 0);
3514
3515      /*
3516       * Extract image data and convert to RGBA floats
3517       */
3518      assert(n <= MAX_WIDTH);
3519      if (srcFormat == GL_COLOR_INDEX) {
3520         GLuint indexes[MAX_WIDTH];
3521         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3522                              srcPacking);
3523
3524         if (dstFormat == GL_COLOR_INDEX) {
3525            GLuint i;
3526            _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3527            /* convert to GLchan and return */
3528            for (i = 0; i < n; i++) {
3529               dest[i] = (GLchan) (indexes[i] & 0xff);
3530            }
3531            return;
3532         }
3533         else {
3534            /* Convert indexes to RGBA */
3535            if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3536               shift_and_offset_ci(ctx, n, indexes);
3537            }
3538            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3539         }
3540
3541         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3542          * with color indexes.
3543          */
3544         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3545      }
3546      else {
3547         /* non-color index data */
3548         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3549                            srcPacking->SwapBytes);
3550      }
3551
3552      /* Need to clamp if returning GLubytes or GLushorts */
3553#if CHAN_TYPE != GL_FLOAT
3554      transferOps |= IMAGE_CLAMP_BIT;
3555#endif
3556
3557      if (transferOps) {
3558         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3559      }
3560
3561      /* Now determine which color channels we need to produce.
3562       * And determine the dest index (offset) within each color tuple.
3563       */
3564      switch (dstFormat) {
3565         case GL_ALPHA:
3566            dstAlphaIndex = 0;
3567            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3568            dstLuminanceIndex = dstIntensityIndex = -1;
3569            break;
3570         case GL_LUMINANCE:
3571            dstLuminanceIndex = 0;
3572            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3573            dstIntensityIndex = -1;
3574            break;
3575         case GL_LUMINANCE_ALPHA:
3576            dstLuminanceIndex = 0;
3577            dstAlphaIndex = 1;
3578            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3579            dstIntensityIndex = -1;
3580            break;
3581         case GL_INTENSITY:
3582            dstIntensityIndex = 0;
3583            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3584            dstLuminanceIndex = -1;
3585            break;
3586         case GL_RGB:
3587            dstRedIndex = 0;
3588            dstGreenIndex = 1;
3589            dstBlueIndex = 2;
3590            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3591            break;
3592         case GL_RGBA:
3593            dstRedIndex = 0;
3594            dstGreenIndex = 1;
3595            dstBlueIndex = 2;
3596            dstAlphaIndex = 3;
3597            dstLuminanceIndex = dstIntensityIndex = -1;
3598            break;
3599         default:
3600            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_chan_span()");
3601            return;
3602      }
3603
3604
3605      /* Now return the GLchan data in the requested dstFormat */
3606
3607      if (dstRedIndex >= 0) {
3608         GLchan *dst = dest;
3609         GLuint i;
3610         for (i = 0; i < n; i++) {
3611            CLAMPED_FLOAT_TO_CHAN(dst[dstRedIndex], rgba[i][RCOMP]);
3612            dst += dstComponents;
3613         }
3614      }
3615
3616      if (dstGreenIndex >= 0) {
3617         GLchan *dst = dest;
3618         GLuint i;
3619         for (i = 0; i < n; i++) {
3620            CLAMPED_FLOAT_TO_CHAN(dst[dstGreenIndex], rgba[i][GCOMP]);
3621            dst += dstComponents;
3622         }
3623      }
3624
3625      if (dstBlueIndex >= 0) {
3626         GLchan *dst = dest;
3627         GLuint i;
3628         for (i = 0; i < n; i++) {
3629            CLAMPED_FLOAT_TO_CHAN(dst[dstBlueIndex], rgba[i][BCOMP]);
3630            dst += dstComponents;
3631         }
3632      }
3633
3634      if (dstAlphaIndex >= 0) {
3635         GLchan *dst = dest;
3636         GLuint i;
3637         for (i = 0; i < n; i++) {
3638            CLAMPED_FLOAT_TO_CHAN(dst[dstAlphaIndex], rgba[i][ACOMP]);
3639            dst += dstComponents;
3640         }
3641      }
3642
3643      if (dstIntensityIndex >= 0) {
3644         GLchan *dst = dest;
3645         GLuint i;
3646         assert(dstIntensityIndex == 0);
3647         assert(dstComponents == 1);
3648         for (i = 0; i < n; i++) {
3649            /* Intensity comes from red channel */
3650            CLAMPED_FLOAT_TO_CHAN(dst[i], rgba[i][RCOMP]);
3651         }
3652      }
3653
3654      if (dstLuminanceIndex >= 0) {
3655         GLchan *dst = dest;
3656         GLuint i;
3657         assert(dstLuminanceIndex == 0);
3658         for (i = 0; i < n; i++) {
3659            /* Luminance comes from red channel */
3660            CLAMPED_FLOAT_TO_CHAN(dst[0], rgba[i][RCOMP]);
3661            dst += dstComponents;
3662         }
3663      }
3664   }
3665}
3666
3667
3668/**
3669 * Same as _mesa_unpack_color_span_chan(), but return GLfloat data
3670 * instead of GLchan.
3671 */
3672void
3673_mesa_unpack_color_span_float( GLcontext *ctx,
3674                               GLuint n, GLenum dstFormat, GLfloat dest[],
3675                               GLenum srcFormat, GLenum srcType,
3676                               const GLvoid *source,
3677                               const struct gl_pixelstore_attrib *srcPacking,
3678                               GLbitfield transferOps )
3679{
3680   ASSERT(dstFormat == GL_ALPHA ||
3681          dstFormat == GL_LUMINANCE ||
3682          dstFormat == GL_LUMINANCE_ALPHA ||
3683          dstFormat == GL_INTENSITY ||
3684          dstFormat == GL_RGB ||
3685          dstFormat == GL_RGBA ||
3686          dstFormat == GL_COLOR_INDEX);
3687
3688   ASSERT(srcFormat == GL_RED ||
3689          srcFormat == GL_GREEN ||
3690          srcFormat == GL_BLUE ||
3691          srcFormat == GL_ALPHA ||
3692          srcFormat == GL_LUMINANCE ||
3693          srcFormat == GL_LUMINANCE_ALPHA ||
3694          srcFormat == GL_INTENSITY ||
3695          srcFormat == GL_RGB ||
3696          srcFormat == GL_BGR ||
3697          srcFormat == GL_RGBA ||
3698          srcFormat == GL_BGRA ||
3699          srcFormat == GL_ABGR_EXT ||
3700          srcFormat == GL_COLOR_INDEX);
3701
3702   ASSERT(srcType == GL_BITMAP ||
3703          srcType == GL_UNSIGNED_BYTE ||
3704          srcType == GL_BYTE ||
3705          srcType == GL_UNSIGNED_SHORT ||
3706          srcType == GL_SHORT ||
3707          srcType == GL_UNSIGNED_INT ||
3708          srcType == GL_INT ||
3709          srcType == GL_HALF_FLOAT_ARB ||
3710          srcType == GL_FLOAT ||
3711          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3712          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3713          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3714          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3715          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3716          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3717          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3718          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3719          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3720          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3721          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3722          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3723
3724   /* general solution, no special cases, yet */
3725   {
3726      GLint dstComponents;
3727      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3728      GLint dstLuminanceIndex, dstIntensityIndex;
3729      GLfloat rgba[MAX_WIDTH][4];
3730
3731      dstComponents = _mesa_components_in_format( dstFormat );
3732      /* source & dest image formats should have been error checked by now */
3733      assert(dstComponents > 0);
3734
3735      /*
3736       * Extract image data and convert to RGBA floats
3737       */
3738      assert(n <= MAX_WIDTH);
3739      if (srcFormat == GL_COLOR_INDEX) {
3740         GLuint indexes[MAX_WIDTH];
3741         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3742                              srcPacking);
3743
3744         if (dstFormat == GL_COLOR_INDEX) {
3745            GLuint i;
3746            _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3747            /* convert to GLchan and return */
3748            for (i = 0; i < n; i++) {
3749               dest[i] = (GLchan) (indexes[i] & 0xff);
3750            }
3751            return;
3752         }
3753         else {
3754            /* Convert indexes to RGBA */
3755            if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3756               shift_and_offset_ci(ctx, n, indexes);
3757            }
3758            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3759         }
3760
3761         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3762          * with color indexes.
3763          */
3764         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3765      }
3766      else {
3767         /* non-color index data */
3768         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3769                            srcPacking->SwapBytes);
3770      }
3771
3772      if (transferOps) {
3773         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3774      }
3775
3776      /* Now determine which color channels we need to produce.
3777       * And determine the dest index (offset) within each color tuple.
3778       */
3779      switch (dstFormat) {
3780         case GL_ALPHA:
3781            dstAlphaIndex = 0;
3782            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3783            dstLuminanceIndex = dstIntensityIndex = -1;
3784            break;
3785         case GL_LUMINANCE:
3786            dstLuminanceIndex = 0;
3787            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3788            dstIntensityIndex = -1;
3789            break;
3790         case GL_LUMINANCE_ALPHA:
3791            dstLuminanceIndex = 0;
3792            dstAlphaIndex = 1;
3793            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3794            dstIntensityIndex = -1;
3795            break;
3796         case GL_INTENSITY:
3797            dstIntensityIndex = 0;
3798            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3799            dstLuminanceIndex = -1;
3800            break;
3801         case GL_RGB:
3802            dstRedIndex = 0;
3803            dstGreenIndex = 1;
3804            dstBlueIndex = 2;
3805            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3806            break;
3807         case GL_RGBA:
3808            dstRedIndex = 0;
3809            dstGreenIndex = 1;
3810            dstBlueIndex = 2;
3811            dstAlphaIndex = 3;
3812            dstLuminanceIndex = dstIntensityIndex = -1;
3813            break;
3814         default:
3815            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_color_span_float()");
3816            return;
3817      }
3818
3819      /* Now pack results in the requested dstFormat */
3820      if (dstRedIndex >= 0) {
3821         GLfloat *dst = dest;
3822         GLuint i;
3823         for (i = 0; i < n; i++) {
3824            dst[dstRedIndex] = rgba[i][RCOMP];
3825            dst += dstComponents;
3826         }
3827      }
3828
3829      if (dstGreenIndex >= 0) {
3830         GLfloat *dst = dest;
3831         GLuint i;
3832         for (i = 0; i < n; i++) {
3833            dst[dstGreenIndex] = rgba[i][GCOMP];
3834            dst += dstComponents;
3835         }
3836      }
3837
3838      if (dstBlueIndex >= 0) {
3839         GLfloat *dst = dest;
3840         GLuint i;
3841         for (i = 0; i < n; i++) {
3842            dst[dstBlueIndex] = rgba[i][BCOMP];
3843            dst += dstComponents;
3844         }
3845      }
3846
3847      if (dstAlphaIndex >= 0) {
3848         GLfloat *dst = dest;
3849         GLuint i;
3850         for (i = 0; i < n; i++) {
3851            dst[dstAlphaIndex] = rgba[i][ACOMP];
3852            dst += dstComponents;
3853         }
3854      }
3855
3856      if (dstIntensityIndex >= 0) {
3857         GLfloat *dst = dest;
3858         GLuint i;
3859         assert(dstIntensityIndex == 0);
3860         assert(dstComponents == 1);
3861         for (i = 0; i < n; i++) {
3862            /* Intensity comes from red channel */
3863            dst[i] = rgba[i][RCOMP];
3864         }
3865      }
3866
3867      if (dstLuminanceIndex >= 0) {
3868         GLfloat *dst = dest;
3869         GLuint i;
3870         assert(dstLuminanceIndex == 0);
3871         for (i = 0; i < n; i++) {
3872            /* Luminance comes from red channel */
3873            dst[0] = rgba[i][RCOMP];
3874            dst += dstComponents;
3875         }
3876      }
3877   }
3878}
3879
3880
3881/*
3882 * Unpack a row of color index data from a client buffer according to
3883 * the pixel unpacking parameters.
3884 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3885 *
3886 * Args:  ctx - the context
3887 *        n - number of pixels
3888 *        dstType - destination data type
3889 *        dest - destination array
3890 *        srcType - source pixel type
3891 *        source - source data pointer
3892 *        srcPacking - pixel unpacking parameters
3893 *        transferOps - the pixel transfer operations to apply
3894 */
3895void
3896_mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
3897                         GLenum dstType, GLvoid *dest,
3898                         GLenum srcType, const GLvoid *source,
3899                         const struct gl_pixelstore_attrib *srcPacking,
3900                         GLbitfield transferOps )
3901{
3902   ASSERT(srcType == GL_BITMAP ||
3903          srcType == GL_UNSIGNED_BYTE ||
3904          srcType == GL_BYTE ||
3905          srcType == GL_UNSIGNED_SHORT ||
3906          srcType == GL_SHORT ||
3907          srcType == GL_UNSIGNED_INT ||
3908          srcType == GL_INT ||
3909          srcType == GL_HALF_FLOAT_ARB ||
3910          srcType == GL_FLOAT);
3911
3912   ASSERT(dstType == GL_UNSIGNED_BYTE ||
3913          dstType == GL_UNSIGNED_SHORT ||
3914          dstType == GL_UNSIGNED_INT);
3915
3916
3917   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3918
3919   /*
3920    * Try simple cases first
3921    */
3922   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
3923       && dstType == GL_UNSIGNED_BYTE) {
3924      _mesa_memcpy(dest, source, n * sizeof(GLubyte));
3925   }
3926   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
3927            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
3928      _mesa_memcpy(dest, source, n * sizeof(GLuint));
3929   }
3930   else {
3931      /*
3932       * general solution
3933       */
3934      GLuint indexes[MAX_WIDTH];
3935      assert(n <= MAX_WIDTH);
3936
3937      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
3938                           srcPacking);
3939
3940      if (transferOps)
3941         _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3942
3943      /* convert to dest type */
3944      switch (dstType) {
3945         case GL_UNSIGNED_BYTE:
3946            {
3947               GLubyte *dst = (GLubyte *) dest;
3948               GLuint i;
3949               for (i = 0; i < n; i++) {
3950                  dst[i] = (GLubyte) (indexes[i] & 0xff);
3951               }
3952            }
3953            break;
3954         case GL_UNSIGNED_SHORT:
3955            {
3956               GLuint *dst = (GLuint *) dest;
3957               GLuint i;
3958               for (i = 0; i < n; i++) {
3959                  dst[i] = (GLushort) (indexes[i] & 0xffff);
3960               }
3961            }
3962            break;
3963         case GL_UNSIGNED_INT:
3964            _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
3965            break;
3966         default:
3967            _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
3968      }
3969   }
3970}
3971
3972
3973void
3974_mesa_pack_index_span( const GLcontext *ctx, GLuint n,
3975                       GLenum dstType, GLvoid *dest, const GLuint *source,
3976                       const struct gl_pixelstore_attrib *dstPacking,
3977                       GLbitfield transferOps )
3978{
3979   GLuint indexes[MAX_WIDTH];
3980
3981   ASSERT(n <= MAX_WIDTH);
3982
3983   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3984
3985   if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
3986      /* make a copy of input */
3987      _mesa_memcpy(indexes, source, n * sizeof(GLuint));
3988      _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3989      source = indexes;
3990   }
3991
3992   switch (dstType) {
3993   case GL_UNSIGNED_BYTE:
3994      {
3995         GLubyte *dst = (GLubyte *) dest;
3996         GLuint i;
3997         for (i = 0; i < n; i++) {
3998            *dst++ = (GLubyte) source[i];
3999         }
4000      }
4001      break;
4002   case GL_BYTE:
4003      {
4004         GLbyte *dst = (GLbyte *) dest;
4005         GLuint i;
4006         for (i = 0; i < n; i++) {
4007            dst[i] = (GLbyte) source[i];
4008         }
4009      }
4010      break;
4011   case GL_UNSIGNED_SHORT:
4012      {
4013         GLushort *dst = (GLushort *) dest;
4014         GLuint i;
4015         for (i = 0; i < n; i++) {
4016            dst[i] = (GLushort) source[i];
4017         }
4018         if (dstPacking->SwapBytes) {
4019            _mesa_swap2( (GLushort *) dst, n );
4020         }
4021      }
4022      break;
4023   case GL_SHORT:
4024      {
4025         GLshort *dst = (GLshort *) dest;
4026         GLuint i;
4027         for (i = 0; i < n; i++) {
4028            dst[i] = (GLshort) source[i];
4029         }
4030         if (dstPacking->SwapBytes) {
4031            _mesa_swap2( (GLushort *) dst, n );
4032         }
4033      }
4034      break;
4035   case GL_UNSIGNED_INT:
4036      {
4037         GLuint *dst = (GLuint *) dest;
4038         GLuint i;
4039         for (i = 0; i < n; i++) {
4040            dst[i] = (GLuint) source[i];
4041         }
4042         if (dstPacking->SwapBytes) {
4043            _mesa_swap4( (GLuint *) dst, n );
4044         }
4045      }
4046      break;
4047   case GL_INT:
4048      {
4049         GLint *dst = (GLint *) dest;
4050         GLuint i;
4051         for (i = 0; i < n; i++) {
4052            dst[i] = (GLint) source[i];
4053         }
4054         if (dstPacking->SwapBytes) {
4055            _mesa_swap4( (GLuint *) dst, n );
4056         }
4057      }
4058      break;
4059   case GL_FLOAT:
4060      {
4061         GLfloat *dst = (GLfloat *) dest;
4062         GLuint i;
4063         for (i = 0; i < n; i++) {
4064            dst[i] = (GLfloat) source[i];
4065         }
4066         if (dstPacking->SwapBytes) {
4067            _mesa_swap4( (GLuint *) dst, n );
4068         }
4069      }
4070      break;
4071   case GL_HALF_FLOAT_ARB:
4072      {
4073         GLhalfARB *dst = (GLhalfARB *) dest;
4074         GLuint i;
4075         for (i = 0; i < n; i++) {
4076            dst[i] = _mesa_float_to_half((GLfloat) source[i]);
4077         }
4078         if (dstPacking->SwapBytes) {
4079            _mesa_swap2( (GLushort *) dst, n );
4080         }
4081      }
4082      break;
4083   default:
4084      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4085   }
4086}
4087
4088
4089/*
4090 * Unpack a row of stencil data from a client buffer according to
4091 * the pixel unpacking parameters.
4092 * This is (or will be) used by glDrawPixels
4093 *
4094 * Args:  ctx - the context
4095 *        n - number of pixels
4096 *        dstType - destination data type
4097 *        dest - destination array
4098 *        srcType - source pixel type
4099 *        source - source data pointer
4100 *        srcPacking - pixel unpacking parameters
4101 *        transferOps - apply offset/bias/lookup ops?
4102 */
4103void
4104_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
4105                           GLenum dstType, GLvoid *dest,
4106                           GLenum srcType, const GLvoid *source,
4107                           const struct gl_pixelstore_attrib *srcPacking,
4108                           GLbitfield transferOps )
4109{
4110   ASSERT(srcType == GL_BITMAP ||
4111          srcType == GL_UNSIGNED_BYTE ||
4112          srcType == GL_BYTE ||
4113          srcType == GL_UNSIGNED_SHORT ||
4114          srcType == GL_SHORT ||
4115          srcType == GL_UNSIGNED_INT ||
4116          srcType == GL_INT ||
4117          srcType == GL_UNSIGNED_INT_24_8_EXT ||
4118          srcType == GL_HALF_FLOAT_ARB ||
4119          srcType == GL_FLOAT);
4120
4121   ASSERT(dstType == GL_UNSIGNED_BYTE ||
4122          dstType == GL_UNSIGNED_SHORT ||
4123          dstType == GL_UNSIGNED_INT);
4124
4125   /* only shift and offset apply to stencil */
4126   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
4127
4128   /*
4129    * Try simple cases first
4130    */
4131   if (transferOps == 0 &&
4132       !ctx->Pixel.MapStencilFlag &&
4133       srcType == GL_UNSIGNED_BYTE &&
4134       dstType == GL_UNSIGNED_BYTE) {
4135      _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4136   }
4137   else if (transferOps == 0 &&
4138            !ctx->Pixel.MapStencilFlag &&
4139            srcType == GL_UNSIGNED_INT &&
4140            dstType == GL_UNSIGNED_INT &&
4141            !srcPacking->SwapBytes) {
4142      _mesa_memcpy(dest, source, n * sizeof(GLuint));
4143   }
4144   else {
4145      /*
4146       * general solution
4147       */
4148      GLuint indexes[MAX_WIDTH];
4149      assert(n <= MAX_WIDTH);
4150
4151      extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
4152                           srcPacking);
4153
4154      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4155         /* shift and offset indexes */
4156         shift_and_offset_ci(ctx, n, indexes);
4157      }
4158
4159      if (ctx->Pixel.MapStencilFlag) {
4160         /* Apply stencil lookup table */
4161         const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
4162         GLuint i;
4163         for (i = 0; i < n; i++) {
4164            indexes[i] = ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
4165         }
4166      }
4167
4168      /* convert to dest type */
4169      switch (dstType) {
4170         case GL_UNSIGNED_BYTE:
4171            {
4172               GLubyte *dst = (GLubyte *) dest;
4173               GLuint i;
4174               for (i = 0; i < n; i++) {
4175                  dst[i] = (GLubyte) (indexes[i] & 0xff);
4176               }
4177            }
4178            break;
4179         case GL_UNSIGNED_SHORT:
4180            {
4181               GLuint *dst = (GLuint *) dest;
4182               GLuint i;
4183               for (i = 0; i < n; i++) {
4184                  dst[i] = (GLushort) (indexes[i] & 0xffff);
4185               }
4186            }
4187            break;
4188         case GL_UNSIGNED_INT:
4189            _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4190            break;
4191         default:
4192            _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
4193      }
4194   }
4195}
4196
4197
4198void
4199_mesa_pack_stencil_span( const GLcontext *ctx, GLuint n,
4200                         GLenum dstType, GLvoid *dest, const GLstencil *source,
4201                         const struct gl_pixelstore_attrib *dstPacking )
4202{
4203   GLstencil stencil[MAX_WIDTH];
4204
4205   ASSERT(n <= MAX_WIDTH);
4206
4207   if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
4208       ctx->Pixel.MapStencilFlag) {
4209      /* make a copy of input */
4210      _mesa_memcpy(stencil, source, n * sizeof(GLstencil));
4211      _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
4212      source = stencil;
4213   }
4214
4215   switch (dstType) {
4216   case GL_UNSIGNED_BYTE:
4217      if (sizeof(GLstencil) == 1) {
4218         _mesa_memcpy( dest, source, n );
4219      }
4220      else {
4221         GLubyte *dst = (GLubyte *) dest;
4222         GLuint i;
4223         for (i=0;i<n;i++) {
4224            dst[i] = (GLubyte) source[i];
4225         }
4226      }
4227      break;
4228   case GL_BYTE:
4229      {
4230         GLbyte *dst = (GLbyte *) dest;
4231         GLuint i;
4232         for (i=0;i<n;i++) {
4233            dst[i] = (GLbyte) (source[i] & 0x7f);
4234         }
4235      }
4236      break;
4237   case GL_UNSIGNED_SHORT:
4238      {
4239         GLushort *dst = (GLushort *) dest;
4240         GLuint i;
4241         for (i=0;i<n;i++) {
4242            dst[i] = (GLushort) source[i];
4243         }
4244         if (dstPacking->SwapBytes) {
4245            _mesa_swap2( (GLushort *) dst, n );
4246         }
4247      }
4248      break;
4249   case GL_SHORT:
4250      {
4251         GLshort *dst = (GLshort *) dest;
4252         GLuint i;
4253         for (i=0;i<n;i++) {
4254            dst[i] = (GLshort) source[i];
4255         }
4256         if (dstPacking->SwapBytes) {
4257            _mesa_swap2( (GLushort *) dst, n );
4258         }
4259      }
4260      break;
4261   case GL_UNSIGNED_INT:
4262      {
4263         GLuint *dst = (GLuint *) dest;
4264         GLuint i;
4265         for (i=0;i<n;i++) {
4266            dst[i] = (GLuint) source[i];
4267         }
4268         if (dstPacking->SwapBytes) {
4269            _mesa_swap4( (GLuint *) dst, n );
4270         }
4271      }
4272      break;
4273   case GL_INT:
4274      {
4275         GLint *dst = (GLint *) dest;
4276         GLuint i;
4277         for (i=0;i<n;i++) {
4278            dst[i] = (GLint) source[i];
4279         }
4280         if (dstPacking->SwapBytes) {
4281            _mesa_swap4( (GLuint *) dst, n );
4282         }
4283      }
4284      break;
4285   case GL_FLOAT:
4286      {
4287         GLfloat *dst = (GLfloat *) dest;
4288         GLuint i;
4289         for (i=0;i<n;i++) {
4290            dst[i] = (GLfloat) source[i];
4291         }
4292         if (dstPacking->SwapBytes) {
4293            _mesa_swap4( (GLuint *) dst, n );
4294         }
4295      }
4296      break;
4297   case GL_HALF_FLOAT_ARB:
4298      {
4299         GLhalfARB *dst = (GLhalfARB *) dest;
4300         GLuint i;
4301         for (i=0;i<n;i++) {
4302            dst[i] = _mesa_float_to_half( (float) source[i] );
4303         }
4304         if (dstPacking->SwapBytes) {
4305            _mesa_swap2( (GLushort *) dst, n );
4306         }
4307      }
4308      break;
4309   case GL_BITMAP:
4310      if (dstPacking->LsbFirst) {
4311         GLubyte *dst = (GLubyte *) dest;
4312         GLint shift = 0;
4313         GLuint i;
4314         for (i = 0; i < n; i++) {
4315            if (shift == 0)
4316               *dst = 0;
4317            *dst |= ((source[i] != 0) << shift);
4318            shift++;
4319            if (shift == 8) {
4320               shift = 0;
4321               dst++;
4322            }
4323         }
4324      }
4325      else {
4326         GLubyte *dst = (GLubyte *) dest;
4327         GLint shift = 7;
4328         GLuint i;
4329         for (i = 0; i < n; i++) {
4330            if (shift == 7)
4331               *dst = 0;
4332            *dst |= ((source[i] != 0) << shift);
4333            shift--;
4334            if (shift < 0) {
4335               shift = 7;
4336               dst++;
4337            }
4338         }
4339      }
4340      break;
4341   default:
4342      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4343   }
4344}
4345
4346#define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT)                              \
4347    do {                                                                \
4348        GLuint i;                                                       \
4349        const GLTYPE *src = (const GLTYPE *)source;                     \
4350        for (i = 0; i < n; i++) {                                       \
4351            GLTYPE value = src[i];                                      \
4352            if (srcPacking->SwapBytes) {                                \
4353                if (sizeof(GLTYPE) == 2) {                              \
4354                    SWAP2BYTE(value);                                   \
4355                } else if (sizeof(GLTYPE) == 4) {                       \
4356                    SWAP4BYTE(value);                                   \
4357                }                                                       \
4358            }                                                           \
4359            depthValues[i] = GLTYPE2FLOAT(value);                       \
4360        }                                                               \
4361    } while (0)
4362
4363
4364/**
4365 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
4366 * or GLfloat values.
4367 * The glPixelTransfer (scale/bias) params will be applied.
4368 *
4369 * \param dstType  one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
4370 * \param depthMax  max value for returned GLushort or GLuint values
4371 *                  (ignored for GLfloat).
4372 */
4373void
4374_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n,
4375                         GLenum dstType, GLvoid *dest, GLuint depthMax,
4376                         GLenum srcType, const GLvoid *source,
4377                         const struct gl_pixelstore_attrib *srcPacking )
4378{
4379   GLfloat depthTemp[MAX_WIDTH], *depthValues;
4380   GLboolean needClamp = GL_FALSE;
4381
4382   /* Look for special cases first.
4383    * Not only are these faster, they're less prone to numeric conversion
4384    * problems.  Otherwise, converting from an int type to a float then
4385    * back to an int type can introduce errors that will show up as
4386    * artifacts in things like depth peeling which uses glCopyTexImage.
4387    */
4388   if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
4389      if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
4390         const GLuint *src = (const GLuint *) source;
4391         GLushort *dst = (GLushort *) dest;
4392         GLuint i;
4393         for (i = 0; i < n; i++) {
4394            dst[i] = src[i] >> 16;
4395         }
4396         return;
4397      }
4398      if (srcType == GL_UNSIGNED_SHORT
4399          && dstType == GL_UNSIGNED_INT
4400          && depthMax == 0xffffffff) {
4401         const GLushort *src = (const GLushort *) source;
4402         GLuint *dst = (GLuint *) dest;
4403         GLuint i;
4404         for (i = 0; i < n; i++) {
4405            dst[i] = src[i] | (src[i] << 16);
4406         }
4407         return;
4408      }
4409      /* XXX may want to add additional cases here someday */
4410   }
4411
4412   /* general case path follows */
4413
4414   if (dstType == GL_FLOAT) {
4415      depthValues = (GLfloat *) dest;
4416   }
4417   else {
4418      depthValues = depthTemp;
4419   }
4420
4421   /* Convert incoming values to GLfloat.  Some conversions will require
4422    * clamping, below.
4423    */
4424   switch (srcType) {
4425      case GL_BYTE:
4426         DEPTH_VALUES(GLbyte, BYTE_TO_FLOAT);
4427         needClamp = GL_TRUE;
4428         break;
4429      case GL_UNSIGNED_BYTE:
4430         DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
4431         break;
4432      case GL_SHORT:
4433         DEPTH_VALUES(GLshort, SHORT_TO_FLOAT);
4434         needClamp = GL_TRUE;
4435         break;
4436      case GL_UNSIGNED_SHORT:
4437         DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
4438         break;
4439      case GL_INT:
4440         DEPTH_VALUES(GLint, INT_TO_FLOAT);
4441         needClamp = GL_TRUE;
4442         break;
4443      case GL_UNSIGNED_INT:
4444         DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
4445         break;
4446      case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
4447         if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
4448             depthMax == 0xffffff &&
4449             ctx->Pixel.DepthScale == 1.0 &&
4450             ctx->Pixel.DepthBias == 0.0) {
4451            const GLuint *src = (const GLuint *) source;
4452            GLuint *zValues = (GLuint *) dest;
4453            GLuint i;
4454            for (i = 0; i < n; i++) {
4455                GLuint value = src[i];
4456                if (srcPacking->SwapBytes) {
4457                    SWAP4BYTE(value);
4458                }
4459                zValues[i] = value & 0xffffff00;
4460            }
4461            return;
4462         }
4463         else {
4464            const GLuint *src = (const GLuint *) source;
4465            const GLfloat scale = 1.0f / 0xffffff;
4466            GLuint i;
4467            for (i = 0; i < n; i++) {
4468                GLuint value = src[i];
4469                if (srcPacking->SwapBytes) {
4470                    SWAP4BYTE(value);
4471                }
4472                depthValues[i] = (value >> 8) * scale;
4473            }
4474         }
4475         break;
4476      case GL_FLOAT:
4477         DEPTH_VALUES(GLfloat, 1*);
4478         needClamp = GL_TRUE;
4479         break;
4480      case GL_HALF_FLOAT_ARB:
4481         {
4482            GLuint i;
4483            const GLhalfARB *src = (const GLhalfARB *) source;
4484            for (i = 0; i < n; i++) {
4485               GLhalfARB value = src[i];
4486               if (srcPacking->SwapBytes) {
4487                  SWAP2BYTE(value);
4488               }
4489               depthValues[i] = _mesa_half_to_float(value);
4490            }
4491            needClamp = GL_TRUE;
4492         }
4493         break;
4494      default:
4495         _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
4496         return;
4497   }
4498
4499   /* apply depth scale and bias */
4500   {
4501      const GLfloat scale = ctx->Pixel.DepthScale;
4502      const GLfloat bias = ctx->Pixel.DepthBias;
4503      if (scale != 1.0 || bias != 0.0) {
4504         GLuint i;
4505         for (i = 0; i < n; i++) {
4506            depthValues[i] = depthValues[i] * scale + bias;
4507         }
4508         needClamp = GL_TRUE;
4509      }
4510   }
4511
4512   /* clamp to [0, 1] */
4513   if (needClamp) {
4514      GLuint i;
4515      for (i = 0; i < n; i++) {
4516         depthValues[i] = CLAMP(depthValues[i], 0.0, 1.0);
4517      }
4518   }
4519
4520   /*
4521    * Convert values to dstType
4522    */
4523   if (dstType == GL_UNSIGNED_INT) {
4524      GLuint *zValues = (GLuint *) dest;
4525      GLuint i;
4526      if (depthMax <= 0xffffff) {
4527         /* no overflow worries */
4528         for (i = 0; i < n; i++) {
4529            zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
4530         }
4531      }
4532      else {
4533         /* need to use double precision to prevent overflow problems */
4534         for (i = 0; i < n; i++) {
4535            GLdouble z = depthValues[i] * (GLfloat) depthMax;
4536            if (z >= (GLdouble) 0xffffffff)
4537               zValues[i] = 0xffffffff;
4538            else
4539               zValues[i] = (GLuint) z;
4540         }
4541      }
4542   }
4543   else if (dstType == GL_UNSIGNED_SHORT) {
4544      GLushort *zValues = (GLushort *) dest;
4545      GLuint i;
4546      ASSERT(depthMax <= 0xffff);
4547      for (i = 0; i < n; i++) {
4548         zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
4549      }
4550   }
4551   else {
4552      ASSERT(dstType == GL_FLOAT);
4553      /*ASSERT(depthMax == 1.0F);*/
4554   }
4555}
4556
4557
4558/*
4559 * Pack an array of depth values.  The values are floats in [0,1].
4560 */
4561void
4562_mesa_pack_depth_span( const GLcontext *ctx, GLuint n, GLvoid *dest,
4563                       GLenum dstType, const GLfloat *depthSpan,
4564                       const struct gl_pixelstore_attrib *dstPacking )
4565{
4566   GLfloat depthCopy[MAX_WIDTH];
4567
4568   ASSERT(n <= MAX_WIDTH);
4569
4570   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4571      _mesa_memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
4572      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4573      depthSpan = depthCopy;
4574   }
4575
4576   switch (dstType) {
4577   case GL_UNSIGNED_BYTE:
4578      {
4579         GLubyte *dst = (GLubyte *) dest;
4580         GLuint i;
4581         for (i = 0; i < n; i++) {
4582            dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
4583         }
4584      }
4585      break;
4586   case GL_BYTE:
4587      {
4588         GLbyte *dst = (GLbyte *) dest;
4589         GLuint i;
4590         for (i = 0; i < n; i++) {
4591            dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
4592         }
4593      }
4594      break;
4595   case GL_UNSIGNED_SHORT:
4596      {
4597         GLushort *dst = (GLushort *) dest;
4598         GLuint i;
4599         for (i = 0; i < n; i++) {
4600            CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
4601         }
4602         if (dstPacking->SwapBytes) {
4603            _mesa_swap2( (GLushort *) dst, n );
4604         }
4605      }
4606      break;
4607   case GL_SHORT:
4608      {
4609         GLshort *dst = (GLshort *) dest;
4610         GLuint i;
4611         for (i = 0; i < n; i++) {
4612            dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
4613         }
4614         if (dstPacking->SwapBytes) {
4615            _mesa_swap2( (GLushort *) dst, n );
4616         }
4617      }
4618      break;
4619   case GL_UNSIGNED_INT:
4620      {
4621         GLuint *dst = (GLuint *) dest;
4622         GLuint i;
4623         for (i = 0; i < n; i++) {
4624            dst[i] = FLOAT_TO_UINT( depthSpan[i] );
4625         }
4626         if (dstPacking->SwapBytes) {
4627            _mesa_swap4( (GLuint *) dst, n );
4628         }
4629      }
4630      break;
4631   case GL_INT:
4632      {
4633         GLint *dst = (GLint *) dest;
4634         GLuint i;
4635         for (i = 0; i < n; i++) {
4636            dst[i] = FLOAT_TO_INT( depthSpan[i] );
4637         }
4638         if (dstPacking->SwapBytes) {
4639            _mesa_swap4( (GLuint *) dst, n );
4640         }
4641      }
4642      break;
4643   case GL_FLOAT:
4644      {
4645         GLfloat *dst = (GLfloat *) dest;
4646         GLuint i;
4647         for (i = 0; i < n; i++) {
4648            dst[i] = depthSpan[i];
4649         }
4650         if (dstPacking->SwapBytes) {
4651            _mesa_swap4( (GLuint *) dst, n );
4652         }
4653      }
4654      break;
4655   case GL_HALF_FLOAT_ARB:
4656      {
4657         GLhalfARB *dst = (GLhalfARB *) dest;
4658         GLuint i;
4659         for (i = 0; i < n; i++) {
4660            dst[i] = _mesa_float_to_half(depthSpan[i]);
4661         }
4662         if (dstPacking->SwapBytes) {
4663            _mesa_swap2( (GLushort *) dst, n );
4664         }
4665      }
4666      break;
4667   default:
4668      _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
4669   }
4670}
4671
4672
4673
4674/**
4675 * Pack depth and stencil values as GL_DEPTH_STENCIL/GL_UNSIGNED_INT_24_8.
4676 */
4677void
4678_mesa_pack_depth_stencil_span(const GLcontext *ctx, GLuint n, GLuint *dest,
4679                              const GLfloat *depthVals,
4680                              const GLstencil *stencilVals,
4681                              const struct gl_pixelstore_attrib *dstPacking)
4682{
4683   GLfloat depthCopy[MAX_WIDTH];
4684   GLstencil stencilCopy[MAX_WIDTH];
4685   GLuint i;
4686
4687   ASSERT(n <= MAX_WIDTH);
4688
4689   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4690      _mesa_memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
4691      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4692      depthVals = depthCopy;
4693   }
4694
4695   if (ctx->Pixel.IndexShift ||
4696       ctx->Pixel.IndexOffset ||
4697       ctx->Pixel.MapStencilFlag) {
4698      _mesa_memcpy(stencilCopy, stencilVals, n * sizeof(GLstencil));
4699      _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
4700      stencilVals = stencilCopy;
4701   }
4702
4703   for (i = 0; i < n; i++) {
4704      GLuint z = (GLuint) (depthVals[i] * 0xffffff);
4705      dest[i] = (z << 8) | (stencilVals[i] & 0xff);
4706   }
4707
4708   if (dstPacking->SwapBytes) {
4709      _mesa_swap4(dest, n);
4710   }
4711}
4712
4713
4714
4715
4716/**
4717 * Unpack image data.  Apply byte swapping, byte flipping (bitmap).
4718 * Return all image data in a contiguous block.  This is used when we
4719 * compile glDrawPixels, glTexImage, etc into a display list.  We
4720 * need a copy of the data in a standard format.
4721 */
4722void *
4723_mesa_unpack_image( GLuint dimensions,
4724                    GLsizei width, GLsizei height, GLsizei depth,
4725                    GLenum format, GLenum type, const GLvoid *pixels,
4726                    const struct gl_pixelstore_attrib *unpack )
4727{
4728   GLint bytesPerRow, compsPerRow;
4729   GLboolean flipBytes, swap2, swap4;
4730
4731   if (!pixels)
4732      return NULL;  /* not necessarily an error */
4733
4734   if (width <= 0 || height <= 0 || depth <= 0)
4735      return NULL;  /* generate error later */
4736
4737   if (type == GL_BITMAP) {
4738      bytesPerRow = (width + 7) >> 3;
4739      flipBytes = unpack->LsbFirst;
4740      swap2 = swap4 = GL_FALSE;
4741      compsPerRow = 0;
4742   }
4743   else {
4744      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
4745      GLint components = _mesa_components_in_format(format);
4746      GLint bytesPerComp;
4747
4748      if (_mesa_type_is_packed(type))
4749          components = 1;
4750
4751      if (bytesPerPixel <= 0 || components <= 0)
4752         return NULL;   /* bad format or type.  generate error later */
4753      bytesPerRow = bytesPerPixel * width;
4754      bytesPerComp = bytesPerPixel / components;
4755      flipBytes = GL_FALSE;
4756      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
4757      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
4758      compsPerRow = components * width;
4759      assert(compsPerRow >= width);
4760   }
4761
4762   {
4763      GLubyte *destBuffer
4764         = (GLubyte *) _mesa_malloc(bytesPerRow * height * depth);
4765      GLubyte *dst;
4766      GLint img, row;
4767      if (!destBuffer)
4768         return NULL;   /* generate GL_OUT_OF_MEMORY later */
4769
4770      dst = destBuffer;
4771      for (img = 0; img < depth; img++) {
4772         for (row = 0; row < height; row++) {
4773            const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
4774                               width, height, format, type, img, row, 0);
4775
4776            if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
4777               GLint i;
4778               flipBytes = GL_FALSE;
4779               if (unpack->LsbFirst) {
4780                  GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
4781                  GLubyte dstMask = 128;
4782                  const GLubyte *s = src;
4783                  GLubyte *d = dst;
4784                  *d = 0;
4785                  for (i = 0; i < width; i++) {
4786                     if (*s & srcMask) {
4787                        *d |= dstMask;
4788                     }
4789                     if (srcMask == 128) {
4790                        srcMask = 1;
4791                        s++;
4792                     }
4793                     else {
4794                        srcMask = srcMask << 1;
4795                     }
4796                     if (dstMask == 1) {
4797                        dstMask = 128;
4798                        d++;
4799                        *d = 0;
4800                     }
4801                     else {
4802                        dstMask = dstMask >> 1;
4803                     }
4804                  }
4805               }
4806               else {
4807                  GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
4808                  GLubyte dstMask = 128;
4809                  const GLubyte *s = src;
4810                  GLubyte *d = dst;
4811                  *d = 0;
4812                  for (i = 0; i < width; i++) {
4813                     if (*s & srcMask) {
4814                        *d |= dstMask;
4815                     }
4816                     if (srcMask == 1) {
4817                        srcMask = 128;
4818                        s++;
4819                     }
4820                     else {
4821                        srcMask = srcMask >> 1;
4822                     }
4823                     if (dstMask == 1) {
4824                        dstMask = 128;
4825                        d++;
4826                        *d = 0;
4827                     }
4828                     else {
4829                        dstMask = dstMask >> 1;
4830                     }
4831                  }
4832               }
4833            }
4834            else {
4835               _mesa_memcpy(dst, src, bytesPerRow);
4836            }
4837
4838            /* byte flipping/swapping */
4839            if (flipBytes) {
4840               flip_bytes((GLubyte *) dst, bytesPerRow);
4841            }
4842            else if (swap2) {
4843               _mesa_swap2((GLushort*) dst, compsPerRow);
4844            }
4845            else if (swap4) {
4846               _mesa_swap4((GLuint*) dst, compsPerRow);
4847            }
4848            dst += bytesPerRow;
4849         }
4850      }
4851      return destBuffer;
4852   }
4853}
4854
4855#endif /* _HAVE_FULL_GL */
4856
4857
4858
4859/**
4860 * Convert an array of RGBA colors from one datatype to another.
4861 * NOTE: src may equal dst.  In that case, we use a temporary buffer.
4862 */
4863void
4864_mesa_convert_colors(GLenum srcType, const GLvoid *src,
4865                     GLenum dstType, GLvoid *dst,
4866                     GLuint count, const GLubyte mask[])
4867{
4868   GLuint tempBuffer[MAX_WIDTH][4];
4869   const GLboolean useTemp = (src == dst);
4870
4871   ASSERT(srcType != dstType);
4872
4873   switch (srcType) {
4874   case GL_UNSIGNED_BYTE:
4875      if (dstType == GL_UNSIGNED_SHORT) {
4876         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
4877         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
4878         GLuint i;
4879         for (i = 0; i < count; i++) {
4880            if (!mask || mask[i]) {
4881               dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
4882               dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
4883               dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
4884               dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
4885            }
4886         }
4887         if (useTemp)
4888            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
4889      }
4890      else {
4891         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
4892         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
4893         GLuint i;
4894         ASSERT(dstType == GL_FLOAT);
4895         for (i = 0; i < count; i++) {
4896            if (!mask || mask[i]) {
4897               dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
4898               dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
4899               dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
4900               dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
4901            }
4902         }
4903         if (useTemp)
4904            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
4905      }
4906      break;
4907   case GL_UNSIGNED_SHORT:
4908      if (dstType == GL_UNSIGNED_BYTE) {
4909         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
4910         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
4911         GLuint i;
4912         for (i = 0; i < count; i++) {
4913            if (!mask || mask[i]) {
4914               dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
4915               dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
4916               dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
4917               dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
4918            }
4919         }
4920         if (useTemp)
4921            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
4922      }
4923      else {
4924         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
4925         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
4926         GLuint i;
4927         ASSERT(dstType == GL_FLOAT);
4928         for (i = 0; i < count; i++) {
4929            if (!mask || mask[i]) {
4930               dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
4931               dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
4932               dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
4933               dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
4934            }
4935         }
4936         if (useTemp)
4937            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
4938      }
4939      break;
4940   case GL_FLOAT:
4941      if (dstType == GL_UNSIGNED_BYTE) {
4942         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
4943         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
4944         GLuint i;
4945         for (i = 0; i < count; i++) {
4946            if (!mask || mask[i]) {
4947               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
4948               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
4949               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
4950               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
4951            }
4952         }
4953         if (useTemp)
4954            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
4955      }
4956      else {
4957         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
4958         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
4959         GLuint i;
4960         ASSERT(dstType == GL_UNSIGNED_SHORT);
4961         for (i = 0; i < count; i++) {
4962            if (!mask || mask[i]) {
4963               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
4964               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
4965               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
4966               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
4967            }
4968         }
4969         if (useTemp)
4970            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
4971      }
4972      break;
4973   default:
4974      _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
4975   }
4976}
4977
4978
4979
4980
4981/**
4982 * Perform basic clipping for glDrawPixels.  The image's position and size
4983 * and the unpack SkipPixels and SkipRows are adjusted so that the image
4984 * region is entirely within the window and scissor bounds.
4985 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
4986 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
4987 * we'll actually write.  Beforehand, *destY-1 is the first drawing row.
4988 *
4989 * \return  GL_TRUE if image is ready for drawing or
4990 *          GL_FALSE if image was completely clipped away (draw nothing)
4991 */
4992GLboolean
4993_mesa_clip_drawpixels(const GLcontext *ctx,
4994                      GLint *destX, GLint *destY,
4995                      GLsizei *width, GLsizei *height,
4996                      struct gl_pixelstore_attrib *unpack)
4997{
4998   const GLframebuffer *buffer = ctx->DrawBuffer;
4999
5000   if (unpack->RowLength == 0) {
5001      unpack->RowLength = *width;
5002   }
5003
5004   ASSERT(ctx->Pixel.ZoomX == 1.0F);
5005   ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
5006
5007   /* left clipping */
5008   if (*destX < buffer->_Xmin) {
5009      unpack->SkipPixels += (buffer->_Xmin - *destX);
5010      *width -= (buffer->_Xmin - *destX);
5011      *destX = buffer->_Xmin;
5012   }
5013   /* right clipping */
5014   if (*destX + *width > buffer->_Xmax)
5015      *width -= (*destX + *width - buffer->_Xmax);
5016
5017   if (*width <= 0)
5018      return GL_FALSE;
5019
5020   if (ctx->Pixel.ZoomY == 1.0F) {
5021      /* bottom clipping */
5022      if (*destY < buffer->_Ymin) {
5023         unpack->SkipRows += (buffer->_Ymin - *destY);
5024         *height -= (buffer->_Ymin - *destY);
5025         *destY = buffer->_Ymin;
5026      }
5027      /* top clipping */
5028      if (*destY + *height > buffer->_Ymax)
5029         *height -= (*destY + *height - buffer->_Ymax);
5030   }
5031   else { /* upside down */
5032      /* top clipping */
5033      if (*destY > buffer->_Ymax) {
5034         unpack->SkipRows += (*destY - buffer->_Ymax);
5035         *height -= (*destY - buffer->_Ymax);
5036         *destY = buffer->_Ymax;
5037      }
5038      /* bottom clipping */
5039      if (*destY - *height < buffer->_Ymin)
5040         *height -= (buffer->_Ymin - (*destY - *height));
5041      /* adjust destY so it's the first row to write to */
5042      (*destY)--;
5043   }
5044
5045   if (*height <= 0)
5046      return GL_TRUE;
5047
5048   return GL_TRUE;
5049}
5050
5051
5052/**
5053 * Perform clipping for glReadPixels.  The image's window position
5054 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
5055 * so that the image region is entirely within the window bounds.
5056 * Note: this is different from _mesa_clip_drawpixels() in that the
5057 * scissor box is ignored, and we use the bounds of the current readbuffer
5058 * surface.
5059 *
5060 * \return  GL_TRUE if image is ready for drawing or
5061 *          GL_FALSE if image was completely clipped away (draw nothing)
5062 */
5063GLboolean
5064_mesa_clip_readpixels(const GLcontext *ctx,
5065                      GLint *srcX, GLint *srcY,
5066                      GLsizei *width, GLsizei *height,
5067                      struct gl_pixelstore_attrib *pack)
5068{
5069   const GLframebuffer *buffer = ctx->ReadBuffer;
5070
5071   if (pack->RowLength == 0) {
5072      pack->RowLength = *width;
5073   }
5074
5075   /* left clipping */
5076   if (*srcX < 0) {
5077      pack->SkipPixels += (0 - *srcX);
5078      *width -= (0 - *srcX);
5079      *srcX = 0;
5080   }
5081   /* right clipping */
5082   if (*srcX + *width > (GLsizei) buffer->Width)
5083      *width -= (*srcX + *width - buffer->Width);
5084
5085   if (*width <= 0)
5086      return GL_FALSE;
5087
5088   /* bottom clipping */
5089   if (*srcY < 0) {
5090      pack->SkipRows += (0 - *srcY);
5091      *height -= (0 - *srcY);
5092      *srcY = 0;
5093   }
5094   /* top clipping */
5095   if (*srcY + *height > (GLsizei) buffer->Height)
5096      *height -= (*srcY + *height - buffer->Height);
5097
5098   if (*height <= 0)
5099      return GL_TRUE;
5100
5101   return GL_TRUE;
5102}
5103
5104
5105/**
5106 * Do clipping for a glCopyTexSubImage call.
5107 * The framebuffer source region might extend outside the framebuffer
5108 * bounds.  Clip the source region against the framebuffer bounds and
5109 * adjust the texture/dest position and size accordingly.
5110 *
5111 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
5112 */
5113GLboolean
5114_mesa_clip_copytexsubimage(const GLcontext *ctx,
5115                           GLint *destX, GLint *destY,
5116                           GLint *srcX, GLint *srcY,
5117                           GLsizei *width, GLsizei *height)
5118{
5119   const struct gl_framebuffer *fb = ctx->ReadBuffer;
5120   const GLint srcX0 = *srcX, srcY0 = *srcY;
5121
5122   if (_mesa_clip_to_region(fb->_Xmin, fb->_Ymin, fb->_Xmax, fb->_Ymax,
5123                            srcX, srcY, width, height)) {
5124      *destX = *destX + *srcX - srcX0;
5125      *destY = *destY + *srcY - srcY0;
5126
5127      return GL_TRUE;
5128   }
5129   else {
5130      return GL_FALSE;
5131   }
5132}
5133
5134
5135
5136/**
5137 * Clip the rectangle defined by (x, y, width, height) against the bounds
5138 * specified by [xmin, xmax) and [ymin, ymax).
5139 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
5140 */
5141GLboolean
5142_mesa_clip_to_region(GLint xmin, GLint ymin,
5143                     GLint xmax, GLint ymax,
5144                     GLint *x, GLint *y,
5145                     GLsizei *width, GLsizei *height )
5146{
5147   /* left clipping */
5148   if (*x < xmin) {
5149      *width -= (xmin - *x);
5150      *x = xmin;
5151   }
5152
5153   /* right clipping */
5154   if (*x + *width > xmax)
5155      *width -= (*x + *width - xmax - 1);
5156
5157   if (*width <= 0)
5158      return GL_FALSE;
5159
5160   /* bottom (or top) clipping */
5161   if (*y < ymin) {
5162      *height -= (ymin - *y);
5163      *y = ymin;
5164   }
5165
5166   /* top (or bottom) clipping */
5167   if (*y + *height > ymax)
5168      *height -= (*y + *height - ymax - 1);
5169
5170   if (*height <= 0)
5171      return GL_FALSE;
5172
5173   return GL_TRUE;
5174}
5175