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