static void writeColorConversionPath(ColorInfo c1, ColorInfo c2) { var path = ColorDepthConverter.GetPath(c1, c2); //var path = ColorDepthConverter.GetMostInexepnsiveConversionPath(c1, c2); Console.Write("{0} => {1}: ", c1, c2); Console.CursorLeft = 40; //Console.Write("Cast: {0}", !ColorDepthConverter.ConversionPathCopiesData(path)); Console.CursorLeft = 55; if (path.Count == 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("NO PATH"); Console.ResetColor(); return; } else if (path.CopiesData() == false) { Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("cast"); Console.ResetColor(); return; } Console.ForegroundColor = ConsoleColor.Green; foreach (var stage in path) { Console.Write("=> ({0} => {1})", stage.Source, stage.Destination); } Console.ResetColor(); Console.WriteLine(); }
/// <summary> /// Returns if an image can be casted to UnmanagedImage without data copy. /// </summary> /// <param name="image">Generic image</param> /// <returns>Is image cast-able to UnmanagedImage.</returns> public static bool CanCastToAForgeImage(this IImage image) { ColorInfo[] preferedColors = BitmapConversionExtensions.PixelFormatMappings.Forward.ToArray(); var conversionPath = ColorDepthConverter.GetPath(image.ColorInfo, preferedColors); bool isImageCopied = ColorDepthConverter.CopiesData(conversionPath); return(!isImageCopied); }
/// <summary> /// Converts the image from source to destination color and depth. /// Data may be shared if casting is used. To prevent that set <paramref name="copyAlways"/> to true. /// </summary> /// <param name="image">Image.</param> /// <param name="destColor">Destination color info.</param> /// <param name="copyAlways">Forces data copy even if a casting is enough.</param> /// <param name="failIfCannotCast">If data copy is needed throws an exception.</param> /// <returns>Converted image.</returns> public static IImage Convert(this IImage image, ColorInfo destColor, bool copyAlways = false, bool failIfCannotCast = false) { if (image == null) { return(null); } var conversionPath = ColorDepthConverter.GetPath(image.ColorInfo, destColor); if (conversionPath == null /*it should never be null*/ || conversionPath.Count == 0) { throw new Exception(String.Format("Image does not support conversion from {0} to {1}", image.ColorInfo.ColorType, destColor.ColorType)); } if (failIfCannotCast && conversionPath.CopiesData() == true) { throw new Exception("Fail if cannot cast is set to true: Image data must be copied"); } var convertedIm = ColorDepthConverter.Convert(image, conversionPath.ToArray(), copyAlways); return(convertedIm); }