image.c revision 53f83b435ce9a94092503712e3d4e16d4c942752
1/*
2 * Mesa 3-D graphics library
3 * Version:  6.5.2
4 *
5 * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26/**
27 * \file image.c
28 * Image handling.
29 */
30
31
32#include "glheader.h"
33#include "colormac.h"
34#include "context.h"
35#include "image.h"
36#include "imports.h"
37#include "histogram.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 */
65static GLboolean
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   register GLuint i, a, b;
107
108   for (i=0;i<n;i++) {
109      b = (GLuint) p[i];        /* words are often faster than bytes */
110      a = ((b & 0x01) << 7) |
111	  ((b & 0x02) << 5) |
112	  ((b & 0x04) << 3) |
113	  ((b & 0x08) << 1) |
114	  ((b & 0x10) >> 1) |
115	  ((b & 0x20) >> 3) |
116	  ((b & 0x40) >> 5) |
117	  ((b & 0x80) >> 7);
118      p[i] = (GLubyte) a;
119   }
120}
121
122
123/**
124 * Flip the order of the 2 bytes in each word in the given array.
125 *
126 * \param p array.
127 * \param n number of words.
128 */
129void
130_mesa_swap2( GLushort *p, GLuint n )
131{
132   register GLuint i;
133
134   for (i=0;i<n;i++) {
135      p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
136   }
137}
138
139
140
141/*
142 * Flip the order of the 4 bytes in each word in the given array.
143 */
144void
145_mesa_swap4( GLuint *p, GLuint n )
146{
147   register GLuint i, a, b;
148
149   for (i=0;i<n;i++) {
150      b = p[i];
151      a =  (b >> 24)
152	| ((b >> 8) & 0xff00)
153	| ((b << 8) & 0xff0000)
154	| ((b << 24) & 0xff000000);
155      p[i] = a;
156   }
157}
158
159
160/**
161 * Get the size of a GL data type.
162 *
163 * \param type GL data type.
164 *
165 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
166 * if an invalid type enum.
167 */
168GLint
169_mesa_sizeof_type( GLenum type )
170{
171   switch (type) {
172      case GL_BITMAP:
173	 return 0;
174      case GL_UNSIGNED_BYTE:
175         return sizeof(GLubyte);
176      case GL_BYTE:
177	 return sizeof(GLbyte);
178      case GL_UNSIGNED_SHORT:
179	 return sizeof(GLushort);
180      case GL_SHORT:
181	 return sizeof(GLshort);
182      case GL_UNSIGNED_INT:
183	 return sizeof(GLuint);
184      case GL_INT:
185	 return sizeof(GLint);
186      case GL_FLOAT:
187	 return sizeof(GLfloat);
188      case GL_HALF_FLOAT_ARB:
189	 return sizeof(GLhalfARB);
190      default:
191         return -1;
192   }
193}
194
195
196/**
197 * Same as _mesa_sizeof_type() but also accepting the packed pixel
198 * format data types.
199 */
200GLint
201_mesa_sizeof_packed_type( GLenum type )
202{
203   switch (type) {
204      case GL_BITMAP:
205	 return 0;
206      case GL_UNSIGNED_BYTE:
207         return sizeof(GLubyte);
208      case GL_BYTE:
209	 return sizeof(GLbyte);
210      case GL_UNSIGNED_SHORT:
211	 return sizeof(GLushort);
212      case GL_SHORT:
213	 return sizeof(GLshort);
214      case GL_UNSIGNED_INT:
215	 return sizeof(GLuint);
216      case GL_INT:
217	 return sizeof(GLint);
218      case GL_HALF_FLOAT_ARB:
219	 return sizeof(GLhalfARB);
220      case GL_FLOAT:
221	 return sizeof(GLfloat);
222      case GL_UNSIGNED_BYTE_3_3_2:
223         return sizeof(GLubyte);
224      case GL_UNSIGNED_BYTE_2_3_3_REV:
225         return sizeof(GLubyte);
226      case GL_UNSIGNED_SHORT_5_6_5:
227         return sizeof(GLushort);
228      case GL_UNSIGNED_SHORT_5_6_5_REV:
229         return sizeof(GLushort);
230      case GL_UNSIGNED_SHORT_4_4_4_4:
231         return sizeof(GLushort);
232      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
233         return sizeof(GLushort);
234      case GL_UNSIGNED_SHORT_5_5_5_1:
235         return sizeof(GLushort);
236      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
237         return sizeof(GLushort);
238      case GL_UNSIGNED_INT_8_8_8_8:
239         return sizeof(GLuint);
240      case GL_UNSIGNED_INT_8_8_8_8_REV:
241         return sizeof(GLuint);
242      case GL_UNSIGNED_INT_10_10_10_2:
243         return sizeof(GLuint);
244      case GL_UNSIGNED_INT_2_10_10_10_REV:
245         return sizeof(GLuint);
246      case GL_UNSIGNED_SHORT_8_8_MESA:
247      case GL_UNSIGNED_SHORT_8_8_REV_MESA:
248         return sizeof(GLushort);
249      case GL_UNSIGNED_INT_24_8_EXT:
250         return sizeof(GLuint);
251      default:
252         return -1;
253   }
254}
255
256
257/**
258 * Get the number of components in a pixel format.
259 *
260 * \param format pixel format.
261 *
262 * \return the number of components in the given format, or -1 if a bad format.
263 */
264GLint
265_mesa_components_in_format( GLenum format )
266{
267   switch (format) {
268      case GL_COLOR_INDEX:
269      case GL_COLOR_INDEX1_EXT:
270      case GL_COLOR_INDEX2_EXT:
271      case GL_COLOR_INDEX4_EXT:
272      case GL_COLOR_INDEX8_EXT:
273      case GL_COLOR_INDEX12_EXT:
274      case GL_COLOR_INDEX16_EXT:
275      case GL_STENCIL_INDEX:
276      case GL_DEPTH_COMPONENT:
277      case GL_RED:
278      case GL_GREEN:
279      case GL_BLUE:
280      case GL_ALPHA:
281      case GL_LUMINANCE:
282      case GL_INTENSITY:
283         return 1;
284      case GL_LUMINANCE_ALPHA:
285	 return 2;
286      case GL_RGB:
287	 return 3;
288      case GL_RGBA:
289	 return 4;
290      case GL_BGR:
291	 return 3;
292      case GL_BGRA:
293	 return 4;
294      case GL_ABGR_EXT:
295         return 4;
296      case GL_YCBCR_MESA:
297         return 2;
298      case GL_DEPTH_STENCIL_EXT:
299         return 2;
300      default:
301         return -1;
302   }
303}
304
305
306/**
307 * Get the bytes per pixel of pixel format type pair.
308 *
309 * \param format pixel format.
310 * \param type pixel type.
311 *
312 * \return bytes per pixel, or -1 if a bad format or type was given.
313 */
314GLint
315_mesa_bytes_per_pixel( GLenum format, GLenum type )
316{
317   GLint comps = _mesa_components_in_format( format );
318   if (comps < 0)
319      return -1;
320
321   switch (type) {
322      case GL_BITMAP:
323         return 0;  /* special case */
324      case GL_BYTE:
325      case GL_UNSIGNED_BYTE:
326         return comps * sizeof(GLubyte);
327      case GL_SHORT:
328      case GL_UNSIGNED_SHORT:
329         return comps * sizeof(GLshort);
330      case GL_INT:
331      case GL_UNSIGNED_INT:
332         return comps * sizeof(GLint);
333      case GL_FLOAT:
334         return comps * sizeof(GLfloat);
335      case GL_HALF_FLOAT_ARB:
336         return comps * sizeof(GLhalfARB);
337      case GL_UNSIGNED_BYTE_3_3_2:
338      case GL_UNSIGNED_BYTE_2_3_3_REV:
339         if (format == GL_RGB || format == GL_BGR)
340            return sizeof(GLubyte);
341         else
342            return -1;  /* error */
343      case GL_UNSIGNED_SHORT_5_6_5:
344      case GL_UNSIGNED_SHORT_5_6_5_REV:
345         if (format == GL_RGB || format == GL_BGR)
346            return sizeof(GLushort);
347         else
348            return -1;  /* error */
349      case GL_UNSIGNED_SHORT_4_4_4_4:
350      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
351      case GL_UNSIGNED_SHORT_5_5_5_1:
352      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
353         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
354            return sizeof(GLushort);
355         else
356            return -1;
357      case GL_UNSIGNED_INT_8_8_8_8:
358      case GL_UNSIGNED_INT_8_8_8_8_REV:
359      case GL_UNSIGNED_INT_10_10_10_2:
360      case GL_UNSIGNED_INT_2_10_10_10_REV:
361         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
362            return sizeof(GLuint);
363         else
364            return -1;
365      case GL_UNSIGNED_SHORT_8_8_MESA:
366      case GL_UNSIGNED_SHORT_8_8_REV_MESA:
367         if (format == GL_YCBCR_MESA)
368            return sizeof(GLushort);
369         else
370            return -1;
371      case GL_UNSIGNED_INT_24_8_EXT:
372         if (format == GL_DEPTH_STENCIL_EXT)
373            return sizeof(GLuint);
374         else
375            return -1;
376      default:
377         return -1;
378   }
379}
380
381
382/**
383 * Test for a legal pixel format and type.
384 *
385 * \param format pixel format.
386 * \param type pixel type.
387 *
388 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
389 * otherwise.
390 */
391GLboolean
392_mesa_is_legal_format_and_type( GLcontext *ctx, GLenum format, GLenum type )
393{
394   switch (format) {
395      case GL_COLOR_INDEX:
396      case GL_STENCIL_INDEX:
397         switch (type) {
398            case GL_BITMAP:
399            case GL_BYTE:
400            case GL_UNSIGNED_BYTE:
401            case GL_SHORT:
402            case GL_UNSIGNED_SHORT:
403            case GL_INT:
404            case GL_UNSIGNED_INT:
405            case GL_FLOAT:
406               return GL_TRUE;
407            case GL_HALF_FLOAT_ARB:
408               return ctx->Extensions.ARB_half_float_pixel;
409            default:
410               return GL_FALSE;
411         }
412      case GL_RED:
413      case GL_GREEN:
414      case GL_BLUE:
415      case GL_ALPHA:
416#if 0 /* not legal!  see table 3.6 of the 1.5 spec */
417      case GL_INTENSITY:
418#endif
419      case GL_LUMINANCE:
420      case GL_LUMINANCE_ALPHA:
421      case GL_DEPTH_COMPONENT:
422         switch (type) {
423            case GL_BYTE:
424            case GL_UNSIGNED_BYTE:
425            case GL_SHORT:
426            case GL_UNSIGNED_SHORT:
427            case GL_INT:
428            case GL_UNSIGNED_INT:
429            case GL_FLOAT:
430               return GL_TRUE;
431            case GL_HALF_FLOAT_ARB:
432               return ctx->Extensions.ARB_half_float_pixel;
433            default:
434               return GL_FALSE;
435         }
436      case GL_RGB:
437         switch (type) {
438            case GL_BYTE:
439            case GL_UNSIGNED_BYTE:
440            case GL_SHORT:
441            case GL_UNSIGNED_SHORT:
442            case GL_INT:
443            case GL_UNSIGNED_INT:
444            case GL_FLOAT:
445            case GL_UNSIGNED_BYTE_3_3_2:
446            case GL_UNSIGNED_BYTE_2_3_3_REV:
447            case GL_UNSIGNED_SHORT_5_6_5:
448            case GL_UNSIGNED_SHORT_5_6_5_REV:
449               return GL_TRUE;
450            case GL_HALF_FLOAT_ARB:
451               return ctx->Extensions.ARB_half_float_pixel;
452            default:
453               return GL_FALSE;
454         }
455      case GL_BGR:
456         switch (type) {
457            /* NOTE: no packed types are supported with BGR.  That's
458             * intentional, according to the GL spec.
459             */
460            case GL_BYTE:
461            case GL_UNSIGNED_BYTE:
462            case GL_SHORT:
463            case GL_UNSIGNED_SHORT:
464            case GL_INT:
465            case GL_UNSIGNED_INT:
466            case GL_FLOAT:
467               return GL_TRUE;
468            case GL_HALF_FLOAT_ARB:
469               return ctx->Extensions.ARB_half_float_pixel;
470            default:
471               return GL_FALSE;
472         }
473      case GL_RGBA:
474      case GL_BGRA:
475      case GL_ABGR_EXT:
476         switch (type) {
477            case GL_BYTE:
478            case GL_UNSIGNED_BYTE:
479            case GL_SHORT:
480            case GL_UNSIGNED_SHORT:
481            case GL_INT:
482            case GL_UNSIGNED_INT:
483            case GL_FLOAT:
484            case GL_UNSIGNED_SHORT_4_4_4_4:
485            case GL_UNSIGNED_SHORT_4_4_4_4_REV:
486            case GL_UNSIGNED_SHORT_5_5_5_1:
487            case GL_UNSIGNED_SHORT_1_5_5_5_REV:
488            case GL_UNSIGNED_INT_8_8_8_8:
489            case GL_UNSIGNED_INT_8_8_8_8_REV:
490            case GL_UNSIGNED_INT_10_10_10_2:
491            case GL_UNSIGNED_INT_2_10_10_10_REV:
492               return GL_TRUE;
493            case GL_HALF_FLOAT_ARB:
494               return ctx->Extensions.ARB_half_float_pixel;
495            default:
496               return GL_FALSE;
497         }
498      case GL_YCBCR_MESA:
499         if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
500             type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
501            return GL_TRUE;
502         else
503            return GL_FALSE;
504      case GL_DEPTH_STENCIL_EXT:
505         if (ctx->Extensions.EXT_packed_depth_stencil
506             && type == GL_UNSIGNED_INT_24_8_EXT)
507            return GL_TRUE;
508         else
509            return GL_FALSE;
510      default:
511         ; /* fall-through */
512   }
513   return GL_FALSE;
514}
515
516
517/**
518 * Return the address of a specific pixel in an image (1D, 2D or 3D).
519 *
520 * Pixel unpacking/packing parameters are observed according to \p packing.
521 *
522 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
523 * \param image  starting address of image data
524 * \param width  the image width
525 * \param height  theimage height
526 * \param format  the pixel format
527 * \param type  the pixel data type
528 * \param packing  the pixelstore attributes
529 * \param img  which image in the volume (0 for 1D or 2D images)
530 * \param row  row of pixel in the image (0 for 1D images)
531 * \param column column of pixel in the image
532 *
533 * \return address of pixel on success, or NULL on error.
534 *
535 * \sa gl_pixelstore_attrib.
536 */
537GLvoid *
538_mesa_image_address( GLuint dimensions,
539                     const struct gl_pixelstore_attrib *packing,
540                     const GLvoid *image,
541                     GLsizei width, GLsizei height,
542                     GLenum format, GLenum type,
543                     GLint img, GLint row, GLint column )
544{
545   GLint alignment;        /* 1, 2 or 4 */
546   GLint pixels_per_row;
547   GLint rows_per_image;
548   GLint skiprows;
549   GLint skippixels;
550   GLint skipimages;       /* for 3-D volume images */
551   GLubyte *pixel_addr;
552
553   ASSERT(dimensions >= 1 && dimensions <= 3);
554
555   alignment = packing->Alignment;
556   if (packing->RowLength > 0) {
557      pixels_per_row = packing->RowLength;
558   }
559   else {
560      pixels_per_row = width;
561   }
562   if (packing->ImageHeight > 0) {
563      rows_per_image = packing->ImageHeight;
564   }
565   else {
566      rows_per_image = height;
567   }
568
569   skippixels = packing->SkipPixels;
570   /* Note: SKIP_ROWS _is_ used for 1D images */
571   skiprows = packing->SkipRows;
572   /* Note: SKIP_IMAGES is only used for 3D images */
573   skipimages = (dimensions == 3) ? packing->SkipImages : 0;
574
575   if (type == GL_BITMAP) {
576      /* BITMAP data */
577      GLint comp_per_pixel;   /* components per pixel */
578      GLint bytes_per_comp;   /* bytes per component */
579      GLint bytes_per_row;
580      GLint bytes_per_image;
581
582      /* Compute bytes per component */
583      bytes_per_comp = _mesa_sizeof_packed_type( type );
584      if (bytes_per_comp < 0) {
585         return NULL;
586      }
587
588      /* Compute number of components per pixel */
589      comp_per_pixel = _mesa_components_in_format( format );
590      if (comp_per_pixel < 0) {
591         return NULL;
592      }
593
594      bytes_per_row = alignment
595                    * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
596
597      bytes_per_image = bytes_per_row * rows_per_image;
598
599      pixel_addr = (GLubyte *) image
600                 + (skipimages + img) * bytes_per_image
601                 + (skiprows + row) * bytes_per_row
602                 + (skippixels + column) / 8;
603   }
604   else {
605      /* Non-BITMAP data */
606      GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
607      GLint topOfImage;
608
609      bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
610
611      /* The pixel type and format should have been error checked earlier */
612      assert(bytes_per_pixel > 0);
613
614      bytes_per_row = pixels_per_row * bytes_per_pixel;
615      remainder = bytes_per_row % alignment;
616      if (remainder > 0)
617         bytes_per_row += (alignment - remainder);
618
619      ASSERT(bytes_per_row % alignment == 0);
620
621      bytes_per_image = bytes_per_row * rows_per_image;
622
623      if (packing->Invert) {
624         /* set pixel_addr to the last row */
625         topOfImage = bytes_per_row * (height - 1);
626         bytes_per_row = -bytes_per_row;
627      }
628      else {
629         topOfImage = 0;
630      }
631
632      /* compute final pixel address */
633      pixel_addr = (GLubyte *) image
634                 + (skipimages + img) * bytes_per_image
635                 + topOfImage
636                 + (skiprows + row) * bytes_per_row
637                 + (skippixels + column) * bytes_per_pixel;
638   }
639
640   return (GLvoid *) pixel_addr;
641}
642
643
644GLvoid *
645_mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
646                       const GLvoid *image,
647                       GLsizei width,
648                       GLenum format, GLenum type,
649                       GLint column )
650{
651   return _mesa_image_address(1, packing, image, width, 1,
652                              format, type, 0, 0, column);
653}
654
655
656GLvoid *
657_mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
658                       const GLvoid *image,
659                       GLsizei width, GLsizei height,
660                       GLenum format, GLenum type,
661                       GLint row, GLint column )
662{
663   return _mesa_image_address(2, packing, image, width, height,
664                              format, type, 0, row, column);
665}
666
667
668GLvoid *
669_mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
670                       const GLvoid *image,
671                       GLsizei width, GLsizei height,
672                       GLenum format, GLenum type,
673                       GLint img, GLint row, GLint column )
674{
675   return _mesa_image_address(3, packing, image, width, height,
676                              format, type, img, row, column);
677}
678
679
680
681/**
682 * Compute the stride (in bytes) between image rows.
683 *
684 * \param packing the pixelstore attributes
685 * \param width image width.
686 * \param format pixel format.
687 * \param type pixel data type.
688 *
689 * \return the stride in bytes for the given parameters, or -1 if error
690 */
691GLint
692_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
693                        GLint width, GLenum format, GLenum type )
694{
695   GLint bytesPerRow, remainder;
696
697   ASSERT(packing);
698
699   if (type == GL_BITMAP) {
700      if (packing->RowLength == 0) {
701         bytesPerRow = (width + 7) / 8;
702      }
703      else {
704         bytesPerRow = (packing->RowLength + 7) / 8;
705      }
706   }
707   else {
708      /* Non-BITMAP data */
709      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
710      if (bytesPerPixel <= 0)
711         return -1;  /* error */
712      if (packing->RowLength == 0) {
713         bytesPerRow = bytesPerPixel * width;
714      }
715      else {
716         bytesPerRow = bytesPerPixel * packing->RowLength;
717      }
718   }
719
720   remainder = bytesPerRow % packing->Alignment;
721   if (remainder > 0) {
722      bytesPerRow += (packing->Alignment - remainder);
723   }
724
725   if (packing->Invert) {
726      /* negate the bytes per row (negative row stride) */
727      bytesPerRow = -bytesPerRow;
728   }
729
730   return bytesPerRow;
731}
732
733
734#if _HAVE_FULL_GL
735
736/*
737 * Compute the stride between images in a 3D texture (in bytes) for the given
738 * pixel packing parameters and image width, format and type.
739 */
740GLint
741_mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
742                          GLint width, GLint height,
743                          GLenum format, GLenum type )
744{
745   ASSERT(packing);
746   ASSERT(type != GL_BITMAP);
747
748   {
749      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
750      GLint bytesPerRow, bytesPerImage, remainder;
751
752      if (bytesPerPixel <= 0)
753         return -1;  /* error */
754      if (packing->RowLength == 0) {
755         bytesPerRow = bytesPerPixel * width;
756      }
757      else {
758         bytesPerRow = bytesPerPixel * packing->RowLength;
759      }
760      remainder = bytesPerRow % packing->Alignment;
761      if (remainder > 0)
762         bytesPerRow += (packing->Alignment - remainder);
763
764      if (packing->ImageHeight == 0)
765         bytesPerImage = bytesPerRow * height;
766      else
767         bytesPerImage = bytesPerRow * packing->ImageHeight;
768
769      return bytesPerImage;
770   }
771}
772
773
774/*
775 * Unpack a 32x32 pixel polygon stipple from user memory using the
776 * current pixel unpack settings.
777 */
778void
779_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
780                              const struct gl_pixelstore_attrib *unpacking )
781{
782   GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap(32, 32, pattern, unpacking);
783   if (ptrn) {
784      /* Convert pattern from GLubytes to GLuints and handle big/little
785       * endian differences
786       */
787      GLubyte *p = ptrn;
788      GLint i;
789      for (i = 0; i < 32; i++) {
790         dest[i] = (p[0] << 24)
791                 | (p[1] << 16)
792                 | (p[2] <<  8)
793                 | (p[3]      );
794         p += 4;
795      }
796      _mesa_free(ptrn);
797   }
798}
799
800
801/*
802 * Pack polygon stipple into user memory given current pixel packing
803 * settings.
804 */
805void
806_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
807                            const struct gl_pixelstore_attrib *packing )
808{
809   /* Convert pattern from GLuints to GLubytes to handle big/little
810    * endian differences.
811    */
812   GLubyte ptrn[32*4];
813   GLint i;
814   for (i = 0; i < 32; i++) {
815      ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
816      ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
817      ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
818      ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
819   }
820
821   _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
822}
823
824
825/*
826 * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
827 * order with row alignment = 1 byte.
828 */
829GLvoid *
830_mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
831                     const struct gl_pixelstore_attrib *packing )
832{
833   GLint bytes, row, width_in_bytes;
834   GLubyte *buffer, *dst;
835
836   if (!pixels)
837      return NULL;
838
839   /* Alloc dest storage */
840   bytes = ((width + 7) / 8 * height);
841   buffer = (GLubyte *) _mesa_malloc( bytes );
842   if (!buffer)
843      return NULL;
844
845   width_in_bytes = CEILING( width, 8 );
846   dst = buffer;
847   for (row = 0; row < height; row++) {
848      const GLubyte *src = (const GLubyte *)
849         _mesa_image_address2d(packing, pixels, width, height,
850                               GL_COLOR_INDEX, GL_BITMAP, row, 0);
851      if (!src) {
852         _mesa_free(buffer);
853         return NULL;
854      }
855
856      if (packing->SkipPixels == 0) {
857         _mesa_memcpy( dst, src, width_in_bytes );
858         if (packing->LsbFirst) {
859            flip_bytes( dst, width_in_bytes );
860         }
861      }
862      else {
863         /* handling SkipPixels is a bit tricky (no pun intended!) */
864         GLint i;
865         if (packing->LsbFirst) {
866            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
867            GLubyte dstMask = 128;
868            const GLubyte *s = src;
869            GLubyte *d = dst;
870            *d = 0;
871            for (i = 0; i < width; i++) {
872               if (*s & srcMask) {
873                  *d |= dstMask;
874               }
875               if (srcMask == 128) {
876                  srcMask = 1;
877                  s++;
878               }
879               else {
880                  srcMask = srcMask << 1;
881               }
882               if (dstMask == 1) {
883                  dstMask = 128;
884                  d++;
885                  *d = 0;
886               }
887               else {
888                  dstMask = dstMask >> 1;
889               }
890            }
891         }
892         else {
893            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
894            GLubyte dstMask = 128;
895            const GLubyte *s = src;
896            GLubyte *d = dst;
897            *d = 0;
898            for (i = 0; i < width; i++) {
899               if (*s & srcMask) {
900                  *d |= dstMask;
901               }
902               if (srcMask == 1) {
903                  srcMask = 128;
904                  s++;
905               }
906               else {
907                  srcMask = srcMask >> 1;
908               }
909               if (dstMask == 1) {
910                  dstMask = 128;
911                  d++;
912                  *d = 0;
913               }
914               else {
915                  dstMask = dstMask >> 1;
916               }
917            }
918         }
919      }
920      dst += width_in_bytes;
921   }
922
923   return buffer;
924}
925
926
927/*
928 * Pack bitmap data.
929 */
930void
931_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
932                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
933{
934   GLint row, width_in_bytes;
935   const GLubyte *src;
936
937   if (!source)
938      return;
939
940   width_in_bytes = CEILING( width, 8 );
941   src = source;
942   for (row = 0; row < height; row++) {
943      GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
944                       width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
945      if (!dst)
946         return;
947
948      if (packing->SkipPixels == 0) {
949         _mesa_memcpy( dst, src, width_in_bytes );
950         if (packing->LsbFirst) {
951            flip_bytes( dst, width_in_bytes );
952         }
953      }
954      else {
955         /* handling SkipPixels is a bit tricky (no pun intended!) */
956         GLint i;
957         if (packing->LsbFirst) {
958            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
959            GLubyte dstMask = 128;
960            const GLubyte *s = src;
961            GLubyte *d = dst;
962            *d = 0;
963            for (i = 0; i < width; i++) {
964               if (*s & srcMask) {
965                  *d |= dstMask;
966               }
967               if (srcMask == 128) {
968                  srcMask = 1;
969                  s++;
970               }
971               else {
972                  srcMask = srcMask << 1;
973               }
974               if (dstMask == 1) {
975                  dstMask = 128;
976                  d++;
977                  *d = 0;
978               }
979               else {
980                  dstMask = dstMask >> 1;
981               }
982            }
983         }
984         else {
985            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
986            GLubyte dstMask = 128;
987            const GLubyte *s = src;
988            GLubyte *d = dst;
989            *d = 0;
990            for (i = 0; i < width; i++) {
991               if (*s & srcMask) {
992                  *d |= dstMask;
993               }
994               if (srcMask == 1) {
995                  srcMask = 128;
996                  s++;
997               }
998               else {
999                  srcMask = srcMask >> 1;
1000               }
1001               if (dstMask == 1) {
1002                  dstMask = 128;
1003                  d++;
1004                  *d = 0;
1005               }
1006               else {
1007                  dstMask = dstMask >> 1;
1008               }
1009            }
1010         }
1011      }
1012      src += width_in_bytes;
1013   }
1014}
1015
1016
1017/**
1018 * Apply various pixel transfer operations to an array of RGBA pixels
1019 * as indicated by the transferOps bitmask
1020 */
1021void
1022_mesa_apply_rgba_transfer_ops(GLcontext *ctx, GLbitfield transferOps,
1023                              GLuint n, GLfloat rgba[][4])
1024{
1025   /* scale & bias */
1026   if (transferOps & IMAGE_SCALE_BIAS_BIT) {
1027      _mesa_scale_and_bias_rgba(n, rgba,
1028                                ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
1029                                ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
1030                                ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
1031                                ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
1032   }
1033   /* color map lookup */
1034   if (transferOps & IMAGE_MAP_COLOR_BIT) {
1035      _mesa_map_rgba( ctx, n, rgba );
1036   }
1037   /* GL_COLOR_TABLE lookup */
1038   if (transferOps & IMAGE_COLOR_TABLE_BIT) {
1039      _mesa_lookup_rgba_float(&ctx->ColorTable, n, rgba);
1040   }
1041   /* convolution */
1042   if (transferOps & IMAGE_CONVOLUTION_BIT) {
1043      /* this has to be done in the calling code */
1044      _mesa_problem(ctx, "IMAGE_CONVOLUTION_BIT set in _mesa_apply_transfer_ops");
1045   }
1046   /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
1047   if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
1048      _mesa_scale_and_bias_rgba(n, rgba,
1049                                ctx->Pixel.PostConvolutionScale[RCOMP],
1050                                ctx->Pixel.PostConvolutionScale[GCOMP],
1051                                ctx->Pixel.PostConvolutionScale[BCOMP],
1052                                ctx->Pixel.PostConvolutionScale[ACOMP],
1053                                ctx->Pixel.PostConvolutionBias[RCOMP],
1054                                ctx->Pixel.PostConvolutionBias[GCOMP],
1055                                ctx->Pixel.PostConvolutionBias[BCOMP],
1056                                ctx->Pixel.PostConvolutionBias[ACOMP]);
1057   }
1058   /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
1059   if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
1060      _mesa_lookup_rgba_float(&ctx->PostConvolutionColorTable, n, rgba);
1061   }
1062   /* color matrix transform */
1063   if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
1064      _mesa_transform_rgba(ctx, n, rgba);
1065   }
1066   /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
1067   if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
1068      _mesa_lookup_rgba_float(&ctx->PostColorMatrixColorTable, n, rgba);
1069   }
1070   /* update histogram count */
1071   if (transferOps & IMAGE_HISTOGRAM_BIT) {
1072      _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
1073   }
1074   /* update min/max values */
1075   if (transferOps & IMAGE_MIN_MAX_BIT) {
1076      _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
1077   }
1078   /* clamping to [0,1] */
1079   if (transferOps & IMAGE_CLAMP_BIT) {
1080      GLuint i;
1081      for (i = 0; i < n; i++) {
1082         rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1083         rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1084         rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1085         rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1086      }
1087   }
1088}
1089
1090
1091/*
1092 * Apply color index shift and offset to an array of pixels.
1093 */
1094static void
1095shift_and_offset_ci( const GLcontext *ctx, GLuint n, GLuint indexes[] )
1096{
1097   GLint shift = ctx->Pixel.IndexShift;
1098   GLint offset = ctx->Pixel.IndexOffset;
1099   GLuint i;
1100   if (shift > 0) {
1101      for (i=0;i<n;i++) {
1102         indexes[i] = (indexes[i] << shift) + offset;
1103      }
1104   }
1105   else if (shift < 0) {
1106      shift = -shift;
1107      for (i=0;i<n;i++) {
1108         indexes[i] = (indexes[i] >> shift) + offset;
1109      }
1110   }
1111   else {
1112      for (i=0;i<n;i++) {
1113         indexes[i] = indexes[i] + offset;
1114      }
1115   }
1116}
1117
1118
1119
1120/**
1121 * Apply color index shift, offset and table lookup to an array
1122 * of color indexes;
1123 */
1124void
1125_mesa_apply_ci_transfer_ops(const GLcontext *ctx, GLbitfield transferOps,
1126                            GLuint n, GLuint indexes[])
1127{
1128   if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
1129      shift_and_offset_ci(ctx, n, indexes);
1130   }
1131   if (transferOps & IMAGE_MAP_COLOR_BIT) {
1132      const GLuint mask = ctx->Pixel.MapItoIsize - 1;
1133      GLuint i;
1134      for (i = 0; i < n; i++) {
1135         const GLuint j = indexes[i] & mask;
1136         indexes[i] = IROUND(ctx->Pixel.MapItoI[j]);
1137      }
1138   }
1139}
1140
1141
1142/**
1143 * Apply stencil index shift, offset and table lookup to an array
1144 * of stencil values.
1145 */
1146void
1147_mesa_apply_stencil_transfer_ops(const GLcontext *ctx, GLuint n,
1148                                 GLstencil stencil[])
1149{
1150   if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
1151      const GLint offset = ctx->Pixel.IndexOffset;
1152      GLint shift = ctx->Pixel.IndexShift;
1153      GLuint i;
1154      if (shift > 0) {
1155         for (i = 0; i < n; i++) {
1156            stencil[i] = (stencil[i] << shift) + offset;
1157         }
1158      }
1159      else if (shift < 0) {
1160         shift = -shift;
1161         for (i = 0; i < n; i++) {
1162            stencil[i] = (stencil[i] >> shift) + offset;
1163         }
1164      }
1165      else {
1166         for (i = 0; i < n; i++) {
1167            stencil[i] = stencil[i] + offset;
1168         }
1169      }
1170   }
1171   if (ctx->Pixel.MapStencilFlag) {
1172      GLuint mask = ctx->Pixel.MapStoSsize - 1;
1173      GLuint i;
1174      for (i = 0; i < n; i++) {
1175         stencil[i] = ctx->Pixel.MapStoS[ stencil[i] & mask ];
1176      }
1177   }
1178}
1179
1180
1181/**
1182 * Used to pack an array [][4] of RGBA float colors as specified
1183 * by the dstFormat, dstType and dstPacking.  Used by glReadPixels,
1184 * glGetConvolutionFilter(), etc.
1185 * Incoming colors will be clamped to [0,1] if needed.
1186 * Note: the rgba values will be modified by this function when any pixel
1187 * transfer ops are enabled.
1188 */
1189void
1190_mesa_pack_rgba_span_float(GLcontext *ctx, GLuint n, GLfloat rgba[][4],
1191                           GLenum dstFormat, GLenum dstType,
1192                           GLvoid *dstAddr,
1193                           const struct gl_pixelstore_attrib *dstPacking,
1194                           GLbitfield transferOps)
1195{
1196   GLfloat luminance[MAX_WIDTH];
1197   const GLint comps = _mesa_components_in_format(dstFormat);
1198   GLuint i;
1199
1200   if (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE) {
1201      /* need to clamp to [0, 1] */
1202      transferOps |= IMAGE_CLAMP_BIT;
1203   }
1204
1205   if (transferOps) {
1206      _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
1207      if ((transferOps & IMAGE_MIN_MAX_BIT) && ctx->MinMax.Sink) {
1208         return;
1209      }
1210   }
1211
1212   if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
1213      /* compute luminance values */
1214      if (transferOps & IMAGE_RED_TO_LUMINANCE) {
1215         /* Luminance = Red (glGetTexImage) */
1216         for (i = 0; i < n; i++) {
1217            luminance[i] = rgba[i][RCOMP];
1218         }
1219      }
1220      else {
1221         /* Luminance = Red + Green + Blue (glReadPixels) */
1222         if (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE) {
1223            for (i = 0; i < n; i++) {
1224               GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1225               luminance[i] = CLAMP(sum, 0.0F, 1.0F);
1226            }
1227         }
1228         else {
1229            for (i = 0; i < n; i++) {
1230               luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1231            }
1232         }
1233      }
1234   }
1235
1236   /*
1237    * Pack/store the pixels.  Ugh!  Lots of cases!!!
1238    */
1239   switch (dstType) {
1240      case GL_UNSIGNED_BYTE:
1241         {
1242            GLubyte *dst = (GLubyte *) dstAddr;
1243            switch (dstFormat) {
1244               case GL_RED:
1245                  for (i=0;i<n;i++)
1246                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1247                  break;
1248               case GL_GREEN:
1249                  for (i=0;i<n;i++)
1250                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1251                  break;
1252               case GL_BLUE:
1253                  for (i=0;i<n;i++)
1254                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1255                  break;
1256               case GL_ALPHA:
1257                  for (i=0;i<n;i++)
1258                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1259                  break;
1260               case GL_LUMINANCE:
1261                  for (i=0;i<n;i++)
1262                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1263                  break;
1264               case GL_LUMINANCE_ALPHA:
1265                  for (i=0;i<n;i++) {
1266                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1267                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1268                  }
1269                  break;
1270               case GL_RGB:
1271                  for (i=0;i<n;i++) {
1272                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1273                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1274                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1275                  }
1276                  break;
1277               case GL_RGBA:
1278                  for (i=0;i<n;i++) {
1279                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1280                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1281                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1282                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1283                  }
1284                  break;
1285               case GL_BGR:
1286                  for (i=0;i<n;i++) {
1287                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1288                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1289                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1290                  }
1291                  break;
1292               case GL_BGRA:
1293                  for (i=0;i<n;i++) {
1294                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1295                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1296                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1297                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1298                  }
1299                  break;
1300               case GL_ABGR_EXT:
1301                  for (i=0;i<n;i++) {
1302                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1303                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1304                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1305                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1306                  }
1307                  break;
1308               default:
1309                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1310            }
1311         }
1312         break;
1313      case GL_BYTE:
1314         {
1315            GLbyte *dst = (GLbyte *) dstAddr;
1316            switch (dstFormat) {
1317               case GL_RED:
1318                  for (i=0;i<n;i++)
1319                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1320                  break;
1321               case GL_GREEN:
1322                  for (i=0;i<n;i++)
1323                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1324                  break;
1325               case GL_BLUE:
1326                  for (i=0;i<n;i++)
1327                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1328                  break;
1329               case GL_ALPHA:
1330                  for (i=0;i<n;i++)
1331                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1332                  break;
1333               case GL_LUMINANCE:
1334                  for (i=0;i<n;i++)
1335                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
1336                  break;
1337               case GL_LUMINANCE_ALPHA:
1338                  for (i=0;i<n;i++) {
1339                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1340                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1341                  }
1342                  break;
1343               case GL_RGB:
1344                  for (i=0;i<n;i++) {
1345                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1346                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1347                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1348                  }
1349                  break;
1350               case GL_RGBA:
1351                  for (i=0;i<n;i++) {
1352                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1353                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1354                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1355                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1356                  }
1357                  break;
1358               case GL_BGR:
1359                  for (i=0;i<n;i++) {
1360                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1361                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1362                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1363                  }
1364                  break;
1365               case GL_BGRA:
1366                  for (i=0;i<n;i++) {
1367                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1368                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1369                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1370                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1371                  }
1372		  break;
1373               case GL_ABGR_EXT:
1374                  for (i=0;i<n;i++) {
1375                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1376                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1377                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1378                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1379                  }
1380                  break;
1381               default:
1382                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1383            }
1384         }
1385         break;
1386      case GL_UNSIGNED_SHORT:
1387         {
1388            GLushort *dst = (GLushort *) dstAddr;
1389            switch (dstFormat) {
1390               case GL_RED:
1391                  for (i=0;i<n;i++)
1392                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
1393                  break;
1394               case GL_GREEN:
1395                  for (i=0;i<n;i++)
1396                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
1397                  break;
1398               case GL_BLUE:
1399                  for (i=0;i<n;i++)
1400                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
1401                  break;
1402               case GL_ALPHA:
1403                  for (i=0;i<n;i++)
1404                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
1405                  break;
1406               case GL_LUMINANCE:
1407                  for (i=0;i<n;i++)
1408                     UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
1409                  break;
1410               case GL_LUMINANCE_ALPHA:
1411                  for (i=0;i<n;i++) {
1412                     UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
1413                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
1414                  }
1415                  break;
1416               case GL_RGB:
1417                  for (i=0;i<n;i++) {
1418                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
1419                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1420                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
1421                  }
1422                  break;
1423               case GL_RGBA:
1424                  for (i=0;i<n;i++) {
1425                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
1426                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1427                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
1428                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1429                  }
1430                  break;
1431               case GL_BGR:
1432                  for (i=0;i<n;i++) {
1433                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
1434                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1435                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
1436                  }
1437                  break;
1438               case GL_BGRA:
1439                  for (i=0;i<n;i++) {
1440                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
1441                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1442                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
1443                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1444                  }
1445                  break;
1446               case GL_ABGR_EXT:
1447                  for (i=0;i<n;i++) {
1448                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
1449                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
1450                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
1451                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
1452                  }
1453                  break;
1454               default:
1455                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1456            }
1457            if (dstPacking->SwapBytes) {
1458               _mesa_swap2( (GLushort *) dst, n * comps);
1459            }
1460         }
1461         break;
1462      case GL_SHORT:
1463         {
1464            GLshort *dst = (GLshort *) dstAddr;
1465            switch (dstFormat) {
1466               case GL_RED:
1467                  for (i=0;i<n;i++)
1468                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1469                  break;
1470               case GL_GREEN:
1471                  for (i=0;i<n;i++)
1472                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1473                  break;
1474               case GL_BLUE:
1475                  for (i=0;i<n;i++)
1476                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1477                  break;
1478               case GL_ALPHA:
1479                  for (i=0;i<n;i++)
1480                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1481                  break;
1482               case GL_LUMINANCE:
1483                  for (i=0;i<n;i++)
1484                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
1485                  break;
1486               case GL_LUMINANCE_ALPHA:
1487                  for (i=0;i<n;i++) {
1488                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1489                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1490                  }
1491                  break;
1492               case GL_RGB:
1493                  for (i=0;i<n;i++) {
1494                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1495                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1496                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1497                  }
1498                  break;
1499               case GL_RGBA:
1500                  for (i=0;i<n;i++) {
1501                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1502                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1503                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1504                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1505                  }
1506                  break;
1507               case GL_BGR:
1508                  for (i=0;i<n;i++) {
1509                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1510                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1511                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1512                  }
1513                  break;
1514               case GL_BGRA:
1515                  for (i=0;i<n;i++) {
1516                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1517                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1518                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1519                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1520                  }
1521		  break;
1522               case GL_ABGR_EXT:
1523                  for (i=0;i<n;i++) {
1524                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1525                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1526                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1527                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1528                  }
1529                  break;
1530               default:
1531                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1532            }
1533            if (dstPacking->SwapBytes) {
1534               _mesa_swap2( (GLushort *) dst, n * comps );
1535            }
1536         }
1537         break;
1538      case GL_UNSIGNED_INT:
1539         {
1540            GLuint *dst = (GLuint *) dstAddr;
1541            switch (dstFormat) {
1542               case GL_RED:
1543                  for (i=0;i<n;i++)
1544                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1545                  break;
1546               case GL_GREEN:
1547                  for (i=0;i<n;i++)
1548                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1549                  break;
1550               case GL_BLUE:
1551                  for (i=0;i<n;i++)
1552                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1553                  break;
1554               case GL_ALPHA:
1555                  for (i=0;i<n;i++)
1556                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1557                  break;
1558               case GL_LUMINANCE:
1559                  for (i=0;i<n;i++)
1560                     dst[i] = FLOAT_TO_UINT(luminance[i]);
1561                  break;
1562               case GL_LUMINANCE_ALPHA:
1563                  for (i=0;i<n;i++) {
1564                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
1565                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1566                  }
1567                  break;
1568               case GL_RGB:
1569                  for (i=0;i<n;i++) {
1570                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1571                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1572                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1573                  }
1574                  break;
1575               case GL_RGBA:
1576                  for (i=0;i<n;i++) {
1577                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1578                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1579                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1580                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1581                  }
1582                  break;
1583               case GL_BGR:
1584                  for (i=0;i<n;i++) {
1585                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1586                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1587                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1588                  }
1589                  break;
1590               case GL_BGRA:
1591                  for (i=0;i<n;i++) {
1592                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1593                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1594                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1595                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1596                  }
1597                  break;
1598               case GL_ABGR_EXT:
1599                  for (i=0;i<n;i++) {
1600                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1601                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1602                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1603                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1604                  }
1605                  break;
1606               default:
1607                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1608            }
1609            if (dstPacking->SwapBytes) {
1610               _mesa_swap4( (GLuint *) dst, n * comps );
1611            }
1612         }
1613         break;
1614      case GL_INT:
1615         {
1616            GLint *dst = (GLint *) dstAddr;
1617            switch (dstFormat) {
1618               case GL_RED:
1619                  for (i=0;i<n;i++)
1620                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
1621                  break;
1622               case GL_GREEN:
1623                  for (i=0;i<n;i++)
1624                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
1625                  break;
1626               case GL_BLUE:
1627                  for (i=0;i<n;i++)
1628                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
1629                  break;
1630               case GL_ALPHA:
1631                  for (i=0;i<n;i++)
1632                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
1633                  break;
1634               case GL_LUMINANCE:
1635                  for (i=0;i<n;i++)
1636                     dst[i] = FLOAT_TO_INT(luminance[i]);
1637                  break;
1638               case GL_LUMINANCE_ALPHA:
1639                  for (i=0;i<n;i++) {
1640                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
1641                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
1642                  }
1643                  break;
1644               case GL_RGB:
1645                  for (i=0;i<n;i++) {
1646                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1647                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1648                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1649                  }
1650                  break;
1651               case GL_RGBA:
1652                  for (i=0;i<n;i++) {
1653                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1654                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1655                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1656                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1657                  }
1658                  break;
1659               case GL_BGR:
1660                  for (i=0;i<n;i++) {
1661                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1662                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1663                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1664                  }
1665                  break;
1666               case GL_BGRA:
1667                  for (i=0;i<n;i++) {
1668                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1669                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1670                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1671                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1672                  }
1673                  break;
1674               case GL_ABGR_EXT:
1675                  for (i=0;i<n;i++) {
1676                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
1677                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
1678                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
1679                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
1680                  }
1681                  break;
1682               default:
1683                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1684            }
1685            if (dstPacking->SwapBytes) {
1686               _mesa_swap4( (GLuint *) dst, n * comps );
1687            }
1688         }
1689         break;
1690      case GL_FLOAT:
1691         {
1692            GLfloat *dst = (GLfloat *) dstAddr;
1693            switch (dstFormat) {
1694               case GL_RED:
1695                  for (i=0;i<n;i++)
1696                     dst[i] = rgba[i][RCOMP];
1697                  break;
1698               case GL_GREEN:
1699                  for (i=0;i<n;i++)
1700                     dst[i] = rgba[i][GCOMP];
1701                  break;
1702               case GL_BLUE:
1703                  for (i=0;i<n;i++)
1704                     dst[i] = rgba[i][BCOMP];
1705                  break;
1706               case GL_ALPHA:
1707                  for (i=0;i<n;i++)
1708                     dst[i] = rgba[i][ACOMP];
1709                  break;
1710               case GL_LUMINANCE:
1711                  for (i=0;i<n;i++)
1712                     dst[i] = luminance[i];
1713                  break;
1714               case GL_LUMINANCE_ALPHA:
1715                  for (i=0;i<n;i++) {
1716                     dst[i*2+0] = luminance[i];
1717                     dst[i*2+1] = rgba[i][ACOMP];
1718                  }
1719                  break;
1720               case GL_RGB:
1721                  for (i=0;i<n;i++) {
1722                     dst[i*3+0] = rgba[i][RCOMP];
1723                     dst[i*3+1] = rgba[i][GCOMP];
1724                     dst[i*3+2] = rgba[i][BCOMP];
1725                  }
1726                  break;
1727               case GL_RGBA:
1728                  for (i=0;i<n;i++) {
1729                     dst[i*4+0] = rgba[i][RCOMP];
1730                     dst[i*4+1] = rgba[i][GCOMP];
1731                     dst[i*4+2] = rgba[i][BCOMP];
1732                     dst[i*4+3] = rgba[i][ACOMP];
1733                  }
1734                  break;
1735               case GL_BGR:
1736                  for (i=0;i<n;i++) {
1737                     dst[i*3+0] = rgba[i][BCOMP];
1738                     dst[i*3+1] = rgba[i][GCOMP];
1739                     dst[i*3+2] = rgba[i][RCOMP];
1740                  }
1741                  break;
1742               case GL_BGRA:
1743                  for (i=0;i<n;i++) {
1744                     dst[i*4+0] = rgba[i][BCOMP];
1745                     dst[i*4+1] = rgba[i][GCOMP];
1746                     dst[i*4+2] = rgba[i][RCOMP];
1747                     dst[i*4+3] = rgba[i][ACOMP];
1748                  }
1749                  break;
1750               case GL_ABGR_EXT:
1751                  for (i=0;i<n;i++) {
1752                     dst[i*4+0] = rgba[i][ACOMP];
1753                     dst[i*4+1] = rgba[i][BCOMP];
1754                     dst[i*4+2] = rgba[i][GCOMP];
1755                     dst[i*4+3] = rgba[i][RCOMP];
1756                  }
1757                  break;
1758               default:
1759                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1760            }
1761            if (dstPacking->SwapBytes) {
1762               _mesa_swap4( (GLuint *) dst, n * comps );
1763            }
1764         }
1765         break;
1766      case GL_HALF_FLOAT_ARB:
1767         {
1768            GLhalfARB *dst = (GLhalfARB *) dstAddr;
1769            switch (dstFormat) {
1770               case GL_RED:
1771                  for (i=0;i<n;i++)
1772                     dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
1773                  break;
1774               case GL_GREEN:
1775                  for (i=0;i<n;i++)
1776                     dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
1777                  break;
1778               case GL_BLUE:
1779                  for (i=0;i<n;i++)
1780                     dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
1781                  break;
1782               case GL_ALPHA:
1783                  for (i=0;i<n;i++)
1784                     dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
1785                  break;
1786               case GL_LUMINANCE:
1787                  for (i=0;i<n;i++)
1788                     dst[i] = _mesa_float_to_half(luminance[i]);
1789                  break;
1790               case GL_LUMINANCE_ALPHA:
1791                  for (i=0;i<n;i++) {
1792                     dst[i*2+0] = _mesa_float_to_half(luminance[i]);
1793                     dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
1794                  }
1795                  break;
1796               case GL_RGB:
1797                  for (i=0;i<n;i++) {
1798                     dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
1799                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1800                     dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
1801                  }
1802                  break;
1803               case GL_RGBA:
1804                  for (i=0;i<n;i++) {
1805                     dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
1806                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1807                     dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
1808                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
1809                  }
1810                  break;
1811               case GL_BGR:
1812                  for (i=0;i<n;i++) {
1813                     dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
1814                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1815                     dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
1816                  }
1817                  break;
1818               case GL_BGRA:
1819                  for (i=0;i<n;i++) {
1820                     dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
1821                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1822                     dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
1823                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
1824                  }
1825                  break;
1826               case GL_ABGR_EXT:
1827                  for (i=0;i<n;i++) {
1828                     dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
1829                     dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
1830                     dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
1831                     dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
1832                  }
1833                  break;
1834               default:
1835                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1836            }
1837            if (dstPacking->SwapBytes) {
1838               _mesa_swap2( (GLushort *) dst, n * comps );
1839            }
1840         }
1841         break;
1842      case GL_UNSIGNED_BYTE_3_3_2:
1843         if (dstFormat == GL_RGB) {
1844            GLubyte *dst = (GLubyte *) dstAddr;
1845            for (i=0;i<n;i++) {
1846               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F)) << 5)
1847                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 2)
1848                      | (((GLint) (rgba[i][BCOMP] * 3.0F))     );
1849            }
1850         }
1851         break;
1852      case GL_UNSIGNED_BYTE_2_3_3_REV:
1853         if (dstFormat == GL_RGB) {
1854            GLubyte *dst = (GLubyte *) dstAddr;
1855            for (i=0;i<n;i++) {
1856               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F))     )
1857                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 3)
1858                      | (((GLint) (rgba[i][BCOMP] * 3.0F)) << 6);
1859            }
1860         }
1861         break;
1862      case GL_UNSIGNED_SHORT_5_6_5:
1863         if (dstFormat == GL_RGB) {
1864            GLushort *dst = (GLushort *) dstAddr;
1865            for (i=0;i<n;i++) {
1866               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1867                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1868                      | (((GLint) (rgba[i][BCOMP] * 31.0F))      );
1869            }
1870         }
1871         break;
1872      case GL_UNSIGNED_SHORT_5_6_5_REV:
1873         if (dstFormat == GL_RGB) {
1874            GLushort *dst = (GLushort *) dstAddr;
1875            for (i=0;i<n;i++) {
1876               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1877                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1878                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11);
1879            }
1880         }
1881         break;
1882      case GL_UNSIGNED_SHORT_4_4_4_4:
1883         if (dstFormat == GL_RGBA) {
1884            GLushort *dst = (GLushort *) dstAddr;
1885            for (i=0;i<n;i++) {
1886               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12)
1887                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1888                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
1889                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1890            }
1891         }
1892         else if (dstFormat == GL_BGRA) {
1893            GLushort *dst = (GLushort *) dstAddr;
1894            for (i=0;i<n;i++) {
1895               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F)) << 12)
1896                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1897                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  4)
1898                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1899            }
1900         }
1901         else if (dstFormat == GL_ABGR_EXT) {
1902            GLushort *dst = (GLushort *) dstAddr;
1903            for (i=0;i<n;i++) {
1904               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12)
1905                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
1906                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1907                      | (((GLint) (rgba[i][RCOMP] * 15.0F))      );
1908            }
1909         }
1910         break;
1911      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1912         if (dstFormat == GL_RGBA) {
1913            GLushort *dst = (GLushort *) dstAddr;
1914            for (i=0;i<n;i++) {
1915               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F))      )
1916                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1917                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
1918                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1919            }
1920         }
1921         else if (dstFormat == GL_BGRA) {
1922            GLushort *dst = (GLushort *) dstAddr;
1923            for (i=0;i<n;i++) {
1924               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F))      )
1925                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1926                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  8)
1927                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1928            }
1929         }
1930         else if (dstFormat == GL_ABGR_EXT) {
1931            GLushort *dst = (GLushort *) dstAddr;
1932            for (i=0;i<n;i++) {
1933               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F))      )
1934                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
1935                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1936                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12);
1937            }
1938         }
1939         break;
1940      case GL_UNSIGNED_SHORT_5_5_5_1:
1941         if (dstFormat == GL_RGBA) {
1942            GLushort *dst = (GLushort *) dstAddr;
1943            for (i=0;i<n;i++) {
1944               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1945                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1946                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  1)
1947                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1948            }
1949         }
1950         else if (dstFormat == GL_BGRA) {
1951            GLushort *dst = (GLushort *) dstAddr;
1952            for (i=0;i<n;i++) {
1953               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11)
1954                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1955                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) <<  1)
1956                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1957            }
1958         }
1959         else if (dstFormat == GL_ABGR_EXT) {
1960            GLushort *dst = (GLushort *) dstAddr;
1961            for (i=0;i<n;i++) {
1962               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F)) << 11)
1963                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  6)
1964                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  1)
1965                      | (((GLint) (rgba[i][RCOMP] *  1.0F))      );
1966            }
1967         }
1968         break;
1969      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1970         if (dstFormat == GL_RGBA) {
1971            GLushort *dst = (GLushort *) dstAddr;
1972            for (i=0;i<n;i++) {
1973               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1974                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1975                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 10)
1976                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1977            }
1978         }
1979         else if (dstFormat == GL_BGRA) {
1980            GLushort *dst = (GLushort *) dstAddr;
1981            for (i=0;i<n;i++) {
1982               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F))      )
1983                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1984                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) << 10)
1985                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1986            }
1987         }
1988         else if (dstFormat == GL_ABGR_EXT) {
1989            GLushort *dst = (GLushort *) dstAddr;
1990            for (i=0;i<n;i++) {
1991               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F))      )
1992                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  5)
1993                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) << 10)
1994                      | (((GLint) (rgba[i][RCOMP] *  1.0F)) << 15);
1995            }
1996         }
1997         break;
1998      case GL_UNSIGNED_INT_8_8_8_8:
1999         if (dstFormat == GL_RGBA) {
2000            GLuint *dst = (GLuint *) dstAddr;
2001            for (i=0;i<n;i++) {
2002               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24)
2003                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
2004                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
2005                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
2006            }
2007         }
2008         else if (dstFormat == GL_BGRA) {
2009            GLuint *dst = (GLuint *) dstAddr;
2010            for (i=0;i<n;i++) {
2011               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 24)
2012                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
2013                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) <<  8)
2014                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
2015            }
2016         }
2017         else if (dstFormat == GL_ABGR_EXT) {
2018            GLuint *dst = (GLuint *) dstAddr;
2019            for (i=0;i<n;i++) {
2020               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24)
2021                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
2022                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
2023                      | (((GLuint) (rgba[i][RCOMP] * 255.0F))      );
2024            }
2025         }
2026         break;
2027      case GL_UNSIGNED_INT_8_8_8_8_REV:
2028         if (dstFormat == GL_RGBA) {
2029            GLuint *dst = (GLuint *) dstAddr;
2030            for (i=0;i<n;i++) {
2031               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F))      )
2032                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
2033                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
2034                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
2035            }
2036         }
2037         else if (dstFormat == GL_BGRA) {
2038            GLuint *dst = (GLuint *) dstAddr;
2039            for (i=0;i<n;i++) {
2040               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F))      )
2041                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
2042                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 16)
2043                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
2044            }
2045         }
2046         else if (dstFormat == GL_ABGR_EXT) {
2047            GLuint *dst = (GLuint *) dstAddr;
2048            for (i=0;i<n;i++) {
2049               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F))      )
2050                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
2051                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
2052                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24);
2053            }
2054         }
2055         break;
2056      case GL_UNSIGNED_INT_10_10_10_2:
2057         if (dstFormat == GL_RGBA) {
2058            GLuint *dst = (GLuint *) dstAddr;
2059            for (i=0;i<n;i++) {
2060               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 22)
2061                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
2062                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) <<  2)
2063                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
2064            }
2065         }
2066         else if (dstFormat == GL_BGRA) {
2067            GLuint *dst = (GLuint *) dstAddr;
2068            for (i=0;i<n;i++) {
2069               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 22)
2070                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
2071                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) <<  2)
2072                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
2073            }
2074         }
2075         else if (dstFormat == GL_ABGR_EXT) {
2076            GLuint *dst = (GLuint *) dstAddr;
2077            for (i=0;i<n;i++) {
2078               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F)) << 22)
2079                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 12)
2080                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) <<  2)
2081                      | (((GLuint) (rgba[i][RCOMP] *    3.0F))      );
2082            }
2083         }
2084         break;
2085      case GL_UNSIGNED_INT_2_10_10_10_REV:
2086         if (dstFormat == GL_RGBA) {
2087            GLuint *dst = (GLuint *) dstAddr;
2088            for (i=0;i<n;i++) {
2089               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F))      )
2090                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
2091                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 20)
2092                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
2093            }
2094         }
2095         else if (dstFormat == GL_BGRA) {
2096            GLuint *dst = (GLuint *) dstAddr;
2097            for (i=0;i<n;i++) {
2098               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F))      )
2099                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
2100                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 20)
2101                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
2102            }
2103         }
2104         else if (dstFormat == GL_ABGR_EXT) {
2105            GLuint *dst = (GLuint *) dstAddr;
2106            for (i=0;i<n;i++) {
2107               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F))      )
2108                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 10)
2109                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 20)
2110                      | (((GLuint) (rgba[i][RCOMP] *    3.0F)) << 30);
2111            }
2112         }
2113         break;
2114      default:
2115         _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2116   }
2117}
2118
2119
2120#define SWAP2BYTE(VALUE)			\
2121   {						\
2122      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2123      GLubyte tmp = bytes[0];			\
2124      bytes[0] = bytes[1];			\
2125      bytes[1] = tmp;				\
2126   }
2127
2128#define SWAP4BYTE(VALUE)			\
2129   {						\
2130      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2131      GLubyte tmp = bytes[0];			\
2132      bytes[0] = bytes[3];			\
2133      bytes[3] = tmp;				\
2134      tmp = bytes[1];				\
2135      bytes[1] = bytes[2];			\
2136      bytes[2] = tmp;				\
2137   }
2138
2139
2140static void
2141extract_uint_indexes(GLuint n, GLuint indexes[],
2142                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
2143                     const struct gl_pixelstore_attrib *unpack )
2144{
2145   ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2146
2147   ASSERT(srcType == GL_BITMAP ||
2148          srcType == GL_UNSIGNED_BYTE ||
2149          srcType == GL_BYTE ||
2150          srcType == GL_UNSIGNED_SHORT ||
2151          srcType == GL_SHORT ||
2152          srcType == GL_UNSIGNED_INT ||
2153          srcType == GL_INT ||
2154          srcType == GL_UNSIGNED_INT_24_8_EXT ||
2155          srcType == GL_HALF_FLOAT_ARB ||
2156          srcType == GL_FLOAT);
2157
2158   switch (srcType) {
2159      case GL_BITMAP:
2160         {
2161            GLubyte *ubsrc = (GLubyte *) src;
2162            if (unpack->LsbFirst) {
2163               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2164               GLuint i;
2165               for (i = 0; i < n; i++) {
2166                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2167                  if (mask == 128) {
2168                     mask = 1;
2169                     ubsrc++;
2170                  }
2171                  else {
2172                     mask = mask << 1;
2173                  }
2174               }
2175            }
2176            else {
2177               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2178               GLuint i;
2179               for (i = 0; i < n; i++) {
2180                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2181                  if (mask == 1) {
2182                     mask = 128;
2183                     ubsrc++;
2184                  }
2185                  else {
2186                     mask = mask >> 1;
2187                  }
2188               }
2189            }
2190         }
2191         break;
2192      case GL_UNSIGNED_BYTE:
2193         {
2194            GLuint i;
2195            const GLubyte *s = (const GLubyte *) src;
2196            for (i = 0; i < n; i++)
2197               indexes[i] = s[i];
2198         }
2199         break;
2200      case GL_BYTE:
2201         {
2202            GLuint i;
2203            const GLbyte *s = (const GLbyte *) src;
2204            for (i = 0; i < n; i++)
2205               indexes[i] = s[i];
2206         }
2207         break;
2208      case GL_UNSIGNED_SHORT:
2209         {
2210            GLuint i;
2211            const GLushort *s = (const GLushort *) src;
2212            if (unpack->SwapBytes) {
2213               for (i = 0; i < n; i++) {
2214                  GLushort value = s[i];
2215                  SWAP2BYTE(value);
2216                  indexes[i] = value;
2217               }
2218            }
2219            else {
2220               for (i = 0; i < n; i++)
2221                  indexes[i] = s[i];
2222            }
2223         }
2224         break;
2225      case GL_SHORT:
2226         {
2227            GLuint i;
2228            const GLshort *s = (const GLshort *) src;
2229            if (unpack->SwapBytes) {
2230               for (i = 0; i < n; i++) {
2231                  GLshort value = s[i];
2232                  SWAP2BYTE(value);
2233                  indexes[i] = value;
2234               }
2235            }
2236            else {
2237               for (i = 0; i < n; i++)
2238                  indexes[i] = s[i];
2239            }
2240         }
2241         break;
2242      case GL_UNSIGNED_INT:
2243         {
2244            GLuint i;
2245            const GLuint *s = (const GLuint *) src;
2246            if (unpack->SwapBytes) {
2247               for (i = 0; i < n; i++) {
2248                  GLuint value = s[i];
2249                  SWAP4BYTE(value);
2250                  indexes[i] = value;
2251               }
2252            }
2253            else {
2254               for (i = 0; i < n; i++)
2255                  indexes[i] = s[i];
2256            }
2257         }
2258         break;
2259      case GL_INT:
2260         {
2261            GLuint i;
2262            const GLint *s = (const GLint *) src;
2263            if (unpack->SwapBytes) {
2264               for (i = 0; i < n; i++) {
2265                  GLint value = s[i];
2266                  SWAP4BYTE(value);
2267                  indexes[i] = value;
2268               }
2269            }
2270            else {
2271               for (i = 0; i < n; i++)
2272                  indexes[i] = s[i];
2273            }
2274         }
2275         break;
2276      case GL_FLOAT:
2277         {
2278            GLuint i;
2279            const GLfloat *s = (const GLfloat *) src;
2280            if (unpack->SwapBytes) {
2281               for (i = 0; i < n; i++) {
2282                  GLfloat value = s[i];
2283                  SWAP4BYTE(value);
2284                  indexes[i] = (GLuint) value;
2285               }
2286            }
2287            else {
2288               for (i = 0; i < n; i++)
2289                  indexes[i] = (GLuint) s[i];
2290            }
2291         }
2292         break;
2293      case GL_HALF_FLOAT_ARB:
2294         {
2295            GLuint i;
2296            const GLhalfARB *s = (const GLhalfARB *) src;
2297            if (unpack->SwapBytes) {
2298               for (i = 0; i < n; i++) {
2299                  GLhalfARB value = s[i];
2300                  SWAP2BYTE(value);
2301                  indexes[i] = (GLuint) _mesa_half_to_float(value);
2302               }
2303            }
2304            else {
2305               for (i = 0; i < n; i++)
2306                  indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
2307            }
2308         }
2309         break;
2310      case GL_UNSIGNED_INT_24_8_EXT:
2311         {
2312            GLuint i;
2313            const GLuint *s = (const GLuint *) src;
2314            if (unpack->SwapBytes) {
2315               for (i = 0; i < n; i++) {
2316                  GLuint value = s[i];
2317                  SWAP4BYTE(value);
2318                  indexes[i] = value & 0xff;  /* lower 8 bits */
2319               }
2320            }
2321            else {
2322               for (i = 0; i < n; i++)
2323                  indexes[i] = s[i] & 0xfff;  /* lower 8 bits */
2324            }
2325         }
2326         break;
2327
2328      default:
2329         _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
2330         return;
2331   }
2332}
2333
2334
2335/*
2336 * This function extracts floating point RGBA values from arbitrary
2337 * image data.  srcFormat and srcType are the format and type parameters
2338 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2339 *
2340 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2341 * implements the "Conversion to floating point", "Conversion to RGB",
2342 * and "Final Expansion to RGBA" operations.
2343 *
2344 * Args:  n - number of pixels
2345 *        rgba - output colors
2346 *        srcFormat - format of incoming data
2347 *        srcType - data type of incoming data
2348 *        src - source data pointer
2349 *        swapBytes - perform byteswapping of incoming data?
2350 */
2351static void
2352extract_float_rgba(GLuint n, GLfloat rgba[][4],
2353                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
2354                   GLboolean swapBytes)
2355{
2356   GLint redIndex, greenIndex, blueIndex, alphaIndex;
2357   GLint stride;
2358   GLint rComp, bComp, gComp, aComp;
2359
2360   ASSERT(srcFormat == GL_RED ||
2361          srcFormat == GL_GREEN ||
2362          srcFormat == GL_BLUE ||
2363          srcFormat == GL_ALPHA ||
2364          srcFormat == GL_LUMINANCE ||
2365          srcFormat == GL_LUMINANCE_ALPHA ||
2366          srcFormat == GL_INTENSITY ||
2367          srcFormat == GL_RGB ||
2368          srcFormat == GL_BGR ||
2369          srcFormat == GL_RGBA ||
2370          srcFormat == GL_BGRA ||
2371          srcFormat == GL_ABGR_EXT);
2372
2373   ASSERT(srcType == GL_UNSIGNED_BYTE ||
2374          srcType == GL_BYTE ||
2375          srcType == GL_UNSIGNED_SHORT ||
2376          srcType == GL_SHORT ||
2377          srcType == GL_UNSIGNED_INT ||
2378          srcType == GL_INT ||
2379          srcType == GL_HALF_FLOAT_ARB ||
2380          srcType == GL_FLOAT ||
2381          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2382          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2383          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2384          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2385          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2386          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2387          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2388          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2389          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2390          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2391          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2392          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2393
2394   rComp = gComp = bComp = aComp = -1;
2395
2396   switch (srcFormat) {
2397      case GL_RED:
2398         redIndex = 0;
2399         greenIndex = blueIndex = alphaIndex = -1;
2400         stride = 1;
2401         break;
2402      case GL_GREEN:
2403         greenIndex = 0;
2404         redIndex = blueIndex = alphaIndex = -1;
2405         stride = 1;
2406         break;
2407      case GL_BLUE:
2408         blueIndex = 0;
2409         redIndex = greenIndex = alphaIndex = -1;
2410         stride = 1;
2411         break;
2412      case GL_ALPHA:
2413         redIndex = greenIndex = blueIndex = -1;
2414         alphaIndex = 0;
2415         stride = 1;
2416         break;
2417      case GL_LUMINANCE:
2418         redIndex = greenIndex = blueIndex = 0;
2419         alphaIndex = -1;
2420         stride = 1;
2421         break;
2422      case GL_LUMINANCE_ALPHA:
2423         redIndex = greenIndex = blueIndex = 0;
2424         alphaIndex = 1;
2425         stride = 2;
2426         break;
2427      case GL_INTENSITY:
2428         redIndex = greenIndex = blueIndex = alphaIndex = 0;
2429         stride = 1;
2430         break;
2431      case GL_RGB:
2432         redIndex = 0;
2433         greenIndex = 1;
2434         blueIndex = 2;
2435         alphaIndex = -1;
2436         rComp = 0;
2437         gComp = 1;
2438         bComp = 2;
2439         aComp = 3;
2440         stride = 3;
2441         break;
2442      case GL_BGR:
2443         redIndex = 2;
2444         greenIndex = 1;
2445         blueIndex = 0;
2446         alphaIndex = -1;
2447         rComp = 2;
2448         gComp = 1;
2449         bComp = 0;
2450         aComp = 3;
2451         stride = 3;
2452         break;
2453      case GL_RGBA:
2454         redIndex = 0;
2455         greenIndex = 1;
2456         blueIndex = 2;
2457         alphaIndex = 3;
2458         rComp = 0;
2459         gComp = 1;
2460         bComp = 2;
2461         aComp = 3;
2462         stride = 4;
2463         break;
2464      case GL_BGRA:
2465         redIndex = 2;
2466         greenIndex = 1;
2467         blueIndex = 0;
2468         alphaIndex = 3;
2469         rComp = 2;
2470         gComp = 1;
2471         bComp = 0;
2472         aComp = 3;
2473         stride = 4;
2474         break;
2475      case GL_ABGR_EXT:
2476         redIndex = 3;
2477         greenIndex = 2;
2478         blueIndex = 1;
2479         alphaIndex = 0;
2480         rComp = 3;
2481         gComp = 2;
2482         bComp = 1;
2483         aComp = 0;
2484         stride = 4;
2485         break;
2486      default:
2487         _mesa_problem(NULL, "bad srcFormat in extract float data");
2488         return;
2489   }
2490
2491
2492#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)		\
2493   if ((INDEX) < 0) {							\
2494      GLuint i;								\
2495      for (i = 0; i < n; i++) {						\
2496         rgba[i][CHANNEL] = DEFAULT;					\
2497      }									\
2498   }									\
2499   else if (swapBytes) {						\
2500      const TYPE *s = (const TYPE *) src;				\
2501      GLuint i;								\
2502      for (i = 0; i < n; i++) {						\
2503         TYPE value = s[INDEX];						\
2504         if (sizeof(TYPE) == 2) {					\
2505            SWAP2BYTE(value);						\
2506         }								\
2507         else if (sizeof(TYPE) == 4) {					\
2508            SWAP4BYTE(value);						\
2509         }								\
2510         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);		\
2511         s += stride;							\
2512      }									\
2513   }									\
2514   else {								\
2515      const TYPE *s = (const TYPE *) src;				\
2516      GLuint i;								\
2517      for (i = 0; i < n; i++) {						\
2518         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);		\
2519         s += stride;							\
2520      }									\
2521   }
2522
2523   switch (srcType) {
2524      case GL_UNSIGNED_BYTE:
2525         PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2526         PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2527         PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2528         PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
2529         break;
2530      case GL_BYTE:
2531         PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2532         PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2533         PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2534         PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
2535         break;
2536      case GL_UNSIGNED_SHORT:
2537         PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2538         PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2539         PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2540         PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
2541         break;
2542      case GL_SHORT:
2543         PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2544         PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2545         PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2546         PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
2547         break;
2548      case GL_UNSIGNED_INT:
2549         PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2550         PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2551         PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2552         PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
2553         break;
2554      case GL_INT:
2555         PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
2556         PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
2557         PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
2558         PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
2559         break;
2560      case GL_FLOAT:
2561         PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
2562         PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
2563         PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
2564         PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
2565         break;
2566      case GL_HALF_FLOAT_ARB:
2567         PROCESS(redIndex,   RCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
2568         PROCESS(greenIndex, GCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
2569         PROCESS(blueIndex,  BCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
2570         PROCESS(alphaIndex, ACOMP, 1.0F, GLhalfARB, _mesa_half_to_float);
2571         break;
2572      case GL_UNSIGNED_BYTE_3_3_2:
2573         {
2574            const GLubyte *ubsrc = (const GLubyte *) src;
2575            GLuint i;
2576            for (i = 0; i < n; i ++) {
2577               GLubyte p = ubsrc[i];
2578               rgba[i][rComp] = ((p >> 5)      ) * (1.0F / 7.0F);
2579               rgba[i][gComp] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
2580               rgba[i][bComp] = ((p     ) & 0x3) * (1.0F / 3.0F);
2581               rgba[i][aComp] = 1.0F;
2582            }
2583         }
2584         break;
2585      case GL_UNSIGNED_BYTE_2_3_3_REV:
2586         {
2587            const GLubyte *ubsrc = (const GLubyte *) src;
2588            GLuint i;
2589            for (i = 0; i < n; i ++) {
2590               GLubyte p = ubsrc[i];
2591               rgba[i][rComp] = ((p     ) & 0x7) * (1.0F / 7.0F);
2592               rgba[i][gComp] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
2593               rgba[i][bComp] = ((p >> 6)      ) * (1.0F / 3.0F);
2594               rgba[i][aComp] = 1.0F;
2595            }
2596         }
2597         break;
2598      case GL_UNSIGNED_SHORT_5_6_5:
2599         if (swapBytes) {
2600            const GLushort *ussrc = (const GLushort *) src;
2601            GLuint i;
2602            for (i = 0; i < n; i ++) {
2603               GLushort p = ussrc[i];
2604               SWAP2BYTE(p);
2605               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2606               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2607               rgba[i][bComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2608               rgba[i][aComp] = 1.0F;
2609            }
2610         }
2611         else {
2612            const GLushort *ussrc = (const GLushort *) src;
2613            GLuint i;
2614            for (i = 0; i < n; i ++) {
2615               GLushort p = ussrc[i];
2616               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2617               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2618               rgba[i][bComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2619               rgba[i][aComp] = 1.0F;
2620            }
2621         }
2622         break;
2623      case GL_UNSIGNED_SHORT_5_6_5_REV:
2624         if (swapBytes) {
2625            const GLushort *ussrc = (const GLushort *) src;
2626            GLuint i;
2627            for (i = 0; i < n; i ++) {
2628               GLushort p = ussrc[i];
2629               SWAP2BYTE(p);
2630               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2631               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2632               rgba[i][bComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2633               rgba[i][aComp] = 1.0F;
2634            }
2635         }
2636         else {
2637            const GLushort *ussrc = (const GLushort *) src;
2638            GLuint i;
2639            for (i = 0; i < n; i ++) {
2640               GLushort p = ussrc[i];
2641               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2642               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2643               rgba[i][bComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2644               rgba[i][aComp] = 1.0F;
2645            }
2646         }
2647         break;
2648      case GL_UNSIGNED_SHORT_4_4_4_4:
2649         if (swapBytes) {
2650            const GLushort *ussrc = (const GLushort *) src;
2651            GLuint i;
2652            for (i = 0; i < n; i ++) {
2653               GLushort p = ussrc[i];
2654               SWAP2BYTE(p);
2655               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2656               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2657               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2658               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2659            }
2660         }
2661         else {
2662            const GLushort *ussrc = (const GLushort *) src;
2663            GLuint i;
2664            for (i = 0; i < n; i ++) {
2665               GLushort p = ussrc[i];
2666               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2667               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2668               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2669               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2670            }
2671         }
2672         break;
2673      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2674         if (swapBytes) {
2675            const GLushort *ussrc = (const GLushort *) src;
2676            GLuint i;
2677            for (i = 0; i < n; i ++) {
2678               GLushort p = ussrc[i];
2679               SWAP2BYTE(p);
2680               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2681               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2682               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2683               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2684            }
2685         }
2686         else {
2687            const GLushort *ussrc = (const GLushort *) src;
2688            GLuint i;
2689            for (i = 0; i < n; i ++) {
2690               GLushort p = ussrc[i];
2691               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2692               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2693               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2694               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2695            }
2696         }
2697         break;
2698      case GL_UNSIGNED_SHORT_5_5_5_1:
2699         if (swapBytes) {
2700            const GLushort *ussrc = (const GLushort *) src;
2701            GLuint i;
2702            for (i = 0; i < n; i ++) {
2703               GLushort p = ussrc[i];
2704               SWAP2BYTE(p);
2705               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2706               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2707               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2708               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2709            }
2710         }
2711         else {
2712            const GLushort *ussrc = (const GLushort *) src;
2713            GLuint i;
2714            for (i = 0; i < n; i ++) {
2715               GLushort p = ussrc[i];
2716               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2717               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2718               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2719               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2720            }
2721         }
2722         break;
2723      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2724         if (swapBytes) {
2725            const GLushort *ussrc = (const GLushort *) src;
2726            GLuint i;
2727            for (i = 0; i < n; i ++) {
2728               GLushort p = ussrc[i];
2729               SWAP2BYTE(p);
2730               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2731               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2732               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2733               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2734            }
2735         }
2736         else {
2737            const GLushort *ussrc = (const GLushort *) src;
2738            GLuint i;
2739            for (i = 0; i < n; i ++) {
2740               GLushort p = ussrc[i];
2741               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2742               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2743               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2744               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2745            }
2746         }
2747         break;
2748      case GL_UNSIGNED_INT_8_8_8_8:
2749         if (swapBytes) {
2750            const GLuint *uisrc = (const GLuint *) src;
2751            GLuint i;
2752            for (i = 0; i < n; i ++) {
2753               GLuint p = uisrc[i];
2754               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2755               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2756               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2757               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
2758            }
2759         }
2760         else {
2761            const GLuint *uisrc = (const GLuint *) src;
2762            GLuint i;
2763            for (i = 0; i < n; i ++) {
2764               GLuint p = uisrc[i];
2765               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
2766               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2767               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2768               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2769            }
2770         }
2771         break;
2772      case GL_UNSIGNED_INT_8_8_8_8_REV:
2773         if (swapBytes) {
2774            const GLuint *uisrc = (const GLuint *) src;
2775            GLuint i;
2776            for (i = 0; i < n; i ++) {
2777               GLuint p = uisrc[i];
2778               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
2779               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2780               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2781               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2782            }
2783         }
2784         else {
2785            const GLuint *uisrc = (const GLuint *) src;
2786            GLuint i;
2787            for (i = 0; i < n; i ++) {
2788               GLuint p = uisrc[i];
2789               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2790               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2791               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2792               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
2793            }
2794         }
2795         break;
2796      case GL_UNSIGNED_INT_10_10_10_2:
2797         if (swapBytes) {
2798            const GLuint *uisrc = (const GLuint *) src;
2799            GLuint i;
2800            for (i = 0; i < n; i ++) {
2801               GLuint p = uisrc[i];
2802               SWAP4BYTE(p);
2803               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2804               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2805               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2806               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2807            }
2808         }
2809         else {
2810            const GLuint *uisrc = (const GLuint *) src;
2811            GLuint i;
2812            for (i = 0; i < n; i ++) {
2813               GLuint p = uisrc[i];
2814               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2815               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2816               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2817               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2818            }
2819         }
2820         break;
2821      case GL_UNSIGNED_INT_2_10_10_10_REV:
2822         if (swapBytes) {
2823            const GLuint *uisrc = (const GLuint *) src;
2824            GLuint i;
2825            for (i = 0; i < n; i ++) {
2826               GLuint p = uisrc[i];
2827               SWAP4BYTE(p);
2828               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2829               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2830               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2831               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2832            }
2833         }
2834         else {
2835            const GLuint *uisrc = (const GLuint *) src;
2836            GLuint i;
2837            for (i = 0; i < n; i ++) {
2838               GLuint p = uisrc[i];
2839               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2840               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2841               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2842               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2843            }
2844         }
2845         break;
2846      default:
2847         _mesa_problem(NULL, "bad srcType in extract float data");
2848         break;
2849   }
2850}
2851
2852
2853/*
2854 * Unpack a row of color image data from a client buffer according to
2855 * the pixel unpacking parameters.
2856 * Return GLchan values in the specified dest image format.
2857 * This is used by glDrawPixels and glTexImage?D().
2858 * \param ctx - the context
2859 *         n - number of pixels in the span
2860 *         dstFormat - format of destination color array
2861 *         dest - the destination color array
2862 *         srcFormat - source image format
2863 *         srcType - source image  data type
2864 *         source - source image pointer
2865 *         srcPacking - pixel unpacking parameters
2866 *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
2867 *
2868 * XXX perhaps expand this to process whole images someday.
2869 */
2870void
2871_mesa_unpack_color_span_chan( GLcontext *ctx,
2872                              GLuint n, GLenum dstFormat, GLchan dest[],
2873                              GLenum srcFormat, GLenum srcType,
2874                              const GLvoid *source,
2875                              const struct gl_pixelstore_attrib *srcPacking,
2876                              GLbitfield transferOps )
2877{
2878   ASSERT(dstFormat == GL_ALPHA ||
2879          dstFormat == GL_LUMINANCE ||
2880          dstFormat == GL_LUMINANCE_ALPHA ||
2881          dstFormat == GL_INTENSITY ||
2882          dstFormat == GL_RGB ||
2883          dstFormat == GL_RGBA ||
2884          dstFormat == GL_COLOR_INDEX);
2885
2886   ASSERT(srcFormat == GL_RED ||
2887          srcFormat == GL_GREEN ||
2888          srcFormat == GL_BLUE ||
2889          srcFormat == GL_ALPHA ||
2890          srcFormat == GL_LUMINANCE ||
2891          srcFormat == GL_LUMINANCE_ALPHA ||
2892          srcFormat == GL_INTENSITY ||
2893          srcFormat == GL_RGB ||
2894          srcFormat == GL_BGR ||
2895          srcFormat == GL_RGBA ||
2896          srcFormat == GL_BGRA ||
2897          srcFormat == GL_ABGR_EXT ||
2898          srcFormat == GL_COLOR_INDEX);
2899
2900   ASSERT(srcType == GL_BITMAP ||
2901          srcType == GL_UNSIGNED_BYTE ||
2902          srcType == GL_BYTE ||
2903          srcType == GL_UNSIGNED_SHORT ||
2904          srcType == GL_SHORT ||
2905          srcType == GL_UNSIGNED_INT ||
2906          srcType == GL_INT ||
2907          srcType == GL_HALF_FLOAT_ARB ||
2908          srcType == GL_FLOAT ||
2909          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2910          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2911          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2912          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2913          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2914          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2915          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2916          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2917          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2918          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2919          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2920          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2921
2922   /* Try simple cases first */
2923   if (transferOps == 0) {
2924      if (srcType == CHAN_TYPE) {
2925         if (dstFormat == GL_RGBA) {
2926            if (srcFormat == GL_RGBA) {
2927               _mesa_memcpy( dest, source, n * 4 * sizeof(GLchan) );
2928               return;
2929            }
2930            else if (srcFormat == GL_RGB) {
2931               GLuint i;
2932               const GLchan *src = (const GLchan *) source;
2933               GLchan *dst = dest;
2934               for (i = 0; i < n; i++) {
2935                  dst[0] = src[0];
2936                  dst[1] = src[1];
2937                  dst[2] = src[2];
2938                  dst[3] = CHAN_MAX;
2939                  src += 3;
2940                  dst += 4;
2941               }
2942               return;
2943            }
2944         }
2945         else if (dstFormat == GL_RGB) {
2946            if (srcFormat == GL_RGB) {
2947               _mesa_memcpy( dest, source, n * 3 * sizeof(GLchan) );
2948               return;
2949            }
2950            else if (srcFormat == GL_RGBA) {
2951               GLuint i;
2952               const GLchan *src = (const GLchan *) source;
2953               GLchan *dst = dest;
2954               for (i = 0; i < n; i++) {
2955                  dst[0] = src[0];
2956                  dst[1] = src[1];
2957                  dst[2] = src[2];
2958                  src += 4;
2959                  dst += 3;
2960               }
2961               return;
2962            }
2963         }
2964         else if (dstFormat == srcFormat) {
2965            GLint comps = _mesa_components_in_format(srcFormat);
2966            assert(comps > 0);
2967            _mesa_memcpy( dest, source, n * comps * sizeof(GLchan) );
2968            return;
2969         }
2970      }
2971      /*
2972       * Common situation, loading 8bit RGBA/RGB source images
2973       * into 16/32 bit destination. (OSMesa16/32)
2974       */
2975      else if (srcType == GL_UNSIGNED_BYTE) {
2976         if (dstFormat == GL_RGBA) {
2977            if (srcFormat == GL_RGB) {
2978               GLuint i;
2979               const GLubyte *src = (const GLubyte *) source;
2980               GLchan *dst = dest;
2981               for (i = 0; i < n; i++) {
2982                  dst[0] = UBYTE_TO_CHAN(src[0]);
2983                  dst[1] = UBYTE_TO_CHAN(src[1]);
2984                  dst[2] = UBYTE_TO_CHAN(src[2]);
2985                  dst[3] = CHAN_MAX;
2986                  src += 3;
2987                  dst += 4;
2988               }
2989               return;
2990            }
2991            else if (srcFormat == GL_RGBA) {
2992               GLuint i;
2993               const GLubyte *src = (const GLubyte *) source;
2994               GLchan *dst = dest;
2995               for (i = 0; i < n; i++) {
2996                  dst[0] = UBYTE_TO_CHAN(src[0]);
2997                  dst[1] = UBYTE_TO_CHAN(src[1]);
2998                  dst[2] = UBYTE_TO_CHAN(src[2]);
2999                  dst[3] = UBYTE_TO_CHAN(src[3]);
3000                  src += 4;
3001                  dst += 4;
3002               }
3003               return;
3004             }
3005         }
3006         else if (dstFormat == GL_RGB) {
3007            if (srcFormat == GL_RGB) {
3008               GLuint i;
3009               const GLubyte *src = (const GLubyte *) source;
3010               GLchan *dst = dest;
3011               for (i = 0; i < n; i++) {
3012                  dst[0] = UBYTE_TO_CHAN(src[0]);
3013                  dst[1] = UBYTE_TO_CHAN(src[1]);
3014                  dst[2] = UBYTE_TO_CHAN(src[2]);
3015                  src += 3;
3016                  dst += 3;
3017               }
3018               return;
3019            }
3020            else if (srcFormat == GL_RGBA) {
3021               GLuint i;
3022               const GLubyte *src = (const GLubyte *) source;
3023               GLchan *dst = dest;
3024               for (i = 0; i < n; i++) {
3025                  dst[0] = UBYTE_TO_CHAN(src[0]);
3026                  dst[1] = UBYTE_TO_CHAN(src[1]);
3027                  dst[2] = UBYTE_TO_CHAN(src[2]);
3028                  src += 4;
3029                  dst += 3;
3030               }
3031               return;
3032            }
3033         }
3034      }
3035   }
3036
3037
3038   /* general solution begins here */
3039   {
3040      GLint dstComponents;
3041      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3042      GLint dstLuminanceIndex, dstIntensityIndex;
3043      GLfloat rgba[MAX_WIDTH][4];
3044
3045      dstComponents = _mesa_components_in_format( dstFormat );
3046      /* source & dest image formats should have been error checked by now */
3047      assert(dstComponents > 0);
3048
3049      /*
3050       * Extract image data and convert to RGBA floats
3051       */
3052      assert(n <= MAX_WIDTH);
3053      if (srcFormat == GL_COLOR_INDEX) {
3054         GLuint indexes[MAX_WIDTH];
3055         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3056                              srcPacking);
3057
3058         if (dstFormat == GL_COLOR_INDEX) {
3059            GLuint i;
3060            _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3061            /* convert to GLchan and return */
3062            for (i = 0; i < n; i++) {
3063               dest[i] = (GLchan) (indexes[i] & 0xff);
3064            }
3065            return;
3066         }
3067         else {
3068            /* Convert indexes to RGBA */
3069            if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3070               shift_and_offset_ci(ctx, n, indexes);
3071            }
3072            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3073         }
3074
3075         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3076          * with color indexes.
3077          */
3078         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3079      }
3080      else {
3081         /* non-color index data */
3082         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3083                            srcPacking->SwapBytes);
3084      }
3085
3086      /* Need to clamp if returning GLubytes or GLushorts */
3087#if CHAN_TYPE != GL_FLOAT
3088      transferOps |= IMAGE_CLAMP_BIT;
3089#endif
3090
3091      if (transferOps) {
3092         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3093      }
3094
3095      /* Now determine which color channels we need to produce.
3096       * And determine the dest index (offset) within each color tuple.
3097       */
3098      switch (dstFormat) {
3099         case GL_ALPHA:
3100            dstAlphaIndex = 0;
3101            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3102            dstLuminanceIndex = dstIntensityIndex = -1;
3103            break;
3104         case GL_LUMINANCE:
3105            dstLuminanceIndex = 0;
3106            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3107            dstIntensityIndex = -1;
3108            break;
3109         case GL_LUMINANCE_ALPHA:
3110            dstLuminanceIndex = 0;
3111            dstAlphaIndex = 1;
3112            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3113            dstIntensityIndex = -1;
3114            break;
3115         case GL_INTENSITY:
3116            dstIntensityIndex = 0;
3117            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3118            dstLuminanceIndex = -1;
3119            break;
3120         case GL_RGB:
3121            dstRedIndex = 0;
3122            dstGreenIndex = 1;
3123            dstBlueIndex = 2;
3124            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3125            break;
3126         case GL_RGBA:
3127            dstRedIndex = 0;
3128            dstGreenIndex = 1;
3129            dstBlueIndex = 2;
3130            dstAlphaIndex = 3;
3131            dstLuminanceIndex = dstIntensityIndex = -1;
3132            break;
3133         default:
3134            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_chan_span()");
3135            return;
3136      }
3137
3138
3139      /* Now return the GLchan data in the requested dstFormat */
3140
3141      if (dstRedIndex >= 0) {
3142         GLchan *dst = dest;
3143         GLuint i;
3144         for (i = 0; i < n; i++) {
3145            CLAMPED_FLOAT_TO_CHAN(dst[dstRedIndex], rgba[i][RCOMP]);
3146            dst += dstComponents;
3147         }
3148      }
3149
3150      if (dstGreenIndex >= 0) {
3151         GLchan *dst = dest;
3152         GLuint i;
3153         for (i = 0; i < n; i++) {
3154            CLAMPED_FLOAT_TO_CHAN(dst[dstGreenIndex], rgba[i][GCOMP]);
3155            dst += dstComponents;
3156         }
3157      }
3158
3159      if (dstBlueIndex >= 0) {
3160         GLchan *dst = dest;
3161         GLuint i;
3162         for (i = 0; i < n; i++) {
3163            CLAMPED_FLOAT_TO_CHAN(dst[dstBlueIndex], rgba[i][BCOMP]);
3164            dst += dstComponents;
3165         }
3166      }
3167
3168      if (dstAlphaIndex >= 0) {
3169         GLchan *dst = dest;
3170         GLuint i;
3171         for (i = 0; i < n; i++) {
3172            CLAMPED_FLOAT_TO_CHAN(dst[dstAlphaIndex], rgba[i][ACOMP]);
3173            dst += dstComponents;
3174         }
3175      }
3176
3177      if (dstIntensityIndex >= 0) {
3178         GLchan *dst = dest;
3179         GLuint i;
3180         assert(dstIntensityIndex == 0);
3181         assert(dstComponents == 1);
3182         for (i = 0; i < n; i++) {
3183            /* Intensity comes from red channel */
3184            CLAMPED_FLOAT_TO_CHAN(dst[i], rgba[i][RCOMP]);
3185         }
3186      }
3187
3188      if (dstLuminanceIndex >= 0) {
3189         GLchan *dst = dest;
3190         GLuint i;
3191         assert(dstLuminanceIndex == 0);
3192         for (i = 0; i < n; i++) {
3193            /* Luminance comes from red channel */
3194            CLAMPED_FLOAT_TO_CHAN(dst[0], rgba[i][RCOMP]);
3195            dst += dstComponents;
3196         }
3197      }
3198   }
3199}
3200
3201
3202/**
3203 * Same as _mesa_unpack_color_span_chan(), but return GLfloat data
3204 * instead of GLchan.
3205 */
3206void
3207_mesa_unpack_color_span_float( GLcontext *ctx,
3208                               GLuint n, GLenum dstFormat, GLfloat dest[],
3209                               GLenum srcFormat, GLenum srcType,
3210                               const GLvoid *source,
3211                               const struct gl_pixelstore_attrib *srcPacking,
3212                               GLbitfield transferOps )
3213{
3214   ASSERT(dstFormat == GL_ALPHA ||
3215          dstFormat == GL_LUMINANCE ||
3216          dstFormat == GL_LUMINANCE_ALPHA ||
3217          dstFormat == GL_INTENSITY ||
3218          dstFormat == GL_RGB ||
3219          dstFormat == GL_RGBA ||
3220          dstFormat == GL_COLOR_INDEX);
3221
3222   ASSERT(srcFormat == GL_RED ||
3223          srcFormat == GL_GREEN ||
3224          srcFormat == GL_BLUE ||
3225          srcFormat == GL_ALPHA ||
3226          srcFormat == GL_LUMINANCE ||
3227          srcFormat == GL_LUMINANCE_ALPHA ||
3228          srcFormat == GL_INTENSITY ||
3229          srcFormat == GL_RGB ||
3230          srcFormat == GL_BGR ||
3231          srcFormat == GL_RGBA ||
3232          srcFormat == GL_BGRA ||
3233          srcFormat == GL_ABGR_EXT ||
3234          srcFormat == GL_COLOR_INDEX);
3235
3236   ASSERT(srcType == GL_BITMAP ||
3237          srcType == GL_UNSIGNED_BYTE ||
3238          srcType == GL_BYTE ||
3239          srcType == GL_UNSIGNED_SHORT ||
3240          srcType == GL_SHORT ||
3241          srcType == GL_UNSIGNED_INT ||
3242          srcType == GL_INT ||
3243          srcType == GL_HALF_FLOAT_ARB ||
3244          srcType == GL_FLOAT ||
3245          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3246          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3247          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3248          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3249          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3250          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3251          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3252          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3253          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3254          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3255          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3256          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3257
3258   /* general solution, no special cases, yet */
3259   {
3260      GLint dstComponents;
3261      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3262      GLint dstLuminanceIndex, dstIntensityIndex;
3263      GLfloat rgba[MAX_WIDTH][4];
3264
3265      dstComponents = _mesa_components_in_format( dstFormat );
3266      /* source & dest image formats should have been error checked by now */
3267      assert(dstComponents > 0);
3268
3269      /*
3270       * Extract image data and convert to RGBA floats
3271       */
3272      assert(n <= MAX_WIDTH);
3273      if (srcFormat == GL_COLOR_INDEX) {
3274         GLuint indexes[MAX_WIDTH];
3275         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3276                              srcPacking);
3277
3278         if (dstFormat == GL_COLOR_INDEX) {
3279            GLuint i;
3280            _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3281            /* convert to GLchan and return */
3282            for (i = 0; i < n; i++) {
3283               dest[i] = (GLchan) (indexes[i] & 0xff);
3284            }
3285            return;
3286         }
3287         else {
3288            /* Convert indexes to RGBA */
3289            if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3290               shift_and_offset_ci(ctx, n, indexes);
3291            }
3292            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3293         }
3294
3295         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3296          * with color indexes.
3297          */
3298         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3299      }
3300      else {
3301         /* non-color index data */
3302         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3303                            srcPacking->SwapBytes);
3304      }
3305
3306      if (transferOps) {
3307         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3308      }
3309
3310      /* Now determine which color channels we need to produce.
3311       * And determine the dest index (offset) within each color tuple.
3312       */
3313      switch (dstFormat) {
3314         case GL_ALPHA:
3315            dstAlphaIndex = 0;
3316            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3317            dstLuminanceIndex = dstIntensityIndex = -1;
3318            break;
3319         case GL_LUMINANCE:
3320            dstLuminanceIndex = 0;
3321            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3322            dstIntensityIndex = -1;
3323            break;
3324         case GL_LUMINANCE_ALPHA:
3325            dstLuminanceIndex = 0;
3326            dstAlphaIndex = 1;
3327            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3328            dstIntensityIndex = -1;
3329            break;
3330         case GL_INTENSITY:
3331            dstIntensityIndex = 0;
3332            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3333            dstLuminanceIndex = -1;
3334            break;
3335         case GL_RGB:
3336            dstRedIndex = 0;
3337            dstGreenIndex = 1;
3338            dstBlueIndex = 2;
3339            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3340            break;
3341         case GL_RGBA:
3342            dstRedIndex = 0;
3343            dstGreenIndex = 1;
3344            dstBlueIndex = 2;
3345            dstAlphaIndex = 3;
3346            dstLuminanceIndex = dstIntensityIndex = -1;
3347            break;
3348         default:
3349            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_color_span_float()");
3350            return;
3351      }
3352
3353      /* Now pack results in the requested dstFormat */
3354      if (dstRedIndex >= 0) {
3355         GLfloat *dst = dest;
3356         GLuint i;
3357         for (i = 0; i < n; i++) {
3358            dst[dstRedIndex] = rgba[i][RCOMP];
3359            dst += dstComponents;
3360         }
3361      }
3362
3363      if (dstGreenIndex >= 0) {
3364         GLfloat *dst = dest;
3365         GLuint i;
3366         for (i = 0; i < n; i++) {
3367            dst[dstGreenIndex] = rgba[i][GCOMP];
3368            dst += dstComponents;
3369         }
3370      }
3371
3372      if (dstBlueIndex >= 0) {
3373         GLfloat *dst = dest;
3374         GLuint i;
3375         for (i = 0; i < n; i++) {
3376            dst[dstBlueIndex] = rgba[i][BCOMP];
3377            dst += dstComponents;
3378         }
3379      }
3380
3381      if (dstAlphaIndex >= 0) {
3382         GLfloat *dst = dest;
3383         GLuint i;
3384         for (i = 0; i < n; i++) {
3385            dst[dstAlphaIndex] = rgba[i][ACOMP];
3386            dst += dstComponents;
3387         }
3388      }
3389
3390      if (dstIntensityIndex >= 0) {
3391         GLfloat *dst = dest;
3392         GLuint i;
3393         assert(dstIntensityIndex == 0);
3394         assert(dstComponents == 1);
3395         for (i = 0; i < n; i++) {
3396            /* Intensity comes from red channel */
3397            dst[i] = rgba[i][RCOMP];
3398         }
3399      }
3400
3401      if (dstLuminanceIndex >= 0) {
3402         GLfloat *dst = dest;
3403         GLuint i;
3404         assert(dstLuminanceIndex == 0);
3405         for (i = 0; i < n; i++) {
3406            /* Luminance comes from red channel */
3407            dst[0] = rgba[i][RCOMP];
3408            dst += dstComponents;
3409         }
3410      }
3411   }
3412}
3413
3414
3415/*
3416 * Unpack a row of color index data from a client buffer according to
3417 * the pixel unpacking parameters.
3418 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3419 *
3420 * Args:  ctx - the context
3421 *        n - number of pixels
3422 *        dstType - destination data type
3423 *        dest - destination array
3424 *        srcType - source pixel type
3425 *        source - source data pointer
3426 *        srcPacking - pixel unpacking parameters
3427 *        transferOps - the pixel transfer operations to apply
3428 */
3429void
3430_mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
3431                         GLenum dstType, GLvoid *dest,
3432                         GLenum srcType, const GLvoid *source,
3433                         const struct gl_pixelstore_attrib *srcPacking,
3434                         GLbitfield transferOps )
3435{
3436   ASSERT(srcType == GL_BITMAP ||
3437          srcType == GL_UNSIGNED_BYTE ||
3438          srcType == GL_BYTE ||
3439          srcType == GL_UNSIGNED_SHORT ||
3440          srcType == GL_SHORT ||
3441          srcType == GL_UNSIGNED_INT ||
3442          srcType == GL_INT ||
3443          srcType == GL_HALF_FLOAT_ARB ||
3444          srcType == GL_FLOAT);
3445
3446   ASSERT(dstType == GL_UNSIGNED_BYTE ||
3447          dstType == GL_UNSIGNED_SHORT ||
3448          dstType == GL_UNSIGNED_INT);
3449
3450
3451   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3452
3453   /*
3454    * Try simple cases first
3455    */
3456   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
3457       && dstType == GL_UNSIGNED_BYTE) {
3458      _mesa_memcpy(dest, source, n * sizeof(GLubyte));
3459   }
3460   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
3461            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
3462      _mesa_memcpy(dest, source, n * sizeof(GLuint));
3463   }
3464   else {
3465      /*
3466       * general solution
3467       */
3468      GLuint indexes[MAX_WIDTH];
3469      assert(n <= MAX_WIDTH);
3470
3471      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
3472                           srcPacking);
3473
3474      if (transferOps)
3475         _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3476
3477      /* convert to dest type */
3478      switch (dstType) {
3479         case GL_UNSIGNED_BYTE:
3480            {
3481               GLubyte *dst = (GLubyte *) dest;
3482               GLuint i;
3483               for (i = 0; i < n; i++) {
3484                  dst[i] = (GLubyte) (indexes[i] & 0xff);
3485               }
3486            }
3487            break;
3488         case GL_UNSIGNED_SHORT:
3489            {
3490               GLuint *dst = (GLuint *) dest;
3491               GLuint i;
3492               for (i = 0; i < n; i++) {
3493                  dst[i] = (GLushort) (indexes[i] & 0xffff);
3494               }
3495            }
3496            break;
3497         case GL_UNSIGNED_INT:
3498            _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
3499            break;
3500         default:
3501            _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
3502      }
3503   }
3504}
3505
3506
3507void
3508_mesa_pack_index_span( const GLcontext *ctx, GLuint n,
3509                       GLenum dstType, GLvoid *dest, const GLuint *source,
3510                       const struct gl_pixelstore_attrib *dstPacking,
3511                       GLbitfield transferOps )
3512{
3513   GLuint indexes[MAX_WIDTH];
3514
3515   ASSERT(n <= MAX_WIDTH);
3516
3517   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3518
3519   if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
3520      /* make a copy of input */
3521      _mesa_memcpy(indexes, source, n * sizeof(GLuint));
3522      _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3523      source = indexes;
3524   }
3525
3526   switch (dstType) {
3527   case GL_UNSIGNED_BYTE:
3528      {
3529         GLubyte *dst = (GLubyte *) dest;
3530         GLuint i;
3531         for (i = 0; i < n; i++) {
3532            *dst++ = (GLubyte) source[i];
3533         }
3534      }
3535      break;
3536   case GL_BYTE:
3537      {
3538         GLbyte *dst = (GLbyte *) dest;
3539         GLuint i;
3540         for (i = 0; i < n; i++) {
3541            dst[i] = (GLbyte) source[i];
3542         }
3543      }
3544      break;
3545   case GL_UNSIGNED_SHORT:
3546      {
3547         GLushort *dst = (GLushort *) dest;
3548         GLuint i;
3549         for (i = 0; i < n; i++) {
3550            dst[i] = (GLushort) source[i];
3551         }
3552         if (dstPacking->SwapBytes) {
3553            _mesa_swap2( (GLushort *) dst, n );
3554         }
3555      }
3556      break;
3557   case GL_SHORT:
3558      {
3559         GLshort *dst = (GLshort *) dest;
3560         GLuint i;
3561         for (i = 0; i < n; i++) {
3562            dst[i] = (GLshort) source[i];
3563         }
3564         if (dstPacking->SwapBytes) {
3565            _mesa_swap2( (GLushort *) dst, n );
3566         }
3567      }
3568      break;
3569   case GL_UNSIGNED_INT:
3570      {
3571         GLuint *dst = (GLuint *) dest;
3572         GLuint i;
3573         for (i = 0; i < n; i++) {
3574            dst[i] = (GLuint) source[i];
3575         }
3576         if (dstPacking->SwapBytes) {
3577            _mesa_swap4( (GLuint *) dst, n );
3578         }
3579      }
3580      break;
3581   case GL_INT:
3582      {
3583         GLint *dst = (GLint *) dest;
3584         GLuint i;
3585         for (i = 0; i < n; i++) {
3586            dst[i] = (GLint) source[i];
3587         }
3588         if (dstPacking->SwapBytes) {
3589            _mesa_swap4( (GLuint *) dst, n );
3590         }
3591      }
3592      break;
3593   case GL_FLOAT:
3594      {
3595         GLfloat *dst = (GLfloat *) dest;
3596         GLuint i;
3597         for (i = 0; i < n; i++) {
3598            dst[i] = (GLfloat) source[i];
3599         }
3600         if (dstPacking->SwapBytes) {
3601            _mesa_swap4( (GLuint *) dst, n );
3602         }
3603      }
3604      break;
3605   case GL_HALF_FLOAT_ARB:
3606      {
3607         GLhalfARB *dst = (GLhalfARB *) dest;
3608         GLuint i;
3609         for (i = 0; i < n; i++) {
3610            dst[i] = _mesa_float_to_half((GLfloat) source[i]);
3611         }
3612         if (dstPacking->SwapBytes) {
3613            _mesa_swap2( (GLushort *) dst, n );
3614         }
3615      }
3616      break;
3617   default:
3618      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
3619   }
3620}
3621
3622
3623/*
3624 * Unpack a row of stencil data from a client buffer according to
3625 * the pixel unpacking parameters.
3626 * This is (or will be) used by glDrawPixels
3627 *
3628 * Args:  ctx - the context
3629 *        n - number of pixels
3630 *        dstType - destination data type
3631 *        dest - destination array
3632 *        srcType - source pixel type
3633 *        source - source data pointer
3634 *        srcPacking - pixel unpacking parameters
3635 *        transferOps - apply offset/bias/lookup ops?
3636 */
3637void
3638_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
3639                           GLenum dstType, GLvoid *dest,
3640                           GLenum srcType, const GLvoid *source,
3641                           const struct gl_pixelstore_attrib *srcPacking,
3642                           GLbitfield transferOps )
3643{
3644   ASSERT(srcType == GL_BITMAP ||
3645          srcType == GL_UNSIGNED_BYTE ||
3646          srcType == GL_BYTE ||
3647          srcType == GL_UNSIGNED_SHORT ||
3648          srcType == GL_SHORT ||
3649          srcType == GL_UNSIGNED_INT ||
3650          srcType == GL_INT ||
3651          srcType == GL_UNSIGNED_INT_24_8_EXT ||
3652          srcType == GL_HALF_FLOAT_ARB ||
3653          srcType == GL_FLOAT);
3654
3655   ASSERT(dstType == GL_UNSIGNED_BYTE ||
3656          dstType == GL_UNSIGNED_SHORT ||
3657          dstType == GL_UNSIGNED_INT);
3658
3659   /* only shift and offset apply to stencil */
3660   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
3661
3662   /*
3663    * Try simple cases first
3664    */
3665   if (transferOps == 0 &&
3666       srcType == GL_UNSIGNED_BYTE &&
3667       dstType == GL_UNSIGNED_BYTE) {
3668      _mesa_memcpy(dest, source, n * sizeof(GLubyte));
3669   }
3670   else if (transferOps == 0 &&
3671            srcType == GL_UNSIGNED_INT &&
3672            dstType == GL_UNSIGNED_INT &&
3673            !srcPacking->SwapBytes) {
3674      _mesa_memcpy(dest, source, n * sizeof(GLuint));
3675   }
3676   else {
3677      /*
3678       * general solution
3679       */
3680      GLuint indexes[MAX_WIDTH];
3681      assert(n <= MAX_WIDTH);
3682
3683      extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
3684                           srcPacking);
3685
3686      if (transferOps) {
3687         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3688            /* shift and offset indexes */
3689            shift_and_offset_ci(ctx, n, indexes);
3690         }
3691
3692         if (ctx->Pixel.MapStencilFlag) {
3693            /* Apply stencil lookup table */
3694            GLuint mask = ctx->Pixel.MapStoSsize - 1;
3695            GLuint i;
3696            for (i=0;i<n;i++) {
3697               indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
3698            }
3699         }
3700      }
3701
3702      /* convert to dest type */
3703      switch (dstType) {
3704         case GL_UNSIGNED_BYTE:
3705            {
3706               GLubyte *dst = (GLubyte *) dest;
3707               GLuint i;
3708               for (i = 0; i < n; i++) {
3709                  dst[i] = (GLubyte) (indexes[i] & 0xff);
3710               }
3711            }
3712            break;
3713         case GL_UNSIGNED_SHORT:
3714            {
3715               GLuint *dst = (GLuint *) dest;
3716               GLuint i;
3717               for (i = 0; i < n; i++) {
3718                  dst[i] = (GLushort) (indexes[i] & 0xffff);
3719               }
3720            }
3721            break;
3722         case GL_UNSIGNED_INT:
3723            _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
3724            break;
3725         default:
3726            _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
3727      }
3728   }
3729}
3730
3731
3732void
3733_mesa_pack_stencil_span( const GLcontext *ctx, GLuint n,
3734                         GLenum dstType, GLvoid *dest, const GLstencil *source,
3735                         const struct gl_pixelstore_attrib *dstPacking )
3736{
3737   GLstencil stencil[MAX_WIDTH];
3738
3739   ASSERT(n <= MAX_WIDTH);
3740
3741   if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
3742       ctx->Pixel.MapStencilFlag) {
3743      /* make a copy of input */
3744      _mesa_memcpy(stencil, source, n * sizeof(GLstencil));
3745      _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
3746      source = stencil;
3747   }
3748
3749   switch (dstType) {
3750   case GL_UNSIGNED_BYTE:
3751      if (sizeof(GLstencil) == 8) {
3752         _mesa_memcpy( dest, source, n );
3753      }
3754      else {
3755         GLubyte *dst = (GLubyte *) dest;
3756         GLuint i;
3757         for (i=0;i<n;i++) {
3758            dst[i] = (GLubyte) source[i];
3759         }
3760      }
3761      break;
3762   case GL_BYTE:
3763      if (sizeof(GLstencil) == 8) {
3764         _mesa_memcpy( dest, source, n );
3765      }
3766      else {
3767         GLbyte *dst = (GLbyte *) dest;
3768         GLuint i;
3769         for (i=0;i<n;i++) {
3770            dst[i] = (GLbyte) source[i];
3771         }
3772      }
3773      break;
3774   case GL_UNSIGNED_SHORT:
3775      {
3776         GLushort *dst = (GLushort *) dest;
3777         GLuint i;
3778         for (i=0;i<n;i++) {
3779            dst[i] = (GLushort) source[i];
3780         }
3781         if (dstPacking->SwapBytes) {
3782            _mesa_swap2( (GLushort *) dst, n );
3783         }
3784      }
3785      break;
3786   case GL_SHORT:
3787      {
3788         GLshort *dst = (GLshort *) dest;
3789         GLuint i;
3790         for (i=0;i<n;i++) {
3791            dst[i] = (GLshort) source[i];
3792         }
3793         if (dstPacking->SwapBytes) {
3794            _mesa_swap2( (GLushort *) dst, n );
3795         }
3796      }
3797      break;
3798   case GL_UNSIGNED_INT:
3799      {
3800         GLuint *dst = (GLuint *) dest;
3801         GLuint i;
3802         for (i=0;i<n;i++) {
3803            dst[i] = (GLuint) source[i];
3804         }
3805         if (dstPacking->SwapBytes) {
3806            _mesa_swap4( (GLuint *) dst, n );
3807         }
3808      }
3809      break;
3810   case GL_INT:
3811      {
3812         GLint *dst = (GLint *) dest;
3813         GLuint i;
3814         for (i=0;i<n;i++) {
3815            *dst++ = (GLint) source[i];
3816         }
3817         if (dstPacking->SwapBytes) {
3818            _mesa_swap4( (GLuint *) dst, n );
3819         }
3820      }
3821      break;
3822   case GL_FLOAT:
3823      {
3824         GLfloat *dst = (GLfloat *) dest;
3825         GLuint i;
3826         for (i=0;i<n;i++) {
3827            dst[i] = (GLfloat) source[i];
3828         }
3829         if (dstPacking->SwapBytes) {
3830            _mesa_swap4( (GLuint *) dst, n );
3831         }
3832      }
3833      break;
3834   case GL_HALF_FLOAT_ARB:
3835      {
3836         GLhalfARB *dst = (GLhalfARB *) dest;
3837         GLuint i;
3838         for (i=0;i<n;i++) {
3839            dst[i] = _mesa_float_to_half( (float) source[i] );
3840         }
3841         if (dstPacking->SwapBytes) {
3842            _mesa_swap2( (GLushort *) dst, n );
3843         }
3844      }
3845      break;
3846   case GL_BITMAP:
3847      if (dstPacking->LsbFirst) {
3848         GLubyte *dst = (GLubyte *) dest;
3849         GLint shift = 0;
3850         GLuint i;
3851         for (i = 0; i < n; i++) {
3852            if (shift == 0)
3853               *dst = 0;
3854            *dst |= ((source[i] != 0) << shift);
3855            shift++;
3856            if (shift == 8) {
3857               shift = 0;
3858               dst++;
3859            }
3860         }
3861      }
3862      else {
3863         GLubyte *dst = (GLubyte *) dest;
3864         GLint shift = 7;
3865         GLuint i;
3866         for (i = 0; i < n; i++) {
3867            if (shift == 7)
3868               *dst = 0;
3869            *dst |= ((source[i] != 0) << shift);
3870            shift--;
3871            if (shift < 0) {
3872               shift = 7;
3873               dst++;
3874            }
3875         }
3876      }
3877      break;
3878   default:
3879      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
3880   }
3881}
3882
3883
3884void
3885_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n,
3886                         GLenum dstType, GLvoid *dest, GLfloat depthScale,
3887                         GLenum srcType, const GLvoid *source,
3888                         const struct gl_pixelstore_attrib *srcPacking )
3889{
3890   GLfloat depthTemp[MAX_WIDTH], *depthValues;
3891
3892   if (dstType == GL_FLOAT) {
3893      depthValues = (GLfloat *) dest;
3894   }
3895   else {
3896      depthValues = depthTemp;
3897   }
3898
3899   /* XXX we need to obey srcPacking->SwapBytes here!!! */
3900   (void) srcPacking;
3901
3902   switch (srcType) {
3903      case GL_BYTE:
3904         {
3905            GLuint i;
3906            const GLubyte *src = (const GLubyte *) source;
3907            for (i = 0; i < n; i++) {
3908               depthValues[i] = BYTE_TO_FLOAT(src[i]);
3909            }
3910         }
3911         break;
3912      case GL_UNSIGNED_BYTE:
3913         {
3914            GLuint i;
3915            const GLubyte *src = (const GLubyte *) source;
3916            for (i = 0; i < n; i++) {
3917               depthValues[i] = UBYTE_TO_FLOAT(src[i]);
3918            }
3919         }
3920         break;
3921      case GL_SHORT:
3922         {
3923            GLuint i;
3924            const GLshort *src = (const GLshort *) source;
3925            for (i = 0; i < n; i++) {
3926               depthValues[i] = SHORT_TO_FLOAT(src[i]);
3927            }
3928         }
3929         break;
3930      case GL_UNSIGNED_SHORT:
3931         {
3932            GLuint i;
3933            const GLushort *src = (const GLushort *) source;
3934            for (i = 0; i < n; i++) {
3935               depthValues[i] = USHORT_TO_FLOAT(src[i]);
3936            }
3937         }
3938         break;
3939      case GL_INT:
3940         {
3941            GLuint i;
3942            const GLint *src = (const GLint *) source;
3943            for (i = 0; i < n; i++) {
3944               depthValues[i] = INT_TO_FLOAT(src[i]);
3945            }
3946         }
3947         break;
3948      case GL_UNSIGNED_INT:
3949         {
3950            GLuint i;
3951            const GLuint *src = (const GLuint *) source;
3952            for (i = 0; i < n; i++) {
3953               depthValues[i] = UINT_TO_FLOAT(src[i]);
3954            }
3955         }
3956         break;
3957      case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
3958         if (dstType == GL_UNSIGNED_INT &&
3959             depthScale == (GLfloat) 0xffffff &&
3960             ctx->Pixel.DepthScale == 1.0 &&
3961             ctx->Pixel.DepthBias == 0.0) {
3962            const GLuint *src = (const GLuint *) source;
3963            GLuint *zValues = (GLuint *) dest;
3964            GLuint i;
3965            for (i = 0; i < n; i++) {
3966               zValues[i] = src[i] & 0xffffff00;
3967            }
3968            return;
3969         }
3970         else {
3971            const GLuint *src = (const GLuint *) source;
3972            const GLfloat scale = 1.0f / 0xffffff;
3973            GLuint i;
3974            for (i = 0; i < n; i++) {
3975               depthValues[i] = (src[i] >> 8) * scale;
3976            }
3977         }
3978         break;
3979      case GL_FLOAT:
3980         _mesa_memcpy(depthValues, source, n * sizeof(GLfloat));
3981         break;
3982      case GL_HALF_FLOAT_ARB:
3983         {
3984            GLuint i;
3985            const GLhalfARB *src = (const GLhalfARB *) source;
3986            for (i = 0; i < n; i++) {
3987               depthValues[i] = _mesa_half_to_float(src[i]);
3988            }
3989         }
3990         break;
3991      default:
3992         _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
3993         return;
3994   }
3995
3996
3997   /* apply depth scale and bias and clamp to [0,1] */
3998   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
3999      _mesa_scale_and_bias_depth(ctx, n, depthValues);
4000   }
4001
4002   if (dstType == GL_UNSIGNED_INT) {
4003      GLuint *zValues = (GLuint *) dest;
4004      GLuint i;
4005      if (depthScale <= (GLfloat) 0xffffff) {
4006         /* no overflow worries */
4007         for (i = 0; i < n; i++) {
4008            zValues[i] = (GLuint) (depthValues[i] * depthScale);
4009         }
4010      }
4011      else {
4012         /* need to use double precision to prevent overflow problems */
4013         for (i = 0; i < n; i++) {
4014            GLdouble z = depthValues[i] * depthScale;
4015            if (z >= (GLdouble) 0xffffffff)
4016               zValues[i] = 0xffffffff;
4017            else
4018               zValues[i] = (GLuint) z;
4019         }
4020      }
4021   }
4022   else if (dstType == GL_UNSIGNED_SHORT) {
4023      GLushort *zValues = (GLushort *) dest;
4024      GLuint i;
4025      ASSERT(depthScale <= 65535.0);
4026      for (i = 0; i < n; i++) {
4027         zValues[i] = (GLushort) (depthValues[i] * depthScale);
4028      }
4029   }
4030   else {
4031      ASSERT(dstType == GL_FLOAT);
4032      ASSERT(depthScale == 1.0F);
4033   }
4034}
4035
4036
4037/*
4038 * Pack an array of depth values.  The values are floats in [0,1].
4039 */
4040void
4041_mesa_pack_depth_span( const GLcontext *ctx, GLuint n, GLvoid *dest,
4042                       GLenum dstType, const GLfloat *depthSpan,
4043                       const struct gl_pixelstore_attrib *dstPacking )
4044{
4045   GLfloat depthCopy[MAX_WIDTH];
4046
4047   ASSERT(n <= MAX_WIDTH);
4048
4049   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4050      _mesa_memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
4051      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4052      depthSpan = depthCopy;
4053   }
4054
4055   switch (dstType) {
4056   case GL_UNSIGNED_BYTE:
4057      {
4058         GLubyte *dst = (GLubyte *) dest;
4059         GLuint i;
4060         for (i = 0; i < n; i++) {
4061            dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
4062         }
4063      }
4064      break;
4065   case GL_BYTE:
4066      {
4067         GLbyte *dst = (GLbyte *) dest;
4068         GLuint i;
4069         for (i = 0; i < n; i++) {
4070            dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
4071         }
4072      }
4073      break;
4074   case GL_UNSIGNED_SHORT:
4075      {
4076         GLushort *dst = (GLushort *) dest;
4077         GLuint i;
4078         for (i = 0; i < n; i++) {
4079            CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
4080         }
4081         if (dstPacking->SwapBytes) {
4082            _mesa_swap2( (GLushort *) dst, n );
4083         }
4084      }
4085      break;
4086   case GL_SHORT:
4087      {
4088         GLshort *dst = (GLshort *) dest;
4089         GLuint i;
4090         for (i = 0; i < n; i++) {
4091            dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
4092         }
4093         if (dstPacking->SwapBytes) {
4094            _mesa_swap2( (GLushort *) dst, n );
4095         }
4096      }
4097      break;
4098   case GL_UNSIGNED_INT:
4099      {
4100         GLuint *dst = (GLuint *) dest;
4101         GLuint i;
4102         for (i = 0; i < n; i++) {
4103            dst[i] = FLOAT_TO_UINT( depthSpan[i] );
4104         }
4105         if (dstPacking->SwapBytes) {
4106            _mesa_swap4( (GLuint *) dst, n );
4107         }
4108      }
4109      break;
4110   case GL_INT:
4111      {
4112         GLint *dst = (GLint *) dest;
4113         GLuint i;
4114         for (i = 0; i < n; i++) {
4115            dst[i] = FLOAT_TO_INT( depthSpan[i] );
4116         }
4117         if (dstPacking->SwapBytes) {
4118            _mesa_swap4( (GLuint *) dst, n );
4119         }
4120      }
4121      break;
4122   case GL_FLOAT:
4123      {
4124         GLfloat *dst = (GLfloat *) dest;
4125         GLuint i;
4126         for (i = 0; i < n; i++) {
4127            dst[i] = depthSpan[i];
4128         }
4129         if (dstPacking->SwapBytes) {
4130            _mesa_swap4( (GLuint *) dst, n );
4131         }
4132      }
4133      break;
4134   case GL_HALF_FLOAT_ARB:
4135      {
4136         GLhalfARB *dst = (GLhalfARB *) dest;
4137         GLuint i;
4138         for (i = 0; i < n; i++) {
4139            dst[i] = _mesa_float_to_half(depthSpan[i]);
4140         }
4141         if (dstPacking->SwapBytes) {
4142            _mesa_swap2( (GLushort *) dst, n );
4143         }
4144      }
4145      break;
4146   default:
4147      _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
4148   }
4149}
4150
4151
4152
4153/**
4154 * Pack depth and stencil values as GL_DEPTH_STENCIL/GL_UNSIGNED_INT_24_8.
4155 */
4156void
4157_mesa_pack_depth_stencil_span(const GLcontext *ctx, GLuint n, GLuint *dest,
4158                              const GLfloat *depthVals,
4159                              const GLstencil *stencilVals,
4160                              const struct gl_pixelstore_attrib *dstPacking)
4161{
4162   GLfloat depthCopy[MAX_WIDTH];
4163   GLstencil stencilCopy[MAX_WIDTH];
4164   GLuint i;
4165
4166   ASSERT(n <= MAX_WIDTH);
4167
4168   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4169      _mesa_memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
4170      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4171      depthVals = depthCopy;
4172   }
4173
4174   if (ctx->Pixel.IndexShift ||
4175       ctx->Pixel.IndexOffset ||
4176       ctx->Pixel.MapStencilFlag) {
4177      _mesa_memcpy(stencilCopy, stencilVals, n * sizeof(GLstencil));
4178      _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
4179      stencilVals = stencilCopy;
4180   }
4181
4182   for (i = 0; i < n; i++) {
4183      GLuint z = (GLuint) (depthVals[i] * 0xffffff);
4184      dest[i] = (z << 8) | (stencilVals[i] & 0xff);
4185   }
4186
4187   if (dstPacking->SwapBytes) {
4188      _mesa_swap4(dest, n);
4189   }
4190}
4191
4192
4193
4194
4195/**
4196 * Unpack image data.  Apply byte swapping, byte flipping (bitmap).
4197 * Return all image data in a contiguous block.  This is used when we
4198 * compile glDrawPixels, glTexImage, etc into a display list.  We
4199 * need a copy of the data in a standard format.
4200 */
4201void *
4202_mesa_unpack_image( GLuint dimensions,
4203                    GLsizei width, GLsizei height, GLsizei depth,
4204                    GLenum format, GLenum type, const GLvoid *pixels,
4205                    const struct gl_pixelstore_attrib *unpack )
4206{
4207   GLint bytesPerRow, compsPerRow;
4208   GLboolean flipBytes, swap2, swap4;
4209
4210   if (!pixels)
4211      return NULL;  /* not necessarily an error */
4212
4213   if (width <= 0 || height <= 0 || depth <= 0)
4214      return NULL;  /* generate error later */
4215
4216   if (type == GL_BITMAP) {
4217      bytesPerRow = (width + 7) >> 3;
4218      flipBytes = unpack->LsbFirst;
4219      swap2 = swap4 = GL_FALSE;
4220      compsPerRow = 0;
4221   }
4222   else {
4223      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
4224      GLint components = _mesa_components_in_format(format);
4225      GLint bytesPerComp;
4226
4227      if (_mesa_type_is_packed(type))
4228          components = 1;
4229
4230      if (bytesPerPixel <= 0 || components <= 0)
4231         return NULL;   /* bad format or type.  generate error later */
4232      bytesPerRow = bytesPerPixel * width;
4233      bytesPerComp = bytesPerPixel / components;
4234      flipBytes = GL_FALSE;
4235      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
4236      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
4237      compsPerRow = components * width;
4238      assert(compsPerRow >= width);
4239   }
4240
4241   {
4242      GLubyte *destBuffer
4243         = (GLubyte *) _mesa_malloc(bytesPerRow * height * depth);
4244      GLubyte *dst;
4245      GLint img, row;
4246      if (!destBuffer)
4247         return NULL;   /* generate GL_OUT_OF_MEMORY later */
4248
4249      dst = destBuffer;
4250      for (img = 0; img < depth; img++) {
4251         for (row = 0; row < height; row++) {
4252            const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
4253                               width, height, format, type, img, row, 0);
4254
4255                if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
4256                    GLint i;
4257                    flipBytes = GL_FALSE;
4258                    if (unpack->LsbFirst) {
4259                            GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
4260                            GLubyte dstMask = 128;
4261                            const GLubyte *s = src;
4262                            GLubyte *d = dst;
4263                            *d = 0;
4264                            for (i = 0; i < width; i++) {
4265                                if (*s & srcMask) {
4266                                    *d |= dstMask;
4267                                }
4268                                if (srcMask == 128) {
4269                                    srcMask = 1;
4270                                    s++;
4271                                } else {
4272                                    srcMask = srcMask << 1;
4273                                }
4274                                if (dstMask == 1) {
4275                                    dstMask = 128;
4276                                    d++;
4277                                    *d = 0;
4278                                } else {
4279                                    dstMask = dstMask >> 1;
4280                                }
4281                            }
4282                    } else {
4283                        GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
4284                        GLubyte dstMask = 128;
4285                        const GLubyte *s = src;
4286                        GLubyte *d = dst;
4287                        *d = 0;
4288                        for (i = 0; i < width; i++) {
4289                            if (*s & srcMask) {
4290                                *d |= dstMask;
4291                            }
4292                            if (srcMask == 1) {
4293                                srcMask = 128;
4294                                s++;
4295                            } else {
4296                                srcMask = srcMask >> 1;
4297                            }
4298                            if (dstMask == 1) {
4299                                dstMask = 128;
4300                                d++;
4301                                *d = 0;
4302                            } else {
4303                                dstMask = dstMask >> 1;
4304                            }
4305                        }
4306                    }
4307                } else
4308                    _mesa_memcpy(dst, src, bytesPerRow);
4309            /* byte flipping/swapping */
4310            if (flipBytes) {
4311               flip_bytes((GLubyte *) dst, bytesPerRow);
4312            }
4313            else if (swap2) {
4314               _mesa_swap2((GLushort*) dst, compsPerRow);
4315            }
4316            else if (swap4) {
4317               _mesa_swap4((GLuint*) dst, compsPerRow);
4318            }
4319            dst += bytesPerRow;
4320         }
4321      }
4322      return destBuffer;
4323   }
4324}
4325
4326#endif /* _HAVE_FULL_GL */
4327
4328
4329
4330/**
4331 * Convert an array of RGBA colors from one datatype to another.
4332 * NOTE: src may equal dst.  In that case, we use a temporary buffer.
4333 */
4334void
4335_mesa_convert_colors(GLenum srcType, const GLvoid *src,
4336                     GLenum dstType, GLvoid *dst,
4337                     GLuint count, const GLubyte mask[])
4338{
4339   GLuint tempBuffer[MAX_WIDTH][4];
4340   const GLboolean useTemp = (src == dst);
4341
4342   ASSERT(srcType != dstType);
4343
4344   switch (srcType) {
4345   case GL_UNSIGNED_BYTE:
4346      if (dstType == GL_UNSIGNED_SHORT) {
4347         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
4348         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
4349         GLuint i;
4350         for (i = 0; i < count; i++) {
4351            if (!mask || mask[i]) {
4352               dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
4353               dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
4354               dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
4355               dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
4356            }
4357         }
4358         if (useTemp)
4359            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
4360      }
4361      else {
4362         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
4363         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
4364         GLuint i;
4365         ASSERT(dstType == GL_FLOAT);
4366         for (i = 0; i < count; i++) {
4367            if (!mask || mask[i]) {
4368               dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
4369               dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
4370               dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
4371               dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
4372            }
4373         }
4374         if (useTemp)
4375            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
4376      }
4377      break;
4378   case GL_UNSIGNED_SHORT:
4379      if (dstType == GL_UNSIGNED_BYTE) {
4380         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
4381         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
4382         GLuint i;
4383         for (i = 0; i < count; i++) {
4384            if (!mask || mask[i]) {
4385               dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
4386               dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
4387               dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
4388               dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
4389            }
4390         }
4391         if (useTemp)
4392            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
4393      }
4394      else {
4395         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
4396         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
4397         GLuint i;
4398         ASSERT(dstType == GL_FLOAT);
4399         for (i = 0; i < count; i++) {
4400            if (!mask || mask[i]) {
4401               dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
4402               dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
4403               dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
4404               dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
4405            }
4406         }
4407         if (useTemp)
4408            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
4409      }
4410      break;
4411   case GL_FLOAT:
4412      if (dstType == GL_UNSIGNED_BYTE) {
4413         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
4414         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
4415         GLuint i;
4416         for (i = 0; i < count; i++) {
4417            if (!mask || mask[i]) {
4418               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
4419               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
4420               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
4421               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
4422            }
4423         }
4424         if (useTemp)
4425            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
4426      }
4427      else {
4428         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
4429         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
4430         GLuint i;
4431         ASSERT(dstType == GL_UNSIGNED_SHORT);
4432         for (i = 0; i < count; i++) {
4433            if (!mask || mask[i]) {
4434               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
4435               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
4436               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
4437               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
4438            }
4439         }
4440         if (useTemp)
4441            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
4442      }
4443      break;
4444   default:
4445      _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
4446   }
4447}
4448
4449
4450
4451
4452/**
4453 * Perform basic clipping for glDrawPixels.  The image's position and size
4454 * and the unpack SkipPixels and SkipRows are adjusted so that the image
4455 * region is entirely within the window and scissor bounds.
4456 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
4457 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
4458 * we'll actually write.  Beforehand, *destY-1 is the first drawing row.
4459 *
4460 * \return  GL_TRUE if image is ready for drawing or
4461 *          GL_FALSE if image was completely clipped away (draw nothing)
4462 */
4463GLboolean
4464_mesa_clip_drawpixels(const GLcontext *ctx,
4465                      GLint *destX, GLint *destY,
4466                      GLsizei *width, GLsizei *height,
4467                      struct gl_pixelstore_attrib *unpack)
4468{
4469   const GLframebuffer *buffer = ctx->DrawBuffer;
4470
4471   if (unpack->RowLength == 0) {
4472      unpack->RowLength = *width;
4473   }
4474
4475   ASSERT(ctx->Pixel.ZoomX == 1.0F);
4476   ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
4477
4478   /* left clipping */
4479   if (*destX < buffer->_Xmin) {
4480      unpack->SkipPixels += (buffer->_Xmin - *destX);
4481      *width -= (buffer->_Xmin - *destX);
4482      *destX = buffer->_Xmin;
4483   }
4484   /* right clipping */
4485   if (*destX + *width > buffer->_Xmax)
4486      *width -= (*destX + *width - buffer->_Xmax);
4487
4488   if (*width <= 0)
4489      return GL_FALSE;
4490
4491   if (ctx->Pixel.ZoomY == 1.0F) {
4492      /* bottom clipping */
4493      if (*destY < buffer->_Ymin) {
4494         unpack->SkipRows += (buffer->_Ymin - *destY);
4495         *height -= (buffer->_Ymin - *destY);
4496         *destY = buffer->_Ymin;
4497      }
4498      /* top clipping */
4499      if (*destY + *height > buffer->_Ymax)
4500         *height -= (*destY + *height - buffer->_Ymax);
4501   }
4502   else { /* upside down */
4503      /* top clipping */
4504      if (*destY > buffer->_Ymax) {
4505         unpack->SkipRows += (*destY - buffer->_Ymax);
4506         *height -= (*destY - buffer->_Ymax);
4507         *destY = buffer->_Ymax;
4508      }
4509      /* bottom clipping */
4510      if (*destY - *height < buffer->_Ymin)
4511         *height -= (buffer->_Ymin - (*destY - *height));
4512      /* adjust destY so it's the first row to write to */
4513      (*destY)--;
4514   }
4515
4516   if (*height <= 0)
4517      return GL_TRUE;
4518
4519   return GL_TRUE;
4520}
4521
4522
4523/**
4524 * Perform clipping for glReadPixels.  The image's window position
4525 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
4526 * so that the image region is entirely within the window bounds.
4527 * Note: this is different from _mesa_clip_drawpixels() in that the
4528 * scissor box is ignored, and we use the bounds of the current readbuffer
4529 * surface.
4530 *
4531 * \return  GL_TRUE if image is ready for drawing or
4532 *          GL_FALSE if image was completely clipped away (draw nothing)
4533 */
4534GLboolean
4535_mesa_clip_readpixels(const GLcontext *ctx,
4536                      GLint *srcX, GLint *srcY,
4537                      GLsizei *width, GLsizei *height,
4538                      struct gl_pixelstore_attrib *pack)
4539{
4540   const GLframebuffer *buffer = ctx->ReadBuffer;
4541
4542   if (pack->RowLength == 0) {
4543      pack->RowLength = *width;
4544   }
4545
4546   /* left clipping */
4547   if (*srcX < 0) {
4548      pack->SkipPixels += (0 - *srcX);
4549      *width -= (0 - *srcX);
4550      *srcX = 0;
4551   }
4552   /* right clipping */
4553   if (*srcX + *width > (GLsizei) buffer->Width)
4554      *width -= (*srcX + *width - buffer->Width);
4555
4556   if (*width <= 0)
4557      return GL_FALSE;
4558
4559   /* bottom clipping */
4560   if (*srcY < 0) {
4561      pack->SkipRows += (0 - *srcY);
4562      *height -= (0 - *srcY);
4563      *srcY = 0;
4564   }
4565   /* top clipping */
4566   if (*srcY + *height > (GLsizei) buffer->Height)
4567      *height -= (*srcY + *height - buffer->Height);
4568
4569   if (*height <= 0)
4570      return GL_TRUE;
4571
4572   return GL_TRUE;
4573}
4574
4575
4576/**
4577 * Clip the rectangle defined by (x, y, width, height) against the bounds
4578 * specified by [xmin, xmax) and [ymin, ymax).
4579 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
4580 */
4581GLboolean
4582_mesa_clip_to_region(GLint xmin, GLint ymin,
4583                     GLint xmax, GLint ymax,
4584                     GLint *x, GLint *y,
4585                     GLsizei *width, GLsizei *height )
4586{
4587   /* left clipping */
4588   if (*x < xmin) {
4589      *width -= (xmin - *x);
4590      *x = xmin;
4591   }
4592
4593   /* right clipping */
4594   if (*x + *width > xmax)
4595      *width -= (*x + *width - xmax - 1);
4596
4597   if (*width <= 0)
4598      return GL_FALSE;
4599
4600   /* bottom (or top) clipping */
4601   if (*y < ymin) {
4602      *height -= (ymin - *y);
4603      *y = ymin;
4604   }
4605
4606   /* top (or bottom) clipping */
4607   if (*y + *height > ymax)
4608      *height -= (*y + *height - ymax - 1);
4609
4610   if (*height <= 0)
4611      return GL_FALSE;
4612
4613   return GL_TRUE;
4614}
4615