From ced3a3303495ca469a673152df3bc81fbf1e30c2 Mon Sep 17 00:00:00 2001
From: Tsanie Lily <tsorgy@gmail.com>
Date: Tue, 19 May 2020 15:41:31 +0800
Subject: [PATCH] fix: export video scans error

---
 Pixiview/Illust/ViewIllustPage.xaml.cs |  4 ++++
 Pixiview/Utils/Ugoira.cs               | 27 +++++++++++++++++++++++---
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/Pixiview/Illust/ViewIllustPage.xaml.cs b/Pixiview/Illust/ViewIllustPage.xaml.cs
index fee41f4..6ecbb0a 100644
--- a/Pixiview/Illust/ViewIllustPage.xaml.cs
+++ b/Pixiview/Illust/ViewIllustPage.xaml.cs
@@ -743,9 +743,13 @@ namespace Pixiview.Illust
                 var success = await Task.Run(ugoira.ExportVideo);   // ugoira.ExportGif
                 if (success != null)
                 {
+#if DEBUG
+                    msg = ResourceHelper.ExportSuccess;
+#else
                     var result = await FileStore.SaveVideoToGalleryAsync(success);
 
                     msg = result ?? ResourceHelper.ExportSuccess;
+#endif
                 }
             }
 
diff --git a/Pixiview/Utils/Ugoira.cs b/Pixiview/Utils/Ugoira.cs
index 38ebc55..05ee50e 100644
--- a/Pixiview/Utils/Ugoira.cs
+++ b/Pixiview/Utils/Ugoira.cs
@@ -443,6 +443,9 @@ namespace Pixiview.Utils
             {
                 videoWriter.StartSessionAtSourceTime(CMTime.Zero);
                 var lastTime = CMTime.Zero;
+#if DEBUG
+                bool log = false;
+#endif
                 for (int i = 0; i < images.Length; i++)
                 {
                     while (true)
@@ -465,6 +468,24 @@ namespace Pixiview.Utils
                                             var cgImage = images[i].CGImage;
                                             var width = cgImage.Width;
                                             var height = cgImage.Height;
+                                            var bitsPerComponent = cgImage.BitsPerComponent;
+                                            var bytesPerRow = cgImage.BytesPerRow;
+                                            var bytes = bytesPerRow >> 6 << 6;
+                                            if (bytes < bytesPerRow)
+                                            {
+                                                bytes += 64;
+                                            }
+#if DEBUG
+                                            if (!log)
+                                            {
+                                                log = true;
+                                                App.DebugPrint($"animation, width: {width}, height: {height}, type: {cgImage.UTType}\n" +
+                                                    $"bitmapInfo: {cgImage.BitmapInfo}\n" +
+                                                    $"bpc: {bitsPerComponent}\n" +
+                                                    $"bpp: {cgImage.BitsPerPixel}\n" +
+                                                    $"calculated: {bytesPerRow} => {bytes}");
+                                            }
+#endif
                                             //var scan = cgImage.BytesPerRow;
                                             //var mod = scan % 16;
                                             //if (mod > 0)
@@ -474,8 +495,8 @@ namespace Pixiview.Utils
                                             //}
                                             using (CGBitmapContext bitmapContext = new CGBitmapContext(
                                                 pxdata, width, height,
-                                                cgImage.BitsPerComponent,
-                                                4 * (width + width % 4),
+                                                bitsPerComponent,
+                                                bytes,
                                                 rgbColorSpace,
                                                 CGImageAlphaInfo.NoneSkipFirst))
                                             {
@@ -503,7 +524,7 @@ namespace Pixiview.Utils
                             lastTime = CMTime.Add(lastTime, frameTime);
                             break;
                         }
-                        Thread.Sleep(16);
+                        Thread.Sleep(32);
                     }
                 }
                 writerInput.MarkAsFinished();