public ThirdPartyEmote(List <SKBitmap> Emote_frames, SKCodec Codec, string Name, string ImageType, string Id, int ImageScale)
    {
        emote_frames = Emote_frames;
        codec        = Codec;
        name         = Name;
        imageType    = ImageType;
        id           = Id;
        width        = Emote_frames.First().Width;
        height       = Emote_frames.First().Height;
        imageScale   = ImageScale;

        if (imageType == "gif")
        {
            emote_frames.Clear();
            for (int i = 0; i < Codec.FrameCount; i++)
            {
                SKImageInfo    imageInfo    = new SKImageInfo(codec.Info.Width, codec.Info.Height);
                SKBitmap       newBitmap    = new SKBitmap(imageInfo);
                IntPtr         pointer      = newBitmap.GetPixels();
                SKCodecOptions codecOptions = new SKCodecOptions(i);
                codec.GetPixels(imageInfo, pointer, codecOptions);
                emote_frames.Add(newBitmap);
            }
        }
    }
Beispiel #2
0
        private void Animation(SKCanvas canvas, Animation img)
        {
            if (img.BinaryImage != null && img.BinaryImage.Length > 0)
            {
                using (SKStream stream = new SKMemoryStream(img.BinaryImage))
                    using (SKCodec codec = SKCodec.Create(stream))
                    {
                        int frameCount = codec.FrameCount;

                        int         frame     = 0;
                        SKImageInfo imageInfo = new SKImageInfo(
                            codec.Info.Width,
                            codec.Info.Height,
                            codec.Info.ColorType,
                            codec.Info.AlphaType,
                            codec.Info.ColorSpace);

                        var    bmp     = new SKBitmap(imageInfo);
                        IntPtr pointer = bmp.GetPixels();

                        SKCodecOptions codecOptions = new SKCodecOptions(frame);

                        codec.GetPixels(imageInfo, pointer, codecOptions);

                        canvas.DrawBitmap(bmp, (float)img.Position.X, (float)img.Position.Y);
                    }
            }
            else
            {
                throw new Exception("Bitmap not provided");
            }
        }
Beispiel #3
0
        public void Load(Stream stream)
        {
            IsReady = false;

            using SKManagedStream sKManagedStream = new SKManagedStream(stream);
            using SKCodec sKCodec = SKCodec.Create(sKManagedStream);

            int frameCount = sKCodec.FrameCount;

            _bitmaps              = new SKBitmap[frameCount];
            _durations            = new int[frameCount];
            _accumulatedDurations = new int[frameCount];

            for (int frame = 0; frame < frameCount; frame++)
            {
                //get time line
                _durations[frame]            = sKCodec.FrameInfo[frame].Duration;
                _totalDuration              += _durations[frame];
                _accumulatedDurations[frame] = _durations[frame] + (frame == 0 ? 0 : _accumulatedDurations[frame - 1]);

                //get image
                SKImageInfo sKImageInfo = new SKImageInfo(sKCodec.Info.Width, sKCodec.Info.Height);
                _bitmaps[frame] = new SKBitmap(sKImageInfo);

                IntPtr pointer = _bitmaps[frame].GetPixels();

                sKCodec.GetPixels(sKImageInfo, pointer, new SKCodecOptions(frame));
            }

            IsReady = true;

            GlobalSettings.Logger.LogDebug("SkGif已经加载完毕");
        }
Beispiel #4
0
        public T Decode(Stream stream, ImageCodecSettings settings = null)
        {
            using (SKData data = SKData.Create(stream))
            {
                SKCodec     codec   = SKCodec.Create(data);
                var         format  = codec.EncodedFormat;
                var         info    = codec.Info;
                SKImageInfo decInfo = new SKImageInfo
                {
                    ColorType = info.ColorType,
                    Height    = info.Height,
                    Width     = info.Width
                };
                byte[]   buffer = new byte[info.Width * info.Height * 4];
                GCHandle hData  = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                var      result = codec.GetPixels(decInfo, hData.AddrOfPinnedObject());
                if (hData.IsAllocated)
                {
                    hData.Free();
                }

                IPixelMap pixMap = new PixelMap();
                return((T)pixMap);
            }
        }
Beispiel #5
0
        private void LoadGifData()
        {
            if (!File.Exists(Properties.FileName.BaseValue))
            {
                return;
            }

            lock (myLock)
            {
                using SKCodec codec = SKCodec.Create(Properties.FileName.BaseValue);

                SKImageInfo info = new SKImageInfo(codec.Info.Width, codec.Info.Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);

                frameCount   = codec.FrameCount;
                currentFrame = 0;
                originals    = new SKBitmap[frameCount];
                durations    = new int[frameCount];

                for (int i = 0; i < frameCount; i++)
                {
                    durations[i] = codec.FrameInfo[i].Duration;
                    originals[i] = new SKBitmap(new SKImageInfo(codec.Info.Width, codec.Info.Height));
                    codec.GetPixels(info, originals[i].GetPixels(), new SKCodecOptions(i));
                }

                bitmaps = new SKBitmap[frameCount];
                for (int i = 0; i < frameCount; i++)
                {
                    bitmaps[i] = originals[i].Copy();
                }
            }
        }
        public TwitchEmote(List <SKBitmap> Emote_frames, SKCodec Codec, string Name, string ImageType, string Id, int ImageScale, byte[] ImageData)
        {
            emote_frames = Emote_frames;
            codec        = Codec;
            name         = Name;
            id           = Id;
            width        = Emote_frames.First().Width;
            height       = Emote_frames.First().Height;
            imageScale   = ImageScale;
            imageData    = ImageData;

            // If we are webp, with zero frame count then we are a static image
            // Thus we should just treat it as a differnt imageType so we don't animate it
            imageType = ImageType;
            if (imageType == "webp" && Codec.FrameCount == 0)
            {
                imageType = "webp_static";
            }

            // Split animated image into a list of images
            if (imageType == "gif" || imageType == "webp")
            {
                emote_frames.Clear();
                for (int i = 0; i < Codec.FrameCount; i++)
                {
                    SKImageInfo    imageInfo    = new SKImageInfo(codec.Info.Width, codec.Info.Height);
                    SKBitmap       newBitmap    = new SKBitmap(imageInfo);
                    IntPtr         pointer      = newBitmap.GetPixels();
                    SKCodecOptions codecOptions = new SKCodecOptions(i);
                    codec.GetPixels(imageInfo, pointer, codecOptions);
                    emote_frames.Add(newBitmap);
                }
            }
        }
    public SKBitmap GetFrame(int frameNum)
    {
        SKImageInfo    imageInfo    = new SKImageInfo(codec.Info.Width, codec.Info.Height);
        SKBitmap       newBitmap    = new SKBitmap(imageInfo);
        IntPtr         pointer      = newBitmap.GetPixels();
        SKCodecOptions codecOptions = new SKCodecOptions(frameNum);

        codec.GetPixels(imageInfo, pointer, codecOptions);
        return(newBitmap);
    }
        protected override void OnDrawSample(SKCanvas canvas, int width, int height)
        {
            canvas.Clear(SKColors.Black);

            var opts = new SKCodecOptions(currentFrame, false);

            if (codec?.GetPixels(info, bitmap.GetPixels(), opts) == SKCodecResult.Success)
            {
                canvas.DrawBitmap(bitmap, 0, 0);
            }
        }
        public AnimatedGifPage()
        {
            InitializeComponent();

            string   resourceID = "SkiaSharpFormsDemos.Media.Newtons_cradle_animation_book_2.gif";
            Assembly assembly   = GetType().GetTypeInfo().Assembly;

            using (Stream stream = assembly.GetManifestResourceStream(resourceID))
                using (SKManagedStream skStream = new SKManagedStream(stream))
                    using (SKCodec codec = SKCodec.Create(skStream))
                    {
                        // Get frame count and allocate bitmaps
                        int frameCount = codec.FrameCount;
                        bitmaps              = new SKBitmap[frameCount];
                        durations            = new int[frameCount];
                        accumulatedDurations = new int[frameCount];

                        // Note: There's also a RepetitionCount property of SKCodec not used here

                        // Loop through the frames
                        for (int frame = 0; frame < frameCount; frame++)
                        {
                            // From the FrameInfo collection, get the duration of each frame
                            durations[frame] = codec.FrameInfo[frame].Duration;

                            // Create a full-color bitmap for each frame
                            SKImageInfo imageInfo = new SKImageInfo(codec.Info.Width, codec.Info.Height);
                            bitmaps[frame] = new SKBitmap(imageInfo);

                            // Get the address of the pixels in that bitmap
                            IntPtr pointer = bitmaps[frame].GetPixels();

                            // Create an SKCodecOptions value to specify the frame
                            SKCodecOptions codecOptions = new SKCodecOptions(frame, false);

                            // Copy pixels from the frame into the bitmap
                            codec.GetPixels(imageInfo, pointer, codecOptions);
                        }

                        // Sum up the total duration
                        for (int frame = 0; frame < durations.Length; frame++)
                        {
                            totalDuration += durations[frame];
                        }

                        // Calculate the accumulated durations
                        for (int frame = 0; frame < durations.Length; frame++)
                        {
                            accumulatedDurations[frame] = durations[frame] +
                                                          (frame == 0 ? 0 : accumulatedDurations[frame - 1]);
                        }
                    }
        }
Beispiel #10
0
        static bool DecodeImageFrame(SKCodec codec, out Frame decoded, int index = -1)
        {
            decoded = null;
            SKBitmap frame = new SKBitmap(codec.Info);

            if ((
                    (index == -1)
                    ? codec.GetPixels(codec.Info, frame.GetPixels())
                    : codec.GetPixels(codec.Info, frame.GetPixels(), new SKCodecOptions(index))
                    ) == SKCodecResult.Success)
            {
                frame = frame.Resize(targetInfo, SKFilterQuality.High);

                decoded = new Frame(new Time(
                                        false,
                                        free: (index == -1)
                        ? 1000
                        : codec.FrameInfo[index].Duration
                                        ));

                for (int x = 0; x <= 9; x++)
                {
                    for (int y = 0; y <= 9; y++)
                    {
                        SKColor color = frame.GetPixel(x, 9 - y);
                        decoded.Screen[y * 10 + x] = new Color(
                            (byte)(color.Red >> 2),
                            (byte)(color.Green >> 2),
                            (byte)(color.Blue >> 2)
                            );
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #11
0
        public static bool FramesFromImage(string path, out List <Frame> ret)
        {
            ret = null;

            if (!File.Exists(path))
            {
                return(false);
            }

            using (SKCodec codec = SKCodec.Create(path)){
                if (codec == null)
                {
                    return(false);
                }

                SKImageInfo info = codec.Info;
                ret = new List <Frame>();

                for (int i = 0; i < codec.FrameCount; i++)
                {
                    SKBitmap frame = new SKBitmap(info);

                    if (codec.GetPixels(info, frame.GetPixels(), new SKCodecOptions(i)) == SKCodecResult.Success)
                    {
                        frame = frame.Resize(targetInfo, SKFilterQuality.High);

                        ret.Add(new Frame(new Time(false, null, codec.FrameInfo[i].Duration)));

                        for (int x = 0; x <= 9; x++)
                        {
                            for (int y = 0; y <= 9; y++)
                            {
                                SKColor color = frame.GetPixel(x, 9 - y);
                                ret[i].Screen[y * 10 + x] = new Color(
                                    (byte)(color.Red >> 2),
                                    (byte)(color.Green >> 2),
                                    (byte)(color.Blue >> 2)
                                    );
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }

                return(true);
            }
        }
Beispiel #12
0
        public static BitmapDrawable From(SKCodec codec, SKImageInfo bitmapInfo)
        {
            if (codec == null)
            {
                throw new ArgumentNullException(nameof(codec));
            }

            var bitmap = new BitmapDrawable(bitmapInfo);
            var result = codec.GetPixels(bitmapInfo, bitmap.GetPixels(out var length));

            if (result != SKCodecResult.Success && result != SKCodecResult.IncompleteInput)
            {
                bitmap.Dispose();
                bitmap = null;
            }
            return(bitmap);
        }
Beispiel #13
0
        private SKBitmap LoadBitmap(Stream stream, out SKEncodedOrigin origin)
        {
            using SKManagedStream s = new SKManagedStream(stream);
            using SKCodec codec     = SKCodec.Create(s);
            origin = codec.EncodedOrigin;
            SKImageInfo info   = codec.Info;
            SKBitmap    bitmap = new SKBitmap(info.Width, info.Height, SKImageInfo.PlatformColorType, info.IsOpaque ? SKAlphaType.Opaque : SKAlphaType.Premul);

            SKCodecResult result = codec.GetPixels(bitmap.Info, bitmap.GetPixels(out IntPtr length));

            if (result == SKCodecResult.Success || result == SKCodecResult.IncompleteInput)
            {
                return(bitmap);
            }
            else
            {
                throw new ArgumentException("Unable to load bitmap from provided data");
            }
        }