c# printing colors rgb cmyk

¿C#convierte el valor RGB a CMYK usando un perfil ICC?



printing colors (4)

Esta pregunta parece publicada en muchos lugares a través de las redes y SO, pero no pude encontrar una respuesta satisfactoria :(

¿Cómo puedo convertir un valor RGB a un valor CMYK utilizando un perfil ICC?

La respuesta más cercana que tengo está ahí, donde se explica cómo convertir de CMYK a RGB pero no al revés, que es lo que necesito. (http://stackoverflow.com/questions/4920482/cmyk-to-rgb-formula-of-photoshop/5076731#5076731)

float[] colorValues = new float[4]; colorValues[0] = c / 255f; colorValues[1] = m / 255f; colorValues[2] = y / 255f; colorValues[3] = k / 255f; System.Windows.Media.Color color = Color.FromValues(colorValues, new Uri(@"C:/Users/me/Documents/ISOcoated_v2_300_eci.icc")); System.Drawing.Color rgbColor = System.Drawing.Color.FromArgb(color.R, color.G, color.B);

Supongo que debería usar algunas clases / estructuras / métodos del espacio de nombres System.Windows.Media.

La estructura System.Windows.Media.Color contiene un método FromRgb, pero no puedo obtener los valores CMYK después de ese System.Windows.Media.Color!

Muchas gracias


Ninguna de las respuestas aquí parece abordar satisfactoriamente la necesidad de usar un perfil ICC.

Encontré una página de problemas de MS Connect que tiene algún código de ejemplo que utiliza los componentes de imágenes de Windows para convertir un RBG JPEG a CMYK utilizando un perfil ICC.

Si tiene su archivo ICC y un archivo JPEG de muestra, puede configurar una aplicación de consola para usar este código muy rápidamente.

He guardado el perfil ICC en una carpeta llamada "Perfiles" y he establecido el valor "Copiar en el directorio de salida" en "Siempre".

El JPEG se guarda en una carpeta llamada "Imágenes", y he establecido su valor de "Acción de compilación" en "Recurso incrustado".

El proyecto de la aplicación de consola necesita referencias a los siguientes módulos:

  • PresentaciónCore
  • System.Xaml
  • WindowsBase

La aplicación de la consola en su totalidad (llamada CMYKConversion):

Program.cs:

using System; namespace CMYKConversion { class Program { static void Main(string[] args) { Converter c = new Converter(); c.Convert(); Console.ReadKey(); } } }

Converter.cs:

using System; using System.IO; using System.Reflection; using System.Windows.Media; using System.Windows.Media.Imaging; namespace CMYKConversion { public class Converter { public void Convert() { var rgbJpeg = BitmapFrame.Create(GetStreamFromResource("CMYKConversion.Images.Desert.jpg")); var iccCmykJpeg = new ColorConvertedBitmap( rgbJpeg, new ColorContext(PixelFormats.Default), new ColorContext(GetProfilePath("Profiles/1010_ISO_Coated_39L.icc")), PixelFormats.Cmyk32 ); var jpegBitmapEncoder = new JpegBitmapEncoder(); jpegBitmapEncoder.Frames.Add(BitmapFrame.Create(iccCmykJpeg)); var iccCmykJpegStream = new MemoryStream(); jpegBitmapEncoder.Save(iccCmykJpegStream); iccCmykJpegStream.Flush(); SaveMemoryStream(iccCmykJpegStream, "C://desertCMYK.jpg"); iccCmykJpegStream.Close(); } private Stream GetStreamFromResource(string name) { return typeof(Program).Assembly.GetManifestResourceStream(name); } private Uri GetProfilePath(string name) { string folder = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program)).CodeBase); return new Uri(Path.Combine(folder, name)); } private void SaveMemoryStream(MemoryStream ms, string fileName) { FileStream outStream = File.OpenWrite(fileName); ms.WriteTo(outStream); outStream.Flush(); outStream.Close(); } } }


No conozco ninguna API o biblioteca de C # que pueda lograr esto. Sin embargo, si tiene suficiente conocimiento de C / C ++ para crear un contenedor para C #, veo dos opciones:

El espacio de nombres System.Windows.Media es muy limitado. Probablemente haya un motor potente (WCS?) Detrás de él, pero solo una pequeña parte está disponible.

Actualizar:

Aquí hay un código C # para hacer la conversión usando WCS. Ciertamente podría usar un envoltorio que lo haría más fácil de usar:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace ICM { public class WindowsColorSystem { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public class ProfileFilename { public uint type; [MarshalAs(UnmanagedType.LPTStr)] public string profileData; public uint dataSize; public ProfileFilename(string filename) { type = ProfileFilenameType; profileData = filename; dataSize = (uint)filename.Length * 2 + 2; } }; public const uint ProfileFilenameType = 1; public const uint ProfileMembufferType = 2; public const uint ProfileRead = 1; public const uint ProfileReadWrite = 2; public enum FileShare : uint { Read = 1, Write = 2, Delete = 4 }; public enum CreateDisposition : uint { CreateNew = 1, CreateAlways = 2, OpenExisting = 3, OpenAlways = 4, TruncateExisting = 5 }; public enum LogicalColorSpace : uint { CalibratedRGB = 0x00000000, sRGB = 0x73524742, WindowsColorSpace = 0x57696E20 }; public enum ColorTransformMode : uint { ProofMode = 0x00000001, NormalMode = 0x00000002, BestMode = 0x00000003, EnableGamutChecking = 0x00010000, UseRelativeColorimetric = 0x00020000, FastTranslate = 0x00040000, PreserveBlack = 0x00100000, WCSAlways = 0x00200000 }; enum ColorType : int { Gray = 1, RGB = 2, XYZ = 3, Yxy = 4, Lab = 5, _3_Channel = 6, CMYK = 7, _5_Channel = 8, _6_Channel = 9, _7_Channel = 10, _8_Channel = 11, Named = 12 }; public const uint IntentPerceptual = 0; public const uint IntentRelativeColorimetric = 1; public const uint IntentSaturation = 2; public const uint IntentAbsoluteColorimetric = 3; public const uint IndexDontCare = 0; [StructLayout(LayoutKind.Sequential)] public struct RGBColor { public ushort red; public ushort green; public ushort blue; public ushort pad; }; [StructLayout(LayoutKind.Sequential)] public struct CMYKColor { public ushort cyan; public ushort magenta; public ushort yellow; public ushort black; }; [DllImport("mscms.dll", SetLastError = true, EntryPoint = "OpenColorProfileW", CallingConvention = CallingConvention.Winapi)] static extern IntPtr OpenColorProfile( [MarshalAs(UnmanagedType.LPStruct)] ProfileFilename profile, uint desiredAccess, FileShare shareMode, CreateDisposition creationMode); [DllImport("mscms.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] static extern bool CloseColorProfile(IntPtr hProfile); [DllImport("mscms.dll", SetLastError = true, EntryPoint = "GetStandardColorSpaceProfileW", CallingConvention = CallingConvention.Winapi)] static extern bool GetStandardColorSpaceProfile( uint machineName, LogicalColorSpace profileID, [MarshalAs(UnmanagedType.LPTStr), In, Out] StringBuilder profileName, ref uint size); [DllImport("mscms.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] static extern IntPtr CreateMultiProfileTransform( [In] IntPtr[] profiles, uint nProfiles, [In] uint[] intents, uint nIntents, ColorTransformMode flags, uint indexPreferredCMM); [DllImport("mscms.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] static extern bool DeleteColorTransform(IntPtr hTransform); [DllImport("mscms.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] static extern bool TranslateColors( IntPtr hColorTransform, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2), In] RGBColor[] inputColors, uint nColors, ColorType ctInput, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2), Out] CMYKColor[] outputColors, ColorType ctOutput); public static void Test() { bool success; StringBuilder profileName = new StringBuilder(256); uint size = (uint)profileName.Capacity * 2; success = GetStandardColorSpaceProfile(0, LogicalColorSpace.sRGB, profileName, ref size); ProfileFilename sRGBFilename = new ProfileFilename(profileName.ToString()); IntPtr hSRGBProfile = OpenColorProfile(sRGBFilename, ProfileRead, FileShare.Read, CreateDisposition.OpenExisting); ProfileFilename isoCoatedFilename = new ProfileFilename(@"C:/Users/me/Documents/ISOcoated_v2_300_eci.icc"); IntPtr hIsoCoatedProfile = OpenColorProfile(isoCoatedFilename, ProfileRead, FileShare.Read, CreateDisposition.OpenExisting); IntPtr[] profiles = new IntPtr[] { hSRGBProfile, hIsoCoatedProfile }; uint[] intents = new uint[] { IntentPerceptual }; IntPtr transform = CreateMultiProfileTransform(profiles, 2, intents, 1, ColorTransformMode.BestMode, IndexDontCare); RGBColor[] rgbColors = new RGBColor[1]; rgbColors[0] = new RGBColor(); CMYKColor[] cmykColors = new CMYKColor[1]; cmykColors[0] = new CMYKColor(); rgbColors[0].red = 30204; rgbColors[0].green = 4420; rgbColors[0].blue = 60300; success = TranslateColors(transform, rgbColors, 1, ColorType.RGB, cmykColors, ColorType.CMYK); success = DeleteColorTransform(transform); success = CloseColorProfile(hSRGBProfile); success = CloseColorProfile(hIsoCoatedProfile); } } }


Podrías echarle un vistazo a esto: ¿ Convertir el color RGB a CMYK?

Aunque esta conversión es bastante subjetiva, de ahí la necesidad del perfil ICC, ¿es posible que pueda extraer ese "factor" del ICC y ajustar la fórmula?

¿Cuál es el contexto que necesita para convertir los valores RGB a CMYK?


Según un MVP, GDI + puede leer CMYK pero no puede codificarlo (Fuente: http://www.pcreview.co.uk/forums/convert-rgb-image-cmyk-t1419911.html ). Continúan diciendo que usar TIF como un formato de intermediación puede ser el camino a seguir.

Aparte de eso, puede probar Graphics Millennium Edition SDK para .NET en http://imaging.aurigma.com/ (No estoy afiliado a esta empresa).

Sé que esto no es una gran respuesta, pero espero que arroje algo de luz y te apunte en la dirección correcta.