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