/// <summary> /// Creates a new ColorProfileViewModel from a given file, but only /// if the color profile's color space is known by the view model. /// Note that the lcms2.dll must have been loaded prior to calling /// this method! /// </summary> /// <param name="fileName">Full file name of the ICS color profile.</param> /// <returns>Instance of ColorProfileViewModel, or null if the color /// profile has an unknown color space.</returns> public static ColorProfileViewModel CreateFromFile(string fileName) { cmsHProfile h = cmsOpenProfileFromFile(fileName, "r"); ColorProfileViewModel cpvm = null; if (h != IntPtr.Zero) { ColorSpaceSignature c; try { // Only create a view model if we can handle // the current profile's color space. // Attempt to convert the CMS color space signature // to an XL Toolbox ColorSpaceSignature enum value. // If this fails, cpvm will remain null. c = cmsGetColorSpace(h); ColorSpace colorSpace = c.ToColorSpace(); cpvm = new ColorProfileViewModel(fileName); cpvm.ColorSpace = colorSpace; } catch (NotImplementedException) { // NO-OP } finally { cmsCloseProfile(h); } } return(cpvm); }
/// <summary> /// Transforms the pixels of a FreeImageBitmap using a standard color profile /// for the bitmap's color space and the current color profile. /// </summary> /// <param name="freeImageBitmap">FreeImageBitmap whose pixels to convert.</param> /// <remarks> /// Since Excel does not support color management at all, conversions are done /// using standard color profiles that LittleCMS provides. /// </remarks> public void TransformFromStandardProfile(FreeImageAPI.FreeImageBitmap freeImageBitmap) { cmsHProfile standardProfile = CreateStandardProfile(freeImageBitmap); if (standardProfile == cmsHProfile.Zero) { throw new InvalidOperationException( "Unable to create standard profile for " + freeImageBitmap.ColorType.ToString()); } cmsHProfile targetProfile = cmsOpenProfileFromFile(GetPathFromName(), "r"); if (targetProfile == cmsHProfile.Zero) { throw new InvalidOperationException( "Unable to open desired color profile: " + Name); } // Create transform with perceptual intent (0) and no special options (0) cmsHTransform t = cmsCreateTransform( standardProfile, Lcms.Formatters.TYPE_BGRA_8, targetProfile, GetLcmsPixelFormat(ColorSpace), 0, 0); if (t == cmsHTransform.Zero) { throw new InvalidOperationException("Unable to create CMS transform."); } UInt32 numPixels = (UInt32)freeImageBitmap.Size.Width * (UInt32)freeImageBitmap.Size.Height; cmsDoTransform(t, freeImageBitmap.Bits, freeImageBitmap.Bits, numPixels); freeImageBitmap.CreateICCProfile(GetIccBytes()); cmsDeleteTransform(t); cmsCloseProfile(standardProfile); cmsCloseProfile(targetProfile); }
private static extern cmsHTransform cmsCreateTransform( cmsHProfile Input, cmsUInt32Number InputFormat, cmsHProfile Output, cmsUInt32Number OutputFormat, cmsUInt32Number Intent, cmsUInt32Number dwFlags);
private static extern ColorSpaceSignature cmsGetColorSpace(cmsHProfile hProfile);
private static extern cmsBool cmsCloseProfile(cmsHProfile hProfile);