Example #1
0
        internal ColorTransform(SafeMILHandle bitmapSource, ColorContext srcContext, ColorContext dstContext, System.Windows.Media.PixelFormat pixelFormat)
        {
            InitializeICM();

            if (srcContext == null)
            {
                srcContext = new ColorContext(pixelFormat);
            }
            if (dstContext == null)
            {
                dstContext = new ColorContext(pixelFormat);
            }

            _inputColorType  = srcContext.ColorType;
            _outputColorType = dstContext.ColorType;

            //if this failed or the handle is invalid, we can't continue
            if (srcContext.ProfileHandle != null && !srcContext.ProfileHandle.IsInvalid)
            {
                //if this failed or the handle is invalid, we can't continue
                if (dstContext.ProfileHandle != null && !dstContext.ProfileHandle.IsInvalid)
                {
                    _colorTransformHelper.CreateTransform(srcContext.ProfileHandle, dstContext.ProfileHandle);
                }
            }
        }
Example #2
0
 internal override void SetupFrame(SafeMILHandle frameEncodeHandle, SafeMILHandle encoderOptions)
 {
     HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.Initialize(
         frameEncodeHandle,
         encoderOptions
         ));
 }
Example #3
0
        internal ColorTransform(SafeMILHandle bitmapSource, ColorContext srcContext, ColorContext dstContext, System.Windows.Media.PixelFormat pixelFormat) 
        {
            InitializeICM(); 

            if (srcContext == null)
            {
                srcContext = new ColorContext(pixelFormat); 
            }
            if (dstContext == null) 
            { 
                dstContext = new ColorContext(pixelFormat);
            } 

            _inputColorType = srcContext.ColorType;
            _outputColorType = dstContext.ColorType;
 
            //if this failed or the handle is invalid, we can't continue
            if (srcContext.ProfileHandle != null && !srcContext.ProfileHandle.IsInvalid) 
            { 
                //if this failed or the handle is invalid, we can't continue
                if (dstContext.ProfileHandle != null && !dstContext.ProfileHandle.IsInvalid) 
                {
                    _colorTransformHelper.CreateTransform(srcContext.ProfileHandle, dstContext.ProfileHandle);
                }
            } 
        }
Example #4
0
        private void CreateMedia(MediaPlayer mediaPlayer)
        {
            CheckMediaDisabledFlags();

            SafeMILHandle unmanagedProxy = null;

            MediaEventsHelper.CreateMediaEventsHelper(mediaPlayer, out _mediaEventsHelper, out unmanagedProxy);
            try
            {
                using (FactoryMaker myFactory = new FactoryMaker())
                {
                    HRESULT.Check(UnsafeNativeMethods.MILFactory2.CreateMediaPlayer(
                                      myFactory.FactoryPtr,
                                      unmanagedProxy,
                                      SecurityHelper.CallerHasMediaPermission(MediaPermissionAudio.AllAudio, MediaPermissionVideo.AllVideo, MediaPermissionImage.NoImage),
                                      out _nativeMedia
                                      ));
                }
            }
            catch
            {
                if (_nativeMedia != null && !_nativeMedia.IsInvalid)
                {
                    _nativeMedia.Close();
                }

                throw;
            }

            _helper = new Helper(_nativeMedia);
            AppDomain.CurrentDomain.ProcessExit += _helper.ProcessExitHandler;
        }
Example #5
0
        /// <summary>
        /// Create the unmanaged media resources
        /// </summary>
        private void CreateMedia(MediaPlayer mediaPlayer)
        {
            SafeMILHandle unmanagedProxy = null;

            MediaEventsHelper.CreateMediaEventsHelper(mediaPlayer, out _mediaEventsHelper, out unmanagedProxy);
            try
            {
                using (FactoryMaker myFactory = new FactoryMaker())
                {
                    HRESULT.Check(UnsafeNativeMethods.MILFactory2.CreateMediaPlayer(
                                      myFactory.FactoryPtr,
                                      unmanagedProxy,
                                      true,
                                      out _nativeMedia
                                      ));
                }
            }
            catch
            {
                if (_nativeMedia != null && !_nativeMedia.IsInvalid)
                {
                    _nativeMedia.Close();
                }

                throw;
            }

            _helper = new Helper(_nativeMedia);
            AppDomain.CurrentDomain.ProcessExit += _helper.ProcessExitHandler;
        }
 internal void CopyMemoryPressure(SafeMILHandle original)
 {
     _gcPressure = original._gcPressure;
     if (_gcPressure != null)
     {
         _gcPressure.AddRef();
     }
 }
 internal void CopyMemoryPressure(SafeMILHandle original)
 {
     _gcPressure = original._gcPressure;
     if (_gcPressure != null)
     {
         _gcPressure.AddRef();
     }
 }
Example #8
0
        internal static PixelFormat GetPixelFormat(
            SafeMILHandle /* IWICBitmapSource */ bitmapSource
            )
        {
            Guid guidPixelFormat = WICPixelFormatGUIDs.WICPixelFormatDontCare;

            HRESULT.Check(UnsafeNativeMethods.WICBitmapSource.GetPixelFormat(bitmapSource, out guidPixelFormat));

            return(new PixelFormat(guidPixelFormat));
        }
Example #9
0
        /// <summary>
        /// Create
        /// </summary>
        internal static void CreateMediaEventsHelper(MediaPlayer mediaPlayer,
                                                     out MediaEventsHelper eventsHelper,
                                                     out SafeMILHandle unmanagedProxy)
        {
            eventsHelper = new MediaEventsHelper(mediaPlayer);

            // Created with ref count = 1. Since this object does not hold on
            // to the unmanaged proxy, the lifetime is now controlled by whoever
            // called CreateMediaEventsHelper.
            unmanagedProxy = EventProxyWrapper.CreateEventProxyWrapper(eventsHelper);
        }
Example #10
0
        internal static void CreateMediaEventsHelper(MediaPlayer mediaPlayer, 
                                                     out MediaEventsHelper eventsHelper,
                                                     out SafeMILHandle unmanagedProxy)
        {
            eventsHelper = new MediaEventsHelper(mediaPlayer); 

            // Created with ref count = 1. Since this object does not hold on 
            // to the unmanaged proxy, the lifetime is now controlled by whoever 
            // called CreateMediaEventsHelper.
            unmanagedProxy = EventProxyWrapper.CreateEventProxyWrapper(eventsHelper); 
        }
 internal UnknownBitmapDecoder(
     SafeMILHandle decoderHandle,
     BitmapDecoder decoder,
     Uri baseUri,
     Uri uri,
     Stream stream,
     BitmapCreateOptions createOptions,
     BitmapCacheOption cacheOption,
     bool insertInDecoderCache,
     bool originalWritable,
     Stream uriStream,
     UnmanagedMemoryStream unmanagedMemoryStream,
     SafeFileHandle safeFilehandle             
     ) : base(decoderHandle, decoder, baseUri, uri, stream, createOptions, cacheOption, insertInDecoderCache, originalWritable, uriStream, unmanagedMemoryStream, safeFilehandle)
 {
 }
Example #12
0
        /// <summary>
        /// Helper method that will retrieve ColorContexts from an unmanaged object (e.g. BitmapDecoder or BitmapFrameDecode)
        /// </summary>
        internal static IList <ColorContext> GetColorContextsHelper(GetColorContextsDelegate getColorContexts)
        {
            uint numContexts = 0;
            List <ColorContext> colorContextsList = null;

            int hr = getColorContexts(ref numContexts, null);

            if (hr != (int)WinCodecErrors.WINCODEC_ERR_UNSUPPORTEDOPERATION)
            {
                HRESULT.Check(hr);
            }

            if (numContexts > 0)
            {
                // GetColorContexts does not create new IWICColorContexts. Instead, it initializes existing
                // ones so we must create them beforehand.
                SafeMILHandle[] colorContextHandles = new SafeMILHandle[numContexts];

                using (FactoryMaker factoryMaker = new FactoryMaker())
                {
                    for (uint i = 0; i < numContexts; ++i)
                    {
                        HRESULT.Check(UnsafeNativeMethodsMilCoreApi.WICCodec.CreateColorContext(factoryMaker.ImagingFactoryPtr, out colorContextHandles[i]));
                    }
                }

                // The Marshal is unable to handle SafeMILHandle[] so we will convert it to an IntPtr[] ourselves.
                {
                    IntPtr[] colorContextPtrs = new IntPtr[numContexts];

                    for (uint i = 0; i < numContexts; ++i)
                    {
                        colorContextPtrs[i] = colorContextHandles[i].DangerousGetHandle();
                    }

                    HRESULT.Check(getColorContexts(ref numContexts, colorContextPtrs));
                }

                colorContextsList = new List <ColorContext>((int)numContexts);
                for (uint i = 0; i < numContexts; ++i)
                {
                    colorContextsList.Add(new ColorContext(colorContextHandles[i]));
                }
            }

            return(colorContextsList);
        }
Example #13
0
        internal static SafeMILHandle CreateEventProxyWrapper(IInvokable invokable)
        {
            if (invokable == null)
            {
                throw new System.ArgumentNullException("invokable");
            }

            SafeMILHandle eventProxy = null;

            EventProxyWrapper    epw = new EventProxyWrapper(invokable);
            EventProxyDescriptor epd = new EventProxyDescriptor();

            epd.pfnDispose    = EventProxyStaticPtrs.pfnDispose;
            epd.pfnRaiseEvent = EventProxyStaticPtrs.pfnRaiseEvent;

            epd.m_handle = System.Runtime.InteropServices.GCHandle.Alloc(epw, System.Runtime.InteropServices.GCHandleType.Normal);

            HRESULT.Check(MILCreateEventProxy(ref epd, out eventProxy));

            return(eventProxy);
        }
 internal static extern int /* HRESULT */ GetEncoderInfo(SafeMILHandle /* IWICBitmapEncoder* */ THIS_PTR, 
     out SafeMILHandle /* IWICBitmapEncoderInfo ** */ ppIEncoderInfo
     ); 
 internal static extern int /* HRESULT */ Initialize(SafeMILHandle /* IWICBitmapEncoder* */ THIS_PTR,
     IntPtr /* IStream */ pStream,
     WICBitmapEncodeCacheOption option); 
 internal static extern int /* HRESULT */ SetColorContexts(SafeMILHandle /* IWICBitmapEncoder*  */ THIS_PTR,
         uint nIndex, 
         IntPtr[] /* IWICColorContext */ ppIColorContext 
         );
Example #17
0
 private extern static int /* HRESULT */ MILCreateEventProxy(ref EventProxyDescriptor pEPD, out SafeMILHandle ppEventProxy);
 internal static extern int /* HRESULT */ GetFrameCount(
     SafeMILHandle THIS_PTR, 
     out uint pFrameCount 
     );
 internal static extern int /* HRESULT */ Commit(SafeMILHandle /* IWICBitmapEncoder* */ THIS_PTR);
 internal static extern int /* HRESULT */ SetPalette(SafeMILHandle /* IWICBitmapEncoder*  */ THIS_PTR,
     System.Windows.Media.SafeMILHandle /* IWICPalette* */ pIPalette);
 internal static extern UInt32 AddRef(SafeMILHandle pIUnkown);
 internal static extern int /* HRESULT */ GetColorContexts( 
     SafeMILHandle /* IWICBitmapFrameDecode */ THIS_PTR,
     uint count, 
     IntPtr[] /* IWICColorContext */ ppIColorContext,
     out uint pActualCount
     );
 internal static extern int /* HRESULT */ GetMetadataQueryReader(
     SafeMILHandle /* IWICBitmapFrameDecode */ THIS_PTR,
     out IntPtr /* IWICMetadataQueryReader */ ppIQueryReader
     ); 
 internal static extern int /* HRESULT */ GetThumbnail(
     SafeMILHandle /* IWICBitmapFrameDecode */ THIS_PTR,
     out IntPtr /* IWICBitmap */ ppIThumbnail
     ); 
 internal static extern int /* HRESULT */ GetFrame(
     SafeMILHandle /* IWICBitmapDecoder */ THIS_PTR, 
     UInt32 index, 
     out IntPtr /* IWICBitmapFrameDecode */ ppIFrameDecode
     ); 
 internal static extern int /* HRESULT */ CreateNewFrame(SafeMILHandle /* IWICBitmapEncoder* */ THIS_PTR,
     out SafeMILHandle /* IWICBitmapFrameEncode ** */ ppIFramEncode, 
     out SafeMILHandle /* IPropertyBag2 ** */ ppIEncoderOptions
 );
 internal static extern int /* HRESULT */ SetThumbnail(SafeMILHandle /* IWICBitmapEncoder*  */ THIS_PTR,
     System.Windows.Media.SafeMILHandle /* IWICBitmapSource* */ pIThumbnail);
Example #28
0
 /// <summary>
 /// Setups the encoder and other properties before encoding each frame
 /// </summary>
 internal virtual void SetupFrame(SafeMILHandle frameEncodeHandle, SafeMILHandle encoderOptions)
 {
     throw new NotImplementedException();
 }
 internal static extern int /* HRESULT */ GetMetadataQueryWriter(
     SafeMILHandle /* IWICBitmapEncoder */ THIS_PTR,
     out SafeMILHandle /* IWICMetadataQueryWriter */ ppIQueryWriter
     ); 
Example #30
0
        private void SaveFrame(SafeMILHandle frameEncodeHandle, SafeMILHandle encoderOptions, BitmapFrame frame)
        {
            SetupFrame(frameEncodeHandle, encoderOptions);

            // Helpful for debugging stress and remote dumps
            _encodeState = EncodeState.FrameEncodeInitialized;

            // Set the size
            HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.SetSize(
                frameEncodeHandle,
                frame.PixelWidth,
                frame.PixelHeight
                ));

            // Helpful for debugging stress and remote dumps
            _encodeState = EncodeState.FrameEncodeSizeSet;

            // Set the resolution
            double dpiX = frame.DpiX;
            double dpiY = frame.DpiY;

            if (dpiX <= 0)
            {
                dpiX = 96;
            }
            if (dpiY <= 0)
            {
                dpiY = 96;
            }

            HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.SetResolution(
                frameEncodeHandle,
                dpiX,
                dpiY
                ));

            // Helpful for debugging stress and remote dumps
            _encodeState = EncodeState.FrameEncodeResolutionSet;

            if (_supportsFrameThumbnails)
            {
                // Set the thumbnail.
                BitmapSource thumbnail = frame.Thumbnail;

                if (thumbnail != null)
                {
                    SafeMILHandle thumbnailHandle = thumbnail.WicSourceHandle;

                    lock (thumbnail.SyncObject)
                    {
                        HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.SetThumbnail(
                            frameEncodeHandle,
                            thumbnailHandle
                            ));

                        // Helpful for debugging stress and remote dumps
                        _encodeState = EncodeState.FrameEncodeThumbnailSet;
                    }
                }
            }

            // if the source has been color corrected, we want to use a corresponding color profile
            if (frame._isColorCorrected)
            {
                ColorContext colorContext = new ColorContext(frame.Format);
                IntPtr[] colorContextPtrs = new IntPtr[1] { colorContext.ColorContextHandle.DangerousGetHandle() };

                int hr = UnsafeNativeMethods.WICBitmapFrameEncode.SetColorContexts(
                    frameEncodeHandle,
                    1,
                    colorContextPtrs
                    );

                // It's possible that some encoders may not support color contexts so don't check hr
                if (hr == HRESULT.S_OK)
                {
                    // Helpful for debugging stress and remote dumps
                    _encodeState = EncodeState.FrameEncodeColorContextsSet;
                }
            }
            // if the caller has explicitly provided color contexts, add them to the encoder
            else
            {
                IList<ColorContext> colorContexts = frame.ColorContexts;
                if (colorContexts != null && colorContexts.Count > 0)
                {             
                    int count = colorContexts.Count;

                    // Marshal can't convert SafeMILHandle[] so we must
                    {
                        IntPtr[] colorContextPtrs = new IntPtr[count];
                        for (int i = 0; i < count; ++i)
                        {
                            colorContextPtrs[i] = colorContexts[i].ColorContextHandle.DangerousGetHandle();
                        }

                        int hr = UnsafeNativeMethods.WICBitmapFrameEncode.SetColorContexts(
                            frameEncodeHandle,
                            (uint)count,
                            colorContextPtrs
                            );

                        // It's possible that some encoders may not support color contexts so don't check hr
                        if (hr == HRESULT.S_OK)
                        {
                            // Helpful for debugging stress and remote dumps
                            _encodeState = EncodeState.FrameEncodeColorContextsSet;
                        }
                    }
                }
            }

            // Set the pixel format and palette

            lock (frame.SyncObject)
            {
                SafeMILHandle outSourceHandle = new SafeMILHandle();
                SafeMILHandle bitmapSourceHandle = frame.WicSourceHandle;
                SafeMILHandle paletteHandle = new SafeMILHandle();

                // Set the pixel format and palette of the bitmap.
                // This could (but hopefully won't) introduce a format converter.
                HRESULT.Check(UnsafeNativeMethods.WICCodec.WICSetEncoderFormat(
                    bitmapSourceHandle,
                    paletteHandle,
                    frameEncodeHandle,
                    out outSourceHandle
                    ));

                // Helpful for debugging stress and remote dumps
                _encodeState = EncodeState.FrameEncodeFormatSet;
                _writeSourceHandles.Add(outSourceHandle);

                // Set the metadata
                if (_supportsFrameMetadata)
                {
                    BitmapMetadata metadata = frame.Metadata as BitmapMetadata;

                    // If the frame has metadata associated with a different container format, then we ignore it.
                    if (metadata != null && metadata.GuidFormat == ContainerFormat)
                    {
                        SafeMILHandle /* IWICMetadataQueryWriter */ metadataHandle = new SafeMILHandle();

                        HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.GetMetadataQueryWriter(
                            frameEncodeHandle,
                            out metadataHandle
                            ));

                        PROPVARIANT propVar = new PROPVARIANT();

                        try
                        {
                            propVar.Init(metadata);

                            lock (metadata.SyncObject)
                            {
                                HRESULT.Check(UnsafeNativeMethods.WICMetadataQueryWriter.SetMetadataByName(
                                    metadataHandle,
                                    "/",
                                    ref propVar
                                    ));

                                // Helpful for debugging stress and remote dumps
                                _encodeState = EncodeState.FrameEncodeMetadataSet;
                            }
                        }
                        finally
                        {
                            propVar.Clear();
                        }
                    }
                }

                Int32Rect r = new Int32Rect();
                HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.WriteSource(
                    frameEncodeHandle,
                    outSourceHandle,
                    ref r
                    ));

                // Helpful for debugging stress and remote dumps
                _encodeState = EncodeState.FrameEncodeSourceWritten;

                HRESULT.Check(UnsafeNativeMethods.WICBitmapFrameEncode.Commit(
                    frameEncodeHandle
                    ));

                // Helpful for debugging stress and remote dumps
                _encodeState = EncodeState.FrameEncodeCommitted;
            }
        }
Example #31
0
        public virtual void Save(System.IO.Stream stream)
        {
            VerifyAccess();
            EnsureBuiltIn();
            EnsureUnmanagedEncoder();

            // No-op to get rid of build error
            if (_encodeState == EncodeState.None)
            {
            }

            if (_hasSaved)
            {
                throw new InvalidOperationException(SR.Get(SRID.Image_OnlyOneSave));
            }

            if (_frames == null)
            {
                throw new System.NotSupportedException(SR.Get(SRID.Image_NoFrames, null));
            }

            int count = _frames.Count;
            if (count <= 0)
            {
                throw new System.NotSupportedException(SR.Get(SRID.Image_NoFrames, null));
            }

            IntPtr comStream = IntPtr.Zero;
            SafeMILHandle encoderHandle = _encoderHandle;

            try
            {
                comStream = StreamAsIStream.IStreamFrom(stream);

                // does this addref the stream?
                HRESULT.Check(UnsafeNativeMethods.WICBitmapEncoder.Initialize(
                    encoderHandle,
                    comStream,
                    WICBitmapEncodeCacheOption.WICBitmapEncodeNoCache
                    ));

                // Helpful for debugging stress and remote dumps
                _encodeState = EncodeState.EncoderInitialized;

                // Save global thumbnail if any.
                if (_thumbnail != null)
                {
                    Debug.Assert(_supportsGlobalThumbnail);
                    SafeMILHandle thumbnailBitmapSource = _thumbnail.WicSourceHandle;

                    lock (_thumbnail.SyncObject)
                    {
                        HRESULT.Check(UnsafeNativeMethods.WICBitmapEncoder.SetThumbnail(
                            encoderHandle,
                            thumbnailBitmapSource
                            ));

                        // Helpful for debugging stress and remote dumps
                        _encodeState = EncodeState.EncoderThumbnailSet;
                    }
                }

                // Save global palette if any.
                if (_palette != null && _palette.Colors.Count > 0)
                {
                    SafeMILHandle paletteHandle = _palette.InternalPalette;

                    HRESULT.Check(UnsafeNativeMethods.WICBitmapEncoder.SetPalette(
                        encoderHandle,
                        paletteHandle
                        ));

                    // Helpful for debugging stress and remote dumps
                    _encodeState = EncodeState.EncoderPaletteSet;
                }

                // Save global metadata if any.
                if (_metadata != null && _metadata.GuidFormat == ContainerFormat)
                {
                    Debug.Assert(_supportsGlobalMetadata);

                    EnsureMetadata(false);

                    if (_metadata.InternalMetadataHandle != _metadataHandle)
                    {
                        PROPVARIANT propVar = new PROPVARIANT();

                        try
                        {
                            propVar.Init(_metadata);

                            lock (_metadata.SyncObject)
                            {
                                HRESULT.Check(UnsafeNativeMethods.WICMetadataQueryWriter.SetMetadataByName(
                                    _metadataHandle,
                                    "/",
                                    ref propVar
                                    ));
                            }
                        }
                        finally
                        {
                            propVar.Clear();
                        }
                    }
                }

                for (int i = 0; i < count; i++)
                {
                    SafeMILHandle frameEncodeHandle = new SafeMILHandle();
                    SafeMILHandle encoderOptions = new SafeMILHandle();
                    HRESULT.Check(UnsafeNativeMethods.WICBitmapEncoder.CreateNewFrame(
                        encoderHandle,
                        out frameEncodeHandle,
                        out encoderOptions
                        ));

                    // Helpful for debugging stress and remote dumps
                    _encodeState = EncodeState.EncoderCreatedNewFrame;
                    _frameHandles.Add(frameEncodeHandle);

                    SaveFrame(frameEncodeHandle, encoderOptions, _frames[i]);

                    // If multiple frames are not supported, break out
                    if (!_supportsMultipleFrames)
                    {
                        break;
                    }
                }

                // Now let the encoder know we are done encoding the file.
                HRESULT.Check(UnsafeNativeMethods.WICBitmapEncoder.Commit(encoderHandle));

                // Helpful for debugging stress and remote dumps
                _encodeState = EncodeState.EncoderCommitted;
            }
            finally
            {
                UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref comStream);
            }

            _hasSaved = true;
        }
 internal static extern int /* HRESULT */ GetPreview(
     SafeMILHandle THIS_PTR, 
     out IntPtr /* IWICBitmapSource */ ppIBitmapSource
     ); 
Example #33
0
        private void EnsureMetadata(bool createBitmapMetadata)
        {
            if (!_supportsGlobalMetadata)
            {
                return;
            }

            if (_metadataHandle == null)
            {
                SafeMILHandle /* IWICMetadataQueryWriter */ metadataHandle = new SafeMILHandle();

                int hr = UnsafeNativeMethods.WICBitmapEncoder.GetMetadataQueryWriter(
                    _encoderHandle,
                    out metadataHandle
                    );

                if (hr == (int)WinCodecErrors.WINCODEC_ERR_UNSUPPORTEDOPERATION)
                {
                    _supportsGlobalMetadata = false;
                    return;
                }
                HRESULT.Check(hr);

                _metadataHandle = metadataHandle;
            }

            if (createBitmapMetadata &&
                _metadata == null &&
                _metadataHandle != null)
            {
                _metadata = new BitmapMetadata(_metadataHandle, false, IsMetadataFixedSize, _metadataHandle);
            }
        }
 internal static extern int /* HRESULT */ SetResolution(SafeMILHandle /* IWICBitmapFrameEncode*  */ THIS_PTR,
     double dpiX,
     double dpiY);
 internal static extern int /* HRESULT */ QueryInterface(
     SafeMILHandle pIUnknown, 
     ref Guid guid,
     out IntPtr ppvObject); 
 internal static extern int /* HRESULT */ WriteSource(SafeMILHandle /* IWICBitmapFrameEncode*  */ THIS_PTR,
     SafeMILHandle /* IWICBitmapSource* */ pIBitmapSource,
     ref Int32Rect /* MILRect* */ r); 
        internal BitmapPalette(SafeMILHandle unmanagedPalette)
        {
            _palette = unmanagedPalette;

            // Fill in the Colors property.
            UpdateManaged();
        }
 internal static extern int /* HRESULT */ Initialize(SafeMILHandle /* IWICBitmapFrameEncode*  */ THIS_PTR,
     SafeMILHandle /* IPropertyBag2* */ pIEncoderOptions);
Example #39
0
        /// <summary>
        /// Create a ColorContext from an unmanaged color context
        /// </summary>
        private ColorContext(SafeMILHandle colorContextHandle)
        {
            _colorContextHandle = colorContextHandle;

            //
            // For 3.* backwards compat, we aren't going to HRESULT.Check() anywhere because
            // that could introduce new exceptions. If anything fails, _colorContextHelper
            // will be invalid and we'll emulate the old failure behavior later in
            // OpenProfileStream()
            //

            IWICCC.WICColorContextType type;
            if (HRESULT.Failed(IWICCC.GetType(_colorContextHandle, out type)))
            {
                return;
            }

            switch (type)
            {
            case IWICCC.WICColorContextType.WICColorContextProfile:
                uint cbProfileActual;
                int  hr = IWICCC.GetProfileBytes(_colorContextHandle, 0, null, out cbProfileActual);
                if (HRESULT.Succeeded(hr) && cbProfileActual != 0)
                {
                    byte[] profileData = new byte[cbProfileActual];
                    if (HRESULT.Failed(IWICCC.GetProfileBytes(
                                           _colorContextHandle, cbProfileActual, profileData, out cbProfileActual))
                        )
                    {
                        return;
                    }

                    FromRawBytes(profileData, (int)cbProfileActual, /* dontThrowException = */ true);
                }

                break;

            case IWICCC.WICColorContextType.WICColorContextExifColorSpace:
                uint colorSpace;
                if (HRESULT.Failed(IWICCC.GetExifColorSpace(_colorContextHandle, out colorSpace)))
                {
                    return;
                }

                //
                // From MSDN:
                //     "1" is sRGB. We will use our built-in sRGB profile.
                //     "2" is Adobe RGB. WIC says we should never see this because they are nonstandard and instead a
                //     real profile will be returned.
                //     "3-65534" is unused.
                //
                // From the Exif spec:
                //     B. Tag Relating to Color Space
                //     ColorSpace
                //
                //     The color space information tag (ColorSpace) is always recorded as the color space specifier.
                //     Normally sRGB (=1) is used to define the color space based on the PC monitor conditions and environment. If a
                //     color space other than sRGB is used, Uncalibrated (=FFFF.H) is set. Image data recorded as Uncalibrated can be
                //     treated as sRGB when it is converted to Flashpix. On sRGB see Annex E.
                //     Tag = 40961 (A001.H)
                //     Type = SHORT
                //     Count = 1
                //     1 = sRGB
                //     FFFF.H = Uncalibrated
                //
                // So for 65535 we will return sRGB since it is acceptible rather than having an invalid ColorContext. The Exif
                // CC should always be the second one so the real one is given priority. Alternatively, we could ignore the
                // uncalibrated CC but that would be a breaking change with 3.* (returning 1 instead of 2).
                //
                // If anything other than 1 or 65535 happens, _colorContextHelper will remain invalid and we will emulate
                // the old crash behavior in OpenProfileStream().
                //

                if (colorSpace == 1 || colorSpace == 65535)
                {
                    ResourceManager resourceManager = new ResourceManager(
                        _colorProfileResources, Assembly.GetAssembly(typeof(ColorContext))
                        );
                    byte[] sRGBProfile = (byte[])resourceManager.GetObject(_sRGBProfileName);
                    // The existing ColorContext has already been initialized as Exif so we can't initialize it again
                    // and instead must create a new one.
                    using (FactoryMaker factoryMaker = new FactoryMaker())
                    {
                        _colorContextHandle.Dispose();
                        _colorContextHandle = null;

                        if (HRESULT.Failed(UnsafeNativeMethodsMilCoreApi.WICCodec.CreateColorContext(
                                               factoryMaker.ImagingFactoryPtr, out _colorContextHandle))
                            )
                        {
                            return;
                        }

                        if (HRESULT.Failed(IWICCC.InitializeFromMemory(
                                               _colorContextHandle, sRGBProfile, (uint)sRGBProfile.Length))
                            )
                        {
                            return;
                        }
                    }

                    // Finally, fill in _colorContextHelper
                    FromRawBytes(sRGBProfile, sRGBProfile.Length, /* dontThrowException = */ true);
                }
                else if (Invariant.Strict)
                {
                    Invariant.Assert(false, String.Format(CultureInfo.InvariantCulture, "IWICColorContext::GetExifColorSpace returned {0}.", colorSpace));
                }

                break;

            default:
                if (Invariant.Strict)
                {
                    Invariant.Assert(false, "IWICColorContext::GetType() returned WICColorContextUninitialized.");
                }

                break;
            }


            // SECURITY NOTE: This constructor does not set a Uri because the profile comes from raw file
            //                data. Thus, we don't set _isProfileUriNotFromUser to true because we
            //                don't want get_ProfileUri to demand permission to return null.
            Debug.Assert(_profileUri.Value == null);
        }
 internal static extern int /* HRESULT */ SetSize(SafeMILHandle /* IWICBitmapFrameEncode*  */ THIS_PTR, 
     int width,
     int height);