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