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