代码之家  ›  专栏  ›  技术社区  ›  Kaminari

插补算法的速度,C++和C++的协同工作

  •  2
  • Kaminari  · 技术社区  · 14 年前

    我需要快速实现流行的插值算法。我发现C在这样简单的算法中会比C++慢得多,所以我想编写一些本地代码并将其应用到我的C语言GUI中。

    首先,我运行一些测试和一些操作 1024x1024x3 矩阵在C中使用了32毫秒,在C++中使用了4ms,这就是我所需要的。

    但插值不是一个好词,因为我只需要他们缩小规模。但问题是:它是否会比Drawing2D中的C方法更快?

    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();
    

    这些方法有的在20毫秒内完成,有的在80毫秒内完成。有没有办法更快的完成?


    编辑1:

    首先,我在这个应用程序中使用xna,但是似乎没有办法选择不同的插值方法。当然它工作得很快。

    理想的方法是在图形卡上实现这些方法。


    编辑2:

    以下是我的整个方法:

    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;
        }
    

    有趣的是高质量双三次比双三次快得多。(40毫秒对100毫秒)

    2 回复  |  直到 14 年前
        1
  •  1
  •   Foxfire    14 年前

    你是如何在C_中实现矩阵的?

    在您的情况下,由于默认情况下.NET必须在矩阵数组中执行边界检查,因此可能会损失很多速度。在这种情况下,您可以使用不安全的c(从而删除任何边界检查)使代码运行得更快。

    但是,如果它已经足够快地与外部方法一起工作,为什么不使用它们呢?

        2
  •  0
  •   Community kfsone    7 年前

    你试过这个吗 GetThumbnailImage 方法?

    进一步扩展 Adam Robinson's comment :我希望您使用 Stopwatch class ?