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_image(2, 32, 32, 1, GL_COLOR_INDEX
,
104 GL_BITMAP
, pattern
, unpacking
);
106 /* Convert pattern from GLubytes to GLuints and handle big/little
111 for (i
= 0; i
< 32; i
++) {
112 dest
[i
] = (p
[0] << 24)
124 * Pack polygon stipple into user memory given current pixel packing
128 _mesa_pack_polygon_stipple( const GLuint pattern
[32], GLubyte
*dest
,
129 const struct gl_pixelstore_attrib
*packing
)
131 /* Convert pattern from GLuints to GLubytes to handle big/little
132 * endian differences.
136 for (i
= 0; i
< 32; i
++) {
137 ptrn
[i
* 4 + 0] = (GLubyte
) ((pattern
[i
] >> 24) & 0xff);
138 ptrn
[i
* 4 + 1] = (GLubyte
) ((pattern
[i
] >> 16) & 0xff);
139 ptrn
[i
* 4 + 2] = (GLubyte
) ((pattern
[i
] >> 8 ) & 0xff);
140 ptrn
[i
* 4 + 3] = (GLubyte
) ((pattern
[i
] ) & 0xff);
143 _mesa_pack_bitmap(32, 32, ptrn
, dest
, packing
);
151 _mesa_pack_bitmap( GLint width
, GLint height
, const GLubyte
*source
,
152 GLubyte
*dest
, const struct gl_pixelstore_attrib
*packing
)
154 GLint row
, width_in_bytes
;
160 width_in_bytes
= CEILING( width
, 8 );
162 for (row
= 0; row
< height
; row
++) {
163 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, dest
,
164 width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
168 if ((packing
->SkipPixels
& 7) == 0) {
169 memcpy( dst
, src
, width_in_bytes
);
170 if (packing
->LsbFirst
) {
171 flip_bytes( dst
, width_in_bytes
);
175 /* handling SkipPixels is a bit tricky (no pun intended!) */
177 if (packing
->LsbFirst
) {
178 GLubyte srcMask
= 128;
179 GLubyte dstMask
= 1 << (packing
->SkipPixels
& 0x7);
180 const GLubyte
*s
= src
;
183 for (i
= 0; i
< width
; i
++) {
192 srcMask
= srcMask
>> 1;
194 if (dstMask
== 128) {
200 dstMask
= dstMask
<< 1;
205 GLubyte srcMask
= 128;
206 GLubyte dstMask
= 128 >> (packing
->SkipPixels
& 0x7);
207 const GLubyte
*s
= src
;
210 for (i
= 0; i
< width
; i
++) {
219 srcMask
= srcMask
>> 1;
227 dstMask
= dstMask
>> 1;
232 src
+= width_in_bytes
;
238 * Get indexes of color components for a basic color format, such as
239 * GL_RGBA, GL_RED, GL_LUMINANCE_ALPHA, etc. Return -1 for indexes
243 get_component_indexes(GLenum format
,
248 GLint
*luminanceIndex
,
249 GLint
*intensityIndex
)
255 *luminanceIndex
= -1;
256 *intensityIndex
= -1;
260 case GL_LUMINANCE_INTEGER_EXT
:
263 case GL_LUMINANCE_ALPHA
:
264 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
272 case GL_RED_INTEGER_EXT
:
276 case GL_GREEN_INTEGER_EXT
:
280 case GL_BLUE_INTEGER_EXT
:
284 case GL_ALPHA_INTEGER_EXT
:
293 case GL_RGB_INTEGER_EXT
:
299 case GL_BGR_INTEGER_EXT
:
305 case GL_RGBA_INTEGER_EXT
:
312 case GL_BGRA_INTEGER
:
325 assert(0 && "bad format in get_component_indexes()");
332 * For small integer types, return the min and max possible values.
333 * Used for clamping floats to unscaled integer types.
334 * \return GL_TRUE if type is handled, GL_FALSE otherwise.
337 get_type_min_max(GLenum type
, GLfloat
*min
, GLfloat
*max
)
344 case GL_UNSIGNED_BYTE
:
352 case GL_UNSIGNED_SHORT
:
361 /* Customization of unsigned integer packing.
363 #define SRC_TYPE GLuint
365 #define DST_TYPE GLuint
366 #define SRC_CONVERT(x) (x)
367 #define FN_NAME pack_uint_from_uint_rgba
368 #include "pack_tmp.h"
373 #define DST_TYPE GLint
374 #define SRC_CONVERT(x) MIN2(x, 0x7fffffff)
375 #define FN_NAME pack_int_from_uint_rgba
376 #include "pack_tmp.h"
381 #define DST_TYPE GLushort
382 #define SRC_CONVERT(x) MIN2(x, 0xffff)
383 #define FN_NAME pack_ushort_from_uint_rgba
384 #include "pack_tmp.h"
389 #define DST_TYPE GLshort
390 #define SRC_CONVERT(x) CLAMP((int)x, -32768, 32767)
391 #define FN_NAME pack_short_from_uint_rgba
392 #include "pack_tmp.h"
397 #define DST_TYPE GLubyte
398 #define SRC_CONVERT(x) MIN2(x, 0xff)
399 #define FN_NAME pack_ubyte_from_uint_rgba
400 #include "pack_tmp.h"
405 #define DST_TYPE GLbyte
406 #define SRC_CONVERT(x) CLAMP((int)x, -128, 127)
407 #define FN_NAME pack_byte_from_uint_rgba
408 #include "pack_tmp.h"
416 _pack_rgba_span_from_uints_problem(struct gl_context
*ctx
,
417 GLenum dstFormat
, GLenum dstType
)
420 "Unsupported type (%s) / format (%s) "
421 "in _mesa_pack_rgba_span_from_uints",
422 _mesa_lookup_enum_by_nr(dstType
),
423 _mesa_lookup_enum_by_nr(dstFormat
));
427 _mesa_pack_rgba_span_from_uints(struct gl_context
*ctx
, GLuint n
, GLuint rgba
[][4],
428 GLenum dstFormat
, GLenum dstType
,
431 uint32_t dstMesaFormat
;
434 case GL_UNSIGNED_INT
:
435 pack_uint_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
438 pack_int_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
440 case GL_UNSIGNED_SHORT
:
441 pack_ushort_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
444 pack_short_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
446 case GL_UNSIGNED_BYTE
:
447 pack_ubyte_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
450 pack_byte_from_uint_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
452 case GL_UNSIGNED_BYTE_3_3_2
:
453 case GL_UNSIGNED_BYTE_2_3_3_REV
:
454 case GL_UNSIGNED_SHORT_5_6_5
:
455 case GL_UNSIGNED_SHORT_5_6_5_REV
:
456 case GL_UNSIGNED_SHORT_4_4_4_4
:
457 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
458 case GL_UNSIGNED_SHORT_5_5_5_1
:
459 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
460 case GL_UNSIGNED_INT_8_8_8_8
:
461 case GL_UNSIGNED_INT_8_8_8_8_REV
:
462 case GL_UNSIGNED_INT_10_10_10_2
:
463 case GL_UNSIGNED_INT_2_10_10_10_REV
:
464 dstMesaFormat
= _mesa_format_from_format_and_type(dstFormat
, dstType
);
465 if (!(dstMesaFormat
& MESA_ARRAY_FORMAT_BIT
)) {
466 _mesa_pack_uint_rgba_row(dstMesaFormat
, n
, (void *)rgba
[0], (void *)dstAddr
);
472 _pack_rgba_span_from_uints_problem(ctx
, dstFormat
, dstType
);
477 /* Customization of signed integer packing.
479 #define SRC_TYPE GLint
481 #define DST_TYPE GLuint
482 #define SRC_CONVERT(x) MAX2(x, 0)
483 #define FN_NAME pack_uint_from_int_rgba
484 #include "pack_tmp.h"
489 #define DST_TYPE GLushort
490 #define SRC_CONVERT(x) MAX2(x, 0)
491 #define FN_NAME pack_ushort_from_int_rgba
492 #include "pack_tmp.h"
497 #define DST_TYPE GLshort
498 #define SRC_CONVERT(x) CLAMP(x, -0x8000, 0x7fff)
499 #define FN_NAME pack_short_from_int_rgba
500 #include "pack_tmp.h"
505 #define DST_TYPE GLubyte
506 #define SRC_CONVERT(x) MAX2(x, 0)
507 #define FN_NAME pack_ubyte_from_int_rgba
508 #include "pack_tmp.h"
513 #define DST_TYPE GLbyte
514 #define SRC_CONVERT(x) CLAMP(x, -0x80, 0x7f)
515 #define FN_NAME pack_byte_from_int_rgba
516 #include "pack_tmp.h"
524 _pack_rgba_span_from_ints_problem(struct gl_context
*ctx
,
525 GLenum dstFormat
, GLenum dstType
)
528 "Unsupported type (%s) / format (%s) "
529 "in _mesa_pack_rgba_span_from_ints",
530 _mesa_lookup_enum_by_nr(dstType
),
531 _mesa_lookup_enum_by_nr(dstFormat
));
535 _mesa_pack_rgba_span_from_ints(struct gl_context
*ctx
, GLuint n
, GLint rgba
[][4],
536 GLenum dstFormat
, GLenum dstType
,
539 uint32_t dstMesaFormat
;
542 case GL_UNSIGNED_INT
:
543 pack_uint_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
546 /* No conversion necessary. */
547 pack_uint_from_uint_rgba(ctx
, dstAddr
, dstFormat
, (GLuint (*)[4]) rgba
, NULL
, n
);
549 case GL_UNSIGNED_SHORT
:
550 pack_ushort_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
553 pack_short_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
555 case GL_UNSIGNED_BYTE
:
556 pack_ubyte_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
559 pack_byte_from_int_rgba(ctx
, dstAddr
, dstFormat
, rgba
, NULL
, n
);
561 case GL_UNSIGNED_BYTE_3_3_2
:
562 case GL_UNSIGNED_BYTE_2_3_3_REV
:
563 case GL_UNSIGNED_SHORT_5_6_5
:
564 case GL_UNSIGNED_SHORT_5_6_5_REV
:
565 case GL_UNSIGNED_SHORT_4_4_4_4
:
566 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
567 case GL_UNSIGNED_SHORT_5_5_5_1
:
568 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
569 case GL_UNSIGNED_INT_8_8_8_8
:
570 case GL_UNSIGNED_INT_8_8_8_8_REV
:
571 case GL_UNSIGNED_INT_10_10_10_2
:
572 case GL_UNSIGNED_INT_2_10_10_10_REV
:
573 dstMesaFormat
= _mesa_format_from_format_and_type(dstFormat
, dstType
);
574 if (!(dstMesaFormat
& MESA_ARRAY_FORMAT_BIT
)) {
575 _mesa_pack_int_rgba_row(dstMesaFormat
, n
, (void *)rgba
[0], (void *)dstAddr
);
581 _pack_rgba_span_from_ints_problem(ctx
, dstFormat
, dstType
);
586 /* Customization of float packing.
588 #define SRC_TYPE GLfloat
590 #define DST_TYPE GLuint
591 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UINT(x)
592 #define SRC_CONVERT(x) (GLuint) x
593 #define FN_NAME pack_uint_from_float_rgba
594 #include "pack_tmp.h"
597 #undef FLOAT_SRC_CONVERT
600 #define DST_TYPE GLint
601 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_INT(x)
602 #define SRC_CONVERT(x) (GLint) x
603 #define FN_NAME pack_int_from_float_rgba
604 #include "pack_tmp.h"
607 #undef FLOAT_SRC_CONVERT
610 #define DST_TYPE GLshort
611 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_SHORT_TEX(x)
612 #define SRC_CONVERT(x) (GLshort) x
613 #define FN_NAME pack_short_from_float_rgba
614 #include "pack_tmp.h"
617 #undef FLOAT_SRC_CONVERT
620 #define DST_TYPE GLubyte
621 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UBYTE(x)
622 #define SRC_CONVERT(x) (GLubyte) x
623 #define FN_NAME pack_ubyte_from_float_rgba
624 #include "pack_tmp.h"
627 #undef FLOAT_SRC_CONVERT
630 #define DST_TYPE GLbyte
631 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_BYTE_TEX(x)
632 #define SRC_CONVERT(x) (GLbyte) x
633 #define FN_NAME pack_byte_from_float_rgba
634 #include "pack_tmp.h"
637 #undef FLOAT_SRC_CONVERT
640 #define DST_TYPE GLfloat
641 #define FLOAT_SRC_CONVERT(x) x
642 #define SRC_CONVERT(x) x
643 #define FN_NAME pack_float_from_float_rgba
644 #include "pack_tmp.h"
647 #undef FLOAT_SRC_CONVERT
650 #define DST_TYPE GLhalfARB
651 #define FLOAT_SRC_CONVERT(x) _mesa_float_to_half(x)
652 #define FN_NAME pack_half_float_from_float_rgba
653 #include "pack_tmp.h"
656 #undef FLOAT_SRC_CONVERT
662 * Used to pack an array [][4] of RGBA float colors as specified
663 * by the dstFormat, dstType and dstPacking. Used by glReadPixels.
664 * Historically, the RGBA values were in [0,1] and rescaled to fit
665 * into GLubytes, etc. But with new integer formats, the RGBA values
666 * may have any value and we don't always rescale when converting to
669 * Note: the rgba values will be modified by this function when any pixel
670 * transfer ops are enabled.
673 _mesa_pack_rgba_span_float(struct gl_context
*ctx
, GLuint n
, GLfloat rgba
[][4],
674 GLenum dstFormat
, GLenum dstType
,
676 const struct gl_pixelstore_attrib
*dstPacking
,
677 GLbitfield transferOps
)
680 const GLint comps
= _mesa_components_in_format(dstFormat
);
681 const GLboolean intDstFormat
= _mesa_is_enum_format_integer(dstFormat
);
683 uint32_t dstMesaFormat
;
685 if (dstFormat
== GL_LUMINANCE
||
686 dstFormat
== GL_LUMINANCE_ALPHA
||
687 dstFormat
== GL_LUMINANCE_INTEGER_EXT
||
688 dstFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
) {
689 luminance
= malloc(n
* sizeof(GLfloat
));
691 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
699 /* EXT_texture_integer specifies no transfer ops on integer
700 * types in the resolved issues section. Just set them to 0
701 * for integer surfaces.
707 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
711 * Component clamping (besides clamping to [0,1] in
712 * _mesa_apply_rgba_transfer_ops()).
715 /* clamping to dest type's min/max values */
717 if (get_type_min_max(dstType
, &min
, &max
)) {
718 for (i
= 0; i
< n
; i
++) {
719 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], min
, max
);
720 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], min
, max
);
721 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], min
, max
);
722 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], min
, max
);
726 else if (dstFormat
== GL_LUMINANCE
|| dstFormat
== GL_LUMINANCE_ALPHA
) {
727 /* compute luminance values */
728 if (transferOps
& IMAGE_CLAMP_BIT
) {
729 for (i
= 0; i
< n
; i
++) {
730 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
731 luminance
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
735 for (i
= 0; i
< n
; i
++) {
736 luminance
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
742 * Pack/store the pixels. Ugh! Lots of cases!!!
745 case GL_UNSIGNED_BYTE
:
746 pack_ubyte_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
749 pack_byte_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
751 case GL_UNSIGNED_SHORT
:
753 GLushort
*dst
= (GLushort
*) dstAddr
;
757 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][RCOMP
]);
761 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][GCOMP
]);
765 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][BCOMP
]);
769 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][ACOMP
]);
773 UNCLAMPED_FLOAT_TO_USHORT(dst
[i
], luminance
[i
]);
775 case GL_LUMINANCE_ALPHA
:
777 UNCLAMPED_FLOAT_TO_USHORT(dst
[i
*2+0], luminance
[i
]);
778 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+1], rgba
[i
][ACOMP
]);
783 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+0], rgba
[i
][RCOMP
]);
784 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+1], rgba
[i
][GCOMP
]);
789 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+0], rgba
[i
][RCOMP
]);
790 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+1], rgba
[i
][GCOMP
]);
791 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+2], rgba
[i
][BCOMP
]);
796 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][RCOMP
]);
797 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][GCOMP
]);
798 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][BCOMP
]);
799 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][ACOMP
]);
804 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+0], rgba
[i
][BCOMP
]);
805 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+1], rgba
[i
][GCOMP
]);
806 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+2], rgba
[i
][RCOMP
]);
811 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][BCOMP
]);
812 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][GCOMP
]);
813 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][RCOMP
]);
814 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][ACOMP
]);
819 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][ACOMP
]);
820 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][BCOMP
]);
821 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][GCOMP
]);
822 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][RCOMP
]);
825 case GL_RED_INTEGER_EXT
:
827 dst
[i
] = (GLushort
) rgba
[i
][RCOMP
];
830 case GL_GREEN_INTEGER_EXT
:
832 dst
[i
] = (GLushort
) rgba
[i
][GCOMP
];
835 case GL_BLUE_INTEGER_EXT
:
837 dst
[i
] = (GLushort
) rgba
[i
][BCOMP
];
840 case GL_ALPHA_INTEGER_EXT
:
842 dst
[i
] = (GLushort
) rgba
[i
][ACOMP
];
847 dst
[i
*2+0] = (GLushort
) rgba
[i
][RCOMP
];
848 dst
[i
*2+1] = (GLushort
) rgba
[i
][GCOMP
];
851 case GL_RGB_INTEGER_EXT
:
853 dst
[i
*3+0] = (GLushort
) rgba
[i
][RCOMP
];
854 dst
[i
*3+1] = (GLushort
) rgba
[i
][GCOMP
];
855 dst
[i
*3+2] = (GLushort
) rgba
[i
][BCOMP
];
858 case GL_RGBA_INTEGER_EXT
:
860 dst
[i
*4+0] = (GLushort
) rgba
[i
][RCOMP
];
861 dst
[i
*4+1] = (GLushort
) rgba
[i
][GCOMP
];
862 dst
[i
*4+2] = (GLushort
) rgba
[i
][BCOMP
];
863 dst
[i
*4+3] = (GLushort
) rgba
[i
][ACOMP
];
866 case GL_BGR_INTEGER_EXT
:
868 dst
[i
*3+0] = (GLushort
) rgba
[i
][BCOMP
];
869 dst
[i
*3+1] = (GLushort
) rgba
[i
][GCOMP
];
870 dst
[i
*3+2] = (GLushort
) rgba
[i
][RCOMP
];
873 case GL_BGRA_INTEGER_EXT
:
875 dst
[i
*4+0] = (GLushort
) rgba
[i
][BCOMP
];
876 dst
[i
*4+1] = (GLushort
) rgba
[i
][GCOMP
];
877 dst
[i
*4+2] = (GLushort
) rgba
[i
][RCOMP
];
878 dst
[i
*4+3] = (GLushort
) rgba
[i
][ACOMP
];
881 case GL_LUMINANCE_INTEGER_EXT
:
883 dst
[i
*2+0] = (GLushort
) (rgba
[i
][RCOMP
] +
886 dst
[i
*2+1] = (GLushort
) rgba
[i
][ACOMP
];
889 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
891 dst
[i
] = (GLushort
) (rgba
[i
][RCOMP
] +
897 _mesa_problem(ctx
, "bad format in _mesa_pack_rgba_span\n");
902 pack_short_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
904 case GL_UNSIGNED_INT
:
905 pack_uint_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
908 pack_int_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
911 /* No conversion necessary. */
912 pack_float_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
914 case GL_HALF_FLOAT_ARB
:
915 pack_half_float_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
917 case GL_UNSIGNED_BYTE_3_3_2
:
918 case GL_UNSIGNED_BYTE_2_3_3_REV
:
919 case GL_UNSIGNED_SHORT_5_6_5
:
920 case GL_UNSIGNED_SHORT_5_6_5_REV
:
921 case GL_UNSIGNED_SHORT_4_4_4_4
:
922 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
923 case GL_UNSIGNED_SHORT_5_5_5_1
:
924 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
925 case GL_UNSIGNED_INT_8_8_8_8
:
926 case GL_UNSIGNED_INT_8_8_8_8_REV
:
927 case GL_UNSIGNED_INT_10_10_10_2
:
928 case GL_UNSIGNED_INT_2_10_10_10_REV
:
929 case GL_UNSIGNED_INT_5_9_9_9_REV
:
930 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
931 dstMesaFormat
= _mesa_format_from_format_and_type(dstFormat
, dstType
);
932 if (!(dstMesaFormat
& MESA_ARRAY_FORMAT_BIT
)) {
933 _mesa_pack_float_rgba_row(dstMesaFormat
, n
, (void *)rgba
[0], (void *)dstAddr
);
939 _mesa_problem(ctx
, "bad type in _mesa_pack_rgba_span_float");
944 if (dstPacking
->SwapBytes
) {
945 GLint swapSize
= _mesa_sizeof_packed_type(dstType
);
947 _mesa_swap2((GLushort
*) dstAddr
, n
* comps
);
949 else if (swapSize
== 4) {
950 _mesa_swap4((GLuint
*) dstAddr
, n
* comps
);
959 #define SWAP2BYTE(VALUE) \
961 GLubyte *bytes = (GLubyte *) &(VALUE); \
962 GLubyte tmp = bytes[0]; \
963 bytes[0] = bytes[1]; \
967 #define SWAP4BYTE(VALUE) \
969 GLubyte *bytes = (GLubyte *) &(VALUE); \
970 GLubyte tmp = bytes[0]; \
971 bytes[0] = bytes[3]; \
974 bytes[1] = bytes[2]; \
980 extract_uint_indexes(GLuint n
, GLuint indexes
[],
981 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
982 const struct gl_pixelstore_attrib
*unpack
)
984 ASSERT(srcFormat
== GL_COLOR_INDEX
|| srcFormat
== GL_STENCIL_INDEX
);
986 ASSERT(srcType
== GL_BITMAP
||
987 srcType
== GL_UNSIGNED_BYTE
||
988 srcType
== GL_BYTE
||
989 srcType
== GL_UNSIGNED_SHORT
||
990 srcType
== GL_SHORT
||
991 srcType
== GL_UNSIGNED_INT
||
993 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
994 srcType
== GL_HALF_FLOAT_ARB
||
995 srcType
== GL_FLOAT
||
996 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
1001 GLubyte
*ubsrc
= (GLubyte
*) src
;
1002 if (unpack
->LsbFirst
) {
1003 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
1005 for (i
= 0; i
< n
; i
++) {
1006 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1017 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
1019 for (i
= 0; i
< n
; i
++) {
1020 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1032 case GL_UNSIGNED_BYTE
:
1035 const GLubyte
*s
= (const GLubyte
*) src
;
1036 for (i
= 0; i
< n
; i
++)
1043 const GLbyte
*s
= (const GLbyte
*) src
;
1044 for (i
= 0; i
< n
; i
++)
1048 case GL_UNSIGNED_SHORT
:
1051 const GLushort
*s
= (const GLushort
*) src
;
1052 if (unpack
->SwapBytes
) {
1053 for (i
= 0; i
< n
; i
++) {
1054 GLushort value
= s
[i
];
1060 for (i
= 0; i
< n
; i
++)
1068 const GLshort
*s
= (const GLshort
*) src
;
1069 if (unpack
->SwapBytes
) {
1070 for (i
= 0; i
< n
; i
++) {
1071 GLshort value
= s
[i
];
1077 for (i
= 0; i
< n
; i
++)
1082 case GL_UNSIGNED_INT
:
1085 const GLuint
*s
= (const GLuint
*) src
;
1086 if (unpack
->SwapBytes
) {
1087 for (i
= 0; i
< n
; i
++) {
1088 GLuint value
= s
[i
];
1094 for (i
= 0; i
< n
; i
++)
1102 const GLint
*s
= (const GLint
*) src
;
1103 if (unpack
->SwapBytes
) {
1104 for (i
= 0; i
< n
; i
++) {
1111 for (i
= 0; i
< n
; i
++)
1119 const GLfloat
*s
= (const GLfloat
*) src
;
1120 if (unpack
->SwapBytes
) {
1121 for (i
= 0; i
< n
; i
++) {
1122 GLfloat value
= s
[i
];
1124 indexes
[i
] = (GLuint
) value
;
1128 for (i
= 0; i
< n
; i
++)
1129 indexes
[i
] = (GLuint
) s
[i
];
1133 case GL_HALF_FLOAT_ARB
:
1136 const GLhalfARB
*s
= (const GLhalfARB
*) src
;
1137 if (unpack
->SwapBytes
) {
1138 for (i
= 0; i
< n
; i
++) {
1139 GLhalfARB value
= s
[i
];
1141 indexes
[i
] = (GLuint
) _mesa_half_to_float(value
);
1145 for (i
= 0; i
< n
; i
++)
1146 indexes
[i
] = (GLuint
) _mesa_half_to_float(s
[i
]);
1150 case GL_UNSIGNED_INT_24_8_EXT
:
1153 const GLuint
*s
= (const GLuint
*) src
;
1154 if (unpack
->SwapBytes
) {
1155 for (i
= 0; i
< n
; i
++) {
1156 GLuint value
= s
[i
];
1158 indexes
[i
] = value
& 0xff; /* lower 8 bits */
1162 for (i
= 0; i
< n
; i
++)
1163 indexes
[i
] = s
[i
] & 0xff; /* lower 8 bits */
1167 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1170 const GLuint
*s
= (const GLuint
*) src
;
1171 if (unpack
->SwapBytes
) {
1172 for (i
= 0; i
< n
; i
++) {
1173 GLuint value
= s
[i
*2+1];
1175 indexes
[i
] = value
& 0xff; /* lower 8 bits */
1179 for (i
= 0; i
< n
; i
++)
1180 indexes
[i
] = s
[i
*2+1] & 0xff; /* lower 8 bits */
1186 _mesa_problem(NULL
, "bad srcType in extract_uint_indexes");
1193 * Return source/dest RGBA indexes for unpacking pixels.
1196 get_component_mapping(GLenum format
,
1208 case GL_RED_INTEGER_EXT
:
1210 *gSrc
= *bSrc
= *aSrc
= -1;
1213 case GL_GREEN_INTEGER_EXT
:
1215 *rSrc
= *bSrc
= *aSrc
= -1;
1218 case GL_BLUE_INTEGER_EXT
:
1220 *rSrc
= *gSrc
= *aSrc
= -1;
1223 case GL_ALPHA_INTEGER_EXT
:
1224 *rSrc
= *gSrc
= *bSrc
= -1;
1228 case GL_LUMINANCE_INTEGER_EXT
:
1229 *rSrc
= *gSrc
= *bSrc
= 0;
1232 case GL_LUMINANCE_ALPHA
:
1233 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
1234 *rSrc
= *gSrc
= *bSrc
= 0;
1238 *rSrc
= *gSrc
= *bSrc
= *aSrc
= 0;
1252 case GL_RGB_INTEGER
:
1263 case GL_BGR_INTEGER
:
1274 case GL_RGBA_INTEGER
:
1285 case GL_BGRA_INTEGER
:
1306 _mesa_problem(NULL
, "bad srcFormat %s in get_component_mapping",
1307 _mesa_lookup_enum_by_nr(format
));
1315 * This function extracts floating point RGBA values from arbitrary
1316 * image data. srcFormat and srcType are the format and type parameters
1317 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1319 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1320 * implements the "Conversion to floating point", "Conversion to RGB",
1321 * and "Final Expansion to RGBA" operations.
1323 * Args: n - number of pixels
1324 * rgba - output colors
1325 * srcFormat - format of incoming data
1326 * srcType - data type of incoming data
1327 * src - source data pointer
1328 * swapBytes - perform byteswapping of incoming data?
1331 extract_float_rgba(GLuint n
, GLfloat rgba
[][4],
1332 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1333 GLboolean swapBytes
)
1335 GLint rSrc
, gSrc
, bSrc
, aSrc
;
1337 GLint rDst
, bDst
, gDst
, aDst
;
1338 GLboolean intFormat
;
1339 GLfloat rs
= 1.0f
, gs
= 1.0f
, bs
= 1.0f
, as
= 1.0f
; /* scale factors */
1341 ASSERT(srcFormat
== GL_RED
||
1342 srcFormat
== GL_GREEN
||
1343 srcFormat
== GL_BLUE
||
1344 srcFormat
== GL_ALPHA
||
1345 srcFormat
== GL_LUMINANCE
||
1346 srcFormat
== GL_LUMINANCE_ALPHA
||
1347 srcFormat
== GL_INTENSITY
||
1348 srcFormat
== GL_RG
||
1349 srcFormat
== GL_RGB
||
1350 srcFormat
== GL_BGR
||
1351 srcFormat
== GL_RGBA
||
1352 srcFormat
== GL_BGRA
||
1353 srcFormat
== GL_ABGR_EXT
||
1354 srcFormat
== GL_RED_INTEGER_EXT
||
1355 srcFormat
== GL_GREEN_INTEGER_EXT
||
1356 srcFormat
== GL_BLUE_INTEGER_EXT
||
1357 srcFormat
== GL_ALPHA_INTEGER_EXT
||
1358 srcFormat
== GL_RG_INTEGER
||
1359 srcFormat
== GL_RGB_INTEGER_EXT
||
1360 srcFormat
== GL_RGBA_INTEGER_EXT
||
1361 srcFormat
== GL_BGR_INTEGER_EXT
||
1362 srcFormat
== GL_BGRA_INTEGER_EXT
||
1363 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
1364 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
1366 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
1367 srcType
== GL_BYTE
||
1368 srcType
== GL_UNSIGNED_SHORT
||
1369 srcType
== GL_SHORT
||
1370 srcType
== GL_UNSIGNED_INT
||
1371 srcType
== GL_INT
||
1372 srcType
== GL_HALF_FLOAT_ARB
||
1373 srcType
== GL_FLOAT
||
1374 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
1375 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
1376 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
1377 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
1378 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
1379 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
1380 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
1381 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
1382 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
1383 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
1384 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
1385 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
1386 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
1387 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
1389 get_component_mapping(srcFormat
,
1390 &rSrc
, &gSrc
, &bSrc
, &aSrc
,
1391 &rDst
, &gDst
, &bDst
, &aDst
);
1393 stride
= _mesa_components_in_format(srcFormat
);
1395 intFormat
= _mesa_is_enum_format_integer(srcFormat
);
1397 #define PROCESS(SRC_INDEX, DST_INDEX, DEFAULT_FLT, DEFAULT_INT, TYPE, CONVERSION) \
1398 if ((SRC_INDEX) < 0) { \
1401 for (i = 0; i < n; i++) { \
1402 rgba[i][DST_INDEX] = DEFAULT_INT; \
1406 for (i = 0; i < n; i++) { \
1407 rgba[i][DST_INDEX] = DEFAULT_FLT; \
1411 else if (swapBytes) { \
1412 const TYPE *s = (const TYPE *) src; \
1414 for (i = 0; i < n; i++) { \
1415 TYPE value = s[SRC_INDEX]; \
1416 if (sizeof(TYPE) == 2) { \
1419 else if (sizeof(TYPE) == 4) { \
1423 rgba[i][DST_INDEX] = (GLfloat) value; \
1425 rgba[i][DST_INDEX] = (GLfloat) CONVERSION(value); \
1430 const TYPE *s = (const TYPE *) src; \
1433 for (i = 0; i < n; i++) { \
1434 rgba[i][DST_INDEX] = (GLfloat) s[SRC_INDEX]; \
1439 for (i = 0; i < n; i++) { \
1440 rgba[i][DST_INDEX] = (GLfloat) CONVERSION(s[SRC_INDEX]); \
1447 case GL_UNSIGNED_BYTE
:
1448 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1449 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1450 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLubyte
, UBYTE_TO_FLOAT
);
1451 PROCESS(aSrc
, ACOMP
, 1.0F
, 255, GLubyte
, UBYTE_TO_FLOAT
);
1454 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1455 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1456 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLbyte
, BYTE_TO_FLOAT_TEX
);
1457 PROCESS(aSrc
, ACOMP
, 1.0F
, 127, GLbyte
, BYTE_TO_FLOAT_TEX
);
1459 case GL_UNSIGNED_SHORT
:
1460 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1461 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1462 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLushort
, USHORT_TO_FLOAT
);
1463 PROCESS(aSrc
, ACOMP
, 1.0F
, 0xffff, GLushort
, USHORT_TO_FLOAT
);
1466 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1467 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1468 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLshort
, SHORT_TO_FLOAT_TEX
);
1469 PROCESS(aSrc
, ACOMP
, 1.0F
, 32767, GLshort
, SHORT_TO_FLOAT_TEX
);
1471 case GL_UNSIGNED_INT
:
1472 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1473 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1474 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLuint
, UINT_TO_FLOAT
);
1475 PROCESS(aSrc
, ACOMP
, 1.0F
, 0xffffffff, GLuint
, UINT_TO_FLOAT
);
1478 PROCESS(rSrc
, RCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1479 PROCESS(gSrc
, GCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1480 PROCESS(bSrc
, BCOMP
, 0.0F
, 0, GLint
, INT_TO_FLOAT
);
1481 PROCESS(aSrc
, ACOMP
, 1.0F
, 2147483647, GLint
, INT_TO_FLOAT
);
1484 PROCESS(rSrc
, RCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1485 PROCESS(gSrc
, GCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1486 PROCESS(bSrc
, BCOMP
, 0.0F
, 0.0F
, GLfloat
, (GLfloat
));
1487 PROCESS(aSrc
, ACOMP
, 1.0F
, 1.0F
, GLfloat
, (GLfloat
));
1489 case GL_HALF_FLOAT_ARB
:
1490 PROCESS(rSrc
, RCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1491 PROCESS(gSrc
, GCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1492 PROCESS(bSrc
, BCOMP
, 0.0F
, 0.0F
, GLhalfARB
, _mesa_half_to_float
);
1493 PROCESS(aSrc
, ACOMP
, 1.0F
, 1.0F
, GLhalfARB
, _mesa_half_to_float
);
1495 case GL_UNSIGNED_BYTE_3_3_2
:
1497 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1504 for (i
= 0; i
< n
; i
++) {
1505 GLubyte p
= ubsrc
[i
];
1506 rgba
[i
][rDst
] = ((p
>> 5) ) * rs
;
1507 rgba
[i
][gDst
] = ((p
>> 2) & 0x7) * gs
;
1508 rgba
[i
][bDst
] = ((p
) & 0x3) * bs
;
1509 rgba
[i
][aDst
] = 1.0F
;
1513 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1515 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1522 for (i
= 0; i
< n
; i
++) {
1523 GLubyte p
= ubsrc
[i
];
1524 rgba
[i
][rDst
] = ((p
) & 0x7) * rs
;
1525 rgba
[i
][gDst
] = ((p
>> 3) & 0x7) * gs
;
1526 rgba
[i
][bDst
] = ((p
>> 6) ) * bs
;
1527 rgba
[i
][aDst
] = 1.0F
;
1531 case GL_UNSIGNED_SHORT_5_6_5
:
1538 const GLushort
*ussrc
= (const GLushort
*) src
;
1540 for (i
= 0; i
< n
; i
++) {
1541 GLushort p
= ussrc
[i
];
1543 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1544 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1545 rgba
[i
][bDst
] = ((p
) & 0x1f) * bs
;
1546 rgba
[i
][aDst
] = 1.0F
;
1550 const GLushort
*ussrc
= (const GLushort
*) src
;
1552 for (i
= 0; i
< n
; i
++) {
1553 GLushort p
= ussrc
[i
];
1554 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1555 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1556 rgba
[i
][bDst
] = ((p
) & 0x1f) * bs
;
1557 rgba
[i
][aDst
] = 1.0F
;
1561 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1568 const GLushort
*ussrc
= (const GLushort
*) src
;
1570 for (i
= 0; i
< n
; i
++) {
1571 GLushort p
= ussrc
[i
];
1573 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1574 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1575 rgba
[i
][bDst
] = ((p
>> 11) ) * bs
;
1576 rgba
[i
][aDst
] = 1.0F
;
1580 const GLushort
*ussrc
= (const GLushort
*) src
;
1582 for (i
= 0; i
< n
; i
++) {
1583 GLushort p
= ussrc
[i
];
1584 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1585 rgba
[i
][gDst
] = ((p
>> 5) & 0x3f) * gs
;
1586 rgba
[i
][bDst
] = ((p
>> 11) ) * bs
;
1587 rgba
[i
][aDst
] = 1.0F
;
1591 case GL_UNSIGNED_SHORT_4_4_4_4
:
1593 rs
= gs
= bs
= as
= 1.0F
/ 15.0F
;
1596 const GLushort
*ussrc
= (const GLushort
*) src
;
1598 for (i
= 0; i
< n
; i
++) {
1599 GLushort p
= ussrc
[i
];
1601 rgba
[i
][rDst
] = ((p
>> 12) ) * rs
;
1602 rgba
[i
][gDst
] = ((p
>> 8) & 0xf) * gs
;
1603 rgba
[i
][bDst
] = ((p
>> 4) & 0xf) * bs
;
1604 rgba
[i
][aDst
] = ((p
) & 0xf) * as
;
1608 const GLushort
*ussrc
= (const GLushort
*) src
;
1610 for (i
= 0; i
< n
; i
++) {
1611 GLushort p
= ussrc
[i
];
1612 rgba
[i
][rDst
] = ((p
>> 12) ) * rs
;
1613 rgba
[i
][gDst
] = ((p
>> 8) & 0xf) * gs
;
1614 rgba
[i
][bDst
] = ((p
>> 4) & 0xf) * bs
;
1615 rgba
[i
][aDst
] = ((p
) & 0xf) * as
;
1619 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1621 rs
= gs
= bs
= as
= 1.0F
/ 15.0F
;
1624 const GLushort
*ussrc
= (const GLushort
*) src
;
1626 for (i
= 0; i
< n
; i
++) {
1627 GLushort p
= ussrc
[i
];
1629 rgba
[i
][rDst
] = ((p
) & 0xf) * rs
;
1630 rgba
[i
][gDst
] = ((p
>> 4) & 0xf) * gs
;
1631 rgba
[i
][bDst
] = ((p
>> 8) & 0xf) * bs
;
1632 rgba
[i
][aDst
] = ((p
>> 12) ) * as
;
1636 const GLushort
*ussrc
= (const GLushort
*) src
;
1638 for (i
= 0; i
< n
; i
++) {
1639 GLushort p
= ussrc
[i
];
1640 rgba
[i
][rDst
] = ((p
) & 0xf) * rs
;
1641 rgba
[i
][gDst
] = ((p
>> 4) & 0xf) * gs
;
1642 rgba
[i
][bDst
] = ((p
>> 8) & 0xf) * bs
;
1643 rgba
[i
][aDst
] = ((p
>> 12) ) * as
;
1647 case GL_UNSIGNED_SHORT_5_5_5_1
:
1649 rs
= gs
= bs
= 1.0F
/ 31.0F
;
1652 const GLushort
*ussrc
= (const GLushort
*) src
;
1654 for (i
= 0; i
< n
; i
++) {
1655 GLushort p
= ussrc
[i
];
1657 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1658 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f) * gs
;
1659 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f) * bs
;
1660 rgba
[i
][aDst
] = ((p
) & 0x1) * as
;
1664 const GLushort
*ussrc
= (const GLushort
*) src
;
1666 for (i
= 0; i
< n
; i
++) {
1667 GLushort p
= ussrc
[i
];
1668 rgba
[i
][rDst
] = ((p
>> 11) ) * rs
;
1669 rgba
[i
][gDst
] = ((p
>> 6) & 0x1f) * gs
;
1670 rgba
[i
][bDst
] = ((p
>> 1) & 0x1f) * bs
;
1671 rgba
[i
][aDst
] = ((p
) & 0x1) * as
;
1675 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1677 rs
= gs
= bs
= 1.0F
/ 31.0F
;
1680 const GLushort
*ussrc
= (const GLushort
*) src
;
1682 for (i
= 0; i
< n
; i
++) {
1683 GLushort p
= ussrc
[i
];
1685 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1686 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f) * gs
;
1687 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f) * bs
;
1688 rgba
[i
][aDst
] = ((p
>> 15) ) * as
;
1692 const GLushort
*ussrc
= (const GLushort
*) src
;
1694 for (i
= 0; i
< n
; i
++) {
1695 GLushort p
= ussrc
[i
];
1696 rgba
[i
][rDst
] = ((p
) & 0x1f) * rs
;
1697 rgba
[i
][gDst
] = ((p
>> 5) & 0x1f) * gs
;
1698 rgba
[i
][bDst
] = ((p
>> 10) & 0x1f) * bs
;
1699 rgba
[i
][aDst
] = ((p
>> 15) ) * as
;
1703 case GL_UNSIGNED_INT_8_8_8_8
:
1705 const GLuint
*uisrc
= (const GLuint
*) src
;
1708 for (i
= 0; i
< n
; i
++) {
1709 GLuint p
= uisrc
[i
];
1710 rgba
[i
][rDst
] = (GLfloat
) ((p
) & 0xff);
1711 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 8) & 0xff);
1712 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 16) & 0xff);
1713 rgba
[i
][aDst
] = (GLfloat
) ((p
>> 24) );
1717 for (i
= 0; i
< n
; i
++) {
1718 GLuint p
= uisrc
[i
];
1719 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
1720 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
1721 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
1722 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
>> 24) );
1727 const GLuint
*uisrc
= (const GLuint
*) src
;
1730 for (i
= 0; i
< n
; i
++) {
1731 GLuint p
= uisrc
[i
];
1732 rgba
[i
][rDst
] = (GLfloat
) ((p
>> 24) );
1733 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 16) & 0xff);
1734 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 8) & 0xff);
1735 rgba
[i
][aDst
] = (GLfloat
) ((p
) & 0xff);
1739 for (i
= 0; i
< n
; i
++) {
1740 GLuint p
= uisrc
[i
];
1741 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
>> 24) );
1742 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
1743 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
1744 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
1749 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1751 const GLuint
*uisrc
= (const GLuint
*) src
;
1754 for (i
= 0; i
< n
; i
++) {
1755 GLuint p
= uisrc
[i
];
1756 rgba
[i
][rDst
] = (GLfloat
) ((p
>> 24) );
1757 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 16) & 0xff);
1758 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 8) & 0xff);
1759 rgba
[i
][aDst
] = (GLfloat
) ((p
) & 0xff);
1763 for (i
= 0; i
< n
; i
++) {
1764 GLuint p
= uisrc
[i
];
1765 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
>> 24) );
1766 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
1767 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
1768 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
1773 const GLuint
*uisrc
= (const GLuint
*) src
;
1776 for (i
= 0; i
< n
; i
++) {
1777 GLuint p
= uisrc
[i
];
1778 rgba
[i
][rDst
] = (GLfloat
) ((p
) & 0xff);
1779 rgba
[i
][gDst
] = (GLfloat
) ((p
>> 8) & 0xff);
1780 rgba
[i
][bDst
] = (GLfloat
) ((p
>> 16) & 0xff);
1781 rgba
[i
][aDst
] = (GLfloat
) ((p
>> 24) );
1785 for (i
= 0; i
< n
; i
++) {
1786 GLuint p
= uisrc
[i
];
1787 rgba
[i
][rDst
] = UBYTE_TO_FLOAT((p
) & 0xff);
1788 rgba
[i
][gDst
] = UBYTE_TO_FLOAT((p
>> 8) & 0xff);
1789 rgba
[i
][bDst
] = UBYTE_TO_FLOAT((p
>> 16) & 0xff);
1790 rgba
[i
][aDst
] = UBYTE_TO_FLOAT((p
>> 24) );
1795 case GL_UNSIGNED_INT_10_10_10_2
:
1797 rs
= 1.0F
/ 1023.0F
;
1798 gs
= 1.0F
/ 1023.0F
;
1799 bs
= 1.0F
/ 1023.0F
;
1803 const GLuint
*uisrc
= (const GLuint
*) src
;
1805 for (i
= 0; i
< n
; i
++) {
1806 GLuint p
= uisrc
[i
];
1808 rgba
[i
][rDst
] = ((p
>> 22) ) * rs
;
1809 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff) * gs
;
1810 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff) * bs
;
1811 rgba
[i
][aDst
] = ((p
) & 0x3 ) * as
;
1815 const GLuint
*uisrc
= (const GLuint
*) src
;
1817 for (i
= 0; i
< n
; i
++) {
1818 GLuint p
= uisrc
[i
];
1819 rgba
[i
][rDst
] = ((p
>> 22) ) * rs
;
1820 rgba
[i
][gDst
] = ((p
>> 12) & 0x3ff) * gs
;
1821 rgba
[i
][bDst
] = ((p
>> 2) & 0x3ff) * bs
;
1822 rgba
[i
][aDst
] = ((p
) & 0x3 ) * as
;
1826 case GL_UNSIGNED_INT_2_10_10_10_REV
:
1828 rs
= 1.0F
/ 1023.0F
;
1829 gs
= 1.0F
/ 1023.0F
;
1830 bs
= 1.0F
/ 1023.0F
;
1834 const GLuint
*uisrc
= (const GLuint
*) src
;
1836 for (i
= 0; i
< n
; i
++) {
1837 GLuint p
= uisrc
[i
];
1839 rgba
[i
][rDst
] = ((p
) & 0x3ff) * rs
;
1840 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff) * gs
;
1841 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff) * bs
;
1843 rgba
[i
][aDst
] = 1.0F
;
1845 rgba
[i
][aDst
] = (p
>> 30) * as
;
1850 const GLuint
*uisrc
= (const GLuint
*) src
;
1852 for (i
= 0; i
< n
; i
++) {
1853 GLuint p
= uisrc
[i
];
1854 rgba
[i
][rDst
] = ((p
) & 0x3ff) * rs
;
1855 rgba
[i
][gDst
] = ((p
>> 10) & 0x3ff) * gs
;
1856 rgba
[i
][bDst
] = ((p
>> 20) & 0x3ff) * bs
;
1858 rgba
[i
][aDst
] = 1.0F
;
1860 rgba
[i
][aDst
] = (p
>> 30) * as
;
1865 case GL_UNSIGNED_INT_5_9_9_9_REV
:
1867 const GLuint
*uisrc
= (const GLuint
*) src
;
1870 for (i
= 0; i
< n
; i
++) {
1871 GLuint p
= uisrc
[i
];
1873 rgb9e5_to_float3(p
, f
);
1874 rgba
[i
][rDst
] = f
[0];
1875 rgba
[i
][gDst
] = f
[1];
1876 rgba
[i
][bDst
] = f
[2];
1877 rgba
[i
][aDst
] = 1.0F
;
1881 const GLuint
*uisrc
= (const GLuint
*) src
;
1884 for (i
= 0; i
< n
; i
++) {
1885 rgb9e5_to_float3(uisrc
[i
], f
);
1886 rgba
[i
][rDst
] = f
[0];
1887 rgba
[i
][gDst
] = f
[1];
1888 rgba
[i
][bDst
] = f
[2];
1889 rgba
[i
][aDst
] = 1.0F
;
1893 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
1895 const GLuint
*uisrc
= (const GLuint
*) src
;
1898 for (i
= 0; i
< n
; i
++) {
1899 GLuint p
= uisrc
[i
];
1901 r11g11b10f_to_float3(p
, f
);
1902 rgba
[i
][rDst
] = f
[0];
1903 rgba
[i
][gDst
] = f
[1];
1904 rgba
[i
][bDst
] = f
[2];
1905 rgba
[i
][aDst
] = 1.0F
;
1909 const GLuint
*uisrc
= (const GLuint
*) src
;
1912 for (i
= 0; i
< n
; i
++) {
1913 r11g11b10f_to_float3(uisrc
[i
], f
);
1914 rgba
[i
][rDst
] = f
[0];
1915 rgba
[i
][gDst
] = f
[1];
1916 rgba
[i
][bDst
] = f
[2];
1917 rgba
[i
][aDst
] = 1.0F
;
1922 _mesa_problem(NULL
, "bad srcType in extract float data");
1929 static inline GLuint
1930 clamp_float_to_uint(GLfloat f
)
1932 return f
< 0.0F
? 0 : F_TO_I(f
);
1936 static inline GLuint
1937 clamp_half_to_uint(GLhalfARB h
)
1939 GLfloat f
= _mesa_half_to_float(h
);
1940 return f
< 0.0F
? 0 : F_TO_I(f
);
1945 * Same as _mesa_unpack_color_span_ubyte(), but return GLfloat data
1946 * instead of GLubyte.
1949 _mesa_unpack_color_span_float( struct gl_context
*ctx
,
1950 GLuint n
, GLenum dstFormat
, GLfloat dest
[],
1951 GLenum srcFormat
, GLenum srcType
,
1952 const GLvoid
*source
,
1953 const struct gl_pixelstore_attrib
*srcPacking
,
1954 GLbitfield transferOps
)
1956 ASSERT(dstFormat
== GL_ALPHA
||
1957 dstFormat
== GL_LUMINANCE
||
1958 dstFormat
== GL_LUMINANCE_ALPHA
||
1959 dstFormat
== GL_INTENSITY
||
1960 dstFormat
== GL_RED
||
1961 dstFormat
== GL_RG
||
1962 dstFormat
== GL_RGB
||
1963 dstFormat
== GL_RGBA
);
1965 ASSERT(srcFormat
== GL_RED
||
1966 srcFormat
== GL_GREEN
||
1967 srcFormat
== GL_BLUE
||
1968 srcFormat
== GL_ALPHA
||
1969 srcFormat
== GL_LUMINANCE
||
1970 srcFormat
== GL_LUMINANCE_ALPHA
||
1971 srcFormat
== GL_INTENSITY
||
1972 srcFormat
== GL_RG
||
1973 srcFormat
== GL_RGB
||
1974 srcFormat
== GL_BGR
||
1975 srcFormat
== GL_RGBA
||
1976 srcFormat
== GL_BGRA
||
1977 srcFormat
== GL_ABGR_EXT
||
1978 srcFormat
== GL_RED_INTEGER_EXT
||
1979 srcFormat
== GL_GREEN_INTEGER_EXT
||
1980 srcFormat
== GL_BLUE_INTEGER_EXT
||
1981 srcFormat
== GL_ALPHA_INTEGER_EXT
||
1982 srcFormat
== GL_RG_INTEGER
||
1983 srcFormat
== GL_RGB_INTEGER_EXT
||
1984 srcFormat
== GL_RGBA_INTEGER_EXT
||
1985 srcFormat
== GL_BGR_INTEGER_EXT
||
1986 srcFormat
== GL_BGRA_INTEGER_EXT
||
1987 srcFormat
== GL_LUMINANCE_INTEGER_EXT
||
1988 srcFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
||
1989 srcFormat
== GL_COLOR_INDEX
);
1991 ASSERT(srcType
== GL_BITMAP
||
1992 srcType
== GL_UNSIGNED_BYTE
||
1993 srcType
== GL_BYTE
||
1994 srcType
== GL_UNSIGNED_SHORT
||
1995 srcType
== GL_SHORT
||
1996 srcType
== GL_UNSIGNED_INT
||
1997 srcType
== GL_INT
||
1998 srcType
== GL_HALF_FLOAT_ARB
||
1999 srcType
== GL_FLOAT
||
2000 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2001 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2002 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2003 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2004 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2005 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2006 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2007 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2008 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2009 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2010 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2011 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
||
2012 srcType
== GL_UNSIGNED_INT_5_9_9_9_REV
||
2013 srcType
== GL_UNSIGNED_INT_10F_11F_11F_REV
);
2015 /* general solution, no special cases, yet */
2017 GLint dstComponents
;
2018 GLint rDst
, gDst
, bDst
, aDst
, lDst
, iDst
;
2019 GLfloat (*rgba
)[4] = malloc(4 * n
* sizeof(GLfloat
));
2020 GLboolean intFormat
= _mesa_is_enum_format_integer(srcFormat
);
2023 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2027 dstComponents
= _mesa_components_in_format( dstFormat
);
2028 /* source & dest image formats should have been error checked by now */
2029 assert(dstComponents
> 0);
2031 /* EXT_texture_integer specifies no transfer ops on integer
2032 * types in the resolved issues section. Just set them to 0
2033 * for integer surfaces.
2039 * Extract image data and convert to RGBA floats
2041 if (srcFormat
== GL_COLOR_INDEX
) {
2042 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
2045 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2050 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
2053 /* Convert indexes to RGBA */
2054 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
2055 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
2057 _mesa_map_ci_to_rgba(ctx
, n
, indexes
, rgba
);
2059 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
2060 * with color indexes.
2062 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
2067 /* non-color index data */
2068 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
2069 srcPacking
->SwapBytes
);
2073 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
2076 get_component_indexes(dstFormat
,
2077 &rDst
, &gDst
, &bDst
, &aDst
, &lDst
, &iDst
);
2079 /* Now pack results in the requested dstFormat */
2081 GLfloat
*dst
= dest
;
2083 for (i
= 0; i
< n
; i
++) {
2084 dst
[rDst
] = rgba
[i
][RCOMP
];
2085 dst
+= dstComponents
;
2090 GLfloat
*dst
= dest
;
2092 for (i
= 0; i
< n
; i
++) {
2093 dst
[gDst
] = rgba
[i
][GCOMP
];
2094 dst
+= dstComponents
;
2099 GLfloat
*dst
= dest
;
2101 for (i
= 0; i
< n
; i
++) {
2102 dst
[bDst
] = rgba
[i
][BCOMP
];
2103 dst
+= dstComponents
;
2108 GLfloat
*dst
= dest
;
2110 for (i
= 0; i
< n
; i
++) {
2111 dst
[aDst
] = rgba
[i
][ACOMP
];
2112 dst
+= dstComponents
;
2117 GLfloat
*dst
= dest
;
2120 assert(dstComponents
== 1);
2121 for (i
= 0; i
< n
; i
++) {
2122 /* Intensity comes from red channel */
2123 dst
[i
] = rgba
[i
][RCOMP
];
2128 GLfloat
*dst
= dest
;
2131 for (i
= 0; i
< n
; i
++) {
2132 /* Luminance comes from red channel */
2133 dst
[0] = rgba
[i
][RCOMP
];
2134 dst
+= dstComponents
;
2144 * Unpack a row of color index data from a client buffer according to
2145 * the pixel unpacking parameters.
2146 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2148 * Args: ctx - the context
2149 * n - number of pixels
2150 * dstType - destination data type
2151 * dest - destination array
2152 * srcType - source pixel type
2153 * source - source data pointer
2154 * srcPacking - pixel unpacking parameters
2155 * transferOps - the pixel transfer operations to apply
2158 _mesa_unpack_index_span( struct gl_context
*ctx
, GLuint n
,
2159 GLenum dstType
, GLvoid
*dest
,
2160 GLenum srcType
, const GLvoid
*source
,
2161 const struct gl_pixelstore_attrib
*srcPacking
,
2162 GLbitfield transferOps
)
2164 ASSERT(srcType
== GL_BITMAP
||
2165 srcType
== GL_UNSIGNED_BYTE
||
2166 srcType
== GL_BYTE
||
2167 srcType
== GL_UNSIGNED_SHORT
||
2168 srcType
== GL_SHORT
||
2169 srcType
== GL_UNSIGNED_INT
||
2170 srcType
== GL_INT
||
2171 srcType
== GL_HALF_FLOAT_ARB
||
2172 srcType
== GL_FLOAT
);
2174 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
2175 dstType
== GL_UNSIGNED_SHORT
||
2176 dstType
== GL_UNSIGNED_INT
);
2179 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
2182 * Try simple cases first
2184 if (transferOps
== 0 && srcType
== GL_UNSIGNED_BYTE
2185 && dstType
== GL_UNSIGNED_BYTE
) {
2186 memcpy(dest
, source
, n
* sizeof(GLubyte
));
2188 else if (transferOps
== 0 && srcType
== GL_UNSIGNED_INT
2189 && dstType
== GL_UNSIGNED_INT
&& !srcPacking
->SwapBytes
) {
2190 memcpy(dest
, source
, n
* sizeof(GLuint
));
2196 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
2199 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2203 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
2207 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
2209 /* convert to dest type */
2211 case GL_UNSIGNED_BYTE
:
2213 GLubyte
*dst
= (GLubyte
*) dest
;
2215 for (i
= 0; i
< n
; i
++) {
2216 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2220 case GL_UNSIGNED_SHORT
:
2222 GLuint
*dst
= (GLuint
*) dest
;
2224 for (i
= 0; i
< n
; i
++) {
2225 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
2229 case GL_UNSIGNED_INT
:
2230 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
2233 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
2242 _mesa_pack_index_span( struct gl_context
*ctx
, GLuint n
,
2243 GLenum dstType
, GLvoid
*dest
, const GLuint
*source
,
2244 const struct gl_pixelstore_attrib
*dstPacking
,
2245 GLbitfield transferOps
)
2247 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
2250 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
2254 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
2256 if (transferOps
& (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
)) {
2257 /* make a copy of input */
2258 memcpy(indexes
, source
, n
* sizeof(GLuint
));
2259 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
2264 case GL_UNSIGNED_BYTE
:
2266 GLubyte
*dst
= (GLubyte
*) dest
;
2268 for (i
= 0; i
< n
; i
++) {
2269 *dst
++ = (GLubyte
) source
[i
];
2275 GLbyte
*dst
= (GLbyte
*) dest
;
2277 for (i
= 0; i
< n
; i
++) {
2278 dst
[i
] = (GLbyte
) source
[i
];
2282 case GL_UNSIGNED_SHORT
:
2284 GLushort
*dst
= (GLushort
*) dest
;
2286 for (i
= 0; i
< n
; i
++) {
2287 dst
[i
] = (GLushort
) source
[i
];
2289 if (dstPacking
->SwapBytes
) {
2290 _mesa_swap2( (GLushort
*) dst
, n
);
2296 GLshort
*dst
= (GLshort
*) dest
;
2298 for (i
= 0; i
< n
; i
++) {
2299 dst
[i
] = (GLshort
) source
[i
];
2301 if (dstPacking
->SwapBytes
) {
2302 _mesa_swap2( (GLushort
*) dst
, n
);
2306 case GL_UNSIGNED_INT
:
2308 GLuint
*dst
= (GLuint
*) dest
;
2310 for (i
= 0; i
< n
; i
++) {
2311 dst
[i
] = (GLuint
) source
[i
];
2313 if (dstPacking
->SwapBytes
) {
2314 _mesa_swap4( (GLuint
*) dst
, n
);
2320 GLint
*dst
= (GLint
*) dest
;
2322 for (i
= 0; i
< n
; i
++) {
2323 dst
[i
] = (GLint
) source
[i
];
2325 if (dstPacking
->SwapBytes
) {
2326 _mesa_swap4( (GLuint
*) dst
, n
);
2332 GLfloat
*dst
= (GLfloat
*) dest
;
2334 for (i
= 0; i
< n
; i
++) {
2335 dst
[i
] = (GLfloat
) source
[i
];
2337 if (dstPacking
->SwapBytes
) {
2338 _mesa_swap4( (GLuint
*) dst
, n
);
2342 case GL_HALF_FLOAT_ARB
:
2344 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
2346 for (i
= 0; i
< n
; i
++) {
2347 dst
[i
] = _mesa_float_to_half((GLfloat
) source
[i
]);
2349 if (dstPacking
->SwapBytes
) {
2350 _mesa_swap2( (GLushort
*) dst
, n
);
2355 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
2363 * Unpack a row of stencil data from a client buffer according to
2364 * the pixel unpacking parameters.
2365 * This is (or will be) used by glDrawPixels
2367 * Args: ctx - the context
2368 * n - number of pixels
2369 * dstType - destination data type
2370 * dest - destination array
2371 * srcType - source pixel type
2372 * source - source data pointer
2373 * srcPacking - pixel unpacking parameters
2374 * transferOps - apply offset/bias/lookup ops?
2377 _mesa_unpack_stencil_span( struct gl_context
*ctx
, GLuint n
,
2378 GLenum dstType
, GLvoid
*dest
,
2379 GLenum srcType
, const GLvoid
*source
,
2380 const struct gl_pixelstore_attrib
*srcPacking
,
2381 GLbitfield transferOps
)
2383 ASSERT(srcType
== GL_BITMAP
||
2384 srcType
== GL_UNSIGNED_BYTE
||
2385 srcType
== GL_BYTE
||
2386 srcType
== GL_UNSIGNED_SHORT
||
2387 srcType
== GL_SHORT
||
2388 srcType
== GL_UNSIGNED_INT
||
2389 srcType
== GL_INT
||
2390 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
2391 srcType
== GL_HALF_FLOAT_ARB
||
2392 srcType
== GL_FLOAT
||
2393 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
2395 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
2396 dstType
== GL_UNSIGNED_SHORT
||
2397 dstType
== GL_UNSIGNED_INT
||
2398 dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
2400 /* only shift and offset apply to stencil */
2401 transferOps
&= IMAGE_SHIFT_OFFSET_BIT
;
2404 * Try simple cases first
2406 if (transferOps
== 0 &&
2407 !ctx
->Pixel
.MapStencilFlag
&&
2408 srcType
== GL_UNSIGNED_BYTE
&&
2409 dstType
== GL_UNSIGNED_BYTE
) {
2410 memcpy(dest
, source
, n
* sizeof(GLubyte
));
2412 else if (transferOps
== 0 &&
2413 !ctx
->Pixel
.MapStencilFlag
&&
2414 srcType
== GL_UNSIGNED_INT
&&
2415 dstType
== GL_UNSIGNED_INT
&&
2416 !srcPacking
->SwapBytes
) {
2417 memcpy(dest
, source
, n
* sizeof(GLuint
));
2423 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
2426 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil unpacking");
2430 extract_uint_indexes(n
, indexes
, GL_STENCIL_INDEX
, srcType
, source
,
2433 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
2434 /* shift and offset indexes */
2435 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
2438 if (ctx
->Pixel
.MapStencilFlag
) {
2439 /* Apply stencil lookup table */
2440 const GLuint mask
= ctx
->PixelMaps
.StoS
.Size
- 1;
2442 for (i
= 0; i
< n
; i
++) {
2443 indexes
[i
] = (GLuint
)ctx
->PixelMaps
.StoS
.Map
[ indexes
[i
] & mask
];
2447 /* convert to dest type */
2449 case GL_UNSIGNED_BYTE
:
2451 GLubyte
*dst
= (GLubyte
*) dest
;
2453 for (i
= 0; i
< n
; i
++) {
2454 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2458 case GL_UNSIGNED_SHORT
:
2460 GLuint
*dst
= (GLuint
*) dest
;
2462 for (i
= 0; i
< n
; i
++) {
2463 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
2467 case GL_UNSIGNED_INT
:
2468 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
2470 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
2472 GLuint
*dst
= (GLuint
*) dest
;
2474 for (i
= 0; i
< n
; i
++) {
2475 dst
[i
*2+1] = indexes
[i
] & 0xff; /* lower 8 bits */
2480 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
2489 _mesa_pack_stencil_span( struct gl_context
*ctx
, GLuint n
,
2490 GLenum dstType
, GLvoid
*dest
, const GLubyte
*source
,
2491 const struct gl_pixelstore_attrib
*dstPacking
)
2493 GLubyte
*stencil
= malloc(n
* sizeof(GLubyte
));
2496 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil packing");
2500 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
2501 ctx
->Pixel
.MapStencilFlag
) {
2502 /* make a copy of input */
2503 memcpy(stencil
, source
, n
* sizeof(GLubyte
));
2504 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencil
);
2509 case GL_UNSIGNED_BYTE
:
2510 memcpy(dest
, source
, n
);
2514 GLbyte
*dst
= (GLbyte
*) dest
;
2517 dst
[i
] = (GLbyte
) (source
[i
] & 0x7f);
2521 case GL_UNSIGNED_SHORT
:
2523 GLushort
*dst
= (GLushort
*) dest
;
2526 dst
[i
] = (GLushort
) source
[i
];
2528 if (dstPacking
->SwapBytes
) {
2529 _mesa_swap2( (GLushort
*) dst
, n
);
2535 GLshort
*dst
= (GLshort
*) dest
;
2538 dst
[i
] = (GLshort
) source
[i
];
2540 if (dstPacking
->SwapBytes
) {
2541 _mesa_swap2( (GLushort
*) dst
, n
);
2545 case GL_UNSIGNED_INT
:
2547 GLuint
*dst
= (GLuint
*) dest
;
2550 dst
[i
] = (GLuint
) source
[i
];
2552 if (dstPacking
->SwapBytes
) {
2553 _mesa_swap4( (GLuint
*) dst
, n
);
2559 GLint
*dst
= (GLint
*) dest
;
2562 dst
[i
] = (GLint
) source
[i
];
2564 if (dstPacking
->SwapBytes
) {
2565 _mesa_swap4( (GLuint
*) dst
, n
);
2571 GLfloat
*dst
= (GLfloat
*) dest
;
2574 dst
[i
] = (GLfloat
) source
[i
];
2576 if (dstPacking
->SwapBytes
) {
2577 _mesa_swap4( (GLuint
*) dst
, n
);
2581 case GL_HALF_FLOAT_ARB
:
2583 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
2586 dst
[i
] = _mesa_float_to_half( (float) source
[i
] );
2588 if (dstPacking
->SwapBytes
) {
2589 _mesa_swap2( (GLushort
*) dst
, n
);
2594 if (dstPacking
->LsbFirst
) {
2595 GLubyte
*dst
= (GLubyte
*) dest
;
2598 for (i
= 0; i
< n
; i
++) {
2601 *dst
|= ((source
[i
] != 0) << shift
);
2610 GLubyte
*dst
= (GLubyte
*) dest
;
2613 for (i
= 0; i
< n
; i
++) {
2616 *dst
|= ((source
[i
] != 0) << shift
);
2626 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
2632 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
2635 const GLTYPE *src = (const GLTYPE *)source; \
2636 for (i = 0; i < n; i++) { \
2637 GLTYPE value = src[i]; \
2638 if (srcPacking->SwapBytes) { \
2639 if (sizeof(GLTYPE) == 2) { \
2641 } else if (sizeof(GLTYPE) == 4) { \
2645 depthValues[i] = GLTYPE2FLOAT(value); \
2651 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
2652 * or GLfloat values.
2653 * The glPixelTransfer (scale/bias) params will be applied.
2655 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
2656 * \param depthMax max value for returned GLushort or GLuint values
2657 * (ignored for GLfloat).
2660 _mesa_unpack_depth_span( struct gl_context
*ctx
, GLuint n
,
2661 GLenum dstType
, GLvoid
*dest
, GLuint depthMax
,
2662 GLenum srcType
, const GLvoid
*source
,
2663 const struct gl_pixelstore_attrib
*srcPacking
)
2665 GLfloat
*depthTemp
= NULL
, *depthValues
;
2666 GLboolean needClamp
= GL_FALSE
;
2668 /* Look for special cases first.
2669 * Not only are these faster, they're less prone to numeric conversion
2670 * problems. Otherwise, converting from an int type to a float then
2671 * back to an int type can introduce errors that will show up as
2672 * artifacts in things like depth peeling which uses glCopyTexImage.
2674 if (ctx
->Pixel
.DepthScale
== 1.0 && ctx
->Pixel
.DepthBias
== 0.0) {
2675 if (srcType
== GL_UNSIGNED_INT
&& dstType
== GL_UNSIGNED_SHORT
) {
2676 const GLuint
*src
= (const GLuint
*) source
;
2677 GLushort
*dst
= (GLushort
*) dest
;
2679 for (i
= 0; i
< n
; i
++) {
2680 dst
[i
] = src
[i
] >> 16;
2684 if (srcType
== GL_UNSIGNED_SHORT
2685 && dstType
== GL_UNSIGNED_INT
2686 && depthMax
== 0xffffffff) {
2687 const GLushort
*src
= (const GLushort
*) source
;
2688 GLuint
*dst
= (GLuint
*) dest
;
2690 for (i
= 0; i
< n
; i
++) {
2691 dst
[i
] = src
[i
] | (src
[i
] << 16);
2695 if (srcType
== GL_UNSIGNED_INT_24_8
2696 && dstType
== GL_UNSIGNED_INT
2697 && depthMax
== 0xffffff) {
2698 const GLuint
*src
= (const GLuint
*) source
;
2699 GLuint
*dst
= (GLuint
*) dest
;
2701 for (i
= 0; i
< n
; i
++) {
2702 dst
[i
] = src
[i
] >> 8;
2706 /* XXX may want to add additional cases here someday */
2709 /* general case path follows */
2711 if (dstType
== GL_FLOAT
) {
2712 depthValues
= (GLfloat
*) dest
;
2715 depthTemp
= malloc(n
* sizeof(GLfloat
));
2717 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2721 depthValues
= depthTemp
;
2724 /* Convert incoming values to GLfloat. Some conversions will require
2729 DEPTH_VALUES(GLbyte
, BYTE_TO_FLOATZ
);
2730 needClamp
= GL_TRUE
;
2732 case GL_UNSIGNED_BYTE
:
2733 DEPTH_VALUES(GLubyte
, UBYTE_TO_FLOAT
);
2736 DEPTH_VALUES(GLshort
, SHORT_TO_FLOATZ
);
2737 needClamp
= GL_TRUE
;
2739 case GL_UNSIGNED_SHORT
:
2740 DEPTH_VALUES(GLushort
, USHORT_TO_FLOAT
);
2743 DEPTH_VALUES(GLint
, INT_TO_FLOAT
);
2744 needClamp
= GL_TRUE
;
2746 case GL_UNSIGNED_INT
:
2747 DEPTH_VALUES(GLuint
, UINT_TO_FLOAT
);
2749 case GL_UNSIGNED_INT_24_8_EXT
: /* GL_EXT_packed_depth_stencil */
2750 if (dstType
== GL_UNSIGNED_INT_24_8_EXT
&&
2751 depthMax
== 0xffffff &&
2752 ctx
->Pixel
.DepthScale
== 1.0 &&
2753 ctx
->Pixel
.DepthBias
== 0.0) {
2754 const GLuint
*src
= (const GLuint
*) source
;
2755 GLuint
*zValues
= (GLuint
*) dest
;
2757 for (i
= 0; i
< n
; i
++) {
2758 GLuint value
= src
[i
];
2759 if (srcPacking
->SwapBytes
) {
2762 zValues
[i
] = value
& 0xffffff00;
2768 const GLuint
*src
= (const GLuint
*) source
;
2769 const GLfloat scale
= 1.0f
/ 0xffffff;
2771 for (i
= 0; i
< n
; i
++) {
2772 GLuint value
= src
[i
];
2773 if (srcPacking
->SwapBytes
) {
2776 depthValues
[i
] = (value
>> 8) * scale
;
2780 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
2783 const GLfloat
*src
= (const GLfloat
*)source
;
2784 for (i
= 0; i
< n
; i
++) {
2785 GLfloat value
= src
[i
* 2];
2786 if (srcPacking
->SwapBytes
) {
2789 depthValues
[i
] = value
;
2791 needClamp
= GL_TRUE
;
2795 DEPTH_VALUES(GLfloat
, 1*);
2796 needClamp
= GL_TRUE
;
2798 case GL_HALF_FLOAT_ARB
:
2801 const GLhalfARB
*src
= (const GLhalfARB
*) source
;
2802 for (i
= 0; i
< n
; i
++) {
2803 GLhalfARB value
= src
[i
];
2804 if (srcPacking
->SwapBytes
) {
2807 depthValues
[i
] = _mesa_half_to_float(value
);
2809 needClamp
= GL_TRUE
;
2813 _mesa_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
2818 /* apply depth scale and bias */
2820 const GLfloat scale
= ctx
->Pixel
.DepthScale
;
2821 const GLfloat bias
= ctx
->Pixel
.DepthBias
;
2822 if (scale
!= 1.0 || bias
!= 0.0) {
2824 for (i
= 0; i
< n
; i
++) {
2825 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
2827 needClamp
= GL_TRUE
;
2831 /* clamp to [0, 1] */
2834 for (i
= 0; i
< n
; i
++) {
2835 depthValues
[i
] = (GLfloat
)CLAMP(depthValues
[i
], 0.0, 1.0);
2840 * Convert values to dstType
2842 if (dstType
== GL_UNSIGNED_INT
) {
2843 GLuint
*zValues
= (GLuint
*) dest
;
2845 if (depthMax
<= 0xffffff) {
2846 /* no overflow worries */
2847 for (i
= 0; i
< n
; i
++) {
2848 zValues
[i
] = (GLuint
) (depthValues
[i
] * (GLfloat
) depthMax
);
2852 /* need to use double precision to prevent overflow problems */
2853 for (i
= 0; i
< n
; i
++) {
2854 GLdouble z
= depthValues
[i
] * (GLdouble
) depthMax
;
2855 if (z
>= (GLdouble
) 0xffffffff)
2856 zValues
[i
] = 0xffffffff;
2858 zValues
[i
] = (GLuint
) z
;
2862 else if (dstType
== GL_UNSIGNED_SHORT
) {
2863 GLushort
*zValues
= (GLushort
*) dest
;
2865 ASSERT(depthMax
<= 0xffff);
2866 for (i
= 0; i
< n
; i
++) {
2867 zValues
[i
] = (GLushort
) (depthValues
[i
] * (GLfloat
) depthMax
);
2870 else if (dstType
== GL_FLOAT
) {
2871 /* Nothing to do. depthValues is pointing to dest. */
2873 else if (dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
) {
2874 GLfloat
*zValues
= (GLfloat
*) dest
;
2876 for (i
= 0; i
< n
; i
++) {
2877 zValues
[i
*2] = depthValues
[i
];
2889 * Pack an array of depth values. The values are floats in [0,1].
2892 _mesa_pack_depth_span( struct gl_context
*ctx
, GLuint n
, GLvoid
*dest
,
2893 GLenum dstType
, const GLfloat
*depthSpan
,
2894 const struct gl_pixelstore_attrib
*dstPacking
)
2896 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
2898 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
2902 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
2903 memcpy(depthCopy
, depthSpan
, n
* sizeof(GLfloat
));
2904 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
2905 depthSpan
= depthCopy
;
2909 case GL_UNSIGNED_BYTE
:
2911 GLubyte
*dst
= (GLubyte
*) dest
;
2913 for (i
= 0; i
< n
; i
++) {
2914 dst
[i
] = FLOAT_TO_UBYTE( depthSpan
[i
] );
2920 GLbyte
*dst
= (GLbyte
*) dest
;
2922 for (i
= 0; i
< n
; i
++) {
2923 dst
[i
] = FLOAT_TO_BYTE( depthSpan
[i
] );
2927 case GL_UNSIGNED_SHORT
:
2929 GLushort
*dst
= (GLushort
*) dest
;
2931 for (i
= 0; i
< n
; i
++) {
2932 CLAMPED_FLOAT_TO_USHORT(dst
[i
], depthSpan
[i
]);
2934 if (dstPacking
->SwapBytes
) {
2935 _mesa_swap2( (GLushort
*) dst
, n
);
2941 GLshort
*dst
= (GLshort
*) dest
;
2943 for (i
= 0; i
< n
; i
++) {
2944 dst
[i
] = FLOAT_TO_SHORT( depthSpan
[i
] );
2946 if (dstPacking
->SwapBytes
) {
2947 _mesa_swap2( (GLushort
*) dst
, n
);
2951 case GL_UNSIGNED_INT
:
2953 GLuint
*dst
= (GLuint
*) dest
;
2955 for (i
= 0; i
< n
; i
++) {
2956 dst
[i
] = FLOAT_TO_UINT( depthSpan
[i
] );
2958 if (dstPacking
->SwapBytes
) {
2959 _mesa_swap4( (GLuint
*) dst
, n
);
2965 GLint
*dst
= (GLint
*) dest
;
2967 for (i
= 0; i
< n
; i
++) {
2968 dst
[i
] = FLOAT_TO_INT( depthSpan
[i
] );
2970 if (dstPacking
->SwapBytes
) {
2971 _mesa_swap4( (GLuint
*) dst
, n
);
2977 GLfloat
*dst
= (GLfloat
*) dest
;
2979 for (i
= 0; i
< n
; i
++) {
2980 dst
[i
] = depthSpan
[i
];
2982 if (dstPacking
->SwapBytes
) {
2983 _mesa_swap4( (GLuint
*) dst
, n
);
2987 case GL_HALF_FLOAT_ARB
:
2989 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
2991 for (i
= 0; i
< n
; i
++) {
2992 dst
[i
] = _mesa_float_to_half(depthSpan
[i
]);
2994 if (dstPacking
->SwapBytes
) {
2995 _mesa_swap2( (GLushort
*) dst
, n
);
3000 _mesa_problem(ctx
, "bad type in _mesa_pack_depth_span");
3009 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
3012 _mesa_pack_depth_stencil_span(struct gl_context
*ctx
,GLuint n
,
3013 GLenum dstType
, GLuint
*dest
,
3014 const GLfloat
*depthVals
,
3015 const GLubyte
*stencilVals
,
3016 const struct gl_pixelstore_attrib
*dstPacking
)
3018 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
3019 GLubyte
*stencilCopy
= malloc(n
* sizeof(GLubyte
));
3022 if (!depthCopy
|| !stencilCopy
) {
3023 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
3029 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
3030 memcpy(depthCopy
, depthVals
, n
* sizeof(GLfloat
));
3031 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
3032 depthVals
= depthCopy
;
3035 if (ctx
->Pixel
.IndexShift
||
3036 ctx
->Pixel
.IndexOffset
||
3037 ctx
->Pixel
.MapStencilFlag
) {
3038 memcpy(stencilCopy
, stencilVals
, n
* sizeof(GLubyte
));
3039 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencilCopy
);
3040 stencilVals
= stencilCopy
;
3044 case GL_UNSIGNED_INT_24_8
:
3045 for (i
= 0; i
< n
; i
++) {
3046 GLuint z
= (GLuint
) (depthVals
[i
] * 0xffffff);
3047 dest
[i
] = (z
<< 8) | (stencilVals
[i
] & 0xff);
3050 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
3051 for (i
= 0; i
< n
; i
++) {
3052 ((GLfloat
*)dest
)[i
*2] = depthVals
[i
];
3053 dest
[i
*2+1] = stencilVals
[i
] & 0xff;
3058 if (dstPacking
->SwapBytes
) {
3059 _mesa_swap4(dest
, n
);
3070 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
3071 * Return all image data in a contiguous block. This is used when we
3072 * compile glDrawPixels, glTexImage, etc into a display list. We
3073 * need a copy of the data in a standard format.
3076 _mesa_unpack_image( GLuint dimensions
,
3077 GLsizei width
, GLsizei height
, GLsizei depth
,
3078 GLenum format
, GLenum type
, const GLvoid
*pixels
,
3079 const struct gl_pixelstore_attrib
*unpack
)
3081 GLint bytesPerRow
, compsPerRow
;
3082 GLboolean flipBytes
, swap2
, swap4
;
3085 return NULL
; /* not necessarily an error */
3087 if (width
<= 0 || height
<= 0 || depth
<= 0)
3088 return NULL
; /* generate error later */
3090 if (type
== GL_BITMAP
) {
3091 bytesPerRow
= (width
+ 7) >> 3;
3092 flipBytes
= unpack
->LsbFirst
;
3093 swap2
= swap4
= GL_FALSE
;
3097 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
3098 GLint components
= _mesa_components_in_format(format
);
3101 if (_mesa_type_is_packed(type
))
3104 if (bytesPerPixel
<= 0 || components
<= 0)
3105 return NULL
; /* bad format or type. generate error later */
3106 bytesPerRow
= bytesPerPixel
* width
;
3107 bytesPerComp
= bytesPerPixel
/ components
;
3108 flipBytes
= GL_FALSE
;
3109 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
3110 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
3111 compsPerRow
= components
* width
;
3112 assert(compsPerRow
>= width
);
3117 = malloc(bytesPerRow
* height
* depth
);
3121 return NULL
; /* generate GL_OUT_OF_MEMORY later */
3124 for (img
= 0; img
< depth
; img
++) {
3125 for (row
= 0; row
< height
; row
++) {
3126 const GLvoid
*src
= _mesa_image_address(dimensions
, unpack
, pixels
,
3127 width
, height
, format
, type
, img
, row
, 0);
3129 if ((type
== GL_BITMAP
) && (unpack
->SkipPixels
& 0x7)) {
3131 flipBytes
= GL_FALSE
;
3132 if (unpack
->LsbFirst
) {
3133 GLubyte srcMask
= 1 << (unpack
->SkipPixels
& 0x7);
3134 GLubyte dstMask
= 128;
3135 const GLubyte
*s
= src
;
3138 for (i
= 0; i
< width
; i
++) {
3142 if (srcMask
== 128) {
3147 srcMask
= srcMask
<< 1;
3155 dstMask
= dstMask
>> 1;
3160 GLubyte srcMask
= 128 >> (unpack
->SkipPixels
& 0x7);
3161 GLubyte dstMask
= 128;
3162 const GLubyte
*s
= src
;
3165 for (i
= 0; i
< width
; i
++) {
3174 srcMask
= srcMask
>> 1;
3182 dstMask
= dstMask
>> 1;
3188 memcpy(dst
, src
, bytesPerRow
);
3191 /* byte flipping/swapping */
3193 flip_bytes((GLubyte
*) dst
, bytesPerRow
);
3196 _mesa_swap2((GLushort
*) dst
, compsPerRow
);
3199 _mesa_swap4((GLuint
*) dst
, compsPerRow
);
3211 * If we unpack colors from a luminance surface, we'll get pixel colors
3212 * such as (l, l, l, a).
3213 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
3214 * function will compute L=R+G+B before packing. The net effect is we'll
3215 * accidentally store luminance values = 3*l.
3216 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
3218 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
3221 * Finally, we also need to do this when the actual surface format does
3222 * not match the logical surface format. For example, suppose the user
3223 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
3224 * Again, we'll get pixel values like (l,l,l,a).
3227 _mesa_rebase_rgba_float(GLuint n
, GLfloat rgba
[][4], GLenum baseFormat
)
3231 switch (baseFormat
) {
3233 for (i
= 0; i
< n
; i
++) {
3234 rgba
[i
][RCOMP
] = 0.0F
;
3235 rgba
[i
][GCOMP
] = 0.0F
;
3236 rgba
[i
][BCOMP
] = 0.0F
;
3242 for (i
= 0; i
< n
; i
++) {
3243 rgba
[i
][GCOMP
] = 0.0F
;
3244 rgba
[i
][BCOMP
] = 0.0F
;
3245 rgba
[i
][ACOMP
] = 1.0F
;
3248 case GL_LUMINANCE_ALPHA
:
3249 for (i
= 0; i
< n
; i
++) {
3250 rgba
[i
][GCOMP
] = 0.0F
;
3251 rgba
[i
][BCOMP
] = 0.0F
;
3255 for (i
= 0; i
< n
; i
++) {
3256 rgba
[i
][ACOMP
] = 1.0F
;
3260 for (i
= 0; i
< n
; i
++) {
3261 rgba
[i
][BCOMP
] = 0.0F
;
3262 rgba
[i
][ACOMP
] = 1.0F
;
3266 for (i
= 0; i
< n
; i
++) {
3267 rgba
[i
][GCOMP
] = 0.0F
;
3268 rgba
[i
][BCOMP
] = 0.0F
;
3269 rgba
[i
][ACOMP
] = 1.0F
;
3281 * As above, but GLuint components.
3284 _mesa_rebase_rgba_uint(GLuint n
, GLuint rgba
[][4], GLenum baseFormat
)
3288 switch (baseFormat
) {
3290 for (i
= 0; i
< n
; i
++) {
3299 for (i
= 0; i
< n
; i
++) {
3305 case GL_LUMINANCE_ALPHA
:
3306 for (i
= 0; i
< n
; i
++) {
3312 for (i
= 0; i
< n
; i
++) {
3317 for (i
= 0; i
< n
; i
++) {
3323 for (i
= 0; i
< n
; i
++) {
3335 _mesa_pack_luminance_from_rgba_float(GLuint n
, GLfloat rgba
[][4],
3336 GLvoid
*dstAddr
, GLenum dst_format
,
3337 GLbitfield transferOps
)
3340 GLfloat
*dst
= (GLfloat
*) dstAddr
;
3342 switch (dst_format
) {
3344 if (transferOps
& IMAGE_CLAMP_BIT
) {
3345 for (i
= 0; i
< n
; i
++) {
3346 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
3347 dst
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
3350 for (i
= 0; i
< n
; i
++) {
3351 dst
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
3355 case GL_LUMINANCE_ALPHA
:
3356 if (transferOps
& IMAGE_CLAMP_BIT
) {
3357 for (i
= 0; i
< n
; i
++) {
3358 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
3359 dst
[2*i
] = CLAMP(sum
, 0.0F
, 1.0F
);
3360 dst
[2*i
+1] = rgba
[i
][ACOMP
];
3363 for (i
= 0; i
< n
; i
++) {
3364 dst
[2*i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
3365 dst
[2*i
+1] = rgba
[i
][ACOMP
];
3370 assert(!"Unsupported format");
3375 clamp_sint64_to_sint32(int64_t src
)
3377 return CLAMP(src
, INT32_MIN
, INT32_MAX
);
3381 clamp_sint64_to_uint32(int64_t src
)
3383 return CLAMP(src
, 0, UINT32_MAX
);
3387 clamp_uint64_to_uint32(uint64_t src
)
3389 return MIN2(src
, UINT32_MAX
);
3393 clamp_uint64_to_sint32(uint64_t src
)
3395 return MIN2(src
, INT32_MAX
);
3399 convert_integer_luminance64(int64_t src64
, int bits
,
3400 bool dst_is_signed
, bool src_is_signed
)
3404 /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
3405 * any signed<->unsigned conversion too.
3407 if (src_is_signed
&& dst_is_signed
)
3408 src32
= clamp_sint64_to_sint32(src64
);
3409 else if (src_is_signed
&& !dst_is_signed
)
3410 src32
= clamp_sint64_to_uint32(src64
);
3411 else if (!src_is_signed
&& dst_is_signed
)
3412 src32
= clamp_uint64_to_sint32(src64
);
3414 src32
= clamp_uint64_to_uint32(src64
);
3416 /* If the dst type is < 32-bit, we need an extra clamp */
3421 return _mesa_signed_to_signed(src32
, bits
);
3423 return _mesa_unsigned_to_unsigned(src32
, bits
);
3428 convert_integer(int32_t src
, int bits
, bool dst_is_signed
, bool src_is_signed
)
3430 if (src_is_signed
&& dst_is_signed
)
3431 return _mesa_signed_to_signed(src
, bits
);
3432 else if (src_is_signed
&& !dst_is_signed
)
3433 return _mesa_signed_to_unsigned(src
, bits
);
3434 else if (!src_is_signed
&& dst_is_signed
)
3435 return _mesa_unsigned_to_signed(src
, bits
);
3437 return _mesa_unsigned_to_unsigned(src
, bits
);
3441 _mesa_pack_luminance_from_rgba_integer(GLuint n
,
3442 GLuint rgba
[][4], bool rgba_is_signed
,
3447 assert(dst_format
== GL_LUMINANCE_INTEGER_EXT
||
3448 dst_format
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
3452 int32_t lum32
, alpha
;
3456 /* We first compute luminance values as a 64-bit addition of the
3457 * 32-bit R,G,B components, then we clamp the result to the dst type size.
3459 * Notice that this operation involves casting the 32-bit R,G,B components
3460 * to 64-bit before the addition. Since rgba is defined as a GLuint array
3461 * we need to be careful when rgba packs signed data and make sure
3462 * that we cast to a 32-bit signed integer values before casting them to
3463 * 64-bit signed integers.
3465 dst_is_signed
= (dst_type
== GL_BYTE
|| dst_type
== GL_SHORT
||
3466 dst_type
== GL_INT
);
3468 dst_bits
= _mesa_sizeof_type(dst_type
) * 8;
3469 assert(dst_bits
> 0);
3471 switch (dst_format
) {
3472 case GL_LUMINANCE_INTEGER_EXT
:
3473 for (i
= 0; i
< n
; i
++) {
3474 if (!rgba_is_signed
) {
3475 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
3476 (uint64_t) rgba
[i
][GCOMP
] +
3477 (uint64_t) rgba
[i
][BCOMP
];
3479 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
3480 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
3481 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
3483 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
3484 dst_is_signed
, rgba_is_signed
);
3487 case GL_UNSIGNED_BYTE
: {
3488 GLbyte
*dst
= (GLbyte
*) dstAddr
;
3493 case GL_UNSIGNED_SHORT
: {
3494 GLshort
*dst
= (GLshort
*) dstAddr
;
3499 case GL_UNSIGNED_INT
: {
3500 GLint
*dst
= (GLint
*) dstAddr
;
3507 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
3508 for (i
= 0; i
< n
; i
++) {
3509 if (!rgba_is_signed
) {
3510 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
3511 (uint64_t) rgba
[i
][GCOMP
] +
3512 (uint64_t) rgba
[i
][BCOMP
];
3514 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
3515 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
3516 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
3518 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
3519 dst_is_signed
, rgba_is_signed
);
3520 alpha
= convert_integer(rgba
[i
][ACOMP
], dst_bits
,
3521 dst_is_signed
, rgba_is_signed
);
3524 case GL_UNSIGNED_BYTE
: {
3525 GLbyte
*dst
= (GLbyte
*) dstAddr
;
3530 case GL_UNSIGNED_SHORT
: {
3531 GLshort
*dst
= (GLshort
*) dstAddr
;
3537 case GL_UNSIGNED_INT
: {
3538 GLint
*dst
= (GLint
*) dstAddr
;
3550 _mesa_unpack_color_index_to_rgba_float(struct gl_context
*ctx
, GLuint dims
,
3551 const void *src
, GLenum srcFormat
, GLenum srcType
,
3552 int srcWidth
, int srcHeight
, int srcDepth
,
3553 const struct gl_pixelstore_attrib
*srcPacking
,
3554 GLbitfield transferOps
)
3558 GLfloat
*rgba
, *dstPtr
;
3560 count
= srcWidth
* srcHeight
;
3561 indexes
= malloc(count
* sizeof(GLuint
));
3563 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3567 rgba
= malloc(4 * count
* srcDepth
* sizeof(GLfloat
));
3570 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
3574 /* Convert indexes to RGBA float */
3576 for (img
= 0; img
< srcDepth
; img
++) {
3577 const GLubyte
*srcPtr
=
3578 (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, src
,
3579 srcWidth
, srcHeight
,
3583 extract_uint_indexes(count
, indexes
, srcFormat
, srcType
, srcPtr
, srcPacking
);
3585 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
)
3586 _mesa_shift_and_offset_ci(ctx
, count
, indexes
);
3588 _mesa_map_ci_to_rgba(ctx
, count
, indexes
, (float (*)[4])dstPtr
);
3590 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3591 * with color indexes.
3593 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
3594 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, count
, (float (*)[4])dstPtr
);
3596 dstPtr
+= srcHeight
* srcWidth
* 4;
3605 _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context
*ctx
, GLuint dims
,
3606 const void *src
, GLenum srcFormat
, GLenum srcType
,
3607 int srcWidth
, int srcHeight
, int srcDepth
,
3608 const struct gl_pixelstore_attrib
*srcPacking
,
3609 GLbitfield transferOps
)
3615 transferOps
|= IMAGE_CLAMP_BIT
;
3616 rgba
= _mesa_unpack_color_index_to_rgba_float(ctx
, dims
,
3617 src
, srcFormat
, srcType
,
3618 srcWidth
, srcHeight
, srcDepth
,
3619 srcPacking
, transferOps
);
3621 count
= srcWidth
* srcHeight
* srcDepth
;
3622 dst
= malloc(count
* 4 * sizeof(GLubyte
));
3623 for (i
= 0; i
< count
; i
++) {
3624 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 0], rgba
[i
* 4 + 0]);
3625 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 1], rgba
[i
* 4 + 1]);
3626 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 2], rgba
[i
* 4 + 2]);
3627 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 3], rgba
[i
* 4 + 3]);