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