示例#1
0
 private static bool IsFullFrame(FrameMetadata metadata, Int32Size fullSize)
 {
     return(metadata.Left == 0 &&
            metadata.Top == 0 &&
            metadata.Width == fullSize.Width &&
            metadata.Height == fullSize.Height);
 }
示例#2
0
        private static BitmapSource MakeFrame(Int32Size fullSize, BitmapSource rawFrame, FrameMetadata metadata, BitmapSource baseFrame)
        {
            if ((baseFrame == null) && IsFullFrame(metadata, fullSize))
            {
                return(rawFrame);
            }
            DrawingVisual visual = new DrawingVisual();

            using (DrawingContext context = visual.RenderOpen())
            {
                if (baseFrame != null)
                {
                    Rect rect = new Rect(0.0, 0.0, (double)fullSize.Width, (double)fullSize.Height);
                    context.DrawImage(baseFrame, rect);
                }
                Rect rectangle = new Rect((double)metadata.Left, (double)metadata.Top, (double)metadata.Width, (double)metadata.Height);
                context.DrawImage(rawFrame, rectangle);
            }
            RenderTargetBitmap bitmap = new RenderTargetBitmap(fullSize.Width, fullSize.Height, 96.0, 96.0, PixelFormats.Pbgra32);

            bitmap.Render(visual);
            if (bitmap.CanFreeze && !bitmap.IsFrozen)
            {
                bitmap.Freeze();
            }
            return(bitmap);
        }
示例#3
0
 private static bool IsFullFrame(FrameInfo info, Int32Size fullSize)
 {
     return(info.Left.Equals(0) &&
            info.Top.Equals(0) &&
            info.Width.Equals(fullSize.Width) &&
            info.Height.Equals(fullSize.Height));
 }
示例#4
0
        private static BitmapSource MakeFrame(Int32Size fullSize, BitmapSource rawFrame, FrameInfo frameInfo, BitmapSource previousFrame)
        {
            //if (previousFrame == null && IsFullFrame(frameInfo, fullSize))
            //{
            //    return rawFrame;
            //}

            var visual = new DrawingVisual();

            using (var context = visual.RenderOpen())
            {
                if (previousFrame != null)
                {
                    var fullRect = new Rect(0, 0, fullSize.Width, fullSize.Height);
                    context.DrawImage(previousFrame, fullRect);
                }

                context.DrawImage(rawFrame, frameInfo.Rect);
            }
            var bitmap = new RenderTargetBitmap(
                fullSize.Width, fullSize.Height,
                96, 96,
                PixelFormats.Pbgra32);

            bitmap.Render(visual);

            if (bitmap.CanFreeze && !bitmap.IsFrozen)
            {
                bitmap.Freeze();
            }

            return(bitmap);
        }
示例#5
0
            public bool Equals(Int32Size obj)
            {
                if (!obj.Width.Equals(this.Width))
                {
                    return(false);
                }
                if (!obj.Height.Equals(this.Height))
                {
                    return(false);
                }

                return(true);
            }
示例#6
0
        private static ObjectAnimationUsingKeyFrames GetAnimation(Image imageControl, BitmapSource source)
        {
            ObjectAnimationUsingKeyFrames animation = AnimationCache.GetAnimation(source, GetRepeatBehavior(imageControl));

            if (animation == null)
            {
                GifFile          file;
                GifBitmapDecoder decoder = GetDecoder(source, out file) as GifBitmapDecoder;
                if ((decoder == null) || (decoder.Frames.Count <= 1))
                {
                    return(null);
                }
                Int32Size fullSize   = GetFullSize(decoder, file);
                int       frameIndex = 0;
                animation = new ObjectAnimationUsingKeyFrames();
                TimeSpan     zero      = TimeSpan.Zero;
                BitmapSource baseFrame = null;
                foreach (BitmapFrame frame in decoder.Frames)
                {
                    FrameMetadata          metadata = GetFrameMetadata(decoder, file, frameIndex);
                    BitmapSource           source3  = MakeFrame(fullSize, frame, metadata, baseFrame);
                    DiscreteObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(source3, zero);
                    animation.KeyFrames.Add(keyFrame);
                    zero += metadata.Delay;
                    FrameDisposalMethod disposalMethod = metadata.DisposalMethod;
                    switch (disposalMethod)
                    {
                    case FrameDisposalMethod.None:
                    case FrameDisposalMethod.DoNotDispose:
                        baseFrame = source3;
                        break;

                    case FrameDisposalMethod.RestoreBackground:
                        baseFrame = !IsFullFrame(metadata, fullSize) ? ClearArea(source3, metadata) : null;
                        break;

                    default:
                        break;
                    }
                    frameIndex++;
                }
                animation.Duration       = zero;
                animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, file);
                AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation);
                AnimationCache.IncrementReferenceCount(source, GetRepeatBehavior(imageControl));
            }
            return(animation);
        }
示例#7
0
        private static BitmapSource MakeFrame(
            Int32Size fullSize,
            BitmapSource rawFrame, FrameMetadata metadata,
            BitmapSource baseFrame)
        {
            if (baseFrame == null && IsFullFrame(metadata, fullSize))
            {
                // No previous image to combine with, and same size as the full image
                // Just return the frame as is
                return(rawFrame);
            }

            DrawingVisual visual = new DrawingVisual();

            using (var context = visual.RenderOpen())
            {
                if (baseFrame != null)
                {
                    var fullRect = new Rect(0, 0, fullSize.Width, fullSize.Height);
                    context.DrawImage(baseFrame, fullRect);
                }

                var rect = new Rect(metadata.Left, metadata.Top, metadata.Width, metadata.Height);
                context.DrawImage(rawFrame, rect);
            }
            var bitmap = new RenderTargetBitmap(
                fullSize.Width, fullSize.Height,
                96, 96,
                PixelFormats.Pbgra32);

            bitmap.Render(visual);

            var result = new WriteableBitmap(bitmap);

            bitmap = null;

            if (result.CanFreeze && !result.IsFrozen)
            {
                result.Freeze();
            }
            return(result);
        }
示例#8
0
        private Int32Rect calcolaRettangolo(Crop cropCorrezione, int imgWidth, int imgHeight)
        {
            // Rettangolo di crop
            Int32Rect croppingRect = new Int32Rect();

            croppingRect.X      = cropCorrezione.x;
            croppingRect.Y      = cropCorrezione.y;
            croppingRect.Width  = cropCorrezione.w;
            croppingRect.Height = cropCorrezione.h;

            // Immagine sorgente piccola
            Int32Size rectImgSorg = new Int32Size();

            rectImgSorg.Width  = cropCorrezione.imgWidth;
            rectImgSorg.Height = cropCorrezione.imgHeight;

            // Immagine di destinazione grande
            Int32Size rectImgDest = new Int32Size();

            rectImgDest.Width  = imgWidth;
            rectImgDest.Height = imgHeight;

            return(Geometrie.proporziona(croppingRect, rectImgSorg, rectImgDest));
        }
示例#9
0
        private static CompressedBitmapSource MakeFrame(
            Int32Size fullSize,
            CompressedBitmapSource rawFrame, FrameMetadata metadata,
            CompressedBitmapSource baseFrame)
        {
            if (baseFrame == null && IsFullFrame(metadata, fullSize))
            {
                // No previous image to combine with, and same size as the full image
                // Just return the frame as is
                return(rawFrame);
            }

            WriteableBitmap bitmap;

            if (baseFrame == null)
            {
                bitmap = new WriteableBitmap(fullSize.Width, fullSize.Height, 96, 96, PixelFormats.Pbgra32, null);
            }
            else
            {
                bitmap = new WriteableBitmap(baseFrame);
            }

            var rawBitmap = new WriteableBitmap(rawFrame);

            bitmap.Blit(new Rect(metadata.Left, metadata.Top, metadata.Width, metadata.Height), rawBitmap, new Rect(0, 0, rawFrame.Width, rawFrame.Height));

            var result = new CompressedBitmapSource((BitmapSource)bitmap);

            bitmap    = null;
            rawBitmap = null;

            GC.Collect();
            Dispatcher.CurrentDispatcher.Invoke(() => {}, DispatcherPriority.ContextIdle);
            return(result);
        }
示例#10
0
        public unsafe ColorMatrix Resize(Int32Size size, Interpolations interpolation)
        {
            var result = new ColorMatrix(size.Height.UInt32(), size.Width.UInt32());

            var xs = Columns.Single() / size.Width.Single();
            var ys = Rows.Single() / size.Height.Single();

            float fracx, fracy, ifracx, ifracy, sx, sy, l0, l1, rf, gf, bf;
            int   c, x0, x1, y0, y1;
            byte  c1a, c1r, c1g, c1b, c2a, c2r, c2g, c2b, c3a, c3r, c3g, c3b, c4a, c4r, c4g, c4b;
            byte  a, r, g, b;

            int[] pixels = new int[Columns * Rows];

            var total = 0;

            for (uint x = 0; x < Columns; x++)
            {
                for (uint y = 0; y < Rows; y++)
                {
                    pixels[total++] = Convert(GetValue(y, x));
                }
            }

            int widthSource = Columns.Int32(), heightSource = Rows.Int32();

            //Nearest Neighbor
            if (interpolation == Interpolations.NearestNeighbor)
            {
                for (var y = 0; y < size.Height; y++)
                {
                    for (var x = 0; x < size.Width; x++)
                    {
                        sx = x * xs;
                        sy = y * ys;
                        x0 = (int)sx;
                        y0 = (int)sy;

                        var i  = y0 * widthSource + x0;
                        var cc = Convert(pixels[i]);
                        result.SetValue(y.UInt32(), x.UInt32(), cc);
                    }
                }
            }

            //Bilinear
            else if (interpolation == Interpolations.Bilinear)
            {
                for (var y = 0; y < size.Height; y++)
                {
                    for (var x = 0; x < size.Width; x++)
                    {
                        sx = x * xs;
                        sy = y * ys;
                        x0 = (int)sx;
                        y0 = (int)sy;

                        // Calculate coordinates of the 4 interpolation points
                        fracx  = sx - x0;
                        fracy  = sy - y0;
                        ifracx = 1f - fracx;
                        ifracy = 1f - fracy;
                        x1     = x0 + 1;
                        if (x1 >= widthSource)
                        {
                            x1 = x0;
                        }
                        y1 = y0 + 1;
                        if (y1 >= heightSource)
                        {
                            y1 = y0;
                        }


                        // Read source color
                        c   = pixels[y0 * widthSource + x0];
                        c1a = (byte)(c >> 24);
                        c1r = (byte)(c >> 16);
                        c1g = (byte)(c >> 8);
                        c1b = (byte)(c);

                        c   = pixels[y0 * widthSource + x1];
                        c2a = (byte)(c >> 24);
                        c2r = (byte)(c >> 16);
                        c2g = (byte)(c >> 8);
                        c2b = (byte)(c);

                        c   = pixels[y1 * widthSource + x0];
                        c3a = (byte)(c >> 24);
                        c3r = (byte)(c >> 16);
                        c3g = (byte)(c >> 8);
                        c3b = (byte)(c);

                        c   = pixels[y1 * widthSource + x1];
                        c4a = (byte)(c >> 24);
                        c4r = (byte)(c >> 16);
                        c4g = (byte)(c >> 8);
                        c4b = (byte)(c);


                        // Calculate colors
                        // Alpha
                        l0 = ifracx * c1a + fracx * c2a;
                        l1 = ifracx * c3a + fracx * c4a;
                        a  = (byte)(ifracy * l0 + fracy * l1);

                        // Red
                        l0 = ifracx * c1r + fracx * c2r;
                        l1 = ifracx * c3r + fracx * c4r;
                        rf = ifracy * l0 + fracy * l1;

                        // Green
                        l0 = ifracx * c1g + fracx * c2g;
                        l1 = ifracx * c3g + fracx * c4g;
                        gf = ifracy * l0 + fracy * l1;

                        // Blue
                        l0 = ifracx * c1b + fracx * c2b;
                        l1 = ifracx * c3b + fracx * c4b;
                        bf = ifracy * l0 + fracy * l1;

                        // Cast to byte
                        r = (byte)rf;
                        g = (byte)gf;
                        b = (byte)bf;

                        result.SetValue(y.UInt32(), x.UInt32(), Color.FromArgb(a, r, g, b));
                    }
                }
            }
            return(result);
        }
示例#11
0
 public override Matrix <Color> Render(Int32Size size) => default; //CircleEllipseDiamond(Shapes.Diamond, size);
示例#12
0
 public bool Equals(Int32Size obj)
 {
     return((Width == obj.Width) && (Height == obj.Height));
 }
示例#13
0
 private static bool IsFullFrame(FrameMetadata metadata, Int32Size fullSize) =>
 ((metadata.Left == 0) && ((metadata.Top == 0) && ((metadata.Width == fullSize.Width) && (metadata.Height == fullSize.Height))));
示例#14
0
 public override Matrix <Color> Render(Int32Size size) => default;
示例#15
0
 public override void Resize(Int32Size oldSize, Int32Size newSize, Interpolations interpolation)
 {
 }
示例#16
0
        private static ObjectAnimationUsingKeyFrames?GetAnimation(Image imageControl, BitmapSource source)
        {
            ObjectAnimationUsingKeyFrames?animation = AnimationCache.GetAnimation(source,
                                                                                  GetRepeatBehavior(imageControl));

            if (animation != null)
            {
                return(animation);
            }

            GifFile?gifMetadata;
            var     decoder = GetDecoder(source, out gifMetadata) as GifBitmapDecoder;

            if (decoder != null && decoder.Frames.Count > 1)
            {
                Int32Size fullSize = GetFullSize(decoder, gifMetadata);
                int       index    = 0;
                animation = new ObjectAnimationUsingKeyFrames();
                TimeSpan     totalDuration = TimeSpan.Zero;
                BitmapSource?baseFrame     = null;
                foreach (BitmapFrame rawFrame in decoder.Frames)
                {
                    FrameMetadata metadata = GetFrameMetadata(decoder, gifMetadata, index);

                    BitmapSource frame    = MakeFrame(fullSize, rawFrame, metadata, baseFrame);
                    var          keyFrame = new DiscreteObjectKeyFrame(frame, totalDuration);
                    animation.KeyFrames.Add(keyFrame);

                    totalDuration += metadata.Delay;

                    switch (metadata.DisposalMethod)
                    {
                    case FrameDisposalMethod.None:
                    case FrameDisposalMethod.DoNotDispose:
                        baseFrame = frame;
                        break;

                    case FrameDisposalMethod.RestoreBackground:
                        if (IsFullFrame(metadata, fullSize))
                        {
                            baseFrame = null;
                        }
                        else
                        {
                            baseFrame = ClearArea(frame, metadata);
                        }
                        break;

                    case FrameDisposalMethod.RestorePrevious:
                        // Reuse same base frame
                        break;
                    }

                    index++;
                }
                animation.Duration       = totalDuration;
                animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, gifMetadata);

                AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation);
                return(animation);
            }
            return(null);
        }
示例#17
0
			public bool Equals(Int32Size obj)
			{
				return (Width == obj.Width) && (Height == obj.Height);
			}