Ejemplo n.º 1
0
        private void EncodeGlobal <TPixel>(Image <TPixel> image, QuantizedFrame <TPixel> quantized, int transparencyIndex, Stream stream)
            where TPixel : struct, IPixel <TPixel>
        {
            var palleteQuantizer = new PaletteQuantizer(this.quantizer.Diffuser);

            for (int i = 0; i < image.Frames.Count; i++)
            {
                ImageFrame <TPixel> frame         = image.Frames[i];
                ImageFrameMetaData  metaData      = frame.MetaData;
                GifFrameMetaData    frameMetaData = metaData.GetFormatMetaData(GifFormat.Instance);
                this.WriteGraphicalControlExtension(frameMetaData, transparencyIndex, stream);
                this.WriteImageDescriptor(frame, false, stream);

                if (i == 0)
                {
                    this.WriteImageData(quantized, stream);
                }
                else
                {
                    using (QuantizedFrame <TPixel> paletteQuantized
                               = palleteQuantizer.CreateFrameQuantizer(image.GetConfiguration(), () => quantized.Palette).QuantizeFrame(frame))
                    {
                        this.WriteImageData(paletteQuantized, stream);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void CloneIsDeep()
        {
            var metaData             = new ImageFrameMetaData();
            ImageFrameMetaData clone = metaData.DeepClone();

            Assert.False(metaData.GetFormatMetaData(GifFormat.Instance).Equals(clone.GetFormatMetaData(GifFormat.Instance)));
        }
        public void ConstructorImageFrameMetaData()
        {
            const int frameDelay                   = 42;
            const int colorTableLength             = 128;
            const GifDisposalMethod disposalMethod = GifDisposalMethod.RestoreToBackground;

            var metaData = new ImageFrameMetaData();
            GifFrameMetaData gifFrameMetaData = metaData.GetFormatMetaData(GifFormat.Instance);

            gifFrameMetaData.FrameDelay       = frameDelay;
            gifFrameMetaData.ColorTableLength = colorTableLength;
            gifFrameMetaData.DisposalMethod   = disposalMethod;

            var clone = new ImageFrameMetaData(metaData);
            GifFrameMetaData cloneGifFrameMetaData = clone.GetFormatMetaData(GifFormat.Instance);

            Assert.Equal(frameDelay, cloneGifFrameMetaData.FrameDelay);
            Assert.Equal(colorTableLength, cloneGifFrameMetaData.ColorTableLength);
            Assert.Equal(disposalMethod, cloneGifFrameMetaData.DisposalMethod);
        }
Ejemplo n.º 4
0
        private void EncodeLocal <TPixel>(Image <TPixel> image, QuantizedFrame <TPixel> quantized, Stream stream)
            where TPixel : struct, IPixel <TPixel>
        {
            ImageFrame <TPixel> previousFrame = null;
            GifFrameMetaData    previousMeta  = null;

            foreach (ImageFrame <TPixel> frame in image.Frames)
            {
                ImageFrameMetaData metaData      = frame.MetaData;
                GifFrameMetaData   frameMetaData = metaData.GetFormatMetaData(GifFormat.Instance);
                if (quantized is null)
                {
                    // Allow each frame to be encoded at whatever color depth the frame designates if set.
                    if (previousFrame != null && previousMeta.ColorTableLength != frameMetaData.ColorTableLength &&
                        frameMetaData.ColorTableLength > 0)
                    {
                        quantized = this.quantizer.CreateFrameQuantizer <TPixel>(
                            image.GetConfiguration(),
                            frameMetaData.ColorTableLength).QuantizeFrame(frame);
                    }
                    else
                    {
                        quantized = this.quantizer.CreateFrameQuantizer <TPixel>(image.GetConfiguration())
                                    .QuantizeFrame(frame);
                    }
                }

                this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(quantized.Palette.Length).Clamp(1, 8);
                this.WriteGraphicalControlExtension(frameMetaData, this.GetTransparentIndex(quantized), stream);
                this.WriteImageDescriptor(frame, true, stream);
                this.WriteColorTable(quantized, stream);
                this.WriteImageData(quantized, stream);

                quantized?.Dispose();
                quantized     = null; // So next frame can regenerate it
                previousFrame = frame;
                previousMeta  = frameMetaData;
            }
        }