image.c revision 10d81f466794c61c11225c94b2125bf2dd23748e
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/*****                  Pixel processing functions               ******/
1264/**********************************************************************/
1265
1266/*
1267 * Apply scale and bias factors to an array of RGBA pixels.
1268 */
1269void
1270_mesa_scale_and_bias_rgba(GLuint n, GLfloat rgba[][4],
1271                          GLfloat rScale, GLfloat gScale,
1272                          GLfloat bScale, GLfloat aScale,
1273                          GLfloat rBias, GLfloat gBias,
1274                          GLfloat bBias, GLfloat aBias)
1275{
1276   if (rScale != 1.0 || rBias != 0.0) {
1277      GLuint i;
1278      for (i = 0; i < n; i++) {
1279         rgba[i][RCOMP] = rgba[i][RCOMP] * rScale + rBias;
1280      }
1281   }
1282   if (gScale != 1.0 || gBias != 0.0) {
1283      GLuint i;
1284      for (i = 0; i < n; i++) {
1285         rgba[i][GCOMP] = rgba[i][GCOMP] * gScale + gBias;
1286      }
1287   }
1288   if (bScale != 1.0 || bBias != 0.0) {
1289      GLuint i;
1290      for (i = 0; i < n; i++) {
1291         rgba[i][BCOMP] = rgba[i][BCOMP] * bScale + bBias;
1292      }
1293   }
1294   if (aScale != 1.0 || aBias != 0.0) {
1295      GLuint i;
1296      for (i = 0; i < n; i++) {
1297         rgba[i][ACOMP] = rgba[i][ACOMP] * aScale + aBias;
1298      }
1299   }
1300}
1301
1302
1303/*
1304 * Apply pixel mapping to an array of floating point RGBA pixels.
1305 */
1306void
1307_mesa_map_rgba( const GLcontext *ctx, GLuint n, GLfloat rgba[][4] )
1308{
1309   const GLfloat rscale = (GLfloat) (ctx->PixelMaps.RtoR.Size - 1);
1310   const GLfloat gscale = (GLfloat) (ctx->PixelMaps.GtoG.Size - 1);
1311   const GLfloat bscale = (GLfloat) (ctx->PixelMaps.BtoB.Size - 1);
1312   const GLfloat ascale = (GLfloat) (ctx->PixelMaps.AtoA.Size - 1);
1313   const GLfloat *rMap = ctx->PixelMaps.RtoR.Map;
1314   const GLfloat *gMap = ctx->PixelMaps.GtoG.Map;
1315   const GLfloat *bMap = ctx->PixelMaps.BtoB.Map;
1316   const GLfloat *aMap = ctx->PixelMaps.AtoA.Map;
1317   GLuint i;
1318   for (i=0;i<n;i++) {
1319      GLfloat r = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1320      GLfloat g = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1321      GLfloat b = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1322      GLfloat a = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1323      rgba[i][RCOMP] = rMap[IROUND(r * rscale)];
1324      rgba[i][GCOMP] = gMap[IROUND(g * gscale)];
1325      rgba[i][BCOMP] = bMap[IROUND(b * bscale)];
1326      rgba[i][ACOMP] = aMap[IROUND(a * ascale)];
1327   }
1328}
1329
1330
1331/*
1332 * Apply the color matrix and post color matrix scaling and biasing.
1333 */
1334void
1335_mesa_transform_rgba(const GLcontext *ctx, GLuint n, GLfloat rgba[][4])
1336{
1337   const GLfloat rs = ctx->Pixel.PostColorMatrixScale[0];
1338   const GLfloat rb = ctx->Pixel.PostColorMatrixBias[0];
1339   const GLfloat gs = ctx->Pixel.PostColorMatrixScale[1];
1340   const GLfloat gb = ctx->Pixel.PostColorMatrixBias[1];
1341   const GLfloat bs = ctx->Pixel.PostColorMatrixScale[2];
1342   const GLfloat bb = ctx->Pixel.PostColorMatrixBias[2];
1343   const GLfloat as = ctx->Pixel.PostColorMatrixScale[3];
1344   const GLfloat ab = ctx->Pixel.PostColorMatrixBias[3];
1345   const GLfloat *m = ctx->ColorMatrixStack.Top->m;
1346   GLuint i;
1347   for (i = 0; i < n; i++) {
1348      const GLfloat r = rgba[i][RCOMP];
1349      const GLfloat g = rgba[i][GCOMP];
1350      const GLfloat b = rgba[i][BCOMP];
1351      const GLfloat a = rgba[i][ACOMP];
1352      rgba[i][RCOMP] = (m[0] * r + m[4] * g + m[ 8] * b + m[12] * a) * rs + rb;
1353      rgba[i][GCOMP] = (m[1] * r + m[5] * g + m[ 9] * b + m[13] * a) * gs + gb;
1354      rgba[i][BCOMP] = (m[2] * r + m[6] * g + m[10] * b + m[14] * a) * bs + bb;
1355      rgba[i][ACOMP] = (m[3] * r + m[7] * g + m[11] * b + m[15] * a) * as + ab;
1356   }
1357}
1358
1359
1360/**
1361 * Apply a color table lookup to an array of floating point RGBA colors.
1362 */
1363void
1364_mesa_lookup_rgba_float(const struct gl_color_table *table,
1365                        GLuint n, GLfloat rgba[][4])
1366{
1367   const GLint max = table->Size - 1;
1368   const GLfloat scale = (GLfloat) max;
1369   const GLfloat *lut = table->TableF;
1370   GLuint i;
1371
1372   if (!table->TableF || table->Size == 0)
1373      return;
1374
1375   switch (table->_BaseFormat) {
1376      case GL_INTENSITY:
1377         /* replace RGBA with I */
1378         for (i = 0; i < n; i++) {
1379            GLint j = IROUND(rgba[i][RCOMP] * scale);
1380            GLfloat c = lut[CLAMP(j, 0, max)];
1381            rgba[i][RCOMP] =
1382            rgba[i][GCOMP] =
1383            rgba[i][BCOMP] =
1384            rgba[i][ACOMP] = c;
1385         }
1386         break;
1387      case GL_LUMINANCE:
1388         /* replace RGB with L */
1389         for (i = 0; i < n; i++) {
1390            GLint j = IROUND(rgba[i][RCOMP] * scale);
1391            GLfloat c = lut[CLAMP(j, 0, max)];
1392            rgba[i][RCOMP] =
1393            rgba[i][GCOMP] =
1394            rgba[i][BCOMP] = c;
1395         }
1396         break;
1397      case GL_ALPHA:
1398         /* replace A with A */
1399         for (i = 0; i < n; i++) {
1400            GLint j = IROUND(rgba[i][ACOMP] * scale);
1401            rgba[i][ACOMP] = lut[CLAMP(j, 0, max)];
1402         }
1403         break;
1404      case GL_LUMINANCE_ALPHA:
1405         /* replace RGBA with LLLA */
1406         for (i = 0; i < n; i++) {
1407            GLint jL = IROUND(rgba[i][RCOMP] * scale);
1408            GLint jA = IROUND(rgba[i][ACOMP] * scale);
1409            GLfloat luminance, alpha;
1410            jL = CLAMP(jL, 0, max);
1411            jA = CLAMP(jA, 0, max);
1412            luminance = lut[jL * 2 + 0];
1413            alpha     = lut[jA * 2 + 1];
1414            rgba[i][RCOMP] =
1415            rgba[i][GCOMP] =
1416            rgba[i][BCOMP] = luminance;
1417            rgba[i][ACOMP] = alpha;;
1418         }
1419         break;
1420      case GL_RGB:
1421         /* replace RGB with RGB */
1422         for (i = 0; i < n; i++) {
1423            GLint jR = IROUND(rgba[i][RCOMP] * scale);
1424            GLint jG = IROUND(rgba[i][GCOMP] * scale);
1425            GLint jB = IROUND(rgba[i][BCOMP] * scale);
1426            jR = CLAMP(jR, 0, max);
1427            jG = CLAMP(jG, 0, max);
1428            jB = CLAMP(jB, 0, max);
1429            rgba[i][RCOMP] = lut[jR * 3 + 0];
1430            rgba[i][GCOMP] = lut[jG * 3 + 1];
1431            rgba[i][BCOMP] = lut[jB * 3 + 2];
1432         }
1433         break;
1434      case GL_RGBA:
1435         /* replace RGBA with RGBA */
1436         for (i = 0; i < n; i++) {
1437            GLint jR = IROUND(rgba[i][RCOMP] * scale);
1438            GLint jG = IROUND(rgba[i][GCOMP] * scale);
1439            GLint jB = IROUND(rgba[i][BCOMP] * scale);
1440            GLint jA = IROUND(rgba[i][ACOMP] * scale);
1441            jR = CLAMP(jR, 0, max);
1442            jG = CLAMP(jG, 0, max);
1443            jB = CLAMP(jB, 0, max);
1444            jA = CLAMP(jA, 0, max);
1445            rgba[i][RCOMP] = lut[jR * 4 + 0];
1446            rgba[i][GCOMP] = lut[jG * 4 + 1];
1447            rgba[i][BCOMP] = lut[jB * 4 + 2];
1448            rgba[i][ACOMP] = lut[jA * 4 + 3];
1449         }
1450         break;
1451      default:
1452         _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba_float");
1453         return;
1454   }
1455}
1456
1457
1458
1459/**
1460 * Apply a color table lookup to an array of ubyte/RGBA colors.
1461 */
1462void
1463_mesa_lookup_rgba_ubyte(const struct gl_color_table *table,
1464                        GLuint n, GLubyte rgba[][4])
1465{
1466   const GLubyte *lut = table->TableUB;
1467   const GLfloat scale = (GLfloat) (table->Size - 1) / (GLfloat)255.0;
1468   GLuint i;
1469
1470   if (!table->TableUB || table->Size == 0)
1471      return;
1472
1473   switch (table->_BaseFormat) {
1474   case GL_INTENSITY:
1475      /* replace RGBA with I */
1476      if (table->Size == 256) {
1477         for (i = 0; i < n; i++) {
1478            const GLubyte c = lut[rgba[i][RCOMP]];
1479            rgba[i][RCOMP] =
1480            rgba[i][GCOMP] =
1481            rgba[i][BCOMP] =
1482            rgba[i][ACOMP] = c;
1483         }
1484      }
1485      else {
1486         for (i = 0; i < n; i++) {
1487            GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1488            rgba[i][RCOMP] =
1489            rgba[i][GCOMP] =
1490            rgba[i][BCOMP] =
1491            rgba[i][ACOMP] = lut[j];
1492         }
1493      }
1494      break;
1495   case GL_LUMINANCE:
1496      /* replace RGB with L */
1497      if (table->Size == 256) {
1498         for (i = 0; i < n; i++) {
1499            const GLubyte c = lut[rgba[i][RCOMP]];
1500            rgba[i][RCOMP] =
1501            rgba[i][GCOMP] =
1502            rgba[i][BCOMP] = c;
1503         }
1504      }
1505      else {
1506         for (i = 0; i < n; i++) {
1507            GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1508            rgba[i][RCOMP] =
1509            rgba[i][GCOMP] =
1510            rgba[i][BCOMP] = lut[j];
1511         }
1512      }
1513      break;
1514   case GL_ALPHA:
1515      /* replace A with A */
1516      if (table->Size == 256) {
1517         for (i = 0; i < n; i++) {
1518            rgba[i][ACOMP] = lut[rgba[i][ACOMP]];
1519         }
1520      }
1521      else {
1522         for (i = 0; i < n; i++) {
1523            GLint j = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1524            rgba[i][ACOMP] = lut[j];
1525         }
1526      }
1527      break;
1528   case GL_LUMINANCE_ALPHA:
1529      /* replace RGBA with LLLA */
1530      if (table->Size == 256) {
1531         for (i = 0; i < n; i++) {
1532            GLubyte l = lut[rgba[i][RCOMP] * 2 + 0];
1533            GLubyte a = lut[rgba[i][ACOMP] * 2 + 1];;
1534            rgba[i][RCOMP] =
1535            rgba[i][GCOMP] =
1536            rgba[i][BCOMP] = l;
1537            rgba[i][ACOMP] = a;
1538         }
1539      }
1540      else {
1541         for (i = 0; i < n; i++) {
1542            GLint jL = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1543            GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1544            GLubyte luminance = lut[jL * 2 + 0];
1545            GLubyte alpha     = lut[jA * 2 + 1];
1546            rgba[i][RCOMP] =
1547            rgba[i][GCOMP] =
1548            rgba[i][BCOMP] = luminance;
1549            rgba[i][ACOMP] = alpha;
1550         }
1551      }
1552      break;
1553   case GL_RGB:
1554      if (table->Size == 256) {
1555         for (i = 0; i < n; i++) {
1556            rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 3 + 0];
1557            rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 3 + 1];
1558            rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 3 + 2];
1559         }
1560      }
1561      else {
1562         for (i = 0; i < n; i++) {
1563            GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1564            GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
1565            GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
1566            rgba[i][RCOMP] = lut[jR * 3 + 0];
1567            rgba[i][GCOMP] = lut[jG * 3 + 1];
1568            rgba[i][BCOMP] = lut[jB * 3 + 2];
1569         }
1570      }
1571      break;
1572   case GL_RGBA:
1573      if (table->Size == 256) {
1574         for (i = 0; i < n; i++) {
1575            rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 4 + 0];
1576            rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 4 + 1];
1577            rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 4 + 2];
1578            rgba[i][ACOMP] = lut[rgba[i][ACOMP] * 4 + 3];
1579         }
1580      }
1581      else {
1582         for (i = 0; i < n; i++) {
1583            GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1584            GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
1585            GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
1586            GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1587            CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], lut[jR * 4 + 0]);
1588            CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], lut[jG * 4 + 1]);
1589            CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], lut[jB * 4 + 2]);
1590            CLAMPED_FLOAT_TO_CHAN(rgba[i][ACOMP], lut[jA * 4 + 3]);
1591         }
1592      }
1593      break;
1594   default:
1595      _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba_chan");
1596      return;
1597   }
1598}
1599
1600
1601
1602/*
1603 * Map color indexes to float rgba values.
1604 */
1605void
1606_mesa_map_ci_to_rgba( const GLcontext *ctx, GLuint n,
1607                      const GLuint index[], GLfloat rgba[][4] )
1608{
1609   GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1610   GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1611   GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1612   GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1613   const GLfloat *rMap = ctx->PixelMaps.ItoR.Map;
1614   const GLfloat *gMap = ctx->PixelMaps.ItoG.Map;
1615   const GLfloat *bMap = ctx->PixelMaps.ItoB.Map;
1616   const GLfloat *aMap = ctx->PixelMaps.ItoA.Map;
1617   GLuint i;
1618   for (i=0;i<n;i++) {
1619      rgba[i][RCOMP] = rMap[index[i] & rmask];
1620      rgba[i][GCOMP] = gMap[index[i] & gmask];
1621      rgba[i][BCOMP] = bMap[index[i] & bmask];
1622      rgba[i][ACOMP] = aMap[index[i] & amask];
1623   }
1624}
1625
1626
1627/**
1628 * Map ubyte color indexes to ubyte/RGBA values.
1629 */
1630void
1631_mesa_map_ci8_to_rgba8(const GLcontext *ctx, GLuint n, const GLubyte index[],
1632                       GLubyte rgba[][4])
1633{
1634   GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1635   GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1636   GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1637   GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1638   const GLubyte *rMap = ctx->PixelMaps.ItoR.Map8;
1639   const GLubyte *gMap = ctx->PixelMaps.ItoG.Map8;
1640   const GLubyte *bMap = ctx->PixelMaps.ItoB.Map8;
1641   const GLubyte *aMap = ctx->PixelMaps.ItoA.Map8;
1642   GLuint i;
1643   for (i=0;i<n;i++) {
1644      rgba[i][RCOMP] = rMap[index[i] & rmask];
1645      rgba[i][GCOMP] = gMap[index[i] & gmask];
1646      rgba[i][BCOMP] = bMap[index[i] & bmask];
1647      rgba[i][ACOMP] = aMap[index[i] & amask];
1648   }
1649}
1650
1651
1652void
1653_mesa_scale_and_bias_depth(const GLcontext *ctx, GLuint n,
1654                           GLfloat depthValues[])
1655{
1656   const GLfloat scale = ctx->Pixel.DepthScale;
1657   const GLfloat bias = ctx->Pixel.DepthBias;
1658   GLuint i;
1659   for (i = 0; i < n; i++) {
1660      GLfloat d = depthValues[i] * scale + bias;
1661      depthValues[i] = CLAMP(d, 0.0F, 1.0F);
1662   }
1663}
1664
1665
1666void
1667_mesa_scale_and_bias_depth_uint(const GLcontext *ctx, GLuint n,
1668                                GLuint depthValues[])
1669{
1670   const GLdouble max = (double) 0xffffffff;
1671   const GLdouble scale = ctx->Pixel.DepthScale;
1672   const GLdouble bias = ctx->Pixel.DepthBias * max;
1673   GLuint i;
1674   for (i = 0; i < n; i++) {
1675      GLdouble d = (GLdouble) depthValues[i] * scale + bias;
1676      d = CLAMP(d, 0.0, max);
1677      depthValues[i] = (GLuint) d;
1678   }
1679}
1680
1681
1682
1683/*
1684 * Update the min/max values from an array of fragment colors.
1685 */
1686static void
1687update_minmax(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
1688{
1689   GLuint i;
1690   for (i = 0; i < n; i++) {
1691      /* update mins */
1692      if (rgba[i][RCOMP] < ctx->MinMax.Min[RCOMP])
1693         ctx->MinMax.Min[RCOMP] = rgba[i][RCOMP];
1694      if (rgba[i][GCOMP] < ctx->MinMax.Min[GCOMP])
1695         ctx->MinMax.Min[GCOMP] = rgba[i][GCOMP];
1696      if (rgba[i][BCOMP] < ctx->MinMax.Min[BCOMP])
1697         ctx->MinMax.Min[BCOMP] = rgba[i][BCOMP];
1698      if (rgba[i][ACOMP] < ctx->MinMax.Min[ACOMP])
1699         ctx->MinMax.Min[ACOMP] = rgba[i][ACOMP];
1700
1701      /* update maxs */
1702      if (rgba[i][RCOMP] > ctx->MinMax.Max[RCOMP])
1703         ctx->MinMax.Max[RCOMP] = rgba[i][RCOMP];
1704      if (rgba[i][GCOMP] > ctx->MinMax.Max[GCOMP])
1705         ctx->MinMax.Max[GCOMP] = rgba[i][GCOMP];
1706      if (rgba[i][BCOMP] > ctx->MinMax.Max[BCOMP])
1707         ctx->MinMax.Max[BCOMP] = rgba[i][BCOMP];
1708      if (rgba[i][ACOMP] > ctx->MinMax.Max[ACOMP])
1709         ctx->MinMax.Max[ACOMP] = rgba[i][ACOMP];
1710   }
1711}
1712
1713
1714/*
1715 * Update the histogram values from an array of fragment colors.
1716 */
1717static void
1718update_histogram(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
1719{
1720   const GLint max = ctx->Histogram.Width - 1;
1721   GLfloat w = (GLfloat) max;
1722   GLuint i;
1723
1724   if (ctx->Histogram.Width == 0)
1725      return;
1726
1727   for (i = 0; i < n; i++) {
1728      GLint ri = IROUND(rgba[i][RCOMP] * w);
1729      GLint gi = IROUND(rgba[i][GCOMP] * w);
1730      GLint bi = IROUND(rgba[i][BCOMP] * w);
1731      GLint ai = IROUND(rgba[i][ACOMP] * w);
1732      ri = CLAMP(ri, 0, max);
1733      gi = CLAMP(gi, 0, max);
1734      bi = CLAMP(bi, 0, max);
1735      ai = CLAMP(ai, 0, max);
1736      ctx->Histogram.Count[ri][RCOMP]++;
1737      ctx->Histogram.Count[gi][GCOMP]++;
1738      ctx->Histogram.Count[bi][BCOMP]++;
1739      ctx->Histogram.Count[ai][ACOMP]++;
1740   }
1741}
1742
1743
1744/**
1745 * Apply various pixel transfer operations to an array of RGBA pixels
1746 * as indicated by the transferOps bitmask
1747 */
1748void
1749_mesa_apply_rgba_transfer_ops(GLcontext *ctx, GLbitfield transferOps,
1750                              GLuint n, GLfloat rgba[][4])
1751{
1752   /* scale & bias */
1753   if (transferOps & IMAGE_SCALE_BIAS_BIT) {
1754      _mesa_scale_and_bias_rgba(n, rgba,
1755                                ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
1756                                ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
1757                                ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
1758                                ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
1759   }
1760   /* color map lookup */
1761   if (transferOps & IMAGE_MAP_COLOR_BIT) {
1762      _mesa_map_rgba( ctx, n, rgba );
1763   }
1764   /* GL_COLOR_TABLE lookup */
1765   if (transferOps & IMAGE_COLOR_TABLE_BIT) {
1766      _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_PRECONVOLUTION], n, rgba);
1767   }
1768   /* convolution */
1769   if (transferOps & IMAGE_CONVOLUTION_BIT) {
1770      /* this has to be done in the calling code */
1771      _mesa_problem(ctx, "IMAGE_CONVOLUTION_BIT set in _mesa_apply_transfer_ops");
1772   }
1773   /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
1774   if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
1775      _mesa_scale_and_bias_rgba(n, rgba,
1776                                ctx->Pixel.PostConvolutionScale[RCOMP],
1777                                ctx->Pixel.PostConvolutionScale[GCOMP],
1778                                ctx->Pixel.PostConvolutionScale[BCOMP],
1779                                ctx->Pixel.PostConvolutionScale[ACOMP],
1780                                ctx->Pixel.PostConvolutionBias[RCOMP],
1781                                ctx->Pixel.PostConvolutionBias[GCOMP],
1782                                ctx->Pixel.PostConvolutionBias[BCOMP],
1783                                ctx->Pixel.PostConvolutionBias[ACOMP]);
1784   }
1785   /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
1786   if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
1787      _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_POSTCONVOLUTION], n, rgba);
1788   }
1789   /* color matrix transform */
1790   if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
1791      _mesa_transform_rgba(ctx, n, rgba);
1792   }
1793   /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
1794   if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
1795      _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX], n, rgba);
1796   }
1797   /* update histogram count */
1798   if (transferOps & IMAGE_HISTOGRAM_BIT) {
1799      update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
1800   }
1801   /* update min/max values */
1802   if (transferOps & IMAGE_MIN_MAX_BIT) {
1803      update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
1804   }
1805   /* clamping to [0,1] */
1806   if (transferOps & IMAGE_CLAMP_BIT) {
1807      GLuint i;
1808      for (i = 0; i < n; i++) {
1809         rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1810         rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1811         rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1812         rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1813      }
1814   }
1815}
1816
1817
1818/*
1819 * Apply color index shift and offset to an array of pixels.
1820 */
1821static void
1822shift_and_offset_ci( const GLcontext *ctx, GLuint n, GLuint indexes[] )
1823{
1824   GLint shift = ctx->Pixel.IndexShift;
1825   GLint offset = ctx->Pixel.IndexOffset;
1826   GLuint i;
1827   if (shift > 0) {
1828      for (i=0;i<n;i++) {
1829         indexes[i] = (indexes[i] << shift) + offset;
1830      }
1831   }
1832   else if (shift < 0) {
1833      shift = -shift;
1834      for (i=0;i<n;i++) {
1835         indexes[i] = (indexes[i] >> shift) + offset;
1836      }
1837   }
1838   else {
1839      for (i=0;i<n;i++) {
1840         indexes[i] = indexes[i] + offset;
1841      }
1842   }
1843}
1844
1845
1846
1847/**
1848 * Apply color index shift, offset and table lookup to an array
1849 * of color indexes;
1850 */
1851void
1852_mesa_apply_ci_transfer_ops(const GLcontext *ctx, GLbitfield transferOps,
1853                            GLuint n, GLuint indexes[])
1854{
1855   if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
1856      shift_and_offset_ci(ctx, n, indexes);
1857   }
1858   if (transferOps & IMAGE_MAP_COLOR_BIT) {
1859      const GLuint mask = ctx->PixelMaps.ItoI.Size - 1;
1860      GLuint i;
1861      for (i = 0; i < n; i++) {
1862         const GLuint j = indexes[i] & mask;
1863         indexes[i] = IROUND(ctx->PixelMaps.ItoI.Map[j]);
1864      }
1865   }
1866}
1867
1868
1869/**
1870 * Apply stencil index shift, offset and table lookup to an array
1871 * of stencil values.
1872 */
1873void
1874_mesa_apply_stencil_transfer_ops(const GLcontext *ctx, GLuint n,
1875                                 GLstencil stencil[])
1876{
1877   if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
1878      const GLint offset = ctx->Pixel.IndexOffset;
1879      GLint shift = ctx->Pixel.IndexShift;
1880      GLuint i;
1881      if (shift > 0) {
1882         for (i = 0; i < n; i++) {
1883            stencil[i] = (stencil[i] << shift) + offset;
1884         }
1885      }
1886      else if (shift < 0) {
1887         shift = -shift;
1888         for (i = 0; i < n; i++) {
1889            stencil[i] = (stencil[i] >> shift) + offset;
1890         }
1891      }
1892      else {
1893         for (i = 0; i < n; i++) {
1894            stencil[i] = stencil[i] + offset;
1895         }
1896      }
1897   }
1898   if (ctx->Pixel.MapStencilFlag) {
1899      GLuint mask = ctx->PixelMaps.StoS.Size - 1;
1900      GLuint i;
1901      for (i = 0; i < n; i++) {
1902         stencil[i] = (GLstencil)ctx->PixelMaps.StoS.Map[ stencil[i] & mask ];
1903      }
1904   }
1905}
1906
1907
1908/**
1909 * Used to pack an array [][4] of RGBA float colors as specified
1910 * by the dstFormat, dstType and dstPacking.  Used by glReadPixels,
1911 * glGetConvolutionFilter(), etc.
1912 * Note: the rgba values will be modified by this function when any pixel
1913 * transfer ops are enabled.
1914 */
1915void
1916_mesa_pack_rgba_span_float(GLcontext *ctx, GLuint n, GLfloat rgba[][4],
1917                           GLenum dstFormat, GLenum dstType,
1918                           GLvoid *dstAddr,
1919                           const struct gl_pixelstore_attrib *dstPacking,
1920                           GLbitfield transferOps)
1921{
1922   GLfloat luminance[MAX_WIDTH];
1923   const GLint comps = _mesa_components_in_format(dstFormat);
1924   GLuint i;
1925
1926   /* XXX
1927    * This test should probably go away.  Have the caller set/clear the
1928    * IMAGE_CLAMP_BIT as needed.
1929    */
1930   if (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE) {
1931      /* need to clamp to [0, 1] */
1932      transferOps |= IMAGE_CLAMP_BIT;
1933   }
1934
1935   if (transferOps) {
1936      _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
1937      if ((transferOps & IMAGE_MIN_MAX_BIT) && ctx->MinMax.Sink) {
1938         return;
1939      }
1940   }
1941
1942   if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
1943      /* compute luminance values */
1944      if (transferOps & IMAGE_CLAMP_BIT) {
1945         for (i = 0; i < n; i++) {
1946            GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1947            luminance[i] = CLAMP(sum, 0.0F, 1.0F);
1948         }
1949      }
1950      else {
1951         for (i = 0; i < n; i++) {
1952            luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1953         }
1954      }
1955   }
1956
1957   /*
1958    * Pack/store the pixels.  Ugh!  Lots of cases!!!
1959    */
1960   switch (dstType) {
1961      case GL_UNSIGNED_BYTE:
1962         {
1963            GLubyte *dst = (GLubyte *) dstAddr;
1964            switch (dstFormat) {
1965               case GL_RED:
1966                  for (i=0;i<n;i++)
1967                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1968                  break;
1969               case GL_GREEN:
1970                  for (i=0;i<n;i++)
1971                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1972                  break;
1973               case GL_BLUE:
1974                  for (i=0;i<n;i++)
1975                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1976                  break;
1977               case GL_ALPHA:
1978                  for (i=0;i<n;i++)
1979                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1980                  break;
1981               case GL_LUMINANCE:
1982                  for (i=0;i<n;i++)
1983                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1984                  break;
1985               case GL_LUMINANCE_ALPHA:
1986                  for (i=0;i<n;i++) {
1987                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1988                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1989                  }
1990                  break;
1991               case GL_RGB:
1992                  for (i=0;i<n;i++) {
1993                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1994                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1995                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1996                  }
1997                  break;
1998               case GL_RGBA:
1999                  for (i=0;i<n;i++) {
2000                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2001                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2002                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2003                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2004                  }
2005                  break;
2006               case GL_BGR:
2007                  for (i=0;i<n;i++) {
2008                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2009                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2010                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2011                  }
2012                  break;
2013               case GL_BGRA:
2014                  for (i=0;i<n;i++) {
2015                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2016                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2017                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2018                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2019                  }
2020                  break;
2021               case GL_ABGR_EXT:
2022                  for (i=0;i<n;i++) {
2023                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2024                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2025                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2026                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2027                  }
2028                  break;
2029               case GL_DUDV_ATI:
2030               case GL_DU8DV8_ATI:
2031                  for (i=0;i<n;i++) {
2032                     dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2033                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2034                  }
2035                  break;
2036               default:
2037                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2038            }
2039         }
2040         break;
2041      case GL_BYTE:
2042         {
2043            GLbyte *dst = (GLbyte *) dstAddr;
2044            switch (dstFormat) {
2045               case GL_RED:
2046                  for (i=0;i<n;i++)
2047                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2048                  break;
2049               case GL_GREEN:
2050                  for (i=0;i<n;i++)
2051                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2052                  break;
2053               case GL_BLUE:
2054                  for (i=0;i<n;i++)
2055                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2056                  break;
2057               case GL_ALPHA:
2058                  for (i=0;i<n;i++)
2059                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2060                  break;
2061               case GL_LUMINANCE:
2062                  for (i=0;i<n;i++)
2063                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
2064                  break;
2065               case GL_LUMINANCE_ALPHA:
2066                  for (i=0;i<n;i++) {
2067                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
2068                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2069                  }
2070                  break;
2071               case GL_RGB:
2072                  for (i=0;i<n;i++) {
2073                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2074                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2075                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2076                  }
2077                  break;
2078               case GL_RGBA:
2079                  for (i=0;i<n;i++) {
2080                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2081                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2082                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2083                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2084                  }
2085                  break;
2086               case GL_BGR:
2087                  for (i=0;i<n;i++) {
2088                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2089                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2090                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2091                  }
2092                  break;
2093               case GL_BGRA:
2094                  for (i=0;i<n;i++) {
2095                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2096                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2097                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2098                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2099                  }
2100		  break;
2101               case GL_ABGR_EXT:
2102                  for (i=0;i<n;i++) {
2103                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2104                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2105                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2106                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2107                  }
2108                  break;
2109               case GL_DUDV_ATI:
2110               case GL_DU8DV8_ATI:
2111                  for (i=0;i<n;i++) {
2112                     dst[i*2+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2113                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2114                  }
2115                  break;
2116               default:
2117                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2118            }
2119         }
2120         break;
2121      case GL_UNSIGNED_SHORT:
2122         {
2123            GLushort *dst = (GLushort *) dstAddr;
2124            switch (dstFormat) {
2125               case GL_RED:
2126                  for (i=0;i<n;i++)
2127                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
2128                  break;
2129               case GL_GREEN:
2130                  for (i=0;i<n;i++)
2131                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
2132                  break;
2133               case GL_BLUE:
2134                  for (i=0;i<n;i++)
2135                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
2136                  break;
2137               case GL_ALPHA:
2138                  for (i=0;i<n;i++)
2139                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
2140                  break;
2141               case GL_LUMINANCE:
2142                  for (i=0;i<n;i++)
2143                     UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
2144                  break;
2145               case GL_LUMINANCE_ALPHA:
2146                  for (i=0;i<n;i++) {
2147                     UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
2148                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
2149                  }
2150                  break;
2151               case GL_RGB:
2152                  for (i=0;i<n;i++) {
2153                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
2154                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
2155                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
2156                  }
2157                  break;
2158               case GL_RGBA:
2159                  for (i=0;i<n;i++) {
2160                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
2161                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
2162                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
2163                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
2164                  }
2165                  break;
2166               case GL_BGR:
2167                  for (i=0;i<n;i++) {
2168                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
2169                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
2170                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
2171                  }
2172                  break;
2173               case GL_BGRA:
2174                  for (i=0;i<n;i++) {
2175                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
2176                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
2177                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
2178                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
2179                  }
2180                  break;
2181               case GL_ABGR_EXT:
2182                  for (i=0;i<n;i++) {
2183                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
2184                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
2185                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
2186                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
2187                  }
2188                  break;
2189               case GL_DUDV_ATI:
2190               case GL_DU8DV8_ATI:
2191                  for (i=0;i<n;i++) {
2192                     dst[i*2+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
2193                     dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
2194                  }
2195                  break;
2196               default:
2197                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2198            }
2199         }
2200         break;
2201      case GL_SHORT:
2202         {
2203            GLshort *dst = (GLshort *) dstAddr;
2204            switch (dstFormat) {
2205               case GL_RED:
2206                  for (i=0;i<n;i++)
2207                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2208                  break;
2209               case GL_GREEN:
2210                  for (i=0;i<n;i++)
2211                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2212                  break;
2213               case GL_BLUE:
2214                  for (i=0;i<n;i++)
2215                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2216                  break;
2217               case GL_ALPHA:
2218                  for (i=0;i<n;i++)
2219                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2220                  break;
2221               case GL_LUMINANCE:
2222                  for (i=0;i<n;i++)
2223                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
2224                  break;
2225               case GL_LUMINANCE_ALPHA:
2226                  for (i=0;i<n;i++) {
2227                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
2228                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2229                  }
2230                  break;
2231               case GL_RGB:
2232                  for (i=0;i<n;i++) {
2233                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2234                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2235                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2236                  }
2237                  break;
2238               case GL_RGBA:
2239                  for (i=0;i<n;i++) {
2240                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2241                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2242                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2243                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2244                  }
2245                  break;
2246               case GL_BGR:
2247                  for (i=0;i<n;i++) {
2248                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2249                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2250                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2251                  }
2252                  break;
2253               case GL_BGRA:
2254                  for (i=0;i<n;i++) {
2255                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2256                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2257                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2258                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2259                  }
2260		  break;
2261               case GL_ABGR_EXT:
2262                  for (i=0;i<n;i++) {
2263                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2264                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2265                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2266                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2267                  }
2268                  break;
2269               case GL_DUDV_ATI:
2270               case GL_DU8DV8_ATI:
2271                  for (i=0;i<n;i++) {
2272                     dst[i*2+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2273                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2274                  }
2275                  break;
2276               default:
2277                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2278            }
2279         }
2280         break;
2281      case GL_UNSIGNED_INT:
2282         {
2283            GLuint *dst = (GLuint *) dstAddr;
2284            switch (dstFormat) {
2285               case GL_RED:
2286                  for (i=0;i<n;i++)
2287                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2288                  break;
2289               case GL_GREEN:
2290                  for (i=0;i<n;i++)
2291                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2292                  break;
2293               case GL_BLUE:
2294                  for (i=0;i<n;i++)
2295                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2296                  break;
2297               case GL_ALPHA:
2298                  for (i=0;i<n;i++)
2299                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2300                  break;
2301               case GL_LUMINANCE:
2302                  for (i=0;i<n;i++)
2303                     dst[i] = FLOAT_TO_UINT(luminance[i]);
2304                  break;
2305               case GL_LUMINANCE_ALPHA:
2306                  for (i=0;i<n;i++) {
2307                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2308                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2309                  }
2310                  break;
2311               case GL_RGB:
2312                  for (i=0;i<n;i++) {
2313                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2314                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2315                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2316                  }
2317                  break;
2318               case GL_RGBA:
2319                  for (i=0;i<n;i++) {
2320                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2321                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2322                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2323                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2324                  }
2325                  break;
2326               case GL_BGR:
2327                  for (i=0;i<n;i++) {
2328                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2329                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2330                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2331                  }
2332                  break;
2333               case GL_BGRA:
2334                  for (i=0;i<n;i++) {
2335                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2336                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2337                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2338                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2339                  }
2340                  break;
2341               case GL_ABGR_EXT:
2342                  for (i=0;i<n;i++) {
2343                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2344                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2345                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2346                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2347                  }
2348                  break;
2349               case GL_DUDV_ATI:
2350               case GL_DU8DV8_ATI:
2351                  for (i=0;i<n;i++) {
2352                     dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2353                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2354                  }
2355                  break;
2356               default:
2357                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2358            }
2359         }
2360         break;
2361      case GL_INT:
2362         {
2363            GLint *dst = (GLint *) dstAddr;
2364            switch (dstFormat) {
2365               case GL_RED:
2366                  for (i=0;i<n;i++)
2367                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
2368                  break;
2369               case GL_GREEN:
2370                  for (i=0;i<n;i++)
2371                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
2372                  break;
2373               case GL_BLUE:
2374                  for (i=0;i<n;i++)
2375                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
2376                  break;
2377               case GL_ALPHA:
2378                  for (i=0;i<n;i++)
2379                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
2380                  break;
2381               case GL_LUMINANCE:
2382                  for (i=0;i<n;i++)
2383                     dst[i] = FLOAT_TO_INT(luminance[i]);
2384                  break;
2385               case GL_LUMINANCE_ALPHA:
2386                  for (i=0;i<n;i++) {
2387                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2388                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
2389                  }
2390                  break;
2391               case GL_RGB:
2392                  for (i=0;i<n;i++) {
2393                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2394                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2395                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2396                  }
2397                  break;
2398               case GL_RGBA:
2399                  for (i=0;i<n;i++) {
2400                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2401                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2402                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2403                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2404                  }
2405                  break;
2406               case GL_BGR:
2407                  for (i=0;i<n;i++) {
2408                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2409                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2410                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2411                  }
2412                  break;
2413               case GL_BGRA:
2414                  for (i=0;i<n;i++) {
2415                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2416                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2417                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2418                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2419                  }
2420                  break;
2421               case GL_ABGR_EXT:
2422                  for (i=0;i<n;i++) {
2423                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
2424                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
2425                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
2426                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
2427                  }
2428                  break;
2429               case GL_DUDV_ATI:
2430               case GL_DU8DV8_ATI:
2431                  for (i=0;i<n;i++) {
2432                     dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2433                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2434                  }
2435                  break;
2436               default:
2437                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2438            }
2439         }
2440         break;
2441      case GL_FLOAT:
2442         {
2443            GLfloat *dst = (GLfloat *) dstAddr;
2444            switch (dstFormat) {
2445               case GL_RED:
2446                  for (i=0;i<n;i++)
2447                     dst[i] = rgba[i][RCOMP];
2448                  break;
2449               case GL_GREEN:
2450                  for (i=0;i<n;i++)
2451                     dst[i] = rgba[i][GCOMP];
2452                  break;
2453               case GL_BLUE:
2454                  for (i=0;i<n;i++)
2455                     dst[i] = rgba[i][BCOMP];
2456                  break;
2457               case GL_ALPHA:
2458                  for (i=0;i<n;i++)
2459                     dst[i] = rgba[i][ACOMP];
2460                  break;
2461               case GL_LUMINANCE:
2462                  for (i=0;i<n;i++)
2463                     dst[i] = luminance[i];
2464                  break;
2465               case GL_LUMINANCE_ALPHA:
2466                  for (i=0;i<n;i++) {
2467                     dst[i*2+0] = luminance[i];
2468                     dst[i*2+1] = rgba[i][ACOMP];
2469                  }
2470                  break;
2471               case GL_RGB:
2472                  for (i=0;i<n;i++) {
2473                     dst[i*3+0] = rgba[i][RCOMP];
2474                     dst[i*3+1] = rgba[i][GCOMP];
2475                     dst[i*3+2] = rgba[i][BCOMP];
2476                  }
2477                  break;
2478               case GL_RGBA:
2479                  for (i=0;i<n;i++) {
2480                     dst[i*4+0] = rgba[i][RCOMP];
2481                     dst[i*4+1] = rgba[i][GCOMP];
2482                     dst[i*4+2] = rgba[i][BCOMP];
2483                     dst[i*4+3] = rgba[i][ACOMP];
2484                  }
2485                  break;
2486               case GL_BGR:
2487                  for (i=0;i<n;i++) {
2488                     dst[i*3+0] = rgba[i][BCOMP];
2489                     dst[i*3+1] = rgba[i][GCOMP];
2490                     dst[i*3+2] = rgba[i][RCOMP];
2491                  }
2492                  break;
2493               case GL_BGRA:
2494                  for (i=0;i<n;i++) {
2495                     dst[i*4+0] = rgba[i][BCOMP];
2496                     dst[i*4+1] = rgba[i][GCOMP];
2497                     dst[i*4+2] = rgba[i][RCOMP];
2498                     dst[i*4+3] = rgba[i][ACOMP];
2499                  }
2500                  break;
2501               case GL_ABGR_EXT:
2502                  for (i=0;i<n;i++) {
2503                     dst[i*4+0] = rgba[i][ACOMP];
2504                     dst[i*4+1] = rgba[i][BCOMP];
2505                     dst[i*4+2] = rgba[i][GCOMP];
2506                     dst[i*4+3] = rgba[i][RCOMP];
2507                  }
2508                  break;
2509               case GL_DUDV_ATI:
2510               case GL_DU8DV8_ATI:
2511                  for (i=0;i<n;i++) {
2512                     dst[i*2+0] = rgba[i][RCOMP];
2513                     dst[i*2+1] = rgba[i][GCOMP];
2514                  }
2515                  break;
2516               default:
2517                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2518            }
2519         }
2520         break;
2521      case GL_HALF_FLOAT_ARB:
2522         {
2523            GLhalfARB *dst = (GLhalfARB *) dstAddr;
2524            switch (dstFormat) {
2525               case GL_RED:
2526                  for (i=0;i<n;i++)
2527                     dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
2528                  break;
2529               case GL_GREEN:
2530                  for (i=0;i<n;i++)
2531                     dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
2532                  break;
2533               case GL_BLUE:
2534                  for (i=0;i<n;i++)
2535                     dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
2536                  break;
2537               case GL_ALPHA:
2538                  for (i=0;i<n;i++)
2539                     dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
2540                  break;
2541               case GL_LUMINANCE:
2542                  for (i=0;i<n;i++)
2543                     dst[i] = _mesa_float_to_half(luminance[i]);
2544                  break;
2545               case GL_LUMINANCE_ALPHA:
2546                  for (i=0;i<n;i++) {
2547                     dst[i*2+0] = _mesa_float_to_half(luminance[i]);
2548                     dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
2549                  }
2550                  break;
2551               case GL_RGB:
2552                  for (i=0;i<n;i++) {
2553                     dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2554                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2555                     dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2556                  }
2557                  break;
2558               case GL_RGBA:
2559                  for (i=0;i<n;i++) {
2560                     dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2561                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2562                     dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2563                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2564                  }
2565                  break;
2566               case GL_BGR:
2567                  for (i=0;i<n;i++) {
2568                     dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2569                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2570                     dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2571                  }
2572                  break;
2573               case GL_BGRA:
2574                  for (i=0;i<n;i++) {
2575                     dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2576                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2577                     dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2578                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2579                  }
2580                  break;
2581               case GL_ABGR_EXT:
2582                  for (i=0;i<n;i++) {
2583                     dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
2584                     dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
2585                     dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
2586                     dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
2587                  }
2588                  break;
2589               case GL_DUDV_ATI:
2590               case GL_DU8DV8_ATI:
2591                  for (i=0;i<n;i++) {
2592                     dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2593                     dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2594                  }
2595                  break;
2596               default:
2597                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2598            }
2599         }
2600         break;
2601      case GL_UNSIGNED_BYTE_3_3_2:
2602         if (dstFormat == GL_RGB) {
2603            GLubyte *dst = (GLubyte *) dstAddr;
2604            for (i=0;i<n;i++) {
2605               dst[i] = (IROUND(rgba[i][RCOMP] * 7.0F) << 5)
2606                      | (IROUND(rgba[i][GCOMP] * 7.0F) << 2)
2607                      | (IROUND(rgba[i][BCOMP] * 3.0F)     );
2608            }
2609         }
2610         break;
2611      case GL_UNSIGNED_BYTE_2_3_3_REV:
2612         if (dstFormat == GL_RGB) {
2613            GLubyte *dst = (GLubyte *) dstAddr;
2614            for (i=0;i<n;i++) {
2615               dst[i] = (IROUND(rgba[i][RCOMP] * 7.0F)     )
2616                      | (IROUND(rgba[i][GCOMP] * 7.0F) << 3)
2617                      | (IROUND(rgba[i][BCOMP] * 3.0F) << 6);
2618            }
2619         }
2620         break;
2621      case GL_UNSIGNED_SHORT_5_6_5:
2622         if (dstFormat == GL_RGB) {
2623            GLushort *dst = (GLushort *) dstAddr;
2624            for (i=0;i<n;i++) {
2625               dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) << 11)
2626                      | (IROUND(rgba[i][GCOMP] * 63.0F) <<  5)
2627                      | (IROUND(rgba[i][BCOMP] * 31.0F)      );
2628            }
2629         }
2630         break;
2631      case GL_UNSIGNED_SHORT_5_6_5_REV:
2632         if (dstFormat == GL_RGB) {
2633            GLushort *dst = (GLushort *) dstAddr;
2634            for (i=0;i<n;i++) {
2635               dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F)      )
2636                      | (IROUND(rgba[i][GCOMP] * 63.0F) <<  5)
2637                      | (IROUND(rgba[i][BCOMP] * 31.0F) << 11);
2638            }
2639         }
2640         break;
2641      case GL_UNSIGNED_SHORT_4_4_4_4:
2642         if (dstFormat == GL_RGBA) {
2643            GLushort *dst = (GLushort *) dstAddr;
2644            for (i=0;i<n;i++) {
2645               dst[i] = (IROUND(rgba[i][RCOMP] * 15.0F) << 12)
2646                      | (IROUND(rgba[i][GCOMP] * 15.0F) <<  8)
2647                      | (IROUND(rgba[i][BCOMP] * 15.0F) <<  4)
2648                      | (IROUND(rgba[i][ACOMP] * 15.0F)      );
2649            }
2650         }
2651         else if (dstFormat == GL_BGRA) {
2652            GLushort *dst = (GLushort *) dstAddr;
2653            for (i=0;i<n;i++) {
2654               dst[i] = (IROUND(rgba[i][BCOMP] * 15.0F) << 12)
2655                      | (IROUND(rgba[i][GCOMP] * 15.0F) <<  8)
2656                      | (IROUND(rgba[i][RCOMP] * 15.0F) <<  4)
2657                      | (IROUND(rgba[i][ACOMP] * 15.0F)      );
2658            }
2659         }
2660         else if (dstFormat == GL_ABGR_EXT) {
2661            GLushort *dst = (GLushort *) dstAddr;
2662            for (i=0;i<n;i++) {
2663               dst[i] = (IROUND(rgba[i][ACOMP] * 15.0F) << 12)
2664                      | (IROUND(rgba[i][BCOMP] * 15.0F) <<  8)
2665                      | (IROUND(rgba[i][GCOMP] * 15.0F) <<  4)
2666                      | (IROUND(rgba[i][RCOMP] * 15.0F)      );
2667            }
2668         }
2669         break;
2670      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2671         if (dstFormat == GL_RGBA) {
2672            GLushort *dst = (GLushort *) dstAddr;
2673            for (i=0;i<n;i++) {
2674               dst[i] = (IROUND(rgba[i][RCOMP] * 15.0F)      )
2675                      | (IROUND(rgba[i][GCOMP] * 15.0F) <<  4)
2676                      | (IROUND(rgba[i][BCOMP] * 15.0F) <<  8)
2677                      | (IROUND(rgba[i][ACOMP] * 15.0F) << 12);
2678            }
2679         }
2680         else if (dstFormat == GL_BGRA) {
2681            GLushort *dst = (GLushort *) dstAddr;
2682            for (i=0;i<n;i++) {
2683               dst[i] = (IROUND(rgba[i][BCOMP] * 15.0F)      )
2684                      | (IROUND(rgba[i][GCOMP] * 15.0F) <<  4)
2685                      | (IROUND(rgba[i][RCOMP] * 15.0F) <<  8)
2686                      | (IROUND(rgba[i][ACOMP] * 15.0F) << 12);
2687            }
2688         }
2689         else if (dstFormat == GL_ABGR_EXT) {
2690            GLushort *dst = (GLushort *) dstAddr;
2691            for (i=0;i<n;i++) {
2692               dst[i] = (IROUND(rgba[i][ACOMP] * 15.0F)      )
2693                      | (IROUND(rgba[i][BCOMP] * 15.0F) <<  4)
2694                      | (IROUND(rgba[i][GCOMP] * 15.0F) <<  8)
2695                      | (IROUND(rgba[i][RCOMP] * 15.0F) << 12);
2696            }
2697         }
2698         break;
2699      case GL_UNSIGNED_SHORT_5_5_5_1:
2700         if (dstFormat == GL_RGBA) {
2701            GLushort *dst = (GLushort *) dstAddr;
2702            for (i=0;i<n;i++) {
2703               dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) << 11)
2704                      | (IROUND(rgba[i][GCOMP] * 31.0F) <<  6)
2705                      | (IROUND(rgba[i][BCOMP] * 31.0F) <<  1)
2706                      | (IROUND(rgba[i][ACOMP] *  1.0F)      );
2707            }
2708         }
2709         else if (dstFormat == GL_BGRA) {
2710            GLushort *dst = (GLushort *) dstAddr;
2711            for (i=0;i<n;i++) {
2712               dst[i] = (IROUND(rgba[i][BCOMP] * 31.0F) << 11)
2713                      | (IROUND(rgba[i][GCOMP] * 31.0F) <<  6)
2714                      | (IROUND(rgba[i][RCOMP] * 31.0F) <<  1)
2715                      | (IROUND(rgba[i][ACOMP] *  1.0F)      );
2716            }
2717         }
2718         else if (dstFormat == GL_ABGR_EXT) {
2719            GLushort *dst = (GLushort *) dstAddr;
2720            for (i=0;i<n;i++) {
2721               dst[i] = (IROUND(rgba[i][ACOMP] * 31.0F) << 11)
2722                      | (IROUND(rgba[i][BCOMP] * 31.0F) <<  6)
2723                      | (IROUND(rgba[i][GCOMP] * 31.0F) <<  1)
2724                      | (IROUND(rgba[i][RCOMP] *  1.0F)      );
2725            }
2726         }
2727         break;
2728      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2729         if (dstFormat == GL_RGBA) {
2730            GLushort *dst = (GLushort *) dstAddr;
2731            for (i=0;i<n;i++) {
2732               dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F)      )
2733                      | (IROUND(rgba[i][GCOMP] * 31.0F) <<  5)
2734                      | (IROUND(rgba[i][BCOMP] * 31.0F) << 10)
2735                      | (IROUND(rgba[i][ACOMP] *  1.0F) << 15);
2736            }
2737         }
2738         else if (dstFormat == GL_BGRA) {
2739            GLushort *dst = (GLushort *) dstAddr;
2740            for (i=0;i<n;i++) {
2741               dst[i] = (IROUND(rgba[i][BCOMP] * 31.0F)      )
2742                      | (IROUND(rgba[i][GCOMP] * 31.0F) <<  5)
2743                      | (IROUND(rgba[i][RCOMP] * 31.0F) << 10)
2744                      | (IROUND(rgba[i][ACOMP] *  1.0F) << 15);
2745            }
2746         }
2747         else if (dstFormat == GL_ABGR_EXT) {
2748            GLushort *dst = (GLushort *) dstAddr;
2749            for (i=0;i<n;i++) {
2750               dst[i] = (IROUND(rgba[i][ACOMP] * 31.0F)      )
2751                      | (IROUND(rgba[i][BCOMP] * 31.0F) <<  5)
2752                      | (IROUND(rgba[i][GCOMP] * 31.0F) << 10)
2753                      | (IROUND(rgba[i][RCOMP] *  1.0F) << 15);
2754            }
2755         }
2756         break;
2757      case GL_UNSIGNED_INT_8_8_8_8:
2758         if (dstFormat == GL_RGBA) {
2759            GLuint *dst = (GLuint *) dstAddr;
2760            for (i=0;i<n;i++) {
2761               dst[i] = (IROUND(rgba[i][RCOMP] * 255.F) << 24)
2762                      | (IROUND(rgba[i][GCOMP] * 255.F) << 16)
2763                      | (IROUND(rgba[i][BCOMP] * 255.F) <<  8)
2764                      | (IROUND(rgba[i][ACOMP] * 255.F)      );
2765            }
2766         }
2767         else if (dstFormat == GL_BGRA) {
2768            GLuint *dst = (GLuint *) dstAddr;
2769            for (i=0;i<n;i++) {
2770               dst[i] = (IROUND(rgba[i][BCOMP] * 255.F) << 24)
2771                      | (IROUND(rgba[i][GCOMP] * 255.F) << 16)
2772                      | (IROUND(rgba[i][RCOMP] * 255.F) <<  8)
2773                      | (IROUND(rgba[i][ACOMP] * 255.F)      );
2774            }
2775         }
2776         else if (dstFormat == GL_ABGR_EXT) {
2777            GLuint *dst = (GLuint *) dstAddr;
2778            for (i=0;i<n;i++) {
2779               dst[i] = (IROUND(rgba[i][ACOMP] * 255.F) << 24)
2780                      | (IROUND(rgba[i][BCOMP] * 255.F) << 16)
2781                      | (IROUND(rgba[i][GCOMP] * 255.F) <<  8)
2782                      | (IROUND(rgba[i][RCOMP] * 255.F)      );
2783            }
2784         }
2785         break;
2786      case GL_UNSIGNED_INT_8_8_8_8_REV:
2787         if (dstFormat == GL_RGBA) {
2788            GLuint *dst = (GLuint *) dstAddr;
2789            for (i=0;i<n;i++) {
2790               dst[i] = (IROUND(rgba[i][RCOMP] * 255.0F)      )
2791                      | (IROUND(rgba[i][GCOMP] * 255.0F) <<  8)
2792                      | (IROUND(rgba[i][BCOMP] * 255.0F) << 16)
2793                      | (IROUND(rgba[i][ACOMP] * 255.0F) << 24);
2794            }
2795         }
2796         else if (dstFormat == GL_BGRA) {
2797            GLuint *dst = (GLuint *) dstAddr;
2798            for (i=0;i<n;i++) {
2799               dst[i] = (IROUND(rgba[i][BCOMP] * 255.0F)      )
2800                      | (IROUND(rgba[i][GCOMP] * 255.0F) <<  8)
2801                      | (IROUND(rgba[i][RCOMP] * 255.0F) << 16)
2802                      | (IROUND(rgba[i][ACOMP] * 255.0F) << 24);
2803            }
2804         }
2805         else if (dstFormat == GL_ABGR_EXT) {
2806            GLuint *dst = (GLuint *) dstAddr;
2807            for (i=0;i<n;i++) {
2808               dst[i] = (IROUND(rgba[i][ACOMP] * 255.0F)      )
2809                      | (IROUND(rgba[i][BCOMP] * 255.0F) <<  8)
2810                      | (IROUND(rgba[i][GCOMP] * 255.0F) << 16)
2811                      | (IROUND(rgba[i][RCOMP] * 255.0F) << 24);
2812            }
2813         }
2814         break;
2815      case GL_UNSIGNED_INT_10_10_10_2:
2816         if (dstFormat == GL_RGBA) {
2817            GLuint *dst = (GLuint *) dstAddr;
2818            for (i=0;i<n;i++) {
2819               dst[i] = (IROUND(rgba[i][RCOMP] * 1023.0F) << 22)
2820                      | (IROUND(rgba[i][GCOMP] * 1023.0F) << 12)
2821                      | (IROUND(rgba[i][BCOMP] * 1023.0F) <<  2)
2822                      | (IROUND(rgba[i][ACOMP] *    3.0F)      );
2823            }
2824         }
2825         else if (dstFormat == GL_BGRA) {
2826            GLuint *dst = (GLuint *) dstAddr;
2827            for (i=0;i<n;i++) {
2828               dst[i] = (IROUND(rgba[i][BCOMP] * 1023.0F) << 22)
2829                      | (IROUND(rgba[i][GCOMP] * 1023.0F) << 12)
2830                      | (IROUND(rgba[i][RCOMP] * 1023.0F) <<  2)
2831                      | (IROUND(rgba[i][ACOMP] *    3.0F)      );
2832            }
2833         }
2834         else if (dstFormat == GL_ABGR_EXT) {
2835            GLuint *dst = (GLuint *) dstAddr;
2836            for (i=0;i<n;i++) {
2837               dst[i] = (IROUND(rgba[i][ACOMP] * 1023.0F) << 22)
2838                      | (IROUND(rgba[i][BCOMP] * 1023.0F) << 12)
2839                      | (IROUND(rgba[i][GCOMP] * 1023.0F) <<  2)
2840                      | (IROUND(rgba[i][RCOMP] *    3.0F)      );
2841            }
2842         }
2843         break;
2844      case GL_UNSIGNED_INT_2_10_10_10_REV:
2845         if (dstFormat == GL_RGBA) {
2846            GLuint *dst = (GLuint *) dstAddr;
2847            for (i=0;i<n;i++) {
2848               dst[i] = (IROUND(rgba[i][RCOMP] * 1023.0F)      )
2849                      | (IROUND(rgba[i][GCOMP] * 1023.0F) << 10)
2850                      | (IROUND(rgba[i][BCOMP] * 1023.0F) << 20)
2851                      | (IROUND(rgba[i][ACOMP] *    3.0F) << 30);
2852            }
2853         }
2854         else if (dstFormat == GL_BGRA) {
2855            GLuint *dst = (GLuint *) dstAddr;
2856            for (i=0;i<n;i++) {
2857               dst[i] = (IROUND(rgba[i][BCOMP] * 1023.0F)      )
2858                      | (IROUND(rgba[i][GCOMP] * 1023.0F) << 10)
2859                      | (IROUND(rgba[i][RCOMP] * 1023.0F) << 20)
2860                      | (IROUND(rgba[i][ACOMP] *    3.0F) << 30);
2861            }
2862         }
2863         else if (dstFormat == GL_ABGR_EXT) {
2864            GLuint *dst = (GLuint *) dstAddr;
2865            for (i=0;i<n;i++) {
2866               dst[i] = (IROUND(rgba[i][ACOMP] * 1023.0F)      )
2867                      | (IROUND(rgba[i][BCOMP] * 1023.0F) << 10)
2868                      | (IROUND(rgba[i][GCOMP] * 1023.0F) << 20)
2869                      | (IROUND(rgba[i][RCOMP] *    3.0F) << 30);
2870            }
2871         }
2872         break;
2873      default:
2874         _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2875         return;
2876   }
2877
2878   if (dstPacking->SwapBytes) {
2879      GLint swapSize = _mesa_sizeof_packed_type(dstType);
2880      if (swapSize == 2) {
2881         if (dstPacking->SwapBytes) {
2882            _mesa_swap2((GLushort *) dstAddr, n * comps);
2883         }
2884      }
2885      else if (swapSize == 4) {
2886         if (dstPacking->SwapBytes) {
2887            _mesa_swap4((GLuint *) dstAddr, n * comps);
2888         }
2889      }
2890   }
2891}
2892
2893
2894#define SWAP2BYTE(VALUE)			\
2895   {						\
2896      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2897      GLubyte tmp = bytes[0];			\
2898      bytes[0] = bytes[1];			\
2899      bytes[1] = tmp;				\
2900   }
2901
2902#define SWAP4BYTE(VALUE)			\
2903   {						\
2904      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2905      GLubyte tmp = bytes[0];			\
2906      bytes[0] = bytes[3];			\
2907      bytes[3] = tmp;				\
2908      tmp = bytes[1];				\
2909      bytes[1] = bytes[2];			\
2910      bytes[2] = tmp;				\
2911   }
2912
2913
2914static void
2915extract_uint_indexes(GLuint n, GLuint indexes[],
2916                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
2917                     const struct gl_pixelstore_attrib *unpack )
2918{
2919   ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2920
2921   ASSERT(srcType == GL_BITMAP ||
2922          srcType == GL_UNSIGNED_BYTE ||
2923          srcType == GL_BYTE ||
2924          srcType == GL_UNSIGNED_SHORT ||
2925          srcType == GL_SHORT ||
2926          srcType == GL_UNSIGNED_INT ||
2927          srcType == GL_INT ||
2928          srcType == GL_UNSIGNED_INT_24_8_EXT ||
2929          srcType == GL_HALF_FLOAT_ARB ||
2930          srcType == GL_FLOAT);
2931
2932   switch (srcType) {
2933      case GL_BITMAP:
2934         {
2935            GLubyte *ubsrc = (GLubyte *) src;
2936            if (unpack->LsbFirst) {
2937               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2938               GLuint i;
2939               for (i = 0; i < n; i++) {
2940                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2941                  if (mask == 128) {
2942                     mask = 1;
2943                     ubsrc++;
2944                  }
2945                  else {
2946                     mask = mask << 1;
2947                  }
2948               }
2949            }
2950            else {
2951               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2952               GLuint i;
2953               for (i = 0; i < n; i++) {
2954                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2955                  if (mask == 1) {
2956                     mask = 128;
2957                     ubsrc++;
2958                  }
2959                  else {
2960                     mask = mask >> 1;
2961                  }
2962               }
2963            }
2964         }
2965         break;
2966      case GL_UNSIGNED_BYTE:
2967         {
2968            GLuint i;
2969            const GLubyte *s = (const GLubyte *) src;
2970            for (i = 0; i < n; i++)
2971               indexes[i] = s[i];
2972         }
2973         break;
2974      case GL_BYTE:
2975         {
2976            GLuint i;
2977            const GLbyte *s = (const GLbyte *) src;
2978            for (i = 0; i < n; i++)
2979               indexes[i] = s[i];
2980         }
2981         break;
2982      case GL_UNSIGNED_SHORT:
2983         {
2984            GLuint i;
2985            const GLushort *s = (const GLushort *) src;
2986            if (unpack->SwapBytes) {
2987               for (i = 0; i < n; i++) {
2988                  GLushort value = s[i];
2989                  SWAP2BYTE(value);
2990                  indexes[i] = value;
2991               }
2992            }
2993            else {
2994               for (i = 0; i < n; i++)
2995                  indexes[i] = s[i];
2996            }
2997         }
2998         break;
2999      case GL_SHORT:
3000         {
3001            GLuint i;
3002            const GLshort *s = (const GLshort *) src;
3003            if (unpack->SwapBytes) {
3004               for (i = 0; i < n; i++) {
3005                  GLshort value = s[i];
3006                  SWAP2BYTE(value);
3007                  indexes[i] = value;
3008               }
3009            }
3010            else {
3011               for (i = 0; i < n; i++)
3012                  indexes[i] = s[i];
3013            }
3014         }
3015         break;
3016      case GL_UNSIGNED_INT:
3017         {
3018            GLuint i;
3019            const GLuint *s = (const GLuint *) src;
3020            if (unpack->SwapBytes) {
3021               for (i = 0; i < n; i++) {
3022                  GLuint value = s[i];
3023                  SWAP4BYTE(value);
3024                  indexes[i] = value;
3025               }
3026            }
3027            else {
3028               for (i = 0; i < n; i++)
3029                  indexes[i] = s[i];
3030            }
3031         }
3032         break;
3033      case GL_INT:
3034         {
3035            GLuint i;
3036            const GLint *s = (const GLint *) src;
3037            if (unpack->SwapBytes) {
3038               for (i = 0; i < n; i++) {
3039                  GLint value = s[i];
3040                  SWAP4BYTE(value);
3041                  indexes[i] = value;
3042               }
3043            }
3044            else {
3045               for (i = 0; i < n; i++)
3046                  indexes[i] = s[i];
3047            }
3048         }
3049         break;
3050      case GL_FLOAT:
3051         {
3052            GLuint i;
3053            const GLfloat *s = (const GLfloat *) src;
3054            if (unpack->SwapBytes) {
3055               for (i = 0; i < n; i++) {
3056                  GLfloat value = s[i];
3057                  SWAP4BYTE(value);
3058                  indexes[i] = (GLuint) value;
3059               }
3060            }
3061            else {
3062               for (i = 0; i < n; i++)
3063                  indexes[i] = (GLuint) s[i];
3064            }
3065         }
3066         break;
3067      case GL_HALF_FLOAT_ARB:
3068         {
3069            GLuint i;
3070            const GLhalfARB *s = (const GLhalfARB *) src;
3071            if (unpack->SwapBytes) {
3072               for (i = 0; i < n; i++) {
3073                  GLhalfARB value = s[i];
3074                  SWAP2BYTE(value);
3075                  indexes[i] = (GLuint) _mesa_half_to_float(value);
3076               }
3077            }
3078            else {
3079               for (i = 0; i < n; i++)
3080                  indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
3081            }
3082         }
3083         break;
3084      case GL_UNSIGNED_INT_24_8_EXT:
3085         {
3086            GLuint i;
3087            const GLuint *s = (const GLuint *) src;
3088            if (unpack->SwapBytes) {
3089               for (i = 0; i < n; i++) {
3090                  GLuint value = s[i];
3091                  SWAP4BYTE(value);
3092                  indexes[i] = value & 0xff;  /* lower 8 bits */
3093               }
3094            }
3095            else {
3096               for (i = 0; i < n; i++)
3097                  indexes[i] = s[i] & 0xff;  /* lower 8 bits */
3098            }
3099         }
3100         break;
3101
3102      default:
3103         _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
3104         return;
3105   }
3106}
3107
3108
3109/*
3110 * This function extracts floating point RGBA values from arbitrary
3111 * image data.  srcFormat and srcType are the format and type parameters
3112 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
3113 *
3114 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
3115 * implements the "Conversion to floating point", "Conversion to RGB",
3116 * and "Final Expansion to RGBA" operations.
3117 *
3118 * Args:  n - number of pixels
3119 *        rgba - output colors
3120 *        srcFormat - format of incoming data
3121 *        srcType - data type of incoming data
3122 *        src - source data pointer
3123 *        swapBytes - perform byteswapping of incoming data?
3124 */
3125static void
3126extract_float_rgba(GLuint n, GLfloat rgba[][4],
3127                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
3128                   GLboolean swapBytes)
3129{
3130   GLint redIndex, greenIndex, blueIndex, alphaIndex;
3131   GLint stride;
3132   GLint rComp, bComp, gComp, aComp;
3133
3134   ASSERT(srcFormat == GL_RED ||
3135          srcFormat == GL_GREEN ||
3136          srcFormat == GL_BLUE ||
3137          srcFormat == GL_ALPHA ||
3138          srcFormat == GL_LUMINANCE ||
3139          srcFormat == GL_LUMINANCE_ALPHA ||
3140          srcFormat == GL_INTENSITY ||
3141          srcFormat == GL_RGB ||
3142          srcFormat == GL_BGR ||
3143          srcFormat == GL_RGBA ||
3144          srcFormat == GL_BGRA ||
3145          srcFormat == GL_ABGR_EXT ||
3146          srcFormat == GL_DUDV_ATI);
3147
3148   ASSERT(srcType == GL_UNSIGNED_BYTE ||
3149          srcType == GL_BYTE ||
3150          srcType == GL_UNSIGNED_SHORT ||
3151          srcType == GL_SHORT ||
3152          srcType == GL_UNSIGNED_INT ||
3153          srcType == GL_INT ||
3154          srcType == GL_HALF_FLOAT_ARB ||
3155          srcType == GL_FLOAT ||
3156          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3157          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3158          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3159          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3160          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3161          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3162          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3163          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3164          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3165          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3166          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3167          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3168
3169   rComp = gComp = bComp = aComp = -1;
3170
3171   switch (srcFormat) {
3172      case GL_RED:
3173         redIndex = 0;
3174         greenIndex = blueIndex = alphaIndex = -1;
3175         stride = 1;
3176         break;
3177      case GL_GREEN:
3178         greenIndex = 0;
3179         redIndex = blueIndex = alphaIndex = -1;
3180         stride = 1;
3181         break;
3182      case GL_BLUE:
3183         blueIndex = 0;
3184         redIndex = greenIndex = alphaIndex = -1;
3185         stride = 1;
3186         break;
3187      case GL_ALPHA:
3188         redIndex = greenIndex = blueIndex = -1;
3189         alphaIndex = 0;
3190         stride = 1;
3191         break;
3192      case GL_LUMINANCE:
3193         redIndex = greenIndex = blueIndex = 0;
3194         alphaIndex = -1;
3195         stride = 1;
3196         break;
3197      case GL_LUMINANCE_ALPHA:
3198         redIndex = greenIndex = blueIndex = 0;
3199         alphaIndex = 1;
3200         stride = 2;
3201         break;
3202      case GL_INTENSITY:
3203         redIndex = greenIndex = blueIndex = alphaIndex = 0;
3204         stride = 1;
3205         break;
3206      case GL_RGB:
3207         redIndex = 0;
3208         greenIndex = 1;
3209         blueIndex = 2;
3210         alphaIndex = -1;
3211         rComp = 0;
3212         gComp = 1;
3213         bComp = 2;
3214         aComp = 3;
3215         stride = 3;
3216         break;
3217      case GL_BGR:
3218         redIndex = 2;
3219         greenIndex = 1;
3220         blueIndex = 0;
3221         alphaIndex = -1;
3222         rComp = 2;
3223         gComp = 1;
3224         bComp = 0;
3225         aComp = 3;
3226         stride = 3;
3227         break;
3228      case GL_RGBA:
3229         redIndex = 0;
3230         greenIndex = 1;
3231         blueIndex = 2;
3232         alphaIndex = 3;
3233         rComp = 0;
3234         gComp = 1;
3235         bComp = 2;
3236         aComp = 3;
3237         stride = 4;
3238         break;
3239      case GL_BGRA:
3240         redIndex = 2;
3241         greenIndex = 1;
3242         blueIndex = 0;
3243         alphaIndex = 3;
3244         rComp = 2;
3245         gComp = 1;
3246         bComp = 0;
3247         aComp = 3;
3248         stride = 4;
3249         break;
3250      case GL_ABGR_EXT:
3251         redIndex = 3;
3252         greenIndex = 2;
3253         blueIndex = 1;
3254         alphaIndex = 0;
3255         rComp = 3;
3256         gComp = 2;
3257         bComp = 1;
3258         aComp = 0;
3259         stride = 4;
3260         break;
3261      case GL_DUDV_ATI:
3262         redIndex = 0;
3263         greenIndex = 1;
3264         blueIndex = -1;
3265         alphaIndex = -1;
3266         stride = 2;
3267         break;
3268      default:
3269         _mesa_problem(NULL, "bad srcFormat in extract float data");
3270         return;
3271   }
3272
3273
3274#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)		\
3275   if ((INDEX) < 0) {							\
3276      GLuint i;								\
3277      for (i = 0; i < n; i++) {						\
3278         rgba[i][CHANNEL] = DEFAULT;					\
3279      }									\
3280   }									\
3281   else if (swapBytes) {						\
3282      const TYPE *s = (const TYPE *) src;				\
3283      GLuint i;								\
3284      for (i = 0; i < n; i++) {						\
3285         TYPE value = s[INDEX];						\
3286         if (sizeof(TYPE) == 2) {					\
3287            SWAP2BYTE(value);						\
3288         }								\
3289         else if (sizeof(TYPE) == 4) {					\
3290            SWAP4BYTE(value);						\
3291         }								\
3292         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);		\
3293         s += stride;							\
3294      }									\
3295   }									\
3296   else {								\
3297      const TYPE *s = (const TYPE *) src;				\
3298      GLuint i;								\
3299      for (i = 0; i < n; i++) {						\
3300         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);		\
3301         s += stride;							\
3302      }									\
3303   }
3304
3305   switch (srcType) {
3306      case GL_UNSIGNED_BYTE:
3307         PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3308         PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3309         PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3310         PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
3311         break;
3312      case GL_BYTE:
3313         PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3314         PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3315         PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3316         PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
3317         break;
3318      case GL_UNSIGNED_SHORT:
3319         PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3320         PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3321         PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3322         PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
3323         break;
3324      case GL_SHORT:
3325         PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3326         PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3327         PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3328         PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
3329         break;
3330      case GL_UNSIGNED_INT:
3331         PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3332         PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3333         PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3334         PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
3335         break;
3336      case GL_INT:
3337         PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
3338         PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
3339         PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
3340         PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
3341         break;
3342      case GL_FLOAT:
3343         PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
3344         PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
3345         PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
3346         PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
3347         break;
3348      case GL_HALF_FLOAT_ARB:
3349         PROCESS(redIndex,   RCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3350         PROCESS(greenIndex, GCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3351         PROCESS(blueIndex,  BCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3352         PROCESS(alphaIndex, ACOMP, 1.0F, GLhalfARB, _mesa_half_to_float);
3353         break;
3354      case GL_UNSIGNED_BYTE_3_3_2:
3355         {
3356            const GLubyte *ubsrc = (const GLubyte *) src;
3357            GLuint i;
3358            for (i = 0; i < n; i ++) {
3359               GLubyte p = ubsrc[i];
3360               rgba[i][rComp] = ((p >> 5)      ) * (1.0F / 7.0F);
3361               rgba[i][gComp] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
3362               rgba[i][bComp] = ((p     ) & 0x3) * (1.0F / 3.0F);
3363               rgba[i][aComp] = 1.0F;
3364            }
3365         }
3366         break;
3367      case GL_UNSIGNED_BYTE_2_3_3_REV:
3368         {
3369            const GLubyte *ubsrc = (const GLubyte *) src;
3370            GLuint i;
3371            for (i = 0; i < n; i ++) {
3372               GLubyte p = ubsrc[i];
3373               rgba[i][rComp] = ((p     ) & 0x7) * (1.0F / 7.0F);
3374               rgba[i][gComp] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
3375               rgba[i][bComp] = ((p >> 6)      ) * (1.0F / 3.0F);
3376               rgba[i][aComp] = 1.0F;
3377            }
3378         }
3379         break;
3380      case GL_UNSIGNED_SHORT_5_6_5:
3381         if (swapBytes) {
3382            const GLushort *ussrc = (const GLushort *) src;
3383            GLuint i;
3384            for (i = 0; i < n; i ++) {
3385               GLushort p = ussrc[i];
3386               SWAP2BYTE(p);
3387               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3388               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
3389               rgba[i][bComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3390               rgba[i][aComp] = 1.0F;
3391            }
3392         }
3393         else {
3394            const GLushort *ussrc = (const GLushort *) src;
3395            GLuint i;
3396            for (i = 0; i < n; i ++) {
3397               GLushort p = ussrc[i];
3398               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3399               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
3400               rgba[i][bComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3401               rgba[i][aComp] = 1.0F;
3402            }
3403         }
3404         break;
3405      case GL_UNSIGNED_SHORT_5_6_5_REV:
3406         if (swapBytes) {
3407            const GLushort *ussrc = (const GLushort *) src;
3408            GLuint i;
3409            for (i = 0; i < n; i ++) {
3410               GLushort p = ussrc[i];
3411               SWAP2BYTE(p);
3412               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3413               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
3414               rgba[i][bComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3415               rgba[i][aComp] = 1.0F;
3416            }
3417         }
3418         else {
3419            const GLushort *ussrc = (const GLushort *) src;
3420            GLuint i;
3421            for (i = 0; i < n; i ++) {
3422               GLushort p = ussrc[i];
3423               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3424               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
3425               rgba[i][bComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3426               rgba[i][aComp] = 1.0F;
3427            }
3428         }
3429         break;
3430      case GL_UNSIGNED_SHORT_4_4_4_4:
3431         if (swapBytes) {
3432            const GLushort *ussrc = (const GLushort *) src;
3433            GLuint i;
3434            for (i = 0; i < n; i ++) {
3435               GLushort p = ussrc[i];
3436               SWAP2BYTE(p);
3437               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
3438               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
3439               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
3440               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
3441            }
3442         }
3443         else {
3444            const GLushort *ussrc = (const GLushort *) src;
3445            GLuint i;
3446            for (i = 0; i < n; i ++) {
3447               GLushort p = ussrc[i];
3448               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
3449               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
3450               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
3451               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
3452            }
3453         }
3454         break;
3455      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3456         if (swapBytes) {
3457            const GLushort *ussrc = (const GLushort *) src;
3458            GLuint i;
3459            for (i = 0; i < n; i ++) {
3460               GLushort p = ussrc[i];
3461               SWAP2BYTE(p);
3462               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
3463               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
3464               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
3465               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
3466            }
3467         }
3468         else {
3469            const GLushort *ussrc = (const GLushort *) src;
3470            GLuint i;
3471            for (i = 0; i < n; i ++) {
3472               GLushort p = ussrc[i];
3473               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
3474               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
3475               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
3476               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
3477            }
3478         }
3479         break;
3480      case GL_UNSIGNED_SHORT_5_5_5_1:
3481         if (swapBytes) {
3482            const GLushort *ussrc = (const GLushort *) src;
3483            GLuint i;
3484            for (i = 0; i < n; i ++) {
3485               GLushort p = ussrc[i];
3486               SWAP2BYTE(p);
3487               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3488               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
3489               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
3490               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
3491            }
3492         }
3493         else {
3494            const GLushort *ussrc = (const GLushort *) src;
3495            GLuint i;
3496            for (i = 0; i < n; i ++) {
3497               GLushort p = ussrc[i];
3498               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
3499               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
3500               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
3501               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
3502            }
3503         }
3504         break;
3505      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3506         if (swapBytes) {
3507            const GLushort *ussrc = (const GLushort *) src;
3508            GLuint i;
3509            for (i = 0; i < n; i ++) {
3510               GLushort p = ussrc[i];
3511               SWAP2BYTE(p);
3512               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3513               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
3514               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3515               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
3516            }
3517         }
3518         else {
3519            const GLushort *ussrc = (const GLushort *) src;
3520            GLuint i;
3521            for (i = 0; i < n; i ++) {
3522               GLushort p = ussrc[i];
3523               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
3524               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
3525               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3526               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
3527            }
3528         }
3529         break;
3530      case GL_UNSIGNED_INT_8_8_8_8:
3531         if (swapBytes) {
3532            const GLuint *uisrc = (const GLuint *) src;
3533            GLuint i;
3534            for (i = 0; i < n; i ++) {
3535               GLuint p = uisrc[i];
3536               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
3537               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3538               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3539               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
3540            }
3541         }
3542         else {
3543            const GLuint *uisrc = (const GLuint *) src;
3544            GLuint i;
3545            for (i = 0; i < n; i ++) {
3546               GLuint p = uisrc[i];
3547               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
3548               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3549               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3550               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
3551            }
3552         }
3553         break;
3554      case GL_UNSIGNED_INT_8_8_8_8_REV:
3555         if (swapBytes) {
3556            const GLuint *uisrc = (const GLuint *) src;
3557            GLuint i;
3558            for (i = 0; i < n; i ++) {
3559               GLuint p = uisrc[i];
3560               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
3561               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3562               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3563               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
3564            }
3565         }
3566         else {
3567            const GLuint *uisrc = (const GLuint *) src;
3568            GLuint i;
3569            for (i = 0; i < n; i ++) {
3570               GLuint p = uisrc[i];
3571               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
3572               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
3573               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3574               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
3575            }
3576         }
3577         break;
3578      case GL_UNSIGNED_INT_10_10_10_2:
3579         if (swapBytes) {
3580            const GLuint *uisrc = (const GLuint *) src;
3581            GLuint i;
3582            for (i = 0; i < n; i ++) {
3583               GLuint p = uisrc[i];
3584               SWAP4BYTE(p);
3585               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
3586               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3587               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
3588               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
3589            }
3590         }
3591         else {
3592            const GLuint *uisrc = (const GLuint *) src;
3593            GLuint i;
3594            for (i = 0; i < n; i ++) {
3595               GLuint p = uisrc[i];
3596               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
3597               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3598               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
3599               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
3600            }
3601         }
3602         break;
3603      case GL_UNSIGNED_INT_2_10_10_10_REV:
3604         if (swapBytes) {
3605            const GLuint *uisrc = (const GLuint *) src;
3606            GLuint i;
3607            for (i = 0; i < n; i ++) {
3608               GLuint p = uisrc[i];
3609               SWAP4BYTE(p);
3610               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
3611               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3612               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3613               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
3614            }
3615         }
3616         else {
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] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
3622               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3623               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3624               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
3625            }
3626         }
3627         break;
3628      default:
3629         _mesa_problem(NULL, "bad srcType in extract float data");
3630         break;
3631   }
3632}
3633
3634
3635/*
3636 * Unpack a row of color image data from a client buffer according to
3637 * the pixel unpacking parameters.
3638 * Return GLchan values in the specified dest image format.
3639 * This is used by glDrawPixels and glTexImage?D().
3640 * \param ctx - the context
3641 *         n - number of pixels in the span
3642 *         dstFormat - format of destination color array
3643 *         dest - the destination color array
3644 *         srcFormat - source image format
3645 *         srcType - source image  data type
3646 *         source - source image pointer
3647 *         srcPacking - pixel unpacking parameters
3648 *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
3649 *
3650 * XXX perhaps expand this to process whole images someday.
3651 */
3652void
3653_mesa_unpack_color_span_chan( GLcontext *ctx,
3654                              GLuint n, GLenum dstFormat, GLchan dest[],
3655                              GLenum srcFormat, GLenum srcType,
3656                              const GLvoid *source,
3657                              const struct gl_pixelstore_attrib *srcPacking,
3658                              GLbitfield transferOps )
3659{
3660   ASSERT(dstFormat == GL_ALPHA ||
3661          dstFormat == GL_LUMINANCE ||
3662          dstFormat == GL_LUMINANCE_ALPHA ||
3663          dstFormat == GL_INTENSITY ||
3664          dstFormat == GL_RGB ||
3665          dstFormat == GL_RGBA ||
3666          dstFormat == GL_COLOR_INDEX);
3667
3668   ASSERT(srcFormat == GL_RED ||
3669          srcFormat == GL_GREEN ||
3670          srcFormat == GL_BLUE ||
3671          srcFormat == GL_ALPHA ||
3672          srcFormat == GL_LUMINANCE ||
3673          srcFormat == GL_LUMINANCE_ALPHA ||
3674          srcFormat == GL_INTENSITY ||
3675          srcFormat == GL_RGB ||
3676          srcFormat == GL_BGR ||
3677          srcFormat == GL_RGBA ||
3678          srcFormat == GL_BGRA ||
3679          srcFormat == GL_ABGR_EXT ||
3680          srcFormat == GL_COLOR_INDEX);
3681
3682   ASSERT(srcType == GL_BITMAP ||
3683          srcType == GL_UNSIGNED_BYTE ||
3684          srcType == GL_BYTE ||
3685          srcType == GL_UNSIGNED_SHORT ||
3686          srcType == GL_SHORT ||
3687          srcType == GL_UNSIGNED_INT ||
3688          srcType == GL_INT ||
3689          srcType == GL_HALF_FLOAT_ARB ||
3690          srcType == GL_FLOAT ||
3691          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3692          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3693          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3694          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3695          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3696          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3697          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3698          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3699          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3700          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3701          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3702          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3703
3704   /* Try simple cases first */
3705   if (transferOps == 0) {
3706      if (srcType == CHAN_TYPE) {
3707         if (dstFormat == GL_RGBA) {
3708            if (srcFormat == GL_RGBA) {
3709               _mesa_memcpy( dest, source, n * 4 * sizeof(GLchan) );
3710               return;
3711            }
3712            else if (srcFormat == GL_RGB) {
3713               GLuint i;
3714               const GLchan *src = (const GLchan *) source;
3715               GLchan *dst = dest;
3716               for (i = 0; i < n; i++) {
3717                  dst[0] = src[0];
3718                  dst[1] = src[1];
3719                  dst[2] = src[2];
3720                  dst[3] = CHAN_MAX;
3721                  src += 3;
3722                  dst += 4;
3723               }
3724               return;
3725            }
3726         }
3727         else if (dstFormat == GL_RGB) {
3728            if (srcFormat == GL_RGB) {
3729               _mesa_memcpy( dest, source, n * 3 * sizeof(GLchan) );
3730               return;
3731            }
3732            else if (srcFormat == GL_RGBA) {
3733               GLuint i;
3734               const GLchan *src = (const GLchan *) source;
3735               GLchan *dst = dest;
3736               for (i = 0; i < n; i++) {
3737                  dst[0] = src[0];
3738                  dst[1] = src[1];
3739                  dst[2] = src[2];
3740                  src += 4;
3741                  dst += 3;
3742               }
3743               return;
3744            }
3745         }
3746         else if (dstFormat == srcFormat) {
3747            GLint comps = _mesa_components_in_format(srcFormat);
3748            assert(comps > 0);
3749            _mesa_memcpy( dest, source, n * comps * sizeof(GLchan) );
3750            return;
3751         }
3752      }
3753      /*
3754       * Common situation, loading 8bit RGBA/RGB source images
3755       * into 16/32 bit destination. (OSMesa16/32)
3756       */
3757      else if (srcType == GL_UNSIGNED_BYTE) {
3758         if (dstFormat == GL_RGBA) {
3759            if (srcFormat == GL_RGB) {
3760               GLuint i;
3761               const GLubyte *src = (const GLubyte *) source;
3762               GLchan *dst = dest;
3763               for (i = 0; i < n; i++) {
3764                  dst[0] = UBYTE_TO_CHAN(src[0]);
3765                  dst[1] = UBYTE_TO_CHAN(src[1]);
3766                  dst[2] = UBYTE_TO_CHAN(src[2]);
3767                  dst[3] = CHAN_MAX;
3768                  src += 3;
3769                  dst += 4;
3770               }
3771               return;
3772            }
3773            else if (srcFormat == GL_RGBA) {
3774               GLuint i;
3775               const GLubyte *src = (const GLubyte *) source;
3776               GLchan *dst = dest;
3777               for (i = 0; i < n; i++) {
3778                  dst[0] = UBYTE_TO_CHAN(src[0]);
3779                  dst[1] = UBYTE_TO_CHAN(src[1]);
3780                  dst[2] = UBYTE_TO_CHAN(src[2]);
3781                  dst[3] = UBYTE_TO_CHAN(src[3]);
3782                  src += 4;
3783                  dst += 4;
3784               }
3785               return;
3786             }
3787         }
3788         else if (dstFormat == GL_RGB) {
3789            if (srcFormat == GL_RGB) {
3790               GLuint i;
3791               const GLubyte *src = (const GLubyte *) source;
3792               GLchan *dst = dest;
3793               for (i = 0; i < n; i++) {
3794                  dst[0] = UBYTE_TO_CHAN(src[0]);
3795                  dst[1] = UBYTE_TO_CHAN(src[1]);
3796                  dst[2] = UBYTE_TO_CHAN(src[2]);
3797                  src += 3;
3798                  dst += 3;
3799               }
3800               return;
3801            }
3802            else if (srcFormat == GL_RGBA) {
3803               GLuint i;
3804               const GLubyte *src = (const GLubyte *) source;
3805               GLchan *dst = dest;
3806               for (i = 0; i < n; i++) {
3807                  dst[0] = UBYTE_TO_CHAN(src[0]);
3808                  dst[1] = UBYTE_TO_CHAN(src[1]);
3809                  dst[2] = UBYTE_TO_CHAN(src[2]);
3810                  src += 4;
3811                  dst += 3;
3812               }
3813               return;
3814            }
3815         }
3816      }
3817   }
3818
3819
3820   /* general solution begins here */
3821   {
3822      GLint dstComponents;
3823      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3824      GLint dstLuminanceIndex, dstIntensityIndex;
3825      GLfloat rgba[MAX_WIDTH][4];
3826
3827      dstComponents = _mesa_components_in_format( dstFormat );
3828      /* source & dest image formats should have been error checked by now */
3829      assert(dstComponents > 0);
3830
3831      /*
3832       * Extract image data and convert to RGBA floats
3833       */
3834      assert(n <= MAX_WIDTH);
3835      if (srcFormat == GL_COLOR_INDEX) {
3836         GLuint indexes[MAX_WIDTH];
3837         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3838                              srcPacking);
3839
3840         if (dstFormat == GL_COLOR_INDEX) {
3841            GLuint i;
3842            _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3843            /* convert to GLchan and return */
3844            for (i = 0; i < n; i++) {
3845               dest[i] = (GLchan) (indexes[i] & 0xff);
3846            }
3847            return;
3848         }
3849         else {
3850            /* Convert indexes to RGBA */
3851            if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3852               shift_and_offset_ci(ctx, n, indexes);
3853            }
3854            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3855         }
3856
3857         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3858          * with color indexes.
3859          */
3860         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3861      }
3862      else {
3863         /* non-color index data */
3864         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3865                            srcPacking->SwapBytes);
3866      }
3867
3868      /* Need to clamp if returning GLubytes or GLushorts */
3869#if CHAN_TYPE != GL_FLOAT
3870      transferOps |= IMAGE_CLAMP_BIT;
3871#endif
3872
3873      if (transferOps) {
3874         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3875      }
3876
3877      /* Now determine which color channels we need to produce.
3878       * And determine the dest index (offset) within each color tuple.
3879       */
3880      switch (dstFormat) {
3881         case GL_ALPHA:
3882            dstAlphaIndex = 0;
3883            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3884            dstLuminanceIndex = dstIntensityIndex = -1;
3885            break;
3886         case GL_LUMINANCE:
3887            dstLuminanceIndex = 0;
3888            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3889            dstIntensityIndex = -1;
3890            break;
3891         case GL_LUMINANCE_ALPHA:
3892            dstLuminanceIndex = 0;
3893            dstAlphaIndex = 1;
3894            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3895            dstIntensityIndex = -1;
3896            break;
3897         case GL_INTENSITY:
3898            dstIntensityIndex = 0;
3899            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3900            dstLuminanceIndex = -1;
3901            break;
3902         case GL_RGB:
3903            dstRedIndex = 0;
3904            dstGreenIndex = 1;
3905            dstBlueIndex = 2;
3906            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3907            break;
3908         case GL_RGBA:
3909            dstRedIndex = 0;
3910            dstGreenIndex = 1;
3911            dstBlueIndex = 2;
3912            dstAlphaIndex = 3;
3913            dstLuminanceIndex = dstIntensityIndex = -1;
3914            break;
3915         default:
3916            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_chan_span()");
3917            return;
3918      }
3919
3920
3921      /* Now return the GLchan data in the requested dstFormat */
3922
3923      if (dstRedIndex >= 0) {
3924         GLchan *dst = dest;
3925         GLuint i;
3926         for (i = 0; i < n; i++) {
3927            CLAMPED_FLOAT_TO_CHAN(dst[dstRedIndex], rgba[i][RCOMP]);
3928            dst += dstComponents;
3929         }
3930      }
3931
3932      if (dstGreenIndex >= 0) {
3933         GLchan *dst = dest;
3934         GLuint i;
3935         for (i = 0; i < n; i++) {
3936            CLAMPED_FLOAT_TO_CHAN(dst[dstGreenIndex], rgba[i][GCOMP]);
3937            dst += dstComponents;
3938         }
3939      }
3940
3941      if (dstBlueIndex >= 0) {
3942         GLchan *dst = dest;
3943         GLuint i;
3944         for (i = 0; i < n; i++) {
3945            CLAMPED_FLOAT_TO_CHAN(dst[dstBlueIndex], rgba[i][BCOMP]);
3946            dst += dstComponents;
3947         }
3948      }
3949
3950      if (dstAlphaIndex >= 0) {
3951         GLchan *dst = dest;
3952         GLuint i;
3953         for (i = 0; i < n; i++) {
3954            CLAMPED_FLOAT_TO_CHAN(dst[dstAlphaIndex], rgba[i][ACOMP]);
3955            dst += dstComponents;
3956         }
3957      }
3958
3959      if (dstIntensityIndex >= 0) {
3960         GLchan *dst = dest;
3961         GLuint i;
3962         assert(dstIntensityIndex == 0);
3963         assert(dstComponents == 1);
3964         for (i = 0; i < n; i++) {
3965            /* Intensity comes from red channel */
3966            CLAMPED_FLOAT_TO_CHAN(dst[i], rgba[i][RCOMP]);
3967         }
3968      }
3969
3970      if (dstLuminanceIndex >= 0) {
3971         GLchan *dst = dest;
3972         GLuint i;
3973         assert(dstLuminanceIndex == 0);
3974         for (i = 0; i < n; i++) {
3975            /* Luminance comes from red channel */
3976            CLAMPED_FLOAT_TO_CHAN(dst[0], rgba[i][RCOMP]);
3977            dst += dstComponents;
3978         }
3979      }
3980   }
3981}
3982
3983
3984/**
3985 * Same as _mesa_unpack_color_span_chan(), but return GLfloat data
3986 * instead of GLchan.
3987 */
3988void
3989_mesa_unpack_color_span_float( GLcontext *ctx,
3990                               GLuint n, GLenum dstFormat, GLfloat dest[],
3991                               GLenum srcFormat, GLenum srcType,
3992                               const GLvoid *source,
3993                               const struct gl_pixelstore_attrib *srcPacking,
3994                               GLbitfield transferOps )
3995{
3996   ASSERT(dstFormat == GL_ALPHA ||
3997          dstFormat == GL_LUMINANCE ||
3998          dstFormat == GL_LUMINANCE_ALPHA ||
3999          dstFormat == GL_INTENSITY ||
4000          dstFormat == GL_RGB ||
4001          dstFormat == GL_RGBA ||
4002          dstFormat == GL_COLOR_INDEX);
4003
4004   ASSERT(srcFormat == GL_RED ||
4005          srcFormat == GL_GREEN ||
4006          srcFormat == GL_BLUE ||
4007          srcFormat == GL_ALPHA ||
4008          srcFormat == GL_LUMINANCE ||
4009          srcFormat == GL_LUMINANCE_ALPHA ||
4010          srcFormat == GL_INTENSITY ||
4011          srcFormat == GL_RGB ||
4012          srcFormat == GL_BGR ||
4013          srcFormat == GL_RGBA ||
4014          srcFormat == GL_BGRA ||
4015          srcFormat == GL_ABGR_EXT ||
4016          srcFormat == GL_COLOR_INDEX);
4017
4018   ASSERT(srcType == GL_BITMAP ||
4019          srcType == GL_UNSIGNED_BYTE ||
4020          srcType == GL_BYTE ||
4021          srcType == GL_UNSIGNED_SHORT ||
4022          srcType == GL_SHORT ||
4023          srcType == GL_UNSIGNED_INT ||
4024          srcType == GL_INT ||
4025          srcType == GL_HALF_FLOAT_ARB ||
4026          srcType == GL_FLOAT ||
4027          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
4028          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
4029          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
4030          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
4031          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
4032          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
4033          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
4034          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
4035          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
4036          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
4037          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
4038          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
4039
4040   /* general solution, no special cases, yet */
4041   {
4042      GLint dstComponents;
4043      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
4044      GLint dstLuminanceIndex, dstIntensityIndex;
4045      GLfloat rgba[MAX_WIDTH][4];
4046
4047      dstComponents = _mesa_components_in_format( dstFormat );
4048      /* source & dest image formats should have been error checked by now */
4049      assert(dstComponents > 0);
4050
4051      /*
4052       * Extract image data and convert to RGBA floats
4053       */
4054      assert(n <= MAX_WIDTH);
4055      if (srcFormat == GL_COLOR_INDEX) {
4056         GLuint indexes[MAX_WIDTH];
4057         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
4058                              srcPacking);
4059
4060         if (dstFormat == GL_COLOR_INDEX) {
4061            GLuint i;
4062            _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4063            /* convert to GLchan and return */
4064            for (i = 0; i < n; i++) {
4065               dest[i] = (GLchan) (indexes[i] & 0xff);
4066            }
4067            return;
4068         }
4069         else {
4070            /* Convert indexes to RGBA */
4071            if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4072               shift_and_offset_ci(ctx, n, indexes);
4073            }
4074            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
4075         }
4076
4077         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
4078          * with color indexes.
4079          */
4080         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
4081      }
4082      else {
4083         /* non-color index data */
4084         extract_float_rgba(n, rgba, srcFormat, srcType, source,
4085                            srcPacking->SwapBytes);
4086      }
4087
4088      if (transferOps) {
4089         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
4090      }
4091
4092      /* Now determine which color channels we need to produce.
4093       * And determine the dest index (offset) within each color tuple.
4094       */
4095      switch (dstFormat) {
4096         case GL_ALPHA:
4097            dstAlphaIndex = 0;
4098            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
4099            dstLuminanceIndex = dstIntensityIndex = -1;
4100            break;
4101         case GL_LUMINANCE:
4102            dstLuminanceIndex = 0;
4103            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
4104            dstIntensityIndex = -1;
4105            break;
4106         case GL_LUMINANCE_ALPHA:
4107            dstLuminanceIndex = 0;
4108            dstAlphaIndex = 1;
4109            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
4110            dstIntensityIndex = -1;
4111            break;
4112         case GL_INTENSITY:
4113            dstIntensityIndex = 0;
4114            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
4115            dstLuminanceIndex = -1;
4116            break;
4117         case GL_RGB:
4118            dstRedIndex = 0;
4119            dstGreenIndex = 1;
4120            dstBlueIndex = 2;
4121            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
4122            break;
4123         case GL_RGBA:
4124            dstRedIndex = 0;
4125            dstGreenIndex = 1;
4126            dstBlueIndex = 2;
4127            dstAlphaIndex = 3;
4128            dstLuminanceIndex = dstIntensityIndex = -1;
4129            break;
4130         default:
4131            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_color_span_float()");
4132            return;
4133      }
4134
4135      /* Now pack results in the requested dstFormat */
4136      if (dstRedIndex >= 0) {
4137         GLfloat *dst = dest;
4138         GLuint i;
4139         for (i = 0; i < n; i++) {
4140            dst[dstRedIndex] = rgba[i][RCOMP];
4141            dst += dstComponents;
4142         }
4143      }
4144
4145      if (dstGreenIndex >= 0) {
4146         GLfloat *dst = dest;
4147         GLuint i;
4148         for (i = 0; i < n; i++) {
4149            dst[dstGreenIndex] = rgba[i][GCOMP];
4150            dst += dstComponents;
4151         }
4152      }
4153
4154      if (dstBlueIndex >= 0) {
4155         GLfloat *dst = dest;
4156         GLuint i;
4157         for (i = 0; i < n; i++) {
4158            dst[dstBlueIndex] = rgba[i][BCOMP];
4159            dst += dstComponents;
4160         }
4161      }
4162
4163      if (dstAlphaIndex >= 0) {
4164         GLfloat *dst = dest;
4165         GLuint i;
4166         for (i = 0; i < n; i++) {
4167            dst[dstAlphaIndex] = rgba[i][ACOMP];
4168            dst += dstComponents;
4169         }
4170      }
4171
4172      if (dstIntensityIndex >= 0) {
4173         GLfloat *dst = dest;
4174         GLuint i;
4175         assert(dstIntensityIndex == 0);
4176         assert(dstComponents == 1);
4177         for (i = 0; i < n; i++) {
4178            /* Intensity comes from red channel */
4179            dst[i] = rgba[i][RCOMP];
4180         }
4181      }
4182
4183      if (dstLuminanceIndex >= 0) {
4184         GLfloat *dst = dest;
4185         GLuint i;
4186         assert(dstLuminanceIndex == 0);
4187         for (i = 0; i < n; i++) {
4188            /* Luminance comes from red channel */
4189            dst[0] = rgba[i][RCOMP];
4190            dst += dstComponents;
4191         }
4192      }
4193   }
4194}
4195
4196/**
4197 * Similar to _mesa_unpack_color_span_float(), but for dudv data instead of rgba,
4198 * directly return GLbyte data, no transfer ops apply.
4199 */
4200void
4201_mesa_unpack_dudv_span_byte( GLcontext *ctx,
4202                             GLuint n, GLenum dstFormat, GLbyte dest[],
4203                             GLenum srcFormat, GLenum srcType,
4204                             const GLvoid *source,
4205                             const struct gl_pixelstore_attrib *srcPacking,
4206                             GLbitfield transferOps )
4207{
4208   ASSERT(dstFormat == GL_DUDV_ATI);
4209   ASSERT(srcFormat == GL_DUDV_ATI);
4210
4211   ASSERT(srcType == GL_UNSIGNED_BYTE ||
4212          srcType == GL_BYTE ||
4213          srcType == GL_UNSIGNED_SHORT ||
4214          srcType == GL_SHORT ||
4215          srcType == GL_UNSIGNED_INT ||
4216          srcType == GL_INT ||
4217          srcType == GL_HALF_FLOAT_ARB ||
4218          srcType == GL_FLOAT);
4219
4220   /* general solution */
4221   {
4222      GLint dstComponents;
4223      GLfloat rgba[MAX_WIDTH][4];
4224      GLbyte *dst = dest;
4225      GLuint i;
4226
4227      dstComponents = _mesa_components_in_format( dstFormat );
4228      /* source & dest image formats should have been error checked by now */
4229      assert(dstComponents > 0);
4230
4231      /*
4232       * Extract image data and convert to RGBA floats
4233       */
4234      assert(n <= MAX_WIDTH);
4235      extract_float_rgba(n, rgba, srcFormat, srcType, source,
4236                         srcPacking->SwapBytes);
4237
4238
4239      /* Now determine which color channels we need to produce.
4240       * And determine the dest index (offset) within each color tuple.
4241       */
4242
4243      /* Now pack results in the requested dstFormat */
4244      for (i = 0; i < n; i++) {
4245         /* not sure - need clamp[-1,1] here? */
4246         dst[0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
4247         dst[1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
4248         dst += dstComponents;
4249      }
4250   }
4251}
4252
4253/*
4254 * Unpack a row of color index data from a client buffer according to
4255 * the pixel unpacking parameters.
4256 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
4257 *
4258 * Args:  ctx - the context
4259 *        n - number of pixels
4260 *        dstType - destination data type
4261 *        dest - destination array
4262 *        srcType - source pixel type
4263 *        source - source data pointer
4264 *        srcPacking - pixel unpacking parameters
4265 *        transferOps - the pixel transfer operations to apply
4266 */
4267void
4268_mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
4269                         GLenum dstType, GLvoid *dest,
4270                         GLenum srcType, const GLvoid *source,
4271                         const struct gl_pixelstore_attrib *srcPacking,
4272                         GLbitfield transferOps )
4273{
4274   ASSERT(srcType == GL_BITMAP ||
4275          srcType == GL_UNSIGNED_BYTE ||
4276          srcType == GL_BYTE ||
4277          srcType == GL_UNSIGNED_SHORT ||
4278          srcType == GL_SHORT ||
4279          srcType == GL_UNSIGNED_INT ||
4280          srcType == GL_INT ||
4281          srcType == GL_HALF_FLOAT_ARB ||
4282          srcType == GL_FLOAT);
4283
4284   ASSERT(dstType == GL_UNSIGNED_BYTE ||
4285          dstType == GL_UNSIGNED_SHORT ||
4286          dstType == GL_UNSIGNED_INT);
4287
4288
4289   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4290
4291   /*
4292    * Try simple cases first
4293    */
4294   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
4295       && dstType == GL_UNSIGNED_BYTE) {
4296      _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4297   }
4298   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
4299            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
4300      _mesa_memcpy(dest, source, n * sizeof(GLuint));
4301   }
4302   else {
4303      /*
4304       * general solution
4305       */
4306      GLuint indexes[MAX_WIDTH];
4307      assert(n <= MAX_WIDTH);
4308
4309      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
4310                           srcPacking);
4311
4312      if (transferOps)
4313         _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4314
4315      /* convert to dest type */
4316      switch (dstType) {
4317         case GL_UNSIGNED_BYTE:
4318            {
4319               GLubyte *dst = (GLubyte *) dest;
4320               GLuint i;
4321               for (i = 0; i < n; i++) {
4322                  dst[i] = (GLubyte) (indexes[i] & 0xff);
4323               }
4324            }
4325            break;
4326         case GL_UNSIGNED_SHORT:
4327            {
4328               GLuint *dst = (GLuint *) dest;
4329               GLuint i;
4330               for (i = 0; i < n; i++) {
4331                  dst[i] = (GLushort) (indexes[i] & 0xffff);
4332               }
4333            }
4334            break;
4335         case GL_UNSIGNED_INT:
4336            _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4337            break;
4338         default:
4339            _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
4340      }
4341   }
4342}
4343
4344
4345void
4346_mesa_pack_index_span( const GLcontext *ctx, GLuint n,
4347                       GLenum dstType, GLvoid *dest, const GLuint *source,
4348                       const struct gl_pixelstore_attrib *dstPacking,
4349                       GLbitfield transferOps )
4350{
4351   GLuint indexes[MAX_WIDTH];
4352
4353   ASSERT(n <= MAX_WIDTH);
4354
4355   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4356
4357   if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
4358      /* make a copy of input */
4359      _mesa_memcpy(indexes, source, n * sizeof(GLuint));
4360      _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4361      source = indexes;
4362   }
4363
4364   switch (dstType) {
4365   case GL_UNSIGNED_BYTE:
4366      {
4367         GLubyte *dst = (GLubyte *) dest;
4368         GLuint i;
4369         for (i = 0; i < n; i++) {
4370            *dst++ = (GLubyte) source[i];
4371         }
4372      }
4373      break;
4374   case GL_BYTE:
4375      {
4376         GLbyte *dst = (GLbyte *) dest;
4377         GLuint i;
4378         for (i = 0; i < n; i++) {
4379            dst[i] = (GLbyte) source[i];
4380         }
4381      }
4382      break;
4383   case GL_UNSIGNED_SHORT:
4384      {
4385         GLushort *dst = (GLushort *) dest;
4386         GLuint i;
4387         for (i = 0; i < n; i++) {
4388            dst[i] = (GLushort) source[i];
4389         }
4390         if (dstPacking->SwapBytes) {
4391            _mesa_swap2( (GLushort *) dst, n );
4392         }
4393      }
4394      break;
4395   case GL_SHORT:
4396      {
4397         GLshort *dst = (GLshort *) dest;
4398         GLuint i;
4399         for (i = 0; i < n; i++) {
4400            dst[i] = (GLshort) source[i];
4401         }
4402         if (dstPacking->SwapBytes) {
4403            _mesa_swap2( (GLushort *) dst, n );
4404         }
4405      }
4406      break;
4407   case GL_UNSIGNED_INT:
4408      {
4409         GLuint *dst = (GLuint *) dest;
4410         GLuint i;
4411         for (i = 0; i < n; i++) {
4412            dst[i] = (GLuint) source[i];
4413         }
4414         if (dstPacking->SwapBytes) {
4415            _mesa_swap4( (GLuint *) dst, n );
4416         }
4417      }
4418      break;
4419   case GL_INT:
4420      {
4421         GLint *dst = (GLint *) dest;
4422         GLuint i;
4423         for (i = 0; i < n; i++) {
4424            dst[i] = (GLint) source[i];
4425         }
4426         if (dstPacking->SwapBytes) {
4427            _mesa_swap4( (GLuint *) dst, n );
4428         }
4429      }
4430      break;
4431   case GL_FLOAT:
4432      {
4433         GLfloat *dst = (GLfloat *) dest;
4434         GLuint i;
4435         for (i = 0; i < n; i++) {
4436            dst[i] = (GLfloat) source[i];
4437         }
4438         if (dstPacking->SwapBytes) {
4439            _mesa_swap4( (GLuint *) dst, n );
4440         }
4441      }
4442      break;
4443   case GL_HALF_FLOAT_ARB:
4444      {
4445         GLhalfARB *dst = (GLhalfARB *) dest;
4446         GLuint i;
4447         for (i = 0; i < n; i++) {
4448            dst[i] = _mesa_float_to_half((GLfloat) source[i]);
4449         }
4450         if (dstPacking->SwapBytes) {
4451            _mesa_swap2( (GLushort *) dst, n );
4452         }
4453      }
4454      break;
4455   default:
4456      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4457   }
4458}
4459
4460
4461/*
4462 * Unpack a row of stencil data from a client buffer according to
4463 * the pixel unpacking parameters.
4464 * This is (or will be) used by glDrawPixels
4465 *
4466 * Args:  ctx - the context
4467 *        n - number of pixels
4468 *        dstType - destination data type
4469 *        dest - destination array
4470 *        srcType - source pixel type
4471 *        source - source data pointer
4472 *        srcPacking - pixel unpacking parameters
4473 *        transferOps - apply offset/bias/lookup ops?
4474 */
4475void
4476_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
4477                           GLenum dstType, GLvoid *dest,
4478                           GLenum srcType, const GLvoid *source,
4479                           const struct gl_pixelstore_attrib *srcPacking,
4480                           GLbitfield transferOps )
4481{
4482   ASSERT(srcType == GL_BITMAP ||
4483          srcType == GL_UNSIGNED_BYTE ||
4484          srcType == GL_BYTE ||
4485          srcType == GL_UNSIGNED_SHORT ||
4486          srcType == GL_SHORT ||
4487          srcType == GL_UNSIGNED_INT ||
4488          srcType == GL_INT ||
4489          srcType == GL_UNSIGNED_INT_24_8_EXT ||
4490          srcType == GL_HALF_FLOAT_ARB ||
4491          srcType == GL_FLOAT);
4492
4493   ASSERT(dstType == GL_UNSIGNED_BYTE ||
4494          dstType == GL_UNSIGNED_SHORT ||
4495          dstType == GL_UNSIGNED_INT);
4496
4497   /* only shift and offset apply to stencil */
4498   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
4499
4500   /*
4501    * Try simple cases first
4502    */
4503   if (transferOps == 0 &&
4504       !ctx->Pixel.MapStencilFlag &&
4505       srcType == GL_UNSIGNED_BYTE &&
4506       dstType == GL_UNSIGNED_BYTE) {
4507      _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4508   }
4509   else if (transferOps == 0 &&
4510            !ctx->Pixel.MapStencilFlag &&
4511            srcType == GL_UNSIGNED_INT &&
4512            dstType == GL_UNSIGNED_INT &&
4513            !srcPacking->SwapBytes) {
4514      _mesa_memcpy(dest, source, n * sizeof(GLuint));
4515   }
4516   else {
4517      /*
4518       * general solution
4519       */
4520      GLuint indexes[MAX_WIDTH];
4521      assert(n <= MAX_WIDTH);
4522
4523      extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
4524                           srcPacking);
4525
4526      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4527         /* shift and offset indexes */
4528         shift_and_offset_ci(ctx, n, indexes);
4529      }
4530
4531      if (ctx->Pixel.MapStencilFlag) {
4532         /* Apply stencil lookup table */
4533         const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
4534         GLuint i;
4535         for (i = 0; i < n; i++) {
4536            indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
4537         }
4538      }
4539
4540      /* convert to dest type */
4541      switch (dstType) {
4542         case GL_UNSIGNED_BYTE:
4543            {
4544               GLubyte *dst = (GLubyte *) dest;
4545               GLuint i;
4546               for (i = 0; i < n; i++) {
4547                  dst[i] = (GLubyte) (indexes[i] & 0xff);
4548               }
4549            }
4550            break;
4551         case GL_UNSIGNED_SHORT:
4552            {
4553               GLuint *dst = (GLuint *) dest;
4554               GLuint i;
4555               for (i = 0; i < n; i++) {
4556                  dst[i] = (GLushort) (indexes[i] & 0xffff);
4557               }
4558            }
4559            break;
4560         case GL_UNSIGNED_INT:
4561            _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4562            break;
4563         default:
4564            _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
4565      }
4566   }
4567}
4568
4569
4570void
4571_mesa_pack_stencil_span( const GLcontext *ctx, GLuint n,
4572                         GLenum dstType, GLvoid *dest, const GLstencil *source,
4573                         const struct gl_pixelstore_attrib *dstPacking )
4574{
4575   GLstencil stencil[MAX_WIDTH];
4576
4577   ASSERT(n <= MAX_WIDTH);
4578
4579   if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
4580       ctx->Pixel.MapStencilFlag) {
4581      /* make a copy of input */
4582      _mesa_memcpy(stencil, source, n * sizeof(GLstencil));
4583      _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
4584      source = stencil;
4585   }
4586
4587   switch (dstType) {
4588   case GL_UNSIGNED_BYTE:
4589      if (sizeof(GLstencil) == 1) {
4590         _mesa_memcpy( dest, source, n );
4591      }
4592      else {
4593         GLubyte *dst = (GLubyte *) dest;
4594         GLuint i;
4595         for (i=0;i<n;i++) {
4596            dst[i] = (GLubyte) source[i];
4597         }
4598      }
4599      break;
4600   case GL_BYTE:
4601      {
4602         GLbyte *dst = (GLbyte *) dest;
4603         GLuint i;
4604         for (i=0;i<n;i++) {
4605            dst[i] = (GLbyte) (source[i] & 0x7f);
4606         }
4607      }
4608      break;
4609   case GL_UNSIGNED_SHORT:
4610      {
4611         GLushort *dst = (GLushort *) dest;
4612         GLuint i;
4613         for (i=0;i<n;i++) {
4614            dst[i] = (GLushort) source[i];
4615         }
4616         if (dstPacking->SwapBytes) {
4617            _mesa_swap2( (GLushort *) dst, n );
4618         }
4619      }
4620      break;
4621   case GL_SHORT:
4622      {
4623         GLshort *dst = (GLshort *) dest;
4624         GLuint i;
4625         for (i=0;i<n;i++) {
4626            dst[i] = (GLshort) source[i];
4627         }
4628         if (dstPacking->SwapBytes) {
4629            _mesa_swap2( (GLushort *) dst, n );
4630         }
4631      }
4632      break;
4633   case GL_UNSIGNED_INT:
4634      {
4635         GLuint *dst = (GLuint *) dest;
4636         GLuint i;
4637         for (i=0;i<n;i++) {
4638            dst[i] = (GLuint) source[i];
4639         }
4640         if (dstPacking->SwapBytes) {
4641            _mesa_swap4( (GLuint *) dst, n );
4642         }
4643      }
4644      break;
4645   case GL_INT:
4646      {
4647         GLint *dst = (GLint *) dest;
4648         GLuint i;
4649         for (i=0;i<n;i++) {
4650            dst[i] = (GLint) source[i];
4651         }
4652         if (dstPacking->SwapBytes) {
4653            _mesa_swap4( (GLuint *) dst, n );
4654         }
4655      }
4656      break;
4657   case GL_FLOAT:
4658      {
4659         GLfloat *dst = (GLfloat *) dest;
4660         GLuint i;
4661         for (i=0;i<n;i++) {
4662            dst[i] = (GLfloat) source[i];
4663         }
4664         if (dstPacking->SwapBytes) {
4665            _mesa_swap4( (GLuint *) dst, n );
4666         }
4667      }
4668      break;
4669   case GL_HALF_FLOAT_ARB:
4670      {
4671         GLhalfARB *dst = (GLhalfARB *) dest;
4672         GLuint i;
4673         for (i=0;i<n;i++) {
4674            dst[i] = _mesa_float_to_half( (float) source[i] );
4675         }
4676         if (dstPacking->SwapBytes) {
4677            _mesa_swap2( (GLushort *) dst, n );
4678         }
4679      }
4680      break;
4681   case GL_BITMAP:
4682      if (dstPacking->LsbFirst) {
4683         GLubyte *dst = (GLubyte *) dest;
4684         GLint shift = 0;
4685         GLuint i;
4686         for (i = 0; i < n; i++) {
4687            if (shift == 0)
4688               *dst = 0;
4689            *dst |= ((source[i] != 0) << shift);
4690            shift++;
4691            if (shift == 8) {
4692               shift = 0;
4693               dst++;
4694            }
4695         }
4696      }
4697      else {
4698         GLubyte *dst = (GLubyte *) dest;
4699         GLint shift = 7;
4700         GLuint i;
4701         for (i = 0; i < n; i++) {
4702            if (shift == 7)
4703               *dst = 0;
4704            *dst |= ((source[i] != 0) << shift);
4705            shift--;
4706            if (shift < 0) {
4707               shift = 7;
4708               dst++;
4709            }
4710         }
4711      }
4712      break;
4713   default:
4714      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4715   }
4716}
4717
4718#define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT)                              \
4719    do {                                                                \
4720        GLuint i;                                                       \
4721        const GLTYPE *src = (const GLTYPE *)source;                     \
4722        for (i = 0; i < n; i++) {                                       \
4723            GLTYPE value = src[i];                                      \
4724            if (srcPacking->SwapBytes) {                                \
4725                if (sizeof(GLTYPE) == 2) {                              \
4726                    SWAP2BYTE(value);                                   \
4727                } else if (sizeof(GLTYPE) == 4) {                       \
4728                    SWAP4BYTE(value);                                   \
4729                }                                                       \
4730            }                                                           \
4731            depthValues[i] = GLTYPE2FLOAT(value);                       \
4732        }                                                               \
4733    } while (0)
4734
4735
4736/**
4737 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
4738 * or GLfloat values.
4739 * The glPixelTransfer (scale/bias) params will be applied.
4740 *
4741 * \param dstType  one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
4742 * \param depthMax  max value for returned GLushort or GLuint values
4743 *                  (ignored for GLfloat).
4744 */
4745void
4746_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n,
4747                         GLenum dstType, GLvoid *dest, GLuint depthMax,
4748                         GLenum srcType, const GLvoid *source,
4749                         const struct gl_pixelstore_attrib *srcPacking )
4750{
4751   GLfloat depthTemp[MAX_WIDTH], *depthValues;
4752   GLboolean needClamp = GL_FALSE;
4753
4754   /* Look for special cases first.
4755    * Not only are these faster, they're less prone to numeric conversion
4756    * problems.  Otherwise, converting from an int type to a float then
4757    * back to an int type can introduce errors that will show up as
4758    * artifacts in things like depth peeling which uses glCopyTexImage.
4759    */
4760   if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
4761      if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
4762         const GLuint *src = (const GLuint *) source;
4763         GLushort *dst = (GLushort *) dest;
4764         GLuint i;
4765         for (i = 0; i < n; i++) {
4766            dst[i] = src[i] >> 16;
4767         }
4768         return;
4769      }
4770      if (srcType == GL_UNSIGNED_SHORT
4771          && dstType == GL_UNSIGNED_INT
4772          && depthMax == 0xffffffff) {
4773         const GLushort *src = (const GLushort *) source;
4774         GLuint *dst = (GLuint *) dest;
4775         GLuint i;
4776         for (i = 0; i < n; i++) {
4777            dst[i] = src[i] | (src[i] << 16);
4778         }
4779         return;
4780      }
4781      if (srcType == GL_UNSIGNED_INT_24_8
4782          && dstType == GL_UNSIGNED_INT
4783          && depthMax == 0xffffff) {
4784         const GLuint *src = (const GLuint *) source;
4785         GLuint *dst = (GLuint *) dest;
4786         GLuint i;
4787         for (i = 0; i < n; i++) {
4788            dst[i] = src[i] >> 8;
4789         }
4790         return;
4791      }
4792      /* XXX may want to add additional cases here someday */
4793   }
4794
4795   /* general case path follows */
4796
4797   if (dstType == GL_FLOAT) {
4798      depthValues = (GLfloat *) dest;
4799   }
4800   else {
4801      depthValues = depthTemp;
4802   }
4803
4804   /* Convert incoming values to GLfloat.  Some conversions will require
4805    * clamping, below.
4806    */
4807   switch (srcType) {
4808      case GL_BYTE:
4809         DEPTH_VALUES(GLbyte, BYTE_TO_FLOAT);
4810         needClamp = GL_TRUE;
4811         break;
4812      case GL_UNSIGNED_BYTE:
4813         DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
4814         break;
4815      case GL_SHORT:
4816         DEPTH_VALUES(GLshort, SHORT_TO_FLOAT);
4817         needClamp = GL_TRUE;
4818         break;
4819      case GL_UNSIGNED_SHORT:
4820         DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
4821         break;
4822      case GL_INT:
4823         DEPTH_VALUES(GLint, INT_TO_FLOAT);
4824         needClamp = GL_TRUE;
4825         break;
4826      case GL_UNSIGNED_INT:
4827         DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
4828         break;
4829      case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
4830         if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
4831             depthMax == 0xffffff &&
4832             ctx->Pixel.DepthScale == 1.0 &&
4833             ctx->Pixel.DepthBias == 0.0) {
4834            const GLuint *src = (const GLuint *) source;
4835            GLuint *zValues = (GLuint *) dest;
4836            GLuint i;
4837            for (i = 0; i < n; i++) {
4838                GLuint value = src[i];
4839                if (srcPacking->SwapBytes) {
4840                    SWAP4BYTE(value);
4841                }
4842                zValues[i] = value & 0xffffff00;
4843            }
4844            return;
4845         }
4846         else {
4847            const GLuint *src = (const GLuint *) source;
4848            const GLfloat scale = 1.0f / 0xffffff;
4849            GLuint i;
4850            for (i = 0; i < n; i++) {
4851                GLuint value = src[i];
4852                if (srcPacking->SwapBytes) {
4853                    SWAP4BYTE(value);
4854                }
4855                depthValues[i] = (value >> 8) * scale;
4856            }
4857         }
4858         break;
4859      case GL_FLOAT:
4860         DEPTH_VALUES(GLfloat, 1*);
4861         needClamp = GL_TRUE;
4862         break;
4863      case GL_HALF_FLOAT_ARB:
4864         {
4865            GLuint i;
4866            const GLhalfARB *src = (const GLhalfARB *) source;
4867            for (i = 0; i < n; i++) {
4868               GLhalfARB value = src[i];
4869               if (srcPacking->SwapBytes) {
4870                  SWAP2BYTE(value);
4871               }
4872               depthValues[i] = _mesa_half_to_float(value);
4873            }
4874            needClamp = GL_TRUE;
4875         }
4876         break;
4877      default:
4878         _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
4879         return;
4880   }
4881
4882   /* apply depth scale and bias */
4883   {
4884      const GLfloat scale = ctx->Pixel.DepthScale;
4885      const GLfloat bias = ctx->Pixel.DepthBias;
4886      if (scale != 1.0 || bias != 0.0) {
4887         GLuint i;
4888         for (i = 0; i < n; i++) {
4889            depthValues[i] = depthValues[i] * scale + bias;
4890         }
4891         needClamp = GL_TRUE;
4892      }
4893   }
4894
4895   /* clamp to [0, 1] */
4896   if (needClamp) {
4897      GLuint i;
4898      for (i = 0; i < n; i++) {
4899         depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
4900      }
4901   }
4902
4903   /*
4904    * Convert values to dstType
4905    */
4906   if (dstType == GL_UNSIGNED_INT) {
4907      GLuint *zValues = (GLuint *) dest;
4908      GLuint i;
4909      if (depthMax <= 0xffffff) {
4910         /* no overflow worries */
4911         for (i = 0; i < n; i++) {
4912            zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
4913         }
4914      }
4915      else {
4916         /* need to use double precision to prevent overflow problems */
4917         for (i = 0; i < n; i++) {
4918            GLdouble z = depthValues[i] * (GLfloat) depthMax;
4919            if (z >= (GLdouble) 0xffffffff)
4920               zValues[i] = 0xffffffff;
4921            else
4922               zValues[i] = (GLuint) z;
4923         }
4924      }
4925   }
4926   else if (dstType == GL_UNSIGNED_SHORT) {
4927      GLushort *zValues = (GLushort *) dest;
4928      GLuint i;
4929      ASSERT(depthMax <= 0xffff);
4930      for (i = 0; i < n; i++) {
4931         zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
4932      }
4933   }
4934   else {
4935      ASSERT(dstType == GL_FLOAT);
4936      /*ASSERT(depthMax == 1.0F);*/
4937   }
4938}
4939
4940
4941/*
4942 * Pack an array of depth values.  The values are floats in [0,1].
4943 */
4944void
4945_mesa_pack_depth_span( const GLcontext *ctx, GLuint n, GLvoid *dest,
4946                       GLenum dstType, const GLfloat *depthSpan,
4947                       const struct gl_pixelstore_attrib *dstPacking )
4948{
4949   GLfloat depthCopy[MAX_WIDTH];
4950
4951   ASSERT(n <= MAX_WIDTH);
4952
4953   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4954      _mesa_memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
4955      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4956      depthSpan = depthCopy;
4957   }
4958
4959   switch (dstType) {
4960   case GL_UNSIGNED_BYTE:
4961      {
4962         GLubyte *dst = (GLubyte *) dest;
4963         GLuint i;
4964         for (i = 0; i < n; i++) {
4965            dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
4966         }
4967      }
4968      break;
4969   case GL_BYTE:
4970      {
4971         GLbyte *dst = (GLbyte *) dest;
4972         GLuint i;
4973         for (i = 0; i < n; i++) {
4974            dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
4975         }
4976      }
4977      break;
4978   case GL_UNSIGNED_SHORT:
4979      {
4980         GLushort *dst = (GLushort *) dest;
4981         GLuint i;
4982         for (i = 0; i < n; i++) {
4983            CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
4984         }
4985         if (dstPacking->SwapBytes) {
4986            _mesa_swap2( (GLushort *) dst, n );
4987         }
4988      }
4989      break;
4990   case GL_SHORT:
4991      {
4992         GLshort *dst = (GLshort *) dest;
4993         GLuint i;
4994         for (i = 0; i < n; i++) {
4995            dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
4996         }
4997         if (dstPacking->SwapBytes) {
4998            _mesa_swap2( (GLushort *) dst, n );
4999         }
5000      }
5001      break;
5002   case GL_UNSIGNED_INT:
5003      {
5004         GLuint *dst = (GLuint *) dest;
5005         GLuint i;
5006         for (i = 0; i < n; i++) {
5007            dst[i] = FLOAT_TO_UINT( depthSpan[i] );
5008         }
5009         if (dstPacking->SwapBytes) {
5010            _mesa_swap4( (GLuint *) dst, n );
5011         }
5012      }
5013      break;
5014   case GL_INT:
5015      {
5016         GLint *dst = (GLint *) dest;
5017         GLuint i;
5018         for (i = 0; i < n; i++) {
5019            dst[i] = FLOAT_TO_INT( depthSpan[i] );
5020         }
5021         if (dstPacking->SwapBytes) {
5022            _mesa_swap4( (GLuint *) dst, n );
5023         }
5024      }
5025      break;
5026   case GL_FLOAT:
5027      {
5028         GLfloat *dst = (GLfloat *) dest;
5029         GLuint i;
5030         for (i = 0; i < n; i++) {
5031            dst[i] = depthSpan[i];
5032         }
5033         if (dstPacking->SwapBytes) {
5034            _mesa_swap4( (GLuint *) dst, n );
5035         }
5036      }
5037      break;
5038   case GL_HALF_FLOAT_ARB:
5039      {
5040         GLhalfARB *dst = (GLhalfARB *) dest;
5041         GLuint i;
5042         for (i = 0; i < n; i++) {
5043            dst[i] = _mesa_float_to_half(depthSpan[i]);
5044         }
5045         if (dstPacking->SwapBytes) {
5046            _mesa_swap2( (GLushort *) dst, n );
5047         }
5048      }
5049      break;
5050   default:
5051      _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
5052   }
5053}
5054
5055
5056
5057/**
5058 * Pack depth and stencil values as GL_DEPTH_STENCIL/GL_UNSIGNED_INT_24_8.
5059 */
5060void
5061_mesa_pack_depth_stencil_span(const GLcontext *ctx, GLuint n, GLuint *dest,
5062                              const GLfloat *depthVals,
5063                              const GLstencil *stencilVals,
5064                              const struct gl_pixelstore_attrib *dstPacking)
5065{
5066   GLfloat depthCopy[MAX_WIDTH];
5067   GLstencil stencilCopy[MAX_WIDTH];
5068   GLuint i;
5069
5070   ASSERT(n <= MAX_WIDTH);
5071
5072   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
5073      _mesa_memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
5074      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
5075      depthVals = depthCopy;
5076   }
5077
5078   if (ctx->Pixel.IndexShift ||
5079       ctx->Pixel.IndexOffset ||
5080       ctx->Pixel.MapStencilFlag) {
5081      _mesa_memcpy(stencilCopy, stencilVals, n * sizeof(GLstencil));
5082      _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
5083      stencilVals = stencilCopy;
5084   }
5085
5086   for (i = 0; i < n; i++) {
5087      GLuint z = (GLuint) (depthVals[i] * 0xffffff);
5088      dest[i] = (z << 8) | (stencilVals[i] & 0xff);
5089   }
5090
5091   if (dstPacking->SwapBytes) {
5092      _mesa_swap4(dest, n);
5093   }
5094}
5095
5096
5097
5098
5099/**
5100 * Unpack image data.  Apply byte swapping, byte flipping (bitmap).
5101 * Return all image data in a contiguous block.  This is used when we
5102 * compile glDrawPixels, glTexImage, etc into a display list.  We
5103 * need a copy of the data in a standard format.
5104 */
5105void *
5106_mesa_unpack_image( GLuint dimensions,
5107                    GLsizei width, GLsizei height, GLsizei depth,
5108                    GLenum format, GLenum type, const GLvoid *pixels,
5109                    const struct gl_pixelstore_attrib *unpack )
5110{
5111   GLint bytesPerRow, compsPerRow;
5112   GLboolean flipBytes, swap2, swap4;
5113
5114   if (!pixels)
5115      return NULL;  /* not necessarily an error */
5116
5117   if (width <= 0 || height <= 0 || depth <= 0)
5118      return NULL;  /* generate error later */
5119
5120   if (type == GL_BITMAP) {
5121      bytesPerRow = (width + 7) >> 3;
5122      flipBytes = unpack->LsbFirst;
5123      swap2 = swap4 = GL_FALSE;
5124      compsPerRow = 0;
5125   }
5126   else {
5127      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
5128      GLint components = _mesa_components_in_format(format);
5129      GLint bytesPerComp;
5130
5131      if (_mesa_type_is_packed(type))
5132          components = 1;
5133
5134      if (bytesPerPixel <= 0 || components <= 0)
5135         return NULL;   /* bad format or type.  generate error later */
5136      bytesPerRow = bytesPerPixel * width;
5137      bytesPerComp = bytesPerPixel / components;
5138      flipBytes = GL_FALSE;
5139      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
5140      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
5141      compsPerRow = components * width;
5142      assert(compsPerRow >= width);
5143   }
5144
5145   {
5146      GLubyte *destBuffer
5147         = (GLubyte *) _mesa_malloc(bytesPerRow * height * depth);
5148      GLubyte *dst;
5149      GLint img, row;
5150      if (!destBuffer)
5151         return NULL;   /* generate GL_OUT_OF_MEMORY later */
5152
5153      dst = destBuffer;
5154      for (img = 0; img < depth; img++) {
5155         for (row = 0; row < height; row++) {
5156            const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
5157                               width, height, format, type, img, row, 0);
5158
5159            if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
5160               GLint i;
5161               flipBytes = GL_FALSE;
5162               if (unpack->LsbFirst) {
5163                  GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
5164                  GLubyte dstMask = 128;
5165                  const GLubyte *s = src;
5166                  GLubyte *d = dst;
5167                  *d = 0;
5168                  for (i = 0; i < width; i++) {
5169                     if (*s & srcMask) {
5170                        *d |= dstMask;
5171                     }
5172                     if (srcMask == 128) {
5173                        srcMask = 1;
5174                        s++;
5175                     }
5176                     else {
5177                        srcMask = srcMask << 1;
5178                     }
5179                     if (dstMask == 1) {
5180                        dstMask = 128;
5181                        d++;
5182                        *d = 0;
5183                     }
5184                     else {
5185                        dstMask = dstMask >> 1;
5186                     }
5187                  }
5188               }
5189               else {
5190                  GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
5191                  GLubyte dstMask = 128;
5192                  const GLubyte *s = src;
5193                  GLubyte *d = dst;
5194                  *d = 0;
5195                  for (i = 0; i < width; i++) {
5196                     if (*s & srcMask) {
5197                        *d |= dstMask;
5198                     }
5199                     if (srcMask == 1) {
5200                        srcMask = 128;
5201                        s++;
5202                     }
5203                     else {
5204                        srcMask = srcMask >> 1;
5205                     }
5206                     if (dstMask == 1) {
5207                        dstMask = 128;
5208                        d++;
5209                        *d = 0;
5210                     }
5211                     else {
5212                        dstMask = dstMask >> 1;
5213                     }
5214                  }
5215               }
5216            }
5217            else {
5218               _mesa_memcpy(dst, src, bytesPerRow);
5219            }
5220
5221            /* byte flipping/swapping */
5222            if (flipBytes) {
5223               flip_bytes((GLubyte *) dst, bytesPerRow);
5224            }
5225            else if (swap2) {
5226               _mesa_swap2((GLushort*) dst, compsPerRow);
5227            }
5228            else if (swap4) {
5229               _mesa_swap4((GLuint*) dst, compsPerRow);
5230            }
5231            dst += bytesPerRow;
5232         }
5233      }
5234      return destBuffer;
5235   }
5236}
5237
5238#endif /* _HAVE_FULL_GL */
5239
5240
5241
5242/**
5243 * Convert an array of RGBA colors from one datatype to another.
5244 * NOTE: src may equal dst.  In that case, we use a temporary buffer.
5245 */
5246void
5247_mesa_convert_colors(GLenum srcType, const GLvoid *src,
5248                     GLenum dstType, GLvoid *dst,
5249                     GLuint count, const GLubyte mask[])
5250{
5251   GLuint tempBuffer[MAX_WIDTH][4];
5252   const GLboolean useTemp = (src == dst);
5253
5254   ASSERT(srcType != dstType);
5255
5256   switch (srcType) {
5257   case GL_UNSIGNED_BYTE:
5258      if (dstType == GL_UNSIGNED_SHORT) {
5259         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
5260         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
5261         GLuint i;
5262         for (i = 0; i < count; i++) {
5263            if (!mask || mask[i]) {
5264               dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
5265               dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
5266               dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
5267               dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
5268            }
5269         }
5270         if (useTemp)
5271            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
5272      }
5273      else {
5274         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
5275         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
5276         GLuint i;
5277         ASSERT(dstType == GL_FLOAT);
5278         for (i = 0; i < count; i++) {
5279            if (!mask || mask[i]) {
5280               dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
5281               dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
5282               dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
5283               dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
5284            }
5285         }
5286         if (useTemp)
5287            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
5288      }
5289      break;
5290   case GL_UNSIGNED_SHORT:
5291      if (dstType == GL_UNSIGNED_BYTE) {
5292         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
5293         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
5294         GLuint i;
5295         for (i = 0; i < count; i++) {
5296            if (!mask || mask[i]) {
5297               dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
5298               dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
5299               dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
5300               dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
5301            }
5302         }
5303         if (useTemp)
5304            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
5305      }
5306      else {
5307         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
5308         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
5309         GLuint i;
5310         ASSERT(dstType == GL_FLOAT);
5311         for (i = 0; i < count; i++) {
5312            if (!mask || mask[i]) {
5313               dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
5314               dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
5315               dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
5316               dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
5317            }
5318         }
5319         if (useTemp)
5320            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
5321      }
5322      break;
5323   case GL_FLOAT:
5324      if (dstType == GL_UNSIGNED_BYTE) {
5325         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
5326         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
5327         GLuint i;
5328         for (i = 0; i < count; i++) {
5329            if (!mask || mask[i]) {
5330               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
5331               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
5332               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
5333               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
5334            }
5335         }
5336         if (useTemp)
5337            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
5338      }
5339      else {
5340         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
5341         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
5342         GLuint i;
5343         ASSERT(dstType == GL_UNSIGNED_SHORT);
5344         for (i = 0; i < count; i++) {
5345            if (!mask || mask[i]) {
5346               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
5347               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
5348               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
5349               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
5350            }
5351         }
5352         if (useTemp)
5353            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
5354      }
5355      break;
5356   default:
5357      _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
5358   }
5359}
5360
5361
5362
5363
5364/**
5365 * Perform basic clipping for glDrawPixels.  The image's position and size
5366 * and the unpack SkipPixels and SkipRows are adjusted so that the image
5367 * region is entirely within the window and scissor bounds.
5368 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
5369 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
5370 * we'll actually write.  Beforehand, *destY-1 is the first drawing row.
5371 *
5372 * \return  GL_TRUE if image is ready for drawing or
5373 *          GL_FALSE if image was completely clipped away (draw nothing)
5374 */
5375GLboolean
5376_mesa_clip_drawpixels(const GLcontext *ctx,
5377                      GLint *destX, GLint *destY,
5378                      GLsizei *width, GLsizei *height,
5379                      struct gl_pixelstore_attrib *unpack)
5380{
5381   const GLframebuffer *buffer = ctx->DrawBuffer;
5382
5383   if (unpack->RowLength == 0) {
5384      unpack->RowLength = *width;
5385   }
5386
5387   ASSERT(ctx->Pixel.ZoomX == 1.0F);
5388   ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
5389
5390   /* left clipping */
5391   if (*destX < buffer->_Xmin) {
5392      unpack->SkipPixels += (buffer->_Xmin - *destX);
5393      *width -= (buffer->_Xmin - *destX);
5394      *destX = buffer->_Xmin;
5395   }
5396   /* right clipping */
5397   if (*destX + *width > buffer->_Xmax)
5398      *width -= (*destX + *width - buffer->_Xmax);
5399
5400   if (*width <= 0)
5401      return GL_FALSE;
5402
5403   if (ctx->Pixel.ZoomY == 1.0F) {
5404      /* bottom clipping */
5405      if (*destY < buffer->_Ymin) {
5406         unpack->SkipRows += (buffer->_Ymin - *destY);
5407         *height -= (buffer->_Ymin - *destY);
5408         *destY = buffer->_Ymin;
5409      }
5410      /* top clipping */
5411      if (*destY + *height > buffer->_Ymax)
5412         *height -= (*destY + *height - buffer->_Ymax);
5413   }
5414   else { /* upside down */
5415      /* top clipping */
5416      if (*destY > buffer->_Ymax) {
5417         unpack->SkipRows += (*destY - buffer->_Ymax);
5418         *height -= (*destY - buffer->_Ymax);
5419         *destY = buffer->_Ymax;
5420      }
5421      /* bottom clipping */
5422      if (*destY - *height < buffer->_Ymin)
5423         *height -= (buffer->_Ymin - (*destY - *height));
5424      /* adjust destY so it's the first row to write to */
5425      (*destY)--;
5426   }
5427
5428   if (*height <= 0)
5429      return GL_TRUE;
5430
5431   return GL_TRUE;
5432}
5433
5434
5435/**
5436 * Perform clipping for glReadPixels.  The image's window position
5437 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
5438 * so that the image region is entirely within the window bounds.
5439 * Note: this is different from _mesa_clip_drawpixels() in that the
5440 * scissor box is ignored, and we use the bounds of the current readbuffer
5441 * surface.
5442 *
5443 * \return  GL_TRUE if image is ready for drawing or
5444 *          GL_FALSE if image was completely clipped away (draw nothing)
5445 */
5446GLboolean
5447_mesa_clip_readpixels(const GLcontext *ctx,
5448                      GLint *srcX, GLint *srcY,
5449                      GLsizei *width, GLsizei *height,
5450                      struct gl_pixelstore_attrib *pack)
5451{
5452   const GLframebuffer *buffer = ctx->ReadBuffer;
5453
5454   if (pack->RowLength == 0) {
5455      pack->RowLength = *width;
5456   }
5457
5458   /* left clipping */
5459   if (*srcX < 0) {
5460      pack->SkipPixels += (0 - *srcX);
5461      *width -= (0 - *srcX);
5462      *srcX = 0;
5463   }
5464   /* right clipping */
5465   if (*srcX + *width > (GLsizei) buffer->Width)
5466      *width -= (*srcX + *width - buffer->Width);
5467
5468   if (*width <= 0)
5469      return GL_FALSE;
5470
5471   /* bottom clipping */
5472   if (*srcY < 0) {
5473      pack->SkipRows += (0 - *srcY);
5474      *height -= (0 - *srcY);
5475      *srcY = 0;
5476   }
5477   /* top clipping */
5478   if (*srcY + *height > (GLsizei) buffer->Height)
5479      *height -= (*srcY + *height - buffer->Height);
5480
5481   if (*height <= 0)
5482      return GL_TRUE;
5483
5484   return GL_TRUE;
5485}
5486
5487
5488/**
5489 * Do clipping for a glCopyTexSubImage call.
5490 * The framebuffer source region might extend outside the framebuffer
5491 * bounds.  Clip the source region against the framebuffer bounds and
5492 * adjust the texture/dest position and size accordingly.
5493 *
5494 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
5495 */
5496GLboolean
5497_mesa_clip_copytexsubimage(const GLcontext *ctx,
5498                           GLint *destX, GLint *destY,
5499                           GLint *srcX, GLint *srcY,
5500                           GLsizei *width, GLsizei *height)
5501{
5502   const struct gl_framebuffer *fb = ctx->ReadBuffer;
5503   const GLint srcX0 = *srcX, srcY0 = *srcY;
5504
5505   if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
5506                            srcX, srcY, width, height)) {
5507      *destX = *destX + *srcX - srcX0;
5508      *destY = *destY + *srcY - srcY0;
5509
5510      return GL_TRUE;
5511   }
5512   else {
5513      return GL_FALSE;
5514   }
5515}
5516
5517
5518
5519/**
5520 * Clip the rectangle defined by (x, y, width, height) against the bounds
5521 * specified by [xmin, xmax) and [ymin, ymax).
5522 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
5523 */
5524GLboolean
5525_mesa_clip_to_region(GLint xmin, GLint ymin,
5526                     GLint xmax, GLint ymax,
5527                     GLint *x, GLint *y,
5528                     GLsizei *width, GLsizei *height )
5529{
5530   /* left clipping */
5531   if (*x < xmin) {
5532      *width -= (xmin - *x);
5533      *x = xmin;
5534   }
5535
5536   /* right clipping */
5537   if (*x + *width > xmax)
5538      *width -= (*x + *width - xmax);
5539
5540   if (*width <= 0)
5541      return GL_FALSE;
5542
5543   /* bottom (or top) clipping */
5544   if (*y < ymin) {
5545      *height -= (ymin - *y);
5546      *y = ymin;
5547   }
5548
5549   /* top (or bottom) clipping */
5550   if (*y + *height > ymax)
5551      *height -= (*y + *height - ymax);
5552
5553   if (*height <= 0)
5554      return GL_FALSE;
5555
5556   return GL_TRUE;
5557}
5558
5559
5560/**
5561 * Clip dst coords against Xmax (or Ymax).
5562 */
5563static INLINE void
5564clip_right_or_top(GLint *srcX0, GLint *srcX1,
5565                  GLint *dstX0, GLint *dstX1,
5566                  GLint maxValue)
5567{
5568   GLfloat t, bias;
5569
5570   if (*dstX1 > maxValue) {
5571      /* X1 outside right edge */
5572      ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */
5573      t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
5574      /* chop off [t, 1] part */
5575      ASSERT(t >= 0.0 && t <= 1.0);
5576      *dstX1 = maxValue;
5577      bias = (*srcX0 < *srcX1) ? 0.5 : -0.5;
5578      *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
5579   }
5580   else if (*dstX0 > maxValue) {
5581      /* X0 outside right edge */
5582      ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */
5583      t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
5584      /* chop off [t, 1] part */
5585      ASSERT(t >= 0.0 && t <= 1.0);
5586      *dstX0 = maxValue;
5587      bias = (*srcX0 < *srcX1) ? -0.5 : 0.5;
5588      *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
5589   }
5590}
5591
5592
5593/**
5594 * Clip dst coords against Xmin (or Ymin).
5595 */
5596static INLINE void
5597clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
5598                    GLint *dstX0, GLint *dstX1,
5599                    GLint minValue)
5600{
5601   GLfloat t, bias;
5602
5603   if (*dstX0 < minValue) {
5604      /* X0 outside left edge */
5605      ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */
5606      t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
5607      /* chop off [0, t] part */
5608      ASSERT(t >= 0.0 && t <= 1.0);
5609      *dstX0 = minValue;
5610      bias = (*srcX0 < *srcX1) ? 0.5 : -0.5; /* flipped??? */
5611      *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
5612   }
5613   else if (*dstX1 < minValue) {
5614      /* X1 outside left edge */
5615      ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */
5616      t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
5617      /* chop off [0, t] part */
5618      ASSERT(t >= 0.0 && t <= 1.0);
5619      *dstX1 = minValue;
5620      bias = (*srcX0 < *srcX1) ? 0.5 : -0.5;
5621      *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
5622   }
5623}
5624
5625
5626/**
5627 * Do clipping of blit src/dest rectangles.
5628 * The dest rect is clipped against both the buffer bounds and scissor bounds.
5629 * The src rect is just clipped against the buffer bounds.
5630 *
5631 * When either the src or dest rect is clipped, the other is also clipped
5632 * proportionately!
5633 *
5634 * Note that X0 need not be less than X1 (same for Y) for either the source
5635 * and dest rects.  That makes the clipping a little trickier.
5636 *
5637 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
5638 */
5639GLboolean
5640_mesa_clip_blit(GLcontext *ctx,
5641                GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
5642                GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
5643{
5644   const GLint srcXmin = 0;
5645   const GLint srcXmax = ctx->ReadBuffer->Width;
5646   const GLint srcYmin = 0;
5647   const GLint srcYmax = ctx->ReadBuffer->Height;
5648
5649   /* these include scissor bounds */
5650   const GLint dstXmin = ctx->DrawBuffer->_Xmin;
5651   const GLint dstXmax = ctx->DrawBuffer->_Xmax;
5652   const GLint dstYmin = ctx->DrawBuffer->_Ymin;
5653   const GLint dstYmax = ctx->DrawBuffer->_Ymax;
5654
5655   /*
5656   printf("PreClipX:  src: %d .. %d  dst: %d .. %d\n",
5657          *srcX0, *srcX1, *dstX0, *dstX1);
5658   printf("PreClipY:  src: %d .. %d  dst: %d .. %d\n",
5659          *srcY0, *srcY1, *dstY0, *dstY1);
5660   */
5661
5662   /* trivial rejection tests */
5663   if (*dstX0 == *dstX1)
5664      return GL_FALSE; /* no width */
5665   if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
5666      return GL_FALSE; /* totally out (left) of bounds */
5667   if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
5668      return GL_FALSE; /* totally out (right) of bounds */
5669
5670   if (*dstY0 == *dstY1)
5671      return GL_FALSE;
5672   if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
5673      return GL_FALSE;
5674   if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
5675      return GL_FALSE;
5676
5677   if (*srcX0 == *srcX1)
5678      return GL_FALSE;
5679   if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
5680      return GL_FALSE;
5681   if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
5682      return GL_FALSE;
5683
5684   if (*srcY0 == *srcY1)
5685      return GL_FALSE;
5686   if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
5687      return GL_FALSE;
5688   if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
5689      return GL_FALSE;
5690
5691   /*
5692    * dest clip
5693    */
5694   clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
5695   clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
5696   clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
5697   clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
5698
5699   /*
5700    * src clip (just swap src/dst values from above)
5701    */
5702   clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
5703   clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
5704   clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
5705   clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
5706
5707   /*
5708   printf("PostClipX: src: %d .. %d  dst: %d .. %d\n",
5709          *srcX0, *srcX1, *dstX0, *dstX1);
5710   printf("PostClipY: src: %d .. %d  dst: %d .. %d\n",
5711          *srcY0, *srcY1, *dstY0, *dstY1);
5712   */
5713
5714   ASSERT(*dstX0 >= dstXmin);
5715   ASSERT(*dstX0 <= dstXmax);
5716   ASSERT(*dstX1 >= dstXmin);
5717   ASSERT(*dstX1 <= dstXmax);
5718
5719   ASSERT(*dstY0 >= dstYmin);
5720   ASSERT(*dstY0 <= dstYmax);
5721   ASSERT(*dstY1 >= dstYmin);
5722   ASSERT(*dstY1 <= dstYmax);
5723
5724   ASSERT(*srcX0 >= srcXmin);
5725   ASSERT(*srcX0 <= srcXmax);
5726   ASSERT(*srcX1 >= srcXmin);
5727   ASSERT(*srcX1 <= srcXmax);
5728
5729   ASSERT(*srcY0 >= srcYmin);
5730   ASSERT(*srcY0 <= srcYmax);
5731   ASSERT(*srcY1 >= srcYmin);
5732   ASSERT(*srcY1 <= srcYmax);
5733
5734   return GL_TRUE;
5735}
5736