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 "format_utils.h"
57 #include "format_pack.h"
61 * Flip the 8 bits in each byte of the given array.
64 * \param n number of bytes.
66 * \todo try this trick to flip bytes someday:
68 * v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
69 * v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
70 * v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
74 flip_bytes( GLubyte
*p
, GLuint n
)
77 for (i
= 0; i
< n
; i
++) {
78 b
= (GLuint
) p
[i
]; /* words are often faster than bytes */
79 a
= ((b
& 0x01) << 7) |
94 * Unpack a 32x32 pixel polygon stipple from user memory using the
95 * current pixel unpack settings.
98 _mesa_unpack_polygon_stipple( const GLubyte
*pattern
, GLuint dest
[32],
99 const struct gl_pixelstore_attrib
*unpacking
)
101 GLubyte
*ptrn
= (GLubyte
*) _mesa_unpack_image(2, 32, 32, 1, GL_COLOR_INDEX
,
102 GL_BITMAP
, pattern
, unpacking
);
104 /* Convert pattern from GLubytes to GLuints and handle big/little
109 for (i
= 0; i
< 32; i
++) {
110 dest
[i
] = (p
[0] << 24)
122 * Pack polygon stipple into user memory given current pixel packing
126 _mesa_pack_polygon_stipple( const GLuint pattern
[32], GLubyte
*dest
,
127 const struct gl_pixelstore_attrib
*packing
)
129 /* Convert pattern from GLuints to GLubytes to handle big/little
130 * endian differences.
134 for (i
= 0; i
< 32; i
++) {
135 ptrn
[i
* 4 + 0] = (GLubyte
) ((pattern
[i
] >> 24) & 0xff);
136 ptrn
[i
* 4 + 1] = (GLubyte
) ((pattern
[i
] >> 16) & 0xff);
137 ptrn
[i
* 4 + 2] = (GLubyte
) ((pattern
[i
] >> 8 ) & 0xff);
138 ptrn
[i
* 4 + 3] = (GLubyte
) ((pattern
[i
] ) & 0xff);
141 _mesa_pack_bitmap(32, 32, ptrn
, dest
, packing
);
149 _mesa_pack_bitmap( GLint width
, GLint height
, const GLubyte
*source
,
150 GLubyte
*dest
, const struct gl_pixelstore_attrib
*packing
)
152 GLint row
, width_in_bytes
;
158 width_in_bytes
= CEILING( width
, 8 );
160 for (row
= 0; row
< height
; row
++) {
161 GLubyte
*dst
= (GLubyte
*) _mesa_image_address2d(packing
, dest
,
162 width
, height
, GL_COLOR_INDEX
, GL_BITMAP
, row
, 0);
166 if ((packing
->SkipPixels
& 7) == 0) {
167 memcpy( dst
, src
, width_in_bytes
);
168 if (packing
->LsbFirst
) {
169 flip_bytes( dst
, width_in_bytes
);
173 /* handling SkipPixels is a bit tricky (no pun intended!) */
175 if (packing
->LsbFirst
) {
176 GLubyte srcMask
= 128;
177 GLubyte dstMask
= 1 << (packing
->SkipPixels
& 0x7);
178 const GLubyte
*s
= src
;
181 for (i
= 0; i
< width
; i
++) {
190 srcMask
= srcMask
>> 1;
192 if (dstMask
== 128) {
198 dstMask
= dstMask
<< 1;
203 GLubyte srcMask
= 128;
204 GLubyte dstMask
= 128 >> (packing
->SkipPixels
& 0x7);
205 const GLubyte
*s
= src
;
208 for (i
= 0; i
< width
; i
++) {
217 srcMask
= srcMask
>> 1;
225 dstMask
= dstMask
>> 1;
230 src
+= width_in_bytes
;
236 * For small integer types, return the min and max possible values.
237 * Used for clamping floats to unscaled integer types.
238 * \return GL_TRUE if type is handled, GL_FALSE otherwise.
241 get_type_min_max(GLenum type
, GLfloat
*min
, GLfloat
*max
)
248 case GL_UNSIGNED_BYTE
:
256 case GL_UNSIGNED_SHORT
:
265 /* Customization of float packing.
267 #define SRC_TYPE GLfloat
269 #define DST_TYPE GLuint
270 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UINT(x)
271 #define SRC_CONVERT(x) (GLuint) x
272 #define FN_NAME pack_uint_from_float_rgba
273 #include "pack_tmp.h"
276 #undef FLOAT_SRC_CONVERT
279 #define DST_TYPE GLint
280 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_INT(x)
281 #define SRC_CONVERT(x) (GLint) x
282 #define FN_NAME pack_int_from_float_rgba
283 #include "pack_tmp.h"
286 #undef FLOAT_SRC_CONVERT
289 #define DST_TYPE GLshort
290 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_SHORT_TEX(x)
291 #define SRC_CONVERT(x) (GLshort) x
292 #define FN_NAME pack_short_from_float_rgba
293 #include "pack_tmp.h"
296 #undef FLOAT_SRC_CONVERT
299 #define DST_TYPE GLubyte
300 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_UBYTE(x)
301 #define SRC_CONVERT(x) (GLubyte) x
302 #define FN_NAME pack_ubyte_from_float_rgba
303 #include "pack_tmp.h"
306 #undef FLOAT_SRC_CONVERT
309 #define DST_TYPE GLbyte
310 #define FLOAT_SRC_CONVERT(x) FLOAT_TO_BYTE_TEX(x)
311 #define SRC_CONVERT(x) (GLbyte) x
312 #define FN_NAME pack_byte_from_float_rgba
313 #include "pack_tmp.h"
316 #undef FLOAT_SRC_CONVERT
319 #define DST_TYPE GLfloat
320 #define FLOAT_SRC_CONVERT(x) x
321 #define SRC_CONVERT(x) x
322 #define FN_NAME pack_float_from_float_rgba
323 #include "pack_tmp.h"
326 #undef FLOAT_SRC_CONVERT
329 #define DST_TYPE GLhalfARB
330 #define FLOAT_SRC_CONVERT(x) _mesa_float_to_half(x)
331 #define FN_NAME pack_half_float_from_float_rgba
332 #include "pack_tmp.h"
335 #undef FLOAT_SRC_CONVERT
341 * Used to pack an array [][4] of RGBA float colors as specified
342 * by the dstFormat, dstType and dstPacking. Used by glReadPixels.
343 * Historically, the RGBA values were in [0,1] and rescaled to fit
344 * into GLubytes, etc. But with new integer formats, the RGBA values
345 * may have any value and we don't always rescale when converting to
348 * Note: the rgba values will be modified by this function when any pixel
349 * transfer ops are enabled.
352 _mesa_pack_rgba_span_float(struct gl_context
*ctx
, GLuint n
, GLfloat rgba
[][4],
353 GLenum dstFormat
, GLenum dstType
,
355 const struct gl_pixelstore_attrib
*dstPacking
,
356 GLbitfield transferOps
)
359 const GLint comps
= _mesa_components_in_format(dstFormat
);
360 const GLboolean intDstFormat
= _mesa_is_enum_format_integer(dstFormat
);
362 uint32_t dstMesaFormat
;
364 if (dstFormat
== GL_LUMINANCE
||
365 dstFormat
== GL_LUMINANCE_ALPHA
||
366 dstFormat
== GL_LUMINANCE_INTEGER_EXT
||
367 dstFormat
== GL_LUMINANCE_ALPHA_INTEGER_EXT
) {
368 luminance
= malloc(n
* sizeof(GLfloat
));
370 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
378 /* EXT_texture_integer specifies no transfer ops on integer
379 * types in the resolved issues section. Just set them to 0
380 * for integer surfaces.
386 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, n
, rgba
);
390 * Component clamping (besides clamping to [0,1] in
391 * _mesa_apply_rgba_transfer_ops()).
394 /* clamping to dest type's min/max values */
396 if (get_type_min_max(dstType
, &min
, &max
)) {
397 for (i
= 0; i
< n
; i
++) {
398 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], min
, max
);
399 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], min
, max
);
400 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], min
, max
);
401 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], min
, max
);
405 else if (dstFormat
== GL_LUMINANCE
|| dstFormat
== GL_LUMINANCE_ALPHA
) {
406 /* compute luminance values */
407 if (transferOps
& IMAGE_CLAMP_BIT
) {
408 for (i
= 0; i
< n
; i
++) {
409 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
410 luminance
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
414 for (i
= 0; i
< n
; i
++) {
415 luminance
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
421 * Pack/store the pixels. Ugh! Lots of cases!!!
424 case GL_UNSIGNED_BYTE
:
425 pack_ubyte_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
428 pack_byte_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
430 case GL_UNSIGNED_SHORT
:
432 GLushort
*dst
= (GLushort
*) dstAddr
;
436 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][RCOMP
]);
440 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][GCOMP
]);
444 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][BCOMP
]);
448 CLAMPED_FLOAT_TO_USHORT(dst
[i
], rgba
[i
][ACOMP
]);
452 UNCLAMPED_FLOAT_TO_USHORT(dst
[i
], luminance
[i
]);
454 case GL_LUMINANCE_ALPHA
:
456 UNCLAMPED_FLOAT_TO_USHORT(dst
[i
*2+0], luminance
[i
]);
457 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+1], rgba
[i
][ACOMP
]);
462 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+0], rgba
[i
][RCOMP
]);
463 CLAMPED_FLOAT_TO_USHORT(dst
[i
*2+1], rgba
[i
][GCOMP
]);
468 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+0], rgba
[i
][RCOMP
]);
469 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+1], rgba
[i
][GCOMP
]);
470 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+2], rgba
[i
][BCOMP
]);
475 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][RCOMP
]);
476 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][GCOMP
]);
477 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][BCOMP
]);
478 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][ACOMP
]);
483 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+0], rgba
[i
][BCOMP
]);
484 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+1], rgba
[i
][GCOMP
]);
485 CLAMPED_FLOAT_TO_USHORT(dst
[i
*3+2], rgba
[i
][RCOMP
]);
490 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][BCOMP
]);
491 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][GCOMP
]);
492 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][RCOMP
]);
493 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][ACOMP
]);
498 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+0], rgba
[i
][ACOMP
]);
499 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+1], rgba
[i
][BCOMP
]);
500 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+2], rgba
[i
][GCOMP
]);
501 CLAMPED_FLOAT_TO_USHORT(dst
[i
*4+3], rgba
[i
][RCOMP
]);
504 case GL_RED_INTEGER_EXT
:
506 dst
[i
] = (GLushort
) rgba
[i
][RCOMP
];
509 case GL_GREEN_INTEGER_EXT
:
511 dst
[i
] = (GLushort
) rgba
[i
][GCOMP
];
514 case GL_BLUE_INTEGER_EXT
:
516 dst
[i
] = (GLushort
) rgba
[i
][BCOMP
];
519 case GL_ALPHA_INTEGER_EXT
:
521 dst
[i
] = (GLushort
) rgba
[i
][ACOMP
];
526 dst
[i
*2+0] = (GLushort
) rgba
[i
][RCOMP
];
527 dst
[i
*2+1] = (GLushort
) rgba
[i
][GCOMP
];
530 case GL_RGB_INTEGER_EXT
:
532 dst
[i
*3+0] = (GLushort
) rgba
[i
][RCOMP
];
533 dst
[i
*3+1] = (GLushort
) rgba
[i
][GCOMP
];
534 dst
[i
*3+2] = (GLushort
) rgba
[i
][BCOMP
];
537 case GL_RGBA_INTEGER_EXT
:
539 dst
[i
*4+0] = (GLushort
) rgba
[i
][RCOMP
];
540 dst
[i
*4+1] = (GLushort
) rgba
[i
][GCOMP
];
541 dst
[i
*4+2] = (GLushort
) rgba
[i
][BCOMP
];
542 dst
[i
*4+3] = (GLushort
) rgba
[i
][ACOMP
];
545 case GL_BGR_INTEGER_EXT
:
547 dst
[i
*3+0] = (GLushort
) rgba
[i
][BCOMP
];
548 dst
[i
*3+1] = (GLushort
) rgba
[i
][GCOMP
];
549 dst
[i
*3+2] = (GLushort
) rgba
[i
][RCOMP
];
552 case GL_BGRA_INTEGER_EXT
:
554 dst
[i
*4+0] = (GLushort
) rgba
[i
][BCOMP
];
555 dst
[i
*4+1] = (GLushort
) rgba
[i
][GCOMP
];
556 dst
[i
*4+2] = (GLushort
) rgba
[i
][RCOMP
];
557 dst
[i
*4+3] = (GLushort
) rgba
[i
][ACOMP
];
560 case GL_LUMINANCE_INTEGER_EXT
:
562 dst
[i
*2+0] = (GLushort
) (rgba
[i
][RCOMP
] +
565 dst
[i
*2+1] = (GLushort
) rgba
[i
][ACOMP
];
568 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
570 dst
[i
] = (GLushort
) (rgba
[i
][RCOMP
] +
576 _mesa_problem(ctx
, "bad format in _mesa_pack_rgba_span\n");
581 pack_short_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
583 case GL_UNSIGNED_INT
:
584 pack_uint_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
587 pack_int_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
590 /* No conversion necessary. */
591 pack_float_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
593 case GL_HALF_FLOAT_ARB
:
594 pack_half_float_from_float_rgba(ctx
, dstAddr
, dstFormat
, rgba
, luminance
, n
);
596 case GL_UNSIGNED_BYTE_3_3_2
:
597 case GL_UNSIGNED_BYTE_2_3_3_REV
:
598 case GL_UNSIGNED_SHORT_5_6_5
:
599 case GL_UNSIGNED_SHORT_5_6_5_REV
:
600 case GL_UNSIGNED_SHORT_4_4_4_4
:
601 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
602 case GL_UNSIGNED_SHORT_5_5_5_1
:
603 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
604 case GL_UNSIGNED_INT_8_8_8_8
:
605 case GL_UNSIGNED_INT_8_8_8_8_REV
:
606 case GL_UNSIGNED_INT_10_10_10_2
:
607 case GL_UNSIGNED_INT_2_10_10_10_REV
:
608 case GL_UNSIGNED_INT_5_9_9_9_REV
:
609 case GL_UNSIGNED_INT_10F_11F_11F_REV
:
610 dstMesaFormat
= _mesa_format_from_format_and_type(dstFormat
, dstType
);
611 if (!(dstMesaFormat
& MESA_ARRAY_FORMAT_BIT
)) {
612 _mesa_pack_float_rgba_row(dstMesaFormat
, n
, (void *)rgba
[0], (void *)dstAddr
);
618 _mesa_problem(ctx
, "bad type in _mesa_pack_rgba_span_float");
623 if (dstPacking
->SwapBytes
) {
624 GLint swapSize
= _mesa_sizeof_packed_type(dstType
);
626 _mesa_swap2((GLushort
*) dstAddr
, n
* comps
);
628 else if (swapSize
== 4) {
629 _mesa_swap4((GLuint
*) dstAddr
, n
* comps
);
638 #define SWAP2BYTE(VALUE) \
640 GLubyte *bytes = (GLubyte *) &(VALUE); \
641 GLubyte tmp = bytes[0]; \
642 bytes[0] = bytes[1]; \
646 #define SWAP4BYTE(VALUE) \
648 GLubyte *bytes = (GLubyte *) &(VALUE); \
649 GLubyte tmp = bytes[0]; \
650 bytes[0] = bytes[3]; \
653 bytes[1] = bytes[2]; \
659 extract_uint_indexes(GLuint n
, GLuint indexes
[],
660 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
661 const struct gl_pixelstore_attrib
*unpack
)
663 ASSERT(srcFormat
== GL_COLOR_INDEX
|| srcFormat
== GL_STENCIL_INDEX
);
665 ASSERT(srcType
== GL_BITMAP
||
666 srcType
== GL_UNSIGNED_BYTE
||
667 srcType
== GL_BYTE
||
668 srcType
== GL_UNSIGNED_SHORT
||
669 srcType
== GL_SHORT
||
670 srcType
== GL_UNSIGNED_INT
||
672 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
673 srcType
== GL_HALF_FLOAT_ARB
||
674 srcType
== GL_FLOAT
||
675 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
680 GLubyte
*ubsrc
= (GLubyte
*) src
;
681 if (unpack
->LsbFirst
) {
682 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
684 for (i
= 0; i
< n
; i
++) {
685 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
696 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
698 for (i
= 0; i
< n
; i
++) {
699 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
711 case GL_UNSIGNED_BYTE
:
714 const GLubyte
*s
= (const GLubyte
*) src
;
715 for (i
= 0; i
< n
; i
++)
722 const GLbyte
*s
= (const GLbyte
*) src
;
723 for (i
= 0; i
< n
; i
++)
727 case GL_UNSIGNED_SHORT
:
730 const GLushort
*s
= (const GLushort
*) src
;
731 if (unpack
->SwapBytes
) {
732 for (i
= 0; i
< n
; i
++) {
733 GLushort value
= s
[i
];
739 for (i
= 0; i
< n
; i
++)
747 const GLshort
*s
= (const GLshort
*) src
;
748 if (unpack
->SwapBytes
) {
749 for (i
= 0; i
< n
; i
++) {
750 GLshort value
= s
[i
];
756 for (i
= 0; i
< n
; i
++)
761 case GL_UNSIGNED_INT
:
764 const GLuint
*s
= (const GLuint
*) src
;
765 if (unpack
->SwapBytes
) {
766 for (i
= 0; i
< n
; i
++) {
773 for (i
= 0; i
< n
; i
++)
781 const GLint
*s
= (const GLint
*) src
;
782 if (unpack
->SwapBytes
) {
783 for (i
= 0; i
< n
; i
++) {
790 for (i
= 0; i
< n
; i
++)
798 const GLfloat
*s
= (const GLfloat
*) src
;
799 if (unpack
->SwapBytes
) {
800 for (i
= 0; i
< n
; i
++) {
801 GLfloat value
= s
[i
];
803 indexes
[i
] = (GLuint
) value
;
807 for (i
= 0; i
< n
; i
++)
808 indexes
[i
] = (GLuint
) s
[i
];
812 case GL_HALF_FLOAT_ARB
:
815 const GLhalfARB
*s
= (const GLhalfARB
*) src
;
816 if (unpack
->SwapBytes
) {
817 for (i
= 0; i
< n
; i
++) {
818 GLhalfARB value
= s
[i
];
820 indexes
[i
] = (GLuint
) _mesa_half_to_float(value
);
824 for (i
= 0; i
< n
; i
++)
825 indexes
[i
] = (GLuint
) _mesa_half_to_float(s
[i
]);
829 case GL_UNSIGNED_INT_24_8_EXT
:
832 const GLuint
*s
= (const GLuint
*) src
;
833 if (unpack
->SwapBytes
) {
834 for (i
= 0; i
< n
; i
++) {
837 indexes
[i
] = value
& 0xff; /* lower 8 bits */
841 for (i
= 0; i
< n
; i
++)
842 indexes
[i
] = s
[i
] & 0xff; /* lower 8 bits */
846 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
849 const GLuint
*s
= (const GLuint
*) src
;
850 if (unpack
->SwapBytes
) {
851 for (i
= 0; i
< n
; i
++) {
852 GLuint value
= s
[i
*2+1];
854 indexes
[i
] = value
& 0xff; /* lower 8 bits */
858 for (i
= 0; i
< n
; i
++)
859 indexes
[i
] = s
[i
*2+1] & 0xff; /* lower 8 bits */
865 _mesa_problem(NULL
, "bad srcType in extract_uint_indexes");
872 clamp_float_to_uint(GLfloat f
)
874 return f
< 0.0F
? 0 : F_TO_I(f
);
879 clamp_half_to_uint(GLhalfARB h
)
881 GLfloat f
= _mesa_half_to_float(h
);
882 return f
< 0.0F
? 0 : F_TO_I(f
);
887 * Unpack a row of color index data from a client buffer according to
888 * the pixel unpacking parameters.
889 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
891 * Args: ctx - the context
892 * n - number of pixels
893 * dstType - destination data type
894 * dest - destination array
895 * srcType - source pixel type
896 * source - source data pointer
897 * srcPacking - pixel unpacking parameters
898 * transferOps - the pixel transfer operations to apply
901 _mesa_unpack_index_span( struct gl_context
*ctx
, GLuint n
,
902 GLenum dstType
, GLvoid
*dest
,
903 GLenum srcType
, const GLvoid
*source
,
904 const struct gl_pixelstore_attrib
*srcPacking
,
905 GLbitfield transferOps
)
907 ASSERT(srcType
== GL_BITMAP
||
908 srcType
== GL_UNSIGNED_BYTE
||
909 srcType
== GL_BYTE
||
910 srcType
== GL_UNSIGNED_SHORT
||
911 srcType
== GL_SHORT
||
912 srcType
== GL_UNSIGNED_INT
||
914 srcType
== GL_HALF_FLOAT_ARB
||
915 srcType
== GL_FLOAT
);
917 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
918 dstType
== GL_UNSIGNED_SHORT
||
919 dstType
== GL_UNSIGNED_INT
);
922 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
925 * Try simple cases first
927 if (transferOps
== 0 && srcType
== GL_UNSIGNED_BYTE
928 && dstType
== GL_UNSIGNED_BYTE
) {
929 memcpy(dest
, source
, n
* sizeof(GLubyte
));
931 else if (transferOps
== 0 && srcType
== GL_UNSIGNED_INT
932 && dstType
== GL_UNSIGNED_INT
&& !srcPacking
->SwapBytes
) {
933 memcpy(dest
, source
, n
* sizeof(GLuint
));
939 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
942 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
946 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
950 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
952 /* convert to dest type */
954 case GL_UNSIGNED_BYTE
:
956 GLubyte
*dst
= (GLubyte
*) dest
;
958 for (i
= 0; i
< n
; i
++) {
959 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
963 case GL_UNSIGNED_SHORT
:
965 GLuint
*dst
= (GLuint
*) dest
;
967 for (i
= 0; i
< n
; i
++) {
968 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
972 case GL_UNSIGNED_INT
:
973 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
976 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
985 _mesa_pack_index_span( struct gl_context
*ctx
, GLuint n
,
986 GLenum dstType
, GLvoid
*dest
, const GLuint
*source
,
987 const struct gl_pixelstore_attrib
*dstPacking
,
988 GLbitfield transferOps
)
990 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
993 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
997 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
999 if (transferOps
& (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
)) {
1000 /* make a copy of input */
1001 memcpy(indexes
, source
, n
* sizeof(GLuint
));
1002 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
1007 case GL_UNSIGNED_BYTE
:
1009 GLubyte
*dst
= (GLubyte
*) dest
;
1011 for (i
= 0; i
< n
; i
++) {
1012 *dst
++ = (GLubyte
) source
[i
];
1018 GLbyte
*dst
= (GLbyte
*) dest
;
1020 for (i
= 0; i
< n
; i
++) {
1021 dst
[i
] = (GLbyte
) source
[i
];
1025 case GL_UNSIGNED_SHORT
:
1027 GLushort
*dst
= (GLushort
*) dest
;
1029 for (i
= 0; i
< n
; i
++) {
1030 dst
[i
] = (GLushort
) source
[i
];
1032 if (dstPacking
->SwapBytes
) {
1033 _mesa_swap2( (GLushort
*) dst
, n
);
1039 GLshort
*dst
= (GLshort
*) dest
;
1041 for (i
= 0; i
< n
; i
++) {
1042 dst
[i
] = (GLshort
) source
[i
];
1044 if (dstPacking
->SwapBytes
) {
1045 _mesa_swap2( (GLushort
*) dst
, n
);
1049 case GL_UNSIGNED_INT
:
1051 GLuint
*dst
= (GLuint
*) dest
;
1053 for (i
= 0; i
< n
; i
++) {
1054 dst
[i
] = (GLuint
) source
[i
];
1056 if (dstPacking
->SwapBytes
) {
1057 _mesa_swap4( (GLuint
*) dst
, n
);
1063 GLint
*dst
= (GLint
*) dest
;
1065 for (i
= 0; i
< n
; i
++) {
1066 dst
[i
] = (GLint
) source
[i
];
1068 if (dstPacking
->SwapBytes
) {
1069 _mesa_swap4( (GLuint
*) dst
, n
);
1075 GLfloat
*dst
= (GLfloat
*) dest
;
1077 for (i
= 0; i
< n
; i
++) {
1078 dst
[i
] = (GLfloat
) source
[i
];
1080 if (dstPacking
->SwapBytes
) {
1081 _mesa_swap4( (GLuint
*) dst
, n
);
1085 case GL_HALF_FLOAT_ARB
:
1087 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
1089 for (i
= 0; i
< n
; i
++) {
1090 dst
[i
] = _mesa_float_to_half((GLfloat
) source
[i
]);
1092 if (dstPacking
->SwapBytes
) {
1093 _mesa_swap2( (GLushort
*) dst
, n
);
1098 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
1106 * Unpack a row of stencil data from a client buffer according to
1107 * the pixel unpacking parameters.
1108 * This is (or will be) used by glDrawPixels
1110 * Args: ctx - the context
1111 * n - number of pixels
1112 * dstType - destination data type
1113 * dest - destination array
1114 * srcType - source pixel type
1115 * source - source data pointer
1116 * srcPacking - pixel unpacking parameters
1117 * transferOps - apply offset/bias/lookup ops?
1120 _mesa_unpack_stencil_span( struct gl_context
*ctx
, GLuint n
,
1121 GLenum dstType
, GLvoid
*dest
,
1122 GLenum srcType
, const GLvoid
*source
,
1123 const struct gl_pixelstore_attrib
*srcPacking
,
1124 GLbitfield transferOps
)
1126 ASSERT(srcType
== GL_BITMAP
||
1127 srcType
== GL_UNSIGNED_BYTE
||
1128 srcType
== GL_BYTE
||
1129 srcType
== GL_UNSIGNED_SHORT
||
1130 srcType
== GL_SHORT
||
1131 srcType
== GL_UNSIGNED_INT
||
1132 srcType
== GL_INT
||
1133 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
1134 srcType
== GL_HALF_FLOAT_ARB
||
1135 srcType
== GL_FLOAT
||
1136 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
1138 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
1139 dstType
== GL_UNSIGNED_SHORT
||
1140 dstType
== GL_UNSIGNED_INT
||
1141 dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
1143 /* only shift and offset apply to stencil */
1144 transferOps
&= IMAGE_SHIFT_OFFSET_BIT
;
1147 * Try simple cases first
1149 if (transferOps
== 0 &&
1150 !ctx
->Pixel
.MapStencilFlag
&&
1151 srcType
== GL_UNSIGNED_BYTE
&&
1152 dstType
== GL_UNSIGNED_BYTE
) {
1153 memcpy(dest
, source
, n
* sizeof(GLubyte
));
1155 else if (transferOps
== 0 &&
1156 !ctx
->Pixel
.MapStencilFlag
&&
1157 srcType
== GL_UNSIGNED_INT
&&
1158 dstType
== GL_UNSIGNED_INT
&&
1159 !srcPacking
->SwapBytes
) {
1160 memcpy(dest
, source
, n
* sizeof(GLuint
));
1166 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
1169 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil unpacking");
1173 extract_uint_indexes(n
, indexes
, GL_STENCIL_INDEX
, srcType
, source
,
1176 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
1177 /* shift and offset indexes */
1178 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
1181 if (ctx
->Pixel
.MapStencilFlag
) {
1182 /* Apply stencil lookup table */
1183 const GLuint mask
= ctx
->PixelMaps
.StoS
.Size
- 1;
1185 for (i
= 0; i
< n
; i
++) {
1186 indexes
[i
] = (GLuint
)ctx
->PixelMaps
.StoS
.Map
[ indexes
[i
] & mask
];
1190 /* convert to dest type */
1192 case GL_UNSIGNED_BYTE
:
1194 GLubyte
*dst
= (GLubyte
*) dest
;
1196 for (i
= 0; i
< n
; i
++) {
1197 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
1201 case GL_UNSIGNED_SHORT
:
1203 GLuint
*dst
= (GLuint
*) dest
;
1205 for (i
= 0; i
< n
; i
++) {
1206 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
1210 case GL_UNSIGNED_INT
:
1211 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
1213 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1215 GLuint
*dst
= (GLuint
*) dest
;
1217 for (i
= 0; i
< n
; i
++) {
1218 dst
[i
*2+1] = indexes
[i
] & 0xff; /* lower 8 bits */
1223 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
1232 _mesa_pack_stencil_span( struct gl_context
*ctx
, GLuint n
,
1233 GLenum dstType
, GLvoid
*dest
, const GLubyte
*source
,
1234 const struct gl_pixelstore_attrib
*dstPacking
)
1236 GLubyte
*stencil
= malloc(n
* sizeof(GLubyte
));
1239 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil packing");
1243 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
1244 ctx
->Pixel
.MapStencilFlag
) {
1245 /* make a copy of input */
1246 memcpy(stencil
, source
, n
* sizeof(GLubyte
));
1247 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencil
);
1252 case GL_UNSIGNED_BYTE
:
1253 memcpy(dest
, source
, n
);
1257 GLbyte
*dst
= (GLbyte
*) dest
;
1260 dst
[i
] = (GLbyte
) (source
[i
] & 0x7f);
1264 case GL_UNSIGNED_SHORT
:
1266 GLushort
*dst
= (GLushort
*) dest
;
1269 dst
[i
] = (GLushort
) source
[i
];
1271 if (dstPacking
->SwapBytes
) {
1272 _mesa_swap2( (GLushort
*) dst
, n
);
1278 GLshort
*dst
= (GLshort
*) dest
;
1281 dst
[i
] = (GLshort
) source
[i
];
1283 if (dstPacking
->SwapBytes
) {
1284 _mesa_swap2( (GLushort
*) dst
, n
);
1288 case GL_UNSIGNED_INT
:
1290 GLuint
*dst
= (GLuint
*) dest
;
1293 dst
[i
] = (GLuint
) source
[i
];
1295 if (dstPacking
->SwapBytes
) {
1296 _mesa_swap4( (GLuint
*) dst
, n
);
1302 GLint
*dst
= (GLint
*) dest
;
1305 dst
[i
] = (GLint
) source
[i
];
1307 if (dstPacking
->SwapBytes
) {
1308 _mesa_swap4( (GLuint
*) dst
, n
);
1314 GLfloat
*dst
= (GLfloat
*) dest
;
1317 dst
[i
] = (GLfloat
) source
[i
];
1319 if (dstPacking
->SwapBytes
) {
1320 _mesa_swap4( (GLuint
*) dst
, n
);
1324 case GL_HALF_FLOAT_ARB
:
1326 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
1329 dst
[i
] = _mesa_float_to_half( (float) source
[i
] );
1331 if (dstPacking
->SwapBytes
) {
1332 _mesa_swap2( (GLushort
*) dst
, n
);
1337 if (dstPacking
->LsbFirst
) {
1338 GLubyte
*dst
= (GLubyte
*) dest
;
1341 for (i
= 0; i
< n
; i
++) {
1344 *dst
|= ((source
[i
] != 0) << shift
);
1353 GLubyte
*dst
= (GLubyte
*) dest
;
1356 for (i
= 0; i
< n
; i
++) {
1359 *dst
|= ((source
[i
] != 0) << shift
);
1369 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
1375 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
1378 const GLTYPE *src = (const GLTYPE *)source; \
1379 for (i = 0; i < n; i++) { \
1380 GLTYPE value = src[i]; \
1381 if (srcPacking->SwapBytes) { \
1382 if (sizeof(GLTYPE) == 2) { \
1384 } else if (sizeof(GLTYPE) == 4) { \
1388 depthValues[i] = GLTYPE2FLOAT(value); \
1394 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
1395 * or GLfloat values.
1396 * The glPixelTransfer (scale/bias) params will be applied.
1398 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
1399 * \param depthMax max value for returned GLushort or GLuint values
1400 * (ignored for GLfloat).
1403 _mesa_unpack_depth_span( struct gl_context
*ctx
, GLuint n
,
1404 GLenum dstType
, GLvoid
*dest
, GLuint depthMax
,
1405 GLenum srcType
, const GLvoid
*source
,
1406 const struct gl_pixelstore_attrib
*srcPacking
)
1408 GLfloat
*depthTemp
= NULL
, *depthValues
;
1409 GLboolean needClamp
= GL_FALSE
;
1411 /* Look for special cases first.
1412 * Not only are these faster, they're less prone to numeric conversion
1413 * problems. Otherwise, converting from an int type to a float then
1414 * back to an int type can introduce errors that will show up as
1415 * artifacts in things like depth peeling which uses glCopyTexImage.
1417 if (ctx
->Pixel
.DepthScale
== 1.0 && ctx
->Pixel
.DepthBias
== 0.0) {
1418 if (srcType
== GL_UNSIGNED_INT
&& dstType
== GL_UNSIGNED_SHORT
) {
1419 const GLuint
*src
= (const GLuint
*) source
;
1420 GLushort
*dst
= (GLushort
*) dest
;
1422 for (i
= 0; i
< n
; i
++) {
1423 dst
[i
] = src
[i
] >> 16;
1427 if (srcType
== GL_UNSIGNED_SHORT
1428 && dstType
== GL_UNSIGNED_INT
1429 && depthMax
== 0xffffffff) {
1430 const GLushort
*src
= (const GLushort
*) source
;
1431 GLuint
*dst
= (GLuint
*) dest
;
1433 for (i
= 0; i
< n
; i
++) {
1434 dst
[i
] = src
[i
] | (src
[i
] << 16);
1438 if (srcType
== GL_UNSIGNED_INT_24_8
1439 && dstType
== GL_UNSIGNED_INT
1440 && depthMax
== 0xffffff) {
1441 const GLuint
*src
= (const GLuint
*) source
;
1442 GLuint
*dst
= (GLuint
*) dest
;
1444 for (i
= 0; i
< n
; i
++) {
1445 dst
[i
] = src
[i
] >> 8;
1449 /* XXX may want to add additional cases here someday */
1452 /* general case path follows */
1454 if (dstType
== GL_FLOAT
) {
1455 depthValues
= (GLfloat
*) dest
;
1458 depthTemp
= malloc(n
* sizeof(GLfloat
));
1460 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1464 depthValues
= depthTemp
;
1467 /* Convert incoming values to GLfloat. Some conversions will require
1472 DEPTH_VALUES(GLbyte
, BYTE_TO_FLOATZ
);
1473 needClamp
= GL_TRUE
;
1475 case GL_UNSIGNED_BYTE
:
1476 DEPTH_VALUES(GLubyte
, UBYTE_TO_FLOAT
);
1479 DEPTH_VALUES(GLshort
, SHORT_TO_FLOATZ
);
1480 needClamp
= GL_TRUE
;
1482 case GL_UNSIGNED_SHORT
:
1483 DEPTH_VALUES(GLushort
, USHORT_TO_FLOAT
);
1486 DEPTH_VALUES(GLint
, INT_TO_FLOAT
);
1487 needClamp
= GL_TRUE
;
1489 case GL_UNSIGNED_INT
:
1490 DEPTH_VALUES(GLuint
, UINT_TO_FLOAT
);
1492 case GL_UNSIGNED_INT_24_8_EXT
: /* GL_EXT_packed_depth_stencil */
1493 if (dstType
== GL_UNSIGNED_INT_24_8_EXT
&&
1494 depthMax
== 0xffffff &&
1495 ctx
->Pixel
.DepthScale
== 1.0 &&
1496 ctx
->Pixel
.DepthBias
== 0.0) {
1497 const GLuint
*src
= (const GLuint
*) source
;
1498 GLuint
*zValues
= (GLuint
*) dest
;
1500 for (i
= 0; i
< n
; i
++) {
1501 GLuint value
= src
[i
];
1502 if (srcPacking
->SwapBytes
) {
1505 zValues
[i
] = value
& 0xffffff00;
1511 const GLuint
*src
= (const GLuint
*) source
;
1512 const GLfloat scale
= 1.0f
/ 0xffffff;
1514 for (i
= 0; i
< n
; i
++) {
1515 GLuint value
= src
[i
];
1516 if (srcPacking
->SwapBytes
) {
1519 depthValues
[i
] = (value
>> 8) * scale
;
1523 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1526 const GLfloat
*src
= (const GLfloat
*)source
;
1527 for (i
= 0; i
< n
; i
++) {
1528 GLfloat value
= src
[i
* 2];
1529 if (srcPacking
->SwapBytes
) {
1532 depthValues
[i
] = value
;
1534 needClamp
= GL_TRUE
;
1538 DEPTH_VALUES(GLfloat
, 1*);
1539 needClamp
= GL_TRUE
;
1541 case GL_HALF_FLOAT_ARB
:
1544 const GLhalfARB
*src
= (const GLhalfARB
*) source
;
1545 for (i
= 0; i
< n
; i
++) {
1546 GLhalfARB value
= src
[i
];
1547 if (srcPacking
->SwapBytes
) {
1550 depthValues
[i
] = _mesa_half_to_float(value
);
1552 needClamp
= GL_TRUE
;
1556 _mesa_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
1561 /* apply depth scale and bias */
1563 const GLfloat scale
= ctx
->Pixel
.DepthScale
;
1564 const GLfloat bias
= ctx
->Pixel
.DepthBias
;
1565 if (scale
!= 1.0 || bias
!= 0.0) {
1567 for (i
= 0; i
< n
; i
++) {
1568 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
1570 needClamp
= GL_TRUE
;
1574 /* clamp to [0, 1] */
1577 for (i
= 0; i
< n
; i
++) {
1578 depthValues
[i
] = (GLfloat
)CLAMP(depthValues
[i
], 0.0, 1.0);
1583 * Convert values to dstType
1585 if (dstType
== GL_UNSIGNED_INT
) {
1586 GLuint
*zValues
= (GLuint
*) dest
;
1588 if (depthMax
<= 0xffffff) {
1589 /* no overflow worries */
1590 for (i
= 0; i
< n
; i
++) {
1591 zValues
[i
] = (GLuint
) (depthValues
[i
] * (GLfloat
) depthMax
);
1595 /* need to use double precision to prevent overflow problems */
1596 for (i
= 0; i
< n
; i
++) {
1597 GLdouble z
= depthValues
[i
] * (GLdouble
) depthMax
;
1598 if (z
>= (GLdouble
) 0xffffffff)
1599 zValues
[i
] = 0xffffffff;
1601 zValues
[i
] = (GLuint
) z
;
1605 else if (dstType
== GL_UNSIGNED_SHORT
) {
1606 GLushort
*zValues
= (GLushort
*) dest
;
1608 ASSERT(depthMax
<= 0xffff);
1609 for (i
= 0; i
< n
; i
++) {
1610 zValues
[i
] = (GLushort
) (depthValues
[i
] * (GLfloat
) depthMax
);
1613 else if (dstType
== GL_FLOAT
) {
1614 /* Nothing to do. depthValues is pointing to dest. */
1616 else if (dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
) {
1617 GLfloat
*zValues
= (GLfloat
*) dest
;
1619 for (i
= 0; i
< n
; i
++) {
1620 zValues
[i
*2] = depthValues
[i
];
1632 * Pack an array of depth values. The values are floats in [0,1].
1635 _mesa_pack_depth_span( struct gl_context
*ctx
, GLuint n
, GLvoid
*dest
,
1636 GLenum dstType
, const GLfloat
*depthSpan
,
1637 const struct gl_pixelstore_attrib
*dstPacking
)
1639 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1641 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1645 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
1646 memcpy(depthCopy
, depthSpan
, n
* sizeof(GLfloat
));
1647 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1648 depthSpan
= depthCopy
;
1652 case GL_UNSIGNED_BYTE
:
1654 GLubyte
*dst
= (GLubyte
*) dest
;
1656 for (i
= 0; i
< n
; i
++) {
1657 dst
[i
] = FLOAT_TO_UBYTE( depthSpan
[i
] );
1663 GLbyte
*dst
= (GLbyte
*) dest
;
1665 for (i
= 0; i
< n
; i
++) {
1666 dst
[i
] = FLOAT_TO_BYTE( depthSpan
[i
] );
1670 case GL_UNSIGNED_SHORT
:
1672 GLushort
*dst
= (GLushort
*) dest
;
1674 for (i
= 0; i
< n
; i
++) {
1675 CLAMPED_FLOAT_TO_USHORT(dst
[i
], depthSpan
[i
]);
1677 if (dstPacking
->SwapBytes
) {
1678 _mesa_swap2( (GLushort
*) dst
, n
);
1684 GLshort
*dst
= (GLshort
*) dest
;
1686 for (i
= 0; i
< n
; i
++) {
1687 dst
[i
] = FLOAT_TO_SHORT( depthSpan
[i
] );
1689 if (dstPacking
->SwapBytes
) {
1690 _mesa_swap2( (GLushort
*) dst
, n
);
1694 case GL_UNSIGNED_INT
:
1696 GLuint
*dst
= (GLuint
*) dest
;
1698 for (i
= 0; i
< n
; i
++) {
1699 dst
[i
] = FLOAT_TO_UINT( depthSpan
[i
] );
1701 if (dstPacking
->SwapBytes
) {
1702 _mesa_swap4( (GLuint
*) dst
, n
);
1708 GLint
*dst
= (GLint
*) dest
;
1710 for (i
= 0; i
< n
; i
++) {
1711 dst
[i
] = FLOAT_TO_INT( depthSpan
[i
] );
1713 if (dstPacking
->SwapBytes
) {
1714 _mesa_swap4( (GLuint
*) dst
, n
);
1720 GLfloat
*dst
= (GLfloat
*) dest
;
1722 for (i
= 0; i
< n
; i
++) {
1723 dst
[i
] = depthSpan
[i
];
1725 if (dstPacking
->SwapBytes
) {
1726 _mesa_swap4( (GLuint
*) dst
, n
);
1730 case GL_HALF_FLOAT_ARB
:
1732 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
1734 for (i
= 0; i
< n
; i
++) {
1735 dst
[i
] = _mesa_float_to_half(depthSpan
[i
]);
1737 if (dstPacking
->SwapBytes
) {
1738 _mesa_swap2( (GLushort
*) dst
, n
);
1743 _mesa_problem(ctx
, "bad type in _mesa_pack_depth_span");
1752 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
1755 _mesa_pack_depth_stencil_span(struct gl_context
*ctx
,GLuint n
,
1756 GLenum dstType
, GLuint
*dest
,
1757 const GLfloat
*depthVals
,
1758 const GLubyte
*stencilVals
,
1759 const struct gl_pixelstore_attrib
*dstPacking
)
1761 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1762 GLubyte
*stencilCopy
= malloc(n
* sizeof(GLubyte
));
1765 if (!depthCopy
|| !stencilCopy
) {
1766 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1772 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
1773 memcpy(depthCopy
, depthVals
, n
* sizeof(GLfloat
));
1774 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1775 depthVals
= depthCopy
;
1778 if (ctx
->Pixel
.IndexShift
||
1779 ctx
->Pixel
.IndexOffset
||
1780 ctx
->Pixel
.MapStencilFlag
) {
1781 memcpy(stencilCopy
, stencilVals
, n
* sizeof(GLubyte
));
1782 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencilCopy
);
1783 stencilVals
= stencilCopy
;
1787 case GL_UNSIGNED_INT_24_8
:
1788 for (i
= 0; i
< n
; i
++) {
1789 GLuint z
= (GLuint
) (depthVals
[i
] * 0xffffff);
1790 dest
[i
] = (z
<< 8) | (stencilVals
[i
] & 0xff);
1793 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1794 for (i
= 0; i
< n
; i
++) {
1795 ((GLfloat
*)dest
)[i
*2] = depthVals
[i
];
1796 dest
[i
*2+1] = stencilVals
[i
] & 0xff;
1801 if (dstPacking
->SwapBytes
) {
1802 _mesa_swap4(dest
, n
);
1812 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
1813 * Return all image data in a contiguous block. This is used when we
1814 * compile glDrawPixels, glTexImage, etc into a display list. We
1815 * need a copy of the data in a standard format.
1818 _mesa_unpack_image( GLuint dimensions
,
1819 GLsizei width
, GLsizei height
, GLsizei depth
,
1820 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1821 const struct gl_pixelstore_attrib
*unpack
)
1823 GLint bytesPerRow
, compsPerRow
;
1824 GLboolean flipBytes
, swap2
, swap4
;
1827 return NULL
; /* not necessarily an error */
1829 if (width
<= 0 || height
<= 0 || depth
<= 0)
1830 return NULL
; /* generate error later */
1832 if (type
== GL_BITMAP
) {
1833 bytesPerRow
= (width
+ 7) >> 3;
1834 flipBytes
= unpack
->LsbFirst
;
1835 swap2
= swap4
= GL_FALSE
;
1839 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1840 GLint components
= _mesa_components_in_format(format
);
1843 if (_mesa_type_is_packed(type
))
1846 if (bytesPerPixel
<= 0 || components
<= 0)
1847 return NULL
; /* bad format or type. generate error later */
1848 bytesPerRow
= bytesPerPixel
* width
;
1849 bytesPerComp
= bytesPerPixel
/ components
;
1850 flipBytes
= GL_FALSE
;
1851 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
1852 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
1853 compsPerRow
= components
* width
;
1854 assert(compsPerRow
>= width
);
1859 = malloc(bytesPerRow
* height
* depth
);
1863 return NULL
; /* generate GL_OUT_OF_MEMORY later */
1866 for (img
= 0; img
< depth
; img
++) {
1867 for (row
= 0; row
< height
; row
++) {
1868 const GLvoid
*src
= _mesa_image_address(dimensions
, unpack
, pixels
,
1869 width
, height
, format
, type
, img
, row
, 0);
1871 if ((type
== GL_BITMAP
) && (unpack
->SkipPixels
& 0x7)) {
1873 flipBytes
= GL_FALSE
;
1874 if (unpack
->LsbFirst
) {
1875 GLubyte srcMask
= 1 << (unpack
->SkipPixels
& 0x7);
1876 GLubyte dstMask
= 128;
1877 const GLubyte
*s
= src
;
1880 for (i
= 0; i
< width
; i
++) {
1884 if (srcMask
== 128) {
1889 srcMask
= srcMask
<< 1;
1897 dstMask
= dstMask
>> 1;
1902 GLubyte srcMask
= 128 >> (unpack
->SkipPixels
& 0x7);
1903 GLubyte dstMask
= 128;
1904 const GLubyte
*s
= src
;
1907 for (i
= 0; i
< width
; i
++) {
1916 srcMask
= srcMask
>> 1;
1924 dstMask
= dstMask
>> 1;
1930 memcpy(dst
, src
, bytesPerRow
);
1933 /* byte flipping/swapping */
1935 flip_bytes((GLubyte
*) dst
, bytesPerRow
);
1938 _mesa_swap2((GLushort
*) dst
, compsPerRow
);
1941 _mesa_swap4((GLuint
*) dst
, compsPerRow
);
1953 * If we unpack colors from a luminance surface, we'll get pixel colors
1954 * such as (l, l, l, a).
1955 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
1956 * function will compute L=R+G+B before packing. The net effect is we'll
1957 * accidentally store luminance values = 3*l.
1958 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
1960 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
1963 * Finally, we also need to do this when the actual surface format does
1964 * not match the logical surface format. For example, suppose the user
1965 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
1966 * Again, we'll get pixel values like (l,l,l,a).
1969 _mesa_rebase_rgba_float(GLuint n
, GLfloat rgba
[][4], GLenum baseFormat
)
1973 switch (baseFormat
) {
1975 for (i
= 0; i
< n
; i
++) {
1976 rgba
[i
][RCOMP
] = 0.0F
;
1977 rgba
[i
][GCOMP
] = 0.0F
;
1978 rgba
[i
][BCOMP
] = 0.0F
;
1984 for (i
= 0; i
< n
; i
++) {
1985 rgba
[i
][GCOMP
] = 0.0F
;
1986 rgba
[i
][BCOMP
] = 0.0F
;
1987 rgba
[i
][ACOMP
] = 1.0F
;
1990 case GL_LUMINANCE_ALPHA
:
1991 for (i
= 0; i
< n
; i
++) {
1992 rgba
[i
][GCOMP
] = 0.0F
;
1993 rgba
[i
][BCOMP
] = 0.0F
;
1997 for (i
= 0; i
< n
; i
++) {
1998 rgba
[i
][ACOMP
] = 1.0F
;
2002 for (i
= 0; i
< n
; i
++) {
2003 rgba
[i
][BCOMP
] = 0.0F
;
2004 rgba
[i
][ACOMP
] = 1.0F
;
2008 for (i
= 0; i
< n
; i
++) {
2009 rgba
[i
][GCOMP
] = 0.0F
;
2010 rgba
[i
][BCOMP
] = 0.0F
;
2011 rgba
[i
][ACOMP
] = 1.0F
;
2023 * As above, but GLuint components.
2026 _mesa_rebase_rgba_uint(GLuint n
, GLuint rgba
[][4], GLenum baseFormat
)
2030 switch (baseFormat
) {
2032 for (i
= 0; i
< n
; i
++) {
2041 for (i
= 0; i
< n
; i
++) {
2047 case GL_LUMINANCE_ALPHA
:
2048 for (i
= 0; i
< n
; i
++) {
2054 for (i
= 0; i
< n
; i
++) {
2059 for (i
= 0; i
< n
; i
++) {
2065 for (i
= 0; i
< n
; i
++) {
2077 _mesa_pack_luminance_from_rgba_float(GLuint n
, GLfloat rgba
[][4],
2078 GLvoid
*dstAddr
, GLenum dst_format
,
2079 GLbitfield transferOps
)
2082 GLfloat
*dst
= (GLfloat
*) dstAddr
;
2084 switch (dst_format
) {
2086 if (transferOps
& IMAGE_CLAMP_BIT
) {
2087 for (i
= 0; i
< n
; i
++) {
2088 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
2089 dst
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
2092 for (i
= 0; i
< n
; i
++) {
2093 dst
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
2097 case GL_LUMINANCE_ALPHA
:
2098 if (transferOps
& IMAGE_CLAMP_BIT
) {
2099 for (i
= 0; i
< n
; i
++) {
2100 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
2101 dst
[2*i
] = CLAMP(sum
, 0.0F
, 1.0F
);
2102 dst
[2*i
+1] = rgba
[i
][ACOMP
];
2105 for (i
= 0; i
< n
; i
++) {
2106 dst
[2*i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
2107 dst
[2*i
+1] = rgba
[i
][ACOMP
];
2112 assert(!"Unsupported format");
2117 clamp_sint64_to_sint32(int64_t src
)
2119 return CLAMP(src
, INT32_MIN
, INT32_MAX
);
2123 clamp_sint64_to_uint32(int64_t src
)
2125 return CLAMP(src
, 0, UINT32_MAX
);
2129 clamp_uint64_to_uint32(uint64_t src
)
2131 return MIN2(src
, UINT32_MAX
);
2135 clamp_uint64_to_sint32(uint64_t src
)
2137 return MIN2(src
, INT32_MAX
);
2141 convert_integer_luminance64(int64_t src64
, int bits
,
2142 bool dst_is_signed
, bool src_is_signed
)
2146 /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
2147 * any signed<->unsigned conversion too.
2149 if (src_is_signed
&& dst_is_signed
)
2150 src32
= clamp_sint64_to_sint32(src64
);
2151 else if (src_is_signed
&& !dst_is_signed
)
2152 src32
= clamp_sint64_to_uint32(src64
);
2153 else if (!src_is_signed
&& dst_is_signed
)
2154 src32
= clamp_uint64_to_sint32(src64
);
2156 src32
= clamp_uint64_to_uint32(src64
);
2158 /* If the dst type is < 32-bit, we need an extra clamp */
2163 return _mesa_signed_to_signed(src32
, bits
);
2165 return _mesa_unsigned_to_unsigned(src32
, bits
);
2170 convert_integer(int32_t src
, int bits
, bool dst_is_signed
, bool src_is_signed
)
2172 if (src_is_signed
&& dst_is_signed
)
2173 return _mesa_signed_to_signed(src
, bits
);
2174 else if (src_is_signed
&& !dst_is_signed
)
2175 return _mesa_signed_to_unsigned(src
, bits
);
2176 else if (!src_is_signed
&& dst_is_signed
)
2177 return _mesa_unsigned_to_signed(src
, bits
);
2179 return _mesa_unsigned_to_unsigned(src
, bits
);
2183 _mesa_pack_luminance_from_rgba_integer(GLuint n
,
2184 GLuint rgba
[][4], bool rgba_is_signed
,
2189 assert(dst_format
== GL_LUMINANCE_INTEGER_EXT
||
2190 dst_format
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
2194 int32_t lum32
, alpha
;
2198 /* We first compute luminance values as a 64-bit addition of the
2199 * 32-bit R,G,B components, then we clamp the result to the dst type size.
2201 * Notice that this operation involves casting the 32-bit R,G,B components
2202 * to 64-bit before the addition. Since rgba is defined as a GLuint array
2203 * we need to be careful when rgba packs signed data and make sure
2204 * that we cast to a 32-bit signed integer values before casting them to
2205 * 64-bit signed integers.
2207 dst_is_signed
= (dst_type
== GL_BYTE
|| dst_type
== GL_SHORT
||
2208 dst_type
== GL_INT
);
2210 dst_bits
= _mesa_sizeof_type(dst_type
) * 8;
2211 assert(dst_bits
> 0);
2213 switch (dst_format
) {
2214 case GL_LUMINANCE_INTEGER_EXT
:
2215 for (i
= 0; i
< n
; i
++) {
2216 if (!rgba_is_signed
) {
2217 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
2218 (uint64_t) rgba
[i
][GCOMP
] +
2219 (uint64_t) rgba
[i
][BCOMP
];
2221 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
2222 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
2223 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
2225 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
2226 dst_is_signed
, rgba_is_signed
);
2229 case GL_UNSIGNED_BYTE
: {
2230 GLbyte
*dst
= (GLbyte
*) dstAddr
;
2235 case GL_UNSIGNED_SHORT
: {
2236 GLshort
*dst
= (GLshort
*) dstAddr
;
2241 case GL_UNSIGNED_INT
: {
2242 GLint
*dst
= (GLint
*) dstAddr
;
2249 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
2250 for (i
= 0; i
< n
; i
++) {
2251 if (!rgba_is_signed
) {
2252 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
2253 (uint64_t) rgba
[i
][GCOMP
] +
2254 (uint64_t) rgba
[i
][BCOMP
];
2256 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
2257 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
2258 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
2260 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
2261 dst_is_signed
, rgba_is_signed
);
2262 alpha
= convert_integer(rgba
[i
][ACOMP
], dst_bits
,
2263 dst_is_signed
, rgba_is_signed
);
2266 case GL_UNSIGNED_BYTE
: {
2267 GLbyte
*dst
= (GLbyte
*) dstAddr
;
2272 case GL_UNSIGNED_SHORT
: {
2273 GLshort
*dst
= (GLshort
*) dstAddr
;
2279 case GL_UNSIGNED_INT
: {
2280 GLint
*dst
= (GLint
*) dstAddr
;
2292 _mesa_unpack_color_index_to_rgba_float(struct gl_context
*ctx
, GLuint dims
,
2293 const void *src
, GLenum srcFormat
, GLenum srcType
,
2294 int srcWidth
, int srcHeight
, int srcDepth
,
2295 const struct gl_pixelstore_attrib
*srcPacking
,
2296 GLbitfield transferOps
)
2300 GLfloat
*rgba
, *dstPtr
;
2302 count
= srcWidth
* srcHeight
;
2303 indexes
= malloc(count
* sizeof(GLuint
));
2305 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2309 rgba
= malloc(4 * count
* srcDepth
* sizeof(GLfloat
));
2312 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
2316 /* Convert indexes to RGBA float */
2318 for (img
= 0; img
< srcDepth
; img
++) {
2319 const GLubyte
*srcPtr
=
2320 (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, src
,
2321 srcWidth
, srcHeight
,
2325 extract_uint_indexes(count
, indexes
, srcFormat
, srcType
, srcPtr
, srcPacking
);
2327 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
)
2328 _mesa_shift_and_offset_ci(ctx
, count
, indexes
);
2330 _mesa_map_ci_to_rgba(ctx
, count
, indexes
, (float (*)[4])dstPtr
);
2332 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
2333 * with color indexes.
2335 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
2336 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, count
, (float (*)[4])dstPtr
);
2338 dstPtr
+= srcHeight
* srcWidth
* 4;
2347 _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context
*ctx
, GLuint dims
,
2348 const void *src
, GLenum srcFormat
, GLenum srcType
,
2349 int srcWidth
, int srcHeight
, int srcDepth
,
2350 const struct gl_pixelstore_attrib
*srcPacking
,
2351 GLbitfield transferOps
)
2357 transferOps
|= IMAGE_CLAMP_BIT
;
2358 rgba
= _mesa_unpack_color_index_to_rgba_float(ctx
, dims
,
2359 src
, srcFormat
, srcType
,
2360 srcWidth
, srcHeight
, srcDepth
,
2361 srcPacking
, transferOps
);
2363 count
= srcWidth
* srcHeight
* srcDepth
;
2364 dst
= malloc(count
* 4 * sizeof(GLubyte
));
2365 for (i
= 0; i
< count
; i
++) {
2366 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 0], rgba
[i
* 4 + 0]);
2367 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 1], rgba
[i
* 4 + 1]);
2368 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 2], rgba
[i
* 4 + 2]);
2369 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 3], rgba
[i
* 4 + 3]);