image.c revision 60a249d009acec34bd61e12f01caf7bdf87e895c
1/* $Id: image.c,v 1.4 1999/10/10 12:54:04 brianp Exp $ */ 2 3/* 4 * Mesa 3-D graphics library 5 * Version: 3.1 6 * 7 * Copyright (C) 1999 Brian Paul All Rights Reserved. 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a 10 * copy of this software and associated documentation files (the "Software"), 11 * to deal in the Software without restriction, including without limitation 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 * and/or sell copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included 17 * in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 27 28 29#ifdef PC_HEADER 30#include "all.h" 31#else 32#ifndef XFree86Server 33#include <assert.h> 34#include <stdlib.h> 35#include <string.h> 36#else 37#include "GL/xf86glx.h" 38#endif 39#include "context.h" 40#include "image.h" 41#include "macros.h" 42#include "mmath.h" 43#include "pixel.h" 44#include "types.h" 45#ifdef XFree86Server 46#include "GL/xf86glx.h" 47#endif 48#endif 49 50 51 52/* 53 * Flip the 8 bits in each byte of the given array. 54 */ 55void gl_flip_bytes( GLubyte *p, GLuint n ) 56{ 57 register GLuint i, a, b; 58 59 for (i=0;i<n;i++) { 60 b = (GLuint) p[i]; 61 a = ((b & 0x01) << 7) | 62 ((b & 0x02) << 5) | 63 ((b & 0x04) << 3) | 64 ((b & 0x08) << 1) | 65 ((b & 0x10) >> 1) | 66 ((b & 0x20) >> 3) | 67 ((b & 0x40) >> 5) | 68 ((b & 0x80) >> 7); 69 p[i] = (GLubyte) a; 70 } 71} 72 73 74/* 75 * Flip the order of the 2 bytes in each word in the given array. 76 */ 77void gl_swap2( GLushort *p, GLuint n ) 78{ 79 register GLuint i; 80 81 for (i=0;i<n;i++) { 82 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00); 83 } 84} 85 86 87 88/* 89 * Flip the order of the 4 bytes in each word in the given array. 90 */ 91void gl_swap4( GLuint *p, GLuint n ) 92{ 93 register GLuint i, a, b; 94 95 for (i=0;i<n;i++) { 96 b = p[i]; 97 a = (b >> 24) 98 | ((b >> 8) & 0xff00) 99 | ((b << 8) & 0xff0000) 100 | ((b << 24) & 0xff000000); 101 p[i] = a; 102 } 103} 104 105 106 107 108/* 109 * Return the size, in bytes, of the given GL datatype. 110 * Return 0 if GL_BITMAP. 111 * Return -1 if invalid type enum. 112 */ 113GLint gl_sizeof_type( GLenum type ) 114{ 115 switch (type) { 116 case GL_BITMAP: 117 return 0; 118 case GL_UNSIGNED_BYTE: 119 return sizeof(GLubyte); 120 case GL_BYTE: 121 return sizeof(GLbyte); 122 case GL_UNSIGNED_SHORT: 123 return sizeof(GLushort); 124 case GL_SHORT: 125 return sizeof(GLshort); 126 case GL_UNSIGNED_INT: 127 return sizeof(GLuint); 128 case GL_INT: 129 return sizeof(GLint); 130 case GL_FLOAT: 131 return sizeof(GLfloat); 132 default: 133 return -1; 134 } 135} 136 137 138/* 139 * Same as gl_sizeof_packed_type() but we also accept the 140 * packed pixel format datatypes. 141 */ 142GLint gl_sizeof_packed_type( GLenum type ) 143{ 144 switch (type) { 145 case GL_BITMAP: 146 return 0; 147 case GL_UNSIGNED_BYTE: 148 return sizeof(GLubyte); 149 case GL_BYTE: 150 return sizeof(GLbyte); 151 case GL_UNSIGNED_SHORT: 152 return sizeof(GLushort); 153 case GL_SHORT: 154 return sizeof(GLshort); 155 case GL_UNSIGNED_INT: 156 return sizeof(GLuint); 157 case GL_INT: 158 return sizeof(GLint); 159 case GL_FLOAT: 160 return sizeof(GLfloat); 161 case GL_UNSIGNED_BYTE_3_3_2: 162 return sizeof(GLubyte); 163 case GL_UNSIGNED_BYTE_2_3_3_REV: 164 return sizeof(GLubyte); 165 case GL_UNSIGNED_SHORT_5_6_5: 166 return sizeof(GLshort); 167 case GL_UNSIGNED_SHORT_5_6_5_REV: 168 return sizeof(GLshort); 169 case GL_UNSIGNED_SHORT_4_4_4_4: 170 return sizeof(GLshort); 171 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 172 return sizeof(GLshort); 173 case GL_UNSIGNED_SHORT_5_5_5_1: 174 return sizeof(GLshort); 175 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 176 return sizeof(GLshort); 177 case GL_UNSIGNED_INT_8_8_8_8: 178 return sizeof(GLuint); 179 case GL_UNSIGNED_INT_8_8_8_8_REV: 180 return sizeof(GLuint); 181 case GL_UNSIGNED_INT_10_10_10_2: 182 return sizeof(GLuint); 183 case GL_UNSIGNED_INT_2_10_10_10_REV: 184 return sizeof(GLuint); 185 default: 186 return -1; 187 } 188} 189 190 191 192/* 193 * Return the number of components in a GL enum pixel type. 194 * Return -1 if bad format. 195 */ 196GLint gl_components_in_format( GLenum format ) 197{ 198 switch (format) { 199 case GL_COLOR_INDEX: 200 case GL_COLOR_INDEX1_EXT: 201 case GL_COLOR_INDEX2_EXT: 202 case GL_COLOR_INDEX4_EXT: 203 case GL_COLOR_INDEX8_EXT: 204 case GL_COLOR_INDEX12_EXT: 205 case GL_COLOR_INDEX16_EXT: 206 case GL_STENCIL_INDEX: 207 case GL_DEPTH_COMPONENT: 208 case GL_RED: 209 case GL_GREEN: 210 case GL_BLUE: 211 case GL_ALPHA: 212 case GL_LUMINANCE: 213 return 1; 214 case GL_LUMINANCE_ALPHA: 215 return 2; 216 case GL_RGB: 217 return 3; 218 case GL_RGBA: 219 return 4; 220 case GL_BGR: 221 return 3; 222 case GL_BGRA: 223 return 4; 224 case GL_ABGR_EXT: 225 return 4; 226 default: 227 return -1; 228 } 229} 230 231 232/* 233 * Return bytes per pixel for given format and type 234 * Return -1 if bad format or type. 235 */ 236GLint gl_bytes_per_pixel( GLenum format, GLenum type ) 237{ 238 GLint comps = gl_components_in_format( format ); 239 if (comps < 0) 240 return -1; 241 242 switch (type) { 243 case GL_BITMAP: 244 return 0; /* special case */ 245 case GL_BYTE: 246 case GL_UNSIGNED_BYTE: 247 return comps * sizeof(GLubyte); 248 case GL_SHORT: 249 case GL_UNSIGNED_SHORT: 250 return comps * sizeof(GLshort); 251 case GL_INT: 252 case GL_UNSIGNED_INT: 253 return comps * sizeof(GLint); 254 case GL_FLOAT: 255 return comps * sizeof(GLfloat); 256 case GL_UNSIGNED_BYTE_3_3_2: 257 case GL_UNSIGNED_BYTE_2_3_3_REV: 258 if (format == GL_RGB || format == GL_BGR) 259 return sizeof(GLubyte); 260 else 261 return -1; /* error */ 262 case GL_UNSIGNED_SHORT_5_6_5: 263 case GL_UNSIGNED_SHORT_5_6_5_REV: 264 if (format == GL_RGB || format == GL_BGR) 265 return sizeof(GLshort); 266 else 267 return -1; /* error */ 268 case GL_UNSIGNED_SHORT_4_4_4_4: 269 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 270 case GL_UNSIGNED_SHORT_5_5_5_1: 271 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 272 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT) 273 return sizeof(GLushort); 274 else 275 return -1; 276 case GL_UNSIGNED_INT_8_8_8_8: 277 case GL_UNSIGNED_INT_8_8_8_8_REV: 278 case GL_UNSIGNED_INT_10_10_10_2: 279 case GL_UNSIGNED_INT_2_10_10_10_REV: 280 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT) 281 return sizeof(GLuint); 282 else 283 return -1; 284 default: 285 return -1; 286 } 287} 288 289 290/* 291 * Test if the given pixel format and type are legal. 292 * Return GL_TRUE for legal, GL_FALSE for illegal. 293 */ 294GLboolean gl_is_legal_format_and_type( GLenum format, GLenum type ) 295{ 296 switch (format) { 297 case GL_COLOR_INDEX: 298 case GL_STENCIL_INDEX: 299 switch (type) { 300 case GL_BITMAP: 301 case GL_BYTE: 302 case GL_UNSIGNED_BYTE: 303 case GL_SHORT: 304 case GL_UNSIGNED_SHORT: 305 case GL_INT: 306 case GL_UNSIGNED_INT: 307 case GL_FLOAT: 308 return GL_TRUE; 309 default: 310 return GL_FALSE; 311 } 312 case GL_RED: 313 case GL_GREEN: 314 case GL_BLUE: 315 case GL_ALPHA: 316 case GL_LUMINANCE: 317 case GL_LUMINANCE_ALPHA: 318 case GL_DEPTH_COMPONENT: 319 case GL_BGR: 320 switch (type) { 321 case GL_BYTE: 322 case GL_UNSIGNED_BYTE: 323 case GL_SHORT: 324 case GL_UNSIGNED_SHORT: 325 case GL_INT: 326 case GL_UNSIGNED_INT: 327 case GL_FLOAT: 328 return GL_TRUE; 329 default: 330 return GL_FALSE; 331 } 332 case GL_RGB: 333 switch (type) { 334 case GL_BYTE: 335 case GL_UNSIGNED_BYTE: 336 case GL_SHORT: 337 case GL_UNSIGNED_SHORT: 338 case GL_INT: 339 case GL_UNSIGNED_INT: 340 case GL_FLOAT: 341 case GL_UNSIGNED_BYTE_3_3_2: 342 case GL_UNSIGNED_BYTE_2_3_3_REV: 343 case GL_UNSIGNED_SHORT_5_6_5: 344 case GL_UNSIGNED_SHORT_5_6_5_REV: 345 return GL_TRUE; 346 default: 347 return GL_FALSE; 348 } 349 case GL_RGBA: 350 case GL_BGRA: 351 case GL_ABGR_EXT: 352 switch (type) { 353 case GL_BYTE: 354 case GL_UNSIGNED_BYTE: 355 case GL_SHORT: 356 case GL_UNSIGNED_SHORT: 357 case GL_INT: 358 case GL_UNSIGNED_INT: 359 case GL_FLOAT: 360 case GL_UNSIGNED_SHORT_4_4_4_4: 361 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 362 case GL_UNSIGNED_SHORT_5_5_5_1: 363 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 364 case GL_UNSIGNED_INT_8_8_8_8: 365 case GL_UNSIGNED_INT_8_8_8_8_REV: 366 case GL_UNSIGNED_INT_10_10_10_2: 367 case GL_UNSIGNED_INT_2_10_10_10_REV: 368 return GL_TRUE; 369 default: 370 return GL_FALSE; 371 } 372 default: 373 ; /* fall-through */ 374 } 375 return GL_FALSE; 376} 377 378 379 380/* 381 * Return the address of a pixel in an image (actually a volume). 382 * Pixel unpacking/packing parameters are observed according to 'packing'. 383 * Input: image - start of image data 384 * width, height - size of image 385 * format - image format 386 * type - pixel component type 387 * packing - the pixelstore attributes 388 * img - which image in the volume (0 for 1D or 2D images) 389 * row, column - location of pixel in the image 390 * Return: address of pixel at (image,row,column) in image or NULL if error. 391 */ 392GLvoid *gl_pixel_addr_in_image( const struct gl_pixelstore_attrib *packing, 393 const GLvoid *image, GLsizei width, 394 GLsizei height, GLenum format, GLenum type, 395 GLint img, GLint row, GLint column ) 396{ 397 GLint alignment; /* 1, 2 or 4 */ 398 GLint pixels_per_row; 399 GLint rows_per_image; 400 GLint skiprows; 401 GLint skippixels; 402 GLint skipimages; /* for 3-D volume images */ 403 GLubyte *pixel_addr; 404 405 alignment = packing->Alignment; 406 if (packing->RowLength > 0) { 407 pixels_per_row = packing->RowLength; 408 } 409 else { 410 pixels_per_row = width; 411 } 412 if (packing->ImageHeight > 0) { 413 rows_per_image = packing->ImageHeight; 414 } 415 else { 416 rows_per_image = height; 417 } 418 skiprows = packing->SkipRows; 419 skippixels = packing->SkipPixels; 420 skipimages = packing->SkipImages; 421 422 if (type==GL_BITMAP) { 423 /* BITMAP data */ 424 GLint comp_per_pixel; /* components per pixel */ 425 GLint bytes_per_comp; /* bytes per component */ 426 GLint bytes_per_row; 427 GLint bytes_per_image; 428 429 /* Compute bytes per component */ 430 bytes_per_comp = gl_sizeof_packed_type( type ); 431 if (bytes_per_comp<0) { 432 return NULL; 433 } 434 435 /* Compute number of components per pixel */ 436 comp_per_pixel = gl_components_in_format( format ); 437 if (comp_per_pixel<0 && type != GL_BITMAP) { 438 return NULL; 439 } 440 441 bytes_per_row = alignment 442 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment ); 443 444 bytes_per_image = bytes_per_row * rows_per_image; 445 446 pixel_addr = (GLubyte *) image 447 + (skipimages + img) * bytes_per_image 448 + (skiprows + row) * bytes_per_row 449 + (skippixels + column) / 8; 450 } 451 else { 452 /* Non-BITMAP data */ 453 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image; 454 455 bytes_per_pixel = gl_bytes_per_pixel( format, type ); 456 457 /* The pixel type and format should have been error checked earlier */ 458 assert(bytes_per_pixel > 0); 459 460 bytes_per_row = pixels_per_row * bytes_per_pixel; 461 remainder = bytes_per_row % alignment; 462 if (remainder > 0) 463 bytes_per_row += (alignment - remainder); 464 465 ASSERT(bytes_per_row % alignment == 0); 466 467 bytes_per_image = bytes_per_row * rows_per_image; 468 469 /* compute final pixel address */ 470 pixel_addr = (GLubyte *) image 471 + (skipimages + img) * bytes_per_image 472 + (skiprows + row) * bytes_per_row 473 + (skippixels + column) * bytes_per_pixel; 474 } 475 476 return (GLvoid *) pixel_addr; 477} 478 479 480 481/* 482 * Allocate a new gl_image. All fields are initialized to zero. 483 */ 484static struct gl_image *alloc_image( void ) 485{ 486 return (struct gl_image *) calloc(sizeof(struct gl_image), 1); 487} 488 489 490 491/* 492 * Allocate a new gl_image with the error flag set. 493 */ 494static struct gl_image *alloc_error_image( GLint width, GLint height, 495 GLint depth, GLenum format, 496 GLenum type ) 497{ 498 struct gl_image *image = alloc_image(); 499 if (image) { 500 image->Width = width; 501 image->Height = height; 502 image->Depth = depth; 503 image->Format = format; 504 image->Type = type; 505 image->ErrorFlag = GL_TRUE; 506 } 507 return image; 508} 509 510 511 512/* 513 * Free a gl_image. 514 */ 515void gl_free_image( struct gl_image *image ) 516{ 517 if (image->Data) { 518 GL_FREE(image->Data); 519 } 520 GL_FREE(image); 521} 522 523 524 525/* 526 * Do error checking on an image. If there's an error, register it and 527 * return GL_TRUE, else return GL_FALSE. 528 */ 529GLboolean gl_image_error_test( GLcontext *ctx, const struct gl_image *image, 530 const char *msg ) 531{ 532 if (!image) { 533 gl_error( ctx, GL_OUT_OF_MEMORY, msg ); 534 return GL_TRUE; 535 } 536 if (image->Width <= 0 || image->Height <= 0 || image->Depth <= 0) { 537 gl_error( ctx, GL_INVALID_VALUE, msg ); 538 return GL_TRUE; 539 } 540 else { 541 return GL_FALSE; 542 } 543} 544 545 546 547/* 548 * Unpack a depth-buffer image storing values as GLshort, GLuint, or GLfloats. 549 * Input: type - datatype of src depth image 550 * Return pointer to a new gl_image structure. 551 * 552 * Notes: if the source image type is GLushort then the gl_image will 553 * also store GLushorts. If the src image type is GLuint then the gl_image 554 * will also store GLuints. For all other src image types the gl_image 555 * will store GLfloats. The integer cases can later be optimized. 556 */ 557static struct gl_image * 558unpack_depth_image( GLcontext *ctx, GLenum type, GLint width, GLint height, 559 const GLvoid *pixels, 560 const struct gl_pixelstore_attrib *packing) 561 562{ 563 struct gl_image *image; 564 GLfloat *fDst; 565 GLushort *sDst; 566 GLuint *iDst; 567 GLint i, j; 568 569 image = alloc_image(); 570 if (image) { 571 image->Width = width; 572 image->Height = height; 573 image->Depth = 1; 574 image->Components = 1; 575 image->Format = GL_DEPTH_COMPONENT; 576 if (type==GL_UNSIGNED_SHORT) { 577 image->Type = GL_UNSIGNED_SHORT; 578 image->Data = GL_ALLOC( width * height * sizeof(GLushort)); 579 } 580 else if (type==GL_UNSIGNED_INT) { 581 image->Type = GL_UNSIGNED_INT; 582 image->Data = GL_ALLOC( width * height * sizeof(GLuint)); 583 } 584 else { 585 image->Type = GL_FLOAT; 586 image->Data = GL_ALLOC( width * height * sizeof(GLfloat)); 587 } 588 image->RefCount = 0; 589 if (!image->Data) 590 return image; 591 } 592 else { 593 return NULL; 594 } 595 596 fDst = (GLfloat *) image->Data; 597 sDst = (GLushort *) image->Data; 598 iDst = (GLuint *) image->Data; 599 600 for (i=0;i<height;i++) { 601 GLvoid *src = gl_pixel_addr_in_image( packing, pixels, 602 width, height, 603 GL_DEPTH_COMPONENT, type, 604 0, i, 0 ); 605 if (!src) { 606 return image; 607 } 608 609 switch (type) { 610 case GL_BYTE: 611 assert(image->Type == GL_FLOAT); 612 for (j=0; j<width; j++) { 613 *fDst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]); 614 } 615 break; 616 case GL_UNSIGNED_BYTE: 617 assert(image->Type == GL_FLOAT); 618 for (j=0; j<width; j++) { 619 *fDst++ = UBYTE_TO_FLOAT(((GLubyte*)src)[j]); 620 } 621 break; 622 case GL_UNSIGNED_SHORT: 623 assert(image->Type == GL_UNSIGNED_SHORT); 624 MEMCPY( sDst, src, width * sizeof(GLushort) ); 625 if (packing->SwapBytes) { 626 gl_swap2( sDst, width ); 627 } 628 sDst += width; 629 break; 630 case GL_SHORT: 631 assert(image->Type == GL_FLOAT); 632 if (packing->SwapBytes) { 633 for (j=0;j<width;j++) { 634 GLshort value = ((GLshort*)src)[j]; 635 value = ((value >> 8) & 0xff) | ((value&0xff) << 8); 636 *fDst++ = SHORT_TO_FLOAT(value); 637 } 638 } 639 else { 640 for (j=0;j<width;j++) { 641 *fDst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]); 642 } 643 } 644 break; 645 case GL_INT: 646 assert(image->Type == GL_FLOAT); 647 if (packing->SwapBytes) { 648 for (j=0;j<width;j++) { 649 GLint value = ((GLint*)src)[j]; 650 value = ((value >> 24) & 0x000000ff) | 651 ((value >> 8) & 0x0000ff00) | 652 ((value << 8) & 0x00ff0000) | 653 ((value << 24) & 0xff000000); 654 *fDst++ = INT_TO_FLOAT(value); 655 } 656 } 657 else { 658 for (j=0;j<width;j++) { 659 *fDst++ = INT_TO_FLOAT(((GLint*)src)[j]); 660 } 661 } 662 iDst += width; 663 break; 664 case GL_UNSIGNED_INT: 665 assert(image->Type == GL_UNSIGNED_INT); 666 MEMCPY( iDst, src, width * sizeof(GLuint) ); 667 if (packing->SwapBytes) { 668 gl_swap4( iDst, width ); 669 } 670 iDst += width; 671 break; 672 case GL_FLOAT: 673 assert(image->Type == GL_FLOAT); 674 MEMCPY( fDst, src, width * sizeof(GLfloat) ); 675 if (packing->SwapBytes) { 676 gl_swap4( (GLuint*) fDst, width ); 677 } 678 fDst += width; 679 break; 680 default: 681 gl_problem(ctx, "unpack_depth_image type" ); 682 return image; 683 } 684 } 685 686 return image; 687} 688 689 690 691/* 692 * Unpack a stencil image. Store as GLubytes in a gl_image structure. 693 * Return: pointer to new gl_image structure. 694 */ 695static struct gl_image * 696unpack_stencil_image( GLcontext *ctx, GLenum type, GLint width, GLint height, 697 const GLvoid *pixels, 698 const struct gl_pixelstore_attrib *packing ) 699{ 700 struct gl_image *image; 701 GLubyte *dst; 702 GLint i, j; 703 704 assert(sizeof(GLstencil) == sizeof(GLubyte)); 705 706 image = alloc_image(); 707 if (image) { 708 image->Width = width; 709 image->Height = height; 710 image->Depth = 1; 711 image->Components = 1; 712 image->Format = GL_STENCIL_INDEX; 713 image->Type = GL_UNSIGNED_BYTE; 714 image->Data = GL_ALLOC( width * height * sizeof(GLubyte)); 715 image->RefCount = 0; 716 if (!image->Data) 717 return image; 718 } 719 else { 720 return NULL; 721 } 722 723 dst = (GLubyte *) image->Data; 724 725 for (i=0;i<height;i++) { 726 GLvoid *src = gl_pixel_addr_in_image( packing, pixels, 727 width, height, 728 GL_STENCIL_INDEX, type, 729 0, i, 0 ); 730 if (!src) { 731 return image; 732 } 733 734 switch (type) { 735 case GL_UNSIGNED_BYTE: 736 case GL_BYTE: 737 MEMCPY( dst, src, width * sizeof(GLubyte) ); 738 dst += width * sizeof(GLubyte); 739 break; 740 case GL_UNSIGNED_SHORT: 741 case GL_SHORT: 742 if (packing->SwapBytes) { 743 /* grab upper byte */ 744 for (j=0; j < width; j++) { 745 *dst++ = (((GLushort*)src)[j] & 0xff00) >> 8; 746 } 747 } 748 else { 749 for (j=0; j < width; j++) { 750 *dst++ = (((GLushort*)src)[j]) & 0xff; 751 } 752 } 753 break; 754 case GL_INT: 755 if (packing->SwapBytes) { 756 /* grab upper byte */ 757 for (j=0; j < width; j++) { 758 *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8; 759 } 760 } 761 else { 762 for (j=0; j < width; j++) { 763 *dst++ = (((GLuint*)src)[j]) & 0xff; 764 } 765 } 766 break; 767 case GL_UNSIGNED_INT: 768 if (packing->SwapBytes) { 769 /* grab upper byte */ 770 for (j=0; j < width; j++) { 771 *dst++ = (((GLuint*)src)[j] & 0xff000000) >> 8; 772 } 773 } 774 else { 775 for (j=0; j < width; j++) { 776 *dst++ = (((GLuint*)src)[j]) & 0xff; 777 } 778 } 779 break; 780 case GL_FLOAT: 781 if (packing->SwapBytes) { 782 for (j=0; j < width; j++) { 783 GLfloat fvalue; 784 GLint value = ((GLuint*)src)[j]; 785 value = ((value & 0xff000000) >> 24) 786 | ((value & 0x00ff0000) >> 8) 787 | ((value & 0x0000ff00) << 8) 788 | ((value & 0x000000ff) << 24); 789 fvalue = *((GLfloat*) &value); 790 *dst++ = ((GLint) fvalue) & 0xff; 791 } 792 } 793 else { 794 for (j=0; j < width; j++) { 795 GLfloat fvalue = ((GLfloat *)src)[j]; 796 *dst++ = ((GLint) fvalue) & 0xff; 797 } 798 } 799 break; 800 default: 801 gl_problem(ctx, "unpack_stencil_image type" ); 802 return image; 803 } 804 } 805 806 return image; 807} 808 809 810 811/* 812 * Unpack a bitmap, return a new gl_image struct. 813 */ 814static struct gl_image * 815unpack_bitmap( GLenum format, GLint width, GLint height, 816 const GLvoid *pixels, 817 const struct gl_pixelstore_attrib *packing ) 818{ 819 struct gl_image *image; 820 GLint bytes, i, width_in_bytes; 821 GLubyte *buffer, *dst; 822 823 assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX); 824 825 /* Alloc dest storage */ 826 bytes = ((width+7)/8 * height); 827 if (bytes>0 && pixels!=NULL) { 828 buffer = (GLubyte *) GL_ALLOC( bytes ); 829 if (!buffer) { 830 return NULL; 831 } 832 /* Copy/unpack pixel data to buffer */ 833 width_in_bytes = CEILING( width, 8 ); 834 dst = buffer; 835 for (i=0; i<height; i++) { 836 GLvoid *src = gl_pixel_addr_in_image( packing, pixels, 837 width, height, 838 GL_COLOR_INDEX, GL_BITMAP, 839 0, i, 0 ); 840 if (!src) { 841 GL_FREE(buffer); 842 return NULL; 843 } 844 MEMCPY( dst, src, width_in_bytes ); 845 dst += width_in_bytes; 846 } 847 /* Bit flipping */ 848 if (packing->LsbFirst) { 849 gl_flip_bytes( buffer, bytes ); 850 } 851 } 852 else { 853 /* a 'null' bitmap */ 854 buffer = NULL; 855 } 856 857 image = alloc_image(); 858 if (image) { 859 image->Width = width; 860 image->Height = height; 861 image->Depth = 1; 862 image->Components = 0; 863 image->Format = format; 864 image->Type = GL_BITMAP; 865 image->Data = buffer; 866 image->RefCount = 0; 867 } 868 else { 869 GL_FREE( buffer ); 870 return NULL; 871 } 872 873 return image; 874} 875 876 877 878/* 879 * Unpack a 32x32 pixel polygon stipple from user memory using the 880 * current pixel unpack settings. 881 */ 882void gl_unpack_polygon_stipple( const GLcontext *ctx, 883 const GLubyte *pattern, GLuint dest[32] ) 884{ 885 GLint i; 886 for (i = 0; i < 32; i++) { 887 GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( &ctx->Unpack, pattern, 888 32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 ); 889 dest[i] = (src[0] << 24) 890 | (src[1] << 16) 891 | (src[2] << 8) 892 | (src[3] ); 893 } 894 895 /* Bit flipping within each byte */ 896 if (ctx->Unpack.LsbFirst) { 897 gl_flip_bytes( (GLubyte *) dest, 32 * 4 ); 898 } 899} 900 901 902 903/* 904 * Pack polygon stipple into user memory given current pixel packing 905 * settings. 906 */ 907void gl_pack_polygon_stipple( const GLcontext *ctx, 908 const GLuint pattern[32], 909 GLubyte *dest ) 910{ 911 GLint i; 912 for (i = 0; i < 32; i++) { 913 GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image( &ctx->Pack, dest, 914 32, 32, GL_COLOR_INDEX, GL_BITMAP, 0, i, 0 ); 915 dst[0] = (pattern[i] >> 24) & 0xff; 916 dst[1] = (pattern[i] >> 16) & 0xff; 917 dst[2] = (pattern[i] >> 8) & 0xff; 918 dst[3] = (pattern[i] ) & 0xff; 919 920 /* Bit flipping within each byte */ 921 if (ctx->Pack.LsbFirst) { 922 gl_flip_bytes( (GLubyte *) dst, 4 ); 923 } 924 } 925} 926 927 928 929/* 930 * Unpack an RGBA or CI image and store it as unsigned bytes 931 */ 932static struct gl_image * 933unpack_ubyte_image( GLint width, GLint height, 934 GLint depth, GLenum format, const GLvoid *pixels, 935 const struct gl_pixelstore_attrib *packing ) 936{ 937 struct gl_image *image; 938 GLint width_in_bytes; 939 GLint components; 940 GLubyte *buffer, *dst; 941 GLint i, d; 942 943 components = gl_components_in_format( format ); 944 945 width_in_bytes = width * components * sizeof(GLubyte); 946 buffer = (GLubyte *) GL_ALLOC( height * width_in_bytes * depth ); 947 if (!buffer) { 948 return NULL; 949 } 950 951 /* Copy/unpack pixel data to buffer */ 952 dst = buffer; 953 for (d=0; d<depth; d++ ) { 954 for (i=0;i<height;i++) { 955 GLubyte *src = (GLubyte *) gl_pixel_addr_in_image( packing, 956 pixels, width, height, format, GL_UNSIGNED_BYTE, 957 d, i, 0 ); 958 if (!src) { 959 GL_FREE(buffer); 960 return NULL; 961 } 962 MEMCPY( dst, src, width_in_bytes ); 963 dst += width_in_bytes; 964 } 965 } 966 967 if (format == GL_BGR) { 968 /* swap order of every ubyte triplet from BGR to RGB */ 969 for (i=0; i<width*height; i++) { 970 GLubyte b = buffer[i*3+0]; 971 GLubyte r = buffer[i*3+2]; 972 buffer[i*3+0] = r; 973 buffer[i*3+2] = b; 974 } 975 } 976 else if (format == GL_BGRA) { 977 /* swap order of every ubyte quadruplet from BGRA to RGBA */ 978 for (i=0; i<width*height; i++) { 979 GLubyte b = buffer[i*4+0]; 980 GLubyte r = buffer[i*4+2]; 981 buffer[i*4+0] = r; 982 buffer[i*4+2] = b; 983 } 984 } 985 else if (format == GL_ABGR_EXT) { 986 /* swap order of every ubyte quadruplet from ABGR to RGBA */ 987 for (i=0; i<width*height; i++) { 988 GLubyte a = buffer[i*4+0]; 989 GLubyte b = buffer[i*4+1]; 990 GLubyte g = buffer[i*4+2]; 991 GLubyte r = buffer[i*4+3]; 992 buffer[i*4+0] = r; 993 buffer[i*4+1] = g; 994 buffer[i*4+2] = b; 995 buffer[i*4+3] = a; 996 } 997 } 998 999 1000 image = alloc_image(); 1001 if (image) { 1002 image->Width = width; 1003 image->Height = height; 1004 image->Depth = depth; 1005 image->Components = components; 1006 if (format == GL_BGR) 1007 image->Format = GL_RGB; 1008 else if (format == GL_BGRA) 1009 image->Format = GL_RGBA; 1010 else if (format == GL_ABGR_EXT) 1011 image->Format = GL_RGBA; 1012 else 1013 image->Format = format; 1014 image->Type = GL_UNSIGNED_BYTE; 1015 image->Data = buffer; 1016 image->RefCount = 0; 1017 } 1018 else { 1019 GL_FREE( buffer ); 1020 } 1021 1022 return image; 1023} 1024 1025 1026 1027/* 1028 * Unpack a color image storing image as GLfloats 1029 */ 1030static struct gl_image * 1031unpack_float_image( GLcontext *ctx, GLint width, GLint height, GLint depth, 1032 GLenum format, GLenum type, const GLvoid *pixels, 1033 const struct gl_pixelstore_attrib *packing ) 1034{ 1035 struct gl_image *image; 1036 GLfloat *dst; 1037 GLint elems_per_row; 1038 GLint components; 1039 GLint i, j, d; 1040 GLboolean normalize; 1041 1042 assert(type != GL_BITMAP); 1043 1044 components = gl_components_in_format( format ); 1045 assert(components > 0); /* should have been caught earlier */ 1046 1047 if (!gl_is_legal_format_and_type( format, type )) { 1048 /* bad pixel type for format, make dummy image */ 1049 image = alloc_image(); 1050 if (image) { 1051 image->Width = width; 1052 image->Height = height; 1053 image->Depth = depth; 1054 image->Components = components; 1055 image->Format = format; 1056 image->Type = type; 1057 image->Data = NULL; 1058 image->RefCount = 0; 1059 } 1060 return image; 1061 } 1062 1063 elems_per_row = width * components; 1064 1065 image = alloc_image(); 1066 if (image) { 1067 image->Width = width; 1068 image->Height = height; 1069 image->Depth = depth; 1070 image->Components = components; 1071 if (format == GL_BGR) 1072 image->Format = GL_RGB; 1073 else if (format == GL_BGRA) 1074 image->Format = GL_RGBA; 1075 else if (format == GL_ABGR_EXT) 1076 image->Format = GL_RGBA; 1077 else 1078 image->Format = format; 1079 image->Type = GL_FLOAT; 1080 image->Data = GL_ALLOC( elems_per_row * height * depth * sizeof(GLfloat)); 1081 image->RefCount = 0; 1082 if (!image->Data) 1083 return image; 1084 } 1085 else { 1086 return NULL; 1087 } 1088 1089 normalize = (format != GL_COLOR_INDEX) && (format != GL_STENCIL_INDEX); 1090 1091 dst = (GLfloat *) image->Data; 1092 1093 for (d=0; d<depth; d++) { 1094 for (i=0;i<height;i++) { 1095 GLvoid *src = gl_pixel_addr_in_image( packing, pixels, 1096 width, height, 1097 format, type, 1098 d, i, 0 ); 1099 if (!src) { 1100 return image; 1101 } 1102 1103 switch (type) { 1104 case GL_UNSIGNED_BYTE: 1105 { 1106 GLubyte *ubsrc = (GLubyte *) src; 1107 if (normalize) { 1108 for (j=0;j<elems_per_row;j++) { 1109 *dst++ = UBYTE_TO_FLOAT(ubsrc[j]); 1110 } 1111 } 1112 else { 1113 for (j=0;j<elems_per_row;j++) { 1114 *dst++ = (GLfloat) ubsrc[j]; 1115 } 1116 } 1117 } 1118 break; 1119 case GL_BYTE: 1120 if (normalize) { 1121 for (j=0;j<elems_per_row;j++) { 1122 *dst++ = BYTE_TO_FLOAT(((GLbyte*)src)[j]); 1123 } 1124 } 1125 else { 1126 for (j=0;j<elems_per_row;j++) { 1127 *dst++ = (GLfloat) ((GLbyte*)src)[j]; 1128 } 1129 } 1130 break; 1131 case GL_UNSIGNED_SHORT: 1132 if (packing->SwapBytes) { 1133 for (j=0;j<elems_per_row;j++) { 1134 GLushort value = ((GLushort*)src)[j]; 1135 value = ((value >> 8) & 0xff) | ((value&0xff) << 8); 1136 if (normalize) { 1137 *dst++ = USHORT_TO_FLOAT(value); 1138 } 1139 else { 1140 *dst++ = (GLfloat) value; 1141 } 1142 } 1143 } 1144 else { 1145 if (normalize) { 1146 for (j=0;j<elems_per_row;j++) { 1147 *dst++ = USHORT_TO_FLOAT(((GLushort*)src)[j]); 1148 } 1149 } 1150 else { 1151 for (j=0;j<elems_per_row;j++) { 1152 *dst++ = (GLfloat) ((GLushort*)src)[j]; 1153 } 1154 } 1155 } 1156 break; 1157 case GL_SHORT: 1158 if (packing->SwapBytes) { 1159 for (j=0;j<elems_per_row;j++) { 1160 GLshort value = ((GLshort*)src)[j]; 1161 value = ((value >> 8) & 0xff) | ((value&0xff) << 8); 1162 if (normalize) { 1163 *dst++ = SHORT_TO_FLOAT(value); 1164 } 1165 else { 1166 *dst++ = (GLfloat) value; 1167 } 1168 } 1169 } 1170 else { 1171 if (normalize) { 1172 for (j=0;j<elems_per_row;j++) { 1173 *dst++ = SHORT_TO_FLOAT(((GLshort*)src)[j]); 1174 } 1175 } 1176 else { 1177 for (j=0;j<elems_per_row;j++) { 1178 *dst++ = (GLfloat) ((GLshort*)src)[j]; 1179 } 1180 } 1181 } 1182 break; 1183 case GL_UNSIGNED_INT: 1184 if (packing->SwapBytes) { 1185 GLuint value; 1186 for (j=0;j<elems_per_row;j++) { 1187 value = ((GLuint*)src)[j]; 1188 value = ((value & 0xff000000) >> 24) 1189 | ((value & 0x00ff0000) >> 8) 1190 | ((value & 0x0000ff00) << 8) 1191 | ((value & 0x000000ff) << 24); 1192 if (normalize) { 1193 *dst++ = UINT_TO_FLOAT(value); 1194 } 1195 else { 1196 *dst++ = (GLfloat) value; 1197 } 1198 } 1199 } 1200 else { 1201 if (normalize) { 1202 for (j=0;j<elems_per_row;j++) { 1203 *dst++ = UINT_TO_FLOAT(((GLuint*)src)[j]); 1204 } 1205 } 1206 else { 1207 for (j=0;j<elems_per_row;j++) { 1208 *dst++ = (GLfloat) ((GLuint*)src)[j]; 1209 } 1210 } 1211 } 1212 break; 1213 case GL_INT: 1214 if (packing->SwapBytes) { 1215 GLint value; 1216 for (j=0;j<elems_per_row;j++) { 1217 value = ((GLint*)src)[j]; 1218 value = ((value & 0xff000000) >> 24) 1219 | ((value & 0x00ff0000) >> 8) 1220 | ((value & 0x0000ff00) << 8) 1221 | ((value & 0x000000ff) << 24); 1222 if (normalize) { 1223 *dst++ = INT_TO_FLOAT(value); 1224 } 1225 else { 1226 *dst++ = (GLfloat) value; 1227 } 1228 } 1229 } 1230 else { 1231 if (normalize) { 1232 for (j=0;j<elems_per_row;j++) { 1233 *dst++ = INT_TO_FLOAT(((GLint*)src)[j]); 1234 } 1235 } 1236 else { 1237 for (j=0;j<elems_per_row;j++) { 1238 *dst++ = (GLfloat) ((GLint*)src)[j]; 1239 } 1240 } 1241 } 1242 break; 1243 case GL_FLOAT: 1244 if (packing->SwapBytes) { 1245 GLint value; 1246 for (j=0;j<elems_per_row;j++) { 1247 value = ((GLuint*)src)[j]; 1248 value = ((value & 0xff000000) >> 24) 1249 | ((value & 0x00ff0000) >> 8) 1250 | ((value & 0x0000ff00) << 8) 1251 | ((value & 0x000000ff) << 24); 1252 *dst++ = *((GLfloat*) &value); 1253 } 1254 } 1255 else { 1256 MEMCPY( dst, src, elems_per_row*sizeof(GLfloat) ); 1257 dst += elems_per_row; 1258 } 1259 break; 1260 case GL_UNSIGNED_BYTE_3_3_2: 1261 { 1262 GLubyte *ubsrc = (GLubyte *) src; 1263 for (j=0;j<width;j++) { 1264 GLubyte p = ubsrc[j]; 1265 *dst++ = ((p >> 5) ) * (1.0F / 7.0F); /* red */ 1266 *dst++ = ((p >> 2) & 0x7) * (1.0F / 7.0F); /* green */ 1267 *dst++ = ((p ) & 0x3) * (1.0F / 3.0F); /* blue */ 1268 } 1269 } 1270 break; 1271 case GL_UNSIGNED_BYTE_2_3_3_REV: 1272 { 1273 GLubyte *ubsrc = (GLubyte *) src; 1274 for (j=0;j<width;j++) { 1275 GLubyte p = ubsrc[j]; 1276 *dst++ = ((p ) & 0x7) * (1.0F / 7.0F); /* red */ 1277 *dst++ = ((p >> 3) & 0x7) * (1.0F / 7.0F); /* green */ 1278 *dst++ = ((p >> 6) ) * (1.0F / 3.0F); /* blue */ 1279 } 1280 } 1281 break; 1282 case GL_UNSIGNED_SHORT_5_6_5: 1283 { 1284 GLushort *ussrc = (GLushort *) src; 1285 for (j=0;j<width;j++) { 1286 GLushort p = ussrc[j]; 1287 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* red */ 1288 *dst++ = ((p >> 5) & 0x3f) * (1.0F / 63.0F); /* green */ 1289 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* blue */ 1290 } 1291 } 1292 break; 1293 case GL_UNSIGNED_SHORT_5_6_5_REV: 1294 { 1295 GLushort *ussrc = (GLushort *) src; 1296 for (j=0;j<width;j++) { 1297 GLushort p = ussrc[j]; 1298 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* red */ 1299 *dst++ = ((p >> 5) & 0x3f) * (1.0F / 63.0F); /* green */ 1300 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* blue */ 1301 } 1302 } 1303 break; 1304 case GL_UNSIGNED_SHORT_4_4_4_4: 1305 { 1306 GLushort *ussrc = (GLushort *) src; 1307 for (j=0;j<width;j++) { 1308 GLushort p = ussrc[j]; 1309 *dst++ = ((p >> 12) ) * (1.0F / 15.0F); /* red */ 1310 *dst++ = ((p >> 8) & 0xf) * (1.0F / 15.0F); /* green */ 1311 *dst++ = ((p >> 4) & 0xf) * (1.0F / 15.0F); /* blue */ 1312 *dst++ = ((p ) & 0xf) * (1.0F / 15.0F); /* alpha */ 1313 } 1314 } 1315 break; 1316 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 1317 { 1318 GLushort *ussrc = (GLushort *) src; 1319 for (j=0;j<width;j++) { 1320 GLushort p = ussrc[j]; 1321 *dst++ = ((p ) & 0xf) * (1.0F / 15.0F); /* red */ 1322 *dst++ = ((p >> 4) & 0xf) * (1.0F / 15.0F); /* green */ 1323 *dst++ = ((p >> 8) & 0xf) * (1.0F / 15.0F); /* blue */ 1324 *dst++ = ((p >> 12) ) * (1.0F / 15.0F); /* alpha */ 1325 } 1326 } 1327 break; 1328 case GL_UNSIGNED_SHORT_5_5_5_1: 1329 { 1330 GLushort *ussrc = (GLushort *) src; 1331 for (j=0;j<width;j++) { 1332 GLushort p = ussrc[j]; 1333 *dst++ = ((p >> 11) ) * (1.0F / 31.0F); /* red */ 1334 *dst++ = ((p >> 6) & 0x1f) * (1.0F / 31.0F); /* green */ 1335 *dst++ = ((p >> 1) & 0x1f) * (1.0F / 31.0F); /* blue */ 1336 *dst++ = ((p ) & 0x1) * (1.0F / 1.0F); /* alpha */ 1337 } 1338 } 1339 break; 1340 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 1341 { 1342 GLushort *ussrc = (GLushort *) src; 1343 for (j=0;j<width;j++) { 1344 GLushort p = ussrc[j]; 1345 *dst++ = ((p ) & 0x1f) * (1.0F / 31.0F); /* red */ 1346 *dst++ = ((p >> 5) & 0x1f) * (1.0F / 31.0F); /* green */ 1347 *dst++ = ((p >> 10) & 0x1f) * (1.0F / 31.0F); /* blue */ 1348 *dst++ = ((p >> 15) ) * (1.0F / 1.0F); /* alpha */ 1349 } 1350 } 1351 break; 1352 case GL_UNSIGNED_INT_8_8_8_8: 1353 { 1354 GLuint *uisrc = (GLuint *) src; 1355 for (j=0;j<width;j++) { 1356 GLuint p = uisrc[j]; 1357 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) ); 1358 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff); 1359 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff); 1360 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff); 1361 } 1362 } 1363 break; 1364 case GL_UNSIGNED_INT_8_8_8_8_REV: 1365 { 1366 GLuint *uisrc = (GLuint *) src; 1367 for (j=0;j<width;j++) { 1368 GLuint p = uisrc[j]; 1369 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p ) & 0xff); 1370 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 8) & 0xff); 1371 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 16) & 0xff); 1372 *dst++ = UBYTE_COLOR_TO_FLOAT_COLOR((p >> 24) ); 1373 } 1374 } 1375 break; 1376 case GL_UNSIGNED_INT_10_10_10_2: 1377 { 1378 GLuint *uisrc = (GLuint *) src; 1379 for (j=0;j<width;j++) { 1380 GLuint p = uisrc[j]; 1381 *dst++ = ((p >> 22) ) * (1.0F / 1023.0F); /* r */ 1382 *dst++ = ((p >> 12) & 0x3ff) * (1.0F / 1023.0F); /* g */ 1383 *dst++ = ((p >> 2) & 0x3ff) * (1.0F / 1023.0F); /* b */ 1384 *dst++ = ((p ) & 0x3 ) * (1.0F / 3.0F); /* a */ 1385 } 1386 } 1387 break; 1388 case GL_UNSIGNED_INT_2_10_10_10_REV: 1389 { 1390 GLuint *uisrc = (GLuint *) src; 1391 for (j=0;j<width;j++) { 1392 GLuint p = uisrc[j]; 1393 *dst++ = ((p ) & 0x3ff) * (1.0F / 1023.0F); /* r*/ 1394 *dst++ = ((p >> 10) & 0x3ff) * (1.0F / 1023.0F); /* g */ 1395 *dst++ = ((p >> 20) & 0x3ff) * (1.0F / 1023.0F); /* b */ 1396 *dst++ = ((p >> 30) ) * (1.0F / 3.0F); /* a */ 1397 } 1398 } 1399 break; 1400 default: 1401 gl_problem(ctx, "unpack_float_image type" ); 1402 return image; 1403 } 1404 } 1405 } 1406 1407 if (format == GL_BGR) { 1408 /* swap order of every float triplet from BGR to RGBA */ 1409 GLfloat *buffer = (GLfloat *) image->Data; 1410 for (i=0; i<width*height*depth; i++) { 1411 GLfloat b = buffer[i*3+0]; 1412 GLfloat r = buffer[i*3+2]; 1413 buffer[i*3+0] = r; 1414 buffer[i*3+2] = b; 1415 } 1416 } 1417 else if (format == GL_BGRA) { 1418 /* swap order of every float quadruplet from BGRA to RGBA */ 1419 GLfloat *buffer = (GLfloat *) image->Data; 1420 for (i=0; i<width*height*depth; i++) { 1421 GLfloat b = buffer[i*4+0]; 1422 GLfloat r = buffer[i*4+2]; 1423 buffer[i*4+0] = r; 1424 buffer[i*4+2] = b; 1425 } 1426 } 1427 else if (format == GL_ABGR_EXT) { 1428 /* swap order of every float quadruplet from ABGR to RGBA */ 1429 GLfloat *buffer = (GLfloat *) image->Data; 1430 for (i=0; i<width*height*depth; i++) { 1431 GLfloat a = buffer[i*4+0]; 1432 GLfloat b = buffer[i*4+1]; 1433 GLfloat g = buffer[i*4+2]; 1434 GLfloat r = buffer[i*4+3]; 1435 buffer[i*4+0] = r; 1436 buffer[i*4+1] = g; 1437 buffer[i*4+2] = b; 1438 buffer[i*4+3] = a; 1439 } 1440 } 1441 1442 return image; 1443} 1444 1445 1446 1447/* 1448 * Unpack a bitmap image, using current glPixelStore parameters, 1449 * making a new gl_image. 1450 */ 1451struct gl_image *gl_unpack_bitmap( GLcontext *ctx, 1452 GLsizei width, GLsizei height, 1453 const GLubyte *bitmap, 1454 const struct gl_pixelstore_attrib *packing ) 1455{ 1456 return gl_unpack_image( ctx, width, height, 1457 GL_COLOR_INDEX, GL_BITMAP, bitmap, packing ); 1458} 1459 1460 1461 1462/* 1463 * Unpack a 2-D image from user's buffer. Return pointer to new 1464 * gl_image struct. 1465 * 1466 * Input: width, height - size in pixels 1467 * format - format of incoming pixel data 1468 * type - datatype of incoming pixel data 1469 * pixels - pointer to unpacked image in user buffer 1470 */ 1471struct gl_image *gl_unpack_image( GLcontext *ctx, 1472 GLint width, GLint height, 1473 GLenum format, GLenum type, 1474 const GLvoid *pixels, 1475 const struct gl_pixelstore_attrib *packing ) 1476{ 1477 return gl_unpack_image3D( ctx, width, height, 1, 1478 format, type, pixels, packing ); 1479} 1480 1481 1482 1483/* 1484 * Unpack a 1, 2 or 3-D image from user-supplied address, returning a 1485 * pointer to a new gl_image struct. 1486 * This function is always called by a higher-level unpack function such 1487 * as gl_unpack_texsubimage() or gl_unpack_bitmap(). 1488 * 1489 * Input: width, height, depth - size in pixels 1490 * format - format of incoming pixel data 1491 * type - datatype of incoming pixel data 1492 * pixels - pointer to unpacked image. 1493 */ 1494struct gl_image *gl_unpack_image3D( GLcontext *ctx, 1495 GLint width, GLint height, GLint depth, 1496 GLenum format, GLenum type, 1497 const GLvoid *pixels, 1498 const struct gl_pixelstore_attrib *packing) 1499{ 1500 if (width <= 0 || height <= 0 || depth <= 0) { 1501 return alloc_error_image(width, height, depth, format, type); 1502 } 1503 1504 if (type==GL_BITMAP) { 1505 if (format != GL_COLOR_INDEX && format != GL_STENCIL_INDEX) { 1506 return alloc_error_image(width, height, depth, format, type); 1507 } 1508 else { 1509 return unpack_bitmap( format, width, height, pixels, packing ); 1510 } 1511 } 1512 else if (format==GL_DEPTH_COMPONENT) { 1513 /* TODO: pack as GLdepth values (GLushort or GLuint) */ 1514 return unpack_depth_image( ctx, type, width, height, pixels, packing ); 1515 } 1516 else if (format==GL_STENCIL_INDEX) { 1517 /* TODO: pack as GLstencil (GLubyte or GLushort) */ 1518 return unpack_stencil_image( ctx, type, width, height, pixels, packing ); 1519 } 1520 else if (type==GL_UNSIGNED_BYTE) { 1521 /* upack, convert to GLubytes */ 1522 return unpack_ubyte_image( width, height, depth, format, pixels, packing ); 1523 } 1524 else { 1525 /* upack, convert to floats */ 1526 return unpack_float_image( ctx, width, height, depth, 1527 format, type, pixels, packing ); 1528 } 1529 1530 /* never get here */ 1531 /*return NULL;*/ 1532} 1533 1534 1535/* 1536 * Apply pixel-transfer operations (scale, bias, mapping) to a single row 1537 * of a gl_image. Put resulting color components into result array. 1538 */ 1539void gl_scale_bias_map_image_data( const GLcontext *ctx, 1540 const struct gl_image *image, 1541 GLint row, GLubyte result[] ) 1542{ 1543 GLint start, i; 1544 1545 assert(ctx); 1546 assert(image); 1547 assert(result); 1548 assert(row >= 0); 1549 1550 start = row * image->Width * image->Components; 1551 1552 for (i=0; i < image->Width; i++) { 1553 GLint pos = start+i; 1554 GLfloat red, green, blue, alpha; 1555 if (image->Type == GL_UNSIGNED_BYTE) { 1556 const GLubyte *data = (GLubyte *) image->Data; 1557 switch (image->Format) { 1558 case GL_RED: 1559 red = data[pos] * (1.0F/255.0F); 1560 green = 0; 1561 blue = 0; 1562 alpha = 0; 1563 break; 1564 case GL_RGB: 1565 red = data[pos*3+0] * (1.0F/255.0F); 1566 green = data[pos*3+1] * (1.0F/255.0F); 1567 blue = data[pos*3+2] * (1.0F/255.0F); 1568 alpha = 0; 1569 break; 1570 default: 1571 gl_problem(ctx, "bad image format in gl_scale...image_data"); 1572 return; 1573 } 1574 } 1575 else if (image->Type == GL_FLOAT) { 1576 const GLubyte *data = (GLubyte *) image->Data; 1577 switch (image->Format) { 1578 case GL_RED: 1579 red = data[pos]; 1580 green = 0; 1581 blue = 0; 1582 alpha = 0; 1583 break; 1584 case GL_RGB: 1585 red = data[pos*3+0]; 1586 green = data[pos*3+1]; 1587 blue = data[pos*3+2]; 1588 alpha = 0; 1589 break; 1590 default: 1591 gl_problem(ctx, "bad image format in gl_scale...image_data"); 1592 return; 1593 } 1594 } 1595 else { 1596 gl_problem(ctx, "Bad image type in gl_scale_...image_data"); 1597 return; 1598 } 1599 1600 assert(red >= 0.0 && red <= 1.0); 1601 assert(green >= 0.0 && green <= 1.0); 1602 assert(blue >= 0.0 && blue <= 1.0); 1603 assert(alpha >= 0.0 && alpha <= 1.0); 1604 1605 /* 1606 if (scale or bias) { 1607 1608 1609 } 1610 if (mapping) { 1611 1612 } 1613 */ 1614 1615 result[i*4+0] = (GLubyte) (red * 255.0); 1616 result[i*4+1] = (GLubyte) (green * 255.0); 1617 result[i*4+2] = (GLubyte) (blue * 255.0); 1618 result[i*4+3] = (GLubyte) (alpha * 255.0); 1619 } 1620} 1621 1622 1623 1624/* 1625 * Pack the given RGBA span into client memory at 'dest' address 1626 * in the given pixel format and type. 1627 * Optionally apply the enabled pixel transfer ops. 1628 * Pack into memory using the given packing params struct. 1629 * This is used by glReadPixels and glGetTexImage?D() 1630 * Input: ctx - the context 1631 * n - number of pixels in the span 1632 * rgba - the pixels 1633 * format - dest packing format 1634 * type - dest packing datatype 1635 * destination - destination packing address 1636 * packing - pixel packing parameters 1637 * applyTransferOps - apply scale/bias/lookup-table ops? 1638 */ 1639void gl_pack_rgba_span( const GLcontext *ctx, 1640 GLuint n, CONST GLubyte rgba[][4], 1641 GLenum format, GLenum type, GLvoid *destination, 1642 const struct gl_pixelstore_attrib *packing, 1643 GLboolean applyTransferOps ) 1644{ 1645 /* Test for optimized case first */ 1646 if (!ctx->Pixel.ScaleOrBiasRGBA && !ctx->Pixel.MapColorFlag && 1647 format == GL_RGBA && type == GL_UNSIGNED_BYTE) { 1648 /* simple case */ 1649 MEMCPY( destination, rgba, n * 4 * sizeof(GLubyte) ); 1650 } 1651 else { 1652 GLfloat red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH]; 1653 GLfloat alpha[MAX_WIDTH], luminance[MAX_WIDTH]; 1654 GLfloat rscale = 1.0F / 255.0F; 1655 GLfloat gscale = 1.0F / 255.0F; 1656 GLfloat bscale = 1.0F / 255.0F; 1657 GLfloat ascale = 1.0F / 255.0F; 1658 GLuint i; 1659 1660 assert( n < MAX_WIDTH ); 1661 1662 /* convert color components to floating point */ 1663 for (i=0;i<n;i++) { 1664 red[i] = rgba[i][RCOMP] * rscale; 1665 green[i] = rgba[i][GCOMP] * gscale; 1666 blue[i] = rgba[i][BCOMP] * bscale; 1667 alpha[i] = rgba[i][ACOMP] * ascale; 1668 } 1669 1670 /* 1671 * Apply scale, bias and lookup-tables if enabled. 1672 */ 1673 if (applyTransferOps) { 1674 if (ctx->Pixel.ScaleOrBiasRGBA) { 1675 gl_scale_and_bias_color( ctx, n, red, green, blue, alpha ); 1676 } 1677 if (ctx->Pixel.MapColorFlag) { 1678 gl_map_color( ctx, n, red, green, blue, alpha ); 1679 } 1680 } 1681 1682 if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) { 1683 for (i=0;i<n;i++) { 1684 GLfloat sum = red[i] + green[i] + blue[i]; 1685 luminance[i] = CLAMP( sum, 0.0F, 1.0F ); 1686 } 1687 } 1688 1689 /* 1690 * Pack/store the pixels. Ugh! Lots of cases!!! 1691 */ 1692 switch (type) { 1693 case GL_UNSIGNED_BYTE: 1694 { 1695 GLubyte *dst = (GLubyte *) destination; 1696 switch (format) { 1697 case GL_RED: 1698 for (i=0;i<n;i++) 1699 dst[i] = FLOAT_TO_UBYTE(red[i]); 1700 break; 1701 case GL_GREEN: 1702 for (i=0;i<n;i++) 1703 dst[i] = FLOAT_TO_UBYTE(green[i]); 1704 break; 1705 case GL_BLUE: 1706 for (i=0;i<n;i++) 1707 dst[i] = FLOAT_TO_UBYTE(blue[i]); 1708 break; 1709 case GL_ALPHA: 1710 for (i=0;i<n;i++) 1711 dst[i] = FLOAT_TO_UBYTE(alpha[i]); 1712 break; 1713 case GL_LUMINANCE: 1714 for (i=0;i<n;i++) 1715 dst[i] = FLOAT_TO_UBYTE(luminance[i]); 1716 break; 1717 case GL_LUMINANCE_ALPHA: 1718 for (i=0;i<n;i++) { 1719 dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]); 1720 dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]); 1721 } 1722 break; 1723 case GL_RGB: 1724 for (i=0;i<n;i++) { 1725 dst[i*3+0] = FLOAT_TO_UBYTE(red[i]); 1726 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]); 1727 dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]); 1728 } 1729 break; 1730 case GL_RGBA: 1731 for (i=0;i<n;i++) { 1732 dst[i*4+0] = FLOAT_TO_UBYTE(red[i]); 1733 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]); 1734 dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]); 1735 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]); 1736 } 1737 break; 1738 case GL_BGR: 1739 for (i=0;i<n;i++) { 1740 dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]); 1741 dst[i*3+1] = FLOAT_TO_UBYTE(green[i]); 1742 dst[i*3+2] = FLOAT_TO_UBYTE(red[i]); 1743 } 1744 break; 1745 case GL_BGRA: 1746 for (i=0;i<n;i++) { 1747 dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]); 1748 dst[i*4+1] = FLOAT_TO_UBYTE(green[i]); 1749 dst[i*4+2] = FLOAT_TO_UBYTE(red[i]); 1750 dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]); 1751 } 1752 break; 1753 case GL_ABGR_EXT: 1754 for (i=0;i<n;i++) { 1755 dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]); 1756 dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]); 1757 dst[i*4+2] = FLOAT_TO_UBYTE(green[i]); 1758 dst[i*4+3] = FLOAT_TO_UBYTE(red[i]); 1759 } 1760 break; 1761 default: 1762 gl_problem(ctx, "bad format in gl_pack_rgba_span\n"); 1763 } 1764 } 1765 break; 1766 case GL_BYTE: 1767 { 1768 GLbyte *dst = (GLbyte *) destination; 1769 switch (format) { 1770 case GL_RED: 1771 for (i=0;i<n;i++) 1772 dst[i] = FLOAT_TO_BYTE(red[i]); 1773 break; 1774 case GL_GREEN: 1775 for (i=0;i<n;i++) 1776 dst[i] = FLOAT_TO_BYTE(green[i]); 1777 break; 1778 case GL_BLUE: 1779 for (i=0;i<n;i++) 1780 dst[i] = FLOAT_TO_BYTE(blue[i]); 1781 break; 1782 case GL_ALPHA: 1783 for (i=0;i<n;i++) 1784 dst[i] = FLOAT_TO_BYTE(alpha[i]); 1785 break; 1786 case GL_LUMINANCE: 1787 for (i=0;i<n;i++) 1788 dst[i] = FLOAT_TO_BYTE(luminance[i]); 1789 break; 1790 case GL_LUMINANCE_ALPHA: 1791 for (i=0;i<n;i++) { 1792 dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]); 1793 dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]); 1794 } 1795 break; 1796 case GL_RGB: 1797 for (i=0;i<n;i++) { 1798 dst[i*3+0] = FLOAT_TO_BYTE(red[i]); 1799 dst[i*3+1] = FLOAT_TO_BYTE(green[i]); 1800 dst[i*3+2] = FLOAT_TO_BYTE(blue[i]); 1801 } 1802 break; 1803 case GL_RGBA: 1804 for (i=0;i<n;i++) { 1805 dst[i*4+0] = FLOAT_TO_BYTE(red[i]); 1806 dst[i*4+1] = FLOAT_TO_BYTE(green[i]); 1807 dst[i*4+2] = FLOAT_TO_BYTE(blue[i]); 1808 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]); 1809 } 1810 break; 1811 case GL_BGR: 1812 for (i=0;i<n;i++) { 1813 dst[i*3+0] = FLOAT_TO_BYTE(blue[i]); 1814 dst[i*3+1] = FLOAT_TO_BYTE(green[i]); 1815 dst[i*3+2] = FLOAT_TO_BYTE(red[i]); 1816 } 1817 break; 1818 case GL_BGRA: 1819 for (i=0;i<n;i++) { 1820 dst[i*4+0] = FLOAT_TO_BYTE(blue[i]); 1821 dst[i*4+1] = FLOAT_TO_BYTE(green[i]); 1822 dst[i*4+2] = FLOAT_TO_BYTE(red[i]); 1823 dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]); 1824 } 1825 case GL_ABGR_EXT: 1826 for (i=0;i<n;i++) { 1827 dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]); 1828 dst[i*4+1] = FLOAT_TO_BYTE(blue[i]); 1829 dst[i*4+2] = FLOAT_TO_BYTE(green[i]); 1830 dst[i*4+3] = FLOAT_TO_BYTE(red[i]); 1831 } 1832 break; 1833 default: 1834 gl_problem(ctx, "bad format in gl_pack_rgba_span\n"); 1835 } 1836 } 1837 break; 1838 case GL_UNSIGNED_SHORT: 1839 { 1840 GLushort *dst = (GLushort *) destination; 1841 switch (format) { 1842 case GL_RED: 1843 for (i=0;i<n;i++) 1844 dst[i] = FLOAT_TO_USHORT(red[i]); 1845 break; 1846 case GL_GREEN: 1847 for (i=0;i<n;i++) 1848 dst[i] = FLOAT_TO_USHORT(green[i]); 1849 break; 1850 case GL_BLUE: 1851 for (i=0;i<n;i++) 1852 dst[i] = FLOAT_TO_USHORT(blue[i]); 1853 break; 1854 case GL_ALPHA: 1855 for (i=0;i<n;i++) 1856 dst[i] = FLOAT_TO_USHORT(alpha[i]); 1857 break; 1858 case GL_LUMINANCE: 1859 for (i=0;i<n;i++) 1860 dst[i] = FLOAT_TO_USHORT(luminance[i]); 1861 break; 1862 case GL_LUMINANCE_ALPHA: 1863 for (i=0;i<n;i++) { 1864 dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]); 1865 dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]); 1866 } 1867 break; 1868 case GL_RGB: 1869 for (i=0;i<n;i++) { 1870 dst[i*3+0] = FLOAT_TO_USHORT(red[i]); 1871 dst[i*3+1] = FLOAT_TO_USHORT(green[i]); 1872 dst[i*3+2] = FLOAT_TO_USHORT(blue[i]); 1873 } 1874 break; 1875 case GL_RGBA: 1876 for (i=0;i<n;i++) { 1877 dst[i*4+0] = FLOAT_TO_USHORT(red[i]); 1878 dst[i*4+1] = FLOAT_TO_USHORT(green[i]); 1879 dst[i*4+2] = FLOAT_TO_USHORT(blue[i]); 1880 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]); 1881 } 1882 break; 1883 case GL_BGR: 1884 for (i=0;i<n;i++) { 1885 dst[i*3+0] = FLOAT_TO_USHORT(blue[i]); 1886 dst[i*3+1] = FLOAT_TO_USHORT(green[i]); 1887 dst[i*3+2] = FLOAT_TO_USHORT(red[i]); 1888 } 1889 break; 1890 case GL_BGRA: 1891 for (i=0;i<n;i++) { 1892 dst[i*4+0] = FLOAT_TO_USHORT(blue[i]); 1893 dst[i*4+1] = FLOAT_TO_USHORT(green[i]); 1894 dst[i*4+2] = FLOAT_TO_USHORT(red[i]); 1895 dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]); 1896 } 1897 break; 1898 case GL_ABGR_EXT: 1899 for (i=0;i<n;i++) { 1900 dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]); 1901 dst[i*4+1] = FLOAT_TO_USHORT(blue[i]); 1902 dst[i*4+2] = FLOAT_TO_USHORT(green[i]); 1903 dst[i*4+3] = FLOAT_TO_USHORT(red[i]); 1904 } 1905 break; 1906 default: 1907 gl_problem(ctx, "bad format in gl_pack_rgba_span\n"); 1908 } 1909 if (packing->SwapBytes) { 1910 gl_swap2( (GLushort *) dst, n ); 1911 } 1912 } 1913 break; 1914 case GL_SHORT: 1915 { 1916 GLshort *dst = (GLshort *) destination; 1917 switch (format) { 1918 case GL_RED: 1919 for (i=0;i<n;i++) 1920 dst[i] = FLOAT_TO_SHORT(red[i]); 1921 break; 1922 case GL_GREEN: 1923 for (i=0;i<n;i++) 1924 dst[i] = FLOAT_TO_SHORT(green[i]); 1925 break; 1926 case GL_BLUE: 1927 for (i=0;i<n;i++) 1928 dst[i] = FLOAT_TO_SHORT(blue[i]); 1929 break; 1930 case GL_ALPHA: 1931 for (i=0;i<n;i++) 1932 dst[i] = FLOAT_TO_SHORT(alpha[i]); 1933 break; 1934 case GL_LUMINANCE: 1935 for (i=0;i<n;i++) 1936 dst[i] = FLOAT_TO_SHORT(luminance[i]); 1937 break; 1938 case GL_LUMINANCE_ALPHA: 1939 for (i=0;i<n;i++) { 1940 dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]); 1941 dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]); 1942 } 1943 break; 1944 case GL_RGB: 1945 for (i=0;i<n;i++) { 1946 dst[i*3+0] = FLOAT_TO_SHORT(red[i]); 1947 dst[i*3+1] = FLOAT_TO_SHORT(green[i]); 1948 dst[i*3+2] = FLOAT_TO_SHORT(blue[i]); 1949 } 1950 break; 1951 case GL_RGBA: 1952 for (i=0;i<n;i++) { 1953 dst[i*4+0] = FLOAT_TO_SHORT(red[i]); 1954 dst[i*4+1] = FLOAT_TO_SHORT(green[i]); 1955 dst[i*4+2] = FLOAT_TO_SHORT(blue[i]); 1956 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]); 1957 } 1958 break; 1959 case GL_BGR: 1960 for (i=0;i<n;i++) { 1961 dst[i*3+0] = FLOAT_TO_SHORT(blue[i]); 1962 dst[i*3+1] = FLOAT_TO_SHORT(green[i]); 1963 dst[i*3+2] = FLOAT_TO_SHORT(red[i]); 1964 } 1965 break; 1966 case GL_BGRA: 1967 for (i=0;i<n;i++) { 1968 dst[i*4+0] = FLOAT_TO_SHORT(blue[i]); 1969 dst[i*4+1] = FLOAT_TO_SHORT(green[i]); 1970 dst[i*4+2] = FLOAT_TO_SHORT(red[i]); 1971 dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]); 1972 } 1973 case GL_ABGR_EXT: 1974 for (i=0;i<n;i++) { 1975 dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]); 1976 dst[i*4+1] = FLOAT_TO_SHORT(blue[i]); 1977 dst[i*4+2] = FLOAT_TO_SHORT(green[i]); 1978 dst[i*4+3] = FLOAT_TO_SHORT(red[i]); 1979 } 1980 break; 1981 default: 1982 gl_problem(ctx, "bad format in gl_pack_rgba_span\n"); 1983 } 1984 if (packing->SwapBytes) { 1985 gl_swap2( (GLushort *) dst, n ); 1986 } 1987 } 1988 break; 1989 case GL_UNSIGNED_INT: 1990 { 1991 GLuint *dst = (GLuint *) destination; 1992 switch (format) { 1993 case GL_RED: 1994 for (i=0;i<n;i++) 1995 dst[i] = FLOAT_TO_UINT(red[i]); 1996 break; 1997 case GL_GREEN: 1998 for (i=0;i<n;i++) 1999 dst[i] = FLOAT_TO_UINT(green[i]); 2000 break; 2001 case GL_BLUE: 2002 for (i=0;i<n;i++) 2003 dst[i] = FLOAT_TO_UINT(blue[i]); 2004 break; 2005 case GL_ALPHA: 2006 for (i=0;i<n;i++) 2007 dst[i] = FLOAT_TO_UINT(alpha[i]); 2008 break; 2009 case GL_LUMINANCE: 2010 for (i=0;i<n;i++) 2011 dst[i] = FLOAT_TO_UINT(luminance[i]); 2012 break; 2013 case GL_LUMINANCE_ALPHA: 2014 for (i=0;i<n;i++) { 2015 dst[i*2+0] = FLOAT_TO_UINT(luminance[i]); 2016 dst[i*2+1] = FLOAT_TO_UINT(alpha[i]); 2017 } 2018 break; 2019 case GL_RGB: 2020 for (i=0;i<n;i++) { 2021 dst[i*3+0] = FLOAT_TO_UINT(red[i]); 2022 dst[i*3+1] = FLOAT_TO_UINT(green[i]); 2023 dst[i*3+2] = FLOAT_TO_UINT(blue[i]); 2024 } 2025 break; 2026 case GL_RGBA: 2027 for (i=0;i<n;i++) { 2028 dst[i*4+0] = FLOAT_TO_UINT(red[i]); 2029 dst[i*4+1] = FLOAT_TO_UINT(green[i]); 2030 dst[i*4+2] = FLOAT_TO_UINT(blue[i]); 2031 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]); 2032 } 2033 break; 2034 case GL_BGR: 2035 for (i=0;i<n;i++) { 2036 dst[i*3+0] = FLOAT_TO_UINT(blue[i]); 2037 dst[i*3+1] = FLOAT_TO_UINT(green[i]); 2038 dst[i*3+2] = FLOAT_TO_UINT(red[i]); 2039 } 2040 break; 2041 case GL_BGRA: 2042 for (i=0;i<n;i++) { 2043 dst[i*4+0] = FLOAT_TO_UINT(blue[i]); 2044 dst[i*4+1] = FLOAT_TO_UINT(green[i]); 2045 dst[i*4+2] = FLOAT_TO_UINT(red[i]); 2046 dst[i*4+3] = FLOAT_TO_UINT(alpha[i]); 2047 } 2048 break; 2049 case GL_ABGR_EXT: 2050 for (i=0;i<n;i++) { 2051 dst[i*4+0] = FLOAT_TO_UINT(alpha[i]); 2052 dst[i*4+1] = FLOAT_TO_UINT(blue[i]); 2053 dst[i*4+2] = FLOAT_TO_UINT(green[i]); 2054 dst[i*4+3] = FLOAT_TO_UINT(red[i]); 2055 } 2056 break; 2057 default: 2058 gl_problem(ctx, "bad format in gl_pack_rgba_span\n"); 2059 } 2060 if (packing->SwapBytes) { 2061 gl_swap4( (GLuint *) dst, n ); 2062 } 2063 } 2064 break; 2065 case GL_INT: 2066 { 2067 GLint *dst = (GLint *) destination; 2068 switch (format) { 2069 case GL_RED: 2070 for (i=0;i<n;i++) 2071 dst[i] = FLOAT_TO_INT(red[i]); 2072 break; 2073 case GL_GREEN: 2074 for (i=0;i<n;i++) 2075 dst[i] = FLOAT_TO_INT(green[i]); 2076 break; 2077 case GL_BLUE: 2078 for (i=0;i<n;i++) 2079 dst[i] = FLOAT_TO_INT(blue[i]); 2080 break; 2081 case GL_ALPHA: 2082 for (i=0;i<n;i++) 2083 dst[i] = FLOAT_TO_INT(alpha[i]); 2084 break; 2085 case GL_LUMINANCE: 2086 for (i=0;i<n;i++) 2087 dst[i] = FLOAT_TO_INT(luminance[i]); 2088 break; 2089 case GL_LUMINANCE_ALPHA: 2090 for (i=0;i<n;i++) { 2091 dst[i*2+0] = FLOAT_TO_INT(luminance[i]); 2092 dst[i*2+1] = FLOAT_TO_INT(alpha[i]); 2093 } 2094 break; 2095 case GL_RGB: 2096 for (i=0;i<n;i++) { 2097 dst[i*3+0] = FLOAT_TO_INT(red[i]); 2098 dst[i*3+1] = FLOAT_TO_INT(green[i]); 2099 dst[i*3+2] = FLOAT_TO_INT(blue[i]); 2100 } 2101 break; 2102 case GL_RGBA: 2103 for (i=0;i<n;i++) { 2104 dst[i*4+0] = FLOAT_TO_INT(red[i]); 2105 dst[i*4+1] = FLOAT_TO_INT(green[i]); 2106 dst[i*4+2] = FLOAT_TO_INT(blue[i]); 2107 dst[i*4+3] = FLOAT_TO_INT(alpha[i]); 2108 } 2109 break; 2110 case GL_BGR: 2111 for (i=0;i<n;i++) { 2112 dst[i*3+0] = FLOAT_TO_INT(blue[i]); 2113 dst[i*3+1] = FLOAT_TO_INT(green[i]); 2114 dst[i*3+2] = FLOAT_TO_INT(red[i]); 2115 } 2116 break; 2117 case GL_BGRA: 2118 for (i=0;i<n;i++) { 2119 dst[i*4+0] = FLOAT_TO_INT(blue[i]); 2120 dst[i*4+1] = FLOAT_TO_INT(green[i]); 2121 dst[i*4+2] = FLOAT_TO_INT(red[i]); 2122 dst[i*4+3] = FLOAT_TO_INT(alpha[i]); 2123 } 2124 break; 2125 case GL_ABGR_EXT: 2126 for (i=0;i<n;i++) { 2127 dst[i*4+0] = FLOAT_TO_INT(alpha[i]); 2128 dst[i*4+1] = FLOAT_TO_INT(blue[i]); 2129 dst[i*4+2] = FLOAT_TO_INT(green[i]); 2130 dst[i*4+3] = FLOAT_TO_INT(red[i]); 2131 } 2132 break; 2133 default: 2134 gl_problem(ctx, "bad format in gl_pack_rgba_span\n"); 2135 } 2136 if (packing->SwapBytes) { 2137 gl_swap4( (GLuint *) dst, n ); 2138 } 2139 } 2140 break; 2141 case GL_FLOAT: 2142 { 2143 GLfloat *dst = (GLfloat *) destination; 2144 switch (format) { 2145 case GL_RED: 2146 for (i=0;i<n;i++) 2147 dst[i] = red[i]; 2148 break; 2149 case GL_GREEN: 2150 for (i=0;i<n;i++) 2151 dst[i] = green[i]; 2152 break; 2153 case GL_BLUE: 2154 for (i=0;i<n;i++) 2155 dst[i] = blue[i]; 2156 break; 2157 case GL_ALPHA: 2158 for (i=0;i<n;i++) 2159 dst[i] = alpha[i]; 2160 break; 2161 case GL_LUMINANCE: 2162 for (i=0;i<n;i++) 2163 dst[i] = luminance[i]; 2164 break; 2165 case GL_LUMINANCE_ALPHA: 2166 for (i=0;i<n;i++) { 2167 dst[i*2+0] = luminance[i]; 2168 dst[i*2+1] = alpha[i]; 2169 } 2170 break; 2171 case GL_RGB: 2172 for (i=0;i<n;i++) { 2173 dst[i*3+0] = red[i]; 2174 dst[i*3+1] = green[i]; 2175 dst[i*3+2] = blue[i]; 2176 } 2177 break; 2178 case GL_RGBA: 2179 for (i=0;i<n;i++) { 2180 dst[i*4+0] = red[i]; 2181 dst[i*4+1] = green[i]; 2182 dst[i*4+2] = blue[i]; 2183 dst[i*4+3] = alpha[i]; 2184 } 2185 break; 2186 case GL_BGR: 2187 for (i=0;i<n;i++) { 2188 dst[i*3+0] = blue[i]; 2189 dst[i*3+1] = green[i]; 2190 dst[i*3+2] = red[i]; 2191 } 2192 break; 2193 case GL_BGRA: 2194 for (i=0;i<n;i++) { 2195 dst[i*4+0] = blue[i]; 2196 dst[i*4+1] = green[i]; 2197 dst[i*4+2] = red[i]; 2198 dst[i*4+3] = alpha[i]; 2199 } 2200 break; 2201 case GL_ABGR_EXT: 2202 for (i=0;i<n;i++) { 2203 dst[i*4+0] = alpha[i]; 2204 dst[i*4+1] = blue[i]; 2205 dst[i*4+2] = green[i]; 2206 dst[i*4+3] = red[i]; 2207 } 2208 break; 2209 default: 2210 gl_problem(ctx, "bad format in gl_pack_rgba_span\n"); 2211 } 2212 if (packing->SwapBytes) { 2213 gl_swap4( (GLuint *) dst, n ); 2214 } 2215 } 2216 break; 2217 case GL_UNSIGNED_BYTE_3_3_2: 2218 if (format == GL_RGB) { 2219 GLubyte *dst = (GLubyte *) destination; 2220 for (i=0;i<n;i++) { 2221 dst[i] = (((GLint) (red[i] * 7.0F)) << 5) 2222 | (((GLint) (green[i] * 7.0F)) << 2) 2223 | (((GLint) (blue[i] * 3.0F)) ); 2224 } 2225 } 2226 break; 2227 case GL_UNSIGNED_BYTE_2_3_3_REV: 2228 if (format == GL_RGB) { 2229 GLubyte *dst = (GLubyte *) destination; 2230 for (i=0;i<n;i++) { 2231 dst[i] = (((GLint) (red[i] * 7.0F)) ) 2232 | (((GLint) (green[i] * 7.0F)) << 3) 2233 | (((GLint) (blue[i] * 3.0F)) << 5); 2234 } 2235 } 2236 break; 2237 case GL_UNSIGNED_SHORT_5_6_5: 2238 if (format == GL_RGB) { 2239 GLushort *dst = (GLushort *) destination; 2240 for (i=0;i<n;i++) { 2241 dst[i] = (((GLint) (red[i] * 31.0F)) << 11) 2242 | (((GLint) (green[i] * 63.0F)) << 5) 2243 | (((GLint) (blue[i] * 31.0F)) ); 2244 } 2245 } 2246 break; 2247 case GL_UNSIGNED_SHORT_5_6_5_REV: 2248 if (format == GL_RGB) { 2249 GLushort *dst = (GLushort *) destination; 2250 for (i=0;i<n;i++) { 2251 dst[i] = (((GLint) (red[i] * 31.0F)) ) 2252 | (((GLint) (green[i] * 63.0F)) << 5) 2253 | (((GLint) (blue[i] * 31.0F)) << 11); 2254 } 2255 } 2256 break; 2257 case GL_UNSIGNED_SHORT_4_4_4_4: 2258 if (format == GL_RGB) { 2259 GLushort *dst = (GLushort *) destination; 2260 for (i=0;i<n;i++) { 2261 dst[i] = (((GLint) (red[i] * 15.0F)) << 12) 2262 | (((GLint) (green[i] * 15.0F)) << 8) 2263 | (((GLint) (blue[i] * 15.0F)) << 4) 2264 | (((GLint) (alpha[i] * 15.0F)) ); 2265 } 2266 } 2267 break; 2268 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 2269 if (format == GL_RGB) { 2270 GLushort *dst = (GLushort *) destination; 2271 for (i=0;i<n;i++) { 2272 dst[i] = (((GLint) (red[i] * 15.0F)) ) 2273 | (((GLint) (green[i] * 15.0F)) << 4) 2274 | (((GLint) (blue[i] * 15.0F)) << 8) 2275 | (((GLint) (alpha[i] * 15.0F)) << 12); 2276 } 2277 } 2278 break; 2279 case GL_UNSIGNED_SHORT_5_5_5_1: 2280 if (format == GL_RGB) { 2281 GLushort *dst = (GLushort *) destination; 2282 for (i=0;i<n;i++) { 2283 dst[i] = (((GLint) (red[i] * 31.0F)) << 11) 2284 | (((GLint) (green[i] * 31.0F)) << 6) 2285 | (((GLint) (blue[i] * 31.0F)) << 1) 2286 | (((GLint) (alpha[i] * 1.0F)) ); 2287 } 2288 } 2289 break; 2290 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 2291 if (format == GL_RGB) { 2292 GLushort *dst = (GLushort *) destination; 2293 for (i=0;i<n;i++) { 2294 dst[i] = (((GLint) (red[i] * 31.0F)) ) 2295 | (((GLint) (green[i] * 31.0F)) << 5) 2296 | (((GLint) (blue[i] * 31.0F)) << 10) 2297 | (((GLint) (alpha[i] * 1.0F)) << 15); 2298 } 2299 } 2300 break; 2301 case GL_UNSIGNED_INT_8_8_8_8: 2302 if (format == GL_RGBA) { 2303 GLuint *dst = (GLuint *) destination; 2304 for (i=0;i<n;i++) { 2305 dst[i] = (((GLuint) (red[i] * 255.0F)) << 24) 2306 | (((GLuint) (green[i] * 255.0F)) << 16) 2307 | (((GLuint) (blue[i] * 255.0F)) << 8) 2308 | (((GLuint) (alpha[i] * 255.0F)) ); 2309 } 2310 } 2311 else if (format == GL_BGRA) { 2312 GLuint *dst = (GLuint *) destination; 2313 for (i=0;i<n;i++) { 2314 dst[i] = (((GLuint) (blue[i] * 255.0F)) << 24) 2315 | (((GLuint) (green[i] * 255.0F)) << 16) 2316 | (((GLuint) (red[i] * 255.0F)) << 8) 2317 | (((GLuint) (alpha[i] * 255.0F)) ); 2318 } 2319 } 2320 else if (format == GL_ABGR_EXT) { 2321 GLuint *dst = (GLuint *) destination; 2322 for (i=0;i<n;i++) { 2323 dst[i] = (((GLuint) (alpha[i] * 255.0F)) << 24) 2324 | (((GLuint) (blue[i] * 255.0F)) << 16) 2325 | (((GLuint) (green[i] * 255.0F)) << 8) 2326 | (((GLuint) (red[i] * 255.0F)) ); 2327 } 2328 } 2329 break; 2330 case GL_UNSIGNED_INT_8_8_8_8_REV: 2331 if (format == GL_RGBA) { 2332 GLuint *dst = (GLuint *) destination; 2333 for (i=0;i<n;i++) { 2334 dst[i] = (((GLuint) (red[i] * 255.0F)) ) 2335 | (((GLuint) (green[i] * 255.0F)) << 8) 2336 | (((GLuint) (blue[i] * 255.0F)) << 16) 2337 | (((GLuint) (alpha[i] * 255.0F)) << 24); 2338 } 2339 } 2340 else if (format == GL_BGRA) { 2341 GLuint *dst = (GLuint *) destination; 2342 for (i=0;i<n;i++) { 2343 dst[i] = (((GLuint) (blue[i] * 255.0F)) ) 2344 | (((GLuint) (green[i] * 255.0F)) << 8) 2345 | (((GLuint) (red[i] * 255.0F)) << 16) 2346 | (((GLuint) (alpha[i] * 255.0F)) << 24); 2347 } 2348 } 2349 else if (format == GL_ABGR_EXT) { 2350 GLuint *dst = (GLuint *) destination; 2351 for (i=0;i<n;i++) { 2352 dst[i] = (((GLuint) (alpha[i] * 255.0F)) ) 2353 | (((GLuint) (blue[i] * 255.0F)) << 8) 2354 | (((GLuint) (green[i] * 255.0F)) << 16) 2355 | (((GLuint) (red[i] * 255.0F)) << 24); 2356 } 2357 } 2358 break; 2359 case GL_UNSIGNED_INT_10_10_10_2: 2360 if (format == GL_RGBA) { 2361 GLuint *dst = (GLuint *) destination; 2362 for (i=0;i<n;i++) { 2363 dst[i] = (((GLuint) (red[i] * 1023.0F)) << 22) 2364 | (((GLuint) (green[i] * 1023.0F)) << 12) 2365 | (((GLuint) (blue[i] * 1023.0F)) << 2) 2366 | (((GLuint) (alpha[i] * 3.0F)) ); 2367 } 2368 } 2369 else if (format == GL_BGRA) { 2370 GLuint *dst = (GLuint *) destination; 2371 for (i=0;i<n;i++) { 2372 dst[i] = (((GLuint) (blue[i] * 1023.0F)) << 22) 2373 | (((GLuint) (green[i] * 1023.0F)) << 12) 2374 | (((GLuint) (red[i] * 1023.0F)) << 2) 2375 | (((GLuint) (alpha[i] * 3.0F)) ); 2376 } 2377 } 2378 else if (format == GL_ABGR_EXT) { 2379 GLuint *dst = (GLuint *) destination; 2380 for (i=0;i<n;i++) { 2381 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) << 22) 2382 | (((GLuint) (blue[i] * 1023.0F)) << 12) 2383 | (((GLuint) (green[i] * 1023.0F)) << 2) 2384 | (((GLuint) (red[i] * 3.0F)) ); 2385 } 2386 } 2387 break; 2388 case GL_UNSIGNED_INT_2_10_10_10_REV: 2389 if (format == GL_RGBA) { 2390 GLuint *dst = (GLuint *) destination; 2391 for (i=0;i<n;i++) { 2392 dst[i] = (((GLuint) (red[i] * 1023.0F)) ) 2393 | (((GLuint) (green[i] * 1023.0F)) << 10) 2394 | (((GLuint) (blue[i] * 1023.0F)) << 20) 2395 | (((GLuint) (alpha[i] * 3.0F)) << 30); 2396 } 2397 } 2398 else if (format == GL_BGRA) { 2399 GLuint *dst = (GLuint *) destination; 2400 for (i=0;i<n;i++) { 2401 dst[i] = (((GLuint) (blue[i] * 1023.0F)) ) 2402 | (((GLuint) (green[i] * 1023.0F)) << 10) 2403 | (((GLuint) (red[i] * 1023.0F)) << 20) 2404 | (((GLuint) (alpha[i] * 3.0F)) << 30); 2405 } 2406 } 2407 else if (format == GL_ABGR_EXT) { 2408 GLuint *dst = (GLuint *) destination; 2409 for (i=0;i<n;i++) { 2410 dst[i] = (((GLuint) (alpha[i] * 1023.0F)) ) 2411 | (((GLuint) (blue[i] * 1023.0F)) << 10) 2412 | (((GLuint) (green[i] * 1023.0F)) << 20) 2413 | (((GLuint) (red[i] * 3.0F)) << 30); 2414 } 2415 } 2416 break; 2417 default: 2418 gl_problem( ctx, "bad type in gl_pack_rgba_span" ); 2419 } 2420 } 2421} 2422