1 /* $Id: image.c,v 1.16 2000/01/13 00:32: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.
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 rComp
= gComp
= bComp
= aComp
= -1;
1589 switch (srcFormat
) {
1592 greenIndex
= blueIndex
= alphaIndex
= -1;
1597 redIndex
= blueIndex
= alphaIndex
= -1;
1602 redIndex
= greenIndex
= alphaIndex
= -1;
1606 redIndex
= greenIndex
= blueIndex
= -1;
1611 redIndex
= greenIndex
= blueIndex
= 0;
1615 case GL_LUMINANCE_ALPHA
:
1616 redIndex
= greenIndex
= blueIndex
= 0;
1622 greenIndex
= blueIndex
= alphaIndex
= -1;
1673 gl_problem(NULL
, "bad srcFormat in extract float data");
1678 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
1679 if ((INDEX) < 0) { \
1681 for (i = 0; i < n; i++) { \
1682 rgba[i][CHANNEL] = DEFAULT; \
1685 else if (swapBytes) { \
1686 const TYPE *s = (const TYPE *) src; \
1688 for (i = 0; i < n; i++) { \
1689 TYPE value = s[INDEX]; \
1690 if (sizeof(TYPE) == 2) { \
1693 else if (sizeof(TYPE) == 4) { \
1696 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
1701 const TYPE *s = (const TYPE *) src; \
1703 for (i = 0; i < n; i++) { \
1704 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
1710 case GL_UNSIGNED_BYTE
:
1711 PROCESS(redIndex
, RCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1712 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1713 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1714 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLubyte
, UBYTE_TO_FLOAT
);
1717 PROCESS(redIndex
, RCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1718 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1719 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLbyte
, BYTE_TO_FLOAT
);
1720 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLbyte
, BYTE_TO_FLOAT
);
1722 case GL_UNSIGNED_SHORT
:
1723 PROCESS(redIndex
, RCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1724 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1725 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLushort
, USHORT_TO_FLOAT
);
1726 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLushort
, USHORT_TO_FLOAT
);
1729 PROCESS(redIndex
, RCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1730 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1731 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLshort
, SHORT_TO_FLOAT
);
1732 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLshort
, SHORT_TO_FLOAT
);
1734 case GL_UNSIGNED_INT
:
1735 PROCESS(redIndex
, RCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1736 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1737 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLuint
, UINT_TO_FLOAT
);
1738 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLuint
, UINT_TO_FLOAT
);
1741 PROCESS(redIndex
, RCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1742 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1743 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLint
, INT_TO_FLOAT
);
1744 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLint
, INT_TO_FLOAT
);
1747 PROCESS(redIndex
, RCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1748 PROCESS(greenIndex
, GCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1749 PROCESS(blueIndex
, BCOMP
, 0.0F
, GLfloat
, (GLfloat
));
1750 PROCESS(alphaIndex
, ACOMP
, 1.0F
, GLfloat
, (GLfloat
));
1752 case GL_UNSIGNED_BYTE_3_3_2
:
1754 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1756 for (i
= 0; i
< n
; i
++) {
1757 GLubyte p
= ubsrc
[i
];
1758 rgba
[i
][RCOMP
] = ((p
>> 5) ) * (1.0F
/ 7.0F
);
1759 rgba
[i
][GCOMP
] = ((p
>> 2) & 0x7) * (1.0F
/ 7.0F
);
1760 rgba
[i
][BCOMP
] = ((p
) & 0x3) * (1.0F
/ 3.0F
);
1761 rgba
[i
][ACOMP
] = 1.0F
;
1765 case GL_UNSIGNED_BYTE_2_3_3_REV
:
1767 const GLubyte
*ubsrc
= (const GLubyte
*) src
;
1769 for (i
= 0; i
< n
; i
++) {
1770 GLubyte p
= ubsrc
[i
];
1771 rgba
[i
][RCOMP
] = ((p
) & 0x7) * (1.0F
/ 7.0F
);
1772 rgba
[i
][GCOMP
] = ((p
>> 3) & 0x7) * (1.0F
/ 7.0F
);
1773 rgba
[i
][BCOMP
] = ((p
>> 6) ) * (1.0F
/ 3.0F
);
1774 rgba
[i
][ACOMP
] = 1.0F
;
1778 case GL_UNSIGNED_SHORT_5_6_5
:
1780 const GLushort
*ussrc
= (const GLushort
*) src
;
1782 for (i
= 0; i
< n
; i
++) {
1783 GLushort p
= ussrc
[i
];
1785 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1786 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1787 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1788 rgba
[i
][ACOMP
] = 1.0F
;
1792 const GLushort
*ussrc
= (const GLushort
*) src
;
1794 for (i
= 0; i
< n
; i
++) {
1795 GLushort p
= ussrc
[i
];
1796 rgba
[i
][RCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1797 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1798 rgba
[i
][BCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1799 rgba
[i
][ACOMP
] = 1.0F
;
1803 case GL_UNSIGNED_SHORT_5_6_5_REV
:
1805 const GLushort
*ussrc
= (const GLushort
*) src
;
1807 for (i
= 0; i
< n
; i
++) {
1808 GLushort p
= ussrc
[i
];
1810 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1811 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1812 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1813 rgba
[i
][ACOMP
] = 1.0F
;
1817 const GLushort
*ussrc
= (const GLushort
*) src
;
1819 for (i
= 0; i
< n
; i
++) {
1820 GLushort p
= ussrc
[i
];
1821 rgba
[i
][RCOMP
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1822 rgba
[i
][GCOMP
] = ((p
>> 5) & 0x3f) * (1.0F
/ 63.0F
);
1823 rgba
[i
][BCOMP
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1824 rgba
[i
][ACOMP
] = 1.0F
;
1828 case GL_UNSIGNED_SHORT_4_4_4_4
:
1830 const GLushort
*ussrc
= (const GLushort
*) src
;
1832 for (i
= 0; i
< n
; i
++) {
1833 GLushort p
= ussrc
[i
];
1835 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1836 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1837 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1838 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1842 const GLushort
*ussrc
= (const GLushort
*) src
;
1844 for (i
= 0; i
< n
; i
++) {
1845 GLushort p
= ussrc
[i
];
1846 rgba
[i
][rComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1847 rgba
[i
][gComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1848 rgba
[i
][bComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1849 rgba
[i
][aComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1853 case GL_UNSIGNED_SHORT_4_4_4_4_REV
:
1855 const GLushort
*ussrc
= (const GLushort
*) src
;
1857 for (i
= 0; i
< n
; i
++) {
1858 GLushort p
= ussrc
[i
];
1860 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1861 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1862 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1863 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1867 const GLushort
*ussrc
= (const GLushort
*) src
;
1869 for (i
= 0; i
< n
; i
++) {
1870 GLushort p
= ussrc
[i
];
1871 rgba
[i
][rComp
] = ((p
) & 0xf) * (1.0F
/ 15.0F
);
1872 rgba
[i
][gComp
] = ((p
>> 4) & 0xf) * (1.0F
/ 15.0F
);
1873 rgba
[i
][bComp
] = ((p
>> 8) & 0xf) * (1.0F
/ 15.0F
);
1874 rgba
[i
][aComp
] = ((p
>> 12) ) * (1.0F
/ 15.0F
);
1878 case GL_UNSIGNED_SHORT_5_5_5_1
:
1880 const GLushort
*ussrc
= (const GLushort
*) src
;
1882 for (i
= 0; i
< n
; i
++) {
1883 GLushort p
= ussrc
[i
];
1885 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1886 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
1887 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
1888 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
1892 const GLushort
*ussrc
= (const GLushort
*) src
;
1894 for (i
= 0; i
< n
; i
++) {
1895 GLushort p
= ussrc
[i
];
1896 rgba
[i
][rComp
] = ((p
>> 11) ) * (1.0F
/ 31.0F
);
1897 rgba
[i
][gComp
] = ((p
>> 6) & 0x1f) * (1.0F
/ 31.0F
);
1898 rgba
[i
][bComp
] = ((p
>> 1) & 0x1f) * (1.0F
/ 31.0F
);
1899 rgba
[i
][aComp
] = ((p
) & 0x1) * (1.0F
/ 1.0F
);
1903 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
1905 const GLushort
*ussrc
= (const GLushort
*) src
;
1907 for (i
= 0; i
< n
; i
++) {
1908 GLushort p
= ussrc
[i
];
1910 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1911 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
1912 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
1913 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
1917 const GLushort
*ussrc
= (const GLushort
*) src
;
1919 for (i
= 0; i
< n
; i
++) {
1920 GLushort p
= ussrc
[i
];
1921 rgba
[i
][rComp
] = ((p
) & 0x1f) * (1.0F
/ 31.0F
);
1922 rgba
[i
][gComp
] = ((p
>> 5) & 0x1f) * (1.0F
/ 31.0F
);
1923 rgba
[i
][bComp
] = ((p
>> 10) & 0x1f) * (1.0F
/ 31.0F
);
1924 rgba
[i
][aComp
] = ((p
>> 15) ) * (1.0F
/ 1.0F
);
1928 case GL_UNSIGNED_INT_8_8_8_8
:
1930 const GLuint
*uisrc
= (const GLuint
*) src
;
1932 for (i
= 0; i
< n
; i
++) {
1933 GLuint p
= uisrc
[i
];
1934 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1935 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1936 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1937 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1941 const GLuint
*uisrc
= (const GLuint
*) src
;
1943 for (i
= 0; i
< n
; i
++) {
1944 GLuint p
= uisrc
[i
];
1945 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1946 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1947 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1948 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1952 case GL_UNSIGNED_INT_8_8_8_8_REV
:
1954 const GLuint
*uisrc
= (const GLuint
*) src
;
1956 for (i
= 0; i
< n
; i
++) {
1957 GLuint p
= uisrc
[i
];
1958 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1959 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1960 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1961 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1965 const GLuint
*uisrc
= (const GLuint
*) src
;
1967 for (i
= 0; i
< n
; i
++) {
1968 GLuint p
= uisrc
[i
];
1969 rgba
[i
][rComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
) & 0xff);
1970 rgba
[i
][gComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 8) & 0xff);
1971 rgba
[i
][bComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 16) & 0xff);
1972 rgba
[i
][aComp
] = UBYTE_COLOR_TO_FLOAT_COLOR((p
>> 24) );
1976 case GL_UNSIGNED_INT_10_10_10_2
:
1978 const GLuint
*uisrc
= (const GLuint
*) src
;
1980 for (i
= 0; i
< n
; i
++) {
1981 GLuint p
= uisrc
[i
];
1983 rgba
[i
][rComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
1984 rgba
[i
][gComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
1985 rgba
[i
][bComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
1986 rgba
[i
][aComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
1990 const GLuint
*uisrc
= (const GLuint
*) src
;
1992 for (i
= 0; i
< n
; i
++) {
1993 GLuint p
= uisrc
[i
];
1994 rgba
[i
][rComp
] = ((p
) & 0x3 ) * (1.0F
/ 3.0F
);
1995 rgba
[i
][gComp
] = ((p
>> 2) & 0x3ff) * (1.0F
/ 1023.0F
);
1996 rgba
[i
][bComp
] = ((p
>> 12) & 0x3ff) * (1.0F
/ 1023.0F
);
1997 rgba
[i
][aComp
] = ((p
>> 22) ) * (1.0F
/ 1023.0F
);
2001 case GL_UNSIGNED_INT_2_10_10_10_REV
:
2003 const GLuint
*uisrc
= (const GLuint
*) src
;
2005 for (i
= 0; i
< n
; i
++) {
2006 GLuint p
= uisrc
[i
];
2008 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
2009 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
2010 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
2011 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
2015 const GLuint
*uisrc
= (const GLuint
*) src
;
2017 for (i
= 0; i
< n
; i
++) {
2018 GLuint p
= uisrc
[i
];
2019 rgba
[i
][rComp
] = ((p
) & 0x3ff) * (1.0F
/ 1023.0F
);
2020 rgba
[i
][gComp
] = ((p
>> 10) & 0x3ff) * (1.0F
/ 1023.0F
);
2021 rgba
[i
][bComp
] = ((p
>> 20) & 0x3ff) * (1.0F
/ 1023.0F
);
2022 rgba
[i
][aComp
] = ((p
>> 30) ) * (1.0F
/ 3.0F
);
2027 gl_problem(NULL
, "bad srcType in extract float data");
2035 * Unpack a row of color image data from a client buffer according to
2036 * the pixel unpacking parameters. Apply any enabled pixel transfer
2037 * ops (PixelMap, scale/bias) if the applyTransferOps flag is enabled.
2038 * Return GLubyte values in the specified dest image format.
2039 * This is (or will be) used by glDrawPixels and glTexImage?D().
2040 * Input: ctx - the context
2041 * n - number of pixels in the span
2042 * dstFormat - format of destination color array
2043 * dest - the destination color array
2044 * srcFormat - source image format
2045 * srcType - source image datatype
2046 * source - source image pointer
2047 * unpacking - pixel unpacking parameters
2048 * applyTransferOps - apply scale/bias/lookup-table ops?
2050 * XXX perhaps expand this to process whole images someday.
2053 _mesa_unpack_ubyte_color_span( const GLcontext
*ctx
,
2054 GLuint n
, GLenum dstFormat
, GLubyte dest
[],
2055 GLenum srcFormat
, GLenum srcType
,
2056 const GLvoid
*source
,
2057 const struct gl_pixelstore_attrib
*unpacking
,
2058 GLboolean applyTransferOps
)
2060 ASSERT(dstFormat
== GL_ALPHA
||
2061 dstFormat
== GL_LUMINANCE
||
2062 dstFormat
== GL_LUMINANCE_ALPHA
||
2063 dstFormat
== GL_INTENSITY
||
2064 dstFormat
== GL_RGB
||
2065 dstFormat
== GL_RGBA
||
2066 dstFormat
== GL_COLOR_INDEX
);
2068 ASSERT(srcFormat
== GL_RED
||
2069 srcFormat
== GL_GREEN
||
2070 srcFormat
== GL_BLUE
||
2071 srcFormat
== GL_ALPHA
||
2072 srcFormat
== GL_LUMINANCE
||
2073 srcFormat
== GL_LUMINANCE_ALPHA
||
2074 srcFormat
== GL_INTENSITY
||
2075 srcFormat
== GL_RGB
||
2076 srcFormat
== GL_BGR
||
2077 srcFormat
== GL_RGBA
||
2078 srcFormat
== GL_BGRA
||
2079 srcFormat
== GL_ABGR_EXT
||
2080 srcFormat
== GL_COLOR_INDEX
);
2082 ASSERT(srcType
== GL_BITMAP
||
2083 srcType
== GL_UNSIGNED_BYTE
||
2084 srcType
== GL_BYTE
||
2085 srcType
== GL_UNSIGNED_SHORT
||
2086 srcType
== GL_SHORT
||
2087 srcType
== GL_UNSIGNED_INT
||
2088 srcType
== GL_INT
||
2089 srcType
== GL_FLOAT
||
2090 srcType
== GL_UNSIGNED_BYTE_3_3_2
||
2091 srcType
== GL_UNSIGNED_SHORT_4_4_4_4
||
2092 srcType
== GL_UNSIGNED_SHORT_5_5_5_1
||
2093 srcType
== GL_UNSIGNED_INT_8_8_8_8
||
2094 srcType
== GL_UNSIGNED_INT_10_10_10_2
);
2096 /* this is intended for RGBA mode */
2097 assert(ctx
->Visual
->RGBAflag
);
2099 applyTransferOps
&= (ctx
->Pixel
.ScaleOrBiasRGBA
||
2100 ctx
->Pixel
.MapColorFlag
||
2101 ctx
->Pixel
.MapColorFlag
);
2103 /* Try simple cases first */
2104 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
) {
2105 if (dstFormat
== GL_RGBA
) {
2106 if (srcFormat
== GL_RGBA
) {
2107 MEMCPY( dest
, source
, n
* 4 * sizeof(GLubyte
) );
2110 else if (srcFormat
== GL_RGB
) {
2112 const GLubyte
*src
= (const GLubyte
*) source
;
2113 GLubyte
*dst
= dest
;
2114 for (i
= 0; i
< n
; i
++) {
2125 else if (dstFormat
== GL_RGB
) {
2126 if (srcFormat
== GL_RGB
) {
2127 MEMCPY( dest
, source
, n
* 3 * sizeof(GLubyte
) );
2130 else if (srcFormat
== GL_RGBA
) {
2132 const GLubyte
*src
= (const GLubyte
*) source
;
2133 GLubyte
*dst
= dest
;
2134 for (i
= 0; i
< n
; i
++) {
2144 else if (dstFormat
== srcFormat
) {
2145 GLint comps
= gl_components_in_format(srcFormat
);
2147 MEMCPY( dest
, source
, n
* comps
* sizeof(GLubyte
) );
2154 /* general solution */
2155 GLfloat rgba
[MAX_WIDTH
][4];
2156 GLint dstComponents
;
2157 GLint dstRedIndex
, dstGreenIndex
, dstBlueIndex
, dstAlphaIndex
;
2158 GLint dstLuminanceIndex
, dstIntensityIndex
;
2160 dstComponents
= gl_components_in_format( dstFormat
);
2161 /* source & dest image formats should have been error checked by now */
2162 assert(dstComponents
> 0);
2165 * Extract image data and convert to RGBA floats
2167 assert(n
<= MAX_WIDTH
);
2168 if (srcFormat
== GL_COLOR_INDEX
) {
2169 GLuint indexes
[MAX_WIDTH
];
2170 extract_uint_indexes(n
, indexes
, srcFormat
, srcType
, source
,
2173 /* shift and offset indexes */
2174 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2176 if (dstFormat
== GL_COLOR_INDEX
) {
2177 if (applyTransferOps
) {
2178 if (ctx
->Pixel
.MapColorFlag
) {
2179 /* Apply lookup table */
2180 gl_map_ci(ctx
, n
, indexes
);
2183 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2188 /* convert to GLubyte and return */
2191 for (i
= 0; i
< n
; i
++) {
2192 dest
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2197 /* Convert indexes to RGBA */
2198 gl_map_ci_to_rgba_float(ctx
, n
, indexes
, rgba
);
2202 extract_float_rgba(n
, rgba
, srcFormat
, srcType
, source
,
2203 unpacking
->SwapBytes
);
2205 if (applyTransferOps
) {
2206 /* scale and bias colors */
2207 gl_scale_and_bias_rgba_float(ctx
, n
, rgba
);
2209 /* color table lookup */
2210 if (ctx
->Pixel
.MapColorFlag
) {
2211 gl_map_rgba_float(ctx
, n
, rgba
);
2218 * XXX This is where more color table lookups, convolution,
2219 * histograms, minmax, color matrix, etc would take place if
2221 * See figure 3.7 in the OpenGL 1.2 specification for more info.
2225 /* clamp to [0,1] */
2228 for (i
= 0; i
< n
; i
++) {
2229 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
2230 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
2231 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
2232 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
2236 /* Now determine which color channels we need to produce.
2237 * And determine the dest index (offset) within each color tuple.
2239 switch (dstFormat
) {
2242 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= -1;
2243 dstLuminanceIndex
= dstIntensityIndex
= -1;
2246 dstLuminanceIndex
= 0;
2247 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= dstAlphaIndex
= -1;
2248 dstIntensityIndex
= -1;
2250 case GL_LUMINANCE_ALPHA
:
2251 dstLuminanceIndex
= 0;
2253 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= -1;
2254 dstIntensityIndex
= -1;
2257 dstIntensityIndex
= 0;
2258 dstRedIndex
= dstGreenIndex
= dstBlueIndex
= dstAlphaIndex
= -1;
2259 dstLuminanceIndex
= -1;
2265 dstAlphaIndex
= dstLuminanceIndex
= dstIntensityIndex
= -1;
2272 dstLuminanceIndex
= dstIntensityIndex
= -1;
2275 gl_problem(ctx
, "bad dstFormat in _mesa_unpack_ubyte_span()");
2280 /* Now return the GLubyte data in the requested dstFormat */
2282 if (dstRedIndex
>= 0) {
2283 GLubyte
*dst
= dest
;
2285 for (i
= 0; i
< n
; i
++) {
2286 dst
[dstRedIndex
] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2287 dst
+= dstComponents
;
2291 if (dstGreenIndex
>= 0) {
2292 GLubyte
*dst
= dest
;
2294 for (i
= 0; i
< n
; i
++) {
2295 dst
[dstGreenIndex
] = FLOAT_TO_UBYTE(rgba
[i
][GCOMP
]);
2296 dst
+= dstComponents
;
2300 if (dstBlueIndex
>= 0) {
2301 GLubyte
*dst
= dest
;
2303 for (i
= 0; i
< n
; i
++) {
2304 dst
[dstBlueIndex
] = FLOAT_TO_UBYTE(rgba
[i
][BCOMP
]);
2305 dst
+= dstComponents
;
2309 if (dstAlphaIndex
>= 0) {
2310 GLubyte
*dst
= dest
;
2312 for (i
= 0; i
< n
; i
++) {
2313 dst
[dstAlphaIndex
] = FLOAT_TO_UBYTE(rgba
[i
][ACOMP
]);
2314 dst
+= dstComponents
;
2318 if (dstIntensityIndex
>= 0) {
2319 GLubyte
*dst
= dest
;
2321 assert(dstIntensityIndex
== 0);
2322 assert(dstComponents
== 1);
2323 for (i
= 0; i
< n
; i
++) {
2324 /* Intensity comes from red channel */
2325 dst
[i
] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2329 if (dstLuminanceIndex
>= 0) {
2330 GLubyte
*dst
= dest
;
2332 assert(dstLuminanceIndex
== 0);
2333 for (i
= 0; i
< n
; i
++) {
2334 /* Luminance comes from red channel */
2335 dst
[0] = FLOAT_TO_UBYTE(rgba
[i
][RCOMP
]);
2336 dst
+= dstComponents
;
2345 * Unpack a row of color index data from a client buffer according to
2346 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2347 * and applyTransferOps is true.
2348 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
2350 * Args: ctx - the context
2351 * n - number of pixels
2352 * dstType - destination datatype
2353 * dest - destination array
2354 * srcType - source pixel type
2355 * source - source data pointer
2356 * unpacking - pixel unpacking parameters
2357 * applyTransferOps - apply offset/bias/lookup ops?
2360 _mesa_unpack_index_span( const GLcontext
*ctx
, GLuint n
,
2361 GLenum dstType
, GLvoid
*dest
,
2362 GLenum srcType
, const GLvoid
*source
,
2363 const struct gl_pixelstore_attrib
*unpacking
,
2364 GLboolean applyTransferOps
)
2366 ASSERT(srcType
== GL_BITMAP
||
2367 srcType
== GL_UNSIGNED_BYTE
||
2368 srcType
== GL_BYTE
||
2369 srcType
== GL_UNSIGNED_SHORT
||
2370 srcType
== GL_SHORT
||
2371 srcType
== GL_UNSIGNED_INT
||
2372 srcType
== GL_INT
||
2373 srcType
== GL_FLOAT
);
2375 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
2376 dstType
== GL_UNSIGNED_SHORT
||
2377 dstType
== GL_UNSIGNED_INT
);
2379 applyTransferOps
&= (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapColorFlag
);
2382 * Try simple cases first
2384 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
2385 && dstType
== GL_UNSIGNED_BYTE
) {
2386 MEMCPY(dest
, source
, n
* sizeof(GLubyte
));
2388 else if (!applyTransferOps
&& srcType
== GL_UNSIGNED_INT
2389 && dstType
== GL_UNSIGNED_INT
&& !unpacking
->SwapBytes
) {
2390 MEMCPY(dest
, source
, n
* sizeof(GLuint
));
2396 GLuint indexes
[MAX_WIDTH
];
2397 assert(n
<= MAX_WIDTH
);
2399 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
2402 if (applyTransferOps
) {
2403 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2404 /* shift and offset indexes */
2405 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2408 if (ctx
->Pixel
.MapColorFlag
) {
2409 /* Apply lookup table */
2410 gl_map_ci(ctx
, n
, indexes
);
2414 /* convert to dest type */
2416 case GL_UNSIGNED_BYTE
:
2418 GLubyte
*dst
= (GLubyte
*) dest
;
2420 for (i
= 0; i
< n
; i
++) {
2421 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2425 case GL_UNSIGNED_SHORT
:
2427 GLuint
*dst
= (GLuint
*) dest
;
2429 for (i
= 0; i
< n
; i
++) {
2430 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
2434 case GL_UNSIGNED_INT
:
2435 MEMCPY(dest
, indexes
, n
* sizeof(GLuint
));
2438 gl_problem(ctx
, "bad dstType in _mesa_unpack_index_span");
2445 * Unpack a row of stencil data from a client buffer according to
2446 * the pixel unpacking parameters. Apply pixel transfer ops if enabled
2447 * and applyTransferOps is true.
2448 * This is (or will be) used by glDrawPixels
2450 * Args: ctx - the context
2451 * n - number of pixels
2452 * dstType - destination datatype
2453 * dest - destination array
2454 * srcType - source pixel type
2455 * source - source data pointer
2456 * unpacking - pixel unpacking parameters
2457 * applyTransferOps - apply offset/bias/lookup ops?
2460 _mesa_unpack_stencil_span( const GLcontext
*ctx
, GLuint n
,
2461 GLenum dstType
, GLvoid
*dest
,
2462 GLenum srcType
, const GLvoid
*source
,
2463 const struct gl_pixelstore_attrib
*unpacking
,
2464 GLboolean applyTransferOps
)
2466 ASSERT(srcType
== GL_BITMAP
||
2467 srcType
== GL_UNSIGNED_BYTE
||
2468 srcType
== GL_BYTE
||
2469 srcType
== GL_UNSIGNED_SHORT
||
2470 srcType
== GL_SHORT
||
2471 srcType
== GL_UNSIGNED_INT
||
2472 srcType
== GL_INT
||
2473 srcType
== GL_FLOAT
);
2475 ASSERT(dstType
== GL_UNSIGNED_BYTE
||
2476 dstType
== GL_UNSIGNED_SHORT
||
2477 dstType
== GL_UNSIGNED_INT
);
2479 applyTransferOps
&= (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
|| ctx
->Pixel
.MapColorFlag
);
2482 * Try simple cases first
2484 if (!applyTransferOps
&& srcType
== GL_UNSIGNED_BYTE
2485 && dstType
== GL_UNSIGNED_BYTE
) {
2486 MEMCPY(dest
, source
, n
* sizeof(GLubyte
));
2488 else if (!applyTransferOps
&& srcType
== GL_UNSIGNED_INT
2489 && dstType
== GL_UNSIGNED_INT
&& !unpacking
->SwapBytes
) {
2490 MEMCPY(dest
, source
, n
* sizeof(GLuint
));
2496 GLuint indexes
[MAX_WIDTH
];
2497 assert(n
<= MAX_WIDTH
);
2499 extract_uint_indexes(n
, indexes
, GL_COLOR_INDEX
, srcType
, source
,
2502 if (applyTransferOps
) {
2503 if (ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
) {
2504 /* shift and offset indexes */
2505 gl_shift_and_offset_ci(ctx
, n
, indexes
);
2508 if (ctx
->Pixel
.MapStencilFlag
) {
2509 /* Apply stencil lookup table */
2510 GLuint mask
= ctx
->Pixel
.MapStoSsize
- 1;
2513 indexes
[i
] = ctx
->Pixel
.MapStoS
[ indexes
[i
] & mask
];
2518 /* convert to dest type */
2520 case GL_UNSIGNED_BYTE
:
2522 GLubyte
*dst
= (GLubyte
*) dest
;
2524 for (i
= 0; i
< n
; i
++) {
2525 dst
[i
] = (GLubyte
) (indexes
[i
] & 0xff);
2529 case GL_UNSIGNED_SHORT
:
2531 GLuint
*dst
= (GLuint
*) dest
;
2533 for (i
= 0; i
< n
; i
++) {
2534 dst
[i
] = (GLushort
) (indexes
[i
] & 0xffff);
2538 case GL_UNSIGNED_INT
:
2539 MEMCPY(dest
, indexes
, n
* sizeof(GLuint
));
2542 gl_problem(ctx
, "bad dstType in _mesa_unpack_stencil_span");
2550 _mesa_unpack_depth_span( const GLcontext
*ctx
, GLuint n
, GLdepth
*dest
,
2551 GLenum srcType
, const GLvoid
*source
,
2552 const struct gl_pixelstore_attrib
*unpacking
,
2553 GLboolean applyTransferOps
)
2555 GLfloat
*depth
= MALLOC(n
* sizeof(GLfloat
));
2563 const GLubyte
*src
= (const GLubyte
*) source
;
2564 for (i
= 0; i
< n
; i
++) {
2565 depth
[i
] = BYTE_TO_FLOAT(src
[i
]);
2569 case GL_UNSIGNED_BYTE
:
2572 const GLubyte
*src
= (const GLubyte
*) source
;
2573 for (i
= 0; i
< n
; i
++) {
2574 depth
[i
] = UBYTE_TO_FLOAT(src
[i
]);
2581 const GLshort
*src
= (const GLshort
*) source
;
2582 for (i
= 0; i
< n
; i
++) {
2583 depth
[i
] = SHORT_TO_FLOAT(src
[i
]);
2587 case GL_UNSIGNED_SHORT
:
2590 const GLushort
*src
= (const GLushort
*) source
;
2591 for (i
= 0; i
< n
; i
++) {
2592 depth
[i
] = USHORT_TO_FLOAT(src
[i
]);
2599 const GLint
*src
= (const GLint
*) source
;
2600 for (i
= 0; i
< n
; i
++) {
2601 depth
[i
] = INT_TO_FLOAT(src
[i
]);
2605 case GL_UNSIGNED_INT
:
2608 const GLuint
*src
= (const GLuint
*) source
;
2609 for (i
= 0; i
< n
; i
++) {
2610 depth
[i
] = UINT_TO_FLOAT(src
[i
]);
2615 MEMCPY(depth
, source
, n
* sizeof(GLfloat
));
2618 gl_problem(NULL
, "bad type in _mesa_unpack_depth_span()");
2623 /* apply depth scale and bias */
2624 if (ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0) {
2626 for (i
= 0; i
< n
; i
++) {
2627 depth
[i
] = depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
2631 /* clamp depth values to [0,1] and convert from floats to integers */
2634 for (i
= 0; i
< n
; i
++) {
2635 dest
[i
] = (GLdepth
) (CLAMP(depth
[i
], 0.0F
, 1.0F
) * DEPTH_SCALE
);
2645 * Unpack image data. Apply byteswapping, byte flipping (bitmap).
2646 * Return all image data in a contiguous block.
2649 _mesa_unpack_image( GLsizei width
, GLsizei height
, GLsizei depth
,
2650 GLenum format
, GLenum type
, const GLvoid
*pixels
,
2651 const struct gl_pixelstore_attrib
*unpack
)
2653 GLint bytesPerRow
, compsPerRow
;
2654 GLboolean flipBytes
, swap2
, swap4
;
2657 return NULL
; /* not necessarily an error */
2659 if (width
<= 0 || height
<= 0 || depth
<= 0)
2660 return NULL
; /* generate error later */
2662 if (format
== GL_BITMAP
) {
2663 bytesPerRow
= (width
+ 7) >> 3;
2664 flipBytes
= !unpack
->LsbFirst
;
2665 swap2
= swap4
= GL_FALSE
;
2669 const GLint bytesPerPixel
= gl_bytes_per_pixel(format
, type
);
2670 const GLint components
= gl_components_in_format(format
);
2672 if (bytesPerPixel
<= 0 || components
<= 0)
2673 return NULL
; /* bad format or type. generate error later */
2674 bytesPerRow
= bytesPerPixel
* width
;
2675 bytesPerComp
= bytesPerPixel
/ components
;
2676 flipBytes
= GL_FALSE
;
2677 swap2
= (bytesPerComp
== 2) && unpack
->SwapBytes
;
2678 swap4
= (bytesPerComp
== 4) && unpack
->SwapBytes
;
2679 compsPerRow
= components
* width
;
2680 assert(compsPerRow
>= width
);
2684 GLubyte
*destBuffer
= MALLOC(bytesPerRow
* height
* depth
);
2688 return NULL
; /* generate GL_OUT_OF_MEMORY later */
2691 for (img
= 0; img
< depth
; img
++) {
2692 for (row
= 0; row
< height
; row
++) {
2693 const GLvoid
*src
= gl_pixel_addr_in_image(unpack
, pixels
,
2694 width
, height
, format
, type
, img
, row
, 0);
2695 MEMCPY(dst
, src
, bytesPerRow
);
2696 /* byte flipping/swapping */
2698 gl_flip_bytes((GLubyte
*) dst
, bytesPerRow
);
2701 gl_swap2((GLushort
*) dst
, compsPerRow
);
2704 gl_swap4((GLuint
*) dst
, compsPerRow
);
2715 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
2716 * order with row alignment = 1 byte.
2719 _mesa_unpack_bitmap( GLint width
, GLint height
, const GLubyte
*pixels
,
2720 const struct gl_pixelstore_attrib
*packing
)
2722 GLint bytes
, row
, width_in_bytes
;
2723 GLubyte
*buffer
, *dst
;
2728 /* Alloc dest storage */
2729 bytes
= ((width
+ 7) / 8 * height
);
2730 buffer
= (GLubyte
*) MALLOC( bytes
);
2735 width_in_bytes
= CEILING( width
, 8 );
2737 for (row
= 0; row
< height
; row
++) {
2738 GLubyte
*src
= gl_pixel_addr_in_image( packing
, pixels
, width
, height
,
2739 GL_COLOR_INDEX
, GL_BITMAP
,
2746 if (packing
->SkipPixels
== 0) {
2747 MEMCPY( dst
, src
, width_in_bytes
);
2748 if (packing
->LsbFirst
) {
2749 gl_flip_bytes( dst
, width_in_bytes
);
2753 /* handling SkipPixels is a bit tricky (no pun intended!) */
2755 if (packing
->LsbFirst
) {
2756 GLubyte srcMask
= 1 << (packing
->SkipPixels
& 0x7);
2757 GLubyte dstMask
= 128;
2761 for (i
= 0; i
< width
; i
++) {
2765 if (srcMask
== 128) {
2770 srcMask
= srcMask
<< 1;
2778 dstMask
= dstMask
>> 1;
2783 GLubyte srcMask
= 128 >> (packing
->SkipPixels
& 0x7);
2784 GLubyte dstMask
= 128;
2788 for (i
= 0; i
< width
; i
++) {
2797 srcMask
= srcMask
>> 1;
2805 dstMask
= dstMask
>> 1;
2810 dst
+= width_in_bytes
;