Example #1
0
        public static async Task RunVisualTestScore(Score score, string referenceFileName,
                                                    Settings?settings     = null,
                                                    IList <double>?tracks = null, string?message = null)
        {
            if (settings == null)
            {
                settings = new Settings();
            }

            if (tracks == null)
            {
                tracks = new List <double> {
                    0
                };
            }

            settings.Core.Engine            = "skia";
            settings.Core.EnableLazyLoading = false;
            settings.Core.UseWorkers        = false;

            if (!referenceFileName.StartsWith("test-data/"))
            {
                referenceFileName = $"test-data/visual-tests/{referenceFileName}";
            }

            var referenceFileData =
                await TestPlatform.LoadFile(referenceFileName);

            var result      = new List <RenderFinishedEventArgs>();
            var totalWidth  = 0.0;
            var totalHeight = 0.0;

            var task     = new TaskCompletionSource <object>();
            var renderer = new ScoreRenderer(settings);

            renderer.Width = 1300;

            renderer.PartialRenderFinished.On(e =>
            {
                if (e != null)
                {
                    result.Add(e);
                }
            });
            renderer.RenderFinished.On(e =>
            {
                totalWidth  = e.TotalWidth;
                totalHeight = e.TotalHeight;
                result.Add(e);
                task.SetResult(null);
            });
            renderer.Error.On((e) => { task.SetException(e); });
            renderer.RenderScore(score, tracks);

            if (await Task.WhenAny(task.Task, Task.Delay(2000)) == task.Task)
            {
                await CompareVisualResult(
                    totalWidth,
                    totalHeight,
                    result,
                    referenceFileName,
                    referenceFileData,
                    message
                    );
            }
            else
            {
                Assert.Fail("Rendering did not complete within timeout");
            }
        }
Example #2
0
        private static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage AlphaTab.Samples.PngDump.exe Path");
                return;
            }

            // load score
            var score = ScoreLoader.LoadScoreFromBytes(File.ReadAllBytes(args[0]));

            // render score with svg engine and desired rendering width
            var settings = new Settings();

            settings.Core.Engine = "skia";
            var renderer = new ScoreRenderer(settings)
            {
                Width = 970
            };

            // iterate tracks
            for (var i = 0; i < score.Tracks.Count; i++)
            {
                var track = score.Tracks[i];

                // render track
                Console.WriteLine("Rendering track {0} - {1}", i + 1, track.Name);
                var images      = new List <SKImage>();
                var totalWidth  = 0;
                var totalHeight = 0;
                renderer.PartialRenderFinished.On(r => { images.Add((SKImage)r.RenderResult); });
                renderer.RenderFinished.On(r =>
                {
                    totalWidth  = (int)r.TotalWidth;
                    totalHeight = (int)r.TotalHeight;
                });
                renderer.RenderScore(score, new List <double> {
                    track.Index
                });

                // write png
                var info = new FileInfo(args[0]);
                var path = Path.Combine(info.DirectoryName,
                                        Path.GetFileNameWithoutExtension(info.Name) + "-" + i + ".png");

                using var full = SKSurface.Create(new SKImageInfo(totalWidth, totalHeight,
                                                                  SKImageInfo.PlatformColorType, SKAlphaType.Premul));

                var y = 0;
                foreach (var image in images)
                {
                    full.Canvas.DrawImage(image, new SKRect(0, 0, image.Width, image.Height),
                                          new SKRect(0, y, image.Width, y + image.Height));
                    y += image.Height;
                }

                using var fullImage = full.Snapshot();
                using var data      = fullImage.Encode(SKEncodedImageFormat.Png, 100)
                                      .AsStream(true);
                using var fileStream =
                          new FileStream(path, FileMode.Create, FileAccess.Write);
                data.CopyTo(fileStream);
            }
        }