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