image.c revision 3363e87d7d5b3f19294fc770dc7c506c26646010
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.5 4 * 5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved. 7 * 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: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions of the Software. 17 * 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. 24 */ 25 26 27/** 28 * \file image.c 29 * Image handling. 30 */ 31 32 33#include "glheader.h" 34#include "colormac.h" 35#include "image.h" 36#include "imports.h" 37#include "macros.h" 38#include "mfeatures.h" 39#include "mtypes.h" 40 41 42 43/** 44 * \return GL_TRUE if type is packed pixel type, GL_FALSE otherwise. 45 */ 46GLboolean 47_mesa_type_is_packed(GLenum type) 48{ 49 switch (type) { 50 case GL_UNSIGNED_BYTE_3_3_2: 51 case GL_UNSIGNED_BYTE_2_3_3_REV: 52 case MESA_UNSIGNED_BYTE_4_4: 53 case GL_UNSIGNED_SHORT_5_6_5: 54 case GL_UNSIGNED_SHORT_5_6_5_REV: 55 case GL_UNSIGNED_SHORT_4_4_4_4: 56 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 57 case GL_UNSIGNED_SHORT_5_5_5_1: 58 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 59 case GL_UNSIGNED_INT_8_8_8_8: 60 case GL_UNSIGNED_INT_8_8_8_8_REV: 61 case GL_UNSIGNED_INT_10_10_10_2: 62 case GL_UNSIGNED_INT_2_10_10_10_REV: 63 case GL_UNSIGNED_SHORT_8_8_MESA: 64 case GL_UNSIGNED_SHORT_8_8_REV_MESA: 65 case GL_UNSIGNED_INT_24_8_EXT: 66 case GL_UNSIGNED_INT_5_9_9_9_REV: 67 case GL_UNSIGNED_INT_10F_11F_11F_REV: 68 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: 69 return GL_TRUE; 70 } 71 72 return GL_FALSE; 73} 74 75 76 77/** 78 * Flip the order of the 2 bytes in each word in the given array. 79 * 80 * \param p array. 81 * \param n number of words. 82 */ 83void 84_mesa_swap2( GLushort *p, GLuint n ) 85{ 86 GLuint i; 87 for (i = 0; i < n; i++) { 88 p[i] = (p[i] >> 8) | ((p[i] << 8) & 0xff00); 89 } 90} 91 92 93 94/* 95 * Flip the order of the 4 bytes in each word in the given array. 96 */ 97void 98_mesa_swap4( GLuint *p, GLuint n ) 99{ 100 GLuint i, a, b; 101 for (i = 0; i < n; i++) { 102 b = p[i]; 103 a = (b >> 24) 104 | ((b >> 8) & 0xff00) 105 | ((b << 8) & 0xff0000) 106 | ((b << 24) & 0xff000000); 107 p[i] = a; 108 } 109} 110 111 112/** 113 * Get the size of a GL data type. 114 * 115 * \param type GL data type. 116 * 117 * \return the size, in bytes, of the given data type, 0 if a GL_BITMAP, or -1 118 * if an invalid type enum. 119 */ 120GLint 121_mesa_sizeof_type( GLenum type ) 122{ 123 switch (type) { 124 case GL_BITMAP: 125 return 0; 126 case GL_UNSIGNED_BYTE: 127 return sizeof(GLubyte); 128 case GL_BYTE: 129 return sizeof(GLbyte); 130 case GL_UNSIGNED_SHORT: 131 return sizeof(GLushort); 132 case GL_SHORT: 133 return sizeof(GLshort); 134 case GL_UNSIGNED_INT: 135 return sizeof(GLuint); 136 case GL_INT: 137 return sizeof(GLint); 138 case GL_FLOAT: 139 return sizeof(GLfloat); 140 case GL_DOUBLE: 141 return sizeof(GLdouble); 142 case GL_HALF_FLOAT_ARB: 143 return sizeof(GLhalfARB); 144 case GL_FIXED: 145 return sizeof(GLfixed); 146 default: 147 return -1; 148 } 149} 150 151 152/** 153 * Same as _mesa_sizeof_type() but also accepting the packed pixel 154 * format data types. 155 */ 156GLint 157_mesa_sizeof_packed_type( GLenum type ) 158{ 159 switch (type) { 160 case GL_BITMAP: 161 return 0; 162 case GL_UNSIGNED_BYTE: 163 return sizeof(GLubyte); 164 case GL_BYTE: 165 return sizeof(GLbyte); 166 case GL_UNSIGNED_SHORT: 167 return sizeof(GLushort); 168 case GL_SHORT: 169 return sizeof(GLshort); 170 case GL_UNSIGNED_INT: 171 return sizeof(GLuint); 172 case GL_INT: 173 return sizeof(GLint); 174 case GL_HALF_FLOAT_ARB: 175 return sizeof(GLhalfARB); 176 case GL_FLOAT: 177 return sizeof(GLfloat); 178 case GL_UNSIGNED_BYTE_3_3_2: 179 case GL_UNSIGNED_BYTE_2_3_3_REV: 180 case MESA_UNSIGNED_BYTE_4_4: 181 return sizeof(GLubyte); 182 case GL_UNSIGNED_SHORT_5_6_5: 183 case GL_UNSIGNED_SHORT_5_6_5_REV: 184 case GL_UNSIGNED_SHORT_4_4_4_4: 185 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 186 case GL_UNSIGNED_SHORT_5_5_5_1: 187 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 188 case GL_UNSIGNED_SHORT_8_8_MESA: 189 case GL_UNSIGNED_SHORT_8_8_REV_MESA: 190 return sizeof(GLushort); 191 case GL_UNSIGNED_INT_8_8_8_8: 192 case GL_UNSIGNED_INT_8_8_8_8_REV: 193 case GL_UNSIGNED_INT_10_10_10_2: 194 case GL_UNSIGNED_INT_2_10_10_10_REV: 195 case GL_UNSIGNED_INT_24_8_EXT: 196 case GL_UNSIGNED_INT_5_9_9_9_REV: 197 case GL_UNSIGNED_INT_10F_11F_11F_REV: 198 return sizeof(GLuint); 199 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: 200 return 8; 201 default: 202 return -1; 203 } 204} 205 206 207/** 208 * Get the number of components in a pixel format. 209 * 210 * \param format pixel format. 211 * 212 * \return the number of components in the given format, or -1 if a bad format. 213 */ 214GLint 215_mesa_components_in_format( GLenum format ) 216{ 217 switch (format) { 218 case GL_COLOR_INDEX: 219 case GL_STENCIL_INDEX: 220 case GL_DEPTH_COMPONENT: 221 case GL_RED: 222 case GL_RED_INTEGER_EXT: 223 case GL_GREEN: 224 case GL_GREEN_INTEGER_EXT: 225 case GL_BLUE: 226 case GL_BLUE_INTEGER_EXT: 227 case GL_ALPHA: 228 case GL_ALPHA_INTEGER_EXT: 229 case GL_LUMINANCE: 230 case GL_LUMINANCE_INTEGER_EXT: 231 case GL_INTENSITY: 232 return 1; 233 234 case GL_LUMINANCE_ALPHA: 235 case GL_LUMINANCE_ALPHA_INTEGER_EXT: 236 case GL_RG: 237 case GL_YCBCR_MESA: 238 case GL_DEPTH_STENCIL_EXT: 239 case GL_DUDV_ATI: 240 case GL_DU8DV8_ATI: 241 case GL_RG_INTEGER: 242 return 2; 243 244 case GL_RGB: 245 case GL_BGR: 246 case GL_RGB_INTEGER_EXT: 247 case GL_BGR_INTEGER_EXT: 248 return 3; 249 250 case GL_RGBA: 251 case GL_BGRA: 252 case GL_ABGR_EXT: 253 case GL_RGBA_INTEGER_EXT: 254 case GL_BGRA_INTEGER_EXT: 255 return 4; 256 257 default: 258 return -1; 259 } 260} 261 262 263/** 264 * Get the bytes per pixel of pixel format type pair. 265 * 266 * \param format pixel format. 267 * \param type pixel type. 268 * 269 * \return bytes per pixel, or -1 if a bad format or type was given. 270 */ 271GLint 272_mesa_bytes_per_pixel( GLenum format, GLenum type ) 273{ 274 GLint comps = _mesa_components_in_format( format ); 275 if (comps < 0) 276 return -1; 277 278 switch (type) { 279 case GL_BITMAP: 280 return 0; /* special case */ 281 case GL_BYTE: 282 case GL_UNSIGNED_BYTE: 283 return comps * sizeof(GLubyte); 284 case GL_SHORT: 285 case GL_UNSIGNED_SHORT: 286 return comps * sizeof(GLshort); 287 case GL_INT: 288 case GL_UNSIGNED_INT: 289 return comps * sizeof(GLint); 290 case GL_FLOAT: 291 return comps * sizeof(GLfloat); 292 case GL_HALF_FLOAT_ARB: 293 return comps * sizeof(GLhalfARB); 294 case GL_UNSIGNED_BYTE_3_3_2: 295 case GL_UNSIGNED_BYTE_2_3_3_REV: 296 if (format == GL_RGB || format == GL_BGR || 297 format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT) 298 return sizeof(GLubyte); 299 else 300 return -1; /* error */ 301 case GL_UNSIGNED_SHORT_5_6_5: 302 case GL_UNSIGNED_SHORT_5_6_5_REV: 303 if (format == GL_RGB || format == GL_BGR || 304 format == GL_RGB_INTEGER_EXT || format == GL_BGR_INTEGER_EXT) 305 return sizeof(GLushort); 306 else 307 return -1; /* error */ 308 case GL_UNSIGNED_SHORT_4_4_4_4: 309 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 310 case GL_UNSIGNED_SHORT_5_5_5_1: 311 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 312 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT || 313 format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) 314 return sizeof(GLushort); 315 else 316 return -1; 317 case GL_UNSIGNED_INT_8_8_8_8: 318 case GL_UNSIGNED_INT_8_8_8_8_REV: 319 case GL_UNSIGNED_INT_10_10_10_2: 320 case GL_UNSIGNED_INT_2_10_10_10_REV: 321 if (format == GL_RGBA || format == GL_BGRA || format == GL_ABGR_EXT || 322 format == GL_RGBA_INTEGER_EXT || format == GL_BGRA_INTEGER_EXT) 323 return sizeof(GLuint); 324 else 325 return -1; 326 case GL_UNSIGNED_SHORT_8_8_MESA: 327 case GL_UNSIGNED_SHORT_8_8_REV_MESA: 328 if (format == GL_YCBCR_MESA) 329 return sizeof(GLushort); 330 else 331 return -1; 332 case GL_UNSIGNED_INT_24_8_EXT: 333 if (format == GL_DEPTH_STENCIL_EXT) 334 return sizeof(GLuint); 335 else 336 return -1; 337 case GL_UNSIGNED_INT_5_9_9_9_REV: 338 if (format == GL_RGB) 339 return sizeof(GLuint); 340 else 341 return -1; 342 case GL_UNSIGNED_INT_10F_11F_11F_REV: 343 if (format == GL_RGB) 344 return sizeof(GLuint); 345 else 346 return -1; 347 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV: 348 if (format == GL_DEPTH_STENCIL) 349 return 8; 350 else 351 return -1; 352 default: 353 return -1; 354 } 355} 356 357 358/** 359 * Test for a legal pixel format and type. 360 * 361 * \param format pixel format. 362 * \param type pixel type. 363 * 364 * \return GL_TRUE if the given pixel format and type are legal, or GL_FALSE 365 * otherwise. 366 */ 367GLboolean 368_mesa_is_legal_format_and_type(const struct gl_context *ctx, 369 GLenum format, GLenum type) 370{ 371 switch (format) { 372 case GL_COLOR_INDEX: 373 case GL_STENCIL_INDEX: 374 switch (type) { 375 case GL_BITMAP: 376 case GL_BYTE: 377 case GL_UNSIGNED_BYTE: 378 case GL_SHORT: 379 case GL_UNSIGNED_SHORT: 380 case GL_INT: 381 case GL_UNSIGNED_INT: 382 case GL_FLOAT: 383 return GL_TRUE; 384 case GL_HALF_FLOAT_ARB: 385 return ctx->Extensions.ARB_half_float_pixel; 386 default: 387 return GL_FALSE; 388 } 389 case GL_RED: 390 case GL_GREEN: 391 case GL_BLUE: 392 case GL_ALPHA: 393#if 0 /* not legal! see table 3.6 of the 1.5 spec */ 394 case GL_INTENSITY: 395#endif 396 case GL_LUMINANCE: 397 case GL_LUMINANCE_ALPHA: 398 case GL_DEPTH_COMPONENT: 399 switch (type) { 400 case GL_BYTE: 401 case GL_UNSIGNED_BYTE: 402 case GL_SHORT: 403 case GL_UNSIGNED_SHORT: 404 case GL_INT: 405 case GL_UNSIGNED_INT: 406 case GL_FLOAT: 407 return GL_TRUE; 408 case GL_HALF_FLOAT_ARB: 409 return ctx->Extensions.ARB_half_float_pixel; 410 default: 411 return GL_FALSE; 412 } 413 case GL_RG: 414 if (!ctx->Extensions.ARB_texture_rg) 415 return GL_FALSE; 416 417 switch (type) { 418 case GL_BYTE: 419 case GL_UNSIGNED_BYTE: 420 case GL_SHORT: 421 case GL_UNSIGNED_SHORT: 422 case GL_INT: 423 case GL_UNSIGNED_INT: 424 case GL_FLOAT: 425 return GL_TRUE; 426 case GL_HALF_FLOAT_ARB: 427 return ctx->Extensions.ARB_half_float_pixel; 428 default: 429 return GL_FALSE; 430 } 431 case GL_RGB: 432 switch (type) { 433 case GL_BYTE: 434 case GL_UNSIGNED_BYTE: 435 case GL_SHORT: 436 case GL_UNSIGNED_SHORT: 437 case GL_INT: 438 case GL_UNSIGNED_INT: 439 case GL_FLOAT: 440 case GL_UNSIGNED_BYTE_3_3_2: 441 case GL_UNSIGNED_BYTE_2_3_3_REV: 442 case GL_UNSIGNED_SHORT_5_6_5: 443 case GL_UNSIGNED_SHORT_5_6_5_REV: 444 return GL_TRUE; 445 case GL_HALF_FLOAT_ARB: 446 return ctx->Extensions.ARB_half_float_pixel; 447 case GL_UNSIGNED_INT_5_9_9_9_REV: 448 return ctx->Extensions.EXT_texture_shared_exponent; 449 case GL_UNSIGNED_INT_10F_11F_11F_REV: 450 return ctx->Extensions.EXT_packed_float; 451 default: 452 return GL_FALSE; 453 } 454 case GL_BGR: 455 switch (type) { 456 /* NOTE: no packed types are supported with BGR. That's 457 * intentional, according to the GL spec. 458 */ 459 case GL_BYTE: 460 case GL_UNSIGNED_BYTE: 461 case GL_SHORT: 462 case GL_UNSIGNED_SHORT: 463 case GL_INT: 464 case GL_UNSIGNED_INT: 465 case GL_FLOAT: 466 return GL_TRUE; 467 case GL_HALF_FLOAT_ARB: 468 return ctx->Extensions.ARB_half_float_pixel; 469 default: 470 return GL_FALSE; 471 } 472 case GL_RGBA: 473 case GL_BGRA: 474 case GL_ABGR_EXT: 475 switch (type) { 476 case GL_BYTE: 477 case GL_UNSIGNED_BYTE: 478 case GL_SHORT: 479 case GL_UNSIGNED_SHORT: 480 case GL_INT: 481 case GL_UNSIGNED_INT: 482 case GL_FLOAT: 483 case GL_UNSIGNED_SHORT_4_4_4_4: 484 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 485 case GL_UNSIGNED_SHORT_5_5_5_1: 486 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 487 case GL_UNSIGNED_INT_8_8_8_8: 488 case GL_UNSIGNED_INT_8_8_8_8_REV: 489 case GL_UNSIGNED_INT_10_10_10_2: 490 case GL_UNSIGNED_INT_2_10_10_10_REV: 491 return GL_TRUE; 492 case GL_HALF_FLOAT_ARB: 493 return ctx->Extensions.ARB_half_float_pixel; 494 default: 495 return GL_FALSE; 496 } 497 case GL_YCBCR_MESA: 498 if (type == GL_UNSIGNED_SHORT_8_8_MESA || 499 type == GL_UNSIGNED_SHORT_8_8_REV_MESA) 500 return GL_TRUE; 501 else 502 return GL_FALSE; 503 case GL_DEPTH_STENCIL_EXT: 504 if ((ctx->Extensions.EXT_packed_depth_stencil && 505 type == GL_UNSIGNED_INT_24_8_EXT) || 506 (ctx->Extensions.ARB_depth_buffer_float && 507 type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV)) 508 return GL_TRUE; 509 else 510 return GL_FALSE; 511 case GL_DUDV_ATI: 512 case GL_DU8DV8_ATI: 513 switch (type) { 514 case GL_BYTE: 515 case GL_UNSIGNED_BYTE: 516 case GL_SHORT: 517 case GL_UNSIGNED_SHORT: 518 case GL_INT: 519 case GL_UNSIGNED_INT: 520 case GL_FLOAT: 521 return GL_TRUE; 522 default: 523 return GL_FALSE; 524 } 525 526 /* integer-valued formats */ 527 case GL_RED_INTEGER_EXT: 528 case GL_GREEN_INTEGER_EXT: 529 case GL_BLUE_INTEGER_EXT: 530 case GL_ALPHA_INTEGER_EXT: 531 case GL_RG_INTEGER: 532 switch (type) { 533 case GL_BYTE: 534 case GL_UNSIGNED_BYTE: 535 case GL_SHORT: 536 case GL_UNSIGNED_SHORT: 537 case GL_INT: 538 case GL_UNSIGNED_INT: 539 return ctx->VersionMajor >= 3 || 540 ctx->Extensions.EXT_texture_integer; 541 default: 542 return GL_FALSE; 543 } 544 545 case GL_RGB_INTEGER_EXT: 546 switch (type) { 547 case GL_BYTE: 548 case GL_UNSIGNED_BYTE: 549 case GL_SHORT: 550 case GL_UNSIGNED_SHORT: 551 case GL_INT: 552 case GL_UNSIGNED_INT: 553 return ctx->VersionMajor >= 3 || 554 ctx->Extensions.EXT_texture_integer; 555 case GL_UNSIGNED_BYTE_3_3_2: 556 case GL_UNSIGNED_BYTE_2_3_3_REV: 557 case GL_UNSIGNED_SHORT_5_6_5: 558 case GL_UNSIGNED_SHORT_5_6_5_REV: 559 return ctx->Extensions.ARB_texture_rgb10_a2ui; 560 default: 561 return GL_FALSE; 562 } 563 564 case GL_BGR_INTEGER_EXT: 565 switch (type) { 566 case GL_BYTE: 567 case GL_UNSIGNED_BYTE: 568 case GL_SHORT: 569 case GL_UNSIGNED_SHORT: 570 case GL_INT: 571 case GL_UNSIGNED_INT: 572 /* NOTE: no packed formats w/ BGR format */ 573 return ctx->VersionMajor >= 3 || 574 ctx->Extensions.EXT_texture_integer; 575 default: 576 return GL_FALSE; 577 } 578 579 case GL_RGBA_INTEGER_EXT: 580 case GL_BGRA_INTEGER_EXT: 581 switch (type) { 582 case GL_BYTE: 583 case GL_UNSIGNED_BYTE: 584 case GL_SHORT: 585 case GL_UNSIGNED_SHORT: 586 case GL_INT: 587 case GL_UNSIGNED_INT: 588 return ctx->VersionMajor >= 3 || 589 ctx->Extensions.EXT_texture_integer; 590 case GL_UNSIGNED_SHORT_4_4_4_4: 591 case GL_UNSIGNED_SHORT_4_4_4_4_REV: 592 case GL_UNSIGNED_SHORT_5_5_5_1: 593 case GL_UNSIGNED_SHORT_1_5_5_5_REV: 594 case GL_UNSIGNED_INT_8_8_8_8: 595 case GL_UNSIGNED_INT_8_8_8_8_REV: 596 case GL_UNSIGNED_INT_10_10_10_2: 597 case GL_UNSIGNED_INT_2_10_10_10_REV: 598 return ctx->Extensions.ARB_texture_rgb10_a2ui; 599 default: 600 return GL_FALSE; 601 } 602 603 case GL_LUMINANCE_INTEGER_EXT: 604 case GL_LUMINANCE_ALPHA_INTEGER_EXT: 605 switch (type) { 606 case GL_BYTE: 607 case GL_UNSIGNED_BYTE: 608 case GL_SHORT: 609 case GL_UNSIGNED_SHORT: 610 case GL_INT: 611 case GL_UNSIGNED_INT: 612 return ctx->Extensions.EXT_texture_integer; 613 default: 614 return GL_FALSE; 615 } 616 617 default: 618 ; /* fall-through */ 619 } 620 return GL_FALSE; 621} 622 623 624/** 625 * Test if the given image format is a color/RGBA format (i.e., not color 626 * index, depth, stencil, etc). 627 * \param format the image format value (may by an internal texture format) 628 * \return GL_TRUE if its a color/RGBA format, GL_FALSE otherwise. 629 */ 630GLboolean 631_mesa_is_color_format(GLenum format) 632{ 633 switch (format) { 634 case GL_RED: 635 case GL_GREEN: 636 case GL_BLUE: 637 case GL_ALPHA: 638 case GL_ALPHA4: 639 case GL_ALPHA8: 640 case GL_ALPHA12: 641 case GL_ALPHA16: 642 case 1: 643 case GL_LUMINANCE: 644 case GL_LUMINANCE4: 645 case GL_LUMINANCE8: 646 case GL_LUMINANCE12: 647 case GL_LUMINANCE16: 648 case 2: 649 case GL_LUMINANCE_ALPHA: 650 case GL_LUMINANCE4_ALPHA4: 651 case GL_LUMINANCE6_ALPHA2: 652 case GL_LUMINANCE8_ALPHA8: 653 case GL_LUMINANCE12_ALPHA4: 654 case GL_LUMINANCE12_ALPHA12: 655 case GL_LUMINANCE16_ALPHA16: 656 case GL_INTENSITY: 657 case GL_INTENSITY4: 658 case GL_INTENSITY8: 659 case GL_INTENSITY12: 660 case GL_INTENSITY16: 661 case GL_R8: 662 case GL_R16: 663 case GL_RG: 664 case GL_RG8: 665 case GL_RG16: 666 case 3: 667 case GL_RGB: 668 case GL_BGR: 669 case GL_R3_G3_B2: 670 case GL_RGB4: 671 case GL_RGB5: 672 case GL_RGB8: 673 case GL_RGB10: 674 case GL_RGB12: 675 case GL_RGB16: 676 case 4: 677 case GL_ABGR_EXT: 678 case GL_RGBA: 679 case GL_BGRA: 680 case GL_RGBA2: 681 case GL_RGBA4: 682 case GL_RGB5_A1: 683 case GL_RGBA8: 684 case GL_RGB10_A2: 685 case GL_RGBA12: 686 case GL_RGBA16: 687 /* float texture formats */ 688 case GL_ALPHA16F_ARB: 689 case GL_ALPHA32F_ARB: 690 case GL_LUMINANCE16F_ARB: 691 case GL_LUMINANCE32F_ARB: 692 case GL_LUMINANCE_ALPHA16F_ARB: 693 case GL_LUMINANCE_ALPHA32F_ARB: 694 case GL_INTENSITY16F_ARB: 695 case GL_INTENSITY32F_ARB: 696 case GL_R16F: 697 case GL_R32F: 698 case GL_RG16F: 699 case GL_RG32F: 700 case GL_RGB16F_ARB: 701 case GL_RGB32F_ARB: 702 case GL_RGBA16F_ARB: 703 case GL_RGBA32F_ARB: 704 /* compressed formats */ 705 case GL_COMPRESSED_ALPHA: 706 case GL_COMPRESSED_LUMINANCE: 707 case GL_COMPRESSED_LUMINANCE_ALPHA: 708 case GL_COMPRESSED_INTENSITY: 709 case GL_COMPRESSED_RED: 710 case GL_COMPRESSED_RG: 711 case GL_COMPRESSED_RGB: 712 case GL_COMPRESSED_RGBA: 713 case GL_RGB_S3TC: 714 case GL_RGB4_S3TC: 715 case GL_RGBA_S3TC: 716 case GL_RGBA4_S3TC: 717 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: 718 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: 719 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: 720 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: 721 case GL_COMPRESSED_RGB_FXT1_3DFX: 722 case GL_COMPRESSED_RGBA_FXT1_3DFX: 723#if FEATURE_EXT_texture_sRGB 724 case GL_SRGB_EXT: 725 case GL_SRGB8_EXT: 726 case GL_SRGB_ALPHA_EXT: 727 case GL_SRGB8_ALPHA8_EXT: 728 case GL_SLUMINANCE_ALPHA_EXT: 729 case GL_SLUMINANCE8_ALPHA8_EXT: 730 case GL_SLUMINANCE_EXT: 731 case GL_SLUMINANCE8_EXT: 732 case GL_COMPRESSED_SRGB_EXT: 733 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT: 734 case GL_COMPRESSED_SRGB_ALPHA_EXT: 735 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: 736 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT: 737 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: 738 case GL_COMPRESSED_SLUMINANCE_EXT: 739 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT: 740#endif /* FEATURE_EXT_texture_sRGB */ 741 case GL_COMPRESSED_RED_RGTC1: 742 case GL_COMPRESSED_SIGNED_RED_RGTC1: 743 case GL_COMPRESSED_RG_RGTC2: 744 case GL_COMPRESSED_SIGNED_RG_RGTC2: 745 case GL_COMPRESSED_LUMINANCE_LATC1_EXT: 746 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT: 747 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT: 748 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT: 749 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI: 750 case GL_ETC1_RGB8_OES: 751 /* generic integer formats */ 752 case GL_RED_INTEGER_EXT: 753 case GL_GREEN_INTEGER_EXT: 754 case GL_BLUE_INTEGER_EXT: 755 case GL_ALPHA_INTEGER_EXT: 756 case GL_RGB_INTEGER_EXT: 757 case GL_RGBA_INTEGER_EXT: 758 case GL_BGR_INTEGER_EXT: 759 case GL_BGRA_INTEGER_EXT: 760 case GL_RG_INTEGER: 761 case GL_LUMINANCE_INTEGER_EXT: 762 case GL_LUMINANCE_ALPHA_INTEGER_EXT: 763 /* sized integer formats */ 764 case GL_RGBA32UI_EXT: 765 case GL_RGB32UI_EXT: 766 case GL_RG32UI: 767 case GL_R32UI: 768 case GL_ALPHA32UI_EXT: 769 case GL_INTENSITY32UI_EXT: 770 case GL_LUMINANCE32UI_EXT: 771 case GL_LUMINANCE_ALPHA32UI_EXT: 772 case GL_RGBA16UI_EXT: 773 case GL_RGB16UI_EXT: 774 case GL_RG16UI: 775 case GL_R16UI: 776 case GL_ALPHA16UI_EXT: 777 case GL_INTENSITY16UI_EXT: 778 case GL_LUMINANCE16UI_EXT: 779 case GL_LUMINANCE_ALPHA16UI_EXT: 780 case GL_RGBA8UI_EXT: 781 case GL_RGB8UI_EXT: 782 case GL_RG8UI: 783 case GL_R8UI: 784 case GL_ALPHA8UI_EXT: 785 case GL_INTENSITY8UI_EXT: 786 case GL_LUMINANCE8UI_EXT: 787 case GL_LUMINANCE_ALPHA8UI_EXT: 788 case GL_RGBA32I_EXT: 789 case GL_RGB32I_EXT: 790 case GL_RG32I: 791 case GL_R32I: 792 case GL_ALPHA32I_EXT: 793 case GL_INTENSITY32I_EXT: 794 case GL_LUMINANCE32I_EXT: 795 case GL_LUMINANCE_ALPHA32I_EXT: 796 case GL_RGBA16I_EXT: 797 case GL_RGB16I_EXT: 798 case GL_RG16I: 799 case GL_R16I: 800 case GL_ALPHA16I_EXT: 801 case GL_INTENSITY16I_EXT: 802 case GL_LUMINANCE16I_EXT: 803 case GL_LUMINANCE_ALPHA16I_EXT: 804 case GL_RGBA8I_EXT: 805 case GL_RGB8I_EXT: 806 case GL_RG8I: 807 case GL_R8I: 808 case GL_ALPHA8I_EXT: 809 case GL_INTENSITY8I_EXT: 810 case GL_LUMINANCE8I_EXT: 811 case GL_LUMINANCE_ALPHA8I_EXT: 812 /* signed, normalized texture formats */ 813 case GL_RED_SNORM: 814 case GL_R8_SNORM: 815 case GL_R16_SNORM: 816 case GL_RG_SNORM: 817 case GL_RG8_SNORM: 818 case GL_RG16_SNORM: 819 case GL_RGB_SNORM: 820 case GL_RGB8_SNORM: 821 case GL_RGB16_SNORM: 822 case GL_RGBA_SNORM: 823 case GL_RGBA8_SNORM: 824 case GL_RGBA16_SNORM: 825 case GL_ALPHA_SNORM: 826 case GL_ALPHA8_SNORM: 827 case GL_ALPHA16_SNORM: 828 case GL_LUMINANCE_SNORM: 829 case GL_LUMINANCE8_SNORM: 830 case GL_LUMINANCE16_SNORM: 831 case GL_LUMINANCE_ALPHA_SNORM: 832 case GL_LUMINANCE8_ALPHA8_SNORM: 833 case GL_LUMINANCE16_ALPHA16_SNORM: 834 case GL_INTENSITY_SNORM: 835 case GL_INTENSITY8_SNORM: 836 case GL_INTENSITY16_SNORM: 837 case GL_RGB9_E5: 838 case GL_R11F_G11F_B10F: 839 case GL_RGB10_A2UI: 840 return GL_TRUE; 841 case GL_YCBCR_MESA: /* not considered to be RGB */ 842 /* fall-through */ 843 default: 844 return GL_FALSE; 845 } 846} 847 848 849/** 850 * Test if the given image format is a depth component format. 851 */ 852GLboolean 853_mesa_is_depth_format(GLenum format) 854{ 855 switch (format) { 856 case GL_DEPTH_COMPONENT: 857 case GL_DEPTH_COMPONENT16: 858 case GL_DEPTH_COMPONENT24: 859 case GL_DEPTH_COMPONENT32: 860 case GL_DEPTH_COMPONENT32F: 861 return GL_TRUE; 862 default: 863 return GL_FALSE; 864 } 865} 866 867 868/** 869 * Test if the given image format is a stencil format. 870 */ 871GLboolean 872_mesa_is_stencil_format(GLenum format) 873{ 874 switch (format) { 875 case GL_STENCIL_INDEX: 876 return GL_TRUE; 877 default: 878 return GL_FALSE; 879 } 880} 881 882 883/** 884 * Test if the given image format is a YCbCr format. 885 */ 886GLboolean 887_mesa_is_ycbcr_format(GLenum format) 888{ 889 switch (format) { 890 case GL_YCBCR_MESA: 891 return GL_TRUE; 892 default: 893 return GL_FALSE; 894 } 895} 896 897 898/** 899 * Test if the given image format is a depth+stencil format. 900 */ 901GLboolean 902_mesa_is_depthstencil_format(GLenum format) 903{ 904 switch (format) { 905 case GL_DEPTH24_STENCIL8_EXT: 906 case GL_DEPTH_STENCIL_EXT: 907 case GL_DEPTH32F_STENCIL8: 908 return GL_TRUE; 909 default: 910 return GL_FALSE; 911 } 912} 913 914 915/** 916 * Test if the given image format is a depth or stencil format. 917 */ 918GLboolean 919_mesa_is_depth_or_stencil_format(GLenum format) 920{ 921 switch (format) { 922 case GL_DEPTH_COMPONENT: 923 case GL_DEPTH_COMPONENT16: 924 case GL_DEPTH_COMPONENT24: 925 case GL_DEPTH_COMPONENT32: 926 case GL_STENCIL_INDEX: 927 case GL_STENCIL_INDEX1_EXT: 928 case GL_STENCIL_INDEX4_EXT: 929 case GL_STENCIL_INDEX8_EXT: 930 case GL_STENCIL_INDEX16_EXT: 931 case GL_DEPTH_STENCIL_EXT: 932 case GL_DEPTH24_STENCIL8_EXT: 933 case GL_DEPTH_COMPONENT32F: 934 case GL_DEPTH32F_STENCIL8: 935 return GL_TRUE; 936 default: 937 return GL_FALSE; 938 } 939} 940 941 942/** 943 * Test if the given image format is a dudv format. 944 */ 945GLboolean 946_mesa_is_dudv_format(GLenum format) 947{ 948 switch (format) { 949 case GL_DUDV_ATI: 950 case GL_DU8DV8_ATI: 951 return GL_TRUE; 952 default: 953 return GL_FALSE; 954 } 955} 956 957 958/** 959 * Test if the given format is an integer (non-normalized) format. 960 */ 961GLboolean 962_mesa_is_integer_format(GLenum format) 963{ 964 switch (format) { 965 /* generic integer formats */ 966 case GL_RED_INTEGER_EXT: 967 case GL_GREEN_INTEGER_EXT: 968 case GL_BLUE_INTEGER_EXT: 969 case GL_ALPHA_INTEGER_EXT: 970 case GL_RGB_INTEGER_EXT: 971 case GL_RGBA_INTEGER_EXT: 972 case GL_BGR_INTEGER_EXT: 973 case GL_BGRA_INTEGER_EXT: 974 case GL_LUMINANCE_INTEGER_EXT: 975 case GL_LUMINANCE_ALPHA_INTEGER_EXT: 976 case GL_RG_INTEGER: 977 /* specific integer formats */ 978 case GL_RGBA32UI_EXT: 979 case GL_RGB32UI_EXT: 980 case GL_RG32UI: 981 case GL_R32UI: 982 case GL_ALPHA32UI_EXT: 983 case GL_INTENSITY32UI_EXT: 984 case GL_LUMINANCE32UI_EXT: 985 case GL_LUMINANCE_ALPHA32UI_EXT: 986 case GL_RGBA16UI_EXT: 987 case GL_RGB16UI_EXT: 988 case GL_RG16UI: 989 case GL_R16UI: 990 case GL_ALPHA16UI_EXT: 991 case GL_INTENSITY16UI_EXT: 992 case GL_LUMINANCE16UI_EXT: 993 case GL_LUMINANCE_ALPHA16UI_EXT: 994 case GL_RGBA8UI_EXT: 995 case GL_RGB8UI_EXT: 996 case GL_RG8UI: 997 case GL_R8UI: 998 case GL_ALPHA8UI_EXT: 999 case GL_INTENSITY8UI_EXT: 1000 case GL_LUMINANCE8UI_EXT: 1001 case GL_LUMINANCE_ALPHA8UI_EXT: 1002 case GL_RGBA32I_EXT: 1003 case GL_RGB32I_EXT: 1004 case GL_RG32I: 1005 case GL_R32I: 1006 case GL_ALPHA32I_EXT: 1007 case GL_INTENSITY32I_EXT: 1008 case GL_LUMINANCE32I_EXT: 1009 case GL_LUMINANCE_ALPHA32I_EXT: 1010 case GL_RGBA16I_EXT: 1011 case GL_RGB16I_EXT: 1012 case GL_RG16I: 1013 case GL_R16I: 1014 case GL_ALPHA16I_EXT: 1015 case GL_INTENSITY16I_EXT: 1016 case GL_LUMINANCE16I_EXT: 1017 case GL_LUMINANCE_ALPHA16I_EXT: 1018 case GL_RGBA8I_EXT: 1019 case GL_RGB8I_EXT: 1020 case GL_RG8I: 1021 case GL_R8I: 1022 case GL_ALPHA8I_EXT: 1023 case GL_INTENSITY8I_EXT: 1024 case GL_LUMINANCE8I_EXT: 1025 case GL_LUMINANCE_ALPHA8I_EXT: 1026 case GL_RGB10_A2UI: 1027 return GL_TRUE; 1028 default: 1029 return GL_FALSE; 1030 } 1031} 1032 1033 1034/** 1035 * Test if an image format is a supported compressed format. 1036 * \param format the internal format token provided by the user. 1037 * \return GL_TRUE if compressed, GL_FALSE if uncompressed 1038 */ 1039GLboolean 1040_mesa_is_compressed_format(struct gl_context *ctx, GLenum format) 1041{ 1042 switch (format) { 1043 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: 1044 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: 1045 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: 1046 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: 1047 return ctx->Extensions.EXT_texture_compression_s3tc; 1048 case GL_RGB_S3TC: 1049 case GL_RGB4_S3TC: 1050 case GL_RGBA_S3TC: 1051 case GL_RGBA4_S3TC: 1052 return ctx->Extensions.S3_s3tc; 1053 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT: 1054 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: 1055 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT: 1056 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: 1057 return ctx->Extensions.EXT_texture_sRGB 1058 && ctx->Extensions.EXT_texture_compression_s3tc; 1059 case GL_COMPRESSED_RGB_FXT1_3DFX: 1060 case GL_COMPRESSED_RGBA_FXT1_3DFX: 1061 return ctx->Extensions.TDFX_texture_compression_FXT1; 1062 case GL_COMPRESSED_RED_RGTC1: 1063 case GL_COMPRESSED_SIGNED_RED_RGTC1: 1064 case GL_COMPRESSED_RG_RGTC2: 1065 case GL_COMPRESSED_SIGNED_RG_RGTC2: 1066 return ctx->Extensions.ARB_texture_compression_rgtc; 1067 case GL_COMPRESSED_LUMINANCE_LATC1_EXT: 1068 case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT: 1069 case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT: 1070 case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT: 1071 return ctx->Extensions.EXT_texture_compression_latc; 1072 case GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI: 1073 return ctx->Extensions.ATI_texture_compression_3dc; 1074 case GL_ETC1_RGB8_OES: 1075 return ctx->Extensions.OES_compressed_ETC1_RGB8_texture; 1076#if FEATURE_ES 1077 case GL_PALETTE4_RGB8_OES: 1078 case GL_PALETTE4_RGBA8_OES: 1079 case GL_PALETTE4_R5_G6_B5_OES: 1080 case GL_PALETTE4_RGBA4_OES: 1081 case GL_PALETTE4_RGB5_A1_OES: 1082 case GL_PALETTE8_RGB8_OES: 1083 case GL_PALETTE8_RGBA8_OES: 1084 case GL_PALETTE8_R5_G6_B5_OES: 1085 case GL_PALETTE8_RGBA4_OES: 1086 case GL_PALETTE8_RGB5_A1_OES: 1087 return ctx->API == API_OPENGLES; 1088#endif 1089 default: 1090 return GL_FALSE; 1091 } 1092} 1093 1094 1095/** 1096 * Does the given base texture/renderbuffer format have the channel 1097 * named by 'pname'? 1098 */ 1099GLboolean 1100_mesa_base_format_has_channel(GLenum base_format, GLenum pname) 1101{ 1102 switch (pname) { 1103 case GL_TEXTURE_RED_SIZE: 1104 case GL_TEXTURE_RED_TYPE: 1105 case GL_RENDERBUFFER_RED_SIZE_EXT: 1106 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE: 1107 if (base_format == GL_RED || 1108 base_format == GL_RG || 1109 base_format == GL_RGB || 1110 base_format == GL_RGBA) { 1111 return GL_TRUE; 1112 } 1113 return GL_FALSE; 1114 case GL_TEXTURE_GREEN_SIZE: 1115 case GL_TEXTURE_GREEN_TYPE: 1116 case GL_RENDERBUFFER_GREEN_SIZE_EXT: 1117 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE: 1118 if (base_format == GL_RG || 1119 base_format == GL_RGB || 1120 base_format == GL_RGBA) { 1121 return GL_TRUE; 1122 } 1123 return GL_FALSE; 1124 case GL_TEXTURE_BLUE_SIZE: 1125 case GL_TEXTURE_BLUE_TYPE: 1126 case GL_RENDERBUFFER_BLUE_SIZE_EXT: 1127 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE: 1128 if (base_format == GL_RGB || 1129 base_format == GL_RGBA) { 1130 return GL_TRUE; 1131 } 1132 return GL_FALSE; 1133 case GL_TEXTURE_ALPHA_SIZE: 1134 case GL_TEXTURE_ALPHA_TYPE: 1135 case GL_RENDERBUFFER_ALPHA_SIZE_EXT: 1136 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE: 1137 if (base_format == GL_RGBA || 1138 base_format == GL_ALPHA || 1139 base_format == GL_LUMINANCE_ALPHA) { 1140 return GL_TRUE; 1141 } 1142 return GL_FALSE; 1143 case GL_TEXTURE_LUMINANCE_SIZE: 1144 case GL_TEXTURE_LUMINANCE_TYPE: 1145 if (base_format == GL_LUMINANCE || 1146 base_format == GL_LUMINANCE_ALPHA) { 1147 return GL_TRUE; 1148 } 1149 return GL_FALSE; 1150 case GL_TEXTURE_INTENSITY_SIZE: 1151 case GL_TEXTURE_INTENSITY_TYPE: 1152 if (base_format == GL_INTENSITY) { 1153 return GL_TRUE; 1154 } 1155 return GL_FALSE; 1156 case GL_TEXTURE_DEPTH_SIZE: 1157 case GL_TEXTURE_DEPTH_TYPE: 1158 case GL_RENDERBUFFER_DEPTH_SIZE_EXT: 1159 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE: 1160 if (base_format == GL_DEPTH_STENCIL || 1161 base_format == GL_DEPTH_COMPONENT) { 1162 return GL_TRUE; 1163 } 1164 return GL_FALSE; 1165 case GL_RENDERBUFFER_STENCIL_SIZE_EXT: 1166 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE: 1167 if (base_format == GL_DEPTH_STENCIL || 1168 base_format == GL_STENCIL_INDEX) { 1169 return GL_TRUE; 1170 } 1171 return GL_FALSE; 1172 default: 1173 _mesa_warning(NULL, "%s: Unexpected channel token 0x%x\n", 1174 __FUNCTION__, pname); 1175 return GL_FALSE; 1176 } 1177 1178 return GL_FALSE; 1179} 1180 1181 1182/** 1183 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D). 1184 * 1185 * Pixel unpacking/packing parameters are observed according to \p packing. 1186 * 1187 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image 1188 * \param packing the pixelstore attributes 1189 * \param width the image width 1190 * \param height the image height 1191 * \param format the pixel format (must be validated beforehand) 1192 * \param type the pixel data type (must be validated beforehand) 1193 * \param img which image in the volume (0 for 1D or 2D images) 1194 * \param row row of pixel in the image (0 for 1D images) 1195 * \param column column of pixel in the image 1196 * 1197 * \return offset of pixel. 1198 * 1199 * \sa gl_pixelstore_attrib. 1200 */ 1201GLintptr 1202_mesa_image_offset( GLuint dimensions, 1203 const struct gl_pixelstore_attrib *packing, 1204 GLsizei width, GLsizei height, 1205 GLenum format, GLenum type, 1206 GLint img, GLint row, GLint column ) 1207{ 1208 GLint alignment; /* 1, 2 or 4 */ 1209 GLint pixels_per_row; 1210 GLint rows_per_image; 1211 GLint skiprows; 1212 GLint skippixels; 1213 GLint skipimages; /* for 3-D volume images */ 1214 GLintptr offset; 1215 1216 ASSERT(dimensions >= 1 && dimensions <= 3); 1217 1218 alignment = packing->Alignment; 1219 if (packing->RowLength > 0) { 1220 pixels_per_row = packing->RowLength; 1221 } 1222 else { 1223 pixels_per_row = width; 1224 } 1225 if (packing->ImageHeight > 0) { 1226 rows_per_image = packing->ImageHeight; 1227 } 1228 else { 1229 rows_per_image = height; 1230 } 1231 1232 skippixels = packing->SkipPixels; 1233 /* Note: SKIP_ROWS _is_ used for 1D images */ 1234 skiprows = packing->SkipRows; 1235 /* Note: SKIP_IMAGES is only used for 3D images */ 1236 skipimages = (dimensions == 3) ? packing->SkipImages : 0; 1237 1238 if (type == GL_BITMAP) { 1239 /* BITMAP data */ 1240 GLint bytes_per_row; 1241 GLint bytes_per_image; 1242 /* components per pixel for color or stencil index: */ 1243 const GLint comp_per_pixel = 1; 1244 1245 /* The pixel type and format should have been error checked earlier */ 1246 assert(format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX); 1247 1248 bytes_per_row = alignment 1249 * CEILING( comp_per_pixel*pixels_per_row, 8*alignment ); 1250 1251 bytes_per_image = bytes_per_row * rows_per_image; 1252 1253 offset = (skipimages + img) * bytes_per_image 1254 + (skiprows + row) * bytes_per_row 1255 + (skippixels + column) / 8; 1256 } 1257 else { 1258 /* Non-BITMAP data */ 1259 GLint bytes_per_pixel, bytes_per_row, remainder, bytes_per_image; 1260 GLint topOfImage; 1261 1262 bytes_per_pixel = _mesa_bytes_per_pixel( format, type ); 1263 1264 /* The pixel type and format should have been error checked earlier */ 1265 assert(bytes_per_pixel > 0); 1266 1267 bytes_per_row = pixels_per_row * bytes_per_pixel; 1268 remainder = bytes_per_row % alignment; 1269 if (remainder > 0) 1270 bytes_per_row += (alignment - remainder); 1271 1272 ASSERT(bytes_per_row % alignment == 0); 1273 1274 bytes_per_image = bytes_per_row * rows_per_image; 1275 1276 if (packing->Invert) { 1277 /* set pixel_addr to the last row */ 1278 topOfImage = bytes_per_row * (height - 1); 1279 bytes_per_row = -bytes_per_row; 1280 } 1281 else { 1282 topOfImage = 0; 1283 } 1284 1285 /* compute final pixel address */ 1286 offset = (skipimages + img) * bytes_per_image 1287 + topOfImage 1288 + (skiprows + row) * bytes_per_row 1289 + (skippixels + column) * bytes_per_pixel; 1290 } 1291 1292 return offset; 1293} 1294 1295 1296/** 1297 * Return the address of a specific pixel in an image (1D, 2D or 3D). 1298 * 1299 * Pixel unpacking/packing parameters are observed according to \p packing. 1300 * 1301 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image 1302 * \param packing the pixelstore attributes 1303 * \param image starting address of image data 1304 * \param width the image width 1305 * \param height the image height 1306 * \param format the pixel format (must be validated beforehand) 1307 * \param type the pixel data type (must be validated beforehand) 1308 * \param img which image in the volume (0 for 1D or 2D images) 1309 * \param row row of pixel in the image (0 for 1D images) 1310 * \param column column of pixel in the image 1311 * 1312 * \return address of pixel. 1313 * 1314 * \sa gl_pixelstore_attrib. 1315 */ 1316GLvoid * 1317_mesa_image_address( GLuint dimensions, 1318 const struct gl_pixelstore_attrib *packing, 1319 const GLvoid *image, 1320 GLsizei width, GLsizei height, 1321 GLenum format, GLenum type, 1322 GLint img, GLint row, GLint column ) 1323{ 1324 const GLubyte *addr = (const GLubyte *) image; 1325 1326 addr += _mesa_image_offset(dimensions, packing, width, height, 1327 format, type, img, row, column); 1328 1329 return (GLvoid *) addr; 1330} 1331 1332 1333GLvoid * 1334_mesa_image_address1d( const struct gl_pixelstore_attrib *packing, 1335 const GLvoid *image, 1336 GLsizei width, 1337 GLenum format, GLenum type, 1338 GLint column ) 1339{ 1340 return _mesa_image_address(1, packing, image, width, 1, 1341 format, type, 0, 0, column); 1342} 1343 1344 1345GLvoid * 1346_mesa_image_address2d( const struct gl_pixelstore_attrib *packing, 1347 const GLvoid *image, 1348 GLsizei width, GLsizei height, 1349 GLenum format, GLenum type, 1350 GLint row, GLint column ) 1351{ 1352 return _mesa_image_address(2, packing, image, width, height, 1353 format, type, 0, row, column); 1354} 1355 1356 1357GLvoid * 1358_mesa_image_address3d( const struct gl_pixelstore_attrib *packing, 1359 const GLvoid *image, 1360 GLsizei width, GLsizei height, 1361 GLenum format, GLenum type, 1362 GLint img, GLint row, GLint column ) 1363{ 1364 return _mesa_image_address(3, packing, image, width, height, 1365 format, type, img, row, column); 1366} 1367 1368 1369 1370/** 1371 * Compute the stride (in bytes) between image rows. 1372 * 1373 * \param packing the pixelstore attributes 1374 * \param width image width. 1375 * \param format pixel format. 1376 * \param type pixel data type. 1377 * 1378 * \return the stride in bytes for the given parameters, or -1 if error 1379 */ 1380GLint 1381_mesa_image_row_stride( const struct gl_pixelstore_attrib *packing, 1382 GLint width, GLenum format, GLenum type ) 1383{ 1384 GLint bytesPerRow, remainder; 1385 1386 ASSERT(packing); 1387 1388 if (type == GL_BITMAP) { 1389 if (packing->RowLength == 0) { 1390 bytesPerRow = (width + 7) / 8; 1391 } 1392 else { 1393 bytesPerRow = (packing->RowLength + 7) / 8; 1394 } 1395 } 1396 else { 1397 /* Non-BITMAP data */ 1398 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type); 1399 if (bytesPerPixel <= 0) 1400 return -1; /* error */ 1401 if (packing->RowLength == 0) { 1402 bytesPerRow = bytesPerPixel * width; 1403 } 1404 else { 1405 bytesPerRow = bytesPerPixel * packing->RowLength; 1406 } 1407 } 1408 1409 remainder = bytesPerRow % packing->Alignment; 1410 if (remainder > 0) { 1411 bytesPerRow += (packing->Alignment - remainder); 1412 } 1413 1414 if (packing->Invert) { 1415 /* negate the bytes per row (negative row stride) */ 1416 bytesPerRow = -bytesPerRow; 1417 } 1418 1419 return bytesPerRow; 1420} 1421 1422 1423/* 1424 * Compute the stride between images in a 3D texture (in bytes) for the given 1425 * pixel packing parameters and image width, format and type. 1426 */ 1427GLint 1428_mesa_image_image_stride( const struct gl_pixelstore_attrib *packing, 1429 GLint width, GLint height, 1430 GLenum format, GLenum type ) 1431{ 1432 GLint bytesPerRow, bytesPerImage, remainder; 1433 1434 ASSERT(packing); 1435 1436 if (type == GL_BITMAP) { 1437 if (packing->RowLength == 0) { 1438 bytesPerRow = (width + 7) / 8; 1439 } 1440 else { 1441 bytesPerRow = (packing->RowLength + 7) / 8; 1442 } 1443 } 1444 else { 1445 const GLint bytesPerPixel = _mesa_bytes_per_pixel(format, type); 1446 1447 if (bytesPerPixel <= 0) 1448 return -1; /* error */ 1449 if (packing->RowLength == 0) { 1450 bytesPerRow = bytesPerPixel * width; 1451 } 1452 else { 1453 bytesPerRow = bytesPerPixel * packing->RowLength; 1454 } 1455 } 1456 1457 remainder = bytesPerRow % packing->Alignment; 1458 if (remainder > 0) 1459 bytesPerRow += (packing->Alignment - remainder); 1460 1461 if (packing->ImageHeight == 0) 1462 bytesPerImage = bytesPerRow * height; 1463 else 1464 bytesPerImage = bytesPerRow * packing->ImageHeight; 1465 1466 return bytesPerImage; 1467} 1468 1469 1470 1471/** 1472 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel. 1473 * This is typically used to convert a bitmap into a GLubyte/pixel texture. 1474 * "On" bits will set texels to \p onValue. 1475 * "Off" bits will not modify texels. 1476 * \param width src bitmap width in pixels 1477 * \param height src bitmap height in pixels 1478 * \param unpack bitmap unpacking state 1479 * \param bitmap the src bitmap data 1480 * \param destBuffer start of dest buffer 1481 * \param destStride row stride in dest buffer 1482 * \param onValue if bit is 1, set destBuffer pixel to this value 1483 */ 1484void 1485_mesa_expand_bitmap(GLsizei width, GLsizei height, 1486 const struct gl_pixelstore_attrib *unpack, 1487 const GLubyte *bitmap, 1488 GLubyte *destBuffer, GLint destStride, 1489 GLubyte onValue) 1490{ 1491 const GLubyte *srcRow = (const GLubyte *) 1492 _mesa_image_address2d(unpack, bitmap, width, height, 1493 GL_COLOR_INDEX, GL_BITMAP, 0, 0); 1494 const GLint srcStride = _mesa_image_row_stride(unpack, width, 1495 GL_COLOR_INDEX, GL_BITMAP); 1496 GLint row, col; 1497 1498#define SET_PIXEL(COL, ROW) \ 1499 destBuffer[(ROW) * destStride + (COL)] = onValue; 1500 1501 for (row = 0; row < height; row++) { 1502 const GLubyte *src = srcRow; 1503 1504 if (unpack->LsbFirst) { 1505 /* Lsb first */ 1506 GLubyte mask = 1U << (unpack->SkipPixels & 0x7); 1507 for (col = 0; col < width; col++) { 1508 1509 if (*src & mask) { 1510 SET_PIXEL(col, row); 1511 } 1512 1513 if (mask == 128U) { 1514 src++; 1515 mask = 1U; 1516 } 1517 else { 1518 mask = mask << 1; 1519 } 1520 } 1521 1522 /* get ready for next row */ 1523 if (mask != 1) 1524 src++; 1525 } 1526 else { 1527 /* Msb first */ 1528 GLubyte mask = 128U >> (unpack->SkipPixels & 0x7); 1529 for (col = 0; col < width; col++) { 1530 1531 if (*src & mask) { 1532 SET_PIXEL(col, row); 1533 } 1534 1535 if (mask == 1U) { 1536 src++; 1537 mask = 128U; 1538 } 1539 else { 1540 mask = mask >> 1; 1541 } 1542 } 1543 1544 /* get ready for next row */ 1545 if (mask != 128) 1546 src++; 1547 } 1548 1549 srcRow += srcStride; 1550 } /* row */ 1551 1552#undef SET_PIXEL 1553} 1554 1555 1556 1557 1558/** 1559 * Convert an array of RGBA colors from one datatype to another. 1560 * NOTE: src may equal dst. In that case, we use a temporary buffer. 1561 */ 1562void 1563_mesa_convert_colors(GLenum srcType, const GLvoid *src, 1564 GLenum dstType, GLvoid *dst, 1565 GLuint count, const GLubyte mask[]) 1566{ 1567 GLuint *tempBuffer; 1568 const GLboolean useTemp = (src == dst); 1569 1570 tempBuffer = malloc(count * MAX_PIXEL_BYTES); 1571 if (!tempBuffer) 1572 return; 1573 1574 ASSERT(srcType != dstType); 1575 1576 switch (srcType) { 1577 case GL_UNSIGNED_BYTE: 1578 if (dstType == GL_UNSIGNED_SHORT) { 1579 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src; 1580 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst); 1581 GLuint i; 1582 for (i = 0; i < count; i++) { 1583 if (!mask || mask[i]) { 1584 dst2[i][RCOMP] = UBYTE_TO_USHORT(src1[i][RCOMP]); 1585 dst2[i][GCOMP] = UBYTE_TO_USHORT(src1[i][GCOMP]); 1586 dst2[i][BCOMP] = UBYTE_TO_USHORT(src1[i][BCOMP]); 1587 dst2[i][ACOMP] = UBYTE_TO_USHORT(src1[i][ACOMP]); 1588 } 1589 } 1590 if (useTemp) 1591 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort)); 1592 } 1593 else { 1594 const GLubyte (*src1)[4] = (const GLubyte (*)[4]) src; 1595 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst); 1596 GLuint i; 1597 ASSERT(dstType == GL_FLOAT); 1598 for (i = 0; i < count; i++) { 1599 if (!mask || mask[i]) { 1600 dst4[i][RCOMP] = UBYTE_TO_FLOAT(src1[i][RCOMP]); 1601 dst4[i][GCOMP] = UBYTE_TO_FLOAT(src1[i][GCOMP]); 1602 dst4[i][BCOMP] = UBYTE_TO_FLOAT(src1[i][BCOMP]); 1603 dst4[i][ACOMP] = UBYTE_TO_FLOAT(src1[i][ACOMP]); 1604 } 1605 } 1606 if (useTemp) 1607 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat)); 1608 } 1609 break; 1610 case GL_UNSIGNED_SHORT: 1611 if (dstType == GL_UNSIGNED_BYTE) { 1612 const GLushort (*src2)[4] = (const GLushort (*)[4]) src; 1613 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst); 1614 GLuint i; 1615 for (i = 0; i < count; i++) { 1616 if (!mask || mask[i]) { 1617 dst1[i][RCOMP] = USHORT_TO_UBYTE(src2[i][RCOMP]); 1618 dst1[i][GCOMP] = USHORT_TO_UBYTE(src2[i][GCOMP]); 1619 dst1[i][BCOMP] = USHORT_TO_UBYTE(src2[i][BCOMP]); 1620 dst1[i][ACOMP] = USHORT_TO_UBYTE(src2[i][ACOMP]); 1621 } 1622 } 1623 if (useTemp) 1624 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte)); 1625 } 1626 else { 1627 const GLushort (*src2)[4] = (const GLushort (*)[4]) src; 1628 GLfloat (*dst4)[4] = (GLfloat (*)[4]) (useTemp ? tempBuffer : dst); 1629 GLuint i; 1630 ASSERT(dstType == GL_FLOAT); 1631 for (i = 0; i < count; i++) { 1632 if (!mask || mask[i]) { 1633 dst4[i][RCOMP] = USHORT_TO_FLOAT(src2[i][RCOMP]); 1634 dst4[i][GCOMP] = USHORT_TO_FLOAT(src2[i][GCOMP]); 1635 dst4[i][BCOMP] = USHORT_TO_FLOAT(src2[i][BCOMP]); 1636 dst4[i][ACOMP] = USHORT_TO_FLOAT(src2[i][ACOMP]); 1637 } 1638 } 1639 if (useTemp) 1640 memcpy(dst, tempBuffer, count * 4 * sizeof(GLfloat)); 1641 } 1642 break; 1643 case GL_FLOAT: 1644 if (dstType == GL_UNSIGNED_BYTE) { 1645 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src; 1646 GLubyte (*dst1)[4] = (GLubyte (*)[4]) (useTemp ? tempBuffer : dst); 1647 GLuint i; 1648 for (i = 0; i < count; i++) { 1649 if (!mask || mask[i]) 1650 _mesa_unclamped_float_rgba_to_ubyte(dst1[i], src4[i]); 1651 } 1652 if (useTemp) 1653 memcpy(dst, tempBuffer, count * 4 * sizeof(GLubyte)); 1654 } 1655 else { 1656 const GLfloat (*src4)[4] = (const GLfloat (*)[4]) src; 1657 GLushort (*dst2)[4] = (GLushort (*)[4]) (useTemp ? tempBuffer : dst); 1658 GLuint i; 1659 ASSERT(dstType == GL_UNSIGNED_SHORT); 1660 for (i = 0; i < count; i++) { 1661 if (!mask || mask[i]) { 1662 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][RCOMP], src4[i][RCOMP]); 1663 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][GCOMP], src4[i][GCOMP]); 1664 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][BCOMP], src4[i][BCOMP]); 1665 UNCLAMPED_FLOAT_TO_USHORT(dst2[i][ACOMP], src4[i][ACOMP]); 1666 } 1667 } 1668 if (useTemp) 1669 memcpy(dst, tempBuffer, count * 4 * sizeof(GLushort)); 1670 } 1671 break; 1672 default: 1673 _mesa_problem(NULL, "Invalid datatype in _mesa_convert_colors"); 1674 } 1675 1676 free(tempBuffer); 1677} 1678 1679 1680 1681 1682/** 1683 * Perform basic clipping for glDrawPixels. The image's position and size 1684 * and the unpack SkipPixels and SkipRows are adjusted so that the image 1685 * region is entirely within the window and scissor bounds. 1686 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1). 1687 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which 1688 * we'll actually write. Beforehand, *destY-1 is the first drawing row. 1689 * 1690 * \return GL_TRUE if image is ready for drawing or 1691 * GL_FALSE if image was completely clipped away (draw nothing) 1692 */ 1693GLboolean 1694_mesa_clip_drawpixels(const struct gl_context *ctx, 1695 GLint *destX, GLint *destY, 1696 GLsizei *width, GLsizei *height, 1697 struct gl_pixelstore_attrib *unpack) 1698{ 1699 const struct gl_framebuffer *buffer = ctx->DrawBuffer; 1700 1701 if (unpack->RowLength == 0) { 1702 unpack->RowLength = *width; 1703 } 1704 1705 ASSERT(ctx->Pixel.ZoomX == 1.0F); 1706 ASSERT(ctx->Pixel.ZoomY == 1.0F || ctx->Pixel.ZoomY == -1.0F); 1707 1708 /* left clipping */ 1709 if (*destX < buffer->_Xmin) { 1710 unpack->SkipPixels += (buffer->_Xmin - *destX); 1711 *width -= (buffer->_Xmin - *destX); 1712 *destX = buffer->_Xmin; 1713 } 1714 /* right clipping */ 1715 if (*destX + *width > buffer->_Xmax) 1716 *width -= (*destX + *width - buffer->_Xmax); 1717 1718 if (*width <= 0) 1719 return GL_FALSE; 1720 1721 if (ctx->Pixel.ZoomY == 1.0F) { 1722 /* bottom clipping */ 1723 if (*destY < buffer->_Ymin) { 1724 unpack->SkipRows += (buffer->_Ymin - *destY); 1725 *height -= (buffer->_Ymin - *destY); 1726 *destY = buffer->_Ymin; 1727 } 1728 /* top clipping */ 1729 if (*destY + *height > buffer->_Ymax) 1730 *height -= (*destY + *height - buffer->_Ymax); 1731 } 1732 else { /* upside down */ 1733 /* top clipping */ 1734 if (*destY > buffer->_Ymax) { 1735 unpack->SkipRows += (*destY - buffer->_Ymax); 1736 *height -= (*destY - buffer->_Ymax); 1737 *destY = buffer->_Ymax; 1738 } 1739 /* bottom clipping */ 1740 if (*destY - *height < buffer->_Ymin) 1741 *height -= (buffer->_Ymin - (*destY - *height)); 1742 /* adjust destY so it's the first row to write to */ 1743 (*destY)--; 1744 } 1745 1746 if (*height <= 0) 1747 return GL_FALSE; 1748 1749 return GL_TRUE; 1750} 1751 1752 1753/** 1754 * Perform clipping for glReadPixels. The image's window position 1755 * and size, and the pack skipPixels, skipRows and rowLength are adjusted 1756 * so that the image region is entirely within the window bounds. 1757 * Note: this is different from _mesa_clip_drawpixels() in that the 1758 * scissor box is ignored, and we use the bounds of the current readbuffer 1759 * surface. 1760 * 1761 * \return GL_TRUE if region to read is in bounds 1762 * GL_FALSE if region is completely out of bounds (nothing to read) 1763 */ 1764GLboolean 1765_mesa_clip_readpixels(const struct gl_context *ctx, 1766 GLint *srcX, GLint *srcY, 1767 GLsizei *width, GLsizei *height, 1768 struct gl_pixelstore_attrib *pack) 1769{ 1770 const struct gl_framebuffer *buffer = ctx->ReadBuffer; 1771 1772 if (pack->RowLength == 0) { 1773 pack->RowLength = *width; 1774 } 1775 1776 /* left clipping */ 1777 if (*srcX < 0) { 1778 pack->SkipPixels += (0 - *srcX); 1779 *width -= (0 - *srcX); 1780 *srcX = 0; 1781 } 1782 /* right clipping */ 1783 if (*srcX + *width > (GLsizei) buffer->Width) 1784 *width -= (*srcX + *width - buffer->Width); 1785 1786 if (*width <= 0) 1787 return GL_FALSE; 1788 1789 /* bottom clipping */ 1790 if (*srcY < 0) { 1791 pack->SkipRows += (0 - *srcY); 1792 *height -= (0 - *srcY); 1793 *srcY = 0; 1794 } 1795 /* top clipping */ 1796 if (*srcY + *height > (GLsizei) buffer->Height) 1797 *height -= (*srcY + *height - buffer->Height); 1798 1799 if (*height <= 0) 1800 return GL_FALSE; 1801 1802 return GL_TRUE; 1803} 1804 1805 1806/** 1807 * Do clipping for a glCopyTexSubImage call. 1808 * The framebuffer source region might extend outside the framebuffer 1809 * bounds. Clip the source region against the framebuffer bounds and 1810 * adjust the texture/dest position and size accordingly. 1811 * 1812 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise. 1813 */ 1814GLboolean 1815_mesa_clip_copytexsubimage(const struct gl_context *ctx, 1816 GLint *destX, GLint *destY, 1817 GLint *srcX, GLint *srcY, 1818 GLsizei *width, GLsizei *height) 1819{ 1820 const struct gl_framebuffer *fb = ctx->ReadBuffer; 1821 const GLint srcX0 = *srcX, srcY0 = *srcY; 1822 1823 if (_mesa_clip_to_region(0, 0, fb->Width, fb->Height, 1824 srcX, srcY, width, height)) { 1825 *destX = *destX + *srcX - srcX0; 1826 *destY = *destY + *srcY - srcY0; 1827 1828 return GL_TRUE; 1829 } 1830 else { 1831 return GL_FALSE; 1832 } 1833} 1834 1835 1836 1837/** 1838 * Clip the rectangle defined by (x, y, width, height) against the bounds 1839 * specified by [xmin, xmax) and [ymin, ymax). 1840 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise. 1841 */ 1842GLboolean 1843_mesa_clip_to_region(GLint xmin, GLint ymin, 1844 GLint xmax, GLint ymax, 1845 GLint *x, GLint *y, 1846 GLsizei *width, GLsizei *height ) 1847{ 1848 /* left clipping */ 1849 if (*x < xmin) { 1850 *width -= (xmin - *x); 1851 *x = xmin; 1852 } 1853 1854 /* right clipping */ 1855 if (*x + *width > xmax) 1856 *width -= (*x + *width - xmax); 1857 1858 if (*width <= 0) 1859 return GL_FALSE; 1860 1861 /* bottom (or top) clipping */ 1862 if (*y < ymin) { 1863 *height -= (ymin - *y); 1864 *y = ymin; 1865 } 1866 1867 /* top (or bottom) clipping */ 1868 if (*y + *height > ymax) 1869 *height -= (*y + *height - ymax); 1870 1871 if (*height <= 0) 1872 return GL_FALSE; 1873 1874 return GL_TRUE; 1875} 1876 1877 1878/** 1879 * Clip dst coords against Xmax (or Ymax). 1880 */ 1881static inline void 1882clip_right_or_top(GLint *srcX0, GLint *srcX1, 1883 GLint *dstX0, GLint *dstX1, 1884 GLint maxValue) 1885{ 1886 GLfloat t, bias; 1887 1888 if (*dstX1 > maxValue) { 1889 /* X1 outside right edge */ 1890 ASSERT(*dstX0 < maxValue); /* X0 should be inside right edge */ 1891 t = (GLfloat) (maxValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0); 1892 /* chop off [t, 1] part */ 1893 ASSERT(t >= 0.0 && t <= 1.0); 1894 *dstX1 = maxValue; 1895 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; 1896 *srcX1 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias); 1897 } 1898 else if (*dstX0 > maxValue) { 1899 /* X0 outside right edge */ 1900 ASSERT(*dstX1 < maxValue); /* X1 should be inside right edge */ 1901 t = (GLfloat) (maxValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1); 1902 /* chop off [t, 1] part */ 1903 ASSERT(t >= 0.0 && t <= 1.0); 1904 *dstX0 = maxValue; 1905 bias = (*srcX0 < *srcX1) ? -0.5F : 0.5F; 1906 *srcX0 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias); 1907 } 1908} 1909 1910 1911/** 1912 * Clip dst coords against Xmin (or Ymin). 1913 */ 1914static inline void 1915clip_left_or_bottom(GLint *srcX0, GLint *srcX1, 1916 GLint *dstX0, GLint *dstX1, 1917 GLint minValue) 1918{ 1919 GLfloat t, bias; 1920 1921 if (*dstX0 < minValue) { 1922 /* X0 outside left edge */ 1923 ASSERT(*dstX1 > minValue); /* X1 should be inside left edge */ 1924 t = (GLfloat) (minValue - *dstX0) / (GLfloat) (*dstX1 - *dstX0); 1925 /* chop off [0, t] part */ 1926 ASSERT(t >= 0.0 && t <= 1.0); 1927 *dstX0 = minValue; 1928 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; /* flipped??? */ 1929 *srcX0 = *srcX0 + (GLint) (t * (*srcX1 - *srcX0) + bias); 1930 } 1931 else if (*dstX1 < minValue) { 1932 /* X1 outside left edge */ 1933 ASSERT(*dstX0 > minValue); /* X0 should be inside left edge */ 1934 t = (GLfloat) (minValue - *dstX1) / (GLfloat) (*dstX0 - *dstX1); 1935 /* chop off [0, t] part */ 1936 ASSERT(t >= 0.0 && t <= 1.0); 1937 *dstX1 = minValue; 1938 bias = (*srcX0 < *srcX1) ? 0.5F : -0.5F; 1939 *srcX1 = *srcX1 + (GLint) (t * (*srcX0 - *srcX1) + bias); 1940 } 1941} 1942 1943 1944/** 1945 * Do clipping of blit src/dest rectangles. 1946 * The dest rect is clipped against both the buffer bounds and scissor bounds. 1947 * The src rect is just clipped against the buffer bounds. 1948 * 1949 * When either the src or dest rect is clipped, the other is also clipped 1950 * proportionately! 1951 * 1952 * Note that X0 need not be less than X1 (same for Y) for either the source 1953 * and dest rects. That makes the clipping a little trickier. 1954 * 1955 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped 1956 */ 1957GLboolean 1958_mesa_clip_blit(struct gl_context *ctx, 1959 GLint *srcX0, GLint *srcY0, GLint *srcX1, GLint *srcY1, 1960 GLint *dstX0, GLint *dstY0, GLint *dstX1, GLint *dstY1) 1961{ 1962 const GLint srcXmin = 0; 1963 const GLint srcXmax = ctx->ReadBuffer->Width; 1964 const GLint srcYmin = 0; 1965 const GLint srcYmax = ctx->ReadBuffer->Height; 1966 1967 /* these include scissor bounds */ 1968 const GLint dstXmin = ctx->DrawBuffer->_Xmin; 1969 const GLint dstXmax = ctx->DrawBuffer->_Xmax; 1970 const GLint dstYmin = ctx->DrawBuffer->_Ymin; 1971 const GLint dstYmax = ctx->DrawBuffer->_Ymax; 1972 1973 /* 1974 printf("PreClipX: src: %d .. %d dst: %d .. %d\n", 1975 *srcX0, *srcX1, *dstX0, *dstX1); 1976 printf("PreClipY: src: %d .. %d dst: %d .. %d\n", 1977 *srcY0, *srcY1, *dstY0, *dstY1); 1978 */ 1979 1980 /* trivial rejection tests */ 1981 if (*dstX0 == *dstX1) 1982 return GL_FALSE; /* no width */ 1983 if (*dstX0 <= dstXmin && *dstX1 <= dstXmin) 1984 return GL_FALSE; /* totally out (left) of bounds */ 1985 if (*dstX0 >= dstXmax && *dstX1 >= dstXmax) 1986 return GL_FALSE; /* totally out (right) of bounds */ 1987 1988 if (*dstY0 == *dstY1) 1989 return GL_FALSE; 1990 if (*dstY0 <= dstYmin && *dstY1 <= dstYmin) 1991 return GL_FALSE; 1992 if (*dstY0 >= dstYmax && *dstY1 >= dstYmax) 1993 return GL_FALSE; 1994 1995 if (*srcX0 == *srcX1) 1996 return GL_FALSE; 1997 if (*srcX0 <= srcXmin && *srcX1 <= srcXmin) 1998 return GL_FALSE; 1999 if (*srcX0 >= srcXmax && *srcX1 >= srcXmax) 2000 return GL_FALSE; 2001 2002 if (*srcY0 == *srcY1) 2003 return GL_FALSE; 2004 if (*srcY0 <= srcYmin && *srcY1 <= srcYmin) 2005 return GL_FALSE; 2006 if (*srcY0 >= srcYmax && *srcY1 >= srcYmax) 2007 return GL_FALSE; 2008 2009 /* 2010 * dest clip 2011 */ 2012 clip_right_or_top(srcX0, srcX1, dstX0, dstX1, dstXmax); 2013 clip_right_or_top(srcY0, srcY1, dstY0, dstY1, dstYmax); 2014 clip_left_or_bottom(srcX0, srcX1, dstX0, dstX1, dstXmin); 2015 clip_left_or_bottom(srcY0, srcY1, dstY0, dstY1, dstYmin); 2016 2017 /* 2018 * src clip (just swap src/dst values from above) 2019 */ 2020 clip_right_or_top(dstX0, dstX1, srcX0, srcX1, srcXmax); 2021 clip_right_or_top(dstY0, dstY1, srcY0, srcY1, srcYmax); 2022 clip_left_or_bottom(dstX0, dstX1, srcX0, srcX1, srcXmin); 2023 clip_left_or_bottom(dstY0, dstY1, srcY0, srcY1, srcYmin); 2024 2025 /* 2026 printf("PostClipX: src: %d .. %d dst: %d .. %d\n", 2027 *srcX0, *srcX1, *dstX0, *dstX1); 2028 printf("PostClipY: src: %d .. %d dst: %d .. %d\n", 2029 *srcY0, *srcY1, *dstY0, *dstY1); 2030 */ 2031 2032 ASSERT(*dstX0 >= dstXmin); 2033 ASSERT(*dstX0 <= dstXmax); 2034 ASSERT(*dstX1 >= dstXmin); 2035 ASSERT(*dstX1 <= dstXmax); 2036 2037 ASSERT(*dstY0 >= dstYmin); 2038 ASSERT(*dstY0 <= dstYmax); 2039 ASSERT(*dstY1 >= dstYmin); 2040 ASSERT(*dstY1 <= dstYmax); 2041 2042 ASSERT(*srcX0 >= srcXmin); 2043 ASSERT(*srcX0 <= srcXmax); 2044 ASSERT(*srcX1 >= srcXmin); 2045 ASSERT(*srcX1 <= srcXmax); 2046 2047 ASSERT(*srcY0 >= srcYmin); 2048 ASSERT(*srcY0 <= srcYmax); 2049 ASSERT(*srcY1 >= srcYmin); 2050 ASSERT(*srcY1 <= srcYmax); 2051 2052 return GL_TRUE; 2053} 2054