views:

277

answers:

3

I am trying to quickly process large images (about 2000x2000). I am using a TrackBar to change the overall brightness of an image. The problem is that the TrackBar (as you all know) can be moved very quickly. This is ok as I do not need to process the image for every tick of the TrackBar, but it does need to be reasonably responsive. Think of the brightness TrackBars found in image editors.

I have tried doing all of the processing in another thread, which works, but it is still too slow (even using LockBits on the Bitmap). I can't use a ColorMatrix because it allows component overflows and I need values to clip at 255. So, I am thinking that I can achieve good performance by using the ThreadPool and splitting the image up into sections.

The problem with this approach is that I just don't have much experience with multi-trheaded applications and I don't know how to avoid the race conditions that crop up (calling LockBits on an already locked image, etc.). Could anyone give me an example of how to do this?

Here is the code that I have so far. I know that it is far from good, but I am just working it out and trying a bunch of different things at this point. The basic concept is to use a base image as a source, perform some operation on each pixel, then draw the processed pixel into a display Bitmap. Any help would be awesome.

    public Form1( )
    {
        InitializeComponent( );
        testPBox1.Image = Properties.Resources.test;
        trackBar1.Value = 100;
        trackBar1.ValueChanged += trackBar1_ValueChanged;        
    }     

    void trackBar1_ValueChanged( object sender, EventArgs e )
    {
        testPBox1.IntensityScale = (float) trackBar1.Value / 100;
    }

class TestPBox : Control
{
    private const int MIN_SAT_WARNING = 240;        
    private Bitmap m_srcBitmap;
    private Bitmap m_dispBitmap;
    private float m_scale;           

    public TestPBox( )
    {
        this.DoubleBuffered = true;                                              
        IntensityScale = 1.0f;
    }      

    public Bitmap Image
    {
        get
        {
            return m_dispBitmap;
        }
        set
        {
            if ( value != null )
            {
                m_srcBitmap = value;
                m_dispBitmap = (Bitmap) value.Clone( );
                Invalidate( );
            }
        }
    }

    [DefaultValue( 1.0f )]
    public float IntensityScale
    {
        get
        {
            return m_scale;
        }
        set
        {
            if ( value == 0.0 || m_scale == value )
            {                   
                return;
            }

            if ( !this.DesignMode )
            {
                m_scale = value;
                ProcessImage( );                    
            }
        }
    }   

    private void ProcessImage( )
    {
        if ( Image != null )
        {
            int sections = 10;
            int sectionHeight = (int) m_srcBitmap.Height / sections;
            for ( int i = 0; i < sections; ++i )
            {
                Rectangle next = new Rectangle( 0, i * sectionHeight, m_srcBitmap.Width, sectionHeight );
                ThreadPool.QueueUserWorkItem( new WaitCallback( delegate { ChangeIntensity( next ); } ) );
            }
        }
    }

    private unsafe void ChangeIntensity( Rectangle rect )
    {            
        BitmapData srcData = m_srcBitmap.LockBits( rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb );
        BitmapData dspData = m_dispBitmap.LockBits( rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb );            
        byte* pSrc = (byte*) srcData.Scan0;
        byte* pDsp = (byte*) dspData.Scan0;

        for ( int y = 0; y < rect.Height; ++y )
        {
            for ( int x = 0; x < rect.Width; ++x )
            {
                // we are dealing with a monochrome image, so r = g = b.
                // We only need to get one component value to use for all r, g, and b.
                byte b = (byte) CompMinMax( 0, 255, (int) ( pSrc[0] * m_scale ) );
                Color c = ( b > MIN_SAT_WARNING ) ? Color.FromArgb( b, Color.Red ) : Color.FromArgb( 255, b, b, b );                     

                // windows stores images internally in 
                // reverse byte order, i.e., Bgra, not Argb.
                pDsp[3] = (byte) c.A;
                pDsp[2] = (byte) c.R;
                pDsp[1] = (byte) c.G;
                pDsp[0] = (byte) c.B;

                pSrc += 4;
                pDsp += 4;                        
            }
        }

        m_srcBitmap.UnlockBits( srcData );
        m_dispBitmap.UnlockBits( dspData );
        this.Invalidate( );                      
    }

    private int CompMinMax( int min, int max, int value )
    {
        if ( value > max ) return max;
        if ( value < min ) return min;
        return value;
    }        

    protected override void OnPaint( PaintEventArgs e )
    {
        if ( Image != null )
        {               
            Graphics g = e.Graphics;
            Rectangle drawingRect = PaintUtils.CenterInRect( ClientRectangle, PaintUtils.ScaleRect( ClientRectangle, Image.Size ).Size );
            g.DrawImage( Image, drawingRect, 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel );         
        }

        base.OnPaint( e );
    }
}
+3  A: 

Have you considered generating a scaled-down version of the image and doing the image manipulation on that while the user is dragging the trackbar? That would give you responsiveness while still giving the user useful feedback until they settle on a final value.

geofftnz
I have thought about it...will try...
Ed Swangren
Excellent 'out of the box' thinking!
Jack
+1  A: 

If you want to use your current approach, there are a couple minor tweaks you could make that would get rid of the race conditions.

The thing you'd need to change would be to adjust it so that you call LockBits/UnlockBits in ProcessImage, not in the individual thread pool requests. However, you'd need to also track a WaitHandle that was passed into your threadpool delegate so you could block until all of your threadpool threads were completed. Just don't unlock the bits, invalidate, and return until after all of your delegates have finished.

However, depending on your release timeframe, you might want to consider using the task parallel library instead of the ThreadPool. C# 4 will include this, but if you can't wait that long, the Mono version works reasonably well today. It actually makes this type of work quite a bit easier than doing it via the ThreadPool.

Reed Copsey
A: 

Generally, multi-threaded processing on subsets of image is possible but usually makes sense for algorithms which can yield the same result by processing subsets as by processing the whole image. In other words, if A and B are subsets of image, this should be valid equation:

alg(A + B) = alg(A) + alg(B)

So, you need to identify if your some operation belongs to this class of algorithms.

mloskot