/// <summary> /// Returns a copy of the image in the given pixel format. /// </summary> /// <param name="scaleFunc">A function that allows for the correction of vector scaling between unknown color formats.</param> /// <typeparam name="TColor2">The pixel format.</typeparam> /// <returns>The <see cref="Image{TColor2}"/></returns> public Image <TColor2> To <TColor2>(Func <Vector4, Vector4> scaleFunc = null) where TColor2 : struct, IPixel <TColor2> { scaleFunc = PackedPixelConverterHelper.ComputeScaleFunction <TColor, TColor2>(scaleFunc); Image <TColor2> target = new Image <TColor2>(this.Width, this.Height, this.Configuration); target.CopyProperties(this); using (PixelAccessor <TColor> pixels = this.Lock()) using (PixelAccessor <TColor2> targetPixels = target.Lock()) { Parallel.For( 0, target.Height, this.Configuration.ParallelOptions, y => { for (int x = 0; x < target.Width; x++) { TColor2 color = default(TColor2); color.PackFromVector4(scaleFunc(pixels[x, y].ToVector4())); targetPixels[x, y] = color; } }); } for (int i = 0; i < this.Frames.Count; i++) { target.Frames.Add(this.Frames[i].To <TColor2>()); } return(target); }
/// <summary> /// Performs the given action on the source image. /// </summary> /// <typeparam name="TColor">The pixel format.</typeparam> /// <typeparam name="TPacked">The packed format. <example>long, float.</example></typeparam> /// <param name="source">The image to perform the action against.</param> /// <param name="clone">Whether to clone the image.</param> /// <param name="action">The <see cref="Action"/> to perform against the image.</param> /// <returns>The <see cref="Image{TColor, TPacked}"/>.</returns> private static Image <TColor, TPacked> PerformAction <TColor, TPacked>(Image <TColor, TPacked> source, bool clone, Action <ImageBase <TColor, TPacked>, ImageBase <TColor, TPacked> > action) where TColor : struct, IPackedPixel <TPacked> where TPacked : struct { Image <TColor, TPacked> transformedImage = clone ? new Image <TColor, TPacked>(source) : new Image <TColor, TPacked>(); // Several properties still require copying if (!clone) { transformedImage.CopyProperties(source); } action(source, transformedImage); for (int i = 0; i < source.Frames.Count; i++) { ImageFrame <TColor, TPacked> sourceFrame = source.Frames[i]; ImageFrame <TColor, TPacked> tranformedFrame = clone ? new ImageFrame <TColor, TPacked>(sourceFrame) : new ImageFrame <TColor, TPacked> { FrameDelay = sourceFrame.FrameDelay }; action(sourceFrame, tranformedFrame); if (!clone) { transformedImage.Frames.Add(tranformedFrame); } else { transformedImage.Frames[i] = tranformedFrame; } } source = transformedImage; return(source); }