public void BezierPositionMapper_GetPointOnBezierCurve()
        {
            // Arrange
            uint  amount = 60;
            float x1     = 0.25f;
            float y1     = 0.25f;
            float x2     = 0.75f;
            float y2     = 0.75f;

            // Act
            var mapper = new BezierPositionMapper(amount, x1, y1, x2, y2);

            // Assert
            Assert.AreEqual(new Tuple <float, float>(0, 0), mapper.GetPointOnBezierCurve(0));
            Assert.AreEqual(new Tuple <float, float>(1, 1), mapper.GetPointOnBezierCurve(1));
            Assert.AreEqual(new Tuple <float, float>(0.5f, 0.5f), mapper.GetPointOnBezierCurve(0.5f));

            float duration = 0;

            for (uint i = 0; i < amount; i++)
            {
                duration += mapper.GetMappedPosition(i);
            }
            Assert.AreEqual(3000, Convert.ToInt32(duration));
        }
        /// <inheritdoc/>
        public async Task <string> WriteAsync(
            string destinationPath,
            IReadOnlyList <PositionAxisContainerModel <string> > images,
            string name,
            int delay,
            BezierEasingType bezierEasingType,
            CancellationToken ct)
        {
            try
            {
                var mapper = new BezierPositionMapper(images.Count(), bezierEasingType);

                var filenames = images.OrderBy(e => e.AxisType).ThenBy(e => e.Position)
                                .Select(e => e.Entity).ToArray();

                var filename = $"{name}{imageExtension}";

                await AbstractWriteAsync(destinationPath, filename, destinationPath, filenames, delay, bezierEasingType, mapper, ct);

                return(filename);
            }
            catch (OperationCanceledException e)
            {
                throw new AmiException("The writing of the GIF has been cancelled.", e);
            }
            catch (Exception e)
            {
                throw new AmiException("The GIF could not be written.", e);
            }
        }
 /// <summary>
 /// Writes the GIF images asynchronous.
 /// </summary>
 /// <param name="destinationPath">The destination path.</param>
 /// <param name="destinationFilename">The destination filename.</param>
 /// <param name="sourcePath">The source path.</param>
 /// <param name="sourceFilenames">The source filenames.</param>
 /// <param name="mapper">The position mapper.</param>
 /// <param name="ct">The cancellation token.</param>
 /// <returns>
 /// A <see cref="Task" /> representing the asynchronous operation.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// destinationPath
 /// or
 /// destinationFilename
 /// or
 /// sourcePath
 /// or
 /// sourceFilenames
 /// or
 /// mapper
 /// or
 /// ct
 /// </exception>
 protected abstract Task AbstractWriteAsync(
     string destinationPath,
     string destinationFilename,
     string sourcePath,
     string[] sourceFilenames,
     BezierPositionMapper mapper,
     CancellationToken ct);
Пример #4
0
        /// <inheritdoc/>
        protected override async Task AbstractWriteAsync(
            string destinationPath,
            string destinationFilename,
            string sourcePath,
            string[] sourceFilenames,
            BezierPositionMapper mapper,
            CancellationToken ct)
        {
            if (string.IsNullOrWhiteSpace(destinationPath))
            {
                throw new ArgumentNullException(nameof(destinationPath));
            }

            if (string.IsNullOrWhiteSpace(destinationFilename))
            {
                throw new ArgumentNullException(nameof(destinationFilename));
            }

            if (string.IsNullOrWhiteSpace(sourcePath))
            {
                throw new ArgumentNullException(nameof(sourcePath));
            }

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

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

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

            await Task.Run(
                () =>
            {
                // 33ms delay (~30fps)
                using (var gif = AnimatedGif.AnimatedGif.Create(Path.Combine(destinationPath, destinationFilename), 33))
                {
                    for (uint i = 0; i < sourceFilenames.Length; i++)
                    {
                        ct.ThrowIfCancellationRequested();

                        using (Image image = Image.FromFile(Path.Combine(sourcePath, sourceFilenames[i])))
                        {
                            int delay = Convert.ToInt32(mapper.GetMappedPosition(i));
                            gif.AddFrame(image, delay, quality: GifQuality.Bit8);
                        }
                    }
                }
            }, ct);
        }
        public void BezierPositionMapper_GetPointOnBezierCurve_BezierEasingType(BezierEasingType bezierEasingType)
        {
            // Arrange
            uint amount = 60;

            // Act
            var mapper = new BezierPositionMapper(amount, bezierEasingType);

            // Assert
            Assert.AreEqual(new Tuple <float, float>(0, 0), mapper.GetPointOnBezierCurve(0));
            Assert.AreEqual(new Tuple <float, float>(1, 1), mapper.GetPointOnBezierCurve(1));

            float duration = 0;

            for (uint i = 0; i < amount; i++)
            {
                duration += mapper.GetMappedPosition(i);
            }
            Assert.IsTrue(duration > 2800f);
            Assert.IsTrue(duration < 3200f);
        }
        public void BezierPositionMapper_GetPointOnBezierCurve_BezierEasingType()
        {
            // Arrange
            uint             amount           = 60;
            BezierEasingType bezierEasingType = BezierEasingType.Linear;

            // Act
            var mapper = new BezierPositionMapper(amount, bezierEasingType);

            // Assert
            Assert.AreEqual(new Tuple <float, float>(0, 0), mapper.GetPointOnBezierCurve(0));
            Assert.AreEqual(new Tuple <float, float>(1, 1), mapper.GetPointOnBezierCurve(1));
            Assert.AreEqual(new Tuple <float, float>(0.5f, 0.5f), mapper.GetPointOnBezierCurve(0.5f));

            float duration = 0;

            for (uint i = 0; i < amount; i++)
            {
                duration += mapper.GetMappedPosition(i);
            }
            Assert.AreEqual(3000, Convert.ToInt32(duration));
        }