示例#1
0
        public void StoreImageToDDS(Stream stream, PixelFormat format = PixelFormat.Unknown)
        {
            stream.WriteUInt32(DDS_TAG);
            stream.WriteInt32(DDS_HEADER_dwSize);
            stream.WriteUInt32(DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_MIPMAPCOUNT | DDSD_PIXELFORMAT | DDSD_LINEARSIZE);
            stream.WriteInt32(mipMaps[0].height);
            stream.WriteInt32(mipMaps[0].width);

            int dataSize = 0;

            for (int i = 0; i < mipMaps.Count; i++)
            {
                dataSize += MipMap.getBufferSize(mipMaps[i].width, mipMaps[i].height, format == PixelFormat.Unknown ? pixelFormat : format);
            }

            stream.WriteInt32(dataSize);

            stream.WriteUInt32(0); // dwDepth
            stream.WriteInt32(mipMaps.Count);
            stream.WriteZeros(44); // dwReserved1

            stream.WriteInt32(DDS_PIXELFORMAT_dwSize);
            DDS_PF pixfmt = getDDSPixelFormat(format == PixelFormat.Unknown ? pixelFormat : format);

            stream.WriteUInt32(pixfmt.flags);
            stream.WriteUInt32(pixfmt.fourCC);
            stream.WriteUInt32(pixfmt.bits);
            stream.WriteUInt32(pixfmt.Rmask);
            stream.WriteUInt32(pixfmt.Gmask);
            stream.WriteUInt32(pixfmt.Bmask);
            stream.WriteUInt32(pixfmt.Amask);

            stream.WriteInt32(DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE);
            stream.WriteUInt32(0); // dwCaps2
            stream.WriteUInt32(0); // dwCaps3
            stream.WriteUInt32(0); // dwCaps4
            stream.WriteUInt32(0); // dwReserved2
            for (int i = 0; i < mipMaps.Count; i++)
            {
                stream.WriteFromBuffer(mipMaps[i].data);
            }
        }
示例#2
0
        static public byte[] StoreMipToDDS(byte[] src, PixelFormat format, int w, int h)
        {
            MemoryStream stream = new MemoryStream();

            stream.WriteUInt32(DDS_TAG);
            stream.WriteInt32(DDS_HEADER_dwSize);
            stream.WriteUInt32(DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_MIPMAPCOUNT | DDSD_PIXELFORMAT | DDSD_LINEARSIZE);
            stream.WriteInt32(h);
            stream.WriteInt32(w);
            stream.WriteInt32(src.Length);

            stream.WriteUInt32(0); // dwDepth
            stream.WriteInt32(1);
            stream.WriteZeros(44); // dwReserved1

            stream.WriteInt32(DDS_PIXELFORMAT_dwSize);
            DDS_PF pixfmt = getDDSPixelFormat(format);

            stream.WriteUInt32(pixfmt.flags);
            stream.WriteUInt32(pixfmt.fourCC);
            stream.WriteUInt32(pixfmt.bits);
            stream.WriteUInt32(pixfmt.Rmask);
            stream.WriteUInt32(pixfmt.Gmask);
            stream.WriteUInt32(pixfmt.Bmask);
            stream.WriteUInt32(pixfmt.Amask);

            stream.WriteInt32(DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE);
            stream.WriteUInt32(0); // dwCaps2
            stream.WriteUInt32(0); // dwCaps3
            stream.WriteUInt32(0); // dwCaps4
            stream.WriteUInt32(0); // dwReserved2

            stream.WriteFromBuffer(src);

            return(stream.ToArray());
        }
示例#3
0
        static private DDS_PF getDDSPixelFormat(PixelFormat format)
        {
            DDS_PF pixelFormat = new DDS_PF();

            switch (format)
            {
            case PixelFormat.DXT1:
                pixelFormat.flags  = DDPF_FOURCC;
                pixelFormat.fourCC = FOURCC_DXT1_TAG;
                break;

            case PixelFormat.DXT3:
                pixelFormat.flags  = DDPF_FOURCC | DDPF_ALPHAPIXELS;
                pixelFormat.fourCC = FOURCC_DXT3_TAG;
                break;

            case PixelFormat.DXT5:
                pixelFormat.flags  = DDPF_FOURCC | DDPF_ALPHAPIXELS;
                pixelFormat.fourCC = FOURCC_DXT5_TAG;
                break;

            case PixelFormat.ATI2:
                pixelFormat.flags  = DDPF_FOURCC;
                pixelFormat.fourCC = FOURCC_ATI2_TAG;
                break;

            case PixelFormat.ARGB:
                pixelFormat.flags = DDPF_ALPHAPIXELS | DDPF_RGB;
                pixelFormat.bits  = 32;
                pixelFormat.Rmask = 0xFF0000;
                pixelFormat.Gmask = 0xFF00;
                pixelFormat.Bmask = 0xFF;
                pixelFormat.Amask = 0xFF000000;
                break;

            case PixelFormat.RGB:
                pixelFormat.flags = DDPF_RGB;
                pixelFormat.bits  = 24;
                pixelFormat.Rmask = 0xFF0000;
                pixelFormat.Gmask = 0xFF00;
                pixelFormat.Bmask = 0xFF;
                break;

            case PixelFormat.V8U8:
                pixelFormat.flags = DDPF_SIGNED;
                pixelFormat.bits  = 16;
                pixelFormat.Rmask = 0xFF;
                pixelFormat.Gmask = 0xFF00;
                break;

            case PixelFormat.G8:
                pixelFormat.flags = DDPF_LUMINANCE;
                pixelFormat.bits  = 8;
                pixelFormat.Rmask = 0xFF;
                break;

            default:
                throw new Exception("invalid texture format " + pixelFormat);
            }
            return(pixelFormat);
        }