2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THEA AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Image and pixel span packing and unpacking.
33 * XXX: MSVC takes forever to compile this module for x86_64 unless we disable
34 * this global optimization.
37 * - http://msdn.microsoft.com/en-us/library/1yk3ydd7.aspx
38 * - http://msdn.microsoft.com/en-us/library/chh3fb0k.aspx
40 #if defined(_MSC_VER) && defined(_M_X64)
41 # pragma optimize( "g", off )
53 #include "pixeltransfer.h"
55 #include "glformats.h"
56 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
57 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
58 #include "format_utils.h"
59 #include "format_pack.h"
63 * Flip the 8 bits in each byte of the given array.
66 * \param n number of bytes.
68 * \todo try this trick to flip bytes someday:
70 * v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
71 * v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
72 * v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
76 flip_bytes( GLubyte
*p
, GLuint n
)
79 for (i
= 0; i
< n
; i
++) {
80 b
= (GLuint
) p
[i
]; /* words are often faster than bytes */
81 a
= ((b
& 0x01) << 7) |
96 * Unpack a 32x32 pixel polygon stipple from user memory using the
97 * current pixel unpack settings.
100 _mesa_unpack_polygon_stipple( const GLubyte
*pattern
, GLuint dest
[32],
101 const struct gl_pixelstore_attrib
*unpacking
)
103 GLubyte
*ptrn
= (GLubyte
*) _mesa_unpack_bitmap(32, 32, pattern
, unpacking
);
105 /* Convert pattern from GLubytes to GLuints and handle big/little
110 for (i
= 0; i
< 32; i
++) {
111 dest
[i
] = (p
[0] << 24)
123 * Pack polygon stipple into user memory given current pixel packing
127 _mesa_pack_polygon_stipple( const GLuint pattern
[32], GLubyte
*dest
,
128 const struct gl_pixelstore_attrib
*packing
)
130 /* Convert pattern from GLuints to GLubytes to handle big/little
131 * endian differences.
135 for (i
= 0; i
< 32; i
++) {
136 ptrn
[i
* 4 + 0] = (GLubyte
) ((pattern
[i
] >> 24) & 0xff);
137 ptrn
[i
* 4 + 1] = (GLubyte
) ((pattern
[i
] >> 16) & 0xff);
138 ptrn
[i
* 4 + 2] = (GLubyte
) ((pattern
[i
] >> 8 ) & 0xff);
139 ptrn
[i
* 4 + 3] = (GLubyte
) ((pattern
[i
] ) & 0xff);
142 _mesa_pack_bitmap(32, 32, ptrn
, dest
, packing
);
147 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
148 * order with row alignment = 1 byte.
151 _mesa_unpack_bitmap( GLint width
, GLint height
, const GLubyte
*pixels
,
152 const struct gl_pixelstore_attrib
*packing
)
154 GLint bytes
, row
, width_in_bytes
;
155 GLubyte
*buffer
, *dst
;
160 /* Alloc dest storage */
161 bytes
= ((width
+ 7) / 8 * height
);
162 buffer
= malloc( bytes
);
166 width_in_bytes
= CEILING( width
, 8 );
168 for (row
= 0; row
< height
; row
++) {
169 const GLubyte
*src
= (const GLubyte
*)
170 _mesa_image_address2d(packing
, pixels
, width
, height
,
171 GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
177 if ((packing
->SkipPixels
& 7) == 0) {
178 memcpy( dst
, src
, width_in_bytes
);
179 if (packing
->LsbFirst
) {
180 flip_bytes( dst
, width_in_bytes
);
184 /* handling SkipPixels is a bit tricky (no pun intended!) */
186 if (packing
->LsbFirst
) {
187 GLubyte srcMask
= 1 << (packing
->SkipPixels
& 0x7);
188 GLubyte dstMask
= 128;
189 const GLubyte
*s
= src
;
192 for (i
= 0; i
< width
; i
++) {
196 if (srcMask
== 128) {
201 srcMask
= srcMask
<< 1;
209 dstMask
= dstMask
>> 1;
214 GLubyte srcMask
= 128 >> (packing
->SkipPixels
& 0x7);
215 GLubyte dstMask
= 128;
216 const GLubyte
*s
= src
;
219 for (i
= 0; i
< width
; i
++) {
228 srcMask
= srcMask
>> 1;
236 dstMask
= dstMask
>> 1;
241 dst
+= width_in_bytes
;
252 _mesa_pack_bitmap( GLint width
, GLint height
, const GLubyte
*source
,
253 GLubyte
*dest
, const struct gl_pixelstore_attrib
*packing
)
255 GLint row
, width_in_bytes
;
261 width_in_bytes
= CEILING( width
, 8 );
263 for (row
= 0; row
< height
; row
++) {
264 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, dest
,
265 width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
269 if ((packing
->SkipPixels
& 7) == 0) {
270 memcpy( dst
, src
, width_in_bytes
);
271 if (packing
->LsbFirst
) {
272 flip_bytes( dst
, width_in_bytes
);
276 /* handling SkipPixels is a bit tricky (no pun intended!) */
278 if (packing
->LsbFirst
) {
279 GLubyte srcMask
= 128;
280 GLubyte dstMask
= 1 << (packing
->SkipPixels
& 0x7);
281 const GLubyte
*s
= src
;
284 for (i
= 0; i
< width
; i
++) {
293 srcMask
= srcMask
>> 1;
295 if (dstMask
== 128) {
301 dstMask
= dstMask
<< 1;
306 GLubyte srcMask
= 128;
307 GLubyte dstMask
= 128 >> (packing
->SkipPixels
& 0x7);
308 const GLubyte
*s
= src
;
311 for (i
= 0; i
< width
; i
++) {
320 srcMask
= srcMask
>> 1;
328 dstMask
= dstMask
>> 1;
333 src
+= width_in_bytes
;
339 * Get indexes of color components for a basic color format, such as
340 * GL_RGBA, GL_RED, GL_LUMINANCE_ALPHA, etc. Return -1 for indexes
344 get_component_indexes(GLenum format
,
349 GLint
*luminanceIndex
,
350 GLint
*intensityIndex
)
356 *luminanceIndex
= -1;
357 *intensityIndex
= -1;
361 case GL_LUMINANCE_INTEGER_EXT
:
364 case GL_LUMINANCE_ALPHA
:
365 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
373 case GL_RED_INTEGER_EXT
:
377 case GL_GREEN_INTEGER_EXT
:
381 case GL_BLUE_INTEGER_EXT
:
385 case GL_ALPHA_INTEGER_EXT
:
394 case GL_RGB_INTEGER_EXT
:
400 case GL_BGR_INTEGER_EXT
:
406 case GL_RGBA_INTEGER_EXT
:
413 case GL_BGRA_INTEGER
:
426 assert(0 && "bad format in get_component_indexes()");
433 * For small integer types, return the min and max possible values.
434 * Used for clamping floats to unscaled integer types.
435 * \return GL_TRUE if type is handled, GL_FALSE otherwise.
438 get_type_min_max(GLenum type
, GLfloat
*min
, GLfloat
*max
)
445 case GL_UNSIGNED_BYTE
:
453 case GL_UNSIGNED_SHORT
:
462 /* Customization of unsigned integer packing.
464 #define SRC_TYPE GLuint
466 #define DST_TYPE GLuint
467 #define SRC_CONVERT(x) (x)
468 #define FN_NAME pack_uint_from_uint_rgba
469 #include "pack_tmp.h"
474 #define DST_TYPE GLint
475 #define SRC_CONVERT(x) MIN2(x, 0x7fffffff)
476 #define FN_NAME pack_int_from_uint_rgba
477 #include "pack_tmp.h"
482 #define DST_TYPE GLushort
483 #define SRC_CONVERT(x) MIN2(x, 0xffff)
484 #define FN_NAME pack_ushort_from_uint_rgba
485 #include "pack_tmp.h"
490 #define DST_TYPE GLshort
491 #define SRC_CONVERT(x) CLAMP((int)x, -32768, 32767)
492 #define FN_NAME pack_short_from_uint_rgba
493 #include "pack_tmp.h"
498 #define DST_TYPE GLubyte
499 #define SRC_CONVERT(x) MIN2(x, 0xff)
500 #define FN_NAME pack_ubyte_from_uint_rgba
501 #include "pack_tmp.h"
506 #define DST_TYPE GLbyte
507 #define SRC_CONVERT(x) CLAMP((int)x, -128, 127)
508 #define FN_NAME pack_byte_from_uint_rgba
509 #include "pack_tmp.h"
517 _pack_rgba_span_from_uints_problem(struct gl_context
*ctx
,
518 GLenum dstFormat
, GLenum dstType
)
521 "Unsupported type (%s) / format (%s) "
522 "in _mesa_pack_rgba_span_from_uints",
523 _mesa_lookup_enum_by_nr(dstType
),
524 _mesa_lookup_enum_by_nr(dstFormat
));
528 _mesa_pack_rgba_span_from_uints(struct gl_context
*ctx
, GLuint n
, GLuint rgba
[][4],
529 GLenum dstFormat
, GLenum dstType
,
532 uint32_t dstMesaFormat
;
535 case GL_UNSIGNED_INT
:
536 pack_uint_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
539 pack_int_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
541 case GL_UNSIGNED_SHORT
:
542 pack_ushort_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
545 pack_short_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
547 case GL_UNSIGNED_BYTE
:
548 pack_ubyte_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
551 pack_byte_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
553 case GL_UNSIGNED_BYTE_3_3_2
:
554 case GL_UNSIGNED_BYTE_2_3_3_REV
:
555 case GL_UNSIGNED_SHORT_5_6_5
:
556 case GL_UNSIGNED_SHORT_5_6_5_REV
:
557 case GL_UNSIGNED_SHORT_4_4_4_4
:
558 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
559 case GL_UNSIGNED_SHORT_5_5_5_1
:
560 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
561 case GL_UNSIGNED_INT_8_8_8_8
:
562 case GL_UNSIGNED_INT_8_8_8_8_REV
:
563 case GL_UNSIGNED_INT_10_10_10_2
:
564 case GL_UNSIGNED_INT_2_10_10_10_REV
:
565 dstMesaFormat
= _mesa_format_from_format_and_type(dstFormat
, dstType
);
566 if (!(dstMesaFormat
& MESA_ARRAY_FORMAT_BIT
)) {
567 _mesa_pack_uint_rgba_row(dstMesaFormat
, n
, (void *)rgba
[0], (void *)dstAddr
);
573 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
578 /* Customization of signed integer packing.
580 #define SRC_TYPE GLint
582 #define DST_TYPE GLuint
583 #define SRC_CONVERT(x) MAX2(x, 0)
584 #define FN_NAME pack_uint_from_int_rgba
585 #include "pack_tmp.h"
590 #define DST_TYPE GLushort
591 #define SRC_CONVERT(x) MAX2(x, 0)
592 #define FN_NAME pack_ushort_from_int_rgba
593 #include "pack_tmp.h"
598 #define DST_TYPE GLshort
599 #define SRC_CONVERT(x) CLAMP(x, -0x8000, 0x7fff)
600 #define FN_NAME pack_short_from_int_rgba
601 #include "pack_tmp.h"
606 #define DST_TYPE GLubyte
607 #define SRC_CONVERT(x) MAX2(x, 0)
608 #define FN_NAME pack_ubyte_from_int_rgba
609 #include "pack_tmp.h"
614 #define DST_TYPE GLbyte
615 #define SRC_CONVERT(x) CLAMP(x, -0x80, 0x7f)
616 #define FN_NAME pack_byte_from_int_rgba
617 #include "pack_tmp.h"
625 _pack_rgba_span_from_ints_problem(struct gl_context
*ctx
,
626 GLenum dstFormat
, GLenum dstType
)
629 "Unsupported type (%s) / format (%s) "
630 "in _mesa_pack_rgba_span_from_ints",
631 _mesa_lookup_enum_by_nr(dstType
),
632 _mesa_lookup_enum_by_nr(dstFormat
));
636 _mesa_pack_rgba_span_from_ints(struct gl_context
*ctx
, GLuint n
, GLint rgba
[][4],
637 GLenum dstFormat
, GLenum dstType
,
640 uint32_t dstMesaFormat
;
643 case GL_UNSIGNED_INT
:
644 pack_uint_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
647 /* No conversion necessary. */
648 pack_uint_from_uint_rgba(ctx
, dstAddr
, dstFormat
, (GLuint (*)[4]) rgba
, NULL
, n
);
650 case GL_UNSIGNED_SHORT
:
651 pack_ushort_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
654 pack_short_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
656 case GL_UNSIGNED_BYTE
:
657 pack_ubyte_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
660 pack_byte_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
662 case GL_UNSIGNED_BYTE_3_3_2
:
663 case GL_UNSIGNED_BYTE_2_3_3_REV
:
664 case GL_UNSIGNED_SHORT_5_6_5
:
665 case GL_UNSIGNED_SHORT_5_6_5_REV
:
666 case GL_UNSIGNED_SHORT_4_4_4_4
:
667 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
668 case GL_UNSIGNED_SHORT_5_5_5_1
:
669 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
670 case GL_UNSIGNED_INT_8_8_8_8
:
671 case GL_UNSIGNED_INT_8_8_8_8_REV
:
672 case GL_UNSIGNED_INT_10_10_10_2
:
673 case GL_UNSIGNED_INT_2_10_10_10_REV
:
674 dstMesaFormat
= _mesa_format_from_format_and_type(dstFormat
, dstType
);
675 if (!(dstMesaFormat
& MESA_ARRAY_FORMAT_BIT
)) {
676 _mesa_pack_int_rgba_row(dstMesaFormat
, n
, (void *)rgba
[0], (void *)dstAddr
);
682 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
687 /* Customization of float packing.
689 #define SRC_TYPE GLfloat
691 #define DST_TYPE GLuint
692 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UINT(x)
693 #define SRC_CONVERT(x) (GLuint) x
694 #define FN_NAME pack_uint_from_float_rgba
695 #include "pack_tmp.h"
698 #undef FLOAT_SRC_CONVERT
701 #define DST_TYPE GLint
702 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_INT(x)
703 #define SRC_CONVERT(x) (GLint) x
704 #define FN_NAME pack_int_from_float_rgba
705 #include "pack_tmp.h"
708 #undef FLOAT_SRC_CONVERT
711 #define DST_TYPE GLshort
712 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_SHORT_TEX(x)
713 #define SRC_CONVERT(x) (GLshort) x
714 #define FN_NAME pack_short_from_float_rgba
715 #include "pack_tmp.h"
718 #undef FLOAT_SRC_CONVERT
721 #define DST_TYPE GLubyte
722 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UBYTE(x)
723 #define SRC_CONVERT(x) (GLubyte) x
724 #define FN_NAME pack_ubyte_from_float_rgba
725 #include "pack_tmp.h"
728 #undef FLOAT_SRC_CONVERT
731 #define DST_TYPE GLbyte
732 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_BYTE_TEX(x)
733 #define SRC_CONVERT(x) (GLbyte) x
734 #define FN_NAME pack_byte_from_float_rgba
735 #include "pack_tmp.h"
738 #undef FLOAT_SRC_CONVERT
741 #define DST_TYPE GLfloat
742 #define FLOAT_SRC_CONVERT(x) x
743 #define SRC_CONVERT(x) x
744 #define FN_NAME pack_float_from_float_rgba
745 #include "pack_tmp.h"
748 #undef FLOAT_SRC_CONVERT
751 #define DST_TYPE GLhalfARB
752 #define FLOAT_SRC_CONVERT(x) _mesa_float_to_half(x)
753 #define FN_NAME pack_half_float_from_float_rgba
754 #include "pack_tmp.h"
757 #undef FLOAT_SRC_CONVERT
763 * Used to pack an array [][4] of RGBA float colors as specified
764 * by the dstFormat, dstType and dstPacking. Used by glReadPixels.
765 * Historically, the RGBA values were in [0,1] and rescaled to fit
766 * into GLubytes, etc. But with new integer formats, the RGBA values
767 * may have any value and we don't always rescale when converting to
770 * Note: the rgba values will be modified by this function when any pixel
771 * transfer ops are enabled.
774 _mesa_pack_rgba_span_float(struct gl_context
*ctx
, GLuint n
, GLfloat rgba
[][4],
775 GLenum dstFormat
, GLenum dstType
,
777 const struct gl_pixelstore_attrib
*dstPacking
,
778 GLbitfield transferOps
)
781 const GLint comps
= _mesa_components_in_format(dstFormat
);
782 const GLboolean intDstFormat
= _mesa_is_enum_format_integer(dstFormat
);
784 uint32_t dstMesaFormat
;
786 if (dstFormat
== GL_LUMINANCE
||
787 dstFormat
== GL_LUMINANCE_ALPHA
||
788 dstFormat
== GL_LUMINANCE_INTEGER_EXT
||
789 dstFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
) {
790 luminance
= malloc(n
* sizeof(GLfloat
));
792 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
800 /* EXT_texture_integer specifies no transfer ops on integer
801 * types in the resolved issues section. Just set them to 0
802 * for integer surfaces.
808 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
812 * Component clamping (besides clamping to [0,1] in
813 * _mesa_apply_rgba_transfer_ops()).
816 /* clamping to dest type's min/max values */
818 if (get_type_min_max(dstType
, &min
, &max
)) {
819 for (i
= 0; i
< n
; i
++) {
820 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], min
, max
);
821 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], min
, max
);
822 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], min
, max
);
823 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], min
, max
);
827 else if (dstFormat
== GL_LUMINANCE
|| dstFormat
== GL_LUMINANCE_ALPHA
) {
828 /* compute luminance values */
829 if (transferOps
& IMAGE_CLAMP_BIT
) {
830 for (i
= 0; i
< n
; i
++) {
831 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
832 luminance
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
836 for (i
= 0; i
< n
; i
++) {
837 luminance
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
843 * Pack/store the pixels. Ugh! Lots of cases!!!
846 case GL_UNSIGNED_BYTE
:
847 pack_ubyte_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
850 pack_byte_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
852 case GL_UNSIGNED_SHORT
:
854 GLushort
*dst
= (GLushort
*) dstAddr
;
858 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][RCOMP
]);
862 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][GCOMP
]);
866 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][BCOMP
]);
870 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][ACOMP
]);
874 UNCLAMPED_FLOAT_TO_USHORT(dst
[i
], luminance
[i
]);
876 case GL_LUMINANCE_ALPHA
:
878 UNCLAMPED_FLOAT_TO_USHORT(dst
[i
*2+0], luminance
[i
]);
879 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+1], rgba
[i
][ACOMP
]);
884 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+0], rgba
[i
][RCOMP
]);
885 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+1], rgba
[i
][GCOMP
]);
890 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+0], rgba
[i
][RCOMP
]);
891 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+1], rgba
[i
][GCOMP
]);
892 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+2], rgba
[i
][BCOMP
]);
897 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][RCOMP
]);
898 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][GCOMP
]);
899 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][BCOMP
]);
900 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][ACOMP
]);
905 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+0], rgba
[i
][BCOMP
]);
906 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+1], rgba
[i
][GCOMP
]);
907 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+2], rgba
[i
][RCOMP
]);
912 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][BCOMP
]);
913 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][GCOMP
]);
914 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][RCOMP
]);
915 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][ACOMP
]);
920 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][ACOMP
]);
921 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][BCOMP
]);
922 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][GCOMP
]);
923 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][RCOMP
]);
926 case GL_RED_INTEGER_EXT
:
928 dst
[i
] = (GLushort
) rgba
[i
][RCOMP
];
931 case GL_GREEN_INTEGER_EXT
:
933 dst
[i
] = (GLushort
) rgba
[i
][GCOMP
];
936 case GL_BLUE_INTEGER_EXT
:
938 dst
[i
] = (GLushort
) rgba
[i
][BCOMP
];
941 case GL_ALPHA_INTEGER_EXT
:
943 dst
[i
] = (GLushort
) rgba
[i
][ACOMP
];
948 dst
[i
*2+0] = (GLushort
) rgba
[i
][RCOMP
];
949 dst
[i
*2+1] = (GLushort
) rgba
[i
][GCOMP
];
952 case GL_RGB_INTEGER_EXT
:
954 dst
[i
*3+0] = (GLushort
) rgba
[i
][RCOMP
];
955 dst
[i
*3+1] = (GLushort
) rgba
[i
][GCOMP
];
956 dst
[i
*3+2] = (GLushort
) rgba
[i
][BCOMP
];
959 case GL_RGBA_INTEGER_EXT
:
961 dst
[i
*4+0] = (GLushort
) rgba
[i
][RCOMP
];
962 dst
[i
*4+1] = (GLushort
) rgba
[i
][GCOMP
];
963 dst
[i
*4+2] = (GLushort
) rgba
[i
][BCOMP
];
964 dst
[i
*4+3] = (GLushort
) rgba
[i
][ACOMP
];
967 case GL_BGR_INTEGER_EXT
:
969 dst
[i
*3+0] = (GLushort
) rgba
[i
][BCOMP
];
970 dst
[i
*3+1] = (GLushort
) rgba
[i
][GCOMP
];
971 dst
[i
*3+2] = (GLushort
) rgba
[i
][RCOMP
];
974 case GL_BGRA_INTEGER_EXT
:
976 dst
[i
*4+0] = (GLushort
) rgba
[i
][BCOMP
];
977 dst
[i
*4+1] = (GLushort
) rgba
[i
][GCOMP
];
978 dst
[i
*4+2] = (GLushort
) rgba
[i
][RCOMP
];
979 dst
[i
*4+3] = (GLushort
) rgba
[i
][ACOMP
];
982 case GL_LUMINANCE_INTEGER_EXT
:
984 dst
[i
*2+0] = (GLushort
) (rgba
[i
][RCOMP
] +
987 dst
[i
*2+1] = (GLushort
) rgba
[i
][ACOMP
];
990 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
992 dst
[i
] = (GLushort
) (rgba
[i
][RCOMP
] +
998 _mesa_problem(ctx
, "bad format in _mesa_pack_rgba_span\n");
1003 pack_short_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1005 case GL_UNSIGNED_INT
:
1006 pack_uint_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1009 pack_int_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1012 /* No conversion necessary. */
1013 pack_float_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1015 case GL_HALF_FLOAT_ARB
:
1016 pack_half_float_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1018 case GL_UNSIGNED_BYTE_3_3_2
:
1019 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1020 case GL_UNSIGNED_SHORT_5_6_5
:
1021 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1022 case GL_UNSIGNED_SHORT_4_4_4_4
:
1023 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1024 case GL_UNSIGNED_SHORT_5_5_5_1
:
1025 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1026 case GL_UNSIGNED_INT_8_8_8_8
:
1027 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1028 case GL_UNSIGNED_INT_10_10_10_2
:
1029 case GL_UNSIGNED_INT_2_10_10_10_REV
:
1030 case GL_UNSIGNED_INT_5_9_9_9_REV
:
1031 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
1032 dstMesaFormat
= _mesa_format_from_format_and_type(dstFormat
, dstType
);
1033 if (!(dstMesaFormat
& MESA_ARRAY_FORMAT_BIT
)) {
1034 _mesa_pack_float_rgba_row(dstMesaFormat
, n
, (void *)rgba
[0], (void *)dstAddr
);
1040 _mesa_problem(ctx
, "bad type in _mesa_pack_rgba_span_float");
1045 if (dstPacking
->SwapBytes
) {
1046 GLint swapSize
= _mesa_sizeof_packed_type(dstType
);
1047 if (swapSize
== 2) {
1048 _mesa_swap2((GLushort
*) dstAddr
, n
* comps
);
1050 else if (swapSize
== 4) {
1051 _mesa_swap4((GLuint
*) dstAddr
, n
* comps
);
1060 #define SWAP2BYTE(VALUE) \
1062 GLubyte *bytes = (GLubyte *) &(VALUE); \
1063 GLubyte tmp = bytes[0]; \
1064 bytes[0] = bytes[1]; \
1068 #define SWAP4BYTE(VALUE) \
1070 GLubyte *bytes = (GLubyte *) &(VALUE); \
1071 GLubyte tmp = bytes[0]; \
1072 bytes[0] = bytes[3]; \
1075 bytes[1] = bytes[2]; \
1081 extract_uint_indexes(GLuint n
, GLuint indexes
[],
1082 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1083 const struct gl_pixelstore_attrib
*unpack
)
1085 ASSERT(srcFormat
== GL_COLOR_INDEX
|| srcFormat
== GL_STENCIL_INDEX
);
1087 ASSERT(srcType
== GL_BITMAP
||
1088 srcType
== GL_UNSIGNED_BYTE
||
1089 srcType
== GL_BYTE
||
1090 srcType
== GL_UNSIGNED_SHORT
||
1091 srcType
== GL_SHORT
||
1092 srcType
== GL_UNSIGNED_INT
||
1093 srcType
== GL_INT
||
1094 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
1095 srcType
== GL_HALF_FLOAT_ARB
||
1096 srcType
== GL_FLOAT
||
1097 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
1102 GLubyte
*ubsrc
= (GLubyte
*) src
;
1103 if (unpack
->LsbFirst
) {
1104 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
1106 for (i
= 0; i
< n
; i
++) {
1107 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1118 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
1120 for (i
= 0; i
< n
; i
++) {
1121 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1133 case GL_UNSIGNED_BYTE
:
1136 const GLubyte
*s
= (const GLubyte
*) src
;
1137 for (i
= 0; i
< n
; i
++)
1144 const GLbyte
*s
= (const GLbyte
*) src
;
1145 for (i
= 0; i
< n
; i
++)
1149 case GL_UNSIGNED_SHORT
:
1152 const GLushort
*s
= (const GLushort
*) src
;
1153 if (unpack
->SwapBytes
) {
1154 for (i
= 0; i
< n
; i
++) {
1155 GLushort value
= s
[i
];
1161 for (i
= 0; i
< n
; i
++)
1169 const GLshort
*s
= (const GLshort
*) src
;
1170 if (unpack
->SwapBytes
) {
1171 for (i
= 0; i
< n
; i
++) {
1172 GLshort value
= s
[i
];
1178 for (i
= 0; i
< n
; i
++)
1183 case GL_UNSIGNED_INT
:
1186 const GLuint
*s
= (const GLuint
*) src
;
1187 if (unpack
->SwapBytes
) {
1188 for (i
= 0; i
< n
; i
++) {
1189 GLuint value
= s
[i
];
1195 for (i
= 0; i
< n
; i
++)
1203 const GLint
*s
= (const GLint
*) src
;
1204 if (unpack
->SwapBytes
) {
1205 for (i
= 0; i
< n
; i
++) {
1212 for (i
= 0; i
< n
; i
++)
1220 const GLfloat
*s
= (const GLfloat
*) src
;
1221 if (unpack
->SwapBytes
) {
1222 for (i
= 0; i
< n
; i
++) {
1223 GLfloat value
= s
[i
];
1225 indexes
[i
] = (GLuint
) value
;
1229 for (i
= 0; i
< n
; i
++)
1230 indexes
[i
] = (GLuint
) s
[i
];
1234 case GL_HALF_FLOAT_ARB
:
1237 const GLhalfARB
*s
= (const GLhalfARB
*) src
;
1238 if (unpack
->SwapBytes
) {
1239 for (i
= 0; i
< n
; i
++) {
1240 GLhalfARB value
= s
[i
];
1242 indexes
[i
] = (GLuint
) _mesa_half_to_float(value
);
1246 for (i
= 0; i
< n
; i
++)
1247 indexes
[i
] = (GLuint
) _mesa_half_to_float(s
[i
]);
1251 case GL_UNSIGNED_INT_24_8_EXT
:
1254 const GLuint
*s
= (const GLuint
*) src
;
1255 if (unpack
->SwapBytes
) {
1256 for (i
= 0; i
< n
; i
++) {
1257 GLuint value
= s
[i
];
1259 indexes
[i
] = value
& 0xff; /* lower 8 bits */
1263 for (i
= 0; i
< n
; i
++)
1264 indexes
[i
] = s
[i
] & 0xff; /* lower 8 bits */
1268 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1271 const GLuint
*s
= (const GLuint
*) src
;
1272 if (unpack
->SwapBytes
) {
1273 for (i
= 0; i
< n
; i
++) {
1274 GLuint value
= s
[i
*2+1];
1276 indexes
[i
] = value
& 0xff; /* lower 8 bits */
1280 for (i
= 0; i
< n
; i
++)
1281 indexes
[i
] = s
[i
*2+1] & 0xff; /* lower 8 bits */
1287 _mesa_problem(NULL
, "bad srcType in extract_uint_indexes");
1294 * Return source/dest RGBA indexes for unpacking pixels.
1297 get_component_mapping(GLenum format
,
1309 case GL_RED_INTEGER_EXT
:
1311 *gSrc
= *bSrc
= *aSrc
= -1;
1314 case GL_GREEN_INTEGER_EXT
:
1316 *rSrc
= *bSrc
= *aSrc
= -1;
1319 case GL_BLUE_INTEGER_EXT
:
1321 *rSrc
= *gSrc
= *aSrc
= -1;
1324 case GL_ALPHA_INTEGER_EXT
:
1325 *rSrc
= *gSrc
= *bSrc
= -1;
1329 case GL_LUMINANCE_INTEGER_EXT
:
1330 *rSrc
= *gSrc
= *bSrc
= 0;
1333 case GL_LUMINANCE_ALPHA
:
1334 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
1335 *rSrc
= *gSrc
= *bSrc
= 0;
1339 *rSrc
= *gSrc
= *bSrc
= *aSrc
= 0;
1353 case GL_RGB_INTEGER
:
1364 case GL_BGR_INTEGER
:
1375 case GL_RGBA_INTEGER
:
1386 case GL_BGRA_INTEGER
:
1407 _mesa_problem(NULL
, "bad srcFormat %s in get_component_mapping",
1408 _mesa_lookup_enum_by_nr(format
));
1416 * This function extracts floating point RGBA values from arbitrary
1417 * image data. srcFormat and srcType are the format and type parameters
1418 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1420 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1421 * implements the "Conversion to floating point", "Conversion to RGB",
1422 * and "Final Expansion to RGBA" operations.
1424 * Args: n - number of pixels
1425 * rgba - output colors
1426 * srcFormat - format of incoming data
1427 * srcType - data type of incoming data
1428 * src - source data pointer
1429 * swapBytes - perform byteswapping of incoming data?
1432 extract_float_rgba(GLuint n
, GLfloat rgba
[][4],
1433 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1434 GLboolean swapBytes
)
1436 GLint rSrc
, gSrc
, bSrc
, aSrc
;
1438 GLint rDst
, bDst
, gDst
, aDst
;
1439 GLboolean intFormat
;
1440 GLfloat rs
= 1.0f
, gs
= 1.0f
, bs
= 1.0f
, as
= 1.0f
; /* scale factors */
1442 ASSERT(srcFormat
== GL_RED
||
1443 srcFormat
== GL_GREEN
||
1444 srcFormat
== GL_BLUE
||
1445 srcFormat
== GL_ALPHA
||
1446 srcFormat
== GL_LUMINANCE
||
1447 srcFormat
== GL_LUMINANCE_ALPHA
||
1448 srcFormat
== GL_INTENSITY
||
1449 srcFormat
== GL_RG
||
1450 srcFormat
== GL_RGB
||
1451 srcFormat
== GL_BGR
||
1452 srcFormat
== GL_RGBA
||
1453 srcFormat
== GL_BGRA
||
1454 srcFormat
== GL_ABGR_EXT
||
1455 srcFormat
== GL_RED_INTEGER_EXT
||
1456 srcFormat
== GL_GREEN_INTEGER_EXT
||
1457 srcFormat
== GL_BLUE_INTEGER_EXT
||
1458 srcFormat
== GL_ALPHA_INTEGER_EXT
||
1459 srcFormat
== GL_RG_INTEGER
||
1460 srcFormat
== GL_RGB_INTEGER_EXT
||
1461 srcFormat
== GL_RGBA_INTEGER_EXT
||
1462 srcFormat
== GL_BGR_INTEGER_EXT
||
1463 srcFormat
== GL_BGRA_INTEGER_EXT
||
1464 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
1465 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
1467 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
1468 srcType
== GL_BYTE
||
1469 srcType
== GL_UNSIGNED_SHORT
||
1470 srcType
== GL_SHORT
||
1471 srcType
== GL_UNSIGNED_INT
||
1472 srcType
== GL_INT
||
1473 srcType
== GL_HALF_FLOAT_ARB
||
1474 srcType
== GL_FLOAT
||
1475 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
1476 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
1477 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
1478 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
1479 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
1480 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
1481 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
1482 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
1483 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
1484 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
1485 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
1486 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
1487 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
1488 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
1490 get_component_mapping(srcFormat
,
1491 &rSrc
, &gSrc
, &bSrc
, &aSrc
,
1492 &rDst
, &gDst
, &bDst
, &aDst
);
1494 stride
= _mesa_components_in_format(srcFormat
);
1496 intFormat
= _mesa_is_enum_format_integer(srcFormat
);
1498 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT_FLT, DEFAULT_INT, TYPE, CONVERSION) \
1499 if ((SRC_INDEX) < 0) { \
1502 for (i = 0; i < n; i++) { \
1503 rgba[i][DST_INDEX] = DEFAULT_INT; \
1507 for (i = 0; i < n; i++) { \
1508 rgba[i][DST_INDEX] = DEFAULT_FLT; \
1512 else if (swapBytes) { \
1513 const TYPE *s = (const TYPE *) src; \
1515 for (i = 0; i < n; i++) { \
1516 TYPE value = s[SRC_INDEX]; \
1517 if (sizeof(TYPE) == 2) { \
1520 else if (sizeof(TYPE) == 4) { \
1524 rgba[i][DST_INDEX] = (GLfloat) value; \
1526 rgba[i][DST_INDEX] = (GLfloat) CONVERSION(value); \
1531 const TYPE *s = (const TYPE *) src; \
1534 for (i = 0; i < n; i++) { \
1535 rgba[i][DST_INDEX] = (GLfloat) s[SRC_INDEX]; \
1540 for (i = 0; i < n; i++) { \
1541 rgba[i][DST_INDEX] = (GLfloat) CONVERSION(s[SRC_INDEX]); \
1548 case GL_UNSIGNED_BYTE
:
1549 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1550 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1551 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1552 PROCESS(aSrc
, ACOMP
, 1.0F
, 255, GLubyte
, UBYTE_TO_FLOAT
);
1555 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1556 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1557 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1558 PROCESS(aSrc
, ACOMP
, 1.0F
, 127, GLbyte
, BYTE_TO_FLOAT_TEX
);
1560 case GL_UNSIGNED_SHORT
:
1561 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1562 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1563 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1564 PROCESS(aSrc
, ACOMP
, 1.0F
, 0xffff, GLushort
, USHORT_TO_FLOAT
);
1567 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1568 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1569 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1570 PROCESS(aSrc
, ACOMP
, 1.0F
, 32767, GLshort
, SHORT_TO_FLOAT_TEX
);
1572 case GL_UNSIGNED_INT
:
1573 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1574 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1575 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1576 PROCESS(aSrc
, ACOMP
, 1.0F
, 0xffffffff, GLuint
, UINT_TO_FLOAT
);
1579 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1580 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1581 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1582 PROCESS(aSrc
, ACOMP
, 1.0F
, 2147483647, GLint
, INT_TO_FLOAT
);
1585 PROCESS(rSrc
, RCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1586 PROCESS(gSrc
, GCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1587 PROCESS(bSrc
, BCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1588 PROCESS(aSrc
, ACOMP
, 1.0F
, 1.0F
, GLfloat
, (GLfloat
));
1590 case GL_HALF_FLOAT_ARB
:
1591 PROCESS(rSrc
, RCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1592 PROCESS(gSrc
, GCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1593 PROCESS(bSrc
, BCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1594 PROCESS(aSrc
, ACOMP
, 1.0F
, 1.0F
, GLhalfARB
, _mesa_half_to_float
);
1596 case GL_UNSIGNED_BYTE_3_3_2
:
1598 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1605 for (i
= 0; i
< n
; i
++) {
1606 GLubyte p
= ubsrc
[i
];
1607 rgba
[i
][rDst
] = ((p
>> 5) ) * rs
;
1608 rgba
[i
][gDst
] = ((p
>> 2) & 0x7) * gs
;
1609 rgba
[i
][bDst
] = ((p
) & 0x3) * bs
;
1610 rgba
[i
][aDst
] = 1.0F
;
1614 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1616 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1623 for (i
= 0; i
< n
; i
++) {
1624 GLubyte p
= ubsrc
[i
];
1625 rgba
[i
][rDst
] = ((p
) & 0x7) * rs
;
1626 rgba
[i
][gDst
] = ((p
>> 3) & 0x7) * gs
;
1627 rgba
[i
][bDst
] = ((p
>> 6) ) * bs
;
1628 rgba
[i
][aDst
] = 1.0F
;
1632 case GL_UNSIGNED_SHORT_5_6_5
:
1639 const GLushort
*ussrc
= (const GLushort
*) src
;
1641 for (i
= 0; i
< n
; i
++) {
1642 GLushort p
= ussrc
[i
];
1644 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1645 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1646 rgba
[i
][bDst
] = ((p
) & 0x1f) * bs
;
1647 rgba
[i
][aDst
] = 1.0F
;
1651 const GLushort
*ussrc
= (const GLushort
*) src
;
1653 for (i
= 0; i
< n
; i
++) {
1654 GLushort p
= ussrc
[i
];
1655 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1656 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1657 rgba
[i
][bDst
] = ((p
) & 0x1f) * bs
;
1658 rgba
[i
][aDst
] = 1.0F
;
1662 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1669 const GLushort
*ussrc
= (const GLushort
*) src
;
1671 for (i
= 0; i
< n
; i
++) {
1672 GLushort p
= ussrc
[i
];
1674 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1675 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1676 rgba
[i
][bDst
] = ((p
>> 11) ) * bs
;
1677 rgba
[i
][aDst
] = 1.0F
;
1681 const GLushort
*ussrc
= (const GLushort
*) src
;
1683 for (i
= 0; i
< n
; i
++) {
1684 GLushort p
= ussrc
[i
];
1685 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1686 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1687 rgba
[i
][bDst
] = ((p
>> 11) ) * bs
;
1688 rgba
[i
][aDst
] = 1.0F
;
1692 case GL_UNSIGNED_SHORT_4_4_4_4
:
1694 rs
= gs
= bs
= as
= 1.0F
/ 15.0F
;
1697 const GLushort
*ussrc
= (const GLushort
*) src
;
1699 for (i
= 0; i
< n
; i
++) {
1700 GLushort p
= ussrc
[i
];
1702 rgba
[i
][rDst
] = ((p
>> 12) ) * rs
;
1703 rgba
[i
][gDst
] = ((p
>> 8) & 0xf) * gs
;
1704 rgba
[i
][bDst
] = ((p
>> 4) & 0xf) * bs
;
1705 rgba
[i
][aDst
] = ((p
) & 0xf) * as
;
1709 const GLushort
*ussrc
= (const GLushort
*) src
;
1711 for (i
= 0; i
< n
; i
++) {
1712 GLushort p
= ussrc
[i
];
1713 rgba
[i
][rDst
] = ((p
>> 12) ) * rs
;
1714 rgba
[i
][gDst
] = ((p
>> 8) & 0xf) * gs
;
1715 rgba
[i
][bDst
] = ((p
>> 4) & 0xf) * bs
;
1716 rgba
[i
][aDst
] = ((p
) & 0xf) * as
;
1720 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1722 rs
= gs
= bs
= as
= 1.0F
/ 15.0F
;
1725 const GLushort
*ussrc
= (const GLushort
*) src
;
1727 for (i
= 0; i
< n
; i
++) {
1728 GLushort p
= ussrc
[i
];
1730 rgba
[i
][rDst
] = ((p
) & 0xf) * rs
;
1731 rgba
[i
][gDst
] = ((p
>> 4) & 0xf) * gs
;
1732 rgba
[i
][bDst
] = ((p
>> 8) & 0xf) * bs
;
1733 rgba
[i
][aDst
] = ((p
>> 12) ) * as
;
1737 const GLushort
*ussrc
= (const GLushort
*) src
;
1739 for (i
= 0; i
< n
; i
++) {
1740 GLushort p
= ussrc
[i
];
1741 rgba
[i
][rDst
] = ((p
) & 0xf) * rs
;
1742 rgba
[i
][gDst
] = ((p
>> 4) & 0xf) * gs
;
1743 rgba
[i
][bDst
] = ((p
>> 8) & 0xf) * bs
;
1744 rgba
[i
][aDst
] = ((p
>> 12) ) * as
;
1748 case GL_UNSIGNED_SHORT_5_5_5_1
:
1750 rs
= gs
= bs
= 1.0F
/ 31.0F
;
1753 const GLushort
*ussrc
= (const GLushort
*) src
;
1755 for (i
= 0; i
< n
; i
++) {
1756 GLushort p
= ussrc
[i
];
1758 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1759 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f) * gs
;
1760 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f) * bs
;
1761 rgba
[i
][aDst
] = ((p
) & 0x1) * as
;
1765 const GLushort
*ussrc
= (const GLushort
*) src
;
1767 for (i
= 0; i
< n
; i
++) {
1768 GLushort p
= ussrc
[i
];
1769 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1770 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f) * gs
;
1771 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f) * bs
;
1772 rgba
[i
][aDst
] = ((p
) & 0x1) * as
;
1776 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1778 rs
= gs
= bs
= 1.0F
/ 31.0F
;
1781 const GLushort
*ussrc
= (const GLushort
*) src
;
1783 for (i
= 0; i
< n
; i
++) {
1784 GLushort p
= ussrc
[i
];
1786 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1787 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f) * gs
;
1788 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f) * bs
;
1789 rgba
[i
][aDst
] = ((p
>> 15) ) * as
;
1793 const GLushort
*ussrc
= (const GLushort
*) src
;
1795 for (i
= 0; i
< n
; i
++) {
1796 GLushort p
= ussrc
[i
];
1797 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1798 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f) * gs
;
1799 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f) * bs
;
1800 rgba
[i
][aDst
] = ((p
>> 15) ) * as
;
1804 case GL_UNSIGNED_INT_8_8_8_8
:
1806 const GLuint
*uisrc
= (const GLuint
*) src
;
1809 for (i
= 0; i
< n
; i
++) {
1810 GLuint p
= uisrc
[i
];
1811 rgba
[i
][rDst
] = (GLfloat
) ((p
) & 0xff);
1812 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 8) & 0xff);
1813 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 16) & 0xff);
1814 rgba
[i
][aDst
] = (GLfloat
) ((p
>> 24) );
1818 for (i
= 0; i
< n
; i
++) {
1819 GLuint p
= uisrc
[i
];
1820 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
1821 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
1822 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
1823 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
>> 24) );
1828 const GLuint
*uisrc
= (const GLuint
*) src
;
1831 for (i
= 0; i
< n
; i
++) {
1832 GLuint p
= uisrc
[i
];
1833 rgba
[i
][rDst
] = (GLfloat
) ((p
>> 24) );
1834 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 16) & 0xff);
1835 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 8) & 0xff);
1836 rgba
[i
][aDst
] = (GLfloat
) ((p
) & 0xff);
1840 for (i
= 0; i
< n
; i
++) {
1841 GLuint p
= uisrc
[i
];
1842 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
>> 24) );
1843 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
1844 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
1845 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
1850 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1852 const GLuint
*uisrc
= (const GLuint
*) src
;
1855 for (i
= 0; i
< n
; i
++) {
1856 GLuint p
= uisrc
[i
];
1857 rgba
[i
][rDst
] = (GLfloat
) ((p
>> 24) );
1858 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 16) & 0xff);
1859 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 8) & 0xff);
1860 rgba
[i
][aDst
] = (GLfloat
) ((p
) & 0xff);
1864 for (i
= 0; i
< n
; i
++) {
1865 GLuint p
= uisrc
[i
];
1866 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
>> 24) );
1867 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
1868 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
1869 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
1874 const GLuint
*uisrc
= (const GLuint
*) src
;
1877 for (i
= 0; i
< n
; i
++) {
1878 GLuint p
= uisrc
[i
];
1879 rgba
[i
][rDst
] = (GLfloat
) ((p
) & 0xff);
1880 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 8) & 0xff);
1881 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 16) & 0xff);
1882 rgba
[i
][aDst
] = (GLfloat
) ((p
>> 24) );
1886 for (i
= 0; i
< n
; i
++) {
1887 GLuint p
= uisrc
[i
];
1888 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
1889 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
1890 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
1891 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
>> 24) );
1896 case GL_UNSIGNED_INT_10_10_10_2
:
1898 rs
= 1.0F
/ 1023.0F
;
1899 gs
= 1.0F
/ 1023.0F
;
1900 bs
= 1.0F
/ 1023.0F
;
1904 const GLuint
*uisrc
= (const GLuint
*) src
;
1906 for (i
= 0; i
< n
; i
++) {
1907 GLuint p
= uisrc
[i
];
1909 rgba
[i
][rDst
] = ((p
>> 22) ) * rs
;
1910 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff) * gs
;
1911 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff) * bs
;
1912 rgba
[i
][aDst
] = ((p
) & 0x3 ) * as
;
1916 const GLuint
*uisrc
= (const GLuint
*) src
;
1918 for (i
= 0; i
< n
; i
++) {
1919 GLuint p
= uisrc
[i
];
1920 rgba
[i
][rDst
] = ((p
>> 22) ) * rs
;
1921 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff) * gs
;
1922 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff) * bs
;
1923 rgba
[i
][aDst
] = ((p
) & 0x3 ) * as
;
1927 case GL_UNSIGNED_INT_2_10_10_10_REV
:
1929 rs
= 1.0F
/ 1023.0F
;
1930 gs
= 1.0F
/ 1023.0F
;
1931 bs
= 1.0F
/ 1023.0F
;
1935 const GLuint
*uisrc
= (const GLuint
*) src
;
1937 for (i
= 0; i
< n
; i
++) {
1938 GLuint p
= uisrc
[i
];
1940 rgba
[i
][rDst
] = ((p
) & 0x3ff) * rs
;
1941 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff) * gs
;
1942 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff) * bs
;
1944 rgba
[i
][aDst
] = 1.0F
;
1946 rgba
[i
][aDst
] = (p
>> 30) * as
;
1951 const GLuint
*uisrc
= (const GLuint
*) src
;
1953 for (i
= 0; i
< n
; i
++) {
1954 GLuint p
= uisrc
[i
];
1955 rgba
[i
][rDst
] = ((p
) & 0x3ff) * rs
;
1956 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff) * gs
;
1957 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff) * bs
;
1959 rgba
[i
][aDst
] = 1.0F
;
1961 rgba
[i
][aDst
] = (p
>> 30) * as
;
1966 case GL_UNSIGNED_INT_5_9_9_9_REV
:
1968 const GLuint
*uisrc
= (const GLuint
*) src
;
1971 for (i
= 0; i
< n
; i
++) {
1972 GLuint p
= uisrc
[i
];
1974 rgb9e5_to_float3(p
, f
);
1975 rgba
[i
][rDst
] = f
[0];
1976 rgba
[i
][gDst
] = f
[1];
1977 rgba
[i
][bDst
] = f
[2];
1978 rgba
[i
][aDst
] = 1.0F
;
1982 const GLuint
*uisrc
= (const GLuint
*) src
;
1985 for (i
= 0; i
< n
; i
++) {
1986 rgb9e5_to_float3(uisrc
[i
], f
);
1987 rgba
[i
][rDst
] = f
[0];
1988 rgba
[i
][gDst
] = f
[1];
1989 rgba
[i
][bDst
] = f
[2];
1990 rgba
[i
][aDst
] = 1.0F
;
1994 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
1996 const GLuint
*uisrc
= (const GLuint
*) src
;
1999 for (i
= 0; i
< n
; i
++) {
2000 GLuint p
= uisrc
[i
];
2002 r11g11b10f_to_float3(p
, f
);
2003 rgba
[i
][rDst
] = f
[0];
2004 rgba
[i
][gDst
] = f
[1];
2005 rgba
[i
][bDst
] = f
[2];
2006 rgba
[i
][aDst
] = 1.0F
;
2010 const GLuint
*uisrc
= (const GLuint
*) src
;
2013 for (i
= 0; i
< n
; i
++) {
2014 r11g11b10f_to_float3(uisrc
[i
], f
);
2015 rgba
[i
][rDst
] = f
[0];
2016 rgba
[i
][gDst
] = f
[1];
2017 rgba
[i
][bDst
] = f
[2];
2018 rgba
[i
][aDst
] = 1.0F
;
2023 _mesa_problem(NULL
, "bad srcType in extract float data");
2030 static inline GLuint
2031 clamp_float_to_uint(GLfloat f
)
2033 return f
< 0.0F
? 0 : F_TO_I(f
);
2037 static inline GLuint
2038 clamp_half_to_uint(GLhalfARB h
)
2040 GLfloat f
= _mesa_half_to_float(h
);
2041 return f
< 0.0F
? 0 : F_TO_I(f
);
2046 * \sa extract_float_rgba()
2049 extract_uint_rgba(GLuint n
, GLuint rgba
[][4],
2050 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
2051 GLboolean swapBytes
)
2053 GLint rSrc
, gSrc
, bSrc
, aSrc
;
2055 GLint rDst
, bDst
, gDst
, aDst
;
2057 ASSERT(srcFormat
== GL_RED
||
2058 srcFormat
== GL_GREEN
||
2059 srcFormat
== GL_BLUE
||
2060 srcFormat
== GL_ALPHA
||
2061 srcFormat
== GL_LUMINANCE
||
2062 srcFormat
== GL_LUMINANCE_ALPHA
||
2063 srcFormat
== GL_INTENSITY
||
2064 srcFormat
== GL_RG
||
2065 srcFormat
== GL_RGB
||
2066 srcFormat
== GL_BGR
||
2067 srcFormat
== GL_RGBA
||
2068 srcFormat
== GL_BGRA
||
2069 srcFormat
== GL_ABGR_EXT
||
2070 srcFormat
== GL_RED_INTEGER_EXT
||
2071 srcFormat
== GL_RG_INTEGER
||
2072 srcFormat
== GL_GREEN_INTEGER_EXT
||
2073 srcFormat
== GL_BLUE_INTEGER_EXT
||
2074 srcFormat
== GL_ALPHA_INTEGER_EXT
||
2075 srcFormat
== GL_RGB_INTEGER_EXT
||
2076 srcFormat
== GL_RGBA_INTEGER_EXT
||
2077 srcFormat
== GL_BGR_INTEGER_EXT
||
2078 srcFormat
== GL_BGRA_INTEGER_EXT
||
2079 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
2080 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
2082 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
2083 srcType
== GL_BYTE
||
2084 srcType
== GL_UNSIGNED_SHORT
||
2085 srcType
== GL_SHORT
||
2086 srcType
== GL_UNSIGNED_INT
||
2087 srcType
== GL_INT
||
2088 srcType
== GL_HALF_FLOAT_ARB
||
2089 srcType
== GL_FLOAT
||
2090 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2091 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2092 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2093 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2094 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2095 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2096 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2097 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2098 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2099 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2100 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2101 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
2102 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
2103 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
2105 get_component_mapping(srcFormat
,
2106 &rSrc
, &gSrc
, &bSrc
, &aSrc
,
2107 &rDst
, &gDst
, &bDst
, &aDst
);
2109 stride
= _mesa_components_in_format(srcFormat
);
2111 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT, TYPE, CONVERSION) \
2112 if ((SRC_INDEX) < 0) { \
2114 for (i = 0; i < n; i++) { \
2115 rgba[i][DST_INDEX] = DEFAULT; \
2118 else if (swapBytes) { \
2119 const TYPE *s = (const TYPE *) src; \
2121 for (i = 0; i < n; i++) { \
2122 TYPE value = s[SRC_INDEX]; \
2123 if (sizeof(TYPE) == 2) { \
2126 else if (sizeof(TYPE) == 4) { \
2129 rgba[i][DST_INDEX] = CONVERSION(value); \
2134 const TYPE *s = (const TYPE *) src; \
2136 for (i = 0; i < n; i++) { \
2137 rgba[i][DST_INDEX] = CONVERSION(s[SRC_INDEX]); \
2143 case GL_UNSIGNED_BYTE
:
2144 PROCESS(rSrc
, RCOMP
, 0, GLubyte
, (GLuint
));
2145 PROCESS(gSrc
, GCOMP
, 0, GLubyte
, (GLuint
));
2146 PROCESS(bSrc
, BCOMP
, 0, GLubyte
, (GLuint
));
2147 PROCESS(aSrc
, ACOMP
, 1, GLubyte
, (GLuint
));
2150 PROCESS(rSrc
, RCOMP
, 0, GLbyte
, (GLuint
));
2151 PROCESS(gSrc
, GCOMP
, 0, GLbyte
, (GLuint
));
2152 PROCESS(bSrc
, BCOMP
, 0, GLbyte
, (GLuint
));
2153 PROCESS(aSrc
, ACOMP
, 1, GLbyte
, (GLuint
));
2155 case GL_UNSIGNED_SHORT
:
2156 PROCESS(rSrc
, RCOMP
, 0, GLushort
, (GLuint
));
2157 PROCESS(gSrc
, GCOMP
, 0, GLushort
, (GLuint
));
2158 PROCESS(bSrc
, BCOMP
, 0, GLushort
, (GLuint
));
2159 PROCESS(aSrc
, ACOMP
, 1, GLushort
, (GLuint
));
2162 PROCESS(rSrc
, RCOMP
, 0, GLshort
, (GLuint
));
2163 PROCESS(gSrc
, GCOMP
, 0, GLshort
, (GLuint
));
2164 PROCESS(bSrc
, BCOMP
, 0, GLshort
, (GLuint
));
2165 PROCESS(aSrc
, ACOMP
, 1, GLshort
, (GLuint
));
2167 case GL_UNSIGNED_INT
:
2168 PROCESS(rSrc
, RCOMP
, 0, GLuint
, (GLuint
));
2169 PROCESS(gSrc
, GCOMP
, 0, GLuint
, (GLuint
));
2170 PROCESS(bSrc
, BCOMP
, 0, GLuint
, (GLuint
));
2171 PROCESS(aSrc
, ACOMP
, 1, GLuint
, (GLuint
));
2174 PROCESS(rSrc
, RCOMP
, 0, GLint
, (GLuint
));
2175 PROCESS(gSrc
, GCOMP
, 0, GLint
, (GLuint
));
2176 PROCESS(bSrc
, BCOMP
, 0, GLint
, (GLuint
));
2177 PROCESS(aSrc
, ACOMP
, 1, GLint
, (GLuint
));
2180 PROCESS(rSrc
, RCOMP
, 0, GLfloat
, clamp_float_to_uint
);
2181 PROCESS(gSrc
, GCOMP
, 0, GLfloat
, clamp_float_to_uint
);
2182 PROCESS(bSrc
, BCOMP
, 0, GLfloat
, clamp_float_to_uint
);
2183 PROCESS(aSrc
, ACOMP
, 1, GLfloat
, clamp_float_to_uint
);
2185 case GL_HALF_FLOAT_ARB
:
2186 PROCESS(rSrc
, RCOMP
, 0, GLhalfARB
, clamp_half_to_uint
);
2187 PROCESS(gSrc
, GCOMP
, 0, GLhalfARB
, clamp_half_to_uint
);
2188 PROCESS(bSrc
, BCOMP
, 0, GLhalfARB
, clamp_half_to_uint
);
2189 PROCESS(aSrc
, ACOMP
, 1, GLhalfARB
, clamp_half_to_uint
);
2191 case GL_UNSIGNED_BYTE_3_3_2
:
2193 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
2195 for (i
= 0; i
< n
; i
++) {
2196 GLubyte p
= ubsrc
[i
];
2197 rgba
[i
][rDst
] = ((p
>> 5) );
2198 rgba
[i
][gDst
] = ((p
>> 2) & 0x7);
2199 rgba
[i
][bDst
] = ((p
) & 0x3);
2204 case GL_UNSIGNED_BYTE_2_3_3_REV
:
2206 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
2208 for (i
= 0; i
< n
; i
++) {
2209 GLubyte p
= ubsrc
[i
];
2210 rgba
[i
][rDst
] = ((p
) & 0x7);
2211 rgba
[i
][gDst
] = ((p
>> 3) & 0x7);
2212 rgba
[i
][bDst
] = ((p
>> 6) );
2217 case GL_UNSIGNED_SHORT_5_6_5
:
2219 const GLushort
*ussrc
= (const GLushort
*) src
;
2221 for (i
= 0; i
< n
; i
++) {
2222 GLushort p
= ussrc
[i
];
2224 rgba
[i
][rDst
] = ((p
>> 11) );
2225 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f);
2226 rgba
[i
][bDst
] = ((p
) & 0x1f);
2231 const GLushort
*ussrc
= (const GLushort
*) src
;
2233 for (i
= 0; i
< n
; i
++) {
2234 GLushort p
= ussrc
[i
];
2235 rgba
[i
][rDst
] = ((p
>> 11) );
2236 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f);
2237 rgba
[i
][bDst
] = ((p
) & 0x1f);
2242 case GL_UNSIGNED_SHORT_5_6_5_REV
:
2244 const GLushort
*ussrc
= (const GLushort
*) src
;
2246 for (i
= 0; i
< n
; i
++) {
2247 GLushort p
= ussrc
[i
];
2249 rgba
[i
][rDst
] = ((p
) & 0x1f);
2250 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f);
2251 rgba
[i
][bDst
] = ((p
>> 11) );
2256 const GLushort
*ussrc
= (const GLushort
*) src
;
2258 for (i
= 0; i
< n
; i
++) {
2259 GLushort p
= ussrc
[i
];
2260 rgba
[i
][rDst
] = ((p
) & 0x1f);
2261 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f);
2262 rgba
[i
][bDst
] = ((p
>> 11) );
2267 case GL_UNSIGNED_SHORT_4_4_4_4
:
2269 const GLushort
*ussrc
= (const GLushort
*) src
;
2271 for (i
= 0; i
< n
; i
++) {
2272 GLushort p
= ussrc
[i
];
2274 rgba
[i
][rDst
] = ((p
>> 12) );
2275 rgba
[i
][gDst
] = ((p
>> 8) & 0xf);
2276 rgba
[i
][bDst
] = ((p
>> 4) & 0xf);
2277 rgba
[i
][aDst
] = ((p
) & 0xf);
2281 const GLushort
*ussrc
= (const GLushort
*) src
;
2283 for (i
= 0; i
< n
; i
++) {
2284 GLushort p
= ussrc
[i
];
2285 rgba
[i
][rDst
] = ((p
>> 12) );
2286 rgba
[i
][gDst
] = ((p
>> 8) & 0xf);
2287 rgba
[i
][bDst
] = ((p
>> 4) & 0xf);
2288 rgba
[i
][aDst
] = ((p
) & 0xf);
2292 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
2294 const GLushort
*ussrc
= (const GLushort
*) src
;
2296 for (i
= 0; i
< n
; i
++) {
2297 GLushort p
= ussrc
[i
];
2299 rgba
[i
][rDst
] = ((p
) & 0xf);
2300 rgba
[i
][gDst
] = ((p
>> 4) & 0xf);
2301 rgba
[i
][bDst
] = ((p
>> 8) & 0xf);
2302 rgba
[i
][aDst
] = ((p
>> 12) );
2306 const GLushort
*ussrc
= (const GLushort
*) src
;
2308 for (i
= 0; i
< n
; i
++) {
2309 GLushort p
= ussrc
[i
];
2310 rgba
[i
][rDst
] = ((p
) & 0xf);
2311 rgba
[i
][gDst
] = ((p
>> 4) & 0xf);
2312 rgba
[i
][bDst
] = ((p
>> 8) & 0xf);
2313 rgba
[i
][aDst
] = ((p
>> 12) );
2317 case GL_UNSIGNED_SHORT_5_5_5_1
:
2319 const GLushort
*ussrc
= (const GLushort
*) src
;
2321 for (i
= 0; i
< n
; i
++) {
2322 GLushort p
= ussrc
[i
];
2324 rgba
[i
][rDst
] = ((p
>> 11) );
2325 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f);
2326 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f);
2327 rgba
[i
][aDst
] = ((p
) & 0x1 );
2331 const GLushort
*ussrc
= (const GLushort
*) src
;
2333 for (i
= 0; i
< n
; i
++) {
2334 GLushort p
= ussrc
[i
];
2335 rgba
[i
][rDst
] = ((p
>> 11) );
2336 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f);
2337 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f);
2338 rgba
[i
][aDst
] = ((p
) & 0x1 );
2342 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
2344 const GLushort
*ussrc
= (const GLushort
*) src
;
2346 for (i
= 0; i
< n
; i
++) {
2347 GLushort p
= ussrc
[i
];
2349 rgba
[i
][rDst
] = ((p
) & 0x1f);
2350 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f);
2351 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f);
2352 rgba
[i
][aDst
] = ((p
>> 15) );
2356 const GLushort
*ussrc
= (const GLushort
*) src
;
2358 for (i
= 0; i
< n
; i
++) {
2359 GLushort p
= ussrc
[i
];
2360 rgba
[i
][rDst
] = ((p
) & 0x1f);
2361 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f);
2362 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f);
2363 rgba
[i
][aDst
] = ((p
>> 15) );
2367 case GL_UNSIGNED_INT_8_8_8_8
:
2369 const GLuint
*uisrc
= (const GLuint
*) src
;
2371 for (i
= 0; i
< n
; i
++) {
2372 GLuint p
= uisrc
[i
];
2373 rgba
[i
][rDst
] = ((p
) & 0xff);
2374 rgba
[i
][gDst
] = ((p
>> 8) & 0xff);
2375 rgba
[i
][bDst
] = ((p
>> 16) & 0xff);
2376 rgba
[i
][aDst
] = ((p
>> 24) );
2380 const GLuint
*uisrc
= (const GLuint
*) src
;
2382 for (i
= 0; i
< n
; i
++) {
2383 GLuint p
= uisrc
[i
];
2384 rgba
[i
][rDst
] = ((p
>> 24) );
2385 rgba
[i
][gDst
] = ((p
>> 16) & 0xff);
2386 rgba
[i
][bDst
] = ((p
>> 8) & 0xff);
2387 rgba
[i
][aDst
] = ((p
) & 0xff);
2391 case GL_UNSIGNED_INT_8_8_8_8_REV
:
2393 const GLuint
*uisrc
= (const GLuint
*) src
;
2395 for (i
= 0; i
< n
; i
++) {
2396 GLuint p
= uisrc
[i
];
2397 rgba
[i
][rDst
] = ((p
>> 24) );
2398 rgba
[i
][gDst
] = ((p
>> 16) & 0xff);
2399 rgba
[i
][bDst
] = ((p
>> 8) & 0xff);
2400 rgba
[i
][aDst
] = ((p
) & 0xff);
2404 const GLuint
*uisrc
= (const GLuint
*) src
;
2406 for (i
= 0; i
< n
; i
++) {
2407 GLuint p
= uisrc
[i
];
2408 rgba
[i
][rDst
] = ((p
) & 0xff);
2409 rgba
[i
][gDst
] = ((p
>> 8) & 0xff);
2410 rgba
[i
][bDst
] = ((p
>> 16) & 0xff);
2411 rgba
[i
][aDst
] = ((p
>> 24) );
2415 case GL_UNSIGNED_INT_10_10_10_2
:
2417 const GLuint
*uisrc
= (const GLuint
*) src
;
2419 for (i
= 0; i
< n
; i
++) {
2420 GLuint p
= uisrc
[i
];
2422 rgba
[i
][rDst
] = ((p
>> 22) );
2423 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff);
2424 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff);
2425 rgba
[i
][aDst
] = ((p
) & 0x3 );
2429 const GLuint
*uisrc
= (const GLuint
*) src
;
2431 for (i
= 0; i
< n
; i
++) {
2432 GLuint p
= uisrc
[i
];
2433 rgba
[i
][rDst
] = ((p
>> 22) );
2434 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff);
2435 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff);
2436 rgba
[i
][aDst
] = ((p
) & 0x3 );
2440 case GL_UNSIGNED_INT_2_10_10_10_REV
:
2442 const GLuint
*uisrc
= (const GLuint
*) src
;
2444 for (i
= 0; i
< n
; i
++) {
2445 GLuint p
= uisrc
[i
];
2447 rgba
[i
][rDst
] = ((p
) & 0x3ff);
2448 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff);
2449 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff);
2450 rgba
[i
][aDst
] = ((p
>> 30) );
2454 const GLuint
*uisrc
= (const GLuint
*) src
;
2456 for (i
= 0; i
< n
; i
++) {
2457 GLuint p
= uisrc
[i
];
2458 rgba
[i
][rDst
] = ((p
) & 0x3ff);
2459 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff);
2460 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff);
2461 rgba
[i
][aDst
] = ((p
>> 30) );
2465 case GL_UNSIGNED_INT_5_9_9_9_REV
:
2467 const GLuint
*uisrc
= (const GLuint
*) src
;
2470 for (i
= 0; i
< n
; i
++) {
2471 GLuint p
= uisrc
[i
];
2473 rgb9e5_to_float3(p
, f
);
2474 rgba
[i
][rDst
] = clamp_float_to_uint(f
[0]);
2475 rgba
[i
][gDst
] = clamp_float_to_uint(f
[1]);
2476 rgba
[i
][bDst
] = clamp_float_to_uint(f
[2]);
2481 const GLuint
*uisrc
= (const GLuint
*) src
;
2484 for (i
= 0; i
< n
; i
++) {
2485 GLuint p
= uisrc
[i
];
2486 rgb9e5_to_float3(p
, f
);
2487 rgba
[i
][rDst
] = clamp_float_to_uint(f
[0]);
2488 rgba
[i
][gDst
] = clamp_float_to_uint(f
[1]);
2489 rgba
[i
][bDst
] = clamp_float_to_uint(f
[2]);
2494 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
2496 const GLuint
*uisrc
= (const GLuint
*) src
;
2499 for (i
= 0; i
< n
; i
++) {
2500 GLuint p
= uisrc
[i
];
2502 r11g11b10f_to_float3(p
, f
);
2503 rgba
[i
][rDst
] = clamp_float_to_uint(f
[0]);
2504 rgba
[i
][gDst
] = clamp_float_to_uint(f
[1]);
2505 rgba
[i
][bDst
] = clamp_float_to_uint(f
[2]);
2510 const GLuint
*uisrc
= (const GLuint
*) src
;
2513 for (i
= 0; i
< n
; i
++) {
2514 GLuint p
= uisrc
[i
];
2515 r11g11b10f_to_float3(p
, f
);
2516 rgba
[i
][rDst
] = clamp_float_to_uint(f
[0]);
2517 rgba
[i
][gDst
] = clamp_float_to_uint(f
[1]);
2518 rgba
[i
][bDst
] = clamp_float_to_uint(f
[2]);
2524 _mesa_problem(NULL
, "bad srcType in extract uint data");
2533 * Unpack a row of color image data from a client buffer according to
2534 * the pixel unpacking parameters.
2535 * Return GLubyte values in the specified dest image format.
2536 * This is used by glDrawPixels and glTexImage?D().
2537 * \param ctx - the context
2538 * n - number of pixels in the span
2539 * dstFormat - format of destination color array
2540 * dest - the destination color array
2541 * srcFormat - source image format
2542 * srcType - source image data type
2543 * source - source image pointer
2544 * srcPacking - pixel unpacking parameters
2545 * transferOps - bitmask of IMAGE_*_BIT values of operations to apply
2547 * XXX perhaps expand this to process whole images someday.
2550 _mesa_unpack_color_span_ubyte(struct gl_context
*ctx
,
2551 GLuint n
, GLenum dstFormat
, GLubyte dest
[],
2552 GLenum srcFormat
, GLenum srcType
,
2553 const GLvoid
*source
,
2554 const struct gl_pixelstore_attrib
*srcPacking
,
2555 GLbitfield transferOps
)
2557 GLboolean intFormat
= _mesa_is_enum_format_integer(srcFormat
);
2558 ASSERT(dstFormat
== GL_ALPHA
||
2559 dstFormat
== GL_LUMINANCE
||
2560 dstFormat
== GL_LUMINANCE_ALPHA
||
2561 dstFormat
== GL_INTENSITY
||
2562 dstFormat
== GL_RED
||
2563 dstFormat
== GL_RG
||
2564 dstFormat
== GL_RGB
||
2565 dstFormat
== GL_RGBA
);
2567 ASSERT(srcFormat
== GL_RED
||
2568 srcFormat
== GL_GREEN
||
2569 srcFormat
== GL_BLUE
||
2570 srcFormat
== GL_ALPHA
||
2571 srcFormat
== GL_LUMINANCE
||
2572 srcFormat
== GL_LUMINANCE_ALPHA
||
2573 srcFormat
== GL_INTENSITY
||
2574 srcFormat
== GL_RG
||
2575 srcFormat
== GL_RGB
||
2576 srcFormat
== GL_BGR
||
2577 srcFormat
== GL_RGBA
||
2578 srcFormat
== GL_BGRA
||
2579 srcFormat
== GL_ABGR_EXT
||
2580 srcFormat
== GL_COLOR_INDEX
);
2582 ASSERT(srcType
== GL_BITMAP
||
2583 srcType
== GL_UNSIGNED_BYTE
||
2584 srcType
== GL_BYTE
||
2585 srcType
== GL_UNSIGNED_SHORT
||
2586 srcType
== GL_SHORT
||
2587 srcType
== GL_UNSIGNED_INT
||
2588 srcType
== GL_INT
||
2589 srcType
== GL_HALF_FLOAT_ARB
||
2590 srcType
== GL_FLOAT
||
2591 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2592 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2593 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2594 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2595 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2596 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2597 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2598 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2599 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2600 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2601 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2602 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
2603 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
2604 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
2606 /* EXT_texture_integer specifies no transfer ops on integer
2607 * types in the resolved issues section. Just set them to 0
2608 * for integer surfaces.
2613 /* Try simple cases first */
2614 if (transferOps
== 0) {
2615 if (srcType
== GL_UNSIGNED_BYTE
) {
2616 if (dstFormat
== GL_RGBA
) {
2617 if (srcFormat
== GL_RGBA
) {
2618 memcpy( dest
, source
, n
* 4 * sizeof(GLubyte
) );
2621 else if (srcFormat
== GL_RGB
) {
2623 const GLubyte
*src
= (const GLubyte
*) source
;
2624 GLubyte
*dst
= dest
;
2625 for (i
= 0; i
< n
; i
++) {
2636 else if (dstFormat
== GL_RGB
) {
2637 if (srcFormat
== GL_RGB
) {
2638 memcpy( dest
, source
, n
* 3 * sizeof(GLubyte
) );
2641 else if (srcFormat
== GL_RGBA
) {
2643 const GLubyte
*src
= (const GLubyte
*) source
;
2644 GLubyte
*dst
= dest
;
2645 for (i
= 0; i
< n
; i
++) {
2655 else if (dstFormat
== srcFormat
) {
2656 GLint comps
= _mesa_components_in_format(srcFormat
);
2658 memcpy( dest
, source
, n
* comps
* sizeof(GLubyte
) );
2665 /* general solution begins here */
2667 GLint dstComponents
;
2668 GLint rDst
, gDst
, bDst
, aDst
, lDst
, iDst
;
2669 GLfloat (*rgba
)[4] = malloc(4 * n
* sizeof(GLfloat
));
2672 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2676 dstComponents
= _mesa_components_in_format( dstFormat
);
2677 /* source & dest image formats should have been error checked by now */
2678 assert(dstComponents
> 0);
2681 * Extract image data and convert to RGBA floats
2683 if (srcFormat
== GL_COLOR_INDEX
) {
2684 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
2687 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2692 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
2695 /* Convert indexes to RGBA */
2696 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
2697 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
2699 _mesa_map_ci_to_rgba(ctx
, n
, indexes
, rgba
);
2701 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
2702 * with color indexes.
2704 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
2709 /* non-color index data */
2710 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
2711 srcPacking
->SwapBytes
);
2714 /* Need to clamp if returning GLubytes */
2715 transferOps
|= IMAGE_CLAMP_BIT
;
2718 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
2721 get_component_indexes(dstFormat
,
2722 &rDst
, &gDst
, &bDst
, &aDst
, &lDst
, &iDst
);
2724 /* Now return the GLubyte data in the requested dstFormat */
2726 GLubyte
*dst
= dest
;
2728 for (i
= 0; i
< n
; i
++) {
2729 CLAMPED_FLOAT_TO_UBYTE(dst
[rDst
], rgba
[i
][RCOMP
]);
2730 dst
+= dstComponents
;
2735 GLubyte
*dst
= dest
;
2737 for (i
= 0; i
< n
; i
++) {
2738 CLAMPED_FLOAT_TO_UBYTE(dst
[gDst
], rgba
[i
][GCOMP
]);
2739 dst
+= dstComponents
;
2744 GLubyte
*dst
= dest
;
2746 for (i
= 0; i
< n
; i
++) {
2747 CLAMPED_FLOAT_TO_UBYTE(dst
[bDst
], rgba
[i
][BCOMP
]);
2748 dst
+= dstComponents
;
2753 GLubyte
*dst
= dest
;
2755 for (i
= 0; i
< n
; i
++) {
2756 CLAMPED_FLOAT_TO_UBYTE(dst
[aDst
], rgba
[i
][ACOMP
]);
2757 dst
+= dstComponents
;
2762 GLubyte
*dst
= dest
;
2765 assert(dstComponents
== 1);
2766 for (i
= 0; i
< n
; i
++) {
2767 /* Intensity comes from red channel */
2768 CLAMPED_FLOAT_TO_UBYTE(dst
[i
], rgba
[i
][RCOMP
]);
2773 GLubyte
*dst
= dest
;
2776 for (i
= 0; i
< n
; i
++) {
2777 /* Luminance comes from red channel */
2778 CLAMPED_FLOAT_TO_UBYTE(dst
[0], rgba
[i
][RCOMP
]);
2779 dst
+= dstComponents
;
2789 * Same as _mesa_unpack_color_span_ubyte(), but return GLfloat data
2790 * instead of GLubyte.
2793 _mesa_unpack_color_span_float( struct gl_context
*ctx
,
2794 GLuint n
, GLenum dstFormat
, GLfloat dest
[],
2795 GLenum srcFormat
, GLenum srcType
,
2796 const GLvoid
*source
,
2797 const struct gl_pixelstore_attrib
*srcPacking
,
2798 GLbitfield transferOps
)
2800 ASSERT(dstFormat
== GL_ALPHA
||
2801 dstFormat
== GL_LUMINANCE
||
2802 dstFormat
== GL_LUMINANCE_ALPHA
||
2803 dstFormat
== GL_INTENSITY
||
2804 dstFormat
== GL_RED
||
2805 dstFormat
== GL_RG
||
2806 dstFormat
== GL_RGB
||
2807 dstFormat
== GL_RGBA
);
2809 ASSERT(srcFormat
== GL_RED
||
2810 srcFormat
== GL_GREEN
||
2811 srcFormat
== GL_BLUE
||
2812 srcFormat
== GL_ALPHA
||
2813 srcFormat
== GL_LUMINANCE
||
2814 srcFormat
== GL_LUMINANCE_ALPHA
||
2815 srcFormat
== GL_INTENSITY
||
2816 srcFormat
== GL_RG
||
2817 srcFormat
== GL_RGB
||
2818 srcFormat
== GL_BGR
||
2819 srcFormat
== GL_RGBA
||
2820 srcFormat
== GL_BGRA
||
2821 srcFormat
== GL_ABGR_EXT
||
2822 srcFormat
== GL_RED_INTEGER_EXT
||
2823 srcFormat
== GL_GREEN_INTEGER_EXT
||
2824 srcFormat
== GL_BLUE_INTEGER_EXT
||
2825 srcFormat
== GL_ALPHA_INTEGER_EXT
||
2826 srcFormat
== GL_RG_INTEGER
||
2827 srcFormat
== GL_RGB_INTEGER_EXT
||
2828 srcFormat
== GL_RGBA_INTEGER_EXT
||
2829 srcFormat
== GL_BGR_INTEGER_EXT
||
2830 srcFormat
== GL_BGRA_INTEGER_EXT
||
2831 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
2832 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
||
2833 srcFormat
== GL_COLOR_INDEX
);
2835 ASSERT(srcType
== GL_BITMAP
||
2836 srcType
== GL_UNSIGNED_BYTE
||
2837 srcType
== GL_BYTE
||
2838 srcType
== GL_UNSIGNED_SHORT
||
2839 srcType
== GL_SHORT
||
2840 srcType
== GL_UNSIGNED_INT
||
2841 srcType
== GL_INT
||
2842 srcType
== GL_HALF_FLOAT_ARB
||
2843 srcType
== GL_FLOAT
||
2844 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2845 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2846 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2847 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2848 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2849 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2850 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2851 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2852 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2853 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2854 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2855 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
2856 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
2857 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
2859 /* general solution, no special cases, yet */
2861 GLint dstComponents
;
2862 GLint rDst
, gDst
, bDst
, aDst
, lDst
, iDst
;
2863 GLfloat (*rgba
)[4] = malloc(4 * n
* sizeof(GLfloat
));
2864 GLboolean intFormat
= _mesa_is_enum_format_integer(srcFormat
);
2867 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2871 dstComponents
= _mesa_components_in_format( dstFormat
);
2872 /* source & dest image formats should have been error checked by now */
2873 assert(dstComponents
> 0);
2875 /* EXT_texture_integer specifies no transfer ops on integer
2876 * types in the resolved issues section. Just set them to 0
2877 * for integer surfaces.
2883 * Extract image data and convert to RGBA floats
2885 if (srcFormat
== GL_COLOR_INDEX
) {
2886 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
2889 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2894 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
2897 /* Convert indexes to RGBA */
2898 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
2899 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
2901 _mesa_map_ci_to_rgba(ctx
, n
, indexes
, rgba
);
2903 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
2904 * with color indexes.
2906 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
2911 /* non-color index data */
2912 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
2913 srcPacking
->SwapBytes
);
2917 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
2920 get_component_indexes(dstFormat
,
2921 &rDst
, &gDst
, &bDst
, &aDst
, &lDst
, &iDst
);
2923 /* Now pack results in the requested dstFormat */
2925 GLfloat
*dst
= dest
;
2927 for (i
= 0; i
< n
; i
++) {
2928 dst
[rDst
] = rgba
[i
][RCOMP
];
2929 dst
+= dstComponents
;
2934 GLfloat
*dst
= dest
;
2936 for (i
= 0; i
< n
; i
++) {
2937 dst
[gDst
] = rgba
[i
][GCOMP
];
2938 dst
+= dstComponents
;
2943 GLfloat
*dst
= dest
;
2945 for (i
= 0; i
< n
; i
++) {
2946 dst
[bDst
] = rgba
[i
][BCOMP
];
2947 dst
+= dstComponents
;
2952 GLfloat
*dst
= dest
;
2954 for (i
= 0; i
< n
; i
++) {
2955 dst
[aDst
] = rgba
[i
][ACOMP
];
2956 dst
+= dstComponents
;
2961 GLfloat
*dst
= dest
;
2964 assert(dstComponents
== 1);
2965 for (i
= 0; i
< n
; i
++) {
2966 /* Intensity comes from red channel */
2967 dst
[i
] = rgba
[i
][RCOMP
];
2972 GLfloat
*dst
= dest
;
2975 for (i
= 0; i
< n
; i
++) {
2976 /* Luminance comes from red channel */
2977 dst
[0] = rgba
[i
][RCOMP
];
2978 dst
+= dstComponents
;
2988 * Same as _mesa_unpack_color_span_ubyte(), but return GLuint data
2989 * instead of GLubyte.
2990 * No pixel transfer ops are applied.
2993 _mesa_unpack_color_span_uint(struct gl_context
*ctx
,
2994 GLuint n
, GLenum dstFormat
, GLuint
*dest
,
2995 GLenum srcFormat
, GLenum srcType
,
2996 const GLvoid
*source
,
2997 const struct gl_pixelstore_attrib
*srcPacking
)
2999 GLuint (*rgba
)[4] = malloc(n
* 4 * sizeof(GLfloat
));
3002 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3006 ASSERT(dstFormat
== GL_ALPHA
||
3007 dstFormat
== GL_LUMINANCE
||
3008 dstFormat
== GL_LUMINANCE_ALPHA
||
3009 dstFormat
== GL_INTENSITY
||
3010 dstFormat
== GL_RED
||
3011 dstFormat
== GL_RG
||
3012 dstFormat
== GL_RGB
||
3013 dstFormat
== GL_RGBA
);
3015 ASSERT(srcFormat
== GL_RED
||
3016 srcFormat
== GL_GREEN
||
3017 srcFormat
== GL_BLUE
||
3018 srcFormat
== GL_ALPHA
||
3019 srcFormat
== GL_LUMINANCE
||
3020 srcFormat
== GL_LUMINANCE_ALPHA
||
3021 srcFormat
== GL_INTENSITY
||
3022 srcFormat
== GL_RG
||
3023 srcFormat
== GL_RGB
||
3024 srcFormat
== GL_BGR
||
3025 srcFormat
== GL_RGBA
||
3026 srcFormat
== GL_BGRA
||
3027 srcFormat
== GL_ABGR_EXT
||
3028 srcFormat
== GL_RED_INTEGER_EXT
||
3029 srcFormat
== GL_GREEN_INTEGER_EXT
||
3030 srcFormat
== GL_BLUE_INTEGER_EXT
||
3031 srcFormat
== GL_ALPHA_INTEGER_EXT
||
3032 srcFormat
== GL_RG_INTEGER
||
3033 srcFormat
== GL_RGB_INTEGER_EXT
||
3034 srcFormat
== GL_RGBA_INTEGER_EXT
||
3035 srcFormat
== GL_BGR_INTEGER_EXT
||
3036 srcFormat
== GL_BGRA_INTEGER_EXT
||
3037 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
3038 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
3040 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
3041 srcType
== GL_BYTE
||
3042 srcType
== GL_UNSIGNED_SHORT
||
3043 srcType
== GL_SHORT
||
3044 srcType
== GL_UNSIGNED_INT
||
3045 srcType
== GL_INT
||
3046 srcType
== GL_HALF_FLOAT_ARB
||
3047 srcType
== GL_FLOAT
||
3048 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
3049 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
3050 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
3051 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
3052 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
3053 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
3054 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
3055 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
3056 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
3057 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
3058 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
3059 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
3060 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
3061 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
3064 /* Extract image data as uint[4] pixels */
3065 extract_uint_rgba(n
, rgba
, srcFormat
, srcType
, source
,
3066 srcPacking
->SwapBytes
);
3068 if (dstFormat
== GL_RGBA
) {
3070 memcpy(dest
, rgba
, 4 * sizeof(GLuint
) * n
);
3074 GLint rDst
, gDst
, bDst
, aDst
, lDst
, iDst
;
3075 GLint dstComponents
= _mesa_components_in_format( dstFormat
);
3077 assert(dstComponents
> 0);
3079 get_component_indexes(dstFormat
,
3080 &rDst
, &gDst
, &bDst
, &aDst
, &lDst
, &iDst
);
3082 /* Now pack values in the requested dest format */
3086 for (i
= 0; i
< n
; i
++) {
3087 dst
[rDst
] = rgba
[i
][RCOMP
];
3088 dst
+= dstComponents
;
3095 for (i
= 0; i
< n
; i
++) {
3096 dst
[gDst
] = rgba
[i
][GCOMP
];
3097 dst
+= dstComponents
;
3104 for (i
= 0; i
< n
; i
++) {
3105 dst
[bDst
] = rgba
[i
][BCOMP
];
3106 dst
+= dstComponents
;
3113 for (i
= 0; i
< n
; i
++) {
3114 dst
[aDst
] = rgba
[i
][ACOMP
];
3115 dst
+= dstComponents
;
3123 assert(dstComponents
== 1);
3124 for (i
= 0; i
< n
; i
++) {
3125 /* Intensity comes from red channel */
3126 dst
[i
] = rgba
[i
][RCOMP
];
3134 for (i
= 0; i
< n
; i
++) {
3135 /* Luminance comes from red channel */
3136 dst
[0] = rgba
[i
][RCOMP
];
3137 dst
+= dstComponents
;
3147 * Unpack a row of color index data from a client buffer according to
3148 * the pixel unpacking parameters.
3149 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3151 * Args: ctx - the context
3152 * n - number of pixels
3153 * dstType - destination data type
3154 * dest - destination array
3155 * srcType - source pixel type
3156 * source - source data pointer
3157 * srcPacking - pixel unpacking parameters
3158 * transferOps - the pixel transfer operations to apply
3161 _mesa_unpack_index_span( struct gl_context
*ctx
, GLuint n
,
3162 GLenum dstType
, GLvoid
*dest
,
3163 GLenum srcType
, const GLvoid
*source
,
3164 const struct gl_pixelstore_attrib
*srcPacking
,
3165 GLbitfield transferOps
)
3167 ASSERT(srcType
== GL_BITMAP
||
3168 srcType
== GL_UNSIGNED_BYTE
||
3169 srcType
== GL_BYTE
||
3170 srcType
== GL_UNSIGNED_SHORT
||
3171 srcType
== GL_SHORT
||
3172 srcType
== GL_UNSIGNED_INT
||
3173 srcType
== GL_INT
||
3174 srcType
== GL_HALF_FLOAT_ARB
||
3175 srcType
== GL_FLOAT
);
3177 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
3178 dstType
== GL_UNSIGNED_SHORT
||
3179 dstType
== GL_UNSIGNED_INT
);
3182 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
3185 * Try simple cases first
3187 if (transferOps
== 0 && srcType
== GL_UNSIGNED_BYTE
3188 && dstType
== GL_UNSIGNED_BYTE
) {
3189 memcpy(dest
, source
, n
* sizeof(GLubyte
));
3191 else if (transferOps
== 0 && srcType
== GL_UNSIGNED_INT
3192 && dstType
== GL_UNSIGNED_INT
&& !srcPacking
->SwapBytes
) {
3193 memcpy(dest
, source
, n
* sizeof(GLuint
));
3199 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
3202 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3206 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
3210 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
3212 /* convert to dest type */
3214 case GL_UNSIGNED_BYTE
:
3216 GLubyte
*dst
= (GLubyte
*) dest
;
3218 for (i
= 0; i
< n
; i
++) {
3219 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
3223 case GL_UNSIGNED_SHORT
:
3225 GLuint
*dst
= (GLuint
*) dest
;
3227 for (i
= 0; i
< n
; i
++) {
3228 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
3232 case GL_UNSIGNED_INT
:
3233 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
3236 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
3245 _mesa_pack_index_span( struct gl_context
*ctx
, GLuint n
,
3246 GLenum dstType
, GLvoid
*dest
, const GLuint
*source
,
3247 const struct gl_pixelstore_attrib
*dstPacking
,
3248 GLbitfield transferOps
)
3250 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
3253 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
3257 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
3259 if (transferOps
& (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
)) {
3260 /* make a copy of input */
3261 memcpy(indexes
, source
, n
* sizeof(GLuint
));
3262 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
3267 case GL_UNSIGNED_BYTE
:
3269 GLubyte
*dst
= (GLubyte
*) dest
;
3271 for (i
= 0; i
< n
; i
++) {
3272 *dst
++ = (GLubyte
) source
[i
];
3278 GLbyte
*dst
= (GLbyte
*) dest
;
3280 for (i
= 0; i
< n
; i
++) {
3281 dst
[i
] = (GLbyte
) source
[i
];
3285 case GL_UNSIGNED_SHORT
:
3287 GLushort
*dst
= (GLushort
*) dest
;
3289 for (i
= 0; i
< n
; i
++) {
3290 dst
[i
] = (GLushort
) source
[i
];
3292 if (dstPacking
->SwapBytes
) {
3293 _mesa_swap2( (GLushort
*) dst
, n
);
3299 GLshort
*dst
= (GLshort
*) dest
;
3301 for (i
= 0; i
< n
; i
++) {
3302 dst
[i
] = (GLshort
) source
[i
];
3304 if (dstPacking
->SwapBytes
) {
3305 _mesa_swap2( (GLushort
*) dst
, n
);
3309 case GL_UNSIGNED_INT
:
3311 GLuint
*dst
= (GLuint
*) dest
;
3313 for (i
= 0; i
< n
; i
++) {
3314 dst
[i
] = (GLuint
) source
[i
];
3316 if (dstPacking
->SwapBytes
) {
3317 _mesa_swap4( (GLuint
*) dst
, n
);
3323 GLint
*dst
= (GLint
*) dest
;
3325 for (i
= 0; i
< n
; i
++) {
3326 dst
[i
] = (GLint
) source
[i
];
3328 if (dstPacking
->SwapBytes
) {
3329 _mesa_swap4( (GLuint
*) dst
, n
);
3335 GLfloat
*dst
= (GLfloat
*) dest
;
3337 for (i
= 0; i
< n
; i
++) {
3338 dst
[i
] = (GLfloat
) source
[i
];
3340 if (dstPacking
->SwapBytes
) {
3341 _mesa_swap4( (GLuint
*) dst
, n
);
3345 case GL_HALF_FLOAT_ARB
:
3347 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
3349 for (i
= 0; i
< n
; i
++) {
3350 dst
[i
] = _mesa_float_to_half((GLfloat
) source
[i
]);
3352 if (dstPacking
->SwapBytes
) {
3353 _mesa_swap2( (GLushort
*) dst
, n
);
3358 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
3366 * Unpack a row of stencil data from a client buffer according to
3367 * the pixel unpacking parameters.
3368 * This is (or will be) used by glDrawPixels
3370 * Args: ctx - the context
3371 * n - number of pixels
3372 * dstType - destination data type
3373 * dest - destination array
3374 * srcType - source pixel type
3375 * source - source data pointer
3376 * srcPacking - pixel unpacking parameters
3377 * transferOps - apply offset/bias/lookup ops?
3380 _mesa_unpack_stencil_span( struct gl_context
*ctx
, GLuint n
,
3381 GLenum dstType
, GLvoid
*dest
,
3382 GLenum srcType
, const GLvoid
*source
,
3383 const struct gl_pixelstore_attrib
*srcPacking
,
3384 GLbitfield transferOps
)
3386 ASSERT(srcType
== GL_BITMAP
||
3387 srcType
== GL_UNSIGNED_BYTE
||
3388 srcType
== GL_BYTE
||
3389 srcType
== GL_UNSIGNED_SHORT
||
3390 srcType
== GL_SHORT
||
3391 srcType
== GL_UNSIGNED_INT
||
3392 srcType
== GL_INT
||
3393 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
3394 srcType
== GL_HALF_FLOAT_ARB
||
3395 srcType
== GL_FLOAT
||
3396 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
3398 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
3399 dstType
== GL_UNSIGNED_SHORT
||
3400 dstType
== GL_UNSIGNED_INT
||
3401 dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
3403 /* only shift and offset apply to stencil */
3404 transferOps
&= IMAGE_SHIFT_OFFSET_BIT
;
3407 * Try simple cases first
3409 if (transferOps
== 0 &&
3410 !ctx
->Pixel
.MapStencilFlag
&&
3411 srcType
== GL_UNSIGNED_BYTE
&&
3412 dstType
== GL_UNSIGNED_BYTE
) {
3413 memcpy(dest
, source
, n
* sizeof(GLubyte
));
3415 else if (transferOps
== 0 &&
3416 !ctx
->Pixel
.MapStencilFlag
&&
3417 srcType
== GL_UNSIGNED_INT
&&
3418 dstType
== GL_UNSIGNED_INT
&&
3419 !srcPacking
->SwapBytes
) {
3420 memcpy(dest
, source
, n
* sizeof(GLuint
));
3426 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
3429 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil unpacking");
3433 extract_uint_indexes(n
, indexes
, GL_STENCIL_INDEX
, srcType
, source
,
3436 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
3437 /* shift and offset indexes */
3438 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
3441 if (ctx
->Pixel
.MapStencilFlag
) {
3442 /* Apply stencil lookup table */
3443 const GLuint mask
= ctx
->PixelMaps
.StoS
.Size
- 1;
3445 for (i
= 0; i
< n
; i
++) {
3446 indexes
[i
] = (GLuint
)ctx
->PixelMaps
.StoS
.Map
[ indexes
[i
] & mask
];
3450 /* convert to dest type */
3452 case GL_UNSIGNED_BYTE
:
3454 GLubyte
*dst
= (GLubyte
*) dest
;
3456 for (i
= 0; i
< n
; i
++) {
3457 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
3461 case GL_UNSIGNED_SHORT
:
3463 GLuint
*dst
= (GLuint
*) dest
;
3465 for (i
= 0; i
< n
; i
++) {
3466 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
3470 case GL_UNSIGNED_INT
:
3471 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
3473 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
3475 GLuint
*dst
= (GLuint
*) dest
;
3477 for (i
= 0; i
< n
; i
++) {
3478 dst
[i
*2+1] = indexes
[i
] & 0xff; /* lower 8 bits */
3483 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
3492 _mesa_pack_stencil_span( struct gl_context
*ctx
, GLuint n
,
3493 GLenum dstType
, GLvoid
*dest
, const GLubyte
*source
,
3494 const struct gl_pixelstore_attrib
*dstPacking
)
3496 GLubyte
*stencil
= malloc(n
* sizeof(GLubyte
));
3499 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil packing");
3503 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
3504 ctx
->Pixel
.MapStencilFlag
) {
3505 /* make a copy of input */
3506 memcpy(stencil
, source
, n
* sizeof(GLubyte
));
3507 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencil
);
3512 case GL_UNSIGNED_BYTE
:
3513 memcpy(dest
, source
, n
);
3517 GLbyte
*dst
= (GLbyte
*) dest
;
3520 dst
[i
] = (GLbyte
) (source
[i
] & 0x7f);
3524 case GL_UNSIGNED_SHORT
:
3526 GLushort
*dst
= (GLushort
*) dest
;
3529 dst
[i
] = (GLushort
) source
[i
];
3531 if (dstPacking
->SwapBytes
) {
3532 _mesa_swap2( (GLushort
*) dst
, n
);
3538 GLshort
*dst
= (GLshort
*) dest
;
3541 dst
[i
] = (GLshort
) source
[i
];
3543 if (dstPacking
->SwapBytes
) {
3544 _mesa_swap2( (GLushort
*) dst
, n
);
3548 case GL_UNSIGNED_INT
:
3550 GLuint
*dst
= (GLuint
*) dest
;
3553 dst
[i
] = (GLuint
) source
[i
];
3555 if (dstPacking
->SwapBytes
) {
3556 _mesa_swap4( (GLuint
*) dst
, n
);
3562 GLint
*dst
= (GLint
*) dest
;
3565 dst
[i
] = (GLint
) source
[i
];
3567 if (dstPacking
->SwapBytes
) {
3568 _mesa_swap4( (GLuint
*) dst
, n
);
3574 GLfloat
*dst
= (GLfloat
*) dest
;
3577 dst
[i
] = (GLfloat
) source
[i
];
3579 if (dstPacking
->SwapBytes
) {
3580 _mesa_swap4( (GLuint
*) dst
, n
);
3584 case GL_HALF_FLOAT_ARB
:
3586 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
3589 dst
[i
] = _mesa_float_to_half( (float) source
[i
] );
3591 if (dstPacking
->SwapBytes
) {
3592 _mesa_swap2( (GLushort
*) dst
, n
);
3597 if (dstPacking
->LsbFirst
) {
3598 GLubyte
*dst
= (GLubyte
*) dest
;
3601 for (i
= 0; i
< n
; i
++) {
3604 *dst
|= ((source
[i
] != 0) << shift
);
3613 GLubyte
*dst
= (GLubyte
*) dest
;
3616 for (i
= 0; i
< n
; i
++) {
3619 *dst
|= ((source
[i
] != 0) << shift
);
3629 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
3635 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
3638 const GLTYPE *src = (const GLTYPE *)source; \
3639 for (i = 0; i < n; i++) { \
3640 GLTYPE value = src[i]; \
3641 if (srcPacking->SwapBytes) { \
3642 if (sizeof(GLTYPE) == 2) { \
3644 } else if (sizeof(GLTYPE) == 4) { \
3648 depthValues[i] = GLTYPE2FLOAT(value); \
3654 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
3655 * or GLfloat values.
3656 * The glPixelTransfer (scale/bias) params will be applied.
3658 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
3659 * \param depthMax max value for returned GLushort or GLuint values
3660 * (ignored for GLfloat).
3663 _mesa_unpack_depth_span( struct gl_context
*ctx
, GLuint n
,
3664 GLenum dstType
, GLvoid
*dest
, GLuint depthMax
,
3665 GLenum srcType
, const GLvoid
*source
,
3666 const struct gl_pixelstore_attrib
*srcPacking
)
3668 GLfloat
*depthTemp
= NULL
, *depthValues
;
3669 GLboolean needClamp
= GL_FALSE
;
3671 /* Look for special cases first.
3672 * Not only are these faster, they're less prone to numeric conversion
3673 * problems. Otherwise, converting from an int type to a float then
3674 * back to an int type can introduce errors that will show up as
3675 * artifacts in things like depth peeling which uses glCopyTexImage.
3677 if (ctx
->Pixel
.DepthScale
== 1.0 && ctx
->Pixel
.DepthBias
== 0.0) {
3678 if (srcType
== GL_UNSIGNED_INT
&& dstType
== GL_UNSIGNED_SHORT
) {
3679 const GLuint
*src
= (const GLuint
*) source
;
3680 GLushort
*dst
= (GLushort
*) dest
;
3682 for (i
= 0; i
< n
; i
++) {
3683 dst
[i
] = src
[i
] >> 16;
3687 if (srcType
== GL_UNSIGNED_SHORT
3688 && dstType
== GL_UNSIGNED_INT
3689 && depthMax
== 0xffffffff) {
3690 const GLushort
*src
= (const GLushort
*) source
;
3691 GLuint
*dst
= (GLuint
*) dest
;
3693 for (i
= 0; i
< n
; i
++) {
3694 dst
[i
] = src
[i
] | (src
[i
] << 16);
3698 if (srcType
== GL_UNSIGNED_INT_24_8
3699 && dstType
== GL_UNSIGNED_INT
3700 && depthMax
== 0xffffff) {
3701 const GLuint
*src
= (const GLuint
*) source
;
3702 GLuint
*dst
= (GLuint
*) dest
;
3704 for (i
= 0; i
< n
; i
++) {
3705 dst
[i
] = src
[i
] >> 8;
3709 /* XXX may want to add additional cases here someday */
3712 /* general case path follows */
3714 if (dstType
== GL_FLOAT
) {
3715 depthValues
= (GLfloat
*) dest
;
3718 depthTemp
= malloc(n
* sizeof(GLfloat
));
3720 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3724 depthValues
= depthTemp
;
3727 /* Convert incoming values to GLfloat. Some conversions will require
3732 DEPTH_VALUES(GLbyte
, BYTE_TO_FLOATZ
);
3733 needClamp
= GL_TRUE
;
3735 case GL_UNSIGNED_BYTE
:
3736 DEPTH_VALUES(GLubyte
, UBYTE_TO_FLOAT
);
3739 DEPTH_VALUES(GLshort
, SHORT_TO_FLOATZ
);
3740 needClamp
= GL_TRUE
;
3742 case GL_UNSIGNED_SHORT
:
3743 DEPTH_VALUES(GLushort
, USHORT_TO_FLOAT
);
3746 DEPTH_VALUES(GLint
, INT_TO_FLOAT
);
3747 needClamp
= GL_TRUE
;
3749 case GL_UNSIGNED_INT
:
3750 DEPTH_VALUES(GLuint
, UINT_TO_FLOAT
);
3752 case GL_UNSIGNED_INT_24_8_EXT
: /* GL_EXT_packed_depth_stencil */
3753 if (dstType
== GL_UNSIGNED_INT_24_8_EXT
&&
3754 depthMax
== 0xffffff &&
3755 ctx
->Pixel
.DepthScale
== 1.0 &&
3756 ctx
->Pixel
.DepthBias
== 0.0) {
3757 const GLuint
*src
= (const GLuint
*) source
;
3758 GLuint
*zValues
= (GLuint
*) dest
;
3760 for (i
= 0; i
< n
; i
++) {
3761 GLuint value
= src
[i
];
3762 if (srcPacking
->SwapBytes
) {
3765 zValues
[i
] = value
& 0xffffff00;
3771 const GLuint
*src
= (const GLuint
*) source
;
3772 const GLfloat scale
= 1.0f
/ 0xffffff;
3774 for (i
= 0; i
< n
; i
++) {
3775 GLuint value
= src
[i
];
3776 if (srcPacking
->SwapBytes
) {
3779 depthValues
[i
] = (value
>> 8) * scale
;
3783 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
3786 const GLfloat
*src
= (const GLfloat
*)source
;
3787 for (i
= 0; i
< n
; i
++) {
3788 GLfloat value
= src
[i
* 2];
3789 if (srcPacking
->SwapBytes
) {
3792 depthValues
[i
] = value
;
3794 needClamp
= GL_TRUE
;
3798 DEPTH_VALUES(GLfloat
, 1*);
3799 needClamp
= GL_TRUE
;
3801 case GL_HALF_FLOAT_ARB
:
3804 const GLhalfARB
*src
= (const GLhalfARB
*) source
;
3805 for (i
= 0; i
< n
; i
++) {
3806 GLhalfARB value
= src
[i
];
3807 if (srcPacking
->SwapBytes
) {
3810 depthValues
[i
] = _mesa_half_to_float(value
);
3812 needClamp
= GL_TRUE
;
3816 _mesa_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
3821 /* apply depth scale and bias */
3823 const GLfloat scale
= ctx
->Pixel
.DepthScale
;
3824 const GLfloat bias
= ctx
->Pixel
.DepthBias
;
3825 if (scale
!= 1.0 || bias
!= 0.0) {
3827 for (i
= 0; i
< n
; i
++) {
3828 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
3830 needClamp
= GL_TRUE
;
3834 /* clamp to [0, 1] */
3837 for (i
= 0; i
< n
; i
++) {
3838 depthValues
[i
] = (GLfloat
)CLAMP(depthValues
[i
], 0.0, 1.0);
3843 * Convert values to dstType
3845 if (dstType
== GL_UNSIGNED_INT
) {
3846 GLuint
*zValues
= (GLuint
*) dest
;
3848 if (depthMax
<= 0xffffff) {
3849 /* no overflow worries */
3850 for (i
= 0; i
< n
; i
++) {
3851 zValues
[i
] = (GLuint
) (depthValues
[i
] * (GLfloat
) depthMax
);
3855 /* need to use double precision to prevent overflow problems */
3856 for (i
= 0; i
< n
; i
++) {
3857 GLdouble z
= depthValues
[i
] * (GLdouble
) depthMax
;
3858 if (z
>= (GLdouble
) 0xffffffff)
3859 zValues
[i
] = 0xffffffff;
3861 zValues
[i
] = (GLuint
) z
;
3865 else if (dstType
== GL_UNSIGNED_SHORT
) {
3866 GLushort
*zValues
= (GLushort
*) dest
;
3868 ASSERT(depthMax
<= 0xffff);
3869 for (i
= 0; i
< n
; i
++) {
3870 zValues
[i
] = (GLushort
) (depthValues
[i
] * (GLfloat
) depthMax
);
3873 else if (dstType
== GL_FLOAT
) {
3874 /* Nothing to do. depthValues is pointing to dest. */
3876 else if (dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
) {
3877 GLfloat
*zValues
= (GLfloat
*) dest
;
3879 for (i
= 0; i
< n
; i
++) {
3880 zValues
[i
*2] = depthValues
[i
];
3892 * Pack an array of depth values. The values are floats in [0,1].
3895 _mesa_pack_depth_span( struct gl_context
*ctx
, GLuint n
, GLvoid
*dest
,
3896 GLenum dstType
, const GLfloat
*depthSpan
,
3897 const struct gl_pixelstore_attrib
*dstPacking
)
3899 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
3901 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
3905 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
3906 memcpy(depthCopy
, depthSpan
, n
* sizeof(GLfloat
));
3907 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
3908 depthSpan
= depthCopy
;
3912 case GL_UNSIGNED_BYTE
:
3914 GLubyte
*dst
= (GLubyte
*) dest
;
3916 for (i
= 0; i
< n
; i
++) {
3917 dst
[i
] = FLOAT_TO_UBYTE( depthSpan
[i
] );
3923 GLbyte
*dst
= (GLbyte
*) dest
;
3925 for (i
= 0; i
< n
; i
++) {
3926 dst
[i
] = FLOAT_TO_BYTE( depthSpan
[i
] );
3930 case GL_UNSIGNED_SHORT
:
3932 GLushort
*dst
= (GLushort
*) dest
;
3934 for (i
= 0; i
< n
; i
++) {
3935 CLAMPED_FLOAT_TO_USHORT(dst
[i
], depthSpan
[i
]);
3937 if (dstPacking
->SwapBytes
) {
3938 _mesa_swap2( (GLushort
*) dst
, n
);
3944 GLshort
*dst
= (GLshort
*) dest
;
3946 for (i
= 0; i
< n
; i
++) {
3947 dst
[i
] = FLOAT_TO_SHORT( depthSpan
[i
] );
3949 if (dstPacking
->SwapBytes
) {
3950 _mesa_swap2( (GLushort
*) dst
, n
);
3954 case GL_UNSIGNED_INT
:
3956 GLuint
*dst
= (GLuint
*) dest
;
3958 for (i
= 0; i
< n
; i
++) {
3959 dst
[i
] = FLOAT_TO_UINT( depthSpan
[i
] );
3961 if (dstPacking
->SwapBytes
) {
3962 _mesa_swap4( (GLuint
*) dst
, n
);
3968 GLint
*dst
= (GLint
*) dest
;
3970 for (i
= 0; i
< n
; i
++) {
3971 dst
[i
] = FLOAT_TO_INT( depthSpan
[i
] );
3973 if (dstPacking
->SwapBytes
) {
3974 _mesa_swap4( (GLuint
*) dst
, n
);
3980 GLfloat
*dst
= (GLfloat
*) dest
;
3982 for (i
= 0; i
< n
; i
++) {
3983 dst
[i
] = depthSpan
[i
];
3985 if (dstPacking
->SwapBytes
) {
3986 _mesa_swap4( (GLuint
*) dst
, n
);
3990 case GL_HALF_FLOAT_ARB
:
3992 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
3994 for (i
= 0; i
< n
; i
++) {
3995 dst
[i
] = _mesa_float_to_half(depthSpan
[i
]);
3997 if (dstPacking
->SwapBytes
) {
3998 _mesa_swap2( (GLushort
*) dst
, n
);
4003 _mesa_problem(ctx
, "bad type in _mesa_pack_depth_span");
4012 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
4015 _mesa_pack_depth_stencil_span(struct gl_context
*ctx
,GLuint n
,
4016 GLenum dstType
, GLuint
*dest
,
4017 const GLfloat
*depthVals
,
4018 const GLubyte
*stencilVals
,
4019 const struct gl_pixelstore_attrib
*dstPacking
)
4021 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
4022 GLubyte
*stencilCopy
= malloc(n
* sizeof(GLubyte
));
4025 if (!depthCopy
|| !stencilCopy
) {
4026 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
4032 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
4033 memcpy(depthCopy
, depthVals
, n
* sizeof(GLfloat
));
4034 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
4035 depthVals
= depthCopy
;
4038 if (ctx
->Pixel
.IndexShift
||
4039 ctx
->Pixel
.IndexOffset
||
4040 ctx
->Pixel
.MapStencilFlag
) {
4041 memcpy(stencilCopy
, stencilVals
, n
* sizeof(GLubyte
));
4042 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencilCopy
);
4043 stencilVals
= stencilCopy
;
4047 case GL_UNSIGNED_INT_24_8
:
4048 for (i
= 0; i
< n
; i
++) {
4049 GLuint z
= (GLuint
) (depthVals
[i
] * 0xffffff);
4050 dest
[i
] = (z
<< 8) | (stencilVals
[i
] & 0xff);
4053 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
4054 for (i
= 0; i
< n
; i
++) {
4055 ((GLfloat
*)dest
)[i
*2] = depthVals
[i
];
4056 dest
[i
*2+1] = stencilVals
[i
] & 0xff;
4061 if (dstPacking
->SwapBytes
) {
4062 _mesa_swap4(dest
, n
);
4073 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
4074 * Return all image data in a contiguous block. This is used when we
4075 * compile glDrawPixels, glTexImage, etc into a display list. We
4076 * need a copy of the data in a standard format.
4079 _mesa_unpack_image( GLuint dimensions
,
4080 GLsizei width
, GLsizei height
, GLsizei depth
,
4081 GLenum format
, GLenum type
, const GLvoid
*pixels
,
4082 const struct gl_pixelstore_attrib
*unpack
)
4084 GLint bytesPerRow
, compsPerRow
;
4085 GLboolean flipBytes
, swap2
, swap4
;
4088 return NULL
; /* not necessarily an error */
4090 if (width
<= 0 || height
<= 0 || depth
<= 0)
4091 return NULL
; /* generate error later */
4093 if (type
== GL_BITMAP
) {
4094 bytesPerRow
= (width
+ 7) >> 3;
4095 flipBytes
= unpack
->LsbFirst
;
4096 swap2
= swap4
= GL_FALSE
;
4100 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
4101 GLint components
= _mesa_components_in_format(format
);
4104 if (_mesa_type_is_packed(type
))
4107 if (bytesPerPixel
<= 0 || components
<= 0)
4108 return NULL
; /* bad format or type. generate error later */
4109 bytesPerRow
= bytesPerPixel
* width
;
4110 bytesPerComp
= bytesPerPixel
/ components
;
4111 flipBytes
= GL_FALSE
;
4112 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
4113 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
4114 compsPerRow
= components
* width
;
4115 assert(compsPerRow
>= width
);
4120 = malloc(bytesPerRow
* height
* depth
);
4124 return NULL
; /* generate GL_OUT_OF_MEMORY later */
4127 for (img
= 0; img
< depth
; img
++) {
4128 for (row
= 0; row
< height
; row
++) {
4129 const GLvoid
*src
= _mesa_image_address(dimensions
, unpack
, pixels
,
4130 width
, height
, format
, type
, img
, row
, 0);
4132 if ((type
== GL_BITMAP
) && (unpack
->SkipPixels
& 0x7)) {
4134 flipBytes
= GL_FALSE
;
4135 if (unpack
->LsbFirst
) {
4136 GLubyte srcMask
= 1 << (unpack
->SkipPixels
& 0x7);
4137 GLubyte dstMask
= 128;
4138 const GLubyte
*s
= src
;
4141 for (i
= 0; i
< width
; i
++) {
4145 if (srcMask
== 128) {
4150 srcMask
= srcMask
<< 1;
4158 dstMask
= dstMask
>> 1;
4163 GLubyte srcMask
= 128 >> (unpack
->SkipPixels
& 0x7);
4164 GLubyte dstMask
= 128;
4165 const GLubyte
*s
= src
;
4168 for (i
= 0; i
< width
; i
++) {
4177 srcMask
= srcMask
>> 1;
4185 dstMask
= dstMask
>> 1;
4191 memcpy(dst
, src
, bytesPerRow
);
4194 /* byte flipping/swapping */
4196 flip_bytes((GLubyte
*) dst
, bytesPerRow
);
4199 _mesa_swap2((GLushort
*) dst
, compsPerRow
);
4202 _mesa_swap4((GLuint
*) dst
, compsPerRow
);
4214 * If we unpack colors from a luminance surface, we'll get pixel colors
4215 * such as (l, l, l, a).
4216 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
4217 * function will compute L=R+G+B before packing. The net effect is we'll
4218 * accidentally store luminance values = 3*l.
4219 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
4221 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
4224 * Finally, we also need to do this when the actual surface format does
4225 * not match the logical surface format. For example, suppose the user
4226 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
4227 * Again, we'll get pixel values like (l,l,l,a).
4230 _mesa_rebase_rgba_float(GLuint n
, GLfloat rgba
[][4], GLenum baseFormat
)
4234 switch (baseFormat
) {
4236 for (i
= 0; i
< n
; i
++) {
4237 rgba
[i
][RCOMP
] = 0.0F
;
4238 rgba
[i
][GCOMP
] = 0.0F
;
4239 rgba
[i
][BCOMP
] = 0.0F
;
4245 for (i
= 0; i
< n
; i
++) {
4246 rgba
[i
][GCOMP
] = 0.0F
;
4247 rgba
[i
][BCOMP
] = 0.0F
;
4248 rgba
[i
][ACOMP
] = 1.0F
;
4251 case GL_LUMINANCE_ALPHA
:
4252 for (i
= 0; i
< n
; i
++) {
4253 rgba
[i
][GCOMP
] = 0.0F
;
4254 rgba
[i
][BCOMP
] = 0.0F
;
4258 for (i
= 0; i
< n
; i
++) {
4259 rgba
[i
][ACOMP
] = 1.0F
;
4263 for (i
= 0; i
< n
; i
++) {
4264 rgba
[i
][BCOMP
] = 0.0F
;
4265 rgba
[i
][ACOMP
] = 1.0F
;
4269 for (i
= 0; i
< n
; i
++) {
4270 rgba
[i
][GCOMP
] = 0.0F
;
4271 rgba
[i
][BCOMP
] = 0.0F
;
4272 rgba
[i
][ACOMP
] = 1.0F
;
4284 * As above, but GLuint components.
4287 _mesa_rebase_rgba_uint(GLuint n
, GLuint rgba
[][4], GLenum baseFormat
)
4291 switch (baseFormat
) {
4293 for (i
= 0; i
< n
; i
++) {
4302 for (i
= 0; i
< n
; i
++) {
4308 case GL_LUMINANCE_ALPHA
:
4309 for (i
= 0; i
< n
; i
++) {
4315 for (i
= 0; i
< n
; i
++) {
4320 for (i
= 0; i
< n
; i
++) {
4326 for (i
= 0; i
< n
; i
++) {