示例#1
0
文件: mscms.cs 项目: clowd/bmplib
    public static SafeTransformHandle CreateTransform(SafeProfileHandle sourceProfile, SafeProfileHandle destinationProfile, mscmsIntent dwIntent)
    {
        if (sourceProfile == null || sourceProfile.IsInvalid)
        {
            throw new ArgumentNullException("sourceProfile");
        }

        if (destinationProfile == null || destinationProfile.IsInvalid)
        {
            throw new ArgumentNullException("destinationProfile");
        }

        IntPtr[] handles = new IntPtr[2];
        bool     success = true;

        sourceProfile.DangerousAddRef(ref success);
        destinationProfile.DangerousAddRef(ref success);

        try
        {
            handles[0] = sourceProfile.DangerousGetHandle();
            handles[1] = destinationProfile.DangerousGetHandle();

            uint[] dwIntents = new uint[2] {
                (uint)dwIntent, (uint)dwIntent
            };

            var htransform = CreateMultiProfileTransform(
                handles, (uint)handles.Length,
                dwIntents, (uint)dwIntents.Length,
                BEST_MODE | USE_RELATIVE_COLORIMETRIC, 0);

            if (htransform.IsInvalid)
            {
                throw new Win32Exception();
            }

            return(htransform);
        }
        finally
        {
            sourceProfile.DangerousRelease();
            destinationProfile.DangerousRelease();
        }
    }
        private static int CreateColorTransform(SafeProfileHandle input, SafeProfileHandle output, out SafeTransformHandle handle)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
#if DEBUG
            System.Diagnostics.Debug.Assert(!input.IsInvalid, "Input handle is invalid.");
            System.Diagnostics.Debug.Assert(!output.IsInvalid, "Output handle is invalid.");
#endif

            handle = null;

            int error = NativeConstants.ERROR_SUCCESS;

            bool inputNeedsRelease  = false;
            bool outputNeedsRelease = false;
            System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                input.DangerousAddRef(ref inputNeedsRelease);
                output.DangerousAddRef(ref outputNeedsRelease);

                IntPtr[] profiles = new IntPtr[2] {
                    input.DangerousGetHandle(), output.DangerousGetHandle()
                };
                uint[] intents = new uint[2]
                {
                    (uint)NativeEnums.Mscms.RenderingIntent.Perceptual,
                    (uint)NativeEnums.Mscms.RenderingIntent.Perceptual
                };

                handle = UnsafeNativeMethods.Mscms.CreateMultiProfileTransform(
                    profiles,
                    (uint)profiles.Length,
                    intents,
                    (uint)intents.Length,
                    NativeEnums.Mscms.TransformFlags.BestMode,
                    NativeConstants.CMM_FROM_PROFILE
                    );

                if (handle == null || handle.IsInvalid)
                {
                    error = Marshal.GetLastWin32Error();
                }
            }
            finally
            {
                if (inputNeedsRelease)
                {
                    input.DangerousRelease();
                }
                if (outputNeedsRelease)
                {
                    output.DangerousRelease();
                }
            }

            return(error);
        }