protected override bool ProcessDecoder(MainForm form, IWICBitmapDecoder decoder, DataEntry[] de, object tag)
        {
            IWICBitmapDecoderInfo info = decoder.GetDecoderInfo();
            try
            {
                Guid clsid;
                info.GetCLSID(out clsid);
                if (clsid != Parent.Clsid)
                {
                    form.Add(this, Resources.IncorrectDecoderPickedUp, de, new DataEntry(Resources.Expected, Parent.Clsid), new DataEntry(Resources.Actual, clsid));
                }
                else
                {
                    Tag t = (Tag)tag;
                    if (!t.SupportsMultiframe && decoder.GetFrameCount() > 1)
                    {
                        form.Add(this, Resources.DecoderDoesNotMultiframe, de, new DataEntry(Resources.FrameCount, decoder.GetFrameCount()));
                    }
                }
            }
            finally
            {
                info.ReleaseComObject();
            }

            return true;
        }
Beispiel #2
0
        protected override bool ProcessDecoder(MainForm form, IWICBitmapDecoder decoder, DataEntry[] de, object tag)
        {
            IWICBitmapDecoderInfo info = decoder.GetDecoderInfo();

            try
            {
                info.GetCLSID(out var clsid);
                if (clsid != Parent.Clsid)
                {
                    form.Add(this, Resources.IncorrectDecoderPickedUp, de, new DataEntry(Resources.Expected, Parent.Clsid), new DataEntry(Resources.Actual, clsid));
                }
                else
                {
                    Tag t = (Tag)tag;
                    if (!t.SupportsMultiframe && decoder.GetFrameCount() > 1)
                    {
                        form.Add(this, Resources.DecoderDoesNotMultiframe, de, new DataEntry(Resources.FrameCount, decoder.GetFrameCount()));
                    }
                }
            }
            finally
            {
                info.ReleaseComObject();
            }

            return(true);
        }
Beispiel #3
0
        public WicImageContainer(IWICBitmapDecoder dec, WicPipelineContext ctx, FileFormat fmt)
        {
            WicDecoder = ctx.AddRef(dec);

            ContainerFormat = fmt;
            FrameCount      = (int)dec.GetFrameCount();
        }
Beispiel #4
0
 public static IEnumerable <IWICBitmapFrameDecode> GetFrames(this IWICBitmapDecoder bitmapDecoder)
 {
     for (int i = 0, n = bitmapDecoder.GetFrameCount(); i < n; ++i)
     {
         yield return(bitmapDecoder.GetFrame(i));
     }
 }
Beispiel #5
0
        private void init(IWICBitmapDecoder dec, WicProcessingContext ctx)
        {
            Decoder = AddRef(dec);

            ctx.ContainerFormat     = dec.GetContainerFormat();
            ctx.ContainerFrameCount = dec.GetFrameCount();
        }
Beispiel #6
0
        private WicImageContainer(IWICBitmapDecoder dec, WicPipelineContext ctx)
        {
            wicContext = ctx;
            WicDecoder = ctx.AddRef(dec);

            WicContainerFormat = dec.GetContainerFormat();
            ContainerFormat    = formatMap.GetValueOrDefault(WicContainerFormat, FileFormat.Unknown);
            FrameCount         = (int)dec.GetFrameCount();
        }
        static void Main(string[] args)
        {
            IWICImagingFactory    factory         = (IWICImagingFactory) new WICImagingFactory();
            IWICBitmapDecoder     decoder         = null;
            IWICBitmapFrameDecode frame           = null;
            IWICFormatConverter   formatConverter = null;

            try
            {
                const string filename = @"<filename>";

                decoder = factory.CreateDecoderFromFilename(filename, null, NativeMethods.GenericAccessRights.GENERIC_READ,
                                                            WICDecodeOptions.WICDecodeMetadataCacheOnDemand);

                uint count = decoder.GetFrameCount();

                frame = decoder.GetFrame(0);

                uint width  = 0;
                uint height = 0;
                frame.GetSize(out width, out height);

                Guid pixelFormat;
                frame.GetPixelFormat(out pixelFormat);

                // The frame can use many different pixel formats.
                // You can copy the raw pixel values by calling "frame.CopyPixels( )".
                // This method needs a buffer that can hold all bytes.
                // The total number of bytes is: width x height x bytes per pixel

                // The disadvantage of this solution is that you have to deal with all possible pixel formats.

                // You can make your life easy by converting the frame to a pixel format of
                // your choice. The code below shows how to convert the pixel format to 24-bit RGB.

                formatConverter = factory.CreateFormatConverter();

                formatConverter.Initialize(frame,
                                           Consts.GUID_WICPixelFormat24bppRGB,
                                           WICBitmapDitherType.WICBitmapDitherTypeNone,
                                           null,
                                           0.0,
                                           WICBitmapPaletteType.WICBitmapPaletteTypeCustom);

                uint   bytesPerPixel = 3; // Because we have converted the frame to 24-bit RGB
                uint   stride        = width * bytesPerPixel;
                byte[] bytes         = new byte[stride * height];

                formatConverter.CopyPixels(null, stride, stride * height, bytes);
            }
            finally
            {
                frame.ReleaseComObject();
                decoder.ReleaseComObject();
                factory.ReleaseComObject();
            }
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            // This example requires the NuGet package 'stakx.WIC'.
            // https://www.nuget.org/packages/stakx.WIC/
            // https://github.com/stakx/WIC

            const string filename = @"<filename>";

            IWICImagingFactory    factory         = new WICImagingFactory();
            IWICBitmapDecoder     decoder         = null;
            IWICBitmapFrameDecode frame           = null;
            IWICFormatConverter   formatConverter = null;

            decoder = factory.CreateDecoderFromFilename(filename, IntPtr.Zero, StreamAccessMode.GENERIC_READ,
                                                        WICDecodeOptions.WICDecodeMetadataCacheOnDemand);

            int count = decoder.GetFrameCount();

            frame = decoder.GetFrame(0);

            int width  = 0;
            int height = 0;

            frame.GetSize(out width, out height);

            Guid pixelFormat = frame.GetPixelFormat();

            // The frame can use many different pixel formats.
            // You can copy the raw pixel values by calling "frame.CopyPixels( )".
            // This method needs a buffer that can hold all bytes.
            // The total number of bytes is: width x height x bytes per pixel

            // The disadvantage of this solution is that you have to deal with all possible pixel formats.

            // You can make your life easy by converting the frame to a pixel format of
            // your choice. The code below shows how to convert the pixel format to 24-bit RGB.

            formatConverter = factory.CreateFormatConverter();

            formatConverter.Initialize(frame,
                                       GUID_WICPixelFormat24bppRGB,
                                       WICBitmapDitherType.WICBitmapDitherTypeNone,
                                       null,
                                       0.0,
                                       WICBitmapPaletteType.WICBitmapPaletteTypeCustom);

            int bytesPerPixel = 3; // Because we have converted the frame to 24-bit RGB
            int stride        = width * bytesPerPixel;

            byte[] bytes = new byte[stride * height];

            formatConverter.CopyPixels(IntPtr.Zero, stride, stride * height, bytes);
        }
Beispiel #9
0
        private void init(IWICBitmapDecoder dec, WicProcessingContext ctx)
        {
            ctx.Decoder = this;

            if (dec is null)
            {
                return;
            }

            Decoder = ctx.AddRef(dec);

            WicContainerFormat = dec.GetContainerFormat();
            ContainerFormat    = formatMap.GetValueOrDefault(WicContainerFormat, () => FileFormat.Unknown);
            FrameCount         = dec.GetFrameCount();
        }
Beispiel #10
0
        protected override bool ProcessDecoder(MainForm form, IWICBitmapDecoder decoder, DataEntry[] de, object tag)
        {
            CheckCopyPalette(form, de, decoder.CopyPalette);

            CheckGetColorContexts(form, de, decoder.GetColorContexts);

            if (decoder.GetFrameCount() == 0)
            {
                form.Add(this, Resources.FileNoFrames, de);
            }

            CheckGetBitmapSource(form, de, decoder.GetPreview, WinCodecError.WINCODEC_ERR_UNSUPPORTEDOPERATION);
            CheckGetBitmapSource(form, de, decoder.GetThumbnail, WinCodecError.WINCODEC_ERR_CODECNOTHUMBNAIL);

            return true;
        }
Beispiel #11
0
        protected override bool ProcessDecoder(MainForm form, IWICBitmapDecoder decoder, DataEntry[] de, object tag)
        {
            CheckCopyPalette(form, de, decoder.CopyPalette);

            CheckGetColorContexts(form, de, decoder.GetColorContexts);

            if (decoder.GetFrameCount() == 0)
            {
                form.Add(this, Resources.FileNoFrames, de);
            }

            CheckGetBitmapSource(form, de, decoder.GetPreview, WinCodecError.WINCODEC_ERR_UNSUPPORTEDOPERATION);
            CheckGetBitmapSource(form, de, decoder.GetThumbnail, WinCodecError.WINCODEC_ERR_CODECNOTHUMBNAIL);

            return(true);
        }
Beispiel #12
0
        private void filesListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (filesListView.SelectedItems.Count > 0)
            {
                string file = filesListView.SelectedItems[0].Text;

                if (decoder != null)
                {
                    decoder.ReleaseComObject();
                    decoder = null;
                }

                if (frame != null)
                {
                    frame.ReleaseComObject();
                    frame = null;
                }

                IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory();

                decoder = factory.CreateDecoderFromFilename(file, null, NativeMethods.GenericAccessRights.GENERIC_READ, WICDecodeOptions.WICDecodeMetadataCacheOnLoad);

                if (decoder != null)
                {
                    uint frameCount = decoder.GetFrameCount();

                    if (frameCount > 0)
                    {
                        frame = decoder.GetFrame(0);
                    }

                    if (frame != null)
                    {
                        setupMode = true;
                        SetupDevelopRaw();
                        setupMode = false;

                        DisplayImage();
                    }
                }

                factory.ReleaseComObject();
            }
        }
Beispiel #13
0
        public GifPlayer(string filePath)
        {
            WICImagingFactory factory    = WicImagingFactory = new WICImagingFactory();
            IWICBitmapDecoder gifDecoder = WicBitmapDecoder = factory.CreateDecoderFromFilename(filePath,
                                                                                                null, // Do not prefer a particular vendor
                                                                                                WICDecodeOptions.WICDecodeMetadataCacheOnDemand);
            var wicMetadataQueryReader = gifDecoder.GetMetadataQueryReader();

            GetBackgroundColor(wicMetadataQueryReader);

            var propertyVariant = new PROPVARIANT();

            wicMetadataQueryReader.GetMetadataByName("/logscrdesc/Width", ref propertyVariant);
            if ((propertyVariant.Type & VARTYPE.VT_UI2) == VARTYPE.VT_UI2)
            {
                Width = propertyVariant.Value.UI2;
            }

            // 清空一下
            propertyVariant = new PROPVARIANT();
            wicMetadataQueryReader.GetMetadataByName("/logscrdesc/Height", ref propertyVariant);
            if ((propertyVariant.Type & VARTYPE.VT_UI2) == VARTYPE.VT_UI2)
            {
                Height = propertyVariant.Value.UI2;
            }

            // 清空一下
            propertyVariant = new PROPVARIANT();
            wicMetadataQueryReader.GetMetadataByName("/logscrdesc/PixelAspectRatio", ref propertyVariant);
            if ((propertyVariant.Type & VARTYPE.VT_UI1) == VARTYPE.VT_UI1)
            {
                var pixelAspRatio = propertyVariant.Value.UI2;
                if (pixelAspRatio != 0)
                {
                    // Need to calculate the ratio. The value in uPixelAspRatio
                    // allows specifying widest pixel 4:1 to the tallest pixel of
                    // 1:4 in increments of 1/64th
                    float pixelAspRatioFloat = (pixelAspRatio + 15F) / 64F;
                    // Calculate the image width and height in pixel based on the
                    // pixel aspect ratio. Only shrink the image.

                    if (pixelAspRatioFloat > 1.0F)
                    {
                        WidthGifImagePixel  = Width;
                        HeightGifImagePixel = (ushort)(Height / pixelAspRatioFloat);
                    }
                    else
                    {
                        WidthGifImagePixel  = (ushort)(Width * pixelAspRatioFloat);
                        HeightGifImagePixel = Height;
                    }
                }
                else
                {
                    // The value is 0, so its ratio is 1
                    WidthGifImagePixel  = Width;
                    HeightGifImagePixel = Height;
                }
            }


            var frameCount = gifDecoder.GetFrameCount();

            for (int i = 0; i < frameCount; i++)
            {
                var wicBitmapFrameDecode = gifDecoder.GetFrame(i);
                var wicFormatConverter   = factory.CreateFormatConverter();
                wicFormatConverter.Initialize(wicBitmapFrameDecode, WICPixelFormat.WICPixelFormat24bppBGR, WICBitmapDitherType.WICBitmapDitherTypeNone, null, 0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom);

                const int bytesPerPixel = 3;// BGR 格式
                var       size          = wicFormatConverter.GetSize();
                var       imageByte     = new byte[size.Width * size.Height * bytesPerPixel];
                wicFormatConverter.CopyPixels(24, imageByte);
            }
        }
Beispiel #14
0
        protected override void RunOverride(MainForm form, object tag)
        {
            bool hasFile   = false;
            bool processed = false;

            IWICImagingFactory    factory = (IWICImagingFactory) new WICImagingFactory();
            IWICBitmapDecoderInfo info    = null;
            IWICBitmapDecoder     decoder = null;

            try
            {
                info = (IWICBitmapDecoderInfo)factory.CreateComponentInfo(Parent.Clsid);
                foreach (string file in GetDecodableFiles(form, Parent.Clsid))
                {
                    decoder.ReleaseComObject();
                    decoder = info.CreateInstance();
                    hasFile = true;

                    IWICStream stream = factory.CreateStream();
                    stream.InitializeFromFilename(file, NativeMethods.GenericAccessRights.GENERIC_READ);
                    try
                    {
                        decoder.Initialize(stream, WICDecodeOptions.WICDecodeMetadataCacheOnDemand);

                        if (ProcessDecoder(form, decoder, new DataEntry[] { new DataEntry(file) }, tag))
                        {
                            for (uint index = 0; index < decoder.GetFrameCount(); index++)
                            {
                                IWICBitmapFrameDecode frame = decoder.GetFrame(index);
                                try
                                {
                                    if (ProcessFrameDecode(form, frame, new DataEntry[] { new DataEntry(file), new DataEntry(index) }, tag))
                                    {
                                        processed = true;
                                    }
                                }
                                finally
                                {
                                    frame.ReleaseComObject();
                                }
                            }
                        }
                        else
                        {
                            processed = true;
                        }
                    }
                    catch (Exception e)
                    {
                        form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(file), new DataEntry(e));
                    }
                    finally
                    {
                        stream.ReleaseComObject();
                    }
                }
            }
            finally
            {
                factory.ReleaseComObject();
                info.ReleaseComObject();
                decoder.ReleaseComObject();
            }

            if (!hasFile)
            {
                form.Add(Parent, string.Format(CultureInfo.CurrentUICulture, Resources.NoFilesFor_0, Parent.Text));
            }
            else if (!processed)
            {
                form.Add(this, string.Format(CultureInfo.CurrentUICulture, Resources.NoFilesFor_0, Text));
            }
        }