示例#1
0
        public static void Apply( Image4f input, Image4f output )
        {
            int w_out = output.Width;
            int h_out = output.Height;

            int w_in = input.Width;
            int h_in = input.Height;

            float x_outToIn = Arithmetic.DivideIntsToFloat( w_in, w_out );
            float y_outToIn = Arithmetic.DivideIntsToFloat( h_in, h_out );

            for( int y = 0; y < output.Height; ++y )
            {
                float yInFloat = ( y + 0.5f ) * y_outToIn;
                int yIn = yInFloat.FloorToInt();

                for( int x = 0; x < output.Width; ++x )
                {
                    float xInFloat = ( x + 0.5f ) * x_outToIn;
                    int xIn = xInFloat.FloorToInt();

                    output[ x, y ] = input[ xIn, yIn ];
                }
            }
        }
示例#2
0
        public void Apply( Image4f input, Image4f output )
        {
            Vector4f[,] controlPoints = new Vector4f[ 4, 4 ];

            int w_out = output.Width;
            int h_out = output.Height;

            int w_in = input.Width;
            int h_in = input.Height;

            // scaling coefficients
            float x_outToIn = Arithmetic.DivideIntsToFloat( w_in, w_out );
            float y_outToIn = Arithmetic.DivideIntsToFloat( h_in, h_out );

            for( int yOut = 0; yOut < h_out; ++yOut )
            {
                float yInFloat = ( yOut + 0.5f ) * y_outToIn;
                int yIn0 = yInFloat.FloorToInt() - 1;
                int yIn1 = yIn0 + 3;

                for( int xOut = 0; xOut < w_out; ++xOut )
                {
                    float xInFloat = ( xOut + 0.5f ) * x_outToIn;
                    int xIn0 = xInFloat.FloorToInt() - 1;
                    int xIn1 = xIn0 + 3;

                    // grab the 16 control points
                    for( int yc = yIn0; yc <= yIn1; ++yc )
                    {   
                        int dy = yc - yIn0;
                        int ycc = Arithmetic.Clamp( yc, 0, h_in );                        

                        for( int xc = xIn0; xc <= xIn1; ++xc )
                        {
                            int dx = xc - xIn0;
                            int xcc = Arithmetic.Clamp( xc, 0, w_in );
                            
                            controlPoints[ xc, yc ] = input[ xcc, ycc ];
                        }
                    }
                    
                    // compute Catmull-Rom splines in the x direction
                    float tx = Arithmetic.FractionalPart( xInFloat );
                    float ty = Arithmetic.FractionalPart( yInFloat );

                    var v0i = EvaluateSpline( controlPoints[ 0, 0 ], controlPoints[ 0, 1 ], controlPoints[ 0, 2 ], controlPoints[ 0, 3 ], tx );
                    var v1i = EvaluateSpline( controlPoints[ 1, 0 ], controlPoints[ 1, 1 ], controlPoints[ 1, 2 ], controlPoints[ 1, 3 ], tx );
                    var v2i = EvaluateSpline( controlPoints[ 2, 0 ], controlPoints[ 2, 1 ], controlPoints[ 2, 2 ], controlPoints[ 2, 3 ], tx );
                    var v3i = EvaluateSpline( controlPoints[ 3, 0 ], controlPoints[ 3, 1 ], controlPoints[ 3, 2 ], controlPoints[ 3, 3 ], tx );

                    var vii = EvaluateSpline( v0i, v1i, v2i, v3i, ty );
                    
                    output[ xOut, yOut ] = vii;
                }
            }
        }
示例#3
0
        /// <summary>
        /// TODO: C# 4.0: default to 0,0,0,0 and 1,1,1,1
        /// 
        /// intensities between blackPoint and whitePoint are linearly scaled between 0 and 1.
        /// intensities < blackPoint are clamped to 0, intensities > whitePoint are clamped to 1.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        /// <param name="blackPoint"></param>
        /// <param name="whitePoint"></param>
        public static void Apply( Image4f input, Image4ub output,
            Vector4f blackPoint, Vector4f whitePoint )
        {
            if( input.Size != output.Size )
            {
                throw new ArgumentException( "input and output must be of the same size" );
            }
            var range = whitePoint - blackPoint;

            for( int k = 0; k < input.NumPixels; ++k )
            {
                Vector4f p = input[ k ];
                Vector4f q = ( p - blackPoint ) / range;
                Vector4ub r = q.ToUnsignedByte();

                output[ k ] = r;
            }
        }
示例#4
0
        public static void CopySubImage( Image4f source, int sx, int sy,
            Image4f target, int tx, int ty,
            int width, int height )
        {
            int sw = source.Width;
            int sh = source.Height;
            int tw = target.Width;
            int th = target.Height;

            if( sx < 0 || sy < 0 || sx >= sw || sy >= sh )
            {
                throw new IndexOutOfRangeException( "Source origin must be inside source image" );
            }
            if( tx < 0 || ty < 0 || tx >= tw || ty >= th )
            {
                throw new IndexOutOfRangeException( "Target origin must be inside target image" );
            }

            int tx0 = tx;
            int tx1 = Math.Min( tx + width, tw );
            int ty0 = ty;
            int ty1 = Math.Min( ty + height, th );

            for( int y = ty0; y < ty1; ++y )
            {
                int dy = y - ty0;
                int iy = sy + dy.Clamp(  0, sh );

                for( int x = tx0; x < tx1; ++x )
                {
                    int dx = x - tx0;
                    int ix = ( sx + dx ).Clamp( 0, sw );

                    target[ x, y ] = source[ ix, iy ];
                }
            }
        }
示例#5
0
        public static void Apply( Image4f input, Image4f output )
        {
            int w_out = output.Width;
            int h_out = output.Height;

            int w_in = input.Width;
            int h_in = input.Height;

            // scaling coefficients
            float x_outToIn = Arithmetic.DivideIntsToFloat( w_in, w_out );
            float y_outToIn = Arithmetic.DivideIntsToFloat( h_in, h_out );

            for( int yOut = 0; yOut < h_out; ++yOut )
            {
                float yInFloat = ( yOut + 0.5f ) * y_outToIn;

                for( int xOut = 0; xOut < w_out; ++xOut )
                {
                    float xInFloat = ( xOut + 0.5f ) * x_outToIn;

                    output[ xOut, yOut ] = input.BilinearSample( xInFloat, yInFloat );
                }
            }
        }
示例#6
0
 public static DynamicTexture2D FromImage( Image4f im )
 {
     var tex = CreateFloat4( im.Size );
     tex.Update( im );
     return tex;
 }
示例#7
0
        public void Update( Image4f im )
        {
            if( Texture.Description.Format != Format.R32G32B32A32_Float )
            {
                throw new ArgumentException( "Can only update a float texture with a float image" );
            }
            
            var rect = Texture.Map( 0, MapMode.WriteDiscard, MapFlags.None );
            int pitch = rect.Pitch;

            int width = im.Width;
            int height = im.Height;
            var pixels = im.Pixels;
            const int pixelSizeBytes = 4 * sizeof( float );

            // if the pitch matches, then just write it all at once
            if( pitch == pixelSizeBytes * width )
            {
                rect.Data.WriteRange( pixels );
            }
            else
            {
                // otherwise, write one row at a time
                for( int y = 0; y < height; ++y )
                {
                    rect.Data.Position = y * pitch;
                    rect.Data.WriteRange( pixels, 4 * y * width, 4 * width );
                }
            }

            rect.Data.Close();
            Texture.Unmap( 0 );
        }
示例#8
0
 /// <summary>
 /// In-place format conversion between two images of the same size.
 /// </summary>
 /// <param name="input"></param>
 /// <param name="output"></param>
 public static void ToUnsignedByte( Image4f input, Image4ub output )
 {            
     for( int k = 0; k < input.NumPixels; ++k )
     {
         output[ k ] = input[ k ].ToUnsignedByte();
     }            
 }