一般的な補間アルゴリズムを高速に実装する必要があります。このような単純なアルゴリズムの C# は C++ よりもはるかに遅いことがわかったので、ネイティブ コードを記述して C# GUI で使用することを考えています。
まず、いくつかのテストを実行し、1024x1024x3
マトリックスでいくつかの操作を実行すると、C# で 32 ミリ秒、C++ で 4 ミリ秒かかりました。これが基本的に必要なものです。
ただし、補間はダウンスケーリングにのみ必要なので、良い言葉ではありません。しかし問題は、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;
}
面白いことに、HighQualityBicubic は Bicubic よりもはるかに高速です。(40ms 対 100ms)