Bitmap rotate image slow (500ms)

When using the RotateImage method of the bitmap object, it takes about 500ms to complete its operation. Is there anyway to speed this up?

I need about a 9Hz refresh rate on the image. What I am doing is making a steering wheel that I can rotate as data is updated which occurs at about 10Hz. The only way I could think of doing this is with the Bitmap since there is no native gauge object.

Thoughts?

// Background
            byte[] bgImage = Resources.GetBytes(Resources.BinaryResources.bg);
            Bitmap bgBitMap = new Bitmap(bgImage, Bitmap.BitmapImageType.Jpeg);
            lcd.DrawImage(0, 0, bgBitMap, 0, 0, SystemMetrics.ScreenWidth, SystemMetrics.ScreenHeight);
            lcd.Flush();
            
            // LR logo
            byte[] lrBytes = Resources.GetBytes(Resources.BinaryResources.lrLogo);
            Bitmap lrBitmap = new Bitmap(lrBytes, Bitmap.BitmapImageType.Jpeg);
            lcd.DrawImage(0,0, lrBitmap, 0, 0, 150,45);
            lcd.Flush(0,0,150,45);

            // Steering wheel
            byte[] image = Resources.GetBytes(Resources.BinaryResources.sw);
            Bitmap bitMap = new Bitmap(image, Bitmap.BitmapImageType.Jpeg);
            lcd.DrawImage(125,50, bitMap, 0, 0, 200, 200);
            lcd.Flush(125,50,200,200);

            int degrees = -90;
            Stopwatch sw = new Stopwatch();

            while (true)
            {
                sw.Start();
                //Thread.Sleep(10); // Update rate on steering wheel is 10Hz or 100ms(p)
                lcd.RotateImage(degrees++, 125,50,bitMap, 0,0, 200,200, 255);
                lcd.Flush(125, 50, 200, 200);
                sw.Stop();

                int et = sw.ElapsedDuration.Milliseconds;

                if (degrees == 180)
                    degrees = -90;
            }

At that kind of speed, you need RLP.

@ Mr. John Smith - rotating uses a lot of trigonometry. It will always be slow without hardware rotate functionality.

Rotating 90 degrees or multiple should be much faster as no trig is need.

The other alternative is to draw the image yourself, rather than using a bitmap.

Or store cached versions of the drawn image that you can load quickly.

2 Likes

The fastest way to do bitmap rotation without dedicated hardware is by shears.

https://www.ocf.berkeley.edu/~fricke/projects/israel/paeth/rotation_by_shearing.html

Though you would probably still need RLP to make it run fast enough.

IMO half the fun of programming on micros is learning weird tricks to get the needed performance.

The simple solution though is what @ Mr. John Smith suggests and have precalculated cached versions. More often than not(probably always) there is such a solution that trades reduced processing for higher storage requirements.

2 Likes