1 /* $Id: image.c,v 1.11 1999/11/03 17:27:05 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"
46 #include "GL/xf86glx.h"
53 * Flip the 8 bits in each byte of the given array.
55 void gl_flip_bytes( GLubyte
*p
, GLuint n
)
57 register GLuint i
, a
, b
;
61 a
= ((b
& 0x01) << 7) |
75 * Flip the order of the 2 bytes in each word in the given array.
77 void gl_swap2( GLushort
*p
, GLuint n
)
82 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
89 * Flip the order of the 4 bytes in each word in the given array.
91 void gl_swap4( GLuint
*p
, GLuint n
)
93 register GLuint i
, a
, b
;
99 | ((b
<< 8) & 0xff0000)
100 | ((b
<< 24) & 0xff000000);
109 * Return the size, in bytes, of the given GL datatype.
110 * Return 0 if GL_BITMAP.
111 * Return -1 if invalid type enum.
113 GLint
gl_sizeof_type( GLenum type
)
118 case GL_UNSIGNED_BYTE
:
119 return sizeof(GLubyte
);
121 return sizeof(GLbyte
);
122 case GL_UNSIGNED_SHORT
:
123 return sizeof(GLushort
);
125 return sizeof(GLshort
);
126 case GL_UNSIGNED_INT
:
127 return sizeof(GLuint
);
129 return sizeof(GLint
);
131 return sizeof(GLfloat
);
139 * Same as gl_sizeof_packed_type() but we also accept the
140 * packed pixel format datatypes.
142 GLint
gl_sizeof_packed_type( GLenum type
)
147 case GL_UNSIGNED_BYTE
:
148 return sizeof(GLubyte
);
150 return sizeof(GLbyte
);
151 case GL_UNSIGNED_SHORT
:
152 return sizeof(GLushort
);
154 return sizeof(GLshort
);
155 case GL_UNSIGNED_INT
:
156 return sizeof(GLuint
);
158 return sizeof(GLint
);
160 return sizeof(GLfloat
);
161 case GL_UNSIGNED_BYTE_3_3_2
:
162 return sizeof(GLubyte
);
163 case GL_UNSIGNED_BYTE_2_3_3_REV
:
164 return sizeof(GLubyte
);
165 case GL_UNSIGNED_SHORT_5_6_5
:
166 return sizeof(GLshort
);
167 case GL_UNSIGNED_SHORT_5_6_5_REV
:
168 return sizeof(GLshort
);
169 case GL_UNSIGNED_SHORT_4_4_4_4
:
170 return sizeof(GLshort
);
171 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
172 return sizeof(GLshort
);
173 case GL_UNSIGNED_SHORT_5_5_5_1
:
174 return sizeof(GLshort
);
175 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
176 return sizeof(GLshort
);
177 case GL_UNSIGNED_INT_8_8_8_8
:
178 return sizeof(GLuint
);
179 case GL_UNSIGNED_INT_8_8_8_8_REV
:
180 return sizeof(GLuint
);
181 case GL_UNSIGNED_INT_10_10_10_2
:
182 return sizeof(GLuint
);
183 case GL_UNSIGNED_INT_2_10_10_10_REV
:
184 return sizeof(GLuint
);
193 * Return the number of components in a GL enum pixel type.
194 * Return -1 if bad format.
196 GLint
gl_components_in_format( GLenum format
)
200 case GL_COLOR_INDEX1_EXT
:
201 case GL_COLOR_INDEX2_EXT
:
202 case GL_COLOR_INDEX4_EXT
:
203 case GL_COLOR_INDEX8_EXT
:
204 case GL_COLOR_INDEX12_EXT
:
205 case GL_COLOR_INDEX16_EXT
:
206 case GL_STENCIL_INDEX
:
207 case GL_DEPTH_COMPONENT
:
215 case GL_LUMINANCE_ALPHA
:
234 * Return bytes per pixel for given format and type
235 * Return -1 if bad format or type.
237 GLint
gl_bytes_per_pixel( GLenum format
, GLenum type
)
239 GLint comps
= gl_components_in_format( format
);
245 return 0; /* special case */
247 case GL_UNSIGNED_BYTE
:
248 return comps
* sizeof(GLubyte
);
250 case GL_UNSIGNED_SHORT
:
251 return comps
* sizeof(GLshort
);
253 case GL_UNSIGNED_INT
:
254 return comps
* sizeof(GLint
);
256 return comps
* sizeof(GLfloat
);
257 case GL_UNSIGNED_BYTE_3_3_2
:
258 case GL_UNSIGNED_BYTE_2_3_3_REV
:
259 if (format
== GL_RGB
|| format
== GL_BGR
)
260 return sizeof(GLubyte
);
262 return -1; /* error */
263 case GL_UNSIGNED_SHORT_5_6_5
:
264 case GL_UNSIGNED_SHORT_5_6_5_REV
:
265 if (format
== GL_RGB
|| format
== GL_BGR
)
266 return sizeof(GLshort
);
268 return -1; /* error */
269 case GL_UNSIGNED_SHORT_4_4_4_4
:
270 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
271 case GL_UNSIGNED_SHORT_5_5_5_1
:
272 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
273 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
)
274 return sizeof(GLushort
);
277 case GL_UNSIGNED_INT_8_8_8_8
:
278 case GL_UNSIGNED_INT_8_8_8_8_REV
:
279 case GL_UNSIGNED_INT_10_10_10_2
:
280 case GL_UNSIGNED_INT_2_10_10_10_REV
:
281 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
)
282 return sizeof(GLuint
);
292 * Test if the given pixel format and type are legal.
293 * Return GL_TRUE for legal, GL_FALSE for illegal.
295 GLboolean
gl_is_legal_format_and_type( GLenum format
, GLenum type
)
299 case GL_STENCIL_INDEX
:
303 case GL_UNSIGNED_BYTE
:
305 case GL_UNSIGNED_SHORT
:
307 case GL_UNSIGNED_INT
:
318 case GL_LUMINANCE_ALPHA
:
319 case GL_DEPTH_COMPONENT
:
323 case GL_UNSIGNED_BYTE
:
325 case GL_UNSIGNED_SHORT
:
327 case GL_UNSIGNED_INT
:
336 case GL_UNSIGNED_BYTE
:
338 case GL_UNSIGNED_SHORT
:
340 case GL_UNSIGNED_INT
:
342 case GL_UNSIGNED_BYTE_3_3_2
:
343 case GL_UNSIGNED_BYTE_2_3_3_REV
:
344 case GL_UNSIGNED_SHORT_5_6_5
:
345 case GL_UNSIGNED_SHORT_5_6_5_REV
:
355 case GL_UNSIGNED_BYTE
:
357 case GL_UNSIGNED_SHORT
:
359 case GL_UNSIGNED_INT
:
361 case GL_UNSIGNED_SHORT_4_4_4_4
:
362 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
363 case GL_UNSIGNED_SHORT_5_5_5_1
:
364 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
365 case GL_UNSIGNED_INT_8_8_8_8
:
366 case GL_UNSIGNED_INT_8_8_8_8_REV
:
367 case GL_UNSIGNED_INT_10_10_10_2
:
368 case GL_UNSIGNED_INT_2_10_10_10_REV
:
382 * Return the address of a pixel in an image (actually a volume).
383 * Pixel unpacking/packing parameters are observed according to 'packing'.
384 * Input: image - start of image data
385 * width, height - size of image
386 * format - image format
387 * type - pixel component type
388 * packing - the pixelstore attributes
389 * img - which image in the volume (0 for 1D or 2D images)
390 * row, column - location of pixel in the image
391 * Return: address of pixel at (image,row,column) in image or NULL if error.
393 GLvoid
*gl_pixel_addr_in_image( const struct gl_pixelstore_attrib
*packing
,
394 const GLvoid
*image
, GLsizei width
,
395 GLsizei height
, GLenum format
, GLenum type
,
396 GLint img
, GLint row
, GLint column
)
398 GLint alignment
; /* 1, 2 or 4 */
399 GLint pixels_per_row
;
400 GLint rows_per_image
;
403 GLint skipimages
; /* for 3-D volume images */
406 alignment
= packing
->Alignment
;
407 if (packing
->RowLength
> 0) {
408 pixels_per_row
= packing
->RowLength
;
411 pixels_per_row
= width
;
413 if (packing
->ImageHeight
> 0) {
414 rows_per_image
= packing
->ImageHeight
;
417 rows_per_image
= height
;
419 skiprows
= packing
->SkipRows
;
420 skippixels
= packing
->SkipPixels
;
421 skipimages
= packing
->SkipImages
;
423 if (type
==GL_BITMAP
) {
425 GLint comp_per_pixel
; /* components per pixel */
426 GLint bytes_per_comp
; /* bytes per component */
428 GLint bytes_per_image
;
430 /* Compute bytes per component */
431 bytes_per_comp
= gl_sizeof_packed_type( type
);
432 if (bytes_per_comp
<0) {
436 /* Compute number of components per pixel */
437 comp_per_pixel
= gl_components_in_format( format
);
438 if (comp_per_pixel
<0 && type
!= GL_BITMAP
) {
442 bytes_per_row
= alignment
443 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
445 bytes_per_image
= bytes_per_row
* rows_per_image
;
447 pixel_addr
= (GLubyte
*) image
448 + (skipimages
+ img
) * bytes_per_image
449 + (skiprows
+ row
) * bytes_per_row
450 + (skippixels
+ column
) / 8;
453 /* Non-BITMAP data */
454 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
456 bytes_per_pixel
= gl_bytes_per_pixel( format
, type
);
458 /* The pixel type and format should have been error checked earlier */
459 assert(bytes_per_pixel
> 0);
461 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
462 remainder
= bytes_per_row
% alignment
;
464 bytes_per_row
+= (alignment
- remainder
);
466 ASSERT(bytes_per_row
% alignment
== 0);
468 bytes_per_image
= bytes_per_row
* rows_per_image
;
470 /* compute final pixel address */
471 pixel_addr
= (GLubyte
*) image
472 + (skipimages
+ img
) * bytes_per_image
473 + (skiprows
+ row
) * bytes_per_row
474 + (skippixels
+ column
) * bytes_per_pixel
;
477 return (GLvoid
*) pixel_addr
;
483 * Allocate a new gl_image. All fields are initialized to zero.
485 static struct gl_image
*alloc_image( void )
487 return CALLOC_STRUCT(gl_image
);
493 * Allocate a new gl_image with the error flag set.
495 static struct gl_image
*alloc_error_image( GLint width
, GLint height
,
496 GLint depth
, GLenum format
,
499 struct gl_image
*image
= alloc_image();
501 image
->Width
= width
;
502 image
->Height
= height
;
503 image
->Depth
= depth
;
504 image
->Format
= format
;
506 image
->ErrorFlag
= GL_TRUE
;
516 void gl_free_image( struct gl_image
*image
)
527 * Do error checking on an image. If there's an error, register it and
528 * return GL_TRUE, else return GL_FALSE.
530 GLboolean
gl_image_error_test( GLcontext
*ctx
, const struct gl_image
*image
,
534 gl_error( ctx
, GL_OUT_OF_MEMORY
, msg
);
537 if (image
->Width
<= 0 || image
->Height
<= 0 || image
->Depth
<= 0) {
538 gl_error( ctx
, GL_INVALID_VALUE
, msg
);
541 else if (!gl_is_legal_format_and_type(image
->Format
, image
->Type
)) {
552 * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats.
553 * Input: type - datatype of src depth image
554 * Return pointer to a new gl_image structure.
556 * Notes: if the source image type is GLushort then the gl_image will
557 * also store GLushorts. If the src image type is GLuint then the gl_image
558 * will also store GLuints. For all other src image types the gl_image
559 * will store GLfloats. The integer cases can later be optimized.
561 static struct gl_image
*
562 unpack_depth_image( GLcontext
*ctx
, GLenum type
, GLint width
, GLint height
,
563 const GLvoid
*pixels
,
564 const struct gl_pixelstore_attrib
*packing
)
567 struct gl_image
*image
;
574 errorType
= type
!= GL_BYTE
&&
575 type
!= GL_UNSIGNED_BYTE
&&
577 type
!= GL_UNSIGNED_SHORT
&&
579 type
!= GL_UNSIGNED_INT
&&
582 image
= alloc_image();
584 image
->Width
= width
;
585 image
->Height
= height
;
587 image
->Components
= 1;
588 image
->Format
= GL_DEPTH_COMPONENT
;
593 if (type
==GL_UNSIGNED_SHORT
) {
594 image
->Type
= GL_UNSIGNED_SHORT
;
595 image
->Data
= MALLOC( width
* height
* sizeof(GLushort
));
597 else if (type
==GL_UNSIGNED_INT
) {
598 image
->Type
= GL_UNSIGNED_INT
;
599 image
->Data
= MALLOC( width
* height
* sizeof(GLuint
));
602 image
->Type
= GL_FLOAT
;
603 image
->Data
= MALLOC( width
* height
* sizeof(GLfloat
));
616 fDst
= (GLfloat
*) image
->Data
;
617 sDst
= (GLushort
*) image
->Data
;
618 iDst
= (GLuint
*) image
->Data
;
620 for (i
=0;i
<height
;i
++) {
621 GLvoid
*src
= gl_pixel_addr_in_image( packing
, pixels
,
623 GL_DEPTH_COMPONENT
, type
,
631 assert(image
->Type
== GL_FLOAT
);
632 for (j
=0; j
<width
; j
++) {
633 *fDst
++ = BYTE_TO_FLOAT(((GLbyte
*)src
)[j
]);
636 case GL_UNSIGNED_BYTE
:
637 assert(image
->Type
== GL_FLOAT
);
638 for (j
=0; j
<width
; j
++) {
639 *fDst
++ = UBYTE_TO_FLOAT(((GLubyte
*)src
)[j
]);
642 case GL_UNSIGNED_SHORT
:
643 assert(image
->Type
== GL_UNSIGNED_SHORT
);
644 MEMCPY( sDst
, src
, width
* sizeof(GLushort
) );
645 if (packing
->SwapBytes
) {
646 gl_swap2( sDst
, width
);
651 assert(image
->Type
== GL_FLOAT
);
652 if (packing
->SwapBytes
) {
653 for (j
=0;j
<width
;j
++) {
654 GLshort value
= ((GLshort
*)src
)[j
];
655 value
= ((value
>> 8) & 0xff) | ((value
&0xff) << 8);
656 *fDst
++ = SHORT_TO_FLOAT(value
);
660 for (j
=0;j
<width
;j
++) {
661 *fDst
++ = SHORT_TO_FLOAT(((GLshort
*)src
)[j
]);
666 assert(image
->Type
== GL_FLOAT
);
667 if (packing
->SwapBytes
) {
668 for (j
=0;j
<width
;j
++) {
669 GLint value
= ((GLint
*)src
)[j
];
670 value
= ((value
>> 24) & 0x000000ff) |
671 ((value
>> 8) & 0x0000ff00) |
672 ((value
<< 8) & 0x00ff0000) |
673 ((value
<< 24) & 0xff000000);
674 *fDst
++ = INT_TO_FLOAT(value
);
678 for (j
=0;j
<width
;j
++) {
679 *fDst
++ = INT_TO_FLOAT(((GLint
*)src
)[j
]);
684 case GL_UNSIGNED_INT
:
685 assert(image
->Type
== GL_UNSIGNED_INT
);
686 MEMCPY( iDst
, src
, width
* sizeof(GLuint
) );
687 if (packing
->SwapBytes
) {
688 gl_swap4( iDst
, width
);
693 assert(image
->Type
== GL_FLOAT
);
694 MEMCPY( fDst
, src
, width
* sizeof(GLfloat
) );
695 if (packing
->SwapBytes
) {
696 gl_swap4( (GLuint
*) fDst
, width
);
701 gl_problem(ctx
, "unpack_depth_image type" );
712 * Unpack a stencil image. Store as GLubytes in a gl_image structure.
713 * Return: pointer to new gl_image structure.
715 static struct gl_image
*
716 unpack_stencil_image( GLcontext
*ctx
, GLenum type
, GLint width
, GLint height
,
717 const GLvoid
*pixels
,
718 const struct gl_pixelstore_attrib
*packing
)
720 struct gl_image
*image
;
725 assert(sizeof(GLstencil
) == sizeof(GLubyte
));
727 errorType
= type
!= GL_BYTE
&&
728 type
!= GL_UNSIGNED_BYTE
&&
730 type
!= GL_UNSIGNED_SHORT
&&
732 type
!= GL_UNSIGNED_INT
&&
736 image
= alloc_image();
738 image
->Width
= width
;
739 image
->Height
= height
;
741 image
->Components
= 1;
742 image
->Format
= GL_STENCIL_INDEX
;
748 image
->Type
= GL_UNSIGNED_BYTE
;
749 image
->Data
= MALLOC( width
* height
* sizeof(GLubyte
));
760 return image
; /* error will be generated later */
762 dst
= (GLubyte
*) image
->Data
;
764 for (i
=0;i
<height
;i
++) {
765 GLvoid
*src
= gl_pixel_addr_in_image( packing
, pixels
,
767 GL_STENCIL_INDEX
, type
,
774 case GL_UNSIGNED_BYTE
:
776 MEMCPY( dst
, src
, width
* sizeof(GLubyte
) );
777 dst
+= width
* sizeof(GLubyte
);
779 case GL_UNSIGNED_SHORT
:
781 if (packing
->SwapBytes
) {
782 /* grab upper byte */
783 for (j
=0; j
< width
; j
++) {
784 *dst
++ = (((GLushort
*)src
)[j
] & 0xff00) >> 8;
788 for (j
=0; j
< width
; j
++) {
789 *dst
++ = (((GLushort
*)src
)[j
]) & 0xff;
794 if (packing
->SwapBytes
) {
795 /* grab upper byte */
796 for (j
=0; j
< width
; j
++) {
797 *dst
++ = (((GLuint
*)src
)[j
] & 0xff000000) >> 8;
801 for (j
=0; j
< width
; j
++) {
802 *dst
++ = (((GLuint
*)src
)[j
]) & 0xff;
806 case GL_UNSIGNED_INT
:
807 if (packing
->SwapBytes
) {
808 /* grab upper byte */
809 for (j
=0; j
< width
; j
++) {
810 *dst
++ = (((GLuint
*)src
)[j
] & 0xff000000) >> 8;
814 for (j
=0; j
< width
; j
++) {
815 *dst
++ = (((GLuint
*)src
)[j
]) & 0xff;
820 if (packing
->SwapBytes
) {
821 for (j
=0; j
< width
; j
++) {
823 GLint value
= ((GLuint
*)src
)[j
];
824 value
= ((value
& 0xff000000) >> 24)
825 | ((value
& 0x00ff0000) >> 8)
826 | ((value
& 0x0000ff00) << 8)
827 | ((value
& 0x000000ff) << 24);
828 fvalue
= *((GLfloat
*) &value
);
829 *dst
++ = ((GLint
) fvalue
) & 0xff;
833 for (j
=0; j
< width
; j
++) {
834 GLfloat fvalue
= ((GLfloat
*)src
)[j
];
835 *dst
++ = ((GLint
) fvalue
) & 0xff;
840 gl_problem(ctx
, "unpack_stencil_image type" );
851 * Unpack a bitmap, return a new gl_image struct.
853 static struct gl_image
*
854 unpack_bitmap( GLenum format
, GLint width
, GLint height
,
855 const GLvoid
*pixels
,
856 const struct gl_pixelstore_attrib
*packing
)
858 struct gl_image
*image
;
859 GLint bytes
, i
, width_in_bytes
;
860 GLubyte
*buffer
, *dst
;
862 assert(format
== GL_COLOR_INDEX
|| format
== GL_STENCIL_INDEX
);
864 /* Alloc dest storage */
865 bytes
= ((width
+7)/8 * height
);
866 if (bytes
>0 && pixels
!=NULL
) {
867 buffer
= (GLubyte
*) MALLOC( bytes
);
871 /* Copy/unpack pixel data to buffer */
872 width_in_bytes
= CEILING( width
, 8 );
874 for (i
=0; i
<height
; i
++) {
875 GLvoid
*src
= gl_pixel_addr_in_image( packing
, pixels
,
877 GL_COLOR_INDEX
, GL_BITMAP
,
883 MEMCPY( dst
, src
, width_in_bytes
);
884 dst
+= width_in_bytes
;
887 if (packing
->LsbFirst
) {
888 gl_flip_bytes( buffer
, bytes
);
892 /* a 'null' bitmap */
896 image
= alloc_image();
898 image
->Width
= width
;
899 image
->Height
= height
;
901 image
->Components
= 0;
902 image
->Format
= format
;
903 image
->Type
= GL_BITMAP
;
904 image
->Data
= buffer
;
918 * Unpack a 32x32 pixel polygon stipple from user memory using the
919 * current pixel unpack settings.
921 void gl_unpack_polygon_stipple( const GLcontext
*ctx
,
922 const GLubyte
*pattern
, GLuint dest
[32] )
925 for (i
= 0; i
< 32; i
++) {
926 GLubyte
*src
= (GLubyte
*) gl_pixel_addr_in_image( &ctx
->Unpack
, pattern
,
927 32, 32, GL_COLOR_INDEX
, GL_BITMAP
, 0, i
, 0 );
928 dest
[i
] = (src
[0] << 24)
934 /* Bit flipping within each byte */
935 if (ctx
->Unpack
.LsbFirst
) {
936 gl_flip_bytes( (GLubyte
*) dest
, 32 * 4 );
943 * Pack polygon stipple into user memory given current pixel packing
946 void gl_pack_polygon_stipple( const GLcontext
*ctx
,
947 const GLuint pattern
[32],
951 for (i
= 0; i
< 32; i
++) {
952 GLubyte
*dst
= (GLubyte
*) gl_pixel_addr_in_image( &ctx
->Pack
, dest
,
953 32, 32, GL_COLOR_INDEX
, GL_BITMAP
, 0, i
, 0 );
954 dst
[0] = (pattern
[i
] >> 24) & 0xff;
955 dst
[1] = (pattern
[i
] >> 16) & 0xff;
956 dst
[2] = (pattern
[i
] >> 8) & 0xff;
957 dst
[3] = (pattern
[i
] ) & 0xff;
959 /* Bit flipping within each byte */
960 if (ctx
->Pack
.LsbFirst
) {
961 gl_flip_bytes( (GLubyte
*) dst
, 4 );
969 * Unpack an RGBA or CI image and store it as unsigned bytes
971 static struct gl_image
*
972 unpack_ubyte_image( GLint width
, GLint height
,
973 GLint depth
, GLenum format
, const GLvoid
*pixels
,
974 const struct gl_pixelstore_attrib
*packing
)
976 struct gl_image
*image
;
977 GLint width_in_bytes
;
979 GLubyte
*buffer
, *dst
;
982 components
= gl_components_in_format( format
);
984 width_in_bytes
= width
* components
* sizeof(GLubyte
);
985 buffer
= (GLubyte
*) MALLOC( height
* width_in_bytes
* depth
);
990 /* Copy/unpack pixel data to buffer */
992 for (d
=0; d
<depth
; d
++ ) {
993 for (i
=0;i
<height
;i
++) {
994 GLubyte
*src
= (GLubyte
*) gl_pixel_addr_in_image( packing
,
995 pixels
, width
, height
, format
, GL_UNSIGNED_BYTE
,
1001 MEMCPY( dst
, src
, width_in_bytes
);
1002 dst
+= width_in_bytes
;
1006 if (format
== GL_BGR
) {
1007 /* swap order of every ubyte triplet from BGR to RGB */
1008 for (i
=0; i
<width
*height
; i
++) {
1009 GLubyte b
= buffer
[i
*3+0];
1010 GLubyte r
= buffer
[i
*3+2];
1015 else if (format
== GL_BGRA
) {
1016 /* swap order of every ubyte quadruplet from BGRA to RGBA */
1017 for (i
=0; i
<width
*height
; i
++) {
1018 GLubyte b
= buffer
[i
*4+0];
1019 GLubyte r
= buffer
[i
*4+2];
1024 else if (format
== GL_ABGR_EXT
) {
1025 /* swap order of every ubyte quadruplet from ABGR to RGBA */
1026 for (i
=0; i
<width
*height
; i
++) {
1027 GLubyte a
= buffer
[i
*4+0];
1028 GLubyte b
= buffer
[i
*4+1];
1029 GLubyte g
= buffer
[i
*4+2];
1030 GLubyte r
= buffer
[i
*4+3];
1039 image
= alloc_image();
1041 image
->Width
= width
;
1042 image
->Height
= height
;
1043 image
->Depth
= depth
;
1044 image
->Components
= components
;
1045 if (format
== GL_BGR
)
1046 image
->Format
= GL_RGB
;
1047 else if (format
== GL_BGRA
)
1048 image
->Format
= GL_RGBA
;
1049 else if (format
== GL_ABGR_EXT
)
1050 image
->Format
= GL_RGBA
;
1052 image
->Format
= format
;
1053 image
->Type
= GL_UNSIGNED_BYTE
;
1054 image
->Data
= buffer
;
1055 image
->RefCount
= 0;
1067 * Unpack a color image storing image as GLfloats
1069 static struct gl_image
*
1070 unpack_float_image( GLcontext
*ctx
, GLint width
, GLint height
, GLint depth
,
1071 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1072 const struct gl_pixelstore_attrib
*packing
)
1074 struct gl_image
*image
;
1076 GLint elems_per_row
;
1079 GLboolean normalize
;
1081 assert(type
!= GL_BITMAP
);
1083 components
= gl_components_in_format( format
);
1084 assert(components
> 0); /* should have been caught earlier */
1086 if (!gl_is_legal_format_and_type( format
, type
)) {
1087 /* bad pixel type for format, make dummy image */
1088 image
= alloc_image();
1090 image
->Width
= width
;
1091 image
->Height
= height
;
1092 image
->Depth
= depth
;
1093 image
->Components
= components
;
1094 image
->Format
= format
;
1097 image
->RefCount
= 0;
1102 elems_per_row
= width
* components
;
1104 image
= alloc_image();
1106 image
->Width
= width
;
1107 image
->Height
= height
;
1108 image
->Depth
= depth
;
1109 image
->Components
= components
;
1110 if (format
== GL_BGR
)
1111 image
->Format
= GL_RGB
;
1112 else if (format
== GL_BGRA
)
1113 image
->Format
= GL_RGBA
;
1114 else if (format
== GL_ABGR_EXT
)
1115 image
->Format
= GL_RGBA
;
1117 image
->Format
= format
;
1118 image
->Type
= GL_FLOAT
;
1119 image
->Data
= MALLOC( elems_per_row
* height
* depth
* sizeof(GLfloat
));
1120 image
->RefCount
= 0;
1128 normalize
= (format
!= GL_COLOR_INDEX
) && (format
!= GL_STENCIL_INDEX
);
1130 dst
= (GLfloat
*) image
->Data
;
1132 for (d
=0; d
<depth
; d
++) {
1133 for (i
=0;i
<height
;i
++) {
1134 GLvoid
*src
= gl_pixel_addr_in_image( packing
, pixels
,
1143 case GL_UNSIGNED_BYTE
:
1145 GLubyte
*ubsrc
= (GLubyte
*) src
;
1147 for (j
=0;j
<elems_per_row
;j
++) {
1148 *dst
++ = UBYTE_TO_FLOAT(ubsrc
[j
]);
1152 for (j
=0;j
<elems_per_row
;j
++) {
1153 *dst
++ = (GLfloat
) ubsrc
[j
];
1160 for (j
=0;j
<elems_per_row
;j
++) {
1161 *dst
++ = BYTE_TO_FLOAT(((GLbyte
*)src
)[j
]);
1165 for (j
=0;j
<elems_per_row
;j
++) {
1166 *dst
++ = (GLfloat
) ((GLbyte
*)src
)[j
];
1170 case GL_UNSIGNED_SHORT
:
1171 if (packing
->SwapBytes
) {
1172 for (j
=0;j
<elems_per_row
;j
++) {
1173 GLushort value
= ((GLushort
*)src
)[j
];
1174 value
= ((value
>> 8) & 0xff) | ((value
&0xff) << 8);
1176 *dst
++ = USHORT_TO_FLOAT(value
);
1179 *dst
++ = (GLfloat
) value
;
1185 for (j
=0;j
<elems_per_row
;j
++) {
1186 *dst
++ = USHORT_TO_FLOAT(((GLushort
*)src
)[j
]);
1190 for (j
=0;j
<elems_per_row
;j
++) {
1191 *dst
++ = (GLfloat
) ((GLushort
*)src
)[j
];
1197 if (packing
->SwapBytes
) {
1198 for (j
=0;j
<elems_per_row
;j
++) {
1199 GLshort value
= ((GLshort
*)src
)[j
];
1200 value
= ((value
>> 8) & 0xff) | ((value
&0xff) << 8);
1202 *dst
++ = SHORT_TO_FLOAT(value
);
1205 *dst
++ = (GLfloat
) value
;
1211 for (j
=0;j
<elems_per_row
;j
++) {
1212 *dst
++ = SHORT_TO_FLOAT(((GLshort
*)src
)[j
]);
1216 for (j
=0;j
<elems_per_row
;j
++) {
1217 *dst
++ = (GLfloat
) ((GLshort
*)src
)[j
];
1222 case GL_UNSIGNED_INT
:
1223 if (packing
->SwapBytes
) {
1225 for (j
=0;j
<elems_per_row
;j
++) {
1226 value
= ((GLuint
*)src
)[j
];
1227 value
= ((value
& 0xff000000) >> 24)
1228 | ((value
& 0x00ff0000) >> 8)
1229 | ((value
& 0x0000ff00) << 8)
1230 | ((value
& 0x000000ff) << 24);
1232 *dst
++ = UINT_TO_FLOAT(value
);
1235 *dst
++ = (GLfloat
) value
;
1241 for (j
=0;j
<elems_per_row
;j
++) {
1242 *dst
++ = UINT_TO_FLOAT(((GLuint
*)src
)[j
]);
1246 for (j
=0;j
<elems_per_row
;j
++) {
1247 *dst
++ = (GLfloat
) ((GLuint
*)src
)[j
];
1253 if (packing
->SwapBytes
) {
1255 for (j
=0;j
<elems_per_row
;j
++) {
1256 value
= ((GLint
*)src
)[j
];
1257 value
= ((value
& 0xff000000) >> 24)
1258 | ((value
& 0x00ff0000) >> 8)
1259 | ((value
& 0x0000ff00) << 8)
1260 | ((value
& 0x000000ff) << 24);
1262 *dst
++ = INT_TO_FLOAT(value
);
1265 *dst
++ = (GLfloat
) value
;
1271 for (j
=0;j
<elems_per_row
;j
++) {
1272 *dst
++ = INT_TO_FLOAT(((GLint
*)src
)[j
]);
1276 for (j
=0;j
<elems_per_row
;j
++) {
1277 *dst
++ = (GLfloat
) ((GLint
*)src
)[j
];
1283 if (packing
->SwapBytes
) {
1285 for (j
=0;j
<elems_per_row
;j
++) {
1286 value
= ((GLuint
*)src
)[j
];
1287 value
= ((value
& 0xff000000) >> 24)
1288 | ((value
& 0x00ff0000) >> 8)
1289 | ((value
& 0x0000ff00) << 8)
1290 | ((value
& 0x000000ff) << 24);
1291 *dst
++ = *((GLfloat
*) &value
);
1295 MEMCPY( dst
, src
, elems_per_row
*sizeof(GLfloat
) );
1296 dst
+= elems_per_row
;
1299 case GL_UNSIGNED_BYTE_3_3_2
:
1301 GLubyte
*ubsrc
= (GLubyte
*) src
;
1302 for (j
=0;j
<width
;j
++) {
1303 GLubyte p
= ubsrc
[j
];
1304 *dst
++ = ((p
>> 5) ) * (1.0F
/ 7.0F
); /* red */
1305 *dst
++ = ((p
>> 2) & 0x7) * (1.0F
/ 7.0F
); /* green */
1306 *dst
++ = ((p
) & 0x3) * (1.0F
/ 3.0F
); /* blue */
1310 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1312 GLubyte
*ubsrc
= (GLubyte
*) src
;
1313 for (j
=0;j
<width
;j
++) {
1314 GLubyte p
= ubsrc
[j
];
1315 *dst
++ = ((p
) & 0x7) * (1.0F
/ 7.0F
); /* red */
1316 *dst
++ = ((p
>> 3) & 0x7) * (1.0F
/ 7.0F
); /* green */
1317 *dst
++ = ((p
>> 6) ) * (1.0F
/ 3.0F
); /* blue */
1321 case GL_UNSIGNED_SHORT_5_6_5
:
1323 GLushort
*ussrc
= (GLushort
*) src
;
1324 for (j
=0;j
<width
;j
++) {
1325 GLushort p
= ussrc
[j
];
1326 *dst
++ = ((p
>> 11) ) * (1.0F
/ 31.0F
); /* red */
1327 *dst
++ = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
); /* green */
1328 *dst
++ = ((p
) & 0x1f) * (1.0F
/ 31.0F
); /* blue */
1332 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1334 GLushort
*ussrc
= (GLushort
*) src
;
1335 for (j
=0;j
<width
;j
++) {
1336 GLushort p
= ussrc
[j
];
1337 *dst
++ = ((p
) & 0x1f) * (1.0F
/ 31.0F
); /* red */
1338 *dst
++ = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
); /* green */
1339 *dst
++ = ((p
>> 11) ) * (1.0F
/ 31.0F
); /* blue */
1343 case GL_UNSIGNED_SHORT_4_4_4_4
:
1345 GLushort
*ussrc
= (GLushort
*) src
;
1346 for (j
=0;j
<width
;j
++) {
1347 GLushort p
= ussrc
[j
];
1348 *dst
++ = ((p
>> 12) ) * (1.0F
/ 15.0F
); /* red */
1349 *dst
++ = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
); /* green */
1350 *dst
++ = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
); /* blue */
1351 *dst
++ = ((p
) & 0xf) * (1.0F
/ 15.0F
); /* alpha */
1355 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1357 GLushort
*ussrc
= (GLushort
*) src
;
1358 for (j
=0;j
<width
;j
++) {
1359 GLushort p
= ussrc
[j
];
1360 *dst
++ = ((p
) & 0xf) * (1.0F
/ 15.0F
); /* red */
1361 *dst
++ = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
); /* green */
1362 *dst
++ = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
); /* blue */
1363 *dst
++ = ((p
>> 12) ) * (1.0F
/ 15.0F
); /* alpha */
1367 case GL_UNSIGNED_SHORT_5_5_5_1
:
1369 GLushort
*ussrc
= (GLushort
*) src
;
1370 for (j
=0;j
<width
;j
++) {
1371 GLushort p
= ussrc
[j
];
1372 *dst
++ = ((p
>> 11) ) * (1.0F
/ 31.0F
); /* red */
1373 *dst
++ = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
); /* green */
1374 *dst
++ = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
); /* blue */
1375 *dst
++ = ((p
) & 0x1) * (1.0F
/ 1.0F
); /* alpha */
1379 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1381 GLushort
*ussrc
= (GLushort
*) src
;
1382 for (j
=0;j
<width
;j
++) {
1383 GLushort p
= ussrc
[j
];
1384 *dst
++ = ((p
) & 0x1f) * (1.0F
/ 31.0F
); /* red */
1385 *dst
++ = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
); /* green */
1386 *dst
++ = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
); /* blue */
1387 *dst
++ = ((p
>> 15) ) * (1.0F
/ 1.0F
); /* alpha */
1391 case GL_UNSIGNED_INT_8_8_8_8
:
1393 GLuint
*uisrc
= (GLuint
*) src
;
1394 for (j
=0;j
<width
;j
++) {
1395 GLuint p
= uisrc
[j
];
1396 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1397 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1398 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1399 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1403 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1405 GLuint
*uisrc
= (GLuint
*) src
;
1406 for (j
=0;j
<width
;j
++) {
1407 GLuint p
= uisrc
[j
];
1408 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1409 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1410 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1411 *dst
++ = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1415 case GL_UNSIGNED_INT_10_10_10_2
:
1417 GLuint
*uisrc
= (GLuint
*) src
;
1418 for (j
=0;j
<width
;j
++) {
1419 GLuint p
= uisrc
[j
];
1420 *dst
++ = ((p
>> 22) ) * (1.0F
/ 1023.0F
); /* r */
1421 *dst
++ = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
); /* g */
1422 *dst
++ = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
); /* b */
1423 *dst
++ = ((p
) & 0x3 ) * (1.0F
/ 3.0F
); /* a */
1427 case GL_UNSIGNED_INT_2_10_10_10_REV
:
1429 GLuint
*uisrc
= (GLuint
*) src
;
1430 for (j
=0;j
<width
;j
++) {
1431 GLuint p
= uisrc
[j
];
1432 *dst
++ = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
); /* r*/
1433 *dst
++ = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
); /* g */
1434 *dst
++ = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
); /* b */
1435 *dst
++ = ((p
>> 30) ) * (1.0F
/ 3.0F
); /* a */
1440 gl_problem(ctx
, "unpack_float_image type" );
1446 if (format
== GL_BGR
) {
1447 /* swap order of every float triplet from BGR to RGBA */
1448 GLfloat
*buffer
= (GLfloat
*) image
->Data
;
1449 for (i
=0; i
<width
*height
*depth
; i
++) {
1450 GLfloat b
= buffer
[i
*3+0];
1451 GLfloat r
= buffer
[i
*3+2];
1456 else if (format
== GL_BGRA
) {
1457 /* swap order of every float quadruplet from BGRA to RGBA */
1458 GLfloat
*buffer
= (GLfloat
*) image
->Data
;
1459 for (i
=0; i
<width
*height
*depth
; i
++) {
1460 GLfloat b
= buffer
[i
*4+0];
1461 GLfloat r
= buffer
[i
*4+2];
1466 else if (format
== GL_ABGR_EXT
) {
1467 /* swap order of every float quadruplet from ABGR to RGBA */
1468 GLfloat
*buffer
= (GLfloat
*) image
->Data
;
1469 for (i
=0; i
<width
*height
*depth
; i
++) {
1470 GLfloat a
= buffer
[i
*4+0];
1471 GLfloat b
= buffer
[i
*4+1];
1472 GLfloat g
= buffer
[i
*4+2];
1473 GLfloat r
= buffer
[i
*4+3];
1487 * Unpack a bitmap image, using current glPixelStore parameters,
1488 * making a new gl_image.
1490 struct gl_image
*gl_unpack_bitmap( GLcontext
*ctx
,
1491 GLsizei width
, GLsizei height
,
1492 const GLubyte
*bitmap
,
1493 const struct gl_pixelstore_attrib
*packing
)
1495 return gl_unpack_image( ctx
, width
, height
,
1496 GL_COLOR_INDEX
, GL_BITMAP
, bitmap
, packing
);
1502 * Unpack a 2-D image from user's buffer. Return pointer to new
1505 * Input: width, height - size in pixels
1506 * format - format of incoming pixel data
1507 * type - datatype of incoming pixel data
1508 * pixels - pointer to unpacked image in user buffer
1510 struct gl_image
*gl_unpack_image( GLcontext
*ctx
,
1511 GLint width
, GLint height
,
1512 GLenum format
, GLenum type
,
1513 const GLvoid
*pixels
,
1514 const struct gl_pixelstore_attrib
*packing
)
1516 return gl_unpack_image3D( ctx
, width
, height
, 1,
1517 format
, type
, pixels
, packing
);
1523 * Unpack a 1, 2 or 3-D image from user-supplied address, returning a
1524 * pointer to a new gl_image struct.
1525 * This function is always called by a higher-level unpack function such
1526 * as gl_unpack_texsubimage() or gl_unpack_bitmap().
1528 * Input: width, height, depth - size in pixels
1529 * format - format of incoming pixel data
1530 * type - datatype of incoming pixel data
1531 * pixels - pointer to unpacked image.
1533 struct gl_image
*gl_unpack_image3D( GLcontext
*ctx
,
1534 GLint width
, GLint height
, GLint depth
,
1535 GLenum format
, GLenum type
,
1536 const GLvoid
*pixels
,
1537 const struct gl_pixelstore_attrib
*packing
)
1539 if (width
<= 0 || height
<= 0 || depth
<= 0) {
1540 return alloc_error_image(width
, height
, depth
, format
, type
);
1543 if (type
==GL_BITMAP
) {
1544 if (format
!= GL_COLOR_INDEX
&& format
!= GL_STENCIL_INDEX
) {
1545 return alloc_error_image(width
, height
, depth
, format
, type
);
1548 return unpack_bitmap( format
, width
, height
, pixels
, packing
);
1551 else if (format
==GL_DEPTH_COMPONENT
) {
1552 /* TODO: pack as GLdepth values (GLushort or GLuint) */
1553 return unpack_depth_image( ctx
, type
, width
, height
, pixels
, packing
);
1555 else if (format
==GL_STENCIL_INDEX
) {
1556 /* TODO: pack as GLstencil (GLubyte or GLushort) */
1557 return unpack_stencil_image( ctx
, type
, width
, height
, pixels
, packing
);
1559 else if (type
==GL_UNSIGNED_BYTE
) {
1560 /* upack, convert to GLubytes */
1561 return unpack_ubyte_image( width
, height
, depth
, format
, pixels
, packing
);
1564 /* upack, convert to floats */
1565 return unpack_float_image( ctx
, width
, height
, depth
,
1566 format
, type
, pixels
, packing
);
1569 /* never get here */
1575 * Apply pixel-transfer operations (scale, bias, mapping) to a single row
1576 * of a gl_image. Put resulting color components into result array.
1578 void gl_scale_bias_map_image_data( const GLcontext
*ctx
,
1579 const struct gl_image
*image
,
1580 GLint row
, GLubyte result
[] )
1589 start
= row
* image
->Width
* image
->Components
;
1591 for (i
=0; i
< image
->Width
; i
++) {
1592 GLint pos
= start
+i
;
1593 GLfloat red
, green
, blue
, alpha
;
1594 if (image
->Type
== GL_UNSIGNED_BYTE
) {
1595 const GLubyte
*data
= (GLubyte
*) image
->Data
;
1596 switch (image
->Format
) {
1598 red
= data
[pos
] * (1.0F
/255.0F
);
1604 red
= data
[pos
*3+0] * (1.0F
/255.0F
);
1605 green
= data
[pos
*3+1] * (1.0F
/255.0F
);
1606 blue
= data
[pos
*3+2] * (1.0F
/255.0F
);
1610 gl_problem(ctx
, "bad image format in gl_scale...image_data");
1614 else if (image
->Type
== GL_FLOAT
) {
1615 const GLubyte
*data
= (GLubyte
*) image
->Data
;
1616 switch (image
->Format
) {
1624 red
= data
[pos
*3+0];
1625 green
= data
[pos
*3+1];
1626 blue
= data
[pos
*3+2];
1630 gl_problem(ctx
, "bad image format in gl_scale...image_data");
1635 gl_problem(ctx
, "Bad image type in gl_scale_...image_data");
1639 assert(red
>= 0.0 && red
<= 1.0);
1640 assert(green
>= 0.0 && green
<= 1.0);
1641 assert(blue
>= 0.0 && blue
<= 1.0);
1642 assert(alpha
>= 0.0 && alpha
<= 1.0);
1645 if (scale or bias) {
1654 result
[i
*4+0] = (GLubyte
) (red
* 255.0);
1655 result
[i
*4+1] = (GLubyte
) (green
* 255.0);
1656 result
[i
*4+2] = (GLubyte
) (blue
* 255.0);
1657 result
[i
*4+3] = (GLubyte
) (alpha
* 255.0);
1664 * Pack the given RGBA span into client memory at 'dest' address
1665 * in the given pixel format and type.
1666 * Optionally apply the enabled pixel transfer ops.
1667 * Pack into memory using the given packing params struct.
1668 * This is used by glReadPixels and glGetTexImage?D()
1669 * Input: ctx - the context
1670 * n - number of pixels in the span
1672 * format - dest packing format
1673 * type - dest packing datatype
1674 * destination - destination packing address
1675 * packing - pixel packing parameters
1676 * applyTransferOps - apply scale/bias/lookup-table ops?
1678 void gl_pack_rgba_span( const GLcontext
*ctx
,
1679 GLuint n
, CONST GLubyte rgba
[][4],
1680 GLenum format
, GLenum type
, GLvoid
*destination
,
1681 const struct gl_pixelstore_attrib
*packing
,
1682 GLboolean applyTransferOps
)
1684 /* Test for optimized case first */
1685 if (!ctx
->Pixel
.ScaleOrBiasRGBA
&& !ctx
->Pixel
.MapColorFlag
&&
1686 format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
1687 /* common simple case */
1688 MEMCPY( destination
, rgba
, n
* 4 * sizeof(GLubyte
) );
1690 else if (!ctx
->Pixel
.ScaleOrBiasRGBA
&& !ctx
->Pixel
.MapColorFlag
&&
1691 format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
1692 /* common simple case */
1694 GLubyte
*dest
= (GLubyte
*) destination
;
1695 for (i
= 0; i
< n
; i
++) {
1696 dest
[0] = rgba
[i
][RCOMP
];
1697 dest
[1] = rgba
[i
][GCOMP
];
1698 dest
[2] = rgba
[i
][BCOMP
];
1703 GLfloat red
[MAX_WIDTH
], green
[MAX_WIDTH
], blue
[MAX_WIDTH
];
1704 GLfloat alpha
[MAX_WIDTH
], luminance
[MAX_WIDTH
];
1705 const GLfloat rscale
= 1.0F
/ 255.0F
;
1706 const GLfloat gscale
= 1.0F
/ 255.0F
;
1707 const GLfloat bscale
= 1.0F
/ 255.0F
;
1708 const GLfloat ascale
= 1.0F
/ 255.0F
;
1709 const GLint comps
= gl_components_in_format(format
);
1712 assert(n
<= MAX_WIDTH
);
1714 /* convert color components to floating point */
1716 red
[i
] = rgba
[i
][RCOMP
] * rscale
;
1717 green
[i
] = rgba
[i
][GCOMP
] * gscale
;
1718 blue
[i
] = rgba
[i
][BCOMP
] * bscale
;
1719 alpha
[i
] = rgba
[i
][ACOMP
] * ascale
;
1723 * Apply scale, bias and lookup-tables if enabled.
1725 if (applyTransferOps
) {
1726 if (ctx
->Pixel
.ScaleOrBiasRGBA
) {
1727 gl_scale_and_bias_color( ctx
, n
, red
, green
, blue
, alpha
);
1729 if (ctx
->Pixel
.MapColorFlag
) {
1730 gl_map_color( ctx
, n
, red
, green
, blue
, alpha
);
1734 if (format
==GL_LUMINANCE
|| format
==GL_LUMINANCE_ALPHA
) {
1736 GLfloat sum
= red
[i
] + green
[i
] + blue
[i
];
1737 luminance
[i
] = CLAMP( sum
, 0.0F
, 1.0F
);
1742 * Pack/store the pixels. Ugh! Lots of cases!!!
1745 case GL_UNSIGNED_BYTE
:
1747 GLubyte
*dst
= (GLubyte
*) destination
;
1751 dst
[i
] = FLOAT_TO_UBYTE(red
[i
]);
1755 dst
[i
] = FLOAT_TO_UBYTE(green
[i
]);
1759 dst
[i
] = FLOAT_TO_UBYTE(blue
[i
]);
1763 dst
[i
] = FLOAT_TO_UBYTE(alpha
[i
]);
1767 dst
[i
] = FLOAT_TO_UBYTE(luminance
[i
]);
1769 case GL_LUMINANCE_ALPHA
:
1771 dst
[i
*2+0] = FLOAT_TO_UBYTE(luminance
[i
]);
1772 dst
[i
*2+1] = FLOAT_TO_UBYTE(alpha
[i
]);
1777 dst
[i
*3+0] = FLOAT_TO_UBYTE(red
[i
]);
1778 dst
[i
*3+1] = FLOAT_TO_UBYTE(green
[i
]);
1779 dst
[i
*3+2] = FLOAT_TO_UBYTE(blue
[i
]);
1784 dst
[i
*4+0] = FLOAT_TO_UBYTE(red
[i
]);
1785 dst
[i
*4+1] = FLOAT_TO_UBYTE(green
[i
]);
1786 dst
[i
*4+2] = FLOAT_TO_UBYTE(blue
[i
]);
1787 dst
[i
*4+3] = FLOAT_TO_UBYTE(alpha
[i
]);
1792 dst
[i
*3+0] = FLOAT_TO_UBYTE(blue
[i
]);
1793 dst
[i
*3+1] = FLOAT_TO_UBYTE(green
[i
]);
1794 dst
[i
*3+2] = FLOAT_TO_UBYTE(red
[i
]);
1799 dst
[i
*4+0] = FLOAT_TO_UBYTE(blue
[i
]);
1800 dst
[i
*4+1] = FLOAT_TO_UBYTE(green
[i
]);
1801 dst
[i
*4+2] = FLOAT_TO_UBYTE(red
[i
]);
1802 dst
[i
*4+3] = FLOAT_TO_UBYTE(alpha
[i
]);
1807 dst
[i
*4+0] = FLOAT_TO_UBYTE(alpha
[i
]);
1808 dst
[i
*4+1] = FLOAT_TO_UBYTE(blue
[i
]);
1809 dst
[i
*4+2] = FLOAT_TO_UBYTE(green
[i
]);
1810 dst
[i
*4+3] = FLOAT_TO_UBYTE(red
[i
]);
1814 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1820 GLbyte
*dst
= (GLbyte
*) destination
;
1824 dst
[i
] = FLOAT_TO_BYTE(red
[i
]);
1828 dst
[i
] = FLOAT_TO_BYTE(green
[i
]);
1832 dst
[i
] = FLOAT_TO_BYTE(blue
[i
]);
1836 dst
[i
] = FLOAT_TO_BYTE(alpha
[i
]);
1840 dst
[i
] = FLOAT_TO_BYTE(luminance
[i
]);
1842 case GL_LUMINANCE_ALPHA
:
1844 dst
[i
*2+0] = FLOAT_TO_BYTE(luminance
[i
]);
1845 dst
[i
*2+1] = FLOAT_TO_BYTE(alpha
[i
]);
1850 dst
[i
*3+0] = FLOAT_TO_BYTE(red
[i
]);
1851 dst
[i
*3+1] = FLOAT_TO_BYTE(green
[i
]);
1852 dst
[i
*3+2] = FLOAT_TO_BYTE(blue
[i
]);
1857 dst
[i
*4+0] = FLOAT_TO_BYTE(red
[i
]);
1858 dst
[i
*4+1] = FLOAT_TO_BYTE(green
[i
]);
1859 dst
[i
*4+2] = FLOAT_TO_BYTE(blue
[i
]);
1860 dst
[i
*4+3] = FLOAT_TO_BYTE(alpha
[i
]);
1865 dst
[i
*3+0] = FLOAT_TO_BYTE(blue
[i
]);
1866 dst
[i
*3+1] = FLOAT_TO_BYTE(green
[i
]);
1867 dst
[i
*3+2] = FLOAT_TO_BYTE(red
[i
]);
1872 dst
[i
*4+0] = FLOAT_TO_BYTE(blue
[i
]);
1873 dst
[i
*4+1] = FLOAT_TO_BYTE(green
[i
]);
1874 dst
[i
*4+2] = FLOAT_TO_BYTE(red
[i
]);
1875 dst
[i
*4+3] = FLOAT_TO_BYTE(alpha
[i
]);
1879 dst
[i
*4+0] = FLOAT_TO_BYTE(alpha
[i
]);
1880 dst
[i
*4+1] = FLOAT_TO_BYTE(blue
[i
]);
1881 dst
[i
*4+2] = FLOAT_TO_BYTE(green
[i
]);
1882 dst
[i
*4+3] = FLOAT_TO_BYTE(red
[i
]);
1886 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1890 case GL_UNSIGNED_SHORT
:
1892 GLushort
*dst
= (GLushort
*) destination
;
1896 dst
[i
] = FLOAT_TO_USHORT(red
[i
]);
1900 dst
[i
] = FLOAT_TO_USHORT(green
[i
]);
1904 dst
[i
] = FLOAT_TO_USHORT(blue
[i
]);
1908 dst
[i
] = FLOAT_TO_USHORT(alpha
[i
]);
1912 dst
[i
] = FLOAT_TO_USHORT(luminance
[i
]);
1914 case GL_LUMINANCE_ALPHA
:
1916 dst
[i
*2+0] = FLOAT_TO_USHORT(luminance
[i
]);
1917 dst
[i
*2+1] = FLOAT_TO_USHORT(alpha
[i
]);
1922 dst
[i
*3+0] = FLOAT_TO_USHORT(red
[i
]);
1923 dst
[i
*3+1] = FLOAT_TO_USHORT(green
[i
]);
1924 dst
[i
*3+2] = FLOAT_TO_USHORT(blue
[i
]);
1929 dst
[i
*4+0] = FLOAT_TO_USHORT(red
[i
]);
1930 dst
[i
*4+1] = FLOAT_TO_USHORT(green
[i
]);
1931 dst
[i
*4+2] = FLOAT_TO_USHORT(blue
[i
]);
1932 dst
[i
*4+3] = FLOAT_TO_USHORT(alpha
[i
]);
1937 dst
[i
*3+0] = FLOAT_TO_USHORT(blue
[i
]);
1938 dst
[i
*3+1] = FLOAT_TO_USHORT(green
[i
]);
1939 dst
[i
*3+2] = FLOAT_TO_USHORT(red
[i
]);
1944 dst
[i
*4+0] = FLOAT_TO_USHORT(blue
[i
]);
1945 dst
[i
*4+1] = FLOAT_TO_USHORT(green
[i
]);
1946 dst
[i
*4+2] = FLOAT_TO_USHORT(red
[i
]);
1947 dst
[i
*4+3] = FLOAT_TO_USHORT(alpha
[i
]);
1952 dst
[i
*4+0] = FLOAT_TO_USHORT(alpha
[i
]);
1953 dst
[i
*4+1] = FLOAT_TO_USHORT(blue
[i
]);
1954 dst
[i
*4+2] = FLOAT_TO_USHORT(green
[i
]);
1955 dst
[i
*4+3] = FLOAT_TO_USHORT(red
[i
]);
1959 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1961 if (packing
->SwapBytes
) {
1962 gl_swap2( (GLushort
*) dst
, n
* comps
);
1968 GLshort
*dst
= (GLshort
*) destination
;
1972 dst
[i
] = FLOAT_TO_SHORT(red
[i
]);
1976 dst
[i
] = FLOAT_TO_SHORT(green
[i
]);
1980 dst
[i
] = FLOAT_TO_SHORT(blue
[i
]);
1984 dst
[i
] = FLOAT_TO_SHORT(alpha
[i
]);
1988 dst
[i
] = FLOAT_TO_SHORT(luminance
[i
]);
1990 case GL_LUMINANCE_ALPHA
:
1992 dst
[i
*2+0] = FLOAT_TO_SHORT(luminance
[i
]);
1993 dst
[i
*2+1] = FLOAT_TO_SHORT(alpha
[i
]);
1998 dst
[i
*3+0] = FLOAT_TO_SHORT(red
[i
]);
1999 dst
[i
*3+1] = FLOAT_TO_SHORT(green
[i
]);
2000 dst
[i
*3+2] = FLOAT_TO_SHORT(blue
[i
]);
2005 dst
[i
*4+0] = FLOAT_TO_SHORT(red
[i
]);
2006 dst
[i
*4+1] = FLOAT_TO_SHORT(green
[i
]);
2007 dst
[i
*4+2] = FLOAT_TO_SHORT(blue
[i
]);
2008 dst
[i
*4+3] = FLOAT_TO_SHORT(alpha
[i
]);
2013 dst
[i
*3+0] = FLOAT_TO_SHORT(blue
[i
]);
2014 dst
[i
*3+1] = FLOAT_TO_SHORT(green
[i
]);
2015 dst
[i
*3+2] = FLOAT_TO_SHORT(red
[i
]);
2020 dst
[i
*4+0] = FLOAT_TO_SHORT(blue
[i
]);
2021 dst
[i
*4+1] = FLOAT_TO_SHORT(green
[i
]);
2022 dst
[i
*4+2] = FLOAT_TO_SHORT(red
[i
]);
2023 dst
[i
*4+3] = FLOAT_TO_SHORT(alpha
[i
]);
2027 dst
[i
*4+0] = FLOAT_TO_SHORT(alpha
[i
]);
2028 dst
[i
*4+1] = FLOAT_TO_SHORT(blue
[i
]);
2029 dst
[i
*4+2] = FLOAT_TO_SHORT(green
[i
]);
2030 dst
[i
*4+3] = FLOAT_TO_SHORT(red
[i
]);
2034 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
2036 if (packing
->SwapBytes
) {
2037 gl_swap2( (GLushort
*) dst
, n
* comps
);
2041 case GL_UNSIGNED_INT
:
2043 GLuint
*dst
= (GLuint
*) destination
;
2047 dst
[i
] = FLOAT_TO_UINT(red
[i
]);
2051 dst
[i
] = FLOAT_TO_UINT(green
[i
]);
2055 dst
[i
] = FLOAT_TO_UINT(blue
[i
]);
2059 dst
[i
] = FLOAT_TO_UINT(alpha
[i
]);
2063 dst
[i
] = FLOAT_TO_UINT(luminance
[i
]);
2065 case GL_LUMINANCE_ALPHA
:
2067 dst
[i
*2+0] = FLOAT_TO_UINT(luminance
[i
]);
2068 dst
[i
*2+1] = FLOAT_TO_UINT(alpha
[i
]);
2073 dst
[i
*3+0] = FLOAT_TO_UINT(red
[i
]);
2074 dst
[i
*3+1] = FLOAT_TO_UINT(green
[i
]);
2075 dst
[i
*3+2] = FLOAT_TO_UINT(blue
[i
]);
2080 dst
[i
*4+0] = FLOAT_TO_UINT(red
[i
]);
2081 dst
[i
*4+1] = FLOAT_TO_UINT(green
[i
]);
2082 dst
[i
*4+2] = FLOAT_TO_UINT(blue
[i
]);
2083 dst
[i
*4+3] = FLOAT_TO_UINT(alpha
[i
]);
2088 dst
[i
*3+0] = FLOAT_TO_UINT(blue
[i
]);
2089 dst
[i
*3+1] = FLOAT_TO_UINT(green
[i
]);
2090 dst
[i
*3+2] = FLOAT_TO_UINT(red
[i
]);
2095 dst
[i
*4+0] = FLOAT_TO_UINT(blue
[i
]);
2096 dst
[i
*4+1] = FLOAT_TO_UINT(green
[i
]);
2097 dst
[i
*4+2] = FLOAT_TO_UINT(red
[i
]);
2098 dst
[i
*4+3] = FLOAT_TO_UINT(alpha
[i
]);
2103 dst
[i
*4+0] = FLOAT_TO_UINT(alpha
[i
]);
2104 dst
[i
*4+1] = FLOAT_TO_UINT(blue
[i
]);
2105 dst
[i
*4+2] = FLOAT_TO_UINT(green
[i
]);
2106 dst
[i
*4+3] = FLOAT_TO_UINT(red
[i
]);
2110 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
2112 if (packing
->SwapBytes
) {
2113 gl_swap4( (GLuint
*) dst
, n
* comps
);
2119 GLint
*dst
= (GLint
*) destination
;
2123 dst
[i
] = FLOAT_TO_INT(red
[i
]);
2127 dst
[i
] = FLOAT_TO_INT(green
[i
]);
2131 dst
[i
] = FLOAT_TO_INT(blue
[i
]);
2135 dst
[i
] = FLOAT_TO_INT(alpha
[i
]);
2139 dst
[i
] = FLOAT_TO_INT(luminance
[i
]);
2141 case GL_LUMINANCE_ALPHA
:
2143 dst
[i
*2+0] = FLOAT_TO_INT(luminance
[i
]);
2144 dst
[i
*2+1] = FLOAT_TO_INT(alpha
[i
]);
2149 dst
[i
*3+0] = FLOAT_TO_INT(red
[i
]);
2150 dst
[i
*3+1] = FLOAT_TO_INT(green
[i
]);
2151 dst
[i
*3+2] = FLOAT_TO_INT(blue
[i
]);
2156 dst
[i
*4+0] = FLOAT_TO_INT(red
[i
]);
2157 dst
[i
*4+1] = FLOAT_TO_INT(green
[i
]);
2158 dst
[i
*4+2] = FLOAT_TO_INT(blue
[i
]);
2159 dst
[i
*4+3] = FLOAT_TO_INT(alpha
[i
]);
2164 dst
[i
*3+0] = FLOAT_TO_INT(blue
[i
]);
2165 dst
[i
*3+1] = FLOAT_TO_INT(green
[i
]);
2166 dst
[i
*3+2] = FLOAT_TO_INT(red
[i
]);
2171 dst
[i
*4+0] = FLOAT_TO_INT(blue
[i
]);
2172 dst
[i
*4+1] = FLOAT_TO_INT(green
[i
]);
2173 dst
[i
*4+2] = FLOAT_TO_INT(red
[i
]);
2174 dst
[i
*4+3] = FLOAT_TO_INT(alpha
[i
]);
2179 dst
[i
*4+0] = FLOAT_TO_INT(alpha
[i
]);
2180 dst
[i
*4+1] = FLOAT_TO_INT(blue
[i
]);
2181 dst
[i
*4+2] = FLOAT_TO_INT(green
[i
]);
2182 dst
[i
*4+3] = FLOAT_TO_INT(red
[i
]);
2186 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
2188 if (packing
->SwapBytes
) {
2189 gl_swap4( (GLuint
*) dst
, n
* comps
);
2195 GLfloat
*dst
= (GLfloat
*) destination
;
2215 dst
[i
] = luminance
[i
];
2217 case GL_LUMINANCE_ALPHA
:
2219 dst
[i
*2+0] = luminance
[i
];
2220 dst
[i
*2+1] = alpha
[i
];
2225 dst
[i
*3+0] = red
[i
];
2226 dst
[i
*3+1] = green
[i
];
2227 dst
[i
*3+2] = blue
[i
];
2232 dst
[i
*4+0] = red
[i
];
2233 dst
[i
*4+1] = green
[i
];
2234 dst
[i
*4+2] = blue
[i
];
2235 dst
[i
*4+3] = alpha
[i
];
2240 dst
[i
*3+0] = blue
[i
];
2241 dst
[i
*3+1] = green
[i
];
2242 dst
[i
*3+2] = red
[i
];
2247 dst
[i
*4+0] = blue
[i
];
2248 dst
[i
*4+1] = green
[i
];
2249 dst
[i
*4+2] = red
[i
];
2250 dst
[i
*4+3] = alpha
[i
];
2255 dst
[i
*4+0] = alpha
[i
];
2256 dst
[i
*4+1] = blue
[i
];
2257 dst
[i
*4+2] = green
[i
];
2258 dst
[i
*4+3] = red
[i
];
2262 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
2264 if (packing
->SwapBytes
) {
2265 gl_swap4( (GLuint
*) dst
, n
* comps
);
2269 case GL_UNSIGNED_BYTE_3_3_2
:
2270 if (format
== GL_RGB
) {
2271 GLubyte
*dst
= (GLubyte
*) destination
;
2273 dst
[i
] = (((GLint
) (red
[i
] * 7.0F
)) << 5)
2274 | (((GLint
) (green
[i
] * 7.0F
)) << 2)
2275 | (((GLint
) (blue
[i
] * 3.0F
)) );
2279 case GL_UNSIGNED_BYTE_2_3_3_REV
:
2280 if (format
== GL_RGB
) {
2281 GLubyte
*dst
= (GLubyte
*) destination
;
2283 dst
[i
] = (((GLint
) (red
[i
] * 7.0F
)) )
2284 | (((GLint
) (green
[i
] * 7.0F
)) << 3)
2285 | (((GLint
) (blue
[i
] * 3.0F
)) << 5);
2289 case GL_UNSIGNED_SHORT_5_6_5
:
2290 if (format
== GL_RGB
) {
2291 GLushort
*dst
= (GLushort
*) destination
;
2293 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) << 11)
2294 | (((GLint
) (green
[i
] * 63.0F
)) << 5)
2295 | (((GLint
) (blue
[i
] * 31.0F
)) );
2299 case GL_UNSIGNED_SHORT_5_6_5_REV
:
2300 if (format
== GL_RGB
) {
2301 GLushort
*dst
= (GLushort
*) destination
;
2303 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) )
2304 | (((GLint
) (green
[i
] * 63.0F
)) << 5)
2305 | (((GLint
) (blue
[i
] * 31.0F
)) << 11);
2309 case GL_UNSIGNED_SHORT_4_4_4_4
:
2310 if (format
== GL_RGB
) {
2311 GLushort
*dst
= (GLushort
*) destination
;
2313 dst
[i
] = (((GLint
) (red
[i
] * 15.0F
)) << 12)
2314 | (((GLint
) (green
[i
] * 15.0F
)) << 8)
2315 | (((GLint
) (blue
[i
] * 15.0F
)) << 4)
2316 | (((GLint
) (alpha
[i
] * 15.0F
)) );
2320 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
2321 if (format
== GL_RGB
) {
2322 GLushort
*dst
= (GLushort
*) destination
;
2324 dst
[i
] = (((GLint
) (red
[i
] * 15.0F
)) )
2325 | (((GLint
) (green
[i
] * 15.0F
)) << 4)
2326 | (((GLint
) (blue
[i
] * 15.0F
)) << 8)
2327 | (((GLint
) (alpha
[i
] * 15.0F
)) << 12);
2331 case GL_UNSIGNED_SHORT_5_5_5_1
:
2332 if (format
== GL_RGB
) {
2333 GLushort
*dst
= (GLushort
*) destination
;
2335 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) << 11)
2336 | (((GLint
) (green
[i
] * 31.0F
)) << 6)
2337 | (((GLint
) (blue
[i
] * 31.0F
)) << 1)
2338 | (((GLint
) (alpha
[i
] * 1.0F
)) );
2342 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
2343 if (format
== GL_RGB
) {
2344 GLushort
*dst
= (GLushort
*) destination
;
2346 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) )
2347 | (((GLint
) (green
[i
] * 31.0F
)) << 5)
2348 | (((GLint
) (blue
[i
] * 31.0F
)) << 10)
2349 | (((GLint
) (alpha
[i
] * 1.0F
)) << 15);
2353 case GL_UNSIGNED_INT_8_8_8_8
:
2354 if (format
== GL_RGBA
) {
2355 GLuint
*dst
= (GLuint
*) destination
;
2357 dst
[i
] = (((GLuint
) (red
[i
] * 255.0F
)) << 24)
2358 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
2359 | (((GLuint
) (blue
[i
] * 255.0F
)) << 8)
2360 | (((GLuint
) (alpha
[i
] * 255.0F
)) );
2363 else if (format
== GL_BGRA
) {
2364 GLuint
*dst
= (GLuint
*) destination
;
2366 dst
[i
] = (((GLuint
) (blue
[i
] * 255.0F
)) << 24)
2367 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
2368 | (((GLuint
) (red
[i
] * 255.0F
)) << 8)
2369 | (((GLuint
) (alpha
[i
] * 255.0F
)) );
2372 else if (format
== GL_ABGR_EXT
) {
2373 GLuint
*dst
= (GLuint
*) destination
;
2375 dst
[i
] = (((GLuint
) (alpha
[i
] * 255.0F
)) << 24)
2376 | (((GLuint
) (blue
[i
] * 255.0F
)) << 16)
2377 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
2378 | (((GLuint
) (red
[i
] * 255.0F
)) );
2382 case GL_UNSIGNED_INT_8_8_8_8_REV
:
2383 if (format
== GL_RGBA
) {
2384 GLuint
*dst
= (GLuint
*) destination
;
2386 dst
[i
] = (((GLuint
) (red
[i
] * 255.0F
)) )
2387 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
2388 | (((GLuint
) (blue
[i
] * 255.0F
)) << 16)
2389 | (((GLuint
) (alpha
[i
] * 255.0F
)) << 24);
2392 else if (format
== GL_BGRA
) {
2393 GLuint
*dst
= (GLuint
*) destination
;
2395 dst
[i
] = (((GLuint
) (blue
[i
] * 255.0F
)) )
2396 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
2397 | (((GLuint
) (red
[i
] * 255.0F
)) << 16)
2398 | (((GLuint
) (alpha
[i
] * 255.0F
)) << 24);
2401 else if (format
== GL_ABGR_EXT
) {
2402 GLuint
*dst
= (GLuint
*) destination
;
2404 dst
[i
] = (((GLuint
) (alpha
[i
] * 255.0F
)) )
2405 | (((GLuint
) (blue
[i
] * 255.0F
)) << 8)
2406 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
2407 | (((GLuint
) (red
[i
] * 255.0F
)) << 24);
2411 case GL_UNSIGNED_INT_10_10_10_2
:
2412 if (format
== GL_RGBA
) {
2413 GLuint
*dst
= (GLuint
*) destination
;
2415 dst
[i
] = (((GLuint
) (red
[i
] * 1023.0F
)) << 22)
2416 | (((GLuint
) (green
[i
] * 1023.0F
)) << 12)
2417 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 2)
2418 | (((GLuint
) (alpha
[i
] * 3.0F
)) );
2421 else if (format
== GL_BGRA
) {
2422 GLuint
*dst
= (GLuint
*) destination
;
2424 dst
[i
] = (((GLuint
) (blue
[i
] * 1023.0F
)) << 22)
2425 | (((GLuint
) (green
[i
] * 1023.0F
)) << 12)
2426 | (((GLuint
) (red
[i
] * 1023.0F
)) << 2)
2427 | (((GLuint
) (alpha
[i
] * 3.0F
)) );
2430 else if (format
== GL_ABGR_EXT
) {
2431 GLuint
*dst
= (GLuint
*) destination
;
2433 dst
[i
] = (((GLuint
) (alpha
[i
] * 1023.0F
)) << 22)
2434 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 12)
2435 | (((GLuint
) (green
[i
] * 1023.0F
)) << 2)
2436 | (((GLuint
) (red
[i
] * 3.0F
)) );
2440 case GL_UNSIGNED_INT_2_10_10_10_REV
:
2441 if (format
== GL_RGBA
) {
2442 GLuint
*dst
= (GLuint
*) destination
;
2444 dst
[i
] = (((GLuint
) (red
[i
] * 1023.0F
)) )
2445 | (((GLuint
) (green
[i
] * 1023.0F
)) << 10)
2446 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 20)
2447 | (((GLuint
) (alpha
[i
] * 3.0F
)) << 30);
2450 else if (format
== GL_BGRA
) {
2451 GLuint
*dst
= (GLuint
*) destination
;
2453 dst
[i
] = (((GLuint
) (blue
[i
] * 1023.0F
)) )
2454 | (((GLuint
) (green
[i
] * 1023.0F
)) << 10)
2455 | (((GLuint
) (red
[i
] * 1023.0F
)) << 20)
2456 | (((GLuint
) (alpha
[i
] * 3.0F
)) << 30);
2459 else if (format
== GL_ABGR_EXT
) {
2460 GLuint
*dst
= (GLuint
*) destination
;
2462 dst
[i
] = (((GLuint
) (alpha
[i
] * 1023.0F
)) )
2463 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 10)
2464 | (((GLuint
) (green
[i
] * 1023.0F
)) << 20)
2465 | (((GLuint
) (red
[i
] * 3.0F
)) << 30);
2470 gl_problem( ctx
, "bad type in gl_pack_rgba_span" );
2478 * New (3.3) functions
2481 #define SWAP2BYTE(VALUE) \
2483 GLubyte *bytes = (GLubyte *) &(VALUE); \
2484 GLubyte tmp = bytes[0]; \
2485 bytes[0] = bytes[1]; \
2489 #define SWAP4BYTE(VALUE) \
2491 GLubyte *bytes = (GLubyte *) &(VALUE); \
2492 GLubyte tmp = bytes[0]; \
2493 bytes[0] = bytes[3]; \
2496 bytes[1] = bytes[2]; \
2502 extract_uint_indexes(GLuint n
, GLuint indexes
[],
2503 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
2504 const struct gl_pixelstore_attrib
*unpack
)
2506 assert(srcFormat
== GL_COLOR_INDEX
);
2508 ASSERT(srcType
== GL_BITMAP
||
2509 srcType
== GL_UNSIGNED_BYTE
||
2510 srcType
== GL_BYTE
||
2511 srcType
== GL_UNSIGNED_SHORT
||
2512 srcType
== GL_SHORT
||
2513 srcType
== GL_UNSIGNED_INT
||
2514 srcType
== GL_INT
||
2515 srcType
== GL_FLOAT
);
2520 GLubyte
*ubsrc
= (GLubyte
*) src
;
2521 if (unpack
->LsbFirst
) {
2522 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
2524 for (i
= 0; i
< n
; i
++) {
2525 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
2536 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
2538 for (i
= 0; i
< n
; i
++) {
2539 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
2551 case GL_UNSIGNED_BYTE
:
2554 const GLubyte
*s
= (const GLubyte
*) src
;
2555 for (i
= 0; i
< n
; i
++)
2562 const GLbyte
*s
= (const GLbyte
*) src
;
2563 for (i
= 0; i
< n
; i
++)
2567 case GL_UNSIGNED_SHORT
:
2570 const GLushort
*s
= (const GLushort
*) src
;
2571 if (unpack
->SwapBytes
) {
2572 for (i
= 0; i
< n
; i
++) {
2573 GLushort value
= s
[i
];
2579 for (i
= 0; i
< n
; i
++)
2587 const GLshort
*s
= (const GLshort
*) src
;
2588 if (unpack
->SwapBytes
) {
2589 for (i
= 0; i
< n
; i
++) {
2590 GLshort value
= s
[i
];
2596 for (i
= 0; i
< n
; i
++)
2601 case GL_UNSIGNED_INT
:
2604 const GLuint
*s
= (const GLuint
*) src
;
2605 if (unpack
->SwapBytes
) {
2606 for (i
= 0; i
< n
; i
++) {
2607 GLuint value
= s
[i
];
2613 for (i
= 0; i
< n
; i
++)
2621 const GLint
*s
= (const GLint
*) src
;
2622 if (unpack
->SwapBytes
) {
2623 for (i
= 0; i
< n
; i
++) {
2630 for (i
= 0; i
< n
; i
++)
2638 const GLfloat
*s
= (const GLfloat
*) src
;
2639 if (unpack
->SwapBytes
) {
2640 for (i
= 0; i
< n
; i
++) {
2641 GLfloat value
= s
[i
];
2647 for (i
= 0; i
< n
; i
++)
2653 gl_problem(NULL
, "bad srcType in extract_uint_indexes");
2661 * This function extracts floating point RGBA values from arbitrary
2662 * image data. srcFormat and srcType are the format and type parameters
2663 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
2665 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
2666 * implements the "Conversion to floating point", "Conversion to RGB",
2667 * and "Final Expansion to RGBA" operations.
2669 * Args: n - number of pixels
2670 * rgba - output colors
2671 * srcFormat - format of incoming data
2672 * srcType - datatype of incoming data
2673 * src - source data pointer
2674 * swapBytes - perform byteswapping of incoming data?
2677 extract_float_rgba(GLuint n
, GLfloat rgba
[][4],
2678 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
2679 GLboolean swapBytes
)
2681 GLint redIndex
, greenIndex
, blueIndex
, alphaIndex
;
2683 GLint rComp
, bComp
, gComp
, aComp
;
2688 for (i
= 0; i
<n
;i
++) {
2689 rgba
[i
][0] = rgba
[i
][1] = rgba
[i
][2] = rgba
[i
][3] = 0;
2695 ASSERT(srcFormat
== GL_RED
||
2696 srcFormat
== GL_GREEN
||
2697 srcFormat
== GL_BLUE
||
2698 srcFormat
== GL_ALPHA
||
2699 srcFormat
== GL_LUMINANCE
||
2700 srcFormat
== GL_LUMINANCE_ALPHA
||
2701 srcFormat
== GL_INTENSITY
||
2702 srcFormat
== GL_RGB
||
2703 srcFormat
== GL_BGR
||
2704 srcFormat
== GL_RGBA
||
2705 srcFormat
== GL_BGRA
||
2706 srcFormat
== GL_ABGR_EXT
);
2708 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
2709 srcType
== GL_BYTE
||
2710 srcType
== GL_UNSIGNED_SHORT
||
2711 srcType
== GL_SHORT
||
2712 srcType
== GL_UNSIGNED_INT
||
2713 srcType
== GL_INT
||
2714 srcType
== GL_FLOAT
||
2715 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2716 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2717 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2718 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2719 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2720 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2721 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2722 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2723 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2724 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2725 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2726 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
);
2728 switch (srcFormat
) {
2731 greenIndex
= blueIndex
= alphaIndex
= -1;
2736 redIndex
= blueIndex
= alphaIndex
= -1;
2741 redIndex
= greenIndex
= alphaIndex
= -1;
2745 redIndex
= greenIndex
= blueIndex
= -1;
2750 redIndex
= greenIndex
= blueIndex
= 0;
2754 case GL_LUMINANCE_ALPHA
:
2755 redIndex
= greenIndex
= blueIndex
= 0;
2761 greenIndex
= blueIndex
= alphaIndex
= -1;
2812 gl_problem(NULL
, "bad srcFormat in extract float data");
2816 assert(redIndex
>= -1 && redIndex
<= 4);
2817 assert(greenIndex
>= -1 && greenIndex
<= 4);
2818 assert(blueIndex
>= -1 && blueIndex
<= 4);
2819 assert(alphaIndex
>= -1 && alphaIndex
<= 4);
2821 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
2822 if ((INDEX) < 0) { \
2824 for (i = 0; i < n; i++) { \
2825 rgba[i][CHANNEL] = DEFAULT; \
2828 else if (swapBytes) { \
2829 const TYPE *s = (const TYPE *) src; \
2831 for (i = 0; i < n; i++) { \
2832 TYPE value = s[INDEX]; \
2833 if (sizeof(TYPE) == 2) { \
2836 else if (sizeof(TYPE) == 4) { \
2839 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
2844 const TYPE *s = (const TYPE *) src; \
2846 for (i = 0; i < n; i++) { \
2847 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
2853 case GL_UNSIGNED_BYTE
:
2854 PROCESS(redIndex
, RCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
2855 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
2856 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
2857 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLubyte
, UBYTE_TO_FLOAT
);
2860 PROCESS(redIndex
, RCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
2861 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
2862 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
2863 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLbyte
, BYTE_TO_FLOAT
);
2865 case GL_UNSIGNED_SHORT
:
2866 PROCESS(redIndex
, RCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
2867 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
2868 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
2869 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLushort
, USHORT_TO_FLOAT
);
2872 PROCESS(redIndex
, RCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
2873 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
2874 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
2875 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLshort
, SHORT_TO_FLOAT
);
2877 case GL_UNSIGNED_INT
:
2878 PROCESS(redIndex
, RCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
2879 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
2880 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
2881 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLuint
, UINT_TO_FLOAT
);
2884 PROCESS(redIndex
, RCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
2885 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
2886 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
2887 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLint
, INT_TO_FLOAT
);
2890 PROCESS(redIndex
, RCOMP
, 0.0F
, GLfloat
, (GLfloat
));
2891 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLfloat
, (GLfloat
));
2892 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLfloat
, (GLfloat
));
2893 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLfloat
, (GLfloat
));
2895 case GL_UNSIGNED_BYTE_3_3_2
:
2897 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
2899 for (i
= 0; i
< n
; i
++) {
2900 GLubyte p
= ubsrc
[i
];
2901 rgba
[i
][RCOMP
] = ((p
>> 5) ) * (1.0F
/ 7.0F
);
2902 rgba
[i
][GCOMP
] = ((p
>> 2) & 0x7) * (1.0F
/ 7.0F
);
2903 rgba
[i
][BCOMP
] = ((p
) & 0x3) * (1.0F
/ 3.0F
);
2904 rgba
[i
][ACOMP
] = 1.0F
;
2908 case GL_UNSIGNED_BYTE_2_3_3_REV
:
2910 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
2912 for (i
= 0; i
< n
; i
++) {
2913 GLubyte p
= ubsrc
[i
];
2914 rgba
[i
][RCOMP
] = ((p
) & 0x7) * (1.0F
/ 7.0F
);
2915 rgba
[i
][GCOMP
] = ((p
>> 3) & 0x7) * (1.0F
/ 7.0F
);
2916 rgba
[i
][BCOMP
] = ((p
>> 6) ) * (1.0F
/ 3.0F
);
2917 rgba
[i
][ACOMP
] = 1.0F
;
2921 case GL_UNSIGNED_SHORT_5_6_5
:
2923 const GLushort
*ussrc
= (const GLushort
*) src
;
2925 for (i
= 0; i
< n
; i
++) {
2926 GLushort p
= ussrc
[i
];
2928 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
2929 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
2930 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
2931 rgba
[i
][ACOMP
] = 1.0F
;
2935 const GLushort
*ussrc
= (const GLushort
*) src
;
2937 for (i
= 0; i
< n
; i
++) {
2938 GLushort p
= ussrc
[i
];
2939 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
2940 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
2941 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
2942 rgba
[i
][ACOMP
] = 1.0F
;
2946 case GL_UNSIGNED_SHORT_5_6_5_REV
:
2948 const GLushort
*ussrc
= (const GLushort
*) src
;
2950 for (i
= 0; i
< n
; i
++) {
2951 GLushort p
= ussrc
[i
];
2953 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
2954 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
2955 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
2956 rgba
[i
][ACOMP
] = 1.0F
;
2960 const GLushort
*ussrc
= (const GLushort
*) src
;
2962 for (i
= 0; i
< n
; i
++) {
2963 GLushort p
= ussrc
[i
];
2964 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
2965 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
2966 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
2967 rgba
[i
][ACOMP
] = 1.0F
;
2971 case GL_UNSIGNED_SHORT_4_4_4_4
:
2973 const GLushort
*ussrc
= (const GLushort
*) src
;
2975 for (i
= 0; i
< n
; i
++) {
2976 GLushort p
= ussrc
[i
];
2978 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
2979 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
2980 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
2981 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
2985 const GLushort
*ussrc
= (const GLushort
*) src
;
2987 for (i
= 0; i
< n
; i
++) {
2988 GLushort p
= ussrc
[i
];
2989 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
2990 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
2991 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
2992 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
2996 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
2998 const GLushort
*ussrc
= (const GLushort
*) src
;
3000 for (i
= 0; i
< n
; i
++) {
3001 GLushort p
= ussrc
[i
];
3003 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
3004 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
3005 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
3006 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
3010 const GLushort
*ussrc
= (const GLushort
*) src
;
3012 for (i
= 0; i
< n
; i
++) {
3013 GLushort p
= ussrc
[i
];
3014 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
3015 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
3016 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
3017 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
3021 case GL_UNSIGNED_SHORT_5_5_5_1
:
3023 const GLushort
*ussrc
= (const GLushort
*) src
;
3025 for (i
= 0; i
< n
; i
++) {
3026 GLushort p
= ussrc
[i
];
3028 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
3029 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
3030 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
3031 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
3035 const GLushort
*ussrc
= (const GLushort
*) src
;
3037 for (i
= 0; i
< n
; i
++) {
3038 GLushort p
= ussrc
[i
];
3039 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
3040 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
3041 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
3042 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
3046 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
3048 const GLushort
*ussrc
= (const GLushort
*) src
;
3050 for (i
= 0; i
< n
; i
++) {
3051 GLushort p
= ussrc
[i
];
3053 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
3054 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
3055 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
3056 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
3060 const GLushort
*ussrc
= (const GLushort
*) src
;
3062 for (i
= 0; i
< n
; i
++) {
3063 GLushort p
= ussrc
[i
];
3064 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
3065 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
3066 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
3067 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
3071 case GL_UNSIGNED_INT_8_8_8_8
:
3073 const GLuint
*uisrc
= (const GLuint
*) src
;
3075 for (i
= 0; i
< n
; i
++) {
3076 GLuint p
= uisrc
[i
];
3077 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
3078 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
3079 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
3080 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
3084 const GLuint
*uisrc
= (const GLuint
*) src
;
3086 for (i
= 0; i
< n
; i
++) {
3087 GLuint p
= uisrc
[i
];
3088 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
3089 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
3090 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
3091 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
3095 case GL_UNSIGNED_INT_8_8_8_8_REV
:
3097 const GLuint
*uisrc
= (const GLuint
*) src
;
3099 for (i
= 0; i
< n
; i
++) {
3100 GLuint p
= uisrc
[i
];
3101 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
3102 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
3103 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
3104 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
3108 const GLuint
*uisrc
= (const GLuint
*) src
;
3110 for (i
= 0; i
< n
; i
++) {
3111 GLuint p
= uisrc
[i
];
3112 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
3113 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
3114 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
3115 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
3119 case GL_UNSIGNED_INT_10_10_10_2
:
3121 const GLuint
*uisrc
= (const GLuint
*) src
;
3123 for (i
= 0; i
< n
; i
++) {
3124 GLuint p
= uisrc
[i
];
3126 rgba
[i
][rComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
3127 rgba
[i
][gComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
3128 rgba
[i
][bComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
3129 rgba
[i
][aComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
3133 const GLuint
*uisrc
= (const GLuint
*) src
;
3135 for (i
= 0; i
< n
; i
++) {
3136 GLuint p
= uisrc
[i
];
3137 rgba
[i
][rComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
3138 rgba
[i
][gComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
3139 rgba
[i
][bComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
3140 rgba
[i
][aComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
3144 case GL_UNSIGNED_INT_2_10_10_10_REV
:
3146 const GLuint
*uisrc
= (const GLuint
*) src
;
3148 for (i
= 0; i
< n
; i
++) {
3149 GLuint p
= uisrc
[i
];
3151 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
3152 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
3153 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
3154 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
3158 const GLuint
*uisrc
= (const GLuint
*) src
;
3160 for (i
= 0; i
< n
; i
++) {
3161 GLuint p
= uisrc
[i
];
3162 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
3163 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
3164 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
3165 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
3170 gl_problem(NULL
, "bad srcType in extract float data");
3178 * Unpack a row of color image data from a client buffer according to
3179 * the pixel unpacking parameters. Apply any enabled pixel transfer
3180 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
3181 * Return GLubyte values in the specified dest image format.
3182 * This is (or will be) used by glDrawPixels and glTexImage?D().
3183 * Input: ctx - the context
3184 * n - number of pixels in the span
3185 * dstFormat - format of destination color array
3186 * dest - the destination color array
3187 * srcFormat - source image format
3188 * srcType - source image datatype
3189 * source - source image pointer
3190 * unpacking - pixel unpacking parameters
3191 * applyTransferOps - apply scale/bias/lookup-table ops?
3193 * XXX perhaps expand this to process whole images someday.
3196 _mesa_unpack_ubyte_color_span( const GLcontext
*ctx
,
3197 GLuint n
, GLenum dstFormat
, GLubyte dest
[],
3198 GLenum srcFormat
, GLenum srcType
,
3199 const GLvoid
*source
,
3200 const struct gl_pixelstore_attrib
*unpacking
,
3201 GLboolean applyTransferOps
)
3203 ASSERT(dstFormat
== GL_ALPHA
||
3204 dstFormat
== GL_LUMINANCE
||
3205 dstFormat
== GL_LUMINANCE_ALPHA
||
3206 dstFormat
== GL_INTENSITY
||
3207 dstFormat
== GL_RGB
||
3208 dstFormat
== GL_RGBA
||
3209 dstFormat
== GL_COLOR_INDEX
);
3211 ASSERT(srcFormat
== GL_RED
||
3212 srcFormat
== GL_GREEN
||
3213 srcFormat
== GL_BLUE
||
3214 srcFormat
== GL_ALPHA
||
3215 srcFormat
== GL_LUMINANCE
||
3216 srcFormat
== GL_LUMINANCE_ALPHA
||
3217 srcFormat
== GL_INTENSITY
||
3218 srcFormat
== GL_RGB
||
3219 srcFormat
== GL_BGR
||
3220 srcFormat
== GL_RGBA
||
3221 srcFormat
== GL_BGRA
||
3222 srcFormat
== GL_ABGR_EXT
||
3223 srcFormat
== GL_COLOR_INDEX
);
3225 ASSERT(srcType
== GL_BITMAP
||
3226 srcType
== GL_UNSIGNED_BYTE
||
3227 srcType
== GL_BYTE
||
3228 srcType
== GL_UNSIGNED_SHORT
||
3229 srcType
== GL_SHORT
||
3230 srcType
== GL_UNSIGNED_INT
||
3231 srcType
== GL_INT
||
3232 srcType
== GL_FLOAT
||
3233 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
3234 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
3235 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
3236 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
3237 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
3238 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
3239 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
3240 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
3241 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
3242 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
3243 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
3244 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
);
3246 /* this is intended for RGBA mode */
3247 ASSERT(ctx
->Visual
->RGBAflag
);
3249 applyTransferOps
&= (ctx
->Pixel
.ScaleOrBiasRGBA
||
3250 ctx
->Pixel
.MapColorFlag
||
3251 ctx
->Pixel
.MapColorFlag
);
3253 /* Try simple cases first */
3254 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
) {
3255 if (dstFormat
== GL_RGBA
) {
3256 if (srcFormat
== GL_RGBA
) {
3257 MEMCPY( dest
, source
, n
* 4 * sizeof(GLubyte
) );
3260 else if (srcFormat
== GL_RGB
) {
3262 const GLubyte
*src
= (const GLubyte
*) source
;
3263 GLubyte
*dst
= dest
;
3264 for (i
= 0; i
< n
; i
++) {
3275 else if (dstFormat
== GL_RGB
) {
3276 if (srcFormat
== GL_RGB
) {
3277 MEMCPY( dest
, source
, n
* 3 * sizeof(GLubyte
) );
3280 else if (srcFormat
== GL_RGBA
) {
3282 const GLubyte
*src
= (const GLubyte
*) source
;
3283 GLubyte
*dst
= dest
;
3284 for (i
= 0; i
< n
; i
++) {
3294 else if (dstFormat
== GL_ALPHA
&& srcFormat
== GL_ALPHA
) {
3295 MEMCPY( dest
, source
, n
* sizeof(GLubyte
) );
3302 /* general solution */
3303 GLfloat rgba
[MAX_WIDTH
][4];
3304 GLint dstComponents
;
3305 GLint dstRedIndex
, dstGreenIndex
, dstBlueIndex
, dstAlphaIndex
;
3306 GLint dstLuminanceIndex
, dstIntensityIndex
;
3308 dstComponents
= gl_components_in_format( dstFormat
);
3309 /* source & dest image formats should have been error checked by now */
3310 assert(dstComponents
> 0);
3313 * Extract image data and convert to RGBA floats
3315 assert(n
<= MAX_WIDTH
);
3316 if (srcFormat
== GL_COLOR_INDEX
) {
3317 GLuint indexes
[MAX_WIDTH
];
3318 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
3321 /* shift and offset indexes */
3322 gl_shift_and_offset_ci(ctx
, n
, indexes
);
3324 if (dstFormat
== GL_COLOR_INDEX
) {
3325 if (applyTransferOps
) {
3326 if (ctx
->Pixel
.MapColorFlag
) {
3327 /* Apply lookup table */
3328 gl_map_ci(ctx
, n
, indexes
);
3331 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
3336 /* convert to GLubyte and return */
3339 for (i
= 0; i
< n
; i
++) {
3340 dest
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
3345 /* Convert indexes to RGBA */
3346 gl_map_ci_to_rgba_float(ctx
, n
, indexes
, rgba
);
3350 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
3351 unpacking
->SwapBytes
);
3353 if (applyTransferOps
) {
3354 /* scale and bias colors */
3355 gl_scale_and_bias_rgba_float(ctx
, n
, rgba
);
3357 /* color table lookup */
3358 if (ctx
->Pixel
.MapColorFlag
) {
3359 gl_map_rgba_float(ctx
, n
, rgba
);
3366 * XXX this is where more color table lookups, convolution and
3367 * histogram would take place, if implemented.
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
);