image.c revision afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1c
1/* $Id: image.c,v 1.1 1999/08/19 00:55:41 jtg Exp $ */
2
3/*
4 * Mesa 3-D graphics library
5 * Version:  3.1
6 *
7 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28
29#ifdef PC_HEADER
30#include "all.h"
31#else
32#include <assert.h>
33#include <stdlib.h>
34#include <string.h>
35#include "context.h"
36#include "image.h"
37#include "macros.h"
38#include "mmath.h"
39#include "pixel.h"
40#include "types.h"
41#ifdef XFree86Server
42#include "GL/xf86glx.h"
43#endif
44#endif
45
46
47
48/*
49 * Flip the 8 bits in each byte of the given array.
50 */
51void gl_flip_bytes( GLubyte *p, GLuint n )
52{
53   register GLuint i, a, b;
54
55   for (i=0;i<n;i++) {
56      b = (GLuint) p[i];
57      a = ((b & 0x01) << 7) |
58	  ((b & 0x02) << 5) |
59	  ((b & 0x04) << 3) |
60	  ((b & 0x08) << 1) |
61	  ((b & 0x10) >> 1) |
62	  ((b & 0x20) >> 3) |
63	  ((b & 0x40) >> 5) |
64	  ((b & 0x80) >> 7);
65      p[i] = (GLubyte) a;
66   }
67}
68
69
70/*
71 * Flip the order of the 2 bytes in each word in the given array.
72 */
73void gl_swap2( GLushort *p, GLuint n )
74{
75   register GLuint i;
76
77   for (i=0;i<n;i++) {
78      p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
79   }
80}
81
82
83
84/*
85 * Flip the order of the 4 bytes in each word in the given array.
86 */
87void gl_swap4( GLuint *p, GLuint n )
88{
89   register GLuint i, a, b;
90
91   for (i=0;i<n;i++) {
92      b = p[i];
93      a =  (b >> 24)
94	| ((b >> 8) & 0xff00)
95	| ((b << 8) & 0xff0000)
96	| ((b << 24) & 0xff000000);
97      p[i] = a;
98   }
99}
100
101
102
103
104/*
105 * Return the size, in bytes, of the given GL datatype.
106 * Return 0 if GL_BITMAP.
107 * Return -1 if invalid type enum.
108 */
109GLint gl_sizeof_type( GLenum type )
110{
111   switch (type) {
112      case GL_BITMAP:
113	 return 0;
114      case GL_UNSIGNED_BYTE:
115         return sizeof(GLubyte);
116      case GL_BYTE:
117	 return sizeof(GLbyte);
118      case GL_UNSIGNED_SHORT:
119	 return sizeof(GLushort);
120      case GL_SHORT:
121	 return sizeof(GLshort);
122      case GL_UNSIGNED_INT:
123	 return sizeof(GLuint);
124      case GL_INT:
125	 return sizeof(GLint);
126      case GL_FLOAT:
127	 return sizeof(GLfloat);
128      default:
129         return -1;
130   }
131}
132
133
134/*
135 * Same as gl_sizeof_packed_type() but we also accept the
136 * packed pixel format datatypes.
137 */
138GLint gl_sizeof_packed_type( GLenum type )
139{
140   switch (type) {
141      case GL_BITMAP:
142	 return 0;
143      case GL_UNSIGNED_BYTE:
144         return sizeof(GLubyte);
145      case GL_BYTE:
146	 return sizeof(GLbyte);
147      case GL_UNSIGNED_SHORT:
148	 return sizeof(GLushort);
149      case GL_SHORT:
150	 return sizeof(GLshort);
151      case GL_UNSIGNED_INT:
152	 return sizeof(GLuint);
153      case GL_INT:
154	 return sizeof(GLint);
155      case GL_FLOAT:
156	 return sizeof(GLfloat);
157      case GL_UNSIGNED_BYTE_3_3_2:
158         return sizeof(GLubyte);
159      case GL_UNSIGNED_BYTE_2_3_3_REV:
160         return sizeof(GLubyte);
161      case GL_UNSIGNED_SHORT_5_6_5:
162         return sizeof(GLshort);
163      case GL_UNSIGNED_SHORT_5_6_5_REV:
164         return sizeof(GLshort);
165      case GL_UNSIGNED_SHORT_4_4_4_4:
166         return sizeof(GLshort);
167      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
168         return sizeof(GLshort);
169      case GL_UNSIGNED_SHORT_5_5_5_1:
170         return sizeof(GLshort);
171      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
172         return sizeof(GLshort);
173      case GL_UNSIGNED_INT_8_8_8_8:
174         return sizeof(GLuint);
175      case GL_UNSIGNED_INT_8_8_8_8_REV:
176         return sizeof(GLuint);
177      case GL_UNSIGNED_INT_10_10_10_2:
178         return sizeof(GLuint);
179      case GL_UNSIGNED_INT_2_10_10_10_REV:
180         return sizeof(GLuint);
181      default:
182         return -1;
183   }
184}
185
186
187
188/*
189 * Return the number of components in a GL enum pixel type.
190 * Return -1 if bad format.
191 */
192GLint gl_components_in_format( GLenum format )
193{
194   switch (format) {
195      case GL_COLOR_INDEX:
196      case GL_COLOR_INDEX1_EXT:
197      case GL_COLOR_INDEX2_EXT:
198      case GL_COLOR_INDEX4_EXT:
199      case GL_COLOR_INDEX8_EXT:
200      case GL_COLOR_INDEX12_EXT:
201      case GL_COLOR_INDEX16_EXT:
202      case GL_STENCIL_INDEX:
203      case GL_DEPTH_COMPONENT:
204      case GL_RED:
205      case GL_GREEN:
206      case GL_BLUE:
207      case GL_ALPHA:
208      case GL_LUMINANCE:
209         return 1;
210      case GL_LUMINANCE_ALPHA:
211	 return 2;
212      case GL_RGB:
213	 return 3;
214      case GL_RGBA:
215	 return 4;
216      case GL_BGR:
217	 return 3;
218      case GL_BGRA:
219	 return 4;
220      case GL_ABGR_EXT:
221         return 4;
222      default:
223         return -1;
224   }
225}
226
227
228/*
229 * Return bytes per pixel for given format and type
230 * Return -1 if bad format or type.
231 */
232GLint gl_bytes_per_pixel( GLenum format, GLenum type )
233{
234   GLint comps = gl_components_in_format( format );
235   if (comps < 0)
236      return -1;
237
238   switch (type) {
239      case GL_BITMAP:
240         return 0;  /* special case */
241      case GL_BYTE:
242      case GL_UNSIGNED_BYTE:
243         return comps * sizeof(GLubyte);
244      case GL_SHORT:
245      case GL_UNSIGNED_SHORT:
246         return comps * sizeof(GLshort);
247      case GL_INT:
248      case GL_UNSIGNED_INT:
249         return comps * sizeof(GLint);
250      case GL_FLOAT:
251         return comps * sizeof(GLfloat);
252      case GL_UNSIGNED_BYTE_3_3_2:
253      case GL_UNSIGNED_BYTE_2_3_3_REV:
254         if (format == GL_RGB || format == GL_BGR)
255            return sizeof(GLubyte);
256         else
257            return -1;  /* error */
258      case GL_UNSIGNED_SHORT_5_6_5:
259      case GL_UNSIGNED_SHORT_5_6_5_REV:
260         if (format == GL_RGB || format == GL_BGR)
261            return sizeof(GLshort);
262         else
263            return -1;  /* error */
264      case GL_UNSIGNED_SHORT_4_4_4_4:
265      case GL_UNSIGNED_SHORT_4_4_4_4_REV:
266      case GL_UNSIGNED_SHORT_5_5_5_1:
267      case GL_UNSIGNED_SHORT_1_5_5_5_REV:
268         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
269            return sizeof(GLushort);
270         else
271            return -1;
272      case GL_UNSIGNED_INT_8_8_8_8:
273      case GL_UNSIGNED_INT_8_8_8_8_REV:
274      case GL_UNSIGNED_INT_10_10_10_2:
275      case GL_UNSIGNED_INT_2_10_10_10_REV:
276         if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
277            return sizeof(GLuint);
278         else
279            return -1;
280      default:
281         return -1;
282   }
283}
284
285
286/*
287 * Test if the given pixel format and type are legal.
288 * Return GL_TRUE for legal, GL_FALSE for illegal.
289 */
290GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type )
291{
292   switch (format) {
293      case GL_COLOR_INDEX:
294      case GL_STENCIL_INDEX:
295         switch (type) {
296            case GL_BITMAP:
297            case GL_BYTE:
298            case GL_UNSIGNED_BYTE:
299            case GL_SHORT:
300            case GL_UNSIGNED_SHORT:
301            case GL_INT:
302            case GL_UNSIGNED_INT:
303            case GL_FLOAT:
304               return GL_TRUE;
305            default:
306               return GL_FALSE;
307         }
308      case GL_RED:
309      case GL_GREEN:
310      case GL_BLUE:
311      case GL_ALPHA:
312      case GL_LUMINANCE:
313      case GL_LUMINANCE_ALPHA:
314      case GL_DEPTH_COMPONENT:
315      case GL_BGR:
316         switch (type) {
317            case GL_BYTE:
318            case GL_UNSIGNED_BYTE:
319            case GL_SHORT:
320            case GL_UNSIGNED_SHORT:
321            case GL_INT:
322            case GL_UNSIGNED_INT:
323            case GL_FLOAT:
324               return GL_TRUE;
325            default:
326               return GL_FALSE;
327         }
328      case GL_RGB:
329         switch (type) {
330            case GL_BYTE:
331            case GL_UNSIGNED_BYTE:
332            case GL_SHORT:
333            case GL_UNSIGNED_SHORT:
334            case GL_INT:
335            case GL_UNSIGNED_INT:
336            case GL_FLOAT:
337            case GL_UNSIGNED_BYTE_3_3_2:
338            case GL_UNSIGNED_BYTE_2_3_3_REV:
339            case GL_UNSIGNED_SHORT_5_6_5:
340            case GL_UNSIGNED_SHORT_5_6_5_REV:
341               return GL_TRUE;
342            default:
343               return GL_FALSE;
344         }
345      case GL_RGBA:
346      case GL_BGRA:
347      case GL_ABGR_EXT:
348         switch (type) {
349            case GL_BYTE:
350            case GL_UNSIGNED_BYTE:
351            case GL_SHORT:
352            case GL_UNSIGNED_SHORT:
353            case GL_INT:
354            case GL_UNSIGNED_INT:
355            case GL_FLOAT:
356            case GL_UNSIGNED_SHORT_4_4_4_4:
357            case GL_UNSIGNED_SHORT_4_4_4_4_REV:
358            case GL_UNSIGNED_SHORT_5_5_5_1:
359            case GL_UNSIGNED_SHORT_1_5_5_5_REV:
360            case GL_UNSIGNED_INT_8_8_8_8:
361            case GL_UNSIGNED_INT_8_8_8_8_REV:
362            case GL_UNSIGNED_INT_10_10_10_2:
363            case GL_UNSIGNED_INT_2_10_10_10_REV:
364               return GL_TRUE;
365            default:
366               return GL_FALSE;
367         }
368      default:
369         ; /* fall-through */
370   }
371   return GL_FALSE;
372}
373
374
375
376/*
377 * Return the address of a pixel in an image (actually a volume).
378 * Pixel unpacking/packing parameters are observed according to 'packing'.
379 * Input:  image - start of image data
380 *         width, height - size of image
381 *         format - image format
382 *         type - pixel component type
383 *         packing - the pixelstore attributes
384 *         img - which image in the volume (0 for 1D or 2D images)
385 *         row, column - location of pixel in the image
386 * Return:  address of pixel at (image,row,column) in image or NULL if error.
387 */
388GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing,
389                                const GLvoid *image, GLsizei width,
390                                GLsizei height, GLenum format, GLenum type,
391                                GLint img, GLint row, GLint column )
392{
393   GLint alignment;        /* 1, 2 or 4 */
394   GLint pixels_per_row;
395   GLint rows_per_image;
396   GLint skiprows;
397   GLint skippixels;
398   GLint skipimages;       /* for 3-D volume images */
399   GLubyte *pixel_addr;
400
401   alignment = packing->Alignment;
402   if (packing->RowLength > 0) {
403      pixels_per_row = packing->RowLength;
404   }
405   else {
406      pixels_per_row = width;
407   }
408   if (packing->ImageHeight > 0) {
409      rows_per_image = packing->ImageHeight;
410   }
411   else {
412      rows_per_image = height;
413   }
414   skiprows = packing->SkipRows;
415   skippixels = packing->SkipPixels;
416   skipimages = packing->SkipImages;
417
418   if (type==GL_BITMAP) {
419      /* BITMAP data */
420      GLint comp_per_pixel;   /* components per pixel */
421      GLint bytes_per_comp;   /* bytes per component */
422      GLint bytes_per_row;
423      GLint bytes_per_image;
424
425      /* Compute bytes per component */
426      bytes_per_comp = gl_sizeof_packed_type( type );
427      if (bytes_per_comp<0) {
428         return NULL;
429      }
430
431      /* Compute number of components per pixel */
432      comp_per_pixel = gl_components_in_format( format );
433      if (comp_per_pixel<0 && type != GL_BITMAP) {
434         return NULL;
435      }
436
437      bytes_per_row = alignment
438                    * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
439
440      bytes_per_image = bytes_per_row * rows_per_image;
441
442      pixel_addr = (GLubyte *) image
443                 + (skipimages + img) * bytes_per_image
444                 + (skiprows + row) * bytes_per_row
445                 + (skippixels + column) / 8;
446   }
447   else {
448      /* Non-BITMAP data */
449      GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
450
451      bytes_per_pixel = gl_bytes_per_pixel( format, type );
452
453      /* The pixel type and format should have been error checked earlier */
454      assert(bytes_per_pixel > 0);
455
456      bytes_per_row = pixels_per_row * bytes_per_pixel;
457      remainder = bytes_per_row % alignment;
458      if (remainder > 0)
459         bytes_per_row += (alignment - remainder);
460
461      ASSERT(bytes_per_row % alignment == 0);
462
463      bytes_per_image = bytes_per_row * rows_per_image;
464
465      /* compute final pixel address */
466      pixel_addr = (GLubyte *) image
467                 + (skipimages + img) * bytes_per_image
468                 + (skiprows + row) * bytes_per_row
469                 + (skippixels + column) * bytes_per_pixel;
470   }
471
472   return (GLvoid *) pixel_addr;
473}
474
475
476
477/*
478 * Allocate a new gl_image.  All fields are initialized to zero.
479 */
480static struct gl_image *alloc_image( void )
481{
482   return (struct gl_image *) calloc(sizeof(struct gl_image), 1);
483}
484
485
486
487/*
488 * Allocate a new gl_image with the error flag set.
489 */
490static struct gl_image *alloc_error_image( GLint width, GLint height,
491                                           GLint depth, GLenum format,
492                                           GLenum type )
493{
494   struct gl_image *image = alloc_image();
495   if (image) {
496      image->Width = width;
497      image->Height = height;
498      image->Depth = depth;
499      image->Format = format;
500      image->Type = type;
501      image->ErrorFlag = GL_TRUE;
502   }
503   return image;
504}
505
506
507
508/*
509 * Free a gl_image.
510 */
511void gl_free_image( struct gl_image *image )
512{
513   if (image->Data) {
514      free(image->Data);
515   }
516   free(image);
517}
518
519
520
521/*
522 * Do error checking on an image.  If there's an error, register it and
523 * return GL_TRUE, else return GL_FALSE.
524 */
525GLboolean gl_image_error_test( GLcontext *ctx, const struct gl_image *image,
526                               const char *msg )
527{
528   if (!image) {
529      gl_error( ctx, GL_OUT_OF_MEMORY, msg );
530      return GL_TRUE;
531   }
532   if (image->Width <= 0 || image->Height <= 0 || image->Depth <= 0) {
533      gl_error( ctx, GL_INVALID_VALUE, msg );
534      return GL_TRUE;
535   }
536   else {
537      return GL_FALSE;
538   }
539}
540
541
542
543/*
544 * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats.
545 * Input:  type - datatype of src depth image
546 * Return pointer to a new gl_image structure.
547 *
548 * Notes:  if the source image type is GLushort then the gl_image will
549 * also store GLushorts.  If the src image type is GLuint then the gl_image
550 * will also store GLuints.  For all other src image types the gl_image
551 * will store GLfloats.  The integer cases can later be optimized.
552 */
553static struct gl_image *
554unpack_depth_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
555                    const GLvoid *pixels,
556                    const struct gl_pixelstore_attrib *packing)
557
558{
559   struct gl_image *image;
560   GLfloat *fDst;
561   GLushort *sDst;
562   GLuint *iDst;
563   GLint i, j;
564
565   image = alloc_image();
566   if (image) {
567      image->Width = width;
568      image->Height = height;
569      image->Depth = 1;
570      image->Components = 1;
571      image->Format = GL_DEPTH_COMPONENT;
572      if (type==GL_UNSIGNED_SHORT) {
573         image->Type = GL_UNSIGNED_SHORT;
574         image->Data = malloc( width * height * sizeof(GLushort));
575      }
576      else if (type==GL_UNSIGNED_INT) {
577         image->Type = GL_UNSIGNED_INT;
578         image->Data = malloc( width * height * sizeof(GLuint));
579      }
580      else {
581         image->Type = GL_FLOAT;
582         image->Data = malloc( width * height * sizeof(GLfloat));
583      }
584      image->RefCount = 0;
585      if (!image->Data)
586         return image;
587   }
588   else {
589      return NULL;
590   }
591
592   fDst = (GLfloat *) image->Data;
593   sDst = (GLushort *) image->Data;
594   iDst = (GLuint *) image->Data;
595
596   for (i=0;i<height;i++) {
597      GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
598                                            width, height,
599                                            GL_DEPTH_COMPONENT, type,
600                                            0, i, 0 );
601      if (!src) {
602         return image;
603      }
604
605      switch (type) {
606         case GL_BYTE:
607            assert(image->Type == GL_FLOAT);
608            for (j=0; j<width; j++) {
609               *fDst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
610            }
611            break;
612         case GL_UNSIGNED_BYTE:
613            assert(image->Type == GL_FLOAT);
614            for (j=0; j<width; j++) {
615               *fDst++ = UBYTE_TO_FLOAT(((GLubyte*)src)[j]);
616            }
617            break;
618         case GL_UNSIGNED_SHORT:
619            assert(image->Type == GL_UNSIGNED_SHORT);
620            MEMCPY( sDst, src, width * sizeof(GLushort) );
621            if (packing->SwapBytes) {
622               gl_swap2( sDst, width );
623            }
624            sDst += width;
625            break;
626         case GL_SHORT:
627            assert(image->Type == GL_FLOAT);
628            if (packing->SwapBytes) {
629               for (j=0;j<width;j++) {
630                  GLshort value = ((GLshort*)src)[j];
631                  value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
632                  *fDst++ = SHORT_TO_FLOAT(value);
633               }
634            }
635            else {
636               for (j=0;j<width;j++) {
637                  *fDst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
638               }
639            }
640            break;
641         case GL_INT:
642            assert(image->Type == GL_FLOAT);
643            if (packing->SwapBytes) {
644               for (j=0;j<width;j++) {
645                  GLint value = ((GLint*)src)[j];
646                  value = ((value >> 24) & 0x000000ff) |
647                          ((value >> 8)  & 0x0000ff00) |
648                          ((value << 8)  & 0x00ff0000) |
649                          ((value << 24) & 0xff000000);
650                  *fDst++ = INT_TO_FLOAT(value);
651               }
652            }
653            else {
654               for (j=0;j<width;j++) {
655                  *fDst++ = INT_TO_FLOAT(((GLint*)src)[j]);
656               }
657            }
658            iDst += width;
659            break;
660         case GL_UNSIGNED_INT:
661            assert(image->Type == GL_UNSIGNED_INT);
662            MEMCPY( iDst, src, width * sizeof(GLuint) );
663            if (packing->SwapBytes) {
664               gl_swap4( iDst, width );
665            }
666            iDst += width;
667            break;
668         case GL_FLOAT:
669            assert(image->Type == GL_FLOAT);
670            MEMCPY( fDst, src, width * sizeof(GLfloat) );
671            if (packing->SwapBytes) {
672               gl_swap4( (GLuint*) fDst, width );
673            }
674            fDst += width;
675            break;
676         default:
677            gl_problem(ctx, "unpack_depth_image type" );
678            return image;
679      }
680   }
681
682   return image;
683}
684
685
686
687/*
688 * Unpack a stencil image.  Store as GLubytes in a gl_image structure.
689 * Return:  pointer to new gl_image structure.
690 */
691static struct gl_image *
692unpack_stencil_image( GLcontext *ctx, GLenum type, GLint width, GLint height,
693                      const GLvoid *pixels,
694                      const struct gl_pixelstore_attrib *packing )
695{
696   struct gl_image *image;
697   GLubyte *dst;
698   GLint i, j;
699
700   assert(sizeof(GLstencil) == sizeof(GLubyte));
701
702   image = alloc_image();
703   if (image) {
704      image->Width = width;
705      image->Height = height;
706      image->Depth = 1;
707      image->Components = 1;
708      image->Format = GL_STENCIL_INDEX;
709      image->Type = GL_UNSIGNED_BYTE;
710      image->Data = malloc( width * height * sizeof(GLubyte));
711      image->RefCount = 0;
712      if (!image->Data)
713         return image;
714   }
715   else {
716      return NULL;
717   }
718
719   dst = (GLubyte *) image->Data;
720
721   for (i=0;i<height;i++) {
722      GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
723                                            width, height,
724                                            GL_STENCIL_INDEX, type,
725                                            0, i, 0 );
726      if (!src) {
727         return image;
728      }
729
730      switch (type) {
731         case GL_UNSIGNED_BYTE:
732         case GL_BYTE:
733            MEMCPY( dst, src, width * sizeof(GLubyte) );
734            dst += width * sizeof(GLubyte);
735            break;
736         case GL_UNSIGNED_SHORT:
737         case GL_SHORT:
738            if (packing->SwapBytes) {
739               /* grab upper byte */
740               for (j=0; j < width; j++) {
741                  *dst++ = (((GLushort*)src)[j] & 0xff00) >> 8;
742               }
743            }
744            else {
745               for (j=0; j < width; j++) {
746                  *dst++ = (((GLushort*)src)[j]) & 0xff;
747               }
748            }
749            break;
750         case GL_INT:
751            if (packing->SwapBytes) {
752               /* grab upper byte */
753               for (j=0; j < width; j++) {
754                  *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
755               }
756            }
757            else {
758               for (j=0; j < width; j++) {
759                  *dst++ = (((GLuint*)src)[j]) & 0xff;
760               }
761            }
762            break;
763         case GL_UNSIGNED_INT:
764            if (packing->SwapBytes) {
765               /* grab upper byte */
766               for (j=0; j < width; j++) {
767                  *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8;
768               }
769            }
770            else {
771               for (j=0; j < width; j++) {
772                  *dst++ = (((GLuint*)src)[j]) & 0xff;
773               }
774            }
775            break;
776         case GL_FLOAT:
777            if (packing->SwapBytes) {
778               for (j=0; j < width; j++) {
779                  GLfloat fvalue;
780                  GLint value = ((GLuint*)src)[j];
781                  value = ((value & 0xff000000) >> 24)
782                     | ((value & 0x00ff0000) >> 8)
783                     | ((value & 0x0000ff00) << 8)
784                     | ((value & 0x000000ff) << 24);
785                  fvalue = *((GLfloat*) &value);
786                  *dst++ = ((GLint) fvalue) & 0xff;
787               }
788            }
789            else {
790               for (j=0; j < width; j++) {
791                  GLfloat fvalue = ((GLfloat *)src)[j];
792                  *dst++ = ((GLint) fvalue) & 0xff;
793               }
794            }
795            break;
796         default:
797            gl_problem(ctx, "unpack_stencil_image type" );
798            return image;
799      }
800   }
801
802   return image;
803}
804
805
806
807/*
808 * Unpack a bitmap, return a new gl_image struct.
809 */
810static struct gl_image *
811unpack_bitmap( GLcontext *ctx, GLenum format, GLint width, GLint height,
812               const GLvoid *pixels,
813               const struct gl_pixelstore_attrib *packing )
814{
815   struct gl_image *image;
816   GLint bytes, i, width_in_bytes;
817   GLubyte *buffer, *dst;
818
819   assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX);
820
821   /* Alloc dest storage */
822   bytes = ((width+7)/8 * height);
823   if (bytes>0 && pixels!=NULL) {
824      buffer = (GLubyte *) malloc( bytes );
825      if (!buffer) {
826         return NULL;
827      }
828      /* Copy/unpack pixel data to buffer */
829      width_in_bytes = CEILING( width, 8 );
830      dst = buffer;
831      for (i=0; i<height; i++) {
832         GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
833                                               width, height,
834                                               GL_COLOR_INDEX, GL_BITMAP,
835                                               0, i, 0 );
836         if (!src) {
837            free(buffer);
838            return NULL;
839         }
840         MEMCPY( dst, src, width_in_bytes );
841         dst += width_in_bytes;
842      }
843      /* Bit flipping */
844      if (packing->LsbFirst) {
845         gl_flip_bytes( buffer, bytes );
846      }
847   }
848   else {
849      /* a 'null' bitmap */
850      buffer = NULL;
851   }
852
853   image = alloc_image();
854   if (image) {
855      image->Width = width;
856      image->Height = height;
857      image->Depth = 1;
858      image->Components = 0;
859      image->Format = format;
860      image->Type = GL_BITMAP;
861      image->Data = buffer;
862      image->RefCount = 0;
863   }
864   else {
865      free( buffer );
866      return NULL;
867   }
868
869   return image;
870}
871
872
873
874/*
875 * Unpack a 32x32 pixel polygon stipple from user memory using the
876 * current pixel unpack settings.
877 */
878void gl_unpack_polygon_stipple( const GLcontext *ctx,
879                                const GLubyte *pattern, GLuint dest[32] )
880{
881   GLint i;
882   for (i = 0; i < 32; i++) {
883      GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack, pattern,
884                                  32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
885      dest[i] = (src[0] << 24)
886              | (src[1] << 16)
887              | (src[2] <<  8)
888              | (src[3]      );
889   }
890
891   /* Bit flipping within each byte */
892   if (ctx->Unpack.LsbFirst) {
893      gl_flip_bytes( (GLubyte *) dest, 32 * 4 );
894   }
895}
896
897
898
899/*
900 * Pack polygon stipple into user memory given current pixel packing
901 * settings.
902 */
903void gl_pack_polygon_stipple( const GLcontext *ctx,
904                              const GLuint pattern[32],
905                              GLubyte *dest )
906{
907   GLint i;
908   for (i = 0; i < 32; i++) {
909      GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image( &ctx->Pack, dest,
910                                  32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 );
911      dst[0] = (pattern[i] >> 24) & 0xff;
912      dst[1] = (pattern[i] >> 16) & 0xff;
913      dst[2] = (pattern[i] >>  8) & 0xff;
914      dst[3] = (pattern[i]      ) & 0xff;
915
916      /* Bit flipping within each byte */
917      if (ctx->Pack.LsbFirst) {
918         gl_flip_bytes( (GLubyte *) dst, 4 );
919      }
920   }
921}
922
923
924
925/*
926 * Unpack an RGBA or CI image and store it as unsigned bytes
927 */
928static struct gl_image *
929unpack_ubyte_image( GLcontext *ctx, GLint width, GLint height,
930                    GLint depth, GLenum format, const GLvoid *pixels,
931                    const struct gl_pixelstore_attrib *packing )
932{
933   struct gl_image *image;
934   GLint width_in_bytes;
935   GLint components;
936   GLubyte *buffer, *dst;
937   GLint i, d;
938
939   components = gl_components_in_format( format );
940
941   width_in_bytes = width * components * sizeof(GLubyte);
942   buffer = (GLubyte *) malloc( height * width_in_bytes * depth );
943   if (!buffer) {
944      return NULL;
945   }
946
947   /* Copy/unpack pixel data to buffer */
948   dst = buffer;
949   for (d=0; d<depth; d++ ) {
950      for (i=0;i<height;i++) {
951         GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( packing,
952                       pixels, width, height, format, GL_UNSIGNED_BYTE,
953                       d, i, 0 );
954         if (!src) {
955            free(buffer);
956            return NULL;
957         }
958         MEMCPY( dst, src, width_in_bytes );
959         dst += width_in_bytes;
960      }
961   }
962
963   if (format == GL_BGR) {
964      /* swap order of every ubyte triplet from BGR to RGB */
965      for (i=0; i<width*height; i++) {
966         GLubyte b = buffer[i*3+0];
967         GLubyte r = buffer[i*3+2];
968         buffer[i*3+0] = r;
969         buffer[i*3+2] = b;
970      }
971   }
972   else if (format == GL_BGRA) {
973      /* swap order of every ubyte quadruplet from BGRA to RGBA */
974      for (i=0; i<width*height; i++) {
975         GLubyte b = buffer[i*4+0];
976         GLubyte r = buffer[i*4+2];
977         buffer[i*4+0] = r;
978         buffer[i*4+2] = b;
979      }
980   }
981   else if (format == GL_ABGR_EXT) {
982      /* swap order of every ubyte quadruplet from ABGR to RGBA */
983      for (i=0; i<width*height; i++) {
984         GLubyte a = buffer[i*4+0];
985         GLubyte b = buffer[i*4+1];
986         GLubyte g = buffer[i*4+2];
987         GLubyte r = buffer[i*4+3];
988         buffer[i*4+0] = r;
989         buffer[i*4+1] = g;
990         buffer[i*4+2] = b;
991         buffer[i*4+3] = a;
992      }
993   }
994
995
996   image = alloc_image();
997   if (image) {
998      image->Width = width;
999      image->Height = height;
1000      image->Depth = depth;
1001      image->Components = components;
1002      if (format == GL_BGR)
1003         image->Format = GL_RGB;
1004      else if (format == GL_BGRA)
1005         image->Format = GL_RGBA;
1006      else if (format == GL_ABGR_EXT)
1007         image->Format = GL_RGBA;
1008      else
1009         image->Format = format;
1010      image->Type = GL_UNSIGNED_BYTE;
1011      image->Data = buffer;
1012      image->RefCount = 0;
1013   }
1014   else {
1015      free( buffer );
1016   }
1017
1018   return image;
1019}
1020
1021
1022
1023/*
1024 * Unpack a color image storing image as GLfloats
1025 */
1026static struct gl_image *
1027unpack_float_image( GLcontext *ctx, GLint width, GLint height, GLint depth,
1028                    GLenum format, GLenum type, const GLvoid *pixels,
1029                    const struct gl_pixelstore_attrib *packing )
1030{
1031   struct gl_image *image;
1032   GLfloat *dst;
1033   GLint elems_per_row;
1034   GLint components;
1035   GLint i, j, d;
1036   GLboolean normalize;
1037
1038   assert(type != GL_BITMAP);
1039
1040   components = gl_components_in_format( format );
1041   assert(components > 0);  /* should have been caught earlier */
1042
1043   if (!gl_is_legal_format_and_type( format, type )) {
1044      /* bad pixel type for format, make dummy image */
1045      image = alloc_image();
1046      if (image) {
1047         image->Width = width;
1048         image->Height = height;
1049         image->Depth = depth;
1050         image->Components = components;
1051         image->Format = format;
1052         image->Type = type;
1053         image->Data = NULL;
1054         image->RefCount = 0;
1055      }
1056      return image;
1057   }
1058
1059   elems_per_row = width * components;
1060
1061   image = alloc_image();
1062   if (image) {
1063      image->Width = width;
1064      image->Height = height;
1065      image->Depth = depth;
1066      image->Components = components;
1067      if (format == GL_BGR)
1068         image->Format = GL_RGB;
1069      else if (format == GL_BGRA)
1070         image->Format = GL_RGBA;
1071      else if (format == GL_ABGR_EXT)
1072         image->Format = GL_RGBA;
1073      else
1074         image->Format = format;
1075      image->Type = GL_FLOAT;
1076      image->Data = malloc( elems_per_row * height * depth * sizeof(GLfloat));
1077      image->RefCount = 0;
1078      if (!image->Data)
1079         return image;
1080   }
1081   else {
1082      return NULL;
1083   }
1084
1085   normalize = (format != GL_COLOR_INDEX) && (format != GL_STENCIL_INDEX);
1086
1087   dst = (GLfloat *) image->Data;
1088
1089   for (d=0; d<depth; d++) {
1090      for (i=0;i<height;i++) {
1091         GLvoid *src = gl_pixel_addr_in_image( packing, pixels,
1092                                               width, height,
1093                                               format, type,
1094                                               d, i, 0 );
1095         if (!src) {
1096            return image;
1097         }
1098
1099         switch (type) {
1100            case GL_UNSIGNED_BYTE:
1101               {
1102                  GLubyte *ubsrc = (GLubyte *) src;
1103                  if (normalize) {
1104                     for (j=0;j<elems_per_row;j++) {
1105                        *dst++ = UBYTE_TO_FLOAT(ubsrc[j]);
1106                     }
1107                  }
1108                  else {
1109                     for (j=0;j<elems_per_row;j++) {
1110                        *dst++ = (GLfloat) ubsrc[j];
1111                     }
1112                  }
1113               }
1114               break;
1115            case GL_BYTE:
1116               if (normalize) {
1117                  for (j=0;j<elems_per_row;j++) {
1118                     *dst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]);
1119                  }
1120               }
1121               else {
1122                  for (j=0;j<elems_per_row;j++) {
1123                     *dst++ = (GLfloat) ((GLbyte*)src)[j];
1124                  }
1125               }
1126               break;
1127            case GL_UNSIGNED_SHORT:
1128               if (packing->SwapBytes) {
1129                  for (j=0;j<elems_per_row;j++) {
1130                     GLushort value = ((GLushort*)src)[j];
1131                     value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
1132                     if (normalize) {
1133                        *dst++ = USHORT_TO_FLOAT(value);
1134                     }
1135                     else {
1136                        *dst++ = (GLfloat) value;
1137                     }
1138                  }
1139               }
1140               else {
1141                  if (normalize) {
1142                     for (j=0;j<elems_per_row;j++) {
1143                        *dst++ = USHORT_TO_FLOAT(((GLushort*)src)[j]);
1144                     }
1145                  }
1146                  else {
1147                     for (j=0;j<elems_per_row;j++) {
1148                        *dst++ = (GLfloat) ((GLushort*)src)[j];
1149                     }
1150                  }
1151               }
1152               break;
1153            case GL_SHORT:
1154               if (packing->SwapBytes) {
1155                  for (j=0;j<elems_per_row;j++) {
1156                     GLshort value = ((GLshort*)src)[j];
1157                     value = ((value >> 8) & 0xff) | ((value&0xff) << 8);
1158                     if (normalize) {
1159                        *dst++ = SHORT_TO_FLOAT(value);
1160                     }
1161                     else {
1162                        *dst++ = (GLfloat) value;
1163                     }
1164                  }
1165               }
1166               else {
1167                  if (normalize) {
1168                     for (j=0;j<elems_per_row;j++) {
1169                        *dst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]);
1170                     }
1171                  }
1172                  else {
1173                     for (j=0;j<elems_per_row;j++) {
1174                        *dst++ = (GLfloat) ((GLshort*)src)[j];
1175                     }
1176                  }
1177               }
1178               break;
1179            case GL_UNSIGNED_INT:
1180               if (packing->SwapBytes) {
1181                  GLuint value;
1182                  for (j=0;j<elems_per_row;j++) {
1183                     value = ((GLuint*)src)[j];
1184                     value = ((value & 0xff000000) >> 24)
1185                           | ((value & 0x00ff0000) >> 8)
1186                           | ((value & 0x0000ff00) << 8)
1187                           | ((value & 0x000000ff) << 24);
1188                     if (normalize) {
1189                        *dst++ = UINT_TO_FLOAT(value);
1190                     }
1191                     else {
1192                        *dst++ = (GLfloat) value;
1193                     }
1194                  }
1195               }
1196               else {
1197                  if (normalize) {
1198                     for (j=0;j<elems_per_row;j++) {
1199                        *dst++ = UINT_TO_FLOAT(((GLuint*)src)[j]);
1200                     }
1201                  }
1202                  else {
1203                     for (j=0;j<elems_per_row;j++) {
1204                        *dst++ = (GLfloat) ((GLuint*)src)[j];
1205                     }
1206                  }
1207               }
1208               break;
1209            case GL_INT:
1210               if (packing->SwapBytes) {
1211                  GLint value;
1212                  for (j=0;j<elems_per_row;j++) {
1213                     value = ((GLint*)src)[j];
1214                     value = ((value & 0xff000000) >> 24)
1215                           | ((value & 0x00ff0000) >> 8)
1216                           | ((value & 0x0000ff00) << 8)
1217                           | ((value & 0x000000ff) << 24);
1218                     if (normalize) {
1219                        *dst++ = INT_TO_FLOAT(value);
1220                     }
1221                     else {
1222                        *dst++ = (GLfloat) value;
1223                     }
1224                  }
1225               }
1226               else {
1227                  if (normalize) {
1228                     for (j=0;j<elems_per_row;j++) {
1229                        *dst++ = INT_TO_FLOAT(((GLint*)src)[j]);
1230                     }
1231                  }
1232                  else {
1233                     for (j=0;j<elems_per_row;j++) {
1234                        *dst++ = (GLfloat) ((GLint*)src)[j];
1235                     }
1236                  }
1237               }
1238               break;
1239            case GL_FLOAT:
1240               if (packing->SwapBytes) {
1241                  GLint value;
1242                  for (j=0;j<elems_per_row;j++) {
1243                     value = ((GLuint*)src)[j];
1244                     value = ((value & 0xff000000) >> 24)
1245                           | ((value & 0x00ff0000) >> 8)
1246                           | ((value & 0x0000ff00) << 8)
1247                           | ((value & 0x000000ff) << 24);
1248                     *dst++ = *((GLfloat*) &value);
1249                  }
1250               }
1251               else {
1252                  MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) );
1253                  dst += elems_per_row;
1254               }
1255               break;
1256            case GL_UNSIGNED_BYTE_3_3_2:
1257               {
1258                  GLubyte *ubsrc = (GLubyte *) src;
1259                  for (j=0;j<width;j++) {
1260                     GLubyte p = ubsrc[j];
1261                     *dst++ = ((p >> 5)      ) * (1.0F / 7.0F); /* red */
1262                     *dst++ = ((p >> 2) & 0x7) * (1.0F / 7.0F); /* green */
1263                     *dst++ = ((p     ) & 0x3) * (1.0F / 3.0F); /* blue */
1264                  }
1265               }
1266               break;
1267            case GL_UNSIGNED_BYTE_2_3_3_REV:
1268               {
1269                  GLubyte *ubsrc = (GLubyte *) src;
1270                  for (j=0;j<width;j++) {
1271                     GLubyte p = ubsrc[j];
1272                     *dst++ = ((p     ) & 0x7) * (1.0F / 7.0F); /* red */
1273                     *dst++ = ((p >> 3) & 0x7) * (1.0F / 7.0F); /* green */
1274                     *dst++ = ((p >> 6)      ) * (1.0F / 3.0F); /* blue */
1275                  }
1276               }
1277               break;
1278            case GL_UNSIGNED_SHORT_5_6_5:
1279               {
1280                  GLushort *ussrc = (GLushort *) src;
1281                  for (j=0;j<width;j++) {
1282                     GLushort p = ussrc[j];
1283                     *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* red */
1284                     *dst++ = ((p >>  5) & 0x3f) * (1.0F / 63.0F); /* green */
1285                     *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* blue */
1286                  }
1287               }
1288               break;
1289            case GL_UNSIGNED_SHORT_5_6_5_REV:
1290               {
1291                  GLushort *ussrc = (GLushort *) src;
1292                  for (j=0;j<width;j++) {
1293                     GLushort p = ussrc[j];
1294                     *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* red */
1295                     *dst++ = ((p >>  5) & 0x3f) * (1.0F / 63.0F); /* green */
1296                     *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* blue */
1297                  }
1298               }
1299               break;
1300	    case GL_UNSIGNED_SHORT_4_4_4_4:
1301               {
1302                  GLushort *ussrc = (GLushort *) src;
1303                  for (j=0;j<width;j++) {
1304                     GLushort p = ussrc[j];
1305                     *dst++ = ((p >> 12)      ) * (1.0F / 15.0F); /* red */
1306                     *dst++ = ((p >>  8) & 0xf) * (1.0F / 15.0F); /* green */
1307                     *dst++ = ((p >>  4) & 0xf) * (1.0F / 15.0F); /* blue */
1308                     *dst++ = ((p      ) & 0xf) * (1.0F / 15.0F); /* alpha */
1309                  }
1310               }
1311               break;
1312	    case GL_UNSIGNED_SHORT_4_4_4_4_REV:
1313               {
1314                  GLushort *ussrc = (GLushort *) src;
1315                  for (j=0;j<width;j++) {
1316                     GLushort p = ussrc[j];
1317                     *dst++ = ((p      ) & 0xf) * (1.0F / 15.0F); /* red */
1318                     *dst++ = ((p >>  4) & 0xf) * (1.0F / 15.0F); /* green */
1319                     *dst++ = ((p >>  8) & 0xf) * (1.0F / 15.0F); /* blue */
1320                     *dst++ = ((p >> 12)      ) * (1.0F / 15.0F); /* alpha */
1321                  }
1322               }
1323               break;
1324	    case GL_UNSIGNED_SHORT_5_5_5_1:
1325               {
1326                  GLushort *ussrc = (GLushort *) src;
1327                  for (j=0;j<width;j++) {
1328                     GLushort p = ussrc[j];
1329                     *dst++ = ((p >> 11)       ) * (1.0F / 31.0F); /* red */
1330                     *dst++ = ((p >>  6) & 0x1f) * (1.0F / 31.0F); /* green */
1331                     *dst++ = ((p >>  1) & 0x1f) * (1.0F / 31.0F); /* blue */
1332                     *dst++ = ((p      ) & 0x1)  * (1.0F /  1.0F); /* alpha */
1333                  }
1334               }
1335               break;
1336	    case GL_UNSIGNED_SHORT_1_5_5_5_REV:
1337               {
1338                  GLushort *ussrc = (GLushort *) src;
1339                  for (j=0;j<width;j++) {
1340                     GLushort p = ussrc[j];
1341                     *dst++ = ((p      ) & 0x1f) * (1.0F / 31.0F); /* red */
1342                     *dst++ = ((p >>  5) & 0x1f) * (1.0F / 31.0F); /* green */
1343                     *dst++ = ((p >> 10) & 0x1f) * (1.0F / 31.0F); /* blue */
1344                     *dst++ = ((p >> 15)       ) * (1.0F /  1.0F); /* alpha */
1345                  }
1346               }
1347               break;
1348	    case GL_UNSIGNED_INT_8_8_8_8:
1349               {
1350                  GLuint *uisrc = (GLuint *) src;
1351                  for (j=0;j<width;j++) {
1352                     GLuint p = uisrc[j];
1353                     *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
1354                     *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1355                     *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
1356                     *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
1357                  }
1358               }
1359               break;
1360	    case GL_UNSIGNED_INT_8_8_8_8_REV:
1361               {
1362                  GLuint *uisrc = (GLuint *) src;
1363                  for (j=0;j<width;j++) {
1364                     GLuint p = uisrc[j];
1365                     *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p      ) & 0xff);
1366                     *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >>  8) & 0xff);
1367                     *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff);
1368                     *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24)       );
1369                  }
1370               }
1371               break;
1372	    case GL_UNSIGNED_INT_10_10_10_2:
1373               {
1374                  GLuint *uisrc = (GLuint *) src;
1375                  for (j=0;j<width;j++) {
1376                     GLuint p = uisrc[j];
1377                     *dst++ = ((p >> 22)        ) * (1.0F / 1023.0F); /* r */
1378                     *dst++ = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F); /* g */
1379                     *dst++ = ((p >>  2) & 0x3ff) * (1.0F / 1023.0F); /* b */
1380                     *dst++ = ((p      ) & 0x3  ) * (1.0F /    3.0F); /* a */
1381                  }
1382               }
1383               break;
1384	    case GL_UNSIGNED_INT_2_10_10_10_REV:
1385               {
1386                  GLuint *uisrc = (GLuint *) src;
1387                  for (j=0;j<width;j++) {
1388                     GLuint p = uisrc[j];
1389                     *dst++ = ((p      ) & 0x3ff) * (1.0F / 1023.0F); /* r*/
1390                     *dst++ = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F); /* g */
1391                     *dst++ = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F); /* b */
1392                     *dst++ = ((p >> 30)        ) * (1.0F /    3.0F); /* a */
1393                  }
1394               }
1395               break;
1396            default:
1397               gl_problem(ctx, "unpack_float_image type" );
1398               return image;
1399         }
1400      }
1401   }
1402
1403   if (format == GL_BGR) {
1404      /* swap order of every float triplet from BGR to RGBA */
1405      GLfloat *buffer = (GLfloat *) image->Data;
1406      for (i=0; i<width*height*depth; i++) {
1407         GLfloat b = buffer[i*3+0];
1408         GLfloat r = buffer[i*3+2];
1409         buffer[i*3+0] = r;
1410         buffer[i*3+2] = b;
1411      }
1412   }
1413   else if (format == GL_BGRA) {
1414      /* swap order of every float quadruplet from BGRA to RGBA */
1415      GLfloat *buffer = (GLfloat *) image->Data;
1416      for (i=0; i<width*height*depth; i++) {
1417         GLfloat b = buffer[i*4+0];
1418         GLfloat r = buffer[i*4+2];
1419         buffer[i*4+0] = r;
1420         buffer[i*4+2] = b;
1421      }
1422   }
1423   else if (format == GL_ABGR_EXT) {
1424      /* swap order of every float quadruplet from ABGR to RGBA */
1425      GLfloat *buffer = (GLfloat *) image->Data;
1426      for (i=0; i<width*height*depth; i++) {
1427         GLfloat a = buffer[i*4+0];
1428         GLfloat b = buffer[i*4+1];
1429         GLfloat g = buffer[i*4+2];
1430         GLfloat r = buffer[i*4+3];
1431         buffer[i*4+0] = r;
1432         buffer[i*4+1] = g;
1433         buffer[i*4+2] = b;
1434         buffer[i*4+3] = a;
1435      }
1436   }
1437
1438   return image;
1439}
1440
1441
1442
1443/*
1444 * Unpack a bitmap image, using current glPixelStore parameters,
1445 * making a new gl_image.
1446 */
1447struct gl_image *gl_unpack_bitmap( GLcontext *ctx,
1448                                   GLsizei width, GLsizei height,
1449                                   const GLubyte *bitmap,
1450                                   const struct gl_pixelstore_attrib *packing )
1451{
1452   return gl_unpack_image( ctx, width, height,
1453                           GL_COLOR_INDEX, GL_BITMAP, bitmap, packing );
1454}
1455
1456
1457
1458/*
1459 * Unpack a 2-D image from user's buffer.  Return pointer to new
1460 * gl_image struct.
1461 *
1462 * Input:  width, height - size in pixels
1463 *         format - format of incoming pixel data
1464 *         type - datatype of incoming pixel data
1465 *         pixels - pointer to unpacked image in user buffer
1466 */
1467struct gl_image *gl_unpack_image( GLcontext *ctx,
1468                                  GLint width, GLint height,
1469                                  GLenum format, GLenum type,
1470                                  const GLvoid *pixels,
1471                                  const struct gl_pixelstore_attrib *packing )
1472{
1473   return gl_unpack_image3D( ctx, width, height, 1,
1474                             format, type, pixels, packing );
1475}
1476
1477
1478
1479/*
1480 * Unpack a 1, 2 or 3-D image from user-supplied address, returning a
1481 * pointer to a new gl_image struct.
1482 * This function is always called by a higher-level unpack function such
1483 * as gl_unpack_texsubimage() or gl_unpack_bitmap().
1484 *
1485 * Input:  width, height, depth - size in pixels
1486 *         format - format of incoming pixel data
1487 *         type - datatype of incoming pixel data
1488 *         pixels - pointer to unpacked image.
1489 */
1490struct gl_image *gl_unpack_image3D( GLcontext *ctx,
1491                                    GLint width, GLint height, GLint depth,
1492                                    GLenum format, GLenum type,
1493                                    const GLvoid *pixels,
1494                                    const struct gl_pixelstore_attrib *packing)
1495{
1496   if (width <= 0 || height <= 0 || depth <= 0) {
1497      return alloc_error_image(width, height, depth, format, type);
1498   }
1499
1500   if (type==GL_BITMAP) {
1501      if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) {
1502         return alloc_error_image(width, height, depth, format, type);
1503      }
1504      else {
1505         return unpack_bitmap( ctx, format, width, height, pixels, packing );
1506      }
1507   }
1508   else if (format==GL_DEPTH_COMPONENT) {
1509      /* TODO: pack as GLdepth values (GLushort or GLuint) */
1510      return unpack_depth_image( ctx, type, width, height, pixels, packing );
1511   }
1512   else if (format==GL_STENCIL_INDEX) {
1513      /* TODO: pack as GLstencil (GLubyte or GLushort) */
1514      return unpack_stencil_image( ctx, type, width, height, pixels, packing );
1515   }
1516   else if (type==GL_UNSIGNED_BYTE) {
1517      /* upack, convert to GLubytes */
1518      return unpack_ubyte_image( ctx, width, height, depth, format, pixels, packing );
1519   }
1520   else {
1521      /* upack, convert to floats */
1522      return unpack_float_image( ctx, width, height, depth,
1523                                 format, type, pixels, packing );
1524   }
1525
1526   /* never get here */
1527   /*return NULL;*/
1528}
1529
1530
1531/*
1532 * Apply pixel-transfer operations (scale, bias, mapping) to a single row
1533 * of a gl_image.  Put resulting color components into result array.
1534 */
1535void gl_scale_bias_map_image_data( const GLcontext *ctx,
1536                                   const struct gl_image *image,
1537                                   GLint row, GLubyte result[] )
1538{
1539   GLint start, i;
1540
1541   assert(ctx);
1542   assert(image);
1543   assert(result);
1544   assert(row >= 0);
1545
1546   start = row * image->Width * image->Components;
1547
1548   for (i=0; i < image->Width; i++) {
1549      GLint pos = start+i;
1550      GLfloat red, green, blue, alpha;
1551      if (image->Type == GL_UNSIGNED_BYTE) {
1552         const GLubyte *data = (GLubyte *) image->Data;
1553         switch (image->Format) {
1554            case GL_RED:
1555               red   = data[pos] * (1.0F/255.0F);
1556               green = 0;
1557               blue  = 0;
1558               alpha = 0;
1559               break;
1560            case GL_RGB:
1561               red   = data[pos*3+0] * (1.0F/255.0F);
1562               green = data[pos*3+1] * (1.0F/255.0F);
1563               blue  = data[pos*3+2] * (1.0F/255.0F);
1564               alpha = 0;
1565               break;
1566            default:
1567               gl_problem(ctx, "bad image format in gl_scale...image_data");
1568               return;
1569         }
1570      }
1571      else if (image->Type == GL_FLOAT) {
1572         const GLubyte *data = (GLubyte *) image->Data;
1573         switch (image->Format) {
1574            case GL_RED:
1575               red   = data[pos];
1576               green = 0;
1577               blue  = 0;
1578               alpha = 0;
1579               break;
1580            case GL_RGB:
1581               red   = data[pos*3+0];
1582               green = data[pos*3+1];
1583               blue  = data[pos*3+2];
1584               alpha = 0;
1585               break;
1586            default:
1587               gl_problem(ctx, "bad image format in gl_scale...image_data");
1588               return;
1589         }
1590      }
1591      else {
1592         gl_problem(ctx, "Bad image type in gl_scale_...image_data");
1593         return;
1594      }
1595
1596      assert(red   >= 0.0 && red   <= 1.0);
1597      assert(green >= 0.0 && green <= 1.0);
1598      assert(blue  >= 0.0 && blue  <= 1.0);
1599      assert(alpha >= 0.0 && alpha <= 1.0);
1600
1601      /*
1602      if (scale or bias) {
1603
1604
1605      }
1606      if (mapping) {
1607
1608      }
1609      */
1610
1611      result[i*4+0] = (GLubyte) (red   * 255.0);
1612      result[i*4+1] = (GLubyte) (green * 255.0);
1613      result[i*4+2] = (GLubyte) (blue  * 255.0);
1614      result[i*4+3] = (GLubyte) (alpha * 255.0);
1615   }
1616}
1617
1618
1619
1620/*
1621 * Pack the given RGBA span into client memory at 'dest' address
1622 * in the given pixel format and type.
1623 * Optionally apply the enabled pixel transfer ops.
1624 * Pack into memory using the given packing params struct.
1625 * This is used by glReadPixels and glGetTexImage?D()
1626 * Input:  ctx - the context
1627 *         n - number of pixels in the span
1628 *         rgba - the pixels
1629 *         format - dest packing format
1630 *         type - dest packing datatype
1631 *         destination - destination packing address
1632 *         packing - pixel packing parameters
1633 *         applyTransferOps - apply scale/bias/lookup-table ops?
1634 */
1635void gl_pack_rgba_span( const GLcontext *ctx,
1636                        GLuint n, CONST GLubyte rgba[][4],
1637                        GLenum format, GLenum type, GLvoid *destination,
1638                        const struct gl_pixelstore_attrib *packing,
1639                        GLboolean applyTransferOps )
1640{
1641   /* Test for optimized case first */
1642   if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag &&
1643       format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
1644      /* simple case */
1645      MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) );
1646   }
1647   else {
1648      GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH];
1649      GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH];
1650      GLfloat rscale = 1.0F / 255.0F;
1651      GLfloat gscale = 1.0F / 255.0F;
1652      GLfloat bscale = 1.0F / 255.0F;
1653      GLfloat ascale = 1.0F / 255.0F;
1654      GLuint i;
1655
1656      assert( n < MAX_WIDTH );
1657
1658      /* convert color components to floating point */
1659      for (i=0;i<n;i++) {
1660         red[i]   = rgba[i][RCOMP] * rscale;
1661         green[i] = rgba[i][GCOMP] * gscale;
1662         blue[i]  = rgba[i][BCOMP] * bscale;
1663         alpha[i] = rgba[i][ACOMP] * ascale;
1664      }
1665
1666      /*
1667       * Apply scale, bias and lookup-tables if enabled.
1668       */
1669      if (applyTransferOps) {
1670         if (ctx->Pixel.ScaleOrBiasRGBA) {
1671            gl_scale_and_bias_color( ctx, n, red, green, blue, alpha );
1672         }
1673         if (ctx->Pixel.MapColorFlag) {
1674            gl_map_color( ctx, n, red, green, blue, alpha );
1675         }
1676      }
1677
1678      if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
1679         for (i=0;i<n;i++) {
1680            GLfloat sum = red[i] + green[i] + blue[i];
1681            luminance[i] = CLAMP( sum, 0.0F, 1.0F );
1682         }
1683      }
1684
1685      /*
1686       * Pack/store the pixels.  Ugh!  Lots of cases!!!
1687       */
1688      switch (type) {
1689         case GL_UNSIGNED_BYTE:
1690            {
1691               GLubyte *dst = (GLubyte *) destination;
1692               switch (format) {
1693                  case GL_RED:
1694                     for (i=0;i<n;i++)
1695                        dst[i] = FLOAT_TO_UBYTE(red[i]);
1696                     break;
1697                  case GL_GREEN:
1698                     for (i=0;i<n;i++)
1699                        dst[i] = FLOAT_TO_UBYTE(green[i]);
1700                     break;
1701                  case GL_BLUE:
1702                     for (i=0;i<n;i++)
1703                        dst[i] = FLOAT_TO_UBYTE(blue[i]);
1704                     break;
1705                  case GL_ALPHA:
1706                     for (i=0;i<n;i++)
1707                        dst[i] = FLOAT_TO_UBYTE(alpha[i]);
1708                     break;
1709                  case GL_LUMINANCE:
1710                     for (i=0;i<n;i++)
1711                        dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1712                     break;
1713                  case GL_LUMINANCE_ALPHA:
1714                     for (i=0;i<n;i++) {
1715                        dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1716                        dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
1717                     }
1718                     break;
1719                  case GL_RGB:
1720                     for (i=0;i<n;i++) {
1721                        dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
1722                        dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
1723                        dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
1724                     }
1725                     break;
1726                  case GL_RGBA:
1727                     for (i=0;i<n;i++) {
1728                        dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
1729                        dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
1730                        dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
1731                        dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
1732                     }
1733                     break;
1734                  case GL_BGR:
1735                     for (i=0;i<n;i++) {
1736                        dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
1737                        dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
1738                        dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
1739                     }
1740                     break;
1741                  case GL_BGRA:
1742                     for (i=0;i<n;i++) {
1743                        dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
1744                        dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
1745                        dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
1746                        dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
1747                     }
1748                     break;
1749                  case GL_ABGR_EXT:
1750                     for (i=0;i<n;i++) {
1751                        dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
1752                        dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
1753                        dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
1754                        dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
1755                     }
1756                     break;
1757                  default:
1758                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1759               }
1760	    }
1761	    break;
1762	 case GL_BYTE:
1763            {
1764               GLbyte *dst = (GLbyte *) destination;
1765               switch (format) {
1766                  case GL_RED:
1767                     for (i=0;i<n;i++)
1768                        dst[i] = FLOAT_TO_BYTE(red[i]);
1769                     break;
1770                  case GL_GREEN:
1771                     for (i=0;i<n;i++)
1772                        dst[i] = FLOAT_TO_BYTE(green[i]);
1773                     break;
1774                  case GL_BLUE:
1775                     for (i=0;i<n;i++)
1776                        dst[i] = FLOAT_TO_BYTE(blue[i]);
1777                     break;
1778                  case GL_ALPHA:
1779                     for (i=0;i<n;i++)
1780                        dst[i] = FLOAT_TO_BYTE(alpha[i]);
1781                     break;
1782                  case GL_LUMINANCE:
1783                     for (i=0;i<n;i++)
1784                        dst[i] = FLOAT_TO_BYTE(luminance[i]);
1785                     break;
1786                  case GL_LUMINANCE_ALPHA:
1787                     for (i=0;i<n;i++) {
1788                        dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
1789                        dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
1790                     }
1791                     break;
1792                  case GL_RGB:
1793                     for (i=0;i<n;i++) {
1794                        dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
1795                        dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
1796                        dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
1797                     }
1798                     break;
1799                  case GL_RGBA:
1800                     for (i=0;i<n;i++) {
1801                        dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
1802                        dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
1803                        dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
1804                        dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
1805                     }
1806                     break;
1807                  case GL_BGR:
1808                     for (i=0;i<n;i++) {
1809                        dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
1810                        dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
1811                        dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
1812                     }
1813                     break;
1814                  case GL_BGRA:
1815                     for (i=0;i<n;i++) {
1816                        dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
1817                        dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
1818                        dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
1819                        dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
1820                     }
1821                  case GL_ABGR_EXT:
1822                     for (i=0;i<n;i++) {
1823                        dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
1824                        dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
1825                        dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
1826                        dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
1827                     }
1828                     break;
1829                  default:
1830                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1831               }
1832            }
1833	    break;
1834	 case GL_UNSIGNED_SHORT:
1835            {
1836               GLushort *dst = (GLushort *) destination;
1837               switch (format) {
1838                  case GL_RED:
1839                     for (i=0;i<n;i++)
1840                        dst[i] = FLOAT_TO_USHORT(red[i]);
1841                     break;
1842                  case GL_GREEN:
1843                     for (i=0;i<n;i++)
1844                        dst[i] = FLOAT_TO_USHORT(green[i]);
1845                     break;
1846                  case GL_BLUE:
1847                     for (i=0;i<n;i++)
1848                        dst[i] = FLOAT_TO_USHORT(blue[i]);
1849                     break;
1850                  case GL_ALPHA:
1851                     for (i=0;i<n;i++)
1852                        dst[i] = FLOAT_TO_USHORT(alpha[i]);
1853                     break;
1854                  case GL_LUMINANCE:
1855                     for (i=0;i<n;i++)
1856                        dst[i] = FLOAT_TO_USHORT(luminance[i]);
1857                     break;
1858                  case GL_LUMINANCE_ALPHA:
1859                     for (i=0;i<n;i++) {
1860                        dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
1861                        dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
1862                     }
1863                     break;
1864                  case GL_RGB:
1865                     for (i=0;i<n;i++) {
1866                        dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
1867                        dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
1868                        dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
1869                     }
1870                     break;
1871                  case GL_RGBA:
1872                     for (i=0;i<n;i++) {
1873                        dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
1874                        dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
1875                        dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
1876                        dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
1877                     }
1878                     break;
1879                  case GL_BGR:
1880                     for (i=0;i<n;i++) {
1881                        dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
1882                        dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
1883                        dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
1884                     }
1885                     break;
1886                  case GL_BGRA:
1887                     for (i=0;i<n;i++) {
1888                        dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
1889                        dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
1890                        dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
1891                        dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
1892                     }
1893                     break;
1894                  case GL_ABGR_EXT:
1895                     for (i=0;i<n;i++) {
1896                        dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
1897                        dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
1898                        dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
1899                        dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
1900                     }
1901                     break;
1902                  default:
1903                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1904               }
1905               if (packing->SwapBytes) {
1906                  gl_swap2( (GLushort *) dst, n );
1907               }
1908            }
1909	    break;
1910	 case GL_SHORT:
1911            {
1912               GLshort *dst = (GLshort *) destination;
1913               switch (format) {
1914                  case GL_RED:
1915                     for (i=0;i<n;i++)
1916                        dst[i] = FLOAT_TO_SHORT(red[i]);
1917                     break;
1918                  case GL_GREEN:
1919                     for (i=0;i<n;i++)
1920                        dst[i] = FLOAT_TO_SHORT(green[i]);
1921                     break;
1922                  case GL_BLUE:
1923                     for (i=0;i<n;i++)
1924                        dst[i] = FLOAT_TO_SHORT(blue[i]);
1925                     break;
1926                  case GL_ALPHA:
1927                     for (i=0;i<n;i++)
1928                        dst[i] = FLOAT_TO_SHORT(alpha[i]);
1929                     break;
1930                  case GL_LUMINANCE:
1931                     for (i=0;i<n;i++)
1932                        dst[i] = FLOAT_TO_SHORT(luminance[i]);
1933                     break;
1934                  case GL_LUMINANCE_ALPHA:
1935                     for (i=0;i<n;i++) {
1936                        dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
1937                        dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
1938                     }
1939                     break;
1940                  case GL_RGB:
1941                     for (i=0;i<n;i++) {
1942                        dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
1943                        dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
1944                        dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
1945                     }
1946                     break;
1947                  case GL_RGBA:
1948                     for (i=0;i<n;i++) {
1949                        dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
1950                        dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
1951                        dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
1952                        dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
1953                     }
1954                     break;
1955                  case GL_BGR:
1956                     for (i=0;i<n;i++) {
1957                        dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
1958                        dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
1959                        dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
1960                     }
1961                     break;
1962                  case GL_BGRA:
1963                     for (i=0;i<n;i++) {
1964                        dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
1965                        dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
1966                        dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
1967                        dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
1968                     }
1969                  case GL_ABGR_EXT:
1970                     for (i=0;i<n;i++) {
1971                        dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
1972                        dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
1973                        dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
1974                        dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
1975                     }
1976                     break;
1977                  default:
1978                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
1979               }
1980               if (packing->SwapBytes) {
1981                  gl_swap2( (GLushort *) dst, n );
1982               }
1983            }
1984	    break;
1985	 case GL_UNSIGNED_INT:
1986            {
1987               GLuint *dst = (GLuint *) destination;
1988               switch (format) {
1989                  case GL_RED:
1990                     for (i=0;i<n;i++)
1991                        dst[i] = FLOAT_TO_UINT(red[i]);
1992                     break;
1993                  case GL_GREEN:
1994                     for (i=0;i<n;i++)
1995                        dst[i] = FLOAT_TO_UINT(green[i]);
1996                     break;
1997                  case GL_BLUE:
1998                     for (i=0;i<n;i++)
1999                        dst[i] = FLOAT_TO_UINT(blue[i]);
2000                     break;
2001                  case GL_ALPHA:
2002                     for (i=0;i<n;i++)
2003                        dst[i] = FLOAT_TO_UINT(alpha[i]);
2004                     break;
2005                  case GL_LUMINANCE:
2006                     for (i=0;i<n;i++)
2007                        dst[i] = FLOAT_TO_UINT(luminance[i]);
2008                     break;
2009                  case GL_LUMINANCE_ALPHA:
2010                     for (i=0;i<n;i++) {
2011                        dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2012                        dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
2013                     }
2014                     break;
2015                  case GL_RGB:
2016                     for (i=0;i<n;i++) {
2017                        dst[i*3+0] = FLOAT_TO_UINT(red[i]);
2018                        dst[i*3+1] = FLOAT_TO_UINT(green[i]);
2019                        dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
2020                     }
2021                     break;
2022                  case GL_RGBA:
2023                     for (i=0;i<n;i++) {
2024                        dst[i*4+0] = FLOAT_TO_UINT(red[i]);
2025                        dst[i*4+1] = FLOAT_TO_UINT(green[i]);
2026                        dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
2027                        dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
2028                     }
2029                     break;
2030                  case GL_BGR:
2031                     for (i=0;i<n;i++) {
2032                        dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
2033                        dst[i*3+1] = FLOAT_TO_UINT(green[i]);
2034                        dst[i*3+2] = FLOAT_TO_UINT(red[i]);
2035                     }
2036                     break;
2037                  case GL_BGRA:
2038                     for (i=0;i<n;i++) {
2039                        dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
2040                        dst[i*4+1] = FLOAT_TO_UINT(green[i]);
2041                        dst[i*4+2] = FLOAT_TO_UINT(red[i]);
2042                        dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
2043                     }
2044                     break;
2045                  case GL_ABGR_EXT:
2046                     for (i=0;i<n;i++) {
2047                        dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
2048                        dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
2049                        dst[i*4+2] = FLOAT_TO_UINT(green[i]);
2050                        dst[i*4+3] = FLOAT_TO_UINT(red[i]);
2051                     }
2052                     break;
2053                  default:
2054                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2055               }
2056               if (packing->SwapBytes) {
2057                  gl_swap4( (GLuint *) dst, n );
2058               }
2059            }
2060	    break;
2061	 case GL_INT:
2062	    {
2063               GLint *dst = (GLint *) destination;
2064               switch (format) {
2065                  case GL_RED:
2066                     for (i=0;i<n;i++)
2067                        dst[i] = FLOAT_TO_INT(red[i]);
2068                     break;
2069                  case GL_GREEN:
2070                     for (i=0;i<n;i++)
2071                        dst[i] = FLOAT_TO_INT(green[i]);
2072                     break;
2073                  case GL_BLUE:
2074                     for (i=0;i<n;i++)
2075                        dst[i] = FLOAT_TO_INT(blue[i]);
2076                     break;
2077                  case GL_ALPHA:
2078                     for (i=0;i<n;i++)
2079                        dst[i] = FLOAT_TO_INT(alpha[i]);
2080                     break;
2081                  case GL_LUMINANCE:
2082                     for (i=0;i<n;i++)
2083                        dst[i] = FLOAT_TO_INT(luminance[i]);
2084                     break;
2085                  case GL_LUMINANCE_ALPHA:
2086                     for (i=0;i<n;i++) {
2087                        dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2088                        dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
2089                     }
2090                     break;
2091                  case GL_RGB:
2092                     for (i=0;i<n;i++) {
2093                        dst[i*3+0] = FLOAT_TO_INT(red[i]);
2094                        dst[i*3+1] = FLOAT_TO_INT(green[i]);
2095                        dst[i*3+2] = FLOAT_TO_INT(blue[i]);
2096                     }
2097                     break;
2098                  case GL_RGBA:
2099                     for (i=0;i<n;i++) {
2100                        dst[i*4+0] = FLOAT_TO_INT(red[i]);
2101                        dst[i*4+1] = FLOAT_TO_INT(green[i]);
2102                        dst[i*4+2] = FLOAT_TO_INT(blue[i]);
2103                        dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
2104                     }
2105                     break;
2106                  case GL_BGR:
2107                     for (i=0;i<n;i++) {
2108                        dst[i*3+0] = FLOAT_TO_INT(blue[i]);
2109                        dst[i*3+1] = FLOAT_TO_INT(green[i]);
2110                        dst[i*3+2] = FLOAT_TO_INT(red[i]);
2111                     }
2112                     break;
2113                  case GL_BGRA:
2114                     for (i=0;i<n;i++) {
2115                        dst[i*4+0] = FLOAT_TO_INT(blue[i]);
2116                        dst[i*4+1] = FLOAT_TO_INT(green[i]);
2117                        dst[i*4+2] = FLOAT_TO_INT(red[i]);
2118                        dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
2119                     }
2120                     break;
2121                  case GL_ABGR_EXT:
2122                     for (i=0;i<n;i++) {
2123                        dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
2124                        dst[i*4+1] = FLOAT_TO_INT(blue[i]);
2125                        dst[i*4+2] = FLOAT_TO_INT(green[i]);
2126                        dst[i*4+3] = FLOAT_TO_INT(red[i]);
2127                     }
2128                     break;
2129                  default:
2130                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2131               }
2132	       if (packing->SwapBytes) {
2133		  gl_swap4( (GLuint *) dst, n );
2134	       }
2135	    }
2136	    break;
2137	 case GL_FLOAT:
2138	    {
2139               GLfloat *dst = (GLfloat *) destination;
2140               switch (format) {
2141                  case GL_RED:
2142                     for (i=0;i<n;i++)
2143                        dst[i] = red[i];
2144                     break;
2145                  case GL_GREEN:
2146                     for (i=0;i<n;i++)
2147                        dst[i] = green[i];
2148                     break;
2149                  case GL_BLUE:
2150                     for (i=0;i<n;i++)
2151                        dst[i] = blue[i];
2152                     break;
2153                  case GL_ALPHA:
2154                     for (i=0;i<n;i++)
2155                        dst[i] = alpha[i];
2156                     break;
2157                  case GL_LUMINANCE:
2158                     for (i=0;i<n;i++)
2159                        dst[i] = luminance[i];
2160                     break;
2161                  case GL_LUMINANCE_ALPHA:
2162                     for (i=0;i<n;i++) {
2163                        dst[i*2+0] = luminance[i];
2164                        dst[i*2+1] = alpha[i];
2165                     }
2166                     break;
2167                  case GL_RGB:
2168                     for (i=0;i<n;i++) {
2169                        dst[i*3+0] = red[i];
2170                        dst[i*3+1] = green[i];
2171                        dst[i*3+2] = blue[i];
2172                     }
2173                     break;
2174                  case GL_RGBA:
2175                     for (i=0;i<n;i++) {
2176                        dst[i*4+0] = red[i];
2177                        dst[i*4+1] = green[i];
2178                        dst[i*4+2] = blue[i];
2179                        dst[i*4+3] = alpha[i];
2180                     }
2181                     break;
2182                  case GL_BGR:
2183                     for (i=0;i<n;i++) {
2184                        dst[i*3+0] = blue[i];
2185                        dst[i*3+1] = green[i];
2186                        dst[i*3+2] = red[i];
2187                     }
2188                     break;
2189                  case GL_BGRA:
2190                     for (i=0;i<n;i++) {
2191                        dst[i*4+0] = blue[i];
2192                        dst[i*4+1] = green[i];
2193                        dst[i*4+2] = red[i];
2194                        dst[i*4+3] = alpha[i];
2195                     }
2196                     break;
2197                  case GL_ABGR_EXT:
2198                     for (i=0;i<n;i++) {
2199                        dst[i*4+0] = alpha[i];
2200                        dst[i*4+1] = blue[i];
2201                        dst[i*4+2] = green[i];
2202                        dst[i*4+3] = red[i];
2203                     }
2204                     break;
2205                  default:
2206                     gl_problem(ctx, "bad format in gl_pack_rgba_span\n");
2207               }
2208	       if (packing->SwapBytes) {
2209		  gl_swap4( (GLuint *) dst, n );
2210	       }
2211	    }
2212	    break;
2213         case GL_UNSIGNED_BYTE_3_3_2:
2214            if (format == GL_RGB) {
2215               GLubyte *dst = (GLubyte *) destination;
2216               for (i=0;i<n;i++) {
2217                  dst[i] = (((GLint) (red[i]   * 7.0F)) << 5)
2218                         | (((GLint) (green[i] * 7.0F)) << 2)
2219                         | (((GLint) (blue[i]  * 3.0F))     );
2220               }
2221            }
2222            break;
2223         case GL_UNSIGNED_BYTE_2_3_3_REV:
2224            if (format == GL_RGB) {
2225               GLubyte *dst = (GLubyte *) destination;
2226               for (i=0;i<n;i++) {
2227                  dst[i] = (((GLint) (red[i]   * 7.0F))     )
2228                         | (((GLint) (green[i] * 7.0F)) << 3)
2229                         | (((GLint) (blue[i]  * 3.0F)) << 5);
2230               }
2231            }
2232            break;
2233         case GL_UNSIGNED_SHORT_5_6_5:
2234            if (format == GL_RGB) {
2235               GLushort *dst = (GLushort *) destination;
2236               for (i=0;i<n;i++) {
2237                  dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
2238                         | (((GLint) (green[i] * 63.0F)) <<  5)
2239                         | (((GLint) (blue[i]  * 31.0F))      );
2240               }
2241            }
2242            break;
2243         case GL_UNSIGNED_SHORT_5_6_5_REV:
2244            if (format == GL_RGB) {
2245               GLushort *dst = (GLushort *) destination;
2246               for (i=0;i<n;i++) {
2247                  dst[i] = (((GLint) (red[i]   * 31.0F))      )
2248                         | (((GLint) (green[i] * 63.0F)) <<  5)
2249                         | (((GLint) (blue[i]  * 31.0F)) << 11);
2250               }
2251            }
2252            break;
2253         case GL_UNSIGNED_SHORT_4_4_4_4:
2254            if (format == GL_RGB) {
2255               GLushort *dst = (GLushort *) destination;
2256               for (i=0;i<n;i++) {
2257                  dst[i] = (((GLint) (red[i]   * 15.0F)) << 12)
2258                         | (((GLint) (green[i] * 15.0F)) <<  8)
2259                         | (((GLint) (blue[i]  * 15.0F)) <<  4)
2260                         | (((GLint) (alpha[i] * 15.0F))      );
2261               }
2262            }
2263            break;
2264         case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2265            if (format == GL_RGB) {
2266               GLushort *dst = (GLushort *) destination;
2267               for (i=0;i<n;i++) {
2268                  dst[i] = (((GLint) (red[i]   * 15.0F))      )
2269                         | (((GLint) (green[i] * 15.0F)) <<  4)
2270                         | (((GLint) (blue[i]  * 15.0F)) <<  8)
2271                         | (((GLint) (alpha[i] * 15.0F)) << 12);
2272               }
2273            }
2274            break;
2275         case GL_UNSIGNED_SHORT_5_5_5_1:
2276            if (format == GL_RGB) {
2277               GLushort *dst = (GLushort *) destination;
2278               for (i=0;i<n;i++) {
2279                  dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
2280                         | (((GLint) (green[i] * 31.0F)) <<  6)
2281                         | (((GLint) (blue[i]  * 31.0F)) <<  1)
2282                         | (((GLint) (alpha[i] *  1.0F))      );
2283               }
2284            }
2285            break;
2286         case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2287            if (format == GL_RGB) {
2288               GLushort *dst = (GLushort *) destination;
2289               for (i=0;i<n;i++) {
2290                  dst[i] = (((GLint) (red[i]   * 31.0F))      )
2291                         | (((GLint) (green[i] * 31.0F)) <<  5)
2292                         | (((GLint) (blue[i]  * 31.0F)) << 10)
2293                         | (((GLint) (alpha[i] *  1.0F)) << 15);
2294               }
2295            }
2296            break;
2297         case GL_UNSIGNED_INT_8_8_8_8:
2298            if (format == GL_RGBA) {
2299               GLuint *dst = (GLuint *) destination;
2300               for (i=0;i<n;i++) {
2301                  dst[i] = (((GLuint) (red[i]   * 255.0F)) << 24)
2302                         | (((GLuint) (green[i] * 255.0F)) << 16)
2303                         | (((GLuint) (blue[i]  * 255.0F)) <<  8)
2304                         | (((GLuint) (alpha[i] * 255.0F))      );
2305               }
2306            }
2307            else if (format == GL_BGRA) {
2308               GLuint *dst = (GLuint *) destination;
2309               for (i=0;i<n;i++) {
2310                  dst[i] = (((GLuint) (blue[i]  * 255.0F)) << 24)
2311                         | (((GLuint) (green[i] * 255.0F)) << 16)
2312                         | (((GLuint) (red[i]   * 255.0F)) <<  8)
2313                         | (((GLuint) (alpha[i] * 255.0F))      );
2314               }
2315            }
2316            else if (format == GL_ABGR_EXT) {
2317               GLuint *dst = (GLuint *) destination;
2318               for (i=0;i<n;i++) {
2319                  dst[i] = (((GLuint) (alpha[i] * 255.0F)) << 24)
2320                         | (((GLuint) (blue[i]  * 255.0F)) << 16)
2321                         | (((GLuint) (green[i] * 255.0F)) <<  8)
2322                         | (((GLuint) (red[i]   * 255.0F))      );
2323               }
2324            }
2325            break;
2326         case GL_UNSIGNED_INT_8_8_8_8_REV:
2327            if (format == GL_RGBA) {
2328               GLuint *dst = (GLuint *) destination;
2329               for (i=0;i<n;i++) {
2330                  dst[i] = (((GLuint) (red[i]   * 255.0F))      )
2331                         | (((GLuint) (green[i] * 255.0F)) <<  8)
2332                         | (((GLuint) (blue[i]  * 255.0F)) << 16)
2333                         | (((GLuint) (alpha[i] * 255.0F)) << 24);
2334               }
2335            }
2336            else if (format == GL_BGRA) {
2337               GLuint *dst = (GLuint *) destination;
2338               for (i=0;i<n;i++) {
2339                  dst[i] = (((GLuint) (blue[i]  * 255.0F))      )
2340                         | (((GLuint) (green[i] * 255.0F)) <<  8)
2341                         | (((GLuint) (red[i]   * 255.0F)) << 16)
2342                         | (((GLuint) (alpha[i] * 255.0F)) << 24);
2343               }
2344            }
2345            else if (format == GL_ABGR_EXT) {
2346               GLuint *dst = (GLuint *) destination;
2347               for (i=0;i<n;i++) {
2348                  dst[i] = (((GLuint) (alpha[i] * 255.0F))      )
2349                         | (((GLuint) (blue[i]  * 255.0F)) <<  8)
2350                         | (((GLuint) (green[i] * 255.0F)) << 16)
2351                         | (((GLuint) (red[i]   * 255.0F)) << 24);
2352               }
2353            }
2354            break;
2355         case GL_UNSIGNED_INT_10_10_10_2:
2356            if (format == GL_RGBA) {
2357               GLuint *dst = (GLuint *) destination;
2358               for (i=0;i<n;i++) {
2359                  dst[i] = (((GLuint) (red[i]   * 1023.0F)) << 22)
2360                         | (((GLuint) (green[i] * 1023.0F)) << 12)
2361                         | (((GLuint) (blue[i]  * 1023.0F)) <<  2)
2362                         | (((GLuint) (alpha[i] *    3.0F))      );
2363               }
2364            }
2365            else if (format == GL_BGRA) {
2366               GLuint *dst = (GLuint *) destination;
2367               for (i=0;i<n;i++) {
2368                  dst[i] = (((GLuint) (blue[i]  * 1023.0F)) << 22)
2369                         | (((GLuint) (green[i] * 1023.0F)) << 12)
2370                         | (((GLuint) (red[i]   * 1023.0F)) <<  2)
2371                         | (((GLuint) (alpha[i] *    3.0F))      );
2372               }
2373            }
2374            else if (format == GL_ABGR_EXT) {
2375               GLuint *dst = (GLuint *) destination;
2376               for (i=0;i<n;i++) {
2377                  dst[i] = (((GLuint) (alpha[i] * 1023.0F)) << 22)
2378                         | (((GLuint) (blue[i]  * 1023.0F)) << 12)
2379                         | (((GLuint) (green[i] * 1023.0F)) <<  2)
2380                         | (((GLuint) (red[i]   *    3.0F))      );
2381               }
2382            }
2383            break;
2384         case GL_UNSIGNED_INT_2_10_10_10_REV:
2385            if (format == GL_RGBA) {
2386               GLuint *dst = (GLuint *) destination;
2387               for (i=0;i<n;i++) {
2388                  dst[i] = (((GLuint) (red[i]   * 1023.0F))      )
2389                         | (((GLuint) (green[i] * 1023.0F)) << 10)
2390                         | (((GLuint) (blue[i]  * 1023.0F)) << 20)
2391                         | (((GLuint) (alpha[i] *    3.0F)) << 30);
2392               }
2393            }
2394            else if (format == GL_BGRA) {
2395               GLuint *dst = (GLuint *) destination;
2396               for (i=0;i<n;i++) {
2397                  dst[i] = (((GLuint) (blue[i]  * 1023.0F))      )
2398                         | (((GLuint) (green[i] * 1023.0F)) << 10)
2399                         | (((GLuint) (red[i]   * 1023.0F)) << 20)
2400                         | (((GLuint) (alpha[i] *    3.0F)) << 30);
2401               }
2402            }
2403            else if (format == GL_ABGR_EXT) {
2404               GLuint *dst = (GLuint *) destination;
2405               for (i=0;i<n;i++) {
2406                  dst[i] = (((GLuint) (alpha[i] * 1023.0F))      )
2407                         | (((GLuint) (blue[i]  * 1023.0F)) << 10)
2408                         | (((GLuint) (green[i] * 1023.0F)) << 20)
2409                         | (((GLuint) (red[i]   *    3.0F)) << 30);
2410               }
2411            }
2412            break;
2413         default:
2414            gl_problem( ctx, "bad type in gl_pack_rgba_span" );
2415      }
2416   }
2417}
2418