1 /* $Id: image.c,v 1.15 2000/01/05 09:21:32 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.
496 void gl_unpack_polygon_stipple( const GLcontext
*ctx
,
497 const GLubyte
*pattern
, GLuint dest
[32] )
500 for (i
= 0; i
< 32; i
++) {
501 GLubyte
*src
= (GLubyte
*) gl_pixel_addr_in_image( &ctx
->Unpack
, pattern
,
502 32, 32, GL_COLOR_INDEX
, GL_BITMAP
, 0, i
, 0 );
503 dest
[i
] = (src
[0] << 24)
509 /* Bit flipping within each byte */
510 if (ctx
->Unpack
.LsbFirst
) {
511 gl_flip_bytes( (GLubyte
*) dest
, 32 * 4 );
518 * Pack polygon stipple into user memory given current pixel packing
521 void gl_pack_polygon_stipple( const GLcontext
*ctx
,
522 const GLuint pattern
[32],
526 for (i
= 0; i
< 32; i
++) {
527 GLubyte
*dst
= (GLubyte
*) gl_pixel_addr_in_image( &ctx
->Pack
, dest
,
528 32, 32, GL_COLOR_INDEX
, GL_BITMAP
, 0, i
, 0 );
529 dst
[0] = (pattern
[i
] >> 24) & 0xff;
530 dst
[1] = (pattern
[i
] >> 16) & 0xff;
531 dst
[2] = (pattern
[i
] >> 8) & 0xff;
532 dst
[3] = (pattern
[i
] ) & 0xff;
534 /* Bit flipping within each byte */
535 if (ctx
->Pack
.LsbFirst
) {
536 gl_flip_bytes( (GLubyte
*) dst
, 4 );
544 * Pack the given RGBA span into client memory at 'dest' address
545 * in the given pixel format and type.
546 * Optionally apply the enabled pixel transfer ops.
547 * Pack into memory using the given packing params struct.
548 * This is used by glReadPixels and glGetTexImage?D()
549 * Input: ctx - the context
550 * n - number of pixels in the span
552 * format - dest packing format
553 * type - dest packing datatype
554 * destination - destination packing address
555 * packing - pixel packing parameters
556 * applyTransferOps - apply scale/bias/lookup-table ops?
558 void gl_pack_rgba_span( const GLcontext
*ctx
,
559 GLuint n
, CONST GLubyte rgba
[][4],
560 GLenum format
, GLenum type
, GLvoid
*destination
,
561 const struct gl_pixelstore_attrib
*packing
,
562 GLboolean applyTransferOps
)
564 applyTransferOps
&= (ctx
->Pixel
.ScaleOrBiasRGBA
|| ctx
->Pixel
.MapColorFlag
);
566 /* Test for optimized case first */
567 if (!applyTransferOps
&& format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
568 /* common simple case */
569 MEMCPY( destination
, rgba
, n
* 4 * sizeof(GLubyte
) );
571 else if (!applyTransferOps
&& format
== GL_RGB
&& type
== GL_UNSIGNED_BYTE
) {
572 /* common simple case */
574 GLubyte
*dest
= (GLubyte
*) destination
;
575 for (i
= 0; i
< n
; i
++) {
576 dest
[0] = rgba
[i
][RCOMP
];
577 dest
[1] = rgba
[i
][GCOMP
];
578 dest
[2] = rgba
[i
][BCOMP
];
583 /* general solution */
584 GLfloat red
[MAX_WIDTH
], green
[MAX_WIDTH
], blue
[MAX_WIDTH
];
585 GLfloat alpha
[MAX_WIDTH
], luminance
[MAX_WIDTH
];
586 const GLfloat rscale
= 1.0F
/ 255.0F
;
587 const GLfloat gscale
= 1.0F
/ 255.0F
;
588 const GLfloat bscale
= 1.0F
/ 255.0F
;
589 const GLfloat ascale
= 1.0F
/ 255.0F
;
590 const GLint comps
= gl_components_in_format(format
);
593 assert(n
<= MAX_WIDTH
);
595 /* convert color components to floating point */
597 red
[i
] = rgba
[i
][RCOMP
] * rscale
;
598 green
[i
] = rgba
[i
][GCOMP
] * gscale
;
599 blue
[i
] = rgba
[i
][BCOMP
] * bscale
;
600 alpha
[i
] = rgba
[i
][ACOMP
] * ascale
;
604 * Apply scale, bias and lookup-tables if enabled.
606 if (applyTransferOps
) {
607 if (ctx
->Pixel
.ScaleOrBiasRGBA
) {
608 gl_scale_and_bias_color( ctx
, n
, red
, green
, blue
, alpha
);
610 if (ctx
->Pixel
.MapColorFlag
) {
611 gl_map_color( ctx
, n
, red
, green
, blue
, alpha
);
615 if (format
==GL_LUMINANCE
|| format
==GL_LUMINANCE_ALPHA
) {
617 GLfloat sum
= red
[i
] + green
[i
] + blue
[i
];
618 luminance
[i
] = CLAMP( sum
, 0.0F
, 1.0F
);
623 * Pack/store the pixels. Ugh! Lots of cases!!!
626 case GL_UNSIGNED_BYTE
:
628 GLubyte
*dst
= (GLubyte
*) destination
;
632 dst
[i
] = FLOAT_TO_UBYTE(red
[i
]);
636 dst
[i
] = FLOAT_TO_UBYTE(green
[i
]);
640 dst
[i
] = FLOAT_TO_UBYTE(blue
[i
]);
644 dst
[i
] = FLOAT_TO_UBYTE(alpha
[i
]);
648 dst
[i
] = FLOAT_TO_UBYTE(luminance
[i
]);
650 case GL_LUMINANCE_ALPHA
:
652 dst
[i
*2+0] = FLOAT_TO_UBYTE(luminance
[i
]);
653 dst
[i
*2+1] = FLOAT_TO_UBYTE(alpha
[i
]);
658 dst
[i
*3+0] = FLOAT_TO_UBYTE(red
[i
]);
659 dst
[i
*3+1] = FLOAT_TO_UBYTE(green
[i
]);
660 dst
[i
*3+2] = FLOAT_TO_UBYTE(blue
[i
]);
665 dst
[i
*4+0] = FLOAT_TO_UBYTE(red
[i
]);
666 dst
[i
*4+1] = FLOAT_TO_UBYTE(green
[i
]);
667 dst
[i
*4+2] = FLOAT_TO_UBYTE(blue
[i
]);
668 dst
[i
*4+3] = FLOAT_TO_UBYTE(alpha
[i
]);
673 dst
[i
*3+0] = FLOAT_TO_UBYTE(blue
[i
]);
674 dst
[i
*3+1] = FLOAT_TO_UBYTE(green
[i
]);
675 dst
[i
*3+2] = FLOAT_TO_UBYTE(red
[i
]);
680 dst
[i
*4+0] = FLOAT_TO_UBYTE(blue
[i
]);
681 dst
[i
*4+1] = FLOAT_TO_UBYTE(green
[i
]);
682 dst
[i
*4+2] = FLOAT_TO_UBYTE(red
[i
]);
683 dst
[i
*4+3] = FLOAT_TO_UBYTE(alpha
[i
]);
688 dst
[i
*4+0] = FLOAT_TO_UBYTE(alpha
[i
]);
689 dst
[i
*4+1] = FLOAT_TO_UBYTE(blue
[i
]);
690 dst
[i
*4+2] = FLOAT_TO_UBYTE(green
[i
]);
691 dst
[i
*4+3] = FLOAT_TO_UBYTE(red
[i
]);
695 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
701 GLbyte
*dst
= (GLbyte
*) destination
;
705 dst
[i
] = FLOAT_TO_BYTE(red
[i
]);
709 dst
[i
] = FLOAT_TO_BYTE(green
[i
]);
713 dst
[i
] = FLOAT_TO_BYTE(blue
[i
]);
717 dst
[i
] = FLOAT_TO_BYTE(alpha
[i
]);
721 dst
[i
] = FLOAT_TO_BYTE(luminance
[i
]);
723 case GL_LUMINANCE_ALPHA
:
725 dst
[i
*2+0] = FLOAT_TO_BYTE(luminance
[i
]);
726 dst
[i
*2+1] = FLOAT_TO_BYTE(alpha
[i
]);
731 dst
[i
*3+0] = FLOAT_TO_BYTE(red
[i
]);
732 dst
[i
*3+1] = FLOAT_TO_BYTE(green
[i
]);
733 dst
[i
*3+2] = FLOAT_TO_BYTE(blue
[i
]);
738 dst
[i
*4+0] = FLOAT_TO_BYTE(red
[i
]);
739 dst
[i
*4+1] = FLOAT_TO_BYTE(green
[i
]);
740 dst
[i
*4+2] = FLOAT_TO_BYTE(blue
[i
]);
741 dst
[i
*4+3] = FLOAT_TO_BYTE(alpha
[i
]);
746 dst
[i
*3+0] = FLOAT_TO_BYTE(blue
[i
]);
747 dst
[i
*3+1] = FLOAT_TO_BYTE(green
[i
]);
748 dst
[i
*3+2] = FLOAT_TO_BYTE(red
[i
]);
753 dst
[i
*4+0] = FLOAT_TO_BYTE(blue
[i
]);
754 dst
[i
*4+1] = FLOAT_TO_BYTE(green
[i
]);
755 dst
[i
*4+2] = FLOAT_TO_BYTE(red
[i
]);
756 dst
[i
*4+3] = FLOAT_TO_BYTE(alpha
[i
]);
760 dst
[i
*4+0] = FLOAT_TO_BYTE(alpha
[i
]);
761 dst
[i
*4+1] = FLOAT_TO_BYTE(blue
[i
]);
762 dst
[i
*4+2] = FLOAT_TO_BYTE(green
[i
]);
763 dst
[i
*4+3] = FLOAT_TO_BYTE(red
[i
]);
767 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
771 case GL_UNSIGNED_SHORT
:
773 GLushort
*dst
= (GLushort
*) destination
;
777 dst
[i
] = FLOAT_TO_USHORT(red
[i
]);
781 dst
[i
] = FLOAT_TO_USHORT(green
[i
]);
785 dst
[i
] = FLOAT_TO_USHORT(blue
[i
]);
789 dst
[i
] = FLOAT_TO_USHORT(alpha
[i
]);
793 dst
[i
] = FLOAT_TO_USHORT(luminance
[i
]);
795 case GL_LUMINANCE_ALPHA
:
797 dst
[i
*2+0] = FLOAT_TO_USHORT(luminance
[i
]);
798 dst
[i
*2+1] = FLOAT_TO_USHORT(alpha
[i
]);
803 dst
[i
*3+0] = FLOAT_TO_USHORT(red
[i
]);
804 dst
[i
*3+1] = FLOAT_TO_USHORT(green
[i
]);
805 dst
[i
*3+2] = FLOAT_TO_USHORT(blue
[i
]);
810 dst
[i
*4+0] = FLOAT_TO_USHORT(red
[i
]);
811 dst
[i
*4+1] = FLOAT_TO_USHORT(green
[i
]);
812 dst
[i
*4+2] = FLOAT_TO_USHORT(blue
[i
]);
813 dst
[i
*4+3] = FLOAT_TO_USHORT(alpha
[i
]);
818 dst
[i
*3+0] = FLOAT_TO_USHORT(blue
[i
]);
819 dst
[i
*3+1] = FLOAT_TO_USHORT(green
[i
]);
820 dst
[i
*3+2] = FLOAT_TO_USHORT(red
[i
]);
825 dst
[i
*4+0] = FLOAT_TO_USHORT(blue
[i
]);
826 dst
[i
*4+1] = FLOAT_TO_USHORT(green
[i
]);
827 dst
[i
*4+2] = FLOAT_TO_USHORT(red
[i
]);
828 dst
[i
*4+3] = FLOAT_TO_USHORT(alpha
[i
]);
833 dst
[i
*4+0] = FLOAT_TO_USHORT(alpha
[i
]);
834 dst
[i
*4+1] = FLOAT_TO_USHORT(blue
[i
]);
835 dst
[i
*4+2] = FLOAT_TO_USHORT(green
[i
]);
836 dst
[i
*4+3] = FLOAT_TO_USHORT(red
[i
]);
840 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
842 if (packing
->SwapBytes
) {
843 gl_swap2( (GLushort
*) dst
, n
* comps
);
849 GLshort
*dst
= (GLshort
*) destination
;
853 dst
[i
] = FLOAT_TO_SHORT(red
[i
]);
857 dst
[i
] = FLOAT_TO_SHORT(green
[i
]);
861 dst
[i
] = FLOAT_TO_SHORT(blue
[i
]);
865 dst
[i
] = FLOAT_TO_SHORT(alpha
[i
]);
869 dst
[i
] = FLOAT_TO_SHORT(luminance
[i
]);
871 case GL_LUMINANCE_ALPHA
:
873 dst
[i
*2+0] = FLOAT_TO_SHORT(luminance
[i
]);
874 dst
[i
*2+1] = FLOAT_TO_SHORT(alpha
[i
]);
879 dst
[i
*3+0] = FLOAT_TO_SHORT(red
[i
]);
880 dst
[i
*3+1] = FLOAT_TO_SHORT(green
[i
]);
881 dst
[i
*3+2] = FLOAT_TO_SHORT(blue
[i
]);
886 dst
[i
*4+0] = FLOAT_TO_SHORT(red
[i
]);
887 dst
[i
*4+1] = FLOAT_TO_SHORT(green
[i
]);
888 dst
[i
*4+2] = FLOAT_TO_SHORT(blue
[i
]);
889 dst
[i
*4+3] = FLOAT_TO_SHORT(alpha
[i
]);
894 dst
[i
*3+0] = FLOAT_TO_SHORT(blue
[i
]);
895 dst
[i
*3+1] = FLOAT_TO_SHORT(green
[i
]);
896 dst
[i
*3+2] = FLOAT_TO_SHORT(red
[i
]);
901 dst
[i
*4+0] = FLOAT_TO_SHORT(blue
[i
]);
902 dst
[i
*4+1] = FLOAT_TO_SHORT(green
[i
]);
903 dst
[i
*4+2] = FLOAT_TO_SHORT(red
[i
]);
904 dst
[i
*4+3] = FLOAT_TO_SHORT(alpha
[i
]);
908 dst
[i
*4+0] = FLOAT_TO_SHORT(alpha
[i
]);
909 dst
[i
*4+1] = FLOAT_TO_SHORT(blue
[i
]);
910 dst
[i
*4+2] = FLOAT_TO_SHORT(green
[i
]);
911 dst
[i
*4+3] = FLOAT_TO_SHORT(red
[i
]);
915 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
917 if (packing
->SwapBytes
) {
918 gl_swap2( (GLushort
*) dst
, n
* comps
);
922 case GL_UNSIGNED_INT
:
924 GLuint
*dst
= (GLuint
*) destination
;
928 dst
[i
] = FLOAT_TO_UINT(red
[i
]);
932 dst
[i
] = FLOAT_TO_UINT(green
[i
]);
936 dst
[i
] = FLOAT_TO_UINT(blue
[i
]);
940 dst
[i
] = FLOAT_TO_UINT(alpha
[i
]);
944 dst
[i
] = FLOAT_TO_UINT(luminance
[i
]);
946 case GL_LUMINANCE_ALPHA
:
948 dst
[i
*2+0] = FLOAT_TO_UINT(luminance
[i
]);
949 dst
[i
*2+1] = FLOAT_TO_UINT(alpha
[i
]);
954 dst
[i
*3+0] = FLOAT_TO_UINT(red
[i
]);
955 dst
[i
*3+1] = FLOAT_TO_UINT(green
[i
]);
956 dst
[i
*3+2] = FLOAT_TO_UINT(blue
[i
]);
961 dst
[i
*4+0] = FLOAT_TO_UINT(red
[i
]);
962 dst
[i
*4+1] = FLOAT_TO_UINT(green
[i
]);
963 dst
[i
*4+2] = FLOAT_TO_UINT(blue
[i
]);
964 dst
[i
*4+3] = FLOAT_TO_UINT(alpha
[i
]);
969 dst
[i
*3+0] = FLOAT_TO_UINT(blue
[i
]);
970 dst
[i
*3+1] = FLOAT_TO_UINT(green
[i
]);
971 dst
[i
*3+2] = FLOAT_TO_UINT(red
[i
]);
976 dst
[i
*4+0] = FLOAT_TO_UINT(blue
[i
]);
977 dst
[i
*4+1] = FLOAT_TO_UINT(green
[i
]);
978 dst
[i
*4+2] = FLOAT_TO_UINT(red
[i
]);
979 dst
[i
*4+3] = FLOAT_TO_UINT(alpha
[i
]);
984 dst
[i
*4+0] = FLOAT_TO_UINT(alpha
[i
]);
985 dst
[i
*4+1] = FLOAT_TO_UINT(blue
[i
]);
986 dst
[i
*4+2] = FLOAT_TO_UINT(green
[i
]);
987 dst
[i
*4+3] = FLOAT_TO_UINT(red
[i
]);
991 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
993 if (packing
->SwapBytes
) {
994 gl_swap4( (GLuint
*) dst
, n
* comps
);
1000 GLint
*dst
= (GLint
*) destination
;
1004 dst
[i
] = FLOAT_TO_INT(red
[i
]);
1008 dst
[i
] = FLOAT_TO_INT(green
[i
]);
1012 dst
[i
] = FLOAT_TO_INT(blue
[i
]);
1016 dst
[i
] = FLOAT_TO_INT(alpha
[i
]);
1020 dst
[i
] = FLOAT_TO_INT(luminance
[i
]);
1022 case GL_LUMINANCE_ALPHA
:
1024 dst
[i
*2+0] = FLOAT_TO_INT(luminance
[i
]);
1025 dst
[i
*2+1] = FLOAT_TO_INT(alpha
[i
]);
1030 dst
[i
*3+0] = FLOAT_TO_INT(red
[i
]);
1031 dst
[i
*3+1] = FLOAT_TO_INT(green
[i
]);
1032 dst
[i
*3+2] = FLOAT_TO_INT(blue
[i
]);
1037 dst
[i
*4+0] = FLOAT_TO_INT(red
[i
]);
1038 dst
[i
*4+1] = FLOAT_TO_INT(green
[i
]);
1039 dst
[i
*4+2] = FLOAT_TO_INT(blue
[i
]);
1040 dst
[i
*4+3] = FLOAT_TO_INT(alpha
[i
]);
1045 dst
[i
*3+0] = FLOAT_TO_INT(blue
[i
]);
1046 dst
[i
*3+1] = FLOAT_TO_INT(green
[i
]);
1047 dst
[i
*3+2] = FLOAT_TO_INT(red
[i
]);
1052 dst
[i
*4+0] = FLOAT_TO_INT(blue
[i
]);
1053 dst
[i
*4+1] = FLOAT_TO_INT(green
[i
]);
1054 dst
[i
*4+2] = FLOAT_TO_INT(red
[i
]);
1055 dst
[i
*4+3] = FLOAT_TO_INT(alpha
[i
]);
1060 dst
[i
*4+0] = FLOAT_TO_INT(alpha
[i
]);
1061 dst
[i
*4+1] = FLOAT_TO_INT(blue
[i
]);
1062 dst
[i
*4+2] = FLOAT_TO_INT(green
[i
]);
1063 dst
[i
*4+3] = FLOAT_TO_INT(red
[i
]);
1067 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1069 if (packing
->SwapBytes
) {
1070 gl_swap4( (GLuint
*) dst
, n
* comps
);
1076 GLfloat
*dst
= (GLfloat
*) destination
;
1096 dst
[i
] = luminance
[i
];
1098 case GL_LUMINANCE_ALPHA
:
1100 dst
[i
*2+0] = luminance
[i
];
1101 dst
[i
*2+1] = alpha
[i
];
1106 dst
[i
*3+0] = red
[i
];
1107 dst
[i
*3+1] = green
[i
];
1108 dst
[i
*3+2] = blue
[i
];
1113 dst
[i
*4+0] = red
[i
];
1114 dst
[i
*4+1] = green
[i
];
1115 dst
[i
*4+2] = blue
[i
];
1116 dst
[i
*4+3] = alpha
[i
];
1121 dst
[i
*3+0] = blue
[i
];
1122 dst
[i
*3+1] = green
[i
];
1123 dst
[i
*3+2] = red
[i
];
1128 dst
[i
*4+0] = blue
[i
];
1129 dst
[i
*4+1] = green
[i
];
1130 dst
[i
*4+2] = red
[i
];
1131 dst
[i
*4+3] = alpha
[i
];
1136 dst
[i
*4+0] = alpha
[i
];
1137 dst
[i
*4+1] = blue
[i
];
1138 dst
[i
*4+2] = green
[i
];
1139 dst
[i
*4+3] = red
[i
];
1143 gl_problem(ctx
, "bad format in gl_pack_rgba_span\n");
1145 if (packing
->SwapBytes
) {
1146 gl_swap4( (GLuint
*) dst
, n
* comps
);
1150 case GL_UNSIGNED_BYTE_3_3_2
:
1151 if (format
== GL_RGB
) {
1152 GLubyte
*dst
= (GLubyte
*) destination
;
1154 dst
[i
] = (((GLint
) (red
[i
] * 7.0F
)) << 5)
1155 | (((GLint
) (green
[i
] * 7.0F
)) << 2)
1156 | (((GLint
) (blue
[i
] * 3.0F
)) );
1160 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1161 if (format
== GL_RGB
) {
1162 GLubyte
*dst
= (GLubyte
*) destination
;
1164 dst
[i
] = (((GLint
) (red
[i
] * 7.0F
)) )
1165 | (((GLint
) (green
[i
] * 7.0F
)) << 3)
1166 | (((GLint
) (blue
[i
] * 3.0F
)) << 5);
1170 case GL_UNSIGNED_SHORT_5_6_5
:
1171 if (format
== GL_RGB
) {
1172 GLushort
*dst
= (GLushort
*) destination
;
1174 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) << 11)
1175 | (((GLint
) (green
[i
] * 63.0F
)) << 5)
1176 | (((GLint
) (blue
[i
] * 31.0F
)) );
1180 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1181 if (format
== GL_RGB
) {
1182 GLushort
*dst
= (GLushort
*) destination
;
1184 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) )
1185 | (((GLint
) (green
[i
] * 63.0F
)) << 5)
1186 | (((GLint
) (blue
[i
] * 31.0F
)) << 11);
1190 case GL_UNSIGNED_SHORT_4_4_4_4
:
1191 if (format
== GL_RGB
) {
1192 GLushort
*dst
= (GLushort
*) destination
;
1194 dst
[i
] = (((GLint
) (red
[i
] * 15.0F
)) << 12)
1195 | (((GLint
) (green
[i
] * 15.0F
)) << 8)
1196 | (((GLint
) (blue
[i
] * 15.0F
)) << 4)
1197 | (((GLint
) (alpha
[i
] * 15.0F
)) );
1201 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1202 if (format
== GL_RGB
) {
1203 GLushort
*dst
= (GLushort
*) destination
;
1205 dst
[i
] = (((GLint
) (red
[i
] * 15.0F
)) )
1206 | (((GLint
) (green
[i
] * 15.0F
)) << 4)
1207 | (((GLint
) (blue
[i
] * 15.0F
)) << 8)
1208 | (((GLint
) (alpha
[i
] * 15.0F
)) << 12);
1212 case GL_UNSIGNED_SHORT_5_5_5_1
:
1213 if (format
== GL_RGB
) {
1214 GLushort
*dst
= (GLushort
*) destination
;
1216 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) << 11)
1217 | (((GLint
) (green
[i
] * 31.0F
)) << 6)
1218 | (((GLint
) (blue
[i
] * 31.0F
)) << 1)
1219 | (((GLint
) (alpha
[i
] * 1.0F
)) );
1223 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1224 if (format
== GL_RGB
) {
1225 GLushort
*dst
= (GLushort
*) destination
;
1227 dst
[i
] = (((GLint
) (red
[i
] * 31.0F
)) )
1228 | (((GLint
) (green
[i
] * 31.0F
)) << 5)
1229 | (((GLint
) (blue
[i
] * 31.0F
)) << 10)
1230 | (((GLint
) (alpha
[i
] * 1.0F
)) << 15);
1234 case GL_UNSIGNED_INT_8_8_8_8
:
1235 if (format
== GL_RGBA
) {
1236 GLuint
*dst
= (GLuint
*) destination
;
1238 dst
[i
] = (((GLuint
) (red
[i
] * 255.0F
)) << 24)
1239 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
1240 | (((GLuint
) (blue
[i
] * 255.0F
)) << 8)
1241 | (((GLuint
) (alpha
[i
] * 255.0F
)) );
1244 else if (format
== GL_BGRA
) {
1245 GLuint
*dst
= (GLuint
*) destination
;
1247 dst
[i
] = (((GLuint
) (blue
[i
] * 255.0F
)) << 24)
1248 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
1249 | (((GLuint
) (red
[i
] * 255.0F
)) << 8)
1250 | (((GLuint
) (alpha
[i
] * 255.0F
)) );
1253 else if (format
== GL_ABGR_EXT
) {
1254 GLuint
*dst
= (GLuint
*) destination
;
1256 dst
[i
] = (((GLuint
) (alpha
[i
] * 255.0F
)) << 24)
1257 | (((GLuint
) (blue
[i
] * 255.0F
)) << 16)
1258 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
1259 | (((GLuint
) (red
[i
] * 255.0F
)) );
1263 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1264 if (format
== GL_RGBA
) {
1265 GLuint
*dst
= (GLuint
*) destination
;
1267 dst
[i
] = (((GLuint
) (red
[i
] * 255.0F
)) )
1268 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
1269 | (((GLuint
) (blue
[i
] * 255.0F
)) << 16)
1270 | (((GLuint
) (alpha
[i
] * 255.0F
)) << 24);
1273 else if (format
== GL_BGRA
) {
1274 GLuint
*dst
= (GLuint
*) destination
;
1276 dst
[i
] = (((GLuint
) (blue
[i
] * 255.0F
)) )
1277 | (((GLuint
) (green
[i
] * 255.0F
)) << 8)
1278 | (((GLuint
) (red
[i
] * 255.0F
)) << 16)
1279 | (((GLuint
) (alpha
[i
] * 255.0F
)) << 24);
1282 else if (format
== GL_ABGR_EXT
) {
1283 GLuint
*dst
= (GLuint
*) destination
;
1285 dst
[i
] = (((GLuint
) (alpha
[i
] * 255.0F
)) )
1286 | (((GLuint
) (blue
[i
] * 255.0F
)) << 8)
1287 | (((GLuint
) (green
[i
] * 255.0F
)) << 16)
1288 | (((GLuint
) (red
[i
] * 255.0F
)) << 24);
1292 case GL_UNSIGNED_INT_10_10_10_2
:
1293 if (format
== GL_RGBA
) {
1294 GLuint
*dst
= (GLuint
*) destination
;
1296 dst
[i
] = (((GLuint
) (red
[i
] * 1023.0F
)) << 22)
1297 | (((GLuint
) (green
[i
] * 1023.0F
)) << 12)
1298 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 2)
1299 | (((GLuint
) (alpha
[i
] * 3.0F
)) );
1302 else if (format
== GL_BGRA
) {
1303 GLuint
*dst
= (GLuint
*) destination
;
1305 dst
[i
] = (((GLuint
) (blue
[i
] * 1023.0F
)) << 22)
1306 | (((GLuint
) (green
[i
] * 1023.0F
)) << 12)
1307 | (((GLuint
) (red
[i
] * 1023.0F
)) << 2)
1308 | (((GLuint
) (alpha
[i
] * 3.0F
)) );
1311 else if (format
== GL_ABGR_EXT
) {
1312 GLuint
*dst
= (GLuint
*) destination
;
1314 dst
[i
] = (((GLuint
) (alpha
[i
] * 1023.0F
)) << 22)
1315 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 12)
1316 | (((GLuint
) (green
[i
] * 1023.0F
)) << 2)
1317 | (((GLuint
) (red
[i
] * 3.0F
)) );
1321 case GL_UNSIGNED_INT_2_10_10_10_REV
:
1322 if (format
== GL_RGBA
) {
1323 GLuint
*dst
= (GLuint
*) destination
;
1325 dst
[i
] = (((GLuint
) (red
[i
] * 1023.0F
)) )
1326 | (((GLuint
) (green
[i
] * 1023.0F
)) << 10)
1327 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 20)
1328 | (((GLuint
) (alpha
[i
] * 3.0F
)) << 30);
1331 else if (format
== GL_BGRA
) {
1332 GLuint
*dst
= (GLuint
*) destination
;
1334 dst
[i
] = (((GLuint
) (blue
[i
] * 1023.0F
)) )
1335 | (((GLuint
) (green
[i
] * 1023.0F
)) << 10)
1336 | (((GLuint
) (red
[i
] * 1023.0F
)) << 20)
1337 | (((GLuint
) (alpha
[i
] * 3.0F
)) << 30);
1340 else if (format
== GL_ABGR_EXT
) {
1341 GLuint
*dst
= (GLuint
*) destination
;
1343 dst
[i
] = (((GLuint
) (alpha
[i
] * 1023.0F
)) )
1344 | (((GLuint
) (blue
[i
] * 1023.0F
)) << 10)
1345 | (((GLuint
) (green
[i
] * 1023.0F
)) << 20)
1346 | (((GLuint
) (red
[i
] * 3.0F
)) << 30);
1351 gl_problem( ctx
, "bad type in gl_pack_rgba_span" );
1357 #define SWAP2BYTE(VALUE) \
1359 GLubyte *bytes = (GLubyte *) &(VALUE); \
1360 GLubyte tmp = bytes[0]; \
1361 bytes[0] = bytes[1]; \
1365 #define SWAP4BYTE(VALUE) \
1367 GLubyte *bytes = (GLubyte *) &(VALUE); \
1368 GLubyte tmp = bytes[0]; \
1369 bytes[0] = bytes[3]; \
1372 bytes[1] = bytes[2]; \
1378 extract_uint_indexes(GLuint n
, GLuint indexes
[],
1379 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1380 const struct gl_pixelstore_attrib
*unpack
)
1382 assert(srcFormat
== GL_COLOR_INDEX
);
1384 ASSERT(srcType
== GL_BITMAP
||
1385 srcType
== GL_UNSIGNED_BYTE
||
1386 srcType
== GL_BYTE
||
1387 srcType
== GL_UNSIGNED_SHORT
||
1388 srcType
== GL_SHORT
||
1389 srcType
== GL_UNSIGNED_INT
||
1390 srcType
== GL_INT
||
1391 srcType
== GL_FLOAT
);
1396 GLubyte
*ubsrc
= (GLubyte
*) src
;
1397 if (unpack
->LsbFirst
) {
1398 GLubyte mask
= 1 << (unpack
->SkipPixels
& 0x7);
1400 for (i
= 0; i
< n
; i
++) {
1401 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1412 GLubyte mask
= 128 >> (unpack
->SkipPixels
& 0x7);
1414 for (i
= 0; i
< n
; i
++) {
1415 indexes
[i
] = (*ubsrc
& mask
) ? 1 : 0;
1427 case GL_UNSIGNED_BYTE
:
1430 const GLubyte
*s
= (const GLubyte
*) src
;
1431 for (i
= 0; i
< n
; i
++)
1438 const GLbyte
*s
= (const GLbyte
*) src
;
1439 for (i
= 0; i
< n
; i
++)
1443 case GL_UNSIGNED_SHORT
:
1446 const GLushort
*s
= (const GLushort
*) src
;
1447 if (unpack
->SwapBytes
) {
1448 for (i
= 0; i
< n
; i
++) {
1449 GLushort value
= s
[i
];
1455 for (i
= 0; i
< n
; i
++)
1463 const GLshort
*s
= (const GLshort
*) src
;
1464 if (unpack
->SwapBytes
) {
1465 for (i
= 0; i
< n
; i
++) {
1466 GLshort value
= s
[i
];
1472 for (i
= 0; i
< n
; i
++)
1477 case GL_UNSIGNED_INT
:
1480 const GLuint
*s
= (const GLuint
*) src
;
1481 if (unpack
->SwapBytes
) {
1482 for (i
= 0; i
< n
; i
++) {
1483 GLuint value
= s
[i
];
1489 for (i
= 0; i
< n
; i
++)
1497 const GLint
*s
= (const GLint
*) src
;
1498 if (unpack
->SwapBytes
) {
1499 for (i
= 0; i
< n
; i
++) {
1506 for (i
= 0; i
< n
; i
++)
1514 const GLfloat
*s
= (const GLfloat
*) src
;
1515 if (unpack
->SwapBytes
) {
1516 for (i
= 0; i
< n
; i
++) {
1517 GLfloat value
= s
[i
];
1523 for (i
= 0; i
< n
; i
++)
1529 gl_problem(NULL
, "bad srcType in extract_uint_indexes");
1537 * This function extracts floating point RGBA values from arbitrary
1538 * image data. srcFormat and srcType are the format and type parameters
1539 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
1541 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
1542 * implements the "Conversion to floating point", "Conversion to RGB",
1543 * and "Final Expansion to RGBA" operations.
1545 * Args: n - number of pixels
1546 * rgba - output colors
1547 * srcFormat - format of incoming data
1548 * srcType - datatype of incoming data
1549 * src - source data pointer
1550 * swapBytes - perform byteswapping of incoming data?
1553 extract_float_rgba(GLuint n
, GLfloat rgba
[][4],
1554 GLenum srcFormat
, GLenum srcType
, const GLvoid
*src
,
1555 GLboolean swapBytes
)
1557 GLint redIndex
, greenIndex
, blueIndex
, alphaIndex
;
1559 GLint rComp
, bComp
, gComp
, aComp
;
1561 ASSERT(srcFormat
== GL_RED
||
1562 srcFormat
== GL_GREEN
||
1563 srcFormat
== GL_BLUE
||
1564 srcFormat
== GL_ALPHA
||
1565 srcFormat
== GL_LUMINANCE
||
1566 srcFormat
== GL_LUMINANCE_ALPHA
||
1567 srcFormat
== GL_INTENSITY
||
1568 srcFormat
== GL_RGB
||
1569 srcFormat
== GL_BGR
||
1570 srcFormat
== GL_RGBA
||
1571 srcFormat
== GL_BGRA
||
1572 srcFormat
== GL_ABGR_EXT
);
1574 ASSERT(srcType
== GL_UNSIGNED_BYTE
||
1575 srcType
== GL_BYTE
||
1576 srcType
== GL_UNSIGNED_SHORT
||
1577 srcType
== GL_SHORT
||
1578 srcType
== GL_UNSIGNED_INT
||
1579 srcType
== GL_INT
||
1580 srcType
== GL_FLOAT
||
1581 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
1582 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
1583 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
1584 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
1585 srcType
== GL_UNSIGNED_INT_10_10_10_2
);
1587 switch (srcFormat
) {
1590 greenIndex
= blueIndex
= alphaIndex
= -1;
1595 redIndex
= blueIndex
= alphaIndex
= -1;
1600 redIndex
= greenIndex
= alphaIndex
= -1;
1604 redIndex
= greenIndex
= blueIndex
= -1;
1609 redIndex
= greenIndex
= blueIndex
= 0;
1613 case GL_LUMINANCE_ALPHA
:
1614 redIndex
= greenIndex
= blueIndex
= 0;
1620 greenIndex
= blueIndex
= alphaIndex
= -1;
1671 gl_problem(NULL
, "bad srcFormat in extract float data");
1676 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
1677 if ((INDEX) < 0) { \
1679 for (i = 0; i < n; i++) { \
1680 rgba[i][CHANNEL] = DEFAULT; \
1683 else if (swapBytes) { \
1684 const TYPE *s = (const TYPE *) src; \
1686 for (i = 0; i < n; i++) { \
1687 TYPE value = s[INDEX]; \
1688 if (sizeof(TYPE) == 2) { \
1691 else if (sizeof(TYPE) == 4) { \
1694 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
1699 const TYPE *s = (const TYPE *) src; \
1701 for (i = 0; i < n; i++) { \
1702 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
1708 case GL_UNSIGNED_BYTE
:
1709 PROCESS(redIndex
, RCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1710 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1711 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1712 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1715 PROCESS(redIndex
, RCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1716 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1717 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1718 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLbyte
, BYTE_TO_FLOAT
);
1720 case GL_UNSIGNED_SHORT
:
1721 PROCESS(redIndex
, RCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1722 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1723 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1724 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLushort
, USHORT_TO_FLOAT
);
1727 PROCESS(redIndex
, RCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1728 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1729 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1730 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLshort
, SHORT_TO_FLOAT
);
1732 case GL_UNSIGNED_INT
:
1733 PROCESS(redIndex
, RCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1734 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1735 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1736 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLuint
, UINT_TO_FLOAT
);
1739 PROCESS(redIndex
, RCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1740 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1741 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1742 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLint
, INT_TO_FLOAT
);
1745 PROCESS(redIndex
, RCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1746 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1747 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1748 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLfloat
, (GLfloat
));
1750 case GL_UNSIGNED_BYTE_3_3_2
:
1752 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1754 for (i
= 0; i
< n
; i
++) {
1755 GLubyte p
= ubsrc
[i
];
1756 rgba
[i
][RCOMP
] = ((p
>> 5) ) * (1.0F
/ 7.0F
);
1757 rgba
[i
][GCOMP
] = ((p
>> 2) & 0x7) * (1.0F
/ 7.0F
);
1758 rgba
[i
][BCOMP
] = ((p
) & 0x3) * (1.0F
/ 3.0F
);
1759 rgba
[i
][ACOMP
] = 1.0F
;
1763 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1765 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1767 for (i
= 0; i
< n
; i
++) {
1768 GLubyte p
= ubsrc
[i
];
1769 rgba
[i
][RCOMP
] = ((p
) & 0x7) * (1.0F
/ 7.0F
);
1770 rgba
[i
][GCOMP
] = ((p
>> 3) & 0x7) * (1.0F
/ 7.0F
);
1771 rgba
[i
][BCOMP
] = ((p
>> 6) ) * (1.0F
/ 3.0F
);
1772 rgba
[i
][ACOMP
] = 1.0F
;
1776 case GL_UNSIGNED_SHORT_5_6_5
:
1778 const GLushort
*ussrc
= (const GLushort
*) src
;
1780 for (i
= 0; i
< n
; i
++) {
1781 GLushort p
= ussrc
[i
];
1783 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1784 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1785 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1786 rgba
[i
][ACOMP
] = 1.0F
;
1790 const GLushort
*ussrc
= (const GLushort
*) src
;
1792 for (i
= 0; i
< n
; i
++) {
1793 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 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1803 const GLushort
*ussrc
= (const GLushort
*) src
;
1805 for (i
= 0; i
< n
; i
++) {
1806 GLushort p
= ussrc
[i
];
1808 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1809 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1810 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1811 rgba
[i
][ACOMP
] = 1.0F
;
1815 const GLushort
*ussrc
= (const GLushort
*) src
;
1817 for (i
= 0; i
< n
; i
++) {
1818 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 case GL_UNSIGNED_SHORT_4_4_4_4
:
1828 const GLushort
*ussrc
= (const GLushort
*) src
;
1830 for (i
= 0; i
< n
; i
++) {
1831 GLushort p
= ussrc
[i
];
1833 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1834 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1835 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1836 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1840 const GLushort
*ussrc
= (const GLushort
*) src
;
1842 for (i
= 0; i
< n
; i
++) {
1843 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 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1853 const GLushort
*ussrc
= (const GLushort
*) src
;
1855 for (i
= 0; i
< n
; i
++) {
1856 GLushort p
= ussrc
[i
];
1858 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1859 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1860 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1861 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1865 const GLushort
*ussrc
= (const GLushort
*) src
;
1867 for (i
= 0; i
< n
; i
++) {
1868 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 case GL_UNSIGNED_SHORT_5_5_5_1
:
1878 const GLushort
*ussrc
= (const GLushort
*) src
;
1880 for (i
= 0; i
< n
; i
++) {
1881 GLushort p
= ussrc
[i
];
1883 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1884 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
1885 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
1886 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
1890 const GLushort
*ussrc
= (const GLushort
*) src
;
1892 for (i
= 0; i
< n
; i
++) {
1893 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 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1903 const GLushort
*ussrc
= (const GLushort
*) src
;
1905 for (i
= 0; i
< n
; i
++) {
1906 GLushort p
= ussrc
[i
];
1908 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1909 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
1910 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
1911 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
1915 const GLushort
*ussrc
= (const GLushort
*) src
;
1917 for (i
= 0; i
< n
; i
++) {
1918 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 case GL_UNSIGNED_INT_8_8_8_8
:
1928 const GLuint
*uisrc
= (const GLuint
*) src
;
1930 for (i
= 0; i
< n
; i
++) {
1931 GLuint p
= uisrc
[i
];
1932 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1933 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1934 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1935 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
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
>> 24) );
1944 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1945 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1946 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1950 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1952 const GLuint
*uisrc
= (const GLuint
*) src
;
1954 for (i
= 0; i
< n
; i
++) {
1955 GLuint p
= uisrc
[i
];
1956 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1957 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1958 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1959 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
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
) & 0xff);
1968 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1969 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1970 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1974 case GL_UNSIGNED_INT_10_10_10_2
:
1976 const GLuint
*uisrc
= (const GLuint
*) src
;
1978 for (i
= 0; i
< n
; i
++) {
1979 GLuint p
= uisrc
[i
];
1981 rgba
[i
][rComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
1982 rgba
[i
][gComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
1983 rgba
[i
][bComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
1984 rgba
[i
][aComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
1988 const GLuint
*uisrc
= (const GLuint
*) src
;
1990 for (i
= 0; i
< n
; i
++) {
1991 GLuint p
= uisrc
[i
];
1992 rgba
[i
][rComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
1993 rgba
[i
][gComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
1994 rgba
[i
][bComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
1995 rgba
[i
][aComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
1999 case GL_UNSIGNED_INT_2_10_10_10_REV
:
2001 const GLuint
*uisrc
= (const GLuint
*) src
;
2003 for (i
= 0; i
< n
; i
++) {
2004 GLuint p
= uisrc
[i
];
2006 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
2007 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
2008 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
2009 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
2013 const GLuint
*uisrc
= (const GLuint
*) src
;
2015 for (i
= 0; i
< n
; i
++) {
2016 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
);
2025 gl_problem(NULL
, "bad srcType in extract float data");
2033 * Unpack a row of color image data from a client buffer according to
2034 * the pixel unpacking parameters. Apply any enabled pixel transfer
2035 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
2036 * Return GLubyte values in the specified dest image format.
2037 * This is (or will be) used by glDrawPixels and glTexImage?D().
2038 * Input: ctx - the context
2039 * n - number of pixels in the span
2040 * dstFormat - format of destination color array
2041 * dest - the destination color array
2042 * srcFormat - source image format
2043 * srcType - source image datatype
2044 * source - source image pointer
2045 * unpacking - pixel unpacking parameters
2046 * applyTransferOps - apply scale/bias/lookup-table ops?
2048 * XXX perhaps expand this to process whole images someday.
2051 _mesa_unpack_ubyte_color_span( const GLcontext
*ctx
,
2052 GLuint n
, GLenum dstFormat
, GLubyte dest
[],
2053 GLenum srcFormat
, GLenum srcType
,
2054 const GLvoid
*source
,
2055 const struct gl_pixelstore_attrib
*unpacking
,
2056 GLboolean applyTransferOps
)
2058 ASSERT(dstFormat
== GL_ALPHA
||
2059 dstFormat
== GL_LUMINANCE
||
2060 dstFormat
== GL_LUMINANCE_ALPHA
||
2061 dstFormat
== GL_INTENSITY
||
2062 dstFormat
== GL_RGB
||
2063 dstFormat
== GL_RGBA
||
2064 dstFormat
== GL_COLOR_INDEX
);
2066 ASSERT(srcFormat
== GL_RED
||
2067 srcFormat
== GL_GREEN
||
2068 srcFormat
== GL_BLUE
||
2069 srcFormat
== GL_ALPHA
||
2070 srcFormat
== GL_LUMINANCE
||
2071 srcFormat
== GL_LUMINANCE_ALPHA
||
2072 srcFormat
== GL_INTENSITY
||
2073 srcFormat
== GL_RGB
||
2074 srcFormat
== GL_BGR
||
2075 srcFormat
== GL_RGBA
||
2076 srcFormat
== GL_BGRA
||
2077 srcFormat
== GL_ABGR_EXT
||
2078 srcFormat
== GL_COLOR_INDEX
);
2080 ASSERT(srcType
== GL_BITMAP
||
2081 srcType
== GL_UNSIGNED_BYTE
||
2082 srcType
== GL_BYTE
||
2083 srcType
== GL_UNSIGNED_SHORT
||
2084 srcType
== GL_SHORT
||
2085 srcType
== GL_UNSIGNED_INT
||
2086 srcType
== GL_INT
||
2087 srcType
== GL_FLOAT
||
2088 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2089 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2090 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2091 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2092 srcType
== GL_UNSIGNED_INT_10_10_10_2
);
2094 /* this is intended for RGBA mode */
2095 assert(ctx
->Visual
->RGBAflag
);
2097 applyTransferOps
&= (ctx
->Pixel
.ScaleOrBiasRGBA
||
2098 ctx
->Pixel
.MapColorFlag
||
2099 ctx
->Pixel
.MapColorFlag
);
2101 /* Try simple cases first */
2102 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
) {
2103 if (dstFormat
== GL_RGBA
) {
2104 if (srcFormat
== GL_RGBA
) {
2105 MEMCPY( dest
, source
, n
* 4 * sizeof(GLubyte
) );
2108 else if (srcFormat
== GL_RGB
) {
2110 const GLubyte
*src
= (const GLubyte
*) source
;
2111 GLubyte
*dst
= dest
;
2112 for (i
= 0; i
< n
; i
++) {
2123 else if (dstFormat
== GL_RGB
) {
2124 if (srcFormat
== GL_RGB
) {
2125 MEMCPY( dest
, source
, n
* 3 * sizeof(GLubyte
) );
2128 else if (srcFormat
== GL_RGBA
) {
2130 const GLubyte
*src
= (const GLubyte
*) source
;
2131 GLubyte
*dst
= dest
;
2132 for (i
= 0; i
< n
; i
++) {
2142 else if (dstFormat
== srcFormat
) {
2143 GLint comps
= gl_components_in_format(srcFormat
);
2145 MEMCPY( dest
, source
, n
* comps
* sizeof(GLubyte
) );
2152 /* general solution */
2153 GLfloat rgba
[MAX_WIDTH
][4];
2154 GLint dstComponents
;
2155 GLint dstRedIndex
, dstGreenIndex
, dstBlueIndex
, dstAlphaIndex
;
2156 GLint dstLuminanceIndex
, dstIntensityIndex
;
2158 dstComponents
= gl_components_in_format( dstFormat
);
2159 /* source & dest image formats should have been error checked by now */
2160 assert(dstComponents
> 0);
2163 * Extract image data and convert to RGBA floats
2165 assert(n
<= MAX_WIDTH
);
2166 if (srcFormat
== GL_COLOR_INDEX
) {
2167 GLuint indexes
[MAX_WIDTH
];
2168 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
2171 /* shift and offset indexes */
2172 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2174 if (dstFormat
== GL_COLOR_INDEX
) {
2175 if (applyTransferOps
) {
2176 if (ctx
->Pixel
.MapColorFlag
) {
2177 /* Apply lookup table */
2178 gl_map_ci(ctx
, n
, indexes
);
2181 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2186 /* convert to GLubyte and return */
2189 for (i
= 0; i
< n
; i
++) {
2190 dest
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2195 /* Convert indexes to RGBA */
2196 gl_map_ci_to_rgba_float(ctx
, n
, indexes
, rgba
);
2200 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
2201 unpacking
->SwapBytes
);
2203 if (applyTransferOps
) {
2204 /* scale and bias colors */
2205 gl_scale_and_bias_rgba_float(ctx
, n
, rgba
);
2207 /* color table lookup */
2208 if (ctx
->Pixel
.MapColorFlag
) {
2209 gl_map_rgba_float(ctx
, n
, rgba
);
2216 * XXX This is where more color table lookups, convolution,
2217 * histograms, minmax, color matrix, etc would take place if
2219 * See figure 3.7 in the OpenGL 1.2 specification for more info.
2223 /* clamp to [0,1] */
2226 for (i
= 0; i
< n
; i
++) {
2227 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
2228 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
2229 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
2230 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
2234 /* Now determine which color channels we need to produce.
2235 * And determine the dest index (offset) within each color tuple.
2237 switch (dstFormat
) {
2240 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= -1;
2241 dstLuminanceIndex
= dstIntensityIndex
= -1;
2244 dstLuminanceIndex
= 0;
2245 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= dstAlphaIndex
= -1;
2246 dstIntensityIndex
= -1;
2248 case GL_LUMINANCE_ALPHA
:
2249 dstLuminanceIndex
= 0;
2251 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= -1;
2252 dstIntensityIndex
= -1;
2255 dstIntensityIndex
= 0;
2256 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= dstAlphaIndex
= -1;
2257 dstLuminanceIndex
= -1;
2263 dstAlphaIndex
= dstLuminanceIndex
= dstIntensityIndex
= -1;
2270 dstLuminanceIndex
= dstIntensityIndex
= -1;
2273 gl_problem(ctx
, "bad dstFormat in _mesa_unpack_ubyte_span()");
2278 /* Now return the GLubyte data in the requested dstFormat */
2280 if (dstRedIndex
>= 0) {
2281 GLubyte
*dst
= dest
;
2283 for (i
= 0; i
< n
; i
++) {
2284 dst
[dstRedIndex
] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2285 dst
+= dstComponents
;
2289 if (dstGreenIndex
>= 0) {
2290 GLubyte
*dst
= dest
;
2292 for (i
= 0; i
< n
; i
++) {
2293 dst
[dstGreenIndex
] = FLOAT_TO_UBYTE(rgba
[i
][GCOMP
]);
2294 dst
+= dstComponents
;
2298 if (dstBlueIndex
>= 0) {
2299 GLubyte
*dst
= dest
;
2301 for (i
= 0; i
< n
; i
++) {
2302 dst
[dstBlueIndex
] = FLOAT_TO_UBYTE(rgba
[i
][BCOMP
]);
2303 dst
+= dstComponents
;
2307 if (dstAlphaIndex
>= 0) {
2308 GLubyte
*dst
= dest
;
2310 for (i
= 0; i
< n
; i
++) {
2311 dst
[dstAlphaIndex
] = FLOAT_TO_UBYTE(rgba
[i
][ACOMP
]);
2312 dst
+= dstComponents
;
2316 if (dstIntensityIndex
>= 0) {
2317 GLubyte
*dst
= dest
;
2319 assert(dstIntensityIndex
== 0);
2320 assert(dstComponents
== 1);
2321 for (i
= 0; i
< n
; i
++) {
2322 /* Intensity comes from red channel */
2323 dst
[i
] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2327 if (dstLuminanceIndex
>= 0) {
2328 GLubyte
*dst
= dest
;
2330 assert(dstLuminanceIndex
== 0);
2331 for (i
= 0; i
< n
; i
++) {
2332 /* Luminance comes from red channel */
2333 dst
[0] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2334 dst
+= dstComponents
;
2343 * Unpack a row of color index data from a client buffer according to
2344 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2345 * and applyTransferOps is true.
2346 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2348 * Args: ctx - the context
2349 * n - number of pixels
2350 * dstType - destination datatype
2351 * dest - destination array
2352 * srcType - source pixel type
2353 * source - source data pointer
2354 * unpacking - pixel unpacking parameters
2355 * applyTransferOps - apply offset/bias/lookup ops?
2358 _mesa_unpack_index_span( const GLcontext
*ctx
, GLuint n
,
2359 GLenum dstType
, GLvoid
*dest
,
2360 GLenum srcType
, const GLvoid
*source
,
2361 const struct gl_pixelstore_attrib
*unpacking
,
2362 GLboolean applyTransferOps
)
2364 ASSERT(srcType
== GL_BITMAP
||
2365 srcType
== GL_UNSIGNED_BYTE
||
2366 srcType
== GL_BYTE
||
2367 srcType
== GL_UNSIGNED_SHORT
||
2368 srcType
== GL_SHORT
||
2369 srcType
== GL_UNSIGNED_INT
||
2370 srcType
== GL_INT
||
2371 srcType
== GL_FLOAT
);
2373 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
2374 dstType
== GL_UNSIGNED_SHORT
||
2375 dstType
== GL_UNSIGNED_INT
);
2377 applyTransferOps
&= (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapColorFlag
);
2380 * Try simple cases first
2382 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
2383 && dstType
== GL_UNSIGNED_BYTE
) {
2384 MEMCPY(dest
, source
, n
* sizeof(GLubyte
));
2386 else if (!applyTransferOps
&& srcType
== GL_UNSIGNED_INT
2387 && dstType
== GL_UNSIGNED_INT
&& !unpacking
->SwapBytes
) {
2388 MEMCPY(dest
, source
, n
* sizeof(GLuint
));
2394 GLuint indexes
[MAX_WIDTH
];
2395 assert(n
<= MAX_WIDTH
);
2397 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
2400 if (applyTransferOps
) {
2401 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2402 /* shift and offset indexes */
2403 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2406 if (ctx
->Pixel
.MapColorFlag
) {
2407 /* Apply lookup table */
2408 gl_map_ci(ctx
, n
, indexes
);
2412 /* convert to dest type */
2414 case GL_UNSIGNED_BYTE
:
2416 GLubyte
*dst
= (GLubyte
*) dest
;
2418 for (i
= 0; i
< n
; i
++) {
2419 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2423 case GL_UNSIGNED_SHORT
:
2425 GLuint
*dst
= (GLuint
*) dest
;
2427 for (i
= 0; i
< n
; i
++) {
2428 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
2432 case GL_UNSIGNED_INT
:
2433 MEMCPY(dest
, indexes
, n
* sizeof(GLuint
));
2436 gl_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
2443 * Unpack a row of stencil data from a client buffer according to
2444 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2445 * and applyTransferOps is true.
2446 * This is (or will be) used by glDrawPixels
2448 * Args: ctx - the context
2449 * n - number of pixels
2450 * dstType - destination datatype
2451 * dest - destination array
2452 * srcType - source pixel type
2453 * source - source data pointer
2454 * unpacking - pixel unpacking parameters
2455 * applyTransferOps - apply offset/bias/lookup ops?
2458 _mesa_unpack_stencil_span( const GLcontext
*ctx
, GLuint n
,
2459 GLenum dstType
, GLvoid
*dest
,
2460 GLenum srcType
, const GLvoid
*source
,
2461 const struct gl_pixelstore_attrib
*unpacking
,
2462 GLboolean applyTransferOps
)
2464 ASSERT(srcType
== GL_BITMAP
||
2465 srcType
== GL_UNSIGNED_BYTE
||
2466 srcType
== GL_BYTE
||
2467 srcType
== GL_UNSIGNED_SHORT
||
2468 srcType
== GL_SHORT
||
2469 srcType
== GL_UNSIGNED_INT
||
2470 srcType
== GL_INT
||
2471 srcType
== GL_FLOAT
);
2473 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
2474 dstType
== GL_UNSIGNED_SHORT
||
2475 dstType
== GL_UNSIGNED_INT
);
2477 applyTransferOps
&= (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapColorFlag
);
2480 * Try simple cases first
2482 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
2483 && dstType
== GL_UNSIGNED_BYTE
) {
2484 MEMCPY(dest
, source
, n
* sizeof(GLubyte
));
2486 else if (!applyTransferOps
&& srcType
== GL_UNSIGNED_INT
2487 && dstType
== GL_UNSIGNED_INT
&& !unpacking
->SwapBytes
) {
2488 MEMCPY(dest
, source
, n
* sizeof(GLuint
));
2494 GLuint indexes
[MAX_WIDTH
];
2495 assert(n
<= MAX_WIDTH
);
2497 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
2500 if (applyTransferOps
) {
2501 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2502 /* shift and offset indexes */
2503 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2506 if (ctx
->Pixel
.MapStencilFlag
) {
2507 /* Apply stencil lookup table */
2508 GLuint mask
= ctx
->Pixel
.MapStoSsize
- 1;
2511 indexes
[i
] = ctx
->Pixel
.MapStoS
[ indexes
[i
] & mask
];
2516 /* convert to dest type */
2518 case GL_UNSIGNED_BYTE
:
2520 GLubyte
*dst
= (GLubyte
*) dest
;
2522 for (i
= 0; i
< n
; i
++) {
2523 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2527 case GL_UNSIGNED_SHORT
:
2529 GLuint
*dst
= (GLuint
*) dest
;
2531 for (i
= 0; i
< n
; i
++) {
2532 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
2536 case GL_UNSIGNED_INT
:
2537 MEMCPY(dest
, indexes
, n
* sizeof(GLuint
));
2540 gl_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
2548 _mesa_unpack_depth_span( const GLcontext
*ctx
, GLuint n
, GLdepth
*dest
,
2549 GLenum srcType
, const GLvoid
*source
,
2550 const struct gl_pixelstore_attrib
*unpacking
,
2551 GLboolean applyTransferOps
)
2553 GLfloat
*depth
= MALLOC(n
* sizeof(GLfloat
));
2561 const GLubyte
*src
= (const GLubyte
*) source
;
2562 for (i
= 0; i
< n
; i
++) {
2563 depth
[i
] = BYTE_TO_FLOAT(src
[i
]);
2567 case GL_UNSIGNED_BYTE
:
2570 const GLubyte
*src
= (const GLubyte
*) source
;
2571 for (i
= 0; i
< n
; i
++) {
2572 depth
[i
] = UBYTE_TO_FLOAT(src
[i
]);
2579 const GLshort
*src
= (const GLshort
*) source
;
2580 for (i
= 0; i
< n
; i
++) {
2581 depth
[i
] = SHORT_TO_FLOAT(src
[i
]);
2585 case GL_UNSIGNED_SHORT
:
2588 const GLushort
*src
= (const GLushort
*) source
;
2589 for (i
= 0; i
< n
; i
++) {
2590 depth
[i
] = USHORT_TO_FLOAT(src
[i
]);
2597 const GLint
*src
= (const GLint
*) source
;
2598 for (i
= 0; i
< n
; i
++) {
2599 depth
[i
] = INT_TO_FLOAT(src
[i
]);
2603 case GL_UNSIGNED_INT
:
2606 const GLuint
*src
= (const GLuint
*) source
;
2607 for (i
= 0; i
< n
; i
++) {
2608 depth
[i
] = UINT_TO_FLOAT(src
[i
]);
2613 MEMCPY(depth
, source
, n
* sizeof(GLfloat
));
2616 gl_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
2621 /* apply depth scale and bias */
2622 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
2624 for (i
= 0; i
< n
; i
++) {
2625 depth
[i
] = depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
2629 /* clamp depth values to [0,1] and convert from floats to integers */
2632 for (i
= 0; i
< n
; i
++) {
2633 dest
[i
] = (GLdepth
) (CLAMP(depth
[i
], 0.0F
, 1.0F
) * DEPTH_SCALE
);
2643 * Unpack image data. Apply byteswapping, byte flipping (bitmap).
2644 * Return all image data in a contiguous block.
2647 _mesa_unpack_image( GLsizei width
, GLsizei height
, GLsizei depth
,
2648 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2649 const struct gl_pixelstore_attrib
*unpack
)
2651 GLint bytesPerRow
, compsPerRow
;
2652 GLboolean flipBytes
, swap2
, swap4
;
2655 return NULL
; /* not necessarily an error */
2657 if (width
<= 0 || height
<= 0 || depth
<= 0)
2658 return NULL
; /* generate error later */
2660 if (format
== GL_BITMAP
) {
2661 bytesPerRow
= (width
+ 7) >> 3;
2662 flipBytes
= !unpack
->LsbFirst
;
2663 swap2
= swap4
= GL_FALSE
;
2667 const GLint bytesPerPixel
= gl_bytes_per_pixel(format
, type
);
2668 const GLint components
= gl_components_in_format(format
);
2670 if (bytesPerPixel
<= 0 || components
<= 0)
2671 return NULL
; /* bad format or type. generate error later */
2672 bytesPerRow
= bytesPerPixel
* width
;
2673 bytesPerComp
= bytesPerPixel
/ components
;
2674 flipBytes
= GL_FALSE
;
2675 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
2676 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
2677 compsPerRow
= components
* width
;
2678 assert(compsPerRow
>= width
);
2682 GLubyte
*destBuffer
= MALLOC(bytesPerRow
* height
* depth
);
2686 return NULL
; /* generate GL_OUT_OF_MEMORY later */
2689 for (img
= 0; img
< depth
; img
++) {
2690 for (row
= 0; row
< height
; row
++) {
2691 const GLvoid
*src
= gl_pixel_addr_in_image(unpack
, pixels
,
2692 width
, height
, format
, type
, img
, row
, 0);
2693 MEMCPY(dst
, src
, bytesPerRow
);
2694 /* byte flipping/swapping */
2696 gl_flip_bytes((GLubyte
*) dst
, bytesPerRow
);
2699 gl_swap2((GLushort
*) dst
, compsPerRow
);
2702 gl_swap4((GLuint
*) dst
, compsPerRow
);
2713 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
2714 * order with row alignment = 1 byte.
2717 _mesa_unpack_bitmap( GLint width
, GLint height
, const GLubyte
*pixels
,
2718 const struct gl_pixelstore_attrib
*packing
)
2720 GLint bytes
, row
, width_in_bytes
;
2721 GLubyte
*buffer
, *dst
;
2726 /* Alloc dest storage */
2727 bytes
= ((width
+ 7) / 8 * height
);
2728 buffer
= (GLubyte
*) MALLOC( bytes
);
2733 width_in_bytes
= CEILING( width
, 8 );
2735 for (row
= 0; row
< height
; row
++) {
2736 GLubyte
*src
= gl_pixel_addr_in_image( packing
, pixels
, width
, height
,
2737 GL_COLOR_INDEX
, GL_BITMAP
,
2744 if (packing
->SkipPixels
== 0) {
2745 MEMCPY( dst
, src
, width_in_bytes
);
2746 if (packing
->LsbFirst
) {
2747 gl_flip_bytes( dst
, width_in_bytes
);
2751 /* handling SkipPixels is a bit tricky (no pun intended!) */
2753 if (packing
->LsbFirst
) {
2754 GLubyte srcMask
= 1 << (packing
->SkipPixels
& 0x7);
2755 GLubyte dstMask
= 128;
2759 for (i
= 0; i
< width
; i
++) {
2763 if (srcMask
== 128) {
2768 srcMask
= srcMask
<< 1;
2776 dstMask
= dstMask
>> 1;
2781 GLubyte srcMask
= 128 >> (packing
->SkipPixels
& 0x7);
2782 GLubyte dstMask
= 128;
2786 for (i
= 0; i
< width
; i
++) {
2795 srcMask
= srcMask
>> 1;
2803 dstMask
= dstMask
>> 1;
2808 dst
+= width_in_bytes
;