Exemplo n.º 1
0
        private static IWICColorContext getDefaultColorProfile(Guid pixelFormat)
        {
            var pfi = Wic.CreateComponentInfo(pixelFormat) as IWICPixelFormatInfo;
            var cc  = pfi.GetColorContext();

            Marshal.ReleaseComObject(pfi);
            return(cc);
        }
Exemplo n.º 2
0
        protected WicBitmapSourceBase(IWICBitmapSource source)
        {
            Source = source;

            Source.GetSize(out Width, out Height);
            Format = Source.GetPixelFormat();

            var pfi = AddRef(Wic.CreateComponentInfo(Format)) as IWICPixelFormatInfo;

            Channels = pfi.GetChannelCount();
            Bpp      = pfi.GetBitsPerPixel() / 8u;
            Release(pfi);

            Stride = Width * Bpp + 3u & ~3u;
        }
Exemplo n.º 3
0
        public WicMetadataReader(WicTransform prev, bool basicOnly = false) : base(prev)
        {
            var pfi = AddRef(Wic.CreateComponentInfo(Context.PixelFormat)) as IWICPixelFormatInfo2;

            if (pfi.GetNumericRepresentation() == WICPixelFormatNumericRepresentation.WICPixelFormatNumericRepresentationIndexed)
            {
                var pal = AddRef(Wic.CreatePalette());
                Frame.CopyPalette(pal);

                Context.HasAlpha    = pal.HasAlpha();
                Context.IsGreyscale = pal.IsGrayscale();
            }
            else
            {
                uint chans = pfi.GetChannelCount();
                bool trans = pfi.SupportsTransparency();
                Context.HasAlpha    = trans;
                Context.IsGreyscale = chans == 1u;
                Context.IsCmyk      = (chans == 4u && !trans) || (chans == 5u && trans);
            }

            var metareader = Frame.GetMetadataQueryReaderNoThrow();

            if (metareader != null)
            {
                AddRef(metareader);
                // Exif orientation
                if (metareader.HasMetadataName("System.Photo.Orientation"))
                {
                    ushort orientation = 1;
                    var    ovar        = new PropVariant();
                    metareader.GetMetadataByName("System.Photo.Orientation", ovar);
                    if (ovar.UnmanagedType == VarEnum.VT_UI2)
                    {
                        orientation = (ushort)ovar.Value;
                    }

                    var opt = WICBitmapTransformOptions.WICBitmapTransformRotate0;
                    if (orientation == 3 || orientation == 4)
                    {
                        opt = WICBitmapTransformOptions.WICBitmapTransformRotate180;
                    }
                    else if (orientation == 6 || orientation == 7)
                    {
                        opt = WICBitmapTransformOptions.WICBitmapTransformRotate90;
                    }
                    else if (orientation == 5 || orientation == 8)
                    {
                        opt = WICBitmapTransformOptions.WICBitmapTransformRotate270;
                    }

                    if (orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7)
                    {
                        opt |= WICBitmapTransformOptions.WICBitmapTransformFlipHorizontal;
                    }

                    Context.TransformOptions = opt;
                }

                if (basicOnly)
                {
                    return;
                }

                // other requested properties
                var propdic = new Dictionary <string, PropVariant>();
                foreach (string prop in Context.Settings.MetadataNames ?? Enumerable.Empty <string>())
                {
                    if (metareader.HasMetadataName(prop))
                    {
                        var pvar = new PropVariant();
                        metareader.GetMetadataByName(prop, pvar);
                        if (pvar.Value != null)
                        {
                            propdic[prop] = pvar;
                        }
                    }
                }

                Context.Metadata = propdic;
            }

            // ICC profiles
            //http://ninedegreesbelow.com/photography/embedded-color-space-information.html
            uint ccc      = Frame.GetColorContextCount();
            var  profiles = new IWICColorContext[ccc];
            var  profile  = (IWICColorContext)null;

            if (ccc > 0)
            {
                for (int i = 0; i < ccc; i++)
                {
                    profiles[i] = AddRef(Wic.CreateColorContext());
                }

                Frame.GetColorContexts(ccc, profiles);
            }

            foreach (var cc in profiles)
            {
                var cct = cc.GetType();
                if (cct == WICColorContextType.WICColorContextProfile)
                {
                    uint ccs = cc.GetProfileBytes(0, null);
                    var  ccb = new byte[ccs];
                    cc.GetProfileBytes(ccs, ccb);

                    // match only color profiles that match our intended use. if we have a standard sRGB profile, don't save it; we don't need to convert
                    var cp = new ColorProfileInfo(ccb);
                    if (cp.IsValid && ((cp.IsDisplayRgb && !cp.IsStandardSrgb) || (Context.IsCmyk && cp.IsCmyk) /* || (Context.IsGreyscale && cp.DataColorSpace == "GRAY") */))
                    {
                        profile = cc;
                        break;
                    }
                }
                else if (cct == WICColorContextType.WICColorContextExifColorSpace && cc.GetExifColorSpace() == ExifColorSpace.AdobeRGB)
                {
                    profile = cc;
                    break;
                }
            }

            Context.SourceColorContext = profile ?? (Context.IsCmyk ? cmykProfile.Value : null);
            Context.DestColorContext   = srgbProfile.Value;
        }