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