Speed ​​of interpolation algorithms, C # and C ++ working together

I need a fast implementation of popular interpolation algorithms. I realized that C # in such simple algorithms would be much slower than C ++, so I am thinking about writing my own code and using it in my C # GUI.

First of all, I am running some tests and several operations on the matrix 1024x1024x3

took 32ms in C # and 4ms in C ++ and this is what I basically need.

Interpolation, however, is not a good word because I only need them to zoom out. But the question arises: will it be faster than the C # methods in Drawing2D

Image outputImage = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
Graphics grPhoto = Graphics.FromImage(outputImage);

grPhoto.InterpolationMode = InterpolationMode.*; //all of them

grPhoto.DrawImage(bmp, new Rectangle(0, 0, destWidth, destHeight),
Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

grPhoto.Dispose();

      

Some of these methods take 20ms and some take 80s. Is there a faster way to do this?


EDIT 1:

First of all, I am using XNA in this application, but there seems to be no way to choose another interpolation method. Of course, it works really fast.

The ideal way is to implement these techniques on a graphics card.


EDIT 2:

Here's my method:

private unsafe Texture2D Scale(GraphicsDevice gd, Texture2D texture, float scale)
    {

        int sourceWidth = texture.Width;
        int sourceHeight = texture.Height;

        int destWidth = (int)(sourceWidth * scale);
        int destHeight = (int)(sourceHeight * scale);

        StopwatchEx sw = new StopwatchEx();
        sw.IntervalStart();
        //convert texture into bitmap
        byte[] textureData = new byte[4 * sourceWidth * sourceHeight];
        texture.GetData<byte>(textureData);

        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(sourceWidth, sourceHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
        System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, sourceWidth, sourceHeight), System.Drawing.Imaging.ImageLockMode.WriteOnly,
                       System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        IntPtr safePtr = bmpData.Scan0;
        System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
        bmp.UnlockBits(bmpData);


        //output bitmap
        System.Drawing.Image outputImage = new System.Drawing.Bitmap(destWidth, destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        System.Drawing.Graphics grPhoto = System.Drawing.Graphics.FromImage(outputImage);

        grPhoto.InterpolationMode = (System.Drawing.Drawing2D.InterpolationMode)(interpolationMode);
        grPhoto.SmoothingMode = (System.Drawing.Drawing2D.SmoothingMode)smoothingMode;
        grPhoto.PixelOffsetMode = (System.Drawing.Drawing2D.PixelOffsetMode)pixelOffsetMode;

        grPhoto.DrawImage((System.Drawing.Image)bmp, new System.Drawing.Rectangle(0, 0, destWidth, destHeight),
            new System.Drawing.Rectangle(0, 0, sourceWidth, sourceHeight), System.Drawing.GraphicsUnit.Pixel);

        grPhoto.Dispose();

        textureData = new byte[4 * sourceWidth * sourceHeight];

        MemoryStream ms = new MemoryStream();
        ((System.Drawing.Bitmap)outputImage).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

        ms.Seek(0, SeekOrigin.Begin);
        Texture2D result = Texture2D.FromFile(gd, ms);
        ms.Dispose();
        sw.IntervalStop();
        sw.AppendResults("MEGS.txt");    

        return result;
    }

      

Funny how HighQualityBicubic is much faster than Bicubic. (40ms vs 100ms)

+2


a source to share


2 answers


How did you implement your matrix in C #?

In your case, a lot of speed might be lost due to the fact that .Net has to do bounds checks on the default matrix array. In this case, you can make the code much faster by using unsafe C # (and thus removing all bounds checks).



But if it already works fast enough with external methods, why not use them?

+1


a source


Have you tried this with a method GetThumbnailImage

?



Expanding Further Comment by Adam Robinson : I hope you timed this using the Stopwatch class ?

0


a source







All Articles