コード例 #1
0
ファイル: ILUtil.cs プロジェクト: suntabu/axiom
        public static void ConvertToIL(PixelBox src)
        {
            // ilTexImage http://openil.sourceforge.net/docs/il/f00059.htm
            var ifmt = Convert(src.Format);

            if (src.IsConsecutive && ifmt.IsValid)
            {
                // The easy case, the buffer is laid out in memory just like
                // we want it to be and is in a format DevIL can understand directly
                // We could even save the copy if DevIL would let us
                Il.ilTexImage(src.Width, src.Height, src.Depth, (byte)ifmt.Channels, ifmt.Format, ifmt.Type, src.Data.Pin());
                src.Data.UnPin();
            }
            else if (ifmt.IsValid)
            {
                // The format can be understood directly by DevIL. The only
                // problem is that ilTexImage expects our image data consecutively
                // so we cannot use that directly.

                // Let DevIL allocate the memory for us, and copy the data consecutively
                // to its memory
                Il.ilTexImage(src.Width, src.Height, src.Depth, (byte)ifmt.Channels, ifmt.Format, ifmt.Type, IntPtr.Zero);
                using (var dstbuf = BufferBase.Wrap(Il.ilGetData(), Il.ilGetInteger(Il.IL_IMAGE_SIZE_OF_DATA)))
                {
                    var dst = new PixelBox(src.Width, src.Height, src.Depth, src.Format, dstbuf);
                    PixelConverter.BulkPixelConversion(src, dst);
                }
            }
            else
            {
                // Here it gets ugly. We're stuck with a pixel format that DevIL
                // can't do anything with. We will do a bulk pixel conversion and
                // then feed it to DevIL anyway. The problem is finding the best
                // format to convert to.

                // most general format supported by Axiom and DevIL
                var fmt = PixelUtil.HasAlpha(src.Format) ? PixelFormat.FLOAT32_RGBA : PixelFormat.FLOAT32_RGB;

                // Make up a pixel format
                // We don't have to consider luminance formats as they have
                // straight conversions to DevIL, just weird permutations of RGBA an LA
                var depths = PixelUtil.GetBitDepths(src.Format);

                // Native endian format with all bit depths<8 can safely and quickly be
                // converted to 24/32 bit
                if (PixelUtil.IsNativeEndian(src.Format) && depths[0] <= 8 && depths[1] <= 8 && depths[2] <= 8 &&
                    depths[3] <= 8)
                {
                    if (PixelUtil.HasAlpha(src.Format))
                    {
                        fmt = PixelFormat.A8R8G8B8;
                    }
                    else
                    {
                        fmt = PixelFormat.R8G8B8;
                    }
                }

                // Let DevIL allocate the memory for us, then do the conversion ourselves
                ifmt = Convert(fmt);
                Il.ilTexImage(src.Width, src.Height, src.Depth, (byte)ifmt.Channels, ifmt.Format, ifmt.Type, IntPtr.Zero);
                // TAO 2.0
                //Il.ilTexImage( src.Width, src.Height, src.Depth, (byte)ifmt.Channels, ifmt.Format, ifmt.Type, null );
                using (var dstbuf = BufferBase.Wrap(Il.ilGetData(), Il.ilGetInteger(Il.IL_IMAGE_SIZE_OF_DATA)))
                {
                    var dst = new PixelBox(src.Width, src.Height, src.Depth, fmt, dstbuf);
                    PixelConverter.BulkPixelConversion(src, dst);
                }
            }
        }