for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { minMagnitude = Math.Min(minMagnitude, data[y,x].Magnitude); maxMagnitude = Math.Max(maxMagnitude, data[y,x].Magnitude); minLog = Math.Min(minLog, Math.Log(data[y, x].Magnitude)); maxLog = Math.Max(maxLog, Math.Log(data[y, x].Magnitude)); } } // do the job unsafe { byte* dst = (byte*)dstData.Scan0.ToPointer(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, dst++) { if (logTransform) *dst = (byte)System.Math.Max(0, System.Math.Min(255, (Math.Log(data[y, x].Magnitude) - minLog) * 255 / (maxLog - minLog))); else *dst = (byte)System.Math.Max(0, System.Math.Min(255, data[y, x].Magnitude * scale * 255)); } dst += offset; } }