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