Example #1
0
        /// <summary>
        /// Converts generic image into bitmap compatibility image by using provided preferred pixel formats.
        /// </summary>
        /// <param name="srcImg">Source image.</param>
        /// <param name="preferedDestinationFormats">Preferred destination pixel formats.</param>
        /// <param name="isJustCasted">True if the image is just casted. False if data must be converted.</param>
        /// <returns>Generic image which color corresponds to one of the <paramref name="preferedDestinationFormats"/>.</returns>
        public static IImage ToBitmapCompatibilityImage(IImage srcImg, PixelFormat[] preferedDestinationFormats, out bool isJustCasted)
        {
            var preferedDestinationColors = from preferedFormat in preferedDestinationFormats
                                            let preferedColor = preferedFormat.GetColorInfo()
                                                                where preferedColor != null
                                                                select preferedColor;

            var path = ColorConverter.GetPath(srcImg.ColorInfo, preferedDestinationColors.ToArray());

            if (path == null)
            {
                throw new Exception(String.Format("Image with color: {0} cannot be converted to System.Drawing.Bitmap", srcImg.ColorInfo.ColorType.Name));
            }

            isJustCasted = ColorConverter.CopiesData(path) == false;

            IImage convertedIm = ColorConverter.Convert(srcImg, path.ToArray(), false);

            return(convertedIm);
        }
        internal static Image <TColor, TDepth> Convolve <TColor, TDepth, TKernel>(this Image <TColor, TDepth> src, Image <Gray, TKernel>[] kernels, ConvolutionBorder options, bool forceSpatialConvolution = false)
            where TColor : IColor
            where TDepth : struct
            where TKernel : struct
        {
            bool useFFT = ShouldUseFFT(kernels) && !forceSpatialConvolution;

            Type[] supportedTypes = null;
            if (useFFT)
            {
                supportedTypes = ParallelFFTConvolution.SupportedTypes;
            }
            else
            {
                supportedTypes = ParallelSpatialConvolution.SupportedTypes;
            }

            supportedTypes = supportedTypes.Where(x => x.Equals(typeof(TKernel))).ToArray();
            if (supportedTypes.Length == 0)
            {
                throw new NotSupportedException(string.Format("Kernel of type {0} is not supported. Used convolution: {1}. Supported types for used convolution: {2}." +
                                                              "Please use different kernel type, or force convolution method.",
                                                              typeof(TKernel).Name,
                                                              (useFFT) ? "FFT" : "Spatial",
                                                              supportedTypes.Select(x => x.Name)));
            }

            /************************************** convert src ********************************/
            var    supportedColors = supportedTypes.Select(x => ColorInfo.GetInfo(typeof(TColor), x)).ToArray();
            var    conversionPath  = ColorConverter.GetPath(src.ColorInfo, supportedColors);
            IImage convertedSrc    = ColorConverter.Convert(src, conversionPath.ToArray(), false);

            if (convertedSrc == null)
            {
                throw new Exception(string.Format("Convolution does not support images of type {0}", src.ColorInfo.ChannelType));
            }
            /************************************** convert src ********************************/

            IImage dest = null;

            if (useFFT)
            {
                dest = ParallelFFTConvolution.Convolve <TColor, TKernel>(convertedSrc as Image <TColor, TKernel>, kernels, options);
            }
            else
            {
                dest = ParallelSpatialConvolution.Convolve(convertedSrc, kernels, options);
            }


            /************************************** convert back ********************************/
            var    backwardConversion = ColorConverter.GetPath(dest.ColorInfo, src.ColorInfo);
            IImage convertedDest      = ColorConverter.Convert(dest, backwardConversion.ToArray(), false);

            if (convertedDest == null)
            {
                throw new Exception(string.Format("Convolution does not support images of type {0}", src.ColorInfo.ChannelType));
            }
            /************************************** convert back ********************************/

            return(convertedDest as Image <TColor, TDepth>);
        }