image.c revision 5893474c741274cde7a312c920dacfb00790c302
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 (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE) {
1186         for (i = 0; i < n; i++) {
1187            GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1188            luminance[i] = CLAMP(sum, 0.0F, 1.0F);
1189         }
1190      }
1191      else {
1192         for (i = 0; i < n; i++) {
1193            luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1194         }
1195      }
1196   }
1197
1198   /*
1199    * Pack/store the pixels.  Ugh!  Lots of cases!!!
1200    */
1201   switch (dstType) {
1202      case GL_UNSIGNED_BYTE:
1203         {
1204            GLubyte *dst = (GLubyte *) dstAddr;
1205            switch (dstFormat) {
1206               case GL_RED:
1207                  for (i=0;i<n;i++)
1208                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1209                  break;
1210               case GL_GREEN:
1211                  for (i=0;i<n;i++)
1212                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1213                  break;
1214               case GL_BLUE:
1215                  for (i=0;i<n;i++)
1216                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1217                  break;
1218               case GL_ALPHA:
1219                  for (i=0;i<n;i++)
1220                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1221                  break;
1222               case GL_LUMINANCE:
1223                  for (i=0;i<n;i++)
1224                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1225                  break;
1226               case GL_LUMINANCE_ALPHA:
1227                  for (i=0;i<n;i++) {
1228                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1229                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1230                  }
1231                  break;
1232               case GL_RGB:
1233                  for (i=0;i<n;i++) {
1234                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1235                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1236                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1237                  }
1238                  break;
1239               case GL_RGBA:
1240                  for (i=0;i<n;i++) {
1241                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1242                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1243                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1244                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1245                  }
1246                  break;
1247               case GL_BGR:
1248                  for (i=0;i<n;i++) {
1249                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1250                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1251                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1252                  }
1253                  break;
1254               case GL_BGRA:
1255                  for (i=0;i<n;i++) {
1256                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1257                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1258                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1259                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1260                  }
1261                  break;
1262               case GL_ABGR_EXT:
1263                  for (i=0;i<n;i++) {
1264                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1265                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1266                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1267                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1268                  }
1269                  break;
1270               default:
1271                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1272            }
1273         }
1274         break;
1275      case GL_BYTE:
1276         {
1277            GLbyte *dst = (GLbyte *) dstAddr;
1278            switch (dstFormat) {
1279               case GL_RED:
1280                  for (i=0;i<n;i++)
1281                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1282                  break;
1283               case GL_GREEN:
1284                  for (i=0;i<n;i++)
1285                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1286                  break;
1287               case GL_BLUE:
1288                  for (i=0;i<n;i++)
1289                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1290                  break;
1291               case GL_ALPHA:
1292                  for (i=0;i<n;i++)
1293                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1294                  break;
1295               case GL_LUMINANCE:
1296                  for (i=0;i<n;i++)
1297                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
1298                  break;
1299               case GL_LUMINANCE_ALPHA:
1300                  for (i=0;i<n;i++) {
1301                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1302                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1303                  }
1304                  break;
1305               case GL_RGB:
1306                  for (i=0;i<n;i++) {
1307                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1308                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1309                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1310                  }
1311                  break;
1312               case GL_RGBA:
1313                  for (i=0;i<n;i++) {
1314                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1315                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1316                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1317                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1318                  }
1319                  break;
1320               case GL_BGR:
1321                  for (i=0;i<n;i++) {
1322                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1323                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1324                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1325                  }
1326                  break;
1327               case GL_BGRA:
1328                  for (i=0;i<n;i++) {
1329                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1330                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1331                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1332                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1333                  }
1334		  break;
1335               case GL_ABGR_EXT:
1336                  for (i=0;i<n;i++) {
1337                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
1338                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
1339                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
1340                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
1341                  }
1342                  break;
1343               default:
1344                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1345            }
1346         }
1347         break;
1348      case GL_UNSIGNED_SHORT:
1349         {
1350            GLushort *dst = (GLushort *) dstAddr;
1351            switch (dstFormat) {
1352               case GL_RED:
1353                  for (i=0;i<n;i++)
1354                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
1355                  break;
1356               case GL_GREEN:
1357                  for (i=0;i<n;i++)
1358                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
1359                  break;
1360               case GL_BLUE:
1361                  for (i=0;i<n;i++)
1362                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
1363                  break;
1364               case GL_ALPHA:
1365                  for (i=0;i<n;i++)
1366                     CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
1367                  break;
1368               case GL_LUMINANCE:
1369                  for (i=0;i<n;i++)
1370                     UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
1371                  break;
1372               case GL_LUMINANCE_ALPHA:
1373                  for (i=0;i<n;i++) {
1374                     UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
1375                     CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
1376                  }
1377                  break;
1378               case GL_RGB:
1379                  for (i=0;i<n;i++) {
1380                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
1381                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1382                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
1383                  }
1384                  break;
1385               case GL_RGBA:
1386                  for (i=0;i<n;i++) {
1387                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
1388                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1389                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
1390                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1391                  }
1392                  break;
1393               case GL_BGR:
1394                  for (i=0;i<n;i++) {
1395                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
1396                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
1397                     CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
1398                  }
1399                  break;
1400               case GL_BGRA:
1401                  for (i=0;i<n;i++) {
1402                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
1403                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
1404                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
1405                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
1406                  }
1407                  break;
1408               case GL_ABGR_EXT:
1409                  for (i=0;i<n;i++) {
1410                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
1411                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
1412                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
1413                     CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
1414                  }
1415                  break;
1416               default:
1417                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1418            }
1419            if (dstPacking->SwapBytes) {
1420               _mesa_swap2( (GLushort *) dst, n * comps);
1421            }
1422         }
1423         break;
1424      case GL_SHORT:
1425         {
1426            GLshort *dst = (GLshort *) dstAddr;
1427            switch (dstFormat) {
1428               case GL_RED:
1429                  for (i=0;i<n;i++)
1430                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1431                  break;
1432               case GL_GREEN:
1433                  for (i=0;i<n;i++)
1434                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1435                  break;
1436               case GL_BLUE:
1437                  for (i=0;i<n;i++)
1438                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1439                  break;
1440               case GL_ALPHA:
1441                  for (i=0;i<n;i++)
1442                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1443                  break;
1444               case GL_LUMINANCE:
1445                  for (i=0;i<n;i++)
1446                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
1447                  break;
1448               case GL_LUMINANCE_ALPHA:
1449                  for (i=0;i<n;i++) {
1450                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1451                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1452                  }
1453                  break;
1454               case GL_RGB:
1455                  for (i=0;i<n;i++) {
1456                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1457                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1458                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1459                  }
1460                  break;
1461               case GL_RGBA:
1462                  for (i=0;i<n;i++) {
1463                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1464                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1465                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1466                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1467                  }
1468                  break;
1469               case GL_BGR:
1470                  for (i=0;i<n;i++) {
1471                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1472                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1473                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1474                  }
1475                  break;
1476               case GL_BGRA:
1477                  for (i=0;i<n;i++) {
1478                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1479                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1480                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1481                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1482                  }
1483		  break;
1484               case GL_ABGR_EXT:
1485                  for (i=0;i<n;i++) {
1486                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1487                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1488                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1489                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1490                  }
1491                  break;
1492               default:
1493                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1494            }
1495            if (dstPacking->SwapBytes) {
1496               _mesa_swap2( (GLushort *) dst, n * comps );
1497            }
1498         }
1499         break;
1500      case GL_UNSIGNED_INT:
1501         {
1502            GLuint *dst = (GLuint *) dstAddr;
1503            switch (dstFormat) {
1504               case GL_RED:
1505                  for (i=0;i<n;i++)
1506                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1507                  break;
1508               case GL_GREEN:
1509                  for (i=0;i<n;i++)
1510                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1511                  break;
1512               case GL_BLUE:
1513                  for (i=0;i<n;i++)
1514                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1515                  break;
1516               case GL_ALPHA:
1517                  for (i=0;i<n;i++)
1518                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1519                  break;
1520               case GL_LUMINANCE:
1521                  for (i=0;i<n;i++)
1522                     dst[i] = FLOAT_TO_UINT(luminance[i]);
1523                  break;
1524               case GL_LUMINANCE_ALPHA:
1525                  for (i=0;i<n;i++) {
1526                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
1527                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1528                  }
1529                  break;
1530               case GL_RGB:
1531                  for (i=0;i<n;i++) {
1532                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1533                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1534                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1535                  }
1536                  break;
1537               case GL_RGBA:
1538                  for (i=0;i<n;i++) {
1539                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1540                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1541                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1542                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1543                  }
1544                  break;
1545               case GL_BGR:
1546                  for (i=0;i<n;i++) {
1547                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1548                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1549                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1550                  }
1551                  break;
1552               case GL_BGRA:
1553                  for (i=0;i<n;i++) {
1554                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1555                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1556                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1557                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1558                  }
1559                  break;
1560               case GL_ABGR_EXT:
1561                  for (i=0;i<n;i++) {
1562                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1563                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1564                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1565                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1566                  }
1567                  break;
1568               default:
1569                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1570            }
1571            if (dstPacking->SwapBytes) {
1572               _mesa_swap4( (GLuint *) dst, n * comps );
1573            }
1574         }
1575         break;
1576      case GL_INT:
1577         {
1578            GLint *dst = (GLint *) dstAddr;
1579            switch (dstFormat) {
1580               case GL_RED:
1581                  for (i=0;i<n;i++)
1582                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
1583                  break;
1584               case GL_GREEN:
1585                  for (i=0;i<n;i++)
1586                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
1587                  break;
1588               case GL_BLUE:
1589                  for (i=0;i<n;i++)
1590                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
1591                  break;
1592               case GL_ALPHA:
1593                  for (i=0;i<n;i++)
1594                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
1595                  break;
1596               case GL_LUMINANCE:
1597                  for (i=0;i<n;i++)
1598                     dst[i] = FLOAT_TO_INT(luminance[i]);
1599                  break;
1600               case GL_LUMINANCE_ALPHA:
1601                  for (i=0;i<n;i++) {
1602                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
1603                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
1604                  }
1605                  break;
1606               case GL_RGB:
1607                  for (i=0;i<n;i++) {
1608                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1609                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1610                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1611                  }
1612                  break;
1613               case GL_RGBA:
1614                  for (i=0;i<n;i++) {
1615                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1616                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1617                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1618                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1619                  }
1620                  break;
1621               case GL_BGR:
1622                  for (i=0;i<n;i++) {
1623                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1624                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1625                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1626                  }
1627                  break;
1628               case GL_BGRA:
1629                  for (i=0;i<n;i++) {
1630                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1631                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1632                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1633                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1634                  }
1635                  break;
1636               case GL_ABGR_EXT:
1637                  for (i=0;i<n;i++) {
1638                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
1639                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
1640                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
1641                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
1642                  }
1643                  break;
1644               default:
1645                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1646            }
1647            if (dstPacking->SwapBytes) {
1648               _mesa_swap4( (GLuint *) dst, n * comps );
1649            }
1650         }
1651         break;
1652      case GL_FLOAT:
1653         {
1654            GLfloat *dst = (GLfloat *) dstAddr;
1655            switch (dstFormat) {
1656               case GL_RED:
1657                  for (i=0;i<n;i++)
1658                     dst[i] = rgba[i][RCOMP];
1659                  break;
1660               case GL_GREEN:
1661                  for (i=0;i<n;i++)
1662                     dst[i] = rgba[i][GCOMP];
1663                  break;
1664               case GL_BLUE:
1665                  for (i=0;i<n;i++)
1666                     dst[i] = rgba[i][BCOMP];
1667                  break;
1668               case GL_ALPHA:
1669                  for (i=0;i<n;i++)
1670                     dst[i] = rgba[i][ACOMP];
1671                  break;
1672               case GL_LUMINANCE:
1673                  for (i=0;i<n;i++)
1674                     dst[i] = luminance[i];
1675                  break;
1676               case GL_LUMINANCE_ALPHA:
1677                  for (i=0;i<n;i++) {
1678                     dst[i*2+0] = luminance[i];
1679                     dst[i*2+1] = rgba[i][ACOMP];
1680                  }
1681                  break;
1682               case GL_RGB:
1683                  for (i=0;i<n;i++) {
1684                     dst[i*3+0] = rgba[i][RCOMP];
1685                     dst[i*3+1] = rgba[i][GCOMP];
1686                     dst[i*3+2] = rgba[i][BCOMP];
1687                  }
1688                  break;
1689               case GL_RGBA:
1690                  for (i=0;i<n;i++) {
1691                     dst[i*4+0] = rgba[i][RCOMP];
1692                     dst[i*4+1] = rgba[i][GCOMP];
1693                     dst[i*4+2] = rgba[i][BCOMP];
1694                     dst[i*4+3] = rgba[i][ACOMP];
1695                  }
1696                  break;
1697               case GL_BGR:
1698                  for (i=0;i<n;i++) {
1699                     dst[i*3+0] = rgba[i][BCOMP];
1700                     dst[i*3+1] = rgba[i][GCOMP];
1701                     dst[i*3+2] = rgba[i][RCOMP];
1702                  }
1703                  break;
1704               case GL_BGRA:
1705                  for (i=0;i<n;i++) {
1706                     dst[i*4+0] = rgba[i][BCOMP];
1707                     dst[i*4+1] = rgba[i][GCOMP];
1708                     dst[i*4+2] = rgba[i][RCOMP];
1709                     dst[i*4+3] = rgba[i][ACOMP];
1710                  }
1711                  break;
1712               case GL_ABGR_EXT:
1713                  for (i=0;i<n;i++) {
1714                     dst[i*4+0] = rgba[i][ACOMP];
1715                     dst[i*4+1] = rgba[i][BCOMP];
1716                     dst[i*4+2] = rgba[i][GCOMP];
1717                     dst[i*4+3] = rgba[i][RCOMP];
1718                  }
1719                  break;
1720               default:
1721                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1722            }
1723            if (dstPacking->SwapBytes) {
1724               _mesa_swap4( (GLuint *) dst, n * comps );
1725            }
1726         }
1727         break;
1728      case GL_HALF_FLOAT_ARB:
1729         {
1730            GLhalfARB *dst = (GLhalfARB *) dstAddr;
1731            switch (dstFormat) {
1732               case GL_RED:
1733                  for (i=0;i<n;i++)
1734                     dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
1735                  break;
1736               case GL_GREEN:
1737                  for (i=0;i<n;i++)
1738                     dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
1739                  break;
1740               case GL_BLUE:
1741                  for (i=0;i<n;i++)
1742                     dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
1743                  break;
1744               case GL_ALPHA:
1745                  for (i=0;i<n;i++)
1746                     dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
1747                  break;
1748               case GL_LUMINANCE:
1749                  for (i=0;i<n;i++)
1750                     dst[i] = _mesa_float_to_half(luminance[i]);
1751                  break;
1752               case GL_LUMINANCE_ALPHA:
1753                  for (i=0;i<n;i++) {
1754                     dst[i*2+0] = _mesa_float_to_half(luminance[i]);
1755                     dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
1756                  }
1757                  break;
1758               case GL_RGB:
1759                  for (i=0;i<n;i++) {
1760                     dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
1761                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1762                     dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
1763                  }
1764                  break;
1765               case GL_RGBA:
1766                  for (i=0;i<n;i++) {
1767                     dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
1768                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1769                     dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
1770                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
1771                  }
1772                  break;
1773               case GL_BGR:
1774                  for (i=0;i<n;i++) {
1775                     dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
1776                     dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1777                     dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
1778                  }
1779                  break;
1780               case GL_BGRA:
1781                  for (i=0;i<n;i++) {
1782                     dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
1783                     dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
1784                     dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
1785                     dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
1786                  }
1787                  break;
1788               case GL_ABGR_EXT:
1789                  for (i=0;i<n;i++) {
1790                     dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
1791                     dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
1792                     dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
1793                     dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
1794                  }
1795                  break;
1796               default:
1797                  _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1798            }
1799            if (dstPacking->SwapBytes) {
1800               _mesa_swap2( (GLushort *) dst, n * comps );
1801            }
1802         }
1803         break;
1804      case GL_UNSIGNED_BYTE_3_3_2:
1805         if (dstFormat == GL_RGB) {
1806            GLubyte *dst = (GLubyte *) dstAddr;
1807            for (i=0;i<n;i++) {
1808               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F)) << 5)
1809                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 2)
1810                      | (((GLint) (rgba[i][BCOMP] * 3.0F))     );
1811            }
1812         }
1813         break;
1814      case GL_UNSIGNED_BYTE_2_3_3_REV:
1815         if (dstFormat == GL_RGB) {
1816            GLubyte *dst = (GLubyte *) dstAddr;
1817            for (i=0;i<n;i++) {
1818               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F))     )
1819                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 3)
1820                      | (((GLint) (rgba[i][BCOMP] * 3.0F)) << 6);
1821            }
1822         }
1823         break;
1824      case GL_UNSIGNED_SHORT_5_6_5:
1825         if (dstFormat == GL_RGB) {
1826            GLushort *dst = (GLushort *) dstAddr;
1827            for (i=0;i<n;i++) {
1828               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1829                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1830                      | (((GLint) (rgba[i][BCOMP] * 31.0F))      );
1831            }
1832         }
1833         break;
1834      case GL_UNSIGNED_SHORT_5_6_5_REV:
1835         if (dstFormat == GL_RGB) {
1836            GLushort *dst = (GLushort *) dstAddr;
1837            for (i=0;i<n;i++) {
1838               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1839                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1840                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11);
1841            }
1842         }
1843         break;
1844      case GL_UNSIGNED_SHORT_4_4_4_4:
1845         if (dstFormat == GL_RGBA) {
1846            GLushort *dst = (GLushort *) dstAddr;
1847            for (i=0;i<n;i++) {
1848               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12)
1849                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1850                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
1851                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1852            }
1853         }
1854         else if (dstFormat == GL_BGRA) {
1855            GLushort *dst = (GLushort *) dstAddr;
1856            for (i=0;i<n;i++) {
1857               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F)) << 12)
1858                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1859                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  4)
1860                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1861            }
1862         }
1863         else if (dstFormat == GL_ABGR_EXT) {
1864            GLushort *dst = (GLushort *) dstAddr;
1865            for (i=0;i<n;i++) {
1866               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12)
1867                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
1868                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1869                      | (((GLint) (rgba[i][RCOMP] * 15.0F))      );
1870            }
1871         }
1872         break;
1873      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1874         if (dstFormat == GL_RGBA) {
1875            GLushort *dst = (GLushort *) dstAddr;
1876            for (i=0;i<n;i++) {
1877               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F))      )
1878                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1879                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
1880                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1881            }
1882         }
1883         else if (dstFormat == GL_BGRA) {
1884            GLushort *dst = (GLushort *) dstAddr;
1885            for (i=0;i<n;i++) {
1886               dst[i] = (((GLint) (rgba[i][BCOMP] * 15.0F))      )
1887                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1888                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) <<  8)
1889                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1890            }
1891         }
1892         else if (dstFormat == GL_ABGR_EXT) {
1893            GLushort *dst = (GLushort *) dstAddr;
1894            for (i=0;i<n;i++) {
1895               dst[i] = (((GLint) (rgba[i][ACOMP] * 15.0F))      )
1896                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
1897                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1898                      | (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12);
1899            }
1900         }
1901         break;
1902      case GL_UNSIGNED_SHORT_5_5_5_1:
1903         if (dstFormat == GL_RGBA) {
1904            GLushort *dst = (GLushort *) dstAddr;
1905            for (i=0;i<n;i++) {
1906               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1907                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1908                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  1)
1909                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1910            }
1911         }
1912         else if (dstFormat == GL_BGRA) {
1913            GLushort *dst = (GLushort *) dstAddr;
1914            for (i=0;i<n;i++) {
1915               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11)
1916                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1917                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) <<  1)
1918                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1919            }
1920         }
1921         else if (dstFormat == GL_ABGR_EXT) {
1922            GLushort *dst = (GLushort *) dstAddr;
1923            for (i=0;i<n;i++) {
1924               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F)) << 11)
1925                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  6)
1926                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  1)
1927                      | (((GLint) (rgba[i][RCOMP] *  1.0F))      );
1928            }
1929         }
1930         break;
1931      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1932         if (dstFormat == GL_RGBA) {
1933            GLushort *dst = (GLushort *) dstAddr;
1934            for (i=0;i<n;i++) {
1935               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1936                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1937                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 10)
1938                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1939            }
1940         }
1941         else if (dstFormat == GL_BGRA) {
1942            GLushort *dst = (GLushort *) dstAddr;
1943            for (i=0;i<n;i++) {
1944               dst[i] = (((GLint) (rgba[i][BCOMP] * 31.0F))      )
1945                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1946                      | (((GLint) (rgba[i][RCOMP] * 31.0F)) << 10)
1947                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1948            }
1949         }
1950         else if (dstFormat == GL_ABGR_EXT) {
1951            GLushort *dst = (GLushort *) dstAddr;
1952            for (i=0;i<n;i++) {
1953               dst[i] = (((GLint) (rgba[i][ACOMP] * 31.0F))      )
1954                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  5)
1955                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) << 10)
1956                      | (((GLint) (rgba[i][RCOMP] *  1.0F)) << 15);
1957            }
1958         }
1959         break;
1960      case GL_UNSIGNED_INT_8_8_8_8:
1961         if (dstFormat == GL_RGBA) {
1962            GLuint *dst = (GLuint *) dstAddr;
1963            for (i=0;i<n;i++) {
1964               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24)
1965                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1966                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
1967                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1968            }
1969         }
1970         else if (dstFormat == GL_BGRA) {
1971            GLuint *dst = (GLuint *) dstAddr;
1972            for (i=0;i<n;i++) {
1973               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 24)
1974                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1975                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) <<  8)
1976                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1977            }
1978         }
1979         else if (dstFormat == GL_ABGR_EXT) {
1980            GLuint *dst = (GLuint *) dstAddr;
1981            for (i=0;i<n;i++) {
1982               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24)
1983                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
1984                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1985                      | (((GLuint) (rgba[i][RCOMP] * 255.0F))      );
1986            }
1987         }
1988         break;
1989      case GL_UNSIGNED_INT_8_8_8_8_REV:
1990         if (dstFormat == GL_RGBA) {
1991            GLuint *dst = (GLuint *) dstAddr;
1992            for (i=0;i<n;i++) {
1993               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F))      )
1994                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1995                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
1996                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1997            }
1998         }
1999         else if (dstFormat == GL_BGRA) {
2000            GLuint *dst = (GLuint *) dstAddr;
2001            for (i=0;i<n;i++) {
2002               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F))      )
2003                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
2004                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 16)
2005                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
2006            }
2007         }
2008         else if (dstFormat == GL_ABGR_EXT) {
2009            GLuint *dst = (GLuint *) dstAddr;
2010            for (i=0;i<n;i++) {
2011               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F))      )
2012                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
2013                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
2014                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24);
2015            }
2016         }
2017         break;
2018      case GL_UNSIGNED_INT_10_10_10_2:
2019         if (dstFormat == GL_RGBA) {
2020            GLuint *dst = (GLuint *) dstAddr;
2021            for (i=0;i<n;i++) {
2022               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 22)
2023                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
2024                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) <<  2)
2025                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
2026            }
2027         }
2028         else if (dstFormat == GL_BGRA) {
2029            GLuint *dst = (GLuint *) dstAddr;
2030            for (i=0;i<n;i++) {
2031               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 22)
2032                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
2033                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) <<  2)
2034                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
2035            }
2036         }
2037         else if (dstFormat == GL_ABGR_EXT) {
2038            GLuint *dst = (GLuint *) dstAddr;
2039            for (i=0;i<n;i++) {
2040               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F)) << 22)
2041                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 12)
2042                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) <<  2)
2043                      | (((GLuint) (rgba[i][RCOMP] *    3.0F))      );
2044            }
2045         }
2046         break;
2047      case GL_UNSIGNED_INT_2_10_10_10_REV:
2048         if (dstFormat == GL_RGBA) {
2049            GLuint *dst = (GLuint *) dstAddr;
2050            for (i=0;i<n;i++) {
2051               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F))      )
2052                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
2053                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 20)
2054                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
2055            }
2056         }
2057         else if (dstFormat == GL_BGRA) {
2058            GLuint *dst = (GLuint *) dstAddr;
2059            for (i=0;i<n;i++) {
2060               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F))      )
2061                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
2062                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 20)
2063                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
2064            }
2065         }
2066         else if (dstFormat == GL_ABGR_EXT) {
2067            GLuint *dst = (GLuint *) dstAddr;
2068            for (i=0;i<n;i++) {
2069               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F))      )
2070                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 10)
2071                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 20)
2072                      | (((GLuint) (rgba[i][RCOMP] *    3.0F)) << 30);
2073            }
2074         }
2075         break;
2076      default:
2077         _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2078   }
2079}
2080
2081
2082#define SWAP2BYTE(VALUE)			\
2083   {						\
2084      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2085      GLubyte tmp = bytes[0];			\
2086      bytes[0] = bytes[1];			\
2087      bytes[1] = tmp;				\
2088   }
2089
2090#define SWAP4BYTE(VALUE)			\
2091   {						\
2092      GLubyte *bytes = (GLubyte *) &(VALUE);	\
2093      GLubyte tmp = bytes[0];			\
2094      bytes[0] = bytes[3];			\
2095      bytes[3] = tmp;				\
2096      tmp = bytes[1];				\
2097      bytes[1] = bytes[2];			\
2098      bytes[2] = tmp;				\
2099   }
2100
2101
2102static void
2103extract_uint_indexes(GLuint n, GLuint indexes[],
2104                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
2105                     const struct gl_pixelstore_attrib *unpack )
2106{
2107   ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2108
2109   ASSERT(srcType == GL_BITMAP ||
2110          srcType == GL_UNSIGNED_BYTE ||
2111          srcType == GL_BYTE ||
2112          srcType == GL_UNSIGNED_SHORT ||
2113          srcType == GL_SHORT ||
2114          srcType == GL_UNSIGNED_INT ||
2115          srcType == GL_INT ||
2116          srcType == GL_UNSIGNED_INT_24_8_EXT ||
2117          srcType == GL_HALF_FLOAT_ARB ||
2118          srcType == GL_FLOAT);
2119
2120   switch (srcType) {
2121      case GL_BITMAP:
2122         {
2123            GLubyte *ubsrc = (GLubyte *) src;
2124            if (unpack->LsbFirst) {
2125               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2126               GLuint i;
2127               for (i = 0; i < n; i++) {
2128                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2129                  if (mask == 128) {
2130                     mask = 1;
2131                     ubsrc++;
2132                  }
2133                  else {
2134                     mask = mask << 1;
2135                  }
2136               }
2137            }
2138            else {
2139               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2140               GLuint i;
2141               for (i = 0; i < n; i++) {
2142                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
2143                  if (mask == 1) {
2144                     mask = 128;
2145                     ubsrc++;
2146                  }
2147                  else {
2148                     mask = mask >> 1;
2149                  }
2150               }
2151            }
2152         }
2153         break;
2154      case GL_UNSIGNED_BYTE:
2155         {
2156            GLuint i;
2157            const GLubyte *s = (const GLubyte *) src;
2158            for (i = 0; i < n; i++)
2159               indexes[i] = s[i];
2160         }
2161         break;
2162      case GL_BYTE:
2163         {
2164            GLuint i;
2165            const GLbyte *s = (const GLbyte *) src;
2166            for (i = 0; i < n; i++)
2167               indexes[i] = s[i];
2168         }
2169         break;
2170      case GL_UNSIGNED_SHORT:
2171         {
2172            GLuint i;
2173            const GLushort *s = (const GLushort *) src;
2174            if (unpack->SwapBytes) {
2175               for (i = 0; i < n; i++) {
2176                  GLushort value = s[i];
2177                  SWAP2BYTE(value);
2178                  indexes[i] = value;
2179               }
2180            }
2181            else {
2182               for (i = 0; i < n; i++)
2183                  indexes[i] = s[i];
2184            }
2185         }
2186         break;
2187      case GL_SHORT:
2188         {
2189            GLuint i;
2190            const GLshort *s = (const GLshort *) src;
2191            if (unpack->SwapBytes) {
2192               for (i = 0; i < n; i++) {
2193                  GLshort value = s[i];
2194                  SWAP2BYTE(value);
2195                  indexes[i] = value;
2196               }
2197            }
2198            else {
2199               for (i = 0; i < n; i++)
2200                  indexes[i] = s[i];
2201            }
2202         }
2203         break;
2204      case GL_UNSIGNED_INT:
2205         {
2206            GLuint i;
2207            const GLuint *s = (const GLuint *) src;
2208            if (unpack->SwapBytes) {
2209               for (i = 0; i < n; i++) {
2210                  GLuint value = s[i];
2211                  SWAP4BYTE(value);
2212                  indexes[i] = value;
2213               }
2214            }
2215            else {
2216               for (i = 0; i < n; i++)
2217                  indexes[i] = s[i];
2218            }
2219         }
2220         break;
2221      case GL_INT:
2222         {
2223            GLuint i;
2224            const GLint *s = (const GLint *) src;
2225            if (unpack->SwapBytes) {
2226               for (i = 0; i < n; i++) {
2227                  GLint value = s[i];
2228                  SWAP4BYTE(value);
2229                  indexes[i] = value;
2230               }
2231            }
2232            else {
2233               for (i = 0; i < n; i++)
2234                  indexes[i] = s[i];
2235            }
2236         }
2237         break;
2238      case GL_FLOAT:
2239         {
2240            GLuint i;
2241            const GLfloat *s = (const GLfloat *) src;
2242            if (unpack->SwapBytes) {
2243               for (i = 0; i < n; i++) {
2244                  GLfloat value = s[i];
2245                  SWAP4BYTE(value);
2246                  indexes[i] = (GLuint) value;
2247               }
2248            }
2249            else {
2250               for (i = 0; i < n; i++)
2251                  indexes[i] = (GLuint) s[i];
2252            }
2253         }
2254         break;
2255      case GL_HALF_FLOAT_ARB:
2256         {
2257            GLuint i;
2258            const GLhalfARB *s = (const GLhalfARB *) src;
2259            if (unpack->SwapBytes) {
2260               for (i = 0; i < n; i++) {
2261                  GLhalfARB value = s[i];
2262                  SWAP2BYTE(value);
2263                  indexes[i] = (GLuint) _mesa_half_to_float(value);
2264               }
2265            }
2266            else {
2267               for (i = 0; i < n; i++)
2268                  indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
2269            }
2270         }
2271         break;
2272      case GL_UNSIGNED_INT_24_8_EXT:
2273         {
2274            GLuint i;
2275            const GLuint *s = (const GLuint *) src;
2276            if (unpack->SwapBytes) {
2277               for (i = 0; i < n; i++) {
2278                  GLuint value = s[i];
2279                  SWAP4BYTE(value);
2280                  indexes[i] = value & 0xff;  /* lower 8 bits */
2281               }
2282            }
2283            else {
2284               for (i = 0; i < n; i++)
2285                  indexes[i] = s[i] & 0xfff;  /* lower 8 bits */
2286            }
2287         }
2288         break;
2289
2290      default:
2291         _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
2292         return;
2293   }
2294}
2295
2296
2297/*
2298 * This function extracts floating point RGBA values from arbitrary
2299 * image data.  srcFormat and srcType are the format and type parameters
2300 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2301 *
2302 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2303 * implements the "Conversion to floating point", "Conversion to RGB",
2304 * and "Final Expansion to RGBA" operations.
2305 *
2306 * Args:  n - number of pixels
2307 *        rgba - output colors
2308 *        srcFormat - format of incoming data
2309 *        srcType - data type of incoming data
2310 *        src - source data pointer
2311 *        swapBytes - perform byteswapping of incoming data?
2312 */
2313static void
2314extract_float_rgba(GLuint n, GLfloat rgba[][4],
2315                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
2316                   GLboolean swapBytes)
2317{
2318   GLint redIndex, greenIndex, blueIndex, alphaIndex;
2319   GLint stride;
2320   GLint rComp, bComp, gComp, aComp;
2321
2322   ASSERT(srcFormat == GL_RED ||
2323          srcFormat == GL_GREEN ||
2324          srcFormat == GL_BLUE ||
2325          srcFormat == GL_ALPHA ||
2326          srcFormat == GL_LUMINANCE ||
2327          srcFormat == GL_LUMINANCE_ALPHA ||
2328          srcFormat == GL_INTENSITY ||
2329          srcFormat == GL_RGB ||
2330          srcFormat == GL_BGR ||
2331          srcFormat == GL_RGBA ||
2332          srcFormat == GL_BGRA ||
2333          srcFormat == GL_ABGR_EXT);
2334
2335   ASSERT(srcType == GL_UNSIGNED_BYTE ||
2336          srcType == GL_BYTE ||
2337          srcType == GL_UNSIGNED_SHORT ||
2338          srcType == GL_SHORT ||
2339          srcType == GL_UNSIGNED_INT ||
2340          srcType == GL_INT ||
2341          srcType == GL_HALF_FLOAT_ARB ||
2342          srcType == GL_FLOAT ||
2343          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2344          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2345          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2346          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2347          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2348          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2349          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2350          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2351          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2352          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2353          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2354          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2355
2356   rComp = gComp = bComp = aComp = -1;
2357
2358   switch (srcFormat) {
2359      case GL_RED:
2360         redIndex = 0;
2361         greenIndex = blueIndex = alphaIndex = -1;
2362         stride = 1;
2363         break;
2364      case GL_GREEN:
2365         greenIndex = 0;
2366         redIndex = blueIndex = alphaIndex = -1;
2367         stride = 1;
2368         break;
2369      case GL_BLUE:
2370         blueIndex = 0;
2371         redIndex = greenIndex = alphaIndex = -1;
2372         stride = 1;
2373         break;
2374      case GL_ALPHA:
2375         redIndex = greenIndex = blueIndex = -1;
2376         alphaIndex = 0;
2377         stride = 1;
2378         break;
2379      case GL_LUMINANCE:
2380         redIndex = greenIndex = blueIndex = 0;
2381         alphaIndex = -1;
2382         stride = 1;
2383         break;
2384      case GL_LUMINANCE_ALPHA:
2385         redIndex = greenIndex = blueIndex = 0;
2386         alphaIndex = 1;
2387         stride = 2;
2388         break;
2389      case GL_INTENSITY:
2390         redIndex = greenIndex = blueIndex = alphaIndex = 0;
2391         stride = 1;
2392         break;
2393      case GL_RGB:
2394         redIndex = 0;
2395         greenIndex = 1;
2396         blueIndex = 2;
2397         alphaIndex = -1;
2398         rComp = 0;
2399         gComp = 1;
2400         bComp = 2;
2401         aComp = 3;
2402         stride = 3;
2403         break;
2404      case GL_BGR:
2405         redIndex = 2;
2406         greenIndex = 1;
2407         blueIndex = 0;
2408         alphaIndex = -1;
2409         rComp = 2;
2410         gComp = 1;
2411         bComp = 0;
2412         aComp = 3;
2413         stride = 3;
2414         break;
2415      case GL_RGBA:
2416         redIndex = 0;
2417         greenIndex = 1;
2418         blueIndex = 2;
2419         alphaIndex = 3;
2420         rComp = 0;
2421         gComp = 1;
2422         bComp = 2;
2423         aComp = 3;
2424         stride = 4;
2425         break;
2426      case GL_BGRA:
2427         redIndex = 2;
2428         greenIndex = 1;
2429         blueIndex = 0;
2430         alphaIndex = 3;
2431         rComp = 2;
2432         gComp = 1;
2433         bComp = 0;
2434         aComp = 3;
2435         stride = 4;
2436         break;
2437      case GL_ABGR_EXT:
2438         redIndex = 3;
2439         greenIndex = 2;
2440         blueIndex = 1;
2441         alphaIndex = 0;
2442         rComp = 3;
2443         gComp = 2;
2444         bComp = 1;
2445         aComp = 0;
2446         stride = 4;
2447         break;
2448      default:
2449         _mesa_problem(NULL, "bad srcFormat in extract float data");
2450         return;
2451   }
2452
2453
2454#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)		\
2455   if ((INDEX) < 0) {							\
2456      GLuint i;								\
2457      for (i = 0; i < n; i++) {						\
2458         rgba[i][CHANNEL] = DEFAULT;					\
2459      }									\
2460   }									\
2461   else if (swapBytes) {						\
2462      const TYPE *s = (const TYPE *) src;				\
2463      GLuint i;								\
2464      for (i = 0; i < n; i++) {						\
2465         TYPE value = s[INDEX];						\
2466         if (sizeof(TYPE) == 2) {					\
2467            SWAP2BYTE(value);						\
2468         }								\
2469         else if (sizeof(TYPE) == 4) {					\
2470            SWAP4BYTE(value);						\
2471         }								\
2472         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);		\
2473         s += stride;							\
2474      }									\
2475   }									\
2476   else {								\
2477      const TYPE *s = (const TYPE *) src;				\
2478      GLuint i;								\
2479      for (i = 0; i < n; i++) {						\
2480         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);		\
2481         s += stride;							\
2482      }									\
2483   }
2484
2485   switch (srcType) {
2486      case GL_UNSIGNED_BYTE:
2487         PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2488         PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2489         PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
2490         PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
2491         break;
2492      case GL_BYTE:
2493         PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2494         PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2495         PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
2496         PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
2497         break;
2498      case GL_UNSIGNED_SHORT:
2499         PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2500         PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2501         PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
2502         PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
2503         break;
2504      case GL_SHORT:
2505         PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2506         PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2507         PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
2508         PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
2509         break;
2510      case GL_UNSIGNED_INT:
2511         PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2512         PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2513         PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
2514         PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
2515         break;
2516      case GL_INT:
2517         PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
2518         PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
2519         PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
2520         PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
2521         break;
2522      case GL_FLOAT:
2523         PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
2524         PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
2525         PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
2526         PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
2527         break;
2528      case GL_HALF_FLOAT_ARB:
2529         PROCESS(redIndex,   RCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
2530         PROCESS(greenIndex, GCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
2531         PROCESS(blueIndex,  BCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
2532         PROCESS(alphaIndex, ACOMP, 1.0F, GLhalfARB, _mesa_half_to_float);
2533         break;
2534      case GL_UNSIGNED_BYTE_3_3_2:
2535         {
2536            const GLubyte *ubsrc = (const GLubyte *) src;
2537            GLuint i;
2538            for (i = 0; i < n; i ++) {
2539               GLubyte p = ubsrc[i];
2540               rgba[i][rComp] = ((p >> 5)      ) * (1.0F / 7.0F);
2541               rgba[i][gComp] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
2542               rgba[i][bComp] = ((p     ) & 0x3) * (1.0F / 3.0F);
2543               rgba[i][aComp] = 1.0F;
2544            }
2545         }
2546         break;
2547      case GL_UNSIGNED_BYTE_2_3_3_REV:
2548         {
2549            const GLubyte *ubsrc = (const GLubyte *) src;
2550            GLuint i;
2551            for (i = 0; i < n; i ++) {
2552               GLubyte p = ubsrc[i];
2553               rgba[i][rComp] = ((p     ) & 0x7) * (1.0F / 7.0F);
2554               rgba[i][gComp] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
2555               rgba[i][bComp] = ((p >> 6)      ) * (1.0F / 3.0F);
2556               rgba[i][aComp] = 1.0F;
2557            }
2558         }
2559         break;
2560      case GL_UNSIGNED_SHORT_5_6_5:
2561         if (swapBytes) {
2562            const GLushort *ussrc = (const GLushort *) src;
2563            GLuint i;
2564            for (i = 0; i < n; i ++) {
2565               GLushort p = ussrc[i];
2566               SWAP2BYTE(p);
2567               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2568               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2569               rgba[i][bComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2570               rgba[i][aComp] = 1.0F;
2571            }
2572         }
2573         else {
2574            const GLushort *ussrc = (const GLushort *) src;
2575            GLuint i;
2576            for (i = 0; i < n; i ++) {
2577               GLushort p = ussrc[i];
2578               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2579               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2580               rgba[i][bComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2581               rgba[i][aComp] = 1.0F;
2582            }
2583         }
2584         break;
2585      case GL_UNSIGNED_SHORT_5_6_5_REV:
2586         if (swapBytes) {
2587            const GLushort *ussrc = (const GLushort *) src;
2588            GLuint i;
2589            for (i = 0; i < n; i ++) {
2590               GLushort p = ussrc[i];
2591               SWAP2BYTE(p);
2592               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2593               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2594               rgba[i][bComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2595               rgba[i][aComp] = 1.0F;
2596            }
2597         }
2598         else {
2599            const GLushort *ussrc = (const GLushort *) src;
2600            GLuint i;
2601            for (i = 0; i < n; i ++) {
2602               GLushort p = ussrc[i];
2603               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2604               rgba[i][gComp] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
2605               rgba[i][bComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2606               rgba[i][aComp] = 1.0F;
2607            }
2608         }
2609         break;
2610      case GL_UNSIGNED_SHORT_4_4_4_4:
2611         if (swapBytes) {
2612            const GLushort *ussrc = (const GLushort *) src;
2613            GLuint i;
2614            for (i = 0; i < n; i ++) {
2615               GLushort p = ussrc[i];
2616               SWAP2BYTE(p);
2617               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2618               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2619               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2620               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2621            }
2622         }
2623         else {
2624            const GLushort *ussrc = (const GLushort *) src;
2625            GLuint i;
2626            for (i = 0; i < n; i ++) {
2627               GLushort p = ussrc[i];
2628               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2629               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2630               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2631               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2632            }
2633         }
2634         break;
2635      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2636         if (swapBytes) {
2637            const GLushort *ussrc = (const GLushort *) src;
2638            GLuint i;
2639            for (i = 0; i < n; i ++) {
2640               GLushort p = ussrc[i];
2641               SWAP2BYTE(p);
2642               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2643               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2644               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2645               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2646            }
2647         }
2648         else {
2649            const GLushort *ussrc = (const GLushort *) src;
2650            GLuint i;
2651            for (i = 0; i < n; i ++) {
2652               GLushort p = ussrc[i];
2653               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
2654               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
2655               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
2656               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
2657            }
2658         }
2659         break;
2660      case GL_UNSIGNED_SHORT_5_5_5_1:
2661         if (swapBytes) {
2662            const GLushort *ussrc = (const GLushort *) src;
2663            GLuint i;
2664            for (i = 0; i < n; i ++) {
2665               GLushort p = ussrc[i];
2666               SWAP2BYTE(p);
2667               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2668               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2669               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2670               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2671            }
2672         }
2673         else {
2674            const GLushort *ussrc = (const GLushort *) src;
2675            GLuint i;
2676            for (i = 0; i < n; i ++) {
2677               GLushort p = ussrc[i];
2678               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2679               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2680               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2681               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2682            }
2683         }
2684         break;
2685      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2686         if (swapBytes) {
2687            const GLushort *ussrc = (const GLushort *) src;
2688            GLuint i;
2689            for (i = 0; i < n; i ++) {
2690               GLushort p = ussrc[i];
2691               SWAP2BYTE(p);
2692               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2693               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2694               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2695               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2696            }
2697         }
2698         else {
2699            const GLushort *ussrc = (const GLushort *) src;
2700            GLuint i;
2701            for (i = 0; i < n; i ++) {
2702               GLushort p = ussrc[i];
2703               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2704               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2705               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2706               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2707            }
2708         }
2709         break;
2710      case GL_UNSIGNED_INT_8_8_8_8:
2711         if (swapBytes) {
2712            const GLuint *uisrc = (const GLuint *) src;
2713            GLuint i;
2714            for (i = 0; i < n; i ++) {
2715               GLuint p = uisrc[i];
2716               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2717               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2718               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2719               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
2720            }
2721         }
2722         else {
2723            const GLuint *uisrc = (const GLuint *) src;
2724            GLuint i;
2725            for (i = 0; i < n; i ++) {
2726               GLuint p = uisrc[i];
2727               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
2728               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2729               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2730               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2731            }
2732         }
2733         break;
2734      case GL_UNSIGNED_INT_8_8_8_8_REV:
2735         if (swapBytes) {
2736            const GLuint *uisrc = (const GLuint *) src;
2737            GLuint i;
2738            for (i = 0; i < n; i ++) {
2739               GLuint p = uisrc[i];
2740               rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24)       );
2741               rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2742               rgba[i][bComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2743               rgba[i][aComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2744            }
2745         }
2746         else {
2747            const GLuint *uisrc = (const GLuint *) src;
2748            GLuint i;
2749            for (i = 0; i < n; i ++) {
2750               GLuint p = uisrc[i];
2751               rgba[i][rComp] = UBYTE_TO_FLOAT((p      ) & 0xff);
2752               rgba[i][gComp] = UBYTE_TO_FLOAT((p >>  8) & 0xff);
2753               rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
2754               rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24)       );
2755            }
2756         }
2757         break;
2758      case GL_UNSIGNED_INT_10_10_10_2:
2759         if (swapBytes) {
2760            const GLuint *uisrc = (const GLuint *) src;
2761            GLuint i;
2762            for (i = 0; i < n; i ++) {
2763               GLuint p = uisrc[i];
2764               SWAP4BYTE(p);
2765               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2766               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2767               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2768               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2769            }
2770         }
2771         else {
2772            const GLuint *uisrc = (const GLuint *) src;
2773            GLuint i;
2774            for (i = 0; i < n; i ++) {
2775               GLuint p = uisrc[i];
2776               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2777               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2778               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2779               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2780            }
2781         }
2782         break;
2783      case GL_UNSIGNED_INT_2_10_10_10_REV:
2784         if (swapBytes) {
2785            const GLuint *uisrc = (const GLuint *) src;
2786            GLuint i;
2787            for (i = 0; i < n; i ++) {
2788               GLuint p = uisrc[i];
2789               SWAP4BYTE(p);
2790               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2791               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2792               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2793               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2794            }
2795         }
2796         else {
2797            const GLuint *uisrc = (const GLuint *) src;
2798            GLuint i;
2799            for (i = 0; i < n; i ++) {
2800               GLuint p = uisrc[i];
2801               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2802               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2803               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2804               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2805            }
2806         }
2807         break;
2808      default:
2809         _mesa_problem(NULL, "bad srcType in extract float data");
2810         break;
2811   }
2812}
2813
2814
2815/*
2816 * Unpack a row of color image data from a client buffer according to
2817 * the pixel unpacking parameters.
2818 * Return GLchan values in the specified dest image format.
2819 * This is used by glDrawPixels and glTexImage?D().
2820 * \param ctx - the context
2821 *         n - number of pixels in the span
2822 *         dstFormat - format of destination color array
2823 *         dest - the destination color array
2824 *         srcFormat - source image format
2825 *         srcType - source image  data type
2826 *         source - source image pointer
2827 *         srcPacking - pixel unpacking parameters
2828 *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
2829 *
2830 * XXX perhaps expand this to process whole images someday.
2831 */
2832void
2833_mesa_unpack_color_span_chan( GLcontext *ctx,
2834                              GLuint n, GLenum dstFormat, GLchan dest[],
2835                              GLenum srcFormat, GLenum srcType,
2836                              const GLvoid *source,
2837                              const struct gl_pixelstore_attrib *srcPacking,
2838                              GLbitfield transferOps )
2839{
2840   ASSERT(dstFormat == GL_ALPHA ||
2841          dstFormat == GL_LUMINANCE ||
2842          dstFormat == GL_LUMINANCE_ALPHA ||
2843          dstFormat == GL_INTENSITY ||
2844          dstFormat == GL_RGB ||
2845          dstFormat == GL_RGBA ||
2846          dstFormat == GL_COLOR_INDEX);
2847
2848   ASSERT(srcFormat == GL_RED ||
2849          srcFormat == GL_GREEN ||
2850          srcFormat == GL_BLUE ||
2851          srcFormat == GL_ALPHA ||
2852          srcFormat == GL_LUMINANCE ||
2853          srcFormat == GL_LUMINANCE_ALPHA ||
2854          srcFormat == GL_INTENSITY ||
2855          srcFormat == GL_RGB ||
2856          srcFormat == GL_BGR ||
2857          srcFormat == GL_RGBA ||
2858          srcFormat == GL_BGRA ||
2859          srcFormat == GL_ABGR_EXT ||
2860          srcFormat == GL_COLOR_INDEX);
2861
2862   ASSERT(srcType == GL_BITMAP ||
2863          srcType == GL_UNSIGNED_BYTE ||
2864          srcType == GL_BYTE ||
2865          srcType == GL_UNSIGNED_SHORT ||
2866          srcType == GL_SHORT ||
2867          srcType == GL_UNSIGNED_INT ||
2868          srcType == GL_INT ||
2869          srcType == GL_HALF_FLOAT_ARB ||
2870          srcType == GL_FLOAT ||
2871          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2872          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2873          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2874          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2875          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2876          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2877          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2878          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2879          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2880          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2881          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2882          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2883
2884   /* Try simple cases first */
2885   if (transferOps == 0) {
2886      if (srcType == CHAN_TYPE) {
2887         if (dstFormat == GL_RGBA) {
2888            if (srcFormat == GL_RGBA) {
2889               _mesa_memcpy( dest, source, n * 4 * sizeof(GLchan) );
2890               return;
2891            }
2892            else if (srcFormat == GL_RGB) {
2893               GLuint i;
2894               const GLchan *src = (const GLchan *) source;
2895               GLchan *dst = dest;
2896               for (i = 0; i < n; i++) {
2897                  dst[0] = src[0];
2898                  dst[1] = src[1];
2899                  dst[2] = src[2];
2900                  dst[3] = CHAN_MAX;
2901                  src += 3;
2902                  dst += 4;
2903               }
2904               return;
2905            }
2906         }
2907         else if (dstFormat == GL_RGB) {
2908            if (srcFormat == GL_RGB) {
2909               _mesa_memcpy( dest, source, n * 3 * sizeof(GLchan) );
2910               return;
2911            }
2912            else if (srcFormat == GL_RGBA) {
2913               GLuint i;
2914               const GLchan *src = (const GLchan *) source;
2915               GLchan *dst = dest;
2916               for (i = 0; i < n; i++) {
2917                  dst[0] = src[0];
2918                  dst[1] = src[1];
2919                  dst[2] = src[2];
2920                  src += 4;
2921                  dst += 3;
2922               }
2923               return;
2924            }
2925         }
2926         else if (dstFormat == srcFormat) {
2927            GLint comps = _mesa_components_in_format(srcFormat);
2928            assert(comps > 0);
2929            _mesa_memcpy( dest, source, n * comps * sizeof(GLchan) );
2930            return;
2931         }
2932      }
2933      /*
2934       * Common situation, loading 8bit RGBA/RGB source images
2935       * into 16/32 bit destination. (OSMesa16/32)
2936       */
2937      else if (srcType == GL_UNSIGNED_BYTE) {
2938         if (dstFormat == GL_RGBA) {
2939            if (srcFormat == GL_RGB) {
2940               GLuint i;
2941               const GLubyte *src = (const GLubyte *) source;
2942               GLchan *dst = dest;
2943               for (i = 0; i < n; i++) {
2944                  dst[0] = UBYTE_TO_CHAN(src[0]);
2945                  dst[1] = UBYTE_TO_CHAN(src[1]);
2946                  dst[2] = UBYTE_TO_CHAN(src[2]);
2947                  dst[3] = CHAN_MAX;
2948                  src += 3;
2949                  dst += 4;
2950               }
2951               return;
2952            }
2953            else if (srcFormat == GL_RGBA) {
2954               GLuint i;
2955               const GLubyte *src = (const GLubyte *) source;
2956               GLchan *dst = dest;
2957               for (i = 0; i < n; i++) {
2958                  dst[0] = UBYTE_TO_CHAN(src[0]);
2959                  dst[1] = UBYTE_TO_CHAN(src[1]);
2960                  dst[2] = UBYTE_TO_CHAN(src[2]);
2961                  dst[3] = UBYTE_TO_CHAN(src[3]);
2962                  src += 4;
2963                  dst += 4;
2964               }
2965               return;
2966             }
2967         }
2968         else if (dstFormat == GL_RGB) {
2969            if (srcFormat == GL_RGB) {
2970               GLuint i;
2971               const GLubyte *src = (const GLubyte *) source;
2972               GLchan *dst = dest;
2973               for (i = 0; i < n; i++) {
2974                  dst[0] = UBYTE_TO_CHAN(src[0]);
2975                  dst[1] = UBYTE_TO_CHAN(src[1]);
2976                  dst[2] = UBYTE_TO_CHAN(src[2]);
2977                  src += 3;
2978                  dst += 3;
2979               }
2980               return;
2981            }
2982            else if (srcFormat == GL_RGBA) {
2983               GLuint i;
2984               const GLubyte *src = (const GLubyte *) source;
2985               GLchan *dst = dest;
2986               for (i = 0; i < n; i++) {
2987                  dst[0] = UBYTE_TO_CHAN(src[0]);
2988                  dst[1] = UBYTE_TO_CHAN(src[1]);
2989                  dst[2] = UBYTE_TO_CHAN(src[2]);
2990                  src += 4;
2991                  dst += 3;
2992               }
2993               return;
2994            }
2995         }
2996      }
2997   }
2998
2999
3000   /* general solution begins here */
3001   {
3002      GLint dstComponents;
3003      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3004      GLint dstLuminanceIndex, dstIntensityIndex;
3005      GLfloat rgba[MAX_WIDTH][4];
3006
3007      dstComponents = _mesa_components_in_format( dstFormat );
3008      /* source & dest image formats should have been error checked by now */
3009      assert(dstComponents > 0);
3010
3011      /*
3012       * Extract image data and convert to RGBA floats
3013       */
3014      assert(n <= MAX_WIDTH);
3015      if (srcFormat == GL_COLOR_INDEX) {
3016         GLuint indexes[MAX_WIDTH];
3017         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3018                              srcPacking);
3019
3020         if (dstFormat == GL_COLOR_INDEX) {
3021            GLuint i;
3022            _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3023            /* convert to GLchan and return */
3024            for (i = 0; i < n; i++) {
3025               dest[i] = (GLchan) (indexes[i] & 0xff);
3026            }
3027            return;
3028         }
3029         else {
3030            /* Convert indexes to RGBA */
3031            if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3032               shift_and_offset_ci(ctx, n, indexes);
3033            }
3034            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3035         }
3036
3037         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3038          * with color indexes.
3039          */
3040         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3041      }
3042      else {
3043         /* non-color index data */
3044         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3045                            srcPacking->SwapBytes);
3046      }
3047
3048      /* Need to clamp if returning GLubytes or GLushorts */
3049#if CHAN_TYPE != GL_FLOAT
3050      transferOps |= IMAGE_CLAMP_BIT;
3051#endif
3052
3053      if (transferOps) {
3054         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3055      }
3056
3057      /* Now determine which color channels we need to produce.
3058       * And determine the dest index (offset) within each color tuple.
3059       */
3060      switch (dstFormat) {
3061         case GL_ALPHA:
3062            dstAlphaIndex = 0;
3063            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3064            dstLuminanceIndex = dstIntensityIndex = -1;
3065            break;
3066         case GL_LUMINANCE:
3067            dstLuminanceIndex = 0;
3068            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3069            dstIntensityIndex = -1;
3070            break;
3071         case GL_LUMINANCE_ALPHA:
3072            dstLuminanceIndex = 0;
3073            dstAlphaIndex = 1;
3074            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3075            dstIntensityIndex = -1;
3076            break;
3077         case GL_INTENSITY:
3078            dstIntensityIndex = 0;
3079            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3080            dstLuminanceIndex = -1;
3081            break;
3082         case GL_RGB:
3083            dstRedIndex = 0;
3084            dstGreenIndex = 1;
3085            dstBlueIndex = 2;
3086            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3087            break;
3088         case GL_RGBA:
3089            dstRedIndex = 0;
3090            dstGreenIndex = 1;
3091            dstBlueIndex = 2;
3092            dstAlphaIndex = 3;
3093            dstLuminanceIndex = dstIntensityIndex = -1;
3094            break;
3095         default:
3096            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_chan_span()");
3097            return;
3098      }
3099
3100
3101      /* Now return the GLchan data in the requested dstFormat */
3102
3103      if (dstRedIndex >= 0) {
3104         GLchan *dst = dest;
3105         GLuint i;
3106         for (i = 0; i < n; i++) {
3107            CLAMPED_FLOAT_TO_CHAN(dst[dstRedIndex], rgba[i][RCOMP]);
3108            dst += dstComponents;
3109         }
3110      }
3111
3112      if (dstGreenIndex >= 0) {
3113         GLchan *dst = dest;
3114         GLuint i;
3115         for (i = 0; i < n; i++) {
3116            CLAMPED_FLOAT_TO_CHAN(dst[dstGreenIndex], rgba[i][GCOMP]);
3117            dst += dstComponents;
3118         }
3119      }
3120
3121      if (dstBlueIndex >= 0) {
3122         GLchan *dst = dest;
3123         GLuint i;
3124         for (i = 0; i < n; i++) {
3125            CLAMPED_FLOAT_TO_CHAN(dst[dstBlueIndex], rgba[i][BCOMP]);
3126            dst += dstComponents;
3127         }
3128      }
3129
3130      if (dstAlphaIndex >= 0) {
3131         GLchan *dst = dest;
3132         GLuint i;
3133         for (i = 0; i < n; i++) {
3134            CLAMPED_FLOAT_TO_CHAN(dst[dstAlphaIndex], rgba[i][ACOMP]);
3135            dst += dstComponents;
3136         }
3137      }
3138
3139      if (dstIntensityIndex >= 0) {
3140         GLchan *dst = dest;
3141         GLuint i;
3142         assert(dstIntensityIndex == 0);
3143         assert(dstComponents == 1);
3144         for (i = 0; i < n; i++) {
3145            /* Intensity comes from red channel */
3146            CLAMPED_FLOAT_TO_CHAN(dst[i], rgba[i][RCOMP]);
3147         }
3148      }
3149
3150      if (dstLuminanceIndex >= 0) {
3151         GLchan *dst = dest;
3152         GLuint i;
3153         assert(dstLuminanceIndex == 0);
3154         for (i = 0; i < n; i++) {
3155            /* Luminance comes from red channel */
3156            CLAMPED_FLOAT_TO_CHAN(dst[0], rgba[i][RCOMP]);
3157            dst += dstComponents;
3158         }
3159      }
3160   }
3161}
3162
3163
3164/**
3165 * Same as _mesa_unpack_color_span_chan(), but return GLfloat data
3166 * instead of GLchan.
3167 */
3168void
3169_mesa_unpack_color_span_float( GLcontext *ctx,
3170                               GLuint n, GLenum dstFormat, GLfloat dest[],
3171                               GLenum srcFormat, GLenum srcType,
3172                               const GLvoid *source,
3173                               const struct gl_pixelstore_attrib *srcPacking,
3174                               GLbitfield transferOps )
3175{
3176   ASSERT(dstFormat == GL_ALPHA ||
3177          dstFormat == GL_LUMINANCE ||
3178          dstFormat == GL_LUMINANCE_ALPHA ||
3179          dstFormat == GL_INTENSITY ||
3180          dstFormat == GL_RGB ||
3181          dstFormat == GL_RGBA ||
3182          dstFormat == GL_COLOR_INDEX);
3183
3184   ASSERT(srcFormat == GL_RED ||
3185          srcFormat == GL_GREEN ||
3186          srcFormat == GL_BLUE ||
3187          srcFormat == GL_ALPHA ||
3188          srcFormat == GL_LUMINANCE ||
3189          srcFormat == GL_LUMINANCE_ALPHA ||
3190          srcFormat == GL_INTENSITY ||
3191          srcFormat == GL_RGB ||
3192          srcFormat == GL_BGR ||
3193          srcFormat == GL_RGBA ||
3194          srcFormat == GL_BGRA ||
3195          srcFormat == GL_ABGR_EXT ||
3196          srcFormat == GL_COLOR_INDEX);
3197
3198   ASSERT(srcType == GL_BITMAP ||
3199          srcType == GL_UNSIGNED_BYTE ||
3200          srcType == GL_BYTE ||
3201          srcType == GL_UNSIGNED_SHORT ||
3202          srcType == GL_SHORT ||
3203          srcType == GL_UNSIGNED_INT ||
3204          srcType == GL_INT ||
3205          srcType == GL_HALF_FLOAT_ARB ||
3206          srcType == GL_FLOAT ||
3207          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3208          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3209          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3210          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3211          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3212          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3213          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3214          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3215          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3216          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3217          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3218          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3219
3220   /* general solution, no special cases, yet */
3221   {
3222      GLint dstComponents;
3223      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3224      GLint dstLuminanceIndex, dstIntensityIndex;
3225      GLfloat rgba[MAX_WIDTH][4];
3226
3227      dstComponents = _mesa_components_in_format( dstFormat );
3228      /* source & dest image formats should have been error checked by now */
3229      assert(dstComponents > 0);
3230
3231      /*
3232       * Extract image data and convert to RGBA floats
3233       */
3234      assert(n <= MAX_WIDTH);
3235      if (srcFormat == GL_COLOR_INDEX) {
3236         GLuint indexes[MAX_WIDTH];
3237         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3238                              srcPacking);
3239
3240         if (dstFormat == GL_COLOR_INDEX) {
3241            GLuint i;
3242            _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3243            /* convert to GLchan and return */
3244            for (i = 0; i < n; i++) {
3245               dest[i] = (GLchan) (indexes[i] & 0xff);
3246            }
3247            return;
3248         }
3249         else {
3250            /* Convert indexes to RGBA */
3251            if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3252               shift_and_offset_ci(ctx, n, indexes);
3253            }
3254            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3255         }
3256
3257         /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3258          * with color indexes.
3259          */
3260         transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3261      }
3262      else {
3263         /* non-color index data */
3264         extract_float_rgba(n, rgba, srcFormat, srcType, source,
3265                            srcPacking->SwapBytes);
3266      }
3267
3268      if (transferOps) {
3269         _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3270      }
3271
3272      /* Now determine which color channels we need to produce.
3273       * And determine the dest index (offset) within each color tuple.
3274       */
3275      switch (dstFormat) {
3276         case GL_ALPHA:
3277            dstAlphaIndex = 0;
3278            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3279            dstLuminanceIndex = dstIntensityIndex = -1;
3280            break;
3281         case GL_LUMINANCE:
3282            dstLuminanceIndex = 0;
3283            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3284            dstIntensityIndex = -1;
3285            break;
3286         case GL_LUMINANCE_ALPHA:
3287            dstLuminanceIndex = 0;
3288            dstAlphaIndex = 1;
3289            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3290            dstIntensityIndex = -1;
3291            break;
3292         case GL_INTENSITY:
3293            dstIntensityIndex = 0;
3294            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3295            dstLuminanceIndex = -1;
3296            break;
3297         case GL_RGB:
3298            dstRedIndex = 0;
3299            dstGreenIndex = 1;
3300            dstBlueIndex = 2;
3301            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3302            break;
3303         case GL_RGBA:
3304            dstRedIndex = 0;
3305            dstGreenIndex = 1;
3306            dstBlueIndex = 2;
3307            dstAlphaIndex = 3;
3308            dstLuminanceIndex = dstIntensityIndex = -1;
3309            break;
3310         default:
3311            _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_color_span_float()");
3312            return;
3313      }
3314
3315      /* Now pack results in the requested dstFormat */
3316      if (dstRedIndex >= 0) {
3317         GLfloat *dst = dest;
3318         GLuint i;
3319         for (i = 0; i < n; i++) {
3320            dst[dstRedIndex] = rgba[i][RCOMP];
3321            dst += dstComponents;
3322         }
3323      }
3324
3325      if (dstGreenIndex >= 0) {
3326         GLfloat *dst = dest;
3327         GLuint i;
3328         for (i = 0; i < n; i++) {
3329            dst[dstGreenIndex] = rgba[i][GCOMP];
3330            dst += dstComponents;
3331         }
3332      }
3333
3334      if (dstBlueIndex >= 0) {
3335         GLfloat *dst = dest;
3336         GLuint i;
3337         for (i = 0; i < n; i++) {
3338            dst[dstBlueIndex] = rgba[i][BCOMP];
3339            dst += dstComponents;
3340         }
3341      }
3342
3343      if (dstAlphaIndex >= 0) {
3344         GLfloat *dst = dest;
3345         GLuint i;
3346         for (i = 0; i < n; i++) {
3347            dst[dstAlphaIndex] = rgba[i][ACOMP];
3348            dst += dstComponents;
3349         }
3350      }
3351
3352      if (dstIntensityIndex >= 0) {
3353         GLfloat *dst = dest;
3354         GLuint i;
3355         assert(dstIntensityIndex == 0);
3356         assert(dstComponents == 1);
3357         for (i = 0; i < n; i++) {
3358            /* Intensity comes from red channel */
3359            dst[i] = rgba[i][RCOMP];
3360         }
3361      }
3362
3363      if (dstLuminanceIndex >= 0) {
3364         GLfloat *dst = dest;
3365         GLuint i;
3366         assert(dstLuminanceIndex == 0);
3367         for (i = 0; i < n; i++) {
3368            /* Luminance comes from red channel */
3369            dst[0] = rgba[i][RCOMP];
3370            dst += dstComponents;
3371         }
3372      }
3373   }
3374}
3375
3376
3377/*
3378 * Unpack a row of color index data from a client buffer according to
3379 * the pixel unpacking parameters.
3380 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3381 *
3382 * Args:  ctx - the context
3383 *        n - number of pixels
3384 *        dstType - destination data type
3385 *        dest - destination array
3386 *        srcType - source pixel type
3387 *        source - source data pointer
3388 *        srcPacking - pixel unpacking parameters
3389 *        transferOps - the pixel transfer operations to apply
3390 */
3391void
3392_mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
3393                         GLenum dstType, GLvoid *dest,
3394                         GLenum srcType, const GLvoid *source,
3395                         const struct gl_pixelstore_attrib *srcPacking,
3396                         GLbitfield transferOps )
3397{
3398   ASSERT(srcType == GL_BITMAP ||
3399          srcType == GL_UNSIGNED_BYTE ||
3400          srcType == GL_BYTE ||
3401          srcType == GL_UNSIGNED_SHORT ||
3402          srcType == GL_SHORT ||
3403          srcType == GL_UNSIGNED_INT ||
3404          srcType == GL_INT ||
3405          srcType == GL_HALF_FLOAT_ARB ||
3406          srcType == GL_FLOAT);
3407
3408   ASSERT(dstType == GL_UNSIGNED_BYTE ||
3409          dstType == GL_UNSIGNED_SHORT ||
3410          dstType == GL_UNSIGNED_INT);
3411
3412
3413   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3414
3415   /*
3416    * Try simple cases first
3417    */
3418   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
3419       && dstType == GL_UNSIGNED_BYTE) {
3420      _mesa_memcpy(dest, source, n * sizeof(GLubyte));
3421   }
3422   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
3423            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
3424      _mesa_memcpy(dest, source, n * sizeof(GLuint));
3425   }
3426   else {
3427      /*
3428       * general solution
3429       */
3430      GLuint indexes[MAX_WIDTH];
3431      assert(n <= MAX_WIDTH);
3432
3433      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
3434                           srcPacking);
3435
3436      if (transferOps)
3437         _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3438
3439      /* convert to dest type */
3440      switch (dstType) {
3441         case GL_UNSIGNED_BYTE:
3442            {
3443               GLubyte *dst = (GLubyte *) dest;
3444               GLuint i;
3445               for (i = 0; i < n; i++) {
3446                  dst[i] = (GLubyte) (indexes[i] & 0xff);
3447               }
3448            }
3449            break;
3450         case GL_UNSIGNED_SHORT:
3451            {
3452               GLuint *dst = (GLuint *) dest;
3453               GLuint i;
3454               for (i = 0; i < n; i++) {
3455                  dst[i] = (GLushort) (indexes[i] & 0xffff);
3456               }
3457            }
3458            break;
3459         case GL_UNSIGNED_INT:
3460            _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
3461            break;
3462         default:
3463            _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
3464      }
3465   }
3466}
3467
3468
3469void
3470_mesa_pack_index_span( const GLcontext *ctx, GLuint n,
3471                       GLenum dstType, GLvoid *dest, const GLuint *source,
3472                       const struct gl_pixelstore_attrib *dstPacking,
3473                       GLbitfield transferOps )
3474{
3475   GLuint indexes[MAX_WIDTH];
3476
3477   ASSERT(n <= MAX_WIDTH);
3478
3479   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
3480
3481   if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
3482      /* make a copy of input */
3483      _mesa_memcpy(indexes, source, n * sizeof(GLuint));
3484      _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3485      source = indexes;
3486   }
3487
3488   switch (dstType) {
3489   case GL_UNSIGNED_BYTE:
3490      {
3491         GLubyte *dst = (GLubyte *) dest;
3492         GLuint i;
3493         for (i = 0; i < n; i++) {
3494            *dst++ = (GLubyte) source[i];
3495         }
3496      }
3497      break;
3498   case GL_BYTE:
3499      {
3500         GLbyte *dst = (GLbyte *) dest;
3501         GLuint i;
3502         for (i = 0; i < n; i++) {
3503            dst[i] = (GLbyte) source[i];
3504         }
3505      }
3506      break;
3507   case GL_UNSIGNED_SHORT:
3508      {
3509         GLushort *dst = (GLushort *) dest;
3510         GLuint i;
3511         for (i = 0; i < n; i++) {
3512            dst[i] = (GLushort) source[i];
3513         }
3514         if (dstPacking->SwapBytes) {
3515            _mesa_swap2( (GLushort *) dst, n );
3516         }
3517      }
3518      break;
3519   case GL_SHORT:
3520      {
3521         GLshort *dst = (GLshort *) dest;
3522         GLuint i;
3523         for (i = 0; i < n; i++) {
3524            dst[i] = (GLshort) source[i];
3525         }
3526         if (dstPacking->SwapBytes) {
3527            _mesa_swap2( (GLushort *) dst, n );
3528         }
3529      }
3530      break;
3531   case GL_UNSIGNED_INT:
3532      {
3533         GLuint *dst = (GLuint *) dest;
3534         GLuint i;
3535         for (i = 0; i < n; i++) {
3536            dst[i] = (GLuint) source[i];
3537         }
3538         if (dstPacking->SwapBytes) {
3539            _mesa_swap4( (GLuint *) dst, n );
3540         }
3541      }
3542      break;
3543   case GL_INT:
3544      {
3545         GLint *dst = (GLint *) dest;
3546         GLuint i;
3547         for (i = 0; i < n; i++) {
3548            dst[i] = (GLint) source[i];
3549         }
3550         if (dstPacking->SwapBytes) {
3551            _mesa_swap4( (GLuint *) dst, n );
3552         }
3553      }
3554      break;
3555   case GL_FLOAT:
3556      {
3557         GLfloat *dst = (GLfloat *) dest;
3558         GLuint i;
3559         for (i = 0; i < n; i++) {
3560            dst[i] = (GLfloat) source[i];
3561         }
3562         if (dstPacking->SwapBytes) {
3563            _mesa_swap4( (GLuint *) dst, n );
3564         }
3565      }
3566      break;
3567   case GL_HALF_FLOAT_ARB:
3568      {
3569         GLhalfARB *dst = (GLhalfARB *) dest;
3570         GLuint i;
3571         for (i = 0; i < n; i++) {
3572            dst[i] = _mesa_float_to_half((GLfloat) source[i]);
3573         }
3574         if (dstPacking->SwapBytes) {
3575            _mesa_swap2( (GLushort *) dst, n );
3576         }
3577      }
3578      break;
3579   default:
3580      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
3581   }
3582}
3583
3584
3585/*
3586 * Unpack a row of stencil data from a client buffer according to
3587 * the pixel unpacking parameters.
3588 * This is (or will be) used by glDrawPixels
3589 *
3590 * Args:  ctx - the context
3591 *        n - number of pixels
3592 *        dstType - destination data type
3593 *        dest - destination array
3594 *        srcType - source pixel type
3595 *        source - source data pointer
3596 *        srcPacking - pixel unpacking parameters
3597 *        transferOps - apply offset/bias/lookup ops?
3598 */
3599void
3600_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
3601                           GLenum dstType, GLvoid *dest,
3602                           GLenum srcType, const GLvoid *source,
3603                           const struct gl_pixelstore_attrib *srcPacking,
3604                           GLbitfield transferOps )
3605{
3606   ASSERT(srcType == GL_BITMAP ||
3607          srcType == GL_UNSIGNED_BYTE ||
3608          srcType == GL_BYTE ||
3609          srcType == GL_UNSIGNED_SHORT ||
3610          srcType == GL_SHORT ||
3611          srcType == GL_UNSIGNED_INT ||
3612          srcType == GL_INT ||
3613          srcType == GL_UNSIGNED_INT_24_8_EXT ||
3614          srcType == GL_HALF_FLOAT_ARB ||
3615          srcType == GL_FLOAT);
3616
3617   ASSERT(dstType == GL_UNSIGNED_BYTE ||
3618          dstType == GL_UNSIGNED_SHORT ||
3619          dstType == GL_UNSIGNED_INT);
3620
3621   /* only shift and offset apply to stencil */
3622   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
3623
3624   /*
3625    * Try simple cases first
3626    */
3627   if (transferOps == 0 &&
3628       srcType == GL_UNSIGNED_BYTE &&
3629       dstType == GL_UNSIGNED_BYTE) {
3630      _mesa_memcpy(dest, source, n * sizeof(GLubyte));
3631   }
3632   else if (transferOps == 0 &&
3633            srcType == GL_UNSIGNED_INT &&
3634            dstType == GL_UNSIGNED_INT &&
3635            !srcPacking->SwapBytes) {
3636      _mesa_memcpy(dest, source, n * sizeof(GLuint));
3637   }
3638   else {
3639      /*
3640       * general solution
3641       */
3642      GLuint indexes[MAX_WIDTH];
3643      assert(n <= MAX_WIDTH);
3644
3645      extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
3646                           srcPacking);
3647
3648      if (transferOps) {
3649         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3650            /* shift and offset indexes */
3651            shift_and_offset_ci(ctx, n, indexes);
3652         }
3653
3654         if (ctx->Pixel.MapStencilFlag) {
3655            /* Apply stencil lookup table */
3656            GLuint mask = ctx->Pixel.MapStoSsize - 1;
3657            GLuint i;
3658            for (i=0;i<n;i++) {
3659               indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
3660            }
3661         }
3662      }
3663
3664      /* convert to dest type */
3665      switch (dstType) {
3666         case GL_UNSIGNED_BYTE:
3667            {
3668               GLubyte *dst = (GLubyte *) dest;
3669               GLuint i;
3670               for (i = 0; i < n; i++) {
3671                  dst[i] = (GLubyte) (indexes[i] & 0xff);
3672               }
3673            }
3674            break;
3675         case GL_UNSIGNED_SHORT:
3676            {
3677               GLuint *dst = (GLuint *) dest;
3678               GLuint i;
3679               for (i = 0; i < n; i++) {
3680                  dst[i] = (GLushort) (indexes[i] & 0xffff);
3681               }
3682            }
3683            break;
3684         case GL_UNSIGNED_INT:
3685            _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
3686            break;
3687         default:
3688            _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
3689      }
3690   }
3691}
3692
3693
3694void
3695_mesa_pack_stencil_span( const GLcontext *ctx, GLuint n,
3696                         GLenum dstType, GLvoid *dest, const GLstencil *source,
3697                         const struct gl_pixelstore_attrib *dstPacking )
3698{
3699   GLstencil stencil[MAX_WIDTH];
3700
3701   ASSERT(n <= MAX_WIDTH);
3702
3703   if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
3704       ctx->Pixel.MapStencilFlag) {
3705      /* make a copy of input */
3706      _mesa_memcpy(stencil, source, n * sizeof(GLstencil));
3707      _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
3708      source = stencil;
3709   }
3710
3711   switch (dstType) {
3712   case GL_UNSIGNED_BYTE:
3713      if (sizeof(GLstencil) == 8) {
3714         _mesa_memcpy( dest, source, n );
3715      }
3716      else {
3717         GLubyte *dst = (GLubyte *) dest;
3718         GLuint i;
3719         for (i=0;i<n;i++) {
3720            dst[i] = (GLubyte) source[i];
3721         }
3722      }
3723      break;
3724   case GL_BYTE:
3725      if (sizeof(GLstencil) == 8) {
3726         _mesa_memcpy( dest, source, n );
3727      }
3728      else {
3729         GLbyte *dst = (GLbyte *) dest;
3730         GLuint i;
3731         for (i=0;i<n;i++) {
3732            dst[i] = (GLbyte) source[i];
3733         }
3734      }
3735      break;
3736   case GL_UNSIGNED_SHORT:
3737      {
3738         GLushort *dst = (GLushort *) dest;
3739         GLuint i;
3740         for (i=0;i<n;i++) {
3741            dst[i] = (GLushort) source[i];
3742         }
3743         if (dstPacking->SwapBytes) {
3744            _mesa_swap2( (GLushort *) dst, n );
3745         }
3746      }
3747      break;
3748   case GL_SHORT:
3749      {
3750         GLshort *dst = (GLshort *) dest;
3751         GLuint i;
3752         for (i=0;i<n;i++) {
3753            dst[i] = (GLshort) source[i];
3754         }
3755         if (dstPacking->SwapBytes) {
3756            _mesa_swap2( (GLushort *) dst, n );
3757         }
3758      }
3759      break;
3760   case GL_UNSIGNED_INT:
3761      {
3762         GLuint *dst = (GLuint *) dest;
3763         GLuint i;
3764         for (i=0;i<n;i++) {
3765            dst[i] = (GLuint) source[i];
3766         }
3767         if (dstPacking->SwapBytes) {
3768            _mesa_swap4( (GLuint *) dst, n );
3769         }
3770      }
3771      break;
3772   case GL_INT:
3773      {
3774         GLint *dst = (GLint *) dest;
3775         GLuint i;
3776         for (i=0;i<n;i++) {
3777            *dst++ = (GLint) source[i];
3778         }
3779         if (dstPacking->SwapBytes) {
3780            _mesa_swap4( (GLuint *) dst, n );
3781         }
3782      }
3783      break;
3784   case GL_FLOAT:
3785      {
3786         GLfloat *dst = (GLfloat *) dest;
3787         GLuint i;
3788         for (i=0;i<n;i++) {
3789            dst[i] = (GLfloat) source[i];
3790         }
3791         if (dstPacking->SwapBytes) {
3792            _mesa_swap4( (GLuint *) dst, n );
3793         }
3794      }
3795      break;
3796   case GL_HALF_FLOAT_ARB:
3797      {
3798         GLhalfARB *dst = (GLhalfARB *) dest;
3799         GLuint i;
3800         for (i=0;i<n;i++) {
3801            dst[i] = _mesa_float_to_half( (float) source[i] );
3802         }
3803         if (dstPacking->SwapBytes) {
3804            _mesa_swap2( (GLushort *) dst, n );
3805         }
3806      }
3807      break;
3808   case GL_BITMAP:
3809      if (dstPacking->LsbFirst) {
3810         GLubyte *dst = (GLubyte *) dest;
3811         GLint shift = 0;
3812         GLuint i;
3813         for (i = 0; i < n; i++) {
3814            if (shift == 0)
3815               *dst = 0;
3816            *dst |= ((source[i] != 0) << shift);
3817            shift++;
3818            if (shift == 8) {
3819               shift = 0;
3820               dst++;
3821            }
3822         }
3823      }
3824      else {
3825         GLubyte *dst = (GLubyte *) dest;
3826         GLint shift = 7;
3827         GLuint i;
3828         for (i = 0; i < n; i++) {
3829            if (shift == 7)
3830               *dst = 0;
3831            *dst |= ((source[i] != 0) << shift);
3832            shift--;
3833            if (shift < 0) {
3834               shift = 7;
3835               dst++;
3836            }
3837         }
3838      }
3839      break;
3840   default:
3841      _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
3842   }
3843}
3844
3845
3846void
3847_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n,
3848                         GLenum dstType, GLvoid *dest, GLfloat depthScale,
3849                         GLenum srcType, const GLvoid *source,
3850                         const struct gl_pixelstore_attrib *srcPacking )
3851{
3852   GLfloat depthTemp[MAX_WIDTH], *depthValues;
3853
3854   if (dstType == GL_FLOAT) {
3855      depthValues = (GLfloat *) dest;
3856   }
3857   else {
3858      depthValues = depthTemp;
3859   }
3860
3861   /* XXX we need to obey srcPacking->SwapBytes here!!! */
3862   (void) srcPacking;
3863
3864   switch (srcType) {
3865      case GL_BYTE:
3866         {
3867            GLuint i;
3868            const GLubyte *src = (const GLubyte *) source;
3869            for (i = 0; i < n; i++) {
3870               depthValues[i] = BYTE_TO_FLOAT(src[i]);
3871            }
3872         }
3873         break;
3874      case GL_UNSIGNED_BYTE:
3875         {
3876            GLuint i;
3877            const GLubyte *src = (const GLubyte *) source;
3878            for (i = 0; i < n; i++) {
3879               depthValues[i] = UBYTE_TO_FLOAT(src[i]);
3880            }
3881         }
3882         break;
3883      case GL_SHORT:
3884         {
3885            GLuint i;
3886            const GLshort *src = (const GLshort *) source;
3887            for (i = 0; i < n; i++) {
3888               depthValues[i] = SHORT_TO_FLOAT(src[i]);
3889            }
3890         }
3891         break;
3892      case GL_UNSIGNED_SHORT:
3893         {
3894            GLuint i;
3895            const GLushort *src = (const GLushort *) source;
3896            for (i = 0; i < n; i++) {
3897               depthValues[i] = USHORT_TO_FLOAT(src[i]);
3898            }
3899         }
3900         break;
3901      case GL_INT:
3902         {
3903            GLuint i;
3904            const GLint *src = (const GLint *) source;
3905            for (i = 0; i < n; i++) {
3906               depthValues[i] = INT_TO_FLOAT(src[i]);
3907            }
3908         }
3909         break;
3910      case GL_UNSIGNED_INT:
3911         {
3912            GLuint i;
3913            const GLuint *src = (const GLuint *) source;
3914            for (i = 0; i < n; i++) {
3915               depthValues[i] = UINT_TO_FLOAT(src[i]);
3916            }
3917         }
3918         break;
3919      case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
3920         if (dstType == GL_UNSIGNED_INT &&
3921             depthScale == (GLfloat) 0xffffff &&
3922             ctx->Pixel.DepthScale == 1.0 &&
3923             ctx->Pixel.DepthBias == 0.0) {
3924            const GLuint *src = (const GLuint *) source;
3925            GLuint *zValues = (GLuint *) dest;
3926            GLuint i;
3927            for (i = 0; i < n; i++) {
3928               zValues[i] = src[i] & 0xffffff00;
3929            }
3930            return;
3931         }
3932         else {
3933            const GLuint *src = (const GLuint *) source;
3934            const GLfloat scale = 1.0f / 0xffffff;
3935            GLuint i;
3936            for (i = 0; i < n; i++) {
3937               depthValues[i] = (src[i] >> 8) * scale;
3938            }
3939         }
3940         break;
3941      case GL_FLOAT:
3942         _mesa_memcpy(depthValues, source, n * sizeof(GLfloat));
3943         break;
3944      case GL_HALF_FLOAT_ARB:
3945         {
3946            GLuint i;
3947            const GLhalfARB *src = (const GLhalfARB *) source;
3948            for (i = 0; i < n; i++) {
3949               depthValues[i] = _mesa_half_to_float(src[i]);
3950            }
3951         }
3952         break;
3953      default:
3954         _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
3955         return;
3956   }
3957
3958
3959   /* apply depth scale and bias and clamp to [0,1] */
3960   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
3961      _mesa_scale_and_bias_depth(ctx, n, depthValues);
3962   }
3963
3964   if (dstType == GL_UNSIGNED_INT) {
3965      GLuint *zValues = (GLuint *) dest;
3966      GLuint i;
3967      if (depthScale <= (GLfloat) 0xffffff) {
3968         /* no overflow worries */
3969         for (i = 0; i < n; i++) {
3970            zValues[i] = (GLuint) (depthValues[i] * depthScale);
3971         }
3972      }
3973      else {
3974         /* need to use double precision to prevent overflow problems */
3975         for (i = 0; i < n; i++) {
3976            GLdouble z = depthValues[i] * depthScale;
3977            if (z >= (GLdouble) 0xffffffff)
3978               zValues[i] = 0xffffffff;
3979            else
3980               zValues[i] = (GLuint) z;
3981         }
3982      }
3983   }
3984   else if (dstType == GL_UNSIGNED_SHORT) {
3985      GLushort *zValues = (GLushort *) dest;
3986      GLuint i;
3987      ASSERT(depthScale <= 65535.0);
3988      for (i = 0; i < n; i++) {
3989         zValues[i] = (GLushort) (depthValues[i] * depthScale);
3990      }
3991   }
3992   else {
3993      ASSERT(dstType == GL_FLOAT);
3994      ASSERT(depthScale == 1.0F);
3995   }
3996}
3997
3998
3999/*
4000 * Pack an array of depth values.  The values are floats in [0,1].
4001 */
4002void
4003_mesa_pack_depth_span( const GLcontext *ctx, GLuint n, GLvoid *dest,
4004                       GLenum dstType, const GLfloat *depthSpan,
4005                       const struct gl_pixelstore_attrib *dstPacking )
4006{
4007   GLfloat depthCopy[MAX_WIDTH];
4008
4009   ASSERT(n <= MAX_WIDTH);
4010
4011   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4012      _mesa_memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
4013      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4014      depthSpan = depthCopy;
4015   }
4016
4017   switch (dstType) {
4018   case GL_UNSIGNED_BYTE:
4019      {
4020         GLubyte *dst = (GLubyte *) dest;
4021         GLuint i;
4022         for (i = 0; i < n; i++) {
4023            dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
4024         }
4025      }
4026      break;
4027   case GL_BYTE:
4028      {
4029         GLbyte *dst = (GLbyte *) dest;
4030         GLuint i;
4031         for (i = 0; i < n; i++) {
4032            dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
4033         }
4034      }
4035      break;
4036   case GL_UNSIGNED_SHORT:
4037      {
4038         GLushort *dst = (GLushort *) dest;
4039         GLuint i;
4040         for (i = 0; i < n; i++) {
4041            CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
4042         }
4043         if (dstPacking->SwapBytes) {
4044            _mesa_swap2( (GLushort *) dst, n );
4045         }
4046      }
4047      break;
4048   case GL_SHORT:
4049      {
4050         GLshort *dst = (GLshort *) dest;
4051         GLuint i;
4052         for (i = 0; i < n; i++) {
4053            dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
4054         }
4055         if (dstPacking->SwapBytes) {
4056            _mesa_swap2( (GLushort *) dst, n );
4057         }
4058      }
4059      break;
4060   case GL_UNSIGNED_INT:
4061      {
4062         GLuint *dst = (GLuint *) dest;
4063         GLuint i;
4064         for (i = 0; i < n; i++) {
4065            dst[i] = FLOAT_TO_UINT( depthSpan[i] );
4066         }
4067         if (dstPacking->SwapBytes) {
4068            _mesa_swap4( (GLuint *) dst, n );
4069         }
4070      }
4071      break;
4072   case GL_INT:
4073      {
4074         GLint *dst = (GLint *) dest;
4075         GLuint i;
4076         for (i = 0; i < n; i++) {
4077            dst[i] = FLOAT_TO_INT( depthSpan[i] );
4078         }
4079         if (dstPacking->SwapBytes) {
4080            _mesa_swap4( (GLuint *) dst, n );
4081         }
4082      }
4083      break;
4084   case GL_FLOAT:
4085      {
4086         GLfloat *dst = (GLfloat *) dest;
4087         GLuint i;
4088         for (i = 0; i < n; i++) {
4089            dst[i] = depthSpan[i];
4090         }
4091         if (dstPacking->SwapBytes) {
4092            _mesa_swap4( (GLuint *) dst, n );
4093         }
4094      }
4095      break;
4096   case GL_HALF_FLOAT_ARB:
4097      {
4098         GLhalfARB *dst = (GLhalfARB *) dest;
4099         GLuint i;
4100         for (i = 0; i < n; i++) {
4101            dst[i] = _mesa_float_to_half(depthSpan[i]);
4102         }
4103         if (dstPacking->SwapBytes) {
4104            _mesa_swap2( (GLushort *) dst, n );
4105         }
4106      }
4107      break;
4108   default:
4109      _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
4110   }
4111}
4112
4113
4114
4115/**
4116 * Pack depth and stencil values as GL_DEPTH_STENCIL/GL_UNSIGNED_INT_24_8.
4117 */
4118void
4119_mesa_pack_depth_stencil_span(const GLcontext *ctx, GLuint n, GLuint *dest,
4120                              const GLfloat *depthVals,
4121                              const GLstencil *stencilVals,
4122                              const struct gl_pixelstore_attrib *dstPacking)
4123{
4124   GLfloat depthCopy[MAX_WIDTH];
4125   GLstencil stencilCopy[MAX_WIDTH];
4126   GLuint i;
4127
4128   ASSERT(n <= MAX_WIDTH);
4129
4130   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4131      _mesa_memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
4132      _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4133      depthVals = depthCopy;
4134   }
4135
4136   if (ctx->Pixel.IndexShift ||
4137       ctx->Pixel.IndexOffset ||
4138       ctx->Pixel.MapStencilFlag) {
4139      _mesa_memcpy(stencilCopy, stencilVals, n * sizeof(GLstencil));
4140      _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
4141      stencilVals = stencilCopy;
4142   }
4143
4144   for (i = 0; i < n; i++) {
4145      GLuint z = (GLuint) (depthVals[i] * 0xffffff);
4146      dest[i] = (z << 8) | (stencilVals[i] & 0xff);
4147   }
4148
4149   if (dstPacking->SwapBytes) {
4150      _mesa_swap4(dest, n);
4151   }
4152}
4153
4154
4155
4156
4157/**
4158 * Unpack image data.  Apply byte swapping, byte flipping (bitmap).
4159 * Return all image data in a contiguous block.  This is used when we
4160 * compile glDrawPixels, glTexImage, etc into a display list.  We
4161 * need a copy of the data in a standard format.
4162 */
4163void *
4164_mesa_unpack_image( GLuint dimensions,
4165                    GLsizei width, GLsizei height, GLsizei depth,
4166                    GLenum format, GLenum type, const GLvoid *pixels,
4167                    const struct gl_pixelstore_attrib *unpack )
4168{
4169   GLint bytesPerRow, compsPerRow;
4170   GLboolean flipBytes, swap2, swap4;
4171
4172   if (!pixels)
4173      return NULL;  /* not necessarily an error */
4174
4175   if (width <= 0 || height <= 0 || depth <= 0)
4176      return NULL;  /* generate error later */
4177
4178   if (type == GL_BITMAP) {
4179      bytesPerRow = (width + 7) >> 3;
4180      flipBytes = !unpack->LsbFirst;
4181      swap2 = swap4 = GL_FALSE;
4182      compsPerRow = 0;
4183   }
4184   else {
4185      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
4186      const GLint components = _mesa_components_in_format(format);
4187      GLint bytesPerComp;
4188      if (bytesPerPixel <= 0 || components <= 0)
4189         return NULL;   /* bad format or type.  generate error later */
4190      bytesPerRow = bytesPerPixel * width;
4191      bytesPerComp = bytesPerPixel / components;
4192      flipBytes = GL_FALSE;
4193      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
4194      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
4195      compsPerRow = components * width;
4196      assert(compsPerRow >= width);
4197   }
4198
4199   {
4200      GLubyte *destBuffer
4201         = (GLubyte *) _mesa_malloc(bytesPerRow * height * depth);
4202      GLubyte *dst;
4203      GLint img, row;
4204      if (!destBuffer)
4205         return NULL;   /* generate GL_OUT_OF_MEMORY later */
4206
4207      dst = destBuffer;
4208      for (img = 0; img < depth; img++) {
4209         for (row = 0; row < height; row++) {
4210            const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
4211                               width, height, format, type, img, row, 0);
4212            _mesa_memcpy(dst, src, bytesPerRow);
4213            /* byte flipping/swapping */
4214            if (flipBytes) {
4215               flip_bytes((GLubyte *) dst, bytesPerRow);
4216            }
4217            else if (swap2) {
4218               _mesa_swap2((GLushort*) dst, compsPerRow);
4219            }
4220            else if (swap4) {
4221               _mesa_swap4((GLuint*) dst, compsPerRow);
4222            }
4223            dst += bytesPerRow;
4224         }
4225      }
4226      return destBuffer;
4227   }
4228}
4229
4230#endif /* _HAVE_FULL_GL */
4231
4232
4233
4234/**
4235 * Convert an array of RGBA colors from one datatype to another.
4236 * NOTE: src may equal dst.  In that case, we use a temporary buffer.
4237 */
4238void
4239_mesa_convert_colors(GLenum srcType, const GLvoid *src,
4240                     GLenum dstType, GLvoid *dst,
4241                     GLuint count, const GLubyte mask[])
4242{
4243   GLuint tempBuffer[MAX_WIDTH][4];
4244   const GLboolean useTemp = (src == dst);
4245
4246   ASSERT(srcType != dstType);
4247
4248   switch (srcType) {
4249   case GL_UNSIGNED_BYTE:
4250      if (dstType == GL_UNSIGNED_SHORT) {
4251         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
4252         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
4253         GLuint i;
4254         for (i = 0; i < count; i++) {
4255            if (!mask || mask[i]) {
4256               dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
4257               dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
4258               dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
4259               dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
4260            }
4261         }
4262         if (useTemp)
4263            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
4264      }
4265      else {
4266         const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
4267         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
4268         GLuint i;
4269         ASSERT(dstType == GL_FLOAT);
4270         for (i = 0; i < count; i++) {
4271            if (!mask || mask[i]) {
4272               dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
4273               dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
4274               dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
4275               dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
4276            }
4277         }
4278         if (useTemp)
4279            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
4280      }
4281      break;
4282   case GL_UNSIGNED_SHORT:
4283      if (dstType == GL_UNSIGNED_BYTE) {
4284         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
4285         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
4286         GLuint i;
4287         for (i = 0; i < count; i++) {
4288            if (!mask || mask[i]) {
4289               dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
4290               dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
4291               dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
4292               dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
4293            }
4294         }
4295         if (useTemp)
4296            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
4297      }
4298      else {
4299         const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
4300         GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
4301         GLuint i;
4302         ASSERT(dstType == GL_FLOAT);
4303         for (i = 0; i < count; i++) {
4304            if (!mask || mask[i]) {
4305               dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
4306               dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
4307               dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
4308               dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
4309            }
4310         }
4311         if (useTemp)
4312            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
4313      }
4314      break;
4315   case GL_FLOAT:
4316      if (dstType == GL_UNSIGNED_BYTE) {
4317         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
4318         GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
4319         GLuint i;
4320         for (i = 0; i < count; i++) {
4321            if (!mask || mask[i]) {
4322               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
4323               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
4324               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
4325               UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
4326            }
4327         }
4328         if (useTemp)
4329            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
4330      }
4331      else {
4332         const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
4333         GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
4334         GLuint i;
4335         ASSERT(dstType == GL_UNSIGNED_SHORT);
4336         for (i = 0; i < count; i++) {
4337            if (!mask || mask[i]) {
4338               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
4339               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
4340               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
4341               UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
4342            }
4343         }
4344         if (useTemp)
4345            _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
4346      }
4347      break;
4348   default:
4349      _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
4350   }
4351}
4352
4353
4354
4355
4356/**
4357 * Perform basic clipping for glDrawPixels.  The image's position and size
4358 * and the unpack SkipPixels and SkipRows are adjusted so that the image
4359 * region is entirely within the window and scissor bounds.
4360 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
4361 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
4362 * we'll actually write.  Beforehand, *destY-1 is the first drawing row.
4363 *
4364 * \return  GL_TRUE if image is ready for drawing or
4365 *          GL_FALSE if image was completely clipped away (draw nothing)
4366 */
4367GLboolean
4368_mesa_clip_drawpixels(const GLcontext *ctx,
4369                      GLint *destX, GLint *destY,
4370                      GLsizei *width, GLsizei *height,
4371                      struct gl_pixelstore_attrib *unpack)
4372{
4373   const GLframebuffer *buffer = ctx->DrawBuffer;
4374
4375   if (unpack->RowLength == 0) {
4376      unpack->RowLength = *width;
4377   }
4378
4379   ASSERT(ctx->Pixel.ZoomX == 1.0F);
4380   ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
4381
4382   /* left clipping */
4383   if (*destX < buffer->_Xmin) {
4384      unpack->SkipPixels += (buffer->_Xmin - *destX);
4385      *width -= (buffer->_Xmin - *destX);
4386      *destX = buffer->_Xmin;
4387   }
4388   /* right clipping */
4389   if (*destX + *width > buffer->_Xmax)
4390      *width -= (*destX + *width - buffer->_Xmax);
4391
4392   if (*width <= 0)
4393      return GL_FALSE;
4394
4395   if (ctx->Pixel.ZoomY == 1.0F) {
4396      /* bottom clipping */
4397      if (*destY < buffer->_Ymin) {
4398         unpack->SkipRows += (buffer->_Ymin - *destY);
4399         *height -= (buffer->_Ymin - *destY);
4400         *destY = buffer->_Ymin;
4401      }
4402      /* top clipping */
4403      if (*destY + *height > buffer->_Ymax)
4404         *height -= (*destY + *height - buffer->_Ymax);
4405   }
4406   else { /* upside down */
4407      /* top clipping */
4408      if (*destY > buffer->_Ymax) {
4409         unpack->SkipRows += (*destY - buffer->_Ymax);
4410         *height -= (*destY - buffer->_Ymax);
4411         *destY = buffer->_Ymax;
4412      }
4413      /* bottom clipping */
4414      if (*destY - *height < buffer->_Ymin)
4415         *height -= (buffer->_Ymin - (*destY - *height));
4416      /* adjust destY so it's the first row to write to */
4417      (*destY)--;
4418   }
4419
4420   if (*height <= 0)
4421      return GL_TRUE;
4422
4423   return GL_TRUE;
4424}
4425
4426
4427/**
4428 * Perform clipping for glReadPixels.  The image's window position
4429 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
4430 * so that the image region is entirely within the window bounds.
4431 * Note: this is different from _mesa_clip_drawpixels() in that the
4432 * scissor box is ignored, and we use the bounds of the current readbuffer
4433 * surface.
4434 *
4435 * \return  GL_TRUE if image is ready for drawing or
4436 *          GL_FALSE if image was completely clipped away (draw nothing)
4437 */
4438GLboolean
4439_mesa_clip_readpixels(const GLcontext *ctx,
4440                      GLint *srcX, GLint *srcY,
4441                      GLsizei *width, GLsizei *height,
4442                      struct gl_pixelstore_attrib *pack)
4443{
4444   const GLframebuffer *buffer = ctx->ReadBuffer;
4445
4446   if (pack->RowLength == 0) {
4447      pack->RowLength = *width;
4448   }
4449
4450   /* left clipping */
4451   if (*srcX < 0) {
4452      pack->SkipPixels += (0 - *srcX);
4453      *width -= (0 - *srcX);
4454      *srcX = 0;
4455   }
4456   /* right clipping */
4457   if (*srcX + *width > (GLsizei) buffer->Width)
4458      *width -= (*srcX + *width - buffer->Width);
4459
4460   if (*width <= 0)
4461      return GL_FALSE;
4462
4463   /* bottom clipping */
4464   if (*srcY < 0) {
4465      pack->SkipRows += (0 - *srcY);
4466      *height -= (0 - *srcY);
4467      *srcY = 0;
4468   }
4469   /* top clipping */
4470   if (*srcY + *height > (GLsizei) buffer->Height)
4471      *height -= (*srcY + *height - buffer->Height);
4472
4473   if (*height <= 0)
4474      return GL_TRUE;
4475
4476   return GL_TRUE;
4477}
4478
4479
4480/**
4481 * Clip the rectangle defined by (x, y, width, height) against the bounds
4482 * specified by [xmin, xmax) and [ymin, ymax).
4483 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
4484 */
4485GLboolean
4486_mesa_clip_to_region(GLint xmin, GLint ymin,
4487                     GLint xmax, GLint ymax,
4488                     GLint *x, GLint *y,
4489                     GLsizei *width, GLsizei *height )
4490{
4491   /* left clipping */
4492   if (*x < xmin) {
4493      *width -= (xmin - *x);
4494      *x = xmin;
4495   }
4496
4497   /* right clipping */
4498   if (*x + *width > xmax)
4499      *width -= (*x + *width - xmax - 1);
4500
4501   if (*width <= 0)
4502      return GL_FALSE;
4503
4504   /* bottom (or top) clipping */
4505   if (*y < ymin) {
4506      *height -= (ymin - *y);
4507      *y = ymin;
4508   }
4509
4510   /* top (or bottom) clipping */
4511   if (*y + *height > ymax)
4512      *height -= (*y + *height - ymax - 1);
4513
4514   if (*height <= 0)
4515      return GL_FALSE;
4516
4517   return GL_TRUE;
4518}
4519