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