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