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