/// <summary> /// Converts an image to an bitmap. /// </summary> /// <param name="img">Input image.</param> /// <returns>Bitmap</returns> public static Bitmap ToBitmap(this Gray<short>[,] img) { Bitmap bmp = null; using (var uImg = img.Lock()) { bmp = toBitmap(uImg, PixelFormat.Format16bppGrayScale); } return bmp; }
/// <summary> /// Converts an image to an bitmap. /// </summary> /// <param name="img">Input image.</param> /// <returns>Bitmap</returns> public static Bitmap ToBitmap(this Gray<byte>[,] img) { Bitmap bmp = null; using (var uImg = img.Lock()) { bmp = toBitmap(uImg, PixelFormat.Format8bppIndexed); } return bmp; }
/// <summary> /// Replaces the selected image channel with the specified channel. /// </summary> /// <typeparam name="TSrcColor">Source color type.</typeparam> /// <typeparam name="TDepth">Channel depth type.</typeparam> /// <param name="image">Image.</param> /// <param name="channel">Channel.</param> /// <param name="channelIndex">Index of a channel to replace.</param> public static void ReplaceChannel <TSrcColor, TDepth>(this TSrcColor[,] image, Gray <TDepth>[,] channel, int channelIndex) where TSrcColor : unmanaged, IColor <TDepth> where TDepth : unmanaged { using (var im = image.Lock()) using (var ch = channel.Lock()) { replaceChannel <TSrcColor, TDepth>(im, ch, channelIndex); } }
/// <summary> /// Converts the specified managed array to the corresponding bitmap source. /// </summary> /// <param name="image">Managed array.</param> /// <returns>Bitmap source.</returns> public static BitmapSource ToBitmapSource(this Gray <short>[,] image) { BitmapSource bmpSource = null; using (var uImg = image.Lock()) { bmpSource = BitmapSource.Create(uImg.Width, uImg.Height, 96, 96, PixelFormats.Gray16, BitmapPalettes.Gray256, uImg.ImageData, uImg.Stride * uImg.Height, uImg.Stride); } return(bmpSource); }
/// <summary> /// Extracts a single image channel. /// </summary> /// <typeparam name="TSrcColor">Source color type.</typeparam> /// <typeparam name="TDepth">Channel depth type.</typeparam> /// <param name="image">Image.</param> /// <param name="area">Working area.</param> /// <param name="channelIndex">Channel index.</param> /// <returns>Extracted channel.</returns> public static unsafe Gray <TDepth>[,] GetChannel <TSrcColor, TDepth>(this TSrcColor[,] image, Rectangle area, int channelIndex) where TSrcColor : struct, IColor <TDepth> where TDepth : struct { int width = area.Width; int height = area.Height; var dest = new Gray <TDepth> [area.Height, area.Width]; using (var lockedImage = image.Lock()) using (var dstImg = dest.Lock()) { var srcImg = lockedImage.GetSubRect(area); int channelSize = srcImg.ColorInfo.ChannelSize; int colorSize = srcImg.ColorInfo.Size; byte *srcPtr = (byte *)srcImg.ImageData + channelIndex * srcImg.ColorInfo.ChannelSize; byte *dstPtr = (byte *)dstImg.ImageData; for (int row = 0; row < height; row++) { byte *srcColPtr = srcPtr; byte *dstColPtr = dstPtr; for (int col = 0; col < width; col++) { /********** copy channel byte-per-byte ************/ for (int partIdx = 0; partIdx < channelSize; partIdx++) { dstColPtr[partIdx] = srcColPtr[partIdx]; } srcColPtr += colorSize; //move to the next column dstColPtr += channelSize; /********** copy channel byte-per-byte ************/ } srcPtr += srcImg.Stride; dstPtr += dstImg.Stride; } } return(dest); }