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