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