2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 * Normally, BYTE_TO_FLOAT(0) returns 0.00392 That causes problems when
45 * we later convert the float to a packed integer value (such as for
46 * GL_RGB5_A1) because we'll wind up with a non-zero value.
48 * We redefine the macros here so zero is handled correctly.
51 #define BYTE_TO_FLOAT(B) ((B) == 0 ? 0.0F : ((2.0F * (B) + 1.0F) * (1.0F/255.0F)))
54 #define SHORT_TO_FLOAT(S) ((S) == 0 ? 0.0F : ((2.0F * (S) + 1.0F) * (1.0F/65535.0F)))
58 /** Compute ceiling of integer quotient of A divided by B. */
59 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
63 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise.
66 _mesa_type_is_packed(GLenum type)
69 case GL_UNSIGNED_BYTE_3_3_2:
70 case GL_UNSIGNED_BYTE_2_3_3_REV:
71 case GL_UNSIGNED_SHORT_5_6_5:
72 case GL_UNSIGNED_SHORT_5_6_5_REV:
73 case GL_UNSIGNED_SHORT_4_4_4_4:
74 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
75 case GL_UNSIGNED_SHORT_5_5_5_1:
76 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
77 case GL_UNSIGNED_INT_8_8_8_8:
78 case GL_UNSIGNED_INT_8_8_8_8_REV:
79 case GL_UNSIGNED_INT_10_10_10_2:
80 case GL_UNSIGNED_INT_2_10_10_10_REV:
81 case GL_UNSIGNED_SHORT_8_8_MESA:
82 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
83 case GL_UNSIGNED_INT_24_8_EXT:
91 * Flip the 8 bits in each byte of the given array.
94 * \param n number of bytes.
96 * \todo try this trick to flip bytes someday:
98 * v = ((v & 0x55555555) << 1) | ((v >> 1) & 0x55555555);
99 * v = ((v & 0x33333333) << 2) | ((v >> 2) & 0x33333333);
100 * v = ((v & 0x0f0f0f0f) << 4) | ((v >> 4) & 0x0f0f0f0f);
104 flip_bytes( GLubyte *p, GLuint n )
107 for (i = 0; i < n; i++) {
108 b = (GLuint) p[i]; /* words are often faster than bytes */
109 a = ((b & 0x01) << 7) |
123 * Flip the order of the 2 bytes in each word in the given array.
126 * \param n number of words.
129 _mesa_swap2( GLushort *p, GLuint n )
132 for (i = 0; i < n; i++) {
133 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00);
140 * Flip the order of the 4 bytes in each word in the given array.
143 _mesa_swap4( GLuint *p, GLuint n )
146 for (i = 0; i < n; i++) {
149 | ((b >> 8) & 0xff00)
150 | ((b << 8) & 0xff0000)
151 | ((b << 24) & 0xff000000);
158 * Get the size of a GL data type.
160 * \param type GL data type.
162 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1
163 * if an invalid type enum.
166 _mesa_sizeof_type( GLenum type )
171 case GL_UNSIGNED_BYTE:
172 return sizeof(GLubyte);
174 return sizeof(GLbyte);
175 case GL_UNSIGNED_SHORT:
176 return sizeof(GLushort);
178 return sizeof(GLshort);
179 case GL_UNSIGNED_INT:
180 return sizeof(GLuint);
182 return sizeof(GLint);
184 return sizeof(GLfloat);
186 return sizeof(GLdouble);
187 case GL_HALF_FLOAT_ARB:
188 return sizeof(GLhalfARB);
196 * Same as _mesa_sizeof_type() but also accepting the packed pixel
200 _mesa_sizeof_packed_type( GLenum type )
205 case GL_UNSIGNED_BYTE:
206 return sizeof(GLubyte);
208 return sizeof(GLbyte);
209 case GL_UNSIGNED_SHORT:
210 return sizeof(GLushort);
212 return sizeof(GLshort);
213 case GL_UNSIGNED_INT:
214 return sizeof(GLuint);
216 return sizeof(GLint);
217 case GL_HALF_FLOAT_ARB:
218 return sizeof(GLhalfARB);
220 return sizeof(GLfloat);
221 case GL_UNSIGNED_BYTE_3_3_2:
222 return sizeof(GLubyte);
223 case GL_UNSIGNED_BYTE_2_3_3_REV:
224 return sizeof(GLubyte);
225 case GL_UNSIGNED_SHORT_5_6_5:
226 return sizeof(GLushort);
227 case GL_UNSIGNED_SHORT_5_6_5_REV:
228 return sizeof(GLushort);
229 case GL_UNSIGNED_SHORT_4_4_4_4:
230 return sizeof(GLushort);
231 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
232 return sizeof(GLushort);
233 case GL_UNSIGNED_SHORT_5_5_5_1:
234 return sizeof(GLushort);
235 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
236 return sizeof(GLushort);
237 case GL_UNSIGNED_INT_8_8_8_8:
238 return sizeof(GLuint);
239 case GL_UNSIGNED_INT_8_8_8_8_REV:
240 return sizeof(GLuint);
241 case GL_UNSIGNED_INT_10_10_10_2:
242 return sizeof(GLuint);
243 case GL_UNSIGNED_INT_2_10_10_10_REV:
244 return sizeof(GLuint);
245 case GL_UNSIGNED_SHORT_8_8_MESA:
246 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
247 return sizeof(GLushort);
248 case GL_UNSIGNED_INT_24_8_EXT:
249 return sizeof(GLuint);
257 * Get the number of components in a pixel format.
259 * \param format pixel format.
261 * \return the number of components in the given format, or -1 if a bad format.
264 _mesa_components_in_format( GLenum format )
268 case GL_COLOR_INDEX1_EXT:
269 case GL_COLOR_INDEX2_EXT:
270 case GL_COLOR_INDEX4_EXT:
271 case GL_COLOR_INDEX8_EXT:
272 case GL_COLOR_INDEX12_EXT:
273 case GL_COLOR_INDEX16_EXT:
274 case GL_STENCIL_INDEX:
275 case GL_DEPTH_COMPONENT:
283 case GL_LUMINANCE_ALPHA:
297 case GL_DEPTH_STENCIL_EXT:
309 * Get the bytes per pixel of pixel format type pair.
311 * \param format pixel format.
312 * \param type pixel type.
314 * \return bytes per pixel, or -1 if a bad format or type was given.
317 _mesa_bytes_per_pixel( GLenum format, GLenum type )
319 GLint comps = _mesa_components_in_format( format );
325 return 0; /* special case */
327 case GL_UNSIGNED_BYTE:
328 return comps * sizeof(GLubyte);
330 case GL_UNSIGNED_SHORT:
331 return comps * sizeof(GLshort);
333 case GL_UNSIGNED_INT:
334 return comps * sizeof(GLint);
336 return comps * sizeof(GLfloat);
337 case GL_HALF_FLOAT_ARB:
338 return comps * sizeof(GLhalfARB);
339 case GL_UNSIGNED_BYTE_3_3_2:
340 case GL_UNSIGNED_BYTE_2_3_3_REV:
341 if (format == GL_RGB || format == GL_BGR)
342 return sizeof(GLubyte);
344 return -1; /* error */
345 case GL_UNSIGNED_SHORT_5_6_5:
346 case GL_UNSIGNED_SHORT_5_6_5_REV:
347 if (format == GL_RGB || format == GL_BGR)
348 return sizeof(GLushort);
350 return -1; /* error */
351 case GL_UNSIGNED_SHORT_4_4_4_4:
352 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
353 case GL_UNSIGNED_SHORT_5_5_5_1:
354 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
355 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
356 return sizeof(GLushort);
359 case GL_UNSIGNED_INT_8_8_8_8:
360 case GL_UNSIGNED_INT_8_8_8_8_REV:
361 case GL_UNSIGNED_INT_10_10_10_2:
362 case GL_UNSIGNED_INT_2_10_10_10_REV:
363 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT)
364 return sizeof(GLuint);
367 case GL_UNSIGNED_SHORT_8_8_MESA:
368 case GL_UNSIGNED_SHORT_8_8_REV_MESA:
369 if (format == GL_YCBCR_MESA)
370 return sizeof(GLushort);
373 case GL_UNSIGNED_INT_24_8_EXT:
374 if (format == GL_DEPTH_STENCIL_EXT)
375 return sizeof(GLuint);
385 * Test for a legal pixel format and type.
387 * \param format pixel format.
388 * \param type pixel type.
390 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE
394 _mesa_is_legal_format_and_type( GLcontext *ctx, GLenum format, GLenum type )
398 case GL_STENCIL_INDEX:
402 case GL_UNSIGNED_BYTE:
404 case GL_UNSIGNED_SHORT:
406 case GL_UNSIGNED_INT:
409 case GL_HALF_FLOAT_ARB:
410 return ctx->Extensions.ARB_half_float_pixel;
418 #if 0 /* not legal! see table 3.6 of the 1.5 spec */
422 case GL_LUMINANCE_ALPHA:
423 case GL_DEPTH_COMPONENT:
426 case GL_UNSIGNED_BYTE:
428 case GL_UNSIGNED_SHORT:
430 case GL_UNSIGNED_INT:
433 case GL_HALF_FLOAT_ARB:
434 return ctx->Extensions.ARB_half_float_pixel;
441 case GL_UNSIGNED_BYTE:
443 case GL_UNSIGNED_SHORT:
445 case GL_UNSIGNED_INT:
447 case GL_UNSIGNED_BYTE_3_3_2:
448 case GL_UNSIGNED_BYTE_2_3_3_REV:
449 case GL_UNSIGNED_SHORT_5_6_5:
450 case GL_UNSIGNED_SHORT_5_6_5_REV:
452 case GL_HALF_FLOAT_ARB:
453 return ctx->Extensions.ARB_half_float_pixel;
459 /* NOTE: no packed types are supported with BGR. That's
460 * intentional, according to the GL spec.
463 case GL_UNSIGNED_BYTE:
465 case GL_UNSIGNED_SHORT:
467 case GL_UNSIGNED_INT:
470 case GL_HALF_FLOAT_ARB:
471 return ctx->Extensions.ARB_half_float_pixel;
480 case GL_UNSIGNED_BYTE:
482 case GL_UNSIGNED_SHORT:
484 case GL_UNSIGNED_INT:
486 case GL_UNSIGNED_SHORT_4_4_4_4:
487 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
488 case GL_UNSIGNED_SHORT_5_5_5_1:
489 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
490 case GL_UNSIGNED_INT_8_8_8_8:
491 case GL_UNSIGNED_INT_8_8_8_8_REV:
492 case GL_UNSIGNED_INT_10_10_10_2:
493 case GL_UNSIGNED_INT_2_10_10_10_REV:
495 case GL_HALF_FLOAT_ARB:
496 return ctx->Extensions.ARB_half_float_pixel;
501 if (type == GL_UNSIGNED_SHORT_8_8_MESA ||
502 type == GL_UNSIGNED_SHORT_8_8_REV_MESA)
506 case GL_DEPTH_STENCIL_EXT:
507 if (ctx->Extensions.EXT_packed_depth_stencil
508 && type == GL_UNSIGNED_INT_24_8_EXT)
516 case GL_UNSIGNED_BYTE:
518 case GL_UNSIGNED_SHORT:
520 case GL_UNSIGNED_INT:
534 * Test if the given image format is a color/RGBA format (i.e., not color
535 * index, depth, stencil, etc).
536 * \param format the image format value (may by an internal texture format)
537 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise.
540 _mesa_is_color_format(GLenum format)
558 case GL_LUMINANCE_ALPHA:
559 case GL_LUMINANCE4_ALPHA4:
560 case GL_LUMINANCE6_ALPHA2:
561 case GL_LUMINANCE8_ALPHA8:
562 case GL_LUMINANCE12_ALPHA4:
563 case GL_LUMINANCE12_ALPHA12:
564 case GL_LUMINANCE16_ALPHA16:
591 /* float texture formats */
592 case GL_ALPHA16F_ARB:
593 case GL_ALPHA32F_ARB:
594 case GL_LUMINANCE16F_ARB:
595 case GL_LUMINANCE32F_ARB:
596 case GL_LUMINANCE_ALPHA16F_ARB:
597 case GL_LUMINANCE_ALPHA32F_ARB:
598 case GL_INTENSITY16F_ARB:
599 case GL_INTENSITY32F_ARB:
604 /* compressed formats */
605 case GL_COMPRESSED_ALPHA:
606 case GL_COMPRESSED_LUMINANCE:
607 case GL_COMPRESSED_LUMINANCE_ALPHA:
608 case GL_COMPRESSED_INTENSITY:
609 case GL_COMPRESSED_RGB:
610 case GL_COMPRESSED_RGBA:
615 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
616 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
617 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
618 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
619 case GL_COMPRESSED_RGB_FXT1_3DFX:
620 case GL_COMPRESSED_RGBA_FXT1_3DFX:
621 #if FEATURE_EXT_texture_sRGB
624 case GL_SRGB_ALPHA_EXT:
625 case GL_SRGB8_ALPHA8_EXT:
626 case GL_SLUMINANCE_ALPHA_EXT:
627 case GL_SLUMINANCE8_ALPHA8_EXT:
628 case GL_SLUMINANCE_EXT:
629 case GL_SLUMINANCE8_EXT:
630 case GL_COMPRESSED_SRGB_EXT:
631 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
632 case GL_COMPRESSED_SRGB_ALPHA_EXT:
633 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
634 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
635 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
636 case GL_COMPRESSED_SLUMINANCE_EXT:
637 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
638 #endif /* FEATURE_EXT_texture_sRGB */
640 /* signed texture formats */
644 case GL_YCBCR_MESA: /* not considered to be RGB */
653 * Test if the given image format is a color index format.
656 _mesa_is_index_format(GLenum format)
660 case GL_COLOR_INDEX1_EXT:
661 case GL_COLOR_INDEX2_EXT:
662 case GL_COLOR_INDEX4_EXT:
663 case GL_COLOR_INDEX8_EXT:
664 case GL_COLOR_INDEX12_EXT:
665 case GL_COLOR_INDEX16_EXT:
674 * Test if the given image format is a depth component format.
677 _mesa_is_depth_format(GLenum format)
680 case GL_DEPTH_COMPONENT:
681 case GL_DEPTH_COMPONENT16:
682 case GL_DEPTH_COMPONENT24:
683 case GL_DEPTH_COMPONENT32:
692 * Test if the given image format is a YCbCr format.
695 _mesa_is_ycbcr_format(GLenum format)
707 * Test if the given image format is a depth+stencil format.
710 _mesa_is_depthstencil_format(GLenum format)
713 case GL_DEPTH24_STENCIL8_EXT:
714 case GL_DEPTH_STENCIL_EXT:
722 * Test if the given image format is a dudv format.
725 _mesa_is_dudv_format(GLenum format)
738 * Return the address of a specific pixel in an image (1D, 2D or 3D).
740 * Pixel unpacking/packing parameters are observed according to \p packing.
742 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
743 * \param image starting address of image data
744 * \param width the image width
745 * \param height theimage height
746 * \param format the pixel format
747 * \param type the pixel data type
748 * \param packing the pixelstore attributes
749 * \param img which image in the volume (0 for 1D or 2D images)
750 * \param row row of pixel in the image (0 for 1D images)
751 * \param column column of pixel in the image
753 * \return address of pixel on success, or NULL on error.
755 * \sa gl_pixelstore_attrib.
758 _mesa_image_address( GLuint dimensions,
759 const struct gl_pixelstore_attrib *packing,
761 GLsizei width, GLsizei height,
762 GLenum format, GLenum type,
763 GLint img, GLint row, GLint column )
765 GLint alignment; /* 1, 2 or 4 */
766 GLint pixels_per_row;
767 GLint rows_per_image;
770 GLint skipimages; /* for 3-D volume images */
773 ASSERT(dimensions >= 1 && dimensions <= 3);
775 alignment = packing->Alignment;
776 if (packing->RowLength > 0) {
777 pixels_per_row = packing->RowLength;
780 pixels_per_row = width;
782 if (packing->ImageHeight > 0) {
783 rows_per_image = packing->ImageHeight;
786 rows_per_image = height;
789 skippixels = packing->SkipPixels;
790 /* Note: SKIP_ROWS _is_ used for 1D images */
791 skiprows = packing->SkipRows;
792 /* Note: SKIP_IMAGES is only used for 3D images */
793 skipimages = (dimensions == 3) ? packing->SkipImages : 0;
795 if (type == GL_BITMAP) {
797 GLint comp_per_pixel; /* components per pixel */
798 GLint bytes_per_comp; /* bytes per component */
800 GLint bytes_per_image;
802 /* Compute bytes per component */
803 bytes_per_comp = _mesa_sizeof_packed_type( type );
804 if (bytes_per_comp < 0) {
808 /* Compute number of components per pixel */
809 comp_per_pixel = _mesa_components_in_format( format );
810 if (comp_per_pixel < 0) {
814 bytes_per_row = alignment
815 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment );
817 bytes_per_image = bytes_per_row * rows_per_image;
819 pixel_addr = (GLubyte *) image
820 + (skipimages + img) * bytes_per_image
821 + (skiprows + row) * bytes_per_row
822 + (skippixels + column) / 8;
825 /* Non-BITMAP data */
826 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image;
829 bytes_per_pixel = _mesa_bytes_per_pixel( format, type );
831 /* The pixel type and format should have been error checked earlier */
832 assert(bytes_per_pixel > 0);
834 bytes_per_row = pixels_per_row * bytes_per_pixel;
835 remainder = bytes_per_row % alignment;
837 bytes_per_row += (alignment - remainder);
839 ASSERT(bytes_per_row % alignment == 0);
841 bytes_per_image = bytes_per_row * rows_per_image;
843 if (packing->Invert) {
844 /* set pixel_addr to the last row */
845 topOfImage = bytes_per_row * (height - 1);
846 bytes_per_row = -bytes_per_row;
852 /* compute final pixel address */
853 pixel_addr = (GLubyte *) image
854 + (skipimages + img) * bytes_per_image
856 + (skiprows + row) * bytes_per_row
857 + (skippixels + column) * bytes_per_pixel;
860 return (GLvoid *) pixel_addr;
865 _mesa_image_address1d( const struct gl_pixelstore_attrib *packing,
868 GLenum format, GLenum type,
871 return _mesa_image_address(1, packing, image, width, 1,
872 format, type, 0, 0, column);
877 _mesa_image_address2d( const struct gl_pixelstore_attrib *packing,
879 GLsizei width, GLsizei height,
880 GLenum format, GLenum type,
881 GLint row, GLint column )
883 return _mesa_image_address(2, packing, image, width, height,
884 format, type, 0, row, column);
889 _mesa_image_address3d( const struct gl_pixelstore_attrib *packing,
891 GLsizei width, GLsizei height,
892 GLenum format, GLenum type,
893 GLint img, GLint row, GLint column )
895 return _mesa_image_address(3, packing, image, width, height,
896 format, type, img, row, column);
902 * Compute the stride (in bytes) between image rows.
904 * \param packing the pixelstore attributes
905 * \param width image width.
906 * \param format pixel format.
907 * \param type pixel data type.
909 * \return the stride in bytes for the given parameters, or -1 if error
912 _mesa_image_row_stride( const struct gl_pixelstore_attrib *packing,
913 GLint width, GLenum format, GLenum type )
915 GLint bytesPerRow, remainder;
919 if (type == GL_BITMAP) {
920 if (packing->RowLength == 0) {
921 bytesPerRow = (width + 7) / 8;
924 bytesPerRow = (packing->RowLength + 7) / 8;
928 /* Non-BITMAP data */
929 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
930 if (bytesPerPixel <= 0)
931 return -1; /* error */
932 if (packing->RowLength == 0) {
933 bytesPerRow = bytesPerPixel * width;
936 bytesPerRow = bytesPerPixel * packing->RowLength;
940 remainder = bytesPerRow % packing->Alignment;
942 bytesPerRow += (packing->Alignment - remainder);
945 if (packing->Invert) {
946 /* negate the bytes per row (negative row stride) */
947 bytesPerRow = -bytesPerRow;
957 * Compute the stride between images in a 3D texture (in bytes) for the given
958 * pixel packing parameters and image width, format and type.
961 _mesa_image_image_stride( const struct gl_pixelstore_attrib *packing,
962 GLint width, GLint height,
963 GLenum format, GLenum type )
965 GLint bytesPerRow, bytesPerImage, remainder;
969 if (type == GL_BITMAP) {
970 if (packing->RowLength == 0) {
971 bytesPerRow = (width + 7) / 8;
974 bytesPerRow = (packing->RowLength + 7) / 8;
978 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
980 if (bytesPerPixel <= 0)
981 return -1; /* error */
982 if (packing->RowLength == 0) {
983 bytesPerRow = bytesPerPixel * width;
986 bytesPerRow = bytesPerPixel * packing->RowLength;
990 remainder = bytesPerRow % packing->Alignment;
992 bytesPerRow += (packing->Alignment - remainder);
994 if (packing->ImageHeight == 0)
995 bytesPerImage = bytesPerRow * height;
997 bytesPerImage = bytesPerRow * packing->ImageHeight;
999 return bytesPerImage;
1004 * Unpack a 32x32 pixel polygon stipple from user memory using the
1005 * current pixel unpack settings.
1008 _mesa_unpack_polygon_stipple( const GLubyte *pattern, GLuint dest[32],
1009 const struct gl_pixelstore_attrib *unpacking )
1011 GLubyte *ptrn = (GLubyte *) _mesa_unpack_bitmap(32, 32, pattern, unpacking);
1013 /* Convert pattern from GLubytes to GLuints and handle big/little
1014 * endian differences
1018 for (i = 0; i < 32; i++) {
1019 dest[i] = (p[0] << 24)
1031 * Pack polygon stipple into user memory given current pixel packing
1035 _mesa_pack_polygon_stipple( const GLuint pattern[32], GLubyte *dest,
1036 const struct gl_pixelstore_attrib *packing )
1038 /* Convert pattern from GLuints to GLubytes to handle big/little
1039 * endian differences.
1043 for (i = 0; i < 32; i++) {
1044 ptrn[i * 4 + 0] = (GLubyte) ((pattern[i] >> 24) & 0xff);
1045 ptrn[i * 4 + 1] = (GLubyte) ((pattern[i] >> 16) & 0xff);
1046 ptrn[i * 4 + 2] = (GLubyte) ((pattern[i] >> 8 ) & 0xff);
1047 ptrn[i * 4 + 3] = (GLubyte) ((pattern[i] ) & 0xff);
1050 _mesa_pack_bitmap(32, 32, ptrn, dest, packing);
1055 * Unpack bitmap data. Resulting data will be in most-significant-bit-first
1056 * order with row alignment = 1 byte.
1059 _mesa_unpack_bitmap( GLint width, GLint height, const GLubyte *pixels,
1060 const struct gl_pixelstore_attrib *packing )
1062 GLint bytes, row, width_in_bytes;
1063 GLubyte *buffer, *dst;
1068 /* Alloc dest storage */
1069 bytes = ((width + 7) / 8 * height);
1070 buffer = (GLubyte *) _mesa_malloc( bytes );
1074 width_in_bytes = CEILING( width, 8 );
1076 for (row = 0; row < height; row++) {
1077 const GLubyte *src = (const GLubyte *)
1078 _mesa_image_address2d(packing, pixels, width, height,
1079 GL_COLOR_INDEX, GL_BITMAP, row, 0);
1085 if ((packing->SkipPixels & 7) == 0) {
1086 _mesa_memcpy( dst, src, width_in_bytes );
1087 if (packing->LsbFirst) {
1088 flip_bytes( dst, width_in_bytes );
1092 /* handling SkipPixels is a bit tricky (no pun intended!) */
1094 if (packing->LsbFirst) {
1095 GLubyte srcMask = 1 << (packing->SkipPixels & 0x7);
1096 GLubyte dstMask = 128;
1097 const GLubyte *s = src;
1100 for (i = 0; i < width; i++) {
1104 if (srcMask == 128) {
1109 srcMask = srcMask << 1;
1117 dstMask = dstMask >> 1;
1122 GLubyte srcMask = 128 >> (packing->SkipPixels & 0x7);
1123 GLubyte dstMask = 128;
1124 const GLubyte *s = src;
1127 for (i = 0; i < width; i++) {
1136 srcMask = srcMask >> 1;
1144 dstMask = dstMask >> 1;
1149 dst += width_in_bytes;
1160 _mesa_pack_bitmap( GLint width, GLint height, const GLubyte *source,
1161 GLubyte *dest, const struct gl_pixelstore_attrib *packing )
1163 GLint row, width_in_bytes;
1169 width_in_bytes = CEILING( width, 8 );
1171 for (row = 0; row < height; row++) {
1172 GLubyte *dst = (GLubyte *) _mesa_image_address2d(packing, dest,
1173 width, height, GL_COLOR_INDEX, GL_BITMAP, row, 0);
1177 if ((packing->SkipPixels & 7) == 0) {
1178 _mesa_memcpy( dst, src, width_in_bytes );
1179 if (packing->LsbFirst) {
1180 flip_bytes( dst, width_in_bytes );
1184 /* handling SkipPixels is a bit tricky (no pun intended!) */
1186 if (packing->LsbFirst) {
1187 GLubyte srcMask = 128;
1188 GLubyte dstMask = 1 << (packing->SkipPixels & 0x7);
1189 const GLubyte *s = src;
1192 for (i = 0; i < width; i++) {
1201 srcMask = srcMask >> 1;
1203 if (dstMask == 128) {
1209 dstMask = dstMask << 1;
1214 GLubyte srcMask = 128;
1215 GLubyte dstMask = 128 >> (packing->SkipPixels & 0x7);
1216 const GLubyte *s = src;
1219 for (i = 0; i < width; i++) {
1228 srcMask = srcMask >> 1;
1236 dstMask = dstMask >> 1;
1241 src += width_in_bytes;
1246 /**********************************************************************/
1247 /***** Pixel processing functions ******/
1248 /**********************************************************************/
1251 * Apply scale and bias factors to an array of RGBA pixels.
1254 _mesa_scale_and_bias_rgba(GLuint n, GLfloat rgba[][4],
1255 GLfloat rScale, GLfloat gScale,
1256 GLfloat bScale, GLfloat aScale,
1257 GLfloat rBias, GLfloat gBias,
1258 GLfloat bBias, GLfloat aBias)
1260 if (rScale != 1.0 || rBias != 0.0) {
1262 for (i = 0; i < n; i++) {
1263 rgba[i][RCOMP] = rgba[i][RCOMP] * rScale + rBias;
1266 if (gScale != 1.0 || gBias != 0.0) {
1268 for (i = 0; i < n; i++) {
1269 rgba[i][GCOMP] = rgba[i][GCOMP] * gScale + gBias;
1272 if (bScale != 1.0 || bBias != 0.0) {
1274 for (i = 0; i < n; i++) {
1275 rgba[i][BCOMP] = rgba[i][BCOMP] * bScale + bBias;
1278 if (aScale != 1.0 || aBias != 0.0) {
1280 for (i = 0; i < n; i++) {
1281 rgba[i][ACOMP] = rgba[i][ACOMP] * aScale + aBias;
1288 * Apply pixel mapping to an array of floating point RGBA pixels.
1291 _mesa_map_rgba( const GLcontext *ctx, GLuint n, GLfloat rgba[][4] )
1293 const GLfloat rscale = (GLfloat) (ctx->PixelMaps.RtoR.Size - 1);
1294 const GLfloat gscale = (GLfloat) (ctx->PixelMaps.GtoG.Size - 1);
1295 const GLfloat bscale = (GLfloat) (ctx->PixelMaps.BtoB.Size - 1);
1296 const GLfloat ascale = (GLfloat) (ctx->PixelMaps.AtoA.Size - 1);
1297 const GLfloat *rMap = ctx->PixelMaps.RtoR.Map;
1298 const GLfloat *gMap = ctx->PixelMaps.GtoG.Map;
1299 const GLfloat *bMap = ctx->PixelMaps.BtoB.Map;
1300 const GLfloat *aMap = ctx->PixelMaps.AtoA.Map;
1303 GLfloat r = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1304 GLfloat g = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1305 GLfloat b = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1306 GLfloat a = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1307 rgba[i][RCOMP] = rMap[IROUND(r * rscale)];
1308 rgba[i][GCOMP] = gMap[IROUND(g * gscale)];
1309 rgba[i][BCOMP] = bMap[IROUND(b * bscale)];
1310 rgba[i][ACOMP] = aMap[IROUND(a * ascale)];
1316 * Apply the color matrix and post color matrix scaling and biasing.
1319 _mesa_transform_rgba(const GLcontext *ctx, GLuint n, GLfloat rgba[][4])
1321 const GLfloat rs = ctx->Pixel.PostColorMatrixScale[0];
1322 const GLfloat rb = ctx->Pixel.PostColorMatrixBias[0];
1323 const GLfloat gs = ctx->Pixel.PostColorMatrixScale[1];
1324 const GLfloat gb = ctx->Pixel.PostColorMatrixBias[1];
1325 const GLfloat bs = ctx->Pixel.PostColorMatrixScale[2];
1326 const GLfloat bb = ctx->Pixel.PostColorMatrixBias[2];
1327 const GLfloat as = ctx->Pixel.PostColorMatrixScale[3];
1328 const GLfloat ab = ctx->Pixel.PostColorMatrixBias[3];
1329 const GLfloat *m = ctx->ColorMatrixStack.Top->m;
1331 for (i = 0; i < n; i++) {
1332 const GLfloat r = rgba[i][RCOMP];
1333 const GLfloat g = rgba[i][GCOMP];
1334 const GLfloat b = rgba[i][BCOMP];
1335 const GLfloat a = rgba[i][ACOMP];
1336 rgba[i][RCOMP] = (m[0] * r + m[4] * g + m[ 8] * b + m[12] * a) * rs + rb;
1337 rgba[i][GCOMP] = (m[1] * r + m[5] * g + m[ 9] * b + m[13] * a) * gs + gb;
1338 rgba[i][BCOMP] = (m[2] * r + m[6] * g + m[10] * b + m[14] * a) * bs + bb;
1339 rgba[i][ACOMP] = (m[3] * r + m[7] * g + m[11] * b + m[15] * a) * as + ab;
1345 * Apply a color table lookup to an array of floating point RGBA colors.
1348 _mesa_lookup_rgba_float(const struct gl_color_table *table,
1349 GLuint n, GLfloat rgba[][4])
1351 const GLint max = table->Size - 1;
1352 const GLfloat scale = (GLfloat) max;
1353 const GLfloat *lut = table->TableF;
1356 if (!table->TableF || table->Size == 0)
1359 switch (table->_BaseFormat) {
1361 /* replace RGBA with I */
1362 for (i = 0; i < n; i++) {
1363 GLint j = IROUND(rgba[i][RCOMP] * scale);
1364 GLfloat c = lut[CLAMP(j, 0, max)];
1372 /* replace RGB with L */
1373 for (i = 0; i < n; i++) {
1374 GLint j = IROUND(rgba[i][RCOMP] * scale);
1375 GLfloat c = lut[CLAMP(j, 0, max)];
1382 /* replace A with A */
1383 for (i = 0; i < n; i++) {
1384 GLint j = IROUND(rgba[i][ACOMP] * scale);
1385 rgba[i][ACOMP] = lut[CLAMP(j, 0, max)];
1388 case GL_LUMINANCE_ALPHA:
1389 /* replace RGBA with LLLA */
1390 for (i = 0; i < n; i++) {
1391 GLint jL = IROUND(rgba[i][RCOMP] * scale);
1392 GLint jA = IROUND(rgba[i][ACOMP] * scale);
1393 GLfloat luminance, alpha;
1394 jL = CLAMP(jL, 0, max);
1395 jA = CLAMP(jA, 0, max);
1396 luminance = lut[jL * 2 + 0];
1397 alpha = lut[jA * 2 + 1];
1400 rgba[i][BCOMP] = luminance;
1401 rgba[i][ACOMP] = alpha;;
1405 /* replace RGB with RGB */
1406 for (i = 0; i < n; i++) {
1407 GLint jR = IROUND(rgba[i][RCOMP] * scale);
1408 GLint jG = IROUND(rgba[i][GCOMP] * scale);
1409 GLint jB = IROUND(rgba[i][BCOMP] * scale);
1410 jR = CLAMP(jR, 0, max);
1411 jG = CLAMP(jG, 0, max);
1412 jB = CLAMP(jB, 0, max);
1413 rgba[i][RCOMP] = lut[jR * 3 + 0];
1414 rgba[i][GCOMP] = lut[jG * 3 + 1];
1415 rgba[i][BCOMP] = lut[jB * 3 + 2];
1419 /* replace RGBA with RGBA */
1420 for (i = 0; i < n; i++) {
1421 GLint jR = IROUND(rgba[i][RCOMP] * scale);
1422 GLint jG = IROUND(rgba[i][GCOMP] * scale);
1423 GLint jB = IROUND(rgba[i][BCOMP] * scale);
1424 GLint jA = IROUND(rgba[i][ACOMP] * scale);
1425 jR = CLAMP(jR, 0, max);
1426 jG = CLAMP(jG, 0, max);
1427 jB = CLAMP(jB, 0, max);
1428 jA = CLAMP(jA, 0, max);
1429 rgba[i][RCOMP] = lut[jR * 4 + 0];
1430 rgba[i][GCOMP] = lut[jG * 4 + 1];
1431 rgba[i][BCOMP] = lut[jB * 4 + 2];
1432 rgba[i][ACOMP] = lut[jA * 4 + 3];
1436 _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba_float");
1444 * Apply a color table lookup to an array of ubyte/RGBA colors.
1447 _mesa_lookup_rgba_ubyte(const struct gl_color_table *table,
1448 GLuint n, GLubyte rgba[][4])
1450 const GLubyte *lut = table->TableUB;
1451 const GLfloat scale = (GLfloat) (table->Size - 1) / (GLfloat)255.0;
1454 if (!table->TableUB || table->Size == 0)
1457 switch (table->_BaseFormat) {
1459 /* replace RGBA with I */
1460 if (table->Size == 256) {
1461 for (i = 0; i < n; i++) {
1462 const GLubyte c = lut[rgba[i][RCOMP]];
1470 for (i = 0; i < n; i++) {
1471 GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1475 rgba[i][ACOMP] = lut[j];
1480 /* replace RGB with L */
1481 if (table->Size == 256) {
1482 for (i = 0; i < n; i++) {
1483 const GLubyte c = lut[rgba[i][RCOMP]];
1490 for (i = 0; i < n; i++) {
1491 GLint j = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1494 rgba[i][BCOMP] = lut[j];
1499 /* replace A with A */
1500 if (table->Size == 256) {
1501 for (i = 0; i < n; i++) {
1502 rgba[i][ACOMP] = lut[rgba[i][ACOMP]];
1506 for (i = 0; i < n; i++) {
1507 GLint j = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1508 rgba[i][ACOMP] = lut[j];
1512 case GL_LUMINANCE_ALPHA:
1513 /* replace RGBA with LLLA */
1514 if (table->Size == 256) {
1515 for (i = 0; i < n; i++) {
1516 GLubyte l = lut[rgba[i][RCOMP] * 2 + 0];
1517 GLubyte a = lut[rgba[i][ACOMP] * 2 + 1];;
1525 for (i = 0; i < n; i++) {
1526 GLint jL = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1527 GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1528 GLubyte luminance = lut[jL * 2 + 0];
1529 GLubyte alpha = lut[jA * 2 + 1];
1532 rgba[i][BCOMP] = luminance;
1533 rgba[i][ACOMP] = alpha;
1538 if (table->Size == 256) {
1539 for (i = 0; i < n; i++) {
1540 rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 3 + 0];
1541 rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 3 + 1];
1542 rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 3 + 2];
1546 for (i = 0; i < n; i++) {
1547 GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1548 GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
1549 GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
1550 rgba[i][RCOMP] = lut[jR * 3 + 0];
1551 rgba[i][GCOMP] = lut[jG * 3 + 1];
1552 rgba[i][BCOMP] = lut[jB * 3 + 2];
1557 if (table->Size == 256) {
1558 for (i = 0; i < n; i++) {
1559 rgba[i][RCOMP] = lut[rgba[i][RCOMP] * 4 + 0];
1560 rgba[i][GCOMP] = lut[rgba[i][GCOMP] * 4 + 1];
1561 rgba[i][BCOMP] = lut[rgba[i][BCOMP] * 4 + 2];
1562 rgba[i][ACOMP] = lut[rgba[i][ACOMP] * 4 + 3];
1566 for (i = 0; i < n; i++) {
1567 GLint jR = IROUND((GLfloat) rgba[i][RCOMP] * scale);
1568 GLint jG = IROUND((GLfloat) rgba[i][GCOMP] * scale);
1569 GLint jB = IROUND((GLfloat) rgba[i][BCOMP] * scale);
1570 GLint jA = IROUND((GLfloat) rgba[i][ACOMP] * scale);
1571 CLAMPED_FLOAT_TO_CHAN(rgba[i][RCOMP], lut[jR * 4 + 0]);
1572 CLAMPED_FLOAT_TO_CHAN(rgba[i][GCOMP], lut[jG * 4 + 1]);
1573 CLAMPED_FLOAT_TO_CHAN(rgba[i][BCOMP], lut[jB * 4 + 2]);
1574 CLAMPED_FLOAT_TO_CHAN(rgba[i][ACOMP], lut[jA * 4 + 3]);
1579 _mesa_problem(NULL, "Bad format in _mesa_lookup_rgba_chan");
1587 * Map color indexes to float rgba values.
1590 _mesa_map_ci_to_rgba( const GLcontext *ctx, GLuint n,
1591 const GLuint index[], GLfloat rgba[][4] )
1593 GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1594 GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1595 GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1596 GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1597 const GLfloat *rMap = ctx->PixelMaps.ItoR.Map;
1598 const GLfloat *gMap = ctx->PixelMaps.ItoG.Map;
1599 const GLfloat *bMap = ctx->PixelMaps.ItoB.Map;
1600 const GLfloat *aMap = ctx->PixelMaps.ItoA.Map;
1603 rgba[i][RCOMP] = rMap[index[i] & rmask];
1604 rgba[i][GCOMP] = gMap[index[i] & gmask];
1605 rgba[i][BCOMP] = bMap[index[i] & bmask];
1606 rgba[i][ACOMP] = aMap[index[i] & amask];
1612 * Map ubyte color indexes to ubyte/RGBA values.
1615 _mesa_map_ci8_to_rgba8(const GLcontext *ctx, GLuint n, const GLubyte index[],
1618 GLuint rmask = ctx->PixelMaps.ItoR.Size - 1;
1619 GLuint gmask = ctx->PixelMaps.ItoG.Size - 1;
1620 GLuint bmask = ctx->PixelMaps.ItoB.Size - 1;
1621 GLuint amask = ctx->PixelMaps.ItoA.Size - 1;
1622 const GLubyte *rMap = ctx->PixelMaps.ItoR.Map8;
1623 const GLubyte *gMap = ctx->PixelMaps.ItoG.Map8;
1624 const GLubyte *bMap = ctx->PixelMaps.ItoB.Map8;
1625 const GLubyte *aMap = ctx->PixelMaps.ItoA.Map8;
1628 rgba[i][RCOMP] = rMap[index[i] & rmask];
1629 rgba[i][GCOMP] = gMap[index[i] & gmask];
1630 rgba[i][BCOMP] = bMap[index[i] & bmask];
1631 rgba[i][ACOMP] = aMap[index[i] & amask];
1637 _mesa_scale_and_bias_depth(const GLcontext *ctx, GLuint n,
1638 GLfloat depthValues[])
1640 const GLfloat scale = ctx->Pixel.DepthScale;
1641 const GLfloat bias = ctx->Pixel.DepthBias;
1643 for (i = 0; i < n; i++) {
1644 GLfloat d = depthValues[i] * scale + bias;
1645 depthValues[i] = CLAMP(d, 0.0F, 1.0F);
1651 _mesa_scale_and_bias_depth_uint(const GLcontext *ctx, GLuint n,
1652 GLuint depthValues[])
1654 const GLdouble max = (double) 0xffffffff;
1655 const GLdouble scale = ctx->Pixel.DepthScale;
1656 const GLdouble bias = ctx->Pixel.DepthBias * max;
1658 for (i = 0; i < n; i++) {
1659 GLdouble d = (GLdouble) depthValues[i] * scale + bias;
1660 d = CLAMP(d, 0.0, max);
1661 depthValues[i] = (GLuint) d;
1668 * Update the min/max values from an array of fragment colors.
1671 update_minmax(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
1674 for (i = 0; i < n; i++) {
1676 if (rgba[i][RCOMP] < ctx->MinMax.Min[RCOMP])
1677 ctx->MinMax.Min[RCOMP] = rgba[i][RCOMP];
1678 if (rgba[i][GCOMP] < ctx->MinMax.Min[GCOMP])
1679 ctx->MinMax.Min[GCOMP] = rgba[i][GCOMP];
1680 if (rgba[i][BCOMP] < ctx->MinMax.Min[BCOMP])
1681 ctx->MinMax.Min[BCOMP] = rgba[i][BCOMP];
1682 if (rgba[i][ACOMP] < ctx->MinMax.Min[ACOMP])
1683 ctx->MinMax.Min[ACOMP] = rgba[i][ACOMP];
1686 if (rgba[i][RCOMP] > ctx->MinMax.Max[RCOMP])
1687 ctx->MinMax.Max[RCOMP] = rgba[i][RCOMP];
1688 if (rgba[i][GCOMP] > ctx->MinMax.Max[GCOMP])
1689 ctx->MinMax.Max[GCOMP] = rgba[i][GCOMP];
1690 if (rgba[i][BCOMP] > ctx->MinMax.Max[BCOMP])
1691 ctx->MinMax.Max[BCOMP] = rgba[i][BCOMP];
1692 if (rgba[i][ACOMP] > ctx->MinMax.Max[ACOMP])
1693 ctx->MinMax.Max[ACOMP] = rgba[i][ACOMP];
1699 * Update the histogram values from an array of fragment colors.
1702 update_histogram(GLcontext *ctx, GLuint n, const GLfloat rgba[][4])
1704 const GLint max = ctx->Histogram.Width - 1;
1705 GLfloat w = (GLfloat) max;
1708 if (ctx->Histogram.Width == 0)
1711 for (i = 0; i < n; i++) {
1712 GLint ri = IROUND(rgba[i][RCOMP] * w);
1713 GLint gi = IROUND(rgba[i][GCOMP] * w);
1714 GLint bi = IROUND(rgba[i][BCOMP] * w);
1715 GLint ai = IROUND(rgba[i][ACOMP] * w);
1716 ri = CLAMP(ri, 0, max);
1717 gi = CLAMP(gi, 0, max);
1718 bi = CLAMP(bi, 0, max);
1719 ai = CLAMP(ai, 0, max);
1720 ctx->Histogram.Count[ri][RCOMP]++;
1721 ctx->Histogram.Count[gi][GCOMP]++;
1722 ctx->Histogram.Count[bi][BCOMP]++;
1723 ctx->Histogram.Count[ai][ACOMP]++;
1729 * Apply various pixel transfer operations to an array of RGBA pixels
1730 * as indicated by the transferOps bitmask
1733 _mesa_apply_rgba_transfer_ops(GLcontext *ctx, GLbitfield transferOps,
1734 GLuint n, GLfloat rgba[][4])
1737 if (transferOps & IMAGE_SCALE_BIAS_BIT) {
1738 _mesa_scale_and_bias_rgba(n, rgba,
1739 ctx->Pixel.RedScale, ctx->Pixel.GreenScale,
1740 ctx->Pixel.BlueScale, ctx->Pixel.AlphaScale,
1741 ctx->Pixel.RedBias, ctx->Pixel.GreenBias,
1742 ctx->Pixel.BlueBias, ctx->Pixel.AlphaBias);
1744 /* color map lookup */
1745 if (transferOps & IMAGE_MAP_COLOR_BIT) {
1746 _mesa_map_rgba( ctx, n, rgba );
1748 /* GL_COLOR_TABLE lookup */
1749 if (transferOps & IMAGE_COLOR_TABLE_BIT) {
1750 _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_PRECONVOLUTION], n, rgba);
1753 if (transferOps & IMAGE_CONVOLUTION_BIT) {
1754 /* this has to be done in the calling code */
1755 _mesa_problem(ctx, "IMAGE_CONVOLUTION_BIT set in _mesa_apply_transfer_ops");
1757 /* GL_POST_CONVOLUTION_RED/GREEN/BLUE/ALPHA_SCALE/BIAS */
1758 if (transferOps & IMAGE_POST_CONVOLUTION_SCALE_BIAS) {
1759 _mesa_scale_and_bias_rgba(n, rgba,
1760 ctx->Pixel.PostConvolutionScale[RCOMP],
1761 ctx->Pixel.PostConvolutionScale[GCOMP],
1762 ctx->Pixel.PostConvolutionScale[BCOMP],
1763 ctx->Pixel.PostConvolutionScale[ACOMP],
1764 ctx->Pixel.PostConvolutionBias[RCOMP],
1765 ctx->Pixel.PostConvolutionBias[GCOMP],
1766 ctx->Pixel.PostConvolutionBias[BCOMP],
1767 ctx->Pixel.PostConvolutionBias[ACOMP]);
1769 /* GL_POST_CONVOLUTION_COLOR_TABLE lookup */
1770 if (transferOps & IMAGE_POST_CONVOLUTION_COLOR_TABLE_BIT) {
1771 _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_POSTCONVOLUTION], n, rgba);
1773 /* color matrix transform */
1774 if (transferOps & IMAGE_COLOR_MATRIX_BIT) {
1775 _mesa_transform_rgba(ctx, n, rgba);
1777 /* GL_POST_COLOR_MATRIX_COLOR_TABLE lookup */
1778 if (transferOps & IMAGE_POST_COLOR_MATRIX_COLOR_TABLE_BIT) {
1779 _mesa_lookup_rgba_float(&ctx->ColorTable[COLORTABLE_POSTCOLORMATRIX], n, rgba);
1781 /* update histogram count */
1782 if (transferOps & IMAGE_HISTOGRAM_BIT) {
1783 update_histogram(ctx, n, (CONST GLfloat (*)[4]) rgba);
1785 /* update min/max values */
1786 if (transferOps & IMAGE_MIN_MAX_BIT) {
1787 update_minmax(ctx, n, (CONST GLfloat (*)[4]) rgba);
1789 /* clamping to [0,1] */
1790 if (transferOps & IMAGE_CLAMP_BIT) {
1792 for (i = 0; i < n; i++) {
1793 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F);
1794 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F);
1795 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F);
1796 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F);
1803 * Apply color index shift and offset to an array of pixels.
1806 shift_and_offset_ci( const GLcontext *ctx, GLuint n, GLuint indexes[] )
1808 GLint shift = ctx->Pixel.IndexShift;
1809 GLint offset = ctx->Pixel.IndexOffset;
1813 indexes[i] = (indexes[i] << shift) + offset;
1816 else if (shift < 0) {
1819 indexes[i] = (indexes[i] >> shift) + offset;
1824 indexes[i] = indexes[i] + offset;
1832 * Apply color index shift, offset and table lookup to an array
1836 _mesa_apply_ci_transfer_ops(const GLcontext *ctx, GLbitfield transferOps,
1837 GLuint n, GLuint indexes[])
1839 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
1840 shift_and_offset_ci(ctx, n, indexes);
1842 if (transferOps & IMAGE_MAP_COLOR_BIT) {
1843 const GLuint mask = ctx->PixelMaps.ItoI.Size - 1;
1845 for (i = 0; i < n; i++) {
1846 const GLuint j = indexes[i] & mask;
1847 indexes[i] = IROUND(ctx->PixelMaps.ItoI.Map[j]);
1854 * Apply stencil index shift, offset and table lookup to an array
1855 * of stencil values.
1858 _mesa_apply_stencil_transfer_ops(const GLcontext *ctx, GLuint n,
1859 GLstencil stencil[])
1861 if (ctx->Pixel.IndexShift != 0 || ctx->Pixel.IndexOffset != 0) {
1862 const GLint offset = ctx->Pixel.IndexOffset;
1863 GLint shift = ctx->Pixel.IndexShift;
1866 for (i = 0; i < n; i++) {
1867 stencil[i] = (stencil[i] << shift) + offset;
1870 else if (shift < 0) {
1872 for (i = 0; i < n; i++) {
1873 stencil[i] = (stencil[i] >> shift) + offset;
1877 for (i = 0; i < n; i++) {
1878 stencil[i] = stencil[i] + offset;
1882 if (ctx->Pixel.MapStencilFlag) {
1883 GLuint mask = ctx->PixelMaps.StoS.Size - 1;
1885 for (i = 0; i < n; i++) {
1886 stencil[i] = (GLstencil)ctx->PixelMaps.StoS.Map[ stencil[i] & mask ];
1893 * Used to pack an array [][4] of RGBA float colors as specified
1894 * by the dstFormat, dstType and dstPacking. Used by glReadPixels,
1895 * glGetConvolutionFilter(), etc.
1896 * Note: the rgba values will be modified by this function when any pixel
1897 * transfer ops are enabled.
1900 _mesa_pack_rgba_span_float(GLcontext *ctx, GLuint n, GLfloat rgba[][4],
1901 GLenum dstFormat, GLenum dstType,
1903 const struct gl_pixelstore_attrib *dstPacking,
1904 GLbitfield transferOps)
1906 GLfloat luminance[MAX_WIDTH];
1907 const GLint comps = _mesa_components_in_format(dstFormat);
1911 * This test should probably go away. Have the caller set/clear the
1912 * IMAGE_CLAMP_BIT as needed.
1914 if (dstType != GL_FLOAT || ctx->Color.ClampReadColor == GL_TRUE) {
1915 /* need to clamp to [0, 1] */
1916 transferOps |= IMAGE_CLAMP_BIT;
1920 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
1921 if ((transferOps & IMAGE_MIN_MAX_BIT) && ctx->MinMax.Sink) {
1926 if (dstFormat == GL_LUMINANCE || dstFormat == GL_LUMINANCE_ALPHA) {
1927 /* compute luminance values */
1928 if (transferOps & IMAGE_CLAMP_BIT) {
1929 for (i = 0; i < n; i++) {
1930 GLfloat sum = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1931 luminance[i] = CLAMP(sum, 0.0F, 1.0F);
1935 for (i = 0; i < n; i++) {
1936 luminance[i] = rgba[i][RCOMP] + rgba[i][GCOMP] + rgba[i][BCOMP];
1942 * Pack/store the pixels. Ugh! Lots of cases!!!
1945 case GL_UNSIGNED_BYTE:
1947 GLubyte *dst = (GLubyte *) dstAddr;
1948 switch (dstFormat) {
1951 dst[i] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1955 dst[i] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1959 dst[i] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1963 dst[i] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1967 dst[i] = FLOAT_TO_UBYTE(luminance[i]);
1969 case GL_LUMINANCE_ALPHA:
1971 dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
1972 dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1977 dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1978 dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1979 dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1984 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1985 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1986 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1987 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
1992 dst[i*3+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
1993 dst[i*3+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
1994 dst[i*3+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
1999 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2000 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2001 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2002 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2007 dst[i*4+0] = FLOAT_TO_UBYTE(rgba[i][ACOMP]);
2008 dst[i*4+1] = FLOAT_TO_UBYTE(rgba[i][BCOMP]);
2009 dst[i*4+2] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2010 dst[i*4+3] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2016 dst[i*2+0] = FLOAT_TO_UBYTE(rgba[i][RCOMP]);
2017 dst[i*2+1] = FLOAT_TO_UBYTE(rgba[i][GCOMP]);
2021 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2027 GLbyte *dst = (GLbyte *) dstAddr;
2028 switch (dstFormat) {
2031 dst[i] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2035 dst[i] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2039 dst[i] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2043 dst[i] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2047 dst[i] = FLOAT_TO_BYTE(luminance[i]);
2049 case GL_LUMINANCE_ALPHA:
2051 dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
2052 dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2057 dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2058 dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2059 dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2064 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2065 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2066 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2067 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2072 dst[i*3+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2073 dst[i*3+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2074 dst[i*3+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2079 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2080 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2081 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2082 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2087 dst[i*4+0] = FLOAT_TO_BYTE(rgba[i][ACOMP]);
2088 dst[i*4+1] = FLOAT_TO_BYTE(rgba[i][BCOMP]);
2089 dst[i*4+2] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2090 dst[i*4+3] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2096 dst[i*2+0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
2097 dst[i*2+1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
2101 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2105 case GL_UNSIGNED_SHORT:
2107 GLushort *dst = (GLushort *) dstAddr;
2108 switch (dstFormat) {
2111 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][RCOMP]);
2115 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][GCOMP]);
2119 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][BCOMP]);
2123 CLAMPED_FLOAT_TO_USHORT(dst[i], rgba[i][ACOMP]);
2127 UNCLAMPED_FLOAT_TO_USHORT(dst[i], luminance[i]);
2129 case GL_LUMINANCE_ALPHA:
2131 UNCLAMPED_FLOAT_TO_USHORT(dst[i*2+0], luminance[i]);
2132 CLAMPED_FLOAT_TO_USHORT(dst[i*2+1], rgba[i][ACOMP]);
2137 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][RCOMP]);
2138 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
2139 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][BCOMP]);
2144 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][RCOMP]);
2145 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
2146 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][BCOMP]);
2147 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
2152 CLAMPED_FLOAT_TO_USHORT(dst[i*3+0], rgba[i][BCOMP]);
2153 CLAMPED_FLOAT_TO_USHORT(dst[i*3+1], rgba[i][GCOMP]);
2154 CLAMPED_FLOAT_TO_USHORT(dst[i*3+2], rgba[i][RCOMP]);
2159 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][BCOMP]);
2160 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][GCOMP]);
2161 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][RCOMP]);
2162 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][ACOMP]);
2167 CLAMPED_FLOAT_TO_USHORT(dst[i*4+0], rgba[i][ACOMP]);
2168 CLAMPED_FLOAT_TO_USHORT(dst[i*4+1], rgba[i][BCOMP]);
2169 CLAMPED_FLOAT_TO_USHORT(dst[i*4+2], rgba[i][GCOMP]);
2170 CLAMPED_FLOAT_TO_USHORT(dst[i*4+3], rgba[i][RCOMP]);
2176 dst[i*2+0] = FLOAT_TO_USHORT(rgba[i][RCOMP]);
2177 dst[i*2+1] = FLOAT_TO_USHORT(rgba[i][GCOMP]);
2181 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2187 GLshort *dst = (GLshort *) dstAddr;
2188 switch (dstFormat) {
2191 dst[i] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2195 dst[i] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2199 dst[i] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2203 dst[i] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2207 dst[i] = FLOAT_TO_SHORT(luminance[i]);
2209 case GL_LUMINANCE_ALPHA:
2211 dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
2212 dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2217 dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2218 dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2219 dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2224 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2225 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2226 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2227 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2232 dst[i*3+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2233 dst[i*3+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2234 dst[i*3+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2239 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2240 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2241 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2242 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2247 dst[i*4+0] = FLOAT_TO_SHORT(rgba[i][ACOMP]);
2248 dst[i*4+1] = FLOAT_TO_SHORT(rgba[i][BCOMP]);
2249 dst[i*4+2] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2250 dst[i*4+3] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2256 dst[i*2+0] = FLOAT_TO_SHORT(rgba[i][RCOMP]);
2257 dst[i*2+1] = FLOAT_TO_SHORT(rgba[i][GCOMP]);
2261 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2265 case GL_UNSIGNED_INT:
2267 GLuint *dst = (GLuint *) dstAddr;
2268 switch (dstFormat) {
2271 dst[i] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2275 dst[i] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2279 dst[i] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2283 dst[i] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2287 dst[i] = FLOAT_TO_UINT(luminance[i]);
2289 case GL_LUMINANCE_ALPHA:
2291 dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
2292 dst[i*2+1] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2297 dst[i*3+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2298 dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2299 dst[i*3+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2304 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2305 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2306 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2307 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2312 dst[i*3+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2313 dst[i*3+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2314 dst[i*3+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2319 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2320 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2321 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2322 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2327 dst[i*4+0] = FLOAT_TO_UINT(rgba[i][ACOMP]);
2328 dst[i*4+1] = FLOAT_TO_UINT(rgba[i][BCOMP]);
2329 dst[i*4+2] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2330 dst[i*4+3] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2336 dst[i*2+0] = FLOAT_TO_UINT(rgba[i][RCOMP]);
2337 dst[i*2+1] = FLOAT_TO_UINT(rgba[i][GCOMP]);
2341 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2347 GLint *dst = (GLint *) dstAddr;
2348 switch (dstFormat) {
2351 dst[i] = FLOAT_TO_INT(rgba[i][RCOMP]);
2355 dst[i] = FLOAT_TO_INT(rgba[i][GCOMP]);
2359 dst[i] = FLOAT_TO_INT(rgba[i][BCOMP]);
2363 dst[i] = FLOAT_TO_INT(rgba[i][ACOMP]);
2367 dst[i] = FLOAT_TO_INT(luminance[i]);
2369 case GL_LUMINANCE_ALPHA:
2371 dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
2372 dst[i*2+1] = FLOAT_TO_INT(rgba[i][ACOMP]);
2377 dst[i*3+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2378 dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2379 dst[i*3+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2384 dst[i*4+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2385 dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2386 dst[i*4+2] = FLOAT_TO_INT(rgba[i][BCOMP]);
2387 dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2392 dst[i*3+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2393 dst[i*3+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2394 dst[i*3+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2399 dst[i*4+0] = FLOAT_TO_INT(rgba[i][BCOMP]);
2400 dst[i*4+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2401 dst[i*4+2] = FLOAT_TO_INT(rgba[i][RCOMP]);
2402 dst[i*4+3] = FLOAT_TO_INT(rgba[i][ACOMP]);
2407 dst[i*4+0] = FLOAT_TO_INT(rgba[i][ACOMP]);
2408 dst[i*4+1] = FLOAT_TO_INT(rgba[i][BCOMP]);
2409 dst[i*4+2] = FLOAT_TO_INT(rgba[i][GCOMP]);
2410 dst[i*4+3] = FLOAT_TO_INT(rgba[i][RCOMP]);
2416 dst[i*2+0] = FLOAT_TO_INT(rgba[i][RCOMP]);
2417 dst[i*2+1] = FLOAT_TO_INT(rgba[i][GCOMP]);
2421 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2427 GLfloat *dst = (GLfloat *) dstAddr;
2428 switch (dstFormat) {
2431 dst[i] = rgba[i][RCOMP];
2435 dst[i] = rgba[i][GCOMP];
2439 dst[i] = rgba[i][BCOMP];
2443 dst[i] = rgba[i][ACOMP];
2447 dst[i] = luminance[i];
2449 case GL_LUMINANCE_ALPHA:
2451 dst[i*2+0] = luminance[i];
2452 dst[i*2+1] = rgba[i][ACOMP];
2457 dst[i*3+0] = rgba[i][RCOMP];
2458 dst[i*3+1] = rgba[i][GCOMP];
2459 dst[i*3+2] = rgba[i][BCOMP];
2464 dst[i*4+0] = rgba[i][RCOMP];
2465 dst[i*4+1] = rgba[i][GCOMP];
2466 dst[i*4+2] = rgba[i][BCOMP];
2467 dst[i*4+3] = rgba[i][ACOMP];
2472 dst[i*3+0] = rgba[i][BCOMP];
2473 dst[i*3+1] = rgba[i][GCOMP];
2474 dst[i*3+2] = rgba[i][RCOMP];
2479 dst[i*4+0] = rgba[i][BCOMP];
2480 dst[i*4+1] = rgba[i][GCOMP];
2481 dst[i*4+2] = rgba[i][RCOMP];
2482 dst[i*4+3] = rgba[i][ACOMP];
2487 dst[i*4+0] = rgba[i][ACOMP];
2488 dst[i*4+1] = rgba[i][BCOMP];
2489 dst[i*4+2] = rgba[i][GCOMP];
2490 dst[i*4+3] = rgba[i][RCOMP];
2496 dst[i*2+0] = rgba[i][RCOMP];
2497 dst[i*2+1] = rgba[i][GCOMP];
2501 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2505 case GL_HALF_FLOAT_ARB:
2507 GLhalfARB *dst = (GLhalfARB *) dstAddr;
2508 switch (dstFormat) {
2511 dst[i] = _mesa_float_to_half(rgba[i][RCOMP]);
2515 dst[i] = _mesa_float_to_half(rgba[i][GCOMP]);
2519 dst[i] = _mesa_float_to_half(rgba[i][BCOMP]);
2523 dst[i] = _mesa_float_to_half(rgba[i][ACOMP]);
2527 dst[i] = _mesa_float_to_half(luminance[i]);
2529 case GL_LUMINANCE_ALPHA:
2531 dst[i*2+0] = _mesa_float_to_half(luminance[i]);
2532 dst[i*2+1] = _mesa_float_to_half(rgba[i][ACOMP]);
2537 dst[i*3+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2538 dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2539 dst[i*3+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2544 dst[i*4+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2545 dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2546 dst[i*4+2] = _mesa_float_to_half(rgba[i][BCOMP]);
2547 dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2552 dst[i*3+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2553 dst[i*3+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2554 dst[i*3+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2559 dst[i*4+0] = _mesa_float_to_half(rgba[i][BCOMP]);
2560 dst[i*4+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2561 dst[i*4+2] = _mesa_float_to_half(rgba[i][RCOMP]);
2562 dst[i*4+3] = _mesa_float_to_half(rgba[i][ACOMP]);
2567 dst[i*4+0] = _mesa_float_to_half(rgba[i][ACOMP]);
2568 dst[i*4+1] = _mesa_float_to_half(rgba[i][BCOMP]);
2569 dst[i*4+2] = _mesa_float_to_half(rgba[i][GCOMP]);
2570 dst[i*4+3] = _mesa_float_to_half(rgba[i][RCOMP]);
2576 dst[i*2+0] = _mesa_float_to_half(rgba[i][RCOMP]);
2577 dst[i*2+1] = _mesa_float_to_half(rgba[i][GCOMP]);
2581 _mesa_problem(ctx, "bad format in _mesa_pack_rgba_span\n");
2585 case GL_UNSIGNED_BYTE_3_3_2:
2586 if (dstFormat == GL_RGB) {
2587 GLubyte *dst = (GLubyte *) dstAddr;
2589 dst[i] = (IROUND(rgba[i][RCOMP] * 7.0F) << 5)
2590 | (IROUND(rgba[i][GCOMP] * 7.0F) << 2)
2591 | (IROUND(rgba[i][BCOMP] * 3.0F) );
2595 case GL_UNSIGNED_BYTE_2_3_3_REV:
2596 if (dstFormat == GL_RGB) {
2597 GLubyte *dst = (GLubyte *) dstAddr;
2599 dst[i] = (IROUND(rgba[i][RCOMP] * 7.0F) )
2600 | (IROUND(rgba[i][GCOMP] * 7.0F) << 3)
2601 | (IROUND(rgba[i][BCOMP] * 3.0F) << 6);
2605 case GL_UNSIGNED_SHORT_5_6_5:
2606 if (dstFormat == GL_RGB) {
2607 GLushort *dst = (GLushort *) dstAddr;
2609 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) << 11)
2610 | (IROUND(rgba[i][GCOMP] * 63.0F) << 5)
2611 | (IROUND(rgba[i][BCOMP] * 31.0F) );
2615 case GL_UNSIGNED_SHORT_5_6_5_REV:
2616 if (dstFormat == GL_RGB) {
2617 GLushort *dst = (GLushort *) dstAddr;
2619 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) )
2620 | (IROUND(rgba[i][GCOMP] * 63.0F) << 5)
2621 | (IROUND(rgba[i][BCOMP] * 31.0F) << 11);
2625 case GL_UNSIGNED_SHORT_4_4_4_4:
2626 if (dstFormat == GL_RGBA) {
2627 GLushort *dst = (GLushort *) dstAddr;
2629 dst[i] = (IROUND(rgba[i][RCOMP] * 15.0F) << 12)
2630 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2631 | (IROUND(rgba[i][BCOMP] * 15.0F) << 4)
2632 | (IROUND(rgba[i][ACOMP] * 15.0F) );
2635 else if (dstFormat == GL_BGRA) {
2636 GLushort *dst = (GLushort *) dstAddr;
2638 dst[i] = (IROUND(rgba[i][BCOMP] * 15.0F) << 12)
2639 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2640 | (IROUND(rgba[i][RCOMP] * 15.0F) << 4)
2641 | (IROUND(rgba[i][ACOMP] * 15.0F) );
2644 else if (dstFormat == GL_ABGR_EXT) {
2645 GLushort *dst = (GLushort *) dstAddr;
2647 dst[i] = (IROUND(rgba[i][ACOMP] * 15.0F) << 12)
2648 | (IROUND(rgba[i][BCOMP] * 15.0F) << 8)
2649 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2650 | (IROUND(rgba[i][RCOMP] * 15.0F) );
2654 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
2655 if (dstFormat == GL_RGBA) {
2656 GLushort *dst = (GLushort *) dstAddr;
2658 dst[i] = (IROUND(rgba[i][RCOMP] * 15.0F) )
2659 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2660 | (IROUND(rgba[i][BCOMP] * 15.0F) << 8)
2661 | (IROUND(rgba[i][ACOMP] * 15.0F) << 12);
2664 else if (dstFormat == GL_BGRA) {
2665 GLushort *dst = (GLushort *) dstAddr;
2667 dst[i] = (IROUND(rgba[i][BCOMP] * 15.0F) )
2668 | (IROUND(rgba[i][GCOMP] * 15.0F) << 4)
2669 | (IROUND(rgba[i][RCOMP] * 15.0F) << 8)
2670 | (IROUND(rgba[i][ACOMP] * 15.0F) << 12);
2673 else if (dstFormat == GL_ABGR_EXT) {
2674 GLushort *dst = (GLushort *) dstAddr;
2676 dst[i] = (IROUND(rgba[i][ACOMP] * 15.0F) )
2677 | (IROUND(rgba[i][BCOMP] * 15.0F) << 4)
2678 | (IROUND(rgba[i][GCOMP] * 15.0F) << 8)
2679 | (IROUND(rgba[i][RCOMP] * 15.0F) << 12);
2683 case GL_UNSIGNED_SHORT_5_5_5_1:
2684 if (dstFormat == GL_RGBA) {
2685 GLushort *dst = (GLushort *) dstAddr;
2687 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) << 11)
2688 | (IROUND(rgba[i][GCOMP] * 31.0F) << 6)
2689 | (IROUND(rgba[i][BCOMP] * 31.0F) << 1)
2690 | (IROUND(rgba[i][ACOMP] * 1.0F) );
2693 else if (dstFormat == GL_BGRA) {
2694 GLushort *dst = (GLushort *) dstAddr;
2696 dst[i] = (IROUND(rgba[i][BCOMP] * 31.0F) << 11)
2697 | (IROUND(rgba[i][GCOMP] * 31.0F) << 6)
2698 | (IROUND(rgba[i][RCOMP] * 31.0F) << 1)
2699 | (IROUND(rgba[i][ACOMP] * 1.0F) );
2702 else if (dstFormat == GL_ABGR_EXT) {
2703 GLushort *dst = (GLushort *) dstAddr;
2705 dst[i] = (IROUND(rgba[i][ACOMP] * 31.0F) << 11)
2706 | (IROUND(rgba[i][BCOMP] * 31.0F) << 6)
2707 | (IROUND(rgba[i][GCOMP] * 31.0F) << 1)
2708 | (IROUND(rgba[i][RCOMP] * 1.0F) );
2712 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
2713 if (dstFormat == GL_RGBA) {
2714 GLushort *dst = (GLushort *) dstAddr;
2716 dst[i] = (IROUND(rgba[i][RCOMP] * 31.0F) )
2717 | (IROUND(rgba[i][GCOMP] * 31.0F) << 5)
2718 | (IROUND(rgba[i][BCOMP] * 31.0F) << 10)
2719 | (IROUND(rgba[i][ACOMP] * 1.0F) << 15);
2722 else if (dstFormat == GL_BGRA) {
2723 GLushort *dst = (GLushort *) dstAddr;
2725 dst[i] = (IROUND(rgba[i][BCOMP] * 31.0F) )
2726 | (IROUND(rgba[i][GCOMP] * 31.0F) << 5)
2727 | (IROUND(rgba[i][RCOMP] * 31.0F) << 10)
2728 | (IROUND(rgba[i][ACOMP] * 1.0F) << 15);
2731 else if (dstFormat == GL_ABGR_EXT) {
2732 GLushort *dst = (GLushort *) dstAddr;
2734 dst[i] = (IROUND(rgba[i][ACOMP] * 31.0F) )
2735 | (IROUND(rgba[i][BCOMP] * 31.0F) << 5)
2736 | (IROUND(rgba[i][GCOMP] * 31.0F) << 10)
2737 | (IROUND(rgba[i][RCOMP] * 1.0F) << 15);
2741 case GL_UNSIGNED_INT_8_8_8_8:
2742 if (dstFormat == GL_RGBA) {
2743 GLuint *dst = (GLuint *) dstAddr;
2745 dst[i] = (IROUND(rgba[i][RCOMP] * 255.F) << 24)
2746 | (IROUND(rgba[i][GCOMP] * 255.F) << 16)
2747 | (IROUND(rgba[i][BCOMP] * 255.F) << 8)
2748 | (IROUND(rgba[i][ACOMP] * 255.F) );
2751 else if (dstFormat == GL_BGRA) {
2752 GLuint *dst = (GLuint *) dstAddr;
2754 dst[i] = (IROUND(rgba[i][BCOMP] * 255.F) << 24)
2755 | (IROUND(rgba[i][GCOMP] * 255.F) << 16)
2756 | (IROUND(rgba[i][RCOMP] * 255.F) << 8)
2757 | (IROUND(rgba[i][ACOMP] * 255.F) );
2760 else if (dstFormat == GL_ABGR_EXT) {
2761 GLuint *dst = (GLuint *) dstAddr;
2763 dst[i] = (IROUND(rgba[i][ACOMP] * 255.F) << 24)
2764 | (IROUND(rgba[i][BCOMP] * 255.F) << 16)
2765 | (IROUND(rgba[i][GCOMP] * 255.F) << 8)
2766 | (IROUND(rgba[i][RCOMP] * 255.F) );
2770 case GL_UNSIGNED_INT_8_8_8_8_REV:
2771 if (dstFormat == GL_RGBA) {
2772 GLuint *dst = (GLuint *) dstAddr;
2774 dst[i] = (IROUND(rgba[i][RCOMP] * 255.0F) )
2775 | (IROUND(rgba[i][GCOMP] * 255.0F) << 8)
2776 | (IROUND(rgba[i][BCOMP] * 255.0F) << 16)
2777 | (IROUND(rgba[i][ACOMP] * 255.0F) << 24);
2780 else if (dstFormat == GL_BGRA) {
2781 GLuint *dst = (GLuint *) dstAddr;
2783 dst[i] = (IROUND(rgba[i][BCOMP] * 255.0F) )
2784 | (IROUND(rgba[i][GCOMP] * 255.0F) << 8)
2785 | (IROUND(rgba[i][RCOMP] * 255.0F) << 16)
2786 | (IROUND(rgba[i][ACOMP] * 255.0F) << 24);
2789 else if (dstFormat == GL_ABGR_EXT) {
2790 GLuint *dst = (GLuint *) dstAddr;
2792 dst[i] = (IROUND(rgba[i][ACOMP] * 255.0F) )
2793 | (IROUND(rgba[i][BCOMP] * 255.0F) << 8)
2794 | (IROUND(rgba[i][GCOMP] * 255.0F) << 16)
2795 | (IROUND(rgba[i][RCOMP] * 255.0F) << 24);
2799 case GL_UNSIGNED_INT_10_10_10_2:
2800 if (dstFormat == GL_RGBA) {
2801 GLuint *dst = (GLuint *) dstAddr;
2803 dst[i] = (IROUND(rgba[i][RCOMP] * 1023.0F) << 22)
2804 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 12)
2805 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 2)
2806 | (IROUND(rgba[i][ACOMP] * 3.0F) );
2809 else if (dstFormat == GL_BGRA) {
2810 GLuint *dst = (GLuint *) dstAddr;
2812 dst[i] = (IROUND(rgba[i][BCOMP] * 1023.0F) << 22)
2813 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 12)
2814 | (IROUND(rgba[i][RCOMP] * 1023.0F) << 2)
2815 | (IROUND(rgba[i][ACOMP] * 3.0F) );
2818 else if (dstFormat == GL_ABGR_EXT) {
2819 GLuint *dst = (GLuint *) dstAddr;
2821 dst[i] = (IROUND(rgba[i][ACOMP] * 1023.0F) << 22)
2822 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 12)
2823 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 2)
2824 | (IROUND(rgba[i][RCOMP] * 3.0F) );
2828 case GL_UNSIGNED_INT_2_10_10_10_REV:
2829 if (dstFormat == GL_RGBA) {
2830 GLuint *dst = (GLuint *) dstAddr;
2832 dst[i] = (IROUND(rgba[i][RCOMP] * 1023.0F) )
2833 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 10)
2834 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 20)
2835 | (IROUND(rgba[i][ACOMP] * 3.0F) << 30);
2838 else if (dstFormat == GL_BGRA) {
2839 GLuint *dst = (GLuint *) dstAddr;
2841 dst[i] = (IROUND(rgba[i][BCOMP] * 1023.0F) )
2842 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 10)
2843 | (IROUND(rgba[i][RCOMP] * 1023.0F) << 20)
2844 | (IROUND(rgba[i][ACOMP] * 3.0F) << 30);
2847 else if (dstFormat == GL_ABGR_EXT) {
2848 GLuint *dst = (GLuint *) dstAddr;
2850 dst[i] = (IROUND(rgba[i][ACOMP] * 1023.0F) )
2851 | (IROUND(rgba[i][BCOMP] * 1023.0F) << 10)
2852 | (IROUND(rgba[i][GCOMP] * 1023.0F) << 20)
2853 | (IROUND(rgba[i][RCOMP] * 3.0F) << 30);
2858 _mesa_problem(ctx, "bad type in _mesa_pack_rgba_span_float");
2862 if (dstPacking->SwapBytes) {
2863 GLint swapSize = _mesa_sizeof_packed_type(dstType);
2864 if (swapSize == 2) {
2865 if (dstPacking->SwapBytes) {
2866 _mesa_swap2((GLushort *) dstAddr, n * comps);
2869 else if (swapSize == 4) {
2870 if (dstPacking->SwapBytes) {
2871 _mesa_swap4((GLuint *) dstAddr, n * comps);
2878 #define SWAP2BYTE(VALUE) \
2880 GLubyte *bytes = (GLubyte *) &(VALUE); \
2881 GLubyte tmp = bytes[0]; \
2882 bytes[0] = bytes[1]; \
2886 #define SWAP4BYTE(VALUE) \
2888 GLubyte *bytes = (GLubyte *) &(VALUE); \
2889 GLubyte tmp = bytes[0]; \
2890 bytes[0] = bytes[3]; \
2893 bytes[1] = bytes[2]; \
2899 extract_uint_indexes(GLuint n, GLuint indexes[],
2900 GLenum srcFormat, GLenum srcType, const GLvoid *src,
2901 const struct gl_pixelstore_attrib *unpack )
2903 ASSERT(srcFormat == GL_COLOR_INDEX || srcFormat == GL_STENCIL_INDEX);
2905 ASSERT(srcType == GL_BITMAP ||
2906 srcType == GL_UNSIGNED_BYTE ||
2907 srcType == GL_BYTE ||
2908 srcType == GL_UNSIGNED_SHORT ||
2909 srcType == GL_SHORT ||
2910 srcType == GL_UNSIGNED_INT ||
2911 srcType == GL_INT ||
2912 srcType == GL_UNSIGNED_INT_24_8_EXT ||
2913 srcType == GL_HALF_FLOAT_ARB ||
2914 srcType == GL_FLOAT);
2919 GLubyte *ubsrc = (GLubyte *) src;
2920 if (unpack->LsbFirst) {
2921 GLubyte mask = 1 << (unpack->SkipPixels & 0x7);
2923 for (i = 0; i < n; i++) {
2924 indexes[i] = (*ubsrc & mask) ? 1 : 0;
2935 GLubyte mask = 128 >> (unpack->SkipPixels & 0x7);
2937 for (i = 0; i < n; i++) {
2938 indexes[i] = (*ubsrc & mask) ? 1 : 0;
2950 case GL_UNSIGNED_BYTE:
2953 const GLubyte *s = (const GLubyte *) src;
2954 for (i = 0; i < n; i++)
2961 const GLbyte *s = (const GLbyte *) src;
2962 for (i = 0; i < n; i++)
2966 case GL_UNSIGNED_SHORT:
2969 const GLushort *s = (const GLushort *) src;
2970 if (unpack->SwapBytes) {
2971 for (i = 0; i < n; i++) {
2972 GLushort value = s[i];
2978 for (i = 0; i < n; i++)
2986 const GLshort *s = (const GLshort *) src;
2987 if (unpack->SwapBytes) {
2988 for (i = 0; i < n; i++) {
2989 GLshort value = s[i];
2995 for (i = 0; i < n; i++)
3000 case GL_UNSIGNED_INT:
3003 const GLuint *s = (const GLuint *) src;
3004 if (unpack->SwapBytes) {
3005 for (i = 0; i < n; i++) {
3006 GLuint value = s[i];
3012 for (i = 0; i < n; i++)
3020 const GLint *s = (const GLint *) src;
3021 if (unpack->SwapBytes) {
3022 for (i = 0; i < n; i++) {
3029 for (i = 0; i < n; i++)
3037 const GLfloat *s = (const GLfloat *) src;
3038 if (unpack->SwapBytes) {
3039 for (i = 0; i < n; i++) {
3040 GLfloat value = s[i];
3042 indexes[i] = (GLuint) value;
3046 for (i = 0; i < n; i++)
3047 indexes[i] = (GLuint) s[i];
3051 case GL_HALF_FLOAT_ARB:
3054 const GLhalfARB *s = (const GLhalfARB *) src;
3055 if (unpack->SwapBytes) {
3056 for (i = 0; i < n; i++) {
3057 GLhalfARB value = s[i];
3059 indexes[i] = (GLuint) _mesa_half_to_float(value);
3063 for (i = 0; i < n; i++)
3064 indexes[i] = (GLuint) _mesa_half_to_float(s[i]);
3068 case GL_UNSIGNED_INT_24_8_EXT:
3071 const GLuint *s = (const GLuint *) src;
3072 if (unpack->SwapBytes) {
3073 for (i = 0; i < n; i++) {
3074 GLuint value = s[i];
3076 indexes[i] = value & 0xff; /* lower 8 bits */
3080 for (i = 0; i < n; i++)
3081 indexes[i] = s[i] & 0xff; /* lower 8 bits */
3087 _mesa_problem(NULL, "bad srcType in extract_uint_indexes");
3094 * This function extracts floating point RGBA values from arbitrary
3095 * image data. srcFormat and srcType are the format and type parameters
3096 * passed to glDrawPixels, glTexImage[123]D, glTexSubImage[123]D, etc.
3098 * Refering to section 3.6.4 of the OpenGL 1.2 spec, this function
3099 * implements the "Conversion to floating point", "Conversion to RGB",
3100 * and "Final Expansion to RGBA" operations.
3102 * Args: n - number of pixels
3103 * rgba - output colors
3104 * srcFormat - format of incoming data
3105 * srcType - data type of incoming data
3106 * src - source data pointer
3107 * swapBytes - perform byteswapping of incoming data?
3110 extract_float_rgba(GLuint n, GLfloat rgba[][4],
3111 GLenum srcFormat, GLenum srcType, const GLvoid *src,
3112 GLboolean swapBytes)
3114 GLint redIndex, greenIndex, blueIndex, alphaIndex;
3116 GLint rComp, bComp, gComp, aComp;
3118 ASSERT(srcFormat == GL_RED ||
3119 srcFormat == GL_GREEN ||
3120 srcFormat == GL_BLUE ||
3121 srcFormat == GL_ALPHA ||
3122 srcFormat == GL_LUMINANCE ||
3123 srcFormat == GL_LUMINANCE_ALPHA ||
3124 srcFormat == GL_INTENSITY ||
3125 srcFormat == GL_RGB ||
3126 srcFormat == GL_BGR ||
3127 srcFormat == GL_RGBA ||
3128 srcFormat == GL_BGRA ||
3129 srcFormat == GL_ABGR_EXT ||
3130 srcFormat == GL_DUDV_ATI);
3132 ASSERT(srcType == GL_UNSIGNED_BYTE ||
3133 srcType == GL_BYTE ||
3134 srcType == GL_UNSIGNED_SHORT ||
3135 srcType == GL_SHORT ||
3136 srcType == GL_UNSIGNED_INT ||
3137 srcType == GL_INT ||
3138 srcType == GL_HALF_FLOAT_ARB ||
3139 srcType == GL_FLOAT ||
3140 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3141 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3142 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3143 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3144 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3145 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3146 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3147 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3148 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3149 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3150 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3151 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3153 rComp = gComp = bComp = aComp = -1;
3155 switch (srcFormat) {
3158 greenIndex = blueIndex = alphaIndex = -1;
3163 redIndex = blueIndex = alphaIndex = -1;
3168 redIndex = greenIndex = alphaIndex = -1;
3172 redIndex = greenIndex = blueIndex = -1;
3177 redIndex = greenIndex = blueIndex = 0;
3181 case GL_LUMINANCE_ALPHA:
3182 redIndex = greenIndex = blueIndex = 0;
3187 redIndex = greenIndex = blueIndex = alphaIndex = 0;
3253 _mesa_problem(NULL, "bad srcFormat in extract float data");
3258 #define PROCESS(INDEX, CHANNEL, DEFAULT, TYPE, CONVERSION) \
3259 if ((INDEX) < 0) { \
3261 for (i = 0; i < n; i++) { \
3262 rgba[i][CHANNEL] = DEFAULT; \
3265 else if (swapBytes) { \
3266 const TYPE *s = (const TYPE *) src; \
3268 for (i = 0; i < n; i++) { \
3269 TYPE value = s[INDEX]; \
3270 if (sizeof(TYPE) == 2) { \
3273 else if (sizeof(TYPE) == 4) { \
3276 rgba[i][CHANNEL] = (GLfloat) CONVERSION(value); \
3281 const TYPE *s = (const TYPE *) src; \
3283 for (i = 0; i < n; i++) { \
3284 rgba[i][CHANNEL] = (GLfloat) CONVERSION(s[INDEX]); \
3290 case GL_UNSIGNED_BYTE:
3291 PROCESS(redIndex, RCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3292 PROCESS(greenIndex, GCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3293 PROCESS(blueIndex, BCOMP, 0.0F, GLubyte, UBYTE_TO_FLOAT);
3294 PROCESS(alphaIndex, ACOMP, 1.0F, GLubyte, UBYTE_TO_FLOAT);
3297 PROCESS(redIndex, RCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3298 PROCESS(greenIndex, GCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3299 PROCESS(blueIndex, BCOMP, 0.0F, GLbyte, BYTE_TO_FLOAT);
3300 PROCESS(alphaIndex, ACOMP, 1.0F, GLbyte, BYTE_TO_FLOAT);
3302 case GL_UNSIGNED_SHORT:
3303 PROCESS(redIndex, RCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3304 PROCESS(greenIndex, GCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3305 PROCESS(blueIndex, BCOMP, 0.0F, GLushort, USHORT_TO_FLOAT);
3306 PROCESS(alphaIndex, ACOMP, 1.0F, GLushort, USHORT_TO_FLOAT);
3309 PROCESS(redIndex, RCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3310 PROCESS(greenIndex, GCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3311 PROCESS(blueIndex, BCOMP, 0.0F, GLshort, SHORT_TO_FLOAT);
3312 PROCESS(alphaIndex, ACOMP, 1.0F, GLshort, SHORT_TO_FLOAT);
3314 case GL_UNSIGNED_INT:
3315 PROCESS(redIndex, RCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3316 PROCESS(greenIndex, GCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3317 PROCESS(blueIndex, BCOMP, 0.0F, GLuint, UINT_TO_FLOAT);
3318 PROCESS(alphaIndex, ACOMP, 1.0F, GLuint, UINT_TO_FLOAT);
3321 PROCESS(redIndex, RCOMP, 0.0F, GLint, INT_TO_FLOAT);
3322 PROCESS(greenIndex, GCOMP, 0.0F, GLint, INT_TO_FLOAT);
3323 PROCESS(blueIndex, BCOMP, 0.0F, GLint, INT_TO_FLOAT);
3324 PROCESS(alphaIndex, ACOMP, 1.0F, GLint, INT_TO_FLOAT);
3327 PROCESS(redIndex, RCOMP, 0.0F, GLfloat, (GLfloat));
3328 PROCESS(greenIndex, GCOMP, 0.0F, GLfloat, (GLfloat));
3329 PROCESS(blueIndex, BCOMP, 0.0F, GLfloat, (GLfloat));
3330 PROCESS(alphaIndex, ACOMP, 1.0F, GLfloat, (GLfloat));
3332 case GL_HALF_FLOAT_ARB:
3333 PROCESS(redIndex, RCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3334 PROCESS(greenIndex, GCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3335 PROCESS(blueIndex, BCOMP, 0.0F, GLhalfARB, _mesa_half_to_float);
3336 PROCESS(alphaIndex, ACOMP, 1.0F, GLhalfARB, _mesa_half_to_float);
3338 case GL_UNSIGNED_BYTE_3_3_2:
3340 const GLubyte *ubsrc = (const GLubyte *) src;
3342 for (i = 0; i < n; i ++) {
3343 GLubyte p = ubsrc[i];
3344 rgba[i][rComp] = ((p >> 5) ) * (1.0F / 7.0F);
3345 rgba[i][gComp] = ((p >> 2) & 0x7) * (1.0F / 7.0F);
3346 rgba[i][bComp] = ((p ) & 0x3) * (1.0F / 3.0F);
3347 rgba[i][aComp] = 1.0F;
3351 case GL_UNSIGNED_BYTE_2_3_3_REV:
3353 const GLubyte *ubsrc = (const GLubyte *) src;
3355 for (i = 0; i < n; i ++) {
3356 GLubyte p = ubsrc[i];
3357 rgba[i][rComp] = ((p ) & 0x7) * (1.0F / 7.0F);
3358 rgba[i][gComp] = ((p >> 3) & 0x7) * (1.0F / 7.0F);
3359 rgba[i][bComp] = ((p >> 6) ) * (1.0F / 3.0F);
3360 rgba[i][aComp] = 1.0F;
3364 case GL_UNSIGNED_SHORT_5_6_5:
3366 const GLushort *ussrc = (const GLushort *) src;
3368 for (i = 0; i < n; i ++) {
3369 GLushort p = ussrc[i];
3371 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3372 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3373 rgba[i][bComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3374 rgba[i][aComp] = 1.0F;
3378 const GLushort *ussrc = (const GLushort *) src;
3380 for (i = 0; i < n; i ++) {
3381 GLushort p = ussrc[i];
3382 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3383 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3384 rgba[i][bComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3385 rgba[i][aComp] = 1.0F;
3389 case GL_UNSIGNED_SHORT_5_6_5_REV:
3391 const GLushort *ussrc = (const GLushort *) src;
3393 for (i = 0; i < n; i ++) {
3394 GLushort p = ussrc[i];
3396 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3397 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3398 rgba[i][bComp] = ((p >> 11) ) * (1.0F / 31.0F);
3399 rgba[i][aComp] = 1.0F;
3403 const GLushort *ussrc = (const GLushort *) src;
3405 for (i = 0; i < n; i ++) {
3406 GLushort p = ussrc[i];
3407 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3408 rgba[i][gComp] = ((p >> 5) & 0x3f) * (1.0F / 63.0F);
3409 rgba[i][bComp] = ((p >> 11) ) * (1.0F / 31.0F);
3410 rgba[i][aComp] = 1.0F;
3414 case GL_UNSIGNED_SHORT_4_4_4_4:
3416 const GLushort *ussrc = (const GLushort *) src;
3418 for (i = 0; i < n; i ++) {
3419 GLushort p = ussrc[i];
3421 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
3422 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3423 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3424 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3428 const GLushort *ussrc = (const GLushort *) src;
3430 for (i = 0; i < n; i ++) {
3431 GLushort p = ussrc[i];
3432 rgba[i][rComp] = ((p >> 12) ) * (1.0F / 15.0F);
3433 rgba[i][gComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3434 rgba[i][bComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3435 rgba[i][aComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3439 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
3441 const GLushort *ussrc = (const GLushort *) src;
3443 for (i = 0; i < n; i ++) {
3444 GLushort p = ussrc[i];
3446 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3447 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3448 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3449 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
3453 const GLushort *ussrc = (const GLushort *) src;
3455 for (i = 0; i < n; i ++) {
3456 GLushort p = ussrc[i];
3457 rgba[i][rComp] = ((p ) & 0xf) * (1.0F / 15.0F);
3458 rgba[i][gComp] = ((p >> 4) & 0xf) * (1.0F / 15.0F);
3459 rgba[i][bComp] = ((p >> 8) & 0xf) * (1.0F / 15.0F);
3460 rgba[i][aComp] = ((p >> 12) ) * (1.0F / 15.0F);
3464 case GL_UNSIGNED_SHORT_5_5_5_1:
3466 const GLushort *ussrc = (const GLushort *) src;
3468 for (i = 0; i < n; i ++) {
3469 GLushort p = ussrc[i];
3471 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3472 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
3473 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
3474 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
3478 const GLushort *ussrc = (const GLushort *) src;
3480 for (i = 0; i < n; i ++) {
3481 GLushort p = ussrc[i];
3482 rgba[i][rComp] = ((p >> 11) ) * (1.0F / 31.0F);
3483 rgba[i][gComp] = ((p >> 6) & 0x1f) * (1.0F / 31.0F);
3484 rgba[i][bComp] = ((p >> 1) & 0x1f) * (1.0F / 31.0F);
3485 rgba[i][aComp] = ((p ) & 0x1) * (1.0F / 1.0F);
3489 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
3491 const GLushort *ussrc = (const GLushort *) src;
3493 for (i = 0; i < n; i ++) {
3494 GLushort p = ussrc[i];
3496 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3497 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
3498 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3499 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
3503 const GLushort *ussrc = (const GLushort *) src;
3505 for (i = 0; i < n; i ++) {
3506 GLushort p = ussrc[i];
3507 rgba[i][rComp] = ((p ) & 0x1f) * (1.0F / 31.0F);
3508 rgba[i][gComp] = ((p >> 5) & 0x1f) * (1.0F / 31.0F);
3509 rgba[i][bComp] = ((p >> 10) & 0x1f) * (1.0F / 31.0F);
3510 rgba[i][aComp] = ((p >> 15) ) * (1.0F / 1.0F);
3514 case GL_UNSIGNED_INT_8_8_8_8:
3516 const GLuint *uisrc = (const GLuint *) src;
3518 for (i = 0; i < n; i ++) {
3519 GLuint p = uisrc[i];
3520 rgba[i][rComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3521 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3522 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3523 rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24) );
3527 const GLuint *uisrc = (const GLuint *) src;
3529 for (i = 0; i < n; i ++) {
3530 GLuint p = uisrc[i];
3531 rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24) );
3532 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3533 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3534 rgba[i][aComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3538 case GL_UNSIGNED_INT_8_8_8_8_REV:
3540 const GLuint *uisrc = (const GLuint *) src;
3542 for (i = 0; i < n; i ++) {
3543 GLuint p = uisrc[i];
3544 rgba[i][rComp] = UBYTE_TO_FLOAT((p >> 24) );
3545 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3546 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3547 rgba[i][aComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3551 const GLuint *uisrc = (const GLuint *) src;
3553 for (i = 0; i < n; i ++) {
3554 GLuint p = uisrc[i];
3555 rgba[i][rComp] = UBYTE_TO_FLOAT((p ) & 0xff);
3556 rgba[i][gComp] = UBYTE_TO_FLOAT((p >> 8) & 0xff);
3557 rgba[i][bComp] = UBYTE_TO_FLOAT((p >> 16) & 0xff);
3558 rgba[i][aComp] = UBYTE_TO_FLOAT((p >> 24) );
3562 case GL_UNSIGNED_INT_10_10_10_2:
3564 const GLuint *uisrc = (const GLuint *) src;
3566 for (i = 0; i < n; i ++) {
3567 GLuint p = uisrc[i];
3569 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
3570 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3571 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
3572 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
3576 const GLuint *uisrc = (const GLuint *) src;
3578 for (i = 0; i < n; i ++) {
3579 GLuint p = uisrc[i];
3580 rgba[i][rComp] = ((p >> 22) ) * (1.0F / 1023.0F);
3581 rgba[i][gComp] = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F);
3582 rgba[i][bComp] = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F);
3583 rgba[i][aComp] = ((p ) & 0x3 ) * (1.0F / 3.0F);
3587 case GL_UNSIGNED_INT_2_10_10_10_REV:
3589 const GLuint *uisrc = (const GLuint *) src;
3591 for (i = 0; i < n; i ++) {
3592 GLuint p = uisrc[i];
3594 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
3595 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3596 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3597 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
3601 const GLuint *uisrc = (const GLuint *) src;
3603 for (i = 0; i < n; i ++) {
3604 GLuint p = uisrc[i];
3605 rgba[i][rComp] = ((p ) & 0x3ff) * (1.0F / 1023.0F);
3606 rgba[i][gComp] = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F);
3607 rgba[i][bComp] = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F);
3608 rgba[i][aComp] = ((p >> 30) ) * (1.0F / 3.0F);
3613 _mesa_problem(NULL, "bad srcType in extract float data");
3620 * Unpack a row of color image data from a client buffer according to
3621 * the pixel unpacking parameters.
3622 * Return GLchan values in the specified dest image format.
3623 * This is used by glDrawPixels and glTexImage?D().
3624 * \param ctx - the context
3625 * n - number of pixels in the span
3626 * dstFormat - format of destination color array
3627 * dest - the destination color array
3628 * srcFormat - source image format
3629 * srcType - source image data type
3630 * source - source image pointer
3631 * srcPacking - pixel unpacking parameters
3632 * transferOps - bitmask of IMAGE_*_BIT values of operations to apply
3634 * XXX perhaps expand this to process whole images someday.
3637 _mesa_unpack_color_span_chan( GLcontext *ctx,
3638 GLuint n, GLenum dstFormat, GLchan dest[],
3639 GLenum srcFormat, GLenum srcType,
3640 const GLvoid *source,
3641 const struct gl_pixelstore_attrib *srcPacking,
3642 GLbitfield transferOps )
3644 ASSERT(dstFormat == GL_ALPHA ||
3645 dstFormat == GL_LUMINANCE ||
3646 dstFormat == GL_LUMINANCE_ALPHA ||
3647 dstFormat == GL_INTENSITY ||
3648 dstFormat == GL_RGB ||
3649 dstFormat == GL_RGBA ||
3650 dstFormat == GL_COLOR_INDEX);
3652 ASSERT(srcFormat == GL_RED ||
3653 srcFormat == GL_GREEN ||
3654 srcFormat == GL_BLUE ||
3655 srcFormat == GL_ALPHA ||
3656 srcFormat == GL_LUMINANCE ||
3657 srcFormat == GL_LUMINANCE_ALPHA ||
3658 srcFormat == GL_INTENSITY ||
3659 srcFormat == GL_RGB ||
3660 srcFormat == GL_BGR ||
3661 srcFormat == GL_RGBA ||
3662 srcFormat == GL_BGRA ||
3663 srcFormat == GL_ABGR_EXT ||
3664 srcFormat == GL_COLOR_INDEX);
3666 ASSERT(srcType == GL_BITMAP ||
3667 srcType == GL_UNSIGNED_BYTE ||
3668 srcType == GL_BYTE ||
3669 srcType == GL_UNSIGNED_SHORT ||
3670 srcType == GL_SHORT ||
3671 srcType == GL_UNSIGNED_INT ||
3672 srcType == GL_INT ||
3673 srcType == GL_HALF_FLOAT_ARB ||
3674 srcType == GL_FLOAT ||
3675 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
3676 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
3677 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
3678 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
3679 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
3680 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
3681 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
3682 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
3683 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
3684 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
3685 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
3686 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
3688 /* Try simple cases first */
3689 if (transferOps == 0) {
3690 if (srcType == CHAN_TYPE) {
3691 if (dstFormat == GL_RGBA) {
3692 if (srcFormat == GL_RGBA) {
3693 _mesa_memcpy( dest, source, n * 4 * sizeof(GLchan) );
3696 else if (srcFormat == GL_RGB) {
3698 const GLchan *src = (const GLchan *) source;
3700 for (i = 0; i < n; i++) {
3711 else if (dstFormat == GL_RGB) {
3712 if (srcFormat == GL_RGB) {
3713 _mesa_memcpy( dest, source, n * 3 * sizeof(GLchan) );
3716 else if (srcFormat == GL_RGBA) {
3718 const GLchan *src = (const GLchan *) source;
3720 for (i = 0; i < n; i++) {
3730 else if (dstFormat == srcFormat) {
3731 GLint comps = _mesa_components_in_format(srcFormat);
3733 _mesa_memcpy( dest, source, n * comps * sizeof(GLchan) );
3738 * Common situation, loading 8bit RGBA/RGB source images
3739 * into 16/32 bit destination. (OSMesa16/32)
3741 else if (srcType == GL_UNSIGNED_BYTE) {
3742 if (dstFormat == GL_RGBA) {
3743 if (srcFormat == GL_RGB) {
3745 const GLubyte *src = (const GLubyte *) source;
3747 for (i = 0; i < n; i++) {
3748 dst[0] = UBYTE_TO_CHAN(src[0]);
3749 dst[1] = UBYTE_TO_CHAN(src[1]);
3750 dst[2] = UBYTE_TO_CHAN(src[2]);
3757 else if (srcFormat == GL_RGBA) {
3759 const GLubyte *src = (const GLubyte *) source;
3761 for (i = 0; i < n; i++) {
3762 dst[0] = UBYTE_TO_CHAN(src[0]);
3763 dst[1] = UBYTE_TO_CHAN(src[1]);
3764 dst[2] = UBYTE_TO_CHAN(src[2]);
3765 dst[3] = UBYTE_TO_CHAN(src[3]);
3772 else if (dstFormat == GL_RGB) {
3773 if (srcFormat == GL_RGB) {
3775 const GLubyte *src = (const GLubyte *) source;
3777 for (i = 0; i < n; i++) {
3778 dst[0] = UBYTE_TO_CHAN(src[0]);
3779 dst[1] = UBYTE_TO_CHAN(src[1]);
3780 dst[2] = UBYTE_TO_CHAN(src[2]);
3786 else if (srcFormat == GL_RGBA) {
3788 const GLubyte *src = (const GLubyte *) source;
3790 for (i = 0; i < n; i++) {
3791 dst[0] = UBYTE_TO_CHAN(src[0]);
3792 dst[1] = UBYTE_TO_CHAN(src[1]);
3793 dst[2] = UBYTE_TO_CHAN(src[2]);
3804 /* general solution begins here */
3806 GLint dstComponents;
3807 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
3808 GLint dstLuminanceIndex, dstIntensityIndex;
3809 GLfloat rgba[MAX_WIDTH][4];
3811 dstComponents = _mesa_components_in_format( dstFormat );
3812 /* source & dest image formats should have been error checked by now */
3813 assert(dstComponents > 0);
3816 * Extract image data and convert to RGBA floats
3818 assert(n <= MAX_WIDTH);
3819 if (srcFormat == GL_COLOR_INDEX) {
3820 GLuint indexes[MAX_WIDTH];
3821 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
3824 if (dstFormat == GL_COLOR_INDEX) {
3826 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
3827 /* convert to GLchan and return */
3828 for (i = 0; i < n; i++) {
3829 dest[i] = (GLchan) (indexes[i] & 0xff);
3834 /* Convert indexes to RGBA */
3835 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
3836 shift_and_offset_ci(ctx, n, indexes);
3838 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
3841 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
3842 * with color indexes.
3844 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
3847 /* non-color index data */
3848 extract_float_rgba(n, rgba, srcFormat, srcType, source,
3849 srcPacking->SwapBytes);
3852 /* Need to clamp if returning GLubytes or GLushorts */
3853 #if CHAN_TYPE != GL_FLOAT
3854 transferOps |= IMAGE_CLAMP_BIT;
3858 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
3861 /* Now determine which color channels we need to produce.
3862 * And determine the dest index (offset) within each color tuple.
3864 switch (dstFormat) {
3867 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3868 dstLuminanceIndex = dstIntensityIndex = -1;
3871 dstLuminanceIndex = 0;
3872 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3873 dstIntensityIndex = -1;
3875 case GL_LUMINANCE_ALPHA:
3876 dstLuminanceIndex = 0;
3878 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
3879 dstIntensityIndex = -1;
3882 dstIntensityIndex = 0;
3883 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
3884 dstLuminanceIndex = -1;
3890 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
3897 dstLuminanceIndex = dstIntensityIndex = -1;
3900 _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_chan_span()");
3905 /* Now return the GLchan data in the requested dstFormat */
3907 if (dstRedIndex >= 0) {
3910 for (i = 0; i < n; i++) {
3911 CLAMPED_FLOAT_TO_CHAN(dst[dstRedIndex], rgba[i][RCOMP]);
3912 dst += dstComponents;
3916 if (dstGreenIndex >= 0) {
3919 for (i = 0; i < n; i++) {
3920 CLAMPED_FLOAT_TO_CHAN(dst[dstGreenIndex], rgba[i][GCOMP]);
3921 dst += dstComponents;
3925 if (dstBlueIndex >= 0) {
3928 for (i = 0; i < n; i++) {
3929 CLAMPED_FLOAT_TO_CHAN(dst[dstBlueIndex], rgba[i][BCOMP]);
3930 dst += dstComponents;
3934 if (dstAlphaIndex >= 0) {
3937 for (i = 0; i < n; i++) {
3938 CLAMPED_FLOAT_TO_CHAN(dst[dstAlphaIndex], rgba[i][ACOMP]);
3939 dst += dstComponents;
3943 if (dstIntensityIndex >= 0) {
3946 assert(dstIntensityIndex == 0);
3947 assert(dstComponents == 1);
3948 for (i = 0; i < n; i++) {
3949 /* Intensity comes from red channel */
3950 CLAMPED_FLOAT_TO_CHAN(dst[i], rgba[i][RCOMP]);
3954 if (dstLuminanceIndex >= 0) {
3957 assert(dstLuminanceIndex == 0);
3958 for (i = 0; i < n; i++) {
3959 /* Luminance comes from red channel */
3960 CLAMPED_FLOAT_TO_CHAN(dst[0], rgba[i][RCOMP]);
3961 dst += dstComponents;
3969 * Same as _mesa_unpack_color_span_chan(), but return GLfloat data
3970 * instead of GLchan.
3973 _mesa_unpack_color_span_float( GLcontext *ctx,
3974 GLuint n, GLenum dstFormat, GLfloat dest[],
3975 GLenum srcFormat, GLenum srcType,
3976 const GLvoid *source,
3977 const struct gl_pixelstore_attrib *srcPacking,
3978 GLbitfield transferOps )
3980 ASSERT(dstFormat == GL_ALPHA ||
3981 dstFormat == GL_LUMINANCE ||
3982 dstFormat == GL_LUMINANCE_ALPHA ||
3983 dstFormat == GL_INTENSITY ||
3984 dstFormat == GL_RGB ||
3985 dstFormat == GL_RGBA ||
3986 dstFormat == GL_COLOR_INDEX);
3988 ASSERT(srcFormat == GL_RED ||
3989 srcFormat == GL_GREEN ||
3990 srcFormat == GL_BLUE ||
3991 srcFormat == GL_ALPHA ||
3992 srcFormat == GL_LUMINANCE ||
3993 srcFormat == GL_LUMINANCE_ALPHA ||
3994 srcFormat == GL_INTENSITY ||
3995 srcFormat == GL_RGB ||
3996 srcFormat == GL_BGR ||
3997 srcFormat == GL_RGBA ||
3998 srcFormat == GL_BGRA ||
3999 srcFormat == GL_ABGR_EXT ||
4000 srcFormat == GL_COLOR_INDEX);
4002 ASSERT(srcType == GL_BITMAP ||
4003 srcType == GL_UNSIGNED_BYTE ||
4004 srcType == GL_BYTE ||
4005 srcType == GL_UNSIGNED_SHORT ||
4006 srcType == GL_SHORT ||
4007 srcType == GL_UNSIGNED_INT ||
4008 srcType == GL_INT ||
4009 srcType == GL_HALF_FLOAT_ARB ||
4010 srcType == GL_FLOAT ||
4011 srcType == GL_UNSIGNED_BYTE_3_3_2 ||
4012 srcType == GL_UNSIGNED_BYTE_2_3_3_REV ||
4013 srcType == GL_UNSIGNED_SHORT_5_6_5 ||
4014 srcType == GL_UNSIGNED_SHORT_5_6_5_REV ||
4015 srcType == GL_UNSIGNED_SHORT_4_4_4_4 ||
4016 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV ||
4017 srcType == GL_UNSIGNED_SHORT_5_5_5_1 ||
4018 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV ||
4019 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
4020 srcType == GL_UNSIGNED_INT_8_8_8_8_REV ||
4021 srcType == GL_UNSIGNED_INT_10_10_10_2 ||
4022 srcType == GL_UNSIGNED_INT_2_10_10_10_REV);
4024 /* general solution, no special cases, yet */
4026 GLint dstComponents;
4027 GLint dstRedIndex, dstGreenIndex, dstBlueIndex, dstAlphaIndex;
4028 GLint dstLuminanceIndex, dstIntensityIndex;
4029 GLfloat rgba[MAX_WIDTH][4];
4031 dstComponents = _mesa_components_in_format( dstFormat );
4032 /* source & dest image formats should have been error checked by now */
4033 assert(dstComponents > 0);
4036 * Extract image data and convert to RGBA floats
4038 assert(n <= MAX_WIDTH);
4039 if (srcFormat == GL_COLOR_INDEX) {
4040 GLuint indexes[MAX_WIDTH];
4041 extract_uint_indexes(n, indexes, srcFormat, srcType, source,
4044 if (dstFormat == GL_COLOR_INDEX) {
4046 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4047 /* convert to GLchan and return */
4048 for (i = 0; i < n; i++) {
4049 dest[i] = (GLchan) (indexes[i] & 0xff);
4054 /* Convert indexes to RGBA */
4055 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4056 shift_and_offset_ci(ctx, n, indexes);
4058 _mesa_map_ci_to_rgba(ctx, n, indexes, rgba);
4061 /* Don't do RGBA scale/bias or RGBA->RGBA mapping if starting
4062 * with color indexes.
4064 transferOps &= ~(IMAGE_SCALE_BIAS_BIT | IMAGE_MAP_COLOR_BIT);
4067 /* non-color index data */
4068 extract_float_rgba(n, rgba, srcFormat, srcType, source,
4069 srcPacking->SwapBytes);
4073 _mesa_apply_rgba_transfer_ops(ctx, transferOps, n, rgba);
4076 /* Now determine which color channels we need to produce.
4077 * And determine the dest index (offset) within each color tuple.
4079 switch (dstFormat) {
4082 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
4083 dstLuminanceIndex = dstIntensityIndex = -1;
4086 dstLuminanceIndex = 0;
4087 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
4088 dstIntensityIndex = -1;
4090 case GL_LUMINANCE_ALPHA:
4091 dstLuminanceIndex = 0;
4093 dstRedIndex = dstGreenIndex = dstBlueIndex = -1;
4094 dstIntensityIndex = -1;
4097 dstIntensityIndex = 0;
4098 dstRedIndex = dstGreenIndex = dstBlueIndex = dstAlphaIndex = -1;
4099 dstLuminanceIndex = -1;
4105 dstAlphaIndex = dstLuminanceIndex = dstIntensityIndex = -1;
4112 dstLuminanceIndex = dstIntensityIndex = -1;
4115 _mesa_problem(ctx, "bad dstFormat in _mesa_unpack_color_span_float()");
4119 /* Now pack results in the requested dstFormat */
4120 if (dstRedIndex >= 0) {
4121 GLfloat *dst = dest;
4123 for (i = 0; i < n; i++) {
4124 dst[dstRedIndex] = rgba[i][RCOMP];
4125 dst += dstComponents;
4129 if (dstGreenIndex >= 0) {
4130 GLfloat *dst = dest;
4132 for (i = 0; i < n; i++) {
4133 dst[dstGreenIndex] = rgba[i][GCOMP];
4134 dst += dstComponents;
4138 if (dstBlueIndex >= 0) {
4139 GLfloat *dst = dest;
4141 for (i = 0; i < n; i++) {
4142 dst[dstBlueIndex] = rgba[i][BCOMP];
4143 dst += dstComponents;
4147 if (dstAlphaIndex >= 0) {
4148 GLfloat *dst = dest;
4150 for (i = 0; i < n; i++) {
4151 dst[dstAlphaIndex] = rgba[i][ACOMP];
4152 dst += dstComponents;
4156 if (dstIntensityIndex >= 0) {
4157 GLfloat *dst = dest;
4159 assert(dstIntensityIndex == 0);
4160 assert(dstComponents == 1);
4161 for (i = 0; i < n; i++) {
4162 /* Intensity comes from red channel */
4163 dst[i] = rgba[i][RCOMP];
4167 if (dstLuminanceIndex >= 0) {
4168 GLfloat *dst = dest;
4170 assert(dstLuminanceIndex == 0);
4171 for (i = 0; i < n; i++) {
4172 /* Luminance comes from red channel */
4173 dst[0] = rgba[i][RCOMP];
4174 dst += dstComponents;
4181 * Similar to _mesa_unpack_color_span_float(), but for dudv data instead of rgba,
4182 * directly return GLbyte data, no transfer ops apply.
4185 _mesa_unpack_dudv_span_byte( GLcontext *ctx,
4186 GLuint n, GLenum dstFormat, GLbyte dest[],
4187 GLenum srcFormat, GLenum srcType,
4188 const GLvoid *source,
4189 const struct gl_pixelstore_attrib *srcPacking,
4190 GLbitfield transferOps )
4192 ASSERT(dstFormat == GL_DUDV_ATI);
4193 ASSERT(srcFormat == GL_DUDV_ATI);
4195 ASSERT(srcType == GL_UNSIGNED_BYTE ||
4196 srcType == GL_BYTE ||
4197 srcType == GL_UNSIGNED_SHORT ||
4198 srcType == GL_SHORT ||
4199 srcType == GL_UNSIGNED_INT ||
4200 srcType == GL_INT ||
4201 srcType == GL_HALF_FLOAT_ARB ||
4202 srcType == GL_FLOAT);
4204 /* general solution */
4206 GLint dstComponents;
4207 GLfloat rgba[MAX_WIDTH][4];
4211 dstComponents = _mesa_components_in_format( dstFormat );
4212 /* source & dest image formats should have been error checked by now */
4213 assert(dstComponents > 0);
4216 * Extract image data and convert to RGBA floats
4218 assert(n <= MAX_WIDTH);
4219 extract_float_rgba(n, rgba, srcFormat, srcType, source,
4220 srcPacking->SwapBytes);
4223 /* Now determine which color channels we need to produce.
4224 * And determine the dest index (offset) within each color tuple.
4227 /* Now pack results in the requested dstFormat */
4228 for (i = 0; i < n; i++) {
4229 /* not sure - need clamp[-1,1] here? */
4230 dst[0] = FLOAT_TO_BYTE(rgba[i][RCOMP]);
4231 dst[1] = FLOAT_TO_BYTE(rgba[i][GCOMP]);
4232 dst += dstComponents;
4238 * Unpack a row of color index data from a client buffer according to
4239 * the pixel unpacking parameters.
4240 * This is (or will be) used by glDrawPixels, glTexImage[123]D, etc.
4242 * Args: ctx - the context
4243 * n - number of pixels
4244 * dstType - destination data type
4245 * dest - destination array
4246 * srcType - source pixel type
4247 * source - source data pointer
4248 * srcPacking - pixel unpacking parameters
4249 * transferOps - the pixel transfer operations to apply
4252 _mesa_unpack_index_span( const GLcontext *ctx, GLuint n,
4253 GLenum dstType, GLvoid *dest,
4254 GLenum srcType, const GLvoid *source,
4255 const struct gl_pixelstore_attrib *srcPacking,
4256 GLbitfield transferOps )
4258 ASSERT(srcType == GL_BITMAP ||
4259 srcType == GL_UNSIGNED_BYTE ||
4260 srcType == GL_BYTE ||
4261 srcType == GL_UNSIGNED_SHORT ||
4262 srcType == GL_SHORT ||
4263 srcType == GL_UNSIGNED_INT ||
4264 srcType == GL_INT ||
4265 srcType == GL_HALF_FLOAT_ARB ||
4266 srcType == GL_FLOAT);
4268 ASSERT(dstType == GL_UNSIGNED_BYTE ||
4269 dstType == GL_UNSIGNED_SHORT ||
4270 dstType == GL_UNSIGNED_INT);
4273 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4276 * Try simple cases first
4278 if (transferOps == 0 && srcType == GL_UNSIGNED_BYTE
4279 && dstType == GL_UNSIGNED_BYTE) {
4280 _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4282 else if (transferOps == 0 && srcType == GL_UNSIGNED_INT
4283 && dstType == GL_UNSIGNED_INT && !srcPacking->SwapBytes) {
4284 _mesa_memcpy(dest, source, n * sizeof(GLuint));
4290 GLuint indexes[MAX_WIDTH];
4291 assert(n <= MAX_WIDTH);
4293 extract_uint_indexes(n, indexes, GL_COLOR_INDEX, srcType, source,
4297 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4299 /* convert to dest type */
4301 case GL_UNSIGNED_BYTE:
4303 GLubyte *dst = (GLubyte *) dest;
4305 for (i = 0; i < n; i++) {
4306 dst[i] = (GLubyte) (indexes[i] & 0xff);
4310 case GL_UNSIGNED_SHORT:
4312 GLuint *dst = (GLuint *) dest;
4314 for (i = 0; i < n; i++) {
4315 dst[i] = (GLushort) (indexes[i] & 0xffff);
4319 case GL_UNSIGNED_INT:
4320 _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4323 _mesa_problem(ctx, "bad dstType in _mesa_unpack_index_span");
4330 _mesa_pack_index_span( const GLcontext *ctx, GLuint n,
4331 GLenum dstType, GLvoid *dest, const GLuint *source,
4332 const struct gl_pixelstore_attrib *dstPacking,
4333 GLbitfield transferOps )
4335 GLuint indexes[MAX_WIDTH];
4337 ASSERT(n <= MAX_WIDTH);
4339 transferOps &= (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT);
4341 if (transferOps & (IMAGE_MAP_COLOR_BIT | IMAGE_SHIFT_OFFSET_BIT)) {
4342 /* make a copy of input */
4343 _mesa_memcpy(indexes, source, n * sizeof(GLuint));
4344 _mesa_apply_ci_transfer_ops(ctx, transferOps, n, indexes);
4349 case GL_UNSIGNED_BYTE:
4351 GLubyte *dst = (GLubyte *) dest;
4353 for (i = 0; i < n; i++) {
4354 *dst++ = (GLubyte) source[i];
4360 GLbyte *dst = (GLbyte *) dest;
4362 for (i = 0; i < n; i++) {
4363 dst[i] = (GLbyte) source[i];
4367 case GL_UNSIGNED_SHORT:
4369 GLushort *dst = (GLushort *) dest;
4371 for (i = 0; i < n; i++) {
4372 dst[i] = (GLushort) source[i];
4374 if (dstPacking->SwapBytes) {
4375 _mesa_swap2( (GLushort *) dst, n );
4381 GLshort *dst = (GLshort *) dest;
4383 for (i = 0; i < n; i++) {
4384 dst[i] = (GLshort) source[i];
4386 if (dstPacking->SwapBytes) {
4387 _mesa_swap2( (GLushort *) dst, n );
4391 case GL_UNSIGNED_INT:
4393 GLuint *dst = (GLuint *) dest;
4395 for (i = 0; i < n; i++) {
4396 dst[i] = (GLuint) source[i];
4398 if (dstPacking->SwapBytes) {
4399 _mesa_swap4( (GLuint *) dst, n );
4405 GLint *dst = (GLint *) dest;
4407 for (i = 0; i < n; i++) {
4408 dst[i] = (GLint) source[i];
4410 if (dstPacking->SwapBytes) {
4411 _mesa_swap4( (GLuint *) dst, n );
4417 GLfloat *dst = (GLfloat *) dest;
4419 for (i = 0; i < n; i++) {
4420 dst[i] = (GLfloat) source[i];
4422 if (dstPacking->SwapBytes) {
4423 _mesa_swap4( (GLuint *) dst, n );
4427 case GL_HALF_FLOAT_ARB:
4429 GLhalfARB *dst = (GLhalfARB *) dest;
4431 for (i = 0; i < n; i++) {
4432 dst[i] = _mesa_float_to_half((GLfloat) source[i]);
4434 if (dstPacking->SwapBytes) {
4435 _mesa_swap2( (GLushort *) dst, n );
4440 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4446 * Unpack a row of stencil data from a client buffer according to
4447 * the pixel unpacking parameters.
4448 * This is (or will be) used by glDrawPixels
4450 * Args: ctx - the context
4451 * n - number of pixels
4452 * dstType - destination data type
4453 * dest - destination array
4454 * srcType - source pixel type
4455 * source - source data pointer
4456 * srcPacking - pixel unpacking parameters
4457 * transferOps - apply offset/bias/lookup ops?
4460 _mesa_unpack_stencil_span( const GLcontext *ctx, GLuint n,
4461 GLenum dstType, GLvoid *dest,
4462 GLenum srcType, const GLvoid *source,
4463 const struct gl_pixelstore_attrib *srcPacking,
4464 GLbitfield transferOps )
4466 ASSERT(srcType == GL_BITMAP ||
4467 srcType == GL_UNSIGNED_BYTE ||
4468 srcType == GL_BYTE ||
4469 srcType == GL_UNSIGNED_SHORT ||
4470 srcType == GL_SHORT ||
4471 srcType == GL_UNSIGNED_INT ||
4472 srcType == GL_INT ||
4473 srcType == GL_UNSIGNED_INT_24_8_EXT ||
4474 srcType == GL_HALF_FLOAT_ARB ||
4475 srcType == GL_FLOAT);
4477 ASSERT(dstType == GL_UNSIGNED_BYTE ||
4478 dstType == GL_UNSIGNED_SHORT ||
4479 dstType == GL_UNSIGNED_INT);
4481 /* only shift and offset apply to stencil */
4482 transferOps &= IMAGE_SHIFT_OFFSET_BIT;
4485 * Try simple cases first
4487 if (transferOps == 0 &&
4488 !ctx->Pixel.MapStencilFlag &&
4489 srcType == GL_UNSIGNED_BYTE &&
4490 dstType == GL_UNSIGNED_BYTE) {
4491 _mesa_memcpy(dest, source, n * sizeof(GLubyte));
4493 else if (transferOps == 0 &&
4494 !ctx->Pixel.MapStencilFlag &&
4495 srcType == GL_UNSIGNED_INT &&
4496 dstType == GL_UNSIGNED_INT &&
4497 !srcPacking->SwapBytes) {
4498 _mesa_memcpy(dest, source, n * sizeof(GLuint));
4504 GLuint indexes[MAX_WIDTH];
4505 assert(n <= MAX_WIDTH);
4507 extract_uint_indexes(n, indexes, GL_STENCIL_INDEX, srcType, source,
4510 if (transferOps & IMAGE_SHIFT_OFFSET_BIT) {
4511 /* shift and offset indexes */
4512 shift_and_offset_ci(ctx, n, indexes);
4515 if (ctx->Pixel.MapStencilFlag) {
4516 /* Apply stencil lookup table */
4517 const GLuint mask = ctx->PixelMaps.StoS.Size - 1;
4519 for (i = 0; i < n; i++) {
4520 indexes[i] = (GLuint)ctx->PixelMaps.StoS.Map[ indexes[i] & mask ];
4524 /* convert to dest type */
4526 case GL_UNSIGNED_BYTE:
4528 GLubyte *dst = (GLubyte *) dest;
4530 for (i = 0; i < n; i++) {
4531 dst[i] = (GLubyte) (indexes[i] & 0xff);
4535 case GL_UNSIGNED_SHORT:
4537 GLuint *dst = (GLuint *) dest;
4539 for (i = 0; i < n; i++) {
4540 dst[i] = (GLushort) (indexes[i] & 0xffff);
4544 case GL_UNSIGNED_INT:
4545 _mesa_memcpy(dest, indexes, n * sizeof(GLuint));
4548 _mesa_problem(ctx, "bad dstType in _mesa_unpack_stencil_span");
4555 _mesa_pack_stencil_span( const GLcontext *ctx, GLuint n,
4556 GLenum dstType, GLvoid *dest, const GLstencil *source,
4557 const struct gl_pixelstore_attrib *dstPacking )
4559 GLstencil stencil[MAX_WIDTH];
4561 ASSERT(n <= MAX_WIDTH);
4563 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset ||
4564 ctx->Pixel.MapStencilFlag) {
4565 /* make a copy of input */
4566 _mesa_memcpy(stencil, source, n * sizeof(GLstencil));
4567 _mesa_apply_stencil_transfer_ops(ctx, n, stencil);
4572 case GL_UNSIGNED_BYTE:
4573 if (sizeof(GLstencil) == 1) {
4574 _mesa_memcpy( dest, source, n );
4577 GLubyte *dst = (GLubyte *) dest;
4580 dst[i] = (GLubyte) source[i];
4586 GLbyte *dst = (GLbyte *) dest;
4589 dst[i] = (GLbyte) (source[i] & 0x7f);
4593 case GL_UNSIGNED_SHORT:
4595 GLushort *dst = (GLushort *) dest;
4598 dst[i] = (GLushort) source[i];
4600 if (dstPacking->SwapBytes) {
4601 _mesa_swap2( (GLushort *) dst, n );
4607 GLshort *dst = (GLshort *) dest;
4610 dst[i] = (GLshort) source[i];
4612 if (dstPacking->SwapBytes) {
4613 _mesa_swap2( (GLushort *) dst, n );
4617 case GL_UNSIGNED_INT:
4619 GLuint *dst = (GLuint *) dest;
4622 dst[i] = (GLuint) source[i];
4624 if (dstPacking->SwapBytes) {
4625 _mesa_swap4( (GLuint *) dst, n );
4631 GLint *dst = (GLint *) dest;
4634 dst[i] = (GLint) source[i];
4636 if (dstPacking->SwapBytes) {
4637 _mesa_swap4( (GLuint *) dst, n );
4643 GLfloat *dst = (GLfloat *) dest;
4646 dst[i] = (GLfloat) source[i];
4648 if (dstPacking->SwapBytes) {
4649 _mesa_swap4( (GLuint *) dst, n );
4653 case GL_HALF_FLOAT_ARB:
4655 GLhalfARB *dst = (GLhalfARB *) dest;
4658 dst[i] = _mesa_float_to_half( (float) source[i] );
4660 if (dstPacking->SwapBytes) {
4661 _mesa_swap2( (GLushort *) dst, n );
4666 if (dstPacking->LsbFirst) {
4667 GLubyte *dst = (GLubyte *) dest;
4670 for (i = 0; i < n; i++) {
4673 *dst |= ((source[i] != 0) << shift);
4682 GLubyte *dst = (GLubyte *) dest;
4685 for (i = 0; i < n; i++) {
4688 *dst |= ((source[i] != 0) << shift);
4698 _mesa_problem(ctx, "bad type in _mesa_pack_index_span");
4702 #define DEPTH_VALUES(GLTYPE, GLTYPE2FLOAT) \
4705 const GLTYPE *src = (const GLTYPE *)source; \
4706 for (i = 0; i < n; i++) { \
4707 GLTYPE value = src[i]; \
4708 if (srcPacking->SwapBytes) { \
4709 if (sizeof(GLTYPE) == 2) { \
4711 } else if (sizeof(GLTYPE) == 4) { \
4715 depthValues[i] = GLTYPE2FLOAT(value); \
4721 * Unpack a row of depth/z values from memory, returning GLushort, GLuint
4722 * or GLfloat values.
4723 * The glPixelTransfer (scale/bias) params will be applied.
4725 * \param dstType one of GL_UNSIGNED_SHORT, GL_UNSIGNED_INT, GL_FLOAT
4726 * \param depthMax max value for returned GLushort or GLuint values
4727 * (ignored for GLfloat).
4730 _mesa_unpack_depth_span( const GLcontext *ctx, GLuint n,
4731 GLenum dstType, GLvoid *dest, GLuint depthMax,
4732 GLenum srcType, const GLvoid *source,
4733 const struct gl_pixelstore_attrib *srcPacking )
4735 GLfloat depthTemp[MAX_WIDTH], *depthValues;
4736 GLboolean needClamp = GL_FALSE;
4738 /* Look for special cases first.
4739 * Not only are these faster, they're less prone to numeric conversion
4740 * problems. Otherwise, converting from an int type to a float then
4741 * back to an int type can introduce errors that will show up as
4742 * artifacts in things like depth peeling which uses glCopyTexImage.
4744 if (ctx->Pixel.DepthScale == 1.0 && ctx->Pixel.DepthBias == 0.0) {
4745 if (srcType == GL_UNSIGNED_INT && dstType == GL_UNSIGNED_SHORT) {
4746 const GLuint *src = (const GLuint *) source;
4747 GLushort *dst = (GLushort *) dest;
4749 for (i = 0; i < n; i++) {
4750 dst[i] = src[i] >> 16;
4754 if (srcType == GL_UNSIGNED_SHORT
4755 && dstType == GL_UNSIGNED_INT
4756 && depthMax == 0xffffffff) {
4757 const GLushort *src = (const GLushort *) source;
4758 GLuint *dst = (GLuint *) dest;
4760 for (i = 0; i < n; i++) {
4761 dst[i] = src[i] | (src[i] << 16);
4765 if (srcType == GL_UNSIGNED_INT_24_8
4766 && dstType == GL_UNSIGNED_INT
4767 && depthMax == 0xffffff) {
4768 const GLuint *src = (const GLuint *) source;
4769 GLuint *dst = (GLuint *) dest;
4771 for (i = 0; i < n; i++) {
4772 dst[i] = src[i] >> 8;
4776 /* XXX may want to add additional cases here someday */
4779 /* general case path follows */
4781 if (dstType == GL_FLOAT) {
4782 depthValues = (GLfloat *) dest;
4785 depthValues = depthTemp;
4788 /* Convert incoming values to GLfloat. Some conversions will require
4793 DEPTH_VALUES(GLbyte, BYTE_TO_FLOAT);
4794 needClamp = GL_TRUE;
4796 case GL_UNSIGNED_BYTE:
4797 DEPTH_VALUES(GLubyte, UBYTE_TO_FLOAT);
4800 DEPTH_VALUES(GLshort, SHORT_TO_FLOAT);
4801 needClamp = GL_TRUE;
4803 case GL_UNSIGNED_SHORT:
4804 DEPTH_VALUES(GLushort, USHORT_TO_FLOAT);
4807 DEPTH_VALUES(GLint, INT_TO_FLOAT);
4808 needClamp = GL_TRUE;
4810 case GL_UNSIGNED_INT:
4811 DEPTH_VALUES(GLuint, UINT_TO_FLOAT);
4813 case GL_UNSIGNED_INT_24_8_EXT: /* GL_EXT_packed_depth_stencil */
4814 if (dstType == GL_UNSIGNED_INT_24_8_EXT &&
4815 depthMax == 0xffffff &&
4816 ctx->Pixel.DepthScale == 1.0 &&
4817 ctx->Pixel.DepthBias == 0.0) {
4818 const GLuint *src = (const GLuint *) source;
4819 GLuint *zValues = (GLuint *) dest;
4821 for (i = 0; i < n; i++) {
4822 GLuint value = src[i];
4823 if (srcPacking->SwapBytes) {
4826 zValues[i] = value & 0xffffff00;
4831 const GLuint *src = (const GLuint *) source;
4832 const GLfloat scale = 1.0f / 0xffffff;
4834 for (i = 0; i < n; i++) {
4835 GLuint value = src[i];
4836 if (srcPacking->SwapBytes) {
4839 depthValues[i] = (value >> 8) * scale;
4844 DEPTH_VALUES(GLfloat, 1*);
4845 needClamp = GL_TRUE;
4847 case GL_HALF_FLOAT_ARB:
4850 const GLhalfARB *src = (const GLhalfARB *) source;
4851 for (i = 0; i < n; i++) {
4852 GLhalfARB value = src[i];
4853 if (srcPacking->SwapBytes) {
4856 depthValues[i] = _mesa_half_to_float(value);
4858 needClamp = GL_TRUE;
4862 _mesa_problem(NULL, "bad type in _mesa_unpack_depth_span()");
4866 /* apply depth scale and bias */
4868 const GLfloat scale = ctx->Pixel.DepthScale;
4869 const GLfloat bias = ctx->Pixel.DepthBias;
4870 if (scale != 1.0 || bias != 0.0) {
4872 for (i = 0; i < n; i++) {
4873 depthValues[i] = depthValues[i] * scale + bias;
4875 needClamp = GL_TRUE;
4879 /* clamp to [0, 1] */
4882 for (i = 0; i < n; i++) {
4883 depthValues[i] = (GLfloat)CLAMP(depthValues[i], 0.0, 1.0);
4888 * Convert values to dstType
4890 if (dstType == GL_UNSIGNED_INT) {
4891 GLuint *zValues = (GLuint *) dest;
4893 if (depthMax <= 0xffffff) {
4894 /* no overflow worries */
4895 for (i = 0; i < n; i++) {
4896 zValues[i] = (GLuint) (depthValues[i] * (GLfloat) depthMax);
4900 /* need to use double precision to prevent overflow problems */
4901 for (i = 0; i < n; i++) {
4902 GLdouble z = depthValues[i] * (GLfloat) depthMax;
4903 if (z >= (GLdouble) 0xffffffff)
4904 zValues[i] = 0xffffffff;
4906 zValues[i] = (GLuint) z;
4910 else if (dstType == GL_UNSIGNED_SHORT) {
4911 GLushort *zValues = (GLushort *) dest;
4913 ASSERT(depthMax <= 0xffff);
4914 for (i = 0; i < n; i++) {
4915 zValues[i] = (GLushort) (depthValues[i] * (GLfloat) depthMax);
4919 ASSERT(dstType == GL_FLOAT);
4920 /*ASSERT(depthMax == 1.0F);*/
4926 * Pack an array of depth values. The values are floats in [0,1].
4929 _mesa_pack_depth_span( const GLcontext *ctx, GLuint n, GLvoid *dest,
4930 GLenum dstType, const GLfloat *depthSpan,
4931 const struct gl_pixelstore_attrib *dstPacking )
4933 GLfloat depthCopy[MAX_WIDTH];
4935 ASSERT(n <= MAX_WIDTH);
4937 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
4938 _mesa_memcpy(depthCopy, depthSpan, n * sizeof(GLfloat));
4939 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
4940 depthSpan = depthCopy;
4944 case GL_UNSIGNED_BYTE:
4946 GLubyte *dst = (GLubyte *) dest;
4948 for (i = 0; i < n; i++) {
4949 dst[i] = FLOAT_TO_UBYTE( depthSpan[i] );
4955 GLbyte *dst = (GLbyte *) dest;
4957 for (i = 0; i < n; i++) {
4958 dst[i] = FLOAT_TO_BYTE( depthSpan[i] );
4962 case GL_UNSIGNED_SHORT:
4964 GLushort *dst = (GLushort *) dest;
4966 for (i = 0; i < n; i++) {
4967 CLAMPED_FLOAT_TO_USHORT(dst[i], depthSpan[i]);
4969 if (dstPacking->SwapBytes) {
4970 _mesa_swap2( (GLushort *) dst, n );
4976 GLshort *dst = (GLshort *) dest;
4978 for (i = 0; i < n; i++) {
4979 dst[i] = FLOAT_TO_SHORT( depthSpan[i] );
4981 if (dstPacking->SwapBytes) {
4982 _mesa_swap2( (GLushort *) dst, n );
4986 case GL_UNSIGNED_INT:
4988 GLuint *dst = (GLuint *) dest;
4990 for (i = 0; i < n; i++) {
4991 dst[i] = FLOAT_TO_UINT( depthSpan[i] );
4993 if (dstPacking->SwapBytes) {
4994 _mesa_swap4( (GLuint *) dst, n );
5000 GLint *dst = (GLint *) dest;
5002 for (i = 0; i < n; i++) {
5003 dst[i] = FLOAT_TO_INT( depthSpan[i] );
5005 if (dstPacking->SwapBytes) {
5006 _mesa_swap4( (GLuint *) dst, n );
5012 GLfloat *dst = (GLfloat *) dest;
5014 for (i = 0; i < n; i++) {
5015 dst[i] = depthSpan[i];
5017 if (dstPacking->SwapBytes) {
5018 _mesa_swap4( (GLuint *) dst, n );
5022 case GL_HALF_FLOAT_ARB:
5024 GLhalfARB *dst = (GLhalfARB *) dest;
5026 for (i = 0; i < n; i++) {
5027 dst[i] = _mesa_float_to_half(depthSpan[i]);
5029 if (dstPacking->SwapBytes) {
5030 _mesa_swap2( (GLushort *) dst, n );
5035 _mesa_problem(ctx, "bad type in _mesa_pack_depth_span");
5042 * Pack depth and stencil values as GL_DEPTH_STENCIL/GL_UNSIGNED_INT_24_8.
5045 _mesa_pack_depth_stencil_span(const GLcontext *ctx, GLuint n, GLuint *dest,
5046 const GLfloat *depthVals,
5047 const GLstencil *stencilVals,
5048 const struct gl_pixelstore_attrib *dstPacking)
5050 GLfloat depthCopy[MAX_WIDTH];
5051 GLstencil stencilCopy[MAX_WIDTH];
5054 ASSERT(n <= MAX_WIDTH);
5056 if (ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0) {
5057 _mesa_memcpy(depthCopy, depthVals, n * sizeof(GLfloat));
5058 _mesa_scale_and_bias_depth(ctx, n, depthCopy);
5059 depthVals = depthCopy;
5062 if (ctx->Pixel.IndexShift ||
5063 ctx->Pixel.IndexOffset ||
5064 ctx->Pixel.MapStencilFlag) {
5065 _mesa_memcpy(stencilCopy, stencilVals, n * sizeof(GLstencil));
5066 _mesa_apply_stencil_transfer_ops(ctx, n, stencilCopy);
5067 stencilVals = stencilCopy;
5070 for (i = 0; i < n; i++) {
5071 GLuint z = (GLuint) (depthVals[i] * 0xffffff);
5072 dest[i] = (z << 8) | (stencilVals[i] & 0xff);
5075 if (dstPacking->SwapBytes) {
5076 _mesa_swap4(dest, n);
5084 * Unpack image data. Apply byte swapping, byte flipping (bitmap).
5085 * Return all image data in a contiguous block. This is used when we
5086 * compile glDrawPixels, glTexImage, etc into a display list. We
5087 * need a copy of the data in a standard format.
5090 _mesa_unpack_image( GLuint dimensions,
5091 GLsizei width, GLsizei height, GLsizei depth,
5092 GLenum format, GLenum type, const GLvoid *pixels,
5093 const struct gl_pixelstore_attrib *unpack )
5095 GLint bytesPerRow, compsPerRow;
5096 GLboolean flipBytes, swap2, swap4;
5099 return NULL; /* not necessarily an error */
5101 if (width <= 0 || height <= 0 || depth <= 0)
5102 return NULL; /* generate error later */
5104 if (type == GL_BITMAP) {
5105 bytesPerRow = (width + 7) >> 3;
5106 flipBytes = unpack->LsbFirst;
5107 swap2 = swap4 = GL_FALSE;
5111 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type);
5112 GLint components = _mesa_components_in_format(format);
5115 if (_mesa_type_is_packed(type))
5118 if (bytesPerPixel <= 0 || components <= 0)
5119 return NULL; /* bad format or type. generate error later */
5120 bytesPerRow = bytesPerPixel * width;
5121 bytesPerComp = bytesPerPixel / components;
5122 flipBytes = GL_FALSE;
5123 swap2 = (bytesPerComp == 2) && unpack->SwapBytes;
5124 swap4 = (bytesPerComp == 4) && unpack->SwapBytes;
5125 compsPerRow = components * width;
5126 assert(compsPerRow >= width);
5131 = (GLubyte *) _mesa_malloc(bytesPerRow * height * depth);
5135 return NULL; /* generate GL_OUT_OF_MEMORY later */
5138 for (img = 0; img < depth; img++) {
5139 for (row = 0; row < height; row++) {
5140 const GLvoid *src = _mesa_image_address(dimensions, unpack, pixels,
5141 width, height, format, type, img, row, 0);
5143 if ((type == GL_BITMAP) && (unpack->SkipPixels & 0x7)) {
5145 flipBytes = GL_FALSE;
5146 if (unpack->LsbFirst) {
5147 GLubyte srcMask = 1 << (unpack->SkipPixels & 0x7);
5148 GLubyte dstMask = 128;
5149 const GLubyte *s = src;
5152 for (i = 0; i < width; i++) {
5156 if (srcMask == 128) {
5161 srcMask = srcMask << 1;
5169 dstMask = dstMask >> 1;
5174 GLubyte srcMask = 128 >> (unpack->SkipPixels & 0x7);
5175 GLubyte dstMask = 128;
5176 const GLubyte *s = src;
5179 for (i = 0; i < width; i++) {
5188 srcMask = srcMask >> 1;
5196 dstMask = dstMask >> 1;
5202 _mesa_memcpy(dst, src, bytesPerRow);
5205 /* byte flipping/swapping */
5207 flip_bytes((GLubyte *) dst, bytesPerRow);
5210 _mesa_swap2((GLushort*) dst, compsPerRow);
5213 _mesa_swap4((GLuint*) dst, compsPerRow);
5222 #endif /* _HAVE_FULL_GL */
5227 * Convert an array of RGBA colors from one datatype to another.
5228 * NOTE: src may equal dst. In that case, we use a temporary buffer.
5231 _mesa_convert_colors(GLenum srcType, const GLvoid *src,
5232 GLenum dstType, GLvoid *dst,
5233 GLuint count, const GLubyte mask[])
5235 GLuint tempBuffer[MAX_WIDTH][4];
5236 const GLboolean useTemp = (src == dst);
5238 ASSERT(srcType != dstType);
5241 case GL_UNSIGNED_BYTE:
5242 if (dstType == GL_UNSIGNED_SHORT) {
5243 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
5244 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
5246 for (i = 0; i < count; i++) {
5247 if (!mask || mask[i]) {
5248 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]);
5249 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]);
5250 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]);
5251 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]);
5255 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
5258 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src;
5259 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
5261 ASSERT(dstType == GL_FLOAT);
5262 for (i = 0; i < count; i++) {
5263 if (!mask || mask[i]) {
5264 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]);
5265 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]);
5266 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]);
5267 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]);
5271 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
5274 case GL_UNSIGNED_SHORT:
5275 if (dstType == GL_UNSIGNED_BYTE) {
5276 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
5277 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
5279 for (i = 0; i < count; i++) {
5280 if (!mask || mask[i]) {
5281 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]);
5282 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]);
5283 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]);
5284 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]);
5288 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
5291 const GLushort (*src2)[4] = (const GLushort (*)[4]) src;
5292 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst);
5294 ASSERT(dstType == GL_FLOAT);
5295 for (i = 0; i < count; i++) {
5296 if (!mask || mask[i]) {
5297 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]);
5298 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]);
5299 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]);
5300 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]);
5304 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat));
5308 if (dstType == GL_UNSIGNED_BYTE) {
5309 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
5310 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst);
5312 for (i = 0; i < count; i++) {
5313 if (!mask || mask[i]) {
5314 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][RCOMP], src4[i][RCOMP]);
5315 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][GCOMP], src4[i][GCOMP]);
5316 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][BCOMP], src4[i][BCOMP]);
5317 UNCLAMPED_FLOAT_TO_UBYTE(dst1[i][ACOMP], src4[i][ACOMP]);
5321 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte));
5324 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src;
5325 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst);
5327 ASSERT(dstType == GL_UNSIGNED_SHORT);
5328 for (i = 0; i < count; i++) {
5329 if (!mask || mask[i]) {
5330 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]);
5331 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]);
5332 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]);
5333 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]);
5337 _mesa_memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort));
5341 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors");
5349 * Perform basic clipping for glDrawPixels. The image's position and size
5350 * and the unpack SkipPixels and SkipRows are adjusted so that the image
5351 * region is entirely within the window and scissor bounds.
5352 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
5353 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
5354 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
5356 * \return GL_TRUE if image is ready for drawing or
5357 * GL_FALSE if image was completely clipped away (draw nothing)
5360 _mesa_clip_drawpixels(const GLcontext *ctx,
5361 GLint *destX, GLint *destY,
5362 GLsizei *width, GLsizei *height,
5363 struct gl_pixelstore_attrib *unpack)
5365 const GLframebuffer *buffer = ctx->DrawBuffer;
5367 if (unpack->RowLength == 0) {
5368 unpack->RowLength = *width;
5371 ASSERT(ctx->Pixel.ZoomX == 1.0F);
5372 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F);
5375 if (*destX < buffer->_Xmin) {
5376 unpack->SkipPixels += (buffer->_Xmin - *destX);
5377 *width -= (buffer->_Xmin - *destX);
5378 *destX = buffer->_Xmin;
5380 /* right clipping */
5381 if (*destX + *width > buffer->_Xmax)
5382 *width -= (*destX + *width - buffer->_Xmax);
5387 if (ctx->Pixel.ZoomY == 1.0F) {
5388 /* bottom clipping */
5389 if (*destY < buffer->_Ymin) {
5390 unpack->SkipRows += (buffer->_Ymin - *destY);
5391 *height -= (buffer->_Ymin - *destY);
5392 *destY = buffer->_Ymin;
5395 if (*destY + *height > buffer->_Ymax)
5396 *height -= (*destY + *height - buffer->_Ymax);
5398 else { /* upside down */
5400 if (*destY > buffer->_Ymax) {
5401 unpack->SkipRows += (*destY - buffer->_Ymax);
5402 *height -= (*destY - buffer->_Ymax);
5403 *destY = buffer->_Ymax;
5405 /* bottom clipping */
5406 if (*destY - *height < buffer->_Ymin)
5407 *height -= (buffer->_Ymin - (*destY - *height));
5408 /* adjust destY so it's the first row to write to */
5420 * Perform clipping for glReadPixels. The image's window position
5421 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
5422 * so that the image region is entirely within the window bounds.
5423 * Note: this is different from _mesa_clip_drawpixels() in that the
5424 * scissor box is ignored, and we use the bounds of the current readbuffer
5427 * \return GL_TRUE if image is ready for drawing or
5428 * GL_FALSE if image was completely clipped away (draw nothing)
5431 _mesa_clip_readpixels(const GLcontext *ctx,
5432 GLint *srcX, GLint *srcY,
5433 GLsizei *width, GLsizei *height,
5434 struct gl_pixelstore_attrib *pack)
5436 const GLframebuffer *buffer = ctx->ReadBuffer;
5438 if (pack->RowLength == 0) {
5439 pack->RowLength = *width;
5444 pack->SkipPixels += (0 - *srcX);
5445 *width -= (0 - *srcX);
5448 /* right clipping */
5449 if (*srcX + *width > (GLsizei) buffer->Width)
5450 *width -= (*srcX + *width - buffer->Width);
5455 /* bottom clipping */
5457 pack->SkipRows += (0 - *srcY);
5458 *height -= (0 - *srcY);
5462 if (*srcY + *height > (GLsizei) buffer->Height)
5463 *height -= (*srcY + *height - buffer->Height);
5473 * Do clipping for a glCopyTexSubImage call.
5474 * The framebuffer source region might extend outside the framebuffer
5475 * bounds. Clip the source region against the framebuffer bounds and
5476 * adjust the texture/dest position and size accordingly.
5478 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
5481 _mesa_clip_copytexsubimage(const GLcontext *ctx,
5482 GLint *destX, GLint *destY,
5483 GLint *srcX, GLint *srcY,
5484 GLsizei *width, GLsizei *height)
5486 const struct gl_framebuffer *fb = ctx->ReadBuffer;
5487 const GLint srcX0 = *srcX, srcY0 = *srcY;
5489 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height,
5490 srcX, srcY, width, height)) {
5491 *destX = *destX + *srcX - srcX0;
5492 *destY = *destY + *srcY - srcY0;
5504 * Clip the rectangle defined by (x, y, width, height) against the bounds
5505 * specified by [xmin, xmax) and [ymin, ymax).
5506 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
5509 _mesa_clip_to_region(GLint xmin, GLint ymin,
5510 GLint xmax, GLint ymax,
5512 GLsizei *width, GLsizei *height )
5516 *width -= (xmin - *x);
5520 /* right clipping */
5521 if (*x + *width > xmax)
5522 *width -= (*x + *width - xmax);
5527 /* bottom (or top) clipping */
5529 *height -= (ymin - *y);
5533 /* top (or bottom) clipping */
5534 if (*y + *height > ymax)
5535 *height -= (*y + *height - ymax);
5545 * Clip dst coords against Xmax (or Ymax).
5548 clip_right_or_top(GLint *srcX0, GLint *srcX1,
5549 GLint *dstX0, GLint *dstX1,
5554 if (*dstX1 > maxValue) {
5555 /* X1 outside right edge */
5556 ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */
5557 t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
5558 /* chop off [t, 1] part */
5559 ASSERT(t >= 0.0 && t <= 1.0);
5561 bias = (*srcX0 < *srcX1) ? 0.5 : -0.5;
5562 *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
5564 else if (*dstX0 > maxValue) {
5565 /* X0 outside right edge */
5566 ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */
5567 t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
5568 /* chop off [t, 1] part */
5569 ASSERT(t >= 0.0 && t <= 1.0);
5571 bias = (*srcX0 < *srcX1) ? -0.5 : 0.5;
5572 *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
5578 * Clip dst coords against Xmin (or Ymin).
5581 clip_left_or_bottom(GLint *srcX0, GLint *srcX1,
5582 GLint *dstX0, GLint *dstX1,
5587 if (*dstX0 < minValue) {
5588 /* X0 outside left edge */
5589 ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */
5590 t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0);
5591 /* chop off [0, t] part */
5592 ASSERT(t >= 0.0 && t <= 1.0);
5594 bias = (*srcX0 < *srcX1) ? 0.5 : -0.5; /* flipped??? */
5595 *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias);
5597 else if (*dstX1 < minValue) {
5598 /* X1 outside left edge */
5599 ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */
5600 t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1);
5601 /* chop off [0, t] part */
5602 ASSERT(t >= 0.0 && t <= 1.0);
5604 bias = (*srcX0 < *srcX1) ? 0.5 : -0.5;
5605 *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias);
5611 * Do clipping of blit src/dest rectangles.
5612 * The dest rect is clipped against both the buffer bounds and scissor bounds.
5613 * The src rect is just clipped against the buffer bounds.
5615 * When either the src or dest rect is clipped, the other is also clipped
5618 * Note that X0 need not be less than X1 (same for Y) for either the source
5619 * and dest rects. That makes the clipping a little trickier.
5621 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
5624 _mesa_clip_blit(GLcontext *ctx,
5625 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1,
5626 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1)
5628 const GLint srcXmin = 0;
5629 const GLint srcXmax = ctx->ReadBuffer->Width;
5630 const GLint srcYmin = 0;
5631 const GLint srcYmax = ctx->ReadBuffer->Height;
5633 /* these include scissor bounds */
5634 const GLint dstXmin = ctx->DrawBuffer->_Xmin;
5635 const GLint dstXmax = ctx->DrawBuffer->_Xmax;
5636 const GLint dstYmin = ctx->DrawBuffer->_Ymin;
5637 const GLint dstYmax = ctx->DrawBuffer->_Ymax;
5640 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
5641 *srcX0, *srcX1, *dstX0, *dstX1);
5642 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
5643 *srcY0, *srcY1, *dstY0, *dstY1);
5646 /* trivial rejection tests */
5647 if (*dstX0 == *dstX1)
5648 return GL_FALSE; /* no width */
5649 if (*dstX0 <= dstXmin && *dstX1 <= dstXmin)
5650 return GL_FALSE; /* totally out (left) of bounds */
5651 if (*dstX0 >= dstXmax && *dstX1 >= dstXmax)
5652 return GL_FALSE; /* totally out (right) of bounds */
5654 if (*dstY0 == *dstY1)
5656 if (*dstY0 <= dstYmin && *dstY1 <= dstYmin)
5658 if (*dstY0 >= dstYmax && *dstY1 >= dstYmax)
5661 if (*srcX0 == *srcX1)
5663 if (*srcX0 <= srcXmin && *srcX1 <= srcXmin)
5665 if (*srcX0 >= srcXmax && *srcX1 >= srcXmax)
5668 if (*srcY0 == *srcY1)
5670 if (*srcY0 <= srcYmin && *srcY1 <= srcYmin)
5672 if (*srcY0 >= srcYmax && *srcY1 >= srcYmax)
5678 clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax);
5679 clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax);
5680 clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin);
5681 clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin);
5684 * src clip (just swap src/dst values from above)
5686 clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax);
5687 clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax);
5688 clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin);
5689 clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin);
5692 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
5693 *srcX0, *srcX1, *dstX0, *dstX1);
5694 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
5695 *srcY0, *srcY1, *dstY0, *dstY1);
5698 ASSERT(*dstX0 >= dstXmin);
5699 ASSERT(*dstX0 <= dstXmax);
5700 ASSERT(*dstX1 >= dstXmin);
5701 ASSERT(*dstX1 <= dstXmax);
5703 ASSERT(*dstY0 >= dstYmin);
5704 ASSERT(*dstY0 <= dstYmax);
5705 ASSERT(*dstY1 >= dstYmin);
5706 ASSERT(*dstY1 <= dstYmax);
5708 ASSERT(*srcX0 >= srcXmin);
5709 ASSERT(*srcX0 <= srcXmax);
5710 ASSERT(*srcX1 >= srcXmin);
5711 ASSERT(*srcX1 <= srcXmax);
5713 ASSERT(*srcY0 >= srcYmin);
5714 ASSERT(*srcY0 <= srcYmax);
5715 ASSERT(*srcY1 >= srcYmin);
5716 ASSERT(*srcY1 <= srcYmax);