public static PixImage <T> ToPixImage <T>(this Matrix <C4f> matrix) { var pixImage = new PixImage <T>(Col.FormatDefaultOf(typeof(T), 4), (V2i)matrix.Size); pixImage.GetMatrix <C4f>().Set(matrix); return(pixImage); }
public static PixImage <T> ToPixImage <T, TMatrixData>(this Matrix <TMatrixData, C3us> matrix) { var pixImage = new PixImage <T>(Col.FormatDefaultOf(typeof(T), 3), (V2i)matrix.Size); pixImage.GetMatrix <C3us>().Set(matrix); return(pixImage); }
public static Matrix <ushort> ToGrayscaleMatrix(this PixImage <ushort> pixImage) { if (pixImage.ChannelCount == 1L && pixImage.Format == Col.Format.Gray) { return(pixImage.Volume.AsMatrixWindow()); } return(pixImage.GetMatrix <C3us>().MapWindow <ushort>(Col.GrayUShortFromC3us)); }
public static Matrix <float> ToClampedGrayscaleMatrix(this PixImage <float> pixImage) { if (pixImage.ChannelCount == 1L && pixImage.Format == Col.Format.Gray) { return(pixImage.Volume.AsMatrixWindow()); } return(pixImage.GetMatrix <C3f>().MapWindow <float>(Col.GrayFloatClampedFromC3f)); }
public static Matrix <byte> ToBlackAndWhiteMatrix(this PixImage <byte> pixImage, int threshold) { if (pixImage.ChannelCount != 1) { throw new ArgumentOutOfRangeException(nameof(pixImage.ChannelCount)); } return(pixImage.GetChannel(Col.Channel.Gray).MapWindow <byte>(b => (byte)(b < threshold ? 0 : 255))); }
public static PixImage <float> ToClampedGrayscalePixImage(this PixImage <float> pixImage) { if (pixImage.ChannelCount == 1L && pixImage.Format == Col.Format.Gray) { return(pixImage); } return(new PixImage <float>(pixImage.ToClampedGrayscaleMatrix())); }
/// <summary> /// Stitches the images provided to a new image with next best square size. /// {1, 2} {1, 2, 3} {1, 2, 3, 4, 5 } /// produces the image /// 1 1 2 1, 2, 3 /// 3 X 4, 5 /// The color format of all images must be equal and have byte format. /// </summary> /// <param name="images">The image array that should be stitched.</param> /// <returns>Stitched image</returns> public static PixImage StitchSquare(this PixImage[] images) { var squareSize = (int)Fun.Ceiling(images.Length.Sqrt()); var array = new PixImage[squareSize][].SetByIndex( row => new PixImage[squareSize].SetByIndex( col => { var ii = squareSize * row + col; return(ii < images.Length ? images[ii] : null); })); return(array.Stitch()); }
/// <summary> /// Returns 6 square PixImages of the supplied size containing the /// textures of a cube map in the following arrangement (right-handed): /// UV -> /// | | 4 | /// v | 0 | 1 | 2 | 3 | /// | 5 | /// XN YP XP YN ZP ZN /// </summary> public static PixImage <T> CreateCubeMapSide <T, Tcol>( int cubeSide, int size, int channelCount, Func <V3d, Tcol> vec_colFun) { var image = new PixImage <T>(size, size, channelCount); var mat = image.GetMatrix <Tcol>(); var d = 1.0 / size; V3d dx = d * s_dx[cubeSide], dy = d * s_dy[cubeSide], v0 = s_v0[cubeSide] + 0.5 * dx + 0.5 * dy; mat.SetByCoordParallelY((x, y) => { var v = v0 + x * dx + y * dy; return(vec_colFun(v.Normalized)); }); return(image); }
public static Matrix <byte> ToGrayscaleMatrix(this PixImage <byte> pixImage) { if (pixImage.ChannelCount == 1L) { if (pixImage.Format == Col.Format.BW || pixImage.Format == Col.Format.Gray) { return(pixImage.Volume.AsMatrixWindow()); } } return(pixImage.GetMatrix <C3b>().MapWindow <byte>(Col.GrayByteFromC3b)); }
public static PixImageMipMap ToPixImageMipMap(this IPixMipMap2d mipmap) { if (mipmap is PixImageMipMap pixImageMipMap) { return(pixImageMipMap); } var count = mipmap.LevelCount; var pixImageArray = new PixImage[count].SetByIndex(i => mipmap[i].ToPixImage()); return(new PixImageMipMap(pixImageArray)); }
public virtual Func <Tr> PixImage(PixImage pi) { return(() => { var typedOther = Other as PixImage; if (typedOther == null) { throw new ArgumentException(); } return ProductOp.PixImage(pi, typedOther); }); }
public static PixImage <T> ToPixImage <T>(this IMatrix <C4f> matrix) { if (matrix is Matrix <float, C4f> ) { return(((Matrix <float, C4f>)matrix).ToPixImage <T, float>()); } ; var pixImage = new PixImage <T>(Col.FormatDefaultOf(typeof(T), 4), (V2i)matrix.Dim); pixImage.GetMatrix <C4f>().Set(matrix); return(pixImage); }
internal static bool SaveAsImageDevil( this PixImage image, string file, PixFileFormat format, PixSaveOptions options, int qualityLevel) { Devil.ImageType imageType; if (!s_fileFormats.TryGetValue(format, out imageType)) { return(false); } return(image.SaveDevIL(() => IL.Save(imageType, file), qualityLevel)); }
public override Func <int> PixImage(PixImage pi) { return(() => { var typedOther = Other as FileImage; if (typedOther == null) { throw new ArgumentException(); } pi.SaveAsImage(PathPrefix + typedOther.Path); return 1; }); }
public static PixImage <T> CreateDome <T, Tcol>( int width, int height, int channelCount, Func <double, double, Tcol> phi_theta_colFun) { var image = new PixImage <T>(width, height, channelCount); var mat = image.GetMatrix <Tcol>(); double dx = Constant.PiTimesTwo / width, dy = Constant.PiHalf / height; double x0 = 0.5 * dx, y0 = 0.5 * dy; mat.SetByCoordParallelY((x, y) => phi_theta_colFun(x0 + x * dx, y0 + y * dy)); return(image); }
public static PixImage <T> ToPixImage <T>(this IMatrix <C3us> matrix) { if (matrix is Matrix <ushort, C3us> ) { return(((Matrix <ushort, C3us>)matrix).ToPixImage <T, ushort>()); } ; var pixImage = new PixImage <T>(Col.FormatDefaultOf(typeof(T), 3), (V2i)matrix.Dim); pixImage.GetMatrix <C3us>().Set(matrix); return(pixImage); }
private static bool SaveDevIL(this PixImage image, Func <bool> save, int qualityLevel) { //Devil.ImageType imageType; //if (!s_fileFormats.TryGetValue(format, out imageType)) // return false; var img = IL.GenImage(); IL.BindImage(img); ChannelType type; ChannelFormat fmt; if (!s_devilDataTypes.TryGetValue(image.PixFormat.Type, out type)) { return(false); } if (!s_devilColorFormats.TryGetValue(image.PixFormat.Format, out fmt)) { return(false); } var gc = GCHandle.Alloc(image.Data, GCHandleType.Pinned); try { if (!IL.TexImage(image.Size.X, image.Size.Y, 1, (byte)image.ChannelCount, fmt, type, gc.AddrOfPinnedObject())) { return(false); } IL.RegisterOrigin(OriginMode.UpperLeft); if (qualityLevel != -1) { IL.SetInteger(IntName.JpgQuality, qualityLevel); } return(save()); } catch (Exception) { return(false); } finally { gc.Free(); IL.BindImage(0); IL.DeleteImage(img); } }
/// <summary> /// Stitches the images provided in the 2D array into one large image according to the position in the array. /// The array is treated as column-major. Therefore, the array /// {{1, 2, 3}} /// {{4, 5, 6}} /// produces the image /// 1 2 3 /// 4 6 7 /// Null-items are allowed an result in black spot at the corresponding position. /// The color format of all images must be equal and have byte format. /// </summary> /// <param name="images">The 2D image array that should be stitched.</param> /// <returns>Stitched image</returns> public static PixImage <T> Stitch <T>(this PixImage <T>[][] images) { if (images.IsEmptyOrNull()) { return(null); } // Find largest extensions var numRows = images.Length; var numColumns = images.Max(img => img.Length); // Find largest X Y var sizesX = new int[numColumns].SetByIndex(c => images.Max(ic => (ic[c] == null) ? 0 : ic[c].Size.X)); var sizesY = images.Map(img => img.Max(img2 => (img2 == null) ? 0 : img2.Size.Y)); var width = sizesX.Sum(); var height = sizesY.Sum(); // The first image in the array, used to determine the color format var fst = images.First(img => img.Length > 0).First(img => img != null); if (fst == null) { Report.Line("Empty image array!"); return(null); } // Assure that the color format fits if (images.TrueForAny(imgs => imgs.Where(i => i != null).TrueForAny(img => img.Format != fst.Format))) { Report.Line("Color format not matching!"); return(null); } // Allocate new image var target = new PixImage <T>(fst.Format, new V2i(width, height)); for (int r = 0, y = 0; r < numRows; r++) { for (int c = 0, x = 0; c < Fun.Max(numColumns, images[r].Length); c++) { var source = images[r][c]; if (source != null) { target.Set(x, y, source); } x += sizesX[c]; } y += sizesY[r]; } return(target); }
public static PixImage ToPixImage(this IPixImage2d image) { if (image is PixImage pixImage) { return(pixImage); } var size = image.Size; var pixFormat = image.PixFormat; if (image.Data.GetType().GetElementType() != pixFormat.Type) { throw new ArgumentException("type mismatch in supplied IPixImage2d"); } return(PixImage.Create(image.Data, pixFormat.Format, size.X, size.Y)); }
/// <summary> /// Save image to stream via devil. /// </summary> /// <returns>True if the file was successfully saved.</returns> internal static bool SaveAsImageDevil( this PixImage image, Stream stream, PixFileFormat format, PixSaveOptions options, int qualityLevel) { Devil.ImageType imageType; if (!s_fileFormats.TryGetValue(format, out imageType)) { return(false); } lock (s_devilLock) { return(image.SaveDevIL(() => IL.SaveStream(imageType, stream), qualityLevel)); } }
protected static PixImage CreateRaw(FormatConvertedBitmap fcBitmap, PixFormat pixFormat, int channels) { var sx = fcBitmap.PixelWidth; var sy = fcBitmap.PixelHeight; if (pixFormat.Format == Col.Format.BW) { var bitImage = new PixImage <byte>(Col.Format.BW, 1 + (sx - 1) / 8, sy, channels); fcBitmap.CopyPixels(bitImage.Array, bitImage.IntStride, 0); var pixImage = new PixImage <byte>(Col.Format.BW, sx, sy, 1); ExpandPixels(bitImage, pixImage); return(pixImage); } else { var pixImage = Create(pixFormat, sx, sy, channels); fcBitmap.CopyPixels(pixImage.Array, pixImage.IntStride, 0); return(pixImage); } }
protected static PixImage CreateRawBitmap(System.Drawing.Bitmap bitmap) { var sdipf = GetLockFormat(bitmap.PixelFormat); var pfc = s_pixFormatAndCountOfPixelFormatBitmap[sdipf]; var sx = bitmap.Width; var sy = bitmap.Height; var ch = pfc.E1; var pixImage = Create(pfc.E0, sx, sy, ch); var array = pixImage.Array; if (pfc.E0.Format == Col.Format.BW) { var bitImage = new PixImage <byte>(Col.Format.BW, 1 + (sx - 1) / 8, sy, 1); System.Drawing.Imaging.BitmapData bdata = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, sx, sy), System.Drawing.Imaging.ImageLockMode.ReadOnly, sdipf); bdata.Scan0.CopyTo(bitImage.Volume.Data); bitmap.UnlockBits(bdata); ExpandPixels(bitImage, pixImage.ToPixImage <byte>()); } else { System.Drawing.Imaging.BitmapData bdata = bitmap.LockBits( new System.Drawing.Rectangle(0, 0, sx, sy), System.Drawing.Imaging.ImageLockMode.ReadOnly, sdipf); bdata.Scan0.CopyTo(array); bitmap.UnlockBits(bdata); } return(pixImage); }
/// <summary> /// In-place invert. No Copy is created. /// </summary> /// <param name="pixImage">Returns this pixImage.</param> /// <returns></returns> public static PixImage <uint> Invert(this PixImage <uint> pixImage) { pixImage.Volume.Apply(ui => (uint)(0xffffffffu - ui)); return(pixImage); }
public static Matrix <byte> ToBlackAndWhiteMatrix(this PixImage <byte> pixImage) { return(pixImage.ToBlackAndWhiteMatrix(128)); }
public static PixImage <byte> ToBlackAndWhitePixImage(this PixImage <byte> pixImage) { return(new PixImage <byte>(Col.Format.BW, pixImage.ToBlackAndWhiteMatrix())); }
public static PixImage <double> Inverted(this PixImage <double> pixImage) { return(new PixImage <double>(pixImage.Volume.MapToImageWindow(d => 1.0 - d))); }
/// <summary> /// In-place invert. No Copy is created. /// </summary> /// <param name="pixImage">Returns this pixImage.</param> /// <returns></returns> public static PixImage <byte> Invert(this PixImage <byte> pixImage) { pixImage.Volume.Apply(b => (byte)(255 - b)); return(pixImage); }
/// <summary> /// In-place invert. No Copy is created. /// </summary> /// <param name="pixImage">Returns this pixImage.</param> /// <returns></returns> public static PixImage <ushort> Invert(this PixImage <ushort> pixImage) { pixImage.Volume.Apply(us => (ushort)(65535 - us)); return(pixImage); }
/// <summary> /// In-place invert. No Copy is created. /// </summary> /// <param name="pixImage">Returns this pixImage.</param> /// <returns></returns> public static PixImage <float> Invert(this PixImage <float> pixImage) { pixImage.Volume.Apply(f => (float)(1.0f - f)); return(pixImage); }
/// <summary> /// In-place invert. No Copy is created. /// </summary> /// <param name="pixImage">Returns this pixImage.</param> /// <returns></returns> public static PixImage <double> Invert(this PixImage <double> pixImage) { pixImage.Volume.Apply(d => 1.0 - d); return(pixImage); }