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