1 /* $Id: image.c,v 1.13 1999/11/08 07:36:44 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
37 #include "GL/xf86glx.h"
50 * Flip the 8 bits in each byte of the given array.
52 void gl_flip_bytes( GLubyte
*p
, GLuint n
)
54 register GLuint i
, a
, b
;
58 a
= ((b
& 0x01) << 7) |
72 * Flip the order of the 2 bytes in each word in the given array.
74 void gl_swap2( GLushort
*p
, GLuint n
)
79 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
86 * Flip the order of the 4 bytes in each word in the given array.
88 void gl_swap4( GLuint
*p
, GLuint n
)
90 register GLuint i
, a
, b
;
96 | ((b
<< 8) & 0xff0000)
97 | ((b
<< 24) & 0xff000000);
106 * Return the size, in bytes, of the given GL datatype.
107 * Return 0 if GL_BITMAP.
108 * Return -1 if invalid type enum.
110 GLint
gl_sizeof_type( GLenum type
)
115 case GL_UNSIGNED_BYTE
:
116 return sizeof(GLubyte
);
118 return sizeof(GLbyte
);
119 case GL_UNSIGNED_SHORT
:
120 return sizeof(GLushort
);
122 return sizeof(GLshort
);
123 case GL_UNSIGNED_INT
:
124 return sizeof(GLuint
);
126 return sizeof(GLint
);
128 return sizeof(GLfloat
);
136 * Same as gl_sizeof_packed_type() but we also accept the
137 * packed pixel format datatypes.
139 GLint
gl_sizeof_packed_type( GLenum type
)
144 case GL_UNSIGNED_BYTE
:
145 return sizeof(GLubyte
);
147 return sizeof(GLbyte
);
148 case GL_UNSIGNED_SHORT
:
149 return sizeof(GLushort
);
151 return sizeof(GLshort
);
152 case GL_UNSIGNED_INT
:
153 return sizeof(GLuint
);
155 return sizeof(GLint
);
157 return sizeof(GLfloat
);
158 case GL_UNSIGNED_BYTE_3_3_2
:
159 return sizeof(GLubyte
);
160 case GL_UNSIGNED_BYTE_2_3_3_REV
:
161 return sizeof(GLubyte
);
162 case GL_UNSIGNED_SHORT_5_6_5
:
163 return sizeof(GLshort
);
164 case GL_UNSIGNED_SHORT_5_6_5_REV
:
165 return sizeof(GLshort
);
166 case GL_UNSIGNED_SHORT_4_4_4_4
:
167 return sizeof(GLshort
);
168 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
169 return sizeof(GLshort
);
170 case GL_UNSIGNED_SHORT_5_5_5_1
:
171 return sizeof(GLshort
);
172 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
173 return sizeof(GLshort
);
174 case GL_UNSIGNED_INT_8_8_8_8
:
175 return sizeof(GLuint
);
176 case GL_UNSIGNED_INT_8_8_8_8_REV
:
177 return sizeof(GLuint
);
178 case GL_UNSIGNED_INT_10_10_10_2
:
179 return sizeof(GLuint
);
180 case GL_UNSIGNED_INT_2_10_10_10_REV
:
181 return sizeof(GLuint
);
190 * Return the number of components in a GL enum pixel type.
191 * Return -1 if bad format.
193 GLint
gl_components_in_format( GLenum format
)
197 case GL_COLOR_INDEX1_EXT
:
198 case GL_COLOR_INDEX2_EXT
:
199 case GL_COLOR_INDEX4_EXT
:
200 case GL_COLOR_INDEX8_EXT
:
201 case GL_COLOR_INDEX12_EXT
:
202 case GL_COLOR_INDEX16_EXT
:
203 case GL_STENCIL_INDEX
:
204 case GL_DEPTH_COMPONENT
:
212 case GL_LUMINANCE_ALPHA
:
231 * Return bytes per pixel for given format and type
232 * Return -1 if bad format or type.
234 GLint
gl_bytes_per_pixel( GLenum format
, GLenum type
)
236 GLint comps
= gl_components_in_format( format
);
242 return 0; /* special case */
244 case GL_UNSIGNED_BYTE
:
245 return comps
* sizeof(GLubyte
);
247 case GL_UNSIGNED_SHORT
:
248 return comps
* sizeof(GLshort
);
250 case GL_UNSIGNED_INT
:
251 return comps
* sizeof(GLint
);
253 return comps
* sizeof(GLfloat
);
254 case GL_UNSIGNED_BYTE_3_3_2
:
255 case GL_UNSIGNED_BYTE_2_3_3_REV
:
256 if (format
== GL_RGB
|| format
== GL_BGR
)
257 return sizeof(GLubyte
);
259 return -1; /* error */
260 case GL_UNSIGNED_SHORT_5_6_5
:
261 case GL_UNSIGNED_SHORT_5_6_5_REV
:
262 if (format
== GL_RGB
|| format
== GL_BGR
)
263 return sizeof(GLshort
);
265 return -1; /* error */
266 case GL_UNSIGNED_SHORT_4_4_4_4
:
267 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
268 case GL_UNSIGNED_SHORT_5_5_5_1
:
269 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
270 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
)
271 return sizeof(GLushort
);
274 case GL_UNSIGNED_INT_8_8_8_8
:
275 case GL_UNSIGNED_INT_8_8_8_8_REV
:
276 case GL_UNSIGNED_INT_10_10_10_2
:
277 case GL_UNSIGNED_INT_2_10_10_10_REV
:
278 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
)
279 return sizeof(GLuint
);
289 * Test if the given pixel format and type are legal.
290 * Return GL_TRUE for legal, GL_FALSE for illegal.
292 GLboolean
gl_is_legal_format_and_type( GLenum format
, GLenum type
)
296 case GL_STENCIL_INDEX
:
300 case GL_UNSIGNED_BYTE
:
302 case GL_UNSIGNED_SHORT
:
304 case GL_UNSIGNED_INT
:
315 case GL_LUMINANCE_ALPHA
:
316 case GL_DEPTH_COMPONENT
:
320 case GL_UNSIGNED_BYTE
:
322 case GL_UNSIGNED_SHORT
:
324 case GL_UNSIGNED_INT
:
333 case GL_UNSIGNED_BYTE
:
335 case GL_UNSIGNED_SHORT
:
337 case GL_UNSIGNED_INT
:
339 case GL_UNSIGNED_BYTE_3_3_2
:
340 case GL_UNSIGNED_BYTE_2_3_3_REV
:
341 case GL_UNSIGNED_SHORT_5_6_5
:
342 case GL_UNSIGNED_SHORT_5_6_5_REV
:
352 case GL_UNSIGNED_BYTE
:
354 case GL_UNSIGNED_SHORT
:
356 case GL_UNSIGNED_INT
:
358 case GL_UNSIGNED_SHORT_4_4_4_4
:
359 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
360 case GL_UNSIGNED_SHORT_5_5_5_1
:
361 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
362 case GL_UNSIGNED_INT_8_8_8_8
:
363 case GL_UNSIGNED_INT_8_8_8_8_REV
:
364 case GL_UNSIGNED_INT_10_10_10_2
:
365 case GL_UNSIGNED_INT_2_10_10_10_REV
:
379 * Return the address of a pixel in an image (actually a volume).
380 * Pixel unpacking/packing parameters are observed according to 'packing'.
381 * Input: image - start of image data
382 * width, height - size of image
383 * format - image format
384 * type - pixel component type
385 * packing - the pixelstore attributes
386 * img - which image in the volume (0 for 1D or 2D images)
387 * row, column - location of pixel in the image
388 * Return: address of pixel at (image,row,column) in image or NULL if error.
390 GLvoid
*gl_pixel_addr_in_image( const struct gl_pixelstore_attrib
*packing
,
391 const GLvoid
*image
, GLsizei width
,
392 GLsizei height
, GLenum format
, GLenum type
,
393 GLint img
, GLint row
, GLint column
)
395 GLint alignment
; /* 1, 2 or 4 */
396 GLint pixels_per_row
;
397 GLint rows_per_image
;
400 GLint skipimages
; /* for 3-D volume images */
403 alignment
= packing
->Alignment
;
404 if (packing
->RowLength
> 0) {
405 pixels_per_row
= packing
->RowLength
;
408 pixels_per_row
= width
;
410 if (packing
->ImageHeight
> 0) {
411 rows_per_image
= packing
->ImageHeight
;
414 rows_per_image
= height
;
416 skiprows
= packing
->SkipRows
;
417 skippixels
= packing
->SkipPixels
;
418 skipimages
= packing
->SkipImages
;
420 if (type
==GL_BITMAP
) {
422 GLint comp_per_pixel
; /* components per pixel */
423 GLint bytes_per_comp
; /* bytes per component */
425 GLint bytes_per_image
;
427 /* Compute bytes per component */
428 bytes_per_comp
= gl_sizeof_packed_type( type
);
429 if (bytes_per_comp
<0) {
433 /* Compute number of components per pixel */
434 comp_per_pixel
= gl_components_in_format( format
);
435 if (comp_per_pixel
<0 && type
!= GL_BITMAP
) {
439 bytes_per_row
= alignment
440 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
442 bytes_per_image
= bytes_per_row
* rows_per_image
;
444 pixel_addr
= (GLubyte
*) image
445 + (skipimages
+ img
) * bytes_per_image
446 + (skiprows
+ row
) * bytes_per_row
447 + (skippixels
+ column
) / 8;
450 /* Non-BITMAP data */
451 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
453 bytes_per_pixel
= gl_bytes_per_pixel( format
, type
);
455 /* The pixel type and format should have been error checked earlier */
456 assert(bytes_per_pixel
> 0);
458 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
459 remainder
= bytes_per_row
% alignment
;
461 bytes_per_row
+= (alignment
- remainder
);
463 ASSERT(bytes_per_row
% alignment
== 0);
465 bytes_per_image
= bytes_per_row
* rows_per_image
;
467 /* compute final pixel address */
468 pixel_addr
= (GLubyte
*) image
469 + (skipimages
+ img
) * bytes_per_image
470 + (skiprows
+ row
) * bytes_per_row
471 + (skippixels
+ column
) * bytes_per_pixel
;
474 return (GLvoid
*) pixel_addr
;
480 * Allocate a new gl_image. All fields are initialized to zero.
482 static struct gl_image
*alloc_image( void )
484 return CALLOC_STRUCT(gl_image
);
490 * Allocate a new gl_image with the error flag set.
492 static struct gl_image
*alloc_error_image( GLint width
, GLint height
,
493 GLint depth
, GLenum format
,
496 struct gl_image
*image
= alloc_image();
498 image
->Width
= width
;
499 image
->Height
= height
;
500 image
->Depth
= depth
;
501 image
->Format
= format
;
503 image
->ErrorFlag
= GL_TRUE
;
513 void gl_free_image( struct gl_image
*image
)
524 * Do error checking on an image. If there's an error, register it and
525 * return GL_TRUE, else return GL_FALSE.
527 GLboolean
gl_image_error_test( GLcontext
*ctx
, const struct gl_image
*image
,
531 gl_error( ctx
, GL_OUT_OF_MEMORY
, msg
);
534 if (image
->Width
<= 0 || image
->Height
<= 0 || image
->Depth
<= 0) {
535 gl_error( ctx
, GL_INVALID_VALUE
, msg
);
538 else if (!gl_is_legal_format_and_type(image
->Format
, image
->Type
)) {
549 * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats.
550 * Input: type - datatype of src depth image
551 * Return pointer to a new gl_image structure.
553 * Notes: if the source image type is GLushort then the gl_image will
554 * also store GLushorts. If the src image type is GLuint then the gl_image
555 * will also store GLuints. For all other src image types the gl_image
556 * will store GLfloats. The integer cases can later be optimized.
558 static struct gl_image
*
559 unpack_depth_image( GLcontext
*ctx
, GLenum type
, GLint width
, GLint height
,
560 const GLvoid
*pixels
,
561 const struct gl_pixelstore_attrib
*packing
)
564 struct gl_image
*image
;
571 errorType
= type
!= GL_BYTE
&&
572 type
!= GL_UNSIGNED_BYTE
&&
574 type
!= GL_UNSIGNED_SHORT
&&
576 type
!= GL_UNSIGNED_INT
&&
579 image
= alloc_image();
581 image
->Width
= width
;
582 image
->Height
= height
;
584 image
->Components
= 1;
585 image
->Format
= GL_DEPTH_COMPONENT
;
590 if (type
==GL_UNSIGNED_SHORT
) {
591 image
->Type
= GL_UNSIGNED_SHORT
;
592 image
->Data
= MALLOC( width
* height
* sizeof(GLushort
));
594 else if (type
==GL_UNSIGNED_INT
) {
595 image
->Type
= GL_UNSIGNED_INT
;
596 image
->Data
= MALLOC( width
* height
* sizeof(GLuint
));
599 image
->Type
= GL_FLOAT
;
600 image
->Data
= MALLOC( width
* height
* sizeof(GLfloat
));
613 fDst
= (GLfloat
*) image
->Data
;
614 sDst
= (GLushort
*) image
->Data
;
615 iDst
= (GLuint
*) image
->Data
;
617 for (i
=0;i
<height
;i
++) {
618 GLvoid
*src
= gl_pixel_addr_in_image( packing
, pixels
,
620 GL_DEPTH_COMPONENT
, type
,
628 assert(image
->Type
== GL_FLOAT
);
629 for (j
=0; j
<width
; j
++) {
630 *fDst
++ = BYTE_TO_FLOAT(((GLbyte
*)src
)[j
]);
633 case GL_UNSIGNED_BYTE
:
634 assert(image
->Type
== GL_FLOAT
);
635 for (j
=0; j
<width
; j
++) {
636 *fDst
++ = UBYTE_TO_FLOAT(((GLubyte
*)src
)[j
]);
639 case GL_UNSIGNED_SHORT
:
640 assert(image
->Type
== GL_UNSIGNED_SHORT
);
641 MEMCPY( sDst
, src
, width
* sizeof(GLushort
) );
642 if (packing
->SwapBytes
) {
643 gl_swap2( sDst
, width
);
648 assert(image
->Type
== GL_FLOAT
);
649 if (packing
->SwapBytes
) {
650 for (j
=0;j
<width
;j
++) {
651 GLshort value
= ((GLshort
*)src
)[j
];
652 value
= ((value
>> 8) & 0xff) | ((value
&0xff) << 8);
653 *fDst
++ = SHORT_TO_FLOAT(value
);
657 for (j
=0;j
<width
;j
++) {
658 *fDst
++ = SHORT_TO_FLOAT(((GLshort
*)src
)[j
]);
663 assert(image
->Type
== GL_FLOAT
);
664 if (packing
->SwapBytes
) {
665 for (j
=0;j
<width
;j
++) {
666 GLint value
= ((GLint
*)src
)[j
];
667 value
= ((value
>> 24) & 0x000000ff) |
668 ((value
>> 8) & 0x0000ff00) |
669 ((value
<< 8) & 0x00ff0000) |
670 ((value
<< 24) & 0xff000000);
671 *fDst
++ = INT_TO_FLOAT(value
);
675 for (j
=0;j
<width
;j
++) {
676 *fDst
++ = INT_TO_FLOAT(((GLint
*)src
)[j
]);
681 case GL_UNSIGNED_INT
:
682 assert(image
->Type
== GL_UNSIGNED_INT
);
683 MEMCPY( iDst
, src
, width
* sizeof(GLuint
) );
684 if (packing
->SwapBytes
) {
685 gl_swap4( iDst
, width
);
690 assert(image
->Type
== GL_FLOAT
);
691 MEMCPY( fDst
, src
, width
* sizeof(GLfloat
) );
692 if (packing
->SwapBytes
) {
693 gl_swap4( (GLuint
*) fDst
, width
);
698 gl_problem(ctx
, "unpack_depth_image type" );
709 * Unpack a stencil image. Store as GLubytes in a gl_image structure.
710 * Return: pointer to new gl_image structure.
712 static struct gl_image
*
713 unpack_stencil_image( GLcontext
*ctx
, GLenum type
, GLint width
, GLint height
,
714 const GLvoid
*pixels
,
715 const struct gl_pixelstore_attrib
*packing
)
717 struct gl_image
*image
;
722 assert(sizeof(GLstencil
) == sizeof(GLubyte
));
724 errorType
= type
!= GL_BYTE
&&
725 type
!= GL_UNSIGNED_BYTE
&&
727 type
!= GL_UNSIGNED_SHORT
&&
729 type
!= GL_UNSIGNED_INT
&&
733 image
= alloc_image();
735 image
->Width
= width
;
736 image
->Height
= height
;
738 image
->Components
= 1;
739 image
->Format
= GL_STENCIL_INDEX
;
745 image
->Type
= GL_UNSIGNED_BYTE
;
746 image
->Data
= MALLOC( width
* height
* sizeof(GLubyte
));
757 return image
; /* error will be generated later */
759 dst
= (GLubyte
*) image
->Data
;
761 for (i
=0;i
<height
;i
++) {
762 GLvoid
*src
= gl_pixel_addr_in_image( packing
, pixels
,
764 GL_STENCIL_INDEX
, type
,
771 case GL_UNSIGNED_BYTE
:
773 MEMCPY( dst
, src
, width
* sizeof(GLubyte
) );
774 dst
+= width
* sizeof(GLubyte
);
776 case GL_UNSIGNED_SHORT
:
778 if (packing
->SwapBytes
) {
779 /* grab upper byte */
780 for (j
=0; j
< width
; j
++) {
781 *dst
++ = (((GLushort
*)src
)[j
] & 0xff00) >> 8;
785 for (j
=0; j
< width
; j
++) {
786 *dst
++ = (((GLushort
*)src
)[j
]) & 0xff;
791 if (packing
->SwapBytes
) {
792 /* grab upper byte */
793 for (j
=0; j
< width
; j
++) {
794 *dst
++ = (((GLuint
*)src
)[j
] & 0xff000000) >> 8;
798 for (j
=0; j
< width
; j
++) {
799 *dst
++ = (((GLuint
*)src
)[j
]) & 0xff;
803 case GL_UNSIGNED_INT
:
804 if (packing
->SwapBytes
) {
805 /* grab upper byte */
806 for (j
=0; j
< width
; j
++) {
807 *dst
++ = (((GLuint
*)src
)[j
] & 0xff000000) >> 8;
811 for (j
=0; j
< width
; j
++) {
812 *dst
++ = (((GLuint
*)src
)[j
]) & 0xff;
817 if (packing
->SwapBytes
) {
818 for (j
=0; j
< width
; j
++) {
820 GLint value
= ((GLuint
*)src
)[j
];
821 value
= ((value
& 0xff000000) >> 24)
822 | ((value
& 0x00ff0000) >> 8)
823 | ((value
& 0x0000ff00) << 8)
824 | ((value
& 0x000000ff) << 24);
825 fvalue
= *((GLfloat
*) &value
);
826 *dst
++ = ((GLint
) fvalue
) & 0xff;
830 for (j
=0; j
< width
; j
++) {
831 GLfloat fvalue
= ((GLfloat
*)src
)[j
];
832 *dst
++ = ((GLint
) fvalue
) & 0xff;
837 gl_problem(ctx
, "unpack_stencil_image type" );
848 * Unpack a bitmap, return a new gl_image struct.
850 static struct gl_image
*
851 unpack_bitmap( GLenum format
, GLint width
, GLint height
,
852 const GLvoid
*pixels
,
853 const struct gl_pixelstore_attrib
*packing
)
855 struct gl_image
*image
;
856 GLint bytes
, i
, width_in_bytes
;
857 GLubyte
*buffer
, *dst
;
859 assert(format
== GL_COLOR_INDEX
|| format
== GL_STENCIL_INDEX
);
861 /* Alloc dest storage */
862 bytes
= ((width
+7)/8 * height
);
863 if (bytes
>0 && pixels
!=NULL
) {
864 buffer
= (GLubyte
*) MALLOC( bytes
);
868 /* Copy/unpack pixel data to buffer */
869 width_in_bytes
= CEILING( width
, 8 );
871 for (i
=0; i
<height
; i
++) {
872 GLvoid
*src
= gl_pixel_addr_in_image( packing
, pixels
,
874 GL_COLOR_INDEX
, GL_BITMAP
,
880 MEMCPY( dst
, src
, width_in_bytes
);
881 dst
+= width_in_bytes
;
884 if (packing
->LsbFirst
) {
885 gl_flip_bytes( buffer
, bytes
);
889 /* a 'null' bitmap */
893 image
= alloc_image();
895 image
->Width
= width
;
896 image
->Height
= height
;
898 image
->Components
= 0;
899 image
->Format
= format
;
900 image
->Type
= GL_BITMAP
;
901 image
->Data
= buffer
;
915 * Unpack a 32x32 pixel polygon stipple from user memory using the
916 * current pixel unpack settings.
918 void gl_unpack_polygon_stipple( const GLcontext
*ctx
,
919 const GLubyte
*pattern
, GLuint dest
[32] )
922 for (i
= 0; i
< 32; i
++) {
923 GLubyte
*src
= (GLubyte
*) gl_pixel_addr_in_image( &ctx
->Unpack
, pattern
,
924 32, 32, GL_COLOR_INDEX
, GL_BITMAP
, 0, i
, 0 );
925 dest
[i
] = (src
[0] << 24)
931 /* Bit flipping within each byte */
932 if (ctx
->Unpack
.LsbFirst
) {
933 gl_flip_bytes( (GLubyte
*) dest
, 32 * 4 );
940 * Pack polygon stipple into user memory given current pixel packing
943 void gl_pack_polygon_stipple( const GLcontext
*ctx
,
944 const GLuint pattern
[32],
948 for (i
= 0; i
< 32; i
++) {
949 GLubyte
*dst
= (GLubyte
*) gl_pixel_addr_in_image( &ctx
->Pack
, dest
,
950 32, 32, GL_COLOR_INDEX
, GL_BITMAP
, 0, i
, 0 );
951 dst
[0] = (pattern
[i
] >> 24) & 0xff;
952 dst
[1] = (pattern
[i
] >> 16) & 0xff;
953 dst
[2] = (pattern
[i
] >> 8) & 0xff;
954 dst
[3] = (pattern
[i
] ) & 0xff;
956 /* Bit flipping within each byte */
957 if (ctx
->Pack
.LsbFirst
) {
958 gl_flip_bytes( (GLubyte
*) dst
, 4 );
966 * Unpack an RGBA or CI image and store it as unsigned bytes
968 static struct gl_image
*
969 unpack_ubyte_image( GLint width
, GLint height
,
970 GLint depth
, GLenum format
, const GLvoid
*pixels
,
971 const struct gl_pixelstore_attrib
*packing
)
973 struct gl_image
*image
;
974 GLint width_in_bytes
;
976 GLubyte
*buffer
, *dst
;
979 components
= gl_components_in_format( format
);
981 width_in_bytes
= width
* components
* sizeof(GLubyte
);
982 buffer
= (GLubyte
*) MALLOC( height
* width_in_bytes
* depth
);
987 /* Copy/unpack pixel data to buffer */
989 for (d
=0; d
<depth
; d
++ ) {
990 for (i
=0;i
<height
;i
++) {
991 GLubyte
*src
= (GLubyte
*) gl_pixel_addr_in_image( packing
,
992 pixels
, width
, height
, format
, GL_UNSIGNED_BYTE
,
998 MEMCPY( dst
, src
, width_in_bytes
);
999 dst
+= width_in_bytes
;
1003 if (format
== GL_BGR
) {
1004 /* swap order of every ubyte triplet from BGR to RGB */
1005 for (i
=0; i
<width
*height
; i
++) {
1006 GLubyte b
= buffer
[i
*3+0];
1007 GLubyte r
= buffer
[i
*3+2];
1012 else if (format
== GL_BGRA
) {
1013 /* swap order of every ubyte quadruplet from BGRA to RGBA */
1014 for (i
=0; i
<width
*height
; i
++) {
1015 GLubyte b
= buffer
[i
*4+0];
1016 GLubyte r
= buffer
[i
*4+2];
1021 else if (format
== GL_ABGR_EXT
) {
1022 /* swap order of every ubyte quadruplet from ABGR to RGBA */
1023 for (i
=0; i
<width
*height
; i
++) {
1024 GLubyte a
= buffer
[i
*4+0];
1025 GLubyte b
= buffer
[i
*4+1];
1026 GLubyte g
= buffer
[i
*4+2];
1027 GLubyte r
= buffer
[i
*4+3];
1036 image
= alloc_image();
1038 image
->Width
= width
;
1039 image
->Height
= height
;
1040 image
->Depth
= depth
;
1041 image
->Components
= components
;
1042 if (format
== GL_BGR
)
1043 image
->Format
= GL_RGB
;
1044 else if (format
== GL_BGRA
)
1045 image
->Format
= GL_RGBA
;
1046 else if (format
== GL_ABGR_EXT
)
1047 image
->Format
= GL_RGBA
;
1049 image
->Format
= format
;
1050 image
->Type
= GL_UNSIGNED_BYTE
;
1051 image
->Data
= buffer
;
1052 image
->RefCount
= 0;
1064 * Unpack a color image storing image as GLfloats
1066 static struct gl_image
*
1067 unpack_float_image( GLcontext
*ctx
, GLint width
, GLint height
, GLint depth
,
1068 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1069 const struct gl_pixelstore_attrib
*packing
)
1071 struct gl_image
*image
;
1073 GLint elems_per_row
;
1076 GLboolean normalize
;
1078 assert(type
!= GL_BITMAP
);
1080 components
= gl_components_in_format( format
);
1081 assert(components
> 0); /* should have been caught earlier */
1083 if (!gl_is_legal_format_and_type( format
, type
)) {
1084 /* bad pixel type for format, make dummy image */
1085 image
= alloc_image();
1087 image
->Width
= width
;
1088 image
->Height
= height
;
1089 image
->Depth
= depth
;
1090 image
->Components
= components
;
1091 image
->Format
= format
;
1094 image
->RefCount
= 0;
1099 elems_per_row
= width
* components
;
1101 image
= alloc_image();
1103 image
->Width
= width
;
1104 image
->Height
= height
;
1105 image
->Depth
= depth
;
1106 image
->Components
= components
;
1107 if (format
== GL_BGR
)
1108 image
->Format
= GL_RGB
;
1109 else if (format
== GL_BGRA
)
1110 image
->Format
= GL_RGBA
;
1111 else if (format
== GL_ABGR_EXT
)
1112 image
->Format
= GL_RGBA
;
1114 image
->Format
= format
;
1115 image
->Type
= GL_FLOAT
;
1116 image
->Data
= MALLOC( elems_per_row
* height
* depth
* sizeof(GLfloat
));
1117 image
->RefCount
= 0;
1125 normalize
= (format
!= GL_COLOR_INDEX
) && (format
!= GL_STENCIL_INDEX
);
1127 dst
= (GLfloat
*) image
->Data
;
1129 for (d
=0; d
<depth
; d
++) {
1130 for (i
=0;i
<height
;i
++) {
1131 GLvoid
*src
= gl_pixel_addr_in_image( packing
, pixels
,
1140 case GL_UNSIGNED_BYTE
:
1142 GLubyte
*ubsrc
= (GLubyte
*) src
;
1144 for (j
=0;j
<elems_per_row
;j
++) {
1145 *dst
++ = UBYTE_TO_FLOAT(ubsrc
[j
]);
1149 for (j
=0;j
<elems_per_row
;j
++) {
1150 *dst
++ = (GLfloat
) ubsrc
[j
];
1157 for (j
=0;j
<elems_per_row
;j
++) {
1158 *dst
++ = BYTE_TO_FLOAT(((GLbyte
*)src
)[j
]);
1162 for (j
=0;j
<elems_per_row
;j
++) {
1163 *dst
++ = (GLfloat
) ((GLbyte
*)src
)[j
];
1167 case GL_UNSIGNED_SHORT
:
1168 if (packing
->SwapBytes
) {
1169 for (j
=0;j
<elems_per_row
;j
++) {
1170 GLushort value
= ((GLushort
*)src
)[j
];
1171 value
= ((value
>> 8) & 0xff) | ((value
&0xff) << 8);
1173 *dst
++ = USHORT_TO_FLOAT(value
);
1176 *dst
++ = (GLfloat
) value
;
1182 for (j
=0;j
<elems_per_row
;j
++) {
1183 *dst
++ = USHORT_TO_FLOAT(((GLushort
*)src
)[j
]);
1187 for (j
=0;j
<elems_per_row
;j
++) {
1188 *dst
++ = (GLfloat
) ((GLushort
*)src
)[j
];
1194 if (packing
->SwapBytes
) {
1195 for (j
=0;j
<elems_per_row
;j
++) {
1196 GLshort value
= ((GLshort
*)src
)[j
];
1197 value
= ((value
>> 8) & 0xff) | ((value
&0xff) << 8);
1199 *dst
++ = SHORT_TO_FLOAT(value
);
1202 *dst
++ = (GLfloat
) value
;
1208 for (j
=0;j
<elems_per_row
;j
++) {
1209 *dst
++ = SHORT_TO_FLOAT(((GLshort
*)src
)[j
]);
1213 for (j
=0;j
<elems_per_row
;j
++) {
1214 *dst
++ = (GLfloat
) ((GLshort
*)src
)[j
];
1219 case GL_UNSIGNED_INT
:
1220 if (packing
->SwapBytes
) {
1222 for (j
=0;j
<elems_per_row
;j
++) {
1223 value
= ((GLuint
*)src
)[j
];
1224 value
= ((value
& 0xff000000) >> 24)
1225 | ((value
& 0x00ff0000) >> 8)
1226 | ((value
& 0x0000ff00) << 8)
1227 | ((value
& 0x000000ff) << 24);
1229 *dst
++ = UINT_TO_FLOAT(value
);
1232 *dst
++ = (GLfloat
) value
;
1238 for (j
=0;j
<elems_per_row
;j
++) {
1239 *dst
++ = UINT_TO_FLOAT(((GLuint
*)src
)[j
]);
1243 for (j
=0;j
<elems_per_row
;j
++) {
1244 *dst
++ = (GLfloat
) ((GLuint
*)src
)[j
];
1250 if (packing
->SwapBytes
) {
1252 for (j
=0;j
<elems_per_row
;j
++) {
1253 value
= ((GLint
*)src
)[j
];
1254 value
= ((value
& 0xff000000) >> 24)
1255 | ((value
& 0x00ff0000) >> 8)
1256 | ((value
& 0x0000ff00) << 8)
1257 | ((value
& 0x000000ff) << 24);
1259 *dst
++ = INT_TO_FLOAT(value
);
1262 *dst
++ = (GLfloat
) value
;
1268 for (j
=0;j
<elems_per_row
;j
++) {
1269 *dst
++ = INT_TO_FLOAT(((GLint
*)src
)[j
]);
1273 for (j
=0;j
<elems_per_row
;j
++) {
1274 *dst
++ = (GLfloat
) ((GLint
*)src
)[j
];
1280 if (packing
->SwapBytes
) {
1282 for (j
=0;j
<elems_per_row
;j
++) {
1283 value
= ((GLuint
*)src
)[j
];
1284 value
= ((value
& 0xff000000) >> 24)
1285 | ((value
& 0x00ff0000) >> 8)
1286 | ((value
& 0x0000ff00) << 8)
1287 | ((value
& 0x000000ff) << 24);
1288 *dst
++ = *((GLfloat
*) &value
);
1292 MEMCPY( dst
, src
, elems_per_row
*sizeof(GLfloat
) );
1293 dst
+= elems_per_row
;
1296 case GL_UNSIGNED_BYTE_3_3_2
:
1298 GLubyte
*ubsrc
= (GLubyte
*) src
;
1299 for (j
=0;j
<width
;j
++) {
1300 GLubyte p
= ubsrc
[j
];
1301 *dst
++ = ((p
>> 5) ) * (1.0F
/ 7.0F
); /* red */
1302 *dst
++ = ((p
>> 2) & 0x7) * (1.0F
/ 7.0F
); /* green */
1303 *dst
++ = ((p
) & 0x3) * (1.0F
/ 3.0F
); /* blue */
1307 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1309 GLubyte
*ubsrc
= (GLubyte
*) src
;
1310 for (j
=0;j
<width
;j
++) {
1311 GLubyte p
= ubsrc
[j
];
1312 *dst
++ = ((p
) & 0x7) * (1.0F
/ 7.0F
); /* red */
1313 *dst
++ = ((p
>> 3) & 0x7) * (1.0F
/ 7.0F
); /* green */
1314 *dst
++ = ((p
>> 6) ) * (1.0F
/ 3.0F
); /* blue */
1318 case GL_UNSIGNED_SHORT_5_6_5
:
1320 GLushort
*ussrc
= (GLushort
*) src
;
1321 for (j
=0;j
<width
;j
++) {
1322 GLushort p
= ussrc
[j
];
1323 *dst
++ = ((p
>> 11) ) * (1.0F
/ 31.0F
); /* red */
1324 *dst
++ = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
); /* green */
1325 *dst
++ = ((p
) & 0x1f) * (1.0F
/ 31.0F
); /* blue */
1329 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1331 GLushort
*ussrc
= (GLushort
*) src
;
1332 for (j
=0;j
<width
;j
++) {
1333 GLushort p
= ussrc
[j
];
1334 *dst
++ = ((p
) & 0x1f) * (1.0F
/ 31.0F
); /* red */
1335 *dst
++ = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
); /* green */
1336 *dst
++ = ((p
>> 11) ) * (1.0F
/ 31.0F
); /* blue */
1340 case GL_UNSIGNED_SHORT_4_4_4_4
:
1342 GLushort
*ussrc
= (GLushort
*) src
;
1343 for (j
=0;j
<width
;j
++) {
1344 GLushort p
= ussrc
[j
];
1345 *dst
++ = ((p
>> 12) ) * (1.0F
/ 15.0F
); /* red */
1346 *dst
++ = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
); /* green */
1347 *dst
++ = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
); /* blue */
1348 *dst
++ = ((p
) & 0xf) * (1.0F
/ 15.0F
); /* alpha */
1352 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1354 GLushort
*ussrc
= (GLushort
*) src
;
1355 for (j
=0;j
<width
;j
++) {
1356 GLushort p
= ussrc
[j
];
1357 *dst
++ = ((p
) & 0xf) * (1.0F
/ 15.0F
); /* red */
1358 *dst
++ = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
); /* green */
1359 *dst
++ = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
); /* blue */
1360 *dst
++ = ((p
>> 12) ) * (1.0F
/ 15.0F
); /* alpha */
1364 case GL_UNSIGNED_SHORT_5_5_5_1
:
1366 GLushort
*ussrc
= (GLushort
*) src
;
1367 for (j
=0;j
<width
;j
++) {
1368 GLushort p
= ussrc
[j
];
1369 *dst
++ = ((p
>> 11) ) * (1.0F
/ 31.0F
); /* red */
1370 *dst
++ = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
); /* green */
1371 *dst
++ = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
); /* blue */
1372 *dst
++ = ((p
) & 0x1) * (1.0F
/ 1.0F
); /* alpha */
1376 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1378 GLushort
*ussrc
= (GLushort
*) src
;
1379 for (j
=0;j
<width
;j
++) {
1380 GLushort p
= ussrc
[j
];
1381 *dst
++ = ((p
) & 0x1f) * (1.0F
/ 31.0F
); /* red */
1382 *dst
++ = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
); /* green */
1383 *dst
++ = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
); /* blue */
1384 *dst
++ = ((p
>> 15) ) * (1.0F
/ 1.0F
); /* alpha */
1388 case GL_UNSIGNED_INT_8_8_8_8
:
1390 GLuint
*uisrc
= (GLuint
*) src
;
1391 for (j
=0;j
<width
;j
++) {
1392 GLuint p
= uisrc
[j
];
1393 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1394 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1395 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1396 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1400 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1402 GLuint
*uisrc
= (GLuint
*) src
;
1403 for (j
=0;j
<width
;j
++) {
1404 GLuint p
= uisrc
[j
];
1405 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1406 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1407 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1408 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1412 case GL_UNSIGNED_INT_10_10_10_2
:
1414 GLuint
*uisrc
= (GLuint
*) src
;
1415 for (j
=0;j
<width
;j
++) {
1416 GLuint p
= uisrc
[j
];
1417 *dst
++ = ((p
>> 22) ) * (1.0F
/ 1023.0F
); /* r */
1418 *dst
++ = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
); /* g */
1419 *dst
++ = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
); /* b */
1420 *dst
++ = ((p
) & 0x3 ) * (1.0F
/ 3.0F
); /* a */
1424 case GL_UNSIGNED_INT_2_10_10_10_REV
:
1426 GLuint
*uisrc
= (GLuint
*) src
;
1427 for (j
=0;j
<width
;j
++) {
1428 GLuint p
= uisrc
[j
];
1429 *dst
++ = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
); /* r*/
1430 *dst
++ = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
); /* g */
1431 *dst
++ = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
); /* b */
1432 *dst
++ = ((p
>> 30) ) * (1.0F
/ 3.0F
); /* a */
1437 gl_problem(ctx
, "unpack_float_image type" );
1443 if (format
== GL_BGR
) {
1444 /* swap order of every float triplet from BGR to RGBA */
1445 GLfloat
*buffer
= (GLfloat
*) image
->Data
;
1446 for (i
=0; i
<width
*height
*depth
; i
++) {
1447 GLfloat b
= buffer
[i
*3+0];
1448 GLfloat r
= buffer
[i
*3+2];
1453 else if (format
== GL_BGRA
) {
1454 /* swap order of every float quadruplet from BGRA to RGBA */
1455 GLfloat
*buffer
= (GLfloat
*) image
->Data
;
1456 for (i
=0; i
<width
*height
*depth
; i
++) {
1457 GLfloat b
= buffer
[i
*4+0];
1458 GLfloat r
= buffer
[i
*4+2];
1463 else if (format
== GL_ABGR_EXT
) {
1464 /* swap order of every float quadruplet from ABGR to RGBA */
1465 GLfloat
*buffer
= (GLfloat
*) image
->Data
;
1466 for (i
=0; i
<width
*height
*depth
; i
++) {
1467 GLfloat a
= buffer
[i
*4+0];
1468 GLfloat b
= buffer
[i
*4+1];
1469 GLfloat g
= buffer
[i
*4+2];
1470 GLfloat r
= buffer
[i
*4+3];
1484 * Unpack a bitmap image, using current glPixelStore parameters,
1485 * making a new gl_image.
1487 struct gl_image
*gl_unpack_bitmap( GLcontext
*ctx
,
1488 GLsizei width
, GLsizei height
,
1489 const GLubyte
*bitmap
,
1490 const struct gl_pixelstore_attrib
*packing
)
1492 return gl_unpack_image( ctx
, width
, height
,
1493 GL_COLOR_INDEX
, GL_BITMAP
, bitmap
, packing
);
1499 * Unpack a 2-D image from user's buffer. Return pointer to new
1502 * Input: width, height - size in pixels
1503 * format - format of incoming pixel data
1504 * type - datatype of incoming pixel data
1505 * pixels - pointer to unpacked image in user buffer
1507 struct gl_image
*gl_unpack_image( GLcontext
*ctx
,
1508 GLint width
, GLint height
,
1509 GLenum format
, GLenum type
,
1510 const GLvoid
*pixels
,
1511 const struct gl_pixelstore_attrib
*packing
)
1513 return gl_unpack_image3D( ctx
, width
, height
, 1,
1514 format
, type
, pixels
, packing
);
1520 * Unpack a 1, 2 or 3-D image from user-supplied address, returning a
1521 * pointer to a new gl_image struct.
1522 * This function is always called by a higher-level unpack function such
1523 * as gl_unpack_texsubimage() or gl_unpack_bitmap().
1525 * Input: width, height, depth - size in pixels
1526 * format - format of incoming pixel data
1527 * type - datatype of incoming pixel data
1528 * pixels - pointer to unpacked image.
1530 struct gl_image
*gl_unpack_image3D( GLcontext
*ctx
,
1531 GLint width
, GLint height
, GLint depth
,
1532 GLenum format
, GLenum type
,
1533 const GLvoid
*pixels
,
1534 const struct gl_pixelstore_attrib
*packing
)
1536 if (width
<= 0 || height
<= 0 || depth
<= 0) {
1537 return alloc_error_image(width
, height
, depth
, format
, type
);
1540 if (type
==GL_BITMAP
) {
1541 if (format
!= GL_COLOR_INDEX
&& format
!= GL_STENCIL_INDEX
) {
1542 return alloc_error_image(width
, height
, depth
, format
, type
);
1545 return unpack_bitmap( format
, width
, height
, pixels
, packing
);
1548 else if (format
==GL_DEPTH_COMPONENT
) {
1549 /* TODO: pack as GLdepth values (GLushort or GLuint) */
1550 return unpack_depth_image( ctx
, type
, width
, height
, pixels
, packing
);
1552 else if (format
==GL_STENCIL_INDEX
) {
1553 /* TODO: pack as GLstencil (GLubyte or GLushort) */
1554 return unpack_stencil_image( ctx
, type
, width
, height
, pixels
, packing
);
1556 else if (type
==GL_UNSIGNED_BYTE
) {
1557 /* upack, convert to GLubytes */
1558 return unpack_ubyte_image( width
, height
, depth
, format
, pixels
, packing
);
1561 /* upack, convert to floats */
1562 return unpack_float_image( ctx
, width
, height
, depth
,
1563 format
, type
, pixels
, packing
);
1566 /* never get here */
1572 * Apply pixel-transfer operations (scale, bias, mapping) to a single row
1573 * of a gl_image. Put resulting color components into result array.
1575 void gl_scale_bias_map_image_data( const GLcontext
*ctx
,
1576 const struct gl_image
*image
,
1577 GLint row
, GLubyte result
[] )
1586 start
= row
* image
->Width
* image
->Components
;
1588 for (i
=0; i
< image
->Width
; i
++) {
1589 GLint pos
= start
+i
;
1590 GLfloat red
, green
, blue
, alpha
;
1591 if (image
->Type
== GL_UNSIGNED_BYTE
) {
1592 const GLubyte
*data
= (GLubyte
*) image
->Data
;
1593 switch (image
->Format
) {
1595 red
= data
[pos
] * (1.0F
/255.0F
);
1601 red
= data
[pos
*3+0] * (1.0F
/255.0F
);
1602 green
= data
[pos
*3+1] * (1.0F
/255.0F
);
1603 blue
= data
[pos
*3+2] * (1.0F
/255.0F
);
1607 gl_problem(ctx
, "bad image format in gl_scale...image_data");
1611 else if (image
->Type
== GL_FLOAT
) {
1612 const GLubyte
*data
= (GLubyte
*) image
->Data
;
1613 switch (image
->Format
) {
1621 red
= data
[pos
*3+0];
1622 green
= data
[pos
*3+1];
1623 blue
= data
[pos
*3+2];
1627 gl_problem(ctx
, "bad image format in gl_scale...image_data");
1632 gl_problem(ctx
, "Bad image type in gl_scale_...image_data");
1636 assert(red
>= 0.0 && red
<= 1.0);
1637 assert(green
>= 0.0 && green
<= 1.0);
1638 assert(blue
>= 0.0 && blue
<= 1.0);
1639 assert(alpha
>= 0.0 && alpha
<= 1.0);
1642 if (scale or bias) {
1651 result
[i
*4+0] = (GLubyte
) (red
* 255.0);
1652 result
[i
*4+1] = (GLubyte
) (green
* 255.0);
1653 result
[i
*4+2] = (GLubyte
) (blue
* 255.0);
1654 result
[i
*4+3] = (GLubyte
) (alpha
* 255.0);
1661 * Pack the given RGBA span into client memory at 'dest' address
1662 * in the given pixel format and type.
1663 * Optionally apply the enabled pixel transfer ops.
1664 * Pack into memory using the given packing params struct.
1665 * This is used by glReadPixels and glGetTexImage?D()
1666 * Input: ctx - the context
1667 * n - number of pixels in the span
1669 * format - dest packing format
1670 * type - dest packing datatype
1671 * destination - destination packing address
1672 * packing - pixel packing parameters
1673 * applyTransferOps - apply scale/bias/lookup-table ops?
1675 void gl_pack_rgba_span( const GLcontext
*ctx
,
1676 GLuint n
, CONST GLubyte rgba
[][4],
1677 GLenum format
, GLenum type
, GLvoid
*destination
,
1678 const struct gl_pixelstore_attrib
*packing
,
1679 GLboolean applyTransferOps
)
1681 /* Test for optimized case first */
1682 if (!ctx
->Pixel
.ScaleOrBiasRGBA
&& !ctx
->Pixel
.MapColorFlag
&&
1683 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
1684 /* common simple case */
1685 MEMCPY( destination
, rgba
, n
* 4 * sizeof(GLubyte
) );
1687 else if (!ctx
->Pixel
.ScaleOrBiasRGBA
&& !ctx
->Pixel
.MapColorFlag
&&
1688 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
1689 /* common simple case */
1691 GLubyte
*dest
= (GLubyte
*) destination
;
1692 for (i
= 0; i
< n
; i
++) {
1693 dest
[0] = rgba
[i
][RCOMP
];
1694 dest
[1] = rgba
[i
][GCOMP
];
1695 dest
[2] = rgba
[i
][BCOMP
];
1700 GLfloat red
[MAX_WIDTH
], green
[MAX_WIDTH
], blue
[MAX_WIDTH
];
1701 GLfloat alpha
[MAX_WIDTH
], luminance
[MAX_WIDTH
];
1702 const GLfloat rscale
= 1.0F
/ 255.0F
;
1703 const GLfloat gscale
= 1.0F
/ 255.0F
;
1704 const GLfloat bscale
= 1.0F
/ 255.0F
;
1705 const GLfloat ascale
= 1.0F
/ 255.0F
;
1706 const GLint comps
= gl_components_in_format(format
);
1709 assert(n
<= MAX_WIDTH
);
1711 /* convert color components to floating point */
1713 red
[i
] = rgba
[i
][RCOMP
] * rscale
;
1714 green
[i
] = rgba
[i
][GCOMP
] * gscale
;
1715 blue
[i
] = rgba
[i
][BCOMP
] * bscale
;
1716 alpha
[i
] = rgba
[i
][ACOMP
] * ascale
;
1720 * Apply scale, bias and lookup-tables if enabled.
1722 if (applyTransferOps
) {
1723 if (ctx
->Pixel
.ScaleOrBiasRGBA
) {
1724 gl_scale_and_bias_color( ctx
, n
, red
, green
, blue
, alpha
);
1726 if (ctx
->Pixel
.MapColorFlag
) {
1727 gl_map_color( ctx
, n
, red
, green
, blue
, alpha
);
1731 if (format
==GL_LUMINANCE
|| format
==GL_LUMINANCE_ALPHA
) {
1733 GLfloat sum
= red
[i
] + green
[i
] + blue
[i
];
1734 luminance
[i
] = CLAMP( sum
, 0.0F
, 1.0F
);
1739 * Pack/store the pixels. Ugh! Lots of cases!!!
1742 case GL_UNSIGNED_BYTE
:
1744 GLubyte
*dst
= (GLubyte
*) destination
;
1748 dst
[i
] = FLOAT_TO_UBYTE(red
[i
]);
1752 dst
[i
] = FLOAT_TO_UBYTE(green
[i
]);
1756 dst
[i
] = FLOAT_TO_UBYTE(blue
[i
]);
1760 dst
[i
] = FLOAT_TO_UBYTE(alpha
[i
]);
1764 dst
[i
] = FLOAT_TO_UBYTE(luminance
[i
]);
1766 case GL_LUMINANCE_ALPHA
:
1768 dst
[i
*2+0] = FLOAT_TO_UBYTE(luminance
[i
]);
1769 dst
[i
*2+1] = FLOAT_TO_UBYTE(alpha
[i
]);
1774 dst
[i
*3+0] = FLOAT_TO_UBYTE(red
[i
]);
1775 dst
[i
*3+1] = FLOAT_TO_UBYTE(green
[i
]);
1776 dst
[i
*3+2] = FLOAT_TO_UBYTE(blue
[i
]);
1781 dst
[i
*4+0] = FLOAT_TO_UBYTE(red
[i
]);
1782 dst
[i
*4+1] = FLOAT_TO_UBYTE(green
[i
]);
1783 dst
[i
*4+2] = FLOAT_TO_UBYTE(blue
[i
]);
1784 dst
[i
*4+3] = FLOAT_TO_UBYTE(alpha
[i
]);
1789 dst
[i
*3+0] = FLOAT_TO_UBYTE(blue
[i
]);
1790 dst
[i
*3+1] = FLOAT_TO_UBYTE(green
[i
]);
1791 dst
[i
*3+2] = FLOAT_TO_UBYTE(red
[i
]);
1796 dst
[i
*4+0] = FLOAT_TO_UBYTE(blue
[i
]);
1797 dst
[i
*4+1] = FLOAT_TO_UBYTE(green
[i
]);
1798 dst
[i
*4+2] = FLOAT_TO_UBYTE(red
[i
]);
1799 dst
[i
*4+3] = FLOAT_TO_UBYTE(alpha
[i
]);
1804 dst
[i
*4+0] = FLOAT_TO_UBYTE(alpha
[i
]);
1805 dst
[i
*4+1] = FLOAT_TO_UBYTE(blue
[i
]);
1806 dst
[i
*4+2] = FLOAT_TO_UBYTE(green
[i
]);
1807 dst
[i
*4+3] = FLOAT_TO_UBYTE(red
[i
]);
1811 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1817 GLbyte
*dst
= (GLbyte
*) destination
;
1821 dst
[i
] = FLOAT_TO_BYTE(red
[i
]);
1825 dst
[i
] = FLOAT_TO_BYTE(green
[i
]);
1829 dst
[i
] = FLOAT_TO_BYTE(blue
[i
]);
1833 dst
[i
] = FLOAT_TO_BYTE(alpha
[i
]);
1837 dst
[i
] = FLOAT_TO_BYTE(luminance
[i
]);
1839 case GL_LUMINANCE_ALPHA
:
1841 dst
[i
*2+0] = FLOAT_TO_BYTE(luminance
[i
]);
1842 dst
[i
*2+1] = FLOAT_TO_BYTE(alpha
[i
]);
1847 dst
[i
*3+0] = FLOAT_TO_BYTE(red
[i
]);
1848 dst
[i
*3+1] = FLOAT_TO_BYTE(green
[i
]);
1849 dst
[i
*3+2] = FLOAT_TO_BYTE(blue
[i
]);
1854 dst
[i
*4+0] = FLOAT_TO_BYTE(red
[i
]);
1855 dst
[i
*4+1] = FLOAT_TO_BYTE(green
[i
]);
1856 dst
[i
*4+2] = FLOAT_TO_BYTE(blue
[i
]);
1857 dst
[i
*4+3] = FLOAT_TO_BYTE(alpha
[i
]);
1862 dst
[i
*3+0] = FLOAT_TO_BYTE(blue
[i
]);
1863 dst
[i
*3+1] = FLOAT_TO_BYTE(green
[i
]);
1864 dst
[i
*3+2] = FLOAT_TO_BYTE(red
[i
]);
1869 dst
[i
*4+0] = FLOAT_TO_BYTE(blue
[i
]);
1870 dst
[i
*4+1] = FLOAT_TO_BYTE(green
[i
]);
1871 dst
[i
*4+2] = FLOAT_TO_BYTE(red
[i
]);
1872 dst
[i
*4+3] = FLOAT_TO_BYTE(alpha
[i
]);
1876 dst
[i
*4+0] = FLOAT_TO_BYTE(alpha
[i
]);
1877 dst
[i
*4+1] = FLOAT_TO_BYTE(blue
[i
]);
1878 dst
[i
*4+2] = FLOAT_TO_BYTE(green
[i
]);
1879 dst
[i
*4+3] = FLOAT_TO_BYTE(red
[i
]);
1883 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1887 case GL_UNSIGNED_SHORT
:
1889 GLushort
*dst
= (GLushort
*) destination
;
1893 dst
[i
] = FLOAT_TO_USHORT(red
[i
]);
1897 dst
[i
] = FLOAT_TO_USHORT(green
[i
]);
1901 dst
[i
] = FLOAT_TO_USHORT(blue
[i
]);
1905 dst
[i
] = FLOAT_TO_USHORT(alpha
[i
]);
1909 dst
[i
] = FLOAT_TO_USHORT(luminance
[i
]);
1911 case GL_LUMINANCE_ALPHA
:
1913 dst
[i
*2+0] = FLOAT_TO_USHORT(luminance
[i
]);
1914 dst
[i
*2+1] = FLOAT_TO_USHORT(alpha
[i
]);
1919 dst
[i
*3+0] = FLOAT_TO_USHORT(red
[i
]);
1920 dst
[i
*3+1] = FLOAT_TO_USHORT(green
[i
]);
1921 dst
[i
*3+2] = FLOAT_TO_USHORT(blue
[i
]);
1926 dst
[i
*4+0] = FLOAT_TO_USHORT(red
[i
]);
1927 dst
[i
*4+1] = FLOAT_TO_USHORT(green
[i
]);
1928 dst
[i
*4+2] = FLOAT_TO_USHORT(blue
[i
]);
1929 dst
[i
*4+3] = FLOAT_TO_USHORT(alpha
[i
]);
1934 dst
[i
*3+0] = FLOAT_TO_USHORT(blue
[i
]);
1935 dst
[i
*3+1] = FLOAT_TO_USHORT(green
[i
]);
1936 dst
[i
*3+2] = FLOAT_TO_USHORT(red
[i
]);
1941 dst
[i
*4+0] = FLOAT_TO_USHORT(blue
[i
]);
1942 dst
[i
*4+1] = FLOAT_TO_USHORT(green
[i
]);
1943 dst
[i
*4+2] = FLOAT_TO_USHORT(red
[i
]);
1944 dst
[i
*4+3] = FLOAT_TO_USHORT(alpha
[i
]);
1949 dst
[i
*4+0] = FLOAT_TO_USHORT(alpha
[i
]);
1950 dst
[i
*4+1] = FLOAT_TO_USHORT(blue
[i
]);
1951 dst
[i
*4+2] = FLOAT_TO_USHORT(green
[i
]);
1952 dst
[i
*4+3] = FLOAT_TO_USHORT(red
[i
]);
1956 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1958 if (packing
->SwapBytes
) {
1959 gl_swap2( (GLushort
*) dst
, n
* comps
);
1965 GLshort
*dst
= (GLshort
*) destination
;
1969 dst
[i
] = FLOAT_TO_SHORT(red
[i
]);
1973 dst
[i
] = FLOAT_TO_SHORT(green
[i
]);
1977 dst
[i
] = FLOAT_TO_SHORT(blue
[i
]);
1981 dst
[i
] = FLOAT_TO_SHORT(alpha
[i
]);
1985 dst
[i
] = FLOAT_TO_SHORT(luminance
[i
]);
1987 case GL_LUMINANCE_ALPHA
:
1989 dst
[i
*2+0] = FLOAT_TO_SHORT(luminance
[i
]);
1990 dst
[i
*2+1] = FLOAT_TO_SHORT(alpha
[i
]);
1995 dst
[i
*3+0] = FLOAT_TO_SHORT(red
[i
]);
1996 dst
[i
*3+1] = FLOAT_TO_SHORT(green
[i
]);
1997 dst
[i
*3+2] = FLOAT_TO_SHORT(blue
[i
]);
2002 dst
[i
*4+0] = FLOAT_TO_SHORT(red
[i
]);
2003 dst
[i
*4+1] = FLOAT_TO_SHORT(green
[i
]);
2004 dst
[i
*4+2] = FLOAT_TO_SHORT(blue
[i
]);
2005 dst
[i
*4+3] = FLOAT_TO_SHORT(alpha
[i
]);
2010 dst
[i
*3+0] = FLOAT_TO_SHORT(blue
[i
]);
2011 dst
[i
*3+1] = FLOAT_TO_SHORT(green
[i
]);
2012 dst
[i
*3+2] = FLOAT_TO_SHORT(red
[i
]);
2017 dst
[i
*4+0] = FLOAT_TO_SHORT(blue
[i
]);
2018 dst
[i
*4+1] = FLOAT_TO_SHORT(green
[i
]);
2019 dst
[i
*4+2] = FLOAT_TO_SHORT(red
[i
]);
2020 dst
[i
*4+3] = FLOAT_TO_SHORT(alpha
[i
]);
2024 dst
[i
*4+0] = FLOAT_TO_SHORT(alpha
[i
]);
2025 dst
[i
*4+1] = FLOAT_TO_SHORT(blue
[i
]);
2026 dst
[i
*4+2] = FLOAT_TO_SHORT(green
[i
]);
2027 dst
[i
*4+3] = FLOAT_TO_SHORT(red
[i
]);
2031 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
2033 if (packing
->SwapBytes
) {
2034 gl_swap2( (GLushort
*) dst
, n
* comps
);
2038 case GL_UNSIGNED_INT
:
2040 GLuint
*dst
= (GLuint
*) destination
;
2044 dst
[i
] = FLOAT_TO_UINT(red
[i
]);
2048 dst
[i
] = FLOAT_TO_UINT(green
[i
]);
2052 dst
[i
] = FLOAT_TO_UINT(blue
[i
]);
2056 dst
[i
] = FLOAT_TO_UINT(alpha
[i
]);
2060 dst
[i
] = FLOAT_TO_UINT(luminance
[i
]);
2062 case GL_LUMINANCE_ALPHA
:
2064 dst
[i
*2+0] = FLOAT_TO_UINT(luminance
[i
]);
2065 dst
[i
*2+1] = FLOAT_TO_UINT(alpha
[i
]);
2070 dst
[i
*3+0] = FLOAT_TO_UINT(red
[i
]);
2071 dst
[i
*3+1] = FLOAT_TO_UINT(green
[i
]);
2072 dst
[i
*3+2] = FLOAT_TO_UINT(blue
[i
]);
2077 dst
[i
*4+0] = FLOAT_TO_UINT(red
[i
]);
2078 dst
[i
*4+1] = FLOAT_TO_UINT(green
[i
]);
2079 dst
[i
*4+2] = FLOAT_TO_UINT(blue
[i
]);
2080 dst
[i
*4+3] = FLOAT_TO_UINT(alpha
[i
]);
2085 dst
[i
*3+0] = FLOAT_TO_UINT(blue
[i
]);
2086 dst
[i
*3+1] = FLOAT_TO_UINT(green
[i
]);
2087 dst
[i
*3+2] = FLOAT_TO_UINT(red
[i
]);
2092 dst
[i
*4+0] = FLOAT_TO_UINT(blue
[i
]);
2093 dst
[i
*4+1] = FLOAT_TO_UINT(green
[i
]);
2094 dst
[i
*4+2] = FLOAT_TO_UINT(red
[i
]);
2095 dst
[i
*4+3] = FLOAT_TO_UINT(alpha
[i
]);
2100 dst
[i
*4+0] = FLOAT_TO_UINT(alpha
[i
]);
2101 dst
[i
*4+1] = FLOAT_TO_UINT(blue
[i
]);
2102 dst
[i
*4+2] = FLOAT_TO_UINT(green
[i
]);
2103 dst
[i
*4+3] = FLOAT_TO_UINT(red
[i
]);
2107 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
2109 if (packing
->SwapBytes
) {
2110 gl_swap4( (GLuint
*) dst
, n
* comps
);
2116 GLint
*dst
= (GLint
*) destination
;
2120 dst
[i
] = FLOAT_TO_INT(red
[i
]);
2124 dst
[i
] = FLOAT_TO_INT(green
[i
]);
2128 dst
[i
] = FLOAT_TO_INT(blue
[i
]);
2132 dst
[i
] = FLOAT_TO_INT(alpha
[i
]);
2136 dst
[i
] = FLOAT_TO_INT(luminance
[i
]);
2138 case GL_LUMINANCE_ALPHA
:
2140 dst
[i
*2+0] = FLOAT_TO_INT(luminance
[i
]);
2141 dst
[i
*2+1] = FLOAT_TO_INT(alpha
[i
]);
2146 dst
[i
*3+0] = FLOAT_TO_INT(red
[i
]);
2147 dst
[i
*3+1] = FLOAT_TO_INT(green
[i
]);
2148 dst
[i
*3+2] = FLOAT_TO_INT(blue
[i
]);
2153 dst
[i
*4+0] = FLOAT_TO_INT(red
[i
]);
2154 dst
[i
*4+1] = FLOAT_TO_INT(green
[i
]);
2155 dst
[i
*4+2] = FLOAT_TO_INT(blue
[i
]);
2156 dst
[i
*4+3] = FLOAT_TO_INT(alpha
[i
]);
2161 dst
[i
*3+0] = FLOAT_TO_INT(blue
[i
]);
2162 dst
[i
*3+1] = FLOAT_TO_INT(green
[i
]);
2163 dst
[i
*3+2] = FLOAT_TO_INT(red
[i
]);
2168 dst
[i
*4+0] = FLOAT_TO_INT(blue
[i
]);
2169 dst
[i
*4+1] = FLOAT_TO_INT(green
[i
]);
2170 dst
[i
*4+2] = FLOAT_TO_INT(red
[i
]);
2171 dst
[i
*4+3] = FLOAT_TO_INT(alpha
[i
]);
2176 dst
[i
*4+0] = FLOAT_TO_INT(alpha
[i
]);
2177 dst
[i
*4+1] = FLOAT_TO_INT(blue
[i
]);
2178 dst
[i
*4+2] = FLOAT_TO_INT(green
[i
]);
2179 dst
[i
*4+3] = FLOAT_TO_INT(red
[i
]);
2183 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
2185 if (packing
->SwapBytes
) {
2186 gl_swap4( (GLuint
*) dst
, n
* comps
);
2192 GLfloat
*dst
= (GLfloat
*) destination
;
2212 dst
[i
] = luminance
[i
];
2214 case GL_LUMINANCE_ALPHA
:
2216 dst
[i
*2+0] = luminance
[i
];
2217 dst
[i
*2+1] = alpha
[i
];
2222 dst
[i
*3+0] = red
[i
];
2223 dst
[i
*3+1] = green
[i
];
2224 dst
[i
*3+2] = blue
[i
];
2229 dst
[i
*4+0] = red
[i
];
2230 dst
[i
*4+1] = green
[i
];
2231 dst
[i
*4+2] = blue
[i
];
2232 dst
[i
*4+3] = alpha
[i
];
2237 dst
[i
*3+0] = blue
[i
];
2238 dst
[i
*3+1] = green
[i
];
2239 dst
[i
*3+2] = red
[i
];
2244 dst
[i
*4+0] = blue
[i
];
2245 dst
[i
*4+1] = green
[i
];
2246 dst
[i
*4+2] = red
[i
];
2247 dst
[i
*4+3] = alpha
[i
];
2252 dst
[i
*4+0] = alpha
[i
];
2253 dst
[i
*4+1] = blue
[i
];
2254 dst
[i
*4+2] = green
[i
];
2255 dst
[i
*4+3] = red
[i
];
2259 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
2261 if (packing
->SwapBytes
) {
2262 gl_swap4( (GLuint
*) dst
, n
* comps
);
2266 case GL_UNSIGNED_BYTE_3_3_2
:
2267 if (format
== GL_RGB
) {
2268 GLubyte
*dst
= (GLubyte
*) destination
;
2270 dst
[i
] = (((GLint
) (red
[i
] * 7.0F
)) << 5)
2271 | (((GLint
) (green
[i
] * 7.0F
)) << 2)
2272 | (((GLint
) (blue
[i
] * 3.0F
)) );
2276 case GL_UNSIGNED_BYTE_2_3_3_REV
:
2277 if (format
== GL_RGB
) {
2278 GLubyte
*dst
= (GLubyte
*) destination
;
2280 dst
[i
] = (((GLint
) (red
[i
] * 7.0F
)) )
2281 | (((GLint
) (green
[i
] * 7.0F
)) << 3)
2282 | (((GLint
) (blue
[i
] * 3.0F
)) << 5);
2286 case GL_UNSIGNED_SHORT_5_6_5
:
2287 if (format
== GL_RGB
) {
2288 GLushort
*dst
= (GLushort
*) destination
;
2290 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) << 11)
2291 | (((GLint
) (green
[i
] * 63.0F
)) << 5)
2292 | (((GLint
) (blue
[i
] * 31.0F
)) );
2296 case GL_UNSIGNED_SHORT_5_6_5_REV
:
2297 if (format
== GL_RGB
) {
2298 GLushort
*dst
= (GLushort
*) destination
;
2300 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) )
2301 | (((GLint
) (green
[i
] * 63.0F
)) << 5)
2302 | (((GLint
) (blue
[i
] * 31.0F
)) << 11);
2306 case GL_UNSIGNED_SHORT_4_4_4_4
:
2307 if (format
== GL_RGB
) {
2308 GLushort
*dst
= (GLushort
*) destination
;
2310 dst
[i
] = (((GLint
) (red
[i
] * 15.0F
)) << 12)
2311 | (((GLint
) (green
[i
] * 15.0F
)) << 8)
2312 | (((GLint
) (blue
[i
] * 15.0F
)) << 4)
2313 | (((GLint
) (alpha
[i
] * 15.0F
)) );
2317 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
2318 if (format
== GL_RGB
) {
2319 GLushort
*dst
= (GLushort
*) destination
;
2321 dst
[i
] = (((GLint
) (red
[i
] * 15.0F
)) )
2322 | (((GLint
) (green
[i
] * 15.0F
)) << 4)
2323 | (((GLint
) (blue
[i
] * 15.0F
)) << 8)
2324 | (((GLint
) (alpha
[i
] * 15.0F
)) << 12);
2328 case GL_UNSIGNED_SHORT_5_5_5_1
:
2329 if (format
== GL_RGB
) {
2330 GLushort
*dst
= (GLushort
*) destination
;
2332 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) << 11)
2333 | (((GLint
) (green
[i
] * 31.0F
)) << 6)
2334 | (((GLint
) (blue
[i
] * 31.0F
)) << 1)
2335 | (((GLint
) (alpha
[i
] * 1.0F
)) );
2339 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
2340 if (format
== GL_RGB
) {
2341 GLushort
*dst
= (GLushort
*) destination
;
2343 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) )
2344 | (((GLint
) (green
[i
] * 31.0F
)) << 5)
2345 | (((GLint
) (blue
[i
] * 31.0F
)) << 10)
2346 | (((GLint
) (alpha
[i
] * 1.0F
)) << 15);
2350 case GL_UNSIGNED_INT_8_8_8_8
:
2351 if (format
== GL_RGBA
) {
2352 GLuint
*dst
= (GLuint
*) destination
;
2354 dst
[i
] = (((GLuint
) (red
[i
] * 255.0F
)) << 24)
2355 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
2356 | (((GLuint
) (blue
[i
] * 255.0F
)) << 8)
2357 | (((GLuint
) (alpha
[i
] * 255.0F
)) );
2360 else if (format
== GL_BGRA
) {
2361 GLuint
*dst
= (GLuint
*) destination
;
2363 dst
[i
] = (((GLuint
) (blue
[i
] * 255.0F
)) << 24)
2364 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
2365 | (((GLuint
) (red
[i
] * 255.0F
)) << 8)
2366 | (((GLuint
) (alpha
[i
] * 255.0F
)) );
2369 else if (format
== GL_ABGR_EXT
) {
2370 GLuint
*dst
= (GLuint
*) destination
;
2372 dst
[i
] = (((GLuint
) (alpha
[i
] * 255.0F
)) << 24)
2373 | (((GLuint
) (blue
[i
] * 255.0F
)) << 16)
2374 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
2375 | (((GLuint
) (red
[i
] * 255.0F
)) );
2379 case GL_UNSIGNED_INT_8_8_8_8_REV
:
2380 if (format
== GL_RGBA
) {
2381 GLuint
*dst
= (GLuint
*) destination
;
2383 dst
[i
] = (((GLuint
) (red
[i
] * 255.0F
)) )
2384 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
2385 | (((GLuint
) (blue
[i
] * 255.0F
)) << 16)
2386 | (((GLuint
) (alpha
[i
] * 255.0F
)) << 24);
2389 else if (format
== GL_BGRA
) {
2390 GLuint
*dst
= (GLuint
*) destination
;
2392 dst
[i
] = (((GLuint
) (blue
[i
] * 255.0F
)) )
2393 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
2394 | (((GLuint
) (red
[i
] * 255.0F
)) << 16)
2395 | (((GLuint
) (alpha
[i
] * 255.0F
)) << 24);
2398 else if (format
== GL_ABGR_EXT
) {
2399 GLuint
*dst
= (GLuint
*) destination
;
2401 dst
[i
] = (((GLuint
) (alpha
[i
] * 255.0F
)) )
2402 | (((GLuint
) (blue
[i
] * 255.0F
)) << 8)
2403 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
2404 | (((GLuint
) (red
[i
] * 255.0F
)) << 24);
2408 case GL_UNSIGNED_INT_10_10_10_2
:
2409 if (format
== GL_RGBA
) {
2410 GLuint
*dst
= (GLuint
*) destination
;
2412 dst
[i
] = (((GLuint
) (red
[i
] * 1023.0F
)) << 22)
2413 | (((GLuint
) (green
[i
] * 1023.0F
)) << 12)
2414 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 2)
2415 | (((GLuint
) (alpha
[i
] * 3.0F
)) );
2418 else if (format
== GL_BGRA
) {
2419 GLuint
*dst
= (GLuint
*) destination
;
2421 dst
[i
] = (((GLuint
) (blue
[i
] * 1023.0F
)) << 22)
2422 | (((GLuint
) (green
[i
] * 1023.0F
)) << 12)
2423 | (((GLuint
) (red
[i
] * 1023.0F
)) << 2)
2424 | (((GLuint
) (alpha
[i
] * 3.0F
)) );
2427 else if (format
== GL_ABGR_EXT
) {
2428 GLuint
*dst
= (GLuint
*) destination
;
2430 dst
[i
] = (((GLuint
) (alpha
[i
] * 1023.0F
)) << 22)
2431 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 12)
2432 | (((GLuint
) (green
[i
] * 1023.0F
)) << 2)
2433 | (((GLuint
) (red
[i
] * 3.0F
)) );
2437 case GL_UNSIGNED_INT_2_10_10_10_REV
:
2438 if (format
== GL_RGBA
) {
2439 GLuint
*dst
= (GLuint
*) destination
;
2441 dst
[i
] = (((GLuint
) (red
[i
] * 1023.0F
)) )
2442 | (((GLuint
) (green
[i
] * 1023.0F
)) << 10)
2443 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 20)
2444 | (((GLuint
) (alpha
[i
] * 3.0F
)) << 30);
2447 else if (format
== GL_BGRA
) {
2448 GLuint
*dst
= (GLuint
*) destination
;
2450 dst
[i
] = (((GLuint
) (blue
[i
] * 1023.0F
)) )
2451 | (((GLuint
) (green
[i
] * 1023.0F
)) << 10)
2452 | (((GLuint
) (red
[i
] * 1023.0F
)) << 20)
2453 | (((GLuint
) (alpha
[i
] * 3.0F
)) << 30);
2456 else if (format
== GL_ABGR_EXT
) {
2457 GLuint
*dst
= (GLuint
*) destination
;
2459 dst
[i
] = (((GLuint
) (alpha
[i
] * 1023.0F
)) )
2460 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 10)
2461 | (((GLuint
) (green
[i
] * 1023.0F
)) << 20)
2462 | (((GLuint
) (red
[i
] * 3.0F
)) << 30);
2467 gl_problem( ctx
, "bad type in gl_pack_rgba_span" );
2475 * New (3.3) functions
2478 #define SWAP2BYTE(VALUE) \
2480 GLubyte *bytes = (GLubyte *) &(VALUE); \
2481 GLubyte tmp = bytes[0]; \
2482 bytes[0] = bytes[1]; \
2486 #define SWAP4BYTE(VALUE) \
2488 GLubyte *bytes = (GLubyte *) &(VALUE); \
2489 GLubyte tmp = bytes[0]; \
2490 bytes[0] = bytes[3]; \
2493 bytes[1] = bytes[2]; \
2499 extract_uint_indexes(GLuint n
, GLuint indexes
[],
2500 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
2501 const struct gl_pixelstore_attrib
*unpack
)
2503 assert(srcFormat
== GL_COLOR_INDEX
);
2505 ASSERT(srcType
== GL_BITMAP
||
2506 srcType
== GL_UNSIGNED_BYTE
||
2507 srcType
== GL_BYTE
||
2508 srcType
== GL_UNSIGNED_SHORT
||
2509 srcType
== GL_SHORT
||
2510 srcType
== GL_UNSIGNED_INT
||
2511 srcType
== GL_INT
||
2512 srcType
== GL_FLOAT
);
2517 GLubyte
*ubsrc
= (GLubyte
*) src
;
2518 if (unpack
->LsbFirst
) {
2519 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
2521 for (i
= 0; i
< n
; i
++) {
2522 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
2533 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
2535 for (i
= 0; i
< n
; i
++) {
2536 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
2548 case GL_UNSIGNED_BYTE
:
2551 const GLubyte
*s
= (const GLubyte
*) src
;
2552 for (i
= 0; i
< n
; i
++)
2559 const GLbyte
*s
= (const GLbyte
*) src
;
2560 for (i
= 0; i
< n
; i
++)
2564 case GL_UNSIGNED_SHORT
:
2567 const GLushort
*s
= (const GLushort
*) src
;
2568 if (unpack
->SwapBytes
) {
2569 for (i
= 0; i
< n
; i
++) {
2570 GLushort value
= s
[i
];
2576 for (i
= 0; i
< n
; i
++)
2584 const GLshort
*s
= (const GLshort
*) src
;
2585 if (unpack
->SwapBytes
) {
2586 for (i
= 0; i
< n
; i
++) {
2587 GLshort value
= s
[i
];
2593 for (i
= 0; i
< n
; i
++)
2598 case GL_UNSIGNED_INT
:
2601 const GLuint
*s
= (const GLuint
*) src
;
2602 if (unpack
->SwapBytes
) {
2603 for (i
= 0; i
< n
; i
++) {
2604 GLuint value
= s
[i
];
2610 for (i
= 0; i
< n
; i
++)
2618 const GLint
*s
= (const GLint
*) src
;
2619 if (unpack
->SwapBytes
) {
2620 for (i
= 0; i
< n
; i
++) {
2627 for (i
= 0; i
< n
; i
++)
2635 const GLfloat
*s
= (const GLfloat
*) src
;
2636 if (unpack
->SwapBytes
) {
2637 for (i
= 0; i
< n
; i
++) {
2638 GLfloat value
= s
[i
];
2644 for (i
= 0; i
< n
; i
++)
2650 gl_problem(NULL
, "bad srcType in extract_uint_indexes");
2658 * This function extracts floating point RGBA values from arbitrary
2659 * image data. srcFormat and srcType are the format and type parameters
2660 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2662 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2663 * implements the "Conversion to floating point", "Conversion to RGB",
2664 * and "Final Expansion to RGBA" operations.
2666 * Args: n - number of pixels
2667 * rgba - output colors
2668 * srcFormat - format of incoming data
2669 * srcType - datatype of incoming data
2670 * src - source data pointer
2671 * swapBytes - perform byteswapping of incoming data?
2674 extract_float_rgba(GLuint n
, GLfloat rgba
[][4],
2675 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
2676 GLboolean swapBytes
)
2678 GLint redIndex
, greenIndex
, blueIndex
, alphaIndex
;
2680 GLint rComp
, bComp
, gComp
, aComp
;
2685 for (i
= 0; i
<n
;i
++) {
2686 rgba
[i
][0] = rgba
[i
][1] = rgba
[i
][2] = rgba
[i
][3] = 0;
2692 ASSERT(srcFormat
== GL_RED
||
2693 srcFormat
== GL_GREEN
||
2694 srcFormat
== GL_BLUE
||
2695 srcFormat
== GL_ALPHA
||
2696 srcFormat
== GL_LUMINANCE
||
2697 srcFormat
== GL_LUMINANCE_ALPHA
||
2698 srcFormat
== GL_INTENSITY
||
2699 srcFormat
== GL_RGB
||
2700 srcFormat
== GL_BGR
||
2701 srcFormat
== GL_RGBA
||
2702 srcFormat
== GL_BGRA
||
2703 srcFormat
== GL_ABGR_EXT
);
2705 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
2706 srcType
== GL_BYTE
||
2707 srcType
== GL_UNSIGNED_SHORT
||
2708 srcType
== GL_SHORT
||
2709 srcType
== GL_UNSIGNED_INT
||
2710 srcType
== GL_INT
||
2711 srcType
== GL_FLOAT
||
2712 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2713 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2714 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2715 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2716 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2717 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2718 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2719 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2720 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2721 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2722 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2723 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
);
2725 switch (srcFormat
) {
2728 greenIndex
= blueIndex
= alphaIndex
= -1;
2733 redIndex
= blueIndex
= alphaIndex
= -1;
2738 redIndex
= greenIndex
= alphaIndex
= -1;
2742 redIndex
= greenIndex
= blueIndex
= -1;
2747 redIndex
= greenIndex
= blueIndex
= 0;
2751 case GL_LUMINANCE_ALPHA
:
2752 redIndex
= greenIndex
= blueIndex
= 0;
2758 greenIndex
= blueIndex
= alphaIndex
= -1;
2809 gl_problem(NULL
, "bad srcFormat in extract float data");
2813 assert(redIndex
>= -1 && redIndex
<= 4);
2814 assert(greenIndex
>= -1 && greenIndex
<= 4);
2815 assert(blueIndex
>= -1 && blueIndex
<= 4);
2816 assert(alphaIndex
>= -1 && alphaIndex
<= 4);
2818 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
2819 if ((INDEX) < 0) { \
2821 for (i = 0; i < n; i++) { \
2822 rgba[i][CHANNEL] = DEFAULT; \
2825 else if (swapBytes) { \
2826 const TYPE *s = (const TYPE *) src; \
2828 for (i = 0; i < n; i++) { \
2829 TYPE value = s[INDEX]; \
2830 if (sizeof(TYPE) == 2) { \
2833 else if (sizeof(TYPE) == 4) { \
2836 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
2841 const TYPE *s = (const TYPE *) src; \
2843 for (i = 0; i < n; i++) { \
2844 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
2850 case GL_UNSIGNED_BYTE
:
2851 PROCESS(redIndex
, RCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
2852 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
2853 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
2854 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLubyte
, UBYTE_TO_FLOAT
);
2857 PROCESS(redIndex
, RCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
2858 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
2859 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
2860 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLbyte
, BYTE_TO_FLOAT
);
2862 case GL_UNSIGNED_SHORT
:
2863 PROCESS(redIndex
, RCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
2864 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
2865 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
2866 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLushort
, USHORT_TO_FLOAT
);
2869 PROCESS(redIndex
, RCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
2870 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
2871 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
2872 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLshort
, SHORT_TO_FLOAT
);
2874 case GL_UNSIGNED_INT
:
2875 PROCESS(redIndex
, RCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
2876 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
2877 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
2878 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLuint
, UINT_TO_FLOAT
);
2881 PROCESS(redIndex
, RCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
2882 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
2883 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
2884 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLint
, INT_TO_FLOAT
);
2887 PROCESS(redIndex
, RCOMP
, 0.0F
, GLfloat
, (GLfloat
));
2888 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLfloat
, (GLfloat
));
2889 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLfloat
, (GLfloat
));
2890 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLfloat
, (GLfloat
));
2892 case GL_UNSIGNED_BYTE_3_3_2
:
2894 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
2896 for (i
= 0; i
< n
; i
++) {
2897 GLubyte p
= ubsrc
[i
];
2898 rgba
[i
][RCOMP
] = ((p
>> 5) ) * (1.0F
/ 7.0F
);
2899 rgba
[i
][GCOMP
] = ((p
>> 2) & 0x7) * (1.0F
/ 7.0F
);
2900 rgba
[i
][BCOMP
] = ((p
) & 0x3) * (1.0F
/ 3.0F
);
2901 rgba
[i
][ACOMP
] = 1.0F
;
2905 case GL_UNSIGNED_BYTE_2_3_3_REV
:
2907 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
2909 for (i
= 0; i
< n
; i
++) {
2910 GLubyte p
= ubsrc
[i
];
2911 rgba
[i
][RCOMP
] = ((p
) & 0x7) * (1.0F
/ 7.0F
);
2912 rgba
[i
][GCOMP
] = ((p
>> 3) & 0x7) * (1.0F
/ 7.0F
);
2913 rgba
[i
][BCOMP
] = ((p
>> 6) ) * (1.0F
/ 3.0F
);
2914 rgba
[i
][ACOMP
] = 1.0F
;
2918 case GL_UNSIGNED_SHORT_5_6_5
:
2920 const GLushort
*ussrc
= (const GLushort
*) src
;
2922 for (i
= 0; i
< n
; i
++) {
2923 GLushort p
= ussrc
[i
];
2925 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
2926 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
2927 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
2928 rgba
[i
][ACOMP
] = 1.0F
;
2932 const GLushort
*ussrc
= (const GLushort
*) src
;
2934 for (i
= 0; i
< n
; i
++) {
2935 GLushort p
= ussrc
[i
];
2936 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
2937 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
2938 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
2939 rgba
[i
][ACOMP
] = 1.0F
;
2943 case GL_UNSIGNED_SHORT_5_6_5_REV
:
2945 const GLushort
*ussrc
= (const GLushort
*) src
;
2947 for (i
= 0; i
< n
; i
++) {
2948 GLushort p
= ussrc
[i
];
2950 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
2951 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
2952 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
2953 rgba
[i
][ACOMP
] = 1.0F
;
2957 const GLushort
*ussrc
= (const GLushort
*) src
;
2959 for (i
= 0; i
< n
; i
++) {
2960 GLushort p
= ussrc
[i
];
2961 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
2962 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
2963 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
2964 rgba
[i
][ACOMP
] = 1.0F
;
2968 case GL_UNSIGNED_SHORT_4_4_4_4
:
2970 const GLushort
*ussrc
= (const GLushort
*) src
;
2972 for (i
= 0; i
< n
; i
++) {
2973 GLushort p
= ussrc
[i
];
2975 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
2976 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
2977 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
2978 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
2982 const GLushort
*ussrc
= (const GLushort
*) src
;
2984 for (i
= 0; i
< n
; i
++) {
2985 GLushort p
= ussrc
[i
];
2986 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
2987 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
2988 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
2989 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
2993 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
2995 const GLushort
*ussrc
= (const GLushort
*) src
;
2997 for (i
= 0; i
< n
; i
++) {
2998 GLushort p
= ussrc
[i
];
3000 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
3001 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
3002 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
3003 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
3007 const GLushort
*ussrc
= (const GLushort
*) src
;
3009 for (i
= 0; i
< n
; i
++) {
3010 GLushort p
= ussrc
[i
];
3011 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
3012 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
3013 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
3014 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
3018 case GL_UNSIGNED_SHORT_5_5_5_1
:
3020 const GLushort
*ussrc
= (const GLushort
*) src
;
3022 for (i
= 0; i
< n
; i
++) {
3023 GLushort p
= ussrc
[i
];
3025 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
3026 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
3027 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
3028 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
3032 const GLushort
*ussrc
= (const GLushort
*) src
;
3034 for (i
= 0; i
< n
; i
++) {
3035 GLushort p
= ussrc
[i
];
3036 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
3037 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
3038 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
3039 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
3043 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
3045 const GLushort
*ussrc
= (const GLushort
*) src
;
3047 for (i
= 0; i
< n
; i
++) {
3048 GLushort p
= ussrc
[i
];
3050 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
3051 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
3052 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
3053 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
3057 const GLushort
*ussrc
= (const GLushort
*) src
;
3059 for (i
= 0; i
< n
; i
++) {
3060 GLushort p
= ussrc
[i
];
3061 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
3062 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
3063 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
3064 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
3068 case GL_UNSIGNED_INT_8_8_8_8
:
3070 const GLuint
*uisrc
= (const GLuint
*) src
;
3072 for (i
= 0; i
< n
; i
++) {
3073 GLuint p
= uisrc
[i
];
3074 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
3075 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
3076 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
3077 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
3081 const GLuint
*uisrc
= (const GLuint
*) src
;
3083 for (i
= 0; i
< n
; i
++) {
3084 GLuint p
= uisrc
[i
];
3085 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
3086 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
3087 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
3088 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
3092 case GL_UNSIGNED_INT_8_8_8_8_REV
:
3094 const GLuint
*uisrc
= (const GLuint
*) src
;
3096 for (i
= 0; i
< n
; i
++) {
3097 GLuint p
= uisrc
[i
];
3098 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
3099 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
3100 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
3101 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
3105 const GLuint
*uisrc
= (const GLuint
*) src
;
3107 for (i
= 0; i
< n
; i
++) {
3108 GLuint p
= uisrc
[i
];
3109 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
3110 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
3111 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
3112 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
3116 case GL_UNSIGNED_INT_10_10_10_2
:
3118 const GLuint
*uisrc
= (const GLuint
*) src
;
3120 for (i
= 0; i
< n
; i
++) {
3121 GLuint p
= uisrc
[i
];
3123 rgba
[i
][rComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
3124 rgba
[i
][gComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
3125 rgba
[i
][bComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
3126 rgba
[i
][aComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
3130 const GLuint
*uisrc
= (const GLuint
*) src
;
3132 for (i
= 0; i
< n
; i
++) {
3133 GLuint p
= uisrc
[i
];
3134 rgba
[i
][rComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
3135 rgba
[i
][gComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
3136 rgba
[i
][bComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
3137 rgba
[i
][aComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
3141 case GL_UNSIGNED_INT_2_10_10_10_REV
:
3143 const GLuint
*uisrc
= (const GLuint
*) src
;
3145 for (i
= 0; i
< n
; i
++) {
3146 GLuint p
= uisrc
[i
];
3148 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
3149 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
3150 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
3151 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
3155 const GLuint
*uisrc
= (const GLuint
*) src
;
3157 for (i
= 0; i
< n
; i
++) {
3158 GLuint p
= uisrc
[i
];
3159 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
3160 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
3161 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
3162 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
3167 gl_problem(NULL
, "bad srcType in extract float data");
3175 * Unpack a row of color image data from a client buffer according to
3176 * the pixel unpacking parameters. Apply any enabled pixel transfer
3177 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
3178 * Return GLubyte values in the specified dest image format.
3179 * This is (or will be) used by glDrawPixels and glTexImage?D().
3180 * Input: ctx - the context
3181 * n - number of pixels in the span
3182 * dstFormat - format of destination color array
3183 * dest - the destination color array
3184 * srcFormat - source image format
3185 * srcType - source image datatype
3186 * source - source image pointer
3187 * unpacking - pixel unpacking parameters
3188 * applyTransferOps - apply scale/bias/lookup-table ops?
3190 * XXX perhaps expand this to process whole images someday.
3193 _mesa_unpack_ubyte_color_span( const GLcontext
*ctx
,
3194 GLuint n
, GLenum dstFormat
, GLubyte dest
[],
3195 GLenum srcFormat
, GLenum srcType
,
3196 const GLvoid
*source
,
3197 const struct gl_pixelstore_attrib
*unpacking
,
3198 GLboolean applyTransferOps
)
3200 ASSERT(dstFormat
== GL_ALPHA
||
3201 dstFormat
== GL_LUMINANCE
||
3202 dstFormat
== GL_LUMINANCE_ALPHA
||
3203 dstFormat
== GL_INTENSITY
||
3204 dstFormat
== GL_RGB
||
3205 dstFormat
== GL_RGBA
||
3206 dstFormat
== GL_COLOR_INDEX
);
3208 ASSERT(srcFormat
== GL_RED
||
3209 srcFormat
== GL_GREEN
||
3210 srcFormat
== GL_BLUE
||
3211 srcFormat
== GL_ALPHA
||
3212 srcFormat
== GL_LUMINANCE
||
3213 srcFormat
== GL_LUMINANCE_ALPHA
||
3214 srcFormat
== GL_INTENSITY
||
3215 srcFormat
== GL_RGB
||
3216 srcFormat
== GL_BGR
||
3217 srcFormat
== GL_RGBA
||
3218 srcFormat
== GL_BGRA
||
3219 srcFormat
== GL_ABGR_EXT
||
3220 srcFormat
== GL_COLOR_INDEX
);
3222 ASSERT(srcType
== GL_BITMAP
||
3223 srcType
== GL_UNSIGNED_BYTE
||
3224 srcType
== GL_BYTE
||
3225 srcType
== GL_UNSIGNED_SHORT
||
3226 srcType
== GL_SHORT
||
3227 srcType
== GL_UNSIGNED_INT
||
3228 srcType
== GL_INT
||
3229 srcType
== GL_FLOAT
||
3230 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
3231 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
3232 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
3233 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
3234 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
3235 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
3236 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
3237 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
3238 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
3239 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
3240 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
3241 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
);
3243 /* this is intended for RGBA mode */
3244 ASSERT(ctx
->Visual
->RGBAflag
);
3246 applyTransferOps
&= (ctx
->Pixel
.ScaleOrBiasRGBA
||
3247 ctx
->Pixel
.MapColorFlag
);
3249 /* Try simple cases first */
3250 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
) {
3251 if (dstFormat
== GL_RGBA
) {
3252 if (srcFormat
== GL_RGBA
) {
3253 MEMCPY( dest
, source
, n
* 4 * sizeof(GLubyte
) );
3256 else if (srcFormat
== GL_RGB
) {
3258 const GLubyte
*src
= (const GLubyte
*) source
;
3259 GLubyte
*dst
= dest
;
3260 for (i
= 0; i
< n
; i
++) {
3271 else if (dstFormat
== GL_RGB
) {
3272 if (srcFormat
== GL_RGB
) {
3273 MEMCPY( dest
, source
, n
* 3 * sizeof(GLubyte
) );
3276 else if (srcFormat
== GL_RGBA
) {
3278 const GLubyte
*src
= (const GLubyte
*) source
;
3279 GLubyte
*dst
= dest
;
3280 for (i
= 0; i
< n
; i
++) {
3290 else if (dstFormat
== srcFormat
) {
3291 GLint comps
= gl_components_in_format(srcFormat
);
3293 MEMCPY( dest
, source
, n
* comps
* sizeof(GLubyte
) );
3300 /* general solution */
3301 GLfloat rgba
[MAX_WIDTH
][4];
3302 GLint dstComponents
;
3303 GLint dstRedIndex
, dstGreenIndex
, dstBlueIndex
, dstAlphaIndex
;
3304 GLint dstLuminanceIndex
, dstIntensityIndex
;
3306 dstComponents
= gl_components_in_format( dstFormat
);
3307 /* source & dest image formats should have been error checked by now */
3308 assert(dstComponents
> 0);
3311 * Extract image data and convert to RGBA floats
3313 assert(n
<= MAX_WIDTH
);
3314 if (srcFormat
== GL_COLOR_INDEX
) {
3315 GLuint indexes
[MAX_WIDTH
];
3316 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
3319 /* shift and offset indexes */
3320 gl_shift_and_offset_ci(ctx
, n
, indexes
);
3322 if (dstFormat
== GL_COLOR_INDEX
) {
3323 if (applyTransferOps
) {
3324 if (ctx
->Pixel
.MapColorFlag
) {
3325 /* Apply lookup table */
3326 gl_map_ci(ctx
, n
, indexes
);
3329 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
3334 /* convert to GLubyte and return */
3337 for (i
= 0; i
< n
; i
++) {
3338 dest
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
3343 /* Convert indexes to RGBA */
3344 gl_map_ci_to_rgba_float(ctx
, n
, indexes
, rgba
);
3348 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
3349 unpacking
->SwapBytes
);
3351 if (applyTransferOps
) {
3352 /* scale and bias colors */
3353 gl_scale_and_bias_rgba_float(ctx
, n
, rgba
);
3355 /* color table lookup */
3356 if (ctx
->Pixel
.MapColorFlag
) {
3357 gl_map_rgba_float(ctx
, n
, rgba
);
3364 * XXX This is where more color table lookups, convolution,
3365 * histograms, minmax, color matrix, etc would take place if
3367 * See figure 3.7 in the OpenGL 1.2 specification for more info.
3371 /* clamp to [0,1] */
3374 for (i
= 0; i
< n
; i
++) {
3375 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
3376 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
3377 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
3378 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
3382 /* Now determine which color channels we need to produce.
3383 * And determine the dest index (offset) within each color tuple.
3385 switch (dstFormat
) {
3388 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= -1;
3389 dstLuminanceIndex
= dstIntensityIndex
= -1;
3392 dstLuminanceIndex
= 0;
3393 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= dstAlphaIndex
= -1;
3394 dstIntensityIndex
= -1;
3396 case GL_LUMINANCE_ALPHA
:
3397 dstLuminanceIndex
= 0;
3399 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= -1;
3400 dstIntensityIndex
= -1;
3403 dstIntensityIndex
= 0;
3404 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= dstAlphaIndex
= -1;
3405 dstLuminanceIndex
= -1;
3411 dstAlphaIndex
= dstLuminanceIndex
= dstIntensityIndex
= -1;
3418 dstLuminanceIndex
= dstIntensityIndex
= -1;
3420 case GL_COLOR_INDEX
:
3424 gl_problem(ctx
, "bad dstFormat in _mesa_unpack_ubyte_span()");
3428 /* Now return the GLubyte data in the requested dstFormat */
3429 if (dstRedIndex
>= 0) {
3430 GLubyte
*dst
= dest
;
3432 for (i
= 0; i
< n
; i
++) {
3433 dst
[dstRedIndex
] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
3434 dst
+= dstComponents
;
3438 if (dstGreenIndex
>= 0) {
3439 GLubyte
*dst
= dest
;
3441 for (i
= 0; i
< n
; i
++) {
3442 dst
[dstGreenIndex
] = FLOAT_TO_UBYTE(rgba
[i
][GCOMP
]);
3443 dst
+= dstComponents
;
3447 if (dstBlueIndex
>= 0) {
3448 GLubyte
*dst
= dest
;
3450 for (i
= 0; i
< n
; i
++) {
3451 dst
[dstBlueIndex
] = FLOAT_TO_UBYTE(rgba
[i
][BCOMP
]);
3452 dst
+= dstComponents
;
3456 if (dstAlphaIndex
>= 0) {
3457 GLubyte
*dst
= dest
;
3459 for (i
= 0; i
< n
; i
++) {
3460 dst
[dstAlphaIndex
] = FLOAT_TO_UBYTE(rgba
[i
][ACOMP
]);
3461 dst
+= dstComponents
;
3465 if (dstIntensityIndex
>= 0) {
3466 GLubyte
*dst
= dest
;
3468 assert(dstIntensityIndex
== 0);
3469 assert(dstComponents
== 1);
3470 for (i
= 0; i
< n
; i
++) {
3471 /* Intensity comes from red channel */
3472 dst
[i
] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
3476 if (dstLuminanceIndex
>= 0) {
3477 GLubyte
*dst
= dest
;
3479 assert(dstLuminanceIndex
== 0);
3480 for (i
= 0; i
< n
; i
++) {
3481 /* Luminance comes from red channel */
3482 dst
[0] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
3483 dst
+= dstComponents
;
3492 * Unpack a row of color index data from a client buffer according to
3493 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
3494 * and applyTransferOps is true.
3495 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3497 * Args: ctx - the context
3498 * n - number of pixels
3499 * dstType - destination datatype
3500 * dest - destination array
3501 * srcType - source pixel type
3502 * source - source data pointer
3503 * unpacking - pixel unpacking parameters
3504 * applyTransferOps - apply offset/bias/lookup ops?
3507 _mesa_unpack_index_span( const GLcontext
*ctx
, GLuint n
,
3508 GLenum dstType
, GLvoid
*dest
,
3509 GLenum srcType
, const GLvoid
*source
,
3510 const struct gl_pixelstore_attrib
*unpacking
,
3511 GLboolean applyTransferOps
)
3513 ASSERT(srcType
== GL_BITMAP
||
3514 srcType
== GL_UNSIGNED_BYTE
||
3515 srcType
== GL_BYTE
||
3516 srcType
== GL_UNSIGNED_SHORT
||
3517 srcType
== GL_SHORT
||
3518 srcType
== GL_UNSIGNED_INT
||
3519 srcType
== GL_INT
||
3520 srcType
== GL_FLOAT
);
3522 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
3523 dstType
== GL_UNSIGNED_SHORT
||
3524 dstType
== GL_UNSIGNED_INT
);
3526 applyTransferOps
&= (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapColorFlag
);
3529 * Try simple cases first
3531 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
3532 && dstType
== GL_UNSIGNED_BYTE
) {
3533 MEMCPY(dest
, source
, n
* sizeof(GLubyte
));
3535 else if (!applyTransferOps
&& srcType
== GL_UNSIGNED_INT
3536 && dstType
== GL_UNSIGNED_INT
&& !unpacking
->SwapBytes
) {
3537 MEMCPY(dest
, source
, n
* sizeof(GLuint
));
3543 GLuint indexes
[MAX_WIDTH
];
3544 assert(n
<= MAX_WIDTH
);
3546 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
3549 if (applyTransferOps
) {
3550 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
3551 /* shift and offset indexes */
3552 gl_shift_and_offset_ci(ctx
, n
, indexes
);
3555 if (ctx
->Pixel
.MapColorFlag
) {
3556 /* Apply lookup table */
3557 gl_map_ci(ctx
, n
, indexes
);
3561 /* convert to dest type */
3563 case GL_UNSIGNED_BYTE
:
3565 GLubyte
*dst
= (GLubyte
*) dest
;
3567 for (i
= 0; i
< n
; i
++) {
3568 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
3572 case GL_UNSIGNED_SHORT
:
3574 GLuint
*dst
= (GLuint
*) dest
;
3576 for (i
= 0; i
< n
; i
++) {
3577 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
3581 case GL_UNSIGNED_INT
:
3582 MEMCPY(dest
, indexes
, n
* sizeof(GLuint
));
3585 gl_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
3592 * Unpack image data. Apply byteswapping, byte flipping (bitmap).
3593 * Return all image data in a contiguous block.
3596 _mesa_unpack_image( GLsizei width
, GLsizei height
, GLsizei depth
,
3597 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3598 const struct gl_pixelstore_attrib
*unpack
)
3600 GLint bytesPerRow
, compsPerRow
;
3601 GLboolean flipBytes
, swap2
, swap4
;
3604 return NULL
; /* not necessarily an error */
3606 if (width
<= 0 || height
<= 0 || depth
<= 0)
3607 return NULL
; /* generate error later */
3609 if (format
== GL_BITMAP
) {
3610 bytesPerRow
= (width
+ 7) >> 3;
3611 flipBytes
= !unpack
->LsbFirst
;
3612 swap2
= swap4
= GL_FALSE
;
3616 const GLint bytesPerPixel
= gl_bytes_per_pixel(format
, type
);
3617 const GLint components
= gl_components_in_format(format
);
3619 if (bytesPerPixel
<= 0 || components
<= 0)
3620 return NULL
; /* bad format or type. generate error later */
3621 bytesPerRow
= bytesPerPixel
* width
;
3622 bytesPerComp
= bytesPerPixel
/ components
;
3623 flipBytes
= GL_FALSE
;
3624 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
3625 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
3626 compsPerRow
= components
* width
;
3627 assert(compsPerRow
>= width
);
3631 GLubyte
*destBuffer
= MALLOC(bytesPerRow
* height
* depth
);
3635 return NULL
; /* generate GL_OUT_OF_MEMORY later */
3638 for (img
= 0; img
< depth
; img
++) {
3639 for (row
= 0; row
< height
; row
++) {
3640 const GLvoid
*src
= gl_pixel_addr_in_image(unpack
, pixels
,
3641 width
, height
, format
, type
, img
, row
, 0);
3642 MEMCPY(dst
, src
, bytesPerRow
);
3643 /* byte flipping/swapping */
3645 gl_flip_bytes((GLubyte
*) dst
, bytesPerRow
);
3648 gl_swap2((GLushort
*) dst
, compsPerRow
);
3651 gl_swap4((GLuint
*) dst
, compsPerRow
);