1 /* $Id: image.c,v 1.20 2000/03/19 01:10:12 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 * These are the image packing parameters for Mesa's internal images.
45 * That is, _mesa_unpack_image() returns image data in this format.
46 * When we execute image commands (glDrawPixels, glTexImage, etc)
47 * from within display lists we have to be sure to set the current
48 * unpacking params to these values!
50 struct gl_pixelstore_attrib _mesa_native_packing
= {
57 GL_FALSE
, /* SwapBytes */
58 GL_FALSE
/* LsbFirst */
64 * Flip the 8 bits in each byte of the given array.
66 void gl_flip_bytes( GLubyte
*p
, GLuint n
)
68 register GLuint i
, a
, b
;
72 a
= ((b
& 0x01) << 7) |
86 * Flip the order of the 2 bytes in each word in the given array.
88 void gl_swap2( GLushort
*p
, GLuint n
)
93 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
100 * Flip the order of the 4 bytes in each word in the given array.
102 void gl_swap4( GLuint
*p
, GLuint n
)
104 register GLuint i
, a
, b
;
109 | ((b
>> 8) & 0xff00)
110 | ((b
<< 8) & 0xff0000)
111 | ((b
<< 24) & 0xff000000);
120 * Return the size, in bytes, of the given GL datatype.
121 * Return 0 if GL_BITMAP.
122 * Return -1 if invalid type enum.
124 GLint
gl_sizeof_type( GLenum type
)
129 case GL_UNSIGNED_BYTE
:
130 return sizeof(GLubyte
);
132 return sizeof(GLbyte
);
133 case GL_UNSIGNED_SHORT
:
134 return sizeof(GLushort
);
136 return sizeof(GLshort
);
137 case GL_UNSIGNED_INT
:
138 return sizeof(GLuint
);
140 return sizeof(GLint
);
142 return sizeof(GLfloat
);
150 * Same as gl_sizeof_packed_type() but we also accept the
151 * packed pixel format datatypes.
153 GLint
gl_sizeof_packed_type( GLenum type
)
158 case GL_UNSIGNED_BYTE
:
159 return sizeof(GLubyte
);
161 return sizeof(GLbyte
);
162 case GL_UNSIGNED_SHORT
:
163 return sizeof(GLushort
);
165 return sizeof(GLshort
);
166 case GL_UNSIGNED_INT
:
167 return sizeof(GLuint
);
169 return sizeof(GLint
);
171 return sizeof(GLfloat
);
172 case GL_UNSIGNED_BYTE_3_3_2
:
173 return sizeof(GLubyte
);
174 case GL_UNSIGNED_BYTE_2_3_3_REV
:
175 return sizeof(GLubyte
);
176 case GL_UNSIGNED_SHORT_5_6_5
:
177 return sizeof(GLshort
);
178 case GL_UNSIGNED_SHORT_5_6_5_REV
:
179 return sizeof(GLshort
);
180 case GL_UNSIGNED_SHORT_4_4_4_4
:
181 return sizeof(GLshort
);
182 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
183 return sizeof(GLshort
);
184 case GL_UNSIGNED_SHORT_5_5_5_1
:
185 return sizeof(GLshort
);
186 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
187 return sizeof(GLshort
);
188 case GL_UNSIGNED_INT_8_8_8_8
:
189 return sizeof(GLuint
);
190 case GL_UNSIGNED_INT_8_8_8_8_REV
:
191 return sizeof(GLuint
);
192 case GL_UNSIGNED_INT_10_10_10_2
:
193 return sizeof(GLuint
);
194 case GL_UNSIGNED_INT_2_10_10_10_REV
:
195 return sizeof(GLuint
);
204 * Return the number of components in a GL enum pixel type.
205 * Return -1 if bad format.
207 GLint
gl_components_in_format( GLenum format
)
211 case GL_COLOR_INDEX1_EXT
:
212 case GL_COLOR_INDEX2_EXT
:
213 case GL_COLOR_INDEX4_EXT
:
214 case GL_COLOR_INDEX8_EXT
:
215 case GL_COLOR_INDEX12_EXT
:
216 case GL_COLOR_INDEX16_EXT
:
217 case GL_STENCIL_INDEX
:
218 case GL_DEPTH_COMPONENT
:
225 case GL_LUMINANCE_ALPHA
:
244 * Return bytes per pixel for given format and type
245 * Return -1 if bad format or type.
247 GLint
gl_bytes_per_pixel( GLenum format
, GLenum type
)
249 GLint comps
= gl_components_in_format( format
);
255 return 0; /* special case */
257 case GL_UNSIGNED_BYTE
:
258 return comps
* sizeof(GLubyte
);
260 case GL_UNSIGNED_SHORT
:
261 return comps
* sizeof(GLshort
);
263 case GL_UNSIGNED_INT
:
264 return comps
* sizeof(GLint
);
266 return comps
* sizeof(GLfloat
);
267 case GL_UNSIGNED_BYTE_3_3_2
:
268 case GL_UNSIGNED_BYTE_2_3_3_REV
:
269 if (format
== GL_RGB
|| format
== GL_BGR
)
270 return sizeof(GLubyte
);
272 return -1; /* error */
273 case GL_UNSIGNED_SHORT_5_6_5
:
274 case GL_UNSIGNED_SHORT_5_6_5_REV
:
275 if (format
== GL_RGB
|| format
== GL_BGR
)
276 return sizeof(GLshort
);
278 return -1; /* error */
279 case GL_UNSIGNED_SHORT_4_4_4_4
:
280 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
281 case GL_UNSIGNED_SHORT_5_5_5_1
:
282 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
283 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
)
284 return sizeof(GLushort
);
287 case GL_UNSIGNED_INT_8_8_8_8
:
288 case GL_UNSIGNED_INT_8_8_8_8_REV
:
289 case GL_UNSIGNED_INT_10_10_10_2
:
290 case GL_UNSIGNED_INT_2_10_10_10_REV
:
291 if (format
== GL_RGBA
|| format
== GL_BGRA
|| format
== GL_ABGR_EXT
)
292 return sizeof(GLuint
);
302 * Test if the given pixel format and type are legal.
303 * Return GL_TRUE for legal, GL_FALSE for illegal.
305 GLboolean
gl_is_legal_format_and_type( GLenum format
, GLenum type
)
309 case GL_STENCIL_INDEX
:
313 case GL_UNSIGNED_BYTE
:
315 case GL_UNSIGNED_SHORT
:
317 case GL_UNSIGNED_INT
:
328 case GL_LUMINANCE_ALPHA
:
329 case GL_DEPTH_COMPONENT
:
333 case GL_UNSIGNED_BYTE
:
335 case GL_UNSIGNED_SHORT
:
337 case GL_UNSIGNED_INT
:
346 case GL_UNSIGNED_BYTE
:
348 case GL_UNSIGNED_SHORT
:
350 case GL_UNSIGNED_INT
:
352 case GL_UNSIGNED_BYTE_3_3_2
:
353 case GL_UNSIGNED_BYTE_2_3_3_REV
:
354 case GL_UNSIGNED_SHORT_5_6_5
:
355 case GL_UNSIGNED_SHORT_5_6_5_REV
:
365 case GL_UNSIGNED_BYTE
:
367 case GL_UNSIGNED_SHORT
:
369 case GL_UNSIGNED_INT
:
371 case GL_UNSIGNED_SHORT_4_4_4_4
:
372 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
373 case GL_UNSIGNED_SHORT_5_5_5_1
:
374 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
375 case GL_UNSIGNED_INT_8_8_8_8
:
376 case GL_UNSIGNED_INT_8_8_8_8_REV
:
377 case GL_UNSIGNED_INT_10_10_10_2
:
378 case GL_UNSIGNED_INT_2_10_10_10_REV
:
392 * Return the address of a pixel in an image (actually a volume).
393 * Pixel unpacking/packing parameters are observed according to 'packing'.
394 * Input: image - start of image data
395 * width, height - size of image
396 * format - image format
397 * type - pixel component type
398 * packing - the pixelstore attributes
399 * img - which image in the volume (0 for 1D or 2D images)
400 * row, column - location of pixel in the image
401 * Return: address of pixel at (image,row,column) in image or NULL if error.
403 GLvoid
*gl_pixel_addr_in_image( const struct gl_pixelstore_attrib
*packing
,
404 const GLvoid
*image
, GLsizei width
,
405 GLsizei height
, GLenum format
, GLenum type
,
406 GLint img
, GLint row
, GLint column
)
408 GLint alignment
; /* 1, 2 or 4 */
409 GLint pixels_per_row
;
410 GLint rows_per_image
;
413 GLint skipimages
; /* for 3-D volume images */
416 alignment
= packing
->Alignment
;
417 if (packing
->RowLength
> 0) {
418 pixels_per_row
= packing
->RowLength
;
421 pixels_per_row
= width
;
423 if (packing
->ImageHeight
> 0) {
424 rows_per_image
= packing
->ImageHeight
;
427 rows_per_image
= height
;
429 skiprows
= packing
->SkipRows
;
430 skippixels
= packing
->SkipPixels
;
431 skipimages
= packing
->SkipImages
;
433 if (type
==GL_BITMAP
) {
435 GLint comp_per_pixel
; /* components per pixel */
436 GLint bytes_per_comp
; /* bytes per component */
438 GLint bytes_per_image
;
440 /* Compute bytes per component */
441 bytes_per_comp
= gl_sizeof_packed_type( type
);
442 if (bytes_per_comp
<0) {
446 /* Compute number of components per pixel */
447 comp_per_pixel
= gl_components_in_format( format
);
448 if (comp_per_pixel
<0 && type
!= GL_BITMAP
) {
452 bytes_per_row
= alignment
453 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
455 bytes_per_image
= bytes_per_row
* rows_per_image
;
457 pixel_addr
= (GLubyte
*) image
458 + (skipimages
+ img
) * bytes_per_image
459 + (skiprows
+ row
) * bytes_per_row
460 + (skippixels
+ column
) / 8;
463 /* Non-BITMAP data */
464 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
466 bytes_per_pixel
= gl_bytes_per_pixel( format
, type
);
468 /* The pixel type and format should have been error checked earlier */
469 assert(bytes_per_pixel
> 0);
471 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
472 remainder
= bytes_per_row
% alignment
;
474 bytes_per_row
+= (alignment
- remainder
);
476 ASSERT(bytes_per_row
% alignment
== 0);
478 bytes_per_image
= bytes_per_row
* rows_per_image
;
480 /* compute final pixel address */
481 pixel_addr
= (GLubyte
*) image
482 + (skipimages
+ img
) * bytes_per_image
483 + (skiprows
+ row
) * bytes_per_row
484 + (skippixels
+ column
) * bytes_per_pixel
;
487 return (GLvoid
*) pixel_addr
;
493 * Unpack a 32x32 pixel polygon stipple from user memory using the
494 * current pixel unpack settings.
497 _mesa_unpack_polygon_stipple( const GLubyte
*pattern
, GLuint dest
[32],
498 const struct gl_pixelstore_attrib
*unpacking
)
500 GLubyte
*ptrn
= (GLubyte
*) _mesa_unpack_bitmap( 32, 32, pattern
, unpacking
);
502 /* Convert pattern from GLubytes to GLuints and handle big/little
507 for (i
= 0; i
< 32; i
++) {
508 dest
[i
] = (p
[0] << 24)
521 * Pack polygon stipple into user memory given current pixel packing
525 _mesa_pack_polygon_stipple( const GLuint pattern
[32], GLubyte
*dest
,
526 const struct gl_pixelstore_attrib
*packing
)
528 /* Convert pattern from GLuints to GLubytes to handle big/little
529 * endian differences.
533 for (i
= 0; i
< 32; i
++) {
534 ptrn
[i
* 4 + 0] = (GLubyte
) ((pattern
[i
] >> 24) & 0xff);
535 ptrn
[i
* 4 + 1] = (GLubyte
) ((pattern
[i
] >> 16) & 0xff);
536 ptrn
[i
* 4 + 2] = (GLubyte
) ((pattern
[i
] >> 8 ) & 0xff);
537 ptrn
[i
* 4 + 3] = (GLubyte
) ((pattern
[i
] ) & 0xff);
540 _mesa_pack_bitmap(32, 32, ptrn
, dest
, packing
);
546 * Pack the given RGBA span into client memory at 'dest' address
547 * in the given pixel format and type.
548 * Optionally apply the enabled pixel transfer ops.
549 * Pack into memory using the given packing params struct.
550 * This is used by glReadPixels and glGetTexImage?D()
551 * Input: ctx - the context
552 * n - number of pixels in the span
554 * format - dest packing format
555 * type - dest packing datatype
556 * destination - destination packing address
557 * packing - pixel packing parameters
558 * applyTransferOps - apply scale/bias/lookup-table ops?
560 void gl_pack_rgba_span( const GLcontext
*ctx
,
561 GLuint n
, CONST GLubyte rgba
[][4],
562 GLenum format
, GLenum type
, GLvoid
*destination
,
563 const struct gl_pixelstore_attrib
*packing
,
564 GLboolean applyTransferOps
)
566 applyTransferOps
&= (ctx
->Pixel
.ScaleOrBiasRGBA
|| ctx
->Pixel
.MapColorFlag
);
568 /* Test for optimized case first */
569 if (!applyTransferOps
&& format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
570 /* common simple case */
571 MEMCPY( destination
, rgba
, n
* 4 * sizeof(GLubyte
) );
573 else if (!applyTransferOps
&& format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
574 /* common simple case */
576 GLubyte
*dest
= (GLubyte
*) destination
;
577 for (i
= 0; i
< n
; i
++) {
578 dest
[0] = rgba
[i
][RCOMP
];
579 dest
[1] = rgba
[i
][GCOMP
];
580 dest
[2] = rgba
[i
][BCOMP
];
585 /* general solution */
586 GLfloat red
[MAX_WIDTH
], green
[MAX_WIDTH
], blue
[MAX_WIDTH
];
587 GLfloat alpha
[MAX_WIDTH
], luminance
[MAX_WIDTH
];
588 const GLfloat rscale
= 1.0F
/ 255.0F
;
589 const GLfloat gscale
= 1.0F
/ 255.0F
;
590 const GLfloat bscale
= 1.0F
/ 255.0F
;
591 const GLfloat ascale
= 1.0F
/ 255.0F
;
592 const GLint comps
= gl_components_in_format(format
);
595 assert(n
<= MAX_WIDTH
);
597 /* convert color components to floating point */
599 red
[i
] = rgba
[i
][RCOMP
] * rscale
;
600 green
[i
] = rgba
[i
][GCOMP
] * gscale
;
601 blue
[i
] = rgba
[i
][BCOMP
] * bscale
;
602 alpha
[i
] = rgba
[i
][ACOMP
] * ascale
;
606 * Apply scale, bias and lookup-tables if enabled.
608 if (applyTransferOps
) {
609 if (ctx
->Pixel
.ScaleOrBiasRGBA
) {
610 gl_scale_and_bias_color( ctx
, n
, red
, green
, blue
, alpha
);
612 if (ctx
->Pixel
.MapColorFlag
) {
613 gl_map_color( ctx
, n
, red
, green
, blue
, alpha
);
617 if (format
==GL_LUMINANCE
|| format
==GL_LUMINANCE_ALPHA
) {
619 GLfloat sum
= red
[i
] + green
[i
] + blue
[i
];
620 luminance
[i
] = CLAMP( sum
, 0.0F
, 1.0F
);
625 * Pack/store the pixels. Ugh! Lots of cases!!!
628 case GL_UNSIGNED_BYTE
:
630 GLubyte
*dst
= (GLubyte
*) destination
;
634 dst
[i
] = FLOAT_TO_UBYTE(red
[i
]);
638 dst
[i
] = FLOAT_TO_UBYTE(green
[i
]);
642 dst
[i
] = FLOAT_TO_UBYTE(blue
[i
]);
646 dst
[i
] = FLOAT_TO_UBYTE(alpha
[i
]);
650 dst
[i
] = FLOAT_TO_UBYTE(luminance
[i
]);
652 case GL_LUMINANCE_ALPHA
:
654 dst
[i
*2+0] = FLOAT_TO_UBYTE(luminance
[i
]);
655 dst
[i
*2+1] = FLOAT_TO_UBYTE(alpha
[i
]);
660 dst
[i
*3+0] = FLOAT_TO_UBYTE(red
[i
]);
661 dst
[i
*3+1] = FLOAT_TO_UBYTE(green
[i
]);
662 dst
[i
*3+2] = FLOAT_TO_UBYTE(blue
[i
]);
667 dst
[i
*4+0] = FLOAT_TO_UBYTE(red
[i
]);
668 dst
[i
*4+1] = FLOAT_TO_UBYTE(green
[i
]);
669 dst
[i
*4+2] = FLOAT_TO_UBYTE(blue
[i
]);
670 dst
[i
*4+3] = FLOAT_TO_UBYTE(alpha
[i
]);
675 dst
[i
*3+0] = FLOAT_TO_UBYTE(blue
[i
]);
676 dst
[i
*3+1] = FLOAT_TO_UBYTE(green
[i
]);
677 dst
[i
*3+2] = FLOAT_TO_UBYTE(red
[i
]);
682 dst
[i
*4+0] = FLOAT_TO_UBYTE(blue
[i
]);
683 dst
[i
*4+1] = FLOAT_TO_UBYTE(green
[i
]);
684 dst
[i
*4+2] = FLOAT_TO_UBYTE(red
[i
]);
685 dst
[i
*4+3] = FLOAT_TO_UBYTE(alpha
[i
]);
690 dst
[i
*4+0] = FLOAT_TO_UBYTE(alpha
[i
]);
691 dst
[i
*4+1] = FLOAT_TO_UBYTE(blue
[i
]);
692 dst
[i
*4+2] = FLOAT_TO_UBYTE(green
[i
]);
693 dst
[i
*4+3] = FLOAT_TO_UBYTE(red
[i
]);
697 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
703 GLbyte
*dst
= (GLbyte
*) destination
;
707 dst
[i
] = FLOAT_TO_BYTE(red
[i
]);
711 dst
[i
] = FLOAT_TO_BYTE(green
[i
]);
715 dst
[i
] = FLOAT_TO_BYTE(blue
[i
]);
719 dst
[i
] = FLOAT_TO_BYTE(alpha
[i
]);
723 dst
[i
] = FLOAT_TO_BYTE(luminance
[i
]);
725 case GL_LUMINANCE_ALPHA
:
727 dst
[i
*2+0] = FLOAT_TO_BYTE(luminance
[i
]);
728 dst
[i
*2+1] = FLOAT_TO_BYTE(alpha
[i
]);
733 dst
[i
*3+0] = FLOAT_TO_BYTE(red
[i
]);
734 dst
[i
*3+1] = FLOAT_TO_BYTE(green
[i
]);
735 dst
[i
*3+2] = FLOAT_TO_BYTE(blue
[i
]);
740 dst
[i
*4+0] = FLOAT_TO_BYTE(red
[i
]);
741 dst
[i
*4+1] = FLOAT_TO_BYTE(green
[i
]);
742 dst
[i
*4+2] = FLOAT_TO_BYTE(blue
[i
]);
743 dst
[i
*4+3] = FLOAT_TO_BYTE(alpha
[i
]);
748 dst
[i
*3+0] = FLOAT_TO_BYTE(blue
[i
]);
749 dst
[i
*3+1] = FLOAT_TO_BYTE(green
[i
]);
750 dst
[i
*3+2] = FLOAT_TO_BYTE(red
[i
]);
755 dst
[i
*4+0] = FLOAT_TO_BYTE(blue
[i
]);
756 dst
[i
*4+1] = FLOAT_TO_BYTE(green
[i
]);
757 dst
[i
*4+2] = FLOAT_TO_BYTE(red
[i
]);
758 dst
[i
*4+3] = FLOAT_TO_BYTE(alpha
[i
]);
762 dst
[i
*4+0] = FLOAT_TO_BYTE(alpha
[i
]);
763 dst
[i
*4+1] = FLOAT_TO_BYTE(blue
[i
]);
764 dst
[i
*4+2] = FLOAT_TO_BYTE(green
[i
]);
765 dst
[i
*4+3] = FLOAT_TO_BYTE(red
[i
]);
769 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
773 case GL_UNSIGNED_SHORT
:
775 GLushort
*dst
= (GLushort
*) destination
;
779 dst
[i
] = FLOAT_TO_USHORT(red
[i
]);
783 dst
[i
] = FLOAT_TO_USHORT(green
[i
]);
787 dst
[i
] = FLOAT_TO_USHORT(blue
[i
]);
791 dst
[i
] = FLOAT_TO_USHORT(alpha
[i
]);
795 dst
[i
] = FLOAT_TO_USHORT(luminance
[i
]);
797 case GL_LUMINANCE_ALPHA
:
799 dst
[i
*2+0] = FLOAT_TO_USHORT(luminance
[i
]);
800 dst
[i
*2+1] = FLOAT_TO_USHORT(alpha
[i
]);
805 dst
[i
*3+0] = FLOAT_TO_USHORT(red
[i
]);
806 dst
[i
*3+1] = FLOAT_TO_USHORT(green
[i
]);
807 dst
[i
*3+2] = FLOAT_TO_USHORT(blue
[i
]);
812 dst
[i
*4+0] = FLOAT_TO_USHORT(red
[i
]);
813 dst
[i
*4+1] = FLOAT_TO_USHORT(green
[i
]);
814 dst
[i
*4+2] = FLOAT_TO_USHORT(blue
[i
]);
815 dst
[i
*4+3] = FLOAT_TO_USHORT(alpha
[i
]);
820 dst
[i
*3+0] = FLOAT_TO_USHORT(blue
[i
]);
821 dst
[i
*3+1] = FLOAT_TO_USHORT(green
[i
]);
822 dst
[i
*3+2] = FLOAT_TO_USHORT(red
[i
]);
827 dst
[i
*4+0] = FLOAT_TO_USHORT(blue
[i
]);
828 dst
[i
*4+1] = FLOAT_TO_USHORT(green
[i
]);
829 dst
[i
*4+2] = FLOAT_TO_USHORT(red
[i
]);
830 dst
[i
*4+3] = FLOAT_TO_USHORT(alpha
[i
]);
835 dst
[i
*4+0] = FLOAT_TO_USHORT(alpha
[i
]);
836 dst
[i
*4+1] = FLOAT_TO_USHORT(blue
[i
]);
837 dst
[i
*4+2] = FLOAT_TO_USHORT(green
[i
]);
838 dst
[i
*4+3] = FLOAT_TO_USHORT(red
[i
]);
842 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
844 if (packing
->SwapBytes
) {
845 gl_swap2( (GLushort
*) dst
, n
* comps
);
851 GLshort
*dst
= (GLshort
*) destination
;
855 dst
[i
] = FLOAT_TO_SHORT(red
[i
]);
859 dst
[i
] = FLOAT_TO_SHORT(green
[i
]);
863 dst
[i
] = FLOAT_TO_SHORT(blue
[i
]);
867 dst
[i
] = FLOAT_TO_SHORT(alpha
[i
]);
871 dst
[i
] = FLOAT_TO_SHORT(luminance
[i
]);
873 case GL_LUMINANCE_ALPHA
:
875 dst
[i
*2+0] = FLOAT_TO_SHORT(luminance
[i
]);
876 dst
[i
*2+1] = FLOAT_TO_SHORT(alpha
[i
]);
881 dst
[i
*3+0] = FLOAT_TO_SHORT(red
[i
]);
882 dst
[i
*3+1] = FLOAT_TO_SHORT(green
[i
]);
883 dst
[i
*3+2] = FLOAT_TO_SHORT(blue
[i
]);
888 dst
[i
*4+0] = FLOAT_TO_SHORT(red
[i
]);
889 dst
[i
*4+1] = FLOAT_TO_SHORT(green
[i
]);
890 dst
[i
*4+2] = FLOAT_TO_SHORT(blue
[i
]);
891 dst
[i
*4+3] = FLOAT_TO_SHORT(alpha
[i
]);
896 dst
[i
*3+0] = FLOAT_TO_SHORT(blue
[i
]);
897 dst
[i
*3+1] = FLOAT_TO_SHORT(green
[i
]);
898 dst
[i
*3+2] = FLOAT_TO_SHORT(red
[i
]);
903 dst
[i
*4+0] = FLOAT_TO_SHORT(blue
[i
]);
904 dst
[i
*4+1] = FLOAT_TO_SHORT(green
[i
]);
905 dst
[i
*4+2] = FLOAT_TO_SHORT(red
[i
]);
906 dst
[i
*4+3] = FLOAT_TO_SHORT(alpha
[i
]);
910 dst
[i
*4+0] = FLOAT_TO_SHORT(alpha
[i
]);
911 dst
[i
*4+1] = FLOAT_TO_SHORT(blue
[i
]);
912 dst
[i
*4+2] = FLOAT_TO_SHORT(green
[i
]);
913 dst
[i
*4+3] = FLOAT_TO_SHORT(red
[i
]);
917 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
919 if (packing
->SwapBytes
) {
920 gl_swap2( (GLushort
*) dst
, n
* comps
);
924 case GL_UNSIGNED_INT
:
926 GLuint
*dst
= (GLuint
*) destination
;
930 dst
[i
] = FLOAT_TO_UINT(red
[i
]);
934 dst
[i
] = FLOAT_TO_UINT(green
[i
]);
938 dst
[i
] = FLOAT_TO_UINT(blue
[i
]);
942 dst
[i
] = FLOAT_TO_UINT(alpha
[i
]);
946 dst
[i
] = FLOAT_TO_UINT(luminance
[i
]);
948 case GL_LUMINANCE_ALPHA
:
950 dst
[i
*2+0] = FLOAT_TO_UINT(luminance
[i
]);
951 dst
[i
*2+1] = FLOAT_TO_UINT(alpha
[i
]);
956 dst
[i
*3+0] = FLOAT_TO_UINT(red
[i
]);
957 dst
[i
*3+1] = FLOAT_TO_UINT(green
[i
]);
958 dst
[i
*3+2] = FLOAT_TO_UINT(blue
[i
]);
963 dst
[i
*4+0] = FLOAT_TO_UINT(red
[i
]);
964 dst
[i
*4+1] = FLOAT_TO_UINT(green
[i
]);
965 dst
[i
*4+2] = FLOAT_TO_UINT(blue
[i
]);
966 dst
[i
*4+3] = FLOAT_TO_UINT(alpha
[i
]);
971 dst
[i
*3+0] = FLOAT_TO_UINT(blue
[i
]);
972 dst
[i
*3+1] = FLOAT_TO_UINT(green
[i
]);
973 dst
[i
*3+2] = FLOAT_TO_UINT(red
[i
]);
978 dst
[i
*4+0] = FLOAT_TO_UINT(blue
[i
]);
979 dst
[i
*4+1] = FLOAT_TO_UINT(green
[i
]);
980 dst
[i
*4+2] = FLOAT_TO_UINT(red
[i
]);
981 dst
[i
*4+3] = FLOAT_TO_UINT(alpha
[i
]);
986 dst
[i
*4+0] = FLOAT_TO_UINT(alpha
[i
]);
987 dst
[i
*4+1] = FLOAT_TO_UINT(blue
[i
]);
988 dst
[i
*4+2] = FLOAT_TO_UINT(green
[i
]);
989 dst
[i
*4+3] = FLOAT_TO_UINT(red
[i
]);
993 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
995 if (packing
->SwapBytes
) {
996 gl_swap4( (GLuint
*) dst
, n
* comps
);
1002 GLint
*dst
= (GLint
*) destination
;
1006 dst
[i
] = FLOAT_TO_INT(red
[i
]);
1010 dst
[i
] = FLOAT_TO_INT(green
[i
]);
1014 dst
[i
] = FLOAT_TO_INT(blue
[i
]);
1018 dst
[i
] = FLOAT_TO_INT(alpha
[i
]);
1022 dst
[i
] = FLOAT_TO_INT(luminance
[i
]);
1024 case GL_LUMINANCE_ALPHA
:
1026 dst
[i
*2+0] = FLOAT_TO_INT(luminance
[i
]);
1027 dst
[i
*2+1] = FLOAT_TO_INT(alpha
[i
]);
1032 dst
[i
*3+0] = FLOAT_TO_INT(red
[i
]);
1033 dst
[i
*3+1] = FLOAT_TO_INT(green
[i
]);
1034 dst
[i
*3+2] = FLOAT_TO_INT(blue
[i
]);
1039 dst
[i
*4+0] = FLOAT_TO_INT(red
[i
]);
1040 dst
[i
*4+1] = FLOAT_TO_INT(green
[i
]);
1041 dst
[i
*4+2] = FLOAT_TO_INT(blue
[i
]);
1042 dst
[i
*4+3] = FLOAT_TO_INT(alpha
[i
]);
1047 dst
[i
*3+0] = FLOAT_TO_INT(blue
[i
]);
1048 dst
[i
*3+1] = FLOAT_TO_INT(green
[i
]);
1049 dst
[i
*3+2] = FLOAT_TO_INT(red
[i
]);
1054 dst
[i
*4+0] = FLOAT_TO_INT(blue
[i
]);
1055 dst
[i
*4+1] = FLOAT_TO_INT(green
[i
]);
1056 dst
[i
*4+2] = FLOAT_TO_INT(red
[i
]);
1057 dst
[i
*4+3] = FLOAT_TO_INT(alpha
[i
]);
1062 dst
[i
*4+0] = FLOAT_TO_INT(alpha
[i
]);
1063 dst
[i
*4+1] = FLOAT_TO_INT(blue
[i
]);
1064 dst
[i
*4+2] = FLOAT_TO_INT(green
[i
]);
1065 dst
[i
*4+3] = FLOAT_TO_INT(red
[i
]);
1069 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1071 if (packing
->SwapBytes
) {
1072 gl_swap4( (GLuint
*) dst
, n
* comps
);
1078 GLfloat
*dst
= (GLfloat
*) destination
;
1098 dst
[i
] = luminance
[i
];
1100 case GL_LUMINANCE_ALPHA
:
1102 dst
[i
*2+0] = luminance
[i
];
1103 dst
[i
*2+1] = alpha
[i
];
1108 dst
[i
*3+0] = red
[i
];
1109 dst
[i
*3+1] = green
[i
];
1110 dst
[i
*3+2] = blue
[i
];
1115 dst
[i
*4+0] = red
[i
];
1116 dst
[i
*4+1] = green
[i
];
1117 dst
[i
*4+2] = blue
[i
];
1118 dst
[i
*4+3] = alpha
[i
];
1123 dst
[i
*3+0] = blue
[i
];
1124 dst
[i
*3+1] = green
[i
];
1125 dst
[i
*3+2] = red
[i
];
1130 dst
[i
*4+0] = blue
[i
];
1131 dst
[i
*4+1] = green
[i
];
1132 dst
[i
*4+2] = red
[i
];
1133 dst
[i
*4+3] = alpha
[i
];
1138 dst
[i
*4+0] = alpha
[i
];
1139 dst
[i
*4+1] = blue
[i
];
1140 dst
[i
*4+2] = green
[i
];
1141 dst
[i
*4+3] = red
[i
];
1145 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1147 if (packing
->SwapBytes
) {
1148 gl_swap4( (GLuint
*) dst
, n
* comps
);
1152 case GL_UNSIGNED_BYTE_3_3_2
:
1153 if (format
== GL_RGB
) {
1154 GLubyte
*dst
= (GLubyte
*) destination
;
1156 dst
[i
] = (((GLint
) (red
[i
] * 7.0F
)) << 5)
1157 | (((GLint
) (green
[i
] * 7.0F
)) << 2)
1158 | (((GLint
) (blue
[i
] * 3.0F
)) );
1162 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1163 if (format
== GL_RGB
) {
1164 GLubyte
*dst
= (GLubyte
*) destination
;
1166 dst
[i
] = (((GLint
) (red
[i
] * 7.0F
)) )
1167 | (((GLint
) (green
[i
] * 7.0F
)) << 3)
1168 | (((GLint
) (blue
[i
] * 3.0F
)) << 5);
1172 case GL_UNSIGNED_SHORT_5_6_5
:
1173 if (format
== GL_RGB
) {
1174 GLushort
*dst
= (GLushort
*) destination
;
1176 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) << 11)
1177 | (((GLint
) (green
[i
] * 63.0F
)) << 5)
1178 | (((GLint
) (blue
[i
] * 31.0F
)) );
1182 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1183 if (format
== GL_RGB
) {
1184 GLushort
*dst
= (GLushort
*) destination
;
1186 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) )
1187 | (((GLint
) (green
[i
] * 63.0F
)) << 5)
1188 | (((GLint
) (blue
[i
] * 31.0F
)) << 11);
1192 case GL_UNSIGNED_SHORT_4_4_4_4
:
1193 if (format
== GL_RGB
) {
1194 GLushort
*dst
= (GLushort
*) destination
;
1196 dst
[i
] = (((GLint
) (red
[i
] * 15.0F
)) << 12)
1197 | (((GLint
) (green
[i
] * 15.0F
)) << 8)
1198 | (((GLint
) (blue
[i
] * 15.0F
)) << 4)
1199 | (((GLint
) (alpha
[i
] * 15.0F
)) );
1203 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1204 if (format
== GL_RGB
) {
1205 GLushort
*dst
= (GLushort
*) destination
;
1207 dst
[i
] = (((GLint
) (red
[i
] * 15.0F
)) )
1208 | (((GLint
) (green
[i
] * 15.0F
)) << 4)
1209 | (((GLint
) (blue
[i
] * 15.0F
)) << 8)
1210 | (((GLint
) (alpha
[i
] * 15.0F
)) << 12);
1214 case GL_UNSIGNED_SHORT_5_5_5_1
:
1215 if (format
== GL_RGB
) {
1216 GLushort
*dst
= (GLushort
*) destination
;
1218 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) << 11)
1219 | (((GLint
) (green
[i
] * 31.0F
)) << 6)
1220 | (((GLint
) (blue
[i
] * 31.0F
)) << 1)
1221 | (((GLint
) (alpha
[i
] * 1.0F
)) );
1225 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1226 if (format
== GL_RGB
) {
1227 GLushort
*dst
= (GLushort
*) destination
;
1229 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) )
1230 | (((GLint
) (green
[i
] * 31.0F
)) << 5)
1231 | (((GLint
) (blue
[i
] * 31.0F
)) << 10)
1232 | (((GLint
) (alpha
[i
] * 1.0F
)) << 15);
1236 case GL_UNSIGNED_INT_8_8_8_8
:
1237 if (format
== GL_RGBA
) {
1238 GLuint
*dst
= (GLuint
*) destination
;
1240 dst
[i
] = (((GLuint
) (red
[i
] * 255.0F
)) << 24)
1241 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
1242 | (((GLuint
) (blue
[i
] * 255.0F
)) << 8)
1243 | (((GLuint
) (alpha
[i
] * 255.0F
)) );
1246 else if (format
== GL_BGRA
) {
1247 GLuint
*dst
= (GLuint
*) destination
;
1249 dst
[i
] = (((GLuint
) (blue
[i
] * 255.0F
)) << 24)
1250 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
1251 | (((GLuint
) (red
[i
] * 255.0F
)) << 8)
1252 | (((GLuint
) (alpha
[i
] * 255.0F
)) );
1255 else if (format
== GL_ABGR_EXT
) {
1256 GLuint
*dst
= (GLuint
*) destination
;
1258 dst
[i
] = (((GLuint
) (alpha
[i
] * 255.0F
)) << 24)
1259 | (((GLuint
) (blue
[i
] * 255.0F
)) << 16)
1260 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
1261 | (((GLuint
) (red
[i
] * 255.0F
)) );
1265 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1266 if (format
== GL_RGBA
) {
1267 GLuint
*dst
= (GLuint
*) destination
;
1269 dst
[i
] = (((GLuint
) (red
[i
] * 255.0F
)) )
1270 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
1271 | (((GLuint
) (blue
[i
] * 255.0F
)) << 16)
1272 | (((GLuint
) (alpha
[i
] * 255.0F
)) << 24);
1275 else if (format
== GL_BGRA
) {
1276 GLuint
*dst
= (GLuint
*) destination
;
1278 dst
[i
] = (((GLuint
) (blue
[i
] * 255.0F
)) )
1279 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
1280 | (((GLuint
) (red
[i
] * 255.0F
)) << 16)
1281 | (((GLuint
) (alpha
[i
] * 255.0F
)) << 24);
1284 else if (format
== GL_ABGR_EXT
) {
1285 GLuint
*dst
= (GLuint
*) destination
;
1287 dst
[i
] = (((GLuint
) (alpha
[i
] * 255.0F
)) )
1288 | (((GLuint
) (blue
[i
] * 255.0F
)) << 8)
1289 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
1290 | (((GLuint
) (red
[i
] * 255.0F
)) << 24);
1294 case GL_UNSIGNED_INT_10_10_10_2
:
1295 if (format
== GL_RGBA
) {
1296 GLuint
*dst
= (GLuint
*) destination
;
1298 dst
[i
] = (((GLuint
) (red
[i
] * 1023.0F
)) << 22)
1299 | (((GLuint
) (green
[i
] * 1023.0F
)) << 12)
1300 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 2)
1301 | (((GLuint
) (alpha
[i
] * 3.0F
)) );
1304 else if (format
== GL_BGRA
) {
1305 GLuint
*dst
= (GLuint
*) destination
;
1307 dst
[i
] = (((GLuint
) (blue
[i
] * 1023.0F
)) << 22)
1308 | (((GLuint
) (green
[i
] * 1023.0F
)) << 12)
1309 | (((GLuint
) (red
[i
] * 1023.0F
)) << 2)
1310 | (((GLuint
) (alpha
[i
] * 3.0F
)) );
1313 else if (format
== GL_ABGR_EXT
) {
1314 GLuint
*dst
= (GLuint
*) destination
;
1316 dst
[i
] = (((GLuint
) (alpha
[i
] * 1023.0F
)) << 22)
1317 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 12)
1318 | (((GLuint
) (green
[i
] * 1023.0F
)) << 2)
1319 | (((GLuint
) (red
[i
] * 3.0F
)) );
1323 case GL_UNSIGNED_INT_2_10_10_10_REV
:
1324 if (format
== GL_RGBA
) {
1325 GLuint
*dst
= (GLuint
*) destination
;
1327 dst
[i
] = (((GLuint
) (red
[i
] * 1023.0F
)) )
1328 | (((GLuint
) (green
[i
] * 1023.0F
)) << 10)
1329 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 20)
1330 | (((GLuint
) (alpha
[i
] * 3.0F
)) << 30);
1333 else if (format
== GL_BGRA
) {
1334 GLuint
*dst
= (GLuint
*) destination
;
1336 dst
[i
] = (((GLuint
) (blue
[i
] * 1023.0F
)) )
1337 | (((GLuint
) (green
[i
] * 1023.0F
)) << 10)
1338 | (((GLuint
) (red
[i
] * 1023.0F
)) << 20)
1339 | (((GLuint
) (alpha
[i
] * 3.0F
)) << 30);
1342 else if (format
== GL_ABGR_EXT
) {
1343 GLuint
*dst
= (GLuint
*) destination
;
1345 dst
[i
] = (((GLuint
) (alpha
[i
] * 1023.0F
)) )
1346 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 10)
1347 | (((GLuint
) (green
[i
] * 1023.0F
)) << 20)
1348 | (((GLuint
) (red
[i
] * 3.0F
)) << 30);
1353 gl_problem( ctx
, "bad type in gl_pack_rgba_span" );
1359 #define SWAP2BYTE(VALUE) \
1361 GLubyte *bytes = (GLubyte *) &(VALUE); \
1362 GLubyte tmp = bytes[0]; \
1363 bytes[0] = bytes[1]; \
1367 #define SWAP4BYTE(VALUE) \
1369 GLubyte *bytes = (GLubyte *) &(VALUE); \
1370 GLubyte tmp = bytes[0]; \
1371 bytes[0] = bytes[3]; \
1374 bytes[1] = bytes[2]; \
1380 extract_uint_indexes(GLuint n
, GLuint indexes
[],
1381 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1382 const struct gl_pixelstore_attrib
*unpack
)
1384 assert(srcFormat
== GL_COLOR_INDEX
);
1386 ASSERT(srcType
== GL_BITMAP
||
1387 srcType
== GL_UNSIGNED_BYTE
||
1388 srcType
== GL_BYTE
||
1389 srcType
== GL_UNSIGNED_SHORT
||
1390 srcType
== GL_SHORT
||
1391 srcType
== GL_UNSIGNED_INT
||
1392 srcType
== GL_INT
||
1393 srcType
== GL_FLOAT
);
1398 GLubyte
*ubsrc
= (GLubyte
*) src
;
1399 if (unpack
->LsbFirst
) {
1400 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
1402 for (i
= 0; i
< n
; i
++) {
1403 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1414 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
1416 for (i
= 0; i
< n
; i
++) {
1417 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1429 case GL_UNSIGNED_BYTE
:
1432 const GLubyte
*s
= (const GLubyte
*) src
;
1433 for (i
= 0; i
< n
; i
++)
1440 const GLbyte
*s
= (const GLbyte
*) src
;
1441 for (i
= 0; i
< n
; i
++)
1445 case GL_UNSIGNED_SHORT
:
1448 const GLushort
*s
= (const GLushort
*) src
;
1449 if (unpack
->SwapBytes
) {
1450 for (i
= 0; i
< n
; i
++) {
1451 GLushort value
= s
[i
];
1457 for (i
= 0; i
< n
; i
++)
1465 const GLshort
*s
= (const GLshort
*) src
;
1466 if (unpack
->SwapBytes
) {
1467 for (i
= 0; i
< n
; i
++) {
1468 GLshort value
= s
[i
];
1474 for (i
= 0; i
< n
; i
++)
1479 case GL_UNSIGNED_INT
:
1482 const GLuint
*s
= (const GLuint
*) src
;
1483 if (unpack
->SwapBytes
) {
1484 for (i
= 0; i
< n
; i
++) {
1485 GLuint value
= s
[i
];
1491 for (i
= 0; i
< n
; i
++)
1499 const GLint
*s
= (const GLint
*) src
;
1500 if (unpack
->SwapBytes
) {
1501 for (i
= 0; i
< n
; i
++) {
1508 for (i
= 0; i
< n
; i
++)
1516 const GLfloat
*s
= (const GLfloat
*) src
;
1517 if (unpack
->SwapBytes
) {
1518 for (i
= 0; i
< n
; i
++) {
1519 GLfloat value
= s
[i
];
1521 indexes
[i
] = (GLuint
) value
;
1525 for (i
= 0; i
< n
; i
++)
1526 indexes
[i
] = (GLuint
) s
[i
];
1531 gl_problem(NULL
, "bad srcType in extract_uint_indexes");
1539 * This function extracts floating point RGBA values from arbitrary
1540 * image data. srcFormat and srcType are the format and type parameters
1541 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1543 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1544 * implements the "Conversion to floating point", "Conversion to RGB",
1545 * and "Final Expansion to RGBA" operations.
1547 * Args: n - number of pixels
1548 * rgba - output colors
1549 * srcFormat - format of incoming data
1550 * srcType - datatype of incoming data
1551 * src - source data pointer
1552 * swapBytes - perform byteswapping of incoming data?
1555 extract_float_rgba(GLuint n
, GLfloat rgba
[][4],
1556 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1557 GLboolean swapBytes
)
1559 GLint redIndex
, greenIndex
, blueIndex
, alphaIndex
;
1561 GLint rComp
, bComp
, gComp
, aComp
;
1563 ASSERT(srcFormat
== GL_RED
||
1564 srcFormat
== GL_GREEN
||
1565 srcFormat
== GL_BLUE
||
1566 srcFormat
== GL_ALPHA
||
1567 srcFormat
== GL_LUMINANCE
||
1568 srcFormat
== GL_LUMINANCE_ALPHA
||
1569 srcFormat
== GL_INTENSITY
||
1570 srcFormat
== GL_RGB
||
1571 srcFormat
== GL_BGR
||
1572 srcFormat
== GL_RGBA
||
1573 srcFormat
== GL_BGRA
||
1574 srcFormat
== GL_ABGR_EXT
);
1576 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
1577 srcType
== GL_BYTE
||
1578 srcType
== GL_UNSIGNED_SHORT
||
1579 srcType
== GL_SHORT
||
1580 srcType
== GL_UNSIGNED_INT
||
1581 srcType
== GL_INT
||
1582 srcType
== GL_FLOAT
||
1583 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
1584 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
1585 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
1586 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
1587 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
1588 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
1589 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
1590 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
1591 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
1592 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
1593 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
1594 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
);
1596 rComp
= gComp
= bComp
= aComp
= -1;
1598 switch (srcFormat
) {
1601 greenIndex
= blueIndex
= alphaIndex
= -1;
1606 redIndex
= blueIndex
= alphaIndex
= -1;
1611 redIndex
= greenIndex
= alphaIndex
= -1;
1615 redIndex
= greenIndex
= blueIndex
= -1;
1620 redIndex
= greenIndex
= blueIndex
= 0;
1624 case GL_LUMINANCE_ALPHA
:
1625 redIndex
= greenIndex
= blueIndex
= 0;
1631 greenIndex
= blueIndex
= alphaIndex
= -1;
1682 gl_problem(NULL
, "bad srcFormat in extract float data");
1687 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
1688 if ((INDEX) < 0) { \
1690 for (i = 0; i < n; i++) { \
1691 rgba[i][CHANNEL] = DEFAULT; \
1694 else if (swapBytes) { \
1695 const TYPE *s = (const TYPE *) src; \
1697 for (i = 0; i < n; i++) { \
1698 TYPE value = s[INDEX]; \
1699 if (sizeof(TYPE) == 2) { \
1702 else if (sizeof(TYPE) == 4) { \
1705 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
1710 const TYPE *s = (const TYPE *) src; \
1712 for (i = 0; i < n; i++) { \
1713 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
1719 case GL_UNSIGNED_BYTE
:
1720 PROCESS(redIndex
, RCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1721 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1722 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1723 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1726 PROCESS(redIndex
, RCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1727 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1728 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1729 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLbyte
, BYTE_TO_FLOAT
);
1731 case GL_UNSIGNED_SHORT
:
1732 PROCESS(redIndex
, RCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1733 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1734 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1735 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLushort
, USHORT_TO_FLOAT
);
1738 PROCESS(redIndex
, RCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1739 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1740 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1741 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLshort
, SHORT_TO_FLOAT
);
1743 case GL_UNSIGNED_INT
:
1744 PROCESS(redIndex
, RCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1745 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1746 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1747 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLuint
, UINT_TO_FLOAT
);
1750 PROCESS(redIndex
, RCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1751 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1752 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1753 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLint
, INT_TO_FLOAT
);
1756 PROCESS(redIndex
, RCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1757 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1758 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1759 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLfloat
, (GLfloat
));
1761 case GL_UNSIGNED_BYTE_3_3_2
:
1763 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1765 for (i
= 0; i
< n
; i
++) {
1766 GLubyte p
= ubsrc
[i
];
1767 rgba
[i
][RCOMP
] = ((p
>> 5) ) * (1.0F
/ 7.0F
);
1768 rgba
[i
][GCOMP
] = ((p
>> 2) & 0x7) * (1.0F
/ 7.0F
);
1769 rgba
[i
][BCOMP
] = ((p
) & 0x3) * (1.0F
/ 3.0F
);
1770 rgba
[i
][ACOMP
] = 1.0F
;
1774 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1776 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1778 for (i
= 0; i
< n
; i
++) {
1779 GLubyte p
= ubsrc
[i
];
1780 rgba
[i
][RCOMP
] = ((p
) & 0x7) * (1.0F
/ 7.0F
);
1781 rgba
[i
][GCOMP
] = ((p
>> 3) & 0x7) * (1.0F
/ 7.0F
);
1782 rgba
[i
][BCOMP
] = ((p
>> 6) ) * (1.0F
/ 3.0F
);
1783 rgba
[i
][ACOMP
] = 1.0F
;
1787 case GL_UNSIGNED_SHORT_5_6_5
:
1789 const GLushort
*ussrc
= (const GLushort
*) src
;
1791 for (i
= 0; i
< n
; i
++) {
1792 GLushort p
= ussrc
[i
];
1794 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1795 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1796 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1797 rgba
[i
][ACOMP
] = 1.0F
;
1801 const GLushort
*ussrc
= (const GLushort
*) src
;
1803 for (i
= 0; i
< n
; i
++) {
1804 GLushort p
= ussrc
[i
];
1805 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1806 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1807 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1808 rgba
[i
][ACOMP
] = 1.0F
;
1812 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1814 const GLushort
*ussrc
= (const GLushort
*) src
;
1816 for (i
= 0; i
< n
; i
++) {
1817 GLushort p
= ussrc
[i
];
1819 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1820 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1821 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1822 rgba
[i
][ACOMP
] = 1.0F
;
1826 const GLushort
*ussrc
= (const GLushort
*) src
;
1828 for (i
= 0; i
< n
; i
++) {
1829 GLushort p
= ussrc
[i
];
1830 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1831 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1832 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1833 rgba
[i
][ACOMP
] = 1.0F
;
1837 case GL_UNSIGNED_SHORT_4_4_4_4
:
1839 const GLushort
*ussrc
= (const GLushort
*) src
;
1841 for (i
= 0; i
< n
; i
++) {
1842 GLushort p
= ussrc
[i
];
1844 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1845 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1846 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1847 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1851 const GLushort
*ussrc
= (const GLushort
*) src
;
1853 for (i
= 0; i
< n
; i
++) {
1854 GLushort p
= ussrc
[i
];
1855 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1856 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1857 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1858 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1862 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1864 const GLushort
*ussrc
= (const GLushort
*) src
;
1866 for (i
= 0; i
< n
; i
++) {
1867 GLushort p
= ussrc
[i
];
1869 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1870 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1871 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1872 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1876 const GLushort
*ussrc
= (const GLushort
*) src
;
1878 for (i
= 0; i
< n
; i
++) {
1879 GLushort p
= ussrc
[i
];
1880 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1881 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1882 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1883 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1887 case GL_UNSIGNED_SHORT_5_5_5_1
:
1889 const GLushort
*ussrc
= (const GLushort
*) src
;
1891 for (i
= 0; i
< n
; i
++) {
1892 GLushort p
= ussrc
[i
];
1894 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1895 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
1896 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
1897 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
1901 const GLushort
*ussrc
= (const GLushort
*) src
;
1903 for (i
= 0; i
< n
; i
++) {
1904 GLushort p
= ussrc
[i
];
1905 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1906 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
1907 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
1908 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
1912 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1914 const GLushort
*ussrc
= (const GLushort
*) src
;
1916 for (i
= 0; i
< n
; i
++) {
1917 GLushort p
= ussrc
[i
];
1919 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1920 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
1921 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
1922 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
1926 const GLushort
*ussrc
= (const GLushort
*) src
;
1928 for (i
= 0; i
< n
; i
++) {
1929 GLushort p
= ussrc
[i
];
1930 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1931 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
1932 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
1933 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
1937 case GL_UNSIGNED_INT_8_8_8_8
:
1939 const GLuint
*uisrc
= (const GLuint
*) src
;
1941 for (i
= 0; i
< n
; i
++) {
1942 GLuint p
= uisrc
[i
];
1943 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1944 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1945 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1946 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1950 const GLuint
*uisrc
= (const GLuint
*) src
;
1952 for (i
= 0; i
< n
; i
++) {
1953 GLuint p
= uisrc
[i
];
1954 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1955 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1956 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1957 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1961 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1963 const GLuint
*uisrc
= (const GLuint
*) src
;
1965 for (i
= 0; i
< n
; i
++) {
1966 GLuint p
= uisrc
[i
];
1967 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1968 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1969 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1970 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1974 const GLuint
*uisrc
= (const GLuint
*) src
;
1976 for (i
= 0; i
< n
; i
++) {
1977 GLuint p
= uisrc
[i
];
1978 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1979 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1980 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1981 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1985 case GL_UNSIGNED_INT_10_10_10_2
:
1987 const GLuint
*uisrc
= (const GLuint
*) src
;
1989 for (i
= 0; i
< n
; i
++) {
1990 GLuint p
= uisrc
[i
];
1992 rgba
[i
][rComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
1993 rgba
[i
][gComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
1994 rgba
[i
][bComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
1995 rgba
[i
][aComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
1999 const GLuint
*uisrc
= (const GLuint
*) src
;
2001 for (i
= 0; i
< n
; i
++) {
2002 GLuint p
= uisrc
[i
];
2003 rgba
[i
][rComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
2004 rgba
[i
][gComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
2005 rgba
[i
][bComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
2006 rgba
[i
][aComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
2010 case GL_UNSIGNED_INT_2_10_10_10_REV
:
2012 const GLuint
*uisrc
= (const GLuint
*) src
;
2014 for (i
= 0; i
< n
; i
++) {
2015 GLuint p
= uisrc
[i
];
2017 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
2018 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
2019 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
2020 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
2024 const GLuint
*uisrc
= (const GLuint
*) src
;
2026 for (i
= 0; i
< n
; i
++) {
2027 GLuint p
= uisrc
[i
];
2028 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
2029 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
2030 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
2031 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
2036 gl_problem(NULL
, "bad srcType in extract float data");
2044 * Unpack a row of color image data from a client buffer according to
2045 * the pixel unpacking parameters. Apply any enabled pixel transfer
2046 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
2047 * Return GLubyte values in the specified dest image format.
2048 * This is (or will be) used by glDrawPixels and glTexImage?D().
2049 * Input: ctx - the context
2050 * n - number of pixels in the span
2051 * dstFormat - format of destination color array
2052 * dest - the destination color array
2053 * srcFormat - source image format
2054 * srcType - source image datatype
2055 * source - source image pointer
2056 * unpacking - pixel unpacking parameters
2057 * applyTransferOps - apply scale/bias/lookup-table ops?
2059 * XXX perhaps expand this to process whole images someday.
2062 _mesa_unpack_ubyte_color_span( const GLcontext
*ctx
,
2063 GLuint n
, GLenum dstFormat
, GLubyte dest
[],
2064 GLenum srcFormat
, GLenum srcType
,
2065 const GLvoid
*source
,
2066 const struct gl_pixelstore_attrib
*unpacking
,
2067 GLboolean applyTransferOps
)
2069 ASSERT(dstFormat
== GL_ALPHA
||
2070 dstFormat
== GL_LUMINANCE
||
2071 dstFormat
== GL_LUMINANCE_ALPHA
||
2072 dstFormat
== GL_INTENSITY
||
2073 dstFormat
== GL_RGB
||
2074 dstFormat
== GL_RGBA
||
2075 dstFormat
== GL_COLOR_INDEX
);
2077 ASSERT(srcFormat
== GL_RED
||
2078 srcFormat
== GL_GREEN
||
2079 srcFormat
== GL_BLUE
||
2080 srcFormat
== GL_ALPHA
||
2081 srcFormat
== GL_LUMINANCE
||
2082 srcFormat
== GL_LUMINANCE_ALPHA
||
2083 srcFormat
== GL_INTENSITY
||
2084 srcFormat
== GL_RGB
||
2085 srcFormat
== GL_BGR
||
2086 srcFormat
== GL_RGBA
||
2087 srcFormat
== GL_BGRA
||
2088 srcFormat
== GL_ABGR_EXT
||
2089 srcFormat
== GL_COLOR_INDEX
);
2091 ASSERT(srcType
== GL_BITMAP
||
2092 srcType
== GL_UNSIGNED_BYTE
||
2093 srcType
== GL_BYTE
||
2094 srcType
== GL_UNSIGNED_SHORT
||
2095 srcType
== GL_SHORT
||
2096 srcType
== GL_UNSIGNED_INT
||
2097 srcType
== GL_INT
||
2098 srcType
== GL_FLOAT
||
2099 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2100 srcType
== GL_UNSIGNED_BYTE_2_3_3_REV
||
2101 srcType
== GL_UNSIGNED_SHORT_5_6_5
||
2102 srcType
== GL_UNSIGNED_SHORT_5_6_5_REV
||
2103 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2104 srcType
== GL_UNSIGNED_SHORT_4_4_4_4_REV
||
2105 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2106 srcType
== GL_UNSIGNED_SHORT_1_5_5_5_REV
||
2107 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2108 srcType
== GL_UNSIGNED_INT_8_8_8_8_REV
||
2109 srcType
== GL_UNSIGNED_INT_10_10_10_2
||
2110 srcType
== GL_UNSIGNED_INT_2_10_10_10_REV
);
2112 /* this is intended for RGBA mode */
2113 assert(ctx
->Visual
->RGBAflag
);
2115 applyTransferOps
&= (ctx
->Pixel
.ScaleOrBiasRGBA
||
2116 ctx
->Pixel
.MapColorFlag
||
2117 ctx
->Pixel
.MapColorFlag
);
2119 /* Try simple cases first */
2120 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
) {
2121 if (dstFormat
== GL_RGBA
) {
2122 if (srcFormat
== GL_RGBA
) {
2123 MEMCPY( dest
, source
, n
* 4 * sizeof(GLubyte
) );
2126 else if (srcFormat
== GL_RGB
) {
2128 const GLubyte
*src
= (const GLubyte
*) source
;
2129 GLubyte
*dst
= dest
;
2130 for (i
= 0; i
< n
; i
++) {
2141 else if (dstFormat
== GL_RGB
) {
2142 if (srcFormat
== GL_RGB
) {
2143 MEMCPY( dest
, source
, n
* 3 * sizeof(GLubyte
) );
2146 else if (srcFormat
== GL_RGBA
) {
2148 const GLubyte
*src
= (const GLubyte
*) source
;
2149 GLubyte
*dst
= dest
;
2150 for (i
= 0; i
< n
; i
++) {
2160 else if (dstFormat
== srcFormat
) {
2161 GLint comps
= gl_components_in_format(srcFormat
);
2163 MEMCPY( dest
, source
, n
* comps
* sizeof(GLubyte
) );
2170 /* general solution */
2171 GLfloat rgba
[MAX_WIDTH
][4];
2172 GLint dstComponents
;
2173 GLint dstRedIndex
, dstGreenIndex
, dstBlueIndex
, dstAlphaIndex
;
2174 GLint dstLuminanceIndex
, dstIntensityIndex
;
2176 dstComponents
= gl_components_in_format( dstFormat
);
2177 /* source & dest image formats should have been error checked by now */
2178 assert(dstComponents
> 0);
2181 * Extract image data and convert to RGBA floats
2183 assert(n
<= MAX_WIDTH
);
2184 if (srcFormat
== GL_COLOR_INDEX
) {
2185 GLuint indexes
[MAX_WIDTH
];
2186 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
2189 /* shift and offset indexes */
2190 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2192 if (dstFormat
== GL_COLOR_INDEX
) {
2193 if (applyTransferOps
) {
2194 if (ctx
->Pixel
.MapColorFlag
) {
2195 /* Apply lookup table */
2196 gl_map_ci(ctx
, n
, indexes
);
2199 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2204 /* convert to GLubyte and return */
2207 for (i
= 0; i
< n
; i
++) {
2208 dest
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2213 /* Convert indexes to RGBA */
2214 gl_map_ci_to_rgba_float(ctx
, n
, indexes
, rgba
);
2218 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
2219 unpacking
->SwapBytes
);
2221 if (applyTransferOps
) {
2222 /* scale and bias colors */
2223 gl_scale_and_bias_rgba_float(ctx
, n
, rgba
);
2225 /* color table lookup */
2226 if (ctx
->Pixel
.MapColorFlag
) {
2227 gl_map_rgba_float(ctx
, n
, rgba
);
2234 * XXX This is where more color table lookups, convolution,
2235 * histograms, minmax, color matrix, etc would take place if
2237 * See figure 3.7 in the OpenGL 1.2 specification for more info.
2241 /* clamp to [0,1] */
2244 for (i
= 0; i
< n
; i
++) {
2245 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
2246 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
2247 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
2248 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
2252 /* Now determine which color channels we need to produce.
2253 * And determine the dest index (offset) within each color tuple.
2255 switch (dstFormat
) {
2258 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= -1;
2259 dstLuminanceIndex
= dstIntensityIndex
= -1;
2262 dstLuminanceIndex
= 0;
2263 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= dstAlphaIndex
= -1;
2264 dstIntensityIndex
= -1;
2266 case GL_LUMINANCE_ALPHA
:
2267 dstLuminanceIndex
= 0;
2269 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= -1;
2270 dstIntensityIndex
= -1;
2273 dstIntensityIndex
= 0;
2274 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= dstAlphaIndex
= -1;
2275 dstLuminanceIndex
= -1;
2281 dstAlphaIndex
= dstLuminanceIndex
= dstIntensityIndex
= -1;
2288 dstLuminanceIndex
= dstIntensityIndex
= -1;
2291 gl_problem(ctx
, "bad dstFormat in _mesa_unpack_ubyte_span()");
2296 /* Now return the GLubyte data in the requested dstFormat */
2298 if (dstRedIndex
>= 0) {
2299 GLubyte
*dst
= dest
;
2301 for (i
= 0; i
< n
; i
++) {
2302 dst
[dstRedIndex
] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2303 dst
+= dstComponents
;
2307 if (dstGreenIndex
>= 0) {
2308 GLubyte
*dst
= dest
;
2310 for (i
= 0; i
< n
; i
++) {
2311 dst
[dstGreenIndex
] = FLOAT_TO_UBYTE(rgba
[i
][GCOMP
]);
2312 dst
+= dstComponents
;
2316 if (dstBlueIndex
>= 0) {
2317 GLubyte
*dst
= dest
;
2319 for (i
= 0; i
< n
; i
++) {
2320 dst
[dstBlueIndex
] = FLOAT_TO_UBYTE(rgba
[i
][BCOMP
]);
2321 dst
+= dstComponents
;
2325 if (dstAlphaIndex
>= 0) {
2326 GLubyte
*dst
= dest
;
2328 for (i
= 0; i
< n
; i
++) {
2329 dst
[dstAlphaIndex
] = FLOAT_TO_UBYTE(rgba
[i
][ACOMP
]);
2330 dst
+= dstComponents
;
2334 if (dstIntensityIndex
>= 0) {
2335 GLubyte
*dst
= dest
;
2337 assert(dstIntensityIndex
== 0);
2338 assert(dstComponents
== 1);
2339 for (i
= 0; i
< n
; i
++) {
2340 /* Intensity comes from red channel */
2341 dst
[i
] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2345 if (dstLuminanceIndex
>= 0) {
2346 GLubyte
*dst
= dest
;
2348 assert(dstLuminanceIndex
== 0);
2349 for (i
= 0; i
< n
; i
++) {
2350 /* Luminance comes from red channel */
2351 dst
[0] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2352 dst
+= dstComponents
;
2361 * Unpack a row of color index data from a client buffer according to
2362 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2363 * and applyTransferOps is true.
2364 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2366 * Args: ctx - the context
2367 * n - number of pixels
2368 * dstType - destination datatype
2369 * dest - destination array
2370 * srcType - source pixel type
2371 * source - source data pointer
2372 * unpacking - pixel unpacking parameters
2373 * applyTransferOps - apply offset/bias/lookup ops?
2376 _mesa_unpack_index_span( const GLcontext
*ctx
, GLuint n
,
2377 GLenum dstType
, GLvoid
*dest
,
2378 GLenum srcType
, const GLvoid
*source
,
2379 const struct gl_pixelstore_attrib
*unpacking
,
2380 GLboolean applyTransferOps
)
2382 ASSERT(srcType
== GL_BITMAP
||
2383 srcType
== GL_UNSIGNED_BYTE
||
2384 srcType
== GL_BYTE
||
2385 srcType
== GL_UNSIGNED_SHORT
||
2386 srcType
== GL_SHORT
||
2387 srcType
== GL_UNSIGNED_INT
||
2388 srcType
== GL_INT
||
2389 srcType
== GL_FLOAT
);
2391 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
2392 dstType
== GL_UNSIGNED_SHORT
||
2393 dstType
== GL_UNSIGNED_INT
);
2395 applyTransferOps
&= (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapColorFlag
);
2398 * Try simple cases first
2400 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
2401 && dstType
== GL_UNSIGNED_BYTE
) {
2402 MEMCPY(dest
, source
, n
* sizeof(GLubyte
));
2404 else if (!applyTransferOps
&& srcType
== GL_UNSIGNED_INT
2405 && dstType
== GL_UNSIGNED_INT
&& !unpacking
->SwapBytes
) {
2406 MEMCPY(dest
, source
, n
* sizeof(GLuint
));
2412 GLuint indexes
[MAX_WIDTH
];
2413 assert(n
<= MAX_WIDTH
);
2415 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
2418 if (applyTransferOps
) {
2419 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2420 /* shift and offset indexes */
2421 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2424 if (ctx
->Pixel
.MapColorFlag
) {
2425 /* Apply lookup table */
2426 gl_map_ci(ctx
, n
, indexes
);
2430 /* convert to dest type */
2432 case GL_UNSIGNED_BYTE
:
2434 GLubyte
*dst
= (GLubyte
*) dest
;
2436 for (i
= 0; i
< n
; i
++) {
2437 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2441 case GL_UNSIGNED_SHORT
:
2443 GLuint
*dst
= (GLuint
*) dest
;
2445 for (i
= 0; i
< n
; i
++) {
2446 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
2450 case GL_UNSIGNED_INT
:
2451 MEMCPY(dest
, indexes
, n
* sizeof(GLuint
));
2454 gl_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
2461 * Unpack a row of stencil data from a client buffer according to
2462 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2463 * and applyTransferOps is true.
2464 * This is (or will be) used by glDrawPixels
2466 * Args: ctx - the context
2467 * n - number of pixels
2468 * dstType - destination datatype
2469 * dest - destination array
2470 * srcType - source pixel type
2471 * source - source data pointer
2472 * unpacking - pixel unpacking parameters
2473 * applyTransferOps - apply offset/bias/lookup ops?
2476 _mesa_unpack_stencil_span( const GLcontext
*ctx
, GLuint n
,
2477 GLenum dstType
, GLvoid
*dest
,
2478 GLenum srcType
, const GLvoid
*source
,
2479 const struct gl_pixelstore_attrib
*unpacking
,
2480 GLboolean applyTransferOps
)
2482 ASSERT(srcType
== GL_BITMAP
||
2483 srcType
== GL_UNSIGNED_BYTE
||
2484 srcType
== GL_BYTE
||
2485 srcType
== GL_UNSIGNED_SHORT
||
2486 srcType
== GL_SHORT
||
2487 srcType
== GL_UNSIGNED_INT
||
2488 srcType
== GL_INT
||
2489 srcType
== GL_FLOAT
);
2491 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
2492 dstType
== GL_UNSIGNED_SHORT
||
2493 dstType
== GL_UNSIGNED_INT
);
2495 applyTransferOps
&= (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapColorFlag
);
2498 * Try simple cases first
2500 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
2501 && dstType
== GL_UNSIGNED_BYTE
) {
2502 MEMCPY(dest
, source
, n
* sizeof(GLubyte
));
2504 else if (!applyTransferOps
&& srcType
== GL_UNSIGNED_INT
2505 && dstType
== GL_UNSIGNED_INT
&& !unpacking
->SwapBytes
) {
2506 MEMCPY(dest
, source
, n
* sizeof(GLuint
));
2512 GLuint indexes
[MAX_WIDTH
];
2513 assert(n
<= MAX_WIDTH
);
2515 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
2518 if (applyTransferOps
) {
2519 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2520 /* shift and offset indexes */
2521 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2524 if (ctx
->Pixel
.MapStencilFlag
) {
2525 /* Apply stencil lookup table */
2526 GLuint mask
= ctx
->Pixel
.MapStoSsize
- 1;
2529 indexes
[i
] = ctx
->Pixel
.MapStoS
[ indexes
[i
] & mask
];
2534 /* convert to dest type */
2536 case GL_UNSIGNED_BYTE
:
2538 GLubyte
*dst
= (GLubyte
*) dest
;
2540 for (i
= 0; i
< n
; i
++) {
2541 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2545 case GL_UNSIGNED_SHORT
:
2547 GLuint
*dst
= (GLuint
*) dest
;
2549 for (i
= 0; i
< n
; i
++) {
2550 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
2554 case GL_UNSIGNED_INT
:
2555 MEMCPY(dest
, indexes
, n
* sizeof(GLuint
));
2558 gl_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
2566 _mesa_unpack_depth_span( const GLcontext
*ctx
, GLuint n
, GLdepth
*dest
,
2567 GLenum srcType
, const GLvoid
*source
,
2568 const struct gl_pixelstore_attrib
*unpacking
,
2569 GLboolean applyTransferOps
)
2571 GLfloat
*depth
= MALLOC(n
* sizeof(GLfloat
));
2579 const GLubyte
*src
= (const GLubyte
*) source
;
2580 for (i
= 0; i
< n
; i
++) {
2581 depth
[i
] = BYTE_TO_FLOAT(src
[i
]);
2585 case GL_UNSIGNED_BYTE
:
2588 const GLubyte
*src
= (const GLubyte
*) source
;
2589 for (i
= 0; i
< n
; i
++) {
2590 depth
[i
] = UBYTE_TO_FLOAT(src
[i
]);
2597 const GLshort
*src
= (const GLshort
*) source
;
2598 for (i
= 0; i
< n
; i
++) {
2599 depth
[i
] = SHORT_TO_FLOAT(src
[i
]);
2603 case GL_UNSIGNED_SHORT
:
2606 const GLushort
*src
= (const GLushort
*) source
;
2607 for (i
= 0; i
< n
; i
++) {
2608 depth
[i
] = USHORT_TO_FLOAT(src
[i
]);
2615 const GLint
*src
= (const GLint
*) source
;
2616 for (i
= 0; i
< n
; i
++) {
2617 depth
[i
] = INT_TO_FLOAT(src
[i
]);
2621 case GL_UNSIGNED_INT
:
2624 const GLuint
*src
= (const GLuint
*) source
;
2625 for (i
= 0; i
< n
; i
++) {
2626 depth
[i
] = UINT_TO_FLOAT(src
[i
]);
2631 MEMCPY(depth
, source
, n
* sizeof(GLfloat
));
2634 gl_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
2639 /* apply depth scale and bias */
2640 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
2642 for (i
= 0; i
< n
; i
++) {
2643 depth
[i
] = depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
2647 /* clamp depth values to [0,1] and convert from floats to integers */
2649 const GLfloat zs
= ctx
->Visual
->DepthMaxF
;
2651 for (i
= 0; i
< n
; i
++) {
2652 dest
[i
] = (GLdepth
) (CLAMP(depth
[i
], 0.0F
, 1.0F
) * zs
);
2662 * Unpack image data. Apply byteswapping, byte flipping (bitmap).
2663 * Return all image data in a contiguous block.
2666 _mesa_unpack_image( GLsizei width
, GLsizei height
, GLsizei depth
,
2667 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2668 const struct gl_pixelstore_attrib
*unpack
)
2670 GLint bytesPerRow
, compsPerRow
;
2671 GLboolean flipBytes
, swap2
, swap4
;
2674 return NULL
; /* not necessarily an error */
2676 if (width
<= 0 || height
<= 0 || depth
<= 0)
2677 return NULL
; /* generate error later */
2679 if (format
== GL_BITMAP
) {
2680 bytesPerRow
= (width
+ 7) >> 3;
2681 flipBytes
= !unpack
->LsbFirst
;
2682 swap2
= swap4
= GL_FALSE
;
2686 const GLint bytesPerPixel
= gl_bytes_per_pixel(format
, type
);
2687 const GLint components
= gl_components_in_format(format
);
2689 if (bytesPerPixel
<= 0 || components
<= 0)
2690 return NULL
; /* bad format or type. generate error later */
2691 bytesPerRow
= bytesPerPixel
* width
;
2692 bytesPerComp
= bytesPerPixel
/ components
;
2693 flipBytes
= GL_FALSE
;
2694 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
2695 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
2696 compsPerRow
= components
* width
;
2697 assert(compsPerRow
>= width
);
2701 GLubyte
*destBuffer
= MALLOC(bytesPerRow
* height
* depth
);
2705 return NULL
; /* generate GL_OUT_OF_MEMORY later */
2708 for (img
= 0; img
< depth
; img
++) {
2709 for (row
= 0; row
< height
; row
++) {
2710 const GLvoid
*src
= gl_pixel_addr_in_image(unpack
, pixels
,
2711 width
, height
, format
, type
, img
, row
, 0);
2712 MEMCPY(dst
, src
, bytesPerRow
);
2713 /* byte flipping/swapping */
2715 gl_flip_bytes((GLubyte
*) dst
, bytesPerRow
);
2718 gl_swap2((GLushort
*) dst
, compsPerRow
);
2721 gl_swap4((GLuint
*) dst
, compsPerRow
);
2732 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
2733 * order with row alignment = 1 byte.
2736 _mesa_unpack_bitmap( GLint width
, GLint height
, const GLubyte
*pixels
,
2737 const struct gl_pixelstore_attrib
*packing
)
2739 GLint bytes
, row
, width_in_bytes
;
2740 GLubyte
*buffer
, *dst
;
2745 /* Alloc dest storage */
2746 bytes
= ((width
+ 7) / 8 * height
);
2747 buffer
= (GLubyte
*) MALLOC( bytes
);
2752 width_in_bytes
= CEILING( width
, 8 );
2754 for (row
= 0; row
< height
; row
++) {
2755 GLubyte
*src
= gl_pixel_addr_in_image( packing
, pixels
, width
, height
,
2756 GL_COLOR_INDEX
, GL_BITMAP
,
2763 if (packing
->SkipPixels
== 0) {
2764 MEMCPY( dst
, src
, width_in_bytes
);
2765 if (packing
->LsbFirst
) {
2766 gl_flip_bytes( dst
, width_in_bytes
);
2770 /* handling SkipPixels is a bit tricky (no pun intended!) */
2772 if (packing
->LsbFirst
) {
2773 GLubyte srcMask
= 1 << (packing
->SkipPixels
& 0x7);
2774 GLubyte dstMask
= 128;
2778 for (i
= 0; i
< width
; i
++) {
2782 if (srcMask
== 128) {
2787 srcMask
= srcMask
<< 1;
2795 dstMask
= dstMask
>> 1;
2800 GLubyte srcMask
= 128 >> (packing
->SkipPixels
& 0x7);
2801 GLubyte dstMask
= 128;
2805 for (i
= 0; i
< width
; i
++) {
2814 srcMask
= srcMask
>> 1;
2822 dstMask
= dstMask
>> 1;
2827 dst
+= width_in_bytes
;
2838 _mesa_pack_bitmap( GLint width
, GLint height
, const GLubyte
*source
,
2839 GLubyte
*dest
, const struct gl_pixelstore_attrib
*packing
)
2841 GLint row
, width_in_bytes
;
2847 width_in_bytes
= CEILING( width
, 8 );
2849 for (row
= 0; row
< height
; row
++) {
2850 GLubyte
*dst
= gl_pixel_addr_in_image( packing
, dest
, width
, height
,
2851 GL_COLOR_INDEX
, GL_BITMAP
,
2856 if (packing
->SkipPixels
== 0) {
2857 MEMCPY( dst
, src
, width_in_bytes
);
2858 if (packing
->LsbFirst
) {
2859 gl_flip_bytes( dst
, width_in_bytes
);
2863 /* handling SkipPixels is a bit tricky (no pun intended!) */
2865 if (packing
->LsbFirst
) {
2866 GLubyte srcMask
= 1 << (packing
->SkipPixels
& 0x7);
2867 GLubyte dstMask
= 128;
2868 const GLubyte
*s
= src
;
2871 for (i
= 0; i
< width
; i
++) {
2875 if (srcMask
== 128) {
2880 srcMask
= srcMask
<< 1;
2888 dstMask
= dstMask
>> 1;
2893 GLubyte srcMask
= 128 >> (packing
->SkipPixels
& 0x7);
2894 GLubyte dstMask
= 128;
2895 const GLubyte
*s
= src
;
2898 for (i
= 0; i
< width
; i
++) {
2907 srcMask
= srcMask
>> 1;
2915 dstMask
= dstMask
>> 1;
2920 src
+= width_in_bytes
;