Esempio n. 1
0
                uint aBitMask;//104

                public DdsPixelFormat(DdsFormat ddsFormat)
                {
                    if (!Enum.IsDefined(typeof(DdsFormat), ddsFormat))
                        throw new ArgumentException(String.Format("Requested DDS format 0x{0:8X} is unsupported", (uint)ddsFormat));

                    size = 32;
                    flags = 0;
                    fourCC = 0;
                    rgbBitCount = rBitMask = gBitMask = bBitMask = gBitMask = 0;
                    switch (ddsFormat)
                    {
                        case DdsFormat.DXT1:
                            flags |= PFFlags.fourCC;
                            fourCC = PFFourCC.dxt1;
                            break;
                        case DdsFormat.DXT3:
                            flags |= PFFlags.fourCC;
                            fourCC = PFFourCC.dxt3;
                            break;
                        case DdsFormat.DXT5:
                            flags |= PFFlags.fourCC;
                            fourCC = PFFourCC.dxt5;
                            break;
                        case DdsFormat.A8L8:
                            flags |= PFFlags.luminence | PFFlags.alphaPixels;
                            rgbBitCount = 16;
                            aBitMask = 0x0000ff00;
                            rBitMask = 0x000000ff;
                            break;
                        case DdsFormat.A8R8G8B8:
                            flags |= PFFlags.rgb | PFFlags.alphaPixels;
                            rgbBitCount = 32;
                            aBitMask = 0xff000000;
                            rBitMask = 0x00ff0000;
                            gBitMask = 0x0000ff00;
                            bBitMask = 0x000000ff;
                            break;
                        case DdsFormat.X8R8G8B8:
                            flags |= PFFlags.rgb;
                            rgbBitCount = 32;
                            rBitMask = 0x00ff0000;
                            gBitMask = 0x0000ff00;
                            bBitMask = 0x000000ff;
                            break;
                        case DdsFormat.A8B8G8R8:
                            flags |= PFFlags.rgb | PFFlags.alphaPixels;
                            rgbBitCount = 32;
                            aBitMask = 0xff000000;
                            rBitMask = 0x000000ff;
                            gBitMask = 0x0000ff00;
                            bBitMask = 0x00ff0000;
                            break;
                        case DdsFormat.X8B8G8R8:
                            flags |= PFFlags.rgb;
                            rgbBitCount = 32;
                            rBitMask = 0x000000ff;
                            gBitMask = 0x0000ff00;
                            bBitMask = 0x00ff0000;
                            break;
                        case DdsFormat.R8G8B8:
                            flags |= PFFlags.rgb;
                            rgbBitCount = 24;
                            rBitMask = 0x00ff0000;
                            gBitMask = 0x0000ff00;
                            bBitMask = 0x000000ff;
                            break;
                        case DdsFormat.A4R4G4B4:
                            flags |= PFFlags.rgb | PFFlags.alphaPixels;
                            rgbBitCount = 16;
                            aBitMask = 0x0000f000;
                            rBitMask = 0x00000f00;
                            gBitMask = 0x000000f0;
                            bBitMask = 0x0000000f;
                            break;
                        case DdsFormat.A1R5G5B5:
                            flags |= PFFlags.rgb | PFFlags.alphaPixels;
                            rgbBitCount = 16;
                            aBitMask = 0x00008000;
                            rBitMask = 0x00007c00;
                            gBitMask = 0x000003e0;
                            bBitMask = 0x0000001f;
                            break;
                        case DdsFormat.R5G6B5:
                            flags |= PFFlags.rgb;
                            rgbBitCount = 16;
                            rBitMask = 0x0000f800;
                            gBitMask = 0x000007e0;
                            bBitMask = 0x0000001f;
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("Unsupported DDS format.");
                    }
                }
Esempio n. 2
0
                public DdsPixelFormat(Stream s)
                {
                    BinaryReader r = new BinaryReader(s);
                    size = r.ReadUInt32();
                    if (size != 32)
                    {
                        Console.WriteLine(String.Format(
                            "DDS Pixel Format Size invalid.  Got {0}, expected 32.  At 0x{1:X8}.  Correcting..."
                            , size
                            , s.Position
                            ));
                        size = 32;
                    }

                    flags = (PFFlags)r.ReadUInt32();

                    fourCC = (PFFourCC)r.ReadUInt32();
                    rgbBitCount = r.ReadUInt32();
                    rBitMask = r.ReadUInt32();
                    gBitMask = r.ReadUInt32();
                    bBitMask = r.ReadUInt32();
                    aBitMask = r.ReadUInt32();

                    if ((flags & PFFlags.fourCC) != 0)
                    {
                        if (!Enum.IsDefined(typeof(PFFourCC), fourCC))
                            throw new NotSupportedException(String.Format(
                                "Unsupported DDS Pixel Format Four CC.  Got 0x{0:8}, only DXT1/3/5 supported."
                                , (uint)fourCC
                                )
                            );
                        flags &= ~notFourCC;
                        rgbBitCount = 0;
                    }
                    else if (Enum.IsDefined(typeof(PFFourCC), fourCC))
                    {
                        Console.WriteLine("Supported FourCC but Flags.FourCC not set.  Overruling flags.");
                        flags |= PFFlags.fourCC;
                        flags &= ~notFourCC;
                        rgbBitCount = 0;
                    }
                    else
                        fourCC = 0;

                    if (fourCC == 0)
                    {
                        // Fix some of my previous breakage...
                        if (((uint)(flags & (PFFlags.alphaPixels | PFFlags.alpha | PFFlags.rgb))).Bits() == 3)
                        {
                            Console.WriteLine("Alpha-only indicated but AlphaPixels and RGB also set.  Cleaing Alpha-only flag.");
                            flags &= ~PFFlags.alpha;
                        }
                        if (((uint)(flags & (PFFlags.alpha | PFFlags.rgb))).Bits() == 2)
                        {
                            Console.WriteLine("Alpha-only and RGB both set.  Cleaing Alpha-only flag and ensuring alpha-pixels set.");
                            flags &= ~PFFlags.alpha;
                            flags |= PFFlags.alphaPixels;
                        }

                        if (((uint)(flags & notFourCC)).Bits() > 1)
                            throw new NotSupportedException(String.Format(
                                "Unsupported DDS Pixel Format Flags.  Got {0}, only one of _alpha, _paletteIndexed8, _rgb, _yuv, _luminence or _nVidiaNormal should be set."
                                , flags
                                )
                            );

                        if ((flags & PFFlags.alpha) != 0)
                            throw new NotSupportedException("Alpha-only format data is not supported.");

                        if ((flags & PFFlags.paletteIndexed8) != 0)
                            throw new NotSupportedException("Palette indexed format data is not supported.");

                        if ((flags & PFFlags.yuv) != 0)
                            throw new NotSupportedException("YUV format data is not supported.");

                        if ((flags & PFFlags.nVidiaNormal) != 0)
                            throw new NotSupportedException("nVidia normal format data is not supported.");

                        if ((flags & PFFlags.luminence) != 0
                            && (
                                aBitMask != 0x0000ff00 || rBitMask != 0x000000ff
                                || gBitMask != 0 || bBitMask != 0
                            ))
                            throw new NotSupportedException("Only A8L8 luminence data is supported.");

                        if (((uint)(flags & notFourCC)).Bits() == 0)
                        {
                            Console.WriteLine("Uncompressed texture but no format indicated.  Defaulting to RGB.");
                            flags |= PFFlags.rgb;
                        }

                        if ((flags & PFFlags.alphaPixels) != 0
                            && aBitMask == 0)
                        {
                            Console.WriteLine("AlphaPixels indicated but no alpha mask.  Cleaing AlphaPixels flag.");
                            flags &= ~PFFlags.alphaPixels;
                        }

                        if ((flags & PFFlags.rgb) != 0
                            && rgbBitCount == 0)
                        {
                            Console.WriteLine("RGB Bit Count needed but not set - calculating.");
                            rgbBitCount = (uint)(aBitMask.Bits() + rBitMask.Bits() + gBitMask.Bits() + bBitMask.Bits());
                        }
                        if ((flags & PFFlags.rgb) != 0
                            && rgbBitCount == 0)
                            throw new InvalidDataException("Invalid uncompressed header.  RGB Bit Count is zero and no masks set.");

                        if ((flags & PFFlags.rgb) != 0
                            && (rBitMask == 0 || gBitMask == 0 || bBitMask == 0))
                            throw new NotSupportedException(String.Format(
                                "RGB is supported only where data is present for all channels.  RGB mask is {0:X6}."
                                , rBitMask | gBitMask | bBitMask
                                ));
                    }
                }