Axiom.Plugins.DevILCodecs.ILUtil.ConvertToIL C# (CSharp) Method

ConvertToIL() static public method

static public ConvertToIL ( PixelBox src ) : void
src Axiom.Media.PixelBox
return void
		static public void ConvertToIL( PixelBox src )
		{
			// ilTexImage http://openil.sourceforge.net/docs/il/f00059.htm
			ILFormat 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 );
			}
			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 );
				PixelBox dst = new PixelBox( src.Width, src.Height, src.Depth, src.Format, Il.ilGetData() );
				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
				PixelFormat 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
				int[] 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 );
				IntPtr data = Il.ilGetData();
				PixelBox dst = new PixelBox( src.Width, src.Height, src.Depth, fmt, data );
				PixelConverter.BulkPixelConversion( src, dst );
			}
		}

Usage Example

Beispiel #1
0
        public override void EncodeToFile(Stream input, string outFileName, Codec.CodecData codecData)
        {
            int imageID;

            // create and bind a new image
            Il.ilGenImages(1, out imageID);
            Il.ilBindImage(imageID);

            var buffer = new byte[input.Length];

            input.Read(buffer, 0, buffer.Length);

            var imgData = (ImageData)codecData;

            PixelBox src;

            using (var bufHandle = BufferBase.Wrap(buffer))
            {
                src = new PixelBox(imgData.width, imgData.height, imgData.depth, imgData.format, bufHandle);
            }

            try
            {
                // Convert image from Axiom to current IL image
                ILUtil.ConvertToIL(src);
            }
            catch (Exception ex)
            {
                LogManager.Instance.Write("IL Failed image conversion :", ex.Message);
            }

            // flip the image
            Ilu.iluFlipImage();

            // save the image to file
            Il.ilSaveImage(outFileName);

            var error = Il.ilGetError();

            if (error != Il.IL_NO_ERROR)
            {
                LogManager.Instance.Write("IL Error, could not save file: {0} : {1}", outFileName, Ilu.iluErrorString(error));
            }

            Il.ilDeleteImages(1, ref imageID);
        }