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