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