Advertisement
Guest User

C# convert RGB value to CMYK using an ICC profile

a guest
Mar 24th, 2012
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. float[] colorValues = new float[4];
  2. colorValues[0] = c / 255f;
  3. colorValues[1] = m / 255f;
  4. colorValues[2] = y / 255f;
  5. colorValues[3] = k / 255f;
  6.  
  7. System.Windows.Media.Color color = Color.FromValues(colorValues,
  8. new Uri(@"C:UsersmeDocumentsISOcoated_v2_300_eci.icc"));
  9. System.Drawing.Color rgbColor = System.Drawing.Color.FromArgb(color.R, color.G, color.B);
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Runtime.InteropServices;
  16.  
  17. namespace ICM
  18. {
  19. public class WindowsColorSystem
  20. {
  21. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  22. public class ProfileFilename
  23. {
  24. public uint type;
  25. [MarshalAs(UnmanagedType.LPTStr)]
  26. public string profileData;
  27. public uint dataSize;
  28.  
  29. public ProfileFilename(string filename)
  30. {
  31. type = ProfileFilenameType;
  32. profileData = filename;
  33. dataSize = (uint)filename.Length * 2 + 2;
  34. }
  35. };
  36.  
  37. public const uint ProfileFilenameType = 1;
  38. public const uint ProfileMembufferType = 2;
  39.  
  40. public const uint ProfileRead = 1;
  41. public const uint ProfileReadWrite = 2;
  42.  
  43.  
  44. public enum FileShare : uint
  45. {
  46. Read = 1,
  47. Write = 2,
  48. Delete = 4
  49. };
  50.  
  51. public enum CreateDisposition : uint
  52. {
  53. CreateNew = 1,
  54. CreateAlways = 2,
  55. OpenExisting = 3,
  56. OpenAlways = 4,
  57. TruncateExisting = 5
  58. };
  59.  
  60. public enum LogicalColorSpace : uint
  61. {
  62. CalibratedRGB = 0x00000000,
  63. sRGB = 0x73524742,
  64. WindowsColorSpace = 0x57696E20
  65. };
  66.  
  67. public enum ColorTransformMode : uint
  68. {
  69. ProofMode = 0x00000001,
  70. NormalMode = 0x00000002,
  71. BestMode = 0x00000003,
  72. EnableGamutChecking = 0x00010000,
  73. UseRelativeColorimetric = 0x00020000,
  74. FastTranslate = 0x00040000,
  75. PreserveBlack = 0x00100000,
  76. WCSAlways = 0x00200000
  77. };
  78.  
  79.  
  80. enum ColorType : int
  81. {
  82. Gray = 1,
  83. RGB = 2,
  84. XYZ = 3,
  85. Yxy = 4,
  86. Lab = 5,
  87. _3_Channel = 6,
  88. CMYK = 7,
  89. _5_Channel = 8,
  90. _6_Channel = 9,
  91. _7_Channel = 10,
  92. _8_Channel = 11,
  93. Named = 12
  94. };
  95.  
  96.  
  97. public const uint IntentPerceptual = 0;
  98. public const uint IntentRelativeColorimetric = 1;
  99. public const uint IntentSaturation = 2;
  100. public const uint IntentAbsoluteColorimetric = 3;
  101.  
  102. public const uint IndexDontCare = 0;
  103.  
  104.  
  105. [StructLayout(LayoutKind.Sequential)]
  106. public struct RGBColor
  107. {
  108. public ushort red;
  109. public ushort green;
  110. public ushort blue;
  111. public ushort pad;
  112. };
  113.  
  114. [StructLayout(LayoutKind.Sequential)]
  115. public struct CMYKColor
  116. {
  117. public ushort cyan;
  118. public ushort magenta;
  119. public ushort yellow;
  120. public ushort black;
  121. };
  122.  
  123. [DllImport("mscms.dll", SetLastError = true, EntryPoint = "OpenColorProfileW", CallingConvention = CallingConvention.Winapi)]
  124. static extern IntPtr OpenColorProfile(
  125. [MarshalAs(UnmanagedType.LPStruct)] ProfileFilename profile,
  126. uint desiredAccess,
  127. FileShare shareMode,
  128. CreateDisposition creationMode);
  129.  
  130. [DllImport("mscms.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  131. static extern bool CloseColorProfile(IntPtr hProfile);
  132.  
  133. [DllImport("mscms.dll", SetLastError = true, EntryPoint = "GetStandardColorSpaceProfileW", CallingConvention = CallingConvention.Winapi)]
  134. static extern bool GetStandardColorSpaceProfile(
  135. uint machineName,
  136. LogicalColorSpace profileID,
  137. [MarshalAs(UnmanagedType.LPTStr), In, Out] StringBuilder profileName,
  138. ref uint size);
  139.  
  140. [DllImport("mscms.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  141. static extern IntPtr CreateMultiProfileTransform(
  142. [In] IntPtr[] profiles,
  143. uint nProfiles,
  144. [In] uint[] intents,
  145. uint nIntents,
  146. ColorTransformMode flags,
  147. uint indexPreferredCMM);
  148.  
  149. [DllImport("mscms.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  150. static extern bool DeleteColorTransform(IntPtr hTransform);
  151.  
  152. [DllImport("mscms.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
  153. static extern bool TranslateColors(
  154. IntPtr hColorTransform,
  155. [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2), In] RGBColor[] inputColors,
  156. uint nColors,
  157. ColorType ctInput,
  158. [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2), Out] CMYKColor[] outputColors,
  159. ColorType ctOutput);
  160.  
  161.  
  162.  
  163. public static void Test()
  164. {
  165. bool success;
  166.  
  167. StringBuilder profileName = new StringBuilder(256);
  168. uint size = (uint)profileName.Capacity * 2;
  169. success = GetStandardColorSpaceProfile(0, LogicalColorSpace.sRGB, profileName, ref size);
  170.  
  171. ProfileFilename sRGBFilename = new ProfileFilename(profileName.ToString());
  172. IntPtr hSRGBProfile = OpenColorProfile(sRGBFilename, ProfileRead, FileShare.Read, CreateDisposition.OpenExisting);
  173.  
  174. ProfileFilename isoCoatedFilename = new ProfileFilename(@"C:UsersmeDocumentsISOcoated_v2_300_eci.icc");
  175. IntPtr hIsoCoatedProfile = OpenColorProfile(isoCoatedFilename, ProfileRead, FileShare.Read, CreateDisposition.OpenExisting);
  176.  
  177. IntPtr[] profiles = new IntPtr[] { hSRGBProfile, hIsoCoatedProfile };
  178. uint[] intents = new uint[] { IntentPerceptual };
  179. IntPtr transform = CreateMultiProfileTransform(profiles, 2, intents, 1, ColorTransformMode.BestMode, IndexDontCare);
  180.  
  181. RGBColor[] rgbColors = new RGBColor[1];
  182. rgbColors[0] = new RGBColor();
  183. CMYKColor[] cmykColors = new CMYKColor[1];
  184. cmykColors[0] = new CMYKColor();
  185.  
  186. rgbColors[0].red = 30204;
  187. rgbColors[0].green = 4420;
  188. rgbColors[0].blue = 60300;
  189.  
  190. success = TranslateColors(transform, rgbColors, 1, ColorType.RGB, cmykColors, ColorType.CMYK);
  191.  
  192. success = DeleteColorTransform(transform);
  193.  
  194. success = CloseColorProfile(hSRGBProfile);
  195. success = CloseColorProfile(hIsoCoatedProfile);
  196. }
  197. }
  198. }
  199.  
  200. using System;
  201.  
  202. namespace CMYKConversion
  203. {
  204. class Program
  205. {
  206. static void Main(string[] args)
  207. {
  208. Converter c = new Converter();
  209. c.Convert();
  210.  
  211. Console.ReadKey();
  212. }
  213. }
  214. }
  215.  
  216. using System;
  217. using System.IO;
  218. using System.Reflection;
  219. using System.Windows.Media;
  220. using System.Windows.Media.Imaging;
  221.  
  222. namespace CMYKConversion
  223. {
  224. public class Converter
  225. {
  226. public void Convert()
  227. {
  228. var rgbJpeg = BitmapFrame.Create(GetStreamFromResource("CMYKConversion.Images.Desert.jpg"));
  229. var iccCmykJpeg = new ColorConvertedBitmap(
  230. rgbJpeg,
  231. new ColorContext(PixelFormats.Default),
  232. new ColorContext(GetProfilePath("Profiles/1010_ISO_Coated_39L.icc")),
  233. PixelFormats.Cmyk32
  234. );
  235. var jpegBitmapEncoder = new JpegBitmapEncoder();
  236. jpegBitmapEncoder.Frames.Add(BitmapFrame.Create(iccCmykJpeg));
  237. var iccCmykJpegStream = new MemoryStream();
  238. jpegBitmapEncoder.Save(iccCmykJpegStream);
  239.  
  240. iccCmykJpegStream.Flush();
  241. SaveMemoryStream(iccCmykJpegStream, "C:\desertCMYK.jpg");
  242. iccCmykJpegStream.Close();
  243. }
  244.  
  245. private Stream GetStreamFromResource(string name)
  246. {
  247. return typeof(Program).Assembly.GetManifestResourceStream(name);
  248. }
  249.  
  250. private Uri GetProfilePath(string name)
  251. {
  252. string folder = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Program)).CodeBase);
  253. return new Uri(Path.Combine(folder, name));
  254. }
  255.  
  256. private void SaveMemoryStream(MemoryStream ms, string fileName)
  257. {
  258. FileStream outStream = File.OpenWrite(fileName);
  259. ms.WriteTo(outStream);
  260. outStream.Flush();
  261. outStream.Close();
  262. }
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement