예제 #1
0
        /// <summary>
        /// Returns a copy of the image frame in the given pixel format.
        /// </summary>
        /// <typeparam name="TPixel2">The pixel format.</typeparam>
        /// <returns>The <see cref="ImageFrame{TPixel2}"/></returns>
        internal ImageFrame <TPixel2> CloneAs <TPixel2>()
            where TPixel2 : struct, IPixel <TPixel2>
        {
            if (typeof(TPixel2) == typeof(TPixel))
            {
                return(this.Clone() as ImageFrame <TPixel2>);
            }

            Func <Vector4, Vector4> scaleFunc = PackedPixelConverterHelper.ComputeScaleFunction <TPixel, TPixel2>();

            var target = new ImageFrame <TPixel2>(this.MemoryManager, this.Width, this.Height, this.MetaData.Clone());

            using (PixelAccessor <TPixel> pixels = this.Lock())
                using (PixelAccessor <TPixel2> targetPixels = target.Lock())
                {
                    Parallel.For(
                        0,
                        target.Height,
                        Configuration.Default.ParallelOptions,
                        y =>
                    {
                        for (int x = 0; x < target.Width; x++)
                        {
                            var color = default(TPixel2);
                            color.PackFromVector4(scaleFunc(pixels[x, y].ToVector4()));
                            targetPixels[x, y] = color;
                        }
                    });
                }

            return(target);
        }