VintaSoft Imaging .NET SDK 15.0: Documentation for .NET developer
In This Topic
    TIFF: How to convert TIFF file with ICC-profile to a new TIFF file with new ICC-profile?
    In This Topic
    Here is C# code that shows how to convert TIFF file with ICC-profile to a new TIFF file with new ICC-profile:
    /// <summary>
    /// Saves the TIFF file with specified ICC profile.
    /// </summary>
    /// <param name="sourceFilePath">The path to a source TIFF file.</param>
    /// <param name="iccProfilePath">The path to an ICC profile that should be applied to the source TIFF file.</param>
    /// <param name="outputFilePath">The path to an output TIFF file.</param>
    private static void Save(string sourceFilePath, string iccProfilePath, string outputFilePath)
    {
        // open TIFF file
        using (Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffFile tiffFile =
            new Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffFile(sourceFilePath))
        {
            // create decoding settings
            Vintasoft.Imaging.Codecs.Decoders.DecodingSettings decodingSettings =
                new Vintasoft.Imaging.Codecs.Decoders.DecodingSettings();
            // enable color management
            decodingSettings.ColorManagement = new Vintasoft.Imaging.ColorManagement.ColorManagementDecodeSettings();
            // use embedded ICC profile
            decodingSettings.ColorManagement.UseEmbeddedInputProfile = true;
            // get image of TIFF page
            using (Vintasoft.Imaging.VintasoftImage image = tiffFile.Pages[0].GetImage(decodingSettings, null))
            {
                // load ICC profile data
                byte[] iccProfileData = System.IO.File.ReadAllBytes(iccProfilePath);
    
                // create ICC profile data
                using (Vintasoft.Imaging.ColorManagement.Icc.IccProfile profile =
                    new Vintasoft.Imaging.ColorManagement.Icc.IccProfile(iccProfileData))
                {
                    // create color transform command
                    Vintasoft.Imaging.ImageProcessing.Color.ColorTransformCommand command = CreateCommand(image, profile);
                    // transform image
                    command.ExecuteInPlace(image);
                }
    
                // create TIFF file
                using (Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffFile newTiffFile = CreateTiffFile(image, iccProfileData))
                    // save TIFF file
                    newTiffFile.Save(outputFilePath);
            }
        }
    }
    
    /// <summary>
    /// Creates <see cref="Vintasoft.Imaging.ImageProcessing.Color.ColorTransformCommand"/>.
    /// </summary>
    /// <param name="sourceImage">The source image.</param>
    /// <param name="iccProfile">The ICC profile.</param>
    /// <returns>
    /// The <see cref="Vintasoft.Imaging.ImageProcessing.Color.ColorTransformCommand"/>.
    /// </returns>
    private static Vintasoft.Imaging.ImageProcessing.Color.ColorTransformCommand CreateCommand(
        Vintasoft.Imaging.VintasoftImage sourceImage, Vintasoft.Imaging.ColorManagement.Icc.IccProfile iccProfile)
    {
        // create command
        Vintasoft.Imaging.ImageProcessing.Color.ColorTransformCommand command =
            new Vintasoft.Imaging.ImageProcessing.Color.ColorTransformCommand();
    
        // create decode settings
        Vintasoft.Imaging.ColorManagement.ColorManagementDecodeSettings decodeSettings =
            new Vintasoft.Imaging.ColorManagement.ColorManagementDecodeSettings();
    
        // set the ICC profile
        switch (iccProfile.DeviceColorSpace)
        {
            case Vintasoft.Imaging.ColorSpaceType.CMYK:
                decodeSettings.OutputCmykProfile = iccProfile;
                break;
    
            case Vintasoft.Imaging.ColorSpaceType.sRGB:
                decodeSettings.OutputRgbProfile = iccProfile;
                break;
    
            case Vintasoft.Imaging.ColorSpaceType.Gray:
                decodeSettings.OutputGrayscaleProfile = iccProfile;
                break;
    
            default:
                throw new System.NotImplementedException();
        }
    
        // set the color transform
        command.ColorTransform = decodeSettings.ColorSpaceTransforms.GetColorTransform(
            sourceImage.ColorSpaceFormat, sourceImage.ColorSpaceFormat);
    
        return command;
    }
    
    /// <summary>
    /// Creates the TIFF file.
    /// </summary>
    /// <param name="image">The image.</param>
    /// <param name="iccProfileData">The ICC profile data.</param>
    /// <returns>
    /// The TIFF file.
    /// </returns>
    private static Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffFile CreateTiffFile(
        Vintasoft.Imaging.VintasoftImage image, byte[] iccProfileData)
    {
        // create TIFF file
        Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffFile tiffFile =
            new Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffFile();
        // add image
        tiffFile.Pages.Add(image);
    
        // get IFD tags
        Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffTagCollection ifdTags =
            tiffFile.Pages[tiffFile.Pages.Count - 1].IFD.Tags;
    
        // add ICC profile tag
        ifdTags.Add(Vintasoft.Imaging.Codecs.ImageFiles.Tiff.TiffTagId.ICCProfile, iccProfileData);
    
        return tiffFile;
    }