image.c revision 116970154dc3bb148178e1a9fe38554fbbd133c8
1/* $Id: image.c,v 1.38 2000/08/30 18:21:37 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 format, GLenum type, GLvoid *destination,
613                      const struct gl_pixelstore_attrib *packing,
614                      GLuint transferOps)
615{
616   ASSERT(ctx->ImageTransferState != UPDATE_IMAGE_TRANSFER_STATE);
617
618   /* Test for optimized case first */
619   if (transferOps == 0 && format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
620      /* common simple case */
621      MEMCPY( destination, srcRgba, n * 4 * sizeof(GLubyte) );
622   }
623   else if (transferOps == 0 && format == GL_RGB && type == GL_UNSIGNED_BYTE) {
624      /* common simple case */
625      GLint i;
626      GLubyte *dest = (GLubyte *) destination;
627      for (i = 0; i < n; i++) {
628         dest[0] = srcRgba[i][RCOMP];
629         dest[1] = srcRgba[i][GCOMP];
630         dest[2] = srcRgba[i][BCOMP];
631         dest += 3;
632      }
633   }
634   else {
635      /* general solution */
636      GLfloat rgba[MAX_WIDTH][4];
637      const GLfloat rscale = 1.0F / 255.0F;
638      const GLfloat gscale = 1.0F / 255.0F;
639      const GLfloat bscale = 1.0F / 255.0F;
640      const GLfloat ascale = 1.0F / 255.0F;
641      GLuint i;
642      assert(n <= MAX_WIDTH);
643      /* convert color components to floating point */
644      for (i=0;i<n;i++) {
645         rgba[i][RCOMP] = srcRgba[i][RCOMP] * rscale;
646         rgba[i][GCOMP] = srcRgba[i][GCOMP] * gscale;
647         rgba[i][BCOMP] = srcRgba[i][BCOMP] * bscale;
648         rgba[i][ACOMP] = srcRgba[i][ACOMP] * ascale;
649      }
650      _mesa_pack_float_rgba_span(ctx, n, (const GLfloat (*)[4]) rgba,
651                                 format, type, destination,
652                                 packing, transferOps);
653   }
654}
655
656
657void
658_mesa_pack_float_rgba_span( GLcontext *ctx,
659                            GLuint n, CONST GLfloat rgbaIn[][4],
660                            GLenum format, GLenum type, GLvoid *destination,
661                            const struct gl_pixelstore_attrib *packing,
662                            GLuint transferOps )
663{
664   const GLint comps = _mesa_components_in_format(format);
665   GLfloat luminance[MAX_WIDTH];
666   GLfloat (*rgba)[4];
667   GLuint i;
668
669   if (transferOps) {
670      /* make copy of incoming data */
671      GLfloat rgbaCopy[MAX_WIDTH][4];
672      for (i = 0; i < n; i++) {
673         rgbaCopy[i][0] = rgbaIn[i][0];
674         rgbaCopy[i][1] = rgbaIn[i][1];
675         rgbaCopy[i][2] = rgbaIn[i][2];
676         rgbaCopy[i][3] = rgbaIn[i][3];
677      }
678
679      rgba = (GLfloat (*)[4]) rgbaCopy;
680
681      /* scale & bias */
682      if (transferOps & IMAGE_SCALE_BIAS_BIT) {
683         _mesa_scale_and_bias_rgba( ctx, n, rgba );
684      }
685      /* color map lookup */
686      if (transferOps & IMAGE_MAP_COLOR_BIT) {
687         _mesa_map_rgba( ctx, n, rgba );
688      }
689      /* GL_COLOR_TABLE lookup */
690      if (transferOps & IMAGE_COLOR_TABLE_BIT) {
691         _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
692      }
693      /* convolution */
694      if (transferOps & IMAGE_CONVOLUTION_BIT) {
695         /* this has to be done in the calling code */
696      }
697      /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
698      if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
699         _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
700      }
701      /* color matrix transform */
702      if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
703         _mesa_transform_rgba(ctx, n, rgba);
704      }
705      /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
706      if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
707         _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
708      }
709      /* update histogram count */
710      if (transferOps & IMAGE_HISTOGRAM_BIT) {
711         _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
712      }
713      /* min/max here */
714      if (transferOps & IMAGE_MIN_MAX_BIT) {
715         _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
716         if (ctx->MinMax.Sink)
717            return;
718      }
719   }
720   else {
721      /* use incoming data, not a copy */
722      rgba = (GLfloat (*)[4]) rgbaIn;
723   }
724
725   /* XXX clamp rgba to [0,1]? */
726
727
728   if (format == GL_LUMINANCE || format == GL_LUMINANCE_ALPHA) {
729      for (i = 0; i < n; i++) {
730         GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
731         luminance[i] = CLAMP(sum, 0.0F, 1.0F);
732      }
733   }
734
735   /*
736    * Pack/store the pixels.  Ugh!  Lots of cases!!!
737    */
738   switch (type) {
739      case GL_UNSIGNED_BYTE:
740         {
741            GLubyte *dst = (GLubyte *) destination;
742            switch (format) {
743               case GL_RED:
744                  for (i=0;i<n;i++)
745                     dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
746                  break;
747               case GL_GREEN:
748                  for (i=0;i<n;i++)
749                     dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
750                  break;
751               case GL_BLUE:
752                  for (i=0;i<n;i++)
753                     dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
754                  break;
755               case GL_ALPHA:
756                  for (i=0;i<n;i++)
757                     dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
758                  break;
759               case GL_LUMINANCE:
760                  for (i=0;i<n;i++)
761                     dst[i] = FLOAT_TO_UBYTE(luminance[i]);
762                  break;
763               case GL_LUMINANCE_ALPHA:
764                  for (i=0;i<n;i++) {
765                     dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
766                     dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
767                  }
768                  break;
769               case GL_RGB:
770                  for (i=0;i<n;i++) {
771                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
772                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
773                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
774                  }
775                  break;
776               case GL_RGBA:
777                  for (i=0;i<n;i++) {
778                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
779                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
780                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
781                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
782                  }
783                  break;
784               case GL_BGR:
785                  for (i=0;i<n;i++) {
786                     dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
787                     dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
788                     dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
789                  }
790                  break;
791               case GL_BGRA:
792                  for (i=0;i<n;i++) {
793                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
794                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
795                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
796                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
797                  }
798                  break;
799               case GL_ABGR_EXT:
800                  for (i=0;i<n;i++) {
801                     dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
802                     dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
803                     dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
804                     dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
805                  }
806                  break;
807               default:
808                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
809            }
810         }
811         break;
812      case GL_BYTE:
813         {
814            GLbyte *dst = (GLbyte *) destination;
815            switch (format) {
816               case GL_RED:
817                  for (i=0;i<n;i++)
818                     dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
819                  break;
820               case GL_GREEN:
821                  for (i=0;i<n;i++)
822                     dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
823                  break;
824               case GL_BLUE:
825                  for (i=0;i<n;i++)
826                     dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
827                  break;
828               case GL_ALPHA:
829                  for (i=0;i<n;i++)
830                     dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
831                  break;
832               case GL_LUMINANCE:
833                  for (i=0;i<n;i++)
834                     dst[i] = FLOAT_TO_BYTE(luminance[i]);
835                  break;
836               case GL_LUMINANCE_ALPHA:
837                  for (i=0;i<n;i++) {
838                     dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
839                     dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
840                  }
841                  break;
842               case GL_RGB:
843                  for (i=0;i<n;i++) {
844                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
845                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
846                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
847                  }
848                  break;
849               case GL_RGBA:
850                  for (i=0;i<n;i++) {
851                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
852                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
853                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
854                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
855                  }
856                  break;
857               case GL_BGR:
858                  for (i=0;i<n;i++) {
859                     dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
860                     dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
861                     dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
862                  }
863                  break;
864               case GL_BGRA:
865                  for (i=0;i<n;i++) {
866                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
867                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
868                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
869                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
870                  }
871               case GL_ABGR_EXT:
872                  for (i=0;i<n;i++) {
873                     dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
874                     dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
875                     dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
876                     dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
877                  }
878                  break;
879               default:
880                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
881            }
882         }
883         break;
884      case GL_UNSIGNED_SHORT:
885         {
886            GLushort *dst = (GLushort *) destination;
887            switch (format) {
888               case GL_RED:
889                  for (i=0;i<n;i++)
890                     dst[i] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
891                  break;
892               case GL_GREEN:
893                  for (i=0;i<n;i++)
894                     dst[i] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
895                  break;
896               case GL_BLUE:
897                  for (i=0;i<n;i++)
898                     dst[i] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
899                  break;
900               case GL_ALPHA:
901                  for (i=0;i<n;i++)
902                     dst[i] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
903                  break;
904               case GL_LUMINANCE:
905                  for (i=0;i<n;i++)
906                     dst[i] = FLOAT_TO_USHORT(luminance[i]);
907                  break;
908               case GL_LUMINANCE_ALPHA:
909                  for (i=0;i<n;i++) {
910                     dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
911                     dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
912                  }
913                  break;
914               case GL_RGB:
915                  for (i=0;i<n;i++) {
916                     dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
917                     dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
918                     dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
919                  }
920                  break;
921               case GL_RGBA:
922                  for (i=0;i<n;i++) {
923                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
924                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
925                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
926                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
927                  }
928                  break;
929               case GL_BGR:
930                  for (i=0;i<n;i++) {
931                     dst[i*3+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
932                     dst[i*3+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
933                     dst[i*3+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
934                  }
935                  break;
936               case GL_BGRA:
937                  for (i=0;i<n;i++) {
938                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
939                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
940                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
941                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
942                  }
943                  break;
944               case GL_ABGR_EXT:
945                  for (i=0;i<n;i++) {
946                     dst[i*4+0] = FLOAT_TO_USHORT(rgba[i][ACOMP]);
947                     dst[i*4+1] = FLOAT_TO_USHORT(rgba[i][BCOMP]);
948                     dst[i*4+2] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
949                     dst[i*4+3] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
950                  }
951                  break;
952               default:
953                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
954            }
955            if (packing->SwapBytes) {
956               _mesa_swap2( (GLushort *) dst, n * comps);
957            }
958         }
959         break;
960      case GL_SHORT:
961         {
962            GLshort *dst = (GLshort *) destination;
963            switch (format) {
964               case GL_RED:
965                  for (i=0;i<n;i++)
966                     dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
967                  break;
968               case GL_GREEN:
969                  for (i=0;i<n;i++)
970                     dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
971                  break;
972               case GL_BLUE:
973                  for (i=0;i<n;i++)
974                     dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
975                  break;
976               case GL_ALPHA:
977                  for (i=0;i<n;i++)
978                     dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
979                  break;
980               case GL_LUMINANCE:
981                  for (i=0;i<n;i++)
982                     dst[i] = FLOAT_TO_SHORT(luminance[i]);
983                  break;
984               case GL_LUMINANCE_ALPHA:
985                  for (i=0;i<n;i++) {
986                     dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
987                     dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
988                  }
989                  break;
990               case GL_RGB:
991                  for (i=0;i<n;i++) {
992                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
993                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
994                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
995                  }
996                  break;
997               case GL_RGBA:
998                  for (i=0;i<n;i++) {
999                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1000                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1001                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1002                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1003                  }
1004                  break;
1005               case GL_BGR:
1006                  for (i=0;i<n;i++) {
1007                     dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1008                     dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1009                     dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1010                  }
1011                  break;
1012               case GL_BGRA:
1013                  for (i=0;i<n;i++) {
1014                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1015                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1016                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1017                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1018                  }
1019               case GL_ABGR_EXT:
1020                  for (i=0;i<n;i++) {
1021                     dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
1022                     dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
1023                     dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
1024                     dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
1025                  }
1026                  break;
1027               default:
1028                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1029            }
1030            if (packing->SwapBytes) {
1031               _mesa_swap2( (GLushort *) dst, n * comps );
1032            }
1033         }
1034         break;
1035      case GL_UNSIGNED_INT:
1036         {
1037            GLuint *dst = (GLuint *) destination;
1038            switch (format) {
1039               case GL_RED:
1040                  for (i=0;i<n;i++)
1041                     dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1042                  break;
1043               case GL_GREEN:
1044                  for (i=0;i<n;i++)
1045                     dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1046                  break;
1047               case GL_BLUE:
1048                  for (i=0;i<n;i++)
1049                     dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1050                  break;
1051               case GL_ALPHA:
1052                  for (i=0;i<n;i++)
1053                     dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1054                  break;
1055               case GL_LUMINANCE:
1056                  for (i=0;i<n;i++)
1057                     dst[i] = FLOAT_TO_UINT(luminance[i]);
1058                  break;
1059               case GL_LUMINANCE_ALPHA:
1060                  for (i=0;i<n;i++) {
1061                     dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
1062                     dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1063                  }
1064                  break;
1065               case GL_RGB:
1066                  for (i=0;i<n;i++) {
1067                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1068                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1069                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1070                  }
1071                  break;
1072               case GL_RGBA:
1073                  for (i=0;i<n;i++) {
1074                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1075                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1076                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1077                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1078                  }
1079                  break;
1080               case GL_BGR:
1081                  for (i=0;i<n;i++) {
1082                     dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1083                     dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1084                     dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1085                  }
1086                  break;
1087               case GL_BGRA:
1088                  for (i=0;i<n;i++) {
1089                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1090                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1091                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1092                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1093                  }
1094                  break;
1095               case GL_ABGR_EXT:
1096                  for (i=0;i<n;i++) {
1097                     dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
1098                     dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
1099                     dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
1100                     dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
1101                  }
1102                  break;
1103               default:
1104                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1105            }
1106            if (packing->SwapBytes) {
1107               _mesa_swap4( (GLuint *) dst, n * comps );
1108            }
1109         }
1110         break;
1111      case GL_INT:
1112         {
1113            GLint *dst = (GLint *) destination;
1114            switch (format) {
1115               case GL_RED:
1116                  for (i=0;i<n;i++)
1117                     dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
1118                  break;
1119               case GL_GREEN:
1120                  for (i=0;i<n;i++)
1121                     dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
1122                  break;
1123               case GL_BLUE:
1124                  for (i=0;i<n;i++)
1125                     dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
1126                  break;
1127               case GL_ALPHA:
1128                  for (i=0;i<n;i++)
1129                     dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
1130                  break;
1131               case GL_LUMINANCE:
1132                  for (i=0;i<n;i++)
1133                     dst[i] = FLOAT_TO_INT(luminance[i]);
1134                  break;
1135               case GL_LUMINANCE_ALPHA:
1136                  for (i=0;i<n;i++) {
1137                     dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
1138                     dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
1139                  }
1140                  break;
1141               case GL_RGB:
1142                  for (i=0;i<n;i++) {
1143                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1144                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1145                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1146                  }
1147                  break;
1148               case GL_RGBA:
1149                  for (i=0;i<n;i++) {
1150                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
1151                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1152                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
1153                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1154                  }
1155                  break;
1156               case GL_BGR:
1157                  for (i=0;i<n;i++) {
1158                     dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1159                     dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1160                     dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1161                  }
1162                  break;
1163               case GL_BGRA:
1164                  for (i=0;i<n;i++) {
1165                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
1166                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
1167                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
1168                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
1169                  }
1170                  break;
1171               case GL_ABGR_EXT:
1172                  for (i=0;i<n;i++) {
1173                     dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
1174                     dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
1175                     dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
1176                     dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
1177                  }
1178                  break;
1179               default:
1180                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1181            }
1182            if (packing->SwapBytes) {
1183               _mesa_swap4( (GLuint *) dst, n * comps );
1184            }
1185         }
1186         break;
1187      case GL_FLOAT:
1188         {
1189            GLfloat *dst = (GLfloat *) destination;
1190            switch (format) {
1191               case GL_RED:
1192                  for (i=0;i<n;i++)
1193                     dst[i] = rgba[i][RCOMP];
1194                  break;
1195               case GL_GREEN:
1196                  for (i=0;i<n;i++)
1197                     dst[i] = rgba[i][GCOMP];
1198                  break;
1199               case GL_BLUE:
1200                  for (i=0;i<n;i++)
1201                     dst[i] = rgba[i][BCOMP];
1202                  break;
1203               case GL_ALPHA:
1204                  for (i=0;i<n;i++)
1205                     dst[i] = rgba[i][ACOMP];
1206                  break;
1207               case GL_LUMINANCE:
1208                  for (i=0;i<n;i++)
1209                     dst[i] = luminance[i];
1210                  break;
1211               case GL_LUMINANCE_ALPHA:
1212                  for (i=0;i<n;i++) {
1213                     dst[i*2+0] = luminance[i];
1214                     dst[i*2+1] = rgba[i][ACOMP];
1215                  }
1216                  break;
1217               case GL_RGB:
1218                  for (i=0;i<n;i++) {
1219                     dst[i*3+0] = rgba[i][RCOMP];
1220                     dst[i*3+1] = rgba[i][GCOMP];
1221                     dst[i*3+2] = rgba[i][BCOMP];
1222                  }
1223                  break;
1224               case GL_RGBA:
1225                  for (i=0;i<n;i++) {
1226                     dst[i*4+0] = rgba[i][RCOMP];
1227                     dst[i*4+1] = rgba[i][GCOMP];
1228                     dst[i*4+2] = rgba[i][BCOMP];
1229                     dst[i*4+3] = rgba[i][ACOMP];
1230                  }
1231                  break;
1232               case GL_BGR:
1233                  for (i=0;i<n;i++) {
1234                     dst[i*3+0] = rgba[i][BCOMP];
1235                     dst[i*3+1] = rgba[i][GCOMP];
1236                     dst[i*3+2] = rgba[i][RCOMP];
1237                  }
1238                  break;
1239               case GL_BGRA:
1240                  for (i=0;i<n;i++) {
1241                     dst[i*4+0] = rgba[i][BCOMP];
1242                     dst[i*4+1] = rgba[i][GCOMP];
1243                     dst[i*4+2] = rgba[i][RCOMP];
1244                     dst[i*4+3] = rgba[i][ACOMP];
1245                  }
1246                  break;
1247               case GL_ABGR_EXT:
1248                  for (i=0;i<n;i++) {
1249                     dst[i*4+0] = rgba[i][ACOMP];
1250                     dst[i*4+1] = rgba[i][BCOMP];
1251                     dst[i*4+2] = rgba[i][GCOMP];
1252                     dst[i*4+3] = rgba[i][RCOMP];
1253                  }
1254                  break;
1255               default:
1256                  gl_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
1257            }
1258            if (packing->SwapBytes) {
1259               _mesa_swap4( (GLuint *) dst, n * comps );
1260            }
1261         }
1262         break;
1263      case GL_UNSIGNED_BYTE_3_3_2:
1264         if (format == GL_RGB) {
1265            GLubyte *dst = (GLubyte *) destination;
1266            for (i=0;i<n;i++) {
1267               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F)) << 5)
1268                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 2)
1269                      | (((GLint) (rgba[i][BCOMP] * 3.0F))     );
1270            }
1271         }
1272         break;
1273      case GL_UNSIGNED_BYTE_2_3_3_REV:
1274         if (format == GL_RGB) {
1275            GLubyte *dst = (GLubyte *) destination;
1276            for (i=0;i<n;i++) {
1277               dst[i] = (((GLint) (rgba[i][RCOMP] * 7.0F))     )
1278                      | (((GLint) (rgba[i][GCOMP] * 7.0F)) << 3)
1279                      | (((GLint) (rgba[i][BCOMP] * 3.0F)) << 5);
1280            }
1281         }
1282         break;
1283      case GL_UNSIGNED_SHORT_5_6_5:
1284         if (format == GL_RGB) {
1285            GLushort *dst = (GLushort *) destination;
1286            for (i=0;i<n;i++) {
1287               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1288                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1289                      | (((GLint) (rgba[i][BCOMP] * 31.0F))      );
1290            }
1291         }
1292         break;
1293      case GL_UNSIGNED_SHORT_5_6_5_REV:
1294         if (format == GL_RGB) {
1295            GLushort *dst = (GLushort *) destination;
1296            for (i=0;i<n;i++) {
1297               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1298                      | (((GLint) (rgba[i][GCOMP] * 63.0F)) <<  5)
1299                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 11);
1300            }
1301         }
1302         break;
1303      case GL_UNSIGNED_SHORT_4_4_4_4:
1304         if (format == GL_RGB) {
1305            GLushort *dst = (GLushort *) destination;
1306            for (i=0;i<n;i++) {
1307               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F)) << 12)
1308                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  8)
1309                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  4)
1310                      | (((GLint) (rgba[i][ACOMP] * 15.0F))      );
1311            }
1312         }
1313         break;
1314      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1315         if (format == GL_RGB) {
1316            GLushort *dst = (GLushort *) destination;
1317            for (i=0;i<n;i++) {
1318               dst[i] = (((GLint) (rgba[i][RCOMP] * 15.0F))      )
1319                      | (((GLint) (rgba[i][GCOMP] * 15.0F)) <<  4)
1320                      | (((GLint) (rgba[i][BCOMP] * 15.0F)) <<  8)
1321                      | (((GLint) (rgba[i][ACOMP] * 15.0F)) << 12);
1322            }
1323         }
1324         break;
1325      case GL_UNSIGNED_SHORT_5_5_5_1:
1326         if (format == GL_RGB) {
1327            GLushort *dst = (GLushort *) destination;
1328            for (i=0;i<n;i++) {
1329               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F)) << 11)
1330                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  6)
1331                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) <<  1)
1332                      | (((GLint) (rgba[i][ACOMP] *  1.0F))      );
1333            }
1334         }
1335         break;
1336      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1337         if (format == GL_RGB) {
1338            GLushort *dst = (GLushort *) destination;
1339            for (i=0;i<n;i++) {
1340               dst[i] = (((GLint) (rgba[i][RCOMP] * 31.0F))      )
1341                      | (((GLint) (rgba[i][GCOMP] * 31.0F)) <<  5)
1342                      | (((GLint) (rgba[i][BCOMP] * 31.0F)) << 10)
1343                      | (((GLint) (rgba[i][ACOMP] *  1.0F)) << 15);
1344            }
1345         }
1346         break;
1347      case GL_UNSIGNED_INT_8_8_8_8:
1348         if (format == GL_RGBA) {
1349            GLuint *dst = (GLuint *) destination;
1350            for (i=0;i<n;i++) {
1351               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24)
1352                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1353                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
1354                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1355            }
1356         }
1357         else if (format == GL_BGRA) {
1358            GLuint *dst = (GLuint *) destination;
1359            for (i=0;i<n;i++) {
1360               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 24)
1361                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1362                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) <<  8)
1363                      | (((GLuint) (rgba[i][ACOMP] * 255.0F))      );
1364            }
1365         }
1366         else if (format == GL_ABGR_EXT) {
1367            GLuint *dst = (GLuint *) destination;
1368            for (i=0;i<n;i++) {
1369               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24)
1370                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
1371                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1372                      | (((GLuint) (rgba[i][RCOMP] * 255.0F))      );
1373            }
1374         }
1375         break;
1376      case GL_UNSIGNED_INT_8_8_8_8_REV:
1377         if (format == GL_RGBA) {
1378            GLuint *dst = (GLuint *) destination;
1379            for (i=0;i<n;i++) {
1380               dst[i] = (((GLuint) (rgba[i][RCOMP] * 255.0F))      )
1381                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1382                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) << 16)
1383                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1384            }
1385         }
1386         else if (format == GL_BGRA) {
1387            GLuint *dst = (GLuint *) destination;
1388            for (i=0;i<n;i++) {
1389               dst[i] = (((GLuint) (rgba[i][BCOMP] * 255.0F))      )
1390                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) <<  8)
1391                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 16)
1392                      | (((GLuint) (rgba[i][ACOMP] * 255.0F)) << 24);
1393            }
1394         }
1395         else if (format == GL_ABGR_EXT) {
1396            GLuint *dst = (GLuint *) destination;
1397            for (i=0;i<n;i++) {
1398               dst[i] = (((GLuint) (rgba[i][ACOMP] * 255.0F))      )
1399                      | (((GLuint) (rgba[i][BCOMP] * 255.0F)) <<  8)
1400                      | (((GLuint) (rgba[i][GCOMP] * 255.0F)) << 16)
1401                      | (((GLuint) (rgba[i][RCOMP] * 255.0F)) << 24);
1402            }
1403         }
1404         break;
1405      case GL_UNSIGNED_INT_10_10_10_2:
1406         if (format == GL_RGBA) {
1407            GLuint *dst = (GLuint *) destination;
1408            for (i=0;i<n;i++) {
1409               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 22)
1410                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
1411                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) <<  2)
1412                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
1413            }
1414         }
1415         else if (format == GL_BGRA) {
1416            GLuint *dst = (GLuint *) destination;
1417            for (i=0;i<n;i++) {
1418               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 22)
1419                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 12)
1420                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) <<  2)
1421                      | (((GLuint) (rgba[i][ACOMP] *    3.0F))      );
1422            }
1423         }
1424         else if (format == GL_ABGR_EXT) {
1425            GLuint *dst = (GLuint *) destination;
1426            for (i=0;i<n;i++) {
1427               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F)) << 22)
1428                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 12)
1429                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) <<  2)
1430                      | (((GLuint) (rgba[i][RCOMP] *    3.0F))      );
1431            }
1432         }
1433         break;
1434      case GL_UNSIGNED_INT_2_10_10_10_REV:
1435         if (format == GL_RGBA) {
1436            GLuint *dst = (GLuint *) destination;
1437            for (i=0;i<n;i++) {
1438               dst[i] = (((GLuint) (rgba[i][RCOMP] * 1023.0F))      )
1439                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
1440                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 20)
1441                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
1442            }
1443         }
1444         else if (format == GL_BGRA) {
1445            GLuint *dst = (GLuint *) destination;
1446            for (i=0;i<n;i++) {
1447               dst[i] = (((GLuint) (rgba[i][BCOMP] * 1023.0F))      )
1448                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 10)
1449                      | (((GLuint) (rgba[i][RCOMP] * 1023.0F)) << 20)
1450                      | (((GLuint) (rgba[i][ACOMP] *    3.0F)) << 30);
1451            }
1452         }
1453         else if (format == GL_ABGR_EXT) {
1454            GLuint *dst = (GLuint *) destination;
1455            for (i=0;i<n;i++) {
1456               dst[i] = (((GLuint) (rgba[i][ACOMP] * 1023.0F))      )
1457                      | (((GLuint) (rgba[i][BCOMP] * 1023.0F)) << 10)
1458                      | (((GLuint) (rgba[i][GCOMP] * 1023.0F)) << 20)
1459                      | (((GLuint) (rgba[i][RCOMP] *    3.0F)) << 30);
1460            }
1461         }
1462         break;
1463      default:
1464         gl_problem(ctx, "bad type in _mesa_pack_float_rgba_span");
1465   }
1466}
1467
1468
1469
1470#define SWAP2BYTE(VALUE)			\
1471   {						\
1472      GLubyte *bytes = (GLubyte *) &(VALUE);	\
1473      GLubyte tmp = bytes[0];			\
1474      bytes[0] = bytes[1];			\
1475      bytes[1] = tmp;				\
1476   }
1477
1478#define SWAP4BYTE(VALUE)			\
1479   {						\
1480      GLubyte *bytes = (GLubyte *) &(VALUE);	\
1481      GLubyte tmp = bytes[0];			\
1482      bytes[0] = bytes[3];			\
1483      bytes[3] = tmp;				\
1484      tmp = bytes[1];				\
1485      bytes[1] = bytes[2];			\
1486      bytes[2] = tmp;				\
1487   }
1488
1489
1490static void
1491extract_uint_indexes(GLuint n, GLuint indexes[],
1492                     GLenum srcFormat, GLenum srcType, const GLvoid *src,
1493                     const struct gl_pixelstore_attrib *unpack )
1494{
1495   assert(srcFormat == GL_COLOR_INDEX);
1496
1497   ASSERT(srcType == GL_BITMAP ||
1498          srcType == GL_UNSIGNED_BYTE ||
1499          srcType == GL_BYTE ||
1500          srcType == GL_UNSIGNED_SHORT ||
1501          srcType == GL_SHORT ||
1502          srcType == GL_UNSIGNED_INT ||
1503          srcType == GL_INT ||
1504          srcType == GL_FLOAT);
1505
1506   switch (srcType) {
1507      case GL_BITMAP:
1508         {
1509            GLubyte *ubsrc = (GLubyte *) src;
1510            if (unpack->LsbFirst) {
1511               GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
1512               GLuint i;
1513               for (i = 0; i < n; i++) {
1514                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
1515                  if (mask == 128) {
1516                     mask = 1;
1517                     ubsrc++;
1518                  }
1519                  else {
1520                     mask = mask << 1;
1521                  }
1522               }
1523            }
1524            else {
1525               GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
1526               GLuint i;
1527               for (i = 0; i < n; i++) {
1528                  indexes[i] = (*ubsrc & mask) ? 1 : 0;
1529                  if (mask == 1) {
1530                     mask = 128;
1531                     ubsrc++;
1532                  }
1533                  else {
1534                     mask = mask >> 1;
1535                  }
1536               }
1537            }
1538         }
1539         break;
1540      case GL_UNSIGNED_BYTE:
1541         {
1542            GLuint i;
1543            const GLubyte *s = (const GLubyte *) src;
1544            for (i = 0; i < n; i++)
1545               indexes[i] = s[i];
1546         }
1547         break;
1548      case GL_BYTE:
1549         {
1550            GLuint i;
1551            const GLbyte *s = (const GLbyte *) src;
1552            for (i = 0; i < n; i++)
1553               indexes[i] = s[i];
1554         }
1555         break;
1556      case GL_UNSIGNED_SHORT:
1557         {
1558            GLuint i;
1559            const GLushort *s = (const GLushort *) src;
1560            if (unpack->SwapBytes) {
1561               for (i = 0; i < n; i++) {
1562                  GLushort value = s[i];
1563                  SWAP2BYTE(value);
1564                  indexes[i] = value;
1565               }
1566            }
1567            else {
1568               for (i = 0; i < n; i++)
1569                  indexes[i] = s[i];
1570            }
1571         }
1572         break;
1573      case GL_SHORT:
1574         {
1575            GLuint i;
1576            const GLshort *s = (const GLshort *) src;
1577            if (unpack->SwapBytes) {
1578               for (i = 0; i < n; i++) {
1579                  GLshort value = s[i];
1580                  SWAP2BYTE(value);
1581                  indexes[i] = value;
1582               }
1583            }
1584            else {
1585               for (i = 0; i < n; i++)
1586                  indexes[i] = s[i];
1587            }
1588         }
1589         break;
1590      case GL_UNSIGNED_INT:
1591         {
1592            GLuint i;
1593            const GLuint *s = (const GLuint *) src;
1594            if (unpack->SwapBytes) {
1595               for (i = 0; i < n; i++) {
1596                  GLuint value = s[i];
1597                  SWAP4BYTE(value);
1598                  indexes[i] = value;
1599               }
1600            }
1601            else {
1602               for (i = 0; i < n; i++)
1603                  indexes[i] = s[i];
1604            }
1605         }
1606         break;
1607      case GL_INT:
1608         {
1609            GLuint i;
1610            const GLint *s = (const GLint *) src;
1611            if (unpack->SwapBytes) {
1612               for (i = 0; i < n; i++) {
1613                  GLint value = s[i];
1614                  SWAP4BYTE(value);
1615                  indexes[i] = value;
1616               }
1617            }
1618            else {
1619               for (i = 0; i < n; i++)
1620                  indexes[i] = s[i];
1621            }
1622         }
1623         break;
1624      case GL_FLOAT:
1625         {
1626            GLuint i;
1627            const GLfloat *s = (const GLfloat *) src;
1628            if (unpack->SwapBytes) {
1629               for (i = 0; i < n; i++) {
1630                  GLfloat value = s[i];
1631                  SWAP4BYTE(value);
1632                  indexes[i] = (GLuint) value;
1633               }
1634            }
1635            else {
1636               for (i = 0; i < n; i++)
1637                  indexes[i] = (GLuint) s[i];
1638            }
1639         }
1640         break;
1641      default:
1642         gl_problem(NULL, "bad srcType in extract_uint_indexes");
1643         return;
1644   }
1645}
1646
1647
1648
1649/*
1650 * This function extracts floating point RGBA values from arbitrary
1651 * image data.  srcFormat and srcType are the format and type parameters
1652 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1653 *
1654 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1655 * implements the "Conversion to floating point", "Conversion to RGB",
1656 * and "Final Expansion to RGBA" operations.
1657 *
1658 * Args:  n - number of pixels
1659 *        rgba - output colors
1660 *        srcFormat - format of incoming data
1661 *        srcType - datatype of incoming data
1662 *        src - source data pointer
1663 *        swapBytes - perform byteswapping of incoming data?
1664 */
1665static void
1666extract_float_rgba(GLuint n, GLfloat rgba[][4],
1667                   GLenum srcFormat, GLenum srcType, const GLvoid *src,
1668                   GLboolean swapBytes)
1669{
1670   GLint redIndex, greenIndex, blueIndex, alphaIndex;
1671   GLint stride;
1672   GLint rComp, bComp, gComp, aComp;
1673
1674   ASSERT(srcFormat == GL_RED ||
1675          srcFormat == GL_GREEN ||
1676          srcFormat == GL_BLUE ||
1677          srcFormat == GL_ALPHA ||
1678          srcFormat == GL_LUMINANCE ||
1679          srcFormat == GL_LUMINANCE_ALPHA ||
1680          srcFormat == GL_INTENSITY ||
1681          srcFormat == GL_RGB ||
1682          srcFormat == GL_BGR ||
1683          srcFormat == GL_RGBA ||
1684          srcFormat == GL_BGRA ||
1685          srcFormat == GL_ABGR_EXT);
1686
1687   ASSERT(srcType == GL_UNSIGNED_BYTE ||
1688          srcType == GL_BYTE ||
1689          srcType == GL_UNSIGNED_SHORT ||
1690          srcType == GL_SHORT ||
1691          srcType == GL_UNSIGNED_INT ||
1692          srcType == GL_INT ||
1693          srcType == GL_FLOAT ||
1694          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
1695          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
1696          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
1697          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
1698          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
1699          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
1700          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
1701          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
1702          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1703          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
1704          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
1705          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
1706
1707   rComp = gComp = bComp = aComp = -1;
1708
1709   switch (srcFormat) {
1710      case GL_RED:
1711         redIndex = 0;
1712         greenIndex = blueIndex = alphaIndex = -1;
1713         stride = 1;
1714         break;
1715      case GL_GREEN:
1716         greenIndex = 0;
1717         redIndex = blueIndex = alphaIndex = -1;
1718         stride = 1;
1719         break;
1720      case GL_BLUE:
1721         blueIndex = 0;
1722         redIndex = greenIndex = alphaIndex = -1;
1723         stride = 1;
1724         break;
1725      case GL_ALPHA:
1726         redIndex = greenIndex = blueIndex = -1;
1727         alphaIndex = 0;
1728         stride = 1;
1729         break;
1730      case GL_LUMINANCE:
1731         redIndex = greenIndex = blueIndex = 0;
1732         alphaIndex = -1;
1733         stride = 1;
1734         break;
1735      case GL_LUMINANCE_ALPHA:
1736         redIndex = greenIndex = blueIndex = 0;
1737         alphaIndex = 1;
1738         stride = 2;
1739         break;
1740      case GL_INTENSITY:
1741         redIndex = 0;
1742         greenIndex = blueIndex = alphaIndex = -1;
1743         stride = 1;
1744         break;
1745      case GL_RGB:
1746         redIndex = 0;
1747         greenIndex = 1;
1748         blueIndex = 2;
1749         alphaIndex = -1;
1750         stride = 3;
1751         break;
1752      case GL_BGR:
1753         redIndex = 2;
1754         greenIndex = 1;
1755         blueIndex = 0;
1756         alphaIndex = -1;
1757         stride = 3;
1758         break;
1759      case GL_RGBA:
1760         redIndex = 0;
1761         greenIndex = 1;
1762         blueIndex = 2;
1763         alphaIndex = 3;
1764         rComp = 0;
1765         gComp = 1;
1766         bComp = 2;
1767         aComp = 3;
1768         stride = 4;
1769         break;
1770      case GL_BGRA:
1771         redIndex = 2;
1772         greenIndex = 1;
1773         blueIndex = 0;
1774         alphaIndex = 3;
1775         rComp = 2;
1776         gComp = 1;
1777         bComp = 0;
1778         aComp = 3;
1779         stride = 4;
1780         break;
1781      case GL_ABGR_EXT:
1782         redIndex = 3;
1783         greenIndex = 2;
1784         blueIndex = 1;
1785         alphaIndex = 0;
1786         rComp = 3;
1787         gComp = 2;
1788         bComp = 1;
1789         aComp = 0;
1790         stride = 4;
1791         break;
1792      default:
1793         gl_problem(NULL, "bad srcFormat in extract float data");
1794         return;
1795   }
1796
1797
1798#define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION)		\
1799   if ((INDEX) < 0) {							\
1800      GLuint i;								\
1801      for (i = 0; i < n; i++) {						\
1802         rgba[i][CHANNEL] = DEFAULT;					\
1803      }									\
1804   }									\
1805   else if (swapBytes) {						\
1806      const TYPE *s = (const TYPE *) src;				\
1807      GLuint i;								\
1808      for (i = 0; i < n; i++) {						\
1809         TYPE value = s[INDEX];						\
1810         if (sizeof(TYPE) == 2) {					\
1811            SWAP2BYTE(value);						\
1812         }								\
1813         else if (sizeof(TYPE) == 4) {					\
1814            SWAP4BYTE(value);						\
1815         }								\
1816         rgba[i][CHANNEL] = (GLfloat) CONVERSION(value);		\
1817         s += stride;							\
1818      }									\
1819   }									\
1820   else {								\
1821      const TYPE *s = (const TYPE *) src;				\
1822      GLuint i;								\
1823      for (i = 0; i < n; i++) {						\
1824         rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]);		\
1825         s += stride;							\
1826      }									\
1827   }
1828
1829   switch (srcType) {
1830      case GL_UNSIGNED_BYTE:
1831         PROCESS(redIndex,   RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1832         PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1833         PROCESS(blueIndex,  BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
1834         PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
1835         break;
1836      case GL_BYTE:
1837         PROCESS(redIndex,   RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1838         PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1839         PROCESS(blueIndex,  BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
1840         PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
1841         break;
1842      case GL_UNSIGNED_SHORT:
1843         PROCESS(redIndex,   RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1844         PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1845         PROCESS(blueIndex,  BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
1846         PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
1847         break;
1848      case GL_SHORT:
1849         PROCESS(redIndex,   RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1850         PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1851         PROCESS(blueIndex,  BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
1852         PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
1853         break;
1854      case GL_UNSIGNED_INT:
1855         PROCESS(redIndex,   RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1856         PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1857         PROCESS(blueIndex,  BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
1858         PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
1859         break;
1860      case GL_INT:
1861         PROCESS(redIndex,   RCOMP, 0.0F, GLint, INT_TO_FLOAT);
1862         PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
1863         PROCESS(blueIndex,  BCOMP, 0.0F, GLint, INT_TO_FLOAT);
1864         PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
1865         break;
1866      case GL_FLOAT:
1867         PROCESS(redIndex,   RCOMP, 0.0F, GLfloat, (GLfloat));
1868         PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
1869         PROCESS(blueIndex,  BCOMP, 0.0F, GLfloat, (GLfloat));
1870         PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
1871         break;
1872      case GL_UNSIGNED_BYTE_3_3_2:
1873         {
1874            const GLubyte *ubsrc = (const GLubyte *) src;
1875            GLuint i;
1876            for (i = 0; i < n; i ++) {
1877               GLubyte p = ubsrc[i];
1878               rgba[i][RCOMP] = ((p >> 5)      ) * (1.0F / 7.0F);
1879               rgba[i][GCOMP] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
1880               rgba[i][BCOMP] = ((p     ) & 0x3) * (1.0F / 3.0F);
1881               rgba[i][ACOMP] = 1.0F;
1882            }
1883         }
1884         break;
1885      case GL_UNSIGNED_BYTE_2_3_3_REV:
1886         {
1887            const GLubyte *ubsrc = (const GLubyte *) src;
1888            GLuint i;
1889            for (i = 0; i < n; i ++) {
1890               GLubyte p = ubsrc[i];
1891               rgba[i][RCOMP] = ((p     ) & 0x7) * (1.0F / 7.0F);
1892               rgba[i][GCOMP] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
1893               rgba[i][BCOMP] = ((p >> 6)      ) * (1.0F / 3.0F);
1894               rgba[i][ACOMP] = 1.0F;
1895            }
1896         }
1897         break;
1898      case GL_UNSIGNED_SHORT_5_6_5:
1899         if (swapBytes) {
1900            const GLushort *ussrc = (const GLushort *) src;
1901            GLuint i;
1902            for (i = 0; i < n; i ++) {
1903               GLushort p = ussrc[i];
1904               SWAP2BYTE(p);
1905               rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1906               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1907               rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1908               rgba[i][ACOMP] = 1.0F;
1909            }
1910         }
1911         else {
1912            const GLushort *ussrc = (const GLushort *) src;
1913            GLuint i;
1914            for (i = 0; i < n; i ++) {
1915               GLushort p = ussrc[i];
1916               rgba[i][RCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1917               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1918               rgba[i][BCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1919               rgba[i][ACOMP] = 1.0F;
1920            }
1921         }
1922         break;
1923      case GL_UNSIGNED_SHORT_5_6_5_REV:
1924         if (swapBytes) {
1925            const GLushort *ussrc = (const GLushort *) src;
1926            GLuint i;
1927            for (i = 0; i < n; i ++) {
1928               GLushort p = ussrc[i];
1929               SWAP2BYTE(p);
1930               rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1931               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1932               rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1933               rgba[i][ACOMP] = 1.0F;
1934            }
1935         }
1936         else {
1937            const GLushort *ussrc = (const GLushort *) src;
1938            GLuint i;
1939            for (i = 0; i < n; i ++) {
1940               GLushort p = ussrc[i];
1941               rgba[i][RCOMP] = ((p      ) & 0x1f) * (1.0F / 31.0F);
1942               rgba[i][GCOMP] = ((p >>  5) & 0x3f) * (1.0F / 63.0F);
1943               rgba[i][BCOMP] = ((p >> 11)       ) * (1.0F / 31.0F);
1944               rgba[i][ACOMP] = 1.0F;
1945            }
1946         }
1947         break;
1948      case GL_UNSIGNED_SHORT_4_4_4_4:
1949         if (swapBytes) {
1950            const GLushort *ussrc = (const GLushort *) src;
1951            GLuint i;
1952            for (i = 0; i < n; i ++) {
1953               GLushort p = ussrc[i];
1954               SWAP2BYTE(p);
1955               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1956               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1957               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1958               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1959            }
1960         }
1961         else {
1962            const GLushort *ussrc = (const GLushort *) src;
1963            GLuint i;
1964            for (i = 0; i < n; i ++) {
1965               GLushort p = ussrc[i];
1966               rgba[i][rComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1967               rgba[i][gComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1968               rgba[i][bComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1969               rgba[i][aComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1970            }
1971         }
1972         break;
1973      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1974         if (swapBytes) {
1975            const GLushort *ussrc = (const GLushort *) src;
1976            GLuint i;
1977            for (i = 0; i < n; i ++) {
1978               GLushort p = ussrc[i];
1979               SWAP2BYTE(p);
1980               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1981               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1982               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1983               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1984            }
1985         }
1986         else {
1987            const GLushort *ussrc = (const GLushort *) src;
1988            GLuint i;
1989            for (i = 0; i < n; i ++) {
1990               GLushort p = ussrc[i];
1991               rgba[i][rComp] = ((p      ) & 0xf) * (1.0F / 15.0F);
1992               rgba[i][gComp] = ((p >>  4) & 0xf) * (1.0F / 15.0F);
1993               rgba[i][bComp] = ((p >>  8) & 0xf) * (1.0F / 15.0F);
1994               rgba[i][aComp] = ((p >> 12)      ) * (1.0F / 15.0F);
1995            }
1996         }
1997         break;
1998      case GL_UNSIGNED_SHORT_5_5_5_1:
1999         if (swapBytes) {
2000            const GLushort *ussrc = (const GLushort *) src;
2001            GLuint i;
2002            for (i = 0; i < n; i ++) {
2003               GLushort p = ussrc[i];
2004               SWAP2BYTE(p);
2005               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2006               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2007               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2008               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2009            }
2010         }
2011         else {
2012            const GLushort *ussrc = (const GLushort *) src;
2013            GLuint i;
2014            for (i = 0; i < n; i ++) {
2015               GLushort p = ussrc[i];
2016               rgba[i][rComp] = ((p >> 11)       ) * (1.0F / 31.0F);
2017               rgba[i][gComp] = ((p >>  6) & 0x1f) * (1.0F / 31.0F);
2018               rgba[i][bComp] = ((p >>  1) & 0x1f) * (1.0F / 31.0F);
2019               rgba[i][aComp] = ((p      ) & 0x1)  * (1.0F /  1.0F);
2020            }
2021         }
2022         break;
2023      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2024         if (swapBytes) {
2025            const GLushort *ussrc = (const GLushort *) src;
2026            GLuint i;
2027            for (i = 0; i < n; i ++) {
2028               GLushort p = ussrc[i];
2029               SWAP2BYTE(p);
2030               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2031               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2032               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2033               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2034            }
2035         }
2036         else {
2037            const GLushort *ussrc = (const GLushort *) src;
2038            GLuint i;
2039            for (i = 0; i < n; i ++) {
2040               GLushort p = ussrc[i];
2041               rgba[i][rComp] = ((p      ) & 0x1f) * (1.0F / 31.0F);
2042               rgba[i][gComp] = ((p >>  5) & 0x1f) * (1.0F / 31.0F);
2043               rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
2044               rgba[i][aComp] = ((p >> 15)       ) * (1.0F /  1.0F);
2045            }
2046         }
2047         break;
2048      case GL_UNSIGNED_INT_8_8_8_8:
2049         if (swapBytes) {
2050            const GLuint *uisrc = (const GLuint *) src;
2051            GLuint i;
2052            for (i = 0; i < n; i ++) {
2053               GLuint p = uisrc[i];
2054               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2055               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2056               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2057               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2058            }
2059         }
2060         else {
2061            const GLuint *uisrc = (const GLuint *) src;
2062            GLuint i;
2063            for (i = 0; i < n; i ++) {
2064               GLuint p = uisrc[i];
2065               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2066               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2067               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2068               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2069            }
2070         }
2071         break;
2072      case GL_UNSIGNED_INT_8_8_8_8_REV:
2073         if (swapBytes) {
2074            const GLuint *uisrc = (const GLuint *) src;
2075            GLuint i;
2076            for (i = 0; i < n; i ++) {
2077               GLuint p = uisrc[i];
2078               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2079               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2080               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2081               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2082            }
2083         }
2084         else {
2085            const GLuint *uisrc = (const GLuint *) src;
2086            GLuint i;
2087            for (i = 0; i < n; i ++) {
2088               GLuint p = uisrc[i];
2089               rgba[i][rComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
2090               rgba[i][gComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
2091               rgba[i][bComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
2092               rgba[i][aComp] = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
2093            }
2094         }
2095         break;
2096      case GL_UNSIGNED_INT_10_10_10_2:
2097         if (swapBytes) {
2098            const GLuint *uisrc = (const GLuint *) src;
2099            GLuint i;
2100            for (i = 0; i < n; i ++) {
2101               GLuint p = uisrc[i];
2102               SWAP4BYTE(p);
2103               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2104               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2105               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2106               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2107            }
2108         }
2109         else {
2110            const GLuint *uisrc = (const GLuint *) src;
2111            GLuint i;
2112            for (i = 0; i < n; i ++) {
2113               GLuint p = uisrc[i];
2114               rgba[i][rComp] = ((p >> 22)        ) * (1.0F / 1023.0F);
2115               rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
2116               rgba[i][bComp] = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F);
2117               rgba[i][aComp] = ((p      ) & 0x3  ) * (1.0F /    3.0F);
2118            }
2119         }
2120         break;
2121      case GL_UNSIGNED_INT_2_10_10_10_REV:
2122         if (swapBytes) {
2123            const GLuint *uisrc = (const GLuint *) src;
2124            GLuint i;
2125            for (i = 0; i < n; i ++) {
2126               GLuint p = uisrc[i];
2127               SWAP4BYTE(p);
2128               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2129               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2130               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2131               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2132            }
2133         }
2134         else {
2135            const GLuint *uisrc = (const GLuint *) src;
2136            GLuint i;
2137            for (i = 0; i < n; i ++) {
2138               GLuint p = uisrc[i];
2139               rgba[i][rComp] = ((p      ) & 0x3ff) * (1.0F / 1023.0F);
2140               rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
2141               rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
2142               rgba[i][aComp] = ((p >> 30)        ) * (1.0F /    3.0F);
2143            }
2144         }
2145         break;
2146      default:
2147         gl_problem(NULL, "bad srcType in extract float data");
2148         break;
2149   }
2150}
2151
2152
2153
2154/*
2155 * Unpack a row of color image data from a client buffer according to
2156 * the pixel unpacking parameters.
2157 * Return GLubyte values in the specified dest image format.
2158 * This is (or will be) used by glDrawPixels and glTexImage?D().
2159 * Input:  ctx - the context
2160 *         n - number of pixels in the span
2161 *         dstFormat - format of destination color array
2162 *         dest - the destination color array
2163 *         srcFormat - source image format
2164 *         srcType - source image  datatype
2165 *         source - source image pointer
2166 *         unpacking - pixel unpacking parameters
2167 *         transferOps - bitmask of IMAGE_*_BIT values of operations to apply
2168 *
2169 * XXX perhaps expand this to process whole images someday.
2170 */
2171void
2172_mesa_unpack_ubyte_color_span( GLcontext *ctx,
2173                               GLuint n, GLenum dstFormat, GLubyte dest[],
2174                               GLenum srcFormat, GLenum srcType,
2175                               const GLvoid *source,
2176                               const struct gl_pixelstore_attrib *unpacking,
2177                               GLuint transferOps )
2178{
2179   ASSERT(dstFormat == GL_ALPHA ||
2180          dstFormat == GL_LUMINANCE ||
2181          dstFormat == GL_LUMINANCE_ALPHA ||
2182          dstFormat == GL_INTENSITY ||
2183          dstFormat == GL_RGB ||
2184          dstFormat == GL_RGBA ||
2185          dstFormat == GL_COLOR_INDEX);
2186
2187   ASSERT(srcFormat == GL_RED ||
2188          srcFormat == GL_GREEN ||
2189          srcFormat == GL_BLUE ||
2190          srcFormat == GL_ALPHA ||
2191          srcFormat == GL_LUMINANCE ||
2192          srcFormat == GL_LUMINANCE_ALPHA ||
2193          srcFormat == GL_INTENSITY ||
2194          srcFormat == GL_RGB ||
2195          srcFormat == GL_BGR ||
2196          srcFormat == GL_RGBA ||
2197          srcFormat == GL_BGRA ||
2198          srcFormat == GL_ABGR_EXT ||
2199          srcFormat == GL_COLOR_INDEX);
2200
2201   ASSERT(srcType == GL_BITMAP ||
2202          srcType == GL_UNSIGNED_BYTE ||
2203          srcType == GL_BYTE ||
2204          srcType == GL_UNSIGNED_SHORT ||
2205          srcType == GL_SHORT ||
2206          srcType == GL_UNSIGNED_INT ||
2207          srcType == GL_INT ||
2208          srcType == GL_FLOAT ||
2209          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2210          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2211          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2212          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2213          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2214          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2215          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2216          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2217          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2218          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2219          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2220          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2221
2222   /* this is intended for RGBA mode only */
2223   assert(ctx->Visual->RGBAflag);
2224
2225   /* Try simple cases first */
2226   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE) {
2227      if (dstFormat == GL_RGBA) {
2228         if (srcFormat == GL_RGBA) {
2229            MEMCPY( dest, source, n * 4 * sizeof(GLubyte) );
2230            return;
2231         }
2232         else if (srcFormat == GL_RGB) {
2233            GLuint i;
2234            const GLubyte *src = (const GLubyte *) source;
2235            GLubyte *dst = dest;
2236            for (i = 0; i < n; i++) {
2237               dst[0] = src[0];
2238               dst[1] = src[1];
2239               dst[2] = src[2];
2240               dst[3] = 255;
2241               src += 3;
2242               dst += 4;
2243            }
2244            return;
2245         }
2246      }
2247      else if (dstFormat == GL_RGB) {
2248         if (srcFormat == GL_RGB) {
2249            MEMCPY( dest, source, n * 3 * sizeof(GLubyte) );
2250            return;
2251         }
2252         else if (srcFormat == GL_RGBA) {
2253            GLuint i;
2254            const GLubyte *src = (const GLubyte *) source;
2255            GLubyte *dst = dest;
2256            for (i = 0; i < n; i++) {
2257               dst[0] = src[0];
2258               dst[1] = src[1];
2259               dst[2] = src[2];
2260               src += 4;
2261               dst += 3;
2262            }
2263            return;
2264         }
2265      }
2266      else if (dstFormat == srcFormat) {
2267         GLint comps = _mesa_components_in_format(srcFormat);
2268         assert(comps > 0);
2269         MEMCPY( dest, source, n * comps * sizeof(GLubyte) );
2270         return;
2271      }
2272   }
2273
2274
2275   /* general solution begins here */
2276   {
2277      GLfloat rgba[MAX_WIDTH][4];
2278      GLint dstComponents;
2279      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2280      GLint dstLuminanceIndex, dstIntensityIndex;
2281
2282      dstComponents = _mesa_components_in_format( dstFormat );
2283      /* source & dest image formats should have been error checked by now */
2284      assert(dstComponents > 0);
2285
2286      /*
2287       * Extract image data and convert to RGBA floats
2288       */
2289      assert(n <= MAX_WIDTH);
2290      if (srcFormat == GL_COLOR_INDEX) {
2291         GLuint indexes[MAX_WIDTH];
2292         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2293                              unpacking);
2294
2295         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2296            _mesa_map_ci(ctx, n, indexes);
2297         }
2298         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2299            _mesa_shift_and_offset_ci(ctx, n, indexes);
2300         }
2301
2302         if (dstFormat == GL_COLOR_INDEX) {
2303            /* convert to GLubyte and return */
2304            GLuint i;
2305            for (i = 0; i < n; i++) {
2306               dest[i] = (GLubyte) (indexes[i] & 0xff);
2307            }
2308            return;
2309         }
2310         else {
2311            /* Convert indexes to RGBA */
2312            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
2313         }
2314      }
2315      else {
2316         extract_float_rgba(n, rgba, srcFormat, srcType, source,
2317                            unpacking->SwapBytes);
2318
2319         /* scale and bias colors */
2320         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
2321            _mesa_scale_and_bias_rgba(ctx, n, rgba);
2322         }
2323         /* color map lookup */
2324         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2325            _mesa_map_rgba(ctx, n, rgba);
2326         }
2327      }
2328
2329      if (transferOps) {
2330         /* GL_COLOR_TABLE lookup */
2331         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
2332            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
2333         }
2334         /* convolution */
2335         if (transferOps & IMAGE_CONVOLUTION_BIT) {
2336            /* this has to be done in the calling code */
2337         }
2338         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
2339         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
2340            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
2341         }
2342         /* color matrix transform */
2343         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
2344            _mesa_transform_rgba(ctx, n, rgba);
2345         }
2346         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
2347         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
2348            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
2349         }
2350         /* update histogram count */
2351         if (transferOps & IMAGE_HISTOGRAM_BIT) {
2352            _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
2353         }
2354         /* min/max here */
2355         if (transferOps & IMAGE_MIN_MAX_BIT) {
2356            _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
2357         }
2358      }
2359
2360      /* clamp to [0,1] */
2361      {
2362         GLuint i;
2363         for (i = 0; i < n; i++) {
2364            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2365            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2366            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2367            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2368         }
2369      }
2370
2371      /* Now determine which color channels we need to produce.
2372       * And determine the dest index (offset) within each color tuple.
2373       */
2374      switch (dstFormat) {
2375         case GL_ALPHA:
2376            dstAlphaIndex = 0;
2377            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2378            dstLuminanceIndex = dstIntensityIndex = -1;
2379            break;
2380         case GL_LUMINANCE:
2381            dstLuminanceIndex = 0;
2382            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2383            dstIntensityIndex = -1;
2384            break;
2385         case GL_LUMINANCE_ALPHA:
2386            dstLuminanceIndex = 0;
2387            dstAlphaIndex = 1;
2388            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2389            dstIntensityIndex = -1;
2390            break;
2391         case GL_INTENSITY:
2392            dstIntensityIndex = 0;
2393            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2394            dstLuminanceIndex = -1;
2395            break;
2396         case GL_RGB:
2397            dstRedIndex = 0;
2398            dstGreenIndex = 1;
2399            dstBlueIndex = 2;
2400            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2401            break;
2402         case GL_RGBA:
2403            dstRedIndex = 0;
2404            dstGreenIndex = 1;
2405            dstBlueIndex = 2;
2406            dstAlphaIndex = 3;
2407            dstLuminanceIndex = dstIntensityIndex = -1;
2408            break;
2409         default:
2410            gl_problem(ctx, "bad dstFormat in _mesa_unpack_ubyte_span()");
2411            return;
2412      }
2413
2414
2415      /* Now return the GLubyte data in the requested dstFormat */
2416
2417      if (dstRedIndex >= 0) {
2418         GLubyte *dst = dest;
2419         GLuint i;
2420         for (i = 0; i < n; i++) {
2421            dst[dstRedIndex] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2422            dst += dstComponents;
2423         }
2424      }
2425
2426      if (dstGreenIndex >= 0) {
2427         GLubyte *dst = dest;
2428         GLuint i;
2429         for (i = 0; i < n; i++) {
2430            dst[dstGreenIndex] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2431            dst += dstComponents;
2432         }
2433      }
2434
2435      if (dstBlueIndex >= 0) {
2436         GLubyte *dst = dest;
2437         GLuint i;
2438         for (i = 0; i < n; i++) {
2439            dst[dstBlueIndex] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2440            dst += dstComponents;
2441         }
2442      }
2443
2444      if (dstAlphaIndex >= 0) {
2445         GLubyte *dst = dest;
2446         GLuint i;
2447         for (i = 0; i < n; i++) {
2448            dst[dstAlphaIndex] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2449            dst += dstComponents;
2450         }
2451      }
2452
2453      if (dstIntensityIndex >= 0) {
2454         GLubyte *dst = dest;
2455         GLuint i;
2456         assert(dstIntensityIndex == 0);
2457         assert(dstComponents == 1);
2458         for (i = 0; i < n; i++) {
2459            /* Intensity comes from red channel */
2460            dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2461         }
2462      }
2463
2464      if (dstLuminanceIndex >= 0) {
2465         GLubyte *dst = dest;
2466         GLuint i;
2467         assert(dstLuminanceIndex == 0);
2468         for (i = 0; i < n; i++) {
2469            /* Luminance comes from red channel */
2470            dst[0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2471            dst += dstComponents;
2472         }
2473      }
2474   }
2475}
2476
2477
2478void
2479_mesa_unpack_float_color_span( GLcontext *ctx,
2480                               GLuint n, GLenum dstFormat, GLfloat dest[],
2481                               GLenum srcFormat, GLenum srcType,
2482                               const GLvoid *source,
2483                               const struct gl_pixelstore_attrib *unpacking,
2484                               GLuint transferOps, GLboolean clamp )
2485{
2486   ASSERT(dstFormat == GL_ALPHA ||
2487          dstFormat == GL_LUMINANCE ||
2488          dstFormat == GL_LUMINANCE_ALPHA ||
2489          dstFormat == GL_INTENSITY ||
2490          dstFormat == GL_RGB ||
2491          dstFormat == GL_RGBA ||
2492          dstFormat == GL_COLOR_INDEX);
2493
2494   ASSERT(srcFormat == GL_RED ||
2495          srcFormat == GL_GREEN ||
2496          srcFormat == GL_BLUE ||
2497          srcFormat == GL_ALPHA ||
2498          srcFormat == GL_LUMINANCE ||
2499          srcFormat == GL_LUMINANCE_ALPHA ||
2500          srcFormat == GL_INTENSITY ||
2501          srcFormat == GL_RGB ||
2502          srcFormat == GL_BGR ||
2503          srcFormat == GL_RGBA ||
2504          srcFormat == GL_BGRA ||
2505          srcFormat == GL_ABGR_EXT ||
2506          srcFormat == GL_COLOR_INDEX);
2507
2508   ASSERT(srcType == GL_BITMAP ||
2509          srcType == GL_UNSIGNED_BYTE ||
2510          srcType == GL_BYTE ||
2511          srcType == GL_UNSIGNED_SHORT ||
2512          srcType == GL_SHORT ||
2513          srcType == GL_UNSIGNED_INT ||
2514          srcType == GL_INT ||
2515          srcType == GL_FLOAT ||
2516          srcType == GL_UNSIGNED_BYTE_3_3_2 ||
2517          srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
2518          srcType == GL_UNSIGNED_SHORT_5_6_5 ||
2519          srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
2520          srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
2521          srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
2522          srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
2523          srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
2524          srcType == GL_UNSIGNED_INT_8_8_8_8 ||
2525          srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
2526          srcType == GL_UNSIGNED_INT_10_10_10_2 ||
2527          srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
2528
2529   /* this is intended for RGBA mode only */
2530   assert(ctx->Visual->RGBAflag);
2531
2532   /* general solution, no special cases, yet */
2533   {
2534      GLfloat rgba[MAX_WIDTH][4];
2535      GLint dstComponents;
2536      GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
2537      GLint dstLuminanceIndex, dstIntensityIndex;
2538
2539      dstComponents = _mesa_components_in_format( dstFormat );
2540      /* source & dest image formats should have been error checked by now */
2541      assert(dstComponents > 0);
2542
2543      /*
2544       * Extract image data and convert to RGBA floats
2545       */
2546      assert(n <= MAX_WIDTH);
2547      if (srcFormat == GL_COLOR_INDEX) {
2548         GLuint indexes[MAX_WIDTH];
2549         extract_uint_indexes(n, indexes, srcFormat, srcType, source,
2550                              unpacking);
2551
2552         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2553            _mesa_map_ci(ctx, n, indexes);
2554         }
2555         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2556            _mesa_shift_and_offset_ci(ctx, n, indexes);
2557         }
2558
2559         if (dstFormat == GL_COLOR_INDEX) {
2560            /* convert to GLubyte and return */
2561            GLuint i;
2562            for (i = 0; i < n; i++) {
2563               dest[i] = (GLubyte) (indexes[i] & 0xff);
2564            }
2565            return;
2566         }
2567         else {
2568            /* Convert indexes to RGBA */
2569            _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
2570         }
2571      }
2572      else {
2573         extract_float_rgba(n, rgba, srcFormat, srcType, source,
2574                            unpacking->SwapBytes);
2575
2576         /* scale and bias colors */
2577         if (transferOps & IMAGE_SCALE_BIAS_BIT) {
2578            _mesa_scale_and_bias_rgba(ctx, n, rgba);
2579         }
2580         /* color map lookup */
2581         if (transferOps & IMAGE_MAP_COLOR_BIT) {
2582            _mesa_map_rgba(ctx, n, rgba);
2583         }
2584      }
2585
2586      if (transferOps) {
2587         /* GL_COLOR_TABLE lookup */
2588         if (transferOps & IMAGE_COLOR_TABLE_BIT) {
2589            _mesa_lookup_rgba(&ctx->ColorTable, n, rgba);
2590         }
2591         /* convolution */
2592         if (transferOps & IMAGE_CONVOLUTION_BIT) {
2593            /* XXX to do */
2594         }
2595         /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
2596         if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
2597            _mesa_lookup_rgba(&ctx->PostConvolutionColorTable, n, rgba);
2598         }
2599         /* color matrix transform */
2600         if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
2601            _mesa_transform_rgba(ctx, n, rgba);
2602         }
2603         /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
2604         if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
2605            _mesa_lookup_rgba(&ctx->PostColorMatrixColorTable, n, rgba);
2606         }
2607         /* update histogram count */
2608         if (transferOps & IMAGE_HISTOGRAM_BIT) {
2609            _mesa_update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
2610         }
2611         /* min/max here */
2612         if (transferOps & IMAGE_MIN_MAX_BIT) {
2613            _mesa_update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
2614         }
2615      }
2616
2617      /* clamp to [0,1] */
2618      if (clamp) {
2619         GLuint i;
2620         for (i = 0; i < n; i++) {
2621            rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
2622            rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
2623            rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
2624            rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
2625         }
2626      }
2627
2628      /* Now determine which color channels we need to produce.
2629       * And determine the dest index (offset) within each color tuple.
2630       */
2631      switch (dstFormat) {
2632         case GL_ALPHA:
2633            dstAlphaIndex = 0;
2634            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2635            dstLuminanceIndex = dstIntensityIndex = -1;
2636            break;
2637         case GL_LUMINANCE:
2638            dstLuminanceIndex = 0;
2639            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2640            dstIntensityIndex = -1;
2641            break;
2642         case GL_LUMINANCE_ALPHA:
2643            dstLuminanceIndex = 0;
2644            dstAlphaIndex = 1;
2645            dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
2646            dstIntensityIndex = -1;
2647            break;
2648         case GL_INTENSITY:
2649            dstIntensityIndex = 0;
2650            dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
2651            dstLuminanceIndex = -1;
2652            break;
2653         case GL_RGB:
2654            dstRedIndex = 0;
2655            dstGreenIndex = 1;
2656            dstBlueIndex = 2;
2657            dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
2658            break;
2659         case GL_RGBA:
2660            dstRedIndex = 0;
2661            dstGreenIndex = 1;
2662            dstBlueIndex = 2;
2663            dstAlphaIndex = 3;
2664            dstLuminanceIndex = dstIntensityIndex = -1;
2665            break;
2666         default:
2667            gl_problem(ctx, "bad dstFormat in _mesa_unpack_float_color_span()");
2668            return;
2669      }
2670
2671      /* Now pack results in the requested dstFormat */
2672      if (dstRedIndex >= 0) {
2673         GLfloat *dst = dest;
2674         GLuint i;
2675         for (i = 0; i < n; i++) {
2676            dst[dstRedIndex] = rgba[i][RCOMP];
2677            dst += dstComponents;
2678         }
2679      }
2680
2681      if (dstGreenIndex >= 0) {
2682         GLfloat *dst = dest;
2683         GLuint i;
2684         for (i = 0; i < n; i++) {
2685            dst[dstGreenIndex] = rgba[i][GCOMP];
2686            dst += dstComponents;
2687         }
2688      }
2689
2690      if (dstBlueIndex >= 0) {
2691         GLfloat *dst = dest;
2692         GLuint i;
2693         for (i = 0; i < n; i++) {
2694            dst[dstBlueIndex] = rgba[i][BCOMP];
2695            dst += dstComponents;
2696         }
2697      }
2698
2699      if (dstAlphaIndex >= 0) {
2700         GLfloat *dst = dest;
2701         GLuint i;
2702         for (i = 0; i < n; i++) {
2703            dst[dstAlphaIndex] = rgba[i][ACOMP];
2704            dst += dstComponents;
2705         }
2706      }
2707
2708      if (dstIntensityIndex >= 0) {
2709         GLfloat *dst = dest;
2710         GLuint i;
2711         assert(dstIntensityIndex == 0);
2712         assert(dstComponents == 1);
2713         for (i = 0; i < n; i++) {
2714            /* Intensity comes from red channel */
2715            dst[i] = rgba[i][RCOMP];
2716         }
2717      }
2718
2719      if (dstLuminanceIndex >= 0) {
2720         GLfloat *dst = dest;
2721         GLuint i;
2722         assert(dstLuminanceIndex == 0);
2723         for (i = 0; i < n; i++) {
2724            /* Luminance comes from red channel */
2725            dst[0] = rgba[i][RCOMP];
2726            dst += dstComponents;
2727         }
2728      }
2729   }
2730}
2731
2732
2733
2734
2735/*
2736 * Unpack a row of color index data from a client buffer according to
2737 * the pixel unpacking parameters.
2738 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2739 *
2740 * Args:  ctx - the context
2741 *        n - number of pixels
2742 *        dstType - destination datatype
2743 *        dest - destination array
2744 *        srcType - source pixel type
2745 *        source - source data pointer
2746 *        unpacking - pixel unpacking parameters
2747 *        transferOps - the pixel transfer operations to apply
2748 */
2749void
2750_mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
2751                         GLenum dstType, GLvoid *dest,
2752                         GLenum srcType, const GLvoid *source,
2753                         const struct gl_pixelstore_attrib *unpacking,
2754                         GLuint transferOps )
2755{
2756   ASSERT(srcType == GL_BITMAP ||
2757          srcType == GL_UNSIGNED_BYTE ||
2758          srcType == GL_BYTE ||
2759          srcType == GL_UNSIGNED_SHORT ||
2760          srcType == GL_SHORT ||
2761          srcType == GL_UNSIGNED_INT ||
2762          srcType == GL_INT ||
2763          srcType == GL_FLOAT);
2764
2765   ASSERT(dstType == GL_UNSIGNED_BYTE ||
2766          dstType == GL_UNSIGNED_SHORT ||
2767          dstType == GL_UNSIGNED_INT);
2768
2769
2770   transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
2771
2772   /*
2773    * Try simple cases first
2774    */
2775   if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
2776       && dstType == GL_UNSIGNED_BYTE) {
2777      MEMCPY(dest, source, n * sizeof(GLubyte));
2778   }
2779   else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
2780            && dstType == GL_UNSIGNED_INT && !unpacking->SwapBytes) {
2781      MEMCPY(dest, source, n * sizeof(GLuint));
2782   }
2783   else {
2784      /*
2785       * general solution
2786       */
2787      GLuint indexes[MAX_WIDTH];
2788      assert(n <= MAX_WIDTH);
2789
2790      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2791                           unpacking);
2792
2793      if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2794         /* shift and offset indexes */
2795         _mesa_shift_and_offset_ci(ctx, n, indexes);
2796      }
2797      if (transferOps & IMAGE_MAP_COLOR_BIT) {
2798         /* Apply lookup table */
2799         _mesa_map_ci(ctx, n, indexes);
2800      }
2801
2802      /* convert to dest type */
2803      switch (dstType) {
2804         case GL_UNSIGNED_BYTE:
2805            {
2806               GLubyte *dst = (GLubyte *) dest;
2807               GLuint i;
2808               for (i = 0; i < n; i++) {
2809                  dst[i] = (GLubyte) (indexes[i] & 0xff);
2810               }
2811            }
2812            break;
2813         case GL_UNSIGNED_SHORT:
2814            {
2815               GLuint *dst = (GLuint *) dest;
2816               GLuint i;
2817               for (i = 0; i < n; i++) {
2818                  dst[i] = (GLushort) (indexes[i] & 0xffff);
2819               }
2820            }
2821            break;
2822         case GL_UNSIGNED_INT:
2823            MEMCPY(dest, indexes, n * sizeof(GLuint));
2824            break;
2825         default:
2826            gl_problem(ctx, "bad dstType in _mesa_unpack_index_span");
2827      }
2828   }
2829}
2830
2831
2832/*
2833 * Unpack a row of stencil data from a client buffer according to
2834 * the pixel unpacking parameters.
2835 * This is (or will be) used by glDrawPixels
2836 *
2837 * Args:  ctx - the context
2838 *        n - number of pixels
2839 *        dstType - destination datatype
2840 *        dest - destination array
2841 *        srcType - source pixel type
2842 *        source - source data pointer
2843 *        unpacking - pixel unpacking parameters
2844 *        transferOps - apply offset/bias/lookup ops?
2845 */
2846void
2847_mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
2848                           GLenum dstType, GLvoid *dest,
2849                           GLenum srcType, const GLvoid *source,
2850                           const struct gl_pixelstore_attrib *unpacking,
2851                           GLuint transferOps )
2852{
2853   ASSERT(srcType == GL_BITMAP ||
2854          srcType == GL_UNSIGNED_BYTE ||
2855          srcType == GL_BYTE ||
2856          srcType == GL_UNSIGNED_SHORT ||
2857          srcType == GL_SHORT ||
2858          srcType == GL_UNSIGNED_INT ||
2859          srcType == GL_INT ||
2860          srcType == GL_FLOAT);
2861
2862   ASSERT(dstType == GL_UNSIGNED_BYTE ||
2863          dstType == GL_UNSIGNED_SHORT ||
2864          dstType == GL_UNSIGNED_INT);
2865
2866   /* only shift and offset apply to stencil */
2867   transferOps &= IMAGE_SHIFT_OFFSET_BIT;
2868
2869   /*
2870    * Try simple cases first
2871    */
2872   if (transferOps == 0 &&
2873       srcType == GL_UNSIGNED_BYTE &&
2874       dstType == GL_UNSIGNED_BYTE) {
2875      MEMCPY(dest, source, n * sizeof(GLubyte));
2876   }
2877   else if (transferOps == 0 &&
2878            srcType == GL_UNSIGNED_INT &&
2879            dstType == GL_UNSIGNED_INT &&
2880            !unpacking->SwapBytes) {
2881      MEMCPY(dest, source, n * sizeof(GLuint));
2882   }
2883   else {
2884      /*
2885       * general solution
2886       */
2887      GLuint indexes[MAX_WIDTH];
2888      assert(n <= MAX_WIDTH);
2889
2890      extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
2891                           unpacking);
2892
2893      if (transferOps) {
2894         if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
2895            /* shift and offset indexes */
2896            _mesa_shift_and_offset_ci(ctx, n, indexes);
2897         }
2898
2899         if (ctx->Pixel.MapStencilFlag) {
2900            /* Apply stencil lookup table */
2901            GLuint mask = ctx->Pixel.MapStoSsize - 1;
2902            GLuint i;
2903            for (i=0;i<n;i++) {
2904               indexes[i] = ctx->Pixel.MapStoS[ indexes[i] & mask ];
2905            }
2906         }
2907      }
2908
2909      /* convert to dest type */
2910      switch (dstType) {
2911         case GL_UNSIGNED_BYTE:
2912            {
2913               GLubyte *dst = (GLubyte *) dest;
2914               GLuint i;
2915               for (i = 0; i < n; i++) {
2916                  dst[i] = (GLubyte) (indexes[i] & 0xff);
2917               }
2918            }
2919            break;
2920         case GL_UNSIGNED_SHORT:
2921            {
2922               GLuint *dst = (GLuint *) dest;
2923               GLuint i;
2924               for (i = 0; i < n; i++) {
2925                  dst[i] = (GLushort) (indexes[i] & 0xffff);
2926               }
2927            }
2928            break;
2929         case GL_UNSIGNED_INT:
2930            MEMCPY(dest, indexes, n * sizeof(GLuint));
2931            break;
2932         default:
2933            gl_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
2934      }
2935   }
2936}
2937
2938
2939
2940void
2941_mesa_unpack_depth_span( const GLcontext *ctx, GLuint n, GLdepth *dest,
2942                         GLenum srcType, const GLvoid *source,
2943                         const struct gl_pixelstore_attrib *unpacking,
2944                         GLuint transferOps )
2945{
2946   GLfloat *depth = MALLOC(n * sizeof(GLfloat));
2947   if (!depth)
2948      return;
2949
2950   switch (srcType) {
2951      case GL_BYTE:
2952         {
2953            GLuint i;
2954            const GLubyte *src = (const GLubyte *) source;
2955            for (i = 0; i < n; i++) {
2956               depth[i] = BYTE_TO_FLOAT(src[i]);
2957            }
2958         }
2959         break;
2960      case GL_UNSIGNED_BYTE:
2961         {
2962            GLuint i;
2963            const GLubyte *src = (const GLubyte *) source;
2964            for (i = 0; i < n; i++) {
2965               depth[i] = UBYTE_TO_FLOAT(src[i]);
2966            }
2967         }
2968         break;
2969      case GL_SHORT:
2970         {
2971            GLuint i;
2972            const GLshort *src = (const GLshort *) source;
2973            for (i = 0; i < n; i++) {
2974               depth[i] = SHORT_TO_FLOAT(src[i]);
2975            }
2976         }
2977         break;
2978      case GL_UNSIGNED_SHORT:
2979         {
2980            GLuint i;
2981            const GLushort *src = (const GLushort *) source;
2982            for (i = 0; i < n; i++) {
2983               depth[i] = USHORT_TO_FLOAT(src[i]);
2984            }
2985         }
2986         break;
2987      case GL_INT:
2988         {
2989            GLuint i;
2990            const GLint *src = (const GLint *) source;
2991            for (i = 0; i < n; i++) {
2992               depth[i] = INT_TO_FLOAT(src[i]);
2993            }
2994         }
2995         break;
2996      case GL_UNSIGNED_INT:
2997         {
2998            GLuint i;
2999            const GLuint *src = (const GLuint *) source;
3000            for (i = 0; i < n; i++) {
3001               depth[i] = UINT_TO_FLOAT(src[i]);
3002            }
3003         }
3004         break;
3005      case GL_FLOAT:
3006         MEMCPY(depth, source, n * sizeof(GLfloat));
3007         break;
3008      default:
3009         gl_problem(NULL, "bad type in _mesa_unpack_depth_span()");
3010         FREE(depth);
3011         return;
3012   }
3013
3014
3015   /* apply depth scale and bias */
3016   if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
3017      GLuint i;
3018      for (i = 0; i < n; i++) {
3019         depth[i] = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
3020      }
3021   }
3022
3023   /* clamp depth values to [0,1] and convert from floats to integers */
3024   {
3025      const GLfloat zs = ctx->Visual->DepthMaxF;
3026      GLuint i;
3027      for (i = 0; i < n; i++) {
3028         dest[i] = (GLdepth) (CLAMP(depth[i], 0.0F, 1.0F) * zs);
3029      }
3030   }
3031
3032   FREE(depth);
3033}
3034
3035
3036
3037/*
3038 * Unpack image data.  Apply byteswapping, byte flipping (bitmap).
3039 * Return all image data in a contiguous block.
3040 */
3041void *
3042_mesa_unpack_image( GLsizei width, GLsizei height, GLsizei depth,
3043                    GLenum format, GLenum type, const GLvoid *pixels,
3044                    const struct gl_pixelstore_attrib *unpack )
3045{
3046   GLint bytesPerRow, compsPerRow;
3047   GLboolean flipBytes, swap2, swap4;
3048
3049   if (!pixels)
3050      return NULL;  /* not necessarily an error */
3051
3052   if (width <= 0 || height <= 0 || depth <= 0)
3053      return NULL;  /* generate error later */
3054
3055   if (format == GL_BITMAP) {
3056      bytesPerRow = (width + 7) >> 3;
3057      flipBytes = !unpack->LsbFirst;
3058      swap2 = swap4 = GL_FALSE;
3059      compsPerRow = 0;
3060   }
3061   else {
3062      const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
3063      const GLint components = _mesa_components_in_format(format);
3064      GLint bytesPerComp;
3065      if (bytesPerPixel <= 0 || components <= 0)
3066         return NULL;   /* bad format or type.  generate error later */
3067      bytesPerRow = bytesPerPixel * width;
3068      bytesPerComp = bytesPerPixel / components;
3069      flipBytes = GL_FALSE;
3070      swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
3071      swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
3072      compsPerRow = components * width;
3073      assert(compsPerRow >= width);
3074   }
3075
3076   {
3077      GLubyte *destBuffer = MALLOC(bytesPerRow * height * depth);
3078      GLubyte *dst;
3079      GLint img, row;
3080      if (!destBuffer)
3081         return NULL;   /* generate GL_OUT_OF_MEMORY later */
3082
3083      dst = destBuffer;
3084      for (img = 0; img < depth; img++) {
3085         for (row = 0; row < height; row++) {
3086            const GLvoid *src = _mesa_image_address(unpack, pixels,
3087                               width, height, format, type, img, row, 0);
3088            MEMCPY(dst, src, bytesPerRow);
3089            /* byte flipping/swapping */
3090            if (flipBytes) {
3091               flip_bytes((GLubyte *) dst, bytesPerRow);
3092            }
3093            else if (swap2) {
3094               _mesa_swap2((GLushort*) dst, compsPerRow);
3095            }
3096            else if (swap4) {
3097               _mesa_swap4((GLuint*) dst, compsPerRow);
3098            }
3099            dst += bytesPerRow;
3100         }
3101      }
3102      return destBuffer;
3103   }
3104}
3105
3106
3107/*
3108 * Unpack bitmap data.  Resulting data will be in most-significant-bit-first
3109 * order with row alignment = 1 byte.
3110 */
3111GLvoid *
3112_mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
3113                     const struct gl_pixelstore_attrib *packing )
3114{
3115   GLint bytes, row, width_in_bytes;
3116   GLubyte *buffer, *dst;
3117
3118   if (!pixels)
3119      return NULL;
3120
3121   /* Alloc dest storage */
3122   bytes = ((width + 7) / 8 * height);
3123   buffer = (GLubyte *) MALLOC( bytes );
3124   if (!buffer)
3125      return NULL;
3126
3127
3128   width_in_bytes = CEILING( width, 8 );
3129   dst = buffer;
3130   for (row = 0; row < height; row++) {
3131      GLubyte *src = _mesa_image_address( packing, pixels, width, height,
3132                                          GL_COLOR_INDEX, GL_BITMAP,
3133                                          0, row, 0 );
3134      if (!src) {
3135         FREE(buffer);
3136         return NULL;
3137      }
3138
3139      if (packing->SkipPixels == 0) {
3140         MEMCPY( dst, src, width_in_bytes );
3141         if (packing->LsbFirst) {
3142            flip_bytes( dst, width_in_bytes );
3143         }
3144      }
3145      else {
3146         /* handling SkipPixels is a bit tricky (no pun intended!) */
3147         GLint i;
3148         if (packing->LsbFirst) {
3149            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
3150            GLubyte dstMask = 128;
3151            GLubyte *s = src;
3152            GLubyte *d = dst;
3153            *d = 0;
3154            for (i = 0; i < width; i++) {
3155               if (*s & srcMask) {
3156                  *d |= dstMask;
3157               }
3158               if (srcMask == 128) {
3159                  srcMask = 1;
3160                  s++;
3161               }
3162               else {
3163                  srcMask = srcMask << 1;
3164               }
3165               if (dstMask == 1) {
3166                  dstMask = 128;
3167                  d++;
3168                  *d = 0;
3169               }
3170               else {
3171                  dstMask = dstMask >> 1;
3172               }
3173            }
3174         }
3175         else {
3176            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
3177            GLubyte dstMask = 128;
3178            GLubyte *s = src;
3179            GLubyte *d = dst;
3180            *d = 0;
3181            for (i = 0; i < width; i++) {
3182               if (*s & srcMask) {
3183                  *d |= dstMask;
3184               }
3185               if (srcMask == 1) {
3186                  srcMask = 128;
3187                  s++;
3188               }
3189               else {
3190                  srcMask = srcMask >> 1;
3191               }
3192               if (dstMask == 1) {
3193                  dstMask = 128;
3194                  d++;
3195                  *d = 0;
3196               }
3197               else {
3198                  dstMask = dstMask >> 1;
3199               }
3200            }
3201         }
3202      }
3203      dst += width_in_bytes;
3204   }
3205
3206   return buffer;
3207}
3208
3209
3210/*
3211 * Pack bitmap data.
3212 */
3213void
3214_mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
3215                   GLubyte *dest, const struct gl_pixelstore_attrib *packing )
3216{
3217   GLint row, width_in_bytes;
3218   const GLubyte *src;
3219
3220   if (!source)
3221      return;
3222
3223   width_in_bytes = CEILING( width, 8 );
3224   src = source;
3225   for (row = 0; row < height; row++) {
3226      GLubyte *dst = _mesa_image_address( packing, dest, width, height,
3227                                          GL_COLOR_INDEX, GL_BITMAP,
3228                                          0, row, 0 );
3229      if (!dst)
3230         return;
3231
3232      if (packing->SkipPixels == 0) {
3233         MEMCPY( dst, src, width_in_bytes );
3234         if (packing->LsbFirst) {
3235            flip_bytes( dst, width_in_bytes );
3236         }
3237      }
3238      else {
3239         /* handling SkipPixels is a bit tricky (no pun intended!) */
3240         GLint i;
3241         if (packing->LsbFirst) {
3242            GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
3243            GLubyte dstMask = 128;
3244            const GLubyte *s = src;
3245            GLubyte *d = dst;
3246            *d = 0;
3247            for (i = 0; i < width; i++) {
3248               if (*s & srcMask) {
3249                  *d |= dstMask;
3250               }
3251               if (srcMask == 128) {
3252                  srcMask = 1;
3253                  s++;
3254               }
3255               else {
3256                  srcMask = srcMask << 1;
3257               }
3258               if (dstMask == 1) {
3259                  dstMask = 128;
3260                  d++;
3261                  *d = 0;
3262               }
3263               else {
3264                  dstMask = dstMask >> 1;
3265               }
3266            }
3267         }
3268         else {
3269            GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
3270            GLubyte dstMask = 128;
3271            const GLubyte *s = src;
3272            GLubyte *d = dst;
3273            *d = 0;
3274            for (i = 0; i < width; i++) {
3275               if (*s & srcMask) {
3276                  *d |= dstMask;
3277               }
3278               if (srcMask == 1) {
3279                  srcMask = 128;
3280                  s++;
3281               }
3282               else {
3283                  srcMask = srcMask >> 1;
3284               }
3285               if (dstMask == 1) {
3286                  dstMask = 128;
3287                  d++;
3288                  *d = 0;
3289               }
3290               else {
3291                  dstMask = dstMask >> 1;
3292               }
3293            }
3294         }
3295      }
3296      src += width_in_bytes;
3297   }
3298}
3299