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