private static void ThreadedResize(ref float[,] data, int newWidth, int newHeight) { if (_mutex == null) { _mutex = new Mutex(false); } _oldArray = data; _oldWidth = data.GetUpperBound(0) + 1; _newArray = new float[newWidth, newHeight]; _newWidth = newWidth; _ratioX = (float)(data.GetUpperBound(0)) / (float)_newWidth; _ratioY = (float)(data.GetUpperBound(1)) / (float)newHeight; int coresToUse = _processorCount > 1 ? _processorCount : 1; int cores = Math.Min(coresToUse, newHeight); int slice = (int)(Math.Floor((float)newHeight / cores)); _finishedCount = 0; for (int i = 0; i < cores - 1; ++i) { ArrayResizeThreadData td = new ArrayResizeThreadData(slice * i, slice * (i + 1)); ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResizeFuntion); Thread newThread = new Thread(threadStart); newThread.Start(td); } ArrayResizeThreadData lastSlice = new ArrayResizeThreadData(slice * (cores - 1), newHeight); ResizeFuntion(lastSlice); while (_finishedCount < cores) { Thread.Sleep(1); } data = _newArray; }
private static void ResizeFuntion(System.Object obj) { ArrayResizeThreadData td = (ArrayResizeThreadData)obj; int oldArrayWidth = _oldArray.GetUpperBound(0) + 1; for (int y = td.Start; y < td.End; ++y) { int yFloor = (int)(y * _ratioY); int yCeil = (int)Math.Ceiling(y * _ratioY); for (int x = 0; x <= _newArray.GetUpperBound(0); ++x) { int xFloor = (int)(x * _ratioX); int xCeil = (int)Math.Ceiling(x * _ratioX); float xLerp = (x * _ratioX) - (float)xFloor; _newArray [x, y] = Lerp( Lerp(_oldArray [xFloor, yFloor], _oldArray [xCeil, yFloor], xLerp), Lerp(_oldArray [xFloor, yCeil], _oldArray [xCeil, yCeil], xLerp), (y * _ratioY) - (float)yFloor); } } _mutex.WaitOne(); ++_finishedCount; _mutex.ReleaseMutex(); }