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
;
235 #define SWAP2BYTE(VALUE) \
237 GLubyte *bytes = (GLubyte *) &(VALUE); \
238 GLubyte tmp = bytes[0]; \
239 bytes[0] = bytes[1]; \
243 #define SWAP4BYTE(VALUE) \
245 GLubyte *bytes = (GLubyte *) &(VALUE); \
246 GLubyte tmp = bytes[0]; \
247 bytes[0] = bytes[3]; \
250 bytes[1] = bytes[2]; \
256 extract_uint_indexes(GLuint n
, GLuint indexes
[],
257 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
258 const struct gl_pixelstore_attrib
*unpack
)
260 ASSERT(srcFormat
== GL_COLOR_INDEX
|| srcFormat
== GL_STENCIL_INDEX
);
262 ASSERT(srcType
== GL_BITMAP
||
263 srcType
== GL_UNSIGNED_BYTE
||
264 srcType
== GL_BYTE
||
265 srcType
== GL_UNSIGNED_SHORT
||
266 srcType
== GL_SHORT
||
267 srcType
== GL_UNSIGNED_INT
||
269 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
270 srcType
== GL_HALF_FLOAT_ARB
||
271 srcType
== GL_FLOAT
||
272 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
277 GLubyte
*ubsrc
= (GLubyte
*) src
;
278 if (unpack
->LsbFirst
) {
279 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
281 for (i
= 0; i
< n
; i
++) {
282 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
293 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
295 for (i
= 0; i
< n
; i
++) {
296 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
308 case GL_UNSIGNED_BYTE
:
311 const GLubyte
*s
= (const GLubyte
*) src
;
312 for (i
= 0; i
< n
; i
++)
319 const GLbyte
*s
= (const GLbyte
*) src
;
320 for (i
= 0; i
< n
; i
++)
324 case GL_UNSIGNED_SHORT
:
327 const GLushort
*s
= (const GLushort
*) src
;
328 if (unpack
->SwapBytes
) {
329 for (i
= 0; i
< n
; i
++) {
330 GLushort value
= s
[i
];
336 for (i
= 0; i
< n
; i
++)
344 const GLshort
*s
= (const GLshort
*) src
;
345 if (unpack
->SwapBytes
) {
346 for (i
= 0; i
< n
; i
++) {
347 GLshort value
= s
[i
];
353 for (i
= 0; i
< n
; i
++)
358 case GL_UNSIGNED_INT
:
361 const GLuint
*s
= (const GLuint
*) src
;
362 if (unpack
->SwapBytes
) {
363 for (i
= 0; i
< n
; i
++) {
370 for (i
= 0; i
< n
; i
++)
378 const GLint
*s
= (const GLint
*) src
;
379 if (unpack
->SwapBytes
) {
380 for (i
= 0; i
< n
; i
++) {
387 for (i
= 0; i
< n
; i
++)
395 const GLfloat
*s
= (const GLfloat
*) src
;
396 if (unpack
->SwapBytes
) {
397 for (i
= 0; i
< n
; i
++) {
398 GLfloat value
= s
[i
];
400 indexes
[i
] = (GLuint
) value
;
404 for (i
= 0; i
< n
; i
++)
405 indexes
[i
] = (GLuint
) s
[i
];
409 case GL_HALF_FLOAT_ARB
:
412 const GLhalfARB
*s
= (const GLhalfARB
*) src
;
413 if (unpack
->SwapBytes
) {
414 for (i
= 0; i
< n
; i
++) {
415 GLhalfARB value
= s
[i
];
417 indexes
[i
] = (GLuint
) _mesa_half_to_float(value
);
421 for (i
= 0; i
< n
; i
++)
422 indexes
[i
] = (GLuint
) _mesa_half_to_float(s
[i
]);
426 case GL_UNSIGNED_INT_24_8_EXT
:
429 const GLuint
*s
= (const GLuint
*) src
;
430 if (unpack
->SwapBytes
) {
431 for (i
= 0; i
< n
; i
++) {
434 indexes
[i
] = value
& 0xff; /* lower 8 bits */
438 for (i
= 0; i
< n
; i
++)
439 indexes
[i
] = s
[i
] & 0xff; /* lower 8 bits */
443 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
446 const GLuint
*s
= (const GLuint
*) src
;
447 if (unpack
->SwapBytes
) {
448 for (i
= 0; i
< n
; i
++) {
449 GLuint value
= s
[i
*2+1];
451 indexes
[i
] = value
& 0xff; /* lower 8 bits */
455 for (i
= 0; i
< n
; i
++)
456 indexes
[i
] = s
[i
*2+1] & 0xff; /* lower 8 bits */
462 _mesa_problem(NULL
, "bad srcType in extract_uint_indexes");
469 clamp_float_to_uint(GLfloat f
)
471 return f
< 0.0F
? 0 : F_TO_I(f
);
476 clamp_half_to_uint(GLhalfARB h
)
478 GLfloat f
= _mesa_half_to_float(h
);
479 return f
< 0.0F
? 0 : F_TO_I(f
);
484 * Unpack a row of color index data from a client buffer according to
485 * the pixel unpacking parameters.
486 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
488 * Args: ctx - the context
489 * n - number of pixels
490 * dstType - destination data type
491 * dest - destination array
492 * srcType - source pixel type
493 * source - source data pointer
494 * srcPacking - pixel unpacking parameters
495 * transferOps - the pixel transfer operations to apply
498 _mesa_unpack_index_span( struct gl_context
*ctx
, GLuint n
,
499 GLenum dstType
, GLvoid
*dest
,
500 GLenum srcType
, const GLvoid
*source
,
501 const struct gl_pixelstore_attrib
*srcPacking
,
502 GLbitfield transferOps
)
504 ASSERT(srcType
== GL_BITMAP
||
505 srcType
== GL_UNSIGNED_BYTE
||
506 srcType
== GL_BYTE
||
507 srcType
== GL_UNSIGNED_SHORT
||
508 srcType
== GL_SHORT
||
509 srcType
== GL_UNSIGNED_INT
||
511 srcType
== GL_HALF_FLOAT_ARB
||
512 srcType
== GL_FLOAT
);
514 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
515 dstType
== GL_UNSIGNED_SHORT
||
516 dstType
== GL_UNSIGNED_INT
);
519 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
522 * Try simple cases first
524 if (transferOps
== 0 && srcType
== GL_UNSIGNED_BYTE
525 && dstType
== GL_UNSIGNED_BYTE
) {
526 memcpy(dest
, source
, n
* sizeof(GLubyte
));
528 else if (transferOps
== 0 && srcType
== GL_UNSIGNED_INT
529 && dstType
== GL_UNSIGNED_INT
&& !srcPacking
->SwapBytes
) {
530 memcpy(dest
, source
, n
* sizeof(GLuint
));
536 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
539 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
543 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
547 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
549 /* convert to dest type */
551 case GL_UNSIGNED_BYTE
:
553 GLubyte
*dst
= (GLubyte
*) dest
;
555 for (i
= 0; i
< n
; i
++) {
556 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
560 case GL_UNSIGNED_SHORT
:
562 GLuint
*dst
= (GLuint
*) dest
;
564 for (i
= 0; i
< n
; i
++) {
565 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
569 case GL_UNSIGNED_INT
:
570 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
573 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
582 _mesa_pack_index_span( struct gl_context
*ctx
, GLuint n
,
583 GLenum dstType
, GLvoid
*dest
, const GLuint
*source
,
584 const struct gl_pixelstore_attrib
*dstPacking
,
585 GLbitfield transferOps
)
587 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
590 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
594 transferOps
&= (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
);
596 if (transferOps
& (IMAGE_MAP_COLOR_BIT
| IMAGE_SHIFT_OFFSET_BIT
)) {
597 /* make a copy of input */
598 memcpy(indexes
, source
, n
* sizeof(GLuint
));
599 _mesa_apply_ci_transfer_ops(ctx
, transferOps
, n
, indexes
);
604 case GL_UNSIGNED_BYTE
:
606 GLubyte
*dst
= (GLubyte
*) dest
;
608 for (i
= 0; i
< n
; i
++) {
609 *dst
++ = (GLubyte
) source
[i
];
615 GLbyte
*dst
= (GLbyte
*) dest
;
617 for (i
= 0; i
< n
; i
++) {
618 dst
[i
] = (GLbyte
) source
[i
];
622 case GL_UNSIGNED_SHORT
:
624 GLushort
*dst
= (GLushort
*) dest
;
626 for (i
= 0; i
< n
; i
++) {
627 dst
[i
] = (GLushort
) source
[i
];
629 if (dstPacking
->SwapBytes
) {
630 _mesa_swap2( (GLushort
*) dst
, n
);
636 GLshort
*dst
= (GLshort
*) dest
;
638 for (i
= 0; i
< n
; i
++) {
639 dst
[i
] = (GLshort
) source
[i
];
641 if (dstPacking
->SwapBytes
) {
642 _mesa_swap2( (GLushort
*) dst
, n
);
646 case GL_UNSIGNED_INT
:
648 GLuint
*dst
= (GLuint
*) dest
;
650 for (i
= 0; i
< n
; i
++) {
651 dst
[i
] = (GLuint
) source
[i
];
653 if (dstPacking
->SwapBytes
) {
654 _mesa_swap4( (GLuint
*) dst
, n
);
660 GLint
*dst
= (GLint
*) dest
;
662 for (i
= 0; i
< n
; i
++) {
663 dst
[i
] = (GLint
) source
[i
];
665 if (dstPacking
->SwapBytes
) {
666 _mesa_swap4( (GLuint
*) dst
, n
);
672 GLfloat
*dst
= (GLfloat
*) dest
;
674 for (i
= 0; i
< n
; i
++) {
675 dst
[i
] = (GLfloat
) source
[i
];
677 if (dstPacking
->SwapBytes
) {
678 _mesa_swap4( (GLuint
*) dst
, n
);
682 case GL_HALF_FLOAT_ARB
:
684 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
686 for (i
= 0; i
< n
; i
++) {
687 dst
[i
] = _mesa_float_to_half((GLfloat
) source
[i
]);
689 if (dstPacking
->SwapBytes
) {
690 _mesa_swap2( (GLushort
*) dst
, n
);
695 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
703 * Unpack a row of stencil data from a client buffer according to
704 * the pixel unpacking parameters.
705 * This is (or will be) used by glDrawPixels
707 * Args: ctx - the context
708 * n - number of pixels
709 * dstType - destination data type
710 * dest - destination array
711 * srcType - source pixel type
712 * source - source data pointer
713 * srcPacking - pixel unpacking parameters
714 * transferOps - apply offset/bias/lookup ops?
717 _mesa_unpack_stencil_span( struct gl_context
*ctx
, GLuint n
,
718 GLenum dstType
, GLvoid
*dest
,
719 GLenum srcType
, const GLvoid
*source
,
720 const struct gl_pixelstore_attrib
*srcPacking
,
721 GLbitfield transferOps
)
723 ASSERT(srcType
== GL_BITMAP
||
724 srcType
== GL_UNSIGNED_BYTE
||
725 srcType
== GL_BYTE
||
726 srcType
== GL_UNSIGNED_SHORT
||
727 srcType
== GL_SHORT
||
728 srcType
== GL_UNSIGNED_INT
||
730 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
731 srcType
== GL_HALF_FLOAT_ARB
||
732 srcType
== GL_FLOAT
||
733 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
735 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
736 dstType
== GL_UNSIGNED_SHORT
||
737 dstType
== GL_UNSIGNED_INT
||
738 dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
740 /* only shift and offset apply to stencil */
741 transferOps
&= IMAGE_SHIFT_OFFSET_BIT
;
744 * Try simple cases first
746 if (transferOps
== 0 &&
747 !ctx
->Pixel
.MapStencilFlag
&&
748 srcType
== GL_UNSIGNED_BYTE
&&
749 dstType
== GL_UNSIGNED_BYTE
) {
750 memcpy(dest
, source
, n
* sizeof(GLubyte
));
752 else if (transferOps
== 0 &&
753 !ctx
->Pixel
.MapStencilFlag
&&
754 srcType
== GL_UNSIGNED_INT
&&
755 dstType
== GL_UNSIGNED_INT
&&
756 !srcPacking
->SwapBytes
) {
757 memcpy(dest
, source
, n
* sizeof(GLuint
));
763 GLuint
*indexes
= malloc(n
* sizeof(GLuint
));
766 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil unpacking");
770 extract_uint_indexes(n
, indexes
, GL_STENCIL_INDEX
, srcType
, source
,
773 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
) {
774 /* shift and offset indexes */
775 _mesa_shift_and_offset_ci(ctx
, n
, indexes
);
778 if (ctx
->Pixel
.MapStencilFlag
) {
779 /* Apply stencil lookup table */
780 const GLuint mask
= ctx
->PixelMaps
.StoS
.Size
- 1;
782 for (i
= 0; i
< n
; i
++) {
783 indexes
[i
] = (GLuint
)ctx
->PixelMaps
.StoS
.Map
[ indexes
[i
] & mask
];
787 /* convert to dest type */
789 case GL_UNSIGNED_BYTE
:
791 GLubyte
*dst
= (GLubyte
*) dest
;
793 for (i
= 0; i
< n
; i
++) {
794 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
798 case GL_UNSIGNED_SHORT
:
800 GLuint
*dst
= (GLuint
*) dest
;
802 for (i
= 0; i
< n
; i
++) {
803 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
807 case GL_UNSIGNED_INT
:
808 memcpy(dest
, indexes
, n
* sizeof(GLuint
));
810 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
812 GLuint
*dst
= (GLuint
*) dest
;
814 for (i
= 0; i
< n
; i
++) {
815 dst
[i
*2+1] = indexes
[i
] & 0xff; /* lower 8 bits */
820 _mesa_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
829 _mesa_pack_stencil_span( struct gl_context
*ctx
, GLuint n
,
830 GLenum dstType
, GLvoid
*dest
, const GLubyte
*source
,
831 const struct gl_pixelstore_attrib
*dstPacking
)
833 GLubyte
*stencil
= malloc(n
* sizeof(GLubyte
));
836 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "stencil packing");
840 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
||
841 ctx
->Pixel
.MapStencilFlag
) {
842 /* make a copy of input */
843 memcpy(stencil
, source
, n
* sizeof(GLubyte
));
844 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencil
);
849 case GL_UNSIGNED_BYTE
:
850 memcpy(dest
, source
, n
);
854 GLbyte
*dst
= (GLbyte
*) dest
;
857 dst
[i
] = (GLbyte
) (source
[i
] & 0x7f);
861 case GL_UNSIGNED_SHORT
:
863 GLushort
*dst
= (GLushort
*) dest
;
866 dst
[i
] = (GLushort
) source
[i
];
868 if (dstPacking
->SwapBytes
) {
869 _mesa_swap2( (GLushort
*) dst
, n
);
875 GLshort
*dst
= (GLshort
*) dest
;
878 dst
[i
] = (GLshort
) source
[i
];
880 if (dstPacking
->SwapBytes
) {
881 _mesa_swap2( (GLushort
*) dst
, n
);
885 case GL_UNSIGNED_INT
:
887 GLuint
*dst
= (GLuint
*) dest
;
890 dst
[i
] = (GLuint
) source
[i
];
892 if (dstPacking
->SwapBytes
) {
893 _mesa_swap4( (GLuint
*) dst
, n
);
899 GLint
*dst
= (GLint
*) dest
;
902 dst
[i
] = (GLint
) source
[i
];
904 if (dstPacking
->SwapBytes
) {
905 _mesa_swap4( (GLuint
*) dst
, n
);
911 GLfloat
*dst
= (GLfloat
*) dest
;
914 dst
[i
] = (GLfloat
) source
[i
];
916 if (dstPacking
->SwapBytes
) {
917 _mesa_swap4( (GLuint
*) dst
, n
);
921 case GL_HALF_FLOAT_ARB
:
923 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
926 dst
[i
] = _mesa_float_to_half( (float) source
[i
] );
928 if (dstPacking
->SwapBytes
) {
929 _mesa_swap2( (GLushort
*) dst
, n
);
934 if (dstPacking
->LsbFirst
) {
935 GLubyte
*dst
= (GLubyte
*) dest
;
938 for (i
= 0; i
< n
; i
++) {
941 *dst
|= ((source
[i
] != 0) << shift
);
950 GLubyte
*dst
= (GLubyte
*) dest
;
953 for (i
= 0; i
< n
; i
++) {
956 *dst
|= ((source
[i
] != 0) << shift
);
966 _mesa_problem(ctx
, "bad type in _mesa_pack_index_span");
972 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
975 const GLTYPE *src = (const GLTYPE *)source; \
976 for (i = 0; i < n; i++) { \
977 GLTYPE value = src[i]; \
978 if (srcPacking->SwapBytes) { \
979 if (sizeof(GLTYPE) == 2) { \
981 } else if (sizeof(GLTYPE) == 4) { \
985 depthValues[i] = GLTYPE2FLOAT(value); \
991 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
993 * The glPixelTransfer (scale/bias) params will be applied.
995 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
996 * \param depthMax max value for returned GLushort or GLuint values
997 * (ignored for GLfloat).
1000 _mesa_unpack_depth_span( struct gl_context
*ctx
, GLuint n
,
1001 GLenum dstType
, GLvoid
*dest
, GLuint depthMax
,
1002 GLenum srcType
, const GLvoid
*source
,
1003 const struct gl_pixelstore_attrib
*srcPacking
)
1005 GLfloat
*depthTemp
= NULL
, *depthValues
;
1006 GLboolean needClamp
= GL_FALSE
;
1008 /* Look for special cases first.
1009 * Not only are these faster, they're less prone to numeric conversion
1010 * problems. Otherwise, converting from an int type to a float then
1011 * back to an int type can introduce errors that will show up as
1012 * artifacts in things like depth peeling which uses glCopyTexImage.
1014 if (ctx
->Pixel
.DepthScale
== 1.0 && ctx
->Pixel
.DepthBias
== 0.0) {
1015 if (srcType
== GL_UNSIGNED_INT
&& dstType
== GL_UNSIGNED_SHORT
) {
1016 const GLuint
*src
= (const GLuint
*) source
;
1017 GLushort
*dst
= (GLushort
*) dest
;
1019 for (i
= 0; i
< n
; i
++) {
1020 dst
[i
] = src
[i
] >> 16;
1024 if (srcType
== GL_UNSIGNED_SHORT
1025 && dstType
== GL_UNSIGNED_INT
1026 && depthMax
== 0xffffffff) {
1027 const GLushort
*src
= (const GLushort
*) source
;
1028 GLuint
*dst
= (GLuint
*) dest
;
1030 for (i
= 0; i
< n
; i
++) {
1031 dst
[i
] = src
[i
] | (src
[i
] << 16);
1035 if (srcType
== GL_UNSIGNED_INT_24_8
1036 && dstType
== GL_UNSIGNED_INT
1037 && depthMax
== 0xffffff) {
1038 const GLuint
*src
= (const GLuint
*) source
;
1039 GLuint
*dst
= (GLuint
*) dest
;
1041 for (i
= 0; i
< n
; i
++) {
1042 dst
[i
] = src
[i
] >> 8;
1046 /* XXX may want to add additional cases here someday */
1049 /* general case path follows */
1051 if (dstType
== GL_FLOAT
) {
1052 depthValues
= (GLfloat
*) dest
;
1055 depthTemp
= malloc(n
* sizeof(GLfloat
));
1057 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1061 depthValues
= depthTemp
;
1064 /* Convert incoming values to GLfloat. Some conversions will require
1069 DEPTH_VALUES(GLbyte
, BYTE_TO_FLOATZ
);
1070 needClamp
= GL_TRUE
;
1072 case GL_UNSIGNED_BYTE
:
1073 DEPTH_VALUES(GLubyte
, UBYTE_TO_FLOAT
);
1076 DEPTH_VALUES(GLshort
, SHORT_TO_FLOATZ
);
1077 needClamp
= GL_TRUE
;
1079 case GL_UNSIGNED_SHORT
:
1080 DEPTH_VALUES(GLushort
, USHORT_TO_FLOAT
);
1083 DEPTH_VALUES(GLint
, INT_TO_FLOAT
);
1084 needClamp
= GL_TRUE
;
1086 case GL_UNSIGNED_INT
:
1087 DEPTH_VALUES(GLuint
, UINT_TO_FLOAT
);
1089 case GL_UNSIGNED_INT_24_8_EXT
: /* GL_EXT_packed_depth_stencil */
1090 if (dstType
== GL_UNSIGNED_INT_24_8_EXT
&&
1091 depthMax
== 0xffffff &&
1092 ctx
->Pixel
.DepthScale
== 1.0 &&
1093 ctx
->Pixel
.DepthBias
== 0.0) {
1094 const GLuint
*src
= (const GLuint
*) source
;
1095 GLuint
*zValues
= (GLuint
*) dest
;
1097 for (i
= 0; i
< n
; i
++) {
1098 GLuint value
= src
[i
];
1099 if (srcPacking
->SwapBytes
) {
1102 zValues
[i
] = value
& 0xffffff00;
1108 const GLuint
*src
= (const GLuint
*) source
;
1109 const GLfloat scale
= 1.0f
/ 0xffffff;
1111 for (i
= 0; i
< n
; i
++) {
1112 GLuint value
= src
[i
];
1113 if (srcPacking
->SwapBytes
) {
1116 depthValues
[i
] = (value
>> 8) * scale
;
1120 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1123 const GLfloat
*src
= (const GLfloat
*)source
;
1124 for (i
= 0; i
< n
; i
++) {
1125 GLfloat value
= src
[i
* 2];
1126 if (srcPacking
->SwapBytes
) {
1129 depthValues
[i
] = value
;
1131 needClamp
= GL_TRUE
;
1135 DEPTH_VALUES(GLfloat
, 1*);
1136 needClamp
= GL_TRUE
;
1138 case GL_HALF_FLOAT_ARB
:
1141 const GLhalfARB
*src
= (const GLhalfARB
*) source
;
1142 for (i
= 0; i
< n
; i
++) {
1143 GLhalfARB value
= src
[i
];
1144 if (srcPacking
->SwapBytes
) {
1147 depthValues
[i
] = _mesa_half_to_float(value
);
1149 needClamp
= GL_TRUE
;
1153 _mesa_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
1158 /* apply depth scale and bias */
1160 const GLfloat scale
= ctx
->Pixel
.DepthScale
;
1161 const GLfloat bias
= ctx
->Pixel
.DepthBias
;
1162 if (scale
!= 1.0 || bias
!= 0.0) {
1164 for (i
= 0; i
< n
; i
++) {
1165 depthValues
[i
] = depthValues
[i
] * scale
+ bias
;
1167 needClamp
= GL_TRUE
;
1171 /* clamp to [0, 1] */
1174 for (i
= 0; i
< n
; i
++) {
1175 depthValues
[i
] = (GLfloat
)CLAMP(depthValues
[i
], 0.0, 1.0);
1180 * Convert values to dstType
1182 if (dstType
== GL_UNSIGNED_INT
) {
1183 GLuint
*zValues
= (GLuint
*) dest
;
1185 if (depthMax
<= 0xffffff) {
1186 /* no overflow worries */
1187 for (i
= 0; i
< n
; i
++) {
1188 zValues
[i
] = (GLuint
) (depthValues
[i
] * (GLfloat
) depthMax
);
1192 /* need to use double precision to prevent overflow problems */
1193 for (i
= 0; i
< n
; i
++) {
1194 GLdouble z
= depthValues
[i
] * (GLdouble
) depthMax
;
1195 if (z
>= (GLdouble
) 0xffffffff)
1196 zValues
[i
] = 0xffffffff;
1198 zValues
[i
] = (GLuint
) z
;
1202 else if (dstType
== GL_UNSIGNED_SHORT
) {
1203 GLushort
*zValues
= (GLushort
*) dest
;
1205 ASSERT(depthMax
<= 0xffff);
1206 for (i
= 0; i
< n
; i
++) {
1207 zValues
[i
] = (GLushort
) (depthValues
[i
] * (GLfloat
) depthMax
);
1210 else if (dstType
== GL_FLOAT
) {
1211 /* Nothing to do. depthValues is pointing to dest. */
1213 else if (dstType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
) {
1214 GLfloat
*zValues
= (GLfloat
*) dest
;
1216 for (i
= 0; i
< n
; i
++) {
1217 zValues
[i
*2] = depthValues
[i
];
1229 * Pack an array of depth values. The values are floats in [0,1].
1232 _mesa_pack_depth_span( struct gl_context
*ctx
, GLuint n
, GLvoid
*dest
,
1233 GLenum dstType
, const GLfloat
*depthSpan
,
1234 const struct gl_pixelstore_attrib
*dstPacking
)
1236 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1238 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1242 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
1243 memcpy(depthCopy
, depthSpan
, n
* sizeof(GLfloat
));
1244 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1245 depthSpan
= depthCopy
;
1249 case GL_UNSIGNED_BYTE
:
1251 GLubyte
*dst
= (GLubyte
*) dest
;
1253 for (i
= 0; i
< n
; i
++) {
1254 dst
[i
] = FLOAT_TO_UBYTE( depthSpan
[i
] );
1260 GLbyte
*dst
= (GLbyte
*) dest
;
1262 for (i
= 0; i
< n
; i
++) {
1263 dst
[i
] = FLOAT_TO_BYTE( depthSpan
[i
] );
1267 case GL_UNSIGNED_SHORT
:
1269 GLushort
*dst
= (GLushort
*) dest
;
1271 for (i
= 0; i
< n
; i
++) {
1272 CLAMPED_FLOAT_TO_USHORT(dst
[i
], depthSpan
[i
]);
1274 if (dstPacking
->SwapBytes
) {
1275 _mesa_swap2( (GLushort
*) dst
, n
);
1281 GLshort
*dst
= (GLshort
*) dest
;
1283 for (i
= 0; i
< n
; i
++) {
1284 dst
[i
] = FLOAT_TO_SHORT( depthSpan
[i
] );
1286 if (dstPacking
->SwapBytes
) {
1287 _mesa_swap2( (GLushort
*) dst
, n
);
1291 case GL_UNSIGNED_INT
:
1293 GLuint
*dst
= (GLuint
*) dest
;
1295 for (i
= 0; i
< n
; i
++) {
1296 dst
[i
] = FLOAT_TO_UINT( depthSpan
[i
] );
1298 if (dstPacking
->SwapBytes
) {
1299 _mesa_swap4( (GLuint
*) dst
, n
);
1305 GLint
*dst
= (GLint
*) dest
;
1307 for (i
= 0; i
< n
; i
++) {
1308 dst
[i
] = FLOAT_TO_INT( depthSpan
[i
] );
1310 if (dstPacking
->SwapBytes
) {
1311 _mesa_swap4( (GLuint
*) dst
, n
);
1317 GLfloat
*dst
= (GLfloat
*) dest
;
1319 for (i
= 0; i
< n
; i
++) {
1320 dst
[i
] = depthSpan
[i
];
1322 if (dstPacking
->SwapBytes
) {
1323 _mesa_swap4( (GLuint
*) dst
, n
);
1327 case GL_HALF_FLOAT_ARB
:
1329 GLhalfARB
*dst
= (GLhalfARB
*) dest
;
1331 for (i
= 0; i
< n
; i
++) {
1332 dst
[i
] = _mesa_float_to_half(depthSpan
[i
]);
1334 if (dstPacking
->SwapBytes
) {
1335 _mesa_swap2( (GLushort
*) dst
, n
);
1340 _mesa_problem(ctx
, "bad type in _mesa_pack_depth_span");
1349 * Pack depth and stencil values as GL_DEPTH_STENCIL (GL_UNSIGNED_INT_24_8 etc)
1352 _mesa_pack_depth_stencil_span(struct gl_context
*ctx
,GLuint n
,
1353 GLenum dstType
, GLuint
*dest
,
1354 const GLfloat
*depthVals
,
1355 const GLubyte
*stencilVals
,
1356 const struct gl_pixelstore_attrib
*dstPacking
)
1358 GLfloat
*depthCopy
= malloc(n
* sizeof(GLfloat
));
1359 GLubyte
*stencilCopy
= malloc(n
* sizeof(GLubyte
));
1362 if (!depthCopy
|| !stencilCopy
) {
1363 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel packing");
1369 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
1370 memcpy(depthCopy
, depthVals
, n
* sizeof(GLfloat
));
1371 _mesa_scale_and_bias_depth(ctx
, n
, depthCopy
);
1372 depthVals
= depthCopy
;
1375 if (ctx
->Pixel
.IndexShift
||
1376 ctx
->Pixel
.IndexOffset
||
1377 ctx
->Pixel
.MapStencilFlag
) {
1378 memcpy(stencilCopy
, stencilVals
, n
* sizeof(GLubyte
));
1379 _mesa_apply_stencil_transfer_ops(ctx
, n
, stencilCopy
);
1380 stencilVals
= stencilCopy
;
1384 case GL_UNSIGNED_INT_24_8
:
1385 for (i
= 0; i
< n
; i
++) {
1386 GLuint z
= (GLuint
) (depthVals
[i
] * 0xffffff);
1387 dest
[i
] = (z
<< 8) | (stencilVals
[i
] & 0xff);
1390 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV
:
1391 for (i
= 0; i
< n
; i
++) {
1392 ((GLfloat
*)dest
)[i
*2] = depthVals
[i
];
1393 dest
[i
*2+1] = stencilVals
[i
] & 0xff;
1398 if (dstPacking
->SwapBytes
) {
1399 _mesa_swap4(dest
, n
);
1409 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
1410 * Return all image data in a contiguous block. This is used when we
1411 * compile glDrawPixels, glTexImage, etc into a display list. We
1412 * need a copy of the data in a standard format.
1415 _mesa_unpack_image( GLuint dimensions
,
1416 GLsizei width
, GLsizei height
, GLsizei depth
,
1417 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1418 const struct gl_pixelstore_attrib
*unpack
)
1420 GLint bytesPerRow
, compsPerRow
;
1421 GLboolean flipBytes
, swap2
, swap4
;
1424 return NULL
; /* not necessarily an error */
1426 if (width
<= 0 || height
<= 0 || depth
<= 0)
1427 return NULL
; /* generate error later */
1429 if (type
== GL_BITMAP
) {
1430 bytesPerRow
= (width
+ 7) >> 3;
1431 flipBytes
= unpack
->LsbFirst
;
1432 swap2
= swap4
= GL_FALSE
;
1436 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
1437 GLint components
= _mesa_components_in_format(format
);
1440 if (_mesa_type_is_packed(type
))
1443 if (bytesPerPixel
<= 0 || components
<= 0)
1444 return NULL
; /* bad format or type. generate error later */
1445 bytesPerRow
= bytesPerPixel
* width
;
1446 bytesPerComp
= bytesPerPixel
/ components
;
1447 flipBytes
= GL_FALSE
;
1448 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
1449 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
1450 compsPerRow
= components
* width
;
1451 assert(compsPerRow
>= width
);
1456 = malloc(bytesPerRow
* height
* depth
);
1460 return NULL
; /* generate GL_OUT_OF_MEMORY later */
1463 for (img
= 0; img
< depth
; img
++) {
1464 for (row
= 0; row
< height
; row
++) {
1465 const GLvoid
*src
= _mesa_image_address(dimensions
, unpack
, pixels
,
1466 width
, height
, format
, type
, img
, row
, 0);
1468 if ((type
== GL_BITMAP
) && (unpack
->SkipPixels
& 0x7)) {
1470 flipBytes
= GL_FALSE
;
1471 if (unpack
->LsbFirst
) {
1472 GLubyte srcMask
= 1 << (unpack
->SkipPixels
& 0x7);
1473 GLubyte dstMask
= 128;
1474 const GLubyte
*s
= src
;
1477 for (i
= 0; i
< width
; i
++) {
1481 if (srcMask
== 128) {
1486 srcMask
= srcMask
<< 1;
1494 dstMask
= dstMask
>> 1;
1499 GLubyte srcMask
= 128 >> (unpack
->SkipPixels
& 0x7);
1500 GLubyte dstMask
= 128;
1501 const GLubyte
*s
= src
;
1504 for (i
= 0; i
< width
; i
++) {
1513 srcMask
= srcMask
>> 1;
1521 dstMask
= dstMask
>> 1;
1527 memcpy(dst
, src
, bytesPerRow
);
1530 /* byte flipping/swapping */
1532 flip_bytes((GLubyte
*) dst
, bytesPerRow
);
1535 _mesa_swap2((GLushort
*) dst
, compsPerRow
);
1538 _mesa_swap4((GLuint
*) dst
, compsPerRow
);
1550 * If we unpack colors from a luminance surface, we'll get pixel colors
1551 * such as (l, l, l, a).
1552 * When we call _mesa_pack_rgba_span_float(format=GL_LUMINANCE), that
1553 * function will compute L=R+G+B before packing. The net effect is we'll
1554 * accidentally store luminance values = 3*l.
1555 * This function compensates for that by converting (aka rebasing) (l,l,l,a)
1557 * It's a similar story for other formats such as LUMINANCE_ALPHA, ALPHA
1560 * Finally, we also need to do this when the actual surface format does
1561 * not match the logical surface format. For example, suppose the user
1562 * requests a GL_LUMINANCE texture but the driver stores it as RGBA.
1563 * Again, we'll get pixel values like (l,l,l,a).
1566 _mesa_rebase_rgba_float(GLuint n
, GLfloat rgba
[][4], GLenum baseFormat
)
1570 switch (baseFormat
) {
1572 for (i
= 0; i
< n
; i
++) {
1573 rgba
[i
][RCOMP
] = 0.0F
;
1574 rgba
[i
][GCOMP
] = 0.0F
;
1575 rgba
[i
][BCOMP
] = 0.0F
;
1581 for (i
= 0; i
< n
; i
++) {
1582 rgba
[i
][GCOMP
] = 0.0F
;
1583 rgba
[i
][BCOMP
] = 0.0F
;
1584 rgba
[i
][ACOMP
] = 1.0F
;
1587 case GL_LUMINANCE_ALPHA
:
1588 for (i
= 0; i
< n
; i
++) {
1589 rgba
[i
][GCOMP
] = 0.0F
;
1590 rgba
[i
][BCOMP
] = 0.0F
;
1594 for (i
= 0; i
< n
; i
++) {
1595 rgba
[i
][ACOMP
] = 1.0F
;
1599 for (i
= 0; i
< n
; i
++) {
1600 rgba
[i
][BCOMP
] = 0.0F
;
1601 rgba
[i
][ACOMP
] = 1.0F
;
1605 for (i
= 0; i
< n
; i
++) {
1606 rgba
[i
][GCOMP
] = 0.0F
;
1607 rgba
[i
][BCOMP
] = 0.0F
;
1608 rgba
[i
][ACOMP
] = 1.0F
;
1620 * As above, but GLuint components.
1623 _mesa_rebase_rgba_uint(GLuint n
, GLuint rgba
[][4], GLenum baseFormat
)
1627 switch (baseFormat
) {
1629 for (i
= 0; i
< n
; i
++) {
1638 for (i
= 0; i
< n
; i
++) {
1644 case GL_LUMINANCE_ALPHA
:
1645 for (i
= 0; i
< n
; i
++) {
1651 for (i
= 0; i
< n
; i
++) {
1656 for (i
= 0; i
< n
; i
++) {
1662 for (i
= 0; i
< n
; i
++) {
1674 _mesa_pack_luminance_from_rgba_float(GLuint n
, GLfloat rgba
[][4],
1675 GLvoid
*dstAddr
, GLenum dst_format
,
1676 GLbitfield transferOps
)
1679 GLfloat
*dst
= (GLfloat
*) dstAddr
;
1681 switch (dst_format
) {
1683 if (transferOps
& IMAGE_CLAMP_BIT
) {
1684 for (i
= 0; i
< n
; i
++) {
1685 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1686 dst
[i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1689 for (i
= 0; i
< n
; i
++) {
1690 dst
[i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1694 case GL_LUMINANCE_ALPHA
:
1695 if (transferOps
& IMAGE_CLAMP_BIT
) {
1696 for (i
= 0; i
< n
; i
++) {
1697 GLfloat sum
= rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1698 dst
[2*i
] = CLAMP(sum
, 0.0F
, 1.0F
);
1699 dst
[2*i
+1] = rgba
[i
][ACOMP
];
1702 for (i
= 0; i
< n
; i
++) {
1703 dst
[2*i
] = rgba
[i
][RCOMP
] + rgba
[i
][GCOMP
] + rgba
[i
][BCOMP
];
1704 dst
[2*i
+1] = rgba
[i
][ACOMP
];
1709 assert(!"Unsupported format");
1714 clamp_sint64_to_sint32(int64_t src
)
1716 return CLAMP(src
, INT32_MIN
, INT32_MAX
);
1720 clamp_sint64_to_uint32(int64_t src
)
1722 return CLAMP(src
, 0, UINT32_MAX
);
1726 clamp_uint64_to_uint32(uint64_t src
)
1728 return MIN2(src
, UINT32_MAX
);
1732 clamp_uint64_to_sint32(uint64_t src
)
1734 return MIN2(src
, INT32_MAX
);
1738 convert_integer_luminance64(int64_t src64
, int bits
,
1739 bool dst_is_signed
, bool src_is_signed
)
1743 /* Clamp Luminance value from 64-bit to 32-bit. Consider if we need
1744 * any signed<->unsigned conversion too.
1746 if (src_is_signed
&& dst_is_signed
)
1747 src32
= clamp_sint64_to_sint32(src64
);
1748 else if (src_is_signed
&& !dst_is_signed
)
1749 src32
= clamp_sint64_to_uint32(src64
);
1750 else if (!src_is_signed
&& dst_is_signed
)
1751 src32
= clamp_uint64_to_sint32(src64
);
1753 src32
= clamp_uint64_to_uint32(src64
);
1755 /* If the dst type is < 32-bit, we need an extra clamp */
1760 return _mesa_signed_to_signed(src32
, bits
);
1762 return _mesa_unsigned_to_unsigned(src32
, bits
);
1767 convert_integer(int32_t src
, int bits
, bool dst_is_signed
, bool src_is_signed
)
1769 if (src_is_signed
&& dst_is_signed
)
1770 return _mesa_signed_to_signed(src
, bits
);
1771 else if (src_is_signed
&& !dst_is_signed
)
1772 return _mesa_signed_to_unsigned(src
, bits
);
1773 else if (!src_is_signed
&& dst_is_signed
)
1774 return _mesa_unsigned_to_signed(src
, bits
);
1776 return _mesa_unsigned_to_unsigned(src
, bits
);
1780 _mesa_pack_luminance_from_rgba_integer(GLuint n
,
1781 GLuint rgba
[][4], bool rgba_is_signed
,
1786 assert(dst_format
== GL_LUMINANCE_INTEGER_EXT
||
1787 dst_format
== GL_LUMINANCE_ALPHA_INTEGER_EXT
);
1791 int32_t lum32
, alpha
;
1795 /* We first compute luminance values as a 64-bit addition of the
1796 * 32-bit R,G,B components, then we clamp the result to the dst type size.
1798 * Notice that this operation involves casting the 32-bit R,G,B components
1799 * to 64-bit before the addition. Since rgba is defined as a GLuint array
1800 * we need to be careful when rgba packs signed data and make sure
1801 * that we cast to a 32-bit signed integer values before casting them to
1802 * 64-bit signed integers.
1804 dst_is_signed
= (dst_type
== GL_BYTE
|| dst_type
== GL_SHORT
||
1805 dst_type
== GL_INT
);
1807 dst_bits
= _mesa_sizeof_type(dst_type
) * 8;
1808 assert(dst_bits
> 0);
1810 switch (dst_format
) {
1811 case GL_LUMINANCE_INTEGER_EXT
:
1812 for (i
= 0; i
< n
; i
++) {
1813 if (!rgba_is_signed
) {
1814 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
1815 (uint64_t) rgba
[i
][GCOMP
] +
1816 (uint64_t) rgba
[i
][BCOMP
];
1818 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
1819 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
1820 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
1822 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
1823 dst_is_signed
, rgba_is_signed
);
1826 case GL_UNSIGNED_BYTE
: {
1827 GLbyte
*dst
= (GLbyte
*) dstAddr
;
1832 case GL_UNSIGNED_SHORT
: {
1833 GLshort
*dst
= (GLshort
*) dstAddr
;
1838 case GL_UNSIGNED_INT
: {
1839 GLint
*dst
= (GLint
*) dstAddr
;
1846 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
1847 for (i
= 0; i
< n
; i
++) {
1848 if (!rgba_is_signed
) {
1849 lum64
= (uint64_t) rgba
[i
][RCOMP
] +
1850 (uint64_t) rgba
[i
][GCOMP
] +
1851 (uint64_t) rgba
[i
][BCOMP
];
1853 lum64
= (int64_t) ((int32_t) rgba
[i
][RCOMP
]) +
1854 (int64_t) ((int32_t) rgba
[i
][GCOMP
]) +
1855 (int64_t) ((int32_t) rgba
[i
][BCOMP
]);
1857 lum32
= convert_integer_luminance64(lum64
, dst_bits
,
1858 dst_is_signed
, rgba_is_signed
);
1859 alpha
= convert_integer(rgba
[i
][ACOMP
], dst_bits
,
1860 dst_is_signed
, rgba_is_signed
);
1863 case GL_UNSIGNED_BYTE
: {
1864 GLbyte
*dst
= (GLbyte
*) dstAddr
;
1869 case GL_UNSIGNED_SHORT
: {
1870 GLshort
*dst
= (GLshort
*) dstAddr
;
1876 case GL_UNSIGNED_INT
: {
1877 GLint
*dst
= (GLint
*) dstAddr
;
1889 _mesa_unpack_color_index_to_rgba_float(struct gl_context
*ctx
, GLuint dims
,
1890 const void *src
, GLenum srcFormat
, GLenum srcType
,
1891 int srcWidth
, int srcHeight
, int srcDepth
,
1892 const struct gl_pixelstore_attrib
*srcPacking
,
1893 GLbitfield transferOps
)
1897 GLfloat
*rgba
, *dstPtr
;
1899 count
= srcWidth
* srcHeight
;
1900 indexes
= malloc(count
* sizeof(GLuint
));
1902 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1906 rgba
= malloc(4 * count
* srcDepth
* sizeof(GLfloat
));
1909 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "pixel unpacking");
1913 /* Convert indexes to RGBA float */
1915 for (img
= 0; img
< srcDepth
; img
++) {
1916 const GLubyte
*srcPtr
=
1917 (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, src
,
1918 srcWidth
, srcHeight
,
1922 extract_uint_indexes(count
, indexes
, srcFormat
, srcType
, srcPtr
, srcPacking
);
1924 if (transferOps
& IMAGE_SHIFT_OFFSET_BIT
)
1925 _mesa_shift_and_offset_ci(ctx
, count
, indexes
);
1927 _mesa_map_ci_to_rgba(ctx
, count
, indexes
, (float (*)[4])dstPtr
);
1929 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
1930 * with color indexes.
1932 transferOps
&= ~(IMAGE_SCALE_BIAS_BIT
| IMAGE_MAP_COLOR_BIT
);
1933 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, count
, (float (*)[4])dstPtr
);
1935 dstPtr
+= srcHeight
* srcWidth
* 4;
1944 _mesa_unpack_color_index_to_rgba_ubyte(struct gl_context
*ctx
, GLuint dims
,
1945 const void *src
, GLenum srcFormat
, GLenum srcType
,
1946 int srcWidth
, int srcHeight
, int srcDepth
,
1947 const struct gl_pixelstore_attrib
*srcPacking
,
1948 GLbitfield transferOps
)
1954 transferOps
|= IMAGE_CLAMP_BIT
;
1955 rgba
= _mesa_unpack_color_index_to_rgba_float(ctx
, dims
,
1956 src
, srcFormat
, srcType
,
1957 srcWidth
, srcHeight
, srcDepth
,
1958 srcPacking
, transferOps
);
1960 count
= srcWidth
* srcHeight
* srcDepth
;
1961 dst
= malloc(count
* 4 * sizeof(GLubyte
));
1962 for (i
= 0; i
< count
; i
++) {
1963 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 0], rgba
[i
* 4 + 0]);
1964 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 1], rgba
[i
* 4 + 1]);
1965 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 2], rgba
[i
* 4 + 2]);
1966 CLAMPED_FLOAT_TO_UBYTE(dst
[i
* 4 + 3], rgba
[i
* 4 + 3]);