Ejemplo n.º 1
0
 public static void CopyToImage( this BitArray arr, Rect2i rect, int channel, Image4ub im )
 {
     int i = 0;
     int x0 = rect.Origin.x;
     int y0 = rect.Origin.y;
     for( int y = 0; y < rect.Height; ++y )
     {
         for( int x = 0; x < rect.Width; ++x )
         {
             if( arr[ i ] )
             {
                 var color = im.GetPixelFlipped( x0 + x, y0 + y );
                 if( channel == 0 )
                 {
                     color.x = 255;
                 }
                 else if( channel == 1 )
                 {
                     color.y = 255;
                 }
                 else
                 {
                     color.z = 255;
                 }
                 im.SetPixelFlipped( x0 + x, y0 + y, color );
             }
             ++i;
         }
     }            
 }
Ejemplo n.º 2
0
 public static Image4ub ToUnsignedByte( this Image4f im )
 {
     var output = new Image4ub( im.Size );
     for( int k = 0; k < im.NumPixels; ++k )
     {
         output[ k ] = im[ k ].ToUnsignedByte();
     }
     return output;
 }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 4
0
        public static void Apply( Image4ub input, Image4ub 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 );
                }
            }
        }
Ejemplo n.º 5
0
        public void Apply( Image4ub input, Image4ub 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 ] = FormatConversion.ToFloat( 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 ] = FormatConversion.ToUnsignedByte( vii );
                }
            }
        }        
Ejemplo n.º 6
0
 public static DynamicTexture2D FromImage( Image4ub im )
 {
     var tex = CreateUnsignedByte4( im.Size );
     tex.Update( im );
     return tex;
 }
Ejemplo n.º 7
0
        // TODO: if it's not ub4 or float4
        public void UpdateRectangle( Image4ub im, Rect2i source, Rect2i target )
        {
            // TODO: check boundaries

            if( source.Width != target.Width ||
                source.Height != target.Height )
            {
                throw new ArgumentException( "Mismatched rectangle sizes." );
            }

            var rect = Texture.Map( 0, MapMode.Write, MapFlags.None );

            // find the byte array offsets and the count for the rectangles            
            int sourceOffset = 4 * ( source.Origin.y * im.Width + source.Origin.x );
            int sourceCount = 4 * source.Area;
            int targetOffset = 4 * ( target.Origin.y * im.Width + target.Origin.x );

            rect.Data.Position = targetOffset;
            rect.Data.Write( im.Pixels, sourceOffset, sourceCount );
            rect.Data.Close();
            Texture.Unmap( 0 );
        }
Ejemplo n.º 8
0
        public void UpdateRectangles( IList< Image4ub > images, IList< Rect2i > targets )
        {
#if false
            var wrapper = D3D10Wrapper.Instance;

            var im = new Image4ub( this.Size, Color4ub.Red );
            fixed( void* ptr = im.Pixels )
            {
                var userBuffer = new IntPtr( ptr );
                var dataStream = new DataStream( userBuffer, 4 * im.NumPixels, true, true );
                var sourceBox = new DataBox( 4 * im.Width, 4 * im.Width * im.Height, dataStream );

                var targetRegion = new ResourceRegion
                {
                    Back = 1,
                    Front = 0,
                    Bottom = 4096,
                    Top = 0,
                    Left = 0,
                    Right = 4096
                };

                wrapper.Device.UpdateSubresource( sourceBox, Texture, 0, targetRegion );
                dataStream.Close();
            }

            return;
#endif
            if( images.Count != targets.Count )
            {
                throw new ArgumentException( "images and targets must be of the same length" );
            }

            var rect = Texture.Map( 0, MapMode.WriteDiscard, MapFlags.None );

            for( int i = 0; i < images.Count; ++i )
            {
                var im = images[ i ];
                var target = targets[ i ];

                for( int y = 0; y < im.Height; ++y )
                {
                    int sourceOffset = 4 * y * im.Width;
                    int sourceCount = 4 * im.Width;

                    rect.Data.Position = 4 * ( ( y + target.Origin.y ) * Width + target.Origin.x );
                    rect.Data.Write( im.Pixels, sourceOffset, sourceCount );
                }
            }

            rect.Data.Close();
            Texture.Unmap( 0 );
        }
Ejemplo n.º 9
0
        // then the pitch actually matters
        public void Update( Image4ub im )
        {
            if( !( im.Size.Equals( Size ) ) )
            {
                throw new ArgumentException( "Image size mismatch" );
            }

            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;

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

            rect.Data.Close();
            Texture.Unmap( 0 );
        }
Ejemplo n.º 10
0
        public static void CopySubImage( Image4ub source, int sx, int sy,
            Image4ub 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 ];
                }
            }
        }        
Ejemplo n.º 11
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();
     }            
 }
Ejemplo n.º 12
0
        public static void RasterizeTriangle( Vector2f[] vertices, Image4ub image )
        {
            // set up edges
            Vector2f e0 = vertices[ 1 ] - vertices[ 0 ];
            Vector2f e1 = vertices[ 2 ] - vertices[ 1 ];
            Vector2f e2 = vertices[ 0 ] - vertices[ 2 ];

            // get edge normals
            Vector2f n0 = e0.OrthogonalVector();
            Vector2f n1 = e1.OrthogonalVector();
            Vector2f n2 = e2.OrthogonalVector();

            // Get the bounding box
            Rect2f bbox = TriangleUtilities.BoundingBox( vertices );
            int xStart = bbox.Origin.x.FloorToInt().Clamp( 0, image.Width );
            int xEnd = bbox.TopRight.x.FloorToInt().Clamp( 0, image.Width );
            int yStart = bbox.Origin.y.FloorToInt().Clamp( 0, image.Height );
            int yEnd = bbox.TopRight.y.FloorToInt().Clamp( 0, image.Height );
            
            for( int y = yStart; y <= yEnd; ++y )            
            {
                for( int x = xStart; x <= xEnd; ++x )
                {
                    Vector2f p = new Vector2f( x + 0.5f, y + 0.5f );

                    float t0 = EdgeTest( n0, vertices[ 0 ], p );
                    float t1 = EdgeTest( n1, vertices[ 1 ], p );
                    float t2 = EdgeTest( n2, vertices[ 2 ], p );

                    if( ( ( t0 > 0 ) && ( t1 > 0 ) && ( t2 > 0 ) ) ||
                        ( ( t0 < 0 ) && ( t1 < 0 ) && ( t2 < 0 ) ) )
                    {
                        image[ x, y ] = new Vector4ub( 255, 255, 255, 255 );
                    }
                }
            }
        }
Ejemplo n.º 13
0
 public static void RasterizeTriangles( IEnumerable< Vector2f[] > triangles, Image4ub image )
 {            
     foreach( Vector2f[] vertices in triangles )
     {
         RasterizeTriangle( vertices, image );
     }
 }