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
,
533 case GL_UNSIGNED_INT
:
534 pack_uint_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
537 pack_int_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
539 case GL_UNSIGNED_SHORT
:
540 pack_ushort_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
543 pack_short_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
545 case GL_UNSIGNED_BYTE
:
546 pack_ubyte_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
549 pack_byte_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
551 case GL_UNSIGNED_BYTE_3_3_2
:
552 if ((dstFormat
== GL_RGB
) || (dstFormat
== GL_RGB_INTEGER
))
553 _mesa_pack_uint_rgba_row(MESA_FORMAT_B2G3R3_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
555 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
557 case GL_UNSIGNED_BYTE_2_3_3_REV
:
558 if ((dstFormat
== GL_RGB
) || (dstFormat
== GL_RGB_INTEGER
))
559 _mesa_pack_uint_rgba_row(MESA_FORMAT_R3G3B2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
561 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
563 case GL_UNSIGNED_SHORT_5_6_5
:
564 if ((dstFormat
== GL_RGB
) || (dstFormat
== GL_RGB_INTEGER
))
565 _mesa_pack_uint_rgba_row(MESA_FORMAT_B5G6R5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
567 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
569 case GL_UNSIGNED_SHORT_5_6_5_REV
:
570 if ((dstFormat
== GL_RGB
) || (dstFormat
== GL_RGB_INTEGER
))
571 _mesa_pack_uint_rgba_row(MESA_FORMAT_R5G6B5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
573 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
575 case GL_UNSIGNED_SHORT_4_4_4_4
:
576 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
577 _mesa_pack_uint_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
578 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
579 _mesa_pack_uint_rgba_row(MESA_FORMAT_A4R4G4B4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
580 else if (dstFormat
== GL_ABGR_EXT
)
581 _mesa_pack_uint_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
583 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
585 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
586 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
587 _mesa_pack_uint_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
588 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
589 _mesa_pack_uint_rgba_row(MESA_FORMAT_B4G4R4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
590 else if (dstFormat
== GL_ABGR_EXT
)
591 _mesa_pack_uint_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
593 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
595 case GL_UNSIGNED_SHORT_5_5_5_1
:
596 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
597 _mesa_pack_uint_rgba_row(MESA_FORMAT_A1B5G5R5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
598 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
599 _mesa_pack_uint_rgba_row(MESA_FORMAT_A1R5G5B5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
600 else if (dstFormat
== GL_ABGR_EXT
)
601 _mesa_pack_uint_rgba_row(MESA_FORMAT_R1G5B5A5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
603 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
605 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
606 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
607 _mesa_pack_uint_rgba_row(MESA_FORMAT_R5G5B5A1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
608 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
609 _mesa_pack_uint_rgba_row(MESA_FORMAT_B5G5R5A1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
610 else if (dstFormat
== GL_ABGR_EXT
)
611 _mesa_pack_uint_rgba_row(MESA_FORMAT_A5B5G5R1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
613 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
615 case GL_UNSIGNED_INT_8_8_8_8
:
616 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
617 _mesa_pack_uint_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
618 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
619 _mesa_pack_uint_rgba_row(MESA_FORMAT_A8R8G8B8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
620 else if (dstFormat
== GL_ABGR_EXT
)
621 _mesa_pack_uint_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
623 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
625 case GL_UNSIGNED_INT_8_8_8_8_REV
:
626 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
627 _mesa_pack_uint_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
628 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
629 _mesa_pack_uint_rgba_row(MESA_FORMAT_B8G8R8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
630 else if (dstFormat
== GL_ABGR_EXT
)
631 _mesa_pack_uint_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
633 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
635 case GL_UNSIGNED_INT_10_10_10_2
:
636 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
637 _mesa_pack_uint_rgba_row(MESA_FORMAT_A2B10G10R10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
638 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
639 _mesa_pack_uint_rgba_row(MESA_FORMAT_A2R10G10B10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
640 else if (dstFormat
== GL_ABGR_EXT
)
641 _mesa_pack_uint_rgba_row(MESA_FORMAT_R2G10B10A10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
643 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
645 case GL_UNSIGNED_INT_2_10_10_10_REV
:
646 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
647 _mesa_pack_uint_rgba_row(MESA_FORMAT_R10G10B10A2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
648 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
649 _mesa_pack_uint_rgba_row(MESA_FORMAT_B10G10R10A2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
650 else if (dstFormat
== GL_ABGR_EXT
)
651 _mesa_pack_uint_rgba_row(MESA_FORMAT_A10B10G10R2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
653 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
656 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
661 /* Customization of signed integer packing.
663 #define SRC_TYPE GLint
665 #define DST_TYPE GLuint
666 #define SRC_CONVERT(x) MAX2(x, 0)
667 #define FN_NAME pack_uint_from_int_rgba
668 #include "pack_tmp.h"
673 #define DST_TYPE GLushort
674 #define SRC_CONVERT(x) MAX2(x, 0)
675 #define FN_NAME pack_ushort_from_int_rgba
676 #include "pack_tmp.h"
681 #define DST_TYPE GLshort
682 #define SRC_CONVERT(x) CLAMP(x, -0x8000, 0x7fff)
683 #define FN_NAME pack_short_from_int_rgba
684 #include "pack_tmp.h"
689 #define DST_TYPE GLubyte
690 #define SRC_CONVERT(x) MAX2(x, 0)
691 #define FN_NAME pack_ubyte_from_int_rgba
692 #include "pack_tmp.h"
697 #define DST_TYPE GLbyte
698 #define SRC_CONVERT(x) CLAMP(x, -0x80, 0x7f)
699 #define FN_NAME pack_byte_from_int_rgba
700 #include "pack_tmp.h"
708 _pack_rgba_span_from_ints_problem(struct gl_context
*ctx
,
709 GLenum dstFormat
, GLenum dstType
)
712 "Unsupported type (%s) / format (%s) "
713 "in _mesa_pack_rgba_span_from_ints",
714 _mesa_lookup_enum_by_nr(dstType
),
715 _mesa_lookup_enum_by_nr(dstFormat
));
719 _mesa_pack_rgba_span_from_ints(struct gl_context
*ctx
, GLuint n
, GLint rgba
[][4],
720 GLenum dstFormat
, GLenum dstType
,
724 case GL_UNSIGNED_INT
:
725 pack_uint_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
728 /* No conversion necessary. */
729 pack_uint_from_uint_rgba(ctx
, dstAddr
, dstFormat
, (GLuint (*)[4]) rgba
, NULL
, n
);
731 case GL_UNSIGNED_SHORT
:
732 pack_ushort_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
735 pack_short_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
737 case GL_UNSIGNED_BYTE
:
738 pack_ubyte_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
741 pack_byte_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
743 case GL_UNSIGNED_BYTE_3_3_2
:
744 if ((dstFormat
== GL_RGB
) || (dstFormat
== GL_RGB_INTEGER
))
745 _mesa_pack_int_rgba_row(MESA_FORMAT_B2G3R3_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
747 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
749 case GL_UNSIGNED_BYTE_2_3_3_REV
:
750 if ((dstFormat
== GL_RGB
) || (dstFormat
== GL_RGB_INTEGER
))
751 _mesa_pack_int_rgba_row(MESA_FORMAT_R3G3B2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
753 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
755 case GL_UNSIGNED_SHORT_5_6_5
:
756 if ((dstFormat
== GL_RGB
) || (dstFormat
== GL_RGB_INTEGER
))
757 _mesa_pack_int_rgba_row(MESA_FORMAT_B5G6R5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
759 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
761 case GL_UNSIGNED_SHORT_5_6_5_REV
:
762 if ((dstFormat
== GL_RGB
) || (dstFormat
== GL_RGB_INTEGER
))
763 _mesa_pack_int_rgba_row(MESA_FORMAT_R5G6B5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
765 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
767 case GL_UNSIGNED_SHORT_4_4_4_4
:
768 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
769 _mesa_pack_int_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
770 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
771 _mesa_pack_int_rgba_row(MESA_FORMAT_A4R4G4B4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
772 else if (dstFormat
== GL_ABGR_EXT
)
773 _mesa_pack_int_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
775 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
777 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
778 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
779 _mesa_pack_int_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
780 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
781 _mesa_pack_int_rgba_row(MESA_FORMAT_B4G4R4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
782 else if (dstFormat
== GL_ABGR_EXT
)
783 _mesa_pack_int_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
785 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
787 case GL_UNSIGNED_SHORT_5_5_5_1
:
788 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
789 _mesa_pack_int_rgba_row(MESA_FORMAT_A1B5G5R5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
790 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
791 _mesa_pack_int_rgba_row(MESA_FORMAT_A1R5G5B5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
792 else if (dstFormat
== GL_ABGR_EXT
)
793 _mesa_pack_int_rgba_row(MESA_FORMAT_R1G5B5A5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
795 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
797 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
798 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
799 _mesa_pack_int_rgba_row(MESA_FORMAT_R5G5B5A1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
800 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
801 _mesa_pack_int_rgba_row(MESA_FORMAT_B5G5R5A1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
802 else if (dstFormat
== GL_ABGR_EXT
)
803 _mesa_pack_int_rgba_row(MESA_FORMAT_A5B5G5R1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
805 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
807 case GL_UNSIGNED_INT_8_8_8_8
:
808 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
809 _mesa_pack_int_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
810 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
811 _mesa_pack_int_rgba_row(MESA_FORMAT_A8R8G8B8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
812 else if (dstFormat
== GL_ABGR_EXT
)
813 _mesa_pack_int_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
815 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
817 case GL_UNSIGNED_INT_8_8_8_8_REV
:
818 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
819 _mesa_pack_int_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
820 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
821 _mesa_pack_int_rgba_row(MESA_FORMAT_B8G8R8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
822 else if (dstFormat
== GL_ABGR_EXT
)
823 _mesa_pack_int_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
825 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
827 case GL_UNSIGNED_INT_10_10_10_2
:
828 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
829 _mesa_pack_int_rgba_row(MESA_FORMAT_A2B10G10R10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
830 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
831 _mesa_pack_int_rgba_row(MESA_FORMAT_A2R10G10B10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
832 else if (dstFormat
== GL_ABGR_EXT
)
833 _mesa_pack_int_rgba_row(MESA_FORMAT_R2G10B10A10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
835 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
837 case GL_UNSIGNED_INT_2_10_10_10_REV
:
838 if ((dstFormat
== GL_RGBA
) || (dstFormat
== GL_RGBA_INTEGER_EXT
))
839 _mesa_pack_int_rgba_row(MESA_FORMAT_R10G10B10A2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
840 else if ((dstFormat
== GL_BGRA
) || (dstFormat
== GL_BGRA_INTEGER
))
841 _mesa_pack_int_rgba_row(MESA_FORMAT_B10G10R10A2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
842 else if (dstFormat
== GL_ABGR_EXT
)
843 _mesa_pack_int_rgba_row(MESA_FORMAT_A10B10G10R2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
845 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
848 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
853 /* Customization of float packing.
855 #define SRC_TYPE GLfloat
857 #define DST_TYPE GLuint
858 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UINT(x)
859 #define SRC_CONVERT(x) (GLuint) x
860 #define FN_NAME pack_uint_from_float_rgba
861 #include "pack_tmp.h"
864 #undef FLOAT_SRC_CONVERT
867 #define DST_TYPE GLint
868 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_INT(x)
869 #define SRC_CONVERT(x) (GLint) x
870 #define FN_NAME pack_int_from_float_rgba
871 #include "pack_tmp.h"
874 #undef FLOAT_SRC_CONVERT
877 #define DST_TYPE GLshort
878 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_SHORT_TEX(x)
879 #define SRC_CONVERT(x) (GLshort) x
880 #define FN_NAME pack_short_from_float_rgba
881 #include "pack_tmp.h"
884 #undef FLOAT_SRC_CONVERT
887 #define DST_TYPE GLubyte
888 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UBYTE(x)
889 #define SRC_CONVERT(x) (GLubyte) x
890 #define FN_NAME pack_ubyte_from_float_rgba
891 #include "pack_tmp.h"
894 #undef FLOAT_SRC_CONVERT
897 #define DST_TYPE GLbyte
898 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_BYTE_TEX(x)
899 #define SRC_CONVERT(x) (GLbyte) x
900 #define FN_NAME pack_byte_from_float_rgba
901 #include "pack_tmp.h"
904 #undef FLOAT_SRC_CONVERT
907 #define DST_TYPE GLfloat
908 #define FLOAT_SRC_CONVERT(x) x
909 #define SRC_CONVERT(x) x
910 #define FN_NAME pack_float_from_float_rgba
911 #include "pack_tmp.h"
914 #undef FLOAT_SRC_CONVERT
917 #define DST_TYPE GLhalfARB
918 #define FLOAT_SRC_CONVERT(x) _mesa_float_to_half(x)
919 #define FN_NAME pack_half_float_from_float_rgba
920 #include "pack_tmp.h"
923 #undef FLOAT_SRC_CONVERT
929 * Used to pack an array [][4] of RGBA float colors as specified
930 * by the dstFormat, dstType and dstPacking. Used by glReadPixels.
931 * Historically, the RGBA values were in [0,1] and rescaled to fit
932 * into GLubytes, etc. But with new integer formats, the RGBA values
933 * may have any value and we don't always rescale when converting to
936 * Note: the rgba values will be modified by this function when any pixel
937 * transfer ops are enabled.
940 _mesa_pack_rgba_span_float(struct gl_context
*ctx
, GLuint n
, GLfloat rgba
[][4],
941 GLenum dstFormat
, GLenum dstType
,
943 const struct gl_pixelstore_attrib
*dstPacking
,
944 GLbitfield transferOps
)
947 const GLint comps
= _mesa_components_in_format(dstFormat
);
948 const GLboolean intDstFormat
= _mesa_is_enum_format_integer(dstFormat
);
951 if (dstFormat
== GL_LUMINANCE
||
952 dstFormat
== GL_LUMINANCE_ALPHA
||
953 dstFormat
== GL_LUMINANCE_INTEGER_EXT
||
954 dstFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
) {
955 luminance
= malloc(n
* sizeof(GLfloat
));
957 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
965 /* EXT_texture_integer specifies no transfer ops on integer
966 * types in the resolved issues section. Just set them to 0
967 * for integer surfaces.
973 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
977 * Component clamping (besides clamping to [0,1] in
978 * _mesa_apply_rgba_transfer_ops()).
981 /* clamping to dest type's min/max values */
983 if (get_type_min_max(dstType
, &min
, &max
)) {
984 for (i
= 0; i
< n
; i
++) {
985 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], min
, max
);
986 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], min
, max
);
987 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], min
, max
);
988 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], min
, max
);
992 else if (dstFormat
== GL_LUMINANCE
|| dstFormat
== GL_LUMINANCE_ALPHA
) {
993 /* compute luminance values */
994 if (transferOps
& IMAGE_CLAMP_BIT
) {
995 for (i
= 0; i
< n
; i
++) {
996 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
997 luminance
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1001 for (i
= 0; i
< n
; i
++) {
1002 luminance
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1008 * Pack/store the pixels. Ugh! Lots of cases!!!
1011 case GL_UNSIGNED_BYTE
:
1012 pack_ubyte_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1015 pack_byte_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1017 case GL_UNSIGNED_SHORT
:
1019 GLushort
*dst
= (GLushort
*) dstAddr
;
1020 switch (dstFormat
) {
1023 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][RCOMP
]);
1027 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][GCOMP
]);
1031 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][BCOMP
]);
1035 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][ACOMP
]);
1039 UNCLAMPED_FLOAT_TO_USHORT(dst
[i
], luminance
[i
]);
1041 case GL_LUMINANCE_ALPHA
:
1043 UNCLAMPED_FLOAT_TO_USHORT(dst
[i
*2+0], luminance
[i
]);
1044 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+1], rgba
[i
][ACOMP
]);
1049 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+0], rgba
[i
][RCOMP
]);
1050 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+1], rgba
[i
][GCOMP
]);
1055 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+0], rgba
[i
][RCOMP
]);
1056 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+1], rgba
[i
][GCOMP
]);
1057 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+2], rgba
[i
][BCOMP
]);
1062 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][RCOMP
]);
1063 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][GCOMP
]);
1064 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][BCOMP
]);
1065 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][ACOMP
]);
1070 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+0], rgba
[i
][BCOMP
]);
1071 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+1], rgba
[i
][GCOMP
]);
1072 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+2], rgba
[i
][RCOMP
]);
1077 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][BCOMP
]);
1078 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][GCOMP
]);
1079 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][RCOMP
]);
1080 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][ACOMP
]);
1085 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][ACOMP
]);
1086 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][BCOMP
]);
1087 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][GCOMP
]);
1088 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][RCOMP
]);
1091 case GL_RED_INTEGER_EXT
:
1093 dst
[i
] = (GLushort
) rgba
[i
][RCOMP
];
1096 case GL_GREEN_INTEGER_EXT
:
1098 dst
[i
] = (GLushort
) rgba
[i
][GCOMP
];
1101 case GL_BLUE_INTEGER_EXT
:
1103 dst
[i
] = (GLushort
) rgba
[i
][BCOMP
];
1106 case GL_ALPHA_INTEGER_EXT
:
1108 dst
[i
] = (GLushort
) rgba
[i
][ACOMP
];
1113 dst
[i
*2+0] = (GLushort
) rgba
[i
][RCOMP
];
1114 dst
[i
*2+1] = (GLushort
) rgba
[i
][GCOMP
];
1117 case GL_RGB_INTEGER_EXT
:
1119 dst
[i
*3+0] = (GLushort
) rgba
[i
][RCOMP
];
1120 dst
[i
*3+1] = (GLushort
) rgba
[i
][GCOMP
];
1121 dst
[i
*3+2] = (GLushort
) rgba
[i
][BCOMP
];
1124 case GL_RGBA_INTEGER_EXT
:
1126 dst
[i
*4+0] = (GLushort
) rgba
[i
][RCOMP
];
1127 dst
[i
*4+1] = (GLushort
) rgba
[i
][GCOMP
];
1128 dst
[i
*4+2] = (GLushort
) rgba
[i
][BCOMP
];
1129 dst
[i
*4+3] = (GLushort
) rgba
[i
][ACOMP
];
1132 case GL_BGR_INTEGER_EXT
:
1134 dst
[i
*3+0] = (GLushort
) rgba
[i
][BCOMP
];
1135 dst
[i
*3+1] = (GLushort
) rgba
[i
][GCOMP
];
1136 dst
[i
*3+2] = (GLushort
) rgba
[i
][RCOMP
];
1139 case GL_BGRA_INTEGER_EXT
:
1141 dst
[i
*4+0] = (GLushort
) rgba
[i
][BCOMP
];
1142 dst
[i
*4+1] = (GLushort
) rgba
[i
][GCOMP
];
1143 dst
[i
*4+2] = (GLushort
) rgba
[i
][RCOMP
];
1144 dst
[i
*4+3] = (GLushort
) rgba
[i
][ACOMP
];
1147 case GL_LUMINANCE_INTEGER_EXT
:
1149 dst
[i
*2+0] = (GLushort
) (rgba
[i
][RCOMP
] +
1152 dst
[i
*2+1] = (GLushort
) rgba
[i
][ACOMP
];
1155 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
1157 dst
[i
] = (GLushort
) (rgba
[i
][RCOMP
] +
1163 _mesa_problem(ctx
, "bad format in _mesa_pack_rgba_span\n");
1168 pack_short_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1170 case GL_UNSIGNED_INT
:
1171 pack_uint_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1174 pack_int_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1177 /* No conversion necessary. */
1178 pack_float_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1180 case GL_HALF_FLOAT_ARB
:
1181 pack_half_float_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
1183 case GL_UNSIGNED_BYTE_3_3_2
:
1184 if (dstFormat
== GL_RGB
)
1185 _mesa_pack_float_rgba_row(MESA_FORMAT_B2G3R3_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1187 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1188 if (dstFormat
== GL_RGB
)
1189 _mesa_pack_float_rgba_row(MESA_FORMAT_R3G3B2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1191 case GL_UNSIGNED_SHORT_5_6_5
:
1192 if (dstFormat
== GL_RGB
)
1193 _mesa_pack_float_rgba_row(MESA_FORMAT_B5G6R5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1195 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1196 if (dstFormat
== GL_RGB
)
1197 _mesa_pack_float_rgba_row(MESA_FORMAT_R5G6B5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1199 case GL_UNSIGNED_SHORT_4_4_4_4
:
1200 if (dstFormat
== GL_RGBA
)
1201 _mesa_pack_float_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1202 else if (dstFormat
== GL_BGRA
)
1203 _mesa_pack_float_rgba_row(MESA_FORMAT_A4R4G4B4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1204 else if (dstFormat
== GL_ABGR_EXT
)
1205 _mesa_pack_float_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1207 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1208 if (dstFormat
== GL_RGBA
)
1209 _mesa_pack_float_rgba_row(MESA_FORMAT_R4G4B4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1210 else if (dstFormat
== GL_BGRA
)
1211 _mesa_pack_float_rgba_row(MESA_FORMAT_B4G4R4A4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1212 else if (dstFormat
== GL_ABGR_EXT
)
1213 _mesa_pack_float_rgba_row(MESA_FORMAT_A4B4G4R4_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1215 case GL_UNSIGNED_SHORT_5_5_5_1
:
1216 if (dstFormat
== GL_RGBA
)
1217 _mesa_pack_float_rgba_row(MESA_FORMAT_A1B5G5R5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1218 else if (dstFormat
== GL_BGRA
)
1219 _mesa_pack_float_rgba_row(MESA_FORMAT_A1R5G5B5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1220 else if (dstFormat
== GL_ABGR_EXT
)
1221 _mesa_pack_float_rgba_row(MESA_FORMAT_R1G5B5A5_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1223 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1224 if (dstFormat
== GL_RGBA
)
1225 _mesa_pack_float_rgba_row(MESA_FORMAT_R5G5B5A1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1226 else if (dstFormat
== GL_BGRA
)
1227 _mesa_pack_float_rgba_row(MESA_FORMAT_B5G5R5A1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1228 else if (dstFormat
== GL_ABGR_EXT
)
1229 _mesa_pack_float_rgba_row(MESA_FORMAT_A5B5G5R1_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1231 case GL_UNSIGNED_INT_8_8_8_8
:
1232 if (dstFormat
== GL_RGBA
)
1233 _mesa_pack_float_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1234 else if (dstFormat
== GL_BGRA
)
1235 _mesa_pack_float_rgba_row(MESA_FORMAT_A8R8G8B8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1236 else if (dstFormat
== GL_ABGR_EXT
)
1237 _mesa_pack_float_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1239 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1240 if (dstFormat
== GL_RGBA
)
1241 _mesa_pack_float_rgba_row(MESA_FORMAT_R8G8B8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1242 else if (dstFormat
== GL_BGRA
)
1243 _mesa_pack_float_rgba_row(MESA_FORMAT_B8G8R8A8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1244 else if (dstFormat
== GL_ABGR_EXT
)
1245 _mesa_pack_float_rgba_row(MESA_FORMAT_A8B8G8R8_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1247 case GL_UNSIGNED_INT_10_10_10_2
:
1248 if (dstFormat
== GL_RGBA
)
1249 _mesa_pack_float_rgba_row(MESA_FORMAT_A2B10G10R10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1250 else if (dstFormat
== GL_BGRA
)
1251 _mesa_pack_float_rgba_row(MESA_FORMAT_A2R10G10B10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1252 else if (dstFormat
== GL_ABGR_EXT
)
1253 _mesa_pack_float_rgba_row(MESA_FORMAT_R2G10B10A10_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1255 case GL_UNSIGNED_INT_2_10_10_10_REV
:
1256 if (dstFormat
== GL_RGBA
)
1257 _mesa_pack_float_rgba_row(MESA_FORMAT_R10G10B10A2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1258 else if (dstFormat
== GL_BGRA
)
1259 _mesa_pack_float_rgba_row(MESA_FORMAT_B10G10R10A2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1260 else if (dstFormat
== GL_ABGR_EXT
)
1261 _mesa_pack_float_rgba_row(MESA_FORMAT_A10B10G10R2_UNORM
, n
, (void *)rgba
[0], (void *)dstAddr
);
1263 case GL_UNSIGNED_INT_5_9_9_9_REV
:
1264 _mesa_pack_float_rgba_row(MESA_FORMAT_R9G9B9E5_FLOAT
, n
, (void *)rgba
[0], (void *)dstAddr
);
1266 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
1267 _mesa_pack_float_rgba_row(MESA_FORMAT_R11G11B10_FLOAT
, n
, (void *)rgba
[0], (void *)dstAddr
);
1270 _mesa_problem(ctx
, "bad type in _mesa_pack_rgba_span_float");
1275 if (dstPacking
->SwapBytes
) {
1276 GLint swapSize
= _mesa_sizeof_packed_type(dstType
);
1277 if (swapSize
== 2) {
1278 _mesa_swap2((GLushort
*) dstAddr
, n
* comps
);
1280 else if (swapSize
== 4) {
1281 _mesa_swap4((GLuint
*) dstAddr
, n
* comps
);
1290 #define SWAP2BYTE(VALUE) \
1292 GLubyte *bytes = (GLubyte *) &(VALUE); \
1293 GLubyte tmp = bytes[0]; \
1294 bytes[0] = bytes[1]; \
1298 #define SWAP4BYTE(VALUE) \
1300 GLubyte *bytes = (GLubyte *) &(VALUE); \
1301 GLubyte tmp = bytes[0]; \
1302 bytes[0] = bytes[3]; \
1305 bytes[1] = bytes[2]; \
1311 extract_uint_indexes(GLuint n
, GLuint indexes
[],
1312 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1313 const struct gl_pixelstore_attrib
*unpack
)
1315 ASSERT(srcFormat
== GL_COLOR_INDEX
|| srcFormat
== GL_STENCIL_INDEX
);
1317 ASSERT(srcType
== GL_BITMAP
||
1318 srcType
== GL_UNSIGNED_BYTE
||
1319 srcType
== GL_BYTE
||
1320 srcType
== GL_UNSIGNED_SHORT
||
1321 srcType
== GL_SHORT
||
1322 srcType
== GL_UNSIGNED_INT
||
1323 srcType
== GL_INT
||
1324 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
1325 srcType
== GL_HALF_FLOAT_ARB
||
1326 srcType
== GL_FLOAT
||
1327 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
1332 GLubyte
*ubsrc
= (GLubyte
*) src
;
1333 if (unpack
->LsbFirst
) {
1334 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
1336 for (i
= 0; i
< n
; i
++) {
1337 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1348 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
1350 for (i
= 0; i
< n
; i
++) {
1351 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1363 case GL_UNSIGNED_BYTE
:
1366 const GLubyte
*s
= (const GLubyte
*) src
;
1367 for (i
= 0; i
< n
; i
++)
1374 const GLbyte
*s
= (const GLbyte
*) src
;
1375 for (i
= 0; i
< n
; i
++)
1379 case GL_UNSIGNED_SHORT
:
1382 const GLushort
*s
= (const GLushort
*) src
;
1383 if (unpack
->SwapBytes
) {
1384 for (i
= 0; i
< n
; i
++) {
1385 GLushort value
= s
[i
];
1391 for (i
= 0; i
< n
; i
++)
1399 const GLshort
*s
= (const GLshort
*) src
;
1400 if (unpack
->SwapBytes
) {
1401 for (i
= 0; i
< n
; i
++) {
1402 GLshort value
= s
[i
];
1408 for (i
= 0; i
< n
; i
++)
1413 case GL_UNSIGNED_INT
:
1416 const GLuint
*s
= (const GLuint
*) src
;
1417 if (unpack
->SwapBytes
) {
1418 for (i
= 0; i
< n
; i
++) {
1419 GLuint value
= s
[i
];
1425 for (i
= 0; i
< n
; i
++)
1433 const GLint
*s
= (const GLint
*) src
;
1434 if (unpack
->SwapBytes
) {
1435 for (i
= 0; i
< n
; i
++) {
1442 for (i
= 0; i
< n
; i
++)
1450 const GLfloat
*s
= (const GLfloat
*) src
;
1451 if (unpack
->SwapBytes
) {
1452 for (i
= 0; i
< n
; i
++) {
1453 GLfloat value
= s
[i
];
1455 indexes
[i
] = (GLuint
) value
;
1459 for (i
= 0; i
< n
; i
++)
1460 indexes
[i
] = (GLuint
) s
[i
];
1464 case GL_HALF_FLOAT_ARB
:
1467 const GLhalfARB
*s
= (const GLhalfARB
*) src
;
1468 if (unpack
->SwapBytes
) {
1469 for (i
= 0; i
< n
; i
++) {
1470 GLhalfARB value
= s
[i
];
1472 indexes
[i
] = (GLuint
) _mesa_half_to_float(value
);
1476 for (i
= 0; i
< n
; i
++)
1477 indexes
[i
] = (GLuint
) _mesa_half_to_float(s
[i
]);
1481 case GL_UNSIGNED_INT_24_8_EXT
:
1484 const GLuint
*s
= (const GLuint
*) src
;
1485 if (unpack
->SwapBytes
) {
1486 for (i
= 0; i
< n
; i
++) {
1487 GLuint value
= s
[i
];
1489 indexes
[i
] = value
& 0xff; /* lower 8 bits */
1493 for (i
= 0; i
< n
; i
++)
1494 indexes
[i
] = s
[i
] & 0xff; /* lower 8 bits */
1498 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1501 const GLuint
*s
= (const GLuint
*) src
;
1502 if (unpack
->SwapBytes
) {
1503 for (i
= 0; i
< n
; i
++) {
1504 GLuint value
= s
[i
*2+1];
1506 indexes
[i
] = value
& 0xff; /* lower 8 bits */
1510 for (i
= 0; i
< n
; i
++)
1511 indexes
[i
] = s
[i
*2+1] & 0xff; /* lower 8 bits */
1517 _mesa_problem(NULL
, "bad srcType in extract_uint_indexes");
1524 * Return source/dest RGBA indexes for unpacking pixels.
1527 get_component_mapping(GLenum format
,
1539 case GL_RED_INTEGER_EXT
:
1541 *gSrc
= *bSrc
= *aSrc
= -1;
1544 case GL_GREEN_INTEGER_EXT
:
1546 *rSrc
= *bSrc
= *aSrc
= -1;
1549 case GL_BLUE_INTEGER_EXT
:
1551 *rSrc
= *gSrc
= *aSrc
= -1;
1554 case GL_ALPHA_INTEGER_EXT
:
1555 *rSrc
= *gSrc
= *bSrc
= -1;
1559 case GL_LUMINANCE_INTEGER_EXT
:
1560 *rSrc
= *gSrc
= *bSrc
= 0;
1563 case GL_LUMINANCE_ALPHA
:
1564 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
1565 *rSrc
= *gSrc
= *bSrc
= 0;
1569 *rSrc
= *gSrc
= *bSrc
= *aSrc
= 0;
1583 case GL_RGB_INTEGER
:
1594 case GL_BGR_INTEGER
:
1605 case GL_RGBA_INTEGER
:
1616 case GL_BGRA_INTEGER
:
1637 _mesa_problem(NULL
, "bad srcFormat %s in get_component_mapping",
1638 _mesa_lookup_enum_by_nr(format
));
1646 * This function extracts floating point RGBA values from arbitrary
1647 * image data. srcFormat and srcType are the format and type parameters
1648 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1650 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1651 * implements the "Conversion to floating point", "Conversion to RGB",
1652 * and "Final Expansion to RGBA" operations.
1654 * Args: n - number of pixels
1655 * rgba - output colors
1656 * srcFormat - format of incoming data
1657 * srcType - data type of incoming data
1658 * src - source data pointer
1659 * swapBytes - perform byteswapping of incoming data?
1662 extract_float_rgba(GLuint n
, GLfloat rgba
[][4],
1663 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1664 GLboolean swapBytes
)
1666 GLint rSrc
, gSrc
, bSrc
, aSrc
;
1668 GLint rDst
, bDst
, gDst
, aDst
;
1669 GLboolean intFormat
;
1670 GLfloat rs
= 1.0f
, gs
= 1.0f
, bs
= 1.0f
, as
= 1.0f
; /* scale factors */
1672 ASSERT(srcFormat
== GL_RED
||
1673 srcFormat
== GL_GREEN
||
1674 srcFormat
== GL_BLUE
||
1675 srcFormat
== GL_ALPHA
||
1676 srcFormat
== GL_LUMINANCE
||
1677 srcFormat
== GL_LUMINANCE_ALPHA
||
1678 srcFormat
== GL_INTENSITY
||
1679 srcFormat
== GL_RG
||
1680 srcFormat
== GL_RGB
||
1681 srcFormat
== GL_BGR
||
1682 srcFormat
== GL_RGBA
||
1683 srcFormat
== GL_BGRA
||
1684 srcFormat
== GL_ABGR_EXT
||
1685 srcFormat
== GL_RED_INTEGER_EXT
||
1686 srcFormat
== GL_GREEN_INTEGER_EXT
||
1687 srcFormat
== GL_BLUE_INTEGER_EXT
||
1688 srcFormat
== GL_ALPHA_INTEGER_EXT
||
1689 srcFormat
== GL_RG_INTEGER
||
1690 srcFormat
== GL_RGB_INTEGER_EXT
||
1691 srcFormat
== GL_RGBA_INTEGER_EXT
||
1692 srcFormat
== GL_BGR_INTEGER_EXT
||
1693 srcFormat
== GL_BGRA_INTEGER_EXT
||
1694 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
1695 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
1697 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
1698 srcType
== GL_BYTE
||
1699 srcType
== GL_UNSIGNED_SHORT
||
1700 srcType
== GL_SHORT
||
1701 srcType
== GL_UNSIGNED_INT
||
1702 srcType
== GL_INT
||
1703 srcType
== GL_HALF_FLOAT_ARB
||
1704 srcType
== GL_FLOAT
||
1705 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
1706 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
1707 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
1708 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
1709 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
1710 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
1711 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
1712 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
1713 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
1714 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
1715 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
1716 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
1717 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
1718 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
1720 get_component_mapping(srcFormat
,
1721 &rSrc
, &gSrc
, &bSrc
, &aSrc
,
1722 &rDst
, &gDst
, &bDst
, &aDst
);
1724 stride
= _mesa_components_in_format(srcFormat
);
1726 intFormat
= _mesa_is_enum_format_integer(srcFormat
);
1728 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT_FLT, DEFAULT_INT, TYPE, CONVERSION) \
1729 if ((SRC_INDEX) < 0) { \
1732 for (i = 0; i < n; i++) { \
1733 rgba[i][DST_INDEX] = DEFAULT_INT; \
1737 for (i = 0; i < n; i++) { \
1738 rgba[i][DST_INDEX] = DEFAULT_FLT; \
1742 else if (swapBytes) { \
1743 const TYPE *s = (const TYPE *) src; \
1745 for (i = 0; i < n; i++) { \
1746 TYPE value = s[SRC_INDEX]; \
1747 if (sizeof(TYPE) == 2) { \
1750 else if (sizeof(TYPE) == 4) { \
1754 rgba[i][DST_INDEX] = (GLfloat) value; \
1756 rgba[i][DST_INDEX] = (GLfloat) CONVERSION(value); \
1761 const TYPE *s = (const TYPE *) src; \
1764 for (i = 0; i < n; i++) { \
1765 rgba[i][DST_INDEX] = (GLfloat) s[SRC_INDEX]; \
1770 for (i = 0; i < n; i++) { \
1771 rgba[i][DST_INDEX] = (GLfloat) CONVERSION(s[SRC_INDEX]); \
1778 case GL_UNSIGNED_BYTE
:
1779 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1780 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1781 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1782 PROCESS(aSrc
, ACOMP
, 1.0F
, 255, GLubyte
, UBYTE_TO_FLOAT
);
1785 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1786 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1787 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1788 PROCESS(aSrc
, ACOMP
, 1.0F
, 127, GLbyte
, BYTE_TO_FLOAT_TEX
);
1790 case GL_UNSIGNED_SHORT
:
1791 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1792 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1793 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1794 PROCESS(aSrc
, ACOMP
, 1.0F
, 0xffff, GLushort
, USHORT_TO_FLOAT
);
1797 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1798 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1799 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1800 PROCESS(aSrc
, ACOMP
, 1.0F
, 32767, GLshort
, SHORT_TO_FLOAT_TEX
);
1802 case GL_UNSIGNED_INT
:
1803 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1804 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1805 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1806 PROCESS(aSrc
, ACOMP
, 1.0F
, 0xffffffff, GLuint
, UINT_TO_FLOAT
);
1809 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1810 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1811 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1812 PROCESS(aSrc
, ACOMP
, 1.0F
, 2147483647, GLint
, INT_TO_FLOAT
);
1815 PROCESS(rSrc
, RCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1816 PROCESS(gSrc
, GCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1817 PROCESS(bSrc
, BCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1818 PROCESS(aSrc
, ACOMP
, 1.0F
, 1.0F
, GLfloat
, (GLfloat
));
1820 case GL_HALF_FLOAT_ARB
:
1821 PROCESS(rSrc
, RCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1822 PROCESS(gSrc
, GCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1823 PROCESS(bSrc
, BCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1824 PROCESS(aSrc
, ACOMP
, 1.0F
, 1.0F
, GLhalfARB
, _mesa_half_to_float
);
1826 case GL_UNSIGNED_BYTE_3_3_2
:
1828 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1835 for (i
= 0; i
< n
; i
++) {
1836 GLubyte p
= ubsrc
[i
];
1837 rgba
[i
][rDst
] = ((p
>> 5) ) * rs
;
1838 rgba
[i
][gDst
] = ((p
>> 2) & 0x7) * gs
;
1839 rgba
[i
][bDst
] = ((p
) & 0x3) * bs
;
1840 rgba
[i
][aDst
] = 1.0F
;
1844 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1846 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1853 for (i
= 0; i
< n
; i
++) {
1854 GLubyte p
= ubsrc
[i
];
1855 rgba
[i
][rDst
] = ((p
) & 0x7) * rs
;
1856 rgba
[i
][gDst
] = ((p
>> 3) & 0x7) * gs
;
1857 rgba
[i
][bDst
] = ((p
>> 6) ) * bs
;
1858 rgba
[i
][aDst
] = 1.0F
;
1862 case GL_UNSIGNED_SHORT_5_6_5
:
1869 const GLushort
*ussrc
= (const GLushort
*) src
;
1871 for (i
= 0; i
< n
; i
++) {
1872 GLushort p
= ussrc
[i
];
1874 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1875 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1876 rgba
[i
][bDst
] = ((p
) & 0x1f) * bs
;
1877 rgba
[i
][aDst
] = 1.0F
;
1881 const GLushort
*ussrc
= (const GLushort
*) src
;
1883 for (i
= 0; i
< n
; i
++) {
1884 GLushort p
= ussrc
[i
];
1885 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1886 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1887 rgba
[i
][bDst
] = ((p
) & 0x1f) * bs
;
1888 rgba
[i
][aDst
] = 1.0F
;
1892 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1899 const GLushort
*ussrc
= (const GLushort
*) src
;
1901 for (i
= 0; i
< n
; i
++) {
1902 GLushort p
= ussrc
[i
];
1904 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1905 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1906 rgba
[i
][bDst
] = ((p
>> 11) ) * bs
;
1907 rgba
[i
][aDst
] = 1.0F
;
1911 const GLushort
*ussrc
= (const GLushort
*) src
;
1913 for (i
= 0; i
< n
; i
++) {
1914 GLushort p
= ussrc
[i
];
1915 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1916 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1917 rgba
[i
][bDst
] = ((p
>> 11) ) * bs
;
1918 rgba
[i
][aDst
] = 1.0F
;
1922 case GL_UNSIGNED_SHORT_4_4_4_4
:
1924 rs
= gs
= bs
= as
= 1.0F
/ 15.0F
;
1927 const GLushort
*ussrc
= (const GLushort
*) src
;
1929 for (i
= 0; i
< n
; i
++) {
1930 GLushort p
= ussrc
[i
];
1932 rgba
[i
][rDst
] = ((p
>> 12) ) * rs
;
1933 rgba
[i
][gDst
] = ((p
>> 8) & 0xf) * gs
;
1934 rgba
[i
][bDst
] = ((p
>> 4) & 0xf) * bs
;
1935 rgba
[i
][aDst
] = ((p
) & 0xf) * as
;
1939 const GLushort
*ussrc
= (const GLushort
*) src
;
1941 for (i
= 0; i
< n
; i
++) {
1942 GLushort p
= ussrc
[i
];
1943 rgba
[i
][rDst
] = ((p
>> 12) ) * rs
;
1944 rgba
[i
][gDst
] = ((p
>> 8) & 0xf) * gs
;
1945 rgba
[i
][bDst
] = ((p
>> 4) & 0xf) * bs
;
1946 rgba
[i
][aDst
] = ((p
) & 0xf) * as
;
1950 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1952 rs
= gs
= bs
= as
= 1.0F
/ 15.0F
;
1955 const GLushort
*ussrc
= (const GLushort
*) src
;
1957 for (i
= 0; i
< n
; i
++) {
1958 GLushort p
= ussrc
[i
];
1960 rgba
[i
][rDst
] = ((p
) & 0xf) * rs
;
1961 rgba
[i
][gDst
] = ((p
>> 4) & 0xf) * gs
;
1962 rgba
[i
][bDst
] = ((p
>> 8) & 0xf) * bs
;
1963 rgba
[i
][aDst
] = ((p
>> 12) ) * as
;
1967 const GLushort
*ussrc
= (const GLushort
*) src
;
1969 for (i
= 0; i
< n
; i
++) {
1970 GLushort p
= ussrc
[i
];
1971 rgba
[i
][rDst
] = ((p
) & 0xf) * rs
;
1972 rgba
[i
][gDst
] = ((p
>> 4) & 0xf) * gs
;
1973 rgba
[i
][bDst
] = ((p
>> 8) & 0xf) * bs
;
1974 rgba
[i
][aDst
] = ((p
>> 12) ) * as
;
1978 case GL_UNSIGNED_SHORT_5_5_5_1
:
1980 rs
= gs
= bs
= 1.0F
/ 31.0F
;
1983 const GLushort
*ussrc
= (const GLushort
*) src
;
1985 for (i
= 0; i
< n
; i
++) {
1986 GLushort p
= ussrc
[i
];
1988 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1989 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f) * gs
;
1990 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f) * bs
;
1991 rgba
[i
][aDst
] = ((p
) & 0x1) * as
;
1995 const GLushort
*ussrc
= (const GLushort
*) src
;
1997 for (i
= 0; i
< n
; i
++) {
1998 GLushort p
= ussrc
[i
];
1999 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
2000 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f) * gs
;
2001 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f) * bs
;
2002 rgba
[i
][aDst
] = ((p
) & 0x1) * as
;
2006 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
2008 rs
= gs
= bs
= 1.0F
/ 31.0F
;
2011 const GLushort
*ussrc
= (const GLushort
*) src
;
2013 for (i
= 0; i
< n
; i
++) {
2014 GLushort p
= ussrc
[i
];
2016 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
2017 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f) * gs
;
2018 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f) * bs
;
2019 rgba
[i
][aDst
] = ((p
>> 15) ) * as
;
2023 const GLushort
*ussrc
= (const GLushort
*) src
;
2025 for (i
= 0; i
< n
; i
++) {
2026 GLushort p
= ussrc
[i
];
2027 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
2028 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f) * gs
;
2029 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f) * bs
;
2030 rgba
[i
][aDst
] = ((p
>> 15) ) * as
;
2034 case GL_UNSIGNED_INT_8_8_8_8
:
2036 const GLuint
*uisrc
= (const GLuint
*) src
;
2039 for (i
= 0; i
< n
; i
++) {
2040 GLuint p
= uisrc
[i
];
2041 rgba
[i
][rDst
] = (GLfloat
) ((p
) & 0xff);
2042 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 8) & 0xff);
2043 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 16) & 0xff);
2044 rgba
[i
][aDst
] = (GLfloat
) ((p
>> 24) );
2048 for (i
= 0; i
< n
; i
++) {
2049 GLuint p
= uisrc
[i
];
2050 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
2051 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
2052 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
2053 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
>> 24) );
2058 const GLuint
*uisrc
= (const GLuint
*) src
;
2061 for (i
= 0; i
< n
; i
++) {
2062 GLuint p
= uisrc
[i
];
2063 rgba
[i
][rDst
] = (GLfloat
) ((p
>> 24) );
2064 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 16) & 0xff);
2065 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 8) & 0xff);
2066 rgba
[i
][aDst
] = (GLfloat
) ((p
) & 0xff);
2070 for (i
= 0; i
< n
; i
++) {
2071 GLuint p
= uisrc
[i
];
2072 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
>> 24) );
2073 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
2074 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
2075 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
2080 case GL_UNSIGNED_INT_8_8_8_8_REV
:
2082 const GLuint
*uisrc
= (const GLuint
*) src
;
2085 for (i
= 0; i
< n
; i
++) {
2086 GLuint p
= uisrc
[i
];
2087 rgba
[i
][rDst
] = (GLfloat
) ((p
>> 24) );
2088 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 16) & 0xff);
2089 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 8) & 0xff);
2090 rgba
[i
][aDst
] = (GLfloat
) ((p
) & 0xff);
2094 for (i
= 0; i
< n
; i
++) {
2095 GLuint p
= uisrc
[i
];
2096 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
>> 24) );
2097 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
2098 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
2099 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
2104 const GLuint
*uisrc
= (const GLuint
*) src
;
2107 for (i
= 0; i
< n
; i
++) {
2108 GLuint p
= uisrc
[i
];
2109 rgba
[i
][rDst
] = (GLfloat
) ((p
) & 0xff);
2110 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 8) & 0xff);
2111 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 16) & 0xff);
2112 rgba
[i
][aDst
] = (GLfloat
) ((p
>> 24) );
2116 for (i
= 0; i
< n
; i
++) {
2117 GLuint p
= uisrc
[i
];
2118 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
2119 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
2120 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
2121 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
>> 24) );
2126 case GL_UNSIGNED_INT_10_10_10_2
:
2128 rs
= 1.0F
/ 1023.0F
;
2129 gs
= 1.0F
/ 1023.0F
;
2130 bs
= 1.0F
/ 1023.0F
;
2134 const GLuint
*uisrc
= (const GLuint
*) src
;
2136 for (i
= 0; i
< n
; i
++) {
2137 GLuint p
= uisrc
[i
];
2139 rgba
[i
][rDst
] = ((p
>> 22) ) * rs
;
2140 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff) * gs
;
2141 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff) * bs
;
2142 rgba
[i
][aDst
] = ((p
) & 0x3 ) * as
;
2146 const GLuint
*uisrc
= (const GLuint
*) src
;
2148 for (i
= 0; i
< n
; i
++) {
2149 GLuint p
= uisrc
[i
];
2150 rgba
[i
][rDst
] = ((p
>> 22) ) * rs
;
2151 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff) * gs
;
2152 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff) * bs
;
2153 rgba
[i
][aDst
] = ((p
) & 0x3 ) * as
;
2157 case GL_UNSIGNED_INT_2_10_10_10_REV
:
2159 rs
= 1.0F
/ 1023.0F
;
2160 gs
= 1.0F
/ 1023.0F
;
2161 bs
= 1.0F
/ 1023.0F
;
2165 const GLuint
*uisrc
= (const GLuint
*) src
;
2167 for (i
= 0; i
< n
; i
++) {
2168 GLuint p
= uisrc
[i
];
2170 rgba
[i
][rDst
] = ((p
) & 0x3ff) * rs
;
2171 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff) * gs
;
2172 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff) * bs
;
2174 rgba
[i
][aDst
] = 1.0F
;
2176 rgba
[i
][aDst
] = (p
>> 30) * as
;
2181 const GLuint
*uisrc
= (const GLuint
*) src
;
2183 for (i
= 0; i
< n
; i
++) {
2184 GLuint p
= uisrc
[i
];
2185 rgba
[i
][rDst
] = ((p
) & 0x3ff) * rs
;
2186 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff) * gs
;
2187 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff) * bs
;
2189 rgba
[i
][aDst
] = 1.0F
;
2191 rgba
[i
][aDst
] = (p
>> 30) * as
;
2196 case GL_UNSIGNED_INT_5_9_9_9_REV
:
2198 const GLuint
*uisrc
= (const GLuint
*) src
;
2201 for (i
= 0; i
< n
; i
++) {
2202 GLuint p
= uisrc
[i
];
2204 rgb9e5_to_float3(p
, f
);
2205 rgba
[i
][rDst
] = f
[0];
2206 rgba
[i
][gDst
] = f
[1];
2207 rgba
[i
][bDst
] = f
[2];
2208 rgba
[i
][aDst
] = 1.0F
;
2212 const GLuint
*uisrc
= (const GLuint
*) src
;
2215 for (i
= 0; i
< n
; i
++) {
2216 rgb9e5_to_float3(uisrc
[i
], f
);
2217 rgba
[i
][rDst
] = f
[0];
2218 rgba
[i
][gDst
] = f
[1];
2219 rgba
[i
][bDst
] = f
[2];
2220 rgba
[i
][aDst
] = 1.0F
;
2224 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
2226 const GLuint
*uisrc
= (const GLuint
*) src
;
2229 for (i
= 0; i
< n
; i
++) {
2230 GLuint p
= uisrc
[i
];
2232 r11g11b10f_to_float3(p
, f
);
2233 rgba
[i
][rDst
] = f
[0];
2234 rgba
[i
][gDst
] = f
[1];
2235 rgba
[i
][bDst
] = f
[2];
2236 rgba
[i
][aDst
] = 1.0F
;
2240 const GLuint
*uisrc
= (const GLuint
*) src
;
2243 for (i
= 0; i
< n
; i
++) {
2244 r11g11b10f_to_float3(uisrc
[i
], f
);
2245 rgba
[i
][rDst
] = f
[0];
2246 rgba
[i
][gDst
] = f
[1];
2247 rgba
[i
][bDst
] = f
[2];
2248 rgba
[i
][aDst
] = 1.0F
;
2253 _mesa_problem(NULL
, "bad srcType in extract float data");
2260 static inline GLuint
2261 clamp_float_to_uint(GLfloat f
)
2263 return f
< 0.0F
? 0 : F_TO_I(f
);
2267 static inline GLuint
2268 clamp_half_to_uint(GLhalfARB h
)
2270 GLfloat f
= _mesa_half_to_float(h
);
2271 return f
< 0.0F
? 0 : F_TO_I(f
);
2276 * \sa extract_float_rgba()
2279 extract_uint_rgba(GLuint n
, GLuint rgba
[][4],
2280 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
2281 GLboolean swapBytes
)
2283 GLint rSrc
, gSrc
, bSrc
, aSrc
;
2285 GLint rDst
, bDst
, gDst
, aDst
;
2287 ASSERT(srcFormat
== GL_RED
||
2288 srcFormat
== GL_GREEN
||
2289 srcFormat
== GL_BLUE
||
2290 srcFormat
== GL_ALPHA
||
2291 srcFormat
== GL_LUMINANCE
||
2292 srcFormat
== GL_LUMINANCE_ALPHA
||
2293 srcFormat
== GL_INTENSITY
||
2294 srcFormat
== GL_RG
||
2295 srcFormat
== GL_RGB
||
2296 srcFormat
== GL_BGR
||
2297 srcFormat
== GL_RGBA
||
2298 srcFormat
== GL_BGRA
||
2299 srcFormat
== GL_ABGR_EXT
||
2300 srcFormat
== GL_RED_INTEGER_EXT
||
2301 srcFormat
== GL_RG_INTEGER
||
2302 srcFormat
== GL_GREEN_INTEGER_EXT
||
2303 srcFormat
== GL_BLUE_INTEGER_EXT
||
2304 srcFormat
== GL_ALPHA_INTEGER_EXT
||
2305 srcFormat
== GL_RGB_INTEGER_EXT
||
2306 srcFormat
== GL_RGBA_INTEGER_EXT
||
2307 srcFormat
== GL_BGR_INTEGER_EXT
||
2308 srcFormat
== GL_BGRA_INTEGER_EXT
||
2309 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
2310 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
2312 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
2313 srcType
== GL_BYTE
||
2314 srcType
== GL_UNSIGNED_SHORT
||
2315 srcType
== GL_SHORT
||
2316 srcType
== GL_UNSIGNED_INT
||
2317 srcType
== GL_INT
||
2318 srcType
== GL_HALF_FLOAT_ARB
||
2319 srcType
== GL_FLOAT
||
2320 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2321 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2322 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2323 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2324 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2325 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2326 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2327 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2328 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2329 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2330 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2331 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
2332 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
2333 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
2335 get_component_mapping(srcFormat
,
2336 &rSrc
, &gSrc
, &bSrc
, &aSrc
,
2337 &rDst
, &gDst
, &bDst
, &aDst
);
2339 stride
= _mesa_components_in_format(srcFormat
);
2341 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT, TYPE, CONVERSION) \
2342 if ((SRC_INDEX) < 0) { \
2344 for (i = 0; i < n; i++) { \
2345 rgba[i][DST_INDEX] = DEFAULT; \
2348 else if (swapBytes) { \
2349 const TYPE *s = (const TYPE *) src; \
2351 for (i = 0; i < n; i++) { \
2352 TYPE value = s[SRC_INDEX]; \
2353 if (sizeof(TYPE) == 2) { \
2356 else if (sizeof(TYPE) == 4) { \
2359 rgba[i][DST_INDEX] = CONVERSION(value); \
2364 const TYPE *s = (const TYPE *) src; \
2366 for (i = 0; i < n; i++) { \
2367 rgba[i][DST_INDEX] = CONVERSION(s[SRC_INDEX]); \
2373 case GL_UNSIGNED_BYTE
:
2374 PROCESS(rSrc
, RCOMP
, 0, GLubyte
, (GLuint
));
2375 PROCESS(gSrc
, GCOMP
, 0, GLubyte
, (GLuint
));
2376 PROCESS(bSrc
, BCOMP
, 0, GLubyte
, (GLuint
));
2377 PROCESS(aSrc
, ACOMP
, 1, GLubyte
, (GLuint
));
2380 PROCESS(rSrc
, RCOMP
, 0, GLbyte
, (GLuint
));
2381 PROCESS(gSrc
, GCOMP
, 0, GLbyte
, (GLuint
));
2382 PROCESS(bSrc
, BCOMP
, 0, GLbyte
, (GLuint
));
2383 PROCESS(aSrc
, ACOMP
, 1, GLbyte
, (GLuint
));
2385 case GL_UNSIGNED_SHORT
:
2386 PROCESS(rSrc
, RCOMP
, 0, GLushort
, (GLuint
));
2387 PROCESS(gSrc
, GCOMP
, 0, GLushort
, (GLuint
));
2388 PROCESS(bSrc
, BCOMP
, 0, GLushort
, (GLuint
));
2389 PROCESS(aSrc
, ACOMP
, 1, GLushort
, (GLuint
));
2392 PROCESS(rSrc
, RCOMP
, 0, GLshort
, (GLuint
));
2393 PROCESS(gSrc
, GCOMP
, 0, GLshort
, (GLuint
));
2394 PROCESS(bSrc
, BCOMP
, 0, GLshort
, (GLuint
));
2395 PROCESS(aSrc
, ACOMP
, 1, GLshort
, (GLuint
));
2397 case GL_UNSIGNED_INT
:
2398 PROCESS(rSrc
, RCOMP
, 0, GLuint
, (GLuint
));
2399 PROCESS(gSrc
, GCOMP
, 0, GLuint
, (GLuint
));
2400 PROCESS(bSrc
, BCOMP
, 0, GLuint
, (GLuint
));
2401 PROCESS(aSrc
, ACOMP
, 1, GLuint
, (GLuint
));
2404 PROCESS(rSrc
, RCOMP
, 0, GLint
, (GLuint
));
2405 PROCESS(gSrc
, GCOMP
, 0, GLint
, (GLuint
));
2406 PROCESS(bSrc
, BCOMP
, 0, GLint
, (GLuint
));
2407 PROCESS(aSrc
, ACOMP
, 1, GLint
, (GLuint
));
2410 PROCESS(rSrc
, RCOMP
, 0, GLfloat
, clamp_float_to_uint
);
2411 PROCESS(gSrc
, GCOMP
, 0, GLfloat
, clamp_float_to_uint
);
2412 PROCESS(bSrc
, BCOMP
, 0, GLfloat
, clamp_float_to_uint
);
2413 PROCESS(aSrc
, ACOMP
, 1, GLfloat
, clamp_float_to_uint
);
2415 case GL_HALF_FLOAT_ARB
:
2416 PROCESS(rSrc
, RCOMP
, 0, GLhalfARB
, clamp_half_to_uint
);
2417 PROCESS(gSrc
, GCOMP
, 0, GLhalfARB
, clamp_half_to_uint
);
2418 PROCESS(bSrc
, BCOMP
, 0, GLhalfARB
, clamp_half_to_uint
);
2419 PROCESS(aSrc
, ACOMP
, 1, GLhalfARB
, clamp_half_to_uint
);
2421 case GL_UNSIGNED_BYTE_3_3_2
:
2423 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
2425 for (i
= 0; i
< n
; i
++) {
2426 GLubyte p
= ubsrc
[i
];
2427 rgba
[i
][rDst
] = ((p
>> 5) );
2428 rgba
[i
][gDst
] = ((p
>> 2) & 0x7);
2429 rgba
[i
][bDst
] = ((p
) & 0x3);
2434 case GL_UNSIGNED_BYTE_2_3_3_REV
:
2436 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
2438 for (i
= 0; i
< n
; i
++) {
2439 GLubyte p
= ubsrc
[i
];
2440 rgba
[i
][rDst
] = ((p
) & 0x7);
2441 rgba
[i
][gDst
] = ((p
>> 3) & 0x7);
2442 rgba
[i
][bDst
] = ((p
>> 6) );
2447 case GL_UNSIGNED_SHORT_5_6_5
:
2449 const GLushort
*ussrc
= (const GLushort
*) src
;
2451 for (i
= 0; i
< n
; i
++) {
2452 GLushort p
= ussrc
[i
];
2454 rgba
[i
][rDst
] = ((p
>> 11) );
2455 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f);
2456 rgba
[i
][bDst
] = ((p
) & 0x1f);
2461 const GLushort
*ussrc
= (const GLushort
*) src
;
2463 for (i
= 0; i
< n
; i
++) {
2464 GLushort p
= ussrc
[i
];
2465 rgba
[i
][rDst
] = ((p
>> 11) );
2466 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f);
2467 rgba
[i
][bDst
] = ((p
) & 0x1f);
2472 case GL_UNSIGNED_SHORT_5_6_5_REV
:
2474 const GLushort
*ussrc
= (const GLushort
*) src
;
2476 for (i
= 0; i
< n
; i
++) {
2477 GLushort p
= ussrc
[i
];
2479 rgba
[i
][rDst
] = ((p
) & 0x1f);
2480 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f);
2481 rgba
[i
][bDst
] = ((p
>> 11) );
2486 const GLushort
*ussrc
= (const GLushort
*) src
;
2488 for (i
= 0; i
< n
; i
++) {
2489 GLushort p
= ussrc
[i
];
2490 rgba
[i
][rDst
] = ((p
) & 0x1f);
2491 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f);
2492 rgba
[i
][bDst
] = ((p
>> 11) );
2497 case GL_UNSIGNED_SHORT_4_4_4_4
:
2499 const GLushort
*ussrc
= (const GLushort
*) src
;
2501 for (i
= 0; i
< n
; i
++) {
2502 GLushort p
= ussrc
[i
];
2504 rgba
[i
][rDst
] = ((p
>> 12) );
2505 rgba
[i
][gDst
] = ((p
>> 8) & 0xf);
2506 rgba
[i
][bDst
] = ((p
>> 4) & 0xf);
2507 rgba
[i
][aDst
] = ((p
) & 0xf);
2511 const GLushort
*ussrc
= (const GLushort
*) src
;
2513 for (i
= 0; i
< n
; i
++) {
2514 GLushort p
= ussrc
[i
];
2515 rgba
[i
][rDst
] = ((p
>> 12) );
2516 rgba
[i
][gDst
] = ((p
>> 8) & 0xf);
2517 rgba
[i
][bDst
] = ((p
>> 4) & 0xf);
2518 rgba
[i
][aDst
] = ((p
) & 0xf);
2522 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
2524 const GLushort
*ussrc
= (const GLushort
*) src
;
2526 for (i
= 0; i
< n
; i
++) {
2527 GLushort p
= ussrc
[i
];
2529 rgba
[i
][rDst
] = ((p
) & 0xf);
2530 rgba
[i
][gDst
] = ((p
>> 4) & 0xf);
2531 rgba
[i
][bDst
] = ((p
>> 8) & 0xf);
2532 rgba
[i
][aDst
] = ((p
>> 12) );
2536 const GLushort
*ussrc
= (const GLushort
*) src
;
2538 for (i
= 0; i
< n
; i
++) {
2539 GLushort p
= ussrc
[i
];
2540 rgba
[i
][rDst
] = ((p
) & 0xf);
2541 rgba
[i
][gDst
] = ((p
>> 4) & 0xf);
2542 rgba
[i
][bDst
] = ((p
>> 8) & 0xf);
2543 rgba
[i
][aDst
] = ((p
>> 12) );
2547 case GL_UNSIGNED_SHORT_5_5_5_1
:
2549 const GLushort
*ussrc
= (const GLushort
*) src
;
2551 for (i
= 0; i
< n
; i
++) {
2552 GLushort p
= ussrc
[i
];
2554 rgba
[i
][rDst
] = ((p
>> 11) );
2555 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f);
2556 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f);
2557 rgba
[i
][aDst
] = ((p
) & 0x1 );
2561 const GLushort
*ussrc
= (const GLushort
*) src
;
2563 for (i
= 0; i
< n
; i
++) {
2564 GLushort p
= ussrc
[i
];
2565 rgba
[i
][rDst
] = ((p
>> 11) );
2566 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f);
2567 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f);
2568 rgba
[i
][aDst
] = ((p
) & 0x1 );
2572 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
2574 const GLushort
*ussrc
= (const GLushort
*) src
;
2576 for (i
= 0; i
< n
; i
++) {
2577 GLushort p
= ussrc
[i
];
2579 rgba
[i
][rDst
] = ((p
) & 0x1f);
2580 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f);
2581 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f);
2582 rgba
[i
][aDst
] = ((p
>> 15) );
2586 const GLushort
*ussrc
= (const GLushort
*) src
;
2588 for (i
= 0; i
< n
; i
++) {
2589 GLushort p
= ussrc
[i
];
2590 rgba
[i
][rDst
] = ((p
) & 0x1f);
2591 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f);
2592 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f);
2593 rgba
[i
][aDst
] = ((p
>> 15) );
2597 case GL_UNSIGNED_INT_8_8_8_8
:
2599 const GLuint
*uisrc
= (const GLuint
*) src
;
2601 for (i
= 0; i
< n
; i
++) {
2602 GLuint p
= uisrc
[i
];
2603 rgba
[i
][rDst
] = ((p
) & 0xff);
2604 rgba
[i
][gDst
] = ((p
>> 8) & 0xff);
2605 rgba
[i
][bDst
] = ((p
>> 16) & 0xff);
2606 rgba
[i
][aDst
] = ((p
>> 24) );
2610 const GLuint
*uisrc
= (const GLuint
*) src
;
2612 for (i
= 0; i
< n
; i
++) {
2613 GLuint p
= uisrc
[i
];
2614 rgba
[i
][rDst
] = ((p
>> 24) );
2615 rgba
[i
][gDst
] = ((p
>> 16) & 0xff);
2616 rgba
[i
][bDst
] = ((p
>> 8) & 0xff);
2617 rgba
[i
][aDst
] = ((p
) & 0xff);
2621 case GL_UNSIGNED_INT_8_8_8_8_REV
:
2623 const GLuint
*uisrc
= (const GLuint
*) src
;
2625 for (i
= 0; i
< n
; i
++) {
2626 GLuint p
= uisrc
[i
];
2627 rgba
[i
][rDst
] = ((p
>> 24) );
2628 rgba
[i
][gDst
] = ((p
>> 16) & 0xff);
2629 rgba
[i
][bDst
] = ((p
>> 8) & 0xff);
2630 rgba
[i
][aDst
] = ((p
) & 0xff);
2634 const GLuint
*uisrc
= (const GLuint
*) src
;
2636 for (i
= 0; i
< n
; i
++) {
2637 GLuint p
= uisrc
[i
];
2638 rgba
[i
][rDst
] = ((p
) & 0xff);
2639 rgba
[i
][gDst
] = ((p
>> 8) & 0xff);
2640 rgba
[i
][bDst
] = ((p
>> 16) & 0xff);
2641 rgba
[i
][aDst
] = ((p
>> 24) );
2645 case GL_UNSIGNED_INT_10_10_10_2
:
2647 const GLuint
*uisrc
= (const GLuint
*) src
;
2649 for (i
= 0; i
< n
; i
++) {
2650 GLuint p
= uisrc
[i
];
2652 rgba
[i
][rDst
] = ((p
>> 22) );
2653 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff);
2654 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff);
2655 rgba
[i
][aDst
] = ((p
) & 0x3 );
2659 const GLuint
*uisrc
= (const GLuint
*) src
;
2661 for (i
= 0; i
< n
; i
++) {
2662 GLuint p
= uisrc
[i
];
2663 rgba
[i
][rDst
] = ((p
>> 22) );
2664 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff);
2665 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff);
2666 rgba
[i
][aDst
] = ((p
) & 0x3 );
2670 case GL_UNSIGNED_INT_2_10_10_10_REV
:
2672 const GLuint
*uisrc
= (const GLuint
*) src
;
2674 for (i
= 0; i
< n
; i
++) {
2675 GLuint p
= uisrc
[i
];
2677 rgba
[i
][rDst
] = ((p
) & 0x3ff);
2678 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff);
2679 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff);
2680 rgba
[i
][aDst
] = ((p
>> 30) );
2684 const GLuint
*uisrc
= (const GLuint
*) src
;
2686 for (i
= 0; i
< n
; i
++) {
2687 GLuint p
= uisrc
[i
];
2688 rgba
[i
][rDst
] = ((p
) & 0x3ff);
2689 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff);
2690 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff);
2691 rgba
[i
][aDst
] = ((p
>> 30) );
2695 case GL_UNSIGNED_INT_5_9_9_9_REV
:
2697 const GLuint
*uisrc
= (const GLuint
*) src
;
2700 for (i
= 0; i
< n
; i
++) {
2701 GLuint p
= uisrc
[i
];
2703 rgb9e5_to_float3(p
, f
);
2704 rgba
[i
][rDst
] = clamp_float_to_uint(f
[0]);
2705 rgba
[i
][gDst
] = clamp_float_to_uint(f
[1]);
2706 rgba
[i
][bDst
] = clamp_float_to_uint(f
[2]);
2711 const GLuint
*uisrc
= (const GLuint
*) src
;
2714 for (i
= 0; i
< n
; i
++) {
2715 GLuint p
= uisrc
[i
];
2716 rgb9e5_to_float3(p
, f
);
2717 rgba
[i
][rDst
] = clamp_float_to_uint(f
[0]);
2718 rgba
[i
][gDst
] = clamp_float_to_uint(f
[1]);
2719 rgba
[i
][bDst
] = clamp_float_to_uint(f
[2]);
2724 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
2726 const GLuint
*uisrc
= (const GLuint
*) src
;
2729 for (i
= 0; i
< n
; i
++) {
2730 GLuint p
= uisrc
[i
];
2732 r11g11b10f_to_float3(p
, f
);
2733 rgba
[i
][rDst
] = clamp_float_to_uint(f
[0]);
2734 rgba
[i
][gDst
] = clamp_float_to_uint(f
[1]);
2735 rgba
[i
][bDst
] = clamp_float_to_uint(f
[2]);
2740 const GLuint
*uisrc
= (const GLuint
*) src
;
2743 for (i
= 0; i
< n
; i
++) {
2744 GLuint p
= uisrc
[i
];
2745 r11g11b10f_to_float3(p
, f
);
2746 rgba
[i
][rDst
] = clamp_float_to_uint(f
[0]);
2747 rgba
[i
][gDst
] = clamp_float_to_uint(f
[1]);
2748 rgba
[i
][bDst
] = clamp_float_to_uint(f
[2]);
2754 _mesa_problem(NULL
, "bad srcType in extract uint data");
2763 * Unpack a row of color image data from a client buffer according to
2764 * the pixel unpacking parameters.
2765 * Return GLubyte values in the specified dest image format.
2766 * This is used by glDrawPixels and glTexImage?D().
2767 * \param ctx - the context
2768 * n - number of pixels in the span
2769 * dstFormat - format of destination color array
2770 * dest - the destination color array
2771 * srcFormat - source image format
2772 * srcType - source image data type
2773 * source - source image pointer
2774 * srcPacking - pixel unpacking parameters
2775 * transferOps - bitmask of IMAGE_*_BIT values of operations to apply
2777 * XXX perhaps expand this to process whole images someday.
2780 _mesa_unpack_color_span_ubyte(struct gl_context
*ctx
,
2781 GLuint n
, GLenum dstFormat
, GLubyte dest
[],
2782 GLenum srcFormat
, GLenum srcType
,
2783 const GLvoid
*source
,
2784 const struct gl_pixelstore_attrib
*srcPacking
,
2785 GLbitfield transferOps
)
2787 GLboolean intFormat
= _mesa_is_enum_format_integer(srcFormat
);
2788 ASSERT(dstFormat
== GL_ALPHA
||
2789 dstFormat
== GL_LUMINANCE
||
2790 dstFormat
== GL_LUMINANCE_ALPHA
||
2791 dstFormat
== GL_INTENSITY
||
2792 dstFormat
== GL_RED
||
2793 dstFormat
== GL_RG
||
2794 dstFormat
== GL_RGB
||
2795 dstFormat
== GL_RGBA
);
2797 ASSERT(srcFormat
== GL_RED
||
2798 srcFormat
== GL_GREEN
||
2799 srcFormat
== GL_BLUE
||
2800 srcFormat
== GL_ALPHA
||
2801 srcFormat
== GL_LUMINANCE
||
2802 srcFormat
== GL_LUMINANCE_ALPHA
||
2803 srcFormat
== GL_INTENSITY
||
2804 srcFormat
== GL_RG
||
2805 srcFormat
== GL_RGB
||
2806 srcFormat
== GL_BGR
||
2807 srcFormat
== GL_RGBA
||
2808 srcFormat
== GL_BGRA
||
2809 srcFormat
== GL_ABGR_EXT
||
2810 srcFormat
== GL_COLOR_INDEX
);
2812 ASSERT(srcType
== GL_BITMAP
||
2813 srcType
== GL_UNSIGNED_BYTE
||
2814 srcType
== GL_BYTE
||
2815 srcType
== GL_UNSIGNED_SHORT
||
2816 srcType
== GL_SHORT
||
2817 srcType
== GL_UNSIGNED_INT
||
2818 srcType
== GL_INT
||
2819 srcType
== GL_HALF_FLOAT_ARB
||
2820 srcType
== GL_FLOAT
||
2821 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2822 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2823 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2824 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2825 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2826 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2827 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2828 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2829 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2830 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2831 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2832 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
2833 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
2834 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
2836 /* EXT_texture_integer specifies no transfer ops on integer
2837 * types in the resolved issues section. Just set them to 0
2838 * for integer surfaces.
2843 /* Try simple cases first */
2844 if (transferOps
== 0) {
2845 if (srcType
== GL_UNSIGNED_BYTE
) {
2846 if (dstFormat
== GL_RGBA
) {
2847 if (srcFormat
== GL_RGBA
) {
2848 memcpy( dest
, source
, n
* 4 * sizeof(GLubyte
) );
2851 else if (srcFormat
== GL_RGB
) {
2853 const GLubyte
*src
= (const GLubyte
*) source
;
2854 GLubyte
*dst
= dest
;
2855 for (i
= 0; i
< n
; i
++) {
2866 else if (dstFormat
== GL_RGB
) {
2867 if (srcFormat
== GL_RGB
) {
2868 memcpy( dest
, source
, n
* 3 * sizeof(GLubyte
) );
2871 else if (srcFormat
== GL_RGBA
) {
2873 const GLubyte
*src
= (const GLubyte
*) source
;
2874 GLubyte
*dst
= dest
;
2875 for (i
= 0; i
< n
; i
++) {
2885 else if (dstFormat
== srcFormat
) {
2886 GLint comps
= _mesa_components_in_format(srcFormat
);
2888 memcpy( dest
, source
, n
* comps
* sizeof(GLubyte
) );
2895 /* general solution begins here */
2897 GLint dstComponents
;
2898 GLint rDst
, gDst
, bDst
, aDst
, lDst
, iDst
;
2899 GLfloat (*rgba
)[4] = malloc(4 * n
* sizeof(GLfloat
));
2902 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2906 dstComponents
= _mesa_components_in_format( dstFormat
);
2907 /* source & dest image formats should have been error checked by now */
2908 assert(dstComponents
> 0);
2911 * Extract image data and convert to RGBA floats
2913 if (srcFormat
== GL_COLOR_INDEX
) {
2914 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
2917 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2922 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
2925 /* Convert indexes to RGBA */
2926 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
2927 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
2929 _mesa_map_ci_to_rgba(ctx
, n
, indexes
, rgba
);
2931 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
2932 * with color indexes.
2934 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
2939 /* non-color index data */
2940 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
2941 srcPacking
->SwapBytes
);
2944 /* Need to clamp if returning GLubytes */
2945 transferOps
|= IMAGE_CLAMP_BIT
;
2948 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
2951 get_component_indexes(dstFormat
,
2952 &rDst
, &gDst
, &bDst
, &aDst
, &lDst
, &iDst
);
2954 /* Now return the GLubyte data in the requested dstFormat */
2956 GLubyte
*dst
= dest
;
2958 for (i
= 0; i
< n
; i
++) {
2959 CLAMPED_FLOAT_TO_UBYTE(dst
[rDst
], rgba
[i
][RCOMP
]);
2960 dst
+= dstComponents
;
2965 GLubyte
*dst
= dest
;
2967 for (i
= 0; i
< n
; i
++) {
2968 CLAMPED_FLOAT_TO_UBYTE(dst
[gDst
], rgba
[i
][GCOMP
]);
2969 dst
+= dstComponents
;
2974 GLubyte
*dst
= dest
;
2976 for (i
= 0; i
< n
; i
++) {
2977 CLAMPED_FLOAT_TO_UBYTE(dst
[bDst
], rgba
[i
][BCOMP
]);
2978 dst
+= dstComponents
;
2983 GLubyte
*dst
= dest
;
2985 for (i
= 0; i
< n
; i
++) {
2986 CLAMPED_FLOAT_TO_UBYTE(dst
[aDst
], rgba
[i
][ACOMP
]);
2987 dst
+= dstComponents
;
2992 GLubyte
*dst
= dest
;
2995 assert(dstComponents
== 1);
2996 for (i
= 0; i
< n
; i
++) {
2997 /* Intensity comes from red channel */
2998 CLAMPED_FLOAT_TO_UBYTE(dst
[i
], rgba
[i
][RCOMP
]);
3003 GLubyte
*dst
= dest
;
3006 for (i
= 0; i
< n
; i
++) {
3007 /* Luminance comes from red channel */
3008 CLAMPED_FLOAT_TO_UBYTE(dst
[0], rgba
[i
][RCOMP
]);
3009 dst
+= dstComponents
;
3019 * Same as _mesa_unpack_color_span_ubyte(), but return GLfloat data
3020 * instead of GLubyte.
3023 _mesa_unpack_color_span_float( struct gl_context
*ctx
,
3024 GLuint n
, GLenum dstFormat
, GLfloat dest
[],
3025 GLenum srcFormat
, GLenum srcType
,
3026 const GLvoid
*source
,
3027 const struct gl_pixelstore_attrib
*srcPacking
,
3028 GLbitfield transferOps
)
3030 ASSERT(dstFormat
== GL_ALPHA
||
3031 dstFormat
== GL_LUMINANCE
||
3032 dstFormat
== GL_LUMINANCE_ALPHA
||
3033 dstFormat
== GL_INTENSITY
||
3034 dstFormat
== GL_RED
||
3035 dstFormat
== GL_RG
||
3036 dstFormat
== GL_RGB
||
3037 dstFormat
== GL_RGBA
);
3039 ASSERT(srcFormat
== GL_RED
||
3040 srcFormat
== GL_GREEN
||
3041 srcFormat
== GL_BLUE
||
3042 srcFormat
== GL_ALPHA
||
3043 srcFormat
== GL_LUMINANCE
||
3044 srcFormat
== GL_LUMINANCE_ALPHA
||
3045 srcFormat
== GL_INTENSITY
||
3046 srcFormat
== GL_RG
||
3047 srcFormat
== GL_RGB
||
3048 srcFormat
== GL_BGR
||
3049 srcFormat
== GL_RGBA
||
3050 srcFormat
== GL_BGRA
||
3051 srcFormat
== GL_ABGR_EXT
||
3052 srcFormat
== GL_RED_INTEGER_EXT
||
3053 srcFormat
== GL_GREEN_INTEGER_EXT
||
3054 srcFormat
== GL_BLUE_INTEGER_EXT
||
3055 srcFormat
== GL_ALPHA_INTEGER_EXT
||
3056 srcFormat
== GL_RG_INTEGER
||
3057 srcFormat
== GL_RGB_INTEGER_EXT
||
3058 srcFormat
== GL_RGBA_INTEGER_EXT
||
3059 srcFormat
== GL_BGR_INTEGER_EXT
||
3060 srcFormat
== GL_BGRA_INTEGER_EXT
||
3061 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
3062 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
||
3063 srcFormat
== GL_COLOR_INDEX
);
3065 ASSERT(srcType
== GL_BITMAP
||
3066 srcType
== GL_UNSIGNED_BYTE
||
3067 srcType
== GL_BYTE
||
3068 srcType
== GL_UNSIGNED_SHORT
||
3069 srcType
== GL_SHORT
||
3070 srcType
== GL_UNSIGNED_INT
||
3071 srcType
== GL_INT
||
3072 srcType
== GL_HALF_FLOAT_ARB
||
3073 srcType
== GL_FLOAT
||
3074 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
3075 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
3076 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
3077 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
3078 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
3079 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
3080 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
3081 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
3082 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
3083 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
3084 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
3085 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
3086 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
3087 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
3089 /* general solution, no special cases, yet */
3091 GLint dstComponents
;
3092 GLint rDst
, gDst
, bDst
, aDst
, lDst
, iDst
;
3093 GLfloat (*rgba
)[4] = malloc(4 * n
* sizeof(GLfloat
));
3094 GLboolean intFormat
= _mesa_is_enum_format_integer(srcFormat
);
3097 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3101 dstComponents
= _mesa_components_in_format( dstFormat
);
3102 /* source & dest image formats should have been error checked by now */
3103 assert(dstComponents
> 0);
3105 /* EXT_texture_integer specifies no transfer ops on integer
3106 * types in the resolved issues section. Just set them to 0
3107 * for integer surfaces.
3113 * Extract image data and convert to RGBA floats
3115 if (srcFormat
== GL_COLOR_INDEX
) {
3116 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
3119 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3124 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
3127 /* Convert indexes to RGBA */
3128 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
3129 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
3131 _mesa_map_ci_to_rgba(ctx
, n
, indexes
, rgba
);
3133 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3134 * with color indexes.
3136 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
3141 /* non-color index data */
3142 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
3143 srcPacking
->SwapBytes
);
3147 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
3150 get_component_indexes(dstFormat
,
3151 &rDst
, &gDst
, &bDst
, &aDst
, &lDst
, &iDst
);
3153 /* Now pack results in the requested dstFormat */
3155 GLfloat
*dst
= dest
;
3157 for (i
= 0; i
< n
; i
++) {
3158 dst
[rDst
] = rgba
[i
][RCOMP
];
3159 dst
+= dstComponents
;
3164 GLfloat
*dst
= dest
;
3166 for (i
= 0; i
< n
; i
++) {
3167 dst
[gDst
] = rgba
[i
][GCOMP
];
3168 dst
+= dstComponents
;
3173 GLfloat
*dst
= dest
;
3175 for (i
= 0; i
< n
; i
++) {
3176 dst
[bDst
] = rgba
[i
][BCOMP
];
3177 dst
+= dstComponents
;
3182 GLfloat
*dst
= dest
;
3184 for (i
= 0; i
< n
; i
++) {
3185 dst
[aDst
] = rgba
[i
][ACOMP
];
3186 dst
+= dstComponents
;
3191 GLfloat
*dst
= dest
;
3194 assert(dstComponents
== 1);
3195 for (i
= 0; i
< n
; i
++) {
3196 /* Intensity comes from red channel */
3197 dst
[i
] = rgba
[i
][RCOMP
];
3202 GLfloat
*dst
= dest
;
3205 for (i
= 0; i
< n
; i
++) {
3206 /* Luminance comes from red channel */
3207 dst
[0] = rgba
[i
][RCOMP
];
3208 dst
+= dstComponents
;
3218 * Same as _mesa_unpack_color_span_ubyte(), but return GLuint data
3219 * instead of GLubyte.
3220 * No pixel transfer ops are applied.
3223 _mesa_unpack_color_span_uint(struct gl_context
*ctx
,
3224 GLuint n
, GLenum dstFormat
, GLuint
*dest
,
3225 GLenum srcFormat
, GLenum srcType
,
3226 const GLvoid
*source
,
3227 const struct gl_pixelstore_attrib
*srcPacking
)
3229 GLuint (*rgba
)[4] = malloc(n
* 4 * sizeof(GLfloat
));
3232 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3236 ASSERT(dstFormat
== GL_ALPHA
||
3237 dstFormat
== GL_LUMINANCE
||
3238 dstFormat
== GL_LUMINANCE_ALPHA
||
3239 dstFormat
== GL_INTENSITY
||
3240 dstFormat
== GL_RED
||
3241 dstFormat
== GL_RG
||
3242 dstFormat
== GL_RGB
||
3243 dstFormat
== GL_RGBA
);
3245 ASSERT(srcFormat
== GL_RED
||
3246 srcFormat
== GL_GREEN
||
3247 srcFormat
== GL_BLUE
||
3248 srcFormat
== GL_ALPHA
||
3249 srcFormat
== GL_LUMINANCE
||
3250 srcFormat
== GL_LUMINANCE_ALPHA
||
3251 srcFormat
== GL_INTENSITY
||
3252 srcFormat
== GL_RG
||
3253 srcFormat
== GL_RGB
||
3254 srcFormat
== GL_BGR
||
3255 srcFormat
== GL_RGBA
||
3256 srcFormat
== GL_BGRA
||
3257 srcFormat
== GL_ABGR_EXT
||
3258 srcFormat
== GL_RED_INTEGER_EXT
||
3259 srcFormat
== GL_GREEN_INTEGER_EXT
||
3260 srcFormat
== GL_BLUE_INTEGER_EXT
||
3261 srcFormat
== GL_ALPHA_INTEGER_EXT
||
3262 srcFormat
== GL_RG_INTEGER
||
3263 srcFormat
== GL_RGB_INTEGER_EXT
||
3264 srcFormat
== GL_RGBA_INTEGER_EXT
||
3265 srcFormat
== GL_BGR_INTEGER_EXT
||
3266 srcFormat
== GL_BGRA_INTEGER_EXT
||
3267 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
3268 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
3270 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
3271 srcType
== GL_BYTE
||
3272 srcType
== GL_UNSIGNED_SHORT
||
3273 srcType
== GL_SHORT
||
3274 srcType
== GL_UNSIGNED_INT
||
3275 srcType
== GL_INT
||
3276 srcType
== GL_HALF_FLOAT_ARB
||
3277 srcType
== GL_FLOAT
||
3278 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
3279 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
3280 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
3281 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
3282 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
3283 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
3284 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
3285 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
3286 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
3287 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
3288 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
3289 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
3290 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
3291 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
3294 /* Extract image data as uint[4] pixels */
3295 extract_uint_rgba(n
, rgba
, srcFormat
, srcType
, source
,
3296 srcPacking
->SwapBytes
);
3298 if (dstFormat
== GL_RGBA
) {
3300 memcpy(dest
, rgba
, 4 * sizeof(GLuint
) * n
);
3304 GLint rDst
, gDst
, bDst
, aDst
, lDst
, iDst
;
3305 GLint dstComponents
= _mesa_components_in_format( dstFormat
);
3307 assert(dstComponents
> 0);
3309 get_component_indexes(dstFormat
,
3310 &rDst
, &gDst
, &bDst
, &aDst
, &lDst
, &iDst
);
3312 /* Now pack values in the requested dest format */
3316 for (i
= 0; i
< n
; i
++) {
3317 dst
[rDst
] = rgba
[i
][RCOMP
];
3318 dst
+= dstComponents
;
3325 for (i
= 0; i
< n
; i
++) {
3326 dst
[gDst
] = rgba
[i
][GCOMP
];
3327 dst
+= dstComponents
;
3334 for (i
= 0; i
< n
; i
++) {
3335 dst
[bDst
] = rgba
[i
][BCOMP
];
3336 dst
+= dstComponents
;
3343 for (i
= 0; i
< n
; i
++) {
3344 dst
[aDst
] = rgba
[i
][ACOMP
];
3345 dst
+= dstComponents
;
3353 assert(dstComponents
== 1);
3354 for (i
= 0; i
< n
; i
++) {
3355 /* Intensity comes from red channel */
3356 dst
[i
] = rgba
[i
][RCOMP
];
3364 for (i
= 0; i
< n
; i
++) {
3365 /* Luminance comes from red channel */
3366 dst
[0] = rgba
[i
][RCOMP
];
3367 dst
+= dstComponents
;
3377 * Unpack a row of color index data from a client buffer according to
3378 * the pixel unpacking parameters.
3379 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
3381 * Args: ctx - the context
3382 * n - number of pixels
3383 * dstType - destination data type
3384 * dest - destination array
3385 * srcType - source pixel type
3386 * source - source data pointer
3387 * srcPacking - pixel unpacking parameters
3388 * transferOps - the pixel transfer operations to apply
3391 _mesa_unpack_index_span( struct gl_context
*ctx
, GLuint n
,
3392 GLenum dstType
, GLvoid
*dest
,
3393 GLenum srcType
, const GLvoid
*source
,
3394 const struct gl_pixelstore_attrib
*srcPacking
,
3395 GLbitfield transferOps
)
3397 ASSERT(srcType
== GL_BITMAP
||
3398 srcType
== GL_UNSIGNED_BYTE
||
3399 srcType
== GL_BYTE
||
3400 srcType
== GL_UNSIGNED_SHORT
||
3401 srcType
== GL_SHORT
||
3402 srcType
== GL_UNSIGNED_INT
||
3403 srcType
== GL_INT
||
3404 srcType
== GL_HALF_FLOAT_ARB
||
3405 srcType
== GL_FLOAT
);
3407 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
3408 dstType
== GL_UNSIGNED_SHORT
||
3409 dstType
== GL_UNSIGNED_INT
);
3412 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
3415 * Try simple cases first
3417 if (transferOps
== 0 && srcType
== GL_UNSIGNED_BYTE
3418 && dstType
== GL_UNSIGNED_BYTE
) {
3419 memcpy(dest
, source
, n
* sizeof(GLubyte
));
3421 else if (transferOps
== 0 && srcType
== GL_UNSIGNED_INT
3422 && dstType
== GL_UNSIGNED_INT
&& !srcPacking
->SwapBytes
) {
3423 memcpy(dest
, source
, n
* sizeof(GLuint
));
3429 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
3432 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3436 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
3440 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
3442 /* convert to dest type */
3444 case GL_UNSIGNED_BYTE
:
3446 GLubyte
*dst
= (GLubyte
*) dest
;
3448 for (i
= 0; i
< n
; i
++) {
3449 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
3453 case GL_UNSIGNED_SHORT
:
3455 GLuint
*dst
= (GLuint
*) dest
;
3457 for (i
= 0; i
< n
; i
++) {
3458 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
3462 case GL_UNSIGNED_INT
:
3463 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
3466 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
3475 _mesa_pack_index_span( struct gl_context
*ctx
, GLuint n
,
3476 GLenum dstType
, GLvoid
*dest
, const GLuint
*source
,
3477 const struct gl_pixelstore_attrib
*dstPacking
,
3478 GLbitfield transferOps
)
3480 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
3483 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
3487 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
3489 if (transferOps
& (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
)) {
3490 /* make a copy of input */
3491 memcpy(indexes
, source
, n
* sizeof(GLuint
));
3492 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
3497 case GL_UNSIGNED_BYTE
:
3499 GLubyte
*dst
= (GLubyte
*) dest
;
3501 for (i
= 0; i
< n
; i
++) {
3502 *dst
++ = (GLubyte
) source
[i
];
3508 GLbyte
*dst
= (GLbyte
*) dest
;
3510 for (i
= 0; i
< n
; i
++) {
3511 dst
[i
] = (GLbyte
) source
[i
];
3515 case GL_UNSIGNED_SHORT
:
3517 GLushort
*dst
= (GLushort
*) dest
;
3519 for (i
= 0; i
< n
; i
++) {
3520 dst
[i
] = (GLushort
) source
[i
];
3522 if (dstPacking
->SwapBytes
) {
3523 _mesa_swap2( (GLushort
*) dst
, n
);
3529 GLshort
*dst
= (GLshort
*) dest
;
3531 for (i
= 0; i
< n
; i
++) {
3532 dst
[i
] = (GLshort
) source
[i
];
3534 if (dstPacking
->SwapBytes
) {
3535 _mesa_swap2( (GLushort
*) dst
, n
);
3539 case GL_UNSIGNED_INT
:
3541 GLuint
*dst
= (GLuint
*) dest
;
3543 for (i
= 0; i
< n
; i
++) {
3544 dst
[i
] = (GLuint
) source
[i
];
3546 if (dstPacking
->SwapBytes
) {
3547 _mesa_swap4( (GLuint
*) dst
, n
);
3553 GLint
*dst
= (GLint
*) dest
;
3555 for (i
= 0; i
< n
; i
++) {
3556 dst
[i
] = (GLint
) source
[i
];
3558 if (dstPacking
->SwapBytes
) {
3559 _mesa_swap4( (GLuint
*) dst
, n
);
3565 GLfloat
*dst
= (GLfloat
*) dest
;
3567 for (i
= 0; i
< n
; i
++) {
3568 dst
[i
] = (GLfloat
) source
[i
];
3570 if (dstPacking
->SwapBytes
) {
3571 _mesa_swap4( (GLuint
*) dst
, n
);
3575 case GL_HALF_FLOAT_ARB
:
3577 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
3579 for (i
= 0; i
< n
; i
++) {
3580 dst
[i
] = _mesa_float_to_half((GLfloat
) source
[i
]);
3582 if (dstPacking
->SwapBytes
) {
3583 _mesa_swap2( (GLushort
*) dst
, n
);
3588 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
3596 * Unpack a row of stencil data from a client buffer according to
3597 * the pixel unpacking parameters.
3598 * This is (or will be) used by glDrawPixels
3600 * Args: ctx - the context
3601 * n - number of pixels
3602 * dstType - destination data type
3603 * dest - destination array
3604 * srcType - source pixel type
3605 * source - source data pointer
3606 * srcPacking - pixel unpacking parameters
3607 * transferOps - apply offset/bias/lookup ops?
3610 _mesa_unpack_stencil_span( struct gl_context
*ctx
, GLuint n
,
3611 GLenum dstType
, GLvoid
*dest
,
3612 GLenum srcType
, const GLvoid
*source
,
3613 const struct gl_pixelstore_attrib
*srcPacking
,
3614 GLbitfield transferOps
)
3616 ASSERT(srcType
== GL_BITMAP
||
3617 srcType
== GL_UNSIGNED_BYTE
||
3618 srcType
== GL_BYTE
||
3619 srcType
== GL_UNSIGNED_SHORT
||
3620 srcType
== GL_SHORT
||
3621 srcType
== GL_UNSIGNED_INT
||
3622 srcType
== GL_INT
||
3623 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
3624 srcType
== GL_HALF_FLOAT_ARB
||
3625 srcType
== GL_FLOAT
||
3626 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
3628 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
3629 dstType
== GL_UNSIGNED_SHORT
||
3630 dstType
== GL_UNSIGNED_INT
||
3631 dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
3633 /* only shift and offset apply to stencil */
3634 transferOps
&= IMAGE_SHIFT_OFFSET_BIT
;
3637 * Try simple cases first
3639 if (transferOps
== 0 &&
3640 !ctx
->Pixel
.MapStencilFlag
&&
3641 srcType
== GL_UNSIGNED_BYTE
&&
3642 dstType
== GL_UNSIGNED_BYTE
) {
3643 memcpy(dest
, source
, n
* sizeof(GLubyte
));
3645 else if (transferOps
== 0 &&
3646 !ctx
->Pixel
.MapStencilFlag
&&
3647 srcType
== GL_UNSIGNED_INT
&&
3648 dstType
== GL_UNSIGNED_INT
&&
3649 !srcPacking
->SwapBytes
) {
3650 memcpy(dest
, source
, n
* sizeof(GLuint
));
3656 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
3659 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil unpacking");
3663 extract_uint_indexes(n
, indexes
, GL_STENCIL_INDEX
, srcType
, source
,
3666 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
3667 /* shift and offset indexes */
3668 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
3671 if (ctx
->Pixel
.MapStencilFlag
) {
3672 /* Apply stencil lookup table */
3673 const GLuint mask
= ctx
->PixelMaps
.StoS
.Size
- 1;
3675 for (i
= 0; i
< n
; i
++) {
3676 indexes
[i
] = (GLuint
)ctx
->PixelMaps
.StoS
.Map
[ indexes
[i
] & mask
];
3680 /* convert to dest type */
3682 case GL_UNSIGNED_BYTE
:
3684 GLubyte
*dst
= (GLubyte
*) dest
;
3686 for (i
= 0; i
< n
; i
++) {
3687 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
3691 case GL_UNSIGNED_SHORT
:
3693 GLuint
*dst
= (GLuint
*) dest
;
3695 for (i
= 0; i
< n
; i
++) {
3696 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
3700 case GL_UNSIGNED_INT
:
3701 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
3703 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
3705 GLuint
*dst
= (GLuint
*) dest
;
3707 for (i
= 0; i
< n
; i
++) {
3708 dst
[i
*2+1] = indexes
[i
] & 0xff; /* lower 8 bits */
3713 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
3722 _mesa_pack_stencil_span( struct gl_context
*ctx
, GLuint n
,
3723 GLenum dstType
, GLvoid
*dest
, const GLubyte
*source
,
3724 const struct gl_pixelstore_attrib
*dstPacking
)
3726 GLubyte
*stencil
= malloc(n
* sizeof(GLubyte
));
3729 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil packing");
3733 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
3734 ctx
->Pixel
.MapStencilFlag
) {
3735 /* make a copy of input */
3736 memcpy(stencil
, source
, n
* sizeof(GLubyte
));
3737 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencil
);
3742 case GL_UNSIGNED_BYTE
:
3743 memcpy(dest
, source
, n
);
3747 GLbyte
*dst
= (GLbyte
*) dest
;
3750 dst
[i
] = (GLbyte
) (source
[i
] & 0x7f);
3754 case GL_UNSIGNED_SHORT
:
3756 GLushort
*dst
= (GLushort
*) dest
;
3759 dst
[i
] = (GLushort
) source
[i
];
3761 if (dstPacking
->SwapBytes
) {
3762 _mesa_swap2( (GLushort
*) dst
, n
);
3768 GLshort
*dst
= (GLshort
*) dest
;
3771 dst
[i
] = (GLshort
) source
[i
];
3773 if (dstPacking
->SwapBytes
) {
3774 _mesa_swap2( (GLushort
*) dst
, n
);
3778 case GL_UNSIGNED_INT
:
3780 GLuint
*dst
= (GLuint
*) dest
;
3783 dst
[i
] = (GLuint
) source
[i
];
3785 if (dstPacking
->SwapBytes
) {
3786 _mesa_swap4( (GLuint
*) dst
, n
);
3792 GLint
*dst
= (GLint
*) dest
;
3795 dst
[i
] = (GLint
) source
[i
];
3797 if (dstPacking
->SwapBytes
) {
3798 _mesa_swap4( (GLuint
*) dst
, n
);
3804 GLfloat
*dst
= (GLfloat
*) dest
;
3807 dst
[i
] = (GLfloat
) source
[i
];
3809 if (dstPacking
->SwapBytes
) {
3810 _mesa_swap4( (GLuint
*) dst
, n
);
3814 case GL_HALF_FLOAT_ARB
:
3816 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
3819 dst
[i
] = _mesa_float_to_half( (float) source
[i
] );
3821 if (dstPacking
->SwapBytes
) {
3822 _mesa_swap2( (GLushort
*) dst
, n
);
3827 if (dstPacking
->LsbFirst
) {
3828 GLubyte
*dst
= (GLubyte
*) dest
;
3831 for (i
= 0; i
< n
; i
++) {
3834 *dst
|= ((source
[i
] != 0) << shift
);
3843 GLubyte
*dst
= (GLubyte
*) dest
;
3846 for (i
= 0; i
< n
; i
++) {
3849 *dst
|= ((source
[i
] != 0) << shift
);
3859 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
3865 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
3868 const GLTYPE *src = (const GLTYPE *)source; \
3869 for (i = 0; i < n; i++) { \
3870 GLTYPE value = src[i]; \
3871 if (srcPacking->SwapBytes) { \
3872 if (sizeof(GLTYPE) == 2) { \
3874 } else if (sizeof(GLTYPE) == 4) { \
3878 depthValues[i] = GLTYPE2FLOAT(value); \
3884 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
3885 * or GLfloat values.
3886 * The glPixelTransfer (scale/bias) params will be applied.
3888 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
3889 * \param depthMax max value for returned GLushort or GLuint values
3890 * (ignored for GLfloat).
3893 _mesa_unpack_depth_span( struct gl_context
*ctx
, GLuint n
,
3894 GLenum dstType
, GLvoid
*dest
, GLuint depthMax
,
3895 GLenum srcType
, const GLvoid
*source
,
3896 const struct gl_pixelstore_attrib
*srcPacking
)
3898 GLfloat
*depthTemp
= NULL
, *depthValues
;
3899 GLboolean needClamp
= GL_FALSE
;
3901 /* Look for special cases first.
3902 * Not only are these faster, they're less prone to numeric conversion
3903 * problems. Otherwise, converting from an int type to a float then
3904 * back to an int type can introduce errors that will show up as
3905 * artifacts in things like depth peeling which uses glCopyTexImage.
3907 if (ctx
->Pixel
.DepthScale
== 1.0 && ctx
->Pixel
.DepthBias
== 0.0) {
3908 if (srcType
== GL_UNSIGNED_INT
&& dstType
== GL_UNSIGNED_SHORT
) {
3909 const GLuint
*src
= (const GLuint
*) source
;
3910 GLushort
*dst
= (GLushort
*) dest
;
3912 for (i
= 0; i
< n
; i
++) {
3913 dst
[i
] = src
[i
] >> 16;
3917 if (srcType
== GL_UNSIGNED_SHORT
3918 && dstType
== GL_UNSIGNED_INT
3919 && depthMax
== 0xffffffff) {
3920 const GLushort
*src
= (const GLushort
*) source
;
3921 GLuint
*dst
= (GLuint
*) dest
;
3923 for (i
= 0; i
< n
; i
++) {
3924 dst
[i
] = src
[i
] | (src
[i
] << 16);
3928 if (srcType
== GL_UNSIGNED_INT_24_8
3929 && dstType
== GL_UNSIGNED_INT
3930 && depthMax
== 0xffffff) {
3931 const GLuint
*src
= (const GLuint
*) source
;
3932 GLuint
*dst
= (GLuint
*) dest
;
3934 for (i
= 0; i
< n
; i
++) {
3935 dst
[i
] = src
[i
] >> 8;
3939 /* XXX may want to add additional cases here someday */
3942 /* general case path follows */
3944 if (dstType
== GL_FLOAT
) {
3945 depthValues
= (GLfloat
*) dest
;
3948 depthTemp
= malloc(n
* sizeof(GLfloat
));
3950 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3954 depthValues
= depthTemp
;
3957 /* Convert incoming values to GLfloat. Some conversions will require
3962 DEPTH_VALUES(GLbyte
, BYTE_TO_FLOATZ
);
3963 needClamp
= GL_TRUE
;
3965 case GL_UNSIGNED_BYTE
:
3966 DEPTH_VALUES(GLubyte
, UBYTE_TO_FLOAT
);
3969 DEPTH_VALUES(GLshort
, SHORT_TO_FLOATZ
);
3970 needClamp
= GL_TRUE
;
3972 case GL_UNSIGNED_SHORT
:
3973 DEPTH_VALUES(GLushort
, USHORT_TO_FLOAT
);
3976 DEPTH_VALUES(GLint
, INT_TO_FLOAT
);
3977 needClamp
= GL_TRUE
;
3979 case GL_UNSIGNED_INT
:
3980 DEPTH_VALUES(GLuint
, UINT_TO_FLOAT
);
3982 case GL_UNSIGNED_INT_24_8_EXT
: /* GL_EXT_packed_depth_stencil */
3983 if (dstType
== GL_UNSIGNED_INT_24_8_EXT
&&
3984 depthMax
== 0xffffff &&
3985 ctx
->Pixel
.DepthScale
== 1.0 &&
3986 ctx
->Pixel
.DepthBias
== 0.0) {
3987 const GLuint
*src
= (const GLuint
*) source
;
3988 GLuint
*zValues
= (GLuint
*) dest
;
3990 for (i
= 0; i
< n
; i
++) {
3991 GLuint value
= src
[i
];
3992 if (srcPacking
->SwapBytes
) {
3995 zValues
[i
] = value
& 0xffffff00;
4001 const GLuint
*src
= (const GLuint
*) source
;
4002 const GLfloat scale
= 1.0f
/ 0xffffff;
4004 for (i
= 0; i
< n
; i
++) {
4005 GLuint value
= src
[i
];
4006 if (srcPacking
->SwapBytes
) {
4009 depthValues
[i
] = (value
>> 8) * scale
;
4013 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
4016 const GLfloat
*src
= (const GLfloat
*)source
;
4017 for (i
= 0; i
< n
; i
++) {
4018 GLfloat value
= src
[i
* 2];
4019 if (srcPacking
->SwapBytes
) {
4022 depthValues
[i
] = value
;
4024 needClamp
= GL_TRUE
;
4028 DEPTH_VALUES(GLfloat
, 1*);
4029 needClamp
= GL_TRUE
;
4031 case GL_HALF_FLOAT_ARB
:
4034 const GLhalfARB
*src
= (const GLhalfARB
*) source
;
4035 for (i
= 0; i
< n
; i
++) {
4036 GLhalfARB value
= src
[i
];
4037 if (srcPacking
->SwapBytes
) {
4040 depthValues
[i
] = _mesa_half_to_float(value
);
4042 needClamp
= GL_TRUE
;
4046 _mesa_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
4051 /* apply depth scale and bias */
4053 const GLfloat scale
= ctx
->Pixel
.DepthScale
;
4054 const GLfloat bias
= ctx
->Pixel
.DepthBias
;
4055 if (scale
!= 1.0 || bias
!= 0.0) {
4057 for (i
= 0; i
< n
; i
++) {
4058 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
4060 needClamp
= GL_TRUE
;
4064 /* clamp to [0, 1] */
4067 for (i
= 0; i
< n
; i
++) {
4068 depthValues
[i
] = (GLfloat
)CLAMP(depthValues
[i
], 0.0, 1.0);
4073 * Convert values to dstType
4075 if (dstType
== GL_UNSIGNED_INT
) {
4076 GLuint
*zValues
= (GLuint
*) dest
;
4078 if (depthMax
<= 0xffffff) {
4079 /* no overflow worries */
4080 for (i
= 0; i
< n
; i
++) {
4081 zValues
[i
] = (GLuint
) (depthValues
[i
] * (GLfloat
) depthMax
);
4085 /* need to use double precision to prevent overflow problems */
4086 for (i
= 0; i
< n
; i
++) {
4087 GLdouble z
= depthValues
[i
] * (GLdouble
) depthMax
;
4088 if (z
>= (GLdouble
) 0xffffffff)
4089 zValues
[i
] = 0xffffffff;
4091 zValues
[i
] = (GLuint
) z
;
4095 else if (dstType
== GL_UNSIGNED_SHORT
) {
4096 GLushort
*zValues
= (GLushort
*) dest
;
4098 ASSERT(depthMax
<= 0xffff);
4099 for (i
= 0; i
< n
; i
++) {
4100 zValues
[i
] = (GLushort
) (depthValues
[i
] * (GLfloat
) depthMax
);
4103 else if (dstType
== GL_FLOAT
) {
4104 /* Nothing to do. depthValues is pointing to dest. */
4106 else if (dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
) {
4107 GLfloat
*zValues
= (GLfloat
*) dest
;
4109 for (i
= 0; i
< n
; i
++) {
4110 zValues
[i
*2] = depthValues
[i
];
4122 * Pack an array of depth values. The values are floats in [0,1].
4125 _mesa_pack_depth_span( struct gl_context
*ctx
, GLuint n
, GLvoid
*dest
,
4126 GLenum dstType
, const GLfloat
*depthSpan
,
4127 const struct gl_pixelstore_attrib
*dstPacking
)
4129 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
4131 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
4135 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
4136 memcpy(depthCopy
, depthSpan
, n
* sizeof(GLfloat
));
4137 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
4138 depthSpan
= depthCopy
;
4142 case GL_UNSIGNED_BYTE
:
4144 GLubyte
*dst
= (GLubyte
*) dest
;
4146 for (i
= 0; i
< n
; i
++) {
4147 dst
[i
] = FLOAT_TO_UBYTE( depthSpan
[i
] );
4153 GLbyte
*dst
= (GLbyte
*) dest
;
4155 for (i
= 0; i
< n
; i
++) {
4156 dst
[i
] = FLOAT_TO_BYTE( depthSpan
[i
] );
4160 case GL_UNSIGNED_SHORT
:
4162 GLushort
*dst
= (GLushort
*) dest
;
4164 for (i
= 0; i
< n
; i
++) {
4165 CLAMPED_FLOAT_TO_USHORT(dst
[i
], depthSpan
[i
]);
4167 if (dstPacking
->SwapBytes
) {
4168 _mesa_swap2( (GLushort
*) dst
, n
);
4174 GLshort
*dst
= (GLshort
*) dest
;
4176 for (i
= 0; i
< n
; i
++) {
4177 dst
[i
] = FLOAT_TO_SHORT( depthSpan
[i
] );
4179 if (dstPacking
->SwapBytes
) {
4180 _mesa_swap2( (GLushort
*) dst
, n
);
4184 case GL_UNSIGNED_INT
:
4186 GLuint
*dst
= (GLuint
*) dest
;
4188 for (i
= 0; i
< n
; i
++) {
4189 dst
[i
] = FLOAT_TO_UINT( depthSpan
[i
] );
4191 if (dstPacking
->SwapBytes
) {
4192 _mesa_swap4( (GLuint
*) dst
, n
);
4198 GLint
*dst
= (GLint
*) dest
;
4200 for (i
= 0; i
< n
; i
++) {
4201 dst
[i
] = FLOAT_TO_INT( depthSpan
[i
] );
4203 if (dstPacking
->SwapBytes
) {
4204 _mesa_swap4( (GLuint
*) dst
, n
);
4210 GLfloat
*dst
= (GLfloat
*) dest
;
4212 for (i
= 0; i
< n
; i
++) {
4213 dst
[i
] = depthSpan
[i
];
4215 if (dstPacking
->SwapBytes
) {
4216 _mesa_swap4( (GLuint
*) dst
, n
);
4220 case GL_HALF_FLOAT_ARB
:
4222 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
4224 for (i
= 0; i
< n
; i
++) {
4225 dst
[i
] = _mesa_float_to_half(depthSpan
[i
]);
4227 if (dstPacking
->SwapBytes
) {
4228 _mesa_swap2( (GLushort
*) dst
, n
);
4233 _mesa_problem(ctx
, "bad type in _mesa_pack_depth_span");
4242 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
4245 _mesa_pack_depth_stencil_span(struct gl_context
*ctx
,GLuint n
,
4246 GLenum dstType
, GLuint
*dest
,
4247 const GLfloat
*depthVals
,
4248 const GLubyte
*stencilVals
,
4249 const struct gl_pixelstore_attrib
*dstPacking
)
4251 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
4252 GLubyte
*stencilCopy
= malloc(n
* sizeof(GLubyte
));
4255 if (!depthCopy
|| !stencilCopy
) {
4256 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
4262 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
4263 memcpy(depthCopy
, depthVals
, n
* sizeof(GLfloat
));
4264 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
4265 depthVals
= depthCopy
;
4268 if (ctx
->Pixel
.IndexShift
||
4269 ctx
->Pixel
.IndexOffset
||
4270 ctx
->Pixel
.MapStencilFlag
) {
4271 memcpy(stencilCopy
, stencilVals
, n
* sizeof(GLubyte
));
4272 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencilCopy
);
4273 stencilVals
= stencilCopy
;
4277 case GL_UNSIGNED_INT_24_8
:
4278 for (i
= 0; i
< n
; i
++) {
4279 GLuint z
= (GLuint
) (depthVals
[i
] * 0xffffff);
4280 dest
[i
] = (z
<< 8) | (stencilVals
[i
] & 0xff);
4283 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
4284 for (i
= 0; i
< n
; i
++) {
4285 ((GLfloat
*)dest
)[i
*2] = depthVals
[i
];
4286 dest
[i
*2+1] = stencilVals
[i
] & 0xff;
4291 if (dstPacking
->SwapBytes
) {
4292 _mesa_swap4(dest
, n
);
4303 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
4304 * Return all image data in a contiguous block. This is used when we
4305 * compile glDrawPixels, glTexImage, etc into a display list. We
4306 * need a copy of the data in a standard format.
4309 _mesa_unpack_image( GLuint dimensions
,
4310 GLsizei width
, GLsizei height
, GLsizei depth
,
4311 GLenum format
, GLenum type
, const GLvoid
*pixels
,
4312 const struct gl_pixelstore_attrib
*unpack
)
4314 GLint bytesPerRow
, compsPerRow
;
4315 GLboolean flipBytes
, swap2
, swap4
;
4318 return NULL
; /* not necessarily an error */
4320 if (width
<= 0 || height
<= 0 || depth
<= 0)
4321 return NULL
; /* generate error later */
4323 if (type
== GL_BITMAP
) {
4324 bytesPerRow
= (width
+ 7) >> 3;
4325 flipBytes
= unpack
->LsbFirst
;
4326 swap2
= swap4
= GL_FALSE
;
4330 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
4331 GLint components
= _mesa_components_in_format(format
);
4334 if (_mesa_type_is_packed(type
))
4337 if (bytesPerPixel
<= 0 || components
<= 0)
4338 return NULL
; /* bad format or type. generate error later */
4339 bytesPerRow
= bytesPerPixel
* width
;
4340 bytesPerComp
= bytesPerPixel
/ components
;
4341 flipBytes
= GL_FALSE
;
4342 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
4343 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
4344 compsPerRow
= components
* width
;
4345 assert(compsPerRow
>= width
);
4350 = malloc(bytesPerRow
* height
* depth
);
4354 return NULL
; /* generate GL_OUT_OF_MEMORY later */
4357 for (img
= 0; img
< depth
; img
++) {
4358 for (row
= 0; row
< height
; row
++) {
4359 const GLvoid
*src
= _mesa_image_address(dimensions
, unpack
, pixels
,
4360 width
, height
, format
, type
, img
, row
, 0);
4362 if ((type
== GL_BITMAP
) && (unpack
->SkipPixels
& 0x7)) {
4364 flipBytes
= GL_FALSE
;
4365 if (unpack
->LsbFirst
) {
4366 GLubyte srcMask
= 1 << (unpack
->SkipPixels
& 0x7);
4367 GLubyte dstMask
= 128;
4368 const GLubyte
*s
= src
;
4371 for (i
= 0; i
< width
; i
++) {
4375 if (srcMask
== 128) {
4380 srcMask
= srcMask
<< 1;
4388 dstMask
= dstMask
>> 1;
4393 GLubyte srcMask
= 128 >> (unpack
->SkipPixels
& 0x7);
4394 GLubyte dstMask
= 128;
4395 const GLubyte
*s
= src
;
4398 for (i
= 0; i
< width
; i
++) {
4407 srcMask
= srcMask
>> 1;
4415 dstMask
= dstMask
>> 1;
4421 memcpy(dst
, src
, bytesPerRow
);
4424 /* byte flipping/swapping */
4426 flip_bytes((GLubyte
*) dst
, bytesPerRow
);
4429 _mesa_swap2((GLushort
*) dst
, compsPerRow
);
4432 _mesa_swap4((GLuint
*) dst
, compsPerRow
);
4444 * If we unpack colors from a luminance surface, we'll get pixel colors
4445 * such as (l, l, l, a).
4446 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
4447 * function will compute L=R+G+B before packing. The net effect is we'll
4448 * accidentally store luminance values = 3*l.
4449 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
4451 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
4454 * Finally, we also need to do this when the actual surface format does
4455 * not match the logical surface format. For example, suppose the user
4456 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
4457 * Again, we'll get pixel values like (l,l,l,a).
4460 _mesa_rebase_rgba_float(GLuint n
, GLfloat rgba
[][4], GLenum baseFormat
)
4464 switch (baseFormat
) {
4466 for (i
= 0; i
< n
; i
++) {
4467 rgba
[i
][RCOMP
] = 0.0F
;
4468 rgba
[i
][GCOMP
] = 0.0F
;
4469 rgba
[i
][BCOMP
] = 0.0F
;
4475 for (i
= 0; i
< n
; i
++) {
4476 rgba
[i
][GCOMP
] = 0.0F
;
4477 rgba
[i
][BCOMP
] = 0.0F
;
4478 rgba
[i
][ACOMP
] = 1.0F
;
4481 case GL_LUMINANCE_ALPHA
:
4482 for (i
= 0; i
< n
; i
++) {
4483 rgba
[i
][GCOMP
] = 0.0F
;
4484 rgba
[i
][BCOMP
] = 0.0F
;
4488 for (i
= 0; i
< n
; i
++) {
4489 rgba
[i
][ACOMP
] = 1.0F
;
4493 for (i
= 0; i
< n
; i
++) {
4494 rgba
[i
][BCOMP
] = 0.0F
;
4495 rgba
[i
][ACOMP
] = 1.0F
;
4499 for (i
= 0; i
< n
; i
++) {
4500 rgba
[i
][GCOMP
] = 0.0F
;
4501 rgba
[i
][BCOMP
] = 0.0F
;
4502 rgba
[i
][ACOMP
] = 1.0F
;
4514 * As above, but GLuint components.
4517 _mesa_rebase_rgba_uint(GLuint n
, GLuint rgba
[][4], GLenum baseFormat
)
4521 switch (baseFormat
) {
4523 for (i
= 0; i
< n
; i
++) {
4532 for (i
= 0; i
< n
; i
++) {
4538 case GL_LUMINANCE_ALPHA
:
4539 for (i
= 0; i
< n
; i
++) {
4545 for (i
= 0; i
< n
; i
++) {
4550 for (i
= 0; i
< n
; i
++) {
4556 for (i
= 0; i
< n
; i
++) {