using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Threading.Tasks;
namespace Fourier_Transform__Test
public partial class ImageDataConverter
#region private static Complex[,] FromBitmapData(BitmapData bmpData)
private static Complex[,] ToComplex(BitmapData bmpData)
Complex[,] complex2D = null;
if (bmpData.PixelFormat == PixelFormat.Format8bppIndexed)
int width = bmpData.Width;
int height = bmpData.Height;
int offset = bmpData.Stride - (width * 1);
if ((!Tools.IsPowerOf2(width)) || (!Tools.IsPowerOf2(height)))
throw new Exception("Imager width and height should be n of 2.");
complex2D = new Complex[width, height];
byte* src = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++, src++)
complex2D[y, x] = new Complex((float)*src / 255,
complex2D[y, x].Imaginary);
throw new Exception("EightBppIndexedImageRequired");
public static Complex[,] ToComplex(Bitmap bmp)
Complex[,] complex2D = null;
if (bmp.PixelFormat == PixelFormat.Format8bppIndexed)
BitmapData bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
PixelFormat.Format8bppIndexed);
complex2D = ToComplex(bmpData);
throw new Exception("EightBppIndexedImageRequired");
public static Bitmap ToBitmap(Complex[,] complex2D, bool fourierTransformed)
int width = complex2D.GetLength(0);
int height = complex2D.GetLength(1);
Bitmap bmp = Imager.CreateGrayscaleImage(width, height);
BitmapData bmpData = bmp.LockBits(
new Rectangle(0, 0, width, height),
PixelFormat.Format8bppIndexed);
int offset = bmpData.Stride - width;
double scale = (fourierTransformed) ? Math.Sqrt(width * height) : 1;
byte* address = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++, address++)
double min = System.Math.Min(255, complex2D[y, x].Magnitude * scale * 255);
*address = (byte)System.Math.Max(0, min);
public static double[,] ToDouble(Bitmap input, double Min, double Max)
BitmapData bitmapData = input.LockBits(new Rectangle(0, 0, input.Width, input.Height),
ImageLockMode.ReadOnly, input.PixelFormat);
int height = input.Height;
int pixelSize = Bitmap.GetPixelFormatSize(input.PixelFormat) / 8;
int offset = bitmapData.Stride - input.Width * pixelSize;
output = new double[height, width];
fixed (double* ptrData = output)
byte* src = (byte*)bitmapData.Scan0.ToPointer();
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
double scaled = Tools.Scale(fromMin: 0, fromMax: 255, toMin: Min, toMax: Max, x: *src);
input.UnlockBits(bitmapData);
public static Bitmap ToBitmap(double[,] input, double Min, double Max)
int width = input.GetLength(1);
int height = input.GetLength(0);
output = Imager.CreateGrayscaleImage(width, height);
BitmapData data = output.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
int pixelSize = System.Drawing.Image.GetPixelFormatSize(PixelFormat.Format8bppIndexed) / 8;
int offset = data.Stride - width * pixelSize;
byte* dst = (byte*)data.Scan0.ToPointer();
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
double v = 255 * (input[y, x] - Min) / (Max - Min);
byte value = unchecked((byte)v);
for (int c = 0; c < pixelSize; c++, dst++)
dst = (byte*)data.Scan0.ToPointer();
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++, dst += pixelSize)
public static Complex[,] ToComplex(double[,] input, double Min, double Max)
int width = input.GetLength(1);
int height = input.GetLength(0);
Bitmap bImage = ImageDataConverter.ToBitmap(input,0,255);
Complex[,] output = ImageDataConverter.ToComplex(bImage);
public static double[,] ToDouble(Complex[,] input, bool fourierTransformed)
int width = input.GetLength(1);
int height = input.GetLength(0);
double scaleFactor = (fourierTransformed) ? Math.Sqrt(width * height) : 1;
double[,] output = new double[width, height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
double tempValue = System.Math.Min(255, input[y, x].Magnitude * scaleFactor * 255);
double value = (byte)System.Math.Max(0, tempValue);