426{
427 GLubyte alphabase[2], alphause[2];
429 GLuint alphablockerror1, alphablockerror2, alphablockerror3;
431 GLubyte alphaenc1[16], alphaenc2[16], alphaenc3[16];
435
436
437 alphabase[0] = 0xff; alphabase[1] = 0x0;
438 for (
j = 0;
j < numypixels;
j++) {
439 for (
i = 0;
i < numxpixels;
i++) {
440 if (srccolors[
j][
i][3] == 0)
442 else if (srccolors[
j][
i][3] == 255)
444 else {
445 if (srccolors[
j][
i][3] > alphabase[1])
446 alphabase[1] = srccolors[
j][
i][3];
447 if (srccolors[
j][
i][3] < alphabase[0])
448 alphabase[0] = srccolors[
j][
i][3];
449 }
450 }
451 }
452
453
454 if ((alphabase[0] > alphabase[1]) && !(alphaabsmin && alphaabsmax)) {
455
456
457
458
459 *blkaddr++ = srccolors[0][0][3];
460 blkaddr++;
461 *blkaddr++ = 0;
462 *blkaddr++ = 0;
463 *blkaddr++ = 0;
464 *blkaddr++ = 0;
465 *blkaddr++ = 0;
466 *blkaddr++ = 0;
467
468 return;
469 }
470
471
472
473 alphablockerror1 = 0x0;
474 alphablockerror2 = 0xffffffff;
475 alphablockerror3 = 0xffffffff;
476 if (alphaabsmin) alphause[0] = 0;
477 else alphause[0] = alphabase[0];
478 if (alphaabsmax) alphause[1] = 255;
479 else alphause[1] = alphabase[1];
480
481 for (aindex = 0; aindex < 7; aindex++) {
482
483 acutValues[aindex] = (alphause[0] * (2*aindex + 1) + alphause[1] * (14 - (2*aindex + 1))) / 14;
484 }
485
486 for (
j = 0;
j < numypixels;
j++) {
487 for (
i = 0;
i < numxpixels;
i++) {
488
489
490 if (srccolors[
j][
i][3] > acutValues[0]) {
491 alphaenc1[4*
j +
i] = 0;
492 alphadist = srccolors[
j][
i][3] - alphause[1];
493 }
494 else if (srccolors[
j][
i][3] > acutValues[1]) {
495 alphaenc1[4*
j +
i] = 2;
496 alphadist = srccolors[
j][
i][3] - (alphause[1] * 6 + alphause[0] * 1) / 7;
497 }
498 else if (srccolors[
j][
i][3] > acutValues[2]) {
499 alphaenc1[4*
j +
i] = 3;
500 alphadist = srccolors[
j][
i][3] - (alphause[1] * 5 + alphause[0] * 2) / 7;
501 }
502 else if (srccolors[
j][
i][3] > acutValues[3]) {
503 alphaenc1[4*
j +
i] = 4;
504 alphadist = srccolors[
j][
i][3] - (alphause[1] * 4 + alphause[0] * 3) / 7;
505 }
506 else if (srccolors[
j][
i][3] > acutValues[4]) {
507 alphaenc1[4*
j +
i] = 5;
508 alphadist = srccolors[
j][
i][3] - (alphause[1] * 3 + alphause[0] * 4) / 7;
509 }
510 else if (srccolors[
j][
i][3] > acutValues[5]) {
511 alphaenc1[4*
j +
i] = 6;
512 alphadist = srccolors[
j][
i][3] - (alphause[1] * 2 + alphause[0] * 5) / 7;
513 }
514 else if (srccolors[
j][
i][3] > acutValues[6]) {
515 alphaenc1[4*
j +
i] = 7;
516 alphadist = srccolors[
j][
i][3] - (alphause[1] * 1 + alphause[0] * 6) / 7;
517 }
518 else {
519 alphaenc1[4*
j +
i] = 1;
520 alphadist = srccolors[
j][
i][3] - alphause[0];
521 }
522 alphablockerror1 += alphadist * alphadist;
523 }
524 }
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 if (alphablockerror1 >= 32) {
543
544
545
546 alphablockerror2 = 0;
547 for (aindex = 0; aindex < 5; aindex++) {
548
549 acutValues[aindex] = (alphabase[0] * (10 - (2*aindex + 1)) + alphabase[1] * (2*aindex + 1)) / 10;
550 }
551 for (
j = 0;
j < numypixels;
j++) {
552 for (
i = 0;
i < numxpixels;
i++) {
553
554
555 if (srccolors[
j][
i][3] == 0) {
556 alphaenc2[4*
j +
i] = 6;
557 alphadist = 0;
558 }
559 else if (srccolors[
j][
i][3] == 255) {
560 alphaenc2[4*
j +
i] = 7;
561 alphadist = 0;
562 }
563 else if (srccolors[
j][
i][3] <= acutValues[0]) {
564 alphaenc2[4*
j +
i] = 0;
565 alphadist = srccolors[
j][
i][3] - alphabase[0];
566 }
567 else if (srccolors[
j][
i][3] <= acutValues[1]) {
568 alphaenc2[4*
j +
i] = 2;
569 alphadist = srccolors[
j][
i][3] - (alphabase[0] * 4 + alphabase[1] * 1) / 5;
570 }
571 else if (srccolors[
j][
i][3] <= acutValues[2]) {
572 alphaenc2[4*
j +
i] = 3;
573 alphadist = srccolors[
j][
i][3] - (alphabase[0] * 3 + alphabase[1] * 2) / 5;
574 }
575 else if (srccolors[
j][
i][3] <= acutValues[3]) {
576 alphaenc2[4*
j +
i] = 4;
577 alphadist = srccolors[
j][
i][3] - (alphabase[0] * 2 + alphabase[1] * 3) / 5;
578 }
579 else if (srccolors[
j][
i][3] <= acutValues[4]) {
580 alphaenc2[4*
j +
i] = 5;
581 alphadist = srccolors[
j][
i][3] - (alphabase[0] * 1 + alphabase[1] * 4) / 5;
582 }
583 else {
584 alphaenc2[4*
j +
i] = 1;
585 alphadist = srccolors[
j][
i][3] - alphabase[1];
586 }
587 alphablockerror2 += alphadist * alphadist;
588 }
589 }
590
591
592
593
594 if ((alphablockerror2 > 96) && (alphablockerror1 > 96)) {
598 GLubyte nralphainrangehigh = 0;
599 alphatest[0] = 0xff;
600 alphatest[1] = 0x0;
601
602 for (
j = 0;
j < numypixels;
j++) {
603 for (
i = 0;
i < numxpixels;
i++) {
604 if ((srccolors[
j][
i][3] > alphatest[1]) && (srccolors[
j][
i][3] < (255 -(alphabase[1] - alphabase[0]) / 28)))
605 alphatest[1] = srccolors[
j][
i][3];
606 if ((srccolors[
j][
i][3] < alphatest[0]) && (srccolors[
j][
i][3] > (alphabase[1] - alphabase[0]) / 28))
607 alphatest[0] = srccolors[
j][
i][3];
608 }
609 }
610
611 if (alphatest[1] <= alphatest[0]) {
612 alphatest[0] = 1;
613 alphatest[1] = 254;
614
615 }
616 for (aindex = 0; aindex < 5; aindex++) {
617
618 acutValues[aindex] = (alphatest[0] * (10 - (2*aindex + 1)) + alphatest[1] * (2*aindex + 1)) / 10;
619 }
620
621
622
623
624
625
626
627
628 for (
j = 0;
j < numypixels;
j++) {
629 for (
i = 0;
i < numxpixels;
i++) {
630 if (srccolors[
j][
i][3] <= alphatest[0] / 2) {
631 }
632 else if (srccolors[
j][
i][3] > ((255 + alphatest[1]) / 2)) {
633 }
634 else if (srccolors[
j][
i][3] <= acutValues[0]) {
635 blockerrlin1 += (srccolors[
j][
i][3] - alphatest[0]);
636 nralphainrangelow += 1;
637 }
638 else if (srccolors[
j][
i][3] <= acutValues[1]) {
639 blockerrlin1 += (srccolors[
j][
i][3] - (alphatest[0] * 4 + alphatest[1] * 1) / 5);
640 blockerrlin2 += (srccolors[
j][
i][3] - (alphatest[0] * 4 + alphatest[1] * 1) / 5);
641 nralphainrangelow += 1;
642 nralphainrangehigh += 1;
643 }
644 else if (srccolors[
j][
i][3] <= acutValues[2]) {
645 blockerrlin1 += (srccolors[
j][
i][3] - (alphatest[0] * 3 + alphatest[1] * 2) / 5);
646 blockerrlin2 += (srccolors[
j][
i][3] - (alphatest[0] * 3 + alphatest[1] * 2) / 5);
647 nralphainrangelow += 1;
648 nralphainrangehigh += 1;
649 }
650 else if (srccolors[
j][
i][3] <= acutValues[3]) {
651 blockerrlin1 += (srccolors[
j][
i][3] - (alphatest[0] * 2 + alphatest[1] * 3) / 5);
652 blockerrlin2 += (srccolors[
j][
i][3] - (alphatest[0] * 2 + alphatest[1] * 3) / 5);
653 nralphainrangelow += 1;
654 nralphainrangehigh += 1;
655 }
656 else if (srccolors[
j][
i][3] <= acutValues[4]) {
657 blockerrlin1 += (srccolors[
j][
i][3] - (alphatest[0] * 1 + alphatest[1] * 4) / 5);
658 blockerrlin2 += (srccolors[
j][
i][3] - (alphatest[0] * 1 + alphatest[1] * 4) / 5);
659 nralphainrangelow += 1;
660 nralphainrangehigh += 1;
661 }
662 else {
663 blockerrlin2 += (srccolors[
j][
i][3] - alphatest[1]);
664 nralphainrangehigh += 1;
665 }
666 }
667 }
668
669 if (nralphainrangelow == 0) nralphainrangelow = 1;
670 if (nralphainrangehigh == 0) nralphainrangehigh = 1;
671 alphatest[0] = alphatest[0] + (blockerrlin1 / nralphainrangelow);
672
673
674
675 if (alphatest[0] < 0) {
676 alphatest[0] = 0;
677
678 }
679 alphatest[1] = alphatest[1] + (blockerrlin2 / nralphainrangehigh);
680 if (alphatest[1] > 255) {
681 alphatest[1] = 255;
682
683 }
684
685 alphablockerror3 = 0;
686 for (aindex = 0; aindex < 5; aindex++) {
687
688 acutValues[aindex] = (alphatest[0] * (10 - (2*aindex + 1)) + alphatest[1] * (2*aindex + 1)) / 10;
689 }
690 for (
j = 0;
j < numypixels;
j++) {
691 for (
i = 0;
i < numxpixels;
i++) {
692
693
694 if (srccolors[
j][
i][3] <= alphatest[0] / 2) {
695 alphaenc3[4*
j +
i] = 6;
696 alphadist = srccolors[
j][
i][3];
697 }
698 else if (srccolors[
j][
i][3] > ((255 + alphatest[1]) / 2)) {
699 alphaenc3[4*
j +
i] = 7;
700 alphadist = 255 - srccolors[
j][
i][3];
701 }
702 else if (srccolors[
j][
i][3] <= acutValues[0]) {
703 alphaenc3[4*
j +
i] = 0;
704 alphadist = srccolors[
j][
i][3] - alphatest[0];
705 }
706 else if (srccolors[
j][
i][3] <= acutValues[1]) {
707 alphaenc3[4*
j +
i] = 2;
708 alphadist = srccolors[
j][
i][3] - (alphatest[0] * 4 + alphatest[1] * 1) / 5;
709 }
710 else if (srccolors[
j][
i][3] <= acutValues[2]) {
711 alphaenc3[4*
j +
i] = 3;
712 alphadist = srccolors[
j][
i][3] - (alphatest[0] * 3 + alphatest[1] * 2) / 5;
713 }
714 else if (srccolors[
j][
i][3] <= acutValues[3]) {
715 alphaenc3[4*
j +
i] = 4;
716 alphadist = srccolors[
j][
i][3] - (alphatest[0] * 2 + alphatest[1] * 3) / 5;
717 }
718 else if (srccolors[
j][
i][3] <= acutValues[4]) {
719 alphaenc3[4*
j +
i] = 5;
720 alphadist = srccolors[
j][
i][3] - (alphatest[0] * 1 + alphatest[1] * 4) / 5;
721 }
722 else {
723 alphaenc3[4*
j +
i] = 1;
724 alphadist = srccolors[
j][
i][3] - alphatest[1];
725 }
726 alphablockerror3 += alphadist * alphadist;
727 }
728 }
729 }
730 }
731
732 if ((alphablockerror1 <= alphablockerror2) && (alphablockerror1 <= alphablockerror3)) {
733
735 }
736 else if (alphablockerror2 <= alphablockerror3) {
737
739 }
740 else {
741
743 }
744}
static void writedxt5encodedalphablock(GLubyte *blkaddr, GLubyte alphabase1, GLubyte alphabase2, GLubyte alphaenc[16])
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j