Exemplo n.º 1
0
        // AddFrame

        public void AddFrame(SKPixmap frame, SKGifEncoderFrameInfo frameInfo = default)
        {
            using var bmp = new SKBitmap();
            bmp.InstallPixels(frame);

            AddFrame(bmp, frameInfo);
        }
Exemplo n.º 2
0
        // Write*

        private void WriteGraphicControlExtension(SKQuantizedFrame quantizedFrame, SKGifEncoderFrameInfo frameInfo)
        {
            stream.WriteByte(0x21);          // extension introducer
            stream.WriteByte(0xf9);          // GCE label
            stream.WriteByte(4);             // data block size

            int transp, disp;

            if (frameInfo.TransparentColor != SKColor.Empty)
            {
                transp = 1;
                disp   = 2;
            }
            else
            {
                transp = 0;
                disp   = 0;
            }
            if (frameInfo.DisposalMethod >= 0)
            {
                disp = ((int)frameInfo.DisposalMethod) & 7;                 // user override
            }
            disp <<= 2;

            // packed fields
            stream.WriteByte(Convert.ToByte(
                                 0 |             // 1:3 reserved
                                 disp |          // 4:6 disposal
                                 0 |             // 7   user input - 0 = none
                                 transp));       // 8   transparency flag

            WriteShort(frameInfo.Duration / 10); // delay x 1/100 sec

            // transparent color index
            if (frameInfo.TransparentColor != SKColor.Empty)
            {
                stream.WriteByte(quantizer.GetColorIndex(frameInfo.TransparentColor));
            }
            else
            {
                stream.WriteByte(0);
            }

            stream.WriteByte(0);             // block terminator
        }
Exemplo n.º 3
0
        public void AddFrame(SKImage frame, SKGifEncoderFrameInfo frameInfo = default)
        {
            // make sure we have the pixels
            var raster = frame.ToRasterImage(true);

            try
            {
                using var pixmap = raster.PeekPixels();

                AddFrame(pixmap, frameInfo);
            }
            finally
            {
                if (raster != frame)
                {
                    raster.Dispose();
                }
            }
        }
Exemplo n.º 4
0
        public void AddFrame(SKBitmap frame, SKGifEncoderFrameInfo frameInfo = default)
        {
            if (stream == null)
            {
                throw new InvalidOperationException("File is already completed.");
            }

            if (frame == null)
            {
                throw new ArgumentNullException(nameof(frame));
            }

            if (!firstFrame && frame.Info.Size != frameSize)
            {
                throw new InvalidOperationException("Image size does not match the first frame.");
            }

            var quantizedFrame = quantizer.Quantize(frame.Pixels);

            if (firstFrame)
            {
                WriteString("GIF89a");                 // header

                frameSize = frame.Info.Size;

                WriteLogicalScreenDescriptor(quantizedFrame);
                WritePalette(quantizedFrame);
                WriteNetscapeExtension();
            }

            WriteGraphicControlExtension(quantizedFrame, frameInfo);
            WriteImageDescriptor(quantizedFrame);
            if (!firstFrame)
            {
                WritePalette(quantizedFrame);
            }
            WritePixels(quantizedFrame);

            firstFrame = false;
        }