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