|
10 | 10 | using Stride.Graphics; |
11 | 11 | using Stride.TextureConverter.Requests; |
12 | 12 | using FreeImageAPI; |
13 | | -using FreeImageAPI.Plugins; |
14 | | -using System.Runtime.CompilerServices; |
| 13 | +using StridePixelFormat = Stride.Graphics.PixelFormat; |
15 | 14 |
|
16 | 15 | namespace Stride.TextureConverter.TexLibraries |
17 | 16 | { |
@@ -69,7 +68,7 @@ public void StartLibrary(TexImage image) |
69 | 68 |
|
70 | 69 | FREE_IMAGE_TYPE type; |
71 | 70 | uint bpp, redMask, greenMask, blueMask; |
72 | | - if (!FreeImage.GetFormatParameters(image.Format, out type, out bpp, out redMask, out greenMask, out blueMask)) |
| 71 | + if (!GetFormatParameters(image.Format, out type, out bpp, out redMask, out greenMask, out blueMask)) |
73 | 72 | { |
74 | 73 | throw new ArgumentException("The pixel format '{0}' is not supported by FreeImage".ToFormat(image.Format)); |
75 | 74 | } |
@@ -611,6 +610,165 @@ private static int GetAlphaDepth(FREE_IMAGE_FORMAT fileFormat, FIBITMAP bitmap) |
611 | 610 | } |
612 | 611 | return 0; |
613 | 612 | } |
614 | | - |
| 613 | + |
| 614 | + |
| 615 | + /// <summary> |
| 616 | + /// Retrieves all parameters needed to create a new FreeImage bitmap from the pixel format. |
| 617 | + /// </summary> |
| 618 | + /// <param name="format">The <see cref="Stride.Graphics.PixelFormat"/> of the image.</param> |
| 619 | + /// <param name="type">Returns the type used for the new bitmap.</param> |
| 620 | + /// <param name="bpp">Returns the color depth for the new bitmap.</param> |
| 621 | + /// <param name="redMask">Returns the red_mask for the new bitmap.</param> |
| 622 | + /// <param name="greenMask">Returns the green_mask for the new bitmap.</param> |
| 623 | + /// <param name="blueMask">Returns the blue_mask for the new bitmap.</param> |
| 624 | + /// <returns>True in case a matching conversion exists; else false. |
| 625 | + /// </returns> |
| 626 | + private static bool GetFormatParameters( |
| 627 | + StridePixelFormat format, |
| 628 | + out FREE_IMAGE_TYPE type, |
| 629 | + out uint bpp, |
| 630 | + out uint redMask, |
| 631 | + out uint greenMask, |
| 632 | + out uint blueMask) |
| 633 | + { |
| 634 | + var result = true; |
| 635 | + type = FREE_IMAGE_TYPE.FIT_UNKNOWN; |
| 636 | + bpp = 0; |
| 637 | + redMask = 0; |
| 638 | + greenMask = 0; |
| 639 | + blueMask = 0; |
| 640 | + |
| 641 | + switch (format) |
| 642 | + { |
| 643 | + case StridePixelFormat.R32G32B32A32_Float: |
| 644 | + type = FREE_IMAGE_TYPE.FIT_RGBAF; |
| 645 | + bpp = 128; |
| 646 | + break; |
| 647 | + case StridePixelFormat.R32G32B32_Float: |
| 648 | + type = FREE_IMAGE_TYPE.FIT_RGBF; |
| 649 | + bpp = 96; |
| 650 | + break; |
| 651 | + case StridePixelFormat.R16G16B16A16_Typeless: |
| 652 | + case StridePixelFormat.R16G16B16A16_Float: |
| 653 | + case StridePixelFormat.R16G16B16A16_UNorm: |
| 654 | + case StridePixelFormat.R16G16B16A16_UInt: |
| 655 | + case StridePixelFormat.R16G16B16A16_SNorm: |
| 656 | + case StridePixelFormat.R16G16B16A16_SInt: |
| 657 | + type = FREE_IMAGE_TYPE.FIT_RGBA16; |
| 658 | + bpp = 64; |
| 659 | + break; |
| 660 | + case StridePixelFormat.D32_Float: |
| 661 | + case StridePixelFormat.R32_Float: |
| 662 | + type = FREE_IMAGE_TYPE.FIT_FLOAT; |
| 663 | + bpp = 32; |
| 664 | + break; |
| 665 | + case StridePixelFormat.R32_SInt: |
| 666 | + type = FREE_IMAGE_TYPE.FIT_INT32; |
| 667 | + bpp = 32; |
| 668 | + break; |
| 669 | + case StridePixelFormat.R32_UInt: |
| 670 | + type = FREE_IMAGE_TYPE.FIT_UINT32; |
| 671 | + bpp = 32; |
| 672 | + break; |
| 673 | + case StridePixelFormat.R16_SInt: |
| 674 | + type = FREE_IMAGE_TYPE.FIT_INT16; |
| 675 | + bpp = 16; |
| 676 | + break; |
| 677 | + case StridePixelFormat.R16_UInt: |
| 678 | + type = FREE_IMAGE_TYPE.FIT_UINT16; |
| 679 | + bpp = 16; |
| 680 | + break; |
| 681 | + case StridePixelFormat.R32_Typeless: |
| 682 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 683 | + bpp = 32; |
| 684 | + break; |
| 685 | + case StridePixelFormat.R8G8B8A8_Typeless: |
| 686 | + case StridePixelFormat.R8G8B8A8_UNorm: |
| 687 | + case StridePixelFormat.R8G8B8A8_UNorm_SRgb: |
| 688 | + case StridePixelFormat.R8G8B8A8_UInt: |
| 689 | + case StridePixelFormat.R8G8B8A8_SNorm: |
| 690 | + case StridePixelFormat.R8G8B8A8_SInt: |
| 691 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 692 | + bpp = 32; |
| 693 | + redMask = FreeImage.FI_RGBA_RED_MASK; |
| 694 | + greenMask = FreeImage.FI_RGBA_GREEN_MASK; |
| 695 | + blueMask = FreeImage.FI_RGBA_BLUE_MASK; |
| 696 | + break; |
| 697 | + case StridePixelFormat.R16G16_Typeless: |
| 698 | + case StridePixelFormat.R16G16_Float: |
| 699 | + case StridePixelFormat.R16G16_UNorm: |
| 700 | + case StridePixelFormat.R16G16_UInt: |
| 701 | + case StridePixelFormat.R16G16_SNorm: |
| 702 | + case StridePixelFormat.R16G16_SInt: |
| 703 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 704 | + bpp = 32; |
| 705 | + redMask = 0xFFFF0000; |
| 706 | + greenMask = 0x0000FFFF; |
| 707 | + break; |
| 708 | + case StridePixelFormat.B8G8R8A8_Typeless: |
| 709 | + case StridePixelFormat.B8G8R8A8_UNorm_SRgb: |
| 710 | + case StridePixelFormat.B8G8R8X8_Typeless: |
| 711 | + case StridePixelFormat.B8G8R8X8_UNorm_SRgb: |
| 712 | + case StridePixelFormat.B8G8R8A8_UNorm: |
| 713 | + case StridePixelFormat.B8G8R8X8_UNorm: |
| 714 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 715 | + bpp = 32; |
| 716 | + redMask = FreeImage.FI_RGBA_BLUE_MASK; |
| 717 | + greenMask = FreeImage.FI_RGBA_GREEN_MASK; |
| 718 | + blueMask = FreeImage.FI_RGBA_RED_MASK; |
| 719 | + break; |
| 720 | + |
| 721 | + case StridePixelFormat.B5G6R5_UNorm: |
| 722 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 723 | + bpp = 16; |
| 724 | + redMask = FreeImage.FI16_565_RED_MASK; |
| 725 | + greenMask = FreeImage.FI16_565_GREEN_MASK; |
| 726 | + blueMask = FreeImage.FI16_565_BLUE_MASK; |
| 727 | + break; |
| 728 | + case StridePixelFormat.B5G5R5A1_UNorm: |
| 729 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 730 | + bpp = 16; |
| 731 | + redMask = FreeImage.FI16_555_RED_MASK; |
| 732 | + greenMask = FreeImage.FI16_555_GREEN_MASK; |
| 733 | + blueMask = FreeImage.FI16_555_BLUE_MASK; |
| 734 | + break; |
| 735 | + case StridePixelFormat.R16_Typeless: |
| 736 | + case StridePixelFormat.R16_Float: |
| 737 | + case StridePixelFormat.D16_UNorm: |
| 738 | + case StridePixelFormat.R16_UNorm: |
| 739 | + case StridePixelFormat.R16_SNorm: |
| 740 | + type = FREE_IMAGE_TYPE.FIT_UINT16; |
| 741 | + bpp = 16; |
| 742 | + break; |
| 743 | + case StridePixelFormat.R8G8_Typeless: |
| 744 | + case StridePixelFormat.R8G8_UNorm: |
| 745 | + case StridePixelFormat.R8G8_UInt: |
| 746 | + case StridePixelFormat.R8G8_SNorm: |
| 747 | + case StridePixelFormat.R8G8_SInt: |
| 748 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 749 | + bpp = 16; |
| 750 | + redMask = 0xFF00; |
| 751 | + greenMask= 0x00FF; |
| 752 | + break; |
| 753 | + case StridePixelFormat.R8_Typeless: |
| 754 | + case StridePixelFormat.R8_UNorm: |
| 755 | + case StridePixelFormat.R8_UInt: |
| 756 | + case StridePixelFormat.R8_SNorm: |
| 757 | + case StridePixelFormat.R8_SInt: |
| 758 | + case StridePixelFormat.A8_UNorm: |
| 759 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 760 | + bpp = 8; |
| 761 | + break; |
| 762 | + case StridePixelFormat.R1_UNorm: |
| 763 | + type = FREE_IMAGE_TYPE.FIT_BITMAP; |
| 764 | + bpp = 1; |
| 765 | + break; |
| 766 | + default: |
| 767 | + result = false; |
| 768 | + break; |
| 769 | + } |
| 770 | + |
| 771 | + return result; |
| 772 | + } |
615 | 773 | } |
616 | 774 | } |
0 commit comments