image.c revision b92d64e150a3b1389f5a4b6cbfe57425a77f24bf
1/* $Id: image.c,v 1.39 2000/08/31 15:24:07 brianp Exp $ */
2
3/*
4 * Mesa 3-D graphics library
5 * Version:  3.5
6 *
7 * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28#ifdef PC_HEADER
29#include "all.h"
30#else
31#include "glheader.h"
32#include "context.h"
33#include "image.h"
34#include "imaging.h"
35#include "macros.h"
36#include "mem.h"
37#include "mmath.h"
38#include "pixel.h"
39#include "types.h"
40#endif
41
42
43
44/*
45 * These are the image packing parameters for Mesa's internal images.
46 * That is, _mesa_unpack_image() returns image data in this format.
47 * When we execute image commands (glDrawPixels, glTexImage, etc)
48 * from within display lists we have to be sure to set the current
49 * unpacking params to these values!
50 */
51const struct gl_pixelstore_attrib _mesa_native_packing = {
52   1,            /* Alignment */
53   0,            /* RowLength */
54   0,            /* SkipPixels */
55   0,            /* SkipRows */
56   0,            /* ImageHeight */
57   0,            /* SkipImages */
58   GL_FALSE,     /* SwapBytes */
59   GL_FALSE      /* LsbFirst */
60};
61
62
63
64/*
65 * Flip the 8 bits in each byte of the given array.
66 */
67static void
68flip_bytes( GLubyte *p, GLuint n )
69{
70   register GLuint i, a, b;
71
72   for (i=0;i<n;i++) {
73      b = (GLuint) p[i];
74      a = ((b & 0x01) << 7) |
75	  ((b & 0x02) << 5) |
76	  ((b & 0x04) << 3) |
77	  ((b & 0x08) << 1) |
78	  ((b & 0x10) >> 1) |
79	  ((b & 0x20) >> 3) |
80	  ((b & 0x40) >> 5) |
81	  ((b & 0x80) >> 7);
82      p[i] = (GLubyte) a;
83   }
84}
85
86
87/*
88 * Flip the order of the 2 bytes in each word in the given array.
89 */
90void
91_mesa_swap2( GLushort *p, GLuint n )
92{
93   register GLuint i;
94
95   for (i=0;i<n;i++) {
96      p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
97   }
98}
99
100
101
102/*
103 * Flip the order of the 4 bytes in each word in the given array.
104 */
105void
106_mesa_swap4( GLuint *p, GLuint n )
107{
108   register GLuint i, a, b;
109
110   for (i=0;i<n;i++) {
111      b = p[i];
112      a =  (b >> 24)
113	| ((b >> 8) & 0xff00)
114	| ((b << 8) & 0xff0000)
115	| ((b << 24) & 0xff000000);
116      p[i] = a;
117   }
118}
119
120
121
122
123/*
124 * Return the size, in bytes, of the given GL datatype.
125 * Return 0 if GL_BITMAP.
126 * Return -1 if invalid type enum.
127 */
128GLint _mesa_sizeof_type( GLenum type )
129{
130   switch (type) {
131      case GL_BITMAP:
132	 return 0;
133      case GL_UNSIGNED_BYTE:
134         return sizeof(GLubyte);
135      case GL_BYTE:
136	 return sizeof(GLbyte);
137      case GL_UNSIGNED_SHORT:
138	 return sizeof(GLushort);
139      case GL_SHORT:
140	 return sizeof(GLshort);
141      case GL_UNSIGNED_INT:
142	 return sizeof(GLuint);
143      case GL_INT:
144	 return sizeof(GLint);
145      case GL_FLOAT:
146	 return sizeof(GLfloat);
147      default:
148         return -1;
149   }
150}
151
152
153/*
154 * Same as _mesa_sizeof_packed_type() but we also accept the
155 * packed pixel format datatypes.
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_FLOAT:
175	 return sizeof(GLfloat);
176      case GL_UNSIGNED_BYTE_3_3_2:
177         return sizeof(GLubyte);
178      case GL_UNSIGNED_BYTE_2_3_3_REV:
179         return sizeof(GLubyte);
180      case GL_UNSIGNED_SHORT_5_6_5:
181         return sizeof(GLshort);
182      case GL_UNSIGNED_SHORT_5_6_5_REV:
183         return sizeof(GLshort);
184      case GL_UNSIGNED_SHORT_4_4_4_4:
185         return sizeof(GLshort);
186      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
187         return sizeof(GLshort);
188      case GL_UNSIGNED_SHORT_5_5_5_1:
189         return sizeof(GLshort);
190      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
191         return sizeof(GLshort);
192      case GL_UNSIGNED_INT_8_8_8_8:
193         return sizeof(GLuint);
194      case GL_UNSIGNED_INT_8_8_8_8_REV:
195         return sizeof(GLuint);
196      case GL_UNSIGNED_INT_10_10_10_2:
197         return sizeof(GLuint);
198      case GL_UNSIGNED_INT_2_10_10_10_REV:
199         return sizeof(GLuint);
200      default:
201         return -1;
202   }
203}
204
205
206
207/*
208 * Return the number of components in a GL enum pixel type.
209 * Return -1 if bad format.
210 */
211GLint _mesa_components_in_format( GLenum format )
212{
213   switch (format) {
214      case GL_COLOR_INDEX:
215      case GL_COLOR_INDEX1_EXT:
216      case GL_COLOR_INDEX2_EXT:
217      case GL_COLOR_INDEX4_EXT:
218      case GL_COLOR_INDEX8_EXT:
219      case GL_COLOR_INDEX12_EXT:
220      case GL_COLOR_INDEX16_EXT:
221      case GL_STENCIL_INDEX:
222      case GL_DEPTH_COMPONENT:
223      case GL_RED:
224      case GL_GREEN:
225      case GL_BLUE:
226      case GL_ALPHA:
227      case GL_LUMINANCE:
228      case GL_INTENSITY:
229         return 1;
230      case GL_LUMINANCE_ALPHA:
231	 return 2;
232      case GL_RGB:
233	 return 3;
234      case GL_RGBA:
235	 return 4;
236      case GL_BGR:
237	 return 3;
238      case GL_BGRA:
239	 return 4;
240      case GL_ABGR_EXT:
241         return 4;
242      default:
243         return -1;
244   }
245}
246
247
248/*
249 * Return bytes per pixel for given format and type
250 * Return -1 if bad format or type.
251 */
252GLint _mesa_bytes_per_pixel( GLenum format, GLenum type )
253{
254   GLint comps = _mesa_components_in_format( format );
255   if (comps < 0)
256      return -1;
257
258   switch (type) {
259      case GL_BITMAP:
260         return 0;  /* special case */
261      case GL_BYTE:
262      case GL_UNSIGNED_BYTE:
263         return comps * sizeof(GLubyte);
264      case GL_SHORT:
265      case GL_UNSIGNED_SHORT:
266         return comps * sizeof(GLshort);
267      case GL_INT:
268      case GL_UNSIGNED_INT:
269         return comps * sizeof(GLint);
270      case GL_FLOAT:
271         return comps * sizeof(GLfloat);
272      case GL_UNSIGNED_BYTE_3_3_2:
273      case GL_UNSIGNED_BYTE_2_3_3_REV:
274         if (format == GL_RGB || format == GL_BGR)
275            return sizeof(GLubyte);
276         else
277            return -1;  /* error */
278      case GL_UNSIGNED_SHORT_5_6_5:
279      case GL_UNSIGNED_SHORT_5_6_5_REV:
280         if (format == GL_RGB || format == GL_BGR)
281            return sizeof(GLshort);
282         else
283            return -1;  /* error */
284      case GL_UNSIGNED_SHORT_4_4_4_4:
285      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
286      case GL_UNSIGNED_SHORT_5_5_5_1:
287      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
288         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
289            return sizeof(GLushort);
290         else
291            return -1;
292      case GL_UNSIGNED_INT_8_8_8_8:
293      case GL_UNSIGNED_INT_8_8_8_8_REV:
294      case GL_UNSIGNED_INT_10_10_10_2:
295      case GL_UNSIGNED_INT_2_10_10_10_REV:
296         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
297            return sizeof(GLuint);
298         else
299            return -1;
300      default:
301         return -1;
302   }
303}
304
305
306/*
307 * Test if the given pixel format and type are legal.
308 * Return GL_TRUE for legal, GL_FALSE for illegal.
309 */
310GLboolean
311_mesa_is_legal_format_and_type( GLenum format, GLenum type )
312{
313   switch (format) {
314      case GL_COLOR_INDEX:
315      case GL_STENCIL_INDEX:
316         switch (type) {
317            case GL_BITMAP:
318            case GL_BYTE:
319            case GL_UNSIGNED_BYTE:
320            case GL_SHORT:
321            case GL_UNSIGNED_SHORT:
322            case GL_INT:
323            case GL_UNSIGNED_INT:
324            case GL_FLOAT:
325               return GL_TRUE;
326            default:
327               return GL_FALSE;
328         }
329      case GL_RED:
330      case GL_GREEN:
331      case GL_BLUE:
332      case GL_ALPHA:
333      case GL_INTENSITY:
334      case GL_LUMINANCE:
335      case GL_LUMINANCE_ALPHA:
336      case GL_DEPTH_COMPONENT:
337      case GL_BGR:
338         switch (type) {
339            case GL_BYTE:
340            case GL_UNSIGNED_BYTE:
341            case GL_SHORT:
342            case GL_UNSIGNED_SHORT:
343            case GL_INT:
344            case GL_UNSIGNED_INT:
345            case GL_FLOAT:
346               return GL_TRUE;
347            default:
348               return GL_FALSE;
349         }
350      case GL_RGB:
351         switch (type) {
352            case GL_BYTE:
353            case GL_UNSIGNED_BYTE:
354            case GL_SHORT:
355            case GL_UNSIGNED_SHORT:
356            case GL_INT:
357            case GL_UNSIGNED_INT:
358            case GL_FLOAT:
359            case GL_UNSIGNED_BYTE_3_3_2:
360            case GL_UNSIGNED_BYTE_2_3_3_REV:
361            case GL_UNSIGNED_SHORT_5_6_5:
362            case GL_UNSIGNED_SHORT_5_6_5_REV:
363               return GL_TRUE;
364            default:
365               return GL_FALSE;
366         }
367      case GL_RGBA:
368      case GL_BGRA:
369      case GL_ABGR_EXT:
370         switch (type) {
371            case GL_BYTE:
372            case GL_UNSIGNED_BYTE:
373            case GL_SHORT:
374            case GL_UNSIGNED_SHORT:
375            case GL_INT:
376            case GL_UNSIGNED_INT:
377            case GL_FLOAT:
378            case GL_UNSIGNED_SHORT_4_4_4_4:
379            case GL_UNSIGNED_SHORT_4_4_4_4_REV:
380            case GL_UNSIGNED_SHORT_5_5_5_1:
381            case GL_UNSIGNED_SHORT_1_5_5_5_REV:
382            case GL_UNSIGNED_INT_8_8_8_8:
383            case GL_UNSIGNED_INT_8_8_8_8_REV:
384            case GL_UNSIGNED_INT_10_10_10_2:
385            case GL_UNSIGNED_INT_2_10_10_10_REV:
386               return GL_TRUE;
387            default:
388               return GL_FALSE;
389         }
390      default:
391         ; /* fall-through */
392   }
393   return GL_FALSE;
394}
395
396
397
398/*
399 * Return the address of a pixel in an image (actually a volume).
400 * Pixel unpacking/packing parameters are observed according to 'packing'.
401 * Input:  image - start of image data
402 *         width, height - size of image
403 *         format - image format
404 *         type - pixel component type
405 *         packing - the pixelstore attributes
406 *         img - which image in the volume (0 for 1D or 2D images)
407 *         row, column - location of pixel in the image
408 * Return:  address of pixel at (image,row,column) in image or NULL if error.
409 */
410GLvoid *
411_mesa_image_address( const struct gl_pixelstore_attrib *packing,
412                     const GLvoid *image, GLsizei width,
413                     GLsizei height, GLenum format, GLenum type,
414                     GLint img, GLint row, GLint column )
415{
416   GLint alignment;        /* 1, 2 or 4 */
417   GLint pixels_per_row;
418   GLint rows_per_image;
419   GLint skiprows;
420   GLint skippixels;
421   GLint skipimages;       /* for 3-D volume images */
422   GLubyte *pixel_addr;
423
424   alignment = packing->Alignment;
425   if (packing->RowLength > 0) {
426      pixels_per_row = packing->RowLength;
427   }
428   else {
429      pixels_per_row = width;
430   }
431   if (packing->ImageHeight > 0) {
432      rows_per_image = packing->ImageHeight;
433   }
434   else {
435      rows_per_image = height;
436   }
437   skiprows = packing->SkipRows;
438   skippixels = packing->SkipPixels;
439   skipimages = packing->SkipImages;
440
441   if (type==GL_BITMAP) {
442      /* BITMAP data */
443      GLint comp_per_pixel;   /* components per pixel */
444      GLint bytes_per_comp;   /* bytes per component */
445      GLint bytes_per_row;
446      GLint bytes_per_image;
447
448      /* Compute bytes per component */
449      bytes_per_comp = _mesa_sizeof_packed_type( type );
450      if (bytes_per_comp<0) {
451         return NULL;
452      }
453
454      /* Compute number of components per pixel */
455      comp_per_pixel = _mesa_components_in_format( format );
456      if (comp_per_pixel<0 && type != GL_BITMAP) {
457         return NULL;
458      }
459
460      bytes_per_row = alignment
461                    * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
462
463      bytes_per_image = bytes_per_row * rows_per_image;
464
465      pixel_addr = (GLubyte *) image
466                 + (skipimages + img) * bytes_per_image
467                 + (skiprows + row) * bytes_per_row
468                 + (skippixels + column) / 8;
469   }
470   else {
471      /* Non-BITMAP data */
472      GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
473
474      bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
475
476      /* The pixel type and format should have been error checked earlier */
477      assert(bytes_per_pixel > 0);
478
479      bytes_per_row = pixels_per_row * bytes_per_pixel;
480      remainder = bytes_per_row % alignment;
481      if (remainder > 0)
482         bytes_per_row += (alignment - remainder);
483
484      ASSERT(bytes_per_row % alignment == 0);
485
486      bytes_per_image = bytes_per_row * rows_per_image;
487
488      /* compute final pixel address */
489      pixel_addr = (GLubyte *) image
490                 + (skipimages + img) * bytes_per_image
491                 + (skiprows + row) * bytes_per_row
492                 + (skippixels + column) * bytes_per_pixel;
493   }
494
495   return (GLvoid *) pixel_addr;
496}
497
498
499
500/*
501 * Compute the stride between image rows (in bytes) for the given
502 * pixel packing parameters and image width, format and type.
503 */
504GLint
505_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
506                        GLint width, GLenum format, GLenum type )
507{
508   ASSERT(packing);
509   if (type == GL_BITMAP) {
510      /* BITMAP data */
511      if (packing->RowLength == 0) {
512         GLint bytes = (width + 7) / 8;
513         return bytes;
514      }
515      else {
516         GLint bytes = (packing->RowLength + 7) / 8;
517         return bytes;
518      }
519   }
520   else {
521      /* Non-BITMAP data */
522      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
523      GLint bytesPerRow, remainder;
524      if (bytesPerPixel <= 0)
525         return -1;  /* error */
526      if (packing->RowLength == 0) {
527         bytesPerRow = bytesPerPixel * width;
528      }
529      else {
530         bytesPerRow = bytesPerPixel * packing->RowLength;
531      }
532      remainder = bytesPerRow % packing->Alignment;
533      if (remainder > 0)
534         bytesPerRow += (packing->Alignment - remainder);
535      return bytesPerRow;
536   }
537}
538
539
540
541/*
542 * Unpack a 32x32 pixel polygon stipple from user memory using the
543 * current pixel unpack settings.
544 */
545void
546_mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
547                              const struct gl_pixelstore_attrib *unpacking )
548{
549   GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap( 32, 32, pattern, unpacking );
550   if (ptrn) {
551      /* Convert pattern from GLubytes to GLuints and handle big/little
552       * endian differences
553       */
554      GLubyte *p = ptrn;
555      GLint i;
556      for (i = 0; i < 32; i++) {
557         dest[i] = (p[0] << 24)
558                 | (p[1] << 16)
559                 | (p[2] <<  8)
560                 | (p[3]      );
561         p += 4;
562      }
563      FREE(ptrn);
564   }
565}
566
567
568
569/*
570 * Pack polygon stipple into user memory given current pixel packing
571 * settings.
572 */
573void
574_mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
575                            const struct gl_pixelstore_attrib *packing )
576{
577   /* Convert pattern from GLuints to GLubytes to handle big/little
578    * endian differences.
579    */
580   GLubyte ptrn[32*4];
581   GLint i;
582   for (i = 0; i < 32; i++) {
583      ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
584      ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
585      ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
586      ptrn[i * 4 + 3] = (GLubyte) ((pattern[i]      ) & 0xff);
587   }
588
589   _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
590}
591
592
593
594/*
595 * Pack the given RGBA span into client memory at 'dest' address
596 * in the given pixel format and type.
597 * Optionally apply the enabled pixel transfer ops.
598 * Pack into memory using the given packing params struct.
599 * This is used by glReadPixels and glGetTexImage?D()
600 * Input:  ctx - the context
601 *         n - number of pixels in the span
602 *         rgba - the pixels
603 *         format - dest packing format
604 *         type - dest packing datatype
605 *         destination - destination packing address
606 *         packing - pixel packing parameters
607 *         transferOps - bitmask of IMAGE_*_BIT operations to apply
608 */
609void
610_mesa_pack_rgba_span( GLcontext *ctx,
611                      GLuint n, CONST GLubyte srcRgba[][4],
612                      GLenum dstFormat, GLenum dstType,
613                      GLvoid *dstAddr,
614                      const struct gl_pixelstore_attrib *dstPacking,
615                      GLuint transferOps)
616{
617   ASSERT(ctx->ImageTransferState != UPDATE_IMAGE_TRANSFER_STATE);
618
619   /* Test for optimized case first */
620   if (transferOps == 0 && dstFormat == GL_RGBA
621       && dstType == GL_UNSIGNED_BYTE) {
622      /* common simple case */
623      MEMCPY(dstAddr, srcRgba, n * 4 * sizeof(GLubyte));
624   }
625   else if (transferOps == 0 && dstFormat == GL_RGB
626            && dstType == GL_UNSIGNED_BYTE) {
627      /* common simple case */
628      GLint i;
629      GLubyte *dest = (GLubyte *) dstAddr;
630      for (i = 0; i < n; i++) {
631         dest[0] = srcRgba[i][RCOMP];
632         dest[1] = srcRgba[i][GCOMP];
633         dest[2] = srcRgba[i][BCOMP];
634         dest += 3;
635      }
636   }
637   else {
638      /* general solution */
639      GLfloat rgba[MAX_WIDTH][4];
640      const GLfloat rscale = 1.0F / 255.0F;
641      const GLfloat gscale = 1.0F / 255.0F;
642      const GLfloat bscale = 1.0F / 255.0F;
643      const GLfloat ascale = 1.0F / 255.0F;
644      GLuint i;
645      assert(n <= MAX_WIDTH);
646      /* convert color components to floating point */
647      for (i=0;i<n;i++) {
648         rgba[i][RCOMP] = srcRgba[i][RCOMP] * rscale;
649         rgba[i][GCOMP] = srcRgba[i][GCOMP] * gscale;
650         rgba[i][BCOMP] = srcRgba[i][BCOMP] * bscale;
651         rgba[i][ACOMP] = srcRgba[i][ACOMP] * ascale;
652      }
653      _mesa_pack_float_rgba_span(ctx, n, (const GLfloat (*)[4]) rgba,
654                                 dstFormat, dstType, dstAddr,
655                                 dstPacking, transferOps);
656   }
657}
658
659
660void
661_mesa_pack_float_rgba_span( GLcontext *ctx,
662                            GLuint n, CONST GLfloat rgbaIn[][4],
663                            GLenum dstFormat, GLenum dstType,
664                            GLvoid *dstAddr,
665                            const struct gl_pixelstore_attrib *dstPacking,
666                            GLuint transferOps )
667{
668   const GLint comps = _mesa_components_in_format(dstFormat);
669   GLfloat luminance[MAX_WIDTH];
670   GLfloat (*rgba)[4];
671   GLuint i;
672
673   if (transferOps) {
674      /* make copy of incoming data */
675      GLfloat rgbaCopy[MAX_WIDTH][4];
676      for (i = 0; i < n; i++) {
677         rgbaCopy[i][0] = rgbaIn[i][0];
678         rgbaCopy[i][1] = rgbaIn[i][1];
679         rgbaCopy[i][2] = rgbaIn[i][2];
680         rgbaCopy[i][3] = rgbaIn[i][3];
681      }
682
683      rgba = (GLfloat (*)[4]) rgbaCopy;
684
685      /* scale & bias */
686      if (transferOps & IMAGE_SCALE_BIAS_BIT) {
687         _mesa_scale_and_bias_rgba( ctx, n, rgba );
688      }
689      /* color map lookup */
690      if (transferOps & IMAGE_MAP_COLOR_BIT) {
691         _mesa_map_rgba( ctx, n, rgba );
692      }
693      /* GL_COLOR_TABLE lookup */
694      if (transferOps & IMAGE_COLOR_TABLE_BIT) {
695         _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
696      }
697      /* convolution */
698      if (transferOps & IMAGE_CONVOLUTION_BIT) {
699         /* this has to be done in the calling code */
700      }
701      /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
702      if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
703         _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
704      }
705      /* color matrix transform */
706      if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
707         _mesa_transform_rgba(ctx, n, rgba);
708      }
709      /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
710      if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
711         _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
712      }
713      /* update histogram count */
714      if (transferOps & IMAGE_HISTOGRAM_BIT) {
715         _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
716      }
717      /* min/max here */
718      if (transferOps & IMAGE_MIN_MAX_BIT) {
719         _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
720         if (ctx->MinMax.Sink)
721            return;
722      }
723   }
724   else {
725      /* use incoming data, not a copy */
726      rgba = (GLfloat (*)[4]) rgbaIn;
727   }
728
729   /* XXX clamp rgba to [0,1]? */
730
731
732   if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
733      for (i = 0; i < n; i++) {
734         GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
735         luminance[i] = CLAMP(sum, 0.0F, 1.0F);
736      }
737   }
738
739   /*
740    * Pack/store the pixels.  Ugh!  Lots of cases!!!
741    */
742   switch (dstType) {
743      case GL_UNSIGNED_BYTE:
744         {
745            GLubyte *dst = (GLubyte *) dstAddr;
746            switch (dstFormat) {
747               case GL_RED:
748                  for (i=0;i<n;i++)
749                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
750                  break;
751               case GL_GREEN:
752                  for (i=0;i<n;i++)
753                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
754                  break;
755               case GL_BLUE:
756                  for (i=0;i<n;i++)
757                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
758                  break;
759               case GL_ALPHA:
760                  for (i=0;i<n;i++)
761                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
762                  break;
763               case GL_LUMINANCE:
764                  for (i=0;i<n;i++)
765                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
766                  break;
767               case GL_LUMINANCE_ALPHA:
768                  for (i=0;i<n;i++) {
769                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
770                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
771                  }
772                  break;
773               case GL_RGB:
774                  for (i=0;i<n;i++) {
775                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
776                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
777                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
778                  }
779                  break;
780               case GL_RGBA:
781                  for (i=0;i<n;i++) {
782                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
783                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
784                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
785                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
786                  }
787                  break;
788               case GL_BGR:
789                  for (i=0;i<n;i++) {
790                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
791                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
792                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
793                  }
794                  break;
795               case GL_BGRA:
796                  for (i=0;i<n;i++) {
797                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
798                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
799                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
800                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
801                  }
802                  break;
803               case GL_ABGR_EXT:
804                  for (i=0;i<n;i++) {
805                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
806                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
807                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
808                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
809                  }
810                  break;
811               default:
812                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
813            }
814         }
815         break;
816      case GL_BYTE:
817         {
818            GLbyte *dst = (GLbyte *) dstAddr;
819            switch (dstFormat) {
820               case GL_RED:
821                  for (i=0;i<n;i++)
822                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
823                  break;
824               case GL_GREEN:
825                  for (i=0;i<n;i++)
826                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
827                  break;
828               case GL_BLUE:
829                  for (i=0;i<n;i++)
830                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
831                  break;
832               case GL_ALPHA:
833                  for (i=0;i<n;i++)
834                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
835                  break;
836               case GL_LUMINANCE:
837                  for (i=0;i<n;i++)
838                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
839                  break;
840               case GL_LUMINANCE_ALPHA:
841                  for (i=0;i<n;i++) {
842                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
843                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
844                  }
845                  break;
846               case GL_RGB:
847                  for (i=0;i<n;i++) {
848                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
849                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
850                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
851                  }
852                  break;
853               case GL_RGBA:
854                  for (i=0;i<n;i++) {
855                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
856                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
857                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
858                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
859                  }
860                  break;
861               case GL_BGR:
862                  for (i=0;i<n;i++) {
863                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
864                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
865                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
866                  }
867                  break;
868               case GL_BGRA:
869                  for (i=0;i<n;i++) {
870                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
871                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
872                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
873                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
874                  }
875               case GL_ABGR_EXT:
876                  for (i=0;i<n;i++) {
877                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
878                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
879                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
880                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
881                  }
882                  break;
883               default:
884                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
885            }
886         }
887         break;
888      case GL_UNSIGNED_SHORT:
889         {
890            GLushort *dst = (GLushort *) dstAddr;
891            switch (dstFormat) {
892               case GL_RED:
893                  for (i=0;i<n;i++)
894                     dst[i] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
895                  break;
896               case GL_GREEN:
897                  for (i=0;i<n;i++)
898                     dst[i] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
899                  break;
900               case GL_BLUE:
901                  for (i=0;i<n;i++)
902                     dst[i] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
903                  break;
904               case GL_ALPHA:
905                  for (i=0;i<n;i++)
906                     dst[i] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
907                  break;
908               case GL_LUMINANCE:
909                  for (i=0;i<n;i++)
910                     dst[i] = FLOAT_TO_USHORT(luminance[i]);
911                  break;
912               case GL_LUMINANCE_ALPHA:
913                  for (i=0;i<n;i++) {
914                     dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
915                     dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
916                  }
917                  break;
918               case GL_RGB:
919                  for (i=0;i<n;i++) {
920                     dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
921                     dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
922                     dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
923                  }
924                  break;
925               case GL_RGBA:
926                  for (i=0;i<n;i++) {
927                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
928                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
929                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
930                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
931                  }
932                  break;
933               case GL_BGR:
934                  for (i=0;i<n;i++) {
935                     dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
936                     dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
937                     dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
938                  }
939                  break;
940               case GL_BGRA:
941                  for (i=0;i<n;i++) {
942                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
943                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
944                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
945                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
946                  }
947                  break;
948               case GL_ABGR_EXT:
949                  for (i=0;i<n;i++) {
950                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
951                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
952                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
953                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
954                  }
955                  break;
956               default:
957                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
958            }
959            if (dstPacking->SwapBytes) {
960               _mesa_swap2( (GLushort *) dst, n * comps);
961            }
962         }
963         break;
964      case GL_SHORT:
965         {
966            GLshort *dst = (GLshort *) dstAddr;
967            switch (dstFormat) {
968               case GL_RED:
969                  for (i=0;i<n;i++)
970                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
971                  break;
972               case GL_GREEN:
973                  for (i=0;i<n;i++)
974                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
975                  break;
976               case GL_BLUE:
977                  for (i=0;i<n;i++)
978                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
979                  break;
980               case GL_ALPHA:
981                  for (i=0;i<n;i++)
982                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
983                  break;
984               case GL_LUMINANCE:
985                  for (i=0;i<n;i++)
986                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
987                  break;
988               case GL_LUMINANCE_ALPHA:
989                  for (i=0;i<n;i++) {
990                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
991                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
992                  }
993                  break;
994               case GL_RGB:
995                  for (i=0;i<n;i++) {
996                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
997                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
998                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
999                  }
1000                  break;
1001               case GL_RGBA:
1002                  for (i=0;i<n;i++) {
1003                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1004                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1005                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1006                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1007                  }
1008                  break;
1009               case GL_BGR:
1010                  for (i=0;i<n;i++) {
1011                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1012                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1013                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1014                  }
1015                  break;
1016               case GL_BGRA:
1017                  for (i=0;i<n;i++) {
1018                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1019                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1020                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1021                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1022                  }
1023               case GL_ABGR_EXT:
1024                  for (i=0;i<n;i++) {
1025                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1026                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1027                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1028                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1029                  }
1030                  break;
1031               default:
1032                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1033            }
1034            if (dstPacking->SwapBytes) {
1035               _mesa_swap2( (GLushort *) dst, n * comps );
1036            }
1037         }
1038         break;
1039      case GL_UNSIGNED_INT:
1040         {
1041            GLuint *dst = (GLuint *) dstAddr;
1042            switch (dstFormat) {
1043               case GL_RED:
1044                  for (i=0;i<n;i++)
1045                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1046                  break;
1047               case GL_GREEN:
1048                  for (i=0;i<n;i++)
1049                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1050                  break;
1051               case GL_BLUE:
1052                  for (i=0;i<n;i++)
1053                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1054                  break;
1055               case GL_ALPHA:
1056                  for (i=0;i<n;i++)
1057                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1058                  break;
1059               case GL_LUMINANCE:
1060                  for (i=0;i<n;i++)
1061                     dst[i] = FLOAT_TO_UINT(luminance[i]);
1062                  break;
1063               case GL_LUMINANCE_ALPHA:
1064                  for (i=0;i<n;i++) {
1065                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
1066                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1067                  }
1068                  break;
1069               case GL_RGB:
1070                  for (i=0;i<n;i++) {
1071                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1072                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1073                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1074                  }
1075                  break;
1076               case GL_RGBA:
1077                  for (i=0;i<n;i++) {
1078                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1079                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1080                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1081                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1082                  }
1083                  break;
1084               case GL_BGR:
1085                  for (i=0;i<n;i++) {
1086                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1087                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1088                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1089                  }
1090                  break;
1091               case GL_BGRA:
1092                  for (i=0;i<n;i++) {
1093                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1094                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1095                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1096                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1097                  }
1098                  break;
1099               case GL_ABGR_EXT:
1100                  for (i=0;i<n;i++) {
1101                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1102                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1103                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1104                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1105                  }
1106                  break;
1107               default:
1108                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1109            }
1110            if (dstPacking->SwapBytes) {
1111               _mesa_swap4( (GLuint *) dst, n * comps );
1112            }
1113         }
1114         break;
1115      case GL_INT:
1116         {
1117            GLint *dst = (GLint *) dstAddr;
1118            switch (dstFormat) {
1119               case GL_RED:
1120                  for (i=0;i<n;i++)
1121                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
1122                  break;
1123               case GL_GREEN:
1124                  for (i=0;i<n;i++)
1125                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
1126                  break;
1127               case GL_BLUE:
1128                  for (i=0;i<n;i++)
1129                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
1130                  break;
1131               case GL_ALPHA:
1132                  for (i=0;i<n;i++)
1133                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
1134                  break;
1135               case GL_LUMINANCE:
1136                  for (i=0;i<n;i++)
1137                     dst[i] = FLOAT_TO_INT(luminance[i]);
1138                  break;
1139               case GL_LUMINANCE_ALPHA:
1140                  for (i=0;i<n;i++) {
1141                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
1142                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
1143                  }
1144                  break;
1145               case GL_RGB:
1146                  for (i=0;i<n;i++) {
1147                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1148                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1149                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1150                  }
1151                  break;
1152               case GL_RGBA:
1153                  for (i=0;i<n;i++) {
1154                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1155                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1156                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1157                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1158                  }
1159                  break;
1160               case GL_BGR:
1161                  for (i=0;i<n;i++) {
1162                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1163                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1164                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1165                  }
1166                  break;
1167               case GL_BGRA:
1168                  for (i=0;i<n;i++) {
1169                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1170                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1171                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1172                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1173                  }
1174                  break;
1175               case GL_ABGR_EXT:
1176                  for (i=0;i<n;i++) {
1177                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
1178                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
1179                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
1180                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
1181                  }
1182                  break;
1183               default:
1184                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1185            }
1186            if (dstPacking->SwapBytes) {
1187               _mesa_swap4( (GLuint *) dst, n * comps );
1188            }
1189         }
1190         break;
1191      case GL_FLOAT:
1192         {
1193            GLfloat *dst = (GLfloat *) dstAddr;
1194            switch (dstFormat) {
1195               case GL_RED:
1196                  for (i=0;i<n;i++)
1197                     dst[i] = rgba[i][RCOMP];
1198                  break;
1199               case GL_GREEN:
1200                  for (i=0;i<n;i++)
1201                     dst[i] = rgba[i][GCOMP];
1202                  break;
1203               case GL_BLUE:
1204                  for (i=0;i<n;i++)
1205                     dst[i] = rgba[i][BCOMP];
1206                  break;
1207               case GL_ALPHA:
1208                  for (i=0;i<n;i++)
1209                     dst[i] = rgba[i][ACOMP];
1210                  break;
1211               case GL_LUMINANCE:
1212                  for (i=0;i<n;i++)
1213                     dst[i] = luminance[i];
1214                  break;
1215               case GL_LUMINANCE_ALPHA:
1216                  for (i=0;i<n;i++) {
1217                     dst[i*2+0] = luminance[i];
1218                     dst[i*2+1] = rgba[i][ACOMP];
1219                  }
1220                  break;
1221               case GL_RGB:
1222                  for (i=0;i<n;i++) {
1223                     dst[i*3+0] = rgba[i][RCOMP];
1224                     dst[i*3+1] = rgba[i][GCOMP];
1225                     dst[i*3+2] = rgba[i][BCOMP];
1226                  }
1227                  break;
1228               case GL_RGBA:
1229                  for (i=0;i<n;i++) {
1230                     dst[i*4+0] = rgba[i][RCOMP];
1231                     dst[i*4+1] = rgba[i][GCOMP];
1232                     dst[i*4+2] = rgba[i][BCOMP];
1233                     dst[i*4+3] = rgba[i][ACOMP];
1234                  }
1235                  break;
1236               case GL_BGR:
1237                  for (i=0;i<n;i++) {
1238                     dst[i*3+0] = rgba[i][BCOMP];
1239                     dst[i*3+1] = rgba[i][GCOMP];
1240                     dst[i*3+2] = rgba[i][RCOMP];
1241                  }
1242                  break;
1243               case GL_BGRA:
1244                  for (i=0;i<n;i++) {
1245                     dst[i*4+0] = rgba[i][BCOMP];
1246                     dst[i*4+1] = rgba[i][GCOMP];
1247                     dst[i*4+2] = rgba[i][RCOMP];
1248                     dst[i*4+3] = rgba[i][ACOMP];
1249                  }
1250                  break;
1251               case GL_ABGR_EXT:
1252                  for (i=0;i<n;i++) {
1253                     dst[i*4+0] = rgba[i][ACOMP];
1254                     dst[i*4+1] = rgba[i][BCOMP];
1255                     dst[i*4+2] = rgba[i][GCOMP];
1256                     dst[i*4+3] = rgba[i][RCOMP];
1257                  }
1258                  break;
1259               default:
1260                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1261            }
1262            if (dstPacking->SwapBytes) {
1263               _mesa_swap4( (GLuint *) dst, n * comps );
1264            }
1265         }
1266         break;
1267      case GL_UNSIGNED_BYTE_3_3_2:
1268         if (dstFormat == GL_RGB) {
1269            GLubyte *dst = (GLubyte *) dstAddr;
1270            for (i=0;i<n;i++) {
1271               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F)) << 5)
1272                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 2)
1273                      | (((GLint) (rgba[i][BCOMP] * 3.0F))     );
1274            }
1275         }
1276         break;
1277      case GL_UNSIGNED_BYTE_2_3_3_REV:
1278         if (dstFormat == GL_RGB) {
1279            GLubyte *dst = (GLubyte *) dstAddr;
1280            for (i=0;i<n;i++) {
1281               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F))     )
1282                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 3)
1283                      | (((GLint) (rgba[i][BCOMP] * 3.0F)) << 5);
1284            }
1285         }
1286         break;
1287      case GL_UNSIGNED_SHORT_5_6_5:
1288         if (dstFormat == GL_RGB) {
1289            GLushort *dst = (GLushort *) dstAddr;
1290            for (i=0;i<n;i++) {
1291               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1292                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1293                      | (((GLint) (rgba[i][BCOMP] * 31.0F))      );
1294            }
1295         }
1296         break;
1297      case GL_UNSIGNED_SHORT_5_6_5_REV:
1298         if (dstFormat == GL_RGB) {
1299            GLushort *dst = (GLushort *) dstAddr;
1300            for (i=0;i<n;i++) {
1301               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1302                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1303                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11);
1304            }
1305         }
1306         break;
1307      case GL_UNSIGNED_SHORT_4_4_4_4:
1308         if (dstFormat == GL_RGB) {
1309            GLushort *dst = (GLushort *) dstAddr;
1310            for (i=0;i<n;i++) {
1311               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12)
1312                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1313                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
1314                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1315            }
1316         }
1317         break;
1318      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1319         if (dstFormat == GL_RGB) {
1320            GLushort *dst = (GLushort *) dstAddr;
1321            for (i=0;i<n;i++) {
1322               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F))      )
1323                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1324                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
1325                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1326            }
1327         }
1328         break;
1329      case GL_UNSIGNED_SHORT_5_5_5_1:
1330         if (dstFormat == GL_RGB) {
1331            GLushort *dst = (GLushort *) dstAddr;
1332            for (i=0;i<n;i++) {
1333               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1334                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1335                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  1)
1336                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1337            }
1338         }
1339         break;
1340      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1341         if (dstFormat == GL_RGB) {
1342            GLushort *dst = (GLushort *) dstAddr;
1343            for (i=0;i<n;i++) {
1344               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1345                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1346                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 10)
1347                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1348            }
1349         }
1350         break;
1351      case GL_UNSIGNED_INT_8_8_8_8:
1352         if (dstFormat == GL_RGBA) {
1353            GLuint *dst = (GLuint *) dstAddr;
1354            for (i=0;i<n;i++) {
1355               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24)
1356                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1357                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
1358                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1359            }
1360         }
1361         else if (dstFormat == GL_BGRA) {
1362            GLuint *dst = (GLuint *) dstAddr;
1363            for (i=0;i<n;i++) {
1364               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 24)
1365                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1366                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) <<  8)
1367                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1368            }
1369         }
1370         else if (dstFormat == GL_ABGR_EXT) {
1371            GLuint *dst = (GLuint *) dstAddr;
1372            for (i=0;i<n;i++) {
1373               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24)
1374                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
1375                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1376                      | (((GLuint) (rgba[i][RCOMP] * 255.0F))      );
1377            }
1378         }
1379         break;
1380      case GL_UNSIGNED_INT_8_8_8_8_REV:
1381         if (dstFormat == GL_RGBA) {
1382            GLuint *dst = (GLuint *) dstAddr;
1383            for (i=0;i<n;i++) {
1384               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F))      )
1385                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1386                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
1387                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1388            }
1389         }
1390         else if (dstFormat == GL_BGRA) {
1391            GLuint *dst = (GLuint *) dstAddr;
1392            for (i=0;i<n;i++) {
1393               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F))      )
1394                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1395                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 16)
1396                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1397            }
1398         }
1399         else if (dstFormat == GL_ABGR_EXT) {
1400            GLuint *dst = (GLuint *) dstAddr;
1401            for (i=0;i<n;i++) {
1402               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F))      )
1403                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
1404                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1405                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24);
1406            }
1407         }
1408         break;
1409      case GL_UNSIGNED_INT_10_10_10_2:
1410         if (dstFormat == GL_RGBA) {
1411            GLuint *dst = (GLuint *) dstAddr;
1412            for (i=0;i<n;i++) {
1413               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 22)
1414                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
1415                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) <<  2)
1416                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
1417            }
1418         }
1419         else if (dstFormat == GL_BGRA) {
1420            GLuint *dst = (GLuint *) dstAddr;
1421            for (i=0;i<n;i++) {
1422               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 22)
1423                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
1424                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) <<  2)
1425                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
1426            }
1427         }
1428         else if (dstFormat == GL_ABGR_EXT) {
1429            GLuint *dst = (GLuint *) dstAddr;
1430            for (i=0;i<n;i++) {
1431               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F)) << 22)
1432                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 12)
1433                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) <<  2)
1434                      | (((GLuint) (rgba[i][RCOMP] *    3.0F))      );
1435            }
1436         }
1437         break;
1438      case GL_UNSIGNED_INT_2_10_10_10_REV:
1439         if (dstFormat == GL_RGBA) {
1440            GLuint *dst = (GLuint *) dstAddr;
1441            for (i=0;i<n;i++) {
1442               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F))      )
1443                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
1444                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 20)
1445                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
1446            }
1447         }
1448         else if (dstFormat == GL_BGRA) {
1449            GLuint *dst = (GLuint *) dstAddr;
1450            for (i=0;i<n;i++) {
1451               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F))      )
1452                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
1453                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 20)
1454                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
1455            }
1456         }
1457         else if (dstFormat == GL_ABGR_EXT) {
1458            GLuint *dst = (GLuint *) dstAddr;
1459            for (i=0;i<n;i++) {
1460               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F))      )
1461                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 10)
1462                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 20)
1463                      | (((GLuint) (rgba[i][RCOMP] *    3.0F)) << 30);
1464            }
1465         }
1466         break;
1467      default:
1468         gl_problem(ctx, "bad type in _mesa_pack_float_rgba_span");
1469   }
1470}
1471
1472
1473
1474#define SWAP2BYTE(VALUE)			\
1475   {						\
1476      GLubyte *bytes = (GLubyte *) &(VALUE);	\
1477      GLubyte tmp = bytes[0];			\
1478      bytes[0] = bytes[1];			\
1479      bytes[1] = tmp;				\
1480   }
1481
1482#define SWAP4BYTE(VALUE)			\
1483   {						\
1484      GLubyte *bytes = (GLubyte *) &(VALUE);	\
1485      GLubyte tmp = bytes[0];			\
1486      bytes[0] = bytes[3];			\
1487      bytes[3] = tmp;				\
1488      tmp = bytes[1];				\
1489      bytes[1] = bytes[2];			\
1490      bytes[2] = tmp;				\
1491   }
1492
1493
1494static void
1495extract_uint_indexes(GLuint n, GLuint indexes[],
1496                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
1497                     const struct gl_pixelstore_attrib *unpack )
1498{
1499   assert(srcFormat == GL_COLOR_INDEX);
1500
1501   ASSERT(srcType == GL_BITMAP ||
1502          srcType == GL_UNSIGNED_BYTE ||
1503          srcType == GL_BYTE ||
1504          srcType == GL_UNSIGNED_SHORT ||
1505          srcType == GL_SHORT ||
1506          srcType == GL_UNSIGNED_INT ||
1507          srcType == GL_INT ||
1508          srcType == GL_FLOAT);
1509
1510   switch (srcType) {
1511      case GL_BITMAP:
1512         {
1513            GLubyte *ubsrc = (GLubyte *) src;
1514            if (unpack->LsbFirst) {
1515               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
1516               GLuint i;
1517               for (i = 0; i < n; i++) {
1518                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
1519                  if (mask == 128) {
1520                     mask = 1;
1521                     ubsrc++;
1522                  }
1523                  else {
1524                     mask = mask << 1;
1525                  }
1526               }
1527            }
1528            else {
1529               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
1530               GLuint i;
1531               for (i = 0; i < n; i++) {
1532                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
1533                  if (mask == 1) {
1534                     mask = 128;
1535                     ubsrc++;
1536                  }
1537                  else {
1538                     mask = mask >> 1;
1539                  }
1540               }
1541            }
1542         }
1543         break;
1544      case GL_UNSIGNED_BYTE:
1545         {
1546            GLuint i;
1547            const GLubyte *s = (const GLubyte *) src;
1548            for (i = 0; i < n; i++)
1549               indexes[i] = s[i];
1550         }
1551         break;
1552      case GL_BYTE:
1553         {
1554            GLuint i;
1555            const GLbyte *s = (const GLbyte *) src;
1556            for (i = 0; i < n; i++)
1557               indexes[i] = s[i];
1558         }
1559         break;
1560      case GL_UNSIGNED_SHORT:
1561         {
1562            GLuint i;
1563            const GLushort *s = (const GLushort *) src;
1564            if (unpack->SwapBytes) {
1565               for (i = 0; i < n; i++) {
1566                  GLushort value = s[i];
1567                  SWAP2BYTE(value);
1568                  indexes[i] = value;
1569               }
1570            }
1571            else {
1572               for (i = 0; i < n; i++)
1573                  indexes[i] = s[i];
1574            }
1575         }
1576         break;
1577      case GL_SHORT:
1578         {
1579            GLuint i;
1580            const GLshort *s = (const GLshort *) src;
1581            if (unpack->SwapBytes) {
1582               for (i = 0; i < n; i++) {
1583                  GLshort value = s[i];
1584                  SWAP2BYTE(value);
1585                  indexes[i] = value;
1586               }
1587            }
1588            else {
1589               for (i = 0; i < n; i++)
1590                  indexes[i] = s[i];
1591            }
1592         }
1593         break;
1594      case GL_UNSIGNED_INT:
1595         {
1596            GLuint i;
1597            const GLuint *s = (const GLuint *) src;
1598            if (unpack->SwapBytes) {
1599               for (i = 0; i < n; i++) {
1600                  GLuint value = s[i];
1601                  SWAP4BYTE(value);
1602                  indexes[i] = value;
1603               }
1604            }
1605            else {
1606               for (i = 0; i < n; i++)
1607                  indexes[i] = s[i];
1608            }
1609         }
1610         break;
1611      case GL_INT:
1612         {
1613            GLuint i;
1614            const GLint *s = (const GLint *) src;
1615            if (unpack->SwapBytes) {
1616               for (i = 0; i < n; i++) {
1617                  GLint value = s[i];
1618                  SWAP4BYTE(value);
1619                  indexes[i] = value;
1620               }
1621            }
1622            else {
1623               for (i = 0; i < n; i++)
1624                  indexes[i] = s[i];
1625            }
1626         }
1627         break;
1628      case GL_FLOAT:
1629         {
1630            GLuint i;
1631            const GLfloat *s = (const GLfloat *) src;
1632            if (unpack->SwapBytes) {
1633               for (i = 0; i < n; i++) {
1634                  GLfloat value = s[i];
1635                  SWAP4BYTE(value);
1636                  indexes[i] = (GLuint) value;
1637               }
1638            }
1639            else {
1640               for (i = 0; i < n; i++)
1641                  indexes[i] = (GLuint) s[i];
1642            }
1643         }
1644         break;
1645      default:
1646         gl_problem(NULL, "bad srcType in extract_uint_indexes");
1647         return;
1648   }
1649}
1650
1651
1652
1653/*
1654 * This function extracts floating point RGBA values from arbitrary
1655 * image data.  srcFormat and srcType are the format and type parameters
1656 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1657 *
1658 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1659 * implements the "Conversion to floating point", "Conversion to RGB",
1660 * and "Final Expansion to RGBA" operations.
1661 *
1662 * Args:  n - number of pixels
1663 *        rgba - output colors
1664 *        srcFormat - format of incoming data
1665 *        srcType - datatype of incoming data
1666 *        src - source data pointer
1667 *        swapBytes - perform byteswapping of incoming data?
1668 */
1669static void
1670extract_float_rgba(GLuint n, GLfloat rgba[][4],
1671                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
1672                   GLboolean swapBytes)
1673{
1674   GLint redIndex, greenIndex, blueIndex, alphaIndex;
1675   GLint stride;
1676   GLint rComp, bComp, gComp, aComp;
1677
1678   ASSERT(srcFormat == GL_RED ||
1679          srcFormat == GL_GREEN ||
1680          srcFormat == GL_BLUE ||
1681          srcFormat == GL_ALPHA ||
1682          srcFormat == GL_LUMINANCE ||
1683          srcFormat == GL_LUMINANCE_ALPHA ||
1684          srcFormat == GL_INTENSITY ||
1685          srcFormat == GL_RGB ||
1686          srcFormat == GL_BGR ||
1687          srcFormat == GL_RGBA ||
1688          srcFormat == GL_BGRA ||
1689          srcFormat == GL_ABGR_EXT);
1690
1691   ASSERT(srcType == GL_UNSIGNED_BYTE ||
1692          srcType == GL_BYTE ||
1693          srcType == GL_UNSIGNED_SHORT ||
1694          srcType == GL_SHORT ||
1695          srcType == GL_UNSIGNED_INT ||
1696          srcType == GL_INT ||
1697          srcType == GL_FLOAT ||
1698          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
1699          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
1700          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
1701          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
1702          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
1703          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
1704          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
1705          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
1706          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1707          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
1708          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
1709          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
1710
1711   rComp = gComp = bComp = aComp = -1;
1712
1713   switch (srcFormat) {
1714      case GL_RED:
1715         redIndex = 0;
1716         greenIndex = blueIndex = alphaIndex = -1;
1717         stride = 1;
1718         break;
1719      case GL_GREEN:
1720         greenIndex = 0;
1721         redIndex = blueIndex = alphaIndex = -1;
1722         stride = 1;
1723         break;
1724      case GL_BLUE:
1725         blueIndex = 0;
1726         redIndex = greenIndex = alphaIndex = -1;
1727         stride = 1;
1728         break;
1729      case GL_ALPHA:
1730         redIndex = greenIndex = blueIndex = -1;
1731         alphaIndex = 0;
1732         stride = 1;
1733         break;
1734      case GL_LUMINANCE:
1735         redIndex = greenIndex = blueIndex = 0;
1736         alphaIndex = -1;
1737         stride = 1;
1738         break;
1739      case GL_LUMINANCE_ALPHA:
1740         redIndex = greenIndex = blueIndex = 0;
1741         alphaIndex = 1;
1742         stride = 2;
1743         break;
1744      case GL_INTENSITY:
1745         redIndex = 0;
1746         greenIndex = blueIndex = alphaIndex = -1;
1747         stride = 1;
1748         break;
1749      case GL_RGB:
1750         redIndex = 0;
1751         greenIndex = 1;
1752         blueIndex = 2;
1753         alphaIndex = -1;
1754         stride = 3;
1755         break;
1756      case GL_BGR:
1757         redIndex = 2;
1758         greenIndex = 1;
1759         blueIndex = 0;
1760         alphaIndex = -1;
1761         stride = 3;
1762         break;
1763      case GL_RGBA:
1764         redIndex = 0;
1765         greenIndex = 1;
1766         blueIndex = 2;
1767         alphaIndex = 3;
1768         rComp = 0;
1769         gComp = 1;
1770         bComp = 2;
1771         aComp = 3;
1772         stride = 4;
1773         break;
1774      case GL_BGRA:
1775         redIndex = 2;
1776         greenIndex = 1;
1777         blueIndex = 0;
1778         alphaIndex = 3;
1779         rComp = 2;
1780         gComp = 1;
1781         bComp = 0;
1782         aComp = 3;
1783         stride = 4;
1784         break;
1785      case GL_ABGR_EXT:
1786         redIndex = 3;
1787         greenIndex = 2;
1788         blueIndex = 1;
1789         alphaIndex = 0;
1790         rComp = 3;
1791         gComp = 2;
1792         bComp = 1;
1793         aComp = 0;
1794         stride = 4;
1795         break;
1796      default:
1797         gl_problem(NULL, "bad srcFormat in extract float data");
1798         return;
1799   }
1800
1801
1802#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)		\
1803   if ((INDEX) < 0) {							\
1804      GLuint i;								\
1805      for (i = 0; i < n; i++) {						\
1806         rgba[i][CHANNEL] = DEFAULT;					\
1807      }									\
1808   }									\
1809   else if (swapBytes) {						\
1810      const TYPE *s = (const TYPE *) src;				\
1811      GLuint i;								\
1812      for (i = 0; i < n; i++) {						\
1813         TYPE value = s[INDEX];						\
1814         if (sizeof(TYPE) == 2) {					\
1815            SWAP2BYTE(value);						\
1816         }								\
1817         else if (sizeof(TYPE) == 4) {					\
1818            SWAP4BYTE(value);						\
1819         }								\
1820         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);		\
1821         s += stride;							\
1822      }									\
1823   }									\
1824   else {								\
1825      const TYPE *s = (const TYPE *) src;				\
1826      GLuint i;								\
1827      for (i = 0; i < n; i++) {						\
1828         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);		\
1829         s += stride;							\
1830      }									\
1831   }
1832
1833   switch (srcType) {
1834      case GL_UNSIGNED_BYTE:
1835         PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1836         PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1837         PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1838         PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
1839         break;
1840      case GL_BYTE:
1841         PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1842         PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1843         PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1844         PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
1845         break;
1846      case GL_UNSIGNED_SHORT:
1847         PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1848         PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1849         PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1850         PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
1851         break;
1852      case GL_SHORT:
1853         PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1854         PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1855         PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1856         PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
1857         break;
1858      case GL_UNSIGNED_INT:
1859         PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1860         PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1861         PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1862         PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
1863         break;
1864      case GL_INT:
1865         PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
1866         PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
1867         PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
1868         PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
1869         break;
1870      case GL_FLOAT:
1871         PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
1872         PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
1873         PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
1874         PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
1875         break;
1876      case GL_UNSIGNED_BYTE_3_3_2:
1877         {
1878            const GLubyte *ubsrc = (const GLubyte *) src;
1879            GLuint i;
1880            for (i = 0; i < n; i ++) {
1881               GLubyte p = ubsrc[i];
1882               rgba[i][RCOMP] = ((p >> 5)      ) * (1.0F / 7.0F);
1883               rgba[i][GCOMP] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
1884               rgba[i][BCOMP] = ((p     ) & 0x3) * (1.0F / 3.0F);
1885               rgba[i][ACOMP] = 1.0F;
1886            }
1887         }
1888         break;
1889      case GL_UNSIGNED_BYTE_2_3_3_REV:
1890         {
1891            const GLubyte *ubsrc = (const GLubyte *) src;
1892            GLuint i;
1893            for (i = 0; i < n; i ++) {
1894               GLubyte p = ubsrc[i];
1895               rgba[i][RCOMP] = ((p     ) & 0x7) * (1.0F / 7.0F);
1896               rgba[i][GCOMP] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
1897               rgba[i][BCOMP] = ((p >> 6)      ) * (1.0F / 3.0F);
1898               rgba[i][ACOMP] = 1.0F;
1899            }
1900         }
1901         break;
1902      case GL_UNSIGNED_SHORT_5_6_5:
1903         if (swapBytes) {
1904            const GLushort *ussrc = (const GLushort *) src;
1905            GLuint i;
1906            for (i = 0; i < n; i ++) {
1907               GLushort p = ussrc[i];
1908               SWAP2BYTE(p);
1909               rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1910               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1911               rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1912               rgba[i][ACOMP] = 1.0F;
1913            }
1914         }
1915         else {
1916            const GLushort *ussrc = (const GLushort *) src;
1917            GLuint i;
1918            for (i = 0; i < n; i ++) {
1919               GLushort p = ussrc[i];
1920               rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1921               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1922               rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1923               rgba[i][ACOMP] = 1.0F;
1924            }
1925         }
1926         break;
1927      case GL_UNSIGNED_SHORT_5_6_5_REV:
1928         if (swapBytes) {
1929            const GLushort *ussrc = (const GLushort *) src;
1930            GLuint i;
1931            for (i = 0; i < n; i ++) {
1932               GLushort p = ussrc[i];
1933               SWAP2BYTE(p);
1934               rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1935               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1936               rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1937               rgba[i][ACOMP] = 1.0F;
1938            }
1939         }
1940         else {
1941            const GLushort *ussrc = (const GLushort *) src;
1942            GLuint i;
1943            for (i = 0; i < n; i ++) {
1944               GLushort p = ussrc[i];
1945               rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1946               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1947               rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1948               rgba[i][ACOMP] = 1.0F;
1949            }
1950         }
1951         break;
1952      case GL_UNSIGNED_SHORT_4_4_4_4:
1953         if (swapBytes) {
1954            const GLushort *ussrc = (const GLushort *) src;
1955            GLuint i;
1956            for (i = 0; i < n; i ++) {
1957               GLushort p = ussrc[i];
1958               SWAP2BYTE(p);
1959               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1960               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1961               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1962               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1963            }
1964         }
1965         else {
1966            const GLushort *ussrc = (const GLushort *) src;
1967            GLuint i;
1968            for (i = 0; i < n; i ++) {
1969               GLushort p = ussrc[i];
1970               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1971               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1972               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1973               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1974            }
1975         }
1976         break;
1977      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1978         if (swapBytes) {
1979            const GLushort *ussrc = (const GLushort *) src;
1980            GLuint i;
1981            for (i = 0; i < n; i ++) {
1982               GLushort p = ussrc[i];
1983               SWAP2BYTE(p);
1984               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1985               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1986               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1987               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1988            }
1989         }
1990         else {
1991            const GLushort *ussrc = (const GLushort *) src;
1992            GLuint i;
1993            for (i = 0; i < n; i ++) {
1994               GLushort p = ussrc[i];
1995               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1996               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1997               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1998               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1999            }
2000         }
2001         break;
2002      case GL_UNSIGNED_SHORT_5_5_5_1:
2003         if (swapBytes) {
2004            const GLushort *ussrc = (const GLushort *) src;
2005            GLuint i;
2006            for (i = 0; i < n; i ++) {
2007               GLushort p = ussrc[i];
2008               SWAP2BYTE(p);
2009               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2010               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2011               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2012               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2013            }
2014         }
2015         else {
2016            const GLushort *ussrc = (const GLushort *) src;
2017            GLuint i;
2018            for (i = 0; i < n; i ++) {
2019               GLushort p = ussrc[i];
2020               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2021               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2022               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2023               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2024            }
2025         }
2026         break;
2027      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2028         if (swapBytes) {
2029            const GLushort *ussrc = (const GLushort *) src;
2030            GLuint i;
2031            for (i = 0; i < n; i ++) {
2032               GLushort p = ussrc[i];
2033               SWAP2BYTE(p);
2034               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2035               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2036               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2037               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2038            }
2039         }
2040         else {
2041            const GLushort *ussrc = (const GLushort *) src;
2042            GLuint i;
2043            for (i = 0; i < n; i ++) {
2044               GLushort p = ussrc[i];
2045               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2046               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2047               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2048               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2049            }
2050         }
2051         break;
2052      case GL_UNSIGNED_INT_8_8_8_8:
2053         if (swapBytes) {
2054            const GLuint *uisrc = (const GLuint *) src;
2055            GLuint i;
2056            for (i = 0; i < n; i ++) {
2057               GLuint p = uisrc[i];
2058               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2059               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2060               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2061               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2062            }
2063         }
2064         else {
2065            const GLuint *uisrc = (const GLuint *) src;
2066            GLuint i;
2067            for (i = 0; i < n; i ++) {
2068               GLuint p = uisrc[i];
2069               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2070               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2071               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2072               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2073            }
2074         }
2075         break;
2076      case GL_UNSIGNED_INT_8_8_8_8_REV:
2077         if (swapBytes) {
2078            const GLuint *uisrc = (const GLuint *) src;
2079            GLuint i;
2080            for (i = 0; i < n; i ++) {
2081               GLuint p = uisrc[i];
2082               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2083               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2084               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2085               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2086            }
2087         }
2088         else {
2089            const GLuint *uisrc = (const GLuint *) src;
2090            GLuint i;
2091            for (i = 0; i < n; i ++) {
2092               GLuint p = uisrc[i];
2093               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2094               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2095               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2096               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2097            }
2098         }
2099         break;
2100      case GL_UNSIGNED_INT_10_10_10_2:
2101         if (swapBytes) {
2102            const GLuint *uisrc = (const GLuint *) src;
2103            GLuint i;
2104            for (i = 0; i < n; i ++) {
2105               GLuint p = uisrc[i];
2106               SWAP4BYTE(p);
2107               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2108               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2109               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2110               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2111            }
2112         }
2113         else {
2114            const GLuint *uisrc = (const GLuint *) src;
2115            GLuint i;
2116            for (i = 0; i < n; i ++) {
2117               GLuint p = uisrc[i];
2118               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2119               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2120               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2121               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2122            }
2123         }
2124         break;
2125      case GL_UNSIGNED_INT_2_10_10_10_REV:
2126         if (swapBytes) {
2127            const GLuint *uisrc = (const GLuint *) src;
2128            GLuint i;
2129            for (i = 0; i < n; i ++) {
2130               GLuint p = uisrc[i];
2131               SWAP4BYTE(p);
2132               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2133               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2134               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2135               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2136            }
2137         }
2138         else {
2139            const GLuint *uisrc = (const GLuint *) src;
2140            GLuint i;
2141            for (i = 0; i < n; i ++) {
2142               GLuint p = uisrc[i];
2143               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2144               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2145               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2146               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2147            }
2148         }
2149         break;
2150      default:
2151         gl_problem(NULL, "bad srcType in extract float data");
2152         break;
2153   }
2154}
2155
2156
2157
2158/*
2159 * Unpack a row of color image data from a client buffer according to
2160 * the pixel unpacking parameters.
2161 * Return GLubyte values in the specified dest image format.
2162 * This is (or will be) used by glDrawPixels and glTexImage?D().
2163 * Input:  ctx - the context
2164 *         n - number of pixels in the span
2165 *         dstFormat - format of destination color array
2166 *         dest - the destination color array
2167 *         srcFormat - source image format
2168 *         srcType - source image  datatype
2169 *         source - source image pointer
2170 *         srcPacking - pixel unpacking parameters
2171 *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
2172 *
2173 * XXX perhaps expand this to process whole images someday.
2174 */
2175void
2176_mesa_unpack_ubyte_color_span( GLcontext *ctx,
2177                               GLuint n, GLenum dstFormat, GLubyte dest[],
2178                               GLenum srcFormat, GLenum srcType,
2179                               const GLvoid *source,
2180                               const struct gl_pixelstore_attrib *srcPacking,
2181                               GLuint transferOps )
2182{
2183   ASSERT(dstFormat == GL_ALPHA ||
2184          dstFormat == GL_LUMINANCE ||
2185          dstFormat == GL_LUMINANCE_ALPHA ||
2186          dstFormat == GL_INTENSITY ||
2187          dstFormat == GL_RGB ||
2188          dstFormat == GL_RGBA ||
2189          dstFormat == GL_COLOR_INDEX);
2190
2191   ASSERT(srcFormat == GL_RED ||
2192          srcFormat == GL_GREEN ||
2193          srcFormat == GL_BLUE ||
2194          srcFormat == GL_ALPHA ||
2195          srcFormat == GL_LUMINANCE ||
2196          srcFormat == GL_LUMINANCE_ALPHA ||
2197          srcFormat == GL_INTENSITY ||
2198          srcFormat == GL_RGB ||
2199          srcFormat == GL_BGR ||
2200          srcFormat == GL_RGBA ||
2201          srcFormat == GL_BGRA ||
2202          srcFormat == GL_ABGR_EXT ||
2203          srcFormat == GL_COLOR_INDEX);
2204
2205   ASSERT(srcType == GL_BITMAP ||
2206          srcType == GL_UNSIGNED_BYTE ||
2207          srcType == GL_BYTE ||
2208          srcType == GL_UNSIGNED_SHORT ||
2209          srcType == GL_SHORT ||
2210          srcType == GL_UNSIGNED_INT ||
2211          srcType == GL_INT ||
2212          srcType == GL_FLOAT ||
2213          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2214          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2215          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2216          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2217          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2218          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2219          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2220          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2221          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2222          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2223          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2224          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2225
2226   /* this is intended for RGBA mode only */
2227   assert(ctx->Visual->RGBAflag);
2228
2229   /* Try simple cases first */
2230   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE) {
2231      if (dstFormat == GL_RGBA) {
2232         if (srcFormat == GL_RGBA) {
2233            MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
2234            return;
2235         }
2236         else if (srcFormat == GL_RGB) {
2237            GLuint i;
2238            const GLubyte *src = (const GLubyte *) source;
2239            GLubyte *dst = dest;
2240            for (i = 0; i < n; i++) {
2241               dst[0] = src[0];
2242               dst[1] = src[1];
2243               dst[2] = src[2];
2244               dst[3] = 255;
2245               src += 3;
2246               dst += 4;
2247            }
2248            return;
2249         }
2250      }
2251      else if (dstFormat == GL_RGB) {
2252         if (srcFormat == GL_RGB) {
2253            MEMCPY( dest, source, n * 3 * sizeof(GLubyte) );
2254            return;
2255         }
2256         else if (srcFormat == GL_RGBA) {
2257            GLuint i;
2258            const GLubyte *src = (const GLubyte *) source;
2259            GLubyte *dst = dest;
2260            for (i = 0; i < n; i++) {
2261               dst[0] = src[0];
2262               dst[1] = src[1];
2263               dst[2] = src[2];
2264               src += 4;
2265               dst += 3;
2266            }
2267            return;
2268         }
2269      }
2270      else if (dstFormat == srcFormat) {
2271         GLint comps = _mesa_components_in_format(srcFormat);
2272         assert(comps > 0);
2273         MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
2274         return;
2275      }
2276   }
2277
2278
2279   /* general solution begins here */
2280   {
2281      GLfloat rgba[MAX_WIDTH][4];
2282      GLint dstComponents;
2283      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2284      GLint dstLuminanceIndex, dstIntensityIndex;
2285
2286      dstComponents = _mesa_components_in_format( dstFormat );
2287      /* source & dest image formats should have been error checked by now */
2288      assert(dstComponents > 0);
2289
2290      /*
2291       * Extract image data and convert to RGBA floats
2292       */
2293      assert(n <= MAX_WIDTH);
2294      if (srcFormat == GL_COLOR_INDEX) {
2295         GLuint indexes[MAX_WIDTH];
2296         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2297                              srcPacking);
2298
2299         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2300            _mesa_map_ci(ctx, n, indexes);
2301         }
2302         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2303            _mesa_shift_and_offset_ci(ctx, n, indexes);
2304         }
2305
2306         if (dstFormat == GL_COLOR_INDEX) {
2307            /* convert to GLubyte and return */
2308            GLuint i;
2309            for (i = 0; i < n; i++) {
2310               dest[i] = (GLubyte) (indexes[i] & 0xff);
2311            }
2312            return;
2313         }
2314         else {
2315            /* Convert indexes to RGBA */
2316            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
2317         }
2318      }
2319      else {
2320         extract_float_rgba(n, rgba, srcFormat, srcType, source,
2321                            srcPacking->SwapBytes);
2322
2323         /* scale and bias colors */
2324         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
2325            _mesa_scale_and_bias_rgba(ctx, n, rgba);
2326         }
2327         /* color map lookup */
2328         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2329            _mesa_map_rgba(ctx, n, rgba);
2330         }
2331      }
2332
2333      if (transferOps) {
2334         /* GL_COLOR_TABLE lookup */
2335         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
2336            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
2337         }
2338         /* convolution */
2339         if (transferOps & IMAGE_CONVOLUTION_BIT) {
2340            /* this has to be done in the calling code */
2341         }
2342         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
2343         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
2344            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
2345         }
2346         /* color matrix transform */
2347         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
2348            _mesa_transform_rgba(ctx, n, rgba);
2349         }
2350         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
2351         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
2352            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
2353         }
2354         /* update histogram count */
2355         if (transferOps & IMAGE_HISTOGRAM_BIT) {
2356            _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
2357         }
2358         /* min/max here */
2359         if (transferOps & IMAGE_MIN_MAX_BIT) {
2360            _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
2361         }
2362      }
2363
2364      /* clamp to [0,1] */
2365      {
2366         GLuint i;
2367         for (i = 0; i < n; i++) {
2368            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2369            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2370            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2371            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2372         }
2373      }
2374
2375      /* Now determine which color channels we need to produce.
2376       * And determine the dest index (offset) within each color tuple.
2377       */
2378      switch (dstFormat) {
2379         case GL_ALPHA:
2380            dstAlphaIndex = 0;
2381            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2382            dstLuminanceIndex = dstIntensityIndex = -1;
2383            break;
2384         case GL_LUMINANCE:
2385            dstLuminanceIndex = 0;
2386            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2387            dstIntensityIndex = -1;
2388            break;
2389         case GL_LUMINANCE_ALPHA:
2390            dstLuminanceIndex = 0;
2391            dstAlphaIndex = 1;
2392            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2393            dstIntensityIndex = -1;
2394            break;
2395         case GL_INTENSITY:
2396            dstIntensityIndex = 0;
2397            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2398            dstLuminanceIndex = -1;
2399            break;
2400         case GL_RGB:
2401            dstRedIndex = 0;
2402            dstGreenIndex = 1;
2403            dstBlueIndex = 2;
2404            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2405            break;
2406         case GL_RGBA:
2407            dstRedIndex = 0;
2408            dstGreenIndex = 1;
2409            dstBlueIndex = 2;
2410            dstAlphaIndex = 3;
2411            dstLuminanceIndex = dstIntensityIndex = -1;
2412            break;
2413         default:
2414            gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
2415            return;
2416      }
2417
2418
2419      /* Now return the GLubyte data in the requested dstFormat */
2420
2421      if (dstRedIndex >= 0) {
2422         GLubyte *dst = dest;
2423         GLuint i;
2424         for (i = 0; i < n; i++) {
2425            dst[dstRedIndex] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2426            dst += dstComponents;
2427         }
2428      }
2429
2430      if (dstGreenIndex >= 0) {
2431         GLubyte *dst = dest;
2432         GLuint i;
2433         for (i = 0; i < n; i++) {
2434            dst[dstGreenIndex] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2435            dst += dstComponents;
2436         }
2437      }
2438
2439      if (dstBlueIndex >= 0) {
2440         GLubyte *dst = dest;
2441         GLuint i;
2442         for (i = 0; i < n; i++) {
2443            dst[dstBlueIndex] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2444            dst += dstComponents;
2445         }
2446      }
2447
2448      if (dstAlphaIndex >= 0) {
2449         GLubyte *dst = dest;
2450         GLuint i;
2451         for (i = 0; i < n; i++) {
2452            dst[dstAlphaIndex] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2453            dst += dstComponents;
2454         }
2455      }
2456
2457      if (dstIntensityIndex >= 0) {
2458         GLubyte *dst = dest;
2459         GLuint i;
2460         assert(dstIntensityIndex == 0);
2461         assert(dstComponents == 1);
2462         for (i = 0; i < n; i++) {
2463            /* Intensity comes from red channel */
2464            dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2465         }
2466      }
2467
2468      if (dstLuminanceIndex >= 0) {
2469         GLubyte *dst = dest;
2470         GLuint i;
2471         assert(dstLuminanceIndex == 0);
2472         for (i = 0; i < n; i++) {
2473            /* Luminance comes from red channel */
2474            dst[0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2475            dst += dstComponents;
2476         }
2477      }
2478   }
2479}
2480
2481
2482void
2483_mesa_unpack_float_color_span( GLcontext *ctx,
2484                               GLuint n, GLenum dstFormat, GLfloat dest[],
2485                               GLenum srcFormat, GLenum srcType,
2486                               const GLvoid *source,
2487                               const struct gl_pixelstore_attrib *srcPacking,
2488                               GLuint transferOps, GLboolean clamp )
2489{
2490   ASSERT(dstFormat == GL_ALPHA ||
2491          dstFormat == GL_LUMINANCE ||
2492          dstFormat == GL_LUMINANCE_ALPHA ||
2493          dstFormat == GL_INTENSITY ||
2494          dstFormat == GL_RGB ||
2495          dstFormat == GL_RGBA ||
2496          dstFormat == GL_COLOR_INDEX);
2497
2498   ASSERT(srcFormat == GL_RED ||
2499          srcFormat == GL_GREEN ||
2500          srcFormat == GL_BLUE ||
2501          srcFormat == GL_ALPHA ||
2502          srcFormat == GL_LUMINANCE ||
2503          srcFormat == GL_LUMINANCE_ALPHA ||
2504          srcFormat == GL_INTENSITY ||
2505          srcFormat == GL_RGB ||
2506          srcFormat == GL_BGR ||
2507          srcFormat == GL_RGBA ||
2508          srcFormat == GL_BGRA ||
2509          srcFormat == GL_ABGR_EXT ||
2510          srcFormat == GL_COLOR_INDEX);
2511
2512   ASSERT(srcType == GL_BITMAP ||
2513          srcType == GL_UNSIGNED_BYTE ||
2514          srcType == GL_BYTE ||
2515          srcType == GL_UNSIGNED_SHORT ||
2516          srcType == GL_SHORT ||
2517          srcType == GL_UNSIGNED_INT ||
2518          srcType == GL_INT ||
2519          srcType == GL_FLOAT ||
2520          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2521          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2522          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2523          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2524          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2525          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2526          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2527          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2528          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2529          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2530          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2531          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2532
2533   /* this is intended for RGBA mode only */
2534   assert(ctx->Visual->RGBAflag);
2535
2536   /* general solution, no special cases, yet */
2537   {
2538      GLfloat rgba[MAX_WIDTH][4];
2539      GLint dstComponents;
2540      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2541      GLint dstLuminanceIndex, dstIntensityIndex;
2542
2543      dstComponents = _mesa_components_in_format( dstFormat );
2544      /* source & dest image formats should have been error checked by now */
2545      assert(dstComponents > 0);
2546
2547      /*
2548       * Extract image data and convert to RGBA floats
2549       */
2550      assert(n <= MAX_WIDTH);
2551      if (srcFormat == GL_COLOR_INDEX) {
2552         GLuint indexes[MAX_WIDTH];
2553         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2554                              srcPacking);
2555
2556         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2557            _mesa_map_ci(ctx, n, indexes);
2558         }
2559         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2560            _mesa_shift_and_offset_ci(ctx, n, indexes);
2561         }
2562
2563         if (dstFormat == GL_COLOR_INDEX) {
2564            /* convert to GLubyte and return */
2565            GLuint i;
2566            for (i = 0; i < n; i++) {
2567               dest[i] = (GLubyte) (indexes[i] & 0xff);
2568            }
2569            return;
2570         }
2571         else {
2572            /* Convert indexes to RGBA */
2573            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
2574         }
2575      }
2576      else {
2577         extract_float_rgba(n, rgba, srcFormat, srcType, source,
2578                            srcPacking->SwapBytes);
2579
2580         /* scale and bias colors */
2581         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
2582            _mesa_scale_and_bias_rgba(ctx, n, rgba);
2583         }
2584         /* color map lookup */
2585         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2586            _mesa_map_rgba(ctx, n, rgba);
2587         }
2588      }
2589
2590      if (transferOps) {
2591         /* GL_COLOR_TABLE lookup */
2592         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
2593            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
2594         }
2595         /* convolution */
2596         if (transferOps & IMAGE_CONVOLUTION_BIT) {
2597            /* XXX to do */
2598         }
2599         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
2600         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
2601            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
2602         }
2603         /* color matrix transform */
2604         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
2605            _mesa_transform_rgba(ctx, n, rgba);
2606         }
2607         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
2608         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
2609            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
2610         }
2611         /* update histogram count */
2612         if (transferOps & IMAGE_HISTOGRAM_BIT) {
2613            _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
2614         }
2615         /* min/max here */
2616         if (transferOps & IMAGE_MIN_MAX_BIT) {
2617            _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
2618         }
2619      }
2620
2621      /* clamp to [0,1] */
2622      if (clamp) {
2623         GLuint i;
2624         for (i = 0; i < n; i++) {
2625            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2626            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2627            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2628            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2629         }
2630      }
2631
2632      /* Now determine which color channels we need to produce.
2633       * And determine the dest index (offset) within each color tuple.
2634       */
2635      switch (dstFormat) {
2636         case GL_ALPHA:
2637            dstAlphaIndex = 0;
2638            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2639            dstLuminanceIndex = dstIntensityIndex = -1;
2640            break;
2641         case GL_LUMINANCE:
2642            dstLuminanceIndex = 0;
2643            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2644            dstIntensityIndex = -1;
2645            break;
2646         case GL_LUMINANCE_ALPHA:
2647            dstLuminanceIndex = 0;
2648            dstAlphaIndex = 1;
2649            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2650            dstIntensityIndex = -1;
2651            break;
2652         case GL_INTENSITY:
2653            dstIntensityIndex = 0;
2654            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2655            dstLuminanceIndex = -1;
2656            break;
2657         case GL_RGB:
2658            dstRedIndex = 0;
2659            dstGreenIndex = 1;
2660            dstBlueIndex = 2;
2661            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2662            break;
2663         case GL_RGBA:
2664            dstRedIndex = 0;
2665            dstGreenIndex = 1;
2666            dstBlueIndex = 2;
2667            dstAlphaIndex = 3;
2668            dstLuminanceIndex = dstIntensityIndex = -1;
2669            break;
2670         default:
2671            gl_problem(ctx, "bad dstFormat in _mesa_unpack_float_color_span()");
2672            return;
2673      }
2674
2675      /* Now pack results in the requested dstFormat */
2676      if (dstRedIndex >= 0) {
2677         GLfloat *dst = dest;
2678         GLuint i;
2679         for (i = 0; i < n; i++) {
2680            dst[dstRedIndex] = rgba[i][RCOMP];
2681            dst += dstComponents;
2682         }
2683      }
2684
2685      if (dstGreenIndex >= 0) {
2686         GLfloat *dst = dest;
2687         GLuint i;
2688         for (i = 0; i < n; i++) {
2689            dst[dstGreenIndex] = rgba[i][GCOMP];
2690            dst += dstComponents;
2691         }
2692      }
2693
2694      if (dstBlueIndex >= 0) {
2695         GLfloat *dst = dest;
2696         GLuint i;
2697         for (i = 0; i < n; i++) {
2698            dst[dstBlueIndex] = rgba[i][BCOMP];
2699            dst += dstComponents;
2700         }
2701      }
2702
2703      if (dstAlphaIndex >= 0) {
2704         GLfloat *dst = dest;
2705         GLuint i;
2706         for (i = 0; i < n; i++) {
2707            dst[dstAlphaIndex] = rgba[i][ACOMP];
2708            dst += dstComponents;
2709         }
2710      }
2711
2712      if (dstIntensityIndex >= 0) {
2713         GLfloat *dst = dest;
2714         GLuint i;
2715         assert(dstIntensityIndex == 0);
2716         assert(dstComponents == 1);
2717         for (i = 0; i < n; i++) {
2718            /* Intensity comes from red channel */
2719            dst[i] = rgba[i][RCOMP];
2720         }
2721      }
2722
2723      if (dstLuminanceIndex >= 0) {
2724         GLfloat *dst = dest;
2725         GLuint i;
2726         assert(dstLuminanceIndex == 0);
2727         for (i = 0; i < n; i++) {
2728            /* Luminance comes from red channel */
2729            dst[0] = rgba[i][RCOMP];
2730            dst += dstComponents;
2731         }
2732      }
2733   }
2734}
2735
2736
2737
2738
2739/*
2740 * Unpack a row of color index data from a client buffer according to
2741 * the pixel unpacking parameters.
2742 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2743 *
2744 * Args:  ctx - the context
2745 *        n - number of pixels
2746 *        dstType - destination datatype
2747 *        dest - destination array
2748 *        srcType - source pixel type
2749 *        source - source data pointer
2750 *        srcPacking - pixel unpacking parameters
2751 *        transferOps - the pixel transfer operations to apply
2752 */
2753void
2754_mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
2755                         GLenum dstType, GLvoid *dest,
2756                         GLenum srcType, const GLvoid *source,
2757                         const struct gl_pixelstore_attrib *srcPacking,
2758                         GLuint transferOps )
2759{
2760   ASSERT(srcType == GL_BITMAP ||
2761          srcType == GL_UNSIGNED_BYTE ||
2762          srcType == GL_BYTE ||
2763          srcType == GL_UNSIGNED_SHORT ||
2764          srcType == GL_SHORT ||
2765          srcType == GL_UNSIGNED_INT ||
2766          srcType == GL_INT ||
2767          srcType == GL_FLOAT);
2768
2769   ASSERT(dstType == GL_UNSIGNED_BYTE ||
2770          dstType == GL_UNSIGNED_SHORT ||
2771          dstType == GL_UNSIGNED_INT);
2772
2773
2774   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
2775
2776   /*
2777    * Try simple cases first
2778    */
2779   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
2780       && dstType == GL_UNSIGNED_BYTE) {
2781      MEMCPY(dest, source, n * sizeof(GLubyte));
2782   }
2783   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
2784            && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
2785      MEMCPY(dest, source, n * sizeof(GLuint));
2786   }
2787   else {
2788      /*
2789       * general solution
2790       */
2791      GLuint indexes[MAX_WIDTH];
2792      assert(n <= MAX_WIDTH);
2793
2794      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2795                           srcPacking);
2796
2797      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2798         /* shift and offset indexes */
2799         _mesa_shift_and_offset_ci(ctx, n, indexes);
2800      }
2801      if (transferOps & IMAGE_MAP_COLOR_BIT) {
2802         /* Apply lookup table */
2803         _mesa_map_ci(ctx, n, indexes);
2804      }
2805
2806      /* convert to dest type */
2807      switch (dstType) {
2808         case GL_UNSIGNED_BYTE:
2809            {
2810               GLubyte *dst = (GLubyte *) dest;
2811               GLuint i;
2812               for (i = 0; i < n; i++) {
2813                  dst[i] = (GLubyte) (indexes[i] & 0xff);
2814               }
2815            }
2816            break;
2817         case GL_UNSIGNED_SHORT:
2818            {
2819               GLuint *dst = (GLuint *) dest;
2820               GLuint i;
2821               for (i = 0; i < n; i++) {
2822                  dst[i] = (GLushort) (indexes[i] & 0xffff);
2823               }
2824            }
2825            break;
2826         case GL_UNSIGNED_INT:
2827            MEMCPY(dest, indexes, n * sizeof(GLuint));
2828            break;
2829         default:
2830            gl_problem(ctx, "bad dstType in _mesa_unpack_index_span");
2831      }
2832   }
2833}
2834
2835
2836/*
2837 * Unpack a row of stencil data from a client buffer according to
2838 * the pixel unpacking parameters.
2839 * This is (or will be) used by glDrawPixels
2840 *
2841 * Args:  ctx - the context
2842 *        n - number of pixels
2843 *        dstType - destination datatype
2844 *        dest - destination array
2845 *        srcType - source pixel type
2846 *        source - source data pointer
2847 *        srcPacking - pixel unpacking parameters
2848 *        transferOps - apply offset/bias/lookup ops?
2849 */
2850void
2851_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
2852                           GLenum dstType, GLvoid *dest,
2853                           GLenum srcType, const GLvoid *source,
2854                           const struct gl_pixelstore_attrib *srcPacking,
2855                           GLuint transferOps )
2856{
2857   ASSERT(srcType == GL_BITMAP ||
2858          srcType == GL_UNSIGNED_BYTE ||
2859          srcType == GL_BYTE ||
2860          srcType == GL_UNSIGNED_SHORT ||
2861          srcType == GL_SHORT ||
2862          srcType == GL_UNSIGNED_INT ||
2863          srcType == GL_INT ||
2864          srcType == GL_FLOAT);
2865
2866   ASSERT(dstType == GL_UNSIGNED_BYTE ||
2867          dstType == GL_UNSIGNED_SHORT ||
2868          dstType == GL_UNSIGNED_INT);
2869
2870   /* only shift and offset apply to stencil */
2871   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
2872
2873   /*
2874    * Try simple cases first
2875    */
2876   if (transferOps == 0 &&
2877       srcType == GL_UNSIGNED_BYTE &&
2878       dstType == GL_UNSIGNED_BYTE) {
2879      MEMCPY(dest, source, n * sizeof(GLubyte));
2880   }
2881   else if (transferOps == 0 &&
2882            srcType == GL_UNSIGNED_INT &&
2883            dstType == GL_UNSIGNED_INT &&
2884            !srcPacking->SwapBytes) {
2885      MEMCPY(dest, source, n * sizeof(GLuint));
2886   }
2887   else {
2888      /*
2889       * general solution
2890       */
2891      GLuint indexes[MAX_WIDTH];
2892      assert(n <= MAX_WIDTH);
2893
2894      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2895                           srcPacking);
2896
2897      if (transferOps) {
2898         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2899            /* shift and offset indexes */
2900            _mesa_shift_and_offset_ci(ctx, n, indexes);
2901         }
2902
2903         if (ctx->Pixel.MapStencilFlag) {
2904            /* Apply stencil lookup table */
2905            GLuint mask = ctx->Pixel.MapStoSsize - 1;
2906            GLuint i;
2907            for (i=0;i<n;i++) {
2908               indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
2909            }
2910         }
2911      }
2912
2913      /* convert to dest type */
2914      switch (dstType) {
2915         case GL_UNSIGNED_BYTE:
2916            {
2917               GLubyte *dst = (GLubyte *) dest;
2918               GLuint i;
2919               for (i = 0; i < n; i++) {
2920                  dst[i] = (GLubyte) (indexes[i] & 0xff);
2921               }
2922            }
2923            break;
2924         case GL_UNSIGNED_SHORT:
2925            {
2926               GLuint *dst = (GLuint *) dest;
2927               GLuint i;
2928               for (i = 0; i < n; i++) {
2929                  dst[i] = (GLushort) (indexes[i] & 0xffff);
2930               }
2931            }
2932            break;
2933         case GL_UNSIGNED_INT:
2934            MEMCPY(dest, indexes, n * sizeof(GLuint));
2935            break;
2936         default:
2937            gl_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
2938      }
2939   }
2940}
2941
2942
2943
2944void
2945_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
2946                         GLenum srcType, const GLvoid *source,
2947                         const struct gl_pixelstore_attrib *srcPacking,
2948                         GLuint transferOps )
2949{
2950   GLfloat *depth = MALLOC(n * sizeof(GLfloat));
2951   if (!depth)
2952      return;
2953
2954   switch (srcType) {
2955      case GL_BYTE:
2956         {
2957            GLuint i;
2958            const GLubyte *src = (const GLubyte *) source;
2959            for (i = 0; i < n; i++) {
2960               depth[i] = BYTE_TO_FLOAT(src[i]);
2961            }
2962         }
2963         break;
2964      case GL_UNSIGNED_BYTE:
2965         {
2966            GLuint i;
2967            const GLubyte *src = (const GLubyte *) source;
2968            for (i = 0; i < n; i++) {
2969               depth[i] = UBYTE_TO_FLOAT(src[i]);
2970            }
2971         }
2972         break;
2973      case GL_SHORT:
2974         {
2975            GLuint i;
2976            const GLshort *src = (const GLshort *) source;
2977            for (i = 0; i < n; i++) {
2978               depth[i] = SHORT_TO_FLOAT(src[i]);
2979            }
2980         }
2981         break;
2982      case GL_UNSIGNED_SHORT:
2983         {
2984            GLuint i;
2985            const GLushort *src = (const GLushort *) source;
2986            for (i = 0; i < n; i++) {
2987               depth[i] = USHORT_TO_FLOAT(src[i]);
2988            }
2989         }
2990         break;
2991      case GL_INT:
2992         {
2993            GLuint i;
2994            const GLint *src = (const GLint *) source;
2995            for (i = 0; i < n; i++) {
2996               depth[i] = INT_TO_FLOAT(src[i]);
2997            }
2998         }
2999         break;
3000      case GL_UNSIGNED_INT:
3001         {
3002            GLuint i;
3003            const GLuint *src = (const GLuint *) source;
3004            for (i = 0; i < n; i++) {
3005               depth[i] = UINT_TO_FLOAT(src[i]);
3006            }
3007         }
3008         break;
3009      case GL_FLOAT:
3010         MEMCPY(depth, source, n * sizeof(GLfloat));
3011         break;
3012      default:
3013         gl_problem(NULL, "bad type in _mesa_unpack_depth_span()");
3014         FREE(depth);
3015         return;
3016   }
3017
3018
3019   /* apply depth scale and bias */
3020   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
3021      GLuint i;
3022      for (i = 0; i < n; i++) {
3023         depth[i] = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
3024      }
3025   }
3026
3027   /* clamp depth values to [0,1] and convert from floats to integers */
3028   {
3029      const GLfloat zs = ctx->Visual->DepthMaxF;
3030      GLuint i;
3031      for (i = 0; i < n; i++) {
3032         dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
3033      }
3034   }
3035
3036   FREE(depth);
3037}
3038
3039
3040
3041/*
3042 * Unpack image data.  Apply byteswapping, byte flipping (bitmap).
3043 * Return all image data in a contiguous block.
3044 */
3045void *
3046_mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
3047                    GLenum format, GLenum type, const GLvoid *pixels,
3048                    const struct gl_pixelstore_attrib *unpack )
3049{
3050   GLint bytesPerRow, compsPerRow;
3051   GLboolean flipBytes, swap2, swap4;
3052
3053   if (!pixels)
3054      return NULL;  /* not necessarily an error */
3055
3056   if (width <= 0 || height <= 0 || depth <= 0)
3057      return NULL;  /* generate error later */
3058
3059   if (format == GL_BITMAP) {
3060      bytesPerRow = (width + 7) >> 3;
3061      flipBytes = !unpack->LsbFirst;
3062      swap2 = swap4 = GL_FALSE;
3063      compsPerRow = 0;
3064   }
3065   else {
3066      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
3067      const GLint components = _mesa_components_in_format(format);
3068      GLint bytesPerComp;
3069      if (bytesPerPixel <= 0 || components <= 0)
3070         return NULL;   /* bad format or type.  generate error later */
3071      bytesPerRow = bytesPerPixel * width;
3072      bytesPerComp = bytesPerPixel / components;
3073      flipBytes = GL_FALSE;
3074      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
3075      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
3076      compsPerRow = components * width;
3077      assert(compsPerRow >= width);
3078   }
3079
3080   {
3081      GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
3082      GLubyte *dst;
3083      GLint img, row;
3084      if (!destBuffer)
3085         return NULL;   /* generate GL_OUT_OF_MEMORY later */
3086
3087      dst = destBuffer;
3088      for (img = 0; img < depth; img++) {
3089         for (row = 0; row < height; row++) {
3090            const GLvoid *src = _mesa_image_address(unpack, pixels,
3091                               width, height, format, type, img, row, 0);
3092            MEMCPY(dst, src, bytesPerRow);
3093            /* byte flipping/swapping */
3094            if (flipBytes) {
3095               flip_bytes((GLubyte *) dst, bytesPerRow);
3096            }
3097            else if (swap2) {
3098               _mesa_swap2((GLushort*) dst, compsPerRow);
3099            }
3100            else if (swap4) {
3101               _mesa_swap4((GLuint*) dst, compsPerRow);
3102            }
3103            dst += bytesPerRow;
3104         }
3105      }
3106      return destBuffer;
3107   }
3108}
3109
3110
3111/*
3112 * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
3113 * order with row alignment = 1 byte.
3114 */
3115GLvoid *
3116_mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
3117                     const struct gl_pixelstore_attrib *packing )
3118{
3119   GLint bytes, row, width_in_bytes;
3120   GLubyte *buffer, *dst;
3121
3122   if (!pixels)
3123      return NULL;
3124
3125   /* Alloc dest storage */
3126   bytes = ((width + 7) / 8 * height);
3127   buffer = (GLubyte *) MALLOC( bytes );
3128   if (!buffer)
3129      return NULL;
3130
3131
3132   width_in_bytes = CEILING( width, 8 );
3133   dst = buffer;
3134   for (row = 0; row < height; row++) {
3135      GLubyte *src = _mesa_image_address( packing, pixels, width, height,
3136                                          GL_COLOR_INDEX, GL_BITMAP,
3137                                          0, row, 0 );
3138      if (!src) {
3139         FREE(buffer);
3140         return NULL;
3141      }
3142
3143      if (packing->SkipPixels == 0) {
3144         MEMCPY( dst, src, width_in_bytes );
3145         if (packing->LsbFirst) {
3146            flip_bytes( dst, width_in_bytes );
3147         }
3148      }
3149      else {
3150         /* handling SkipPixels is a bit tricky (no pun intended!) */
3151         GLint i;
3152         if (packing->LsbFirst) {
3153            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
3154            GLubyte dstMask = 128;
3155            GLubyte *s = src;
3156            GLubyte *d = dst;
3157            *d = 0;
3158            for (i = 0; i < width; i++) {
3159               if (*s & srcMask) {
3160                  *d |= dstMask;
3161               }
3162               if (srcMask == 128) {
3163                  srcMask = 1;
3164                  s++;
3165               }
3166               else {
3167                  srcMask = srcMask << 1;
3168               }
3169               if (dstMask == 1) {
3170                  dstMask = 128;
3171                  d++;
3172                  *d = 0;
3173               }
3174               else {
3175                  dstMask = dstMask >> 1;
3176               }
3177            }
3178         }
3179         else {
3180            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
3181            GLubyte dstMask = 128;
3182            GLubyte *s = src;
3183            GLubyte *d = dst;
3184            *d = 0;
3185            for (i = 0; i < width; i++) {
3186               if (*s & srcMask) {
3187                  *d |= dstMask;
3188               }
3189               if (srcMask == 1) {
3190                  srcMask = 128;
3191                  s++;
3192               }
3193               else {
3194                  srcMask = srcMask >> 1;
3195               }
3196               if (dstMask == 1) {
3197                  dstMask = 128;
3198                  d++;
3199                  *d = 0;
3200               }
3201               else {
3202                  dstMask = dstMask >> 1;
3203               }
3204            }
3205         }
3206      }
3207      dst += width_in_bytes;
3208   }
3209
3210   return buffer;
3211}
3212
3213
3214/*
3215 * Pack bitmap data.
3216 */
3217void
3218_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
3219                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
3220{
3221   GLint row, width_in_bytes;
3222   const GLubyte *src;
3223
3224   if (!source)
3225      return;
3226
3227   width_in_bytes = CEILING( width, 8 );
3228   src = source;
3229   for (row = 0; row < height; row++) {
3230      GLubyte *dst = _mesa_image_address( packing, dest, width, height,
3231                                          GL_COLOR_INDEX, GL_BITMAP,
3232                                          0, row, 0 );
3233      if (!dst)
3234         return;
3235
3236      if (packing->SkipPixels == 0) {
3237         MEMCPY( dst, src, width_in_bytes );
3238         if (packing->LsbFirst) {
3239            flip_bytes( dst, width_in_bytes );
3240         }
3241      }
3242      else {
3243         /* handling SkipPixels is a bit tricky (no pun intended!) */
3244         GLint i;
3245         if (packing->LsbFirst) {
3246            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
3247            GLubyte dstMask = 128;
3248            const GLubyte *s = src;
3249            GLubyte *d = dst;
3250            *d = 0;
3251            for (i = 0; i < width; i++) {
3252               if (*s & srcMask) {
3253                  *d |= dstMask;
3254               }
3255               if (srcMask == 128) {
3256                  srcMask = 1;
3257                  s++;
3258               }
3259               else {
3260                  srcMask = srcMask << 1;
3261               }
3262               if (dstMask == 1) {
3263                  dstMask = 128;
3264                  d++;
3265                  *d = 0;
3266               }
3267               else {
3268                  dstMask = dstMask >> 1;
3269               }
3270            }
3271         }
3272         else {
3273            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
3274            GLubyte dstMask = 128;
3275            const GLubyte *s = src;
3276            GLubyte *d = dst;
3277            *d = 0;
3278            for (i = 0; i < width; i++) {
3279               if (*s & srcMask) {
3280                  *d |= dstMask;
3281               }
3282               if (srcMask == 1) {
3283                  srcMask = 128;
3284                  s++;
3285               }
3286               else {
3287                  srcMask = srcMask >> 1;
3288               }
3289               if (dstMask == 1) {
3290                  dstMask = 128;
3291                  d++;
3292                  *d = 0;
3293               }
3294               else {
3295                  dstMask = dstMask >> 1;
3296               }
3297            }
3298         }
3299      }
3300      src += width_in_bytes;
3301   }
3302}
3303