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